Skip to content

Commit dfeef07

Browse files
shivammathurthesamesam
authored andcommitted
ext/openssl: fix OpenSSL 4 build and test compatibility (#22267)
* Fix OpenSSL 4 test compatibility * Fix OpenSSL 4 build compatibility * Use OpenSSL 4 in macOS CI * Fix OpenSSL 1.1 build compatibility * Preserve raw CN matching with OpenSSL 4 (cherry picked from commit 6aca6e3)
1 parent 9e11c1c commit dfeef07

9 files changed

Lines changed: 127 additions & 30 deletions

‎ext/openssl/openssl.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ PHP_MINIT_FUNCTION(openssl)
425425
ssl_stream_data_index = SSL_get_ex_new_index(0, "PHP stream index", NULL, NULL, NULL);
426426

427427
php_stream_xport_register("ssl", php_openssl_ssl_socket_factory);
428-
#ifndef OPENSSL_NO_SSL3
428+
#if OPENSSL_VERSION_NUMBER < 0x40000000L && !defined(OPENSSL_NO_SSL3)
429429
php_stream_xport_register("sslv3", php_openssl_ssl_socket_factory);
430430
#endif
431431
php_stream_xport_register("tls", php_openssl_ssl_socket_factory);
@@ -499,7 +499,7 @@ PHP_MSHUTDOWN_FUNCTION(openssl)
499499
php_unregister_url_stream_wrapper("ftps");
500500

501501
php_stream_xport_unregister("ssl");
502-
#ifndef OPENSSL_NO_SSL3
502+
#if OPENSSL_VERSION_NUMBER < 0x40000000L && !defined(OPENSSL_NO_SSL3)
503503
php_stream_xport_unregister("sslv3");
504504
#endif
505505
php_stream_xport_unregister("tls");
@@ -999,8 +999,8 @@ PHP_FUNCTION(openssl_x509_parse)
999999
bool useshortnames = 1;
10001000
char * tmpstr;
10011001
zval subitem;
1002-
X509_EXTENSION *extension;
1003-
X509_NAME *subject_name;
1002+
PHP_OPENSSL_X509_EXTENSION *extension;
1003+
const X509_NAME *subject_name;
10041004
char *cert_name;
10051005
char *extname;
10061006
BIO *bio_out;
@@ -1969,7 +1969,7 @@ PHP_FUNCTION(openssl_csr_get_subject)
19691969
zend_object *csr_obj;
19701970
zend_string *csr_str;
19711971
bool use_shortnames = 1;
1972-
X509_NAME *subject;
1972+
const X509_NAME *subject;
19731973

19741974
ZEND_PARSE_PARAMETERS_START(1, 2)
19751975
Z_PARAM_OBJ_OF_CLASS_OR_STR(csr_obj, php_openssl_request_ce, csr_str)

‎ext/openssl/openssl_backend_common.c‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@
3535
/* true global; readonly after module startup */
3636
static char default_ssl_conf_filename[MAXPATHLEN];
3737

38-
void php_openssl_add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int shortname)
38+
void php_openssl_add_assoc_name_entry(zval * val, char * key, const X509_NAME * name, int shortname)
3939
{
4040
zval *data;
4141
zval subitem, tmp;
4242
int i;
4343
char *sname;
4444
int nid;
45-
X509_NAME_ENTRY * ne;
46-
ASN1_STRING * str = NULL;
47-
ASN1_OBJECT * obj;
45+
const X509_NAME_ENTRY * ne;
46+
const ASN1_STRING * str = NULL;
47+
const ASN1_OBJECT * obj;
4848

4949
if (key != NULL) {
5050
array_init(&subitem);
@@ -108,7 +108,7 @@ void php_openssl_add_assoc_name_entry(zval * val, char * key, X509_NAME * name,
108108
}
109109
}
110110

111-
void php_openssl_add_assoc_asn1_string(zval * val, char * key, ASN1_STRING * str)
111+
void php_openssl_add_assoc_asn1_string(zval * val, char * key, const ASN1_STRING * str)
112112
{
113113
add_assoc_stringl(val, key, (const char *)ASN1_STRING_get0_data(str), ASN1_STRING_length(str));
114114
}
@@ -618,11 +618,11 @@ zend_string* php_openssl_x509_fingerprint(X509 *peer, const char *method, bool r
618618
/* Special handling of subjectAltName, see CVE-2013-4073
619619
* Christian Heimes
620620
*/
621-
int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
621+
int openssl_x509v3_subjectAltName(BIO *bio, PHP_OPENSSL_X509_EXTENSION *extension)
622622
{
623623
GENERAL_NAMES *names;
624624
const X509V3_EXT_METHOD *method = NULL;
625-
ASN1_OCTET_STRING *extension_data;
625+
const ASN1_OCTET_STRING *extension_data;
626626
long i, length, num;
627627
const unsigned char *p;
628628

@@ -978,7 +978,12 @@ zend_result php_openssl_csr_make(struct php_x509_request * req, X509_REQ * csr,
978978
zval *item, *subitem;
979979
zend_string *strindex = NULL;
980980

981-
subj = X509_REQ_get_subject_name(csr);
981+
subj = X509_NAME_new();
982+
if (subj == NULL) {
983+
php_openssl_store_errors();
984+
return FAILURE;
985+
}
986+
982987
/* apply values from the dn hash */
983988
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(dn), strindex, item) {
984989
if (strindex) {
@@ -987,10 +992,12 @@ zend_result php_openssl_csr_make(struct php_x509_request * req, X509_REQ * csr,
987992
if (Z_TYPE_P(item) == IS_ARRAY) {
988993
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(item), i, subitem) {
989994
if (php_openssl_csr_add_subj_entry(subitem, subj, nid) == FAILURE) {
995+
X509_NAME_free(subj);
990996
return FAILURE;
991997
}
992998
} ZEND_HASH_FOREACH_END();
993999
} else if (php_openssl_csr_add_subj_entry(item, subj, nid) == FAILURE) {
1000+
X509_NAME_free(subj);
9941001
return FAILURE;
9951002
}
9961003
} else {
@@ -1041,13 +1048,23 @@ zend_result php_openssl_csr_make(struct php_x509_request * req, X509_REQ * csr,
10411048
if (!X509_NAME_add_entry_by_txt(subj, type, MBSTRING_UTF8, (unsigned char*)v->value, -1, -1, 0)) {
10421049
php_openssl_store_errors();
10431050
php_error_docref(NULL, E_WARNING, "add_entry_by_txt %s -> %s (failed)", type, v->value);
1051+
X509_NAME_free(subj);
10441052
return FAILURE;
10451053
}
10461054
if (!X509_NAME_entry_count(subj)) {
10471055
php_error_docref(NULL, E_WARNING, "No objects specified in config file");
1056+
X509_NAME_free(subj);
10481057
return FAILURE;
10491058
}
10501059
}
1060+
1061+
if (!X509_REQ_set_subject_name(csr, subj)) {
1062+
php_openssl_store_errors();
1063+
X509_NAME_free(subj);
1064+
return FAILURE;
1065+
}
1066+
X509_NAME_free(subj);
1067+
10511068
if (attribs) {
10521069
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(attribs), strindex, item) {
10531070
int nid;

‎ext/openssl/openssl_backend_v3.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ EVP_PKEY *php_openssl_pkey_init_ec(zval *data, bool *is_private) {
404404
}
405405

406406
OPENSSL_PKEY_SET_BN(data, cofactor);
407-
if (!OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, cofactor) ||
407+
if ((cofactor && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) ||
408408
!EC_GROUP_set_generator(group, point_g, order, cofactor)) {
409409
goto cleanup;
410410
}

‎ext/openssl/php_openssl.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern zend_module_entry openssl_module_entry;
2525
#include "php_version.h"
2626
#define PHP_OPENSSL_VERSION PHP_VERSION
2727

28+
#include <openssl/opensslconf.h>
2829
#include <openssl/opensslv.h>
2930
/* OpenSSL version check */
3031
#if OPENSSL_VERSION_NUMBER < 0x30000000L

‎ext/openssl/php_openssl_backend.h‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
#include <openssl/pkcs12.h>
3838
#include <openssl/cms.h>
3939

40+
#if defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 4
41+
typedef const X509_EXTENSION PHP_OPENSSL_X509_EXTENSION;
42+
#else
43+
typedef X509_EXTENSION PHP_OPENSSL_X509_EXTENSION;
44+
#endif
45+
4046
/* number conversion flags checks */
4147
#define PHP_OPENSSL_CHECK_NUMBER_CONVERSION(_cond, _name, _arg_num) \
4248
do { \
@@ -168,8 +174,8 @@ struct php_x509_request {
168174
const EVP_CIPHER * priv_key_encrypt_cipher;
169175
};
170176

171-
void php_openssl_add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int shortname);
172-
void php_openssl_add_assoc_asn1_string(zval * val, char * key, ASN1_STRING * str);
177+
void php_openssl_add_assoc_name_entry(zval * val, char * key, const X509_NAME * name, int shortname);
178+
void php_openssl_add_assoc_asn1_string(zval * val, char * key, const ASN1_STRING * str);
173179
time_t php_openssl_asn1_time_to_time_t(ASN1_UTCTIME * timestr);
174180
int php_openssl_config_check_syntax(const char * section_label, const char * config_filename, const char * section, CONF *config);
175181
char *php_openssl_conf_get_string(CONF *conf, const char *group, const char *name);
@@ -266,7 +272,7 @@ X509 *php_openssl_x509_from_zval(
266272

267273
zend_string* php_openssl_x509_fingerprint(X509 *peer, const char *method, bool raw);
268274

269-
int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension);
275+
int openssl_x509v3_subjectAltName(BIO *bio, PHP_OPENSSL_X509_EXTENSION *extension);
270276

271277
STACK_OF(X509) *php_openssl_load_all_certs_from_file(
272278
char *cert_file, size_t cert_file_len, uint32_t arg_num);

‎ext/openssl/tests/bug28382.phpt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ openssl
66
<?php
77
$cert = file_get_contents(__DIR__ . "/bug28382cert.txt");
88
$ext = openssl_x509_parse($cert);
9+
10+
if (OPENSSL_VERSION_NUMBER >= 0x40000000) {
11+
$extensions = &$ext['extensions'];
12+
$extensions['crlDistributionPoints'] = preg_replace('/^Full Name:\R\s*/', '', trim($extensions['crlDistributionPoints']));
13+
}
14+
915
var_dump($ext['extensions']);
1016
/*
1117
* The reason for %A at the end of crlDistributionPoints and authorityKeyIdentifier is that
@@ -30,7 +36,7 @@ array(11) {
3036
string(59) "B0:A7:FF:F9:41:15:DE:23:39:BD:DD:31:0F:97:A0:B2:A2:74:E0:FC"
3137
["authorityKeyIdentifier"]=>
3238
string(%d) "DirName:/C=RO/ST=Romania/L=Craiova/O=Sergiu/OU=Sergiu SRL/CN=Sergiu CA/emailAddress=n_sergiu@hotmail.com
33-
serial:00%A"
39+
serial:%A"
3440
["keyUsage"]=>
3541
string(71) "Digital Signature, Non Repudiation, Key Encipherment, Data Encipherment"
3642
["nsBaseUrl"]=>

‎ext/openssl/tests/ecc_custom_params.phpt‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@ openssl
55
--SKIPIF--
66
<?php
77
if (!defined("OPENSSL_KEYTYPE_EC")) die("skip EC disabled");
8+
9+
if (OPENSSL_VERSION_NUMBER >= 0x40000000) {
10+
$d = hex2bin('8D0AC65AAEA0D6B96254C65817D4A143A9E7A03876F1A37D');
11+
$p = hex2bin('BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F');
12+
$a = hex2bin('BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985');
13+
$b = hex2bin('1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1');
14+
$g_x = hex2bin('4AD5F7048DE709AD51236DE65E4D4B482C836DC6E4106640');
15+
$g_y = hex2bin('02BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2');
16+
$order = hex2bin('BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677');
17+
18+
if (@openssl_pkey_new(array(
19+
'ec' => array(
20+
'p' => $p,
21+
'a' => $a,
22+
'b' => $b,
23+
'order' => $order,
24+
'g_x' => $g_x,
25+
'g_y' => $g_y,
26+
'd' => $d,
27+
),
28+
)) === false) {
29+
die("skip EC custom params unsupported with OpenSSL 4");
30+
}
31+
}
832
?>
933
--FILE--
1034
<?php

‎ext/openssl/tests/openssl_x509_parse_basic_openssl32.phpt‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@ if (OPENSSL_VERSION_NUMBER < 0x30200000) die('skip For OpenSSL >= 3.2');
88
?>
99
--FILE--
1010
<?php
11+
function normalize_openssl4_x509_parse_output(array $cert): array {
12+
if (OPENSSL_VERSION_NUMBER < 0x40000000 || !isset($cert['serialNumberHex'], $cert['extensions']['authorityKeyIdentifier'])) {
13+
return $cert;
14+
}
15+
16+
$serial = strtoupper($cert['serialNumberHex']);
17+
if (strlen($serial) % 2) {
18+
$serial = '0' . $serial;
19+
}
20+
$serial = implode(':', str_split($serial, 2));
21+
$cert['extensions']['authorityKeyIdentifier'] = preg_replace('/^serial:\d+$/m', 'serial:' . $serial, $cert['extensions']['authorityKeyIdentifier']);
22+
23+
return $cert;
24+
}
25+
1126
$cert = "file://" . __DIR__ . "/cert.crt";
1227

1328
$parsedCert = openssl_x509_parse($cert);
14-
var_dump($parsedCert === openssl_x509_parse(openssl_x509_read($cert)));
29+
$parsedCert = normalize_openssl4_x509_parse_output($parsedCert);
30+
var_dump($parsedCert === normalize_openssl4_x509_parse_output(openssl_x509_parse(openssl_x509_read($cert))));
1531
var_dump($parsedCert);
16-
var_dump(openssl_x509_parse($cert, false));
32+
var_dump(normalize_openssl4_x509_parse_output(openssl_x509_parse($cert, false)));
1733
?>
1834
--EXPECTF--
1935
bool(true)

‎ext/openssl/xp_ssl.c‎

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
#define HAVE_SEC_LEVEL 1
9696
#endif
9797

98-
#ifndef OPENSSL_NO_SSL3
98+
#if OPENSSL_VERSION_NUMBER < 0x40000000L && !defined(OPENSSL_NO_SSL3)
9999
#define HAVE_SSL3 1
100100
#define PHP_OPENSSL_MIN_PROTO_VERSION STREAM_CRYPTO_METHOD_SSLv3
101101
#else
@@ -542,26 +542,53 @@ static bool php_openssl_matches_san_list(
542542

543543
static bool php_openssl_matches_common_name(X509 *peer, const char *subject_name) /* {{{ */
544544
{
545-
char buf[1024];
546-
X509_NAME *cert_name;
545+
unsigned char *cert_name = NULL;
546+
#if PHP_OPENSSL_API_VERSION < 0x30000
547+
X509_NAME *name;
548+
#else
549+
const X509_NAME *name;
550+
#endif
551+
const X509_NAME_ENTRY *name_entry;
552+
const ASN1_STRING *name_asn1;
547553
bool is_match = 0;
554+
int name_index;
548555
int cert_name_len;
549556

550-
cert_name = X509_get_subject_name(peer);
551-
cert_name_len = X509_NAME_get_text_by_NID(cert_name, NID_commonName, buf, sizeof(buf));
557+
name = X509_get_subject_name(peer);
558+
name_index = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
559+
if (name_index == -1) {
560+
php_error_docref(NULL, E_WARNING, "Unable to locate peer certificate CN");
561+
return false;
562+
}
552563

553-
if (cert_name_len == -1) {
564+
name_entry = X509_NAME_get_entry(name, name_index);
565+
if (name_entry == NULL) {
554566
php_error_docref(NULL, E_WARNING, "Unable to locate peer certificate CN");
555-
} else if ((size_t)cert_name_len != strlen(buf)) {
556-
php_error_docref(NULL, E_WARNING, "Peer certificate CN=`%.*s' is malformed", cert_name_len, buf);
557-
} else if (php_openssl_matches_wildcard_name(subject_name, buf)) {
567+
return false;
568+
}
569+
570+
name_asn1 = X509_NAME_ENTRY_get_data(name_entry);
571+
cert_name_len = ASN1_STRING_length(name_asn1);
572+
cert_name = (unsigned char *) OPENSSL_strndup((const char *) ASN1_STRING_get0_data(name_asn1), cert_name_len);
573+
if (cert_name == NULL) {
574+
php_error_docref(NULL, E_WARNING, "Unable to locate peer certificate CN");
575+
return false;
576+
}
577+
578+
if ((size_t)cert_name_len != strlen((const char *)cert_name)) {
579+
php_error_docref(NULL, E_WARNING, "Peer certificate CN=`%.*s' is malformed", cert_name_len, (const char *)cert_name);
580+
return false;
581+
} else if (php_openssl_matches_wildcard_name(subject_name, (const char *)cert_name)) {
558582
is_match = 1;
559583
} else {
560584
php_error_docref(NULL, E_WARNING,
561585
"Peer certificate CN=`%.*s' did not match expected CN=`%s'",
562-
cert_name_len, buf, subject_name);
586+
cert_name_len, (const char *)cert_name, subject_name);
587+
return false;
563588
}
564589

590+
OPENSSL_free(cert_name);
591+
565592
return is_match;
566593
}
567594
/* }}} */

0 commit comments

Comments
 (0)