naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-sk.c,v 1.27 2020/02/06 22:30:54 naddy 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 | |
| 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 | } |
djm@openbsd.org | 6bff952 | 2019-11-14 21:27:29 +0000 | [diff] [blame] | 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; |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 116 | ret->sk_load_resident_keys = ssh_sk_load_resident_keys; |
djm@openbsd.org | 6bff952 | 2019-11-14 21:27:29 +0000 | [diff] [blame] | 117 | #else |
| 118 | error("internal security key support not enabled"); |
| 119 | #endif |
| 120 | return ret; |
| 121 | } |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 122 | if ((ret->dlhandle = dlopen(path, RTLD_NOW)) == NULL) { |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 123 | error("Provider \"%s\" dlopen failed: %s", path, dlerror()); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 124 | goto fail; |
| 125 | } |
| 126 | if ((ret->sk_api_version = dlsym(ret->dlhandle, |
| 127 | "sk_api_version")) == NULL) { |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 128 | error("Provider \"%s\" dlsym(sk_api_version) failed: %s", |
| 129 | path, dlerror()); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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) { |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 136 | error("Provider \"%s\" implements unsupported " |
djm@openbsd.org | d433596 | 2019-12-30 09:24:03 +0000 | [diff] [blame] | 137 | "version 0x%08lx (supported: 0x%08lx)", |
| 138 | path, (u_long)version, (u_long)SSH_SK_VERSION_MAJOR); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 139 | goto fail; |
| 140 | } |
| 141 | if ((ret->sk_enroll = dlsym(ret->dlhandle, "sk_enroll")) == NULL) { |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 142 | error("Provider %s dlsym(sk_enroll) failed: %s", |
| 143 | path, dlerror()); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 144 | goto fail; |
| 145 | } |
| 146 | if ((ret->sk_sign = dlsym(ret->dlhandle, "sk_sign")) == NULL) { |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 147 | error("Provider \"%s\" dlsym(sk_sign) failed: %s", |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 148 | path, dlerror()); |
| 149 | goto fail; |
| 150 | } |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 151 | if ((ret->sk_load_resident_keys = dlsym(ret->dlhandle, |
| 152 | "sk_load_resident_keys")) == NULL) { |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 153 | error("Provider \"%s\" dlsym(sk_load_resident_keys) " |
| 154 | "failed: %s", path, dlerror()); |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 155 | goto fail; |
| 156 | } |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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)); |
djm@openbsd.org | d8b2838 | 2019-11-27 22:32:11 +0000 | [diff] [blame] | 174 | } |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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)); |
djm@openbsd.org | d8b2838 | 2019-11-27 22:32:11 +0000 | [diff] [blame] | 184 | } |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 185 | |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 186 | #ifdef WITH_OPENSSL |
markus@openbsd.org | cef84a0 | 2019-11-12 19:29:54 +0000 | [diff] [blame] | 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) { |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 221 | error("Authenticator returned invalid ECDSA key"); |
markus@openbsd.org | cef84a0 | 2019-11-12 19:29:54 +0000 | [diff] [blame] | 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 | } |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 241 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | cef84a0 | 2019-11-12 19:29:54 +0000 | [diff] [blame] | 242 | |
markus@openbsd.org | 7c32b51 | 2019-11-12 19:31:45 +0000 | [diff] [blame] | 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 | |
djm@openbsd.org | 2fe05fc | 2019-12-30 09:20:36 +0000 | [diff] [blame] | 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 */ |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 285 | if (resp->public_key == NULL || resp->key_handle == NULL) { |
djm@openbsd.org | 2fe05fc | 2019-12-30 09:20:36 +0000 | [diff] [blame] | 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 | |
djm@openbsd.org | 43ce964 | 2019-12-30 09:24:45 +0000 | [diff] [blame] | 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; |
djm@openbsd.org | 59d01f1 | 2020-01-25 23:13:09 +0000 | [diff] [blame] | 340 | case SSH_SK_ERR_DEVICE_NOT_FOUND: |
| 341 | return SSH_ERR_DEVICE_NOT_FOUND; |
djm@openbsd.org | 43ce964 | 2019-12-30 09:24:45 +0000 | [diff] [blame] | 342 | case SSH_SK_ERR_GENERAL: |
| 343 | default: |
| 344 | return SSH_ERR_INVALID_FORMAT; |
| 345 | } |
| 346 | } |
| 347 | |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 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 | |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 418 | int |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 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, |
djm@openbsd.org | c54cd18 | 2019-12-30 09:23:28 +0000 | [diff] [blame] | 422 | struct sshkey **keyp, struct sshbuf *attest) |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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; |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 430 | struct sk_option **opts = NULL; |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 431 | int r = SSH_ERR_INTERNAL_ERROR; |
markus@openbsd.org | fd1a3b5 | 2019-11-12 19:32:30 +0000 | [diff] [blame] | 432 | int alg; |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 433 | |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 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), |
djm@openbsd.org | c54cd18 | 2019-12-30 09:23:28 +0000 | [diff] [blame] | 438 | (pin != NULL && *pin != '\0') ? " with-pin" : ""); |
djm@openbsd.org | 4103a3e | 2019-11-16 22:42:30 +0000 | [diff] [blame] | 439 | |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 440 | *keyp = NULL; |
| 441 | if (attest) |
| 442 | sshbuf_reset(attest); |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 443 | |
| 444 | if ((r = make_options(device, userid, &opts)) != 0) |
| 445 | goto out; |
| 446 | |
markus@openbsd.org | 7c32b51 | 2019-11-12 19:31:45 +0000 | [diff] [blame] | 447 | switch (type) { |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 448 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 7c32b51 | 2019-11-12 19:31:45 +0000 | [diff] [blame] | 449 | case KEY_ECDSA_SK: |
markus@openbsd.org | fd1a3b5 | 2019-11-12 19:32:30 +0000 | [diff] [blame] | 450 | alg = SSH_SK_ECDSA; |
| 451 | break; |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 452 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 7c32b51 | 2019-11-12 19:31:45 +0000 | [diff] [blame] | 453 | case KEY_ED25519_SK: |
markus@openbsd.org | fd1a3b5 | 2019-11-12 19:32:30 +0000 | [diff] [blame] | 454 | alg = SSH_SK_ED25519; |
markus@openbsd.org | 7c32b51 | 2019-11-12 19:31:45 +0000 | [diff] [blame] | 455 | break; |
| 456 | default: |
| 457 | error("%s: unsupported key type", __func__); |
| 458 | r = SSH_ERR_INVALID_ARGUMENT; |
| 459 | goto out; |
| 460 | } |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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 */ |
markus@openbsd.org | fd1a3b5 | 2019-11-12 19:32:30 +0000 | [diff] [blame] | 492 | if ((r = skp->sk_enroll(alg, challenge, challenge_len, application, |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 493 | flags, pin, opts, &resp)) != 0) { |
djm@openbsd.org | 59d01f1 | 2020-01-25 23:13:09 +0000 | [diff] [blame] | 494 | debug("%s: provider \"%s\" returned failure %d", __func__, |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 495 | provider_path, r); |
djm@openbsd.org | 43ce964 | 2019-12-30 09:24:45 +0000 | [diff] [blame] | 496 | r = skerr_to_ssherr(r); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 497 | goto out; |
| 498 | } |
djm@openbsd.org | 2fe05fc | 2019-12-30 09:20:36 +0000 | [diff] [blame] | 499 | |
| 500 | if ((r = sshsk_key_from_response(alg, application, flags, |
| 501 | resp, &key)) != 0) |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 502 | goto out; |
djm@openbsd.org | 2fe05fc | 2019-12-30 09:20:36 +0000 | [diff] [blame] | 503 | |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 504 | /* Optionally fill in the attestation information */ |
| 505 | if (attest != NULL) { |
djm@openbsd.org | 24c0f75 | 2020-01-28 08:01:34 +0000 | [diff] [blame] | 506 | if ((r = sshbuf_put_cstring(attest, |
| 507 | "ssh-sk-attest-v00")) != 0 || |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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 || |
djm@openbsd.org | 24c0f75 | 2020-01-28 08:01:34 +0000 | [diff] [blame] | 512 | (r = sshbuf_put_u32(attest, 0)) != 0 || /* resvd flags */ |
| 513 | (r = sshbuf_put_string(attest, NULL, 0)) != 0 /* resvd */) { |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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: |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 523 | sshsk_free_options(opts); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 524 | sshsk_free(skp); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 525 | sshkey_free(key); |
| 526 | sshsk_free_enroll_response(resp); |
| 527 | explicit_bzero(randchall, sizeof(randchall)); |
| 528 | return r; |
| 529 | } |
| 530 | |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 531 | #ifdef WITH_OPENSSL |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 532 | static int |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 533 | sshsk_ecdsa_sig(struct sk_sign_response *resp, struct sshbuf *sig) |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 534 | { |
| 535 | struct sshbuf *inner_sig = NULL; |
| 536 | int r = SSH_ERR_INTERNAL_ERROR; |
| 537 | |
markus@openbsd.org | 3fcf69a | 2019-11-12 19:34:00 +0000 | [diff] [blame] | 538 | /* Check response validity */ |
markus@openbsd.org | dffd02e | 2019-11-13 20:25:45 +0000 | [diff] [blame] | 539 | if (resp->sig_r == NULL || resp->sig_s == NULL) { |
markus@openbsd.org | 3fcf69a | 2019-11-12 19:34:00 +0000 | [diff] [blame] | 540 | error("%s: sk_sign response invalid", __func__); |
| 541 | r = SSH_ERR_INVALID_FORMAT; |
| 542 | goto out; |
| 543 | } |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 544 | if ((inner_sig = sshbuf_new()) == NULL) { |
| 545 | r = SSH_ERR_ALLOC_FAIL; |
| 546 | goto out; |
| 547 | } |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 548 | /* Prepare and append inner signature object */ |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 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, |
djm@openbsd.org | a70d92f | 2019-11-19 22:23:19 +0000 | [diff] [blame] | 552 | resp->sig_s, resp->sig_s_len)) != 0) { |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 553 | debug("%s: buffer error: %s", __func__, ssh_err(r)); |
| 554 | goto out; |
| 555 | } |
djm@openbsd.org | a70d92f | 2019-11-19 22:23:19 +0000 | [diff] [blame] | 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) { |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 559 | debug("%s: buffer error: %s", __func__, ssh_err(r)); |
| 560 | goto out; |
| 561 | } |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 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); |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 567 | fprintf(stderr, "%s: inner:\n", __func__); |
| 568 | sshbuf_dump(inner_sig, stderr); |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 569 | #endif |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 570 | r = 0; |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 571 | out: |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 572 | sshbuf_free(inner_sig); |
| 573 | return r; |
| 574 | } |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 575 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 576 | |
| 577 | static int |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 578 | sshsk_ed25519_sig(struct sk_sign_response *resp, struct sshbuf *sig) |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 579 | { |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 580 | int r = SSH_ERR_INTERNAL_ERROR; |
| 581 | |
markus@openbsd.org | 3fcf69a | 2019-11-12 19:34:00 +0000 | [diff] [blame] | 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 | } |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 588 | if ((r = sshbuf_put_string(sig, |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 589 | resp->sig_r, resp->sig_r_len)) != 0 || |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 590 | (r = sshbuf_put_u8(sig, resp->flags)) != 0 || |
| 591 | (r = sshbuf_put_u32(sig, resp->counter)) != 0) { |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 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); |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 598 | #endif |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 599 | r = 0; |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 600 | out: |
| 601 | return 0; |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 602 | } |
| 603 | |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 604 | int |
djm@openbsd.org | b52ec0b | 2019-12-13 19:09:10 +0000 | [diff] [blame] | 605 | sshsk_sign(const char *provider_path, struct sshkey *key, |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 606 | 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] | 607 | u_int compat, const char *pin) |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 608 | { |
| 609 | struct sshsk_provider *skp = NULL; |
| 610 | int r = SSH_ERR_INTERNAL_ERROR; |
markus@openbsd.org | fd1a3b5 | 2019-11-12 19:32:30 +0000 | [diff] [blame] | 611 | int type, alg; |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 612 | struct sk_sign_response *resp = NULL; |
| 613 | struct sshbuf *inner_sig = NULL, *sig = NULL; |
| 614 | uint8_t message[32]; |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 615 | struct sk_option **opts = NULL; |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 616 | |
djm@openbsd.org | c54cd18 | 2019-12-30 09:23:28 +0000 | [diff] [blame] | 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" : ""); |
djm@openbsd.org | 4103a3e | 2019-11-16 22:42:30 +0000 | [diff] [blame] | 620 | |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 621 | if (sigp != NULL) |
| 622 | *sigp = NULL; |
| 623 | if (lenp != NULL) |
| 624 | *lenp = 0; |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 625 | type = sshkey_type_plain(key->type); |
| 626 | switch (type) { |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 627 | #ifdef WITH_OPENSSL |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 628 | case KEY_ECDSA_SK: |
markus@openbsd.org | fd1a3b5 | 2019-11-12 19:32:30 +0000 | [diff] [blame] | 629 | alg = SSH_SK_ECDSA; |
| 630 | break; |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 631 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 632 | case KEY_ED25519_SK: |
markus@openbsd.org | fd1a3b5 | 2019-11-12 19:32:30 +0000 | [diff] [blame] | 633 | alg = SSH_SK_ED25519; |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 634 | break; |
| 635 | default: |
| 636 | return SSH_ERR_INVALID_ARGUMENT; |
| 637 | } |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 638 | if (provider_path == NULL || |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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 | } |
markus@openbsd.org | fd1a3b5 | 2019-11-12 19:32:30 +0000 | [diff] [blame] | 656 | if ((r = skp->sk_sign(alg, message, sizeof(message), |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 657 | key->sk_application, |
| 658 | 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] | 659 | key->sk_flags, pin, opts, &resp)) != 0) { |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 660 | debug("%s: sk_sign failed with code %d", __func__, r); |
djm@openbsd.org | 43ce964 | 2019-12-30 09:24:45 +0000 | [diff] [blame] | 661 | r = skerr_to_ssherr(r); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 662 | goto out; |
| 663 | } |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 664 | /* Assemble signature */ |
markus@openbsd.org | bc7b5d6 | 2019-11-12 19:30:21 +0000 | [diff] [blame] | 665 | if ((sig = sshbuf_new()) == NULL) { |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 666 | r = SSH_ERR_ALLOC_FAIL; |
| 667 | goto out; |
| 668 | } |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 669 | 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] | 670 | debug("%s: buffer error (outer): %s", __func__, ssh_err(r)); |
| 671 | goto out; |
| 672 | } |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 673 | switch (type) { |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 674 | #ifdef WITH_OPENSSL |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 675 | case KEY_ECDSA_SK: |
| 676 | if ((r = sshsk_ecdsa_sig(resp, sig)) != 0) |
| 677 | goto out; |
| 678 | break; |
naddy@openbsd.org | 723a536 | 2019-11-18 16:08:57 +0000 | [diff] [blame] | 679 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 680 | case KEY_ED25519_SK: |
| 681 | if ((r = sshsk_ed25519_sig(resp, sig)) != 0) |
| 682 | goto out; |
| 683 | break; |
| 684 | } |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 685 | #ifdef DEBUG_SK |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 686 | fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n", |
| 687 | __func__, resp->flags, resp->counter); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 688 | fprintf(stderr, "%s: hashed message:\n", __func__); |
| 689 | sshbuf_dump_data(message, sizeof(message), stderr); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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: |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 705 | sshsk_free_options(opts); |
djm@openbsd.org | ed3467c | 2019-10-31 21:16:20 +0000 | [diff] [blame] | 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 | } |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 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 |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 734 | sshsk_load_resident(const char *provider_path, const char *device, |
| 735 | const char *pin, struct sshkey ***keysp, size_t *nkeysp) |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 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; |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 743 | struct sk_option **opts = NULL; |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 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 | |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 753 | if ((r = make_options(device, NULL, &opts)) != 0) |
| 754 | goto out; |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 755 | if ((skp = sshsk_open(provider_path)) == NULL) { |
| 756 | r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */ |
| 757 | goto out; |
| 758 | } |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 759 | 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] | 760 | error("Provider \"%s\" returned failure %d", provider_path, r); |
djm@openbsd.org | 43ce964 | 2019-12-30 09:24:45 +0000 | [diff] [blame] | 761 | r = skerr_to_ssherr(r); |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 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: |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 801 | sshsk_free_options(opts); |
djm@openbsd.org | 14cea36 | 2019-12-30 09:21:16 +0000 | [diff] [blame] | 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 | |
Damien Miller | 764d51e | 2019-11-01 13:34:49 +1100 | [diff] [blame] | 813 | #endif /* ENABLE_SK */ |