blob: d4053ea829a50af5a1a3f7058935464e47ac3a86 [file] [log] [blame]
djm@openbsd.org7d6034b2019-09-05 10:05:51 +00001/* $OpenBSD: ssh-pkcs11.c,v 1.45 2019/09/05 10:05:51 djm Exp $ */
Damien Miller7ea845e2010-02-12 09:21:02 +11002/*
3 * Copyright (c) 2010 Markus Friedl. All rights reserved.
djm@openbsd.org93f02102019-01-20 22:51:37 +00004 * Copyright (c) 2014 Pedro Martelletto. All rights reserved.
Damien Miller7ea845e2010-02-12 09:21:02 +11005 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
Damien Miller8ad0fbd2010-02-12 09:49:06 +110019#include "includes.h"
20
Damien Millerdfa41562010-02-12 10:06:28 +110021#ifdef ENABLE_PKCS11
22
Damien Miller8ad0fbd2010-02-12 09:49:06 +110023#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
djm@openbsd.org93f02102019-01-20 22:51:37 +000026
27#include <sys/types.h>
Damien Miller7ea845e2010-02-12 09:21:02 +110028#include <stdarg.h>
29#include <stdio.h>
30
djm@openbsd.org93f02102019-01-20 22:51:37 +000031#include <ctype.h>
Damien Miller7ea845e2010-02-12 09:21:02 +110032#include <string.h>
33#include <dlfcn.h>
34
Damien Miller8ad0fbd2010-02-12 09:49:06 +110035#include "openbsd-compat/sys-queue.h"
Damien Miller48f54b92018-09-13 12:13:50 +100036#include "openbsd-compat/openssl-compat.h"
Damien Miller8ad0fbd2010-02-12 09:49:06 +110037
djm@openbsd.org93f02102019-01-20 22:51:37 +000038#include <openssl/ecdsa.h>
Damien Millerd2252c72013-11-04 07:41:48 +110039#include <openssl/x509.h>
djm@openbsd.org93f02102019-01-20 22:51:37 +000040#include <openssl/err.h>
Damien Millerd2252c72013-11-04 07:41:48 +110041
Damien Miller7ea845e2010-02-12 09:21:02 +110042#define CRYPTOKI_COMPAT
43#include "pkcs11.h"
44
45#include "log.h"
46#include "misc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000047#include "sshkey.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110048#include "ssh-pkcs11.h"
49#include "xmalloc.h"
50
51struct pkcs11_slotinfo {
52 CK_TOKEN_INFO token;
53 CK_SESSION_HANDLE session;
54 int logged_in;
55};
56
57struct pkcs11_provider {
58 char *name;
59 void *handle;
60 CK_FUNCTION_LIST *function_list;
61 CK_INFO info;
62 CK_ULONG nslots;
63 CK_SLOT_ID *slotlist;
64 struct pkcs11_slotinfo *slotinfo;
65 int valid;
66 int refcount;
67 TAILQ_ENTRY(pkcs11_provider) next;
68};
69
70TAILQ_HEAD(, pkcs11_provider) pkcs11_providers;
71
72struct pkcs11_key {
73 struct pkcs11_provider *provider;
74 CK_ULONG slotidx;
Damien Miller7ea845e2010-02-12 09:21:02 +110075 char *keyid;
76 int keyid_len;
77};
78
79int pkcs11_interactive = 0;
80
Damien Miller2efcf812019-01-21 11:57:21 +110081#ifdef HAVE_EC_KEY_METHOD_NEW
djm@openbsd.org93f02102019-01-20 22:51:37 +000082static void
83ossl_error(const char *msg)
84{
djm@openbsd.org65294092019-01-20 23:11:11 +000085 unsigned long e;
djm@openbsd.org93f02102019-01-20 22:51:37 +000086
djm@openbsd.org662be402019-01-21 02:05:38 +000087 error("%s: %s", __func__, msg);
djm@openbsd.org65294092019-01-20 23:11:11 +000088 while ((e = ERR_get_error()) != 0)
djm@openbsd.org662be402019-01-21 02:05:38 +000089 error("%s: libcrypto error: %.100s", __func__,
djm@openbsd.org65294092019-01-20 23:11:11 +000090 ERR_error_string(e, NULL));
djm@openbsd.org93f02102019-01-20 22:51:37 +000091}
Damien Miller2efcf812019-01-21 11:57:21 +110092#endif /* HAVE_EC_KEY_METHOD_NEW */
djm@openbsd.org93f02102019-01-20 22:51:37 +000093
Damien Miller7ea845e2010-02-12 09:21:02 +110094int
95pkcs11_init(int interactive)
96{
97 pkcs11_interactive = interactive;
98 TAILQ_INIT(&pkcs11_providers);
99 return (0);
100}
101
102/*
djm@openbsd.org93f02102019-01-20 22:51:37 +0000103 * finalize a provider shared library, it's no longer usable.
Damien Miller7ea845e2010-02-12 09:21:02 +1100104 * however, there might still be keys referencing this provider,
djm@openbsd.org93f02102019-01-20 22:51:37 +0000105 * so the actual freeing of memory is handled by pkcs11_provider_unref().
Damien Miller7ea845e2010-02-12 09:21:02 +1100106 * this is called when a provider gets unregistered.
107 */
108static void
109pkcs11_provider_finalize(struct pkcs11_provider *p)
110{
111 CK_RV rv;
112 CK_ULONG i;
113
114 debug("pkcs11_provider_finalize: %p refcount %d valid %d",
115 p, p->refcount, p->valid);
116 if (!p->valid)
117 return;
118 for (i = 0; i < p->nslots; i++) {
119 if (p->slotinfo[i].session &&
120 (rv = p->function_list->C_CloseSession(
121 p->slotinfo[i].session)) != CKR_OK)
122 error("C_CloseSession failed: %lu", rv);
123 }
124 if ((rv = p->function_list->C_Finalize(NULL)) != CKR_OK)
125 error("C_Finalize failed: %lu", rv);
126 p->valid = 0;
127 p->function_list = NULL;
128 dlclose(p->handle);
129}
130
131/*
132 * remove a reference to the provider.
133 * called when a key gets destroyed or when the provider is unregistered.
134 */
135static void
136pkcs11_provider_unref(struct pkcs11_provider *p)
137{
138 debug("pkcs11_provider_unref: %p refcount %d", p, p->refcount);
139 if (--p->refcount <= 0) {
140 if (p->valid)
141 error("pkcs11_provider_unref: %p still valid", p);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000142 free(p->name);
Darren Tuckera627d422013-06-02 07:31:17 +1000143 free(p->slotlist);
144 free(p->slotinfo);
145 free(p);
Damien Miller7ea845e2010-02-12 09:21:02 +1100146 }
147}
148
149/* unregister all providers, keys might still point to the providers */
150void
151pkcs11_terminate(void)
152{
153 struct pkcs11_provider *p;
154
155 while ((p = TAILQ_FIRST(&pkcs11_providers)) != NULL) {
156 TAILQ_REMOVE(&pkcs11_providers, p, next);
157 pkcs11_provider_finalize(p);
158 pkcs11_provider_unref(p);
159 }
160}
161
162/* lookup provider by name */
163static struct pkcs11_provider *
164pkcs11_provider_lookup(char *provider_id)
165{
166 struct pkcs11_provider *p;
167
168 TAILQ_FOREACH(p, &pkcs11_providers, next) {
169 debug("check %p %s", p, p->name);
170 if (!strcmp(provider_id, p->name))
171 return (p);
172 }
173 return (NULL);
174}
175
176/* unregister provider by name */
177int
178pkcs11_del_provider(char *provider_id)
179{
180 struct pkcs11_provider *p;
181
182 if ((p = pkcs11_provider_lookup(provider_id)) != NULL) {
183 TAILQ_REMOVE(&pkcs11_providers, p, next);
184 pkcs11_provider_finalize(p);
185 pkcs11_provider_unref(p);
186 return (0);
187 }
188 return (-1);
189}
190
djm@openbsd.orgce46c3a2019-01-21 02:01:03 +0000191static RSA_METHOD *rsa_method;
192static int rsa_idx = 0;
Damien Miller23490a62019-01-21 15:05:43 +1100193#ifdef HAVE_EC_KEY_METHOD_NEW
djm@openbsd.orgce46c3a2019-01-21 02:01:03 +0000194static EC_KEY_METHOD *ec_key_method;
195static int ec_key_idx = 0;
Darren Tuckerb6dd3272019-01-21 13:50:17 +1100196#endif
djm@openbsd.orgce46c3a2019-01-21 02:01:03 +0000197
djm@openbsd.org58622a82019-01-20 23:10:33 +0000198/* release a wrapped object */
199static void
200pkcs11_k11_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
201 long argl, void *argp)
Damien Miller7ea845e2010-02-12 09:21:02 +1100202{
Darren Tucker5590f532019-04-26 18:22:10 +1000203 struct pkcs11_key *k11 = ptr;
Damien Miller7ea845e2010-02-12 09:21:02 +1100204
Darren Tucker5590f532019-04-26 18:22:10 +1000205 debug("%s: parent %p ptr %p idx %d", __func__, parent, ptr, idx);
206 if (k11 == NULL)
207 return;
208 if (k11->provider)
209 pkcs11_provider_unref(k11->provider);
210 free(k11->keyid);
211 free(k11);
Damien Miller7ea845e2010-02-12 09:21:02 +1100212}
213
Damien Miller031c9102010-04-16 15:54:44 +1000214/* find a single 'obj' for given attributes */
215static int
216pkcs11_find(struct pkcs11_provider *p, CK_ULONG slotidx, CK_ATTRIBUTE *attr,
217 CK_ULONG nattr, CK_OBJECT_HANDLE *obj)
218{
219 CK_FUNCTION_LIST *f;
220 CK_SESSION_HANDLE session;
221 CK_ULONG nfound = 0;
222 CK_RV rv;
223 int ret = -1;
224
225 f = p->function_list;
226 session = p->slotinfo[slotidx].session;
227 if ((rv = f->C_FindObjectsInit(session, attr, nattr)) != CKR_OK) {
228 error("C_FindObjectsInit failed (nattr %lu): %lu", nattr, rv);
229 return (-1);
230 }
231 if ((rv = f->C_FindObjects(session, obj, 1, &nfound)) != CKR_OK ||
232 nfound != 1) {
233 debug("C_FindObjects failed (nfound %lu nattr %lu): %lu",
234 nfound, nattr, rv);
235 } else
236 ret = 0;
237 if ((rv = f->C_FindObjectsFinal(session)) != CKR_OK)
238 error("C_FindObjectsFinal failed: %lu", rv);
239 return (ret);
240}
241
Damien Miller7ea845e2010-02-12 09:21:02 +1100242static int
djm@openbsd.org7d6034b2019-09-05 10:05:51 +0000243pkcs11_login_slot(struct pkcs11_provider *provider, struct pkcs11_slotinfo *si,
244 CK_USER_TYPE type)
djm@openbsd.org21621712019-01-22 12:00:50 +0000245{
djm@openbsd.org21621712019-01-22 12:00:50 +0000246 char *pin = NULL, prompt[1024];
247 CK_RV rv;
248
djm@openbsd.org7d6034b2019-09-05 10:05:51 +0000249 if (provider == NULL || si == NULL || !provider->valid) {
djm@openbsd.org21621712019-01-22 12:00:50 +0000250 error("no pkcs11 (valid) provider found");
251 return (-1);
252 }
253
djm@openbsd.org21621712019-01-22 12:00:50 +0000254 if (!pkcs11_interactive) {
255 error("need pin entry%s",
256 (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH) ?
257 " on reader keypad" : "");
258 return (-1);
259 }
260 if (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH)
261 verbose("Deferring PIN entry to reader keypad.");
262 else {
263 snprintf(prompt, sizeof(prompt), "Enter PIN for '%s': ",
264 si->token.label);
265 if ((pin = read_passphrase(prompt, RP_ALLOW_EOF)) == NULL) {
266 debug("%s: no pin specified", __func__);
267 return (-1); /* bail out */
268 }
269 }
djm@openbsd.org7d6034b2019-09-05 10:05:51 +0000270 rv = provider->function_list->C_Login(si->session, type, (u_char *)pin,
djm@openbsd.org21621712019-01-22 12:00:50 +0000271 (pin != NULL) ? strlen(pin) : 0);
272 if (pin != NULL)
273 freezero(pin, strlen(pin));
274 if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
275 error("C_Login failed: %lu", rv);
276 return (-1);
277 }
278 si->logged_in = 1;
279 return (0);
280}
281
282static int
djm@openbsd.org7d6034b2019-09-05 10:05:51 +0000283pkcs11_login(struct pkcs11_key *k11, CK_USER_TYPE type)
284{
285 if (k11 == NULL || k11->provider == NULL || !k11->provider->valid) {
286 error("no pkcs11 (valid) provider found");
287 return (-1);
288 }
289
290 return pkcs11_login_slot(k11->provider,
291 &k11->provider->slotinfo[k11->slotidx], type);
292}
293
294
295static int
djm@openbsd.org21621712019-01-22 12:00:50 +0000296pkcs11_check_obj_bool_attrib(struct pkcs11_key *k11, CK_OBJECT_HANDLE obj,
297 CK_ATTRIBUTE_TYPE type, int *val)
298{
299 struct pkcs11_slotinfo *si;
300 CK_FUNCTION_LIST *f;
301 CK_BBOOL flag = 0;
302 CK_ATTRIBUTE attr;
303 CK_RV rv;
304
305 *val = 0;
306
307 if (!k11->provider || !k11->provider->valid) {
308 error("no pkcs11 (valid) provider found");
309 return (-1);
310 }
311
312 f = k11->provider->function_list;
313 si = &k11->provider->slotinfo[k11->slotidx];
314
315 attr.type = type;
316 attr.pValue = &flag;
317 attr.ulValueLen = sizeof(flag);
318
319 rv = f->C_GetAttributeValue(si->session, obj, &attr, 1);
320 if (rv != CKR_OK) {
321 error("C_GetAttributeValue failed: %lu", rv);
322 return (-1);
323 }
324 *val = flag != 0;
325 debug("%s: provider %p slot %lu object %lu: attrib %lu = %d",
326 __func__, k11->provider, k11->slotidx, obj, type, *val);
327 return (0);
328}
329
330static int
djm@openbsd.org93f02102019-01-20 22:51:37 +0000331pkcs11_get_key(struct pkcs11_key *k11, CK_MECHANISM_TYPE mech_type)
Damien Miller7ea845e2010-02-12 09:21:02 +1100332{
Damien Miller7ea845e2010-02-12 09:21:02 +1100333 struct pkcs11_slotinfo *si;
334 CK_FUNCTION_LIST *f;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000335 CK_OBJECT_HANDLE obj;
336 CK_RV rv;
337 CK_OBJECT_CLASS private_key_class;
338 CK_BBOOL true_val;
339 CK_MECHANISM mech;
340 CK_ATTRIBUTE key_filter[3];
djm@openbsd.org21621712019-01-22 12:00:50 +0000341 int always_auth = 0;
342 int did_login = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100343
Damien Miller7ea845e2010-02-12 09:21:02 +1100344 if (!k11->provider || !k11->provider->valid) {
djm@openbsd.org93f02102019-01-20 22:51:37 +0000345 error("no pkcs11 (valid) provider found");
Damien Miller7ea845e2010-02-12 09:21:02 +1100346 return (-1);
347 }
djm@openbsd.org93f02102019-01-20 22:51:37 +0000348
Damien Miller7ea845e2010-02-12 09:21:02 +1100349 f = k11->provider->function_list;
350 si = &k11->provider->slotinfo[k11->slotidx];
djm@openbsd.org93f02102019-01-20 22:51:37 +0000351
Damien Miller7ea845e2010-02-12 09:21:02 +1100352 if ((si->token.flags & CKF_LOGIN_REQUIRED) && !si->logged_in) {
djm@openbsd.org21621712019-01-22 12:00:50 +0000353 if (pkcs11_login(k11, CKU_USER) < 0) {
354 error("login failed");
Damien Miller7ea845e2010-02-12 09:21:02 +1100355 return (-1);
356 }
djm@openbsd.org21621712019-01-22 12:00:50 +0000357 did_login = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100358 }
djm@openbsd.org93f02102019-01-20 22:51:37 +0000359
360 memset(&key_filter, 0, sizeof(key_filter));
361 private_key_class = CKO_PRIVATE_KEY;
362 key_filter[0].type = CKA_CLASS;
363 key_filter[0].pValue = &private_key_class;
364 key_filter[0].ulValueLen = sizeof(private_key_class);
365
366 key_filter[1].type = CKA_ID;
Damien Miller7ea845e2010-02-12 09:21:02 +1100367 key_filter[1].pValue = k11->keyid;
368 key_filter[1].ulValueLen = k11->keyid_len;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000369
370 true_val = CK_TRUE;
371 key_filter[2].type = CKA_SIGN;
372 key_filter[2].pValue = &true_val;
373 key_filter[2].ulValueLen = sizeof(true_val);
374
Damien Miller031c9102010-04-16 15:54:44 +1000375 /* try to find object w/CKA_SIGN first, retry w/o */
376 if (pkcs11_find(k11->provider, k11->slotidx, key_filter, 3, &obj) < 0 &&
377 pkcs11_find(k11->provider, k11->slotidx, key_filter, 2, &obj) < 0) {
378 error("cannot find private key");
djm@openbsd.org93f02102019-01-20 22:51:37 +0000379 return (-1);
Damien Miller7ea845e2010-02-12 09:21:02 +1100380 }
djm@openbsd.org93f02102019-01-20 22:51:37 +0000381
382 memset(&mech, 0, sizeof(mech));
383 mech.mechanism = mech_type;
384 mech.pParameter = NULL_PTR;
385 mech.ulParameterLen = 0;
386
387 if ((rv = f->C_SignInit(si->session, &mech, obj)) != CKR_OK) {
388 error("C_SignInit failed: %lu", rv);
389 return (-1);
390 }
391
djm@openbsd.org21621712019-01-22 12:00:50 +0000392 pkcs11_check_obj_bool_attrib(k11, obj, CKA_ALWAYS_AUTHENTICATE,
393 &always_auth); /* ignore errors here */
394 if (always_auth && !did_login) {
395 debug("%s: always-auth key", __func__);
396 if (pkcs11_login(k11, CKU_CONTEXT_SPECIFIC) < 0) {
397 error("login failed for always-auth key");
398 return (-1);
399 }
400 }
401
djm@openbsd.org93f02102019-01-20 22:51:37 +0000402 return (0);
403}
404
405/* openssl callback doing the actual signing operation */
406static int
407pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
408 int padding)
409{
410 struct pkcs11_key *k11;
411 struct pkcs11_slotinfo *si;
412 CK_FUNCTION_LIST *f;
413 CK_ULONG tlen = 0;
414 CK_RV rv;
415 int rval = -1;
416
djm@openbsd.orgce46c3a2019-01-21 02:01:03 +0000417 if ((k11 = RSA_get_ex_data(rsa, rsa_idx)) == NULL) {
djm@openbsd.orgf1185422019-01-20 23:08:24 +0000418 error("RSA_get_ex_data failed for rsa %p", rsa);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000419 return (-1);
420 }
421
422 if (pkcs11_get_key(k11, CKM_RSA_PKCS) == -1) {
423 error("pkcs11_get_key failed");
424 return (-1);
425 }
426
427 f = k11->provider->function_list;
428 si = &k11->provider->slotinfo[k11->slotidx];
429 tlen = RSA_size(rsa);
430
431 /* XXX handle CKR_BUFFER_TOO_SMALL */
432 rv = f->C_Sign(si->session, (CK_BYTE *)from, flen, to, &tlen);
433 if (rv == CKR_OK)
434 rval = tlen;
435 else
436 error("C_Sign failed: %lu", rv);
437
Damien Miller7ea845e2010-02-12 09:21:02 +1100438 return (rval);
439}
440
441static int
442pkcs11_rsa_private_decrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
443 int padding)
444{
445 return (-1);
446}
447
djm@openbsd.orgf1185422019-01-20 23:08:24 +0000448static int
449pkcs11_rsa_start_wrapper(void)
450{
451 if (rsa_method != NULL)
452 return (0);
453 rsa_method = RSA_meth_dup(RSA_get_default_method());
454 if (rsa_method == NULL)
455 return (-1);
djm@openbsd.org58622a82019-01-20 23:10:33 +0000456 rsa_idx = RSA_get_ex_new_index(0, "ssh-pkcs11-rsa",
457 NULL, NULL, pkcs11_k11_free);
458 if (rsa_idx == -1)
459 return (-1);
djm@openbsd.orgf1185422019-01-20 23:08:24 +0000460 if (!RSA_meth_set1_name(rsa_method, "pkcs11") ||
461 !RSA_meth_set_priv_enc(rsa_method, pkcs11_rsa_private_encrypt) ||
djm@openbsd.org58622a82019-01-20 23:10:33 +0000462 !RSA_meth_set_priv_dec(rsa_method, pkcs11_rsa_private_decrypt)) {
djm@openbsd.orgf1185422019-01-20 23:08:24 +0000463 error("%s: setup pkcs11 method failed", __func__);
464 return (-1);
465 }
466 return (0);
467}
468
Damien Miller7ea845e2010-02-12 09:21:02 +1100469/* redirect private key operations for rsa key to pkcs11 token */
470static int
471pkcs11_rsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
472 CK_ATTRIBUTE *keyid_attrib, RSA *rsa)
473{
474 struct pkcs11_key *k11;
djm@openbsd.orgf1185422019-01-20 23:08:24 +0000475
476 if (pkcs11_rsa_start_wrapper() == -1)
477 return (-1);
Damien Miller7ea845e2010-02-12 09:21:02 +1100478
479 k11 = xcalloc(1, sizeof(*k11));
480 k11->provider = provider;
481 provider->refcount++; /* provider referenced by RSA key */
482 k11->slotidx = slotidx;
483 /* identify key object on smartcard */
484 k11->keyid_len = keyid_attrib->ulValueLen;
djm@openbsd.orgd2d772f2016-02-12 00:20:30 +0000485 if (k11->keyid_len > 0) {
486 k11->keyid = xmalloc(k11->keyid_len);
487 memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
488 }
djm@openbsd.orgf1185422019-01-20 23:08:24 +0000489
djm@openbsd.orgfcb1b092019-01-20 23:12:35 +0000490 RSA_set_method(rsa, rsa_method);
djm@openbsd.org58622a82019-01-20 23:10:33 +0000491 RSA_set_ex_data(rsa, rsa_idx, k11);
Damien Miller7ea845e2010-02-12 09:21:02 +1100492 return (0);
493}
494
Damien Millere2cb4452019-01-21 11:32:28 +1100495#ifdef HAVE_EC_KEY_METHOD_NEW
djm@openbsd.org93f02102019-01-20 22:51:37 +0000496/* openssl callback doing the actual signing operation */
497static ECDSA_SIG *
498ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
499 const BIGNUM *rp, EC_KEY *ec)
500{
501 struct pkcs11_key *k11;
502 struct pkcs11_slotinfo *si;
503 CK_FUNCTION_LIST *f;
504 CK_ULONG siglen = 0, bnlen;
505 CK_RV rv;
506 ECDSA_SIG *ret = NULL;
507 u_char *sig;
djm@openbsd.org63297642019-01-21 00:47:34 +0000508 BIGNUM *r = NULL, *s = NULL;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000509
djm@openbsd.orgce46c3a2019-01-21 02:01:03 +0000510 if ((k11 = EC_KEY_get_ex_data(ec, ec_key_idx)) == NULL) {
djm@openbsd.org93f02102019-01-20 22:51:37 +0000511 ossl_error("EC_KEY_get_key_method_data failed for ec");
512 return (NULL);
513 }
514
515 if (pkcs11_get_key(k11, CKM_ECDSA) == -1) {
516 error("pkcs11_get_key failed");
517 return (NULL);
518 }
519
520 f = k11->provider->function_list;
521 si = &k11->provider->slotinfo[k11->slotidx];
522
523 siglen = ECDSA_size(ec);
524 sig = xmalloc(siglen);
525
526 /* XXX handle CKR_BUFFER_TOO_SMALL */
527 rv = f->C_Sign(si->session, (CK_BYTE *)dgst, dgst_len, sig, &siglen);
528 if (rv != CKR_OK) {
529 error("C_Sign failed: %lu", rv);
530 goto done;
531 }
djm@openbsd.org749aef32019-01-20 23:00:12 +0000532 if (siglen < 64 || siglen > 132 || siglen % 2) {
533 ossl_error("d2i_ECDSA_SIG failed");
534 goto done;
535 }
536 bnlen = siglen/2;
537 if ((ret = ECDSA_SIG_new()) == NULL) {
538 error("ECDSA_SIG_new failed");
539 goto done;
540 }
djm@openbsd.org63297642019-01-21 00:47:34 +0000541 if ((r = BN_bin2bn(sig, bnlen, NULL)) == NULL ||
542 (s = BN_bin2bn(sig+bnlen, bnlen, NULL)) == NULL) {
djm@openbsd.org749aef32019-01-20 23:00:12 +0000543 ossl_error("d2i_ECDSA_SIG failed");
544 ECDSA_SIG_free(ret);
545 ret = NULL;
546 goto done;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000547 }
djm@openbsd.org63297642019-01-21 00:47:34 +0000548 if (!ECDSA_SIG_set0(ret, r, s)) {
549 error("%s: ECDSA_SIG_set0 failed", __func__);
550 ECDSA_SIG_free(ret);
551 ret = NULL;
552 goto done;
553 }
554 r = s = NULL; /* now owned by ret */
555 /* success */
djm@openbsd.org93f02102019-01-20 22:51:37 +0000556 done:
djm@openbsd.org63297642019-01-21 00:47:34 +0000557 BN_free(r);
558 BN_free(s);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000559 free(sig);
560
561 return (ret);
562}
563
djm@openbsd.org93f02102019-01-20 22:51:37 +0000564static int
565pkcs11_ecdsa_start_wrapper(void)
566{
567 int (*orig_sign)(int, const unsigned char *, int, unsigned char *,
568 unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
569
570 if (ec_key_method != NULL)
571 return (0);
djm@openbsd.org445cfce2019-01-20 23:05:52 +0000572 ec_key_idx = EC_KEY_get_ex_new_index(0, "ssh-pkcs11-ecdsa",
573 NULL, NULL, pkcs11_k11_free);
574 if (ec_key_idx == -1)
575 return (-1);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000576 ec_key_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
577 if (ec_key_method == NULL)
578 return (-1);
579 EC_KEY_METHOD_get_sign(ec_key_method, &orig_sign, NULL, NULL);
580 EC_KEY_METHOD_set_sign(ec_key_method, orig_sign, NULL, ecdsa_do_sign);
581 return (0);
582}
583
584static int
585pkcs11_ecdsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
586 CK_ATTRIBUTE *keyid_attrib, EC_KEY *ec)
587{
588 struct pkcs11_key *k11;
589
590 if (pkcs11_ecdsa_start_wrapper() == -1)
591 return (-1);
592
593 k11 = xcalloc(1, sizeof(*k11));
594 k11->provider = provider;
595 provider->refcount++; /* provider referenced by ECDSA key */
596 k11->slotidx = slotidx;
597 /* identify key object on smartcard */
598 k11->keyid_len = keyid_attrib->ulValueLen;
599 k11->keyid = xmalloc(k11->keyid_len);
600 memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000601
djm@openbsd.orgfcb1b092019-01-20 23:12:35 +0000602 EC_KEY_set_method(ec, ec_key_method);
djm@openbsd.org445cfce2019-01-20 23:05:52 +0000603 EC_KEY_set_ex_data(ec, ec_key_idx, k11);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000604
605 return (0);
606}
Damien Millere2cb4452019-01-21 11:32:28 +1100607#endif /* HAVE_EC_KEY_METHOD_NEW */
djm@openbsd.org93f02102019-01-20 22:51:37 +0000608
Damien Miller7ea845e2010-02-12 09:21:02 +1100609/* remove trailing spaces */
610static void
Damien Miller746d1a62013-07-18 16:13:02 +1000611rmspace(u_char *buf, size_t len)
Damien Miller7ea845e2010-02-12 09:21:02 +1100612{
613 size_t i;
614
615 if (!len)
616 return;
617 for (i = len - 1; i > 0; i--)
618 if (i == len - 1 || buf[i] == ' ')
619 buf[i] = '\0';
620 else
621 break;
622}
623
624/*
625 * open a pkcs11 session and login if required.
626 * if pin == NULL we delay login until key use
627 */
628static int
djm@openbsd.org93f02102019-01-20 22:51:37 +0000629pkcs11_open_session(struct pkcs11_provider *p, CK_ULONG slotidx, char *pin,
630 CK_ULONG user)
Damien Miller7ea845e2010-02-12 09:21:02 +1100631{
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000632 struct pkcs11_slotinfo *si;
Damien Miller7ea845e2010-02-12 09:21:02 +1100633 CK_FUNCTION_LIST *f;
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000634 CK_RV rv;
Damien Miller7ea845e2010-02-12 09:21:02 +1100635 CK_SESSION_HANDLE session;
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000636 int login_required, have_pinpad, ret;
djm@openbsd.org7a7fdca2019-02-04 23:37:54 +0000637 char prompt[1024], *xpin = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100638
639 f = p->function_list;
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000640 si = &p->slotinfo[slotidx];
641
642 have_pinpad = si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH;
643 login_required = si->token.flags & CKF_LOGIN_REQUIRED;
644
645 /* fail early before opening session */
djm@openbsd.org7a7fdca2019-02-04 23:37:54 +0000646 if (login_required && !have_pinpad && !pkcs11_interactive &&
647 (pin == NULL || strlen(pin) == 0)) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100648 error("pin required");
djm@openbsd.org93f02102019-01-20 22:51:37 +0000649 return (-SSH_PKCS11_ERR_PIN_REQUIRED);
Damien Miller7ea845e2010-02-12 09:21:02 +1100650 }
651 if ((rv = f->C_OpenSession(p->slotlist[slotidx], CKF_RW_SESSION|
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000652 CKF_SERIAL_SESSION, NULL, NULL, &session)) != CKR_OK) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100653 error("C_OpenSession failed: %lu", rv);
654 return (-1);
655 }
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000656 if (login_required) {
657 if (have_pinpad && (pin == NULL || strlen(pin) == 0)) {
658 /* defer PIN entry to the reader keypad */
659 rv = f->C_Login(session, CKU_USER, NULL_PTR, 0);
660 } else {
djm@openbsd.org7a7fdca2019-02-04 23:37:54 +0000661 if (pkcs11_interactive) {
662 snprintf(prompt, sizeof(prompt),
663 "Enter PIN for '%s': ", si->token.label);
664 if ((xpin = read_passphrase(prompt,
665 RP_ALLOW_EOF)) == NULL) {
666 debug("%s: no pin specified",
667 __func__);
668 return (-SSH_PKCS11_ERR_PIN_REQUIRED);
669 }
670 pin = xpin;
671 }
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000672 rv = f->C_Login(session, CKU_USER,
673 (u_char *)pin, strlen(pin));
djm@openbsd.org7a7fdca2019-02-04 23:37:54 +0000674 if (xpin != NULL)
675 freezero(xpin, strlen(xpin));
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000676 }
djm@openbsd.orgcb3bde32015-02-02 22:48:53 +0000677 if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100678 error("C_Login failed: %lu", rv);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000679 ret = (rv == CKR_PIN_LOCKED) ?
680 -SSH_PKCS11_ERR_PIN_LOCKED :
681 -SSH_PKCS11_ERR_LOGIN_FAIL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100682 if ((rv = f->C_CloseSession(session)) != CKR_OK)
683 error("C_CloseSession failed: %lu", rv);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000684 return (ret);
Damien Miller7ea845e2010-02-12 09:21:02 +1100685 }
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000686 si->logged_in = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100687 }
djm@openbsd.org41923ce2019-01-22 12:03:58 +0000688 si->session = session;
Damien Miller7ea845e2010-02-12 09:21:02 +1100689 return (0);
690}
691
Damien Millerd2252c72013-11-04 07:41:48 +1100692static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000693pkcs11_key_included(struct sshkey ***keysp, int *nkeys, struct sshkey *key)
Damien Millerd2252c72013-11-04 07:41:48 +1100694{
695 int i;
696
697 for (i = 0; i < *nkeys; i++)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000698 if (sshkey_equal(key, (*keysp)[i]))
Damien Millerd2252c72013-11-04 07:41:48 +1100699 return (1);
700 return (0);
701}
702
Damien Millere2cb4452019-01-21 11:32:28 +1100703#ifdef HAVE_EC_KEY_METHOD_NEW
djm@openbsd.org93f02102019-01-20 22:51:37 +0000704static struct sshkey *
705pkcs11_fetch_ecdsa_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
706 CK_OBJECT_HANDLE *obj)
707{
708 CK_ATTRIBUTE key_attr[3];
709 CK_SESSION_HANDLE session;
710 CK_FUNCTION_LIST *f = NULL;
711 CK_RV rv;
djm@openbsd.org24757c12019-01-20 23:01:59 +0000712 ASN1_OCTET_STRING *octet = NULL;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000713 EC_KEY *ec = NULL;
714 EC_GROUP *group = NULL;
715 struct sshkey *key = NULL;
716 const unsigned char *attrp = NULL;
717 int i;
718 int nid;
719
720 memset(&key_attr, 0, sizeof(key_attr));
721 key_attr[0].type = CKA_ID;
722 key_attr[1].type = CKA_EC_POINT;
723 key_attr[2].type = CKA_EC_PARAMS;
724
725 session = p->slotinfo[slotidx].session;
726 f = p->function_list;
727
728 /* figure out size of the attributes */
729 rv = f->C_GetAttributeValue(session, *obj, key_attr, 3);
730 if (rv != CKR_OK) {
731 error("C_GetAttributeValue failed: %lu", rv);
732 return (NULL);
733 }
734
735 /*
736 * Allow CKA_ID (always first attribute) to be empty, but
737 * ensure that none of the others are zero length.
738 * XXX assumes CKA_ID is always first.
739 */
740 if (key_attr[1].ulValueLen == 0 ||
741 key_attr[2].ulValueLen == 0) {
742 error("invalid attribute length");
743 return (NULL);
744 }
745
746 /* allocate buffers for attributes */
747 for (i = 0; i < 3; i++)
748 if (key_attr[i].ulValueLen > 0)
749 key_attr[i].pValue = xcalloc(1, key_attr[i].ulValueLen);
750
751 /* retrieve ID, public point and curve parameters of EC key */
752 rv = f->C_GetAttributeValue(session, *obj, key_attr, 3);
753 if (rv != CKR_OK) {
754 error("C_GetAttributeValue failed: %lu", rv);
755 goto fail;
756 }
757
758 ec = EC_KEY_new();
759 if (ec == NULL) {
760 error("EC_KEY_new failed");
761 goto fail;
762 }
763
764 attrp = key_attr[2].pValue;
765 group = d2i_ECPKParameters(NULL, &attrp, key_attr[2].ulValueLen);
766 if (group == NULL) {
767 ossl_error("d2i_ECPKParameters failed");
768 goto fail;
769 }
770
771 if (EC_KEY_set_group(ec, group) == 0) {
772 ossl_error("EC_KEY_set_group failed");
773 goto fail;
774 }
775
776 if (key_attr[1].ulValueLen <= 2) {
777 error("CKA_EC_POINT too small");
778 goto fail;
779 }
780
djm@openbsd.org24757c12019-01-20 23:01:59 +0000781 attrp = key_attr[1].pValue;
782 octet = d2i_ASN1_OCTET_STRING(NULL, &attrp, key_attr[1].ulValueLen);
783 if (octet == NULL) {
784 ossl_error("d2i_ASN1_OCTET_STRING failed");
785 goto fail;
786 }
787 attrp = octet->data;
788 if (o2i_ECPublicKey(&ec, &attrp, octet->length) == NULL) {
789 ossl_error("o2i_ECPublicKey failed");
790 goto fail;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000791 }
792
793 nid = sshkey_ecdsa_key_to_nid(ec);
794 if (nid < 0) {
795 error("couldn't get curve nid");
796 goto fail;
797 }
798
799 if (pkcs11_ecdsa_wrap(p, slotidx, &key_attr[0], ec))
800 goto fail;
801
802 key = sshkey_new(KEY_UNSPEC);
803 if (key == NULL) {
804 error("sshkey_new failed");
805 goto fail;
806 }
807
808 key->ecdsa = ec;
809 key->ecdsa_nid = nid;
810 key->type = KEY_ECDSA;
811 key->flags |= SSHKEY_FLAG_EXT;
812 ec = NULL; /* now owned by key */
813
814fail:
815 for (i = 0; i < 3; i++)
816 free(key_attr[i].pValue);
817 if (ec)
818 EC_KEY_free(ec);
819 if (group)
820 EC_GROUP_free(group);
djm@openbsd.org24757c12019-01-20 23:01:59 +0000821 if (octet)
822 ASN1_OCTET_STRING_free(octet);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000823
824 return (key);
825}
Damien Millere2cb4452019-01-21 11:32:28 +1100826#endif /* HAVE_EC_KEY_METHOD_NEW */
djm@openbsd.org93f02102019-01-20 22:51:37 +0000827
828static struct sshkey *
829pkcs11_fetch_rsa_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
830 CK_OBJECT_HANDLE *obj)
831{
832 CK_ATTRIBUTE key_attr[3];
833 CK_SESSION_HANDLE session;
834 CK_FUNCTION_LIST *f = NULL;
835 CK_RV rv;
836 RSA *rsa = NULL;
837 BIGNUM *rsa_n, *rsa_e;
838 struct sshkey *key = NULL;
839 int i;
840
841 memset(&key_attr, 0, sizeof(key_attr));
842 key_attr[0].type = CKA_ID;
843 key_attr[1].type = CKA_MODULUS;
844 key_attr[2].type = CKA_PUBLIC_EXPONENT;
845
846 session = p->slotinfo[slotidx].session;
847 f = p->function_list;
848
849 /* figure out size of the attributes */
850 rv = f->C_GetAttributeValue(session, *obj, key_attr, 3);
851 if (rv != CKR_OK) {
852 error("C_GetAttributeValue failed: %lu", rv);
853 return (NULL);
854 }
855
856 /*
857 * Allow CKA_ID (always first attribute) to be empty, but
858 * ensure that none of the others are zero length.
859 * XXX assumes CKA_ID is always first.
860 */
861 if (key_attr[1].ulValueLen == 0 ||
862 key_attr[2].ulValueLen == 0) {
863 error("invalid attribute length");
864 return (NULL);
865 }
866
867 /* allocate buffers for attributes */
868 for (i = 0; i < 3; i++)
869 if (key_attr[i].ulValueLen > 0)
870 key_attr[i].pValue = xcalloc(1, key_attr[i].ulValueLen);
871
872 /* retrieve ID, modulus and public exponent of RSA key */
873 rv = f->C_GetAttributeValue(session, *obj, key_attr, 3);
874 if (rv != CKR_OK) {
875 error("C_GetAttributeValue failed: %lu", rv);
876 goto fail;
877 }
878
879 rsa = RSA_new();
880 if (rsa == NULL) {
881 error("RSA_new failed");
882 goto fail;
883 }
884
885 rsa_n = BN_bin2bn(key_attr[1].pValue, key_attr[1].ulValueLen, NULL);
886 rsa_e = BN_bin2bn(key_attr[2].pValue, key_attr[2].ulValueLen, NULL);
887 if (rsa_n == NULL || rsa_e == NULL) {
888 error("BN_bin2bn failed");
889 goto fail;
890 }
891 if (!RSA_set0_key(rsa, rsa_n, rsa_e, NULL))
892 fatal("%s: set key", __func__);
893 rsa_n = rsa_e = NULL; /* transferred */
894
895 if (pkcs11_rsa_wrap(p, slotidx, &key_attr[0], rsa))
896 goto fail;
897
898 key = sshkey_new(KEY_UNSPEC);
899 if (key == NULL) {
900 error("sshkey_new failed");
901 goto fail;
902 }
903
904 key->rsa = rsa;
905 key->type = KEY_RSA;
906 key->flags |= SSHKEY_FLAG_EXT;
907 rsa = NULL; /* now owned by key */
908
909fail:
910 for (i = 0; i < 3; i++)
911 free(key_attr[i].pValue);
912 RSA_free(rsa);
913
914 return (key);
915}
916
917static struct sshkey *
918pkcs11_fetch_x509_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
919 CK_OBJECT_HANDLE *obj)
920{
921 CK_ATTRIBUTE cert_attr[3];
922 CK_SESSION_HANDLE session;
923 CK_FUNCTION_LIST *f = NULL;
924 CK_RV rv;
925 X509 *x509 = NULL;
926 EVP_PKEY *evp;
927 RSA *rsa = NULL;
Darren Tucker97370f62019-05-17 10:54:51 +1000928#ifdef OPENSSL_HAS_ECC
djm@openbsd.org93f02102019-01-20 22:51:37 +0000929 EC_KEY *ec = NULL;
Darren Tucker97370f62019-05-17 10:54:51 +1000930#endif
djm@openbsd.org93f02102019-01-20 22:51:37 +0000931 struct sshkey *key = NULL;
932 int i;
Damien Millere2cb4452019-01-21 11:32:28 +1100933#ifdef HAVE_EC_KEY_METHOD_NEW
djm@openbsd.org93f02102019-01-20 22:51:37 +0000934 int nid;
Damien Millere2cb4452019-01-21 11:32:28 +1100935#endif
djm@openbsd.org93f02102019-01-20 22:51:37 +0000936 const u_char *cp;
937
938 memset(&cert_attr, 0, sizeof(cert_attr));
939 cert_attr[0].type = CKA_ID;
940 cert_attr[1].type = CKA_SUBJECT;
941 cert_attr[2].type = CKA_VALUE;
942
943 session = p->slotinfo[slotidx].session;
944 f = p->function_list;
945
946 /* figure out size of the attributes */
947 rv = f->C_GetAttributeValue(session, *obj, cert_attr, 3);
948 if (rv != CKR_OK) {
949 error("C_GetAttributeValue failed: %lu", rv);
950 return (NULL);
951 }
952
953 /*
954 * Allow CKA_ID (always first attribute) to be empty, but
955 * ensure that none of the others are zero length.
956 * XXX assumes CKA_ID is always first.
957 */
958 if (cert_attr[1].ulValueLen == 0 ||
959 cert_attr[2].ulValueLen == 0) {
960 error("invalid attribute length");
961 return (NULL);
962 }
963
964 /* allocate buffers for attributes */
965 for (i = 0; i < 3; i++)
966 if (cert_attr[i].ulValueLen > 0)
967 cert_attr[i].pValue = xcalloc(1, cert_attr[i].ulValueLen);
968
969 /* retrieve ID, subject and value of certificate */
970 rv = f->C_GetAttributeValue(session, *obj, cert_attr, 3);
971 if (rv != CKR_OK) {
972 error("C_GetAttributeValue failed: %lu", rv);
973 goto fail;
974 }
975
976 x509 = X509_new();
977 if (x509 == NULL) {
978 error("x509_new failed");
979 goto fail;
980 }
981
982 cp = cert_attr[2].pValue;
983 if (d2i_X509(&x509, &cp, cert_attr[2].ulValueLen) == NULL) {
984 error("d2i_x509 failed");
985 goto fail;
986 }
987
988 evp = X509_get_pubkey(x509);
989 if (evp == NULL) {
990 error("X509_get_pubkey failed");
991 goto fail;
992 }
993
994 if (EVP_PKEY_base_id(evp) == EVP_PKEY_RSA) {
995 if (EVP_PKEY_get0_RSA(evp) == NULL) {
996 error("invalid x509; no rsa key");
997 goto fail;
998 }
999 if ((rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(evp))) == NULL) {
1000 error("RSAPublicKey_dup failed");
1001 goto fail;
1002 }
1003
1004 if (pkcs11_rsa_wrap(p, slotidx, &cert_attr[0], rsa))
1005 goto fail;
1006
1007 key = sshkey_new(KEY_UNSPEC);
1008 if (key == NULL) {
1009 error("sshkey_new failed");
1010 goto fail;
1011 }
1012
1013 key->rsa = rsa;
1014 key->type = KEY_RSA;
1015 key->flags |= SSHKEY_FLAG_EXT;
1016 rsa = NULL; /* now owned by key */
Damien Millere2cb4452019-01-21 11:32:28 +11001017#ifdef HAVE_EC_KEY_METHOD_NEW
djm@openbsd.org93f02102019-01-20 22:51:37 +00001018 } else if (EVP_PKEY_base_id(evp) == EVP_PKEY_EC) {
djm@openbsd.org8a246752019-01-20 23:03:26 +00001019 if (EVP_PKEY_get0_EC_KEY(evp) == NULL) {
djm@openbsd.org93f02102019-01-20 22:51:37 +00001020 error("invalid x509; no ec key");
1021 goto fail;
1022 }
djm@openbsd.org8a246752019-01-20 23:03:26 +00001023 if ((ec = EC_KEY_dup(EVP_PKEY_get0_EC_KEY(evp))) == NULL) {
djm@openbsd.org93f02102019-01-20 22:51:37 +00001024 error("EC_KEY_dup failed");
1025 goto fail;
1026 }
1027
1028 nid = sshkey_ecdsa_key_to_nid(ec);
1029 if (nid < 0) {
1030 error("couldn't get curve nid");
1031 goto fail;
1032 }
1033
1034 if (pkcs11_ecdsa_wrap(p, slotidx, &cert_attr[0], ec))
1035 goto fail;
1036
1037 key = sshkey_new(KEY_UNSPEC);
1038 if (key == NULL) {
1039 error("sshkey_new failed");
1040 goto fail;
1041 }
1042
1043 key->ecdsa = ec;
1044 key->ecdsa_nid = nid;
1045 key->type = KEY_ECDSA;
1046 key->flags |= SSHKEY_FLAG_EXT;
1047 ec = NULL; /* now owned by key */
Damien Millere2cb4452019-01-21 11:32:28 +11001048#endif /* HAVE_EC_KEY_METHOD_NEW */
djm@openbsd.org93f02102019-01-20 22:51:37 +00001049 } else
1050 error("unknown certificate key type");
1051
1052fail:
1053 for (i = 0; i < 3; i++)
1054 free(cert_attr[i].pValue);
1055 X509_free(x509);
1056 RSA_free(rsa);
Darren Tucker97370f62019-05-17 10:54:51 +10001057#ifdef OPENSSL_HAS_ECC
djm@openbsd.org93f02102019-01-20 22:51:37 +00001058 EC_KEY_free(ec);
Darren Tucker97370f62019-05-17 10:54:51 +10001059#endif
djm@openbsd.org93f02102019-01-20 22:51:37 +00001060
1061 return (key);
1062}
1063
1064#if 0
Damien Millerd2252c72013-11-04 07:41:48 +11001065static int
djm@openbsd.org482d23b2018-09-13 02:08:33 +00001066have_rsa_key(const RSA *rsa)
1067{
1068 const BIGNUM *rsa_n, *rsa_e;
1069
1070 RSA_get0_key(rsa, &rsa_n, &rsa_e, NULL);
1071 return rsa_n != NULL && rsa_e != NULL;
1072}
djm@openbsd.org93f02102019-01-20 22:51:37 +00001073#endif
djm@openbsd.org482d23b2018-09-13 02:08:33 +00001074
djm@openbsd.org93f02102019-01-20 22:51:37 +00001075/*
1076 * lookup certificates for token in slot identified by slotidx,
1077 * add 'wrapped' public keys to the 'keysp' array and increment nkeys.
1078 * keysp points to an (possibly empty) array with *nkeys keys.
1079 */
djm@openbsd.org482d23b2018-09-13 02:08:33 +00001080static int
djm@openbsd.org93f02102019-01-20 22:51:37 +00001081pkcs11_fetch_certs(struct pkcs11_provider *p, CK_ULONG slotidx,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001082 struct sshkey ***keysp, int *nkeys)
Damien Miller7ea845e2010-02-12 09:21:02 +11001083{
djm@openbsd.org93f02102019-01-20 22:51:37 +00001084 struct sshkey *key = NULL;
1085 CK_OBJECT_CLASS key_class;
1086 CK_ATTRIBUTE key_attr[1];
1087 CK_SESSION_HANDLE session;
1088 CK_FUNCTION_LIST *f = NULL;
1089 CK_RV rv;
1090 CK_OBJECT_HANDLE obj;
1091 CK_ULONG n = 0;
1092 int ret = -1;
Tim Rice179eee02010-03-04 12:48:05 -08001093
djm@openbsd.org93f02102019-01-20 22:51:37 +00001094 memset(&key_attr, 0, sizeof(key_attr));
1095 memset(&obj, 0, sizeof(obj));
1096
1097 key_class = CKO_CERTIFICATE;
1098 key_attr[0].type = CKA_CLASS;
1099 key_attr[0].pValue = &key_class;
1100 key_attr[0].ulValueLen = sizeof(key_class);
1101
Damien Miller7ea845e2010-02-12 09:21:02 +11001102 session = p->slotinfo[slotidx].session;
djm@openbsd.org93f02102019-01-20 22:51:37 +00001103 f = p->function_list;
1104
1105 rv = f->C_FindObjectsInit(session, key_attr, 1);
1106 if (rv != CKR_OK) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001107 error("C_FindObjectsInit failed: %lu", rv);
djm@openbsd.org93f02102019-01-20 22:51:37 +00001108 goto fail;
Damien Miller7ea845e2010-02-12 09:21:02 +11001109 }
djm@openbsd.org93f02102019-01-20 22:51:37 +00001110
Damien Miller7ea845e2010-02-12 09:21:02 +11001111 while (1) {
djm@openbsd.org93f02102019-01-20 22:51:37 +00001112 CK_CERTIFICATE_TYPE ck_cert_type;
1113
1114 rv = f->C_FindObjects(session, &obj, 1, &n);
1115 if (rv != CKR_OK) {
1116 error("C_FindObjects failed: %lu", rv);
1117 goto fail;
Damien Miller7ea845e2010-02-12 09:21:02 +11001118 }
djm@openbsd.org93f02102019-01-20 22:51:37 +00001119 if (n == 0)
Damien Miller7ea845e2010-02-12 09:21:02 +11001120 break;
djm@openbsd.org93f02102019-01-20 22:51:37 +00001121
1122 memset(&ck_cert_type, 0, sizeof(ck_cert_type));
1123 memset(&key_attr, 0, sizeof(key_attr));
1124 key_attr[0].type = CKA_CERTIFICATE_TYPE;
1125 key_attr[0].pValue = &ck_cert_type;
1126 key_attr[0].ulValueLen = sizeof(ck_cert_type);
1127
1128 rv = f->C_GetAttributeValue(session, obj, key_attr, 1);
1129 if (rv != CKR_OK) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001130 error("C_GetAttributeValue failed: %lu", rv);
djm@openbsd.org93f02102019-01-20 22:51:37 +00001131 goto fail;
djm@openbsd.org63ebcd02015-07-18 08:02:17 +00001132 }
1133
djm@openbsd.org93f02102019-01-20 22:51:37 +00001134 switch (ck_cert_type) {
1135 case CKC_X_509:
1136 key = pkcs11_fetch_x509_pubkey(p, slotidx, &obj);
1137 break;
1138 default:
1139 /* XXX print key type? */
markus@openbsd.org2aee9a42019-03-08 17:24:43 +00001140 key = NULL;
djm@openbsd.org93f02102019-01-20 22:51:37 +00001141 error("skipping unsupported certificate type");
1142 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +00001143
djm@openbsd.org93f02102019-01-20 22:51:37 +00001144 if (key == NULL) {
1145 error("failed to fetch key");
1146 continue;
1147 }
1148
1149 if (pkcs11_key_included(keysp, nkeys, key)) {
1150 sshkey_free(key);
Damien Miller7ea845e2010-02-12 09:21:02 +11001151 } else {
djm@openbsd.org93f02102019-01-20 22:51:37 +00001152 /* expand key array and add key */
1153 *keysp = xrecallocarray(*keysp, *nkeys,
1154 *nkeys + 1, sizeof(struct sshkey *));
1155 (*keysp)[*nkeys] = key;
1156 *nkeys = *nkeys + 1;
1157 debug("have %d keys", *nkeys);
Damien Millerd2252c72013-11-04 07:41:48 +11001158 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001159 }
djm@openbsd.org93f02102019-01-20 22:51:37 +00001160
1161 ret = 0;
1162fail:
1163 rv = f->C_FindObjectsFinal(session);
1164 if (rv != CKR_OK) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001165 error("C_FindObjectsFinal failed: %lu", rv);
djm@openbsd.org93f02102019-01-20 22:51:37 +00001166 ret = -1;
1167 }
1168
1169 return (ret);
Damien Miller7ea845e2010-02-12 09:21:02 +11001170}
1171
djm@openbsd.org93f02102019-01-20 22:51:37 +00001172/*
1173 * lookup public keys for token in slot identified by slotidx,
1174 * add 'wrapped' public keys to the 'keysp' array and increment nkeys.
1175 * keysp points to an (possibly empty) array with *nkeys keys.
1176 */
1177static int
1178pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx,
1179 struct sshkey ***keysp, int *nkeys)
1180{
1181 struct sshkey *key = NULL;
1182 CK_OBJECT_CLASS key_class;
1183 CK_ATTRIBUTE key_attr[1];
1184 CK_SESSION_HANDLE session;
1185 CK_FUNCTION_LIST *f = NULL;
1186 CK_RV rv;
1187 CK_OBJECT_HANDLE obj;
1188 CK_ULONG n = 0;
1189 int ret = -1;
1190
1191 memset(&key_attr, 0, sizeof(key_attr));
1192 memset(&obj, 0, sizeof(obj));
1193
1194 key_class = CKO_PUBLIC_KEY;
1195 key_attr[0].type = CKA_CLASS;
1196 key_attr[0].pValue = &key_class;
1197 key_attr[0].ulValueLen = sizeof(key_class);
1198
1199 session = p->slotinfo[slotidx].session;
1200 f = p->function_list;
1201
1202 rv = f->C_FindObjectsInit(session, key_attr, 1);
1203 if (rv != CKR_OK) {
1204 error("C_FindObjectsInit failed: %lu", rv);
1205 goto fail;
1206 }
1207
1208 while (1) {
1209 CK_KEY_TYPE ck_key_type;
1210
1211 rv = f->C_FindObjects(session, &obj, 1, &n);
1212 if (rv != CKR_OK) {
1213 error("C_FindObjects failed: %lu", rv);
1214 goto fail;
1215 }
1216 if (n == 0)
1217 break;
1218
1219 memset(&ck_key_type, 0, sizeof(ck_key_type));
1220 memset(&key_attr, 0, sizeof(key_attr));
1221 key_attr[0].type = CKA_KEY_TYPE;
1222 key_attr[0].pValue = &ck_key_type;
1223 key_attr[0].ulValueLen = sizeof(ck_key_type);
1224
1225 rv = f->C_GetAttributeValue(session, obj, key_attr, 1);
1226 if (rv != CKR_OK) {
1227 error("C_GetAttributeValue failed: %lu", rv);
1228 goto fail;
1229 }
1230
1231 switch (ck_key_type) {
1232 case CKK_RSA:
1233 key = pkcs11_fetch_rsa_pubkey(p, slotidx, &obj);
1234 break;
Damien Millere2cb4452019-01-21 11:32:28 +11001235#ifdef HAVE_EC_KEY_METHOD_NEW
djm@openbsd.org93f02102019-01-20 22:51:37 +00001236 case CKK_ECDSA:
1237 key = pkcs11_fetch_ecdsa_pubkey(p, slotidx, &obj);
1238 break;
Damien Millere2cb4452019-01-21 11:32:28 +11001239#endif /* HAVE_EC_KEY_METHOD_NEW */
djm@openbsd.org93f02102019-01-20 22:51:37 +00001240 default:
1241 /* XXX print key type? */
markus@openbsd.org2aee9a42019-03-08 17:24:43 +00001242 key = NULL;
djm@openbsd.org93f02102019-01-20 22:51:37 +00001243 error("skipping unsupported key type");
1244 }
1245
1246 if (key == NULL) {
1247 error("failed to fetch key");
1248 continue;
1249 }
1250
1251 if (pkcs11_key_included(keysp, nkeys, key)) {
1252 sshkey_free(key);
1253 } else {
1254 /* expand key array and add key */
1255 *keysp = xrecallocarray(*keysp, *nkeys,
1256 *nkeys + 1, sizeof(struct sshkey *));
1257 (*keysp)[*nkeys] = key;
1258 *nkeys = *nkeys + 1;
1259 debug("have %d keys", *nkeys);
1260 }
1261 }
1262
1263 ret = 0;
1264fail:
1265 rv = f->C_FindObjectsFinal(session);
1266 if (rv != CKR_OK) {
1267 error("C_FindObjectsFinal failed: %lu", rv);
1268 ret = -1;
1269 }
1270
1271 return (ret);
1272}
1273
1274#ifdef WITH_PKCS11_KEYGEN
1275#define FILL_ATTR(attr, idx, typ, val, len) \
1276 { (attr[idx]).type=(typ); (attr[idx]).pValue=(val); (attr[idx]).ulValueLen=len; idx++; }
1277
1278static struct sshkey *
1279pkcs11_rsa_generate_private_key(struct pkcs11_provider *p, CK_ULONG slotidx,
1280 char *label, CK_ULONG bits, CK_BYTE keyid, u_int32_t *err)
1281{
1282 struct pkcs11_slotinfo *si;
1283 char *plabel = label ? label : "";
1284 int npub = 0, npriv = 0;
1285 CK_RV rv;
1286 CK_FUNCTION_LIST *f;
1287 CK_SESSION_HANDLE session;
1288 CK_BBOOL true_val = CK_TRUE, false_val = CK_FALSE;
1289 CK_OBJECT_HANDLE pubKey, privKey;
1290 CK_ATTRIBUTE tpub[16], tpriv[16];
1291 CK_MECHANISM mech = {
1292 CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0
1293 };
1294 CK_BYTE pubExponent[] = {
1295 0x01, 0x00, 0x01 /* RSA_F4 in bytes */
1296 };
1297 pubkey_filter[0].pValue = &pubkey_class;
1298 cert_filter[0].pValue = &cert_class;
1299
1300 *err = 0;
1301
1302 FILL_ATTR(tpub, npub, CKA_TOKEN, &true_val, sizeof(true_val));
1303 FILL_ATTR(tpub, npub, CKA_LABEL, plabel, strlen(plabel));
1304 FILL_ATTR(tpub, npub, CKA_ENCRYPT, &false_val, sizeof(false_val));
1305 FILL_ATTR(tpub, npub, CKA_VERIFY, &true_val, sizeof(true_val));
1306 FILL_ATTR(tpub, npub, CKA_VERIFY_RECOVER, &false_val,
1307 sizeof(false_val));
1308 FILL_ATTR(tpub, npub, CKA_WRAP, &false_val, sizeof(false_val));
1309 FILL_ATTR(tpub, npub, CKA_DERIVE, &false_val, sizeof(false_val));
1310 FILL_ATTR(tpub, npub, CKA_MODULUS_BITS, &bits, sizeof(bits));
1311 FILL_ATTR(tpub, npub, CKA_PUBLIC_EXPONENT, pubExponent,
1312 sizeof(pubExponent));
1313 FILL_ATTR(tpub, npub, CKA_ID, &keyid, sizeof(keyid));
1314
1315 FILL_ATTR(tpriv, npriv, CKA_TOKEN, &true_val, sizeof(true_val));
1316 FILL_ATTR(tpriv, npriv, CKA_LABEL, plabel, strlen(plabel));
1317 FILL_ATTR(tpriv, npriv, CKA_PRIVATE, &true_val, sizeof(true_val));
1318 FILL_ATTR(tpriv, npriv, CKA_SENSITIVE, &true_val, sizeof(true_val));
1319 FILL_ATTR(tpriv, npriv, CKA_DECRYPT, &false_val, sizeof(false_val));
1320 FILL_ATTR(tpriv, npriv, CKA_SIGN, &true_val, sizeof(true_val));
1321 FILL_ATTR(tpriv, npriv, CKA_SIGN_RECOVER, &false_val,
1322 sizeof(false_val));
1323 FILL_ATTR(tpriv, npriv, CKA_UNWRAP, &false_val, sizeof(false_val));
1324 FILL_ATTR(tpriv, npriv, CKA_DERIVE, &false_val, sizeof(false_val));
1325 FILL_ATTR(tpriv, npriv, CKA_ID, &keyid, sizeof(keyid));
1326
1327 f = p->function_list;
1328 si = &p->slotinfo[slotidx];
1329 session = si->session;
1330
1331 if ((rv = f->C_GenerateKeyPair(session, &mech, tpub, npub, tpriv, npriv,
1332 &pubKey, &privKey)) != CKR_OK) {
1333 error("%s: key generation failed: error 0x%lx", __func__, rv);
1334 *err = rv;
1335 return NULL;
1336 }
1337
1338 return pkcs11_fetch_rsa_pubkey(p, slotidx, &pubKey);
1339}
1340
1341static int
1342pkcs11_decode_hex(const char *hex, unsigned char **dest, size_t *rlen)
1343{
1344 size_t i, len;
1345 char ptr[3];
1346
1347 if (dest)
1348 *dest = NULL;
1349 if (rlen)
1350 *rlen = 0;
1351
1352 if ((len = strlen(hex)) % 2)
1353 return -1;
1354 len /= 2;
1355
1356 *dest = xmalloc(len);
1357
1358 ptr[2] = '\0';
1359 for (i = 0; i < len; i++) {
1360 ptr[0] = hex[2 * i];
1361 ptr[1] = hex[(2 * i) + 1];
1362 if (!isxdigit(ptr[0]) || !isxdigit(ptr[1]))
1363 return -1;
1364 (*dest)[i] = (unsigned char)strtoul(ptr, NULL, 16);
1365 }
1366
1367 if (rlen)
1368 *rlen = len;
1369
1370 return 0;
1371}
1372
1373static struct ec_curve_info {
1374 const char *name;
1375 const char *oid;
1376 const char *oid_encoded;
1377 size_t size;
1378} ec_curve_infos[] = {
1379 {"prime256v1", "1.2.840.10045.3.1.7", "06082A8648CE3D030107", 256},
1380 {"secp384r1", "1.3.132.0.34", "06052B81040022", 384},
1381 {"secp521r1", "1.3.132.0.35", "06052B81040023", 521},
djm@openbsd.org65294092019-01-20 23:11:11 +00001382 {NULL, NULL, NULL, 0},
djm@openbsd.org93f02102019-01-20 22:51:37 +00001383};
1384
1385static struct sshkey *
1386pkcs11_ecdsa_generate_private_key(struct pkcs11_provider *p, CK_ULONG slotidx,
1387 char *label, CK_ULONG bits, CK_BYTE keyid, u_int32_t *err)
1388{
1389 struct pkcs11_slotinfo *si;
1390 char *plabel = label ? label : "";
1391 int i;
1392 size_t ecparams_size;
1393 unsigned char *ecparams = NULL;
1394 int npub = 0, npriv = 0;
1395 CK_RV rv;
1396 CK_FUNCTION_LIST *f;
1397 CK_SESSION_HANDLE session;
1398 CK_BBOOL true_val = CK_TRUE, false_val = CK_FALSE;
1399 CK_OBJECT_HANDLE pubKey, privKey;
1400 CK_MECHANISM mech = {
1401 CKM_EC_KEY_PAIR_GEN, NULL_PTR, 0
1402 };
1403 CK_ATTRIBUTE tpub[16], tpriv[16];
1404
1405 *err = 0;
1406
1407 for (i = 0; ec_curve_infos[i].name; i++) {
1408 if (ec_curve_infos[i].size == bits)
1409 break;
1410 }
1411 if (!ec_curve_infos[i].name) {
1412 error("%s: invalid key size %lu", __func__, bits);
1413 return NULL;
1414 }
1415 if (pkcs11_decode_hex(ec_curve_infos[i].oid_encoded, &ecparams,
1416 &ecparams_size) == -1) {
1417 error("%s: invalid oid", __func__);
1418 return NULL;
1419 }
1420
1421 FILL_ATTR(tpub, npub, CKA_TOKEN, &true_val, sizeof(true_val));
1422 FILL_ATTR(tpub, npub, CKA_LABEL, plabel, strlen(plabel));
1423 FILL_ATTR(tpub, npub, CKA_ENCRYPT, &false_val, sizeof(false_val));
1424 FILL_ATTR(tpub, npub, CKA_VERIFY, &true_val, sizeof(true_val));
1425 FILL_ATTR(tpub, npub, CKA_VERIFY_RECOVER, &false_val,
1426 sizeof(false_val));
1427 FILL_ATTR(tpub, npub, CKA_WRAP, &false_val, sizeof(false_val));
1428 FILL_ATTR(tpub, npub, CKA_DERIVE, &false_val, sizeof(false_val));
1429 FILL_ATTR(tpub, npub, CKA_EC_PARAMS, ecparams, ecparams_size);
1430 FILL_ATTR(tpub, npub, CKA_ID, &keyid, sizeof(keyid));
1431
1432 FILL_ATTR(tpriv, npriv, CKA_TOKEN, &true_val, sizeof(true_val));
1433 FILL_ATTR(tpriv, npriv, CKA_LABEL, plabel, strlen(plabel));
1434 FILL_ATTR(tpriv, npriv, CKA_PRIVATE, &true_val, sizeof(true_val));
1435 FILL_ATTR(tpriv, npriv, CKA_SENSITIVE, &true_val, sizeof(true_val));
1436 FILL_ATTR(tpriv, npriv, CKA_DECRYPT, &false_val, sizeof(false_val));
1437 FILL_ATTR(tpriv, npriv, CKA_SIGN, &true_val, sizeof(true_val));
1438 FILL_ATTR(tpriv, npriv, CKA_SIGN_RECOVER, &false_val,
1439 sizeof(false_val));
1440 FILL_ATTR(tpriv, npriv, CKA_UNWRAP, &false_val, sizeof(false_val));
1441 FILL_ATTR(tpriv, npriv, CKA_DERIVE, &false_val, sizeof(false_val));
1442 FILL_ATTR(tpriv, npriv, CKA_ID, &keyid, sizeof(keyid));
1443
1444 f = p->function_list;
1445 si = &p->slotinfo[slotidx];
1446 session = si->session;
1447
1448 if ((rv = f->C_GenerateKeyPair(session, &mech, tpub, npub, tpriv, npriv,
1449 &pubKey, &privKey)) != CKR_OK) {
1450 error("%s: key generation failed: error 0x%lx", __func__, rv);
1451 *err = rv;
1452 return NULL;
1453 }
1454
1455 return pkcs11_fetch_ecdsa_pubkey(p, slotidx, &pubKey);
1456}
1457#endif /* WITH_PKCS11_KEYGEN */
1458
1459/*
1460 * register a new provider, fails if provider already exists. if
1461 * keyp is provided, fetch keys.
1462 */
1463static int
1464pkcs11_register_provider(char *provider_id, char *pin, struct sshkey ***keyp,
1465 struct pkcs11_provider **providerp, CK_ULONG user)
Damien Miller7ea845e2010-02-12 09:21:02 +11001466{
1467 int nkeys, need_finalize = 0;
djm@openbsd.org93f02102019-01-20 22:51:37 +00001468 int ret = -1;
Damien Miller7ea845e2010-02-12 09:21:02 +11001469 struct pkcs11_provider *p = NULL;
1470 void *handle = NULL;
1471 CK_RV (*getfunctionlist)(CK_FUNCTION_LIST **);
1472 CK_RV rv;
1473 CK_FUNCTION_LIST *f = NULL;
1474 CK_TOKEN_INFO *token;
1475 CK_ULONG i;
1476
djm@openbsd.org93f02102019-01-20 22:51:37 +00001477 if (providerp == NULL)
1478 goto fail;
1479 *providerp = NULL;
1480
1481 if (keyp != NULL)
1482 *keyp = NULL;
1483
Damien Miller7ea845e2010-02-12 09:21:02 +11001484 if (pkcs11_provider_lookup(provider_id) != NULL) {
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001485 debug("%s: provider already registered: %s",
1486 __func__, provider_id);
Damien Miller7ea845e2010-02-12 09:21:02 +11001487 goto fail;
1488 }
djm@openbsd.org93f02102019-01-20 22:51:37 +00001489 /* open shared pkcs11-library */
Damien Miller7ea845e2010-02-12 09:21:02 +11001490 if ((handle = dlopen(provider_id, RTLD_NOW)) == NULL) {
1491 error("dlopen %s failed: %s", provider_id, dlerror());
1492 goto fail;
1493 }
1494 if ((getfunctionlist = dlsym(handle, "C_GetFunctionList")) == NULL) {
1495 error("dlsym(C_GetFunctionList) failed: %s", dlerror());
1496 goto fail;
1497 }
1498 p = xcalloc(1, sizeof(*p));
1499 p->name = xstrdup(provider_id);
1500 p->handle = handle;
1501 /* setup the pkcs11 callbacks */
1502 if ((rv = (*getfunctionlist)(&f)) != CKR_OK) {
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001503 error("C_GetFunctionList for provider %s failed: %lu",
1504 provider_id, rv);
Damien Miller7ea845e2010-02-12 09:21:02 +11001505 goto fail;
1506 }
1507 p->function_list = f;
1508 if ((rv = f->C_Initialize(NULL)) != CKR_OK) {
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001509 error("C_Initialize for provider %s failed: %lu",
1510 provider_id, rv);
Damien Miller7ea845e2010-02-12 09:21:02 +11001511 goto fail;
1512 }
1513 need_finalize = 1;
1514 if ((rv = f->C_GetInfo(&p->info)) != CKR_OK) {
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001515 error("C_GetInfo for provider %s failed: %lu",
1516 provider_id, rv);
Damien Miller7ea845e2010-02-12 09:21:02 +11001517 goto fail;
1518 }
1519 rmspace(p->info.manufacturerID, sizeof(p->info.manufacturerID));
1520 rmspace(p->info.libraryDescription, sizeof(p->info.libraryDescription));
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001521 debug("provider %s: manufacturerID <%s> cryptokiVersion %d.%d"
Damien Miller7ea845e2010-02-12 09:21:02 +11001522 " libraryDescription <%s> libraryVersion %d.%d",
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001523 provider_id,
Damien Miller7ea845e2010-02-12 09:21:02 +11001524 p->info.manufacturerID,
1525 p->info.cryptokiVersion.major,
1526 p->info.cryptokiVersion.minor,
1527 p->info.libraryDescription,
1528 p->info.libraryVersion.major,
1529 p->info.libraryVersion.minor);
1530 if ((rv = f->C_GetSlotList(CK_TRUE, NULL, &p->nslots)) != CKR_OK) {
1531 error("C_GetSlotList failed: %lu", rv);
1532 goto fail;
1533 }
1534 if (p->nslots == 0) {
djm@openbsd.orgb52c0c22019-09-02 00:19:25 +00001535 debug("%s: provider %s returned no slots", __func__,
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001536 provider_id);
djm@openbsd.org93f02102019-01-20 22:51:37 +00001537 ret = -SSH_PKCS11_ERR_NO_SLOTS;
Damien Miller7ea845e2010-02-12 09:21:02 +11001538 goto fail;
1539 }
1540 p->slotlist = xcalloc(p->nslots, sizeof(CK_SLOT_ID));
1541 if ((rv = f->C_GetSlotList(CK_TRUE, p->slotlist, &p->nslots))
1542 != CKR_OK) {
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001543 error("C_GetSlotList for provider %s failed: %lu",
1544 provider_id, rv);
Damien Miller7ea845e2010-02-12 09:21:02 +11001545 goto fail;
1546 }
1547 p->slotinfo = xcalloc(p->nslots, sizeof(struct pkcs11_slotinfo));
1548 p->valid = 1;
1549 nkeys = 0;
1550 for (i = 0; i < p->nslots; i++) {
1551 token = &p->slotinfo[i].token;
1552 if ((rv = f->C_GetTokenInfo(p->slotlist[i], token))
1553 != CKR_OK) {
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001554 error("C_GetTokenInfo for provider %s slot %lu "
1555 "failed: %lu", provider_id, (unsigned long)i, rv);
Damien Miller7ea845e2010-02-12 09:21:02 +11001556 continue;
1557 }
djm@openbsd.orgb15fd982015-07-18 08:00:21 +00001558 if ((token->flags & CKF_TOKEN_INITIALIZED) == 0) {
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001559 debug2("%s: ignoring uninitialised token in "
1560 "provider %s slot %lu", __func__,
1561 provider_id, (unsigned long)i);
djm@openbsd.orgb15fd982015-07-18 08:00:21 +00001562 continue;
1563 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001564 rmspace(token->label, sizeof(token->label));
1565 rmspace(token->manufacturerID, sizeof(token->manufacturerID));
1566 rmspace(token->model, sizeof(token->model));
1567 rmspace(token->serialNumber, sizeof(token->serialNumber));
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001568 debug("provider %s slot %lu: label <%s> manufacturerID <%s> "
1569 "model <%s> serial <%s> flags 0x%lx",
1570 provider_id, (unsigned long)i,
Damien Miller7ea845e2010-02-12 09:21:02 +11001571 token->label, token->manufacturerID, token->model,
1572 token->serialNumber, token->flags);
djm@openbsd.org93f02102019-01-20 22:51:37 +00001573 /*
1574 * open session, login with pin and retrieve public
1575 * keys (if keyp is provided)
1576 */
djm@openbsd.org7d6034b2019-09-05 10:05:51 +00001577 if ((ret = pkcs11_open_session(p, i, pin, user)) != 0 ||
1578 keyp == NULL)
1579 continue;
1580 pkcs11_fetch_keys(p, i, keyp, &nkeys);
1581 pkcs11_fetch_certs(p, i, keyp, &nkeys);
1582 if (nkeys == 0 && !p->slotinfo[i].logged_in &&
1583 pkcs11_interactive) {
1584 /*
1585 * Some tokens require login before they will
1586 * expose keys.
1587 */
1588 if (pkcs11_login_slot(p, &p->slotinfo[i],
1589 CKU_USER) < 0) {
1590 error("login failed");
djm@openbsd.org93f02102019-01-20 22:51:37 +00001591 continue;
djm@openbsd.org7d6034b2019-09-05 10:05:51 +00001592 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001593 pkcs11_fetch_keys(p, i, keyp, &nkeys);
djm@openbsd.org93f02102019-01-20 22:51:37 +00001594 pkcs11_fetch_certs(p, i, keyp, &nkeys);
1595 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001596 }
djm@openbsd.org93f02102019-01-20 22:51:37 +00001597
1598 /* now owned by caller */
1599 *providerp = p;
1600
1601 TAILQ_INSERT_TAIL(&pkcs11_providers, p, next);
1602 p->refcount++; /* add to provider list */
1603
1604 return (nkeys);
Damien Miller7ea845e2010-02-12 09:21:02 +11001605fail:
1606 if (need_finalize && (rv = f->C_Finalize(NULL)) != CKR_OK)
djm@openbsd.orgefb494e2016-10-28 03:33:52 +00001607 error("C_Finalize for provider %s failed: %lu",
1608 provider_id, rv);
Damien Miller7ea845e2010-02-12 09:21:02 +11001609 if (p) {
djm@openbsd.org93f02102019-01-20 22:51:37 +00001610 free(p->name);
Darren Tuckera627d422013-06-02 07:31:17 +10001611 free(p->slotlist);
1612 free(p->slotinfo);
1613 free(p);
Damien Miller7ea845e2010-02-12 09:21:02 +11001614 }
1615 if (handle)
1616 dlclose(handle);
djm@openbsd.org93f02102019-01-20 22:51:37 +00001617 return (ret);
Damien Miller7ea845e2010-02-12 09:21:02 +11001618}
Damien Millerdfa41562010-02-12 10:06:28 +11001619
djm@openbsd.org93f02102019-01-20 22:51:37 +00001620/*
1621 * register a new provider and get number of keys hold by the token,
1622 * fails if provider already exists
1623 */
1624int
1625pkcs11_add_provider(char *provider_id, char *pin, struct sshkey ***keyp)
1626{
1627 struct pkcs11_provider *p = NULL;
1628 int nkeys;
Darren Tucker0dd24e02011-09-04 19:59:26 +10001629
djm@openbsd.org93f02102019-01-20 22:51:37 +00001630 nkeys = pkcs11_register_provider(provider_id, pin, keyp, &p, CKU_USER);
1631
1632 /* no keys found or some other error, de-register provider */
1633 if (nkeys <= 0 && p != NULL) {
1634 TAILQ_REMOVE(&pkcs11_providers, p, next);
1635 pkcs11_provider_finalize(p);
1636 pkcs11_provider_unref(p);
1637 }
1638 if (nkeys == 0)
1639 debug("%s: provider %s returned no keys", __func__,
1640 provider_id);
1641
1642 return (nkeys);
1643}
1644
1645#ifdef WITH_PKCS11_KEYGEN
1646struct sshkey *
1647pkcs11_gakp(char *provider_id, char *pin, unsigned int slotidx, char *label,
1648 unsigned int type, unsigned int bits, unsigned char keyid, u_int32_t *err)
1649{
1650 struct pkcs11_provider *p = NULL;
1651 struct pkcs11_slotinfo *si;
1652 CK_FUNCTION_LIST *f;
1653 CK_SESSION_HANDLE session;
1654 struct sshkey *k = NULL;
1655 int ret = -1, reset_pin = 0, reset_provider = 0;
1656 CK_RV rv;
1657
1658 *err = 0;
1659
1660 if ((p = pkcs11_provider_lookup(provider_id)) != NULL)
1661 debug("%s: provider \"%s\" available", __func__, provider_id);
1662 else if ((ret = pkcs11_register_provider(provider_id, pin, NULL, &p,
1663 CKU_SO)) < 0) {
1664 debug("%s: could not register provider %s", __func__,
1665 provider_id);
1666 goto out;
1667 } else
1668 reset_provider = 1;
1669
1670 f = p->function_list;
1671 si = &p->slotinfo[slotidx];
1672 session = si->session;
1673
1674 if ((rv = f->C_SetOperationState(session , pin, strlen(pin),
1675 CK_INVALID_HANDLE, CK_INVALID_HANDLE)) != CKR_OK) {
1676 debug("%s: could not supply SO pin: %lu", __func__, rv);
1677 reset_pin = 0;
1678 } else
1679 reset_pin = 1;
1680
1681 switch (type) {
1682 case KEY_RSA:
1683 if ((k = pkcs11_rsa_generate_private_key(p, slotidx, label,
1684 bits, keyid, err)) == NULL) {
1685 debug("%s: failed to generate RSA key", __func__);
1686 goto out;
1687 }
1688 break;
1689 case KEY_ECDSA:
1690 if ((k = pkcs11_ecdsa_generate_private_key(p, slotidx, label,
1691 bits, keyid, err)) == NULL) {
1692 debug("%s: failed to generate ECDSA key", __func__);
1693 goto out;
1694 }
1695 break;
1696 default:
1697 *err = SSH_PKCS11_ERR_GENERIC;
1698 debug("%s: unknown type %d", __func__, type);
1699 goto out;
1700 }
1701
1702out:
1703 if (reset_pin)
1704 f->C_SetOperationState(session , NULL, 0, CK_INVALID_HANDLE,
1705 CK_INVALID_HANDLE);
1706
1707 if (reset_provider)
1708 pkcs11_del_provider(provider_id);
1709
1710 return (k);
1711}
1712
1713struct sshkey *
1714pkcs11_destroy_keypair(char *provider_id, char *pin, unsigned long slotidx,
1715 unsigned char keyid, u_int32_t *err)
1716{
1717 struct pkcs11_provider *p = NULL;
1718 struct pkcs11_slotinfo *si;
1719 struct sshkey *k = NULL;
1720 int reset_pin = 0, reset_provider = 0;
1721 CK_ULONG nattrs;
1722 CK_FUNCTION_LIST *f;
1723 CK_SESSION_HANDLE session;
1724 CK_ATTRIBUTE attrs[16];
1725 CK_OBJECT_CLASS key_class;
1726 CK_KEY_TYPE key_type;
1727 CK_OBJECT_HANDLE obj = CK_INVALID_HANDLE;
1728 CK_RV rv;
1729
1730 *err = 0;
1731
1732 if ((p = pkcs11_provider_lookup(provider_id)) != NULL) {
1733 debug("%s: using provider \"%s\"", __func__, provider_id);
1734 } else if (pkcs11_register_provider(provider_id, pin, NULL, &p,
1735 CKU_SO) < 0) {
1736 debug("%s: could not register provider %s", __func__,
1737 provider_id);
1738 goto out;
1739 } else
1740 reset_provider = 1;
1741
1742 f = p->function_list;
1743 si = &p->slotinfo[slotidx];
1744 session = si->session;
1745
1746 if ((rv = f->C_SetOperationState(session , pin, strlen(pin),
1747 CK_INVALID_HANDLE, CK_INVALID_HANDLE)) != CKR_OK) {
1748 debug("%s: could not supply SO pin: %lu", __func__, rv);
1749 reset_pin = 0;
1750 } else
1751 reset_pin = 1;
1752
1753 /* private key */
1754 nattrs = 0;
1755 key_class = CKO_PRIVATE_KEY;
1756 FILL_ATTR(attrs, nattrs, CKA_CLASS, &key_class, sizeof(key_class));
1757 FILL_ATTR(attrs, nattrs, CKA_ID, &keyid, sizeof(keyid));
1758
1759 if (pkcs11_find(p, slotidx, attrs, nattrs, &obj) == 0 &&
1760 obj != CK_INVALID_HANDLE) {
1761 if ((rv = f->C_DestroyObject(session, obj)) != CKR_OK) {
1762 debug("%s: could not destroy private key 0x%hhx",
1763 __func__, keyid);
1764 *err = rv;
1765 goto out;
1766 }
1767 }
1768
1769 /* public key */
1770 nattrs = 0;
1771 key_class = CKO_PUBLIC_KEY;
1772 FILL_ATTR(attrs, nattrs, CKA_CLASS, &key_class, sizeof(key_class));
1773 FILL_ATTR(attrs, nattrs, CKA_ID, &keyid, sizeof(keyid));
1774
1775 if (pkcs11_find(p, slotidx, attrs, nattrs, &obj) == 0 &&
1776 obj != CK_INVALID_HANDLE) {
1777
1778 /* get key type */
1779 nattrs = 0;
1780 FILL_ATTR(attrs, nattrs, CKA_KEY_TYPE, &key_type,
1781 sizeof(key_type));
1782 rv = f->C_GetAttributeValue(session, obj, attrs, nattrs);
1783 if (rv != CKR_OK) {
1784 debug("%s: could not get key type of public key 0x%hhx",
1785 __func__, keyid);
1786 *err = rv;
1787 key_type = -1;
1788 }
1789 if (key_type == CKK_RSA)
1790 k = pkcs11_fetch_rsa_pubkey(p, slotidx, &obj);
1791 else if (key_type == CKK_ECDSA)
1792 k = pkcs11_fetch_ecdsa_pubkey(p, slotidx, &obj);
1793
1794 if ((rv = f->C_DestroyObject(session, obj)) != CKR_OK) {
1795 debug("%s: could not destroy public key 0x%hhx",
1796 __func__, keyid);
1797 *err = rv;
1798 goto out;
1799 }
1800 }
1801
1802out:
1803 if (reset_pin)
1804 f->C_SetOperationState(session , NULL, 0, CK_INVALID_HANDLE,
1805 CK_INVALID_HANDLE);
1806
1807 if (reset_provider)
1808 pkcs11_del_provider(provider_id);
1809
1810 return (k);
1811}
1812#endif /* WITH_PKCS11_KEYGEN */
Damien Miller5de6ac22019-01-21 11:44:19 +11001813#else /* ENABLE_PKCS11 */
Darren Tucker9634ffb2019-07-23 22:25:44 +10001814
1815#include <sys/types.h>
1816#include <stdarg.h>
1817#include <stdio.h>
1818
1819#include "log.h"
1820#include "sshkey.h"
1821
Darren Tucker0dd24e02011-09-04 19:59:26 +10001822int
1823pkcs11_init(int interactive)
1824{
djm@openbsd.org93f02102019-01-20 22:51:37 +00001825 error("%s: dlopen() not supported", __func__);
1826 return (-1);
1827}
1828
1829int
1830pkcs11_add_provider(char *provider_id, char *pin, struct sshkey ***keyp)
1831{
1832 error("%s: dlopen() not supported", __func__);
1833 return (-1);
Darren Tucker0dd24e02011-09-04 19:59:26 +10001834}
1835
1836void
1837pkcs11_terminate(void)
1838{
djm@openbsd.org93f02102019-01-20 22:51:37 +00001839 error("%s: dlopen() not supported", __func__);
Darren Tucker0dd24e02011-09-04 19:59:26 +10001840}
Damien Miller5de6ac22019-01-21 11:44:19 +11001841#endif /* ENABLE_PKCS11 */