Skip to content

Commit 840f27d

Browse files
committed
HSM: openssl_x509_read() get from URI
Let's read a certificate from a URI.
1 parent 4ea00a9 commit 840f27d

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

ext/openssl/openssl.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ PHP_FUNCTION(openssl_get_cert_locations)
13861386

13871387
static X509 *php_openssl_x509_from_str(zend_string *cert_str) {
13881388
X509 *cert = NULL;
1389-
BIO *in;
1389+
BIO *in = NULL;
13901390

13911391
if (ZSTR_LEN(cert_str) > 7 && memcmp(ZSTR_VAL(cert_str), "file://", sizeof("file://") - 1) == 0) {
13921392
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) {
13991399
return NULL;
14001400
}
14011401
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;
14021444
} else {
14031445
in = BIO_new_mem_buf(ZSTR_VAL(cert_str), (int) ZSTR_LEN(cert_str));
14041446
if (in == NULL) {
@@ -1412,7 +1454,7 @@ static X509 *php_openssl_x509_from_str(zend_string *cert_str) {
14121454
#endif
14131455
}
14141456

1415-
if (!BIO_free(in)) {
1457+
if (in && !BIO_free(in)) {
14161458
php_openssl_store_errors();
14171459
}
14181460

0 commit comments

Comments
 (0)