Skip to content

Commit 2f4bf70

Browse files
committed
Removing issue with SSL when no CRL is given on the server and removing deprecated OpenSSL methods.
1 parent 4402e22 commit 2f4bf70

File tree

6 files changed

+79
-91
lines changed

6 files changed

+79
-91
lines changed

source/authentication/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ elseif( TIRPC_FOUND )
2424
include_directories( ${TIRPC_INCLUDE_DIR} )
2525
endif()
2626

27-
add_library( authentication-client-objects OBJECT udaClientSSL.cpp udaClientSSL.h udaServerSSL.h )
28-
target_link_libraries( authentication-client-objects OpenSSL::SSL LibXml2::LibXml2 )
27+
add_library( authentication-client-objects OBJECT udaClientSSL.cpp udaClientSSL.h udaServerSSL.h utils.cpp )
28+
target_link_libraries( authentication-client-objects PRIVATE OpenSSL::SSL LibXml2::LibXml2 )
29+
target_compile_definitions( authentication-client-objects PRIVATE -DOPENSSL_NO_DEPRECATED )
2930

3031
if( NOT CLIENT_ONLY )
31-
add_library( authentication-server-objects OBJECT udaServerSSL.cpp udaClientSSL.h )
32-
target_compile_definitions( authentication-server-objects PRIVATE -DSERVERBUILD )
33-
target_link_libraries( authentication-server-objects OpenSSL::SSL LibXml2::LibXml2 )
32+
add_library( authentication-server-objects OBJECT udaServerSSL.cpp udaClientSSL.h utils.cpp )
33+
target_compile_definitions( authentication-server-objects PRIVATE -DSERVERBUILD -DOPENSSL_NO_DEPRECATED )
34+
target_link_libraries( authentication-server-objects PRIVATE OpenSSL::SSL LibXml2::LibXml2 )
3435
endif()

source/authentication/udaClientSSL.cpp

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <logging/logging.h>
1313
#include <client/udaClientHostList.h>
1414

15+
#include "utils.h"
16+
1517
static bool g_sslDisabled = true; // Default state is not SSL authentication
1618
static int g_sslProtocol = 0; // The default server host name has the SSL protocol name prefix or
1719
static int g_sslSocket = -1;
@@ -51,9 +53,7 @@ static void init_ssl_library()
5153
UDA_LOG(UDA_LOG_DEBUG, "Prior SSL initialisation\n");
5254
return;
5355
}
54-
SSL_library_init();
55-
SSL_load_error_strings();
56-
OpenSSL_add_ssl_algorithms();
56+
OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, nullptr);
5757
#ifdef _WIN32
5858
if (getenv("UDA_SSL_INITIALISED") == nullptr) {
5959
_putenv_s("UDA_SSL_INITIALISED", "1");
@@ -84,7 +84,6 @@ void closeUdaClientSSL()
8484
if (ctx != nullptr) {
8585
SSL_CTX_free(ctx);
8686
}
87-
EVP_cleanup();
8887
g_ssl = nullptr;
8988
g_ctx = nullptr;
9089
#ifdef _WIN32
@@ -272,10 +271,10 @@ int configureUdaClientSSLContext(const HostData* host)
272271
UDA_THROW_ERROR(999, "Unable to parse client certificate [%s] to verify certificate validity");
273272
}
274273

275-
const ASN1_TIME* before = X509_get_notBefore(clientCert);
276-
const ASN1_TIME* after = X509_get_notAfter(clientCert);
274+
const ASN1_TIME* before = X509_getm_notBefore(clientCert);
275+
const ASN1_TIME* after = X509_getm_notAfter(clientCert);
277276

278-
char work[X509STRINGSIZE];
277+
char work[X509_STRING_SIZE];
279278
UDA_LOG(UDA_LOG_DEBUG, "Client X509 subject: %s\n",
280279
X509_NAME_oneline(X509_get_subject_name(clientCert), work, sizeof(work)));
281280
UDA_LOG(UDA_LOG_DEBUG, "Client X509 issuer: %s\n",
@@ -284,31 +283,22 @@ int configureUdaClientSSLContext(const HostData* host)
284283
time_t current_time = time(nullptr);
285284
char* c_time_string = ctime(&current_time);
286285

287-
int rc = 0, count = 0;
288-
BIO* b = BIO_new(BIO_s_mem());
289-
if (b && ASN1_TIME_print(b, before)) {
290-
count = BIO_read(b, work, X509STRINGSIZE - 1);
291-
BIO_free(b);
292-
}
293-
work[count] = '\0';
294-
UDA_LOG(UDA_LOG_DEBUG, "Client X509 not before: %s\n", work);
286+
std::string before_string = to_string(before);
287+
288+
UDA_LOG(UDA_LOG_DEBUG, "Client X509 not before: %s\n", before_string.c_str());
289+
int rc = 0;
295290
if ((rc = X509_cmp_time(before, &current_time)) >= 0) {
296291
// Not Before is after Now!
297292
X509_free(clientCert);
298293
UDA_LOG(UDA_LOG_DEBUG, "Current Time : %s\n", c_time_string);
299294
UDA_LOG(UDA_LOG_DEBUG, "Client X509 not before date is before the current date!\n");
300-
UDA_LOG(UDA_LOG_DEBUG, "The client SSL/x509 certificate is Not Valid - the Vaidity Date is in the future!\n");
301-
UDA_THROW_ERROR(999, "The client SSL/x509 certificate is Not Valid - the Vaidity Date is in the future");
295+
UDA_LOG(UDA_LOG_DEBUG, "The client SSL/x509 certificate is Not Valid - the Validity Date is in the future!\n");
296+
UDA_THROW_ERROR(999, "The client SSL/x509 certificate is Not Valid - the Validity Date is in the future");
302297
}
303298

304-
count = 0;
305-
b = BIO_new(BIO_s_mem());
306-
if (b && ASN1_TIME_print(b, after)) {
307-
count = BIO_read(b, work, X509STRINGSIZE - 1);
308-
BIO_free(b);
309-
}
310-
work[count] = '\0';
311-
UDA_LOG(UDA_LOG_DEBUG, "Client X509 not after : %s\n", work);
299+
std::string after_string = to_string(after);
300+
301+
UDA_LOG(UDA_LOG_DEBUG, "Client X509 not after : %s\n", after_string.c_str());
312302
if ((rc = X509_cmp_time(after, &current_time)) <= 0) {// Not After is before Now!
313303
X509_free(clientCert);
314304
UDA_LOG(UDA_LOG_DEBUG, "Current Time : %s\n", c_time_string);
@@ -319,7 +309,7 @@ int configureUdaClientSSLContext(const HostData* host)
319309
X509_free(clientCert);
320310

321311
UDA_LOG(UDA_LOG_DEBUG, "Current Time : %s\n", c_time_string);
322-
UDA_LOG(UDA_LOG_DEBUG, "Cient certificate date validity checked but not validated \n");
312+
UDA_LOG(UDA_LOG_DEBUG, "Client certificate date validity checked but not validated \n");
323313

324314
return 0;
325315
}
@@ -393,7 +383,7 @@ int startUdaClientSSL()
393383
}
394384

395385
// Get the Server certificate and verify
396-
X509* peer = SSL_get_peer_certificate(g_ssl);
386+
X509* peer = SSL_get1_peer_certificate(g_ssl);
397387

398388
if (peer != nullptr) {
399389

@@ -407,7 +397,7 @@ int startUdaClientSSL()
407397

408398
// Server's details - not required apart from logging
409399

410-
char work[X509STRINGSIZE];
400+
char work[X509_STRING_SIZE];
411401
UDA_LOG(UDA_LOG_DEBUG, "Server certificate verified\n");
412402
UDA_LOG(UDA_LOG_DEBUG, "X509 subject: %s\n",
413403
X509_NAME_oneline(X509_get_subject_name(peer), work, sizeof(work)));
@@ -416,20 +406,15 @@ int startUdaClientSSL()
416406

417407
// Verify Date validity
418408

419-
const ASN1_TIME* before = X509_get_notBefore(peer);
420-
const ASN1_TIME* after = X509_get_notAfter(peer);
409+
const ASN1_TIME* before = X509_getm_notBefore(peer);
410+
const ASN1_TIME* after = X509_getm_notAfter(peer);
421411

422412
time_t current_time = time(nullptr);
423413
char* c_time_string = ctime(&current_time);
424414

425-
int count = 0;
426-
BIO* b = BIO_new(BIO_s_mem());
427-
if (b && ASN1_TIME_print(b, before)) {
428-
count = BIO_read(b, work, X509STRINGSIZE - 1);
429-
BIO_free(b);
430-
}
431-
work[count] = '\0';
432-
UDA_LOG(UDA_LOG_DEBUG, "Server X509 not before: %s\n", work);
415+
std::string before_string = to_string(before);
416+
417+
UDA_LOG(UDA_LOG_DEBUG, "Server X509 not before: %s\n", before_string.c_str());
433418
if ((rc = X509_cmp_time(before, &current_time)) >= 0) {// Not Before is after Now!
434419
X509_free(peer);
435420
UDA_LOG(UDA_LOG_DEBUG, "Current Time : %s\n", c_time_string);
@@ -439,14 +424,9 @@ int startUdaClientSSL()
439424
UDA_THROW_ERROR(999, "The Server's SSL/x509 certificate is Not Valid - the Vaidity Date is in the future");
440425
}
441426

442-
count = 0;
443-
b = BIO_new(BIO_s_mem());
444-
if (b && ASN1_TIME_print(b, after)) {
445-
count = BIO_read(b, work, X509STRINGSIZE - 1);
446-
BIO_free(b);
447-
}
448-
work[count] = '\0';
449-
UDA_LOG(UDA_LOG_DEBUG, "Server X509 not after : %s\n", work);
427+
std::string after_string = to_string(after);
428+
429+
UDA_LOG(UDA_LOG_DEBUG, "Server X509 not after : %s\n", after_string.c_str());
450430
if ((rc = X509_cmp_time(after, &current_time)) <= 0) {// Not After is before Now!
451431
X509_free(peer);
452432
UDA_LOG(UDA_LOG_DEBUG, "Current Time : %s\n", c_time_string);

source/authentication/udaClientSSL.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <openssl/x509.h>
2222

2323
#define VERIFY_DEPTH 4
24-
#define X509STRINGSIZE 256
2524

2625
#include <client/udaClientHostList.h>
2726
#include <clientserver/export.h>

source/authentication/udaServerSSL.cpp

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
#include <logging/logging.h>
1616
#include <server/writer.h>
1717

18+
#include "utils.h"
19+
1820
#define VERIFY_DEPTH 4
19-
#define X509STRINGSIZE 256
2021

2122
/*
2223
Note on initialisation:
@@ -94,9 +95,7 @@ void initUdaServerSSL()
9495
UDA_LOG(UDA_LOG_DEBUG, "Prior SSL initialisation\n");
9596
return;
9697
}
97-
SSL_library_init();
98-
SSL_load_error_strings();
99-
OpenSSL_add_ssl_algorithms();
98+
OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, nullptr);
10099
#ifdef _WIN32
101100
_putenv_s("UDA_SSL_INITIALISED", "1");
102101
#else
@@ -122,7 +121,6 @@ void closeUdaServerSSL()
122121
}
123122
if (g_ctx != nullptr)
124123
SSL_CTX_free(g_ctx);
125-
EVP_cleanup();
126124
g_ssl = nullptr;
127125
g_ctx = nullptr;
128126
#ifdef _WIN32
@@ -151,14 +149,6 @@ SSL_CTX* createUdaServerSSLContext()
151149
// Disable SSLv2 for v3 and TSLv1 negotiation
152150
SSL_CTX_set_options(g_ctx, SSL_OP_NO_SSLv2);
153151

154-
/*
155-
// Set the Cipher List
156-
if (SSL_CTX_set_cipher_list(g_ctx, "AES128-SHA") <= 0) {
157-
printf("Error setting the cipher list.\n");
158-
exit(0);
159-
}
160-
*/
161-
162152
UDA_LOG(UDA_LOG_DEBUG, "SSL Context created\n");
163153

164154
return g_ctx;
@@ -210,12 +200,12 @@ int configureUdaServerSSLContext()
210200
SSL_CTX_set_verify(g_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr);
211201
SSL_CTX_set_verify_depth(g_ctx, VERIFY_DEPTH);
212202

213-
// Add verification against the Certificate Revocation List
214-
X509_VERIFY_PARAM* params = X509_VERIFY_PARAM_new();
215-
X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_CRL_CHECK);
216-
SSL_CTX_set1_param(g_ctx, params);
217-
218203
if (crlist != nullptr) {
204+
// Add verification against the Certificate Revocation List
205+
X509_VERIFY_PARAM* params = X509_VERIFY_PARAM_new();
206+
X509_VERIFY_PARAM_set_flags(params, X509_V_FLAG_CRL_CHECK);
207+
SSL_CTX_set1_param(g_ctx, params);
208+
219209
X509_CRL* crl = loadUdaServerSSLCrl(crlist);
220210
if (!crl) {
221211
return 999; // CRL not loaded
@@ -232,22 +222,6 @@ int configureUdaServerSSLContext()
232222
SSL_CTX_set1_verify_cert_store(g_ctx, st);
233223
}
234224

235-
// Set CA list used for client authentication
236-
237-
/*
238-
if(SSL_CTX_use_certificate_chain_file(g_ctx, getenv("UDA_SERVER_CA_SSL_CERT")) < 1){
239-
//printf("Error setting the CA chain file\n");
240-
exit(0);
241-
}
242-
*/
243-
/*
244-
SSL_CTX_set_client_CA_list(g_ctx, SSL_load_client_CA_file(getenv("UDA_SERVER_CA_SSL_CERT")));
245-
246-
rc = load_CA(g_ssl, g_ctx, getenv("UDA_SERVER_CA_SSL_CERT")); // calls SSL_CTX_add_client_CA(g_ctx, X509
247-
*cacert) and SSL_add_client_CA(g_ssl, X509 *cacert) if(rc == 0)fprintf(logout, "Unable to load Client
248-
CA!\n");
249-
*/
250-
251225
UDA_LOG(UDA_LOG_DEBUG, "SSL Context configured\n");
252226

253227
return 0;
@@ -346,10 +320,10 @@ int startUdaServerSSL()
346320
}
347321

348322
// Get the Client's certificate and verify
349-
X509* peer = SSL_get_peer_certificate(g_ssl);
323+
X509* peer = SSL_get1_peer_certificate(g_ssl);
350324

351325
if (peer != nullptr) {
352-
if ((rc = SSL_get_verify_result(g_ssl)) != X509_V_OK) {
326+
if ((rc = (int)SSL_get_verify_result(g_ssl)) != X509_V_OK) {
353327
// returns X509_V_OK if the certificate was not obtained as no error occured!
354328
X509_free(peer);
355329
UDA_LOG(UDA_LOG_DEBUG, "SSL Client certificate presented but verification error!\n");
@@ -359,13 +333,20 @@ int startUdaServerSSL()
359333

360334
// Client's details
361335

362-
char work[X509STRINGSIZE];
336+
char work[X509_STRING_SIZE];
363337
UDA_LOG(UDA_LOG_DEBUG, "Client certificate verified\n");
364338
UDA_LOG(UDA_LOG_DEBUG, "X509 subject: %s\n",
365339
X509_NAME_oneline(X509_get_subject_name(peer), work, sizeof(work)));
366340
UDA_LOG(UDA_LOG_DEBUG, "X509 issuer: %s\n", X509_NAME_oneline(X509_get_issuer_name(peer), work, sizeof(work)));
367-
UDA_LOG(UDA_LOG_DEBUG, "X509 not before: %d\n", X509_get_notBefore(peer));
368-
UDA_LOG(UDA_LOG_DEBUG, "X509 not after: %d\n", X509_get_notAfter(peer));
341+
342+
ASN1_TIME* before = X509_getm_notBefore(peer);
343+
ASN1_TIME* after = X509_getm_notAfter(peer);
344+
345+
std::string before_string = to_string(before);
346+
std::string after_string = to_string(after);
347+
348+
UDA_LOG(UDA_LOG_DEBUG, "X509 not before: %d\n", before_string.c_str());
349+
UDA_LOG(UDA_LOG_DEBUG, "X509 not after: %d\n", after_string.c_str());
369350
X509_free(peer);
370351
} else {
371352
X509_free(peer);

source/authentication/utils.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "utils.h"
2+
3+
#include <openssl/bio.h>
4+
#include <openssl/asn1.h>
5+
6+
std::string to_string(const ASN1_TIME* asn1_time)
7+
{
8+
char work[X509_STRING_SIZE];
9+
10+
int count = 0;
11+
BIO* b = BIO_new(BIO_s_mem());
12+
if (b && ASN1_TIME_print(b, asn1_time)) {
13+
count = BIO_read(b, work, X509_STRING_SIZE - 1);
14+
BIO_free(b);
15+
}
16+
work[count] = '\0';
17+
18+
return work;
19+
}

source/authentication/utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <openssl/types.h>
5+
6+
#define X509_STRING_SIZE 256
7+
8+
std::string to_string(const ASN1_TIME* time);

0 commit comments

Comments
 (0)