35 #if defined(ENABLE_PKCS11) && defined(ENABLE_CRYPTO_OPENSSL)
41 #include <pkcs11-helper-1.0/pkcs11h-openssl.h>
43 #ifdef HAVE_XKEY_PROVIDER
44 static XKEY_EXTERNAL_SIGN_fn xkey_pkcs11h_sign;
46 #if PKCS11H_VERSION > ((1<<16) | (27<<8))
49 #define MD_TYPE(n) {NID_sha ## n, CKM_SHA ## n, CKG_MGF1_SHA ## n}
55 } mdtypes[] = {MD_TYPE(224), MD_TYPE(256), MD_TYPE(384), MD_TYPE(512),
56 {NID_sha1, CKM_SHA_1, CKG_MGF1_SHA1},
65 set_pss_params(CK_RSA_PKCS_PSS_PARAMS *pss_params, XKEY_SIGALG sigalg,
66 pkcs11h_certificate_t cert)
70 EVP_PKEY *pubkey = NULL;
72 if ((x509 = pkcs11h_openssl_getX509(cert)) == NULL
73 || (pubkey = X509_get0_pubkey(x509)) == NULL)
75 msg(
M_WARN,
"PKCS#11: Unable get public key");
81 int nid = OBJ_sn2nid(sigalg.mdname);
82 while (mdtypes[i].nid != NID_undef && mdtypes[i].nid != nid)
86 pss_params->hashAlg = mdtypes[i].ckm_id;
87 pss_params->mgf = mdtypes[i].mgf_id;
90 const EVP_MD *md = EVP_get_digestbyname(sigalg.mdname);
93 msg(
M_WARN,
"WARN: set_pss_params: EVP_get_digestbyname returned NULL "
94 "for mdname = <%s>", sigalg.mdname);
97 int mdsize = EVP_MD_get_size(md);
100 if (!strcmp(sigalg.saltlen,
"digest"))
104 else if (!strcmp(sigalg.saltlen,
"max"))
106 saltlen = xkey_max_saltlen(EVP_PKEY_get_bits(pubkey), mdsize);
109 if (saltlen < 0 || pss_params->hashAlg == 0)
111 msg(
M_WARN,
"WARN: invalid RSA_PKCS1_PSS parameters: saltlen = <%s> "
112 "mdname = <%s>.", sigalg.saltlen, sigalg.mdname);
115 pss_params->sLen = (
unsigned long) saltlen;
117 msg(
D_XKEY,
"set_pss_params: sLen = %lu, hashAlg = %lu, mgf = %lu",
118 pss_params->sLen, pss_params->hashAlg, pss_params->mgf);
133 #define set_pss_params(...) (1)
141 pkcs11h_certificate_signAny_ex(
const pkcs11h_certificate_t cert,
142 const CK_MECHANISM *mech,
const unsigned char *tbs,
143 size_t tbslen,
unsigned char *sig,
size_t *siglen)
145 if (mech->mechanism == CKM_RSA_PKCS_PSS)
147 msg(
M_NONFATAL,
"PKCS#11: Error: PSS padding is not supported by "
148 "this version of pkcs11-helper library.");
149 return CKR_MECHANISM_INVALID;
151 return pkcs11h_certificate_signAny(cert, mech->mechanism, tbs, tbslen, sig, siglen);
161 xkey_pkcs11h_sign(
void *handle,
unsigned char *sig,
162 size_t *siglen,
const unsigned char *tbs,
size_t tbslen, XKEY_SIGALG sigalg)
164 pkcs11h_certificate_t cert = handle;
165 CK_MECHANISM mech = {CKM_RSA_PKCS, NULL, 0};
166 CK_RSA_PKCS_PSS_PARAMS pss_params = {0};
168 unsigned char buf[EVP_MAX_MD_SIZE];
170 size_t siglen_max = *siglen;
172 unsigned char enc[EVP_MAX_MD_SIZE + 32];
173 size_t enc_len =
sizeof(enc);
175 if (!strcmp(sigalg.op,
"DigestSign"))
177 msg(
D_XKEY,
"xkey_pkcs11h_sign: computing digest");
178 if (xkey_digest(tbs, tbslen, buf, &buflen, sigalg.mdname))
181 tbslen = (size_t) buflen;
190 if (!strcmp(sigalg.keytype,
"EC"))
192 msg(
D_XKEY,
"xkey_pkcs11h_sign: signing with EC key");
193 mech.mechanism = CKM_ECDSA;
195 else if (!strcmp(sigalg.keytype,
"RSA"))
197 msg(
D_XKEY,
"xkey_pkcs11h_sign: signing with RSA key: padmode = %s",
199 if (!strcmp(sigalg.padmode,
"none"))
201 mech.mechanism = CKM_RSA_X_509;
203 else if (!strcmp(sigalg.padmode,
"pss"))
205 mech.mechanism = CKM_RSA_PKCS_PSS;
207 if (!set_pss_params(&pss_params, sigalg, cert))
212 mech.pParameter = &pss_params;
213 mech.ulParameterLen =
sizeof(pss_params);
215 else if (!strcmp(sigalg.padmode,
"pkcs1"))
219 if (!encode_pkcs1(enc, &enc_len, sigalg.mdname, tbs, tbslen))
228 msg(
M_WARN,
"PKCS#11: Unknown padmode <%s>", sigalg.padmode);
236 if (CKR_OK != pkcs11h_certificate_signAny_ex(cert, &mech,
237 tbs, tbslen, sig, siglen))
241 if (strcmp(sigalg.keytype,
"EC"))
247 int derlen = ecdsa_bin2der(sig, (
int) *siglen, siglen_max);
260 xkey_handle_free(
void *handle)
262 pkcs11h_certificate_freeCertificate(handle);
277 xkey_load_from_pkcs11h(pkcs11h_certificate_t certificate,
282 X509 *x509 = pkcs11h_openssl_getX509(certificate);
285 msg(
M_WARN,
"PKCS#11: Unable get x509 certificate object");
289 EVP_PKEY *pubkey = X509_get0_pubkey(x509);
291 XKEY_PRIVKEY_FREE_fn *free_op = xkey_handle_free;
292 XKEY_EXTERNAL_SIGN_fn *sign_op = xkey_pkcs11h_sign;
294 EVP_PKEY *pkey = xkey_load_generic_key(
tls_libctx, certificate, pubkey, sign_op, free_op);
297 msg(
M_WARN,
"PKCS#11: Failed to load private key into xkey provider");
303 if (!SSL_CTX_use_cert_and_key(ctx->
ctx, x509, pkey, NULL, 0))
306 msg(
M_FATAL,
"PKCS#11: Failed to set cert and private key for OpenSSL");
322 pkcs11h_certificate_freeCertificate(certificate);
329 pkcs11_init_tls_session(pkcs11h_certificate_t certificate,
333 #ifdef HAVE_XKEY_PROVIDER
334 return (xkey_load_from_pkcs11h(certificate, ssl_ctx) == 0);
339 EVP_PKEY *evp = NULL;
340 pkcs11h_openssl_session_t openssl_session = NULL;
342 if ((openssl_session = pkcs11h_openssl_createSession(certificate)) == NULL)
344 msg(
M_WARN,
"PKCS#11: Cannot initialize openssl session");
353 if ((evp = pkcs11h_openssl_session_getEVP(openssl_session)) == NULL)
355 msg(
M_WARN,
"PKCS#11: Unable get evp object");
359 if ((x509 = pkcs11h_openssl_session_getX509(openssl_session)) == NULL)
361 msg(
M_WARN,
"PKCS#11: Unable get certificate object");
365 if (!SSL_CTX_use_PrivateKey(ssl_ctx->
ctx, evp))
367 msg(
M_WARN,
"PKCS#11: Cannot set private key for openssl");
371 if (!SSL_CTX_use_certificate(ssl_ctx->
ctx, x509))
374 msg(
M_FATAL,
"PKCS#11: Cannot set certificate for openssl");
384 if (certificate != NULL)
386 pkcs11h_certificate_freeCertificate(certificate);
400 if (openssl_session != NULL)
402 pkcs11h_openssl_freeSession(openssl_session);
403 openssl_session = NULL;
410 pkcs11_certificate_dn(pkcs11h_certificate_t certificate,
struct gc_arena *gc)
416 if ((x509 = pkcs11h_openssl_getX509(certificate)) == NULL)
432 pkcs11_certificate_serial(pkcs11h_certificate_t certificate,
char *serial,
440 if ((x509 = pkcs11h_openssl_getX509(certificate)) == NULL)
446 if ((bio = BIO_new(BIO_s_mem())) == NULL)
452 i2a_ASN1_INTEGER(bio, X509_get_serialNumber(x509));
453 n = BIO_read(bio, serial, serial_len-1);