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+ */
4750static 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+ */
99107static 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+ */
152165amqp_hostname_validation_result amqp_ssl_validate_hostname (
153166 const char * hostname , const X509 * server_cert ) {
154167 amqp_hostname_validation_result result ;
0 commit comments