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