@@ -1386,7 +1386,7 @@ PHP_FUNCTION(openssl_get_cert_locations)
1386
1386
1387
1387
static X509 * php_openssl_x509_from_str (zend_string * cert_str ) {
1388
1388
X509 * cert = NULL ;
1389
- BIO * in ;
1389
+ BIO * in = NULL ;
1390
1390
1391
1391
if (ZSTR_LEN (cert_str ) > 7 && memcmp (ZSTR_VAL (cert_str ), "file://" , sizeof ("file://" ) - 1 ) == 0 ) {
1392
1392
if (php_openssl_open_base_dir_chk (ZSTR_VAL (cert_str ) + (sizeof ("file://" ) - 1 ))) {
@@ -1399,6 +1399,48 @@ static X509 *php_openssl_x509_from_str(zend_string *cert_str) {
1399
1399
return NULL ;
1400
1400
}
1401
1401
cert = PEM_read_bio_X509 (in , NULL , NULL , NULL );
1402
+ } else if (ZSTR_LEN (cert_str ) > 7 && memcmp (ZSTR_VAL (cert_str ), "pkcs11:" , sizeof ("pkcs11:" ) - 1 ) == 0 ) {
1403
+ char * verbose = NULL ;
1404
+ ENGINE * engine ;
1405
+ struct {
1406
+ const char * s_slot_cert_id ;
1407
+ X509 * cert ;
1408
+ } parms = {
1409
+ .s_slot_cert_id = ZSTR_VAL (cert_str ),
1410
+ .cert = NULL ,
1411
+ };
1412
+ int force_login = 0 ;
1413
+
1414
+ engine = ENGINE_by_id ("pkcs11" );
1415
+ if (engine == NULL ) {
1416
+ php_openssl_store_errors ();
1417
+ return NULL ;
1418
+ }
1419
+ verbose = getenv ("OPENSSL_ENGINE_VERBOSE" );
1420
+ if (verbose ) {
1421
+ if (!ENGINE_ctrl_cmd_string (engine , "VERBOSE" , NULL , 0 )) {
1422
+ ENGINE_free (engine );
1423
+ php_openssl_store_errors ();
1424
+ return NULL ;
1425
+ }
1426
+ }
1427
+ if (!ENGINE_init (engine )) {
1428
+ ENGINE_free (engine );
1429
+ php_openssl_store_errors ();
1430
+ return NULL ;
1431
+ }
1432
+ if (!ENGINE_ctrl_cmd (engine , "LOAD_CERT_CTRL" , 0 , & parms , NULL , force_login )) {
1433
+ ENGINE_free (engine );
1434
+ php_openssl_store_errors ();
1435
+ return NULL ;
1436
+ }
1437
+ ENGINE_free (engine );
1438
+ ENGINE_finish (engine );
1439
+ if (parms .cert == NULL ) {
1440
+ php_openssl_store_errors ();
1441
+ return NULL ;
1442
+ }
1443
+ cert = parms .cert ;
1402
1444
} else {
1403
1445
in = BIO_new_mem_buf (ZSTR_VAL (cert_str ), (int ) ZSTR_LEN (cert_str ));
1404
1446
if (in == NULL ) {
@@ -1412,7 +1454,7 @@ static X509 *php_openssl_x509_from_str(zend_string *cert_str) {
1412
1454
#endif
1413
1455
}
1414
1456
1415
- if (!BIO_free (in )) {
1457
+ if (in && !BIO_free (in )) {
1416
1458
php_openssl_store_errors ();
1417
1459
}
1418
1460
0 commit comments