|
35 | 35 | #include <openssl/err.h>
|
36 | 36 | #include <openssl/bn.h>
|
37 | 37 | #include <openssl/dh.h>
|
| 38 | +#include <openssl/engine.h> |
38 | 39 |
|
39 | 40 | #ifdef PHP_WIN32
|
40 | 41 | #include "win32/winutil.h"
|
@@ -950,7 +951,39 @@ static int php_openssl_set_local_cert(SSL_CTX *ctx, php_stream *stream) /* {{{ *
|
950 | 951 | }
|
951 | 952 | GET_VER_OPT_STRING("local_pk", private_key);
|
952 | 953 |
|
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) { |
954 | 987 | char resolved_path_buff_pk[MAXPATHLEN];
|
955 | 988 | if (VCWD_REALPATH(private_key, resolved_path_buff_pk)) {
|
956 | 989 | if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff_pk, SSL_FILETYPE_PEM) != 1) {
|
|
0 commit comments