-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtls_server_lib.cpp
More file actions
46 lines (36 loc) · 876 Bytes
/
tls_server_lib.cpp
File metadata and controls
46 lines (36 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "tls_common_lib.h"
#include "tls_server_lib.h"
// Creates a new SSL session, attaches the client file
// descriptor to it, initiates the connection and
// returns the SSL session.
// ctx = SSL context
// fd = File descriptor for client connection.
//
SSL *connectSSL(SSL_CTX *ctx, int fd, const char *common_name)
{
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, fd);
if(common_name != NULL)
{
setHostVerification(ssl, common_name);
}
if(SSL_accept(ssl) <= 0)
{
ERR_print_errors_fp(stderr);
SSL_free(ssl);
return NULL;
}
X509 *cert;
cert = SSL_get_peer_certificate(ssl);
if(cert == NULL) {
printf("Unable to get certificate\n");
SSL_free(ssl);
return NULL;
}
return ssl;
}