Skip to content

Commit 0d7f84f

Browse files
sigiesecalanxz
authored andcommitted
Lib: remove use of OpenSSL functions deprecated in v1.1.0+
1 parent ef8c3b9 commit 0d7f84f

File tree

4 files changed

+70
-38
lines changed

4 files changed

+70
-38
lines changed

librabbitmq/amqp_openssl.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,15 @@ static int setup_openssl(void) {
583583
CRYPTO_set_id_callback(ssl_threadid_callback);
584584
CRYPTO_set_locking_callback(ssl_locking_callback);
585585

586+
#ifdef AMQP_OPENSSL_V110
587+
if (CONF_modules_load_file(NULL, "rabbitmq-c", CONF_MFLAGS_DEFAULT_SECTION) <=
588+
0) {
589+
status = AMQP_STATUS_SSL_ERROR;
590+
goto out;
591+
}
592+
#else
586593
OPENSSL_config(NULL);
594+
#endif
587595
SSL_library_init();
588596
SSL_load_error_strings();
589597

@@ -660,7 +668,9 @@ int amqp_uninitialize_ssl_library(void) {
660668
amqp_openssl_bio_destroy();
661669
openssl_bio_initialized = 0;
662670

671+
#ifndef AMQP_OPENSSL_V110
663672
ERR_remove_state(0);
673+
#endif
664674
FIPS_mode_set(0);
665675

666676
CRYPTO_set_locking_callback(NULL);

librabbitmq/amqp_openssl_bio.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ static int amqp_ssl_bio_initialized = 0;
4444

4545
#ifdef AMQP_USE_AMQP_BIO
4646

47-
#if (OPENSSL_VERSION_NUMBER > 0x10100000L)
48-
#define AMQP_OPENSSL_V110
49-
#endif
50-
5147
static BIO_METHOD *amqp_bio_method;
5248

5349
static int amqp_openssl_bio_should_retry(int res) {
@@ -147,7 +143,10 @@ int amqp_openssl_bio_init(void) {
147143
return AMQP_STATUS_NO_MEMORY;
148144
}
149145

150-
BIO_METHOD *meth = BIO_s_socket();
146+
// casting away const is necessary until
147+
// https://github.com/openssl/openssl/pull/2181/, which is targeted for
148+
// openssl 1.1.1
149+
BIO_METHOD *meth = (BIO_METHOD *)BIO_s_socket();
151150
BIO_meth_set_create(amqp_bio_method, BIO_meth_get_create(meth));
152151
BIO_meth_set_destroy(amqp_bio_method, BIO_meth_get_destroy(meth));
153152
BIO_meth_set_ctrl(amqp_bio_method, BIO_meth_get_ctrl(meth));
@@ -184,7 +183,7 @@ void amqp_openssl_bio_destroy(void) {
184183
amqp_ssl_bio_initialized = 0;
185184
}
186185

187-
BIO_METHOD *amqp_openssl_bio(void) {
186+
BIO_METHOD_PTR amqp_openssl_bio(void) {
188187
assert(amqp_ssl_bio_initialized);
189188
#ifdef AMQP_USE_AMQP_BIO
190189
return amqp_bio_method;

librabbitmq/amqp_openssl_bio.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ int amqp_openssl_bio_init(void);
2929

3030
void amqp_openssl_bio_destroy(void);
3131

32-
BIO_METHOD* amqp_openssl_bio(void);
32+
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
33+
#define AMQP_OPENSSL_V110
34+
#endif
35+
36+
#ifdef AMQP_OPENSSL_V110
37+
typedef const BIO_METHOD *BIO_METHOD_PTR;
38+
#else
39+
typedef BIO_METHOD *BIO_METHOD_PTR;
40+
#endif
41+
42+
BIO_METHOD_PTR amqp_openssl_bio(void);
3343

3444
#endif /* ifndef AMQP_OPENSSL_BIO */

librabbitmq/amqp_openssl_hostname_validation.c

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,28 @@
3131
#include <openssl/x509v3.h>
3232

3333
#include "amqp_hostcheck.h"
34+
#include "amqp_openssl_bio.h"
3435
#include "amqp_openssl_hostname_validation.h"
3536

37+
#include <string.h>
38+
3639
#define HOSTNAME_MAX_SIZE 255
3740

3841
/**
39-
* Tries to find a match for hostname in the certificate's Common Name field.
40-
*
41-
* Returns AMQP_HVR_MATCH_FOUND if a match was found.
42-
* Returns AMQP_HVR_MATCH_NOT_FOUND if no matches were found.
43-
* Returns AMQP_HVR_MALFORMED_CERTIFICATE if the Common Name had a NUL character
44-
* embedded in it.
45-
* Returns AMQP_HVR_ERROR if the Common Name could not be extracted.
46-
*/
42+
* Tries to find a match for hostname in the certificate's Common Name field.
43+
*
44+
* Returns AMQP_HVR_MATCH_FOUND if a match was found.
45+
* Returns AMQP_HVR_MATCH_NOT_FOUND if no matches were found.
46+
* Returns AMQP_HVR_MALFORMED_CERTIFICATE if the Common Name had a NUL character
47+
* embedded in it.
48+
* Returns AMQP_HVR_ERROR if the Common Name could not be extracted.
49+
*/
4750
static amqp_hostname_validation_result amqp_matches_common_name(
4851
const char *hostname, const X509 *server_cert) {
4952
int common_name_loc = -1;
5053
X509_NAME_ENTRY *common_name_entry = NULL;
5154
ASN1_STRING *common_name_asn1 = NULL;
52-
char *common_name_str = NULL;
55+
const char *common_name_str = NULL;
5356

5457
// Find the position of the CN field in the Subject field of the certificate
5558
common_name_loc = X509_NAME_get_index_by_NID(
@@ -70,7 +73,12 @@ static amqp_hostname_validation_result amqp_matches_common_name(
7073
if (common_name_asn1 == NULL) {
7174
return AMQP_HVR_ERROR;
7275
}
76+
77+
#ifdef AMQP_OPENSSL_V110
78+
common_name_str = (const char *)ASN1_STRING_get0_data(common_name_asn1);
79+
#else
7380
common_name_str = (char *)ASN1_STRING_data(common_name_asn1);
81+
#endif
7482

7583
// Make sure there isn't an embedded NUL character in the CN
7684
if ((size_t)ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {
@@ -86,16 +94,16 @@ static amqp_hostname_validation_result amqp_matches_common_name(
8694
}
8795

8896
/**
89-
* Tries to find a match for hostname in the certificate's Subject Alternative
90-
* Name extension.
91-
*
92-
* Returns AMQP_HVR_MATCH_FOUND if a match was found.
93-
* Returns AMQP_HVR_MATCH_NOT_FOUND if no matches were found.
94-
* Returns AMQP_HVR_MALFORMED_CERTIFICATE if any of the hostnames had a NUL
95-
* character embedded in it.
96-
* Returns AMQP_HVR_NO_SAN_PRESENT if the SAN extension was not present in the
97-
* certificate.
98-
*/
97+
* Tries to find a match for hostname in the certificate's Subject Alternative
98+
* Name extension.
99+
*
100+
* Returns AMQP_HVR_MATCH_FOUND if a match was found.
101+
* Returns AMQP_HVR_MATCH_NOT_FOUND if no matches were found.
102+
* Returns AMQP_HVR_MALFORMED_CERTIFICATE if any of the hostnames had a NUL
103+
* character embedded in it.
104+
* Returns AMQP_HVR_NO_SAN_PRESENT if the SAN extension was not present in the
105+
* certificate.
106+
*/
99107
static amqp_hostname_validation_result amqp_matches_subject_alternative_name(
100108
const char *hostname, const X509 *server_cert) {
101109
amqp_hostname_validation_result result = AMQP_HVR_MATCH_NOT_FOUND;
@@ -117,7 +125,12 @@ static amqp_hostname_validation_result amqp_matches_subject_alternative_name(
117125

118126
if (current_name->type == GEN_DNS) {
119127
// Current name is a DNS name, let's check it
120-
char *dns_name = (char *)ASN1_STRING_data(current_name->d.dNSName);
128+
const char *dns_name = (const char *)
129+
#ifdef AMQP_OPENSSL_V110
130+
ASN1_STRING_get0_data(current_name->d.dNSName);
131+
#else
132+
ASN1_STRING_data(current_name->d.dNSName);
133+
#endif
121134

122135
// Make sure there isn't an embedded NUL character in the DNS name
123136
if ((size_t)ASN1_STRING_length(current_name->d.dNSName) !=
@@ -138,17 +151,17 @@ static amqp_hostname_validation_result amqp_matches_subject_alternative_name(
138151
}
139152

140153
/**
141-
* Validates the server's identity by looking for the expected hostname in the
142-
* server's certificate. As described in RFC 6125, it first tries to find a match
143-
* in the Subject Alternative Name extension. If the extension is not present in
144-
* the certificate, it checks the Common Name instead.
145-
*
146-
* Returns AMQP_HVR_MATCH_FOUND if a match was found.
147-
* Returns AMQP_HVR_MATCH_NOT_FOUND if no matches were found.
148-
* Returns AMQP_HVR_MALFORMED_CERTIFICATE if any of the hostnames had a NUL
149-
* character embedded in it.
150-
* Returns AMQP_HVR_ERROR if there was an error.
151-
*/
154+
* Validates the server's identity by looking for the expected hostname in the
155+
* server's certificate. As described in RFC 6125, it first tries to find a
156+
* match in the Subject Alternative Name extension. If the extension is not
157+
* present in the certificate, it checks the Common Name instead.
158+
*
159+
* Returns AMQP_HVR_MATCH_FOUND if a match was found.
160+
* Returns AMQP_HVR_MATCH_NOT_FOUND if no matches were found.
161+
* Returns AMQP_HVR_MALFORMED_CERTIFICATE if any of the hostnames had a NUL
162+
* character embedded in it.
163+
* Returns AMQP_HVR_ERROR if there was an error.
164+
*/
152165
amqp_hostname_validation_result amqp_ssl_validate_hostname(
153166
const char *hostname, const X509 *server_cert) {
154167
amqp_hostname_validation_result result;

0 commit comments

Comments
 (0)