ssh-sk.c revision 1.8.2.2 1 1.8.2.2 martin /* $NetBSD: ssh-sk.c,v 1.8.2.2 2023/12/25 12:31:08 martin Exp $ */
2 1.8.2.2 martin /* $OpenBSD: ssh-sk.c,v 1.40 2023/07/19 14:02:27 djm Exp $ */
3 1.8.2.2 martin
4 1.8.2.2 martin /*
5 1.8.2.2 martin * Copyright (c) 2019 Google LLC
6 1.8.2.2 martin *
7 1.8.2.2 martin * Permission to use, copy, modify, and distribute this software for any
8 1.8.2.2 martin * purpose with or without fee is hereby granted, provided that the above
9 1.8.2.2 martin * copyright notice and this permission notice appear in all copies.
10 1.8.2.2 martin *
11 1.8.2.2 martin * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.8.2.2 martin * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.8.2.2 martin * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.8.2.2 martin * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.8.2.2 martin * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.8.2.2 martin * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.8.2.2 martin * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.8.2.2 martin */
19 1.8.2.2 martin #include "includes.h"
20 1.8.2.2 martin __RCSID("$NetBSD: ssh-sk.c,v 1.8.2.2 2023/12/25 12:31:08 martin Exp $");
21 1.8.2.2 martin
22 1.8.2.2 martin /* #define DEBUG_SK 1 */
23 1.8.2.2 martin
24 1.8.2.2 martin #include <dlfcn.h>
25 1.8.2.2 martin #include <stddef.h>
26 1.8.2.2 martin #include <stdint.h>
27 1.8.2.2 martin #include <string.h>
28 1.8.2.2 martin #include <stdio.h>
29 1.8.2.2 martin
30 1.8.2.2 martin #ifdef WITH_OPENSSL
31 1.8.2.2 martin #include <openssl/objects.h>
32 1.8.2.2 martin #include <openssl/ec.h>
33 1.8.2.2 martin #endif /* WITH_OPENSSL */
34 1.8.2.2 martin
35 1.8.2.2 martin #include "log.h"
36 1.8.2.2 martin #include "misc.h"
37 1.8.2.2 martin #include "sshbuf.h"
38 1.8.2.2 martin #include "sshkey.h"
39 1.8.2.2 martin #include "ssherr.h"
40 1.8.2.2 martin #include "digest.h"
41 1.8.2.2 martin
42 1.8.2.2 martin #include "ssh-sk.h"
43 1.8.2.2 martin #include "sk-api.h"
44 1.8.2.2 martin #include "crypto_api.h"
45 1.8.2.2 martin
46 1.8.2.2 martin struct sshsk_provider {
47 1.8.2.2 martin char *path;
48 1.8.2.2 martin void *dlhandle;
49 1.8.2.2 martin
50 1.8.2.2 martin /* Return the version of the middleware API */
51 1.8.2.2 martin uint32_t (*sk_api_version)(void);
52 1.8.2.2 martin
53 1.8.2.2 martin /* Enroll a U2F key (private key generation) */
54 1.8.2.2 martin int (*sk_enroll)(int alg, const uint8_t *challenge,
55 1.8.2.2 martin size_t challenge_len, const char *application, uint8_t flags,
56 1.8.2.2 martin const char *pin, struct sk_option **opts,
57 1.8.2.2 martin struct sk_enroll_response **enroll_response);
58 1.8.2.2 martin
59 1.8.2.2 martin /* Sign a challenge */
60 1.8.2.2 martin int (*sk_sign)(int alg, const uint8_t *message, size_t message_len,
61 1.8.2.2 martin const char *application,
62 1.8.2.2 martin const uint8_t *key_handle, size_t key_handle_len,
63 1.8.2.2 martin uint8_t flags, const char *pin, struct sk_option **opts,
64 1.8.2.2 martin struct sk_sign_response **sign_response);
65 1.8.2.2 martin
66 1.8.2.2 martin /* Enumerate resident keys */
67 1.8.2.2 martin int (*sk_load_resident_keys)(const char *pin, struct sk_option **opts,
68 1.8.2.2 martin struct sk_resident_key ***rks, size_t *nrks);
69 1.8.2.2 martin };
70 1.8.2.2 martin
71 1.8.2.2 martin /* Built-in version */
72 1.8.2.2 martin int ssh_sk_enroll(int alg, const uint8_t *challenge,
73 1.8.2.2 martin size_t challenge_len, const char *application, uint8_t flags,
74 1.8.2.2 martin const char *pin, struct sk_option **opts,
75 1.8.2.2 martin struct sk_enroll_response **enroll_response);
76 1.8.2.2 martin int ssh_sk_sign(int alg, const uint8_t *message, size_t message_len,
77 1.8.2.2 martin const char *application,
78 1.8.2.2 martin const uint8_t *key_handle, size_t key_handle_len,
79 1.8.2.2 martin uint8_t flags, const char *pin, struct sk_option **opts,
80 1.8.2.2 martin struct sk_sign_response **sign_response);
81 1.8.2.2 martin int ssh_sk_load_resident_keys(const char *pin, struct sk_option **opts,
82 1.8.2.2 martin struct sk_resident_key ***rks, size_t *nrks);
83 1.8.2.2 martin
84 1.8.2.2 martin static void
85 1.8.2.2 martin sshsk_free(struct sshsk_provider *p)
86 1.8.2.2 martin {
87 1.8.2.2 martin if (p == NULL)
88 1.8.2.2 martin return;
89 1.8.2.2 martin free(p->path);
90 1.8.2.2 martin if (p->dlhandle != NULL)
91 1.8.2.2 martin dlclose(p->dlhandle);
92 1.8.2.2 martin free(p);
93 1.8.2.2 martin }
94 1.8.2.2 martin
95 1.8.2.2 martin static struct sshsk_provider *
96 1.8.2.2 martin sshsk_open(const char *path)
97 1.8.2.2 martin {
98 1.8.2.2 martin struct sshsk_provider *ret = NULL;
99 1.8.2.2 martin uint32_t version;
100 1.8.2.2 martin
101 1.8.2.2 martin if (path == NULL || *path == '\0') {
102 1.8.2.2 martin error("No FIDO SecurityKeyProvider specified");
103 1.8.2.2 martin return NULL;
104 1.8.2.2 martin }
105 1.8.2.2 martin if ((ret = calloc(1, sizeof(*ret))) == NULL) {
106 1.8.2.2 martin error_f("calloc failed");
107 1.8.2.2 martin return NULL;
108 1.8.2.2 martin }
109 1.8.2.2 martin if ((ret->path = strdup(path)) == NULL) {
110 1.8.2.2 martin error_f("strdup failed");
111 1.8.2.2 martin goto fail;
112 1.8.2.2 martin }
113 1.8.2.2 martin /* Skip the rest if we're using the linked in middleware */
114 1.8.2.2 martin if (strcasecmp(ret->path, "internal") == 0) {
115 1.8.2.2 martin ret->sk_enroll = ssh_sk_enroll;
116 1.8.2.2 martin ret->sk_sign = ssh_sk_sign;
117 1.8.2.2 martin ret->sk_load_resident_keys = ssh_sk_load_resident_keys;
118 1.8.2.2 martin return ret;
119 1.8.2.2 martin }
120 1.8.2.2 martin if (lib_contains_symbol(path, "sk_api_version") != 0) {
121 1.8.2.2 martin error("provider %s is not an OpenSSH FIDO library", path);
122 1.8.2.2 martin goto fail;
123 1.8.2.2 martin }
124 1.8.2.2 martin if ((ret->dlhandle = dlopen(path, RTLD_NOW)) == NULL) {
125 1.8.2.2 martin error("Provider \"%s\" dlopen failed: %s", path, dlerror());
126 1.8.2.2 martin goto fail;
127 1.8.2.2 martin }
128 1.8.2.2 martin if ((ret->sk_api_version = dlsym(ret->dlhandle,
129 1.8.2.2 martin "sk_api_version")) == NULL) {
130 1.8.2.2 martin fatal("Provider \"%s\" dlsym(sk_api_version) failed: %s",
131 1.8.2.2 martin path, dlerror());
132 1.8.2.2 martin }
133 1.8.2.2 martin version = ret->sk_api_version();
134 1.8.2.2 martin debug_f("provider %s implements version 0x%08lx", ret->path,
135 1.8.2.2 martin (u_long)version);
136 1.8.2.2 martin if ((version & SSH_SK_VERSION_MAJOR_MASK) != SSH_SK_VERSION_MAJOR) {
137 1.8.2.2 martin error("Provider \"%s\" implements unsupported "
138 1.8.2.2 martin "version 0x%08lx (supported: 0x%08lx)",
139 1.8.2.2 martin path, (u_long)version, (u_long)SSH_SK_VERSION_MAJOR);
140 1.8.2.2 martin goto fail;
141 1.8.2.2 martin }
142 1.8.2.2 martin if ((ret->sk_enroll = dlsym(ret->dlhandle, "sk_enroll")) == NULL) {
143 1.8.2.2 martin error("Provider %s dlsym(sk_enroll) failed: %s",
144 1.8.2.2 martin path, dlerror());
145 1.8.2.2 martin goto fail;
146 1.8.2.2 martin }
147 1.8.2.2 martin if ((ret->sk_sign = dlsym(ret->dlhandle, "sk_sign")) == NULL) {
148 1.8.2.2 martin error("Provider \"%s\" dlsym(sk_sign) failed: %s",
149 1.8.2.2 martin path, dlerror());
150 1.8.2.2 martin goto fail;
151 1.8.2.2 martin }
152 1.8.2.2 martin if ((ret->sk_load_resident_keys = dlsym(ret->dlhandle,
153 1.8.2.2 martin "sk_load_resident_keys")) == NULL) {
154 1.8.2.2 martin error("Provider \"%s\" dlsym(sk_load_resident_keys) "
155 1.8.2.2 martin "failed: %s", path, dlerror());
156 1.8.2.2 martin goto fail;
157 1.8.2.2 martin }
158 1.8.2.2 martin /* success */
159 1.8.2.2 martin return ret;
160 1.8.2.2 martin fail:
161 1.8.2.2 martin sshsk_free(ret);
162 1.8.2.2 martin return NULL;
163 1.8.2.2 martin }
164 1.8.2.2 martin
165 1.8.2.2 martin static void
166 1.8.2.2 martin sshsk_free_enroll_response(struct sk_enroll_response *r)
167 1.8.2.2 martin {
168 1.8.2.2 martin if (r == NULL)
169 1.8.2.2 martin return;
170 1.8.2.2 martin freezero(r->key_handle, r->key_handle_len);
171 1.8.2.2 martin freezero(r->public_key, r->public_key_len);
172 1.8.2.2 martin freezero(r->signature, r->signature_len);
173 1.8.2.2 martin freezero(r->attestation_cert, r->attestation_cert_len);
174 1.8.2.2 martin freezero(r->authdata, r->authdata_len);
175 1.8.2.2 martin freezero(r, sizeof(*r));
176 1.8.2.2 martin }
177 1.8.2.2 martin
178 1.8.2.2 martin static void
179 1.8.2.2 martin sshsk_free_sign_response(struct sk_sign_response *r)
180 1.8.2.2 martin {
181 1.8.2.2 martin if (r == NULL)
182 1.8.2.2 martin return;
183 1.8.2.2 martin freezero(r->sig_r, r->sig_r_len);
184 1.8.2.2 martin freezero(r->sig_s, r->sig_s_len);
185 1.8.2.2 martin freezero(r, sizeof(*r));
186 1.8.2.2 martin }
187 1.8.2.2 martin
188 1.8.2.2 martin #ifdef WITH_OPENSSL
189 1.8.2.2 martin /* Assemble key from response */
190 1.8.2.2 martin static int
191 1.8.2.2 martin sshsk_ecdsa_assemble(struct sk_enroll_response *resp, struct sshkey **keyp)
192 1.8.2.2 martin {
193 1.8.2.2 martin struct sshkey *key = NULL;
194 1.8.2.2 martin struct sshbuf *b = NULL;
195 1.8.2.2 martin EC_POINT *q = NULL;
196 1.8.2.2 martin int r;
197 1.8.2.2 martin
198 1.8.2.2 martin *keyp = NULL;
199 1.8.2.2 martin if ((key = sshkey_new(KEY_ECDSA_SK)) == NULL) {
200 1.8.2.2 martin error_f("sshkey_new failed");
201 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
202 1.8.2.2 martin goto out;
203 1.8.2.2 martin }
204 1.8.2.2 martin key->ecdsa_nid = NID_X9_62_prime256v1;
205 1.8.2.2 martin if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) == NULL ||
206 1.8.2.2 martin (q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL ||
207 1.8.2.2 martin (b = sshbuf_new()) == NULL) {
208 1.8.2.2 martin error_f("allocation failed");
209 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
210 1.8.2.2 martin goto out;
211 1.8.2.2 martin }
212 1.8.2.2 martin if ((r = sshbuf_put_string(b,
213 1.8.2.2 martin resp->public_key, resp->public_key_len)) != 0) {
214 1.8.2.2 martin error_fr(r, "sshbuf_put_string");
215 1.8.2.2 martin goto out;
216 1.8.2.2 martin }
217 1.8.2.2 martin if ((r = sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa))) != 0) {
218 1.8.2.2 martin error_fr(r, "parse");
219 1.8.2.2 martin r = SSH_ERR_INVALID_FORMAT;
220 1.8.2.2 martin goto out;
221 1.8.2.2 martin }
222 1.8.2.2 martin if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa), q) != 0) {
223 1.8.2.2 martin error("Authenticator returned invalid ECDSA key");
224 1.8.2.2 martin r = SSH_ERR_KEY_INVALID_EC_VALUE;
225 1.8.2.2 martin goto out;
226 1.8.2.2 martin }
227 1.8.2.2 martin if (EC_KEY_set_public_key(key->ecdsa, q) != 1) {
228 1.8.2.2 martin /* XXX assume it is a allocation error */
229 1.8.2.2 martin error_f("allocation failed");
230 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
231 1.8.2.2 martin goto out;
232 1.8.2.2 martin }
233 1.8.2.2 martin /* success */
234 1.8.2.2 martin *keyp = key;
235 1.8.2.2 martin key = NULL; /* transferred */
236 1.8.2.2 martin r = 0;
237 1.8.2.2 martin out:
238 1.8.2.2 martin EC_POINT_free(q);
239 1.8.2.2 martin sshkey_free(key);
240 1.8.2.2 martin sshbuf_free(b);
241 1.8.2.2 martin return r;
242 1.8.2.2 martin }
243 1.8.2.2 martin #endif /* WITH_OPENSSL */
244 1.8.2.2 martin
245 1.8.2.2 martin static int
246 1.8.2.2 martin sshsk_ed25519_assemble(struct sk_enroll_response *resp, struct sshkey **keyp)
247 1.8.2.2 martin {
248 1.8.2.2 martin struct sshkey *key = NULL;
249 1.8.2.2 martin int r;
250 1.8.2.2 martin
251 1.8.2.2 martin *keyp = NULL;
252 1.8.2.2 martin if (resp->public_key_len != ED25519_PK_SZ) {
253 1.8.2.2 martin error_f("invalid size: %zu", resp->public_key_len);
254 1.8.2.2 martin r = SSH_ERR_INVALID_FORMAT;
255 1.8.2.2 martin goto out;
256 1.8.2.2 martin }
257 1.8.2.2 martin if ((key = sshkey_new(KEY_ED25519_SK)) == NULL) {
258 1.8.2.2 martin error_f("sshkey_new failed");
259 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
260 1.8.2.2 martin goto out;
261 1.8.2.2 martin }
262 1.8.2.2 martin if ((key->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
263 1.8.2.2 martin error_f("malloc failed");
264 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
265 1.8.2.2 martin goto out;
266 1.8.2.2 martin }
267 1.8.2.2 martin memcpy(key->ed25519_pk, resp->public_key, ED25519_PK_SZ);
268 1.8.2.2 martin /* success */
269 1.8.2.2 martin *keyp = key;
270 1.8.2.2 martin key = NULL; /* transferred */
271 1.8.2.2 martin r = 0;
272 1.8.2.2 martin out:
273 1.8.2.2 martin sshkey_free(key);
274 1.8.2.2 martin return r;
275 1.8.2.2 martin }
276 1.8.2.2 martin
277 1.8.2.2 martin static int
278 1.8.2.2 martin sshsk_key_from_response(int alg, const char *application, uint8_t flags,
279 1.8.2.2 martin struct sk_enroll_response *resp, struct sshkey **keyp)
280 1.8.2.2 martin {
281 1.8.2.2 martin struct sshkey *key = NULL;
282 1.8.2.2 martin int r = SSH_ERR_INTERNAL_ERROR;
283 1.8.2.2 martin
284 1.8.2.2 martin *keyp = NULL;
285 1.8.2.2 martin
286 1.8.2.2 martin /* Check response validity */
287 1.8.2.2 martin if (resp->public_key == NULL || resp->key_handle == NULL) {
288 1.8.2.2 martin error_f("sk_enroll response invalid");
289 1.8.2.2 martin r = SSH_ERR_INVALID_FORMAT;
290 1.8.2.2 martin goto out;
291 1.8.2.2 martin }
292 1.8.2.2 martin switch (alg) {
293 1.8.2.2 martin #ifdef WITH_OPENSSL
294 1.8.2.2 martin case SSH_SK_ECDSA:
295 1.8.2.2 martin if ((r = sshsk_ecdsa_assemble(resp, &key)) != 0)
296 1.8.2.2 martin goto out;
297 1.8.2.2 martin break;
298 1.8.2.2 martin #endif /* WITH_OPENSSL */
299 1.8.2.2 martin case SSH_SK_ED25519:
300 1.8.2.2 martin if ((r = sshsk_ed25519_assemble(resp, &key)) != 0)
301 1.8.2.2 martin goto out;
302 1.8.2.2 martin break;
303 1.8.2.2 martin default:
304 1.8.2.2 martin error_f("unsupported algorithm %d", alg);
305 1.8.2.2 martin r = SSH_ERR_INVALID_ARGUMENT;
306 1.8.2.2 martin goto out;
307 1.8.2.2 martin }
308 1.8.2.2 martin key->sk_flags = flags;
309 1.8.2.2 martin if ((key->sk_key_handle = sshbuf_new()) == NULL ||
310 1.8.2.2 martin (key->sk_reserved = sshbuf_new()) == NULL) {
311 1.8.2.2 martin error_f("allocation failed");
312 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
313 1.8.2.2 martin goto out;
314 1.8.2.2 martin }
315 1.8.2.2 martin if ((key->sk_application = strdup(application)) == NULL) {
316 1.8.2.2 martin error_f("strdup application failed");
317 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
318 1.8.2.2 martin goto out;
319 1.8.2.2 martin }
320 1.8.2.2 martin if ((r = sshbuf_put(key->sk_key_handle, resp->key_handle,
321 1.8.2.2 martin resp->key_handle_len)) != 0) {
322 1.8.2.2 martin error_fr(r, "put key handle");
323 1.8.2.2 martin goto out;
324 1.8.2.2 martin }
325 1.8.2.2 martin /* success */
326 1.8.2.2 martin r = 0;
327 1.8.2.2 martin *keyp = key;
328 1.8.2.2 martin key = NULL;
329 1.8.2.2 martin out:
330 1.8.2.2 martin sshkey_free(key);
331 1.8.2.2 martin return r;
332 1.8.2.2 martin }
333 1.8.2.2 martin
334 1.8.2.2 martin static int
335 1.8.2.2 martin skerr_to_ssherr(int skerr)
336 1.8.2.2 martin {
337 1.8.2.2 martin switch (skerr) {
338 1.8.2.2 martin case SSH_SK_ERR_UNSUPPORTED:
339 1.8.2.2 martin return SSH_ERR_FEATURE_UNSUPPORTED;
340 1.8.2.2 martin case SSH_SK_ERR_PIN_REQUIRED:
341 1.8.2.2 martin return SSH_ERR_KEY_WRONG_PASSPHRASE;
342 1.8.2.2 martin case SSH_SK_ERR_DEVICE_NOT_FOUND:
343 1.8.2.2 martin return SSH_ERR_DEVICE_NOT_FOUND;
344 1.8.2.2 martin case SSH_SK_ERR_CREDENTIAL_EXISTS:
345 1.8.2.2 martin return SSH_ERR_KEY_BAD_PERMISSIONS;
346 1.8.2.2 martin case SSH_SK_ERR_GENERAL:
347 1.8.2.2 martin default:
348 1.8.2.2 martin return SSH_ERR_INVALID_FORMAT;
349 1.8.2.2 martin }
350 1.8.2.2 martin }
351 1.8.2.2 martin
352 1.8.2.2 martin static void
353 1.8.2.2 martin sshsk_free_options(struct sk_option **opts)
354 1.8.2.2 martin {
355 1.8.2.2 martin size_t i;
356 1.8.2.2 martin
357 1.8.2.2 martin if (opts == NULL)
358 1.8.2.2 martin return;
359 1.8.2.2 martin for (i = 0; opts[i] != NULL; i++) {
360 1.8.2.2 martin free(opts[i]->name);
361 1.8.2.2 martin free(opts[i]->value);
362 1.8.2.2 martin free(opts[i]);
363 1.8.2.2 martin }
364 1.8.2.2 martin free(opts);
365 1.8.2.2 martin }
366 1.8.2.2 martin
367 1.8.2.2 martin static int
368 1.8.2.2 martin sshsk_add_option(struct sk_option ***optsp, size_t *noptsp,
369 1.8.2.2 martin const char *name, const char *value, uint8_t required)
370 1.8.2.2 martin {
371 1.8.2.2 martin struct sk_option **opts = *optsp;
372 1.8.2.2 martin size_t nopts = *noptsp;
373 1.8.2.2 martin
374 1.8.2.2 martin if ((opts = recallocarray(opts, nopts, nopts + 2, /* extra for NULL */
375 1.8.2.2 martin sizeof(*opts))) == NULL) {
376 1.8.2.2 martin error_f("array alloc failed");
377 1.8.2.2 martin return SSH_ERR_ALLOC_FAIL;
378 1.8.2.2 martin }
379 1.8.2.2 martin *optsp = opts;
380 1.8.2.2 martin *noptsp = nopts + 1;
381 1.8.2.2 martin if ((opts[nopts] = calloc(1, sizeof(**opts))) == NULL) {
382 1.8.2.2 martin error_f("alloc failed");
383 1.8.2.2 martin return SSH_ERR_ALLOC_FAIL;
384 1.8.2.2 martin }
385 1.8.2.2 martin if ((opts[nopts]->name = strdup(name)) == NULL ||
386 1.8.2.2 martin (opts[nopts]->value = strdup(value)) == NULL) {
387 1.8.2.2 martin error_f("alloc failed");
388 1.8.2.2 martin return SSH_ERR_ALLOC_FAIL;
389 1.8.2.2 martin }
390 1.8.2.2 martin opts[nopts]->required = required;
391 1.8.2.2 martin return 0;
392 1.8.2.2 martin }
393 1.8.2.2 martin
394 1.8.2.2 martin static int
395 1.8.2.2 martin make_options(const char *device, const char *user_id,
396 1.8.2.2 martin struct sk_option ***optsp)
397 1.8.2.2 martin {
398 1.8.2.2 martin struct sk_option **opts = NULL;
399 1.8.2.2 martin size_t nopts = 0;
400 1.8.2.2 martin int r, ret = SSH_ERR_INTERNAL_ERROR;
401 1.8.2.2 martin
402 1.8.2.2 martin if (device != NULL &&
403 1.8.2.2 martin (r = sshsk_add_option(&opts, &nopts, "device", device, 0)) != 0) {
404 1.8.2.2 martin ret = r;
405 1.8.2.2 martin goto out;
406 1.8.2.2 martin }
407 1.8.2.2 martin if (user_id != NULL &&
408 1.8.2.2 martin (r = sshsk_add_option(&opts, &nopts, "user", user_id, 0)) != 0) {
409 1.8.2.2 martin ret = r;
410 1.8.2.2 martin goto out;
411 1.8.2.2 martin }
412 1.8.2.2 martin /* success */
413 1.8.2.2 martin *optsp = opts;
414 1.8.2.2 martin opts = NULL;
415 1.8.2.2 martin nopts = 0;
416 1.8.2.2 martin ret = 0;
417 1.8.2.2 martin out:
418 1.8.2.2 martin sshsk_free_options(opts);
419 1.8.2.2 martin return ret;
420 1.8.2.2 martin }
421 1.8.2.2 martin
422 1.8.2.2 martin
423 1.8.2.2 martin static int
424 1.8.2.2 martin fill_attestation_blob(const struct sk_enroll_response *resp,
425 1.8.2.2 martin struct sshbuf *attest)
426 1.8.2.2 martin {
427 1.8.2.2 martin int r;
428 1.8.2.2 martin
429 1.8.2.2 martin if (attest == NULL)
430 1.8.2.2 martin return 0; /* nothing to do */
431 1.8.2.2 martin if ((r = sshbuf_put_cstring(attest, "ssh-sk-attest-v01")) != 0 ||
432 1.8.2.2 martin (r = sshbuf_put_string(attest,
433 1.8.2.2 martin resp->attestation_cert, resp->attestation_cert_len)) != 0 ||
434 1.8.2.2 martin (r = sshbuf_put_string(attest,
435 1.8.2.2 martin resp->signature, resp->signature_len)) != 0 ||
436 1.8.2.2 martin (r = sshbuf_put_string(attest,
437 1.8.2.2 martin resp->authdata, resp->authdata_len)) != 0 ||
438 1.8.2.2 martin (r = sshbuf_put_u32(attest, 0)) != 0 || /* resvd flags */
439 1.8.2.2 martin (r = sshbuf_put_string(attest, NULL, 0)) != 0 /* resvd */) {
440 1.8.2.2 martin error_fr(r, "compose");
441 1.8.2.2 martin return r;
442 1.8.2.2 martin }
443 1.8.2.2 martin /* success */
444 1.8.2.2 martin return 0;
445 1.8.2.2 martin }
446 1.8.2.2 martin
447 1.8.2.2 martin int
448 1.8.2.2 martin sshsk_enroll(int type, const char *provider_path, const char *device,
449 1.8.2.2 martin const char *application, const char *userid, uint8_t flags,
450 1.8.2.2 martin const char *pin, struct sshbuf *challenge_buf,
451 1.8.2.2 martin struct sshkey **keyp, struct sshbuf *attest)
452 1.8.2.2 martin {
453 1.8.2.2 martin struct sshsk_provider *skp = NULL;
454 1.8.2.2 martin struct sshkey *key = NULL;
455 1.8.2.2 martin u_char randchall[32];
456 1.8.2.2 martin const u_char *challenge;
457 1.8.2.2 martin size_t challenge_len;
458 1.8.2.2 martin struct sk_enroll_response *resp = NULL;
459 1.8.2.2 martin struct sk_option **opts = NULL;
460 1.8.2.2 martin int r = SSH_ERR_INTERNAL_ERROR;
461 1.8.2.2 martin int alg;
462 1.8.2.2 martin
463 1.8.2.2 martin debug_f("provider \"%s\", device \"%s\", application \"%s\", "
464 1.8.2.2 martin "userid \"%s\", flags 0x%02x, challenge len %zu%s",
465 1.8.2.2 martin provider_path, device, application, userid, flags,
466 1.8.2.2 martin challenge_buf == NULL ? 0 : sshbuf_len(challenge_buf),
467 1.8.2.2 martin (pin != NULL && *pin != '\0') ? " with-pin" : "");
468 1.8.2.2 martin
469 1.8.2.2 martin *keyp = NULL;
470 1.8.2.2 martin if (attest)
471 1.8.2.2 martin sshbuf_reset(attest);
472 1.8.2.2 martin
473 1.8.2.2 martin if ((r = make_options(device, userid, &opts)) != 0)
474 1.8.2.2 martin goto out;
475 1.8.2.2 martin
476 1.8.2.2 martin switch (type) {
477 1.8.2.2 martin #ifdef WITH_OPENSSL
478 1.8.2.2 martin case KEY_ECDSA_SK:
479 1.8.2.2 martin alg = SSH_SK_ECDSA;
480 1.8.2.2 martin break;
481 1.8.2.2 martin #endif /* WITH_OPENSSL */
482 1.8.2.2 martin case KEY_ED25519_SK:
483 1.8.2.2 martin alg = SSH_SK_ED25519;
484 1.8.2.2 martin break;
485 1.8.2.2 martin default:
486 1.8.2.2 martin error_f("unsupported key type");
487 1.8.2.2 martin r = SSH_ERR_INVALID_ARGUMENT;
488 1.8.2.2 martin goto out;
489 1.8.2.2 martin }
490 1.8.2.2 martin if (provider_path == NULL) {
491 1.8.2.2 martin error_f("missing provider");
492 1.8.2.2 martin r = SSH_ERR_INVALID_ARGUMENT;
493 1.8.2.2 martin goto out;
494 1.8.2.2 martin }
495 1.8.2.2 martin if (application == NULL || *application == '\0') {
496 1.8.2.2 martin error_f("missing application");
497 1.8.2.2 martin r = SSH_ERR_INVALID_ARGUMENT;
498 1.8.2.2 martin goto out;
499 1.8.2.2 martin }
500 1.8.2.2 martin if (challenge_buf == NULL) {
501 1.8.2.2 martin debug_f("using random challenge");
502 1.8.2.2 martin arc4random_buf(randchall, sizeof(randchall));
503 1.8.2.2 martin challenge = randchall;
504 1.8.2.2 martin challenge_len = sizeof(randchall);
505 1.8.2.2 martin } else if (sshbuf_len(challenge_buf) == 0) {
506 1.8.2.2 martin error("Missing enrollment challenge");
507 1.8.2.2 martin r = SSH_ERR_INVALID_ARGUMENT;
508 1.8.2.2 martin goto out;
509 1.8.2.2 martin } else {
510 1.8.2.2 martin challenge = sshbuf_ptr(challenge_buf);
511 1.8.2.2 martin challenge_len = sshbuf_len(challenge_buf);
512 1.8.2.2 martin debug3_f("using explicit challenge len=%zd", challenge_len);
513 1.8.2.2 martin }
514 1.8.2.2 martin if ((skp = sshsk_open(provider_path)) == NULL) {
515 1.8.2.2 martin r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
516 1.8.2.2 martin goto out;
517 1.8.2.2 martin }
518 1.8.2.2 martin /* XXX validate flags? */
519 1.8.2.2 martin /* enroll key */
520 1.8.2.2 martin if ((r = skp->sk_enroll(alg, challenge, challenge_len, application,
521 1.8.2.2 martin flags, pin, opts, &resp)) != 0) {
522 1.8.2.2 martin debug_f("provider \"%s\" failure %d", provider_path, r);
523 1.8.2.2 martin r = skerr_to_ssherr(r);
524 1.8.2.2 martin goto out;
525 1.8.2.2 martin }
526 1.8.2.2 martin
527 1.8.2.2 martin if ((r = sshsk_key_from_response(alg, application, resp->flags,
528 1.8.2.2 martin resp, &key)) != 0)
529 1.8.2.2 martin goto out;
530 1.8.2.2 martin
531 1.8.2.2 martin /* Optionally fill in the attestation information */
532 1.8.2.2 martin if ((r = fill_attestation_blob(resp, attest)) != 0)
533 1.8.2.2 martin goto out;
534 1.8.2.2 martin
535 1.8.2.2 martin /* success */
536 1.8.2.2 martin *keyp = key;
537 1.8.2.2 martin key = NULL; /* transferred */
538 1.8.2.2 martin r = 0;
539 1.8.2.2 martin out:
540 1.8.2.2 martin sshsk_free_options(opts);
541 1.8.2.2 martin sshsk_free(skp);
542 1.8.2.2 martin sshkey_free(key);
543 1.8.2.2 martin sshsk_free_enroll_response(resp);
544 1.8.2.2 martin explicit_bzero(randchall, sizeof(randchall));
545 1.8.2.2 martin return r;
546 1.8.2.2 martin }
547 1.8.2.2 martin
548 1.8.2.2 martin #ifdef WITH_OPENSSL
549 1.8.2.2 martin static int
550 1.8.2.2 martin sshsk_ecdsa_sig(struct sk_sign_response *resp, struct sshbuf *sig)
551 1.8.2.2 martin {
552 1.8.2.2 martin struct sshbuf *inner_sig = NULL;
553 1.8.2.2 martin int r = SSH_ERR_INTERNAL_ERROR;
554 1.8.2.2 martin
555 1.8.2.2 martin /* Check response validity */
556 1.8.2.2 martin if (resp->sig_r == NULL || resp->sig_s == NULL) {
557 1.8.2.2 martin error_f("sk_sign response invalid");
558 1.8.2.2 martin r = SSH_ERR_INVALID_FORMAT;
559 1.8.2.2 martin goto out;
560 1.8.2.2 martin }
561 1.8.2.2 martin if ((inner_sig = sshbuf_new()) == NULL) {
562 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
563 1.8.2.2 martin goto out;
564 1.8.2.2 martin }
565 1.8.2.2 martin /* Prepare and append inner signature object */
566 1.8.2.2 martin if ((r = sshbuf_put_bignum2_bytes(inner_sig,
567 1.8.2.2 martin resp->sig_r, resp->sig_r_len)) != 0 ||
568 1.8.2.2 martin (r = sshbuf_put_bignum2_bytes(inner_sig,
569 1.8.2.2 martin resp->sig_s, resp->sig_s_len)) != 0) {
570 1.8.2.2 martin error_fr(r, "compose inner");
571 1.8.2.2 martin goto out;
572 1.8.2.2 martin }
573 1.8.2.2 martin if ((r = sshbuf_put_stringb(sig, inner_sig)) != 0 ||
574 1.8.2.2 martin (r = sshbuf_put_u8(sig, resp->flags)) != 0 ||
575 1.8.2.2 martin (r = sshbuf_put_u32(sig, resp->counter)) != 0) {
576 1.8.2.2 martin error_fr(r, "compose");
577 1.8.2.2 martin goto out;
578 1.8.2.2 martin }
579 1.8.2.2 martin #ifdef DEBUG_SK
580 1.8.2.2 martin fprintf(stderr, "%s: sig_r:\n", __func__);
581 1.8.2.2 martin sshbuf_dump_data(resp->sig_r, resp->sig_r_len, stderr);
582 1.8.2.2 martin fprintf(stderr, "%s: sig_s:\n", __func__);
583 1.8.2.2 martin sshbuf_dump_data(resp->sig_s, resp->sig_s_len, stderr);
584 1.8.2.2 martin fprintf(stderr, "%s: inner:\n", __func__);
585 1.8.2.2 martin sshbuf_dump(inner_sig, stderr);
586 1.8.2.2 martin #endif
587 1.8.2.2 martin r = 0;
588 1.8.2.2 martin out:
589 1.8.2.2 martin sshbuf_free(inner_sig);
590 1.8.2.2 martin return r;
591 1.8.2.2 martin }
592 1.8.2.2 martin #endif /* WITH_OPENSSL */
593 1.8.2.2 martin
594 1.8.2.2 martin static int
595 1.8.2.2 martin sshsk_ed25519_sig(struct sk_sign_response *resp, struct sshbuf *sig)
596 1.8.2.2 martin {
597 1.8.2.2 martin int r = SSH_ERR_INTERNAL_ERROR;
598 1.8.2.2 martin
599 1.8.2.2 martin /* Check response validity */
600 1.8.2.2 martin if (resp->sig_r == NULL) {
601 1.8.2.2 martin error_f("sk_sign response invalid");
602 1.8.2.2 martin r = SSH_ERR_INVALID_FORMAT;
603 1.8.2.2 martin goto out;
604 1.8.2.2 martin }
605 1.8.2.2 martin if ((r = sshbuf_put_string(sig,
606 1.8.2.2 martin resp->sig_r, resp->sig_r_len)) != 0 ||
607 1.8.2.2 martin (r = sshbuf_put_u8(sig, resp->flags)) != 0 ||
608 1.8.2.2 martin (r = sshbuf_put_u32(sig, resp->counter)) != 0) {
609 1.8.2.2 martin error_fr(r, "compose");
610 1.8.2.2 martin goto out;
611 1.8.2.2 martin }
612 1.8.2.2 martin #ifdef DEBUG_SK
613 1.8.2.2 martin fprintf(stderr, "%s: sig_r:\n", __func__);
614 1.8.2.2 martin sshbuf_dump_data(resp->sig_r, resp->sig_r_len, stderr);
615 1.8.2.2 martin #endif
616 1.8.2.2 martin r = 0;
617 1.8.2.2 martin out:
618 1.8.2.2 martin return r;
619 1.8.2.2 martin }
620 1.8.2.2 martin
621 1.8.2.2 martin int
622 1.8.2.2 martin sshsk_sign(const char *provider_path, struct sshkey *key,
623 1.8.2.2 martin u_char **sigp, size_t *lenp, const u_char *data, size_t datalen,
624 1.8.2.2 martin u_int compat, const char *pin)
625 1.8.2.2 martin {
626 1.8.2.2 martin struct sshsk_provider *skp = NULL;
627 1.8.2.2 martin int r = SSH_ERR_INTERNAL_ERROR;
628 1.8.2.2 martin int type, alg;
629 1.8.2.2 martin struct sk_sign_response *resp = NULL;
630 1.8.2.2 martin struct sshbuf *inner_sig = NULL, *sig = NULL;
631 1.8.2.2 martin struct sk_option **opts = NULL;
632 1.8.2.2 martin
633 1.8.2.2 martin debug_f("provider \"%s\", key %s, flags 0x%02x%s",
634 1.8.2.2 martin provider_path, sshkey_type(key), key->sk_flags,
635 1.8.2.2 martin (pin != NULL && *pin != '\0') ? " with-pin" : "");
636 1.8.2.2 martin
637 1.8.2.2 martin if (sigp != NULL)
638 1.8.2.2 martin *sigp = NULL;
639 1.8.2.2 martin if (lenp != NULL)
640 1.8.2.2 martin *lenp = 0;
641 1.8.2.2 martin type = sshkey_type_plain(key->type);
642 1.8.2.2 martin switch (type) {
643 1.8.2.2 martin #ifdef WITH_OPENSSL
644 1.8.2.2 martin case KEY_ECDSA_SK:
645 1.8.2.2 martin alg = SSH_SK_ECDSA;
646 1.8.2.2 martin break;
647 1.8.2.2 martin #endif /* WITH_OPENSSL */
648 1.8.2.2 martin case KEY_ED25519_SK:
649 1.8.2.2 martin alg = SSH_SK_ED25519;
650 1.8.2.2 martin break;
651 1.8.2.2 martin default:
652 1.8.2.2 martin return SSH_ERR_INVALID_ARGUMENT;
653 1.8.2.2 martin }
654 1.8.2.2 martin if (provider_path == NULL ||
655 1.8.2.2 martin key->sk_key_handle == NULL ||
656 1.8.2.2 martin key->sk_application == NULL || *key->sk_application == '\0') {
657 1.8.2.2 martin r = SSH_ERR_INVALID_ARGUMENT;
658 1.8.2.2 martin goto out;
659 1.8.2.2 martin }
660 1.8.2.2 martin if ((skp = sshsk_open(provider_path)) == NULL) {
661 1.8.2.2 martin r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
662 1.8.2.2 martin goto out;
663 1.8.2.2 martin }
664 1.8.2.2 martin #ifdef DEBUG_SK
665 1.8.2.2 martin fprintf(stderr, "%s: sk_flags = 0x%02x, sk_application = \"%s\"\n",
666 1.8.2.2 martin __func__, key->sk_flags, key->sk_application);
667 1.8.2.2 martin fprintf(stderr, "%s: sk_key_handle:\n", __func__);
668 1.8.2.2 martin sshbuf_dump(key->sk_key_handle, stderr);
669 1.8.2.2 martin #endif
670 1.8.2.2 martin if ((r = skp->sk_sign(alg, data, datalen, key->sk_application,
671 1.8.2.2 martin sshbuf_ptr(key->sk_key_handle), sshbuf_len(key->sk_key_handle),
672 1.8.2.2 martin key->sk_flags, pin, opts, &resp)) != 0) {
673 1.8.2.2 martin debug_f("sk_sign failed with code %d", r);
674 1.8.2.2 martin r = skerr_to_ssherr(r);
675 1.8.2.2 martin goto out;
676 1.8.2.2 martin }
677 1.8.2.2 martin /* Assemble signature */
678 1.8.2.2 martin if ((sig = sshbuf_new()) == NULL) {
679 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
680 1.8.2.2 martin goto out;
681 1.8.2.2 martin }
682 1.8.2.2 martin if ((r = sshbuf_put_cstring(sig, sshkey_ssh_name_plain(key))) != 0) {
683 1.8.2.2 martin error_fr(r, "compose outer");
684 1.8.2.2 martin goto out;
685 1.8.2.2 martin }
686 1.8.2.2 martin switch (type) {
687 1.8.2.2 martin #ifdef WITH_OPENSSL
688 1.8.2.2 martin case KEY_ECDSA_SK:
689 1.8.2.2 martin if ((r = sshsk_ecdsa_sig(resp, sig)) != 0)
690 1.8.2.2 martin goto out;
691 1.8.2.2 martin break;
692 1.8.2.2 martin #endif /* WITH_OPENSSL */
693 1.8.2.2 martin case KEY_ED25519_SK:
694 1.8.2.2 martin if ((r = sshsk_ed25519_sig(resp, sig)) != 0)
695 1.8.2.2 martin goto out;
696 1.8.2.2 martin break;
697 1.8.2.2 martin }
698 1.8.2.2 martin #ifdef DEBUG_SK
699 1.8.2.2 martin fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
700 1.8.2.2 martin __func__, resp->flags, resp->counter);
701 1.8.2.2 martin fprintf(stderr, "%s: data to sign:\n", __func__);
702 1.8.2.2 martin sshbuf_dump_data(data, datalen, stderr);
703 1.8.2.2 martin fprintf(stderr, "%s: sigbuf:\n", __func__);
704 1.8.2.2 martin sshbuf_dump(sig, stderr);
705 1.8.2.2 martin #endif
706 1.8.2.2 martin if (sigp != NULL) {
707 1.8.2.2 martin if ((*sigp = malloc(sshbuf_len(sig))) == NULL) {
708 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
709 1.8.2.2 martin goto out;
710 1.8.2.2 martin }
711 1.8.2.2 martin memcpy(*sigp, sshbuf_ptr(sig), sshbuf_len(sig));
712 1.8.2.2 martin }
713 1.8.2.2 martin if (lenp != NULL)
714 1.8.2.2 martin *lenp = sshbuf_len(sig);
715 1.8.2.2 martin /* success */
716 1.8.2.2 martin r = 0;
717 1.8.2.2 martin out:
718 1.8.2.2 martin sshsk_free_options(opts);
719 1.8.2.2 martin sshsk_free(skp);
720 1.8.2.2 martin sshsk_free_sign_response(resp);
721 1.8.2.2 martin sshbuf_free(sig);
722 1.8.2.2 martin sshbuf_free(inner_sig);
723 1.8.2.2 martin return r;
724 1.8.2.2 martin }
725 1.8.2.2 martin
726 1.8.2.2 martin static void
727 1.8.2.2 martin sshsk_free_sk_resident_keys(struct sk_resident_key **rks, size_t nrks)
728 1.8.2.2 martin {
729 1.8.2.2 martin size_t i;
730 1.8.2.2 martin
731 1.8.2.2 martin if (nrks == 0 || rks == NULL)
732 1.8.2.2 martin return;
733 1.8.2.2 martin for (i = 0; i < nrks; i++) {
734 1.8.2.2 martin free(rks[i]->application);
735 1.8.2.2 martin freezero(rks[i]->user_id, rks[i]->user_id_len);
736 1.8.2.2 martin freezero(rks[i]->key.key_handle, rks[i]->key.key_handle_len);
737 1.8.2.2 martin freezero(rks[i]->key.public_key, rks[i]->key.public_key_len);
738 1.8.2.2 martin freezero(rks[i]->key.signature, rks[i]->key.signature_len);
739 1.8.2.2 martin freezero(rks[i]->key.attestation_cert,
740 1.8.2.2 martin rks[i]->key.attestation_cert_len);
741 1.8.2.2 martin freezero(rks[i], sizeof(**rks));
742 1.8.2.2 martin }
743 1.8.2.2 martin free(rks);
744 1.8.2.2 martin }
745 1.8.2.2 martin
746 1.8.2.2 martin static void
747 1.8.2.2 martin sshsk_free_resident_key(struct sshsk_resident_key *srk)
748 1.8.2.2 martin {
749 1.8.2.2 martin if (srk == NULL)
750 1.8.2.2 martin return;
751 1.8.2.2 martin sshkey_free(srk->key);
752 1.8.2.2 martin freezero(srk->user_id, srk->user_id_len);
753 1.8.2.2 martin free(srk);
754 1.8.2.2 martin }
755 1.8.2.2 martin
756 1.8.2.2 martin
757 1.8.2.2 martin void
758 1.8.2.2 martin sshsk_free_resident_keys(struct sshsk_resident_key **srks, size_t nsrks)
759 1.8.2.2 martin {
760 1.8.2.2 martin size_t i;
761 1.8.2.2 martin
762 1.8.2.2 martin if (srks == NULL || nsrks == 0)
763 1.8.2.2 martin return;
764 1.8.2.2 martin
765 1.8.2.2 martin for (i = 0; i < nsrks; i++)
766 1.8.2.2 martin sshsk_free_resident_key(srks[i]);
767 1.8.2.2 martin free(srks);
768 1.8.2.2 martin }
769 1.8.2.2 martin
770 1.8.2.2 martin int
771 1.8.2.2 martin sshsk_load_resident(const char *provider_path, const char *device,
772 1.8.2.2 martin const char *pin, u_int flags, struct sshsk_resident_key ***srksp,
773 1.8.2.2 martin size_t *nsrksp)
774 1.8.2.2 martin {
775 1.8.2.2 martin struct sshsk_provider *skp = NULL;
776 1.8.2.2 martin int r = SSH_ERR_INTERNAL_ERROR;
777 1.8.2.2 martin struct sk_resident_key **rks = NULL;
778 1.8.2.2 martin size_t i, nrks = 0, nsrks = 0;
779 1.8.2.2 martin struct sshkey *key = NULL;
780 1.8.2.2 martin struct sshsk_resident_key *srk = NULL, **srks = NULL, **tmp;
781 1.8.2.2 martin uint8_t sk_flags;
782 1.8.2.2 martin struct sk_option **opts = NULL;
783 1.8.2.2 martin
784 1.8.2.2 martin debug_f("provider \"%s\"%s", provider_path,
785 1.8.2.2 martin (pin != NULL && *pin != '\0') ? ", have-pin": "");
786 1.8.2.2 martin
787 1.8.2.2 martin if (srksp == NULL || nsrksp == NULL)
788 1.8.2.2 martin return SSH_ERR_INVALID_ARGUMENT;
789 1.8.2.2 martin *srksp = NULL;
790 1.8.2.2 martin *nsrksp = 0;
791 1.8.2.2 martin
792 1.8.2.2 martin if ((r = make_options(device, NULL, &opts)) != 0)
793 1.8.2.2 martin goto out;
794 1.8.2.2 martin if ((skp = sshsk_open(provider_path)) == NULL) {
795 1.8.2.2 martin r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
796 1.8.2.2 martin goto out;
797 1.8.2.2 martin }
798 1.8.2.2 martin if ((r = skp->sk_load_resident_keys(pin, opts, &rks, &nrks)) != 0) {
799 1.8.2.2 martin error("Provider \"%s\" returned failure %d", provider_path, r);
800 1.8.2.2 martin r = skerr_to_ssherr(r);
801 1.8.2.2 martin goto out;
802 1.8.2.2 martin }
803 1.8.2.2 martin for (i = 0; i < nrks; i++) {
804 1.8.2.2 martin debug3_f("rk %zu: slot %zu, alg %d, app \"%s\", uidlen %zu",
805 1.8.2.2 martin i, rks[i]->slot, rks[i]->alg, rks[i]->application,
806 1.8.2.2 martin rks[i]->user_id_len);
807 1.8.2.2 martin /* XXX need better filter here */
808 1.8.2.2 martin if (strncmp(rks[i]->application, "ssh:", 4) != 0)
809 1.8.2.2 martin continue;
810 1.8.2.2 martin switch (rks[i]->alg) {
811 1.8.2.2 martin case SSH_SK_ECDSA:
812 1.8.2.2 martin case SSH_SK_ED25519:
813 1.8.2.2 martin break;
814 1.8.2.2 martin default:
815 1.8.2.2 martin continue;
816 1.8.2.2 martin }
817 1.8.2.2 martin sk_flags = SSH_SK_USER_PRESENCE_REQD|SSH_SK_RESIDENT_KEY;
818 1.8.2.2 martin if ((rks[i]->flags & SSH_SK_USER_VERIFICATION_REQD))
819 1.8.2.2 martin sk_flags |= SSH_SK_USER_VERIFICATION_REQD;
820 1.8.2.2 martin if ((r = sshsk_key_from_response(rks[i]->alg,
821 1.8.2.2 martin rks[i]->application, sk_flags, &rks[i]->key, &key)) != 0)
822 1.8.2.2 martin goto out;
823 1.8.2.2 martin if ((srk = calloc(1, sizeof(*srk))) == NULL) {
824 1.8.2.2 martin error_f("calloc failed");
825 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
826 1.8.2.2 martin goto out;
827 1.8.2.2 martin }
828 1.8.2.2 martin srk->key = key;
829 1.8.2.2 martin key = NULL; /* transferred */
830 1.8.2.2 martin if ((srk->user_id = calloc(1, rks[i]->user_id_len)) == NULL) {
831 1.8.2.2 martin error_f("calloc failed");
832 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
833 1.8.2.2 martin goto out;
834 1.8.2.2 martin }
835 1.8.2.2 martin memcpy(srk->user_id, rks[i]->user_id, rks[i]->user_id_len);
836 1.8.2.2 martin srk->user_id_len = rks[i]->user_id_len;
837 1.8.2.2 martin if ((tmp = recallocarray(srks, nsrks, nsrks + 1,
838 1.8.2.2 martin sizeof(*tmp))) == NULL) {
839 1.8.2.2 martin error_f("recallocarray failed");
840 1.8.2.2 martin r = SSH_ERR_ALLOC_FAIL;
841 1.8.2.2 martin goto out;
842 1.8.2.2 martin }
843 1.8.2.2 martin srks = tmp;
844 1.8.2.2 martin srks[nsrks++] = srk;
845 1.8.2.2 martin srk = NULL;
846 1.8.2.2 martin /* XXX synthesise comment */
847 1.8.2.2 martin }
848 1.8.2.2 martin /* success */
849 1.8.2.2 martin *srksp = srks;
850 1.8.2.2 martin *nsrksp = nsrks;
851 1.8.2.2 martin srks = NULL;
852 1.8.2.2 martin nsrks = 0;
853 1.8.2.2 martin r = 0;
854 1.8.2.2 martin out:
855 1.8.2.2 martin sshsk_free_options(opts);
856 1.8.2.2 martin sshsk_free(skp);
857 1.8.2.2 martin sshsk_free_sk_resident_keys(rks, nrks);
858 1.8.2.2 martin sshkey_free(key);
859 1.8.2.2 martin sshsk_free_resident_key(srk);
860 1.8.2.2 martin sshsk_free_resident_keys(srks, nsrks);
861 1.8.2.2 martin return r;
862 1.8.2.2 martin }
863 1.8.2.2 martin
864