1*535af610SEd Maste /* $OpenBSD: ssh-sk.c,v 1.40 2023/07/19 14:02:27 djm Exp $ */
219261079SEd Maste /*
319261079SEd Maste * Copyright (c) 2019 Google LLC
419261079SEd Maste *
519261079SEd Maste * Permission to use, copy, modify, and distribute this software for any
619261079SEd Maste * purpose with or without fee is hereby granted, provided that the above
719261079SEd Maste * copyright notice and this permission notice appear in all copies.
819261079SEd Maste *
919261079SEd Maste * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1019261079SEd Maste * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1119261079SEd Maste * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1219261079SEd Maste * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1319261079SEd Maste * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1419261079SEd Maste * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1519261079SEd Maste * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1619261079SEd Maste */
1719261079SEd Maste
1819261079SEd Maste /* #define DEBUG_SK 1 */
1919261079SEd Maste
2019261079SEd Maste #include "includes.h"
2119261079SEd Maste
2219261079SEd Maste #ifdef ENABLE_SK
2319261079SEd Maste
2419261079SEd Maste #include <dlfcn.h>
2519261079SEd Maste #include <stddef.h>
2619261079SEd Maste #ifdef HAVE_STDINT_H
2719261079SEd Maste # include <stdint.h>
2819261079SEd Maste #endif
2919261079SEd Maste #include <string.h>
3019261079SEd Maste #include <stdio.h>
3119261079SEd Maste
321323ec57SEd Maste #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
3319261079SEd Maste #include <openssl/objects.h>
3419261079SEd Maste #include <openssl/ec.h>
351323ec57SEd Maste #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
3619261079SEd Maste
3719261079SEd Maste #include "log.h"
3819261079SEd Maste #include "misc.h"
3919261079SEd Maste #include "sshbuf.h"
4019261079SEd Maste #include "sshkey.h"
4119261079SEd Maste #include "ssherr.h"
4219261079SEd Maste #include "digest.h"
4319261079SEd Maste
4419261079SEd Maste #include "ssh-sk.h"
4519261079SEd Maste #include "sk-api.h"
4619261079SEd Maste #include "crypto_api.h"
4719261079SEd Maste
481323ec57SEd Maste /*
491323ec57SEd Maste * Almost every use of OpenSSL in this file is for ECDSA-NISTP256.
501323ec57SEd Maste * This is strictly a larger hammer than necessary, but it reduces changes
511323ec57SEd Maste * with upstream.
521323ec57SEd Maste */
531323ec57SEd Maste #ifndef OPENSSL_HAS_ECC
541323ec57SEd Maste # undef WITH_OPENSSL
551323ec57SEd Maste #endif
561323ec57SEd Maste
5719261079SEd Maste struct sshsk_provider {
5819261079SEd Maste char *path;
5919261079SEd Maste void *dlhandle;
6019261079SEd Maste
6119261079SEd Maste /* Return the version of the middleware API */
6219261079SEd Maste uint32_t (*sk_api_version)(void);
6319261079SEd Maste
6419261079SEd Maste /* Enroll a U2F key (private key generation) */
6519261079SEd Maste int (*sk_enroll)(int alg, const uint8_t *challenge,
6619261079SEd Maste size_t challenge_len, const char *application, uint8_t flags,
6719261079SEd Maste const char *pin, struct sk_option **opts,
6819261079SEd Maste struct sk_enroll_response **enroll_response);
6919261079SEd Maste
7019261079SEd Maste /* Sign a challenge */
7119261079SEd Maste int (*sk_sign)(int alg, const uint8_t *message, size_t message_len,
7219261079SEd Maste const char *application,
7319261079SEd Maste const uint8_t *key_handle, size_t key_handle_len,
7419261079SEd Maste uint8_t flags, const char *pin, struct sk_option **opts,
7519261079SEd Maste struct sk_sign_response **sign_response);
7619261079SEd Maste
7719261079SEd Maste /* Enumerate resident keys */
7819261079SEd Maste int (*sk_load_resident_keys)(const char *pin, struct sk_option **opts,
7919261079SEd Maste struct sk_resident_key ***rks, size_t *nrks);
8019261079SEd Maste };
8119261079SEd Maste
8219261079SEd Maste /* Built-in version */
8319261079SEd Maste int ssh_sk_enroll(int alg, const uint8_t *challenge,
8419261079SEd Maste size_t challenge_len, const char *application, uint8_t flags,
8519261079SEd Maste const char *pin, struct sk_option **opts,
8619261079SEd Maste struct sk_enroll_response **enroll_response);
8719261079SEd Maste int ssh_sk_sign(int alg, const uint8_t *message, size_t message_len,
8819261079SEd Maste const char *application,
8919261079SEd Maste const uint8_t *key_handle, size_t key_handle_len,
9019261079SEd Maste uint8_t flags, const char *pin, struct sk_option **opts,
9119261079SEd Maste struct sk_sign_response **sign_response);
9219261079SEd Maste int ssh_sk_load_resident_keys(const char *pin, struct sk_option **opts,
9319261079SEd Maste struct sk_resident_key ***rks, size_t *nrks);
9419261079SEd Maste
9519261079SEd Maste static void
sshsk_free(struct sshsk_provider * p)9619261079SEd Maste sshsk_free(struct sshsk_provider *p)
9719261079SEd Maste {
9819261079SEd Maste if (p == NULL)
9919261079SEd Maste return;
10019261079SEd Maste free(p->path);
10119261079SEd Maste if (p->dlhandle != NULL)
10219261079SEd Maste dlclose(p->dlhandle);
10319261079SEd Maste free(p);
10419261079SEd Maste }
10519261079SEd Maste
10619261079SEd Maste static struct sshsk_provider *
sshsk_open(const char * path)10719261079SEd Maste sshsk_open(const char *path)
10819261079SEd Maste {
10919261079SEd Maste struct sshsk_provider *ret = NULL;
11019261079SEd Maste uint32_t version;
11119261079SEd Maste
11219261079SEd Maste if (path == NULL || *path == '\0') {
11319261079SEd Maste error("No FIDO SecurityKeyProvider specified");
11419261079SEd Maste return NULL;
11519261079SEd Maste }
11619261079SEd Maste if ((ret = calloc(1, sizeof(*ret))) == NULL) {
11719261079SEd Maste error_f("calloc failed");
11819261079SEd Maste return NULL;
11919261079SEd Maste }
12019261079SEd Maste if ((ret->path = strdup(path)) == NULL) {
12119261079SEd Maste error_f("strdup failed");
12219261079SEd Maste goto fail;
12319261079SEd Maste }
12419261079SEd Maste /* Skip the rest if we're using the linked in middleware */
12519261079SEd Maste if (strcasecmp(ret->path, "internal") == 0) {
12619261079SEd Maste #ifdef ENABLE_SK_INTERNAL
12719261079SEd Maste ret->sk_enroll = ssh_sk_enroll;
12819261079SEd Maste ret->sk_sign = ssh_sk_sign;
12919261079SEd Maste ret->sk_load_resident_keys = ssh_sk_load_resident_keys;
13038a52bd3SEd Maste return ret;
13119261079SEd Maste #else
13219261079SEd Maste error("internal security key support not enabled");
13338a52bd3SEd Maste goto fail;
13419261079SEd Maste #endif
13519261079SEd Maste }
136*535af610SEd Maste if (lib_contains_symbol(path, "sk_api_version") != 0) {
137*535af610SEd Maste error("provider %s is not an OpenSSH FIDO library", path);
13819261079SEd Maste goto fail;
13919261079SEd Maste }
140*535af610SEd Maste if ((ret->dlhandle = dlopen(path, RTLD_NOW)) == NULL)
141*535af610SEd Maste fatal("Provider \"%s\" dlopen failed: %s", path, dlerror());
14219261079SEd Maste if ((ret->sk_api_version = dlsym(ret->dlhandle,
14319261079SEd Maste "sk_api_version")) == NULL) {
14419261079SEd Maste error("Provider \"%s\" dlsym(sk_api_version) failed: %s",
14519261079SEd Maste path, dlerror());
14619261079SEd Maste goto fail;
14719261079SEd Maste }
14819261079SEd Maste version = ret->sk_api_version();
14919261079SEd Maste debug_f("provider %s implements version 0x%08lx", ret->path,
15019261079SEd Maste (u_long)version);
15119261079SEd Maste if ((version & SSH_SK_VERSION_MAJOR_MASK) != SSH_SK_VERSION_MAJOR) {
15219261079SEd Maste error("Provider \"%s\" implements unsupported "
15319261079SEd Maste "version 0x%08lx (supported: 0x%08lx)",
15419261079SEd Maste path, (u_long)version, (u_long)SSH_SK_VERSION_MAJOR);
15519261079SEd Maste goto fail;
15619261079SEd Maste }
15719261079SEd Maste if ((ret->sk_enroll = dlsym(ret->dlhandle, "sk_enroll")) == NULL) {
15819261079SEd Maste error("Provider %s dlsym(sk_enroll) failed: %s",
15919261079SEd Maste path, dlerror());
16019261079SEd Maste goto fail;
16119261079SEd Maste }
16219261079SEd Maste if ((ret->sk_sign = dlsym(ret->dlhandle, "sk_sign")) == NULL) {
16319261079SEd Maste error("Provider \"%s\" dlsym(sk_sign) failed: %s",
16419261079SEd Maste path, dlerror());
16519261079SEd Maste goto fail;
16619261079SEd Maste }
16719261079SEd Maste if ((ret->sk_load_resident_keys = dlsym(ret->dlhandle,
16819261079SEd Maste "sk_load_resident_keys")) == NULL) {
16919261079SEd Maste error("Provider \"%s\" dlsym(sk_load_resident_keys) "
17019261079SEd Maste "failed: %s", path, dlerror());
17119261079SEd Maste goto fail;
17219261079SEd Maste }
17319261079SEd Maste /* success */
17419261079SEd Maste return ret;
17519261079SEd Maste fail:
17619261079SEd Maste sshsk_free(ret);
17719261079SEd Maste return NULL;
17819261079SEd Maste }
17919261079SEd Maste
18019261079SEd Maste static void
sshsk_free_enroll_response(struct sk_enroll_response * r)18119261079SEd Maste sshsk_free_enroll_response(struct sk_enroll_response *r)
18219261079SEd Maste {
18319261079SEd Maste if (r == NULL)
18419261079SEd Maste return;
18519261079SEd Maste freezero(r->key_handle, r->key_handle_len);
18619261079SEd Maste freezero(r->public_key, r->public_key_len);
18719261079SEd Maste freezero(r->signature, r->signature_len);
18819261079SEd Maste freezero(r->attestation_cert, r->attestation_cert_len);
18919261079SEd Maste freezero(r->authdata, r->authdata_len);
19019261079SEd Maste freezero(r, sizeof(*r));
19119261079SEd Maste }
19219261079SEd Maste
19319261079SEd Maste static void
sshsk_free_sign_response(struct sk_sign_response * r)19419261079SEd Maste sshsk_free_sign_response(struct sk_sign_response *r)
19519261079SEd Maste {
19619261079SEd Maste if (r == NULL)
19719261079SEd Maste return;
19819261079SEd Maste freezero(r->sig_r, r->sig_r_len);
19919261079SEd Maste freezero(r->sig_s, r->sig_s_len);
20019261079SEd Maste freezero(r, sizeof(*r));
20119261079SEd Maste }
20219261079SEd Maste
20319261079SEd Maste #ifdef WITH_OPENSSL
20419261079SEd Maste /* Assemble key from response */
20519261079SEd Maste static int
sshsk_ecdsa_assemble(struct sk_enroll_response * resp,struct sshkey ** keyp)20619261079SEd Maste sshsk_ecdsa_assemble(struct sk_enroll_response *resp, struct sshkey **keyp)
20719261079SEd Maste {
20819261079SEd Maste struct sshkey *key = NULL;
20919261079SEd Maste struct sshbuf *b = NULL;
21019261079SEd Maste EC_POINT *q = NULL;
21119261079SEd Maste int r;
21219261079SEd Maste
21319261079SEd Maste *keyp = NULL;
21419261079SEd Maste if ((key = sshkey_new(KEY_ECDSA_SK)) == NULL) {
21519261079SEd Maste error_f("sshkey_new failed");
21619261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
21719261079SEd Maste goto out;
21819261079SEd Maste }
21919261079SEd Maste key->ecdsa_nid = NID_X9_62_prime256v1;
22019261079SEd Maste if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) == NULL ||
22119261079SEd Maste (q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL ||
22219261079SEd Maste (b = sshbuf_new()) == NULL) {
22319261079SEd Maste error_f("allocation failed");
22419261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
22519261079SEd Maste goto out;
22619261079SEd Maste }
22719261079SEd Maste if ((r = sshbuf_put_string(b,
22819261079SEd Maste resp->public_key, resp->public_key_len)) != 0) {
22919261079SEd Maste error_fr(r, "sshbuf_put_string");
23019261079SEd Maste goto out;
23119261079SEd Maste }
23219261079SEd Maste if ((r = sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa))) != 0) {
23319261079SEd Maste error_fr(r, "parse");
23419261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
23519261079SEd Maste goto out;
23619261079SEd Maste }
23719261079SEd Maste if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa), q) != 0) {
23819261079SEd Maste error("Authenticator returned invalid ECDSA key");
23919261079SEd Maste r = SSH_ERR_KEY_INVALID_EC_VALUE;
24019261079SEd Maste goto out;
24119261079SEd Maste }
24219261079SEd Maste if (EC_KEY_set_public_key(key->ecdsa, q) != 1) {
24319261079SEd Maste /* XXX assume it is a allocation error */
24419261079SEd Maste error_f("allocation failed");
24519261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
24619261079SEd Maste goto out;
24719261079SEd Maste }
24819261079SEd Maste /* success */
24919261079SEd Maste *keyp = key;
25019261079SEd Maste key = NULL; /* transferred */
25119261079SEd Maste r = 0;
25219261079SEd Maste out:
25319261079SEd Maste EC_POINT_free(q);
25419261079SEd Maste sshkey_free(key);
25519261079SEd Maste sshbuf_free(b);
25619261079SEd Maste return r;
25719261079SEd Maste }
25819261079SEd Maste #endif /* WITH_OPENSSL */
25919261079SEd Maste
26019261079SEd Maste static int
sshsk_ed25519_assemble(struct sk_enroll_response * resp,struct sshkey ** keyp)26119261079SEd Maste sshsk_ed25519_assemble(struct sk_enroll_response *resp, struct sshkey **keyp)
26219261079SEd Maste {
26319261079SEd Maste struct sshkey *key = NULL;
26419261079SEd Maste int r;
26519261079SEd Maste
26619261079SEd Maste *keyp = NULL;
26719261079SEd Maste if (resp->public_key_len != ED25519_PK_SZ) {
26819261079SEd Maste error_f("invalid size: %zu", resp->public_key_len);
26919261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
27019261079SEd Maste goto out;
27119261079SEd Maste }
27219261079SEd Maste if ((key = sshkey_new(KEY_ED25519_SK)) == NULL) {
27319261079SEd Maste error_f("sshkey_new failed");
27419261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
27519261079SEd Maste goto out;
27619261079SEd Maste }
27719261079SEd Maste if ((key->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
27819261079SEd Maste error_f("malloc failed");
27919261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
28019261079SEd Maste goto out;
28119261079SEd Maste }
28219261079SEd Maste memcpy(key->ed25519_pk, resp->public_key, ED25519_PK_SZ);
28319261079SEd Maste /* success */
28419261079SEd Maste *keyp = key;
28519261079SEd Maste key = NULL; /* transferred */
28619261079SEd Maste r = 0;
28719261079SEd Maste out:
28819261079SEd Maste sshkey_free(key);
28919261079SEd Maste return r;
29019261079SEd Maste }
29119261079SEd Maste
29219261079SEd Maste static int
sshsk_key_from_response(int alg,const char * application,uint8_t flags,struct sk_enroll_response * resp,struct sshkey ** keyp)29319261079SEd Maste sshsk_key_from_response(int alg, const char *application, uint8_t flags,
29419261079SEd Maste struct sk_enroll_response *resp, struct sshkey **keyp)
29519261079SEd Maste {
29619261079SEd Maste struct sshkey *key = NULL;
29719261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
29819261079SEd Maste
29919261079SEd Maste *keyp = NULL;
30019261079SEd Maste
30119261079SEd Maste /* Check response validity */
30219261079SEd Maste if (resp->public_key == NULL || resp->key_handle == NULL) {
30319261079SEd Maste error_f("sk_enroll response invalid");
30419261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
30519261079SEd Maste goto out;
30619261079SEd Maste }
30719261079SEd Maste switch (alg) {
30819261079SEd Maste #ifdef WITH_OPENSSL
30919261079SEd Maste case SSH_SK_ECDSA:
31019261079SEd Maste if ((r = sshsk_ecdsa_assemble(resp, &key)) != 0)
31119261079SEd Maste goto out;
31219261079SEd Maste break;
31319261079SEd Maste #endif /* WITH_OPENSSL */
31419261079SEd Maste case SSH_SK_ED25519:
31519261079SEd Maste if ((r = sshsk_ed25519_assemble(resp, &key)) != 0)
31619261079SEd Maste goto out;
31719261079SEd Maste break;
31819261079SEd Maste default:
31919261079SEd Maste error_f("unsupported algorithm %d", alg);
32019261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
32119261079SEd Maste goto out;
32219261079SEd Maste }
32319261079SEd Maste key->sk_flags = flags;
32419261079SEd Maste if ((key->sk_key_handle = sshbuf_new()) == NULL ||
32519261079SEd Maste (key->sk_reserved = sshbuf_new()) == NULL) {
32619261079SEd Maste error_f("allocation failed");
32719261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
32819261079SEd Maste goto out;
32919261079SEd Maste }
33019261079SEd Maste if ((key->sk_application = strdup(application)) == NULL) {
33119261079SEd Maste error_f("strdup application failed");
33219261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
33319261079SEd Maste goto out;
33419261079SEd Maste }
33519261079SEd Maste if ((r = sshbuf_put(key->sk_key_handle, resp->key_handle,
33619261079SEd Maste resp->key_handle_len)) != 0) {
33719261079SEd Maste error_fr(r, "put key handle");
33819261079SEd Maste goto out;
33919261079SEd Maste }
34019261079SEd Maste /* success */
34119261079SEd Maste r = 0;
34219261079SEd Maste *keyp = key;
34319261079SEd Maste key = NULL;
34419261079SEd Maste out:
34519261079SEd Maste sshkey_free(key);
34619261079SEd Maste return r;
34719261079SEd Maste }
34819261079SEd Maste
34919261079SEd Maste static int
skerr_to_ssherr(int skerr)35019261079SEd Maste skerr_to_ssherr(int skerr)
35119261079SEd Maste {
35219261079SEd Maste switch (skerr) {
35319261079SEd Maste case SSH_SK_ERR_UNSUPPORTED:
35419261079SEd Maste return SSH_ERR_FEATURE_UNSUPPORTED;
35519261079SEd Maste case SSH_SK_ERR_PIN_REQUIRED:
35619261079SEd Maste return SSH_ERR_KEY_WRONG_PASSPHRASE;
35719261079SEd Maste case SSH_SK_ERR_DEVICE_NOT_FOUND:
35819261079SEd Maste return SSH_ERR_DEVICE_NOT_FOUND;
35938a52bd3SEd Maste case SSH_SK_ERR_CREDENTIAL_EXISTS:
36038a52bd3SEd Maste return SSH_ERR_KEY_BAD_PERMISSIONS;
36119261079SEd Maste case SSH_SK_ERR_GENERAL:
36219261079SEd Maste default:
36319261079SEd Maste return SSH_ERR_INVALID_FORMAT;
36419261079SEd Maste }
36519261079SEd Maste }
36619261079SEd Maste
36719261079SEd Maste static void
sshsk_free_options(struct sk_option ** opts)36819261079SEd Maste sshsk_free_options(struct sk_option **opts)
36919261079SEd Maste {
37019261079SEd Maste size_t i;
37119261079SEd Maste
37219261079SEd Maste if (opts == NULL)
37319261079SEd Maste return;
37419261079SEd Maste for (i = 0; opts[i] != NULL; i++) {
37519261079SEd Maste free(opts[i]->name);
37619261079SEd Maste free(opts[i]->value);
37719261079SEd Maste free(opts[i]);
37819261079SEd Maste }
37919261079SEd Maste free(opts);
38019261079SEd Maste }
38119261079SEd Maste
38219261079SEd Maste static int
sshsk_add_option(struct sk_option *** optsp,size_t * noptsp,const char * name,const char * value,uint8_t required)38319261079SEd Maste sshsk_add_option(struct sk_option ***optsp, size_t *noptsp,
38419261079SEd Maste const char *name, const char *value, uint8_t required)
38519261079SEd Maste {
38619261079SEd Maste struct sk_option **opts = *optsp;
38719261079SEd Maste size_t nopts = *noptsp;
38819261079SEd Maste
38919261079SEd Maste if ((opts = recallocarray(opts, nopts, nopts + 2, /* extra for NULL */
39019261079SEd Maste sizeof(*opts))) == NULL) {
39119261079SEd Maste error_f("array alloc failed");
39219261079SEd Maste return SSH_ERR_ALLOC_FAIL;
39319261079SEd Maste }
39419261079SEd Maste *optsp = opts;
39519261079SEd Maste *noptsp = nopts + 1;
39619261079SEd Maste if ((opts[nopts] = calloc(1, sizeof(**opts))) == NULL) {
39719261079SEd Maste error_f("alloc failed");
39819261079SEd Maste return SSH_ERR_ALLOC_FAIL;
39919261079SEd Maste }
40019261079SEd Maste if ((opts[nopts]->name = strdup(name)) == NULL ||
40119261079SEd Maste (opts[nopts]->value = strdup(value)) == NULL) {
40219261079SEd Maste error_f("alloc failed");
40319261079SEd Maste return SSH_ERR_ALLOC_FAIL;
40419261079SEd Maste }
40519261079SEd Maste opts[nopts]->required = required;
40619261079SEd Maste return 0;
40719261079SEd Maste }
40819261079SEd Maste
40919261079SEd Maste static int
make_options(const char * device,const char * user_id,struct sk_option *** optsp)41019261079SEd Maste make_options(const char *device, const char *user_id,
41119261079SEd Maste struct sk_option ***optsp)
41219261079SEd Maste {
41319261079SEd Maste struct sk_option **opts = NULL;
41419261079SEd Maste size_t nopts = 0;
41519261079SEd Maste int r, ret = SSH_ERR_INTERNAL_ERROR;
41619261079SEd Maste
41719261079SEd Maste if (device != NULL &&
41819261079SEd Maste (r = sshsk_add_option(&opts, &nopts, "device", device, 0)) != 0) {
41919261079SEd Maste ret = r;
42019261079SEd Maste goto out;
42119261079SEd Maste }
42219261079SEd Maste if (user_id != NULL &&
42319261079SEd Maste (r = sshsk_add_option(&opts, &nopts, "user", user_id, 0)) != 0) {
42419261079SEd Maste ret = r;
42519261079SEd Maste goto out;
42619261079SEd Maste }
42719261079SEd Maste /* success */
42819261079SEd Maste *optsp = opts;
42919261079SEd Maste opts = NULL;
43019261079SEd Maste nopts = 0;
43119261079SEd Maste ret = 0;
43219261079SEd Maste out:
43319261079SEd Maste sshsk_free_options(opts);
43419261079SEd Maste return ret;
43519261079SEd Maste }
43619261079SEd Maste
43719261079SEd Maste
43819261079SEd Maste static int
fill_attestation_blob(const struct sk_enroll_response * resp,struct sshbuf * attest)43919261079SEd Maste fill_attestation_blob(const struct sk_enroll_response *resp,
44019261079SEd Maste struct sshbuf *attest)
44119261079SEd Maste {
44219261079SEd Maste int r;
44319261079SEd Maste
44419261079SEd Maste if (attest == NULL)
44519261079SEd Maste return 0; /* nothing to do */
44619261079SEd Maste if ((r = sshbuf_put_cstring(attest, "ssh-sk-attest-v01")) != 0 ||
44719261079SEd Maste (r = sshbuf_put_string(attest,
44819261079SEd Maste resp->attestation_cert, resp->attestation_cert_len)) != 0 ||
44919261079SEd Maste (r = sshbuf_put_string(attest,
45019261079SEd Maste resp->signature, resp->signature_len)) != 0 ||
45119261079SEd Maste (r = sshbuf_put_string(attest,
45219261079SEd Maste resp->authdata, resp->authdata_len)) != 0 ||
45319261079SEd Maste (r = sshbuf_put_u32(attest, 0)) != 0 || /* resvd flags */
45419261079SEd Maste (r = sshbuf_put_string(attest, NULL, 0)) != 0 /* resvd */) {
45519261079SEd Maste error_fr(r, "compose");
45619261079SEd Maste return r;
45719261079SEd Maste }
45819261079SEd Maste /* success */
45919261079SEd Maste return 0;
46019261079SEd Maste }
46119261079SEd Maste
46219261079SEd Maste int
sshsk_enroll(int type,const char * provider_path,const char * device,const char * application,const char * userid,uint8_t flags,const char * pin,struct sshbuf * challenge_buf,struct sshkey ** keyp,struct sshbuf * attest)46319261079SEd Maste sshsk_enroll(int type, const char *provider_path, const char *device,
46419261079SEd Maste const char *application, const char *userid, uint8_t flags,
46519261079SEd Maste const char *pin, struct sshbuf *challenge_buf,
46619261079SEd Maste struct sshkey **keyp, struct sshbuf *attest)
46719261079SEd Maste {
46819261079SEd Maste struct sshsk_provider *skp = NULL;
46919261079SEd Maste struct sshkey *key = NULL;
47019261079SEd Maste u_char randchall[32];
47119261079SEd Maste const u_char *challenge;
47219261079SEd Maste size_t challenge_len;
47319261079SEd Maste struct sk_enroll_response *resp = NULL;
47419261079SEd Maste struct sk_option **opts = NULL;
47519261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
47619261079SEd Maste int alg;
47719261079SEd Maste
47819261079SEd Maste debug_f("provider \"%s\", device \"%s\", application \"%s\", "
47919261079SEd Maste "userid \"%s\", flags 0x%02x, challenge len %zu%s",
48019261079SEd Maste provider_path, device, application, userid, flags,
48119261079SEd Maste challenge_buf == NULL ? 0 : sshbuf_len(challenge_buf),
48219261079SEd Maste (pin != NULL && *pin != '\0') ? " with-pin" : "");
48319261079SEd Maste
48419261079SEd Maste *keyp = NULL;
48519261079SEd Maste if (attest)
48619261079SEd Maste sshbuf_reset(attest);
48719261079SEd Maste
48819261079SEd Maste if ((r = make_options(device, userid, &opts)) != 0)
48919261079SEd Maste goto out;
49019261079SEd Maste
49119261079SEd Maste switch (type) {
49219261079SEd Maste #ifdef WITH_OPENSSL
49319261079SEd Maste case KEY_ECDSA_SK:
49419261079SEd Maste alg = SSH_SK_ECDSA;
49519261079SEd Maste break;
49619261079SEd Maste #endif /* WITH_OPENSSL */
49719261079SEd Maste case KEY_ED25519_SK:
49819261079SEd Maste alg = SSH_SK_ED25519;
49919261079SEd Maste break;
50019261079SEd Maste default:
50119261079SEd Maste error_f("unsupported key type");
50219261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
50319261079SEd Maste goto out;
50419261079SEd Maste }
50519261079SEd Maste if (provider_path == NULL) {
50619261079SEd Maste error_f("missing provider");
50719261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
50819261079SEd Maste goto out;
50919261079SEd Maste }
51019261079SEd Maste if (application == NULL || *application == '\0') {
51119261079SEd Maste error_f("missing application");
51219261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
51319261079SEd Maste goto out;
51419261079SEd Maste }
51519261079SEd Maste if (challenge_buf == NULL) {
51619261079SEd Maste debug_f("using random challenge");
51719261079SEd Maste arc4random_buf(randchall, sizeof(randchall));
51819261079SEd Maste challenge = randchall;
51919261079SEd Maste challenge_len = sizeof(randchall);
52019261079SEd Maste } else if (sshbuf_len(challenge_buf) == 0) {
52119261079SEd Maste error("Missing enrollment challenge");
52219261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
52319261079SEd Maste goto out;
52419261079SEd Maste } else {
52519261079SEd Maste challenge = sshbuf_ptr(challenge_buf);
52619261079SEd Maste challenge_len = sshbuf_len(challenge_buf);
52719261079SEd Maste debug3_f("using explicit challenge len=%zd", challenge_len);
52819261079SEd Maste }
52919261079SEd Maste if ((skp = sshsk_open(provider_path)) == NULL) {
53019261079SEd Maste r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
53119261079SEd Maste goto out;
53219261079SEd Maste }
53319261079SEd Maste /* XXX validate flags? */
53419261079SEd Maste /* enroll key */
53519261079SEd Maste if ((r = skp->sk_enroll(alg, challenge, challenge_len, application,
53619261079SEd Maste flags, pin, opts, &resp)) != 0) {
53719261079SEd Maste debug_f("provider \"%s\" failure %d", provider_path, r);
53819261079SEd Maste r = skerr_to_ssherr(r);
53919261079SEd Maste goto out;
54019261079SEd Maste }
54119261079SEd Maste
5421323ec57SEd Maste if ((r = sshsk_key_from_response(alg, application, resp->flags,
54319261079SEd Maste resp, &key)) != 0)
54419261079SEd Maste goto out;
54519261079SEd Maste
54619261079SEd Maste /* Optionally fill in the attestation information */
54719261079SEd Maste if ((r = fill_attestation_blob(resp, attest)) != 0)
54819261079SEd Maste goto out;
54919261079SEd Maste
55019261079SEd Maste /* success */
55119261079SEd Maste *keyp = key;
55219261079SEd Maste key = NULL; /* transferred */
55319261079SEd Maste r = 0;
55419261079SEd Maste out:
55519261079SEd Maste sshsk_free_options(opts);
55619261079SEd Maste sshsk_free(skp);
55719261079SEd Maste sshkey_free(key);
55819261079SEd Maste sshsk_free_enroll_response(resp);
55919261079SEd Maste explicit_bzero(randchall, sizeof(randchall));
56019261079SEd Maste return r;
56119261079SEd Maste }
56219261079SEd Maste
56319261079SEd Maste #ifdef WITH_OPENSSL
56419261079SEd Maste static int
sshsk_ecdsa_sig(struct sk_sign_response * resp,struct sshbuf * sig)56519261079SEd Maste sshsk_ecdsa_sig(struct sk_sign_response *resp, struct sshbuf *sig)
56619261079SEd Maste {
56719261079SEd Maste struct sshbuf *inner_sig = NULL;
56819261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
56919261079SEd Maste
57019261079SEd Maste /* Check response validity */
57119261079SEd Maste if (resp->sig_r == NULL || resp->sig_s == NULL) {
57219261079SEd Maste error_f("sk_sign response invalid");
57319261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
57419261079SEd Maste goto out;
57519261079SEd Maste }
57619261079SEd Maste if ((inner_sig = sshbuf_new()) == NULL) {
57719261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
57819261079SEd Maste goto out;
57919261079SEd Maste }
58019261079SEd Maste /* Prepare and append inner signature object */
58119261079SEd Maste if ((r = sshbuf_put_bignum2_bytes(inner_sig,
58219261079SEd Maste resp->sig_r, resp->sig_r_len)) != 0 ||
58319261079SEd Maste (r = sshbuf_put_bignum2_bytes(inner_sig,
58419261079SEd Maste resp->sig_s, resp->sig_s_len)) != 0) {
58519261079SEd Maste error_fr(r, "compose inner");
58619261079SEd Maste goto out;
58719261079SEd Maste }
58819261079SEd Maste if ((r = sshbuf_put_stringb(sig, inner_sig)) != 0 ||
58919261079SEd Maste (r = sshbuf_put_u8(sig, resp->flags)) != 0 ||
59019261079SEd Maste (r = sshbuf_put_u32(sig, resp->counter)) != 0) {
59119261079SEd Maste error_fr(r, "compose");
59219261079SEd Maste goto out;
59319261079SEd Maste }
59419261079SEd Maste #ifdef DEBUG_SK
59519261079SEd Maste fprintf(stderr, "%s: sig_r:\n", __func__);
59619261079SEd Maste sshbuf_dump_data(resp->sig_r, resp->sig_r_len, stderr);
59719261079SEd Maste fprintf(stderr, "%s: sig_s:\n", __func__);
59819261079SEd Maste sshbuf_dump_data(resp->sig_s, resp->sig_s_len, stderr);
59919261079SEd Maste fprintf(stderr, "%s: inner:\n", __func__);
60019261079SEd Maste sshbuf_dump(inner_sig, stderr);
60119261079SEd Maste #endif
60219261079SEd Maste r = 0;
60319261079SEd Maste out:
60419261079SEd Maste sshbuf_free(inner_sig);
60519261079SEd Maste return r;
60619261079SEd Maste }
60719261079SEd Maste #endif /* WITH_OPENSSL */
60819261079SEd Maste
60919261079SEd Maste static int
sshsk_ed25519_sig(struct sk_sign_response * resp,struct sshbuf * sig)61019261079SEd Maste sshsk_ed25519_sig(struct sk_sign_response *resp, struct sshbuf *sig)
61119261079SEd Maste {
61219261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
61319261079SEd Maste
61419261079SEd Maste /* Check response validity */
61519261079SEd Maste if (resp->sig_r == NULL) {
61619261079SEd Maste error_f("sk_sign response invalid");
61719261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
61819261079SEd Maste goto out;
61919261079SEd Maste }
62019261079SEd Maste if ((r = sshbuf_put_string(sig,
62119261079SEd Maste resp->sig_r, resp->sig_r_len)) != 0 ||
62219261079SEd Maste (r = sshbuf_put_u8(sig, resp->flags)) != 0 ||
62319261079SEd Maste (r = sshbuf_put_u32(sig, resp->counter)) != 0) {
62419261079SEd Maste error_fr(r, "compose");
62519261079SEd Maste goto out;
62619261079SEd Maste }
62719261079SEd Maste #ifdef DEBUG_SK
62819261079SEd Maste fprintf(stderr, "%s: sig_r:\n", __func__);
62919261079SEd Maste sshbuf_dump_data(resp->sig_r, resp->sig_r_len, stderr);
63019261079SEd Maste #endif
63119261079SEd Maste r = 0;
63219261079SEd Maste out:
63319261079SEd Maste return r;
63419261079SEd Maste }
63519261079SEd Maste
63619261079SEd Maste int
sshsk_sign(const char * provider_path,struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,u_int compat,const char * pin)63719261079SEd Maste sshsk_sign(const char *provider_path, struct sshkey *key,
63819261079SEd Maste u_char **sigp, size_t *lenp, const u_char *data, size_t datalen,
63919261079SEd Maste u_int compat, const char *pin)
64019261079SEd Maste {
64119261079SEd Maste struct sshsk_provider *skp = NULL;
64219261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
64319261079SEd Maste int type, alg;
64419261079SEd Maste struct sk_sign_response *resp = NULL;
64519261079SEd Maste struct sshbuf *inner_sig = NULL, *sig = NULL;
64619261079SEd Maste struct sk_option **opts = NULL;
64719261079SEd Maste
64819261079SEd Maste debug_f("provider \"%s\", key %s, flags 0x%02x%s",
64919261079SEd Maste provider_path, sshkey_type(key), key->sk_flags,
65019261079SEd Maste (pin != NULL && *pin != '\0') ? " with-pin" : "");
65119261079SEd Maste
65219261079SEd Maste if (sigp != NULL)
65319261079SEd Maste *sigp = NULL;
65419261079SEd Maste if (lenp != NULL)
65519261079SEd Maste *lenp = 0;
65619261079SEd Maste type = sshkey_type_plain(key->type);
65719261079SEd Maste switch (type) {
65819261079SEd Maste #ifdef WITH_OPENSSL
65919261079SEd Maste case KEY_ECDSA_SK:
66019261079SEd Maste alg = SSH_SK_ECDSA;
66119261079SEd Maste break;
66219261079SEd Maste #endif /* WITH_OPENSSL */
66319261079SEd Maste case KEY_ED25519_SK:
66419261079SEd Maste alg = SSH_SK_ED25519;
66519261079SEd Maste break;
66619261079SEd Maste default:
66719261079SEd Maste return SSH_ERR_INVALID_ARGUMENT;
66819261079SEd Maste }
66919261079SEd Maste if (provider_path == NULL ||
67019261079SEd Maste key->sk_key_handle == NULL ||
67119261079SEd Maste key->sk_application == NULL || *key->sk_application == '\0') {
67219261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
67319261079SEd Maste goto out;
67419261079SEd Maste }
67519261079SEd Maste if ((skp = sshsk_open(provider_path)) == NULL) {
67619261079SEd Maste r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
67719261079SEd Maste goto out;
67819261079SEd Maste }
67919261079SEd Maste #ifdef DEBUG_SK
68019261079SEd Maste fprintf(stderr, "%s: sk_flags = 0x%02x, sk_application = \"%s\"\n",
68119261079SEd Maste __func__, key->sk_flags, key->sk_application);
68219261079SEd Maste fprintf(stderr, "%s: sk_key_handle:\n", __func__);
68319261079SEd Maste sshbuf_dump(key->sk_key_handle, stderr);
68419261079SEd Maste #endif
68519261079SEd Maste if ((r = skp->sk_sign(alg, data, datalen, key->sk_application,
68619261079SEd Maste sshbuf_ptr(key->sk_key_handle), sshbuf_len(key->sk_key_handle),
68719261079SEd Maste key->sk_flags, pin, opts, &resp)) != 0) {
68819261079SEd Maste debug_f("sk_sign failed with code %d", r);
68919261079SEd Maste r = skerr_to_ssherr(r);
69019261079SEd Maste goto out;
69119261079SEd Maste }
69219261079SEd Maste /* Assemble signature */
69319261079SEd Maste if ((sig = sshbuf_new()) == NULL) {
69419261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
69519261079SEd Maste goto out;
69619261079SEd Maste }
69719261079SEd Maste if ((r = sshbuf_put_cstring(sig, sshkey_ssh_name_plain(key))) != 0) {
69819261079SEd Maste error_fr(r, "compose outer");
69919261079SEd Maste goto out;
70019261079SEd Maste }
70119261079SEd Maste switch (type) {
70219261079SEd Maste #ifdef WITH_OPENSSL
70319261079SEd Maste case KEY_ECDSA_SK:
70419261079SEd Maste if ((r = sshsk_ecdsa_sig(resp, sig)) != 0)
70519261079SEd Maste goto out;
70619261079SEd Maste break;
70719261079SEd Maste #endif /* WITH_OPENSSL */
70819261079SEd Maste case KEY_ED25519_SK:
70919261079SEd Maste if ((r = sshsk_ed25519_sig(resp, sig)) != 0)
71019261079SEd Maste goto out;
71119261079SEd Maste break;
71219261079SEd Maste }
71319261079SEd Maste #ifdef DEBUG_SK
71419261079SEd Maste fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
71519261079SEd Maste __func__, resp->flags, resp->counter);
71619261079SEd Maste fprintf(stderr, "%s: data to sign:\n", __func__);
71719261079SEd Maste sshbuf_dump_data(data, datalen, stderr);
71819261079SEd Maste fprintf(stderr, "%s: sigbuf:\n", __func__);
71919261079SEd Maste sshbuf_dump(sig, stderr);
72019261079SEd Maste #endif
72119261079SEd Maste if (sigp != NULL) {
72219261079SEd Maste if ((*sigp = malloc(sshbuf_len(sig))) == NULL) {
72319261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
72419261079SEd Maste goto out;
72519261079SEd Maste }
72619261079SEd Maste memcpy(*sigp, sshbuf_ptr(sig), sshbuf_len(sig));
72719261079SEd Maste }
72819261079SEd Maste if (lenp != NULL)
72919261079SEd Maste *lenp = sshbuf_len(sig);
73019261079SEd Maste /* success */
73119261079SEd Maste r = 0;
73219261079SEd Maste out:
73319261079SEd Maste sshsk_free_options(opts);
73419261079SEd Maste sshsk_free(skp);
73519261079SEd Maste sshsk_free_sign_response(resp);
73619261079SEd Maste sshbuf_free(sig);
73719261079SEd Maste sshbuf_free(inner_sig);
73819261079SEd Maste return r;
73919261079SEd Maste }
74019261079SEd Maste
74119261079SEd Maste static void
sshsk_free_sk_resident_keys(struct sk_resident_key ** rks,size_t nrks)74219261079SEd Maste sshsk_free_sk_resident_keys(struct sk_resident_key **rks, size_t nrks)
74319261079SEd Maste {
74419261079SEd Maste size_t i;
74519261079SEd Maste
74619261079SEd Maste if (nrks == 0 || rks == NULL)
74719261079SEd Maste return;
74819261079SEd Maste for (i = 0; i < nrks; i++) {
74919261079SEd Maste free(rks[i]->application);
7501323ec57SEd Maste freezero(rks[i]->user_id, rks[i]->user_id_len);
75119261079SEd Maste freezero(rks[i]->key.key_handle, rks[i]->key.key_handle_len);
75219261079SEd Maste freezero(rks[i]->key.public_key, rks[i]->key.public_key_len);
75319261079SEd Maste freezero(rks[i]->key.signature, rks[i]->key.signature_len);
75419261079SEd Maste freezero(rks[i]->key.attestation_cert,
75519261079SEd Maste rks[i]->key.attestation_cert_len);
75619261079SEd Maste freezero(rks[i], sizeof(**rks));
75719261079SEd Maste }
75819261079SEd Maste free(rks);
75919261079SEd Maste }
76019261079SEd Maste
7611323ec57SEd Maste static void
sshsk_free_resident_key(struct sshsk_resident_key * srk)7621323ec57SEd Maste sshsk_free_resident_key(struct sshsk_resident_key *srk)
7631323ec57SEd Maste {
7641323ec57SEd Maste if (srk == NULL)
7651323ec57SEd Maste return;
7661323ec57SEd Maste sshkey_free(srk->key);
7671323ec57SEd Maste freezero(srk->user_id, srk->user_id_len);
7681323ec57SEd Maste free(srk);
7691323ec57SEd Maste }
7701323ec57SEd Maste
7711323ec57SEd Maste
7721323ec57SEd Maste void
sshsk_free_resident_keys(struct sshsk_resident_key ** srks,size_t nsrks)7731323ec57SEd Maste sshsk_free_resident_keys(struct sshsk_resident_key **srks, size_t nsrks)
7741323ec57SEd Maste {
7751323ec57SEd Maste size_t i;
7761323ec57SEd Maste
7771323ec57SEd Maste if (srks == NULL || nsrks == 0)
7781323ec57SEd Maste return;
7791323ec57SEd Maste
7801323ec57SEd Maste for (i = 0; i < nsrks; i++)
7811323ec57SEd Maste sshsk_free_resident_key(srks[i]);
7821323ec57SEd Maste free(srks);
7831323ec57SEd Maste }
7841323ec57SEd Maste
78519261079SEd Maste int
sshsk_load_resident(const char * provider_path,const char * device,const char * pin,u_int flags,struct sshsk_resident_key *** srksp,size_t * nsrksp)78619261079SEd Maste sshsk_load_resident(const char *provider_path, const char *device,
7871323ec57SEd Maste const char *pin, u_int flags, struct sshsk_resident_key ***srksp,
7881323ec57SEd Maste size_t *nsrksp)
78919261079SEd Maste {
79019261079SEd Maste struct sshsk_provider *skp = NULL;
79119261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
79219261079SEd Maste struct sk_resident_key **rks = NULL;
7931323ec57SEd Maste size_t i, nrks = 0, nsrks = 0;
7941323ec57SEd Maste struct sshkey *key = NULL;
7951323ec57SEd Maste struct sshsk_resident_key *srk = NULL, **srks = NULL, **tmp;
7961323ec57SEd Maste uint8_t sk_flags;
79719261079SEd Maste struct sk_option **opts = NULL;
79819261079SEd Maste
79919261079SEd Maste debug_f("provider \"%s\"%s", provider_path,
80019261079SEd Maste (pin != NULL && *pin != '\0') ? ", have-pin": "");
80119261079SEd Maste
8021323ec57SEd Maste if (srksp == NULL || nsrksp == NULL)
80319261079SEd Maste return SSH_ERR_INVALID_ARGUMENT;
8041323ec57SEd Maste *srksp = NULL;
8051323ec57SEd Maste *nsrksp = 0;
80619261079SEd Maste
80719261079SEd Maste if ((r = make_options(device, NULL, &opts)) != 0)
80819261079SEd Maste goto out;
80919261079SEd Maste if ((skp = sshsk_open(provider_path)) == NULL) {
81019261079SEd Maste r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
81119261079SEd Maste goto out;
81219261079SEd Maste }
81319261079SEd Maste if ((r = skp->sk_load_resident_keys(pin, opts, &rks, &nrks)) != 0) {
81419261079SEd Maste error("Provider \"%s\" returned failure %d", provider_path, r);
81519261079SEd Maste r = skerr_to_ssherr(r);
81619261079SEd Maste goto out;
81719261079SEd Maste }
81819261079SEd Maste for (i = 0; i < nrks; i++) {
8191323ec57SEd Maste debug3_f("rk %zu: slot %zu, alg %d, app \"%s\", uidlen %zu",
8201323ec57SEd Maste i, rks[i]->slot, rks[i]->alg, rks[i]->application,
8211323ec57SEd Maste rks[i]->user_id_len);
82219261079SEd Maste /* XXX need better filter here */
82319261079SEd Maste if (strncmp(rks[i]->application, "ssh:", 4) != 0)
82419261079SEd Maste continue;
82519261079SEd Maste switch (rks[i]->alg) {
82619261079SEd Maste case SSH_SK_ECDSA:
82719261079SEd Maste case SSH_SK_ED25519:
82819261079SEd Maste break;
82919261079SEd Maste default:
83019261079SEd Maste continue;
83119261079SEd Maste }
8321323ec57SEd Maste sk_flags = SSH_SK_USER_PRESENCE_REQD|SSH_SK_RESIDENT_KEY;
83319261079SEd Maste if ((rks[i]->flags & SSH_SK_USER_VERIFICATION_REQD))
8341323ec57SEd Maste sk_flags |= SSH_SK_USER_VERIFICATION_REQD;
83519261079SEd Maste if ((r = sshsk_key_from_response(rks[i]->alg,
8361323ec57SEd Maste rks[i]->application, sk_flags, &rks[i]->key, &key)) != 0)
83719261079SEd Maste goto out;
8381323ec57SEd Maste if ((srk = calloc(1, sizeof(*srk))) == NULL) {
8391323ec57SEd Maste error_f("calloc failed");
8401323ec57SEd Maste r = SSH_ERR_ALLOC_FAIL;
8411323ec57SEd Maste goto out;
8421323ec57SEd Maste }
8431323ec57SEd Maste srk->key = key;
8441323ec57SEd Maste key = NULL; /* transferred */
8451323ec57SEd Maste if ((srk->user_id = calloc(1, rks[i]->user_id_len)) == NULL) {
8461323ec57SEd Maste error_f("calloc failed");
8471323ec57SEd Maste r = SSH_ERR_ALLOC_FAIL;
8481323ec57SEd Maste goto out;
8491323ec57SEd Maste }
8501323ec57SEd Maste memcpy(srk->user_id, rks[i]->user_id, rks[i]->user_id_len);
8511323ec57SEd Maste srk->user_id_len = rks[i]->user_id_len;
8521323ec57SEd Maste if ((tmp = recallocarray(srks, nsrks, nsrks + 1,
85319261079SEd Maste sizeof(*tmp))) == NULL) {
85419261079SEd Maste error_f("recallocarray failed");
85519261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
85619261079SEd Maste goto out;
85719261079SEd Maste }
8581323ec57SEd Maste srks = tmp;
8591323ec57SEd Maste srks[nsrks++] = srk;
8601323ec57SEd Maste srk = NULL;
86119261079SEd Maste /* XXX synthesise comment */
86219261079SEd Maste }
86319261079SEd Maste /* success */
8641323ec57SEd Maste *srksp = srks;
8651323ec57SEd Maste *nsrksp = nsrks;
8661323ec57SEd Maste srks = NULL;
8671323ec57SEd Maste nsrks = 0;
86819261079SEd Maste r = 0;
86919261079SEd Maste out:
87019261079SEd Maste sshsk_free_options(opts);
87119261079SEd Maste sshsk_free(skp);
87219261079SEd Maste sshsk_free_sk_resident_keys(rks, nrks);
87319261079SEd Maste sshkey_free(key);
8741323ec57SEd Maste sshsk_free_resident_key(srk);
8751323ec57SEd Maste sshsk_free_resident_keys(srks, nsrks);
87619261079SEd Maste return r;
87719261079SEd Maste }
87819261079SEd Maste
87919261079SEd Maste #endif /* ENABLE_SK */
880