Skip to content

Commit 73bae6b

Browse files
committed
Add 'serialNumberHex' variable to openssl_x509_parse
Currently, openssl_x509_parse returns an integer. This can be unexpected, as the common way of handling serial numbers is with a hex string. This is compounded as php's dechex() function cannot handle >32 bit numbers which will leave people trying to handle large serial numbers frustrated. By adding this extra return variable to openssl_x509_parse, the consumer of the variable is certain that the serialNumberHex that is returned is the exact Hex Serial number as OpenSSL returns everywhere else.
1 parent d432b0e commit 73bae6b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

ext/openssl/openssl.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,7 @@ PHP_FUNCTION(openssl_x509_parse)
19841984
char *extname;
19851985
BIO *bio_out;
19861986
BUF_MEM *bio_buf;
1987+
char * hexserial;
19871988
char buf[256];
19881989

19891990
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &zcert, &useshortnames) == FAILURE) {
@@ -2013,6 +2014,18 @@ PHP_FUNCTION(openssl_x509_parse)
20132014

20142015
add_assoc_string(return_value, "serialNumber", i2s_ASN1_INTEGER(NULL, X509_get_serialNumber(cert)));
20152016

2017+
/* Return the hex representation of the serial number, as defined by OpenSSL */
2018+
hexserial = BN_bn2hex(ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), NULL));
2019+
2020+
/* If we received null back from BN_bn2hex, there was a critical error in openssl,
2021+
* and we should not continue.
2022+
*/
2023+
if (!hexserial) {
2024+
RETURN_FALSE;
2025+
}
2026+
add_assoc_string(return_value, "serialNumberHex", hexserial, 1);
2027+
OPENSSL_free(hexserial);
2028+
20162029
add_assoc_asn1_string(return_value, "validFrom", X509_get_notBefore(cert));
20172030
add_assoc_asn1_string(return_value, "validTo", X509_get_notAfter(cert));
20182031

0 commit comments

Comments
 (0)