Skip to content

Commit c3c4d34

Browse files
committed
HSM: openssl_pkey_get_public(URI) support
Add RFC7512 URIs with openssl_pkey_get_public()
1 parent 1f6ca41 commit c3c4d34

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

ext/openssl/openssl.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3550,6 +3550,8 @@ static EVP_PKEY *php_openssl_pkey_from_zval(zval *val, int public_key, char *pas
35503550
} else if (Z_TYPE_P(val) == IS_OBJECT && Z_OBJCE_P(val) == php_openssl_certificate_ce) {
35513551
cert = php_openssl_certificate_from_obj(Z_OBJ_P(val))->x509;
35523552
} else {
3553+
ENGINE *engine = NULL;
3554+
35533555
/* force it to be a string and check if it refers to a file */
35543556
/* passing non string values leaks, object uses toString, it returns NULL
35553557
* See bug38255.phpt
@@ -3567,13 +3569,43 @@ static EVP_PKEY *php_openssl_pkey_from_zval(zval *val, int public_key, char *pas
35673569
TMP_CLEAN;
35683570
}
35693571
}
3572+
if (Z_STRLEN_P(val) > 7 && memcmp(Z_STRVAL_P(val), "pkcs11:", sizeof("pkcs11:") - 1) == 0) {
3573+
char *verbose = NULL;
3574+
engine = ENGINE_by_id("pkcs11");
3575+
if (engine == NULL) {
3576+
php_error_docref(NULL, E_WARNING, "Cannot load PKCS11 engine");
3577+
TMP_CLEAN;
3578+
}
3579+
verbose = getenv("OPENSSL_ENGINE_VERBOSE");
3580+
if (verbose) {
3581+
if (!ENGINE_ctrl_cmd_string(engine, "VERBOSE", NULL, 0)) {
3582+
ENGINE_free(engine);
3583+
TMP_CLEAN;
3584+
}
3585+
}
3586+
if (!ENGINE_init(engine)) {
3587+
ENGINE_free(engine);
3588+
ENGINE_finish(engine);
3589+
engine = NULL;
3590+
php_error_docref(NULL, E_WARNING, "Cannot init PKCS11 engine");
3591+
TMP_CLEAN;
3592+
}
3593+
}
35703594
/* it's an X509 file/cert of some kind, and we need to extract the data from that */
35713595
if (public_key) {
3572-
cert = php_openssl_x509_from_str(Z_STR_P(val));
3596+
if (engine) {
3597+
key = ENGINE_load_public_key(engine, Z_STRVAL_P(val), NULL, NULL);
3598+
ENGINE_free(engine);
3599+
ENGINE_finish(engine);
3600+
engine = NULL;
3601+
}
3602+
/* val could be a certificate (file, pkcs11:, etc., let's try to extract the key */
3603+
if (!key)
3604+
cert = php_openssl_x509_from_str(Z_STR_P(val));
35733605

35743606
if (cert) {
35753607
free_cert = 1;
3576-
} else {
3608+
} else if (!key) {
35773609
/* not a X509 certificate, try to retrieve public key */
35783610
BIO* in;
35793611
if (filename) {

0 commit comments

Comments
 (0)