38 #ifdef ENABLE_CRYPTOAPI
40 #include <openssl/ssl.h>
41 #include <openssl/evp.h>
42 #include <openssl/err.h>
56 #ifndef HAVE_XKEY_PROVIDER
61 msg(
M_NONFATAL,
"ERROR: this binary was built without cryptoapicert support");
67 static XKEY_EXTERNAL_SIGN_fn xkey_cng_sign;
69 typedef struct _CAPI_DATA {
70 const CERT_CONTEXT *cert_context;
71 HCRYPTPROV_OR_NCRYPT_KEY_HANDLE crypt_prov;
83 static const wchar_t *
84 cng_hash_algo(
int md_type)
86 const wchar_t *alg = L
"UNKNOWN";
90 alg = BCRYPT_MD5_ALGORITHM;
94 alg = BCRYPT_SHA1_ALGORITHM;
98 alg = BCRYPT_SHA256_ALGORITHM;
102 alg = BCRYPT_SHA384_ALGORITHM;
106 alg = BCRYPT_SHA512_ALGORITHM;
115 msg(
M_WARN|
M_INFO,
"cryptoapicert: Unknown hash type NID=0x%x", md_type);
122 CAPI_DATA_free(CAPI_DATA *cd)
124 if (!cd || cd->ref_count-- > 0)
128 if (cd->free_crypt_prov && cd->crypt_prov)
130 if (cd->key_spec == CERT_NCRYPT_KEY_SPEC)
132 NCryptFreeObject(cd->crypt_prov);
136 CryptReleaseContext(cd->crypt_prov, 0);
139 if (cd->cert_context)
141 CertFreeCertificateContext(cd->cert_context);
143 EVP_PKEY_free(cd->pubkey);
157 parse_hexstring(
const char *p,
unsigned char *arr,
size_t capacity)
160 for ( ; *p && i < capacity; p += 2)
172 if (!isxdigit(p[0]) || !isxdigit(p[1])
173 || sscanf(p,
"%2hhx", &arr[i++]) != 1)
182 decode_object(
struct gc_arena *gc, LPCSTR struct_type,
183 const CRYPT_OBJID_BLOB *val, DWORD flags, DWORD *cb)
187 if (!CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, struct_type,
188 val->pbData, val->cbData, flags, NULL, cb))
195 if (!CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, struct_type,
196 val->pbData, val->cbData, flags, buf, cb))
204 static const CRYPT_OID_INFO *
205 find_oid(DWORD keytype,
const void *
key, DWORD groupid)
207 const CRYPT_OID_INFO *info = NULL;
210 info = CryptFindOIDInfo(keytype, (
void *)
key, groupid);
213 if (!info && groupid)
215 info = CryptFindOIDInfo(keytype, (
void *)
key, 0);
222 test_certificate_template(
const char *cert_prop,
const CERT_CONTEXT *cert_ctx)
224 const CERT_INFO *info = cert_ctx->pCertInfo;
225 const CERT_EXTENSION *ext;
229 const WCHAR *tmpl_name =
wide_string(cert_prop, &gc);
232 ext = CertFindExtension(szOID_CERTIFICATE_TEMPLATE, info->cExtension, info->rgExtension);
235 pvext = decode_object(&gc, X509_CERTIFICATE_TEMPLATE, &ext->Value, 0, &cbext);
236 if (pvext && cbext >=
sizeof(CERT_TEMPLATE_EXT))
238 const CERT_TEMPLATE_EXT *cte = (
const CERT_TEMPLATE_EXT *)pvext;
239 if (!stricmp(cert_prop, cte->pszObjId))
246 const CRYPT_OID_INFO *tmpl_oid = find_oid(CRYPT_OID_INFO_NAME_KEY, tmpl_name,
247 CRYPT_TEMPLATE_OID_GROUP_ID);
248 if (tmpl_oid && !stricmp(tmpl_oid->pszOID, cte->pszObjId))
262 static const CERT_CONTEXT *
263 find_certificate_in_store(
const char *cert_prop, HCERTSTORE cert_store)
273 const CERT_CONTEXT *rv = NULL;
275 const void *find_param;
276 unsigned char hash[255];
277 CRYPT_HASH_BLOB blob = {.cbData = 0, .pbData =
hash};
280 if (!strncmp(cert_prop,
"SUBJ:", 5))
284 find_type = CERT_FIND_SUBJECT_STR_W;
286 else if (!strncmp(cert_prop,
"ISSUER:", 7))
289 find_type = CERT_FIND_ISSUER_STR_W;
291 else if (!strncmp(cert_prop,
"THUMB:", 6))
293 find_type = CERT_FIND_HASH;
296 blob.cbData = parse_hexstring(cert_prop + 6,
hash,
sizeof(
hash));
297 if (blob.cbData == 0)
299 msg(
M_WARN|
M_INFO,
"WARNING: cryptoapicert: error parsing <%s>.", cert_prop);
303 else if (!strncmp(cert_prop,
"TMPL:", 5))
307 find_type = CERT_FIND_HAS_PRIVATE_KEY;
311 msg(
M_NONFATAL,
"Error in cryptoapicert: unsupported certificate specification <%s>", cert_prop);
319 rv = CertFindCertificateInStore(cert_store, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
320 0, find_type, find_param, rv);
326 if (find_type == CERT_FIND_HAS_PRIVATE_KEY
327 && !test_certificate_template(cert_prop, rv))
331 validity = CertVerifyTimeValidity(NULL, rv->pCertInfo);
336 msg(
M_WARN|
M_INFO,
"WARNING: cryptoapicert: ignoring certificate in store %s.",
337 validity < 0 ?
"not yet valid" :
"that has expired");
347 xkey_cng_ec_sign(CAPI_DATA *cd,
unsigned char *sig,
size_t *siglen,
const unsigned char *tbs,
352 msg(
D_LOW,
"Signing using NCryptSignHash with EC key");
354 DWORD
status = NCryptSignHash(cd->crypt_prov, NULL, (BYTE *)tbs, tbslen, sig, len, &len, 0);
356 if (
status != ERROR_SUCCESS)
364 int derlen = ecdsa_bin2der(sig, (
int) len, *siglen);
375 xkey_cng_rsa_sign(CAPI_DATA *cd,
unsigned char *sig,
size_t *siglen,
const unsigned char *tbs,
376 size_t tbslen, XKEY_SIGALG sigalg)
384 DWORD
status = ERROR_SUCCESS;
387 const wchar_t *hashalg = cng_hash_algo(OBJ_sn2nid(sigalg.mdname));
389 if (hashalg && wcscmp(hashalg, L
"UNKNOWN") == 0)
391 msg(
M_NONFATAL,
"Error in cryptoapicert: Unknown hash name <%s>", sigalg.mdname);
395 if (!strcmp(sigalg.padmode,
"pkcs1"))
397 msg(
D_LOW,
"Signing using NCryptSignHash with PKCS1 padding: hashalg <%s>", sigalg.mdname);
399 BCRYPT_PKCS1_PADDING_INFO padinfo = {hashalg};
400 status = NCryptSignHash(cd->crypt_prov, &padinfo, (BYTE *)tbs, (DWORD)tbslen,
401 sig, (DWORD)*siglen, &len, BCRYPT_PAD_PKCS1);
403 else if (!strcmp(sigalg.padmode,
"pss"))
405 int saltlen = tbslen;
406 if (!strcmp(sigalg.saltlen,
"max"))
408 saltlen = xkey_max_saltlen(EVP_PKEY_bits(cd->pubkey), tbslen);
411 msg(
M_NONFATAL,
"Error in cryptoapicert: invalid salt length (%d)", saltlen);
416 msg(
D_LOW,
"Signing using NCryptSignHash with PSS padding: hashalg <%s>, saltlen <%d>",
417 sigalg.mdname, saltlen);
419 BCRYPT_PSS_PADDING_INFO padinfo = {hashalg, (DWORD) saltlen};
420 status = NCryptSignHash(cd->crypt_prov, &padinfo, (BYTE *)tbs, (DWORD) tbslen,
421 sig, (DWORD)*siglen, &len, BCRYPT_PAD_PSS);
425 msg(
M_NONFATAL,
"Error in cryptoapicert: Unsupported padding mode <%s>", sigalg.padmode);
429 if (
status != ERROR_SUCCESS)
437 return (*siglen > 0);
442 xkey_cng_sign(
void *handle,
unsigned char *sig,
size_t *siglen,
const unsigned char *tbs,
443 size_t tbslen, XKEY_SIGALG sigalg)
447 CAPI_DATA *cd = handle;
452 unsigned char mdbuf[EVP_MAX_MD_SIZE];
453 size_t buflen = _countof(mdbuf);
456 if (!strcmp(sigalg.op,
"DigestSign"))
458 if (!xkey_digest(tbs, tbslen, mdbuf, &buflen, sigalg.mdname))
466 if (!strcmp(sigalg.keytype,
"EC"))
468 return xkey_cng_ec_sign(cd, sig, siglen, tbs, tbslen);
470 else if (!strcmp(sigalg.keytype,
"RSA"))
472 return xkey_cng_rsa_sign(cd, sig, siglen, tbs, tbslen, sigalg);
481 get_cert_name(
const CERT_CONTEXT *cc,
struct gc_arena *gc)
483 DWORD len = CertGetNameStringW(cc, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0);
487 wchar_t *wname =
gc_malloc(len*
sizeof(
wchar_t),
false, gc);
489 || CertGetNameStringW(cc, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, wname, len) == 0)
505 Load_CryptoAPI_certificate(
const char *cert_prop, X509 **cert, EVP_PKEY **privkey)
509 CAPI_DATA *cd = calloc(1,
sizeof(*cd));
518 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_CURRENT_USER
519 |CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG, L
"MY");
525 cd->cert_context = find_certificate_in_store(cert_prop, cs);
526 CertCloseStore(cs, 0);
527 if (!cd->cert_context)
529 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE
530 |CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG, L
"MY");
536 cd->cert_context = find_certificate_in_store(cert_prop, cs);
537 CertCloseStore(cs, 0);
538 if (cd->cert_context == NULL)
540 msg(
M_NONFATAL,
"Error in cryptoapicert: certificate matching <%s> not found", cert_prop);
546 char *cert_name = get_cert_name(cd->cert_context, &gc);
549 msg(
D_LOW,
"cryptapicert: using certificate with name <%s>", cert_name);
553 *cert = d2i_X509(NULL, (
const unsigned char **) &cd->cert_context->pbCertEncoded,
554 cd->cert_context->cbCertEncoded);
557 msg(
M_NONFATAL,
"Error in cryptoapicert: X509 certificate decode failed");
563 DWORD flags = CRYPT_ACQUIRE_COMPARE_KEY_FLAG
564 | CRYPT_ACQUIRE_ONLY_NCRYPT_KEY_FLAG;
565 if (!CryptAcquireCertificatePrivateKey(cd->cert_context, flags, NULL,
566 &cd->crypt_prov, &cd->key_spec, &cd->free_crypt_prov))
570 "is in a legacy token not supported by Windows CNG API");
576 EVP_PKEY *pkey = X509_get_pubkey(*cert);
579 *privkey = xkey_load_generic_key(
tls_libctx, cd, pkey,
580 xkey_cng_sign, (XKEY_PRIVKEY_FREE_fn *) CAPI_DATA_free);
594 EVP_PKEY *privkey = NULL;
597 if (!Load_CryptoAPI_certificate(cert_prop, &cert, &privkey))
601 if (SSL_CTX_use_certificate(ssl_ctx, cert)
602 && SSL_CTX_use_PrivateKey(ssl_ctx, privkey))
611 EVP_PKEY_free(privkey);