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