Skip to content

Commit 4d49c0c

Browse files
committed
ext/openssl: refactor php_openssl_x509_fingerprint_cmp() function
Use sites only care if the string matches, thus change the function to be an 'is equal' variant. Also use a zend_string* rather than a char* so that we can use the zend_string equals API.
1 parent c85685a commit 4d49c0c

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

ext/openssl/xp_ssl.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,16 @@ static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) /* {{{ */
377377
}
378378
/* }}} */
379379

380-
static int php_openssl_x509_fingerprint_cmp(X509 *peer, const char *method, const char *expected)
380+
static bool php_openssl_x509_fingerprint_is_equal(X509 *peer, const char *method, const zend_string *expected)
381381
{
382-
zend_string *fingerprint;
383-
int result = -1;
384-
385-
fingerprint = php_openssl_x509_fingerprint(peer, method, false);
382+
bool is_equal = false;
383+
zend_string *fingerprint = php_openssl_x509_fingerprint(peer, method, false);
386384
if (fingerprint) {
387-
result = strcasecmp(expected, ZSTR_VAL(fingerprint));
388-
zend_string_release_ex(fingerprint, 0);
385+
is_equal = zend_string_equals_ci(fingerprint, expected);
386+
zend_string_release_ex(fingerprint, false);
389387
}
390388

391-
return result;
389+
return is_equal;
392390
}
393391

394392
static bool php_openssl_x509_fingerprint_match(php_stream *stream, X509 *peer, const zval *val)
@@ -406,7 +404,7 @@ static bool php_openssl_x509_fingerprint_match(php_stream *stream, X509 *peer, c
406404
break;
407405
}
408406

409-
return method && php_openssl_x509_fingerprint_cmp(peer, method, Z_STRVAL_P(val)) == 0;
407+
return method && php_openssl_x509_fingerprint_is_equal(peer, method, Z_STR_P(val));
410408
} else if (Z_TYPE_P(val) == IS_ARRAY) {
411409
zval *current;
412410
zend_string *key;
@@ -423,7 +421,7 @@ static bool php_openssl_x509_fingerprint_match(php_stream *stream, X509 *peer, c
423421
php_stream_warn(stream, Generic, "Invalid peer_fingerprint array; [algo => fingerprint] form required");
424422
return false;
425423
}
426-
if (php_openssl_x509_fingerprint_cmp(peer, ZSTR_VAL(key), Z_STRVAL_P(current)) != 0) {
424+
if (!php_openssl_x509_fingerprint_is_equal(peer, ZSTR_VAL(key), Z_STR_P(current))) {
427425
return false;
428426
}
429427
} ZEND_HASH_FOREACH_END();

0 commit comments

Comments
 (0)