Merge remote-tracking branch 'upstream/dev' into mrtd

This commit is contained in:
Chris van Marle
2022-10-11 22:14:40 +02:00
500 changed files with 10030 additions and 5753 deletions

View File

@@ -66,19 +66,28 @@ MfClassicDict* mf_classic_dict_alloc(MfClassicDictType dict_type) {
}
// Read total amount of keys
string_t next_line;
string_init(next_line);
FuriString* next_line;
next_line = furi_string_alloc();
while(true) {
if(!stream_read_line(dict->stream, next_line)) break;
if(string_get_char(next_line, 0) == '#') continue;
if(string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
if(!stream_read_line(dict->stream, next_line)) {
FURI_LOG_T(TAG, "No keys left in dict");
break;
}
furi_string_trim(next_line);
FURI_LOG_T(
TAG,
"Read line: %s, len: %d",
furi_string_get_cstr(next_line),
furi_string_size(next_line));
if(furi_string_get_char(next_line, 0) == '#') continue;
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN - 1) continue;
dict->total_keys++;
}
string_clear(next_line);
furi_string_free(next_line);
stream_rewind(dict->stream);
dict_loaded = true;
FURI_LOG_I(TAG, "Loaded dictionary with %d keys", dict->total_keys);
FURI_LOG_I(TAG, "Loaded dictionary with %ld keys", dict->total_keys);
} while(false);
if(!dict_loaded) {
@@ -99,20 +108,20 @@ void mf_classic_dict_free(MfClassicDict* dict) {
free(dict);
}
static void mf_classic_dict_int_to_str(uint8_t* key_int, string_t key_str) {
string_reset(key_str);
static void mf_classic_dict_int_to_str(uint8_t* key_int, FuriString* key_str) {
furi_string_reset(key_str);
for(size_t i = 0; i < 6; i++) {
string_cat_printf(key_str, "%02X", key_int[i]);
furi_string_cat_printf(key_str, "%02X", key_int[i]);
}
}
static void mf_classic_dict_str_to_int(string_t key_str, uint64_t* key_int) {
static void mf_classic_dict_str_to_int(FuriString* key_str, uint64_t* key_int) {
uint8_t key_byte_tmp;
*key_int = 0ULL;
for(uint8_t i = 0; i < 12; i += 2) {
args_char_to_hex(
string_get_char(key_str, i), string_get_char(key_str, i + 1), &key_byte_tmp);
furi_string_get_char(key_str, i), furi_string_get_char(key_str, i + 1), &key_byte_tmp);
*key_int |= (uint64_t)key_byte_tmp << 8 * (5 - i / 2);
}
}
@@ -130,17 +139,17 @@ bool mf_classic_dict_rewind(MfClassicDict* dict) {
return stream_rewind(dict->stream);
}
bool mf_classic_dict_get_next_key_str(MfClassicDict* dict, string_t key) {
bool mf_classic_dict_get_next_key_str(MfClassicDict* dict, FuriString* key) {
furi_assert(dict);
furi_assert(dict->stream);
bool key_read = false;
string_reset(key);
furi_string_reset(key);
while(!key_read) {
if(!stream_read_line(dict->stream, key)) break;
if(string_get_char(key, 0) == '#') continue;
if(string_size(key) != NFC_MF_CLASSIC_KEY_LEN) continue;
string_left(key, 12);
if(furi_string_get_char(key, 0) == '#') continue;
if(furi_string_size(key) != NFC_MF_CLASSIC_KEY_LEN) continue;
furi_string_left(key, 12);
key_read = true;
}
@@ -151,53 +160,53 @@ bool mf_classic_dict_get_next_key(MfClassicDict* dict, uint64_t* key) {
furi_assert(dict);
furi_assert(dict->stream);
string_t temp_key;
string_init(temp_key);
FuriString* temp_key;
temp_key = furi_string_alloc();
bool key_read = mf_classic_dict_get_next_key_str(dict, temp_key);
if(key_read) {
mf_classic_dict_str_to_int(temp_key, key);
}
string_clear(temp_key);
furi_string_free(temp_key);
return key_read;
}
bool mf_classic_dict_is_key_present_str(MfClassicDict* dict, string_t key) {
bool mf_classic_dict_is_key_present_str(MfClassicDict* dict, FuriString* key) {
furi_assert(dict);
furi_assert(dict->stream);
string_t next_line;
string_init(next_line);
FuriString* next_line;
next_line = furi_string_alloc();
bool key_found = false;
stream_rewind(dict->stream);
while(!key_found) {
if(!stream_read_line(dict->stream, next_line)) break;
if(string_get_char(next_line, 0) == '#') continue;
if(string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
string_left(next_line, 12);
if(!string_equal_p(key, next_line)) continue;
if(furi_string_get_char(next_line, 0) == '#') continue;
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
furi_string_left(next_line, 12);
if(!furi_string_equal(key, next_line)) continue;
key_found = true;
}
string_clear(next_line);
furi_string_free(next_line);
return key_found;
}
bool mf_classic_dict_is_key_present(MfClassicDict* dict, uint8_t* key) {
string_t temp_key;
FuriString* temp_key;
string_init(temp_key);
temp_key = furi_string_alloc();
mf_classic_dict_int_to_str(key, temp_key);
bool key_found = mf_classic_dict_is_key_present_str(dict, temp_key);
string_clear(temp_key);
furi_string_free(temp_key);
return key_found;
}
bool mf_classic_dict_add_key_str(MfClassicDict* dict, string_t key) {
bool mf_classic_dict_add_key_str(MfClassicDict* dict, FuriString* key) {
furi_assert(dict);
furi_assert(dict->stream);
string_cat_printf(key, "\n");
furi_string_cat_printf(key, "\n");
bool key_added = false;
do {
@@ -207,7 +216,7 @@ bool mf_classic_dict_add_key_str(MfClassicDict* dict, string_t key) {
key_added = true;
} while(false);
string_left(key, 12);
furi_string_left(key, 12);
return key_added;
}
@@ -215,35 +224,35 @@ bool mf_classic_dict_add_key(MfClassicDict* dict, uint8_t* key) {
furi_assert(dict);
furi_assert(dict->stream);
string_t temp_key;
string_init(temp_key);
FuriString* temp_key;
temp_key = furi_string_alloc();
mf_classic_dict_int_to_str(key, temp_key);
bool key_added = mf_classic_dict_add_key_str(dict, temp_key);
string_clear(temp_key);
furi_string_free(temp_key);
return key_added;
}
bool mf_classic_dict_get_key_at_index_str(MfClassicDict* dict, string_t key, uint32_t target) {
bool mf_classic_dict_get_key_at_index_str(MfClassicDict* dict, FuriString* key, uint32_t target) {
furi_assert(dict);
furi_assert(dict->stream);
string_t next_line;
FuriString* next_line;
uint32_t index = 0;
string_init(next_line);
string_reset(key);
next_line = furi_string_alloc();
furi_string_reset(key);
bool key_found = false;
while(!key_found) {
if(!stream_read_line(dict->stream, next_line)) break;
if(string_get_char(next_line, 0) == '#') continue;
if(string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
if(furi_string_get_char(next_line, 0) == '#') continue;
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
if(index++ != target) continue;
string_set_n(key, next_line, 0, 12);
furi_string_set_n(key, next_line, 0, 12);
key_found = true;
}
string_clear(next_line);
furi_string_free(next_line);
return key_found;
}
@@ -251,37 +260,37 @@ bool mf_classic_dict_get_key_at_index(MfClassicDict* dict, uint64_t* key, uint32
furi_assert(dict);
furi_assert(dict->stream);
string_t temp_key;
string_init(temp_key);
FuriString* temp_key;
temp_key = furi_string_alloc();
bool key_found = mf_classic_dict_get_key_at_index_str(dict, temp_key, target);
if(key_found) {
mf_classic_dict_str_to_int(temp_key, key);
}
string_clear(temp_key);
furi_string_free(temp_key);
return key_found;
}
bool mf_classic_dict_find_index_str(MfClassicDict* dict, string_t key, uint32_t* target) {
bool mf_classic_dict_find_index_str(MfClassicDict* dict, FuriString* key, uint32_t* target) {
furi_assert(dict);
furi_assert(dict->stream);
string_t next_line;
string_init(next_line);
FuriString* next_line;
next_line = furi_string_alloc();
bool key_found = false;
uint32_t index = 0;
stream_rewind(dict->stream);
while(!key_found) {
if(!stream_read_line(dict->stream, next_line)) break;
if(string_get_char(next_line, 0) == '#') continue;
if(string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
string_left(next_line, 12);
if(!string_equal_p(key, next_line)) continue;
if(furi_string_get_char(next_line, 0) == '#') continue;
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
furi_string_left(next_line, 12);
if(!furi_string_equal(key, next_line)) continue;
key_found = true;
*target = index;
}
string_clear(next_line);
furi_string_free(next_line);
return key_found;
}
@@ -289,12 +298,12 @@ bool mf_classic_dict_find_index(MfClassicDict* dict, uint8_t* key, uint32_t* tar
furi_assert(dict);
furi_assert(dict->stream);
string_t temp_key;
string_init(temp_key);
FuriString* temp_key;
temp_key = furi_string_alloc();
mf_classic_dict_int_to_str(key, temp_key);
bool key_found = mf_classic_dict_find_index_str(dict, temp_key, target);
string_clear(temp_key);
furi_string_free(temp_key);
return key_found;
}
@@ -302,15 +311,15 @@ bool mf_classic_dict_delete_index(MfClassicDict* dict, uint32_t target) {
furi_assert(dict);
furi_assert(dict->stream);
string_t next_line;
string_init(next_line);
FuriString* next_line;
next_line = furi_string_alloc();
uint32_t index = 0;
bool key_removed = false;
while(!key_removed) {
if(!stream_read_line(dict->stream, next_line)) break;
if(string_get_char(next_line, 0) == '#') continue;
if(string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
if(furi_string_get_char(next_line, 0) == '#') continue;
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
if(index++ != target) continue;
stream_seek(dict->stream, -NFC_MF_CLASSIC_KEY_LEN, StreamOffsetFromCurrent);
if(!stream_delete(dict->stream, NFC_MF_CLASSIC_KEY_LEN)) break;
@@ -318,6 +327,6 @@ bool mf_classic_dict_delete_index(MfClassicDict* dict, uint32_t target) {
key_removed = true;
}
string_clear(next_line);
furi_string_free(next_line);
return key_removed;
}

View File

@@ -48,11 +48,11 @@ bool mf_classic_dict_rewind(MfClassicDict* dict);
bool mf_classic_dict_is_key_present(MfClassicDict* dict, uint8_t* key);
bool mf_classic_dict_is_key_present_str(MfClassicDict* dict, string_t key);
bool mf_classic_dict_is_key_present_str(MfClassicDict* dict, FuriString* key);
bool mf_classic_dict_get_next_key(MfClassicDict* dict, uint64_t* key);
bool mf_classic_dict_get_next_key_str(MfClassicDict* dict, string_t key);
bool mf_classic_dict_get_next_key_str(MfClassicDict* dict, FuriString* key);
/** Get key at target offset as uint64_t
*
@@ -72,7 +72,7 @@ bool mf_classic_dict_get_key_at_index(MfClassicDict* dict, uint64_t* key, uint32
*
* @return true on success
*/
bool mf_classic_dict_get_key_at_index_str(MfClassicDict* dict, string_t key, uint32_t target);
bool mf_classic_dict_get_key_at_index_str(MfClassicDict* dict, FuriString* key, uint32_t target);
bool mf_classic_dict_add_key(MfClassicDict* dict, uint8_t* key);
@@ -83,11 +83,11 @@ bool mf_classic_dict_add_key(MfClassicDict* dict, uint8_t* key);
*
* @return true on success
*/
bool mf_classic_dict_add_key_str(MfClassicDict* dict, string_t key);
bool mf_classic_dict_add_key_str(MfClassicDict* dict, FuriString* key);
bool mf_classic_dict_find_index(MfClassicDict* dict, uint8_t* key, uint32_t* target);
bool mf_classic_dict_find_index_str(MfClassicDict* dict, string_t key, uint32_t* target);
bool mf_classic_dict_find_index_str(MfClassicDict* dict, FuriString* key, uint32_t* target);
/** Delete key at target offset
*

View File

@@ -91,10 +91,8 @@ void mfkey32_set_callback(Mfkey32* instance, Mfkey32ParseDataCallback callback,
}
static bool mfkey32_write_params(Mfkey32* instance, Mfkey32Params* params) {
string_t str;
string_init_printf(
str,
"Sector %d key %c cuid %08x nt0 %08x nr0 %08x ar0 %08x nt1 %08x nr1 %08x ar1 %08x\n",
FuriString* str = furi_string_alloc_printf(
"Sec %d key %c cuid %08lx nt0 %08lx nr0 %08lx ar0 %08lx nt1 %08lx nr1 %08lx ar1 %08lx\n",
params->sector,
params->key == MfClassicKeyA ? 'A' : 'B',
params->cuid,
@@ -105,7 +103,7 @@ static bool mfkey32_write_params(Mfkey32* instance, Mfkey32Params* params) {
params->nr1,
params->ar1);
bool write_success = stream_write_string(instance->file_stream, str);
string_clear(str);
furi_string_free(str);
return write_success;
}
@@ -199,14 +197,14 @@ void mfkey32_process_data(
}
}
uint16_t mfkey32_get_auth_sectors(string_t data_str) {
uint16_t mfkey32_get_auth_sectors(FuriString* data_str) {
furi_assert(data_str);
uint16_t nonces_num = 0;
Storage* storage = furi_record_open(RECORD_STORAGE);
Stream* file_stream = buffered_file_stream_alloc(storage);
string_t temp_str;
string_init(temp_str);
FuriString* temp_str;
temp_str = furi_string_alloc();
do {
if(!buffered_file_stream_open(
@@ -214,17 +212,17 @@ uint16_t mfkey32_get_auth_sectors(string_t data_str) {
break;
while(true) {
if(!stream_read_line(file_stream, temp_str)) break;
size_t uid_pos = string_search_str(temp_str, "cuid");
string_left(temp_str, uid_pos);
string_push_back(temp_str, '\n');
string_cat(data_str, temp_str);
size_t uid_pos = furi_string_search(temp_str, "cuid");
furi_string_left(temp_str, uid_pos);
furi_string_push_back(temp_str, '\n');
furi_string_cat(data_str, temp_str);
nonces_num++;
}
} while(false);
buffered_file_stream_close(file_stream);
stream_free(file_stream);
string_clear(temp_str);
furi_string_free(temp_str);
return nonces_num;
}

View File

@@ -1,7 +1,6 @@
#pragma once
#include <lib/nfc/protocols/mifare_classic.h>
#include <m-string.h>
typedef struct Mfkey32 Mfkey32;
@@ -24,4 +23,4 @@ void mfkey32_process_data(
void mfkey32_set_callback(Mfkey32* instance, Mfkey32ParseDataCallback callback, void* context);
uint16_t mfkey32_get_auth_sectors(string_t string);
uint16_t mfkey32_get_auth_sectors(FuriString* string);

View File

@@ -1,6 +1,5 @@
#include "nfc_debug_log.h"
#include <m-string.h>
#include <storage/storage.h>
#include <stream/buffered_file_stream.h>
@@ -10,7 +9,7 @@
struct NfcDebugLog {
Stream* file_stream;
string_t data_str;
FuriString* data_str;
};
NfcDebugLog* nfc_debug_log_alloc() {
@@ -30,7 +29,7 @@ NfcDebugLog* nfc_debug_log_alloc() {
free(instance);
instance = NULL;
} else {
string_init(instance->data_str);
instance->data_str = furi_string_alloc();
}
furi_record_close(RECORD_STORAGE);
@@ -44,7 +43,7 @@ void nfc_debug_log_free(NfcDebugLog* instance) {
buffered_file_stream_close(instance->file_stream);
stream_free(instance->file_stream);
string_clear(instance->data_str);
furi_string_free(instance->data_str);
free(instance);
}
@@ -61,12 +60,12 @@ void nfc_debug_log_process_data(
furi_assert(data);
UNUSED(crc_dropped);
string_printf(instance->data_str, "%lu %c:", furi_get_tick(), reader_to_tag ? 'R' : 'T');
furi_string_printf(instance->data_str, "%lu %c:", furi_get_tick(), reader_to_tag ? 'R' : 'T');
uint16_t data_len = len;
for(size_t i = 0; i < data_len; i++) {
string_cat_printf(instance->data_str, " %02x", data[i]);
furi_string_cat_printf(instance->data_str, " %02x", data[i]);
}
string_push_back(instance->data_str, '\n');
furi_string_push_back(instance->data_str, '\n');
stream_write_string(instance->file_stream, instance->data_str);
}

View File

@@ -1,5 +1,4 @@
#include "reader_analyzer.h"
#include <stream_buffer.h>
#include <lib/nfc/protocols/nfc_util.h>
#include <lib/nfc/protocols/mifare_classic.h>
#include <m-array.h>
@@ -26,7 +25,7 @@ struct ReaderAnalyzer {
FuriHalNfcDevData nfc_data;
bool alive;
StreamBufferHandle_t stream;
FuriStreamBuffer* stream;
FuriThread* thread;
ReaderAnalyzerParseDataCallback callback;
@@ -86,8 +85,8 @@ int32_t reader_analyzer_thread(void* context) {
ReaderAnalyzer* reader_analyzer = context;
uint8_t buffer[READER_ANALYZER_MAX_BUFF_SIZE] = {};
while(reader_analyzer->alive || !xStreamBufferIsEmpty(reader_analyzer->stream)) {
size_t ret = xStreamBufferReceive(
while(reader_analyzer->alive || !furi_stream_buffer_is_empty(reader_analyzer->stream)) {
size_t ret = furi_stream_buffer_receive(
reader_analyzer->stream, buffer, READER_ANALYZER_MAX_BUFF_SIZE, 50);
if(ret) {
reader_analyzer_parse(reader_analyzer, buffer, ret);
@@ -103,7 +102,7 @@ ReaderAnalyzer* reader_analyzer_alloc() {
instance->nfc_data = reader_analyzer_nfc_data[ReaderAnalyzerNfcDataMfClassic];
instance->alive = false;
instance->stream =
xStreamBufferCreate(READER_ANALYZER_MAX_BUFF_SIZE, sizeof(ReaderAnalyzerHeader));
furi_stream_buffer_alloc(READER_ANALYZER_MAX_BUFF_SIZE, sizeof(ReaderAnalyzerHeader));
instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "ReaderAnalyzerWorker");
@@ -129,7 +128,7 @@ static void reader_analyzer_mfkey_callback(Mfkey32Event event, void* context) {
void reader_analyzer_start(ReaderAnalyzer* instance, ReaderAnalyzerMode mode) {
furi_assert(instance);
xStreamBufferReset(instance->stream);
furi_stream_buffer_reset(instance->stream);
if(mode & ReaderAnalyzerModeDebugLog) {
instance->debug_log = nfc_debug_log_alloc();
}
@@ -171,7 +170,7 @@ void reader_analyzer_free(ReaderAnalyzer* instance) {
reader_analyzer_stop(instance);
furi_thread_free(instance->thread);
vStreamBufferDelete(instance->stream);
furi_stream_buffer_free(instance->stream);
free(instance);
}
@@ -215,12 +214,12 @@ static void reader_analyzer_write(
ReaderAnalyzerHeader header = {
.reader_to_tag = reader_to_tag, .crc_dropped = crc_dropped, .len = len};
size_t data_sent = 0;
data_sent = xStreamBufferSend(
data_sent = furi_stream_buffer_send(
instance->stream, &header, sizeof(ReaderAnalyzerHeader), FuriWaitForever);
if(data_sent != sizeof(ReaderAnalyzerHeader)) {
FURI_LOG_W(TAG, "Sent %d out of %d bytes", data_sent, sizeof(ReaderAnalyzerHeader));
}
data_sent = xStreamBufferSend(instance->stream, data, len, FuriWaitForever);
data_sent = furi_stream_buffer_send(instance->stream, data, len, FuriWaitForever);
if(data_sent != len) {
FURI_LOG_W(TAG, "Sent %d out of %d bytes", data_sent, len);
}