Skip to content

Commit e37f1a7

Browse files
bbockelmh2zh
authored andcommitted
When using a pkcs11:-style URL, load via the engine API
With this, the following works: ``` xrd.tls /home/foo/.config/certificates/tls.crt \ pkcs11:token=test-token;object=priv_key;type=public?pin-value=1234 ```
1 parent 9526add commit e37f1a7

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Xrd/XrdConfig.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2424,7 +2424,7 @@ int XrdConfig::xtls(XrdSysError *eDest, XrdOucStream &Config)
24242424

24252425
if (!(val = Config.GetWord())) return 0;
24262426

2427-
if (*val == '/')
2427+
if (*val == '/' || !strncmp(val, "pkcs11:", 7))
24282428
{tlsKey = strdup(val);
24292429
if (!(val = Config.GetWord())) return 0;
24302430
}

src/XrdTls/XrdTlsContext.cc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <cstdio>
2020
#include <openssl/bio.h>
2121
#include <openssl/crypto.h>
22+
#include <openssl/engine.h>
2223
#include <openssl/err.h>
2324
#include <openssl/ssl.h>
2425
#include <openssl/opensslv.h>
@@ -510,7 +511,7 @@ bool VerPaths(const char *cert, const char *pkey,
510511
// If a private key is present than make sure it's a file and only the
511512
// owner has access to it.
512513
//
513-
if (pkey && (emsg = XrdOucUtils::ValPath(pkey, pkey_mode, false)))
514+
if (pkey && pkey[0] == '/' && (emsg = XrdOucUtils::ValPath(pkey, pkey_mode, false)))
514515
{eMsg = "Unable to use key file ";
515516
eMsg += pkey; eMsg += "; "; eMsg += emsg;
516517
return false;
@@ -766,7 +767,27 @@ XrdTlsContext::XrdTlsContext(const char *cert, const char *key,
766767

767768
// Load the private key
768769
//
769-
if (SSL_CTX_use_PrivateKey_file(pImpl->ctx, key, SSL_FILETYPE_PEM) != 1 )
770+
if (key[0] == 'p') {
771+
772+
ENGINE *e = ENGINE_by_id("pkcs11");
773+
if (e) {
774+
if(!ENGINE_init(e)) {
775+
ENGINE_free(e);
776+
FATAL_SSL("Unable to initialize pkcs11 engine");
777+
}
778+
} else {
779+
FATAL_SSL("Unable to create pkcs11 engine");
780+
}
781+
auto priv_key = ENGINE_load_private_key(e, key, nullptr, nullptr);
782+
783+
if (!priv_key) {
784+
FATAL_SSL("Failed to load private key through engine");
785+
}
786+
if (SSL_CTX_use_PrivateKey(pImpl->ctx, priv_key) != 1)
787+
FATAL_SSL("Failed to have SSL context use private key");
788+
EVP_PKEY_free(priv_key);
789+
790+
} else if (SSL_CTX_use_PrivateKey_file(pImpl->ctx, key, SSL_FILETYPE_PEM) != 1 )
770791
FATAL_SSL("Unable to create TLS context; invalid private key.");
771792

772793
// Make sure the key and certificate file match.

0 commit comments

Comments
 (0)