solo1/fido2/u2f.c

342 wiersze
9.1 KiB
C
Czysty Zwykły widok Historia

2019-02-12 22:18:17 +00:00
// Copyright 2019 SoloKeys Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
2018-05-26 19:29:37 +00:00
#include <stdlib.h>
2018-05-26 15:36:41 +00:00
#include "u2f.h"
#include "ctap.h"
#include "crypto.h"
2018-05-26 19:29:37 +00:00
#include "log.h"
2018-06-02 22:30:59 +00:00
#include "device.h"
2019-02-26 18:27:25 +00:00
#include "apdu.h"
2018-07-12 03:00:53 +00:00
#include "wallet.h"
2019-01-06 06:19:14 +00:00
#ifdef ENABLE_U2F_EXTENSIONS
#include "extensions.h"
#endif
2018-12-01 19:42:49 +00:00
#include APP_CONFIG
2018-05-26 15:36:41 +00:00
// void u2f_response_writeback(uint8_t * buf, uint8_t len);
2019-01-06 06:19:14 +00:00
#ifdef ENABLE_U2F
2018-05-26 15:36:41 +00:00
static int16_t u2f_register(struct u2f_register_request * req);
static int16_t u2f_authenticate(struct u2f_authenticate_request * req, uint8_t control);
2019-01-06 06:19:14 +00:00
#endif
2018-07-08 02:43:06 +00:00
int8_t u2f_response_writeback(const uint8_t * buf, uint16_t len);
2018-07-09 02:36:16 +00:00
void u2f_reset_response();
2018-05-26 15:36:41 +00:00
2018-05-26 19:29:37 +00:00
static CTAP_RESPONSE * _u2f_resp = NULL;
2019-02-26 19:07:27 +00:00
void u2f_request_ex(APDU_HEADER *req, uint8_t *payload, uint32_t len, CTAP_RESPONSE * resp)
2018-05-26 15:36:41 +00:00
{
2018-09-13 02:32:15 +00:00
uint16_t rcode = 0;
2018-05-26 19:29:37 +00:00
uint8_t byte;
2019-01-27 09:44:33 +00:00
ctap_response_init(resp);
u2f_set_writeback_buffer(resp);
2018-05-26 15:36:41 +00:00
if (req->cla != 0)
{
2018-05-26 19:29:37 +00:00
printf1(TAG_U2F, "CLA not zero\n");
rcode = U2F_SW_CLASS_NOT_SUPPORTED;
2018-05-26 15:36:41 +00:00
goto end;
}
2018-09-13 02:32:15 +00:00
#ifdef ENABLE_U2F_EXTENSIONS
2019-02-26 20:30:57 +00:00
rcode = extend_u2f(req, payload, len);
2018-09-13 02:32:15 +00:00
#endif
2019-02-14 20:16:13 +00:00
if (rcode != U2F_SW_NO_ERROR && rcode != U2F_SW_CONDITIONS_NOT_SATISFIED) // If the extension didn't do anything...
2018-07-08 02:43:06 +00:00
{
2018-09-13 02:32:15 +00:00
#ifdef ENABLE_U2F
switch(req->ins)
2018-07-08 02:43:06 +00:00
{
2018-09-13 02:32:15 +00:00
case U2F_REGISTER:
printf1(TAG_U2F, "U2F_REGISTER\n");
if (len != 64)
{
rcode = U2F_SW_WRONG_LENGTH;
}
else
{
timestamp();
2019-02-26 19:07:27 +00:00
rcode = u2f_register((struct u2f_register_request*)payload);
printf1(TAG_TIME,"u2f_register time: %d ms\n", timestamp());
2018-09-13 02:32:15 +00:00
}
break;
case U2F_AUTHENTICATE:
printf1(TAG_U2F, "U2F_AUTHENTICATE\n");
timestamp();
2019-02-26 19:07:27 +00:00
rcode = u2f_authenticate((struct u2f_authenticate_request*)payload, req->p1);
printf1(TAG_TIME,"u2f_authenticate time: %d ms\n", timestamp());
2018-09-13 02:32:15 +00:00
break;
case U2F_VERSION:
printf1(TAG_U2F, "U2F_VERSION\n");
if (len)
{
rcode = U2F_SW_WRONG_LENGTH;
}
else
{
rcode = u2f_version();
}
break;
case U2F_VENDOR_FIRST:
case U2F_VENDOR_LAST:
printf1(TAG_U2F, "U2F_VENDOR\n");
rcode = U2F_SW_NO_ERROR;
break;
default:
printf1(TAG_ERR, "Error, unknown U2F command\n");
rcode = U2F_SW_INS_NOT_SUPPORTED;
break;
}
2018-07-08 02:43:06 +00:00
#endif
2018-09-13 02:32:15 +00:00
}
2018-05-26 15:36:41 +00:00
2018-05-26 19:29:37 +00:00
end:
if (rcode != U2F_SW_NO_ERROR)
{
printf1(TAG_U2F,"U2F Error code %04x\n", rcode);
ctap_response_init(_u2f_resp);
}
2018-05-26 16:51:56 +00:00
2018-05-26 19:29:37 +00:00
byte = (rcode & 0xff00)>>8;
u2f_response_writeback(&byte,1);
byte = rcode & 0xff;
u2f_response_writeback(&byte,1);
2018-05-26 16:51:56 +00:00
2018-05-26 19:29:37 +00:00
printf1(TAG_U2F,"u2f resp: "); dump_hex1(TAG_U2F, _u2f_resp->data, _u2f_resp->length);
2018-05-26 16:51:56 +00:00
}
2019-01-27 09:44:33 +00:00
void u2f_request_nfc(uint8_t * req, int len, CTAP_RESPONSE * resp)
{
if (len < 5 || !req)
return;
2019-02-02 05:32:36 +00:00
2019-01-27 09:44:33 +00:00
uint32_t alen = req[4];
2019-02-02 05:32:36 +00:00
2019-02-26 19:07:27 +00:00
u2f_request_ex((APDU_HEADER *)req, &req[5], alen, resp);
2019-01-27 09:44:33 +00:00
}
void u2f_request(struct u2f_request_apdu* req, CTAP_RESPONSE * resp)
{
uint32_t len = ((req->LC3) | ((uint32_t)req->LC2 << 8) | ((uint32_t)req->LC1 << 16));
2019-02-02 05:32:36 +00:00
2019-02-26 19:07:27 +00:00
u2f_request_ex((APDU_HEADER *)req, req->payload, len, resp);
2019-01-27 09:44:33 +00:00
}
2018-05-26 16:51:56 +00:00
2018-07-08 02:43:06 +00:00
int8_t u2f_response_writeback(const uint8_t * buf, uint16_t len)
2018-05-26 16:51:56 +00:00
{
2018-05-26 19:29:37 +00:00
if ((_u2f_resp->length + len) > _u2f_resp->data_size)
{
printf2(TAG_ERR, "Not enough space for U2F response, writeback\n");
exit(1);
}
memmove(_u2f_resp->data + _u2f_resp->length, buf, len);
_u2f_resp->length += len;
return 0;
2018-05-26 16:51:56 +00:00
}
2018-07-09 02:36:16 +00:00
void u2f_reset_response()
{
ctap_response_init(_u2f_resp);
}
2018-05-26 16:51:56 +00:00
void u2f_set_writeback_buffer(CTAP_RESPONSE * resp)
{
_u2f_resp = resp;
}
2018-05-26 16:51:56 +00:00
2019-01-06 06:19:14 +00:00
#ifdef ENABLE_U2F
2018-05-26 15:36:41 +00:00
static void dump_signature_der(uint8_t * sig)
{
2018-05-26 19:35:58 +00:00
uint8_t sigder[72];
int len;
len = ctap_encode_der_sig(sig, sigder);
u2f_response_writeback(sigder, len);
2018-05-26 15:36:41 +00:00
}
2018-05-26 16:51:56 +00:00
static int8_t u2f_load_key(struct u2f_key_handle * kh, uint8_t * appid)
2018-05-26 15:36:41 +00:00
{
2018-05-26 19:29:37 +00:00
crypto_ecc256_load_key((uint8_t*)kh, U2F_KEY_HANDLE_SIZE, NULL, 0);
2018-05-26 15:36:41 +00:00
return 0;
}
2018-05-26 16:51:56 +00:00
static void u2f_make_auth_tag(struct u2f_key_handle * kh, uint8_t * appid, uint8_t * tag)
2018-05-26 15:36:41 +00:00
{
uint8_t hashbuf[32];
2018-05-26 16:51:56 +00:00
crypto_sha256_hmac_init(CRYPTO_MASTER_KEY, 0, hashbuf);
2018-05-26 15:36:41 +00:00
crypto_sha256_update(kh->key, U2F_KEY_HANDLE_KEY_SIZE);
2018-05-26 16:51:56 +00:00
crypto_sha256_update(appid, U2F_APPLICATION_SIZE);
crypto_sha256_hmac_final(CRYPTO_MASTER_KEY, 0,hashbuf);
2018-05-26 15:36:41 +00:00
memmove(tag, hashbuf, CREDENTIAL_TAG_SIZE);
}
int8_t u2f_new_keypair(struct u2f_key_handle * kh, uint8_t * appid, uint8_t * pubkey)
2018-05-26 19:29:37 +00:00
{
ctap_generate_rng(kh->key, U2F_KEY_HANDLE_KEY_SIZE);
u2f_make_auth_tag(kh, appid, kh->tag);
crypto_ecc256_derive_public_key((uint8_t*)kh, U2F_KEY_HANDLE_SIZE, pubkey, pubkey+32);
return 0;
}
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
static int8_t u2f_appid_eq(struct u2f_key_handle * kh, uint8_t * appid)
{
uint8_t tag[U2F_KEY_HANDLE_TAG_SIZE];
2018-05-26 19:29:37 +00:00
u2f_make_auth_tag(kh, appid, tag);
2018-05-26 16:51:56 +00:00
if (memcmp(kh->tag, tag, U2F_KEY_HANDLE_TAG_SIZE) == 0)
2018-05-26 19:29:37 +00:00
{
2018-05-26 16:51:56 +00:00
return 0;
2018-05-26 19:29:37 +00:00
}
2018-05-26 16:51:56 +00:00
else
2018-05-26 19:29:37 +00:00
{
printf1(TAG_U2F, "key handle + appid not authentic\n");
printf1(TAG_U2F, "calc tag: \n"); dump_hex1(TAG_U2F,tag, U2F_KEY_HANDLE_TAG_SIZE);
printf1(TAG_U2F, "inp tag: \n"); dump_hex1(TAG_U2F,kh->tag, U2F_KEY_HANDLE_TAG_SIZE);
2018-05-26 16:51:56 +00:00
return -1;
2018-05-26 19:29:37 +00:00
}
2018-05-26 16:51:56 +00:00
}
2018-05-26 15:36:41 +00:00
static int16_t u2f_authenticate(struct u2f_authenticate_request * req, uint8_t control)
{
uint8_t up = 1;
uint32_t count;
2018-05-26 16:51:56 +00:00
uint8_t hash[32];
uint8_t * sig = (uint8_t*)req;
2018-05-26 15:36:41 +00:00
if (control == U2F_AUTHENTICATE_CHECK)
{
2019-02-27 01:34:07 +00:00
printf1(TAG_U2F, "CHECK-ONLY\r\n");
2018-05-26 15:36:41 +00:00
if (u2f_appid_eq(&req->kh, req->app) == 0)
{
return U2F_SW_CONDITIONS_NOT_SATISFIED;
}
else
{
return U2F_SW_WRONG_PAYLOAD;
2018-05-26 15:36:41 +00:00
}
}
if (
(control != U2F_AUTHENTICATE_SIGN && control != U2F_AUTHENTICATE_SIGN_NO_USER) ||
req->khl != U2F_KEY_HANDLE_SIZE ||
2018-05-26 15:36:41 +00:00
u2f_appid_eq(&req->kh, req->app) != 0 || // Order of checks is important
u2f_load_key(&req->kh, req->app) != 0
)
{
2019-02-27 01:34:07 +00:00
return U2F_SW_WRONG_DATA;
2018-05-26 15:36:41 +00:00
}
// dont-enforce-user-presence-and-sign
if (control == U2F_AUTHENTICATE_SIGN_NO_USER)
up = 0;
2018-05-26 15:36:41 +00:00
2019-02-26 19:07:27 +00:00
if(!device_is_nfc() && up)
2019-01-27 22:04:17 +00:00
{
if (ctap_user_presence_test() == 0)
{
return U2F_SW_CONDITIONS_NOT_SATISFIED;
}
}
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
count = ctap_atomic_count(0);
2019-03-01 04:13:12 +00:00
hash[0] = (count >> 24) & 0xff;
2019-01-09 02:37:15 +00:00
hash[1] = (count >> 16) & 0xff;
hash[2] = (count >> 8) & 0xff;
hash[3] = (count >> 0) & 0xff;
2018-05-26 16:51:56 +00:00
crypto_sha256_init();
2018-05-26 15:36:41 +00:00
crypto_sha256_update(req->app, 32);
crypto_sha256_update(&up, 1);
2019-02-26 18:27:25 +00:00
crypto_sha256_update(hash, 4);
crypto_sha256_update(req->chal, 32);
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
crypto_sha256_final(hash);
printf1(TAG_U2F, "sha256: "); dump_hex1(TAG_U2F, hash, 32);
2018-05-26 16:51:56 +00:00
crypto_ecc256_sign(hash, 32, sig);
2018-05-26 15:36:41 +00:00
u2f_response_writeback(&up,1);
2019-03-01 04:13:12 +00:00
hash[0] = (count >> 24) & 0xff;
2019-01-09 02:37:15 +00:00
hash[1] = (count >> 16) & 0xff;
hash[2] = (count >> 8) & 0xff;
hash[3] = (count >> 0) & 0xff;
u2f_response_writeback(hash,4);
2018-05-26 16:51:56 +00:00
dump_signature_der(sig);
2018-05-26 15:36:41 +00:00
return U2F_SW_NO_ERROR;
}
static int16_t u2f_register(struct u2f_register_request * req)
{
uint8_t i[] = {0x0,U2F_EC_FMT_UNCOMPRESSED};
2018-05-26 16:51:56 +00:00
struct u2f_key_handle key_handle;
2018-05-26 15:36:41 +00:00
uint8_t pubkey[64];
2018-05-26 16:51:56 +00:00
uint8_t hash[32];
uint8_t * sig = (uint8_t*)req;
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
const uint16_t attest_size = attestation_cert_der_size;
2018-05-26 15:36:41 +00:00
2019-02-26 19:07:27 +00:00
if(!device_is_nfc())
2019-01-26 21:44:51 +00:00
{
if ( ! ctap_user_presence_test())
{
return U2F_SW_CONDITIONS_NOT_SATISFIED;
}
}
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
if ( u2f_new_keypair(&key_handle, req->app, pubkey) == -1)
2018-05-26 15:36:41 +00:00
{
return U2F_SW_INSUFFICIENT_MEMORY;
}
2018-05-26 16:51:56 +00:00
crypto_sha256_init();
crypto_sha256_update(i,1);
crypto_sha256_update(req->app,32);
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
crypto_sha256_update(req->chal,32);
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
crypto_sha256_update((uint8_t*)&key_handle,U2F_KEY_HANDLE_SIZE);
crypto_sha256_update(i+1,1);
crypto_sha256_update(pubkey,64);
crypto_sha256_final(hash);
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
crypto_ecc256_load_attestation_key();
2018-05-26 19:29:37 +00:00
printf1(TAG_U2F, "sha256: "); dump_hex1(TAG_U2F,hash,32);
2018-05-26 16:51:56 +00:00
crypto_ecc256_sign(hash, 32, sig);
2018-05-26 15:36:41 +00:00
i[0] = 0x5;
u2f_response_writeback(i,2);
u2f_response_writeback(pubkey,64);
i[0] = U2F_KEY_HANDLE_SIZE;
u2f_response_writeback(i,1);
2018-05-26 16:51:56 +00:00
u2f_response_writeback((uint8_t*)&key_handle,U2F_KEY_HANDLE_SIZE);
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
u2f_response_writeback(attestation_cert_der,attest_size);
2018-05-26 15:36:41 +00:00
2018-05-26 19:29:37 +00:00
dump_signature_der(sig);
/*printf1(TAG_U2F, "dersig: "); dump_hex1(TAG_U2F,sig,74);*/
2018-05-26 15:36:41 +00:00
return U2F_SW_NO_ERROR;
}
2019-01-06 06:19:14 +00:00
#endif
2018-05-26 15:36:41 +00:00
2018-09-13 02:32:15 +00:00
int16_t u2f_version()
2018-05-26 15:36:41 +00:00
{
2018-05-26 16:51:56 +00:00
const char version[] = "U2F_V2";
2018-06-02 19:48:29 +00:00
u2f_response_writeback((uint8_t*)version, sizeof(version)-1);
2018-05-26 15:36:41 +00:00
return U2F_SW_NO_ERROR;
}