1*3d9fd9fcSEd Maste /* $OpenBSD: ssh-sk.c,v 1.41 2024/08/15 00:51:51 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>
35*3d9fd9fcSEd Maste #include <openssl/evp.h>
361323ec57SEd Maste #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
3719261079SEd Maste
3819261079SEd Maste #include "log.h"
3919261079SEd Maste #include "misc.h"
4019261079SEd Maste #include "sshbuf.h"
4119261079SEd Maste #include "sshkey.h"
4219261079SEd Maste #include "ssherr.h"
4319261079SEd Maste #include "digest.h"
4419261079SEd Maste
4519261079SEd Maste #include "ssh-sk.h"
4619261079SEd Maste #include "sk-api.h"
4719261079SEd Maste #include "crypto_api.h"
4819261079SEd Maste
491323ec57SEd Maste /*
501323ec57SEd Maste * Almost every use of OpenSSL in this file is for ECDSA-NISTP256.
511323ec57SEd Maste * This is strictly a larger hammer than necessary, but it reduces changes
521323ec57SEd Maste * with upstream.
531323ec57SEd Maste */
541323ec57SEd Maste #ifndef OPENSSL_HAS_ECC
551323ec57SEd Maste # undef WITH_OPENSSL
561323ec57SEd Maste #endif
571323ec57SEd Maste
5819261079SEd Maste struct sshsk_provider {
5919261079SEd Maste char *path;
6019261079SEd Maste void *dlhandle;
6119261079SEd Maste
6219261079SEd Maste /* Return the version of the middleware API */
6319261079SEd Maste uint32_t (*sk_api_version)(void);
6419261079SEd Maste
6519261079SEd Maste /* Enroll a U2F key (private key generation) */
6619261079SEd Maste int (*sk_enroll)(int alg, const uint8_t *challenge,
6719261079SEd Maste size_t challenge_len, const char *application, uint8_t flags,
6819261079SEd Maste const char *pin, struct sk_option **opts,
6919261079SEd Maste struct sk_enroll_response **enroll_response);
7019261079SEd Maste
7119261079SEd Maste /* Sign a challenge */
7219261079SEd Maste int (*sk_sign)(int alg, const uint8_t *message, size_t message_len,
7319261079SEd Maste const char *application,
7419261079SEd Maste const uint8_t *key_handle, size_t key_handle_len,
7519261079SEd Maste uint8_t flags, const char *pin, struct sk_option **opts,
7619261079SEd Maste struct sk_sign_response **sign_response);
7719261079SEd Maste
7819261079SEd Maste /* Enumerate resident keys */
7919261079SEd Maste int (*sk_load_resident_keys)(const char *pin, struct sk_option **opts,
8019261079SEd Maste struct sk_resident_key ***rks, size_t *nrks);
8119261079SEd Maste };
8219261079SEd Maste
8319261079SEd Maste /* Built-in version */
8419261079SEd Maste int ssh_sk_enroll(int alg, const uint8_t *challenge,
8519261079SEd Maste size_t challenge_len, const char *application, uint8_t flags,
8619261079SEd Maste const char *pin, struct sk_option **opts,
8719261079SEd Maste struct sk_enroll_response **enroll_response);
8819261079SEd Maste int ssh_sk_sign(int alg, const uint8_t *message, size_t message_len,
8919261079SEd Maste const char *application,
9019261079SEd Maste const uint8_t *key_handle, size_t key_handle_len,
9119261079SEd Maste uint8_t flags, const char *pin, struct sk_option **opts,
9219261079SEd Maste struct sk_sign_response **sign_response);
9319261079SEd Maste int ssh_sk_load_resident_keys(const char *pin, struct sk_option **opts,
9419261079SEd Maste struct sk_resident_key ***rks, size_t *nrks);
9519261079SEd Maste
9619261079SEd Maste static void
sshsk_free(struct sshsk_provider * p)9719261079SEd Maste sshsk_free(struct sshsk_provider *p)
9819261079SEd Maste {
9919261079SEd Maste if (p == NULL)
10019261079SEd Maste return;
10119261079SEd Maste free(p->path);
10219261079SEd Maste if (p->dlhandle != NULL)
10319261079SEd Maste dlclose(p->dlhandle);
10419261079SEd Maste free(p);
10519261079SEd Maste }
10619261079SEd Maste
10719261079SEd Maste static struct sshsk_provider *
sshsk_open(const char * path)10819261079SEd Maste sshsk_open(const char *path)
10919261079SEd Maste {
11019261079SEd Maste struct sshsk_provider *ret = NULL;
11119261079SEd Maste uint32_t version;
11219261079SEd Maste
11319261079SEd Maste if (path == NULL || *path == '\0') {
11419261079SEd Maste error("No FIDO SecurityKeyProvider specified");
11519261079SEd Maste return NULL;
11619261079SEd Maste }
11719261079SEd Maste if ((ret = calloc(1, sizeof(*ret))) == NULL) {
11819261079SEd Maste error_f("calloc failed");
11919261079SEd Maste return NULL;
12019261079SEd Maste }
12119261079SEd Maste if ((ret->path = strdup(path)) == NULL) {
12219261079SEd Maste error_f("strdup failed");
12319261079SEd Maste goto fail;
12419261079SEd Maste }
12519261079SEd Maste /* Skip the rest if we're using the linked in middleware */
12619261079SEd Maste if (strcasecmp(ret->path, "internal") == 0) {
12719261079SEd Maste #ifdef ENABLE_SK_INTERNAL
12819261079SEd Maste ret->sk_enroll = ssh_sk_enroll;
12919261079SEd Maste ret->sk_sign = ssh_sk_sign;
13019261079SEd Maste ret->sk_load_resident_keys = ssh_sk_load_resident_keys;
13138a52bd3SEd Maste return ret;
13219261079SEd Maste #else
13319261079SEd Maste error("internal security key support not enabled");
13438a52bd3SEd Maste goto fail;
13519261079SEd Maste #endif
13619261079SEd Maste }
137535af610SEd Maste if (lib_contains_symbol(path, "sk_api_version") != 0) {
138535af610SEd Maste error("provider %s is not an OpenSSH FIDO library", path);
13919261079SEd Maste goto fail;
14019261079SEd Maste }
141535af610SEd Maste if ((ret->dlhandle = dlopen(path, RTLD_NOW)) == NULL)
142535af610SEd Maste fatal("Provider \"%s\" dlopen failed: %s", path, dlerror());
14319261079SEd Maste if ((ret->sk_api_version = dlsym(ret->dlhandle,
14419261079SEd Maste "sk_api_version")) == NULL) {
14519261079SEd Maste error("Provider \"%s\" dlsym(sk_api_version) failed: %s",
14619261079SEd Maste path, dlerror());
14719261079SEd Maste goto fail;
14819261079SEd Maste }
14919261079SEd Maste version = ret->sk_api_version();
15019261079SEd Maste debug_f("provider %s implements version 0x%08lx", ret->path,
15119261079SEd Maste (u_long)version);
15219261079SEd Maste if ((version & SSH_SK_VERSION_MAJOR_MASK) != SSH_SK_VERSION_MAJOR) {
15319261079SEd Maste error("Provider \"%s\" implements unsupported "
15419261079SEd Maste "version 0x%08lx (supported: 0x%08lx)",
15519261079SEd Maste path, (u_long)version, (u_long)SSH_SK_VERSION_MAJOR);
15619261079SEd Maste goto fail;
15719261079SEd Maste }
15819261079SEd Maste if ((ret->sk_enroll = dlsym(ret->dlhandle, "sk_enroll")) == NULL) {
15919261079SEd Maste error("Provider %s dlsym(sk_enroll) failed: %s",
16019261079SEd Maste path, dlerror());
16119261079SEd Maste goto fail;
16219261079SEd Maste }
16319261079SEd Maste if ((ret->sk_sign = dlsym(ret->dlhandle, "sk_sign")) == NULL) {
16419261079SEd Maste error("Provider \"%s\" dlsym(sk_sign) failed: %s",
16519261079SEd Maste path, dlerror());
16619261079SEd Maste goto fail;
16719261079SEd Maste }
16819261079SEd Maste if ((ret->sk_load_resident_keys = dlsym(ret->dlhandle,
16919261079SEd Maste "sk_load_resident_keys")) == NULL) {
17019261079SEd Maste error("Provider \"%s\" dlsym(sk_load_resident_keys) "
17119261079SEd Maste "failed: %s", path, dlerror());
17219261079SEd Maste goto fail;
17319261079SEd Maste }
17419261079SEd Maste /* success */
17519261079SEd Maste return ret;
17619261079SEd Maste fail:
17719261079SEd Maste sshsk_free(ret);
17819261079SEd Maste return NULL;
17919261079SEd Maste }
18019261079SEd Maste
18119261079SEd Maste static void
sshsk_free_enroll_response(struct sk_enroll_response * r)18219261079SEd Maste sshsk_free_enroll_response(struct sk_enroll_response *r)
18319261079SEd Maste {
18419261079SEd Maste if (r == NULL)
18519261079SEd Maste return;
18619261079SEd Maste freezero(r->key_handle, r->key_handle_len);
18719261079SEd Maste freezero(r->public_key, r->public_key_len);
18819261079SEd Maste freezero(r->signature, r->signature_len);
18919261079SEd Maste freezero(r->attestation_cert, r->attestation_cert_len);
19019261079SEd Maste freezero(r->authdata, r->authdata_len);
19119261079SEd Maste freezero(r, sizeof(*r));
19219261079SEd Maste }
19319261079SEd Maste
19419261079SEd Maste static void
sshsk_free_sign_response(struct sk_sign_response * r)19519261079SEd Maste sshsk_free_sign_response(struct sk_sign_response *r)
19619261079SEd Maste {
19719261079SEd Maste if (r == NULL)
19819261079SEd Maste return;
19919261079SEd Maste freezero(r->sig_r, r->sig_r_len);
20019261079SEd Maste freezero(r->sig_s, r->sig_s_len);
20119261079SEd Maste freezero(r, sizeof(*r));
20219261079SEd Maste }
20319261079SEd Maste
20419261079SEd Maste #ifdef WITH_OPENSSL
20519261079SEd Maste /* Assemble key from response */
20619261079SEd Maste static int
sshsk_ecdsa_assemble(struct sk_enroll_response * resp,struct sshkey ** keyp)20719261079SEd Maste sshsk_ecdsa_assemble(struct sk_enroll_response *resp, struct sshkey **keyp)
20819261079SEd Maste {
20919261079SEd Maste struct sshkey *key = NULL;
21019261079SEd Maste struct sshbuf *b = NULL;
211*3d9fd9fcSEd Maste EC_KEY *ecdsa = NULL;
21219261079SEd Maste EC_POINT *q = NULL;
213*3d9fd9fcSEd Maste const EC_GROUP *g = NULL;
21419261079SEd Maste int r;
21519261079SEd Maste
21619261079SEd Maste *keyp = NULL;
21719261079SEd Maste if ((key = sshkey_new(KEY_ECDSA_SK)) == NULL) {
21819261079SEd Maste error_f("sshkey_new failed");
21919261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
22019261079SEd Maste goto out;
22119261079SEd Maste }
22219261079SEd Maste key->ecdsa_nid = NID_X9_62_prime256v1;
223*3d9fd9fcSEd Maste if ((ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) == NULL ||
224*3d9fd9fcSEd Maste (g = EC_KEY_get0_group(ecdsa)) == NULL ||
225*3d9fd9fcSEd Maste (q = EC_POINT_new(g)) == NULL ||
22619261079SEd Maste (b = sshbuf_new()) == NULL) {
22719261079SEd Maste error_f("allocation failed");
22819261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
22919261079SEd Maste goto out;
23019261079SEd Maste }
23119261079SEd Maste if ((r = sshbuf_put_string(b,
23219261079SEd Maste resp->public_key, resp->public_key_len)) != 0) {
23319261079SEd Maste error_fr(r, "sshbuf_put_string");
23419261079SEd Maste goto out;
23519261079SEd Maste }
236*3d9fd9fcSEd Maste if ((r = sshbuf_get_ec(b, q, g)) != 0) {
23719261079SEd Maste error_fr(r, "parse");
23819261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
23919261079SEd Maste goto out;
24019261079SEd Maste }
241*3d9fd9fcSEd Maste if (sshkey_ec_validate_public(g, q) != 0) {
24219261079SEd Maste error("Authenticator returned invalid ECDSA key");
24319261079SEd Maste r = SSH_ERR_KEY_INVALID_EC_VALUE;
24419261079SEd Maste goto out;
24519261079SEd Maste }
246*3d9fd9fcSEd Maste if (EC_KEY_set_public_key(ecdsa, q) != 1) {
24719261079SEd Maste /* XXX assume it is a allocation error */
24819261079SEd Maste error_f("allocation failed");
24919261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
25019261079SEd Maste goto out;
25119261079SEd Maste }
252*3d9fd9fcSEd Maste if ((key->pkey = EVP_PKEY_new()) == NULL) {
253*3d9fd9fcSEd Maste error_f("allocation failed");
254*3d9fd9fcSEd Maste r = SSH_ERR_ALLOC_FAIL;
255*3d9fd9fcSEd Maste goto out;
256*3d9fd9fcSEd Maste }
257*3d9fd9fcSEd Maste if (EVP_PKEY_set1_EC_KEY(key->pkey, ecdsa) != 1) {
258*3d9fd9fcSEd Maste error_f("Assigning EC_KEY failed");
259*3d9fd9fcSEd Maste r = SSH_ERR_LIBCRYPTO_ERROR;
260*3d9fd9fcSEd Maste goto out;
261*3d9fd9fcSEd Maste }
26219261079SEd Maste /* success */
26319261079SEd Maste *keyp = key;
26419261079SEd Maste key = NULL; /* transferred */
26519261079SEd Maste r = 0;
26619261079SEd Maste out:
26719261079SEd Maste sshkey_free(key);
26819261079SEd Maste sshbuf_free(b);
269*3d9fd9fcSEd Maste EC_KEY_free(ecdsa);
270*3d9fd9fcSEd Maste EC_POINT_free(q);
27119261079SEd Maste return r;
27219261079SEd Maste }
27319261079SEd Maste #endif /* WITH_OPENSSL */
27419261079SEd Maste
27519261079SEd Maste static int
sshsk_ed25519_assemble(struct sk_enroll_response * resp,struct sshkey ** keyp)27619261079SEd Maste sshsk_ed25519_assemble(struct sk_enroll_response *resp, struct sshkey **keyp)
27719261079SEd Maste {
27819261079SEd Maste struct sshkey *key = NULL;
27919261079SEd Maste int r;
28019261079SEd Maste
28119261079SEd Maste *keyp = NULL;
28219261079SEd Maste if (resp->public_key_len != ED25519_PK_SZ) {
28319261079SEd Maste error_f("invalid size: %zu", resp->public_key_len);
28419261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
28519261079SEd Maste goto out;
28619261079SEd Maste }
28719261079SEd Maste if ((key = sshkey_new(KEY_ED25519_SK)) == NULL) {
28819261079SEd Maste error_f("sshkey_new failed");
28919261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
29019261079SEd Maste goto out;
29119261079SEd Maste }
29219261079SEd Maste if ((key->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
29319261079SEd Maste error_f("malloc failed");
29419261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
29519261079SEd Maste goto out;
29619261079SEd Maste }
29719261079SEd Maste memcpy(key->ed25519_pk, resp->public_key, ED25519_PK_SZ);
29819261079SEd Maste /* success */
29919261079SEd Maste *keyp = key;
30019261079SEd Maste key = NULL; /* transferred */
30119261079SEd Maste r = 0;
30219261079SEd Maste out:
30319261079SEd Maste sshkey_free(key);
30419261079SEd Maste return r;
30519261079SEd Maste }
30619261079SEd Maste
30719261079SEd Maste static int
sshsk_key_from_response(int alg,const char * application,uint8_t flags,struct sk_enroll_response * resp,struct sshkey ** keyp)30819261079SEd Maste sshsk_key_from_response(int alg, const char *application, uint8_t flags,
30919261079SEd Maste struct sk_enroll_response *resp, struct sshkey **keyp)
31019261079SEd Maste {
31119261079SEd Maste struct sshkey *key = NULL;
31219261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
31319261079SEd Maste
31419261079SEd Maste *keyp = NULL;
31519261079SEd Maste
31619261079SEd Maste /* Check response validity */
31719261079SEd Maste if (resp->public_key == NULL || resp->key_handle == NULL) {
31819261079SEd Maste error_f("sk_enroll response invalid");
31919261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
32019261079SEd Maste goto out;
32119261079SEd Maste }
32219261079SEd Maste switch (alg) {
32319261079SEd Maste #ifdef WITH_OPENSSL
32419261079SEd Maste case SSH_SK_ECDSA:
32519261079SEd Maste if ((r = sshsk_ecdsa_assemble(resp, &key)) != 0)
32619261079SEd Maste goto out;
32719261079SEd Maste break;
32819261079SEd Maste #endif /* WITH_OPENSSL */
32919261079SEd Maste case SSH_SK_ED25519:
33019261079SEd Maste if ((r = sshsk_ed25519_assemble(resp, &key)) != 0)
33119261079SEd Maste goto out;
33219261079SEd Maste break;
33319261079SEd Maste default:
33419261079SEd Maste error_f("unsupported algorithm %d", alg);
33519261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
33619261079SEd Maste goto out;
33719261079SEd Maste }
33819261079SEd Maste key->sk_flags = flags;
33919261079SEd Maste if ((key->sk_key_handle = sshbuf_new()) == NULL ||
34019261079SEd Maste (key->sk_reserved = sshbuf_new()) == NULL) {
34119261079SEd Maste error_f("allocation failed");
34219261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
34319261079SEd Maste goto out;
34419261079SEd Maste }
34519261079SEd Maste if ((key->sk_application = strdup(application)) == NULL) {
34619261079SEd Maste error_f("strdup application failed");
34719261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
34819261079SEd Maste goto out;
34919261079SEd Maste }
35019261079SEd Maste if ((r = sshbuf_put(key->sk_key_handle, resp->key_handle,
35119261079SEd Maste resp->key_handle_len)) != 0) {
35219261079SEd Maste error_fr(r, "put key handle");
35319261079SEd Maste goto out;
35419261079SEd Maste }
35519261079SEd Maste /* success */
35619261079SEd Maste r = 0;
35719261079SEd Maste *keyp = key;
35819261079SEd Maste key = NULL;
35919261079SEd Maste out:
36019261079SEd Maste sshkey_free(key);
36119261079SEd Maste return r;
36219261079SEd Maste }
36319261079SEd Maste
36419261079SEd Maste static int
skerr_to_ssherr(int skerr)36519261079SEd Maste skerr_to_ssherr(int skerr)
36619261079SEd Maste {
36719261079SEd Maste switch (skerr) {
36819261079SEd Maste case SSH_SK_ERR_UNSUPPORTED:
36919261079SEd Maste return SSH_ERR_FEATURE_UNSUPPORTED;
37019261079SEd Maste case SSH_SK_ERR_PIN_REQUIRED:
37119261079SEd Maste return SSH_ERR_KEY_WRONG_PASSPHRASE;
37219261079SEd Maste case SSH_SK_ERR_DEVICE_NOT_FOUND:
37319261079SEd Maste return SSH_ERR_DEVICE_NOT_FOUND;
37438a52bd3SEd Maste case SSH_SK_ERR_CREDENTIAL_EXISTS:
37538a52bd3SEd Maste return SSH_ERR_KEY_BAD_PERMISSIONS;
37619261079SEd Maste case SSH_SK_ERR_GENERAL:
37719261079SEd Maste default:
37819261079SEd Maste return SSH_ERR_INVALID_FORMAT;
37919261079SEd Maste }
38019261079SEd Maste }
38119261079SEd Maste
38219261079SEd Maste static void
sshsk_free_options(struct sk_option ** opts)38319261079SEd Maste sshsk_free_options(struct sk_option **opts)
38419261079SEd Maste {
38519261079SEd Maste size_t i;
38619261079SEd Maste
38719261079SEd Maste if (opts == NULL)
38819261079SEd Maste return;
38919261079SEd Maste for (i = 0; opts[i] != NULL; i++) {
39019261079SEd Maste free(opts[i]->name);
39119261079SEd Maste free(opts[i]->value);
39219261079SEd Maste free(opts[i]);
39319261079SEd Maste }
39419261079SEd Maste free(opts);
39519261079SEd Maste }
39619261079SEd Maste
39719261079SEd Maste static int
sshsk_add_option(struct sk_option *** optsp,size_t * noptsp,const char * name,const char * value,uint8_t required)39819261079SEd Maste sshsk_add_option(struct sk_option ***optsp, size_t *noptsp,
39919261079SEd Maste const char *name, const char *value, uint8_t required)
40019261079SEd Maste {
40119261079SEd Maste struct sk_option **opts = *optsp;
40219261079SEd Maste size_t nopts = *noptsp;
40319261079SEd Maste
40419261079SEd Maste if ((opts = recallocarray(opts, nopts, nopts + 2, /* extra for NULL */
40519261079SEd Maste sizeof(*opts))) == NULL) {
40619261079SEd Maste error_f("array alloc failed");
40719261079SEd Maste return SSH_ERR_ALLOC_FAIL;
40819261079SEd Maste }
40919261079SEd Maste *optsp = opts;
41019261079SEd Maste *noptsp = nopts + 1;
41119261079SEd Maste if ((opts[nopts] = calloc(1, sizeof(**opts))) == NULL) {
41219261079SEd Maste error_f("alloc failed");
41319261079SEd Maste return SSH_ERR_ALLOC_FAIL;
41419261079SEd Maste }
41519261079SEd Maste if ((opts[nopts]->name = strdup(name)) == NULL ||
41619261079SEd Maste (opts[nopts]->value = strdup(value)) == NULL) {
41719261079SEd Maste error_f("alloc failed");
41819261079SEd Maste return SSH_ERR_ALLOC_FAIL;
41919261079SEd Maste }
42019261079SEd Maste opts[nopts]->required = required;
42119261079SEd Maste return 0;
42219261079SEd Maste }
42319261079SEd Maste
42419261079SEd Maste static int
make_options(const char * device,const char * user_id,struct sk_option *** optsp)42519261079SEd Maste make_options(const char *device, const char *user_id,
42619261079SEd Maste struct sk_option ***optsp)
42719261079SEd Maste {
42819261079SEd Maste struct sk_option **opts = NULL;
42919261079SEd Maste size_t nopts = 0;
43019261079SEd Maste int r, ret = SSH_ERR_INTERNAL_ERROR;
43119261079SEd Maste
43219261079SEd Maste if (device != NULL &&
43319261079SEd Maste (r = sshsk_add_option(&opts, &nopts, "device", device, 0)) != 0) {
43419261079SEd Maste ret = r;
43519261079SEd Maste goto out;
43619261079SEd Maste }
43719261079SEd Maste if (user_id != NULL &&
43819261079SEd Maste (r = sshsk_add_option(&opts, &nopts, "user", user_id, 0)) != 0) {
43919261079SEd Maste ret = r;
44019261079SEd Maste goto out;
44119261079SEd Maste }
44219261079SEd Maste /* success */
44319261079SEd Maste *optsp = opts;
44419261079SEd Maste opts = NULL;
44519261079SEd Maste nopts = 0;
44619261079SEd Maste ret = 0;
44719261079SEd Maste out:
44819261079SEd Maste sshsk_free_options(opts);
44919261079SEd Maste return ret;
45019261079SEd Maste }
45119261079SEd Maste
45219261079SEd Maste
45319261079SEd Maste static int
fill_attestation_blob(const struct sk_enroll_response * resp,struct sshbuf * attest)45419261079SEd Maste fill_attestation_blob(const struct sk_enroll_response *resp,
45519261079SEd Maste struct sshbuf *attest)
45619261079SEd Maste {
45719261079SEd Maste int r;
45819261079SEd Maste
45919261079SEd Maste if (attest == NULL)
46019261079SEd Maste return 0; /* nothing to do */
46119261079SEd Maste if ((r = sshbuf_put_cstring(attest, "ssh-sk-attest-v01")) != 0 ||
46219261079SEd Maste (r = sshbuf_put_string(attest,
46319261079SEd Maste resp->attestation_cert, resp->attestation_cert_len)) != 0 ||
46419261079SEd Maste (r = sshbuf_put_string(attest,
46519261079SEd Maste resp->signature, resp->signature_len)) != 0 ||
46619261079SEd Maste (r = sshbuf_put_string(attest,
46719261079SEd Maste resp->authdata, resp->authdata_len)) != 0 ||
46819261079SEd Maste (r = sshbuf_put_u32(attest, 0)) != 0 || /* resvd flags */
46919261079SEd Maste (r = sshbuf_put_string(attest, NULL, 0)) != 0 /* resvd */) {
47019261079SEd Maste error_fr(r, "compose");
47119261079SEd Maste return r;
47219261079SEd Maste }
47319261079SEd Maste /* success */
47419261079SEd Maste return 0;
47519261079SEd Maste }
47619261079SEd Maste
47719261079SEd 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)47819261079SEd Maste sshsk_enroll(int type, const char *provider_path, const char *device,
47919261079SEd Maste const char *application, const char *userid, uint8_t flags,
48019261079SEd Maste const char *pin, struct sshbuf *challenge_buf,
48119261079SEd Maste struct sshkey **keyp, struct sshbuf *attest)
48219261079SEd Maste {
48319261079SEd Maste struct sshsk_provider *skp = NULL;
48419261079SEd Maste struct sshkey *key = NULL;
48519261079SEd Maste u_char randchall[32];
48619261079SEd Maste const u_char *challenge;
48719261079SEd Maste size_t challenge_len;
48819261079SEd Maste struct sk_enroll_response *resp = NULL;
48919261079SEd Maste struct sk_option **opts = NULL;
49019261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
49119261079SEd Maste int alg;
49219261079SEd Maste
49319261079SEd Maste debug_f("provider \"%s\", device \"%s\", application \"%s\", "
49419261079SEd Maste "userid \"%s\", flags 0x%02x, challenge len %zu%s",
49519261079SEd Maste provider_path, device, application, userid, flags,
49619261079SEd Maste challenge_buf == NULL ? 0 : sshbuf_len(challenge_buf),
49719261079SEd Maste (pin != NULL && *pin != '\0') ? " with-pin" : "");
49819261079SEd Maste
49919261079SEd Maste *keyp = NULL;
50019261079SEd Maste if (attest)
50119261079SEd Maste sshbuf_reset(attest);
50219261079SEd Maste
50319261079SEd Maste if ((r = make_options(device, userid, &opts)) != 0)
50419261079SEd Maste goto out;
50519261079SEd Maste
50619261079SEd Maste switch (type) {
50719261079SEd Maste #ifdef WITH_OPENSSL
50819261079SEd Maste case KEY_ECDSA_SK:
50919261079SEd Maste alg = SSH_SK_ECDSA;
51019261079SEd Maste break;
51119261079SEd Maste #endif /* WITH_OPENSSL */
51219261079SEd Maste case KEY_ED25519_SK:
51319261079SEd Maste alg = SSH_SK_ED25519;
51419261079SEd Maste break;
51519261079SEd Maste default:
51619261079SEd Maste error_f("unsupported key type");
51719261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
51819261079SEd Maste goto out;
51919261079SEd Maste }
52019261079SEd Maste if (provider_path == NULL) {
52119261079SEd Maste error_f("missing provider");
52219261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
52319261079SEd Maste goto out;
52419261079SEd Maste }
52519261079SEd Maste if (application == NULL || *application == '\0') {
52619261079SEd Maste error_f("missing application");
52719261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
52819261079SEd Maste goto out;
52919261079SEd Maste }
53019261079SEd Maste if (challenge_buf == NULL) {
53119261079SEd Maste debug_f("using random challenge");
53219261079SEd Maste arc4random_buf(randchall, sizeof(randchall));
53319261079SEd Maste challenge = randchall;
53419261079SEd Maste challenge_len = sizeof(randchall);
53519261079SEd Maste } else if (sshbuf_len(challenge_buf) == 0) {
53619261079SEd Maste error("Missing enrollment challenge");
53719261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
53819261079SEd Maste goto out;
53919261079SEd Maste } else {
54019261079SEd Maste challenge = sshbuf_ptr(challenge_buf);
54119261079SEd Maste challenge_len = sshbuf_len(challenge_buf);
54219261079SEd Maste debug3_f("using explicit challenge len=%zd", challenge_len);
54319261079SEd Maste }
54419261079SEd Maste if ((skp = sshsk_open(provider_path)) == NULL) {
54519261079SEd Maste r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
54619261079SEd Maste goto out;
54719261079SEd Maste }
54819261079SEd Maste /* XXX validate flags? */
54919261079SEd Maste /* enroll key */
55019261079SEd Maste if ((r = skp->sk_enroll(alg, challenge, challenge_len, application,
55119261079SEd Maste flags, pin, opts, &resp)) != 0) {
55219261079SEd Maste debug_f("provider \"%s\" failure %d", provider_path, r);
55319261079SEd Maste r = skerr_to_ssherr(r);
55419261079SEd Maste goto out;
55519261079SEd Maste }
55619261079SEd Maste
5571323ec57SEd Maste if ((r = sshsk_key_from_response(alg, application, resp->flags,
55819261079SEd Maste resp, &key)) != 0)
55919261079SEd Maste goto out;
56019261079SEd Maste
56119261079SEd Maste /* Optionally fill in the attestation information */
56219261079SEd Maste if ((r = fill_attestation_blob(resp, attest)) != 0)
56319261079SEd Maste goto out;
56419261079SEd Maste
56519261079SEd Maste /* success */
56619261079SEd Maste *keyp = key;
56719261079SEd Maste key = NULL; /* transferred */
56819261079SEd Maste r = 0;
56919261079SEd Maste out:
57019261079SEd Maste sshsk_free_options(opts);
57119261079SEd Maste sshsk_free(skp);
57219261079SEd Maste sshkey_free(key);
57319261079SEd Maste sshsk_free_enroll_response(resp);
57419261079SEd Maste explicit_bzero(randchall, sizeof(randchall));
57519261079SEd Maste return r;
57619261079SEd Maste }
57719261079SEd Maste
57819261079SEd Maste #ifdef WITH_OPENSSL
57919261079SEd Maste static int
sshsk_ecdsa_sig(struct sk_sign_response * resp,struct sshbuf * sig)58019261079SEd Maste sshsk_ecdsa_sig(struct sk_sign_response *resp, struct sshbuf *sig)
58119261079SEd Maste {
58219261079SEd Maste struct sshbuf *inner_sig = NULL;
58319261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
58419261079SEd Maste
58519261079SEd Maste /* Check response validity */
58619261079SEd Maste if (resp->sig_r == NULL || resp->sig_s == NULL) {
58719261079SEd Maste error_f("sk_sign response invalid");
58819261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
58919261079SEd Maste goto out;
59019261079SEd Maste }
59119261079SEd Maste if ((inner_sig = sshbuf_new()) == NULL) {
59219261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
59319261079SEd Maste goto out;
59419261079SEd Maste }
59519261079SEd Maste /* Prepare and append inner signature object */
59619261079SEd Maste if ((r = sshbuf_put_bignum2_bytes(inner_sig,
59719261079SEd Maste resp->sig_r, resp->sig_r_len)) != 0 ||
59819261079SEd Maste (r = sshbuf_put_bignum2_bytes(inner_sig,
59919261079SEd Maste resp->sig_s, resp->sig_s_len)) != 0) {
60019261079SEd Maste error_fr(r, "compose inner");
60119261079SEd Maste goto out;
60219261079SEd Maste }
60319261079SEd Maste if ((r = sshbuf_put_stringb(sig, inner_sig)) != 0 ||
60419261079SEd Maste (r = sshbuf_put_u8(sig, resp->flags)) != 0 ||
60519261079SEd Maste (r = sshbuf_put_u32(sig, resp->counter)) != 0) {
60619261079SEd Maste error_fr(r, "compose");
60719261079SEd Maste goto out;
60819261079SEd Maste }
60919261079SEd Maste #ifdef DEBUG_SK
61019261079SEd Maste fprintf(stderr, "%s: sig_r:\n", __func__);
61119261079SEd Maste sshbuf_dump_data(resp->sig_r, resp->sig_r_len, stderr);
61219261079SEd Maste fprintf(stderr, "%s: sig_s:\n", __func__);
61319261079SEd Maste sshbuf_dump_data(resp->sig_s, resp->sig_s_len, stderr);
61419261079SEd Maste fprintf(stderr, "%s: inner:\n", __func__);
61519261079SEd Maste sshbuf_dump(inner_sig, stderr);
61619261079SEd Maste #endif
61719261079SEd Maste r = 0;
61819261079SEd Maste out:
61919261079SEd Maste sshbuf_free(inner_sig);
62019261079SEd Maste return r;
62119261079SEd Maste }
62219261079SEd Maste #endif /* WITH_OPENSSL */
62319261079SEd Maste
62419261079SEd Maste static int
sshsk_ed25519_sig(struct sk_sign_response * resp,struct sshbuf * sig)62519261079SEd Maste sshsk_ed25519_sig(struct sk_sign_response *resp, struct sshbuf *sig)
62619261079SEd Maste {
62719261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
62819261079SEd Maste
62919261079SEd Maste /* Check response validity */
63019261079SEd Maste if (resp->sig_r == NULL) {
63119261079SEd Maste error_f("sk_sign response invalid");
63219261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
63319261079SEd Maste goto out;
63419261079SEd Maste }
63519261079SEd Maste if ((r = sshbuf_put_string(sig,
63619261079SEd Maste resp->sig_r, resp->sig_r_len)) != 0 ||
63719261079SEd Maste (r = sshbuf_put_u8(sig, resp->flags)) != 0 ||
63819261079SEd Maste (r = sshbuf_put_u32(sig, resp->counter)) != 0) {
63919261079SEd Maste error_fr(r, "compose");
64019261079SEd Maste goto out;
64119261079SEd Maste }
64219261079SEd Maste #ifdef DEBUG_SK
64319261079SEd Maste fprintf(stderr, "%s: sig_r:\n", __func__);
64419261079SEd Maste sshbuf_dump_data(resp->sig_r, resp->sig_r_len, stderr);
64519261079SEd Maste #endif
64619261079SEd Maste r = 0;
64719261079SEd Maste out:
64819261079SEd Maste return r;
64919261079SEd Maste }
65019261079SEd Maste
65119261079SEd 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)65219261079SEd Maste sshsk_sign(const char *provider_path, struct sshkey *key,
65319261079SEd Maste u_char **sigp, size_t *lenp, const u_char *data, size_t datalen,
65419261079SEd Maste u_int compat, const char *pin)
65519261079SEd Maste {
65619261079SEd Maste struct sshsk_provider *skp = NULL;
65719261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
65819261079SEd Maste int type, alg;
65919261079SEd Maste struct sk_sign_response *resp = NULL;
66019261079SEd Maste struct sshbuf *inner_sig = NULL, *sig = NULL;
66119261079SEd Maste struct sk_option **opts = NULL;
66219261079SEd Maste
66319261079SEd Maste debug_f("provider \"%s\", key %s, flags 0x%02x%s",
66419261079SEd Maste provider_path, sshkey_type(key), key->sk_flags,
66519261079SEd Maste (pin != NULL && *pin != '\0') ? " with-pin" : "");
66619261079SEd Maste
66719261079SEd Maste if (sigp != NULL)
66819261079SEd Maste *sigp = NULL;
66919261079SEd Maste if (lenp != NULL)
67019261079SEd Maste *lenp = 0;
67119261079SEd Maste type = sshkey_type_plain(key->type);
67219261079SEd Maste switch (type) {
67319261079SEd Maste #ifdef WITH_OPENSSL
67419261079SEd Maste case KEY_ECDSA_SK:
67519261079SEd Maste alg = SSH_SK_ECDSA;
67619261079SEd Maste break;
67719261079SEd Maste #endif /* WITH_OPENSSL */
67819261079SEd Maste case KEY_ED25519_SK:
67919261079SEd Maste alg = SSH_SK_ED25519;
68019261079SEd Maste break;
68119261079SEd Maste default:
68219261079SEd Maste return SSH_ERR_INVALID_ARGUMENT;
68319261079SEd Maste }
68419261079SEd Maste if (provider_path == NULL ||
68519261079SEd Maste key->sk_key_handle == NULL ||
68619261079SEd Maste key->sk_application == NULL || *key->sk_application == '\0') {
68719261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
68819261079SEd Maste goto out;
68919261079SEd Maste }
69019261079SEd Maste if ((skp = sshsk_open(provider_path)) == NULL) {
69119261079SEd Maste r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
69219261079SEd Maste goto out;
69319261079SEd Maste }
69419261079SEd Maste #ifdef DEBUG_SK
69519261079SEd Maste fprintf(stderr, "%s: sk_flags = 0x%02x, sk_application = \"%s\"\n",
69619261079SEd Maste __func__, key->sk_flags, key->sk_application);
69719261079SEd Maste fprintf(stderr, "%s: sk_key_handle:\n", __func__);
69819261079SEd Maste sshbuf_dump(key->sk_key_handle, stderr);
69919261079SEd Maste #endif
70019261079SEd Maste if ((r = skp->sk_sign(alg, data, datalen, key->sk_application,
70119261079SEd Maste sshbuf_ptr(key->sk_key_handle), sshbuf_len(key->sk_key_handle),
70219261079SEd Maste key->sk_flags, pin, opts, &resp)) != 0) {
70319261079SEd Maste debug_f("sk_sign failed with code %d", r);
70419261079SEd Maste r = skerr_to_ssherr(r);
70519261079SEd Maste goto out;
70619261079SEd Maste }
70719261079SEd Maste /* Assemble signature */
70819261079SEd Maste if ((sig = sshbuf_new()) == NULL) {
70919261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
71019261079SEd Maste goto out;
71119261079SEd Maste }
71219261079SEd Maste if ((r = sshbuf_put_cstring(sig, sshkey_ssh_name_plain(key))) != 0) {
71319261079SEd Maste error_fr(r, "compose outer");
71419261079SEd Maste goto out;
71519261079SEd Maste }
71619261079SEd Maste switch (type) {
71719261079SEd Maste #ifdef WITH_OPENSSL
71819261079SEd Maste case KEY_ECDSA_SK:
71919261079SEd Maste if ((r = sshsk_ecdsa_sig(resp, sig)) != 0)
72019261079SEd Maste goto out;
72119261079SEd Maste break;
72219261079SEd Maste #endif /* WITH_OPENSSL */
72319261079SEd Maste case KEY_ED25519_SK:
72419261079SEd Maste if ((r = sshsk_ed25519_sig(resp, sig)) != 0)
72519261079SEd Maste goto out;
72619261079SEd Maste break;
72719261079SEd Maste }
72819261079SEd Maste #ifdef DEBUG_SK
72919261079SEd Maste fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
73019261079SEd Maste __func__, resp->flags, resp->counter);
73119261079SEd Maste fprintf(stderr, "%s: data to sign:\n", __func__);
73219261079SEd Maste sshbuf_dump_data(data, datalen, stderr);
73319261079SEd Maste fprintf(stderr, "%s: sigbuf:\n", __func__);
73419261079SEd Maste sshbuf_dump(sig, stderr);
73519261079SEd Maste #endif
73619261079SEd Maste if (sigp != NULL) {
73719261079SEd Maste if ((*sigp = malloc(sshbuf_len(sig))) == NULL) {
73819261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
73919261079SEd Maste goto out;
74019261079SEd Maste }
74119261079SEd Maste memcpy(*sigp, sshbuf_ptr(sig), sshbuf_len(sig));
74219261079SEd Maste }
74319261079SEd Maste if (lenp != NULL)
74419261079SEd Maste *lenp = sshbuf_len(sig);
74519261079SEd Maste /* success */
74619261079SEd Maste r = 0;
74719261079SEd Maste out:
74819261079SEd Maste sshsk_free_options(opts);
74919261079SEd Maste sshsk_free(skp);
75019261079SEd Maste sshsk_free_sign_response(resp);
75119261079SEd Maste sshbuf_free(sig);
75219261079SEd Maste sshbuf_free(inner_sig);
75319261079SEd Maste return r;
75419261079SEd Maste }
75519261079SEd Maste
75619261079SEd Maste static void
sshsk_free_sk_resident_keys(struct sk_resident_key ** rks,size_t nrks)75719261079SEd Maste sshsk_free_sk_resident_keys(struct sk_resident_key **rks, size_t nrks)
75819261079SEd Maste {
75919261079SEd Maste size_t i;
76019261079SEd Maste
76119261079SEd Maste if (nrks == 0 || rks == NULL)
76219261079SEd Maste return;
76319261079SEd Maste for (i = 0; i < nrks; i++) {
76419261079SEd Maste free(rks[i]->application);
7651323ec57SEd Maste freezero(rks[i]->user_id, rks[i]->user_id_len);
76619261079SEd Maste freezero(rks[i]->key.key_handle, rks[i]->key.key_handle_len);
76719261079SEd Maste freezero(rks[i]->key.public_key, rks[i]->key.public_key_len);
76819261079SEd Maste freezero(rks[i]->key.signature, rks[i]->key.signature_len);
76919261079SEd Maste freezero(rks[i]->key.attestation_cert,
77019261079SEd Maste rks[i]->key.attestation_cert_len);
77119261079SEd Maste freezero(rks[i], sizeof(**rks));
77219261079SEd Maste }
77319261079SEd Maste free(rks);
77419261079SEd Maste }
77519261079SEd Maste
7761323ec57SEd Maste static void
sshsk_free_resident_key(struct sshsk_resident_key * srk)7771323ec57SEd Maste sshsk_free_resident_key(struct sshsk_resident_key *srk)
7781323ec57SEd Maste {
7791323ec57SEd Maste if (srk == NULL)
7801323ec57SEd Maste return;
7811323ec57SEd Maste sshkey_free(srk->key);
7821323ec57SEd Maste freezero(srk->user_id, srk->user_id_len);
7831323ec57SEd Maste free(srk);
7841323ec57SEd Maste }
7851323ec57SEd Maste
7861323ec57SEd Maste
7871323ec57SEd Maste void
sshsk_free_resident_keys(struct sshsk_resident_key ** srks,size_t nsrks)7881323ec57SEd Maste sshsk_free_resident_keys(struct sshsk_resident_key **srks, size_t nsrks)
7891323ec57SEd Maste {
7901323ec57SEd Maste size_t i;
7911323ec57SEd Maste
7921323ec57SEd Maste if (srks == NULL || nsrks == 0)
7931323ec57SEd Maste return;
7941323ec57SEd Maste
7951323ec57SEd Maste for (i = 0; i < nsrks; i++)
7961323ec57SEd Maste sshsk_free_resident_key(srks[i]);
7971323ec57SEd Maste free(srks);
7981323ec57SEd Maste }
7991323ec57SEd Maste
80019261079SEd 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)80119261079SEd Maste sshsk_load_resident(const char *provider_path, const char *device,
8021323ec57SEd Maste const char *pin, u_int flags, struct sshsk_resident_key ***srksp,
8031323ec57SEd Maste size_t *nsrksp)
80419261079SEd Maste {
80519261079SEd Maste struct sshsk_provider *skp = NULL;
80619261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
80719261079SEd Maste struct sk_resident_key **rks = NULL;
8081323ec57SEd Maste size_t i, nrks = 0, nsrks = 0;
8091323ec57SEd Maste struct sshkey *key = NULL;
8101323ec57SEd Maste struct sshsk_resident_key *srk = NULL, **srks = NULL, **tmp;
8111323ec57SEd Maste uint8_t sk_flags;
81219261079SEd Maste struct sk_option **opts = NULL;
81319261079SEd Maste
81419261079SEd Maste debug_f("provider \"%s\"%s", provider_path,
81519261079SEd Maste (pin != NULL && *pin != '\0') ? ", have-pin": "");
81619261079SEd Maste
8171323ec57SEd Maste if (srksp == NULL || nsrksp == NULL)
81819261079SEd Maste return SSH_ERR_INVALID_ARGUMENT;
8191323ec57SEd Maste *srksp = NULL;
8201323ec57SEd Maste *nsrksp = 0;
82119261079SEd Maste
82219261079SEd Maste if ((r = make_options(device, NULL, &opts)) != 0)
82319261079SEd Maste goto out;
82419261079SEd Maste if ((skp = sshsk_open(provider_path)) == NULL) {
82519261079SEd Maste r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
82619261079SEd Maste goto out;
82719261079SEd Maste }
82819261079SEd Maste if ((r = skp->sk_load_resident_keys(pin, opts, &rks, &nrks)) != 0) {
82919261079SEd Maste error("Provider \"%s\" returned failure %d", provider_path, r);
83019261079SEd Maste r = skerr_to_ssherr(r);
83119261079SEd Maste goto out;
83219261079SEd Maste }
83319261079SEd Maste for (i = 0; i < nrks; i++) {
8341323ec57SEd Maste debug3_f("rk %zu: slot %zu, alg %d, app \"%s\", uidlen %zu",
8351323ec57SEd Maste i, rks[i]->slot, rks[i]->alg, rks[i]->application,
8361323ec57SEd Maste rks[i]->user_id_len);
83719261079SEd Maste /* XXX need better filter here */
83819261079SEd Maste if (strncmp(rks[i]->application, "ssh:", 4) != 0)
83919261079SEd Maste continue;
84019261079SEd Maste switch (rks[i]->alg) {
84119261079SEd Maste case SSH_SK_ECDSA:
84219261079SEd Maste case SSH_SK_ED25519:
84319261079SEd Maste break;
84419261079SEd Maste default:
84519261079SEd Maste continue;
84619261079SEd Maste }
8471323ec57SEd Maste sk_flags = SSH_SK_USER_PRESENCE_REQD|SSH_SK_RESIDENT_KEY;
84819261079SEd Maste if ((rks[i]->flags & SSH_SK_USER_VERIFICATION_REQD))
8491323ec57SEd Maste sk_flags |= SSH_SK_USER_VERIFICATION_REQD;
85019261079SEd Maste if ((r = sshsk_key_from_response(rks[i]->alg,
8511323ec57SEd Maste rks[i]->application, sk_flags, &rks[i]->key, &key)) != 0)
85219261079SEd Maste goto out;
8531323ec57SEd Maste if ((srk = calloc(1, sizeof(*srk))) == NULL) {
8541323ec57SEd Maste error_f("calloc failed");
8551323ec57SEd Maste r = SSH_ERR_ALLOC_FAIL;
8561323ec57SEd Maste goto out;
8571323ec57SEd Maste }
8581323ec57SEd Maste srk->key = key;
8591323ec57SEd Maste key = NULL; /* transferred */
8601323ec57SEd Maste if ((srk->user_id = calloc(1, rks[i]->user_id_len)) == NULL) {
8611323ec57SEd Maste error_f("calloc failed");
8621323ec57SEd Maste r = SSH_ERR_ALLOC_FAIL;
8631323ec57SEd Maste goto out;
8641323ec57SEd Maste }
8651323ec57SEd Maste memcpy(srk->user_id, rks[i]->user_id, rks[i]->user_id_len);
8661323ec57SEd Maste srk->user_id_len = rks[i]->user_id_len;
8671323ec57SEd Maste if ((tmp = recallocarray(srks, nsrks, nsrks + 1,
86819261079SEd Maste sizeof(*tmp))) == NULL) {
86919261079SEd Maste error_f("recallocarray failed");
87019261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
87119261079SEd Maste goto out;
87219261079SEd Maste }
8731323ec57SEd Maste srks = tmp;
8741323ec57SEd Maste srks[nsrks++] = srk;
8751323ec57SEd Maste srk = NULL;
87619261079SEd Maste /* XXX synthesise comment */
87719261079SEd Maste }
87819261079SEd Maste /* success */
8791323ec57SEd Maste *srksp = srks;
8801323ec57SEd Maste *nsrksp = nsrks;
8811323ec57SEd Maste srks = NULL;
8821323ec57SEd Maste nsrks = 0;
88319261079SEd Maste r = 0;
88419261079SEd Maste out:
88519261079SEd Maste sshsk_free_options(opts);
88619261079SEd Maste sshsk_free(skp);
88719261079SEd Maste sshsk_free_sk_resident_keys(rks, nrks);
88819261079SEd Maste sshkey_free(key);
8891323ec57SEd Maste sshsk_free_resident_key(srk);
8901323ec57SEd Maste sshsk_free_resident_keys(srks, nsrks);
89119261079SEd Maste return r;
89219261079SEd Maste }
89319261079SEd Maste
89419261079SEd Maste #endif /* ENABLE_SK */
895