OpenVPN
pkcs11_openssl.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2026 Sentyron B.V. <openvpn@sentyron.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "syshead.h"
34
35#if defined(ENABLE_PKCS11) && defined(ENABLE_CRYPTO_OPENSSL)
36
37#include "errlevel.h"
38#include "pkcs11_backend.h"
39#include "ssl_verify.h"
40#include "xkey_common.h"
41#include <pkcs11-helper-1.0/pkcs11h-openssl.h>
42
43#ifdef HAVE_XKEY_PROVIDER
44static XKEY_EXTERNAL_SIGN_fn xkey_pkcs11h_sign;
45
46/* Table linking OpenSSL digest NID with CKM and CKG constants in PKCS#11 */
47#define MD_TYPE(n) { NID_sha##n, CKM_SHA##n, CKG_MGF1_SHA##n }
48static const struct
49{
50 int nid;
51 unsigned long ckm_id;
52 unsigned long mgf_id;
53} mdtypes[] = { MD_TYPE(224),
54 MD_TYPE(256),
55 MD_TYPE(384),
56 MD_TYPE(512),
57 { NID_sha1, CKM_SHA_1, CKG_MGF1_SHA1 }, /* SHA_1 naming is an oddity */
58 { NID_undef, 0, 0 } };
59
60/* From sigalg, derive parameters for pss signature and fill in pss_params.
61 * Its of type CK_RSA_PKCS_PSS_PARAMS struct with three fields to be filled in:
62 * {enum hashAlg, enum mgf, ulong sLen}
63 * where hashAlg is CKM_SHA256 etc., mgf is CKG_MGF1_SHA256 etc.
64 */
65static int
66set_pss_params(CK_RSA_PKCS_PSS_PARAMS *pss_params, XKEY_SIGALG sigalg, pkcs11h_certificate_t cert)
67{
68 int ret = 0;
69 X509 *x509 = NULL;
70 EVP_PKEY *pubkey = NULL;
71
72 if ((x509 = pkcs11h_openssl_getX509(cert)) == NULL || (pubkey = X509_get0_pubkey(x509)) == NULL)
73 {
74 msg(M_WARN, "PKCS#11: Unable get public key");
75 goto cleanup;
76 }
77
78 /* map mdname to CKM and CKG constants for hash and mgf algorithms */
79 int i = 0;
80 int nid = OBJ_sn2nid(sigalg.mdname);
81 while (mdtypes[i].nid != NID_undef && mdtypes[i].nid != nid)
82 {
83 i++;
84 }
85 pss_params->hashAlg = mdtypes[i].ckm_id;
86 pss_params->mgf = mdtypes[i].mgf_id;
87
88 /* determine salt length */
89 const EVP_MD *md = EVP_get_digestbyname(sigalg.mdname);
90 if (!md)
91 {
92 msg(M_WARN,
93 "WARN: set_pss_params: EVP_get_digestbyname returned NULL "
94 "for mdname = <%s>",
95 sigalg.mdname);
96 goto cleanup;
97 }
98 int mdsize = EVP_MD_get_size(md);
99
100 int saltlen = -1;
101 if (!strcmp(sigalg.saltlen, "digest")) /* same as digest size */
102 {
103 saltlen = mdsize;
104 }
105 else if (!strcmp(sigalg.saltlen, "max")) /* maximum possible value */
106 {
107 saltlen = xkey_max_saltlen(EVP_PKEY_get_bits(pubkey), mdsize);
108 }
109
110 if (saltlen < 0 || pss_params->hashAlg == 0)
111 {
112 msg(M_WARN,
113 "WARN: invalid RSA_PKCS1_PSS parameters: saltlen = <%s> "
114 "mdname = <%s>.",
115 sigalg.saltlen, sigalg.mdname);
116 goto cleanup;
117 }
118 pss_params->sLen = (unsigned long)saltlen; /* saltlen >= 0 at this point */
119
120 msg(D_XKEY, "set_pss_params: sLen = %lu, hashAlg = %lu, mgf = %lu", pss_params->sLen,
121 pss_params->hashAlg, pss_params->mgf);
122
123 ret = 1;
124
125cleanup:
126 if (x509)
127 {
128 X509_free(x509);
129 }
130 return ret;
131}
132
133#if defined(__GNUC__) || defined(__clang__)
134#pragma GCC diagnostic push
135#pragma GCC diagnostic ignored "-Wsign-conversion"
136#endif
137
143static int
144xkey_pkcs11h_sign(void *handle, unsigned char *sig, size_t *siglen, const unsigned char *tbs,
145 size_t tbslen, XKEY_SIGALG sigalg)
146{
147 pkcs11h_certificate_t cert = handle;
148 CK_MECHANISM mech = { CKM_RSA_PKCS, NULL, 0 }; /* default value */
149 CK_RSA_PKCS_PSS_PARAMS pss_params = { 0 };
150
151 unsigned char buf[EVP_MAX_MD_SIZE];
152 size_t buflen = 0;
153 size_t siglen_max = *siglen;
154
155 unsigned char enc[EVP_MAX_MD_SIZE + 32]; /* 32 bytes enough for DigestInfo header */
156 size_t enc_len = sizeof(enc);
157
158 if (!strcmp(sigalg.op, "DigestSign"))
159 {
160 msg(D_XKEY, "xkey_pkcs11h_sign: computing digest");
161 if (xkey_digest(tbs, tbslen, buf, &buflen, sigalg.mdname))
162 {
163 tbs = buf;
164 tbslen = (size_t)buflen;
165 }
166 else
167 {
168 return 0;
169 }
170 }
171
172 if (!strcmp(sigalg.keytype, "EC"))
173 {
174 msg(D_XKEY, "xkey_pkcs11h_sign: signing with EC key");
175 mech.mechanism = CKM_ECDSA;
176 }
177 else if (!strcmp(sigalg.keytype, "RSA"))
178 {
179 msg(D_XKEY, "xkey_pkcs11h_sign: signing with RSA key: padmode = %s", sigalg.padmode);
180 if (!strcmp(sigalg.padmode, "none"))
181 {
182 mech.mechanism = CKM_RSA_X_509;
183 }
184 else if (!strcmp(sigalg.padmode, "pss"))
185 {
186 mech.mechanism = CKM_RSA_PKCS_PSS;
187
188 if (!set_pss_params(&pss_params, sigalg, cert))
189 {
190 return 0;
191 }
192
193 mech.pParameter = &pss_params;
194 mech.ulParameterLen = sizeof(pss_params);
195 }
196 else if (!strcmp(sigalg.padmode, "pkcs1"))
197 {
198 /* CMA_RSA_PKCS needs pkcs1 encoded digest */
199
200 if (!encode_pkcs1(enc, &enc_len, sigalg.mdname, tbs, tbslen))
201 {
202 return 0;
203 }
204 tbs = enc;
205 tbslen = enc_len;
206 }
207 else /* should not happen */
208 {
209 msg(M_WARN, "PKCS#11: Unknown padmode <%s>", sigalg.padmode);
210 }
211 }
212 else
213 {
214 ASSERT(0); /* coding error -- we couldnt have created any such key */
215 }
216
217 if (CKR_OK != pkcs11h_certificate_signAny_ex(cert, &mech, tbs, tbslen, sig, siglen))
218 {
219 return 0;
220 }
221 if (strcmp(sigalg.keytype, "EC"))
222 {
223 return 1;
224 }
225
226 /* For EC keys, pkcs11 returns signature as r|s: convert to der encoded */
227 int derlen = ecdsa_bin2der(sig, (int)*siglen, siglen_max);
228
229 if (derlen <= 0)
230 {
231 return 0;
232 }
233 *siglen = derlen;
234
235 return 1;
236}
237
238#if defined(__GNUC__) || defined(__clang__)
239#pragma GCC diagnostic pop
240#endif
241
242/* wrapper for handle free */
243static void
244xkey_handle_free(void *handle)
245{
246 pkcs11h_certificate_freeCertificate(handle);
247}
248
249
260static int
261xkey_load_from_pkcs11h(pkcs11h_certificate_t certificate, struct tls_root_ctx *const ctx)
262{
263 int ret = 0;
264
265 X509 *x509 = pkcs11h_openssl_getX509(certificate);
266 if (!x509)
267 {
268 msg(M_WARN, "PKCS#11: Unable get x509 certificate object");
269 return 0;
270 }
271
272 EVP_PKEY *pubkey = X509_get0_pubkey(x509);
273
274 XKEY_PRIVKEY_FREE_fn *free_op = xkey_handle_free; /* it calls pkcs11h_..._freeCertificate() */
275 XKEY_EXTERNAL_SIGN_fn *sign_op = xkey_pkcs11h_sign;
276
277 EVP_PKEY *pkey = xkey_load_generic_key(tls_libctx, certificate, pubkey, sign_op, free_op);
278 if (!pkey)
279 {
280 msg(M_WARN, "PKCS#11: Failed to load private key into xkey provider");
281 goto cleanup;
282 }
283 /* provider took ownership of the pkcs11h certificate object -- do not free below */
284 certificate = NULL;
285
286 if (!SSL_CTX_use_cert_and_key(ctx->ctx, x509, pkey, NULL, 0))
287 {
289 msg(M_FATAL, "PKCS#11: Failed to set cert and private key for OpenSSL");
290 goto cleanup;
291 }
292 ret = 1;
293
294cleanup:
295 if (x509)
296 {
297 X509_free(x509);
298 }
299 if (pkey)
300 {
301 EVP_PKEY_free(pkey);
302 }
303 if (certificate)
304 {
305 pkcs11h_certificate_freeCertificate(certificate);
306 }
307 return ret;
308}
309#endif /* HAVE_XKEY_PROVIDER */
310
311int
312pkcs11_init_tls_session(pkcs11h_certificate_t certificate, struct tls_root_ctx *const ssl_ctx)
313{
314#ifdef HAVE_XKEY_PROVIDER
315 return (xkey_load_from_pkcs11h(certificate, ssl_ctx) == 0); /* inverts the return value */
316#else
317 int ret = 1;
318
319 X509 *x509 = NULL;
320 EVP_PKEY *evp = NULL;
321 pkcs11h_openssl_session_t openssl_session = NULL;
322
323 if ((openssl_session = pkcs11h_openssl_createSession(certificate)) == NULL)
324 {
325 msg(M_WARN, "PKCS#11: Cannot initialize openssl session");
326 goto cleanup;
327 }
328
329 /*
330 * Will be released by openssl_session
331 */
332 certificate = NULL;
333
334 if ((evp = pkcs11h_openssl_session_getEVP(openssl_session)) == NULL)
335 {
336 msg(M_WARN, "PKCS#11: Unable get evp object");
337 goto cleanup;
338 }
339
340 if ((x509 = pkcs11h_openssl_session_getX509(openssl_session)) == NULL)
341 {
342 msg(M_WARN, "PKCS#11: Unable get certificate object");
343 goto cleanup;
344 }
345
346 if (!SSL_CTX_use_PrivateKey(ssl_ctx->ctx, evp))
347 {
348 msg(M_WARN, "PKCS#11: Cannot set private key for openssl");
349 goto cleanup;
350 }
351
352 if (!SSL_CTX_use_certificate(ssl_ctx->ctx, x509))
353 {
355 msg(M_FATAL, "PKCS#11: Cannot set certificate for openssl");
356 goto cleanup;
357 }
358 ret = 0;
359
360cleanup:
361 /*
362 * Certificate freeing is usually handled by openssl_session.
363 * If something went wrong, creating the session we have to do it manually.
364 */
365 if (certificate != NULL)
366 {
367 pkcs11h_certificate_freeCertificate(certificate);
368 }
369
370 /*
371 * openssl objects have reference
372 * count, so release them
373 */
374 X509_free(x509);
375 x509 = NULL;
376
377 EVP_PKEY_free(evp);
378 evp = NULL;
379
380 if (openssl_session != NULL)
381 {
382 pkcs11h_openssl_freeSession(openssl_session);
383 }
384 return ret;
385#endif /* ifdef HAVE_XKEY_PROVIDER */
386}
387
388char *
389pkcs11_certificate_dn(pkcs11h_certificate_t certificate, struct gc_arena *gc)
390{
391 X509 *x509 = NULL;
392
393 char *dn = NULL;
394
395 if ((x509 = pkcs11h_openssl_getX509(certificate)) == NULL)
396 {
397 msg(M_FATAL, "PKCS#11: Cannot get X509");
398 goto cleanup;
399 }
400
401 dn = x509_get_subject(x509, gc);
402
403cleanup:
404 X509_free(x509);
405 x509 = NULL;
406
407 return dn;
408}
409
410int
411pkcs11_certificate_serial(pkcs11h_certificate_t certificate, char *serial, size_t serial_len)
412{
413 X509 *x509 = NULL;
414 BIO *bio = NULL;
415 int ret = 1;
416
417 if ((x509 = pkcs11h_openssl_getX509(certificate)) == NULL)
418 {
419 msg(M_FATAL, "PKCS#11: Cannot get X509");
420 goto cleanup;
421 }
422
423 if ((bio = BIO_new(BIO_s_mem())) == NULL)
424 {
425 msg(M_FATAL, "PKCS#11: Cannot create BIO");
426 goto cleanup;
427 }
428
429 i2a_ASN1_INTEGER(bio, X509_get_serialNumber(x509));
430 ASSERT(serial_len <= INT_MAX);
431 int n = BIO_read(bio, serial, (int)serial_len - 1);
432
433 if (n < 0)
434 {
435 serial[0] = '\x0';
436 }
437 else
438 {
439 serial[n] = 0;
440 }
441
442 ret = 0;
443
444cleanup:
445 X509_free(x509);
446 x509 = NULL;
447
448 return ret;
449}
450
451#endif /* defined(ENABLE_PKCS11) && defined(ENABLE_OPENSSL) */
void crypto_print_openssl_errors(const unsigned int flags)
Retrieve any occurred OpenSSL errors and print those errors.
#define D_XKEY
Definition errlevel.h:114
#define M_FATAL
Definition error.h:90
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
PKCS #11 SSL library-specific backend.
OSSL_LIB_CTX * tls_libctx
Definition ssl_openssl.c:78
Control Channel Verification Module.
char * x509_get_subject(openvpn_x509_cert_t *cert, struct gc_arena *gc)
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Structure that wraps the TLS context.
Definition ssl_mbedtls.h:86
SSL_CTX * ctx
Definition ssl_openssl.h:41
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:138