-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_ssl_utils.cpp
More file actions
36 lines (33 loc) · 1002 Bytes
/
Copy pathdemo_ssl_utils.cpp
File metadata and controls
36 lines (33 loc) · 1002 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
#include "demo_ssl_utils.h"
SSL_CTX* newSSLContext(int methodPreference)
{ const SSL_METHOD *method;
SSL_CTX *ctx;
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
SSL_library_init();
switch(methodPreference){
case 1: method = TLSv1_method(); break;
case 2: method = TLSv1_1_method(); break;
case 3: method = TLSv1_2_method(); break;
default: method = SSLv23_method(); break;
}
ctx = SSL_CTX_new(method);
if ( ctx == NULL )
{
ERR_print_errors_fp(stderr);
exit(1);
}
return ctx;
}
void loadPrivateAndPublicKeys(SSL_CTX* ctx, char* cert, char* key)
{
if ( SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM) <= 0 || SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) <= 0 )
{
ERR_print_errors_fp(stderr);
exit(1);
}
if ( !SSL_CTX_check_private_key(ctx) ){
fprintf(stderr, "Private key does not match the public certificate\n");
exit(1);
}
}