2021-12-22 23:04:08 +03:00
|
|
|
#include <furi.h>
|
2022-08-17 18:40:06 +03:00
|
|
|
#include "u2f_data.h"
|
2022-01-05 19:10:18 +03:00
|
|
|
#include <furi_hal.h>
|
2021-12-22 23:04:08 +03:00
|
|
|
#include <storage/storage.h>
|
2022-01-05 19:10:18 +03:00
|
|
|
#include <furi_hal_random.h>
|
2022-02-18 22:53:46 +03:00
|
|
|
#include <flipper_format/flipper_format.h>
|
2021-12-22 23:04:08 +03:00
|
|
|
|
|
|
|
#define TAG "U2F"
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
#define U2F_DATA_FOLDER ANY_PATH("u2f/")
|
2022-02-02 18:58:52 +03:00
|
|
|
#define U2F_CERT_FILE U2F_DATA_FOLDER "assets/cert.der"
|
|
|
|
#define U2F_CERT_KEY_FILE U2F_DATA_FOLDER "assets/cert_key.u2f"
|
2021-12-22 23:04:08 +03:00
|
|
|
#define U2F_KEY_FILE U2F_DATA_FOLDER "key.u2f"
|
|
|
|
#define U2F_CNT_FILE U2F_DATA_FOLDER "cnt.u2f"
|
|
|
|
|
|
|
|
#define U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_FACTORY 2
|
|
|
|
#define U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE 11
|
|
|
|
|
|
|
|
#define U2F_CERT_STOCK 0 // Stock certificate, private key is encrypted with factory key
|
|
|
|
#define U2F_CERT_USER 1 // User certificate, private key is encrypted with unique key
|
2022-01-03 02:33:28 +03:00
|
|
|
#define U2F_CERT_USER_UNENCRYPTED \
|
|
|
|
2 // Unencrypted user certificate, will be encrypted after first load
|
2021-12-22 23:04:08 +03:00
|
|
|
|
|
|
|
#define U2F_CERT_KEY_FILE_TYPE "Flipper U2F Certificate Key File"
|
|
|
|
#define U2F_CERT_KEY_VERSION 1
|
|
|
|
|
|
|
|
#define U2F_DEVICE_KEY_FILE_TYPE "Flipper U2F Device Key File"
|
|
|
|
#define U2F_DEVICE_KEY_VERSION 1
|
|
|
|
|
|
|
|
#define U2F_COUNTER_FILE_TYPE "Flipper U2F Counter File"
|
2022-08-17 18:40:06 +03:00
|
|
|
#define U2F_COUNTER_VERSION 2
|
|
|
|
#define U2F_COUNTER_VERSION_OLD 1
|
2021-12-22 23:04:08 +03:00
|
|
|
|
|
|
|
#define U2F_COUNTER_CONTROL_VAL 0xAA5500FF
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t counter;
|
|
|
|
uint8_t random_salt[24];
|
|
|
|
uint32_t control;
|
|
|
|
} __attribute__((packed)) U2fCounterData;
|
|
|
|
|
2022-02-07 16:34:09 +03:00
|
|
|
bool u2f_data_check(bool cert_only) {
|
2022-02-02 17:27:33 +03:00
|
|
|
bool state = false;
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2022-02-02 17:27:33 +03:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
|
|
|
|
2022-02-07 16:34:09 +03:00
|
|
|
do {
|
|
|
|
if(!storage_file_open(file, U2F_CERT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
|
2022-02-02 17:27:33 +03:00
|
|
|
storage_file_close(file);
|
2022-02-07 16:34:09 +03:00
|
|
|
if(!storage_file_open(file, U2F_CERT_KEY_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
|
|
|
|
if(cert_only) {
|
2022-02-02 17:27:33 +03:00
|
|
|
state = true;
|
2022-02-07 16:34:09 +03:00
|
|
|
break;
|
2022-02-02 17:27:33 +03:00
|
|
|
}
|
2022-02-07 16:34:09 +03:00
|
|
|
storage_file_close(file);
|
|
|
|
if(!storage_file_open(file, U2F_KEY_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
|
|
|
|
storage_file_close(file);
|
|
|
|
if(!storage_file_open(file, U2F_CNT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
|
|
|
|
state = true;
|
|
|
|
} while(0);
|
2022-02-02 17:27:33 +03:00
|
|
|
|
|
|
|
storage_file_close(file);
|
|
|
|
storage_file_free(file);
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-02-02 17:27:33 +03:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2021-12-22 23:04:08 +03:00
|
|
|
bool u2f_data_cert_check() {
|
|
|
|
bool state = false;
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2021-12-22 23:04:08 +03:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
|
|
|
uint8_t file_buf[8];
|
|
|
|
|
|
|
|
if(storage_file_open(file, U2F_CERT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
|
|
|
do {
|
|
|
|
// Read header to check certificate size
|
|
|
|
size_t file_size = storage_file_size(file);
|
|
|
|
size_t len_cur = storage_file_read(file, file_buf, 4);
|
|
|
|
if(len_cur != 4) break;
|
|
|
|
|
|
|
|
if(file_buf[0] != 0x30) {
|
|
|
|
FURI_LOG_E(TAG, "Wrong certificate header");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t temp_len = ((file_buf[2] << 8) | (file_buf[3])) + 4;
|
|
|
|
if(temp_len != file_size) {
|
|
|
|
FURI_LOG_E(TAG, "Wrong certificate length");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state = true;
|
|
|
|
} while(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
storage_file_close(file);
|
|
|
|
storage_file_free(file);
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t u2f_data_cert_load(uint8_t* cert) {
|
|
|
|
furi_assert(cert);
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2021-12-22 23:04:08 +03:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
|
|
|
uint32_t file_size = 0;
|
|
|
|
uint32_t len_cur = 0;
|
|
|
|
|
|
|
|
if(storage_file_open(file, U2F_CERT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
|
|
|
file_size = storage_file_size(file);
|
|
|
|
len_cur = storage_file_read(file, cert, file_size);
|
|
|
|
if(len_cur != file_size) len_cur = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
storage_file_close(file);
|
|
|
|
storage_file_free(file);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
|
|
|
return len_cur;
|
|
|
|
}
|
|
|
|
|
2022-01-03 02:33:28 +03:00
|
|
|
static bool u2f_data_cert_key_encrypt(uint8_t* cert_key) {
|
|
|
|
furi_assert(cert_key);
|
|
|
|
|
|
|
|
bool state = false;
|
|
|
|
uint8_t iv[16];
|
|
|
|
uint8_t key[48];
|
|
|
|
uint32_t cert_type = U2F_CERT_USER;
|
|
|
|
|
|
|
|
FURI_LOG_I(TAG, "Encrypting user cert key");
|
|
|
|
|
|
|
|
// Generate random IV
|
|
|
|
furi_hal_random_fill_buf(iv, 16);
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_encrypt(cert_key, key, 32)) {
|
|
|
|
FURI_LOG_E(TAG, "Encryption failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 22:53:46 +03:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2022-01-03 02:33:28 +03:00
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_file_open_always(flipper_format, U2F_CERT_KEY_FILE)) {
|
2022-01-03 02:33:28 +03:00
|
|
|
do {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_header_cstr(
|
|
|
|
flipper_format, U2F_CERT_KEY_FILE_TYPE, U2F_CERT_KEY_VERSION))
|
2022-01-03 02:33:28 +03:00
|
|
|
break;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_uint32(flipper_format, "Type", &cert_type, 1)) break;
|
|
|
|
if(!flipper_format_write_hex(flipper_format, "IV", iv, 16)) break;
|
|
|
|
if(!flipper_format_write_hex(flipper_format, "Data", key, 48)) break;
|
2022-01-03 02:33:28 +03:00
|
|
|
state = true;
|
|
|
|
} while(0);
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
flipper_format_free(flipper_format);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-01-03 02:33:28 +03:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2021-12-22 23:04:08 +03:00
|
|
|
bool u2f_data_cert_key_load(uint8_t* cert_key) {
|
|
|
|
furi_assert(cert_key);
|
|
|
|
|
|
|
|
bool state = false;
|
|
|
|
uint8_t iv[16];
|
|
|
|
uint8_t key[48];
|
|
|
|
uint32_t cert_type = 0;
|
|
|
|
uint8_t key_slot = 0;
|
|
|
|
uint32_t version = 0;
|
|
|
|
|
|
|
|
// Check if unique key exists in secure eclave and generate it if missing
|
|
|
|
if(!furi_hal_crypto_verify_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE)) return false;
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* filetype;
|
|
|
|
filetype = furi_string_alloc();
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 22:53:46 +03:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_file_open_existing(flipper_format, U2F_CERT_KEY_FILE)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
do {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_header(flipper_format, filetype, &version)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing or incorrect header");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
if(strcmp(furi_string_get_cstr(filetype), U2F_CERT_KEY_FILE_TYPE) != 0 ||
|
2021-12-22 23:04:08 +03:00
|
|
|
version != U2F_CERT_KEY_VERSION) {
|
|
|
|
FURI_LOG_E(TAG, "Type or version mismatch");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_uint32(flipper_format, "Type", &cert_type, 1)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing cert type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cert_type == U2F_CERT_STOCK) {
|
|
|
|
key_slot = U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_FACTORY;
|
|
|
|
} else if(cert_type == U2F_CERT_USER) {
|
|
|
|
key_slot = U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE;
|
2022-01-03 02:33:28 +03:00
|
|
|
} else if(cert_type == U2F_CERT_USER_UNENCRYPTED) {
|
|
|
|
key_slot = 0;
|
2021-12-22 23:04:08 +03:00
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Unknown cert type");
|
|
|
|
break;
|
|
|
|
}
|
2022-01-03 02:33:28 +03:00
|
|
|
if(key_slot != 0) {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
|
2022-01-03 02:33:28 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing IV");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "Data", key, 48)) {
|
2022-01-03 02:33:28 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing data");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
|
|
|
break;
|
|
|
|
}
|
2021-12-22 23:04:08 +03:00
|
|
|
memset(cert_key, 0, 32);
|
2022-01-03 02:33:28 +03:00
|
|
|
|
|
|
|
if(!furi_hal_crypto_decrypt(key, cert_key, 32)) {
|
|
|
|
memset(cert_key, 0, 32);
|
|
|
|
FURI_LOG_E(TAG, "Decryption failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
furi_hal_crypto_store_unload_key(key_slot);
|
|
|
|
} else {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "Data", cert_key, 32)) {
|
2022-01-03 02:33:28 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing data");
|
|
|
|
break;
|
|
|
|
}
|
2021-12-22 23:04:08 +03:00
|
|
|
}
|
|
|
|
state = true;
|
|
|
|
} while(0);
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
flipper_format_free(flipper_format);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(filetype);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-01-03 02:33:28 +03:00
|
|
|
if(cert_type == U2F_CERT_USER_UNENCRYPTED) {
|
|
|
|
return u2f_data_cert_key_encrypt(cert_key);
|
|
|
|
}
|
|
|
|
|
2021-12-22 23:04:08 +03:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool u2f_data_key_load(uint8_t* device_key) {
|
|
|
|
furi_assert(device_key);
|
|
|
|
|
|
|
|
bool state = false;
|
|
|
|
uint8_t iv[16];
|
|
|
|
uint8_t key[48];
|
|
|
|
uint32_t version = 0;
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* filetype;
|
|
|
|
filetype = furi_string_alloc();
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 22:53:46 +03:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_file_open_existing(flipper_format, U2F_KEY_FILE)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
do {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_header(flipper_format, filetype, &version)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing or incorrect header");
|
|
|
|
break;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(strcmp(furi_string_get_cstr(filetype), U2F_DEVICE_KEY_FILE_TYPE) != 0 ||
|
2021-12-22 23:04:08 +03:00
|
|
|
version != U2F_DEVICE_KEY_VERSION) {
|
|
|
|
FURI_LOG_E(TAG, "Type or version mismatch");
|
|
|
|
break;
|
|
|
|
}
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing IV");
|
|
|
|
break;
|
|
|
|
}
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "Data", key, 48)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing data");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memset(device_key, 0, 32);
|
|
|
|
if(!furi_hal_crypto_decrypt(key, device_key, 32)) {
|
|
|
|
memset(device_key, 0, 32);
|
|
|
|
FURI_LOG_E(TAG, "Decryption failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
|
|
|
|
state = true;
|
|
|
|
} while(0);
|
|
|
|
}
|
2022-02-18 22:53:46 +03:00
|
|
|
flipper_format_free(flipper_format);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(filetype);
|
2021-12-22 23:04:08 +03:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool u2f_data_key_generate(uint8_t* device_key) {
|
|
|
|
furi_assert(device_key);
|
|
|
|
|
|
|
|
bool state = false;
|
|
|
|
uint8_t iv[16];
|
|
|
|
uint8_t key[32];
|
|
|
|
uint8_t key_encrypted[48];
|
|
|
|
|
|
|
|
// Generate random IV and key
|
|
|
|
furi_hal_random_fill_buf(iv, 16);
|
|
|
|
furi_hal_random_fill_buf(key, 32);
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_encrypt(key, key_encrypted, 32)) {
|
|
|
|
FURI_LOG_E(TAG, "Encryption failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 22:53:46 +03:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_file_open_always(flipper_format, U2F_KEY_FILE)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
do {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_header_cstr(
|
|
|
|
flipper_format, U2F_DEVICE_KEY_FILE_TYPE, U2F_DEVICE_KEY_VERSION))
|
2021-12-22 23:04:08 +03:00
|
|
|
break;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_hex(flipper_format, "IV", iv, 16)) break;
|
|
|
|
if(!flipper_format_write_hex(flipper_format, "Data", key_encrypted, 48)) break;
|
2021-12-22 23:04:08 +03:00
|
|
|
state = true;
|
|
|
|
memcpy(device_key, key, 32);
|
|
|
|
} while(0);
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
flipper_format_free(flipper_format);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool u2f_data_cnt_read(uint32_t* cnt_val) {
|
|
|
|
furi_assert(cnt_val);
|
|
|
|
|
|
|
|
bool state = false;
|
2022-08-17 18:40:06 +03:00
|
|
|
bool old_counter = false;
|
2021-12-22 23:04:08 +03:00
|
|
|
uint8_t iv[16];
|
|
|
|
U2fCounterData cnt;
|
|
|
|
uint8_t cnt_encr[48];
|
|
|
|
uint32_t version = 0;
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* filetype;
|
|
|
|
filetype = furi_string_alloc();
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 22:53:46 +03:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_file_open_existing(flipper_format, U2F_CNT_FILE)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
do {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_header(flipper_format, filetype, &version)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing or incorrect header");
|
|
|
|
break;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(strcmp(furi_string_get_cstr(filetype), U2F_COUNTER_FILE_TYPE) != 0) {
|
2022-08-17 18:40:06 +03:00
|
|
|
FURI_LOG_E(TAG, "Type mismatch");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(version == U2F_COUNTER_VERSION_OLD) {
|
|
|
|
// Counter is from previous U2F app version with endianness bug
|
|
|
|
FURI_LOG_W(TAG, "Counter from old version");
|
|
|
|
old_counter = true;
|
|
|
|
} else if(version != U2F_COUNTER_VERSION) {
|
|
|
|
FURI_LOG_E(TAG, "Version mismatch");
|
2021-12-22 23:04:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing IV");
|
|
|
|
break;
|
|
|
|
}
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "Data", cnt_encr, 48)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
FURI_LOG_E(TAG, "Missing data");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memset(&cnt, 0, 32);
|
|
|
|
if(!furi_hal_crypto_decrypt(cnt_encr, (uint8_t*)&cnt, 32)) {
|
|
|
|
memset(&cnt, 0, 32);
|
|
|
|
FURI_LOG_E(TAG, "Decryption failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
|
|
|
|
if(cnt.control == U2F_COUNTER_CONTROL_VAL) {
|
|
|
|
*cnt_val = cnt.counter;
|
|
|
|
state = true;
|
|
|
|
}
|
|
|
|
} while(0);
|
|
|
|
}
|
2022-02-18 22:53:46 +03:00
|
|
|
flipper_format_free(flipper_format);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(filetype);
|
2022-08-17 18:40:06 +03:00
|
|
|
|
|
|
|
if(old_counter && state) {
|
|
|
|
// Change counter endianness and rewrite counter file
|
|
|
|
*cnt_val = __REV(cnt.counter);
|
|
|
|
state = u2f_data_cnt_write(*cnt_val);
|
|
|
|
}
|
|
|
|
|
2021-12-22 23:04:08 +03:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool u2f_data_cnt_write(uint32_t cnt_val) {
|
|
|
|
bool state = false;
|
|
|
|
uint8_t iv[16];
|
|
|
|
U2fCounterData cnt;
|
|
|
|
uint8_t cnt_encr[48];
|
|
|
|
|
|
|
|
// Generate random IV and key
|
|
|
|
furi_hal_random_fill_buf(iv, 16);
|
|
|
|
furi_hal_random_fill_buf(cnt.random_salt, 24);
|
|
|
|
cnt.control = U2F_COUNTER_CONTROL_VAL;
|
|
|
|
cnt.counter = cnt_val;
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_store_load_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE, iv)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_encrypt((uint8_t*)&cnt, cnt_encr, 32)) {
|
|
|
|
FURI_LOG_E(TAG, "Encryption failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
furi_hal_crypto_store_unload_key(U2F_DATA_FILE_ENCRYPTION_KEY_SLOT_UNIQUE);
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 22:53:46 +03:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_file_open_always(flipper_format, U2F_CNT_FILE)) {
|
2021-12-22 23:04:08 +03:00
|
|
|
do {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_header_cstr(
|
|
|
|
flipper_format, U2F_COUNTER_FILE_TYPE, U2F_COUNTER_VERSION))
|
2021-12-22 23:04:08 +03:00
|
|
|
break;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_hex(flipper_format, "IV", iv, 16)) break;
|
|
|
|
if(!flipper_format_write_hex(flipper_format, "Data", cnt_encr, 48)) break;
|
2021-12-22 23:04:08 +03:00
|
|
|
state = true;
|
|
|
|
} while(0);
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
flipper_format_free(flipper_format);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-12-22 23:04:08 +03:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|