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