Skip to content

Commit 08fd302

Browse files
bbolligitster
authored andcommitted
imap-send: use the OpenSSL API to access the subject common name
The OpenSSL 4.0 master branch has deprecated the X509_NAME_get_text_by_NID function. Use the recommended replacement APIs instead. They have existed since OpenSSL v1.1.0. Take care to get the constness right for pre-4.0 versions. Signed-off-by: Beat Bolli <dev+git@drbeat.li> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent dfcdd0b commit 08fd302

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

imap-send.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,13 @@ static int host_matches(const char *host, const char *pattern)
233233

234234
static int verify_hostname(X509 *cert, const char *hostname)
235235
{
236-
int len;
236+
#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
237+
const X509_NAME *subj;
238+
#else
237239
X509_NAME *subj;
238-
char cname[1000];
240+
#endif
241+
const X509_NAME_ENTRY *cname_entry;
242+
const ASN1_STRING *cname;
239243
int i, found;
240244
STACK_OF(GENERAL_NAME) *subj_alt_names;
241245

@@ -262,12 +266,15 @@ static int verify_hostname(X509 *cert, const char *hostname)
262266
/* try the common name */
263267
if (!(subj = X509_get_subject_name(cert)))
264268
return error("cannot get certificate subject");
265-
if ((len = X509_NAME_get_text_by_NID(subj, NID_commonName, cname, sizeof(cname))) < 0)
269+
if ((i = X509_NAME_get_index_by_NID(subj, NID_commonName, -1)) < 0 ||
270+
(cname_entry = X509_NAME_get_entry(subj, i)) == NULL ||
271+
(cname = X509_NAME_ENTRY_get_data(cname_entry)) == NULL)
266272
return error("cannot get certificate common name");
267-
if (strlen(cname) == (size_t)len && host_matches(hostname, cname))
273+
if (strlen((const char *)ASN1_STRING_get0_data(cname)) == ASN1_STRING_length(cname) &&
274+
host_matches(hostname, (const char *)ASN1_STRING_get0_data(cname)))
268275
return 0;
269276
return error("certificate owner '%s' does not match hostname '%s'",
270-
cname, hostname);
277+
ASN1_STRING_get0_data(cname), hostname);
271278
}
272279

273280
static int ssl_socket_connect(struct imap_socket *sock,

0 commit comments

Comments
 (0)