Skip to content

Commit 1a1306e

Browse files
committed
RFC7512 URI support
When a URI based on the RFC7512 is used, the private key should be loaded from the engine instead of using a local file.
1 parent 916071d commit 1a1306e

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

ext/openssl/xp_ssl.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <openssl/err.h>
3636
#include <openssl/bn.h>
3737
#include <openssl/dh.h>
38+
#include <openssl/engine.h>
3839

3940
#ifdef PHP_WIN32
4041
#include "win32/winutil.h"
@@ -950,7 +951,39 @@ static int php_openssl_set_local_cert(SSL_CTX *ctx, php_stream *stream) /* {{{ *
950951
}
951952
GET_VER_OPT_STRING("local_pk", private_key);
952953

953-
if (private_key) {
954+
if (private_key && (strncmp(private_key, "pkcs11:", 7) == 0)) {
955+
/* local_pk is a RFC7512 URI */
956+
ENGINE *engine;
957+
EVP_PKEY *pkey;
958+
engine = ENGINE_by_id("pkcs11");
959+
if (engine == NULL) {
960+
php_error_docref(NULL, E_WARNING, "Could not bind PKCS11 engine");
961+
return FAILURE;
962+
}
963+
#if 0
964+
if (!ENGINE_ctrl_cmd_string(engine, "VERBOSE", NULL, 0)) {
965+
ENGINE_free(engine);
966+
php_error_docref(NULL, E_WARNING, "PKCS11 engine VERBOSE error");
967+
return FAILURE;
968+
}
969+
#endif
970+
if (!ENGINE_init(engine)) {
971+
php_error_docref(NULL, E_WARNING, "Could not initialize PKCS11 engine");
972+
return FAILURE;
973+
}
974+
/* ENGINE_init() returned a functional reference, so free the structural reference from ENGINE_by_id(). */
975+
ENGINE_free(engine);
976+
pkey = ENGINE_load_private_key(engine, private_key, 0, 0);
977+
if (pkey == NULL) {
978+
php_error_docref(NULL, E_WARNING, "Could not load private key %s", private_key);
979+
return FAILURE;
980+
}
981+
ENGINE_finish(engine);
982+
if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1) {
983+
php_error_docref(NULL, E_WARNING, "Unable to use private key from the engines %s", private_key);
984+
return FAILURE;
985+
}
986+
} else if (private_key) {
954987
char resolved_path_buff_pk[MAXPATHLEN];
955988
if (VCWD_REALPATH(private_key, resolved_path_buff_pk)) {
956989
if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff_pk, SSL_FILETYPE_PEM) != 1) {

0 commit comments

Comments
 (0)