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