OpenVPN
cryptoapi.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004 Peter 'Luna' Runestig <peter@runestig.com>
3 * Copyright (c) 2018 Selva Nair <selva.nair@gmail.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modifi-
7 * cation, are permitted provided that the following conditions are met:
8 *
9 * o Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * o Redistributions in binary form must reproduce the above copyright no-
13 * tice, this list of conditions and the following disclaimer in the do-
14 * cumentation and/or other materials provided with the distribution.
15 *
16 * o The names of the contributors may not be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LI-
24 * ABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUEN-
25 * TIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEV-
27 * ER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABI-
28 * LITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36#include "syshead.h"
37
38#ifdef ENABLE_CRYPTOAPI
39
40#include <openssl/ssl.h>
41#include <openssl/evp.h>
42#include <openssl/err.h>
43#include <windows.h>
44#include <wincrypt.h>
45#include <ncrypt.h>
46#include <stdio.h>
47#include <ctype.h>
48#include <assert.h>
49
50#include "buffer.h"
51#include "openssl_compat.h"
52#include "win32.h"
53#include "xkey_common.h"
54#include "crypto_openssl.h"
55
56#ifndef HAVE_XKEY_PROVIDER
57
58int
59SSL_CTX_use_CryptoAPI_certificate(SSL_CTX *ssl_ctx, const char *cert_prop)
60{
61 msg(M_NONFATAL, "ERROR: this binary was built without cryptoapicert support");
62 return 0;
63}
64
65#else /* HAVE_XKEY_PROVIDER */
66
67static XKEY_EXTERNAL_SIGN_fn xkey_cng_sign;
68
69typedef struct _CAPI_DATA
70{
71 const CERT_CONTEXT *cert_context;
72 HCRYPTPROV_OR_NCRYPT_KEY_HANDLE crypt_prov;
73 EVP_PKEY *pubkey;
74 DWORD key_spec;
75 BOOL free_crypt_prov;
76 int ref_count;
77} CAPI_DATA;
78
79/*
80 * Translate OpenSSL hash OID to CNG algorithm name. Returns
81 * "UNKNOWN" for unsupported algorithms and NULL for MD5+SHA1
82 * mixed hash used in TLS 1.1 and earlier.
83 */
84static const wchar_t *
85cng_hash_algo(int md_type)
86{
87 const wchar_t *alg = L"UNKNOWN";
88 switch (md_type)
89 {
90 case NID_md5:
91 alg = BCRYPT_MD5_ALGORITHM;
92 break;
93
94 case NID_sha1:
95 alg = BCRYPT_SHA1_ALGORITHM;
96 break;
97
98 case NID_sha256:
99 alg = BCRYPT_SHA256_ALGORITHM;
100 break;
101
102 case NID_sha384:
103 alg = BCRYPT_SHA384_ALGORITHM;
104 break;
105
106 case NID_sha512:
107 alg = BCRYPT_SHA512_ALGORITHM;
108 break;
109
110 case NID_md5_sha1:
111 case 0:
112 alg = NULL;
113 break;
114
115 default:
116 msg(M_WARN | M_INFO, "cryptoapicert: Unknown hash type NID=0x%x", md_type);
117 break;
118 }
119 return alg;
120}
121
122static void
123CAPI_DATA_free(CAPI_DATA *cd)
124{
125 if (!cd || cd->ref_count-- > 0)
126 {
127 return;
128 }
129 if (cd->free_crypt_prov && cd->crypt_prov)
130 {
131 if (cd->key_spec == CERT_NCRYPT_KEY_SPEC)
132 {
133 NCryptFreeObject(cd->crypt_prov);
134 }
135 else
136 {
137 CryptReleaseContext(cd->crypt_prov, 0);
138 }
139 }
140 if (cd->cert_context)
141 {
142 CertFreeCertificateContext(cd->cert_context);
143 }
144 EVP_PKEY_free(cd->pubkey); /* passing NULL is okay */
145
146 free(cd);
147}
148
157int
158parse_hexstring(const char *p, unsigned char *arr, size_t capacity)
159{
160 int i = 0;
161 for (; *p && i < capacity; p += 2)
162 {
163 /* skip spaces */
164 while (*p == ' ')
165 {
166 p++;
167 }
168 if (!*p) /* ending with spaces is not an error */
169 {
170 break;
171 }
172
173 if (!isxdigit(p[0]) || !isxdigit(p[1]) || sscanf(p, "%2hhx", &arr[i++]) != 1)
174 {
175 return 0;
176 }
177 }
178 return i;
179}
180
181static void *
182decode_object(struct gc_arena *gc, LPCSTR struct_type, const CRYPT_OBJID_BLOB *val, DWORD flags,
183 DWORD *cb)
184{
185 /* get byte count for decoding */
186 BYTE *buf;
187 if (!CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, struct_type, val->pbData,
188 val->cbData, flags, NULL, cb))
189 {
190 return NULL;
191 }
192
193 /* do the actual decode */
194 buf = gc_malloc(*cb, false, gc);
195 if (!CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, struct_type, val->pbData,
196 val->cbData, flags, buf, cb))
197 {
198 return NULL;
199 }
200
201 return buf;
202}
203
204static const CRYPT_OID_INFO *
205find_oid(DWORD keytype, const void *key, DWORD groupid)
206{
207 const CRYPT_OID_INFO *info = NULL;
208
209 /* try proper resolve, also including AD */
210 info = CryptFindOIDInfo(keytype, (void *)key, groupid);
211
212 /* fall back to all groups if not found yet */
213 if (!info && groupid)
214 {
215 info = CryptFindOIDInfo(keytype, (void *)key, 0);
216 }
217
218 return info;
219}
220
221static bool
222test_certificate_template(const char *cert_prop, const CERT_CONTEXT *cert_ctx)
223{
224 const CERT_INFO *info = cert_ctx->pCertInfo;
225 const CERT_EXTENSION *ext;
226 DWORD cbext;
227 void *pvext;
228 struct gc_arena gc = gc_new();
229 const WCHAR *tmpl_name = wide_string(cert_prop, &gc);
230
231 /* check for V2 extension (Windows 2003+) */
232 ext = CertFindExtension(szOID_CERTIFICATE_TEMPLATE, info->cExtension, info->rgExtension);
233 if (ext)
234 {
235 pvext = decode_object(&gc, X509_CERTIFICATE_TEMPLATE, &ext->Value, 0, &cbext);
236 if (pvext && cbext >= sizeof(CERT_TEMPLATE_EXT))
237 {
238 const CERT_TEMPLATE_EXT *cte = (const CERT_TEMPLATE_EXT *)pvext;
239 if (!stricmp(cert_prop, cte->pszObjId))
240 {
241 /* found direct OID match with certificate property specified */
242 gc_free(&gc);
243 return true;
244 }
245
246 const CRYPT_OID_INFO *tmpl_oid =
247 find_oid(CRYPT_OID_INFO_NAME_KEY, tmpl_name, CRYPT_TEMPLATE_OID_GROUP_ID);
248 if (tmpl_oid && !stricmp(tmpl_oid->pszOID, cte->pszObjId))
249 {
250 /* found OID match in extension against resolved key */
251 gc_free(&gc);
252 return true;
253 }
254 }
255 }
256
257 /* no extension found, exit */
258 gc_free(&gc);
259 return false;
260}
261
262static const CERT_CONTEXT *
263find_certificate_in_store(const char *cert_prop, HCERTSTORE cert_store)
264{
265 /* Find, and use, the desired certificate from the store. The
266 * 'cert_prop' certificate search string can look like this:
267 * SUBJ:<certificate substring to match>
268 * THUMB:<certificate thumbprint hex value>, e.g.
269 * THUMB:f6 49 24 41 01 b4 fb 44 0c ce f4 36 ae d0 c4 c9 df 7a b6 28
270 * TMPL:<template name or OID>
271 * The first matching certificate that has not expired is returned.
272 */
273 const CERT_CONTEXT *rv = NULL;
274 DWORD find_type;
275 const void *find_param;
276 unsigned char hash[255];
277 CRYPT_HASH_BLOB blob = { .cbData = 0, .pbData = hash };
278 struct gc_arena gc = gc_new();
279
280 if (!strncmp(cert_prop, "SUBJ:", 5))
281 {
282 /* skip the tag */
283 find_param = wide_string(cert_prop + 5, &gc);
284 find_type = CERT_FIND_SUBJECT_STR_W;
285 }
286 else if (!strncmp(cert_prop, "ISSUER:", 7))
287 {
288 find_param = wide_string(cert_prop + 7, &gc);
289 find_type = CERT_FIND_ISSUER_STR_W;
290 }
291 else if (!strncmp(cert_prop, "THUMB:", 6))
292 {
293 find_type = CERT_FIND_HASH;
294 find_param = &blob;
295
296 blob.cbData = parse_hexstring(cert_prop + 6, hash, sizeof(hash));
297 if (blob.cbData == 0)
298 {
299 msg(M_WARN | M_INFO, "WARNING: cryptoapicert: error parsing <%s>.", cert_prop);
300 goto out;
301 }
302 }
303 else if (!strncmp(cert_prop, "TMPL:", 5))
304 {
305 cert_prop += 5;
306 find_param = NULL;
307 find_type = CERT_FIND_HAS_PRIVATE_KEY;
308 }
309 else
310 {
311 msg(M_NONFATAL, "Error in cryptoapicert: unsupported certificate specification <%s>",
312 cert_prop);
313 goto out;
314 }
315
316 while (true)
317 {
318 int validity = 1;
319 /* this frees previous rv, if not NULL */
320 rv = CertFindCertificateInStore(cert_store, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0,
321 find_type, find_param, rv);
322 if (!rv)
323 {
324 break;
325 }
326 /* if searching by template name, check now if it matches */
327 if (find_type == CERT_FIND_HAS_PRIVATE_KEY && !test_certificate_template(cert_prop, rv))
328 {
329 continue;
330 }
331 validity = CertVerifyTimeValidity(NULL, rv->pCertInfo);
332 if (validity == 0)
333 {
334 break;
335 }
336 msg(M_WARN | M_INFO, "WARNING: cryptoapicert: ignoring certificate in store %s.",
337 validity < 0 ? "not yet valid" : "that has expired");
338 }
339
340out:
341 gc_free(&gc);
342 return rv;
343}
344
345#if defined(__GNUC__) || defined(__clang__)
346#pragma GCC diagnostic push
347#pragma GCC diagnostic ignored "-Wconversion"
348#endif
349
351static int
352xkey_cng_ec_sign(CAPI_DATA *cd, unsigned char *sig, size_t *siglen, const unsigned char *tbs,
353 size_t tbslen)
354{
355 DWORD len = *siglen;
356
357 msg(D_LOW, "Signing using NCryptSignHash with EC key");
358
359 DWORD status = NCryptSignHash(cd->crypt_prov, NULL, (BYTE *)tbs, tbslen, sig, len, &len, 0);
360
361 if (status != ERROR_SUCCESS)
362 {
363 SetLastError(status);
364 msg(M_NONFATAL | M_ERRNO, "Error in cryptoapicert: ECDSA signature using CNG failed.");
365 return 0;
366 }
367
368 /* NCryptSignHash returns r|s -- convert to DER encoded buffer expected by OpenSSL */
369 int derlen = ecdsa_bin2der(sig, (int)len, *siglen);
370 if (derlen <= 0)
371 {
372 return 0;
373 }
374 *siglen = derlen;
375 return 1;
376}
377
379static int
380xkey_cng_rsa_sign(CAPI_DATA *cd, unsigned char *sig, size_t *siglen, const unsigned char *tbs,
381 size_t tbslen, XKEY_SIGALG sigalg)
382{
383 dmsg(D_LOW, "In xkey_cng_rsa_sign");
384
385 ASSERT(cd);
386 ASSERT(sig);
387 ASSERT(tbs);
388
389 DWORD status = ERROR_SUCCESS;
390 DWORD len = 0;
391
392 const wchar_t *hashalg = cng_hash_algo(OBJ_sn2nid(sigalg.mdname));
393
394 if (hashalg && wcscmp(hashalg, L"UNKNOWN") == 0)
395 {
396 msg(M_NONFATAL, "Error in cryptoapicert: Unknown hash name <%s>", sigalg.mdname);
397 return 0;
398 }
399
400 if (!strcmp(sigalg.padmode, "pkcs1"))
401 {
402 msg(D_LOW, "Signing using NCryptSignHash with PKCS1 padding: hashalg <%s>", sigalg.mdname);
403
404 BCRYPT_PKCS1_PADDING_INFO padinfo = { hashalg };
405 status = NCryptSignHash(cd->crypt_prov, &padinfo, (BYTE *)tbs, (DWORD)tbslen, sig,
406 (DWORD)*siglen, &len, BCRYPT_PAD_PKCS1);
407 }
408 else if (!strcmp(sigalg.padmode, "pss"))
409 {
410 int saltlen = tbslen; /* digest size by default */
411 if (!strcmp(sigalg.saltlen, "max"))
412 {
413 saltlen = xkey_max_saltlen(EVP_PKEY_bits(cd->pubkey), tbslen);
414 if (saltlen < 0)
415 {
416 msg(M_NONFATAL, "Error in cryptoapicert: invalid salt length (%d)", saltlen);
417 return 0;
418 }
419 }
420
421 msg(D_LOW, "Signing using NCryptSignHash with PSS padding: hashalg <%s>, saltlen <%d>",
422 sigalg.mdname, saltlen);
423
424 BCRYPT_PSS_PADDING_INFO padinfo = { hashalg,
425 (DWORD)saltlen }; /* cast is safe as saltlen >= 0 */
426 status = NCryptSignHash(cd->crypt_prov, &padinfo, (BYTE *)tbs, (DWORD)tbslen, sig,
427 (DWORD)*siglen, &len, BCRYPT_PAD_PSS);
428 }
429 else
430 {
431 msg(M_NONFATAL, "Error in cryptoapicert: Unsupported padding mode <%s>", sigalg.padmode);
432 return 0;
433 }
434
435 if (status != ERROR_SUCCESS)
436 {
437 SetLastError(status);
438 msg(M_NONFATAL | M_ERRNO, "Error in cryptoapicert: RSA signature using CNG failed.");
439 return 0;
440 }
441
442 *siglen = len;
443 return (*siglen > 0);
444}
445
446#if defined(__GNUC__) || defined(__clang__)
447#pragma GCC diagnostic pop
448#endif
449
451static int
452xkey_cng_sign(void *handle, unsigned char *sig, size_t *siglen, const unsigned char *tbs,
453 size_t tbslen, XKEY_SIGALG sigalg)
454{
455 dmsg(D_LOW, "In xkey_cng_sign");
456
457 CAPI_DATA *cd = handle;
458 ASSERT(cd);
459 ASSERT(sig);
460 ASSERT(tbs);
461
462 unsigned char mdbuf[EVP_MAX_MD_SIZE];
463 size_t buflen = _countof(mdbuf);
464
465 /* compute digest if required */
466 if (!strcmp(sigalg.op, "DigestSign"))
467 {
468 if (!xkey_digest(tbs, tbslen, mdbuf, &buflen, sigalg.mdname))
469 {
470 return 0;
471 }
472 tbs = mdbuf;
473 tbslen = buflen;
474 }
475
476 if (!strcmp(sigalg.keytype, "EC"))
477 {
478 return xkey_cng_ec_sign(cd, sig, siglen, tbs, tbslen);
479 }
480 else if (!strcmp(sigalg.keytype, "RSA"))
481 {
482 return xkey_cng_rsa_sign(cd, sig, siglen, tbs, tbslen, sigalg);
483 }
484 else
485 {
486 return 0; /* Unknown keytype -- should not happen */
487 }
488}
489
490static char *
491get_cert_name(const CERT_CONTEXT *cc, struct gc_arena *gc)
492{
493 DWORD len = CertGetNameStringW(cc, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0);
494 char *name = NULL;
495 if (len)
496 {
497 wchar_t *wname = gc_malloc(len * sizeof(wchar_t), false, gc);
498 if (!wname
499 || CertGetNameStringW(cc, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, wname, len) == 0)
500 {
501 return NULL;
502 }
503 name = utf16to8(wname, gc);
504 }
505 return name;
506}
507
514static int
515Load_CryptoAPI_certificate(const char *cert_prop, X509 **cert, EVP_PKEY **privkey)
516{
517 HCERTSTORE cs;
518 CAPI_DATA *cd = calloc(1, sizeof(*cd));
519 struct gc_arena gc = gc_new();
520
521 if (cd == NULL)
522 {
523 msg(M_NONFATAL, "Error in cryptoapicert: out of memory");
524 goto err;
525 }
526 /* search CURRENT_USER first, then LOCAL_MACHINE */
527 cs = CertOpenStore((LPCSTR)CERT_STORE_PROV_SYSTEM, 0, 0,
528 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG
529 | CERT_STORE_READONLY_FLAG,
530 L"MY");
531 if (cs == NULL)
532 {
533 msg(M_NONFATAL | M_ERRNO, "Error in cryptoapicert: failed to open user certficate store");
534 goto err;
535 }
536 cd->cert_context = find_certificate_in_store(cert_prop, cs);
537 CertCloseStore(cs, 0);
538 if (!cd->cert_context)
539 {
540 cs = CertOpenStore((LPCSTR)CERT_STORE_PROV_SYSTEM, 0, 0,
541 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_STORE_OPEN_EXISTING_FLAG
542 | CERT_STORE_READONLY_FLAG,
543 L"MY");
544 if (cs == NULL)
545 {
547 "Error in cryptoapicert: failed to open machine certficate store");
548 goto err;
549 }
550 cd->cert_context = find_certificate_in_store(cert_prop, cs);
551 CertCloseStore(cs, 0);
552 if (cd->cert_context == NULL)
553 {
554 msg(M_NONFATAL, "Error in cryptoapicert: certificate matching <%s> not found",
555 cert_prop);
556 goto err;
557 }
558 }
559
560 /* try to log the "name" of the selected certificate */
561 char *cert_name = get_cert_name(cd->cert_context, &gc);
562 if (cert_name)
563 {
564 msg(D_LOW, "cryptapicert: using certificate with name <%s>", cert_name);
565 }
566
567 /* cert_context->pbCertEncoded is the cert X509 DER encoded. */
568 *cert = d2i_X509(NULL, (const unsigned char **)&cd->cert_context->pbCertEncoded,
569 cd->cert_context->cbCertEncoded);
570 if (*cert == NULL)
571 {
572 msg(M_NONFATAL, "Error in cryptoapicert: X509 certificate decode failed");
573 goto err;
574 }
575
576 /* set up stuff to use the private key */
577 /* We support NCRYPT key handles only */
578 DWORD flags = CRYPT_ACQUIRE_COMPARE_KEY_FLAG | CRYPT_ACQUIRE_ONLY_NCRYPT_KEY_FLAG;
579 if (!CryptAcquireCertificatePrivateKey(cd->cert_context, flags, NULL, &cd->crypt_prov,
580 &cd->key_spec, &cd->free_crypt_prov))
581 {
582 /* private key may be in a token not available, or incompatible with CNG */
584 "Error in cryptoapicert: failed to acquire key. Key not present or "
585 "is in a legacy token not supported by Windows CNG API");
586 X509_free(*cert);
587 goto err;
588 }
589
590 /* the public key */
591 EVP_PKEY *pkey = X509_get_pubkey(*cert);
592 cd->pubkey = pkey; /* will be freed with cd */
593
594 *privkey = xkey_load_generic_key(tls_libctx, cd, pkey, xkey_cng_sign,
595 (XKEY_PRIVKEY_FREE_fn *)CAPI_DATA_free);
596 gc_free(&gc);
597 return 1; /* do not free cd -- its kept by xkey provider */
598
599err:
600 CAPI_DATA_free(cd);
601 gc_free(&gc);
602 return 0;
603}
604
605int
606SSL_CTX_use_CryptoAPI_certificate(SSL_CTX *ssl_ctx, const char *cert_prop)
607{
608 X509 *cert = NULL;
609 EVP_PKEY *privkey = NULL;
610 int ret = 0;
611
612 if (!Load_CryptoAPI_certificate(cert_prop, &cert, &privkey))
613 {
614 return ret;
615 }
616 if (SSL_CTX_use_certificate(ssl_ctx, cert) && SSL_CTX_use_PrivateKey(ssl_ctx, privkey))
617 {
619 ret = 1;
620 }
621
622 /* Always free cert and privkey even if retained by ssl_ctx as
623 * they are reference counted */
624 X509_free(cert);
625 EVP_PKEY_free(privkey);
626 return ret;
627}
628
629#endif /* HAVE_XKEY_PROVIDER */
630#endif /* _WIN32 */
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition buffer.c:336
static void gc_free(struct gc_arena *a)
Definition buffer.h:1015
static struct gc_arena gc_new(void)
Definition buffer.h:1007
void crypto_print_openssl_errors(const unsigned int flags)
Retrieve any occurred OpenSSL errors and print those errors.
Data Channel Cryptography OpenSSL-specific backend interface.
int SSL_CTX_use_CryptoAPI_certificate(SSL_CTX *ssl_ctx, const char *cert_prop)
Definition cryptoapi.c:59
#define D_LOW
Definition errlevel.h:96
#define M_INFO
Definition errlevel.h:54
static SERVICE_STATUS status
Definition interactive.c:51
OpenSSL compatibility stub.
#define M_NONFATAL
Definition error.h:91
#define dmsg(flags,...)
Definition error.h:172
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
OSSL_LIB_CTX * tls_libctx
Definition ssl_openssl.c:78
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Definition list.h:53
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
struct gc_arena gc
Definition test_ssl.c:131
char * utf16to8(const wchar_t *utf16, struct gc_arena *gc)
Definition win32-util.c:49
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)
Definition win32-util.c:40