Skip to content

Bugfix 71519 - return serialNumberHex from openssl_x509_parse #1755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,7 @@ PHP_FUNCTION(openssl_x509_parse)
char *extname;
BIO *bio_out;
BUF_MEM *bio_buf;
char * hexserial;
char buf[256];

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

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

/* Return the hex representation of the serial number, as defined by OpenSSL */
hexserial = BN_bn2hex(ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), NULL));

/* If we received null back from BN_bn2hex, there was a critical error in openssl,
* and we should not continue.
*/
if (!hexserial) {
RETURN_FALSE;
}
add_assoc_string(return_value, "serialNumberHex", hexserial);
OPENSSL_free(hexserial);

add_assoc_asn1_string(return_value, "validFrom", X509_get_notBefore(cert));
add_assoc_asn1_string(return_value, "validTo", X509_get_notAfter(cert));

Expand Down
8 changes: 6 additions & 2 deletions ext/openssl/tests/openssl_x509_parse_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var_dump(openssl_x509_parse($cert));
var_dump(openssl_x509_parse($cert, false));
?>
--EXPECTF--
array(15) {
array(16) {
["name"]=>
string(96) "/C=BR/ST=Rio Grande do Sul/L=Porto Alegre/CN=Henrique do N. Angelo/emailAddress=hnangelo@php.net"
["subject"]=>
Expand Down Expand Up @@ -47,6 +47,8 @@ array(15) {
int(2)
["serialNumber"]=>
string(20) "12593567369101004962"
["serialNumberHex"]=>
string(16) "AEC556CC723750A2"
["validFrom"]=>
string(13) "080630102843Z"
["validTo"]=>
Expand Down Expand Up @@ -158,7 +160,7 @@ serial:AE:C5:56:CC:72:37:50:A2
string(7) "CA:TRUE"
}
}
array(15) {
array(16) {
["name"]=>
string(96) "/C=BR/ST=Rio Grande do Sul/L=Porto Alegre/CN=Henrique do N. Angelo/emailAddress=hnangelo@php.net"
["subject"]=>
Expand Down Expand Up @@ -193,6 +195,8 @@ array(15) {
int(2)
["serialNumber"]=>
string(20) "12593567369101004962"
["serialNumberHex"]=>
string(16) "AEC556CC723750A2"
["validFrom"]=>
string(13) "080630102843Z"
["validTo"]=>
Expand Down