Skip to content

Commit 4ea00a9

Browse files
committed
HSM: RFC7512 URI support for pkey
When a URI based on the RFC7512 is used, the private key should be loaded from the engine instead of using a local file. Suggested-by: Jakub Zelenka <bukka@php.net> It would be better to add this to php_openssl_pkey_from_zval so it's available for other functions and it can also support ENGINE_load_public_key based on public_key argument. Then the val received from local_pk could be passed to php_openssl_pkey_from_zval. It will still require to keep VCWD_REALPATH(private_key, resolved_path_buff_pk) check before that as php_openssl_pkey_from_zval supports a file:// prefix only for path and otherwise it thinks that it's a PEM string. Think that this would be useful as it would add support for pkey objects as well.
1 parent 1d3f224 commit 4ea00a9

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

ext/openssl/xp_ssl.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,14 +949,20 @@ static int php_openssl_set_local_cert(SSL_CTX *ctx, php_stream *stream) /* {{{ *
949949
return FAILURE;
950950
}
951951
GET_VER_OPT_STRING("local_pk", private_key);
952-
953952
if (private_key) {
954953
char resolved_path_buff_pk[MAXPATHLEN];
955954
if (VCWD_REALPATH(private_key, resolved_path_buff_pk)) {
956955
if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff_pk, SSL_FILETYPE_PEM) != 1) {
957956
php_error_docref(NULL, E_WARNING, "Unable to set private key file `%s'", resolved_path_buff_pk);
958957
return FAILURE;
959958
}
959+
} else if (GET_VER_OPT("local_pk")) /* fill val with local_pk if any */ {
960+
EVP_PKEY *pkey = php_openssl_pkey_from_zval(val /* local_pk */, 0/* not public */, NULL, 0);
961+
if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1) {
962+
EVP_PKEY_free(pkey);
963+
php_error_docref(NULL, E_WARNING, "Unable to set private key `%s'", private_key);
964+
return FAILURE;
965+
}
960966
}
961967
} else {
962968
if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff, SSL_FILETYPE_PEM) != 1) {

0 commit comments

Comments
 (0)