Skip to content

Commit ad1f4e1

Browse files
Fix GH-18988: Check unchecked error-prone calls in ext/ssl
1 parent cd568cb commit ad1f4e1

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.4.21
44

5+
- OpenSSL:
6+
. Fixed bug GH-18988 (Check various unchecked error-prone OpenSSL function
7+
return values). (alexandre-daubois)
58

69
09 Apr 2026, PHP 8.4.20
710

ext/openssl/openssl.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,9 @@ static int php_openssl_parse_config(struct php_x509_request * req, zval * option
971971
if (str != NULL && php_openssl_check_path_ex(str, strlen(str), path, 0, false, false, "oid_file")) {
972972
BIO *oid_bio = BIO_new_file(path, PHP_OPENSSL_BIO_MODE_R(PKCS7_BINARY));
973973
if (oid_bio) {
974-
OBJ_create_objects(oid_bio);
974+
if (OBJ_create_objects(oid_bio) == 0) {
975+
php_openssl_store_errors();
976+
}
975977
BIO_free(oid_bio);
976978
php_openssl_store_errors();
977979
}
@@ -1299,7 +1301,10 @@ PHP_MINIT_FUNCTION(openssl)
12991301
OSSL_PROVIDER_load(NULL, "legacy");
13001302
OSSL_PROVIDER_load(NULL, "default");
13011303
#endif
1302-
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
1304+
if (OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL) != 1) {
1305+
php_error_docref(NULL, E_WARNING, "Failed to initialize OpenSSL");
1306+
return FAILURE;
1307+
}
13031308
#endif
13041309

13051310
/* register a resource id number with OpenSSL so that we can map SSL -> stream structures in
@@ -2064,22 +2069,34 @@ static int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
20642069
name = sk_GENERAL_NAME_value(names, i);
20652070
switch (name->type) {
20662071
case GEN_EMAIL:
2067-
BIO_puts(bio, "email:");
2072+
if (BIO_puts(bio, "email:") < 0) {
2073+
php_openssl_store_errors();
2074+
}
20682075
as = name->d.rfc822Name;
2069-
BIO_write(bio, ASN1_STRING_get0_data(as),
2070-
ASN1_STRING_length(as));
2076+
if (BIO_write(bio, ASN1_STRING_get0_data(as),
2077+
ASN1_STRING_length(as)) < 0) {
2078+
php_openssl_store_errors();
2079+
}
20712080
break;
20722081
case GEN_DNS:
2073-
BIO_puts(bio, "DNS:");
2082+
if (BIO_puts(bio, "DNS:") < 0) {
2083+
php_openssl_store_errors();
2084+
}
20742085
as = name->d.dNSName;
2075-
BIO_write(bio, ASN1_STRING_get0_data(as),
2076-
ASN1_STRING_length(as));
2086+
if (BIO_write(bio, ASN1_STRING_get0_data(as),
2087+
ASN1_STRING_length(as)) < 0) {
2088+
php_openssl_store_errors();
2089+
}
20772090
break;
20782091
case GEN_URI:
2079-
BIO_puts(bio, "URI:");
2092+
if (BIO_puts(bio, "URI:") < 0) {
2093+
php_openssl_store_errors();
2094+
}
20802095
as = name->d.uniformResourceIdentifier;
2081-
BIO_write(bio, ASN1_STRING_get0_data(as),
2082-
ASN1_STRING_length(as));
2096+
if (BIO_write(bio, ASN1_STRING_get0_data(as),
2097+
ASN1_STRING_length(as)) < 0) {
2098+
php_openssl_store_errors();
2099+
}
20832100
break;
20842101
default:
20852102
/* use builtin print for GEN_OTHERNAME, GEN_X400,
@@ -2317,7 +2334,10 @@ static STACK_OF(X509) *php_openssl_load_all_certs_from_file(
23172334
while (sk_X509_INFO_num(sk)) {
23182335
xi=sk_X509_INFO_shift(sk);
23192336
if (xi->x509 != NULL) {
2320-
sk_X509_push(stack,xi->x509);
2337+
if (sk_X509_push(stack,xi->x509) == 0) {
2338+
php_openssl_store_errors();
2339+
X509_free(xi->x509);
2340+
}
23212341
xi->x509=NULL;
23222342
}
23232343
X509_INFO_free(xi);
@@ -2582,6 +2602,7 @@ static STACK_OF(X509) *php_array_to_X509_sk(zval * zcerts, uint32_t arg_num, con
25822602

25832603
}
25842604
if (sk_X509_push(sk, cert) <= 0) {
2605+
php_openssl_store_errors();
25852606
X509_free(cert);
25862607
goto push_fail_exit;
25872608
}
@@ -2603,6 +2624,7 @@ static STACK_OF(X509) *php_array_to_X509_sk(zval * zcerts, uint32_t arg_num, con
26032624
}
26042625
}
26052626
if (sk_X509_push(sk, cert) <= 0) {
2627+
php_openssl_store_errors();
26062628
X509_free(cert);
26072629
goto push_fail_exit;
26082630
}
@@ -3381,7 +3403,10 @@ PHP_FUNCTION(openssl_csr_sign)
33813403
PHP_OPENSSL_ASN1_INTEGER_set(X509_get_serialNumber(new_cert), serial);
33823404
}
33833405

3384-
X509_set_subject_name(new_cert, X509_REQ_get_subject_name(csr));
3406+
if (!X509_set_subject_name(new_cert, X509_REQ_get_subject_name(csr))) {
3407+
php_openssl_store_errors();
3408+
goto cleanup;
3409+
}
33853410

33863411
if (cert == NULL) {
33873412
cert = new_cert;
@@ -5853,6 +5878,7 @@ PHP_FUNCTION(openssl_pkcs7_encrypt)
58535878
}
58545879
}
58555880
if (sk_X509_push(recipcerts, cert) <= 0) {
5881+
php_openssl_store_errors();
58565882
X509_free(cert);
58575883
goto clean_exit;
58585884
}
@@ -5877,6 +5903,7 @@ PHP_FUNCTION(openssl_pkcs7_encrypt)
58775903
}
58785904
}
58795905
if (sk_X509_push(recipcerts, cert) <= 0) {
5906+
php_openssl_store_errors();
58805907
X509_free(cert);
58815908
goto clean_exit;
58825909
}

ext/openssl/xp_ssl.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,9 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
17711771
return FAILURE;
17721772
}
17731773
if (sslsock->is_client) {
1774-
SSL_CTX_set_alpn_protos(sslsock->ctx, alpn, alpn_len);
1774+
if (SSL_CTX_set_alpn_protos(sslsock->ctx, alpn, alpn_len) != 0) {
1775+
php_openssl_store_errors();
1776+
}
17751777
} else {
17761778
sslsock->alpn_ctx.data = (unsigned char *) pestrndup((const char*)alpn, alpn_len, php_stream_is_persistent(stream));
17771779
sslsock->alpn_ctx.len = alpn_len;
@@ -1846,8 +1848,9 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
18461848
php_error_docref(NULL, E_WARNING, "Supplied session stream must be an SSL enabled stream");
18471849
} else if (((php_openssl_netstream_data_t*)cparam->inputs.session->abstract)->ssl_handle == NULL) {
18481850
php_error_docref(NULL, E_WARNING, "Supplied SSL session stream is not initialized");
1849-
} else {
1850-
SSL_copy_session_id(sslsock->ssl_handle, ((php_openssl_netstream_data_t*)cparam->inputs.session->abstract)->ssl_handle);
1851+
} else if (SSL_copy_session_id(sslsock->ssl_handle, ((php_openssl_netstream_data_t*)cparam->inputs.session->abstract)->ssl_handle) != 1) {
1852+
php_openssl_store_errors();
1853+
php_error_docref(NULL, E_WARNING, "Failed to copy SSL session");
18511854
}
18521855
}
18531856

0 commit comments

Comments
 (0)