2023-07-19 16:02:27 +02:00
|
|
|
/* $OpenBSD: ssh-sk.c,v 1.40 2023/07/19 14:02:27 djm Exp $ */
|
2019-10-31 22:16:20 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Google LLC
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG_SK 1 */
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2019-11-01 03:34:49 +01:00
|
|
|
#ifdef ENABLE_SK
|
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <stddef.h>
|
2020-01-14 04:41:47 +01:00
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
# include <stdint.h>
|
|
|
|
#endif
|
2019-10-31 22:16:20 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-10-01 08:35:05 +02:00
|
|
|
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
|
2019-10-31 22:16:20 +01:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/ec.h>
|
2021-10-01 08:35:05 +02:00
|
|
|
#endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
|
2019-10-31 22:16:20 +01:00
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "sshkey.h"
|
|
|
|
#include "ssherr.h"
|
|
|
|
#include "digest.h"
|
|
|
|
|
|
|
|
#include "ssh-sk.h"
|
|
|
|
#include "sk-api.h"
|
2019-11-12 20:31:45 +01:00
|
|
|
#include "crypto_api.h"
|
2019-10-31 22:16:20 +01:00
|
|
|
|
2021-10-01 08:35:05 +02:00
|
|
|
/*
|
|
|
|
* Almost every use of OpenSSL in this file is for ECDSA-NISTP256.
|
|
|
|
* This is strictly a larger hammer than necessary, but it reduces changes
|
|
|
|
* with upstream.
|
|
|
|
*/
|
|
|
|
#ifndef OPENSSL_HAS_ECC
|
|
|
|
# undef WITH_OPENSSL
|
|
|
|
#endif
|
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
struct sshsk_provider {
|
|
|
|
char *path;
|
|
|
|
void *dlhandle;
|
|
|
|
|
|
|
|
/* Return the version of the middleware API */
|
|
|
|
uint32_t (*sk_api_version)(void);
|
|
|
|
|
|
|
|
/* Enroll a U2F key (private key generation) */
|
2019-11-12 20:32:30 +01:00
|
|
|
int (*sk_enroll)(int alg, const uint8_t *challenge,
|
|
|
|
size_t challenge_len, const char *application, uint8_t flags,
|
2020-01-06 03:00:46 +01:00
|
|
|
const char *pin, struct sk_option **opts,
|
|
|
|
struct sk_enroll_response **enroll_response);
|
2019-10-31 22:16:20 +01:00
|
|
|
|
|
|
|
/* Sign a challenge */
|
2019-11-12 20:32:30 +01:00
|
|
|
int (*sk_sign)(int alg, const uint8_t *message, size_t message_len,
|
2019-10-31 22:16:20 +01:00
|
|
|
const char *application,
|
|
|
|
const uint8_t *key_handle, size_t key_handle_len,
|
2020-01-06 03:00:46 +01:00
|
|
|
uint8_t flags, const char *pin, struct sk_option **opts,
|
2019-12-30 10:23:28 +01:00
|
|
|
struct sk_sign_response **sign_response);
|
2019-12-30 10:21:16 +01:00
|
|
|
|
|
|
|
/* Enumerate resident keys */
|
2020-01-06 03:00:46 +01:00
|
|
|
int (*sk_load_resident_keys)(const char *pin, struct sk_option **opts,
|
2019-12-30 10:21:16 +01:00
|
|
|
struct sk_resident_key ***rks, size_t *nrks);
|
2019-10-31 22:16:20 +01:00
|
|
|
};
|
|
|
|
|
2019-11-14 22:27:29 +01:00
|
|
|
/* Built-in version */
|
|
|
|
int ssh_sk_enroll(int alg, const uint8_t *challenge,
|
|
|
|
size_t challenge_len, const char *application, uint8_t flags,
|
2020-01-06 03:00:46 +01:00
|
|
|
const char *pin, struct sk_option **opts,
|
|
|
|
struct sk_enroll_response **enroll_response);
|
2019-11-14 22:27:29 +01:00
|
|
|
int ssh_sk_sign(int alg, const uint8_t *message, size_t message_len,
|
|
|
|
const char *application,
|
|
|
|
const uint8_t *key_handle, size_t key_handle_len,
|
2020-01-06 03:00:46 +01:00
|
|
|
uint8_t flags, const char *pin, struct sk_option **opts,
|
|
|
|
struct sk_sign_response **sign_response);
|
|
|
|
int ssh_sk_load_resident_keys(const char *pin, struct sk_option **opts,
|
2019-12-30 10:21:16 +01:00
|
|
|
struct sk_resident_key ***rks, size_t *nrks);
|
2019-11-14 22:27:29 +01:00
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
static void
|
|
|
|
sshsk_free(struct sshsk_provider *p)
|
|
|
|
{
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
|
|
|
free(p->path);
|
|
|
|
if (p->dlhandle != NULL)
|
|
|
|
dlclose(p->dlhandle);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct sshsk_provider *
|
|
|
|
sshsk_open(const char *path)
|
|
|
|
{
|
|
|
|
struct sshsk_provider *ret = NULL;
|
|
|
|
uint32_t version;
|
|
|
|
|
2020-02-28 02:06:05 +01:00
|
|
|
if (path == NULL || *path == '\0') {
|
|
|
|
error("No FIDO SecurityKeyProvider specified");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-10-31 22:16:20 +01:00
|
|
|
if ((ret = calloc(1, sizeof(*ret))) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("calloc failed");
|
2019-10-31 22:16:20 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((ret->path = strdup(path)) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("strdup failed");
|
2019-10-31 22:16:20 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-11-14 22:27:29 +01:00
|
|
|
/* Skip the rest if we're using the linked in middleware */
|
|
|
|
if (strcasecmp(ret->path, "internal") == 0) {
|
|
|
|
#ifdef ENABLE_SK_INTERNAL
|
|
|
|
ret->sk_enroll = ssh_sk_enroll;
|
|
|
|
ret->sk_sign = ssh_sk_sign;
|
2019-12-30 10:21:16 +01:00
|
|
|
ret->sk_load_resident_keys = ssh_sk_load_resident_keys;
|
2022-06-24 05:46:39 +02:00
|
|
|
return ret;
|
2019-11-14 22:27:29 +01:00
|
|
|
#else
|
|
|
|
error("internal security key support not enabled");
|
2022-06-24 05:46:39 +02:00
|
|
|
goto fail;
|
2019-11-14 22:27:29 +01:00
|
|
|
#endif
|
|
|
|
}
|
2023-07-19 16:02:27 +02:00
|
|
|
if (lib_contains_symbol(path, "sk_api_version") != 0) {
|
|
|
|
error("provider %s is not an OpenSSH FIDO library", path);
|
2019-10-31 22:16:20 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2023-07-19 16:02:27 +02:00
|
|
|
if ((ret->dlhandle = dlopen(path, RTLD_NOW)) == NULL)
|
|
|
|
fatal("Provider \"%s\" dlopen failed: %s", path, dlerror());
|
2019-10-31 22:16:20 +01:00
|
|
|
if ((ret->sk_api_version = dlsym(ret->dlhandle,
|
|
|
|
"sk_api_version")) == NULL) {
|
2020-02-06 23:30:54 +01:00
|
|
|
error("Provider \"%s\" dlsym(sk_api_version) failed: %s",
|
|
|
|
path, dlerror());
|
2019-10-31 22:16:20 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
version = ret->sk_api_version();
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("provider %s implements version 0x%08lx", ret->path,
|
|
|
|
(u_long)version);
|
2019-10-31 22:16:20 +01:00
|
|
|
if ((version & SSH_SK_VERSION_MAJOR_MASK) != SSH_SK_VERSION_MAJOR) {
|
2020-02-06 23:30:54 +01:00
|
|
|
error("Provider \"%s\" implements unsupported "
|
2019-12-30 10:24:03 +01:00
|
|
|
"version 0x%08lx (supported: 0x%08lx)",
|
|
|
|
path, (u_long)version, (u_long)SSH_SK_VERSION_MAJOR);
|
2019-10-31 22:16:20 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if ((ret->sk_enroll = dlsym(ret->dlhandle, "sk_enroll")) == NULL) {
|
2020-02-06 23:30:54 +01:00
|
|
|
error("Provider %s dlsym(sk_enroll) failed: %s",
|
|
|
|
path, dlerror());
|
2019-10-31 22:16:20 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if ((ret->sk_sign = dlsym(ret->dlhandle, "sk_sign")) == NULL) {
|
2020-02-06 23:30:54 +01:00
|
|
|
error("Provider \"%s\" dlsym(sk_sign) failed: %s",
|
2019-10-31 22:16:20 +01:00
|
|
|
path, dlerror());
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-12-30 10:21:16 +01:00
|
|
|
if ((ret->sk_load_resident_keys = dlsym(ret->dlhandle,
|
|
|
|
"sk_load_resident_keys")) == NULL) {
|
2020-02-06 23:30:54 +01:00
|
|
|
error("Provider \"%s\" dlsym(sk_load_resident_keys) "
|
|
|
|
"failed: %s", path, dlerror());
|
2019-12-30 10:21:16 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-10-31 22:16:20 +01:00
|
|
|
/* success */
|
|
|
|
return ret;
|
|
|
|
fail:
|
|
|
|
sshsk_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sshsk_free_enroll_response(struct sk_enroll_response *r)
|
|
|
|
{
|
|
|
|
if (r == NULL)
|
|
|
|
return;
|
|
|
|
freezero(r->key_handle, r->key_handle_len);
|
|
|
|
freezero(r->public_key, r->public_key_len);
|
|
|
|
freezero(r->signature, r->signature_len);
|
|
|
|
freezero(r->attestation_cert, r->attestation_cert_len);
|
2020-09-09 05:08:01 +02:00
|
|
|
freezero(r->authdata, r->authdata_len);
|
2019-10-31 22:16:20 +01:00
|
|
|
freezero(r, sizeof(*r));
|
2019-11-27 23:32:11 +01:00
|
|
|
}
|
2019-10-31 22:16:20 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
sshsk_free_sign_response(struct sk_sign_response *r)
|
|
|
|
{
|
|
|
|
if (r == NULL)
|
|
|
|
return;
|
|
|
|
freezero(r->sig_r, r->sig_r_len);
|
|
|
|
freezero(r->sig_s, r->sig_s_len);
|
|
|
|
freezero(r, sizeof(*r));
|
2019-11-27 23:32:11 +01:00
|
|
|
}
|
2019-10-31 22:16:20 +01:00
|
|
|
|
2019-11-18 17:08:57 +01:00
|
|
|
#ifdef WITH_OPENSSL
|
2019-11-12 20:29:54 +01:00
|
|
|
/* Assemble key from response */
|
|
|
|
static int
|
|
|
|
sshsk_ecdsa_assemble(struct sk_enroll_response *resp, struct sshkey **keyp)
|
|
|
|
{
|
|
|
|
struct sshkey *key = NULL;
|
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
EC_POINT *q = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
*keyp = NULL;
|
|
|
|
if ((key = sshkey_new(KEY_ECDSA_SK)) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("sshkey_new failed");
|
2019-11-12 20:29:54 +01:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
key->ecdsa_nid = NID_X9_62_prime256v1;
|
|
|
|
if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) == NULL ||
|
|
|
|
(q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL ||
|
|
|
|
(b = sshbuf_new()) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("allocation failed");
|
2019-11-12 20:29:54 +01:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_string(b,
|
|
|
|
resp->public_key, resp->public_key_len)) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "sshbuf_put_string");
|
2019-11-12 20:29:54 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa))) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "parse");
|
2019-11-12 20:29:54 +01:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa), q) != 0) {
|
2020-02-06 23:30:54 +01:00
|
|
|
error("Authenticator returned invalid ECDSA key");
|
2019-11-12 20:29:54 +01:00
|
|
|
r = SSH_ERR_KEY_INVALID_EC_VALUE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (EC_KEY_set_public_key(key->ecdsa, q) != 1) {
|
|
|
|
/* XXX assume it is a allocation error */
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("allocation failed");
|
2019-11-12 20:29:54 +01:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
*keyp = key;
|
|
|
|
key = NULL; /* transferred */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
EC_POINT_free(q);
|
|
|
|
sshkey_free(key);
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
|
|
|
}
|
2019-11-18 17:08:57 +01:00
|
|
|
#endif /* WITH_OPENSSL */
|
2019-11-12 20:29:54 +01:00
|
|
|
|
2019-11-12 20:31:45 +01:00
|
|
|
static int
|
|
|
|
sshsk_ed25519_assemble(struct sk_enroll_response *resp, struct sshkey **keyp)
|
|
|
|
{
|
|
|
|
struct sshkey *key = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
*keyp = NULL;
|
|
|
|
if (resp->public_key_len != ED25519_PK_SZ) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("invalid size: %zu", resp->public_key_len);
|
2019-11-12 20:31:45 +01:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((key = sshkey_new(KEY_ED25519_SK)) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("sshkey_new failed");
|
2019-11-12 20:31:45 +01:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((key->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("malloc failed");
|
2019-11-12 20:31:45 +01:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(key->ed25519_pk, resp->public_key, ED25519_PK_SZ);
|
|
|
|
/* success */
|
|
|
|
*keyp = key;
|
|
|
|
key = NULL; /* transferred */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshkey_free(key);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-12-30 10:20:36 +01:00
|
|
|
static int
|
|
|
|
sshsk_key_from_response(int alg, const char *application, uint8_t flags,
|
|
|
|
struct sk_enroll_response *resp, struct sshkey **keyp)
|
|
|
|
{
|
|
|
|
struct sshkey *key = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
*keyp = NULL;
|
|
|
|
|
|
|
|
/* Check response validity */
|
2019-12-30 10:21:16 +01:00
|
|
|
if (resp->public_key == NULL || resp->key_handle == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("sk_enroll response invalid");
|
2019-12-30 10:20:36 +01:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
switch (alg) {
|
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
case SSH_SK_ECDSA:
|
|
|
|
if ((r = sshsk_ecdsa_assemble(resp, &key)) != 0)
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
case SSH_SK_ED25519:
|
|
|
|
if ((r = sshsk_ed25519_assemble(resp, &key)) != 0)
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
default:
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("unsupported algorithm %d", alg);
|
2019-12-30 10:20:36 +01:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
key->sk_flags = flags;
|
|
|
|
if ((key->sk_key_handle = sshbuf_new()) == NULL ||
|
|
|
|
(key->sk_reserved = sshbuf_new()) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("allocation failed");
|
2019-12-30 10:20:36 +01:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((key->sk_application = strdup(application)) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("strdup application failed");
|
2019-12-30 10:20:36 +01:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put(key->sk_key_handle, resp->key_handle,
|
|
|
|
resp->key_handle_len)) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "put key handle");
|
2019-12-30 10:20:36 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
*keyp = key;
|
|
|
|
key = NULL;
|
|
|
|
out:
|
|
|
|
sshkey_free(key);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-12-30 10:24:45 +01:00
|
|
|
static int
|
|
|
|
skerr_to_ssherr(int skerr)
|
|
|
|
{
|
|
|
|
switch (skerr) {
|
|
|
|
case SSH_SK_ERR_UNSUPPORTED:
|
|
|
|
return SSH_ERR_FEATURE_UNSUPPORTED;
|
|
|
|
case SSH_SK_ERR_PIN_REQUIRED:
|
|
|
|
return SSH_ERR_KEY_WRONG_PASSPHRASE;
|
2020-01-26 00:13:09 +01:00
|
|
|
case SSH_SK_ERR_DEVICE_NOT_FOUND:
|
|
|
|
return SSH_ERR_DEVICE_NOT_FOUND;
|
2022-07-20 05:29:14 +02:00
|
|
|
case SSH_SK_ERR_CREDENTIAL_EXISTS:
|
|
|
|
return SSH_ERR_KEY_BAD_PERMISSIONS;
|
2019-12-30 10:24:45 +01:00
|
|
|
case SSH_SK_ERR_GENERAL:
|
|
|
|
default:
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:00:46 +01:00
|
|
|
static void
|
|
|
|
sshsk_free_options(struct sk_option **opts)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (opts == NULL)
|
|
|
|
return;
|
|
|
|
for (i = 0; opts[i] != NULL; i++) {
|
|
|
|
free(opts[i]->name);
|
|
|
|
free(opts[i]->value);
|
|
|
|
free(opts[i]);
|
|
|
|
}
|
|
|
|
free(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sshsk_add_option(struct sk_option ***optsp, size_t *noptsp,
|
|
|
|
const char *name, const char *value, uint8_t required)
|
|
|
|
{
|
|
|
|
struct sk_option **opts = *optsp;
|
|
|
|
size_t nopts = *noptsp;
|
|
|
|
|
|
|
|
if ((opts = recallocarray(opts, nopts, nopts + 2, /* extra for NULL */
|
|
|
|
sizeof(*opts))) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("array alloc failed");
|
2020-01-06 03:00:46 +01:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
}
|
|
|
|
*optsp = opts;
|
|
|
|
*noptsp = nopts + 1;
|
|
|
|
if ((opts[nopts] = calloc(1, sizeof(**opts))) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("alloc failed");
|
2020-01-06 03:00:46 +01:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
}
|
|
|
|
if ((opts[nopts]->name = strdup(name)) == NULL ||
|
|
|
|
(opts[nopts]->value = strdup(value)) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("alloc failed");
|
2020-01-06 03:00:46 +01:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
}
|
|
|
|
opts[nopts]->required = required;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
make_options(const char *device, const char *user_id,
|
|
|
|
struct sk_option ***optsp)
|
|
|
|
{
|
|
|
|
struct sk_option **opts = NULL;
|
|
|
|
size_t nopts = 0;
|
|
|
|
int r, ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if (device != NULL &&
|
|
|
|
(r = sshsk_add_option(&opts, &nopts, "device", device, 0)) != 0) {
|
|
|
|
ret = r;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (user_id != NULL &&
|
|
|
|
(r = sshsk_add_option(&opts, &nopts, "user", user_id, 0)) != 0) {
|
|
|
|
ret = r;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
*optsp = opts;
|
|
|
|
opts = NULL;
|
|
|
|
nopts = 0;
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
sshsk_free_options(opts);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-09 05:08:01 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
fill_attestation_blob(const struct sk_enroll_response *resp,
|
|
|
|
struct sshbuf *attest)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (attest == NULL)
|
|
|
|
return 0; /* nothing to do */
|
|
|
|
if ((r = sshbuf_put_cstring(attest, "ssh-sk-attest-v01")) != 0 ||
|
|
|
|
(r = sshbuf_put_string(attest,
|
|
|
|
resp->attestation_cert, resp->attestation_cert_len)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(attest,
|
|
|
|
resp->signature, resp->signature_len)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(attest,
|
|
|
|
resp->authdata, resp->authdata_len)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(attest, 0)) != 0 || /* resvd flags */
|
|
|
|
(r = sshbuf_put_string(attest, NULL, 0)) != 0 /* resvd */) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "compose");
|
2020-09-09 05:08:01 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
int
|
2020-01-06 03:00:46 +01:00
|
|
|
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,
|
2019-12-30 10:23:28 +01:00
|
|
|
struct sshkey **keyp, struct sshbuf *attest)
|
2019-10-31 22:16:20 +01:00
|
|
|
{
|
|
|
|
struct sshsk_provider *skp = NULL;
|
|
|
|
struct sshkey *key = NULL;
|
|
|
|
u_char randchall[32];
|
|
|
|
const u_char *challenge;
|
|
|
|
size_t challenge_len;
|
|
|
|
struct sk_enroll_response *resp = NULL;
|
2020-01-06 03:00:46 +01:00
|
|
|
struct sk_option **opts = NULL;
|
2019-10-31 22:16:20 +01:00
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2019-11-12 20:32:30 +01:00
|
|
|
int alg;
|
2019-10-31 22:16:20 +01:00
|
|
|
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("provider \"%s\", device \"%s\", application \"%s\", "
|
|
|
|
"userid \"%s\", flags 0x%02x, challenge len %zu%s",
|
2020-01-06 03:00:46 +01:00
|
|
|
provider_path, device, application, userid, flags,
|
|
|
|
challenge_buf == NULL ? 0 : sshbuf_len(challenge_buf),
|
2019-12-30 10:23:28 +01:00
|
|
|
(pin != NULL && *pin != '\0') ? " with-pin" : "");
|
2019-11-16 23:42:30 +01:00
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
*keyp = NULL;
|
|
|
|
if (attest)
|
|
|
|
sshbuf_reset(attest);
|
2020-01-06 03:00:46 +01:00
|
|
|
|
|
|
|
if ((r = make_options(device, userid, &opts)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
2019-11-12 20:31:45 +01:00
|
|
|
switch (type) {
|
2019-11-18 17:08:57 +01:00
|
|
|
#ifdef WITH_OPENSSL
|
2019-11-12 20:31:45 +01:00
|
|
|
case KEY_ECDSA_SK:
|
2019-11-12 20:32:30 +01:00
|
|
|
alg = SSH_SK_ECDSA;
|
|
|
|
break;
|
2019-11-18 17:08:57 +01:00
|
|
|
#endif /* WITH_OPENSSL */
|
2019-11-12 20:31:45 +01:00
|
|
|
case KEY_ED25519_SK:
|
2019-11-12 20:32:30 +01:00
|
|
|
alg = SSH_SK_ED25519;
|
2019-11-12 20:31:45 +01:00
|
|
|
break;
|
|
|
|
default:
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("unsupported key type");
|
2019-11-12 20:31:45 +01:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-10-31 22:16:20 +01:00
|
|
|
if (provider_path == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("missing provider");
|
2019-10-31 22:16:20 +01:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (application == NULL || *application == '\0') {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("missing application");
|
2019-10-31 22:16:20 +01:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (challenge_buf == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("using random challenge");
|
2019-10-31 22:16:20 +01:00
|
|
|
arc4random_buf(randchall, sizeof(randchall));
|
|
|
|
challenge = randchall;
|
|
|
|
challenge_len = sizeof(randchall);
|
|
|
|
} else if (sshbuf_len(challenge_buf) == 0) {
|
|
|
|
error("Missing enrollment challenge");
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
challenge = sshbuf_ptr(challenge_buf);
|
|
|
|
challenge_len = sshbuf_len(challenge_buf);
|
2020-10-18 13:32:01 +02:00
|
|
|
debug3_f("using explicit challenge len=%zd", challenge_len);
|
2019-10-31 22:16:20 +01:00
|
|
|
}
|
|
|
|
if ((skp = sshsk_open(provider_path)) == NULL) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* XXX validate flags? */
|
|
|
|
/* enroll key */
|
2019-11-12 20:32:30 +01:00
|
|
|
if ((r = skp->sk_enroll(alg, challenge, challenge_len, application,
|
2020-01-06 03:00:46 +01:00
|
|
|
flags, pin, opts, &resp)) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("provider \"%s\" failure %d", provider_path, r);
|
2019-12-30 10:24:45 +01:00
|
|
|
r = skerr_to_ssherr(r);
|
2019-10-31 22:16:20 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2019-12-30 10:20:36 +01:00
|
|
|
|
2021-11-02 23:56:40 +01:00
|
|
|
if ((r = sshsk_key_from_response(alg, application, resp->flags,
|
2019-12-30 10:20:36 +01:00
|
|
|
resp, &key)) != 0)
|
2019-10-31 22:16:20 +01:00
|
|
|
goto out;
|
2019-12-30 10:20:36 +01:00
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
/* Optionally fill in the attestation information */
|
2020-09-09 05:08:01 +02:00
|
|
|
if ((r = fill_attestation_blob(resp, attest)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
/* success */
|
|
|
|
*keyp = key;
|
|
|
|
key = NULL; /* transferred */
|
|
|
|
r = 0;
|
|
|
|
out:
|
2020-01-06 03:00:46 +01:00
|
|
|
sshsk_free_options(opts);
|
2019-10-31 22:16:20 +01:00
|
|
|
sshsk_free(skp);
|
|
|
|
sshkey_free(key);
|
|
|
|
sshsk_free_enroll_response(resp);
|
|
|
|
explicit_bzero(randchall, sizeof(randchall));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-11-18 17:08:57 +01:00
|
|
|
#ifdef WITH_OPENSSL
|
2019-11-12 20:30:21 +01:00
|
|
|
static int
|
2019-11-12 20:34:40 +01:00
|
|
|
sshsk_ecdsa_sig(struct sk_sign_response *resp, struct sshbuf *sig)
|
2019-11-12 20:30:21 +01:00
|
|
|
{
|
|
|
|
struct sshbuf *inner_sig = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
2019-11-12 20:34:00 +01:00
|
|
|
/* Check response validity */
|
2019-11-13 21:25:45 +01:00
|
|
|
if (resp->sig_r == NULL || resp->sig_s == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("sk_sign response invalid");
|
2019-11-12 20:34:00 +01:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-11-12 20:30:21 +01:00
|
|
|
if ((inner_sig = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-11-12 20:34:40 +01:00
|
|
|
/* Prepare and append inner signature object */
|
2019-11-12 20:30:21 +01:00
|
|
|
if ((r = sshbuf_put_bignum2_bytes(inner_sig,
|
|
|
|
resp->sig_r, resp->sig_r_len)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum2_bytes(inner_sig,
|
2019-11-19 23:23:19 +01:00
|
|
|
resp->sig_s, resp->sig_s_len)) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "compose inner");
|
2019-11-12 20:30:21 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2019-11-19 23:23:19 +01:00
|
|
|
if ((r = sshbuf_put_stringb(sig, inner_sig)) != 0 ||
|
|
|
|
(r = sshbuf_put_u8(sig, resp->flags)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(sig, resp->counter)) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "compose");
|
2019-11-12 20:34:40 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2019-11-12 20:30:21 +01:00
|
|
|
#ifdef DEBUG_SK
|
|
|
|
fprintf(stderr, "%s: sig_r:\n", __func__);
|
|
|
|
sshbuf_dump_data(resp->sig_r, resp->sig_r_len, stderr);
|
|
|
|
fprintf(stderr, "%s: sig_s:\n", __func__);
|
|
|
|
sshbuf_dump_data(resp->sig_s, resp->sig_s_len, stderr);
|
2019-11-12 20:34:40 +01:00
|
|
|
fprintf(stderr, "%s: inner:\n", __func__);
|
|
|
|
sshbuf_dump(inner_sig, stderr);
|
2019-11-12 20:31:18 +01:00
|
|
|
#endif
|
|
|
|
r = 0;
|
2019-11-12 20:34:40 +01:00
|
|
|
out:
|
2019-11-12 20:31:18 +01:00
|
|
|
sshbuf_free(inner_sig);
|
|
|
|
return r;
|
|
|
|
}
|
2019-11-18 17:08:57 +01:00
|
|
|
#endif /* WITH_OPENSSL */
|
2019-11-12 20:31:18 +01:00
|
|
|
|
|
|
|
static int
|
2019-11-12 20:34:40 +01:00
|
|
|
sshsk_ed25519_sig(struct sk_sign_response *resp, struct sshbuf *sig)
|
2019-11-12 20:31:18 +01:00
|
|
|
{
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
2019-11-12 20:34:00 +01:00
|
|
|
/* Check response validity */
|
|
|
|
if (resp->sig_r == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("sk_sign response invalid");
|
2019-11-12 20:34:00 +01:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-11-12 20:34:40 +01:00
|
|
|
if ((r = sshbuf_put_string(sig,
|
2019-11-12 20:31:18 +01:00
|
|
|
resp->sig_r, resp->sig_r_len)) != 0 ||
|
2019-11-12 20:34:40 +01:00
|
|
|
(r = sshbuf_put_u8(sig, resp->flags)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(sig, resp->counter)) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "compose");
|
2019-11-12 20:31:18 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_SK
|
|
|
|
fprintf(stderr, "%s: sig_r:\n", __func__);
|
|
|
|
sshbuf_dump_data(resp->sig_r, resp->sig_r_len, stderr);
|
2019-11-12 20:30:21 +01:00
|
|
|
#endif
|
|
|
|
r = 0;
|
2019-11-12 20:34:40 +01:00
|
|
|
out:
|
2020-03-06 19:25:48 +01:00
|
|
|
return r;
|
2019-11-12 20:30:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
int
|
2019-12-13 20:09:10 +01:00
|
|
|
sshsk_sign(const char *provider_path, struct sshkey *key,
|
2019-10-31 22:16:20 +01:00
|
|
|
u_char **sigp, size_t *lenp, const u_char *data, size_t datalen,
|
2019-12-30 10:23:28 +01:00
|
|
|
u_int compat, const char *pin)
|
2019-10-31 22:16:20 +01:00
|
|
|
{
|
|
|
|
struct sshsk_provider *skp = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2019-11-12 20:32:30 +01:00
|
|
|
int type, alg;
|
2019-10-31 22:16:20 +01:00
|
|
|
struct sk_sign_response *resp = NULL;
|
|
|
|
struct sshbuf *inner_sig = NULL, *sig = NULL;
|
2020-01-06 03:00:46 +01:00
|
|
|
struct sk_option **opts = NULL;
|
2019-10-31 22:16:20 +01:00
|
|
|
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("provider \"%s\", key %s, flags 0x%02x%s",
|
2019-12-30 10:23:28 +01:00
|
|
|
provider_path, sshkey_type(key), key->sk_flags,
|
|
|
|
(pin != NULL && *pin != '\0') ? " with-pin" : "");
|
2019-11-16 23:42:30 +01:00
|
|
|
|
2019-10-31 22:16:20 +01:00
|
|
|
if (sigp != NULL)
|
|
|
|
*sigp = NULL;
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
2019-11-12 20:31:18 +01:00
|
|
|
type = sshkey_type_plain(key->type);
|
|
|
|
switch (type) {
|
2019-11-18 17:08:57 +01:00
|
|
|
#ifdef WITH_OPENSSL
|
2019-11-12 20:31:18 +01:00
|
|
|
case KEY_ECDSA_SK:
|
2019-11-12 20:32:30 +01:00
|
|
|
alg = SSH_SK_ECDSA;
|
|
|
|
break;
|
2019-11-18 17:08:57 +01:00
|
|
|
#endif /* WITH_OPENSSL */
|
2019-11-12 20:31:18 +01:00
|
|
|
case KEY_ED25519_SK:
|
2019-11-12 20:32:30 +01:00
|
|
|
alg = SSH_SK_ED25519;
|
2019-11-12 20:31:18 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2019-10-31 22:16:20 +01:00
|
|
|
if (provider_path == NULL ||
|
|
|
|
key->sk_key_handle == NULL ||
|
|
|
|
key->sk_application == NULL || *key->sk_application == '\0') {
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((skp = sshsk_open(provider_path)) == NULL) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
|
|
|
|
goto out;
|
|
|
|
}
|
2021-02-26 01:16:58 +01:00
|
|
|
#ifdef DEBUG_SK
|
|
|
|
fprintf(stderr, "%s: sk_flags = 0x%02x, sk_application = \"%s\"\n",
|
|
|
|
__func__, key->sk_flags, key->sk_application);
|
|
|
|
fprintf(stderr, "%s: sk_key_handle:\n", __func__);
|
|
|
|
sshbuf_dump(key->sk_key_handle, stderr);
|
|
|
|
#endif
|
2020-04-28 06:02:29 +02:00
|
|
|
if ((r = skp->sk_sign(alg, data, datalen, key->sk_application,
|
2019-10-31 22:16:20 +01:00
|
|
|
sshbuf_ptr(key->sk_key_handle), sshbuf_len(key->sk_key_handle),
|
2020-01-06 03:00:46 +01:00
|
|
|
key->sk_flags, pin, opts, &resp)) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("sk_sign failed with code %d", r);
|
2019-12-30 10:24:45 +01:00
|
|
|
r = skerr_to_ssherr(r);
|
2019-10-31 22:16:20 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2019-11-12 20:34:40 +01:00
|
|
|
/* Assemble signature */
|
|
|
|
if ((sig = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_cstring(sig, sshkey_ssh_name_plain(key))) != 0) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_fr(r, "compose outer");
|
2019-11-12 20:34:40 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2019-11-12 20:31:18 +01:00
|
|
|
switch (type) {
|
2019-11-18 17:08:57 +01:00
|
|
|
#ifdef WITH_OPENSSL
|
2019-11-12 20:31:18 +01:00
|
|
|
case KEY_ECDSA_SK:
|
2019-11-12 20:34:40 +01:00
|
|
|
if ((r = sshsk_ecdsa_sig(resp, sig)) != 0)
|
2019-11-12 20:31:18 +01:00
|
|
|
goto out;
|
|
|
|
break;
|
2019-11-18 17:08:57 +01:00
|
|
|
#endif /* WITH_OPENSSL */
|
2019-11-12 20:31:18 +01:00
|
|
|
case KEY_ED25519_SK:
|
2019-11-12 20:34:40 +01:00
|
|
|
if ((r = sshsk_ed25519_sig(resp, sig)) != 0)
|
2019-11-12 20:31:18 +01:00
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-31 22:16:20 +01:00
|
|
|
#ifdef DEBUG_SK
|
2019-11-12 20:31:18 +01:00
|
|
|
fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
|
|
|
|
__func__, resp->flags, resp->counter);
|
2021-02-18 03:13:58 +01:00
|
|
|
fprintf(stderr, "%s: data to sign:\n", __func__);
|
|
|
|
sshbuf_dump_data(data, datalen, stderr);
|
2019-10-31 22:16:20 +01:00
|
|
|
fprintf(stderr, "%s: sigbuf:\n", __func__);
|
|
|
|
sshbuf_dump(sig, stderr);
|
|
|
|
#endif
|
|
|
|
if (sigp != NULL) {
|
|
|
|
if ((*sigp = malloc(sshbuf_len(sig))) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(*sigp, sshbuf_ptr(sig), sshbuf_len(sig));
|
|
|
|
}
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = sshbuf_len(sig);
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
2020-01-06 03:00:46 +01:00
|
|
|
sshsk_free_options(opts);
|
2019-10-31 22:16:20 +01:00
|
|
|
sshsk_free(skp);
|
|
|
|
sshsk_free_sign_response(resp);
|
|
|
|
sshbuf_free(sig);
|
|
|
|
sshbuf_free(inner_sig);
|
|
|
|
return r;
|
|
|
|
}
|
2019-12-30 10:21:16 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
sshsk_free_sk_resident_keys(struct sk_resident_key **rks, size_t nrks)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (nrks == 0 || rks == NULL)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < nrks; i++) {
|
|
|
|
free(rks[i]->application);
|
2022-01-14 04:35:10 +01:00
|
|
|
freezero(rks[i]->user_id, rks[i]->user_id_len);
|
2019-12-30 10:21:16 +01:00
|
|
|
freezero(rks[i]->key.key_handle, rks[i]->key.key_handle_len);
|
|
|
|
freezero(rks[i]->key.public_key, rks[i]->key.public_key_len);
|
|
|
|
freezero(rks[i]->key.signature, rks[i]->key.signature_len);
|
|
|
|
freezero(rks[i]->key.attestation_cert,
|
|
|
|
rks[i]->key.attestation_cert_len);
|
|
|
|
freezero(rks[i], sizeof(**rks));
|
|
|
|
}
|
|
|
|
free(rks);
|
|
|
|
}
|
|
|
|
|
2021-10-28 04:54:18 +02:00
|
|
|
static void
|
|
|
|
sshsk_free_resident_key(struct sshsk_resident_key *srk)
|
|
|
|
{
|
|
|
|
if (srk == NULL)
|
|
|
|
return;
|
|
|
|
sshkey_free(srk->key);
|
|
|
|
freezero(srk->user_id, srk->user_id_len);
|
|
|
|
free(srk);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
sshsk_free_resident_keys(struct sshsk_resident_key **srks, size_t nsrks)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (srks == NULL || nsrks == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < nsrks; i++)
|
|
|
|
sshsk_free_resident_key(srks[i]);
|
|
|
|
free(srks);
|
|
|
|
}
|
|
|
|
|
2019-12-30 10:21:16 +01:00
|
|
|
int
|
2020-01-06 03:00:46 +01:00
|
|
|
sshsk_load_resident(const char *provider_path, const char *device,
|
2021-10-28 04:54:18 +02:00
|
|
|
const char *pin, u_int flags, struct sshsk_resident_key ***srksp,
|
|
|
|
size_t *nsrksp)
|
2019-12-30 10:21:16 +01:00
|
|
|
{
|
|
|
|
struct sshsk_provider *skp = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
struct sk_resident_key **rks = NULL;
|
2021-10-28 04:54:18 +02:00
|
|
|
size_t i, nrks = 0, nsrks = 0;
|
|
|
|
struct sshkey *key = NULL;
|
|
|
|
struct sshsk_resident_key *srk = NULL, **srks = NULL, **tmp;
|
|
|
|
uint8_t sk_flags;
|
2020-01-06 03:00:46 +01:00
|
|
|
struct sk_option **opts = NULL;
|
2019-12-30 10:21:16 +01:00
|
|
|
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("provider \"%s\"%s", provider_path,
|
2019-12-30 10:21:16 +01:00
|
|
|
(pin != NULL && *pin != '\0') ? ", have-pin": "");
|
|
|
|
|
2021-10-28 04:54:18 +02:00
|
|
|
if (srksp == NULL || nsrksp == NULL)
|
2019-12-30 10:21:16 +01:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2021-10-28 04:54:18 +02:00
|
|
|
*srksp = NULL;
|
|
|
|
*nsrksp = 0;
|
2019-12-30 10:21:16 +01:00
|
|
|
|
2020-01-06 03:00:46 +01:00
|
|
|
if ((r = make_options(device, NULL, &opts)) != 0)
|
|
|
|
goto out;
|
2019-12-30 10:21:16 +01:00
|
|
|
if ((skp = sshsk_open(provider_path)) == NULL) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT; /* XXX sshsk_open return code? */
|
|
|
|
goto out;
|
|
|
|
}
|
2020-01-06 03:00:46 +01:00
|
|
|
if ((r = skp->sk_load_resident_keys(pin, opts, &rks, &nrks)) != 0) {
|
2020-02-06 23:30:54 +01:00
|
|
|
error("Provider \"%s\" returned failure %d", provider_path, r);
|
2019-12-30 10:24:45 +01:00
|
|
|
r = skerr_to_ssherr(r);
|
2019-12-30 10:21:16 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (i = 0; i < nrks; i++) {
|
2021-10-28 04:54:18 +02:00
|
|
|
debug3_f("rk %zu: slot %zu, alg %d, app \"%s\", uidlen %zu",
|
|
|
|
i, rks[i]->slot, rks[i]->alg, rks[i]->application,
|
|
|
|
rks[i]->user_id_len);
|
2019-12-30 10:21:16 +01:00
|
|
|
/* XXX need better filter here */
|
|
|
|
if (strncmp(rks[i]->application, "ssh:", 4) != 0)
|
|
|
|
continue;
|
|
|
|
switch (rks[i]->alg) {
|
|
|
|
case SSH_SK_ECDSA:
|
|
|
|
case SSH_SK_ED25519:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-28 04:54:18 +02:00
|
|
|
sk_flags = SSH_SK_USER_PRESENCE_REQD|SSH_SK_RESIDENT_KEY;
|
2020-08-27 03:08:19 +02:00
|
|
|
if ((rks[i]->flags & SSH_SK_USER_VERIFICATION_REQD))
|
2021-10-28 04:54:18 +02:00
|
|
|
sk_flags |= SSH_SK_USER_VERIFICATION_REQD;
|
2019-12-30 10:21:16 +01:00
|
|
|
if ((r = sshsk_key_from_response(rks[i]->alg,
|
2021-10-28 04:54:18 +02:00
|
|
|
rks[i]->application, sk_flags, &rks[i]->key, &key)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((srk = calloc(1, sizeof(*srk))) == NULL) {
|
|
|
|
error_f("calloc failed");
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
2019-12-30 10:21:16 +01:00
|
|
|
goto out;
|
2021-10-28 04:54:18 +02:00
|
|
|
}
|
|
|
|
srk->key = key;
|
|
|
|
key = NULL; /* transferred */
|
|
|
|
if ((srk->user_id = calloc(1, rks[i]->user_id_len)) == NULL) {
|
|
|
|
error_f("calloc failed");
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(srk->user_id, rks[i]->user_id, rks[i]->user_id_len);
|
|
|
|
srk->user_id_len = rks[i]->user_id_len;
|
|
|
|
if ((tmp = recallocarray(srks, nsrks, nsrks + 1,
|
2019-12-30 10:21:16 +01:00
|
|
|
sizeof(*tmp))) == NULL) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("recallocarray failed");
|
2019-12-30 10:21:16 +01:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-10-28 04:54:18 +02:00
|
|
|
srks = tmp;
|
|
|
|
srks[nsrks++] = srk;
|
|
|
|
srk = NULL;
|
2019-12-30 10:21:16 +01:00
|
|
|
/* XXX synthesise comment */
|
|
|
|
}
|
|
|
|
/* success */
|
2021-10-28 04:54:18 +02:00
|
|
|
*srksp = srks;
|
|
|
|
*nsrksp = nsrks;
|
|
|
|
srks = NULL;
|
|
|
|
nsrks = 0;
|
2019-12-30 10:21:16 +01:00
|
|
|
r = 0;
|
|
|
|
out:
|
2020-01-06 03:00:46 +01:00
|
|
|
sshsk_free_options(opts);
|
2019-12-30 10:21:16 +01:00
|
|
|
sshsk_free(skp);
|
|
|
|
sshsk_free_sk_resident_keys(rks, nrks);
|
|
|
|
sshkey_free(key);
|
2021-10-28 04:54:18 +02:00
|
|
|
sshsk_free_resident_key(srk);
|
|
|
|
sshsk_free_resident_keys(srks, nsrks);
|
2019-12-30 10:21:16 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-11-01 03:34:49 +01:00
|
|
|
#endif /* ENABLE_SK */
|