solo1/fido2/u2f.c

338 wiersze
8.8 KiB
C
Czysty Zwykły widok Historia

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"
2018-07-12 03:00:53 +00:00
#include "wallet.h"
2018-07-08 02:43:06 +00:00
#include "app.h"
2018-05-26 15:36:41 +00:00
// void u2f_response_writeback(uint8_t * buf, uint8_t len);
static int16_t u2f_register(struct u2f_register_request * req);
static int16_t u2f_version();
static int16_t u2f_authenticate(struct u2f_authenticate_request * req, uint8_t control);
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;
void u2f_request(struct u2f_request_apdu* req, CTAP_RESPONSE * resp)
2018-05-26 15:36:41 +00:00
{
2018-05-26 19:29:37 +00:00
uint16_t rcode;
2018-06-02 22:30:59 +00:00
uint64_t t1,t2;
2018-05-26 15:36:41 +00:00
uint32_t len = ((req->LC3) | ((uint32_t)req->LC2 << 8) | ((uint32_t)req->LC1 << 16));
2018-05-26 19:29:37 +00:00
uint8_t byte;
_u2f_resp = 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-03 04:23:10 +00:00
#if defined(BRIDGE_TO_WALLET) || defined(IS_BOOTLOADER)
2018-07-09 02:36:16 +00:00
struct u2f_authenticate_request * auth = (struct u2f_authenticate_request *) req->payload;
2018-07-15 03:03:25 +00:00
2018-07-08 02:43:06 +00:00
if (req->ins == U2F_AUTHENTICATE)
{
if (req->p1 == U2F_AUTHENTICATE_CHECK)
{
2018-07-14 00:29:14 +00:00
if (is_wallet_device((uint8_t *) &auth->kh, auth->khl)) // Pin requests
2018-07-12 04:14:39 +00:00
{
rcode = U2F_SW_CONDITIONS_NOT_SATISFIED;
}
else
{
rcode = U2F_SW_WRONG_DATA;
}
2018-07-14 00:29:14 +00:00
printf1(TAG_WALLET,"Ignoring U2F request\n");
2018-07-12 04:14:39 +00:00
goto end;
2018-07-08 02:43:06 +00:00
}
else
{
2018-07-14 00:29:14 +00:00
if ( ! is_wallet_device((uint8_t *) &auth->kh, auth->khl)) // Pin requests
2018-07-12 04:14:39 +00:00
{
rcode = U2F_SW_WRONG_PAYLOAD;
2018-07-14 00:29:14 +00:00
printf1(TAG_WALLET,"Ignoring U2F request\n");
2018-07-12 04:14:39 +00:00
goto end;
}
2018-07-12 03:00:53 +00:00
rcode = bridge_u2f_to_wallet(auth->chal, auth->app, auth->khl, (uint8_t*)&auth->kh);
2018-07-08 02:43:06 +00:00
}
}
else if (req->ins == U2F_VERSION)
{
printf1(TAG_U2F, "U2F_VERSION\n");
if (len)
{
rcode = U2F_SW_WRONG_LENGTH;
}
else
{
rcode = u2f_version();
}
}
else
{
2018-07-12 03:00:53 +00:00
rcode = U2F_SW_INS_NOT_SUPPORTED;
2018-07-08 02:43:06 +00:00
}
2018-07-15 03:03:25 +00:00
2018-07-08 02:43:06 +00:00
#else
2018-05-26 15:36:41 +00:00
switch(req->ins)
{
case U2F_REGISTER:
2018-05-26 19:29:37 +00:00
printf1(TAG_U2F, "U2F_REGISTER\n");
2018-05-26 15:36:41 +00:00
if (len != 64)
{
2018-05-26 19:29:37 +00:00
rcode = U2F_SW_WRONG_LENGTH;
2018-05-26 15:36:41 +00:00
}
else
{
2018-06-02 22:30:59 +00:00
t1 = millis();
2018-05-26 19:29:37 +00:00
rcode = u2f_register((struct u2f_register_request*)req->payload);
2018-06-02 22:30:59 +00:00
t2 = millis();
printf1(TAG_TIME,"u2f_register time: %d ms\n", t2-t1);
2018-05-26 15:36:41 +00:00
}
break;
case U2F_AUTHENTICATE:
2018-05-26 19:29:37 +00:00
printf1(TAG_U2F, "U2F_AUTHENTICATE\n");
2018-06-02 22:30:59 +00:00
t1 = millis();
2018-05-26 19:29:37 +00:00
rcode = u2f_authenticate((struct u2f_authenticate_request*)req->payload, req->p1);
2018-06-02 22:30:59 +00:00
t2 = millis();
printf1(TAG_TIME,"u2f_authenticate time: %d ms\n", t2-t1);
2018-05-26 15:36:41 +00:00
break;
case U2F_VERSION:
2018-05-26 19:29:37 +00:00
printf1(TAG_U2F, "U2F_VERSION\n");
2018-05-26 15:36:41 +00:00
if (len)
{
2018-05-26 19:29:37 +00:00
rcode = U2F_SW_WRONG_LENGTH;
2018-05-26 15:36:41 +00:00
}
else
{
2018-05-26 19:29:37 +00:00
rcode = u2f_version();
2018-05-26 15:36:41 +00:00
}
break;
case U2F_VENDOR_FIRST:
case U2F_VENDOR_LAST:
2018-05-26 19:29:37 +00:00
printf1(TAG_U2F, "U2F_VENDOR\n");
rcode = U2F_SW_NO_ERROR;
2018-05-26 15:36:41 +00:00
break;
default:
2018-05-26 19:29:37 +00:00
printf1(TAG_ERR, "Error, unknown U2F command\n");
rcode = U2F_SW_INS_NOT_SUPPORTED;
2018-05-26 15:36:41 +00:00
break;
}
2018-07-08 02:43:06 +00:00
#endif
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
}
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
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);
}
2018-05-26 19:29:37 +00:00
static int8_t u2f_new_keypair(struct u2f_key_handle * kh, uint8_t * appid, uint8_t * pubkey)
{
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)
{
if (u2f_appid_eq(&req->kh, req->app) == 0)
{
return U2F_SW_CONDITIONS_NOT_SATISFIED;
}
else
{
return U2F_SW_WRONG_DATA;
}
}
if (
control != U2F_AUTHENTICATE_SIGN ||
req->khl != U2F_KEY_HANDLE_SIZE ||
u2f_appid_eq(&req->kh, req->app) != 0 || // Order of checks is important
u2f_load_key(&req->kh, req->app) != 0
)
{
return U2F_SW_WRONG_PAYLOAD;
}
2018-05-26 19:29:37 +00:00
if (ctap_user_presence_test() == 0)
2018-05-26 15:36:41 +00:00
{
return U2F_SW_CONDITIONS_NOT_SATISFIED;
}
2018-05-26 16:51:56 +00:00
count = ctap_atomic_count(0);
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
crypto_sha256_init();
2018-05-26 15:36:41 +00:00
2018-05-26 16:51:56 +00:00
crypto_sha256_update(req->app,32);
crypto_sha256_update(&up,1);
crypto_sha256_update((uint8_t *)&count,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);
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
u2f_response_writeback(&up,1);
u2f_response_writeback((uint8_t *)&count,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
2018-05-26 19:29:37 +00:00
if ( ! ctap_user_presence_test())
2018-05-26 15:36:41 +00:00
{
return U2F_SW_CONDITIONS_NOT_SATISFIED;
}
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
/*printf("check key handle size: %d vs %d\n", U2F_KEY_HANDLE_SIZE, sizeof(struct u2f_key_handle));*/
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;
}
static int16_t u2f_version()
{
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;
}