Skip to content

Commit 7d75f50

Browse files
committed
Implement suggested expanded flag instead of checking strlen on each iteration
1 parent 6576531 commit 7d75f50

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

ext/openssl/xp_ssl.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,17 +436,20 @@ static bool php_openssl_matches_san_list(X509 *peer, const char *subject_name) /
436436
{
437437
int i, len;
438438
unsigned char *cert_name = NULL;
439-
char ipbuffer[64], ipv6_expanded[40];
440-
unsigned char ipv6[16];
439+
char ipbuffer[64];
441440

442441
GENERAL_NAMES *alt_names = X509_get_ext_d2i(peer, NID_subject_alt_name, 0, 0);
443442
int alt_name_count = sk_GENERAL_NAME_num(alt_names);
444443

445-
/* detect if subject name is an IPv6 address and expand once if required */
446-
ipv6_expanded[0] = 0;
447444
#if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
445+
/* detect if subject name is an IPv6 address and expand once if required */
446+
char subject_name_ipv6_expanded[40];
447+
unsigned char ipv6[16];
448+
bool subject_name_is_ipv6 = false;
449+
subject_name_ipv6_expanded[0] = 0;
448450
if (inet_pton(AF_INET6,subject_name,&ipv6)) {
449-
EXPAND_IPV6_ADDRESS(ipv6_expanded, ipv6);
451+
EXPAND_IPV6_ADDRESS(subject_name_ipv6_expanded, ipv6);
452+
subject_name_is_ipv6 = true;
450453
}
451454
#endif
452455

@@ -487,15 +490,18 @@ static bool php_openssl_matches_san_list(X509 *peer, const char *subject_name) /
487490

488491
return 1;
489492
}
490-
} else if (san->d.ip->length == 16 && strlen(ipv6_expanded) >= 15) { /* shortest expanded IPv6 address is 0:0:0:0:0:0:0:0 */
493+
}
494+
#if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
495+
else if (san->d.ip->length == 16 && subject_name_is_ipv6) {
491496
ipbuffer[0] = 0;
492497
EXPAND_IPV6_ADDRESS(ipbuffer, san->d.iPAddress->data);
493-
if (strcasecmp((const char*)ipv6_expanded, (const char*)ipbuffer) == 0) {
498+
if (strcasecmp((const char*)subject_name_ipv6_expanded, (const char*)ipbuffer) == 0) {
494499
sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
495500

496501
return 1;
497502
}
498503
}
504+
#endif
499505
}
500506
}
501507

0 commit comments

Comments
 (0)