proxmark3/client/fido/cbortools.c

490 lines
13 KiB
C
Raw Normal View History

2018-11-17 00:33:10 +08:00
//-----------------------------------------------------------------------------
// Copyright (C) 2018 Merlok
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Tools for work with CBOR format http://cbor.io/spec.html
// via Intel tinycbor (https://github.com/intel/tinycbor) library
//-----------------------------------------------------------------------------
//
#include "cbortools.h"
2018-11-17 01:47:41 +08:00
#include <stdlib.h>
2018-11-22 01:46:09 +08:00
#include "emv/emvjson.h"
2018-11-17 01:47:41 +08:00
#include "util.h"
2018-11-17 22:19:09 +08:00
#include "fidocore.h"
2018-11-17 00:33:10 +08:00
static void indent(int nestingLevel) {
2019-03-10 06:35:06 +08:00
while (nestingLevel--)
printf(" ");
2018-11-17 01:47:41 +08:00
}
static CborError dumpelm(CborValue *it, bool *got_next, int nestingLevel) {
2019-03-10 06:35:06 +08:00
CborError err;
*got_next = false;
CborType type = cbor_value_get_type(it);
indent(nestingLevel);
switch (type) {
2019-03-10 07:00:59 +08:00
case CborMapType:
case CborArrayType: {
printf(type == CborArrayType ? "Array[" : "Map[");
break;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborIntegerType: {
int64_t val;
cbor_value_get_int64(it, &val); // can't fail
printf("%lld", (long long)val);
break;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborByteStringType: {
uint8_t *buf;
size_t n;
err = cbor_value_dup_byte_string(it, &buf, &n, it);
*got_next = true;
if (err)
return err; // parse error
printf("%s", sprint_hex(buf, n));
free(buf);
break;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborTextStringType: {
char *buf;
size_t n;
err = cbor_value_dup_text_string(it, &buf, &n, it);
*got_next = true;
if (err)
return err; // parse error
printf("%s", buf);
free(buf);
break;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborTagType: {
CborTag tag;
cbor_value_get_tag(it, &tag);
printf("Tag(%lld)", (long long)tag);
break;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborSimpleType: {
uint8_t type;
cbor_value_get_simple_type(it, &type);
printf("simple(%u)", type);
break;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborNullType:
printf("null");
break;
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborUndefinedType:
printf("undefined");
break;
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborBooleanType: {
bool val;
cbor_value_get_boolean(it, &val); // can't fail
printf("%s", val ? "true" : "false");
break;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
case CborDoubleType: {
double val;
if (false) {
float f;
case CborFloatType:
cbor_value_get_float(it, &f);
val = f;
} else {
cbor_value_get_double(it, &val);
}
printf("%g", val);
break;
}
case CborHalfFloatType: {
uint16_t val;
cbor_value_get_half_float(it, &val);
printf("__f16(%04x)", val);
break;
2019-03-10 06:35:06 +08:00
}
2019-03-10 07:00:59 +08:00
case CborInvalidType:
printf("CborInvalidType!!!");
break;
2019-03-10 06:35:06 +08:00
}
return CborNoError;
2018-11-17 01:47:41 +08:00
}
static CborError dumprecursive(uint8_t cmdCode, bool isResponse, CborValue *it, bool isMapType, int nestingLevel) {
2019-03-10 06:35:06 +08:00
int elmCount = 0;
while (!cbor_value_at_end(it)) {
CborError err;
CborType type = cbor_value_get_type(it);
2018-11-17 01:47:41 +08:00
//printf("^%x^", type);
2019-03-10 06:35:06 +08:00
bool got_next;
switch (type) {
2019-03-10 07:00:59 +08:00
case CborMapType:
case CborArrayType: {
// recursive type
CborValue recursed;
assert(cbor_value_is_container(it));
if (!(isMapType && (elmCount % 2)))
indent(nestingLevel);
printf(type == CborArrayType ? "Array[\n" : "Map[\n");
err = cbor_value_enter_container(it, &recursed);
if (err)
return err; // parse error
err = dumprecursive(cmdCode, isResponse, &recursed, (type == CborMapType), nestingLevel + 1);
if (err)
return err; // parse error
err = cbor_value_leave_container(it, &recursed);
if (err)
return err; // parse error
2019-03-10 06:35:06 +08:00
indent(nestingLevel);
2019-03-10 07:00:59 +08:00
printf("]");
got_next = true;
break;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
default: {
err = dumpelm(it, &got_next, (isMapType && (elmCount % 2)) ? 0 : nestingLevel);
if (err)
return err;
if (cmdCode > 0 && nestingLevel == 1 && isMapType && !(elmCount % 2)) {
int64_t val;
cbor_value_get_int64(it, &val);
const char *desc = fido2GetCmdMemberDescription(cmdCode, isResponse, val);
2019-03-10 07:00:59 +08:00
if (desc)
printf(" (%s)", desc);
}
break;
2019-03-10 06:35:06 +08:00
}
}
if (!got_next) {
err = cbor_value_advance_fixed(it);
if (err)
return err;
}
if (isMapType && !(elmCount % 2)) {
printf(": ");
} else {
printf("\n");
}
elmCount++;
}
return CborNoError;
2018-11-17 01:47:41 +08:00
}
static int TinyCborInit(uint8_t *data, size_t length, CborValue *cb) {
2019-03-10 06:35:06 +08:00
CborParser parser;
CborError err = cbor_parser_init(data, length, 0, &parser, cb);
2018-11-17 01:47:41 +08:00
if (err)
2019-03-10 06:35:06 +08:00
return err;
2019-03-10 06:35:06 +08:00
return 0;
2018-11-17 00:33:10 +08:00
}
int TinyCborPrintFIDOPackage(uint8_t cmdCode, bool isResponse, uint8_t *data, size_t length) {
2019-03-10 06:35:06 +08:00
CborValue cb;
int res;
res = TinyCborInit(data, length, &cb);
if (res)
return res;
2018-11-22 02:11:41 +08:00
CborError err = dumprecursive(cmdCode, isResponse, &cb, false, 0);
2018-11-17 01:47:41 +08:00
2019-03-10 06:35:06 +08:00
if (err) {
fprintf(stderr,
2019-03-10 07:00:59 +08:00
"CBOR parsing failure at offset %" PRId32 " : %s\n",
(uint32_t)(cb.ptr - data),
cbor_error_string(err)
2019-04-28 17:09:46 +08:00
);
2019-03-10 06:35:06 +08:00
return 1;
}
2019-03-10 06:35:06 +08:00
return 0;
2018-11-22 01:46:09 +08:00
}
static int JsonObjElmCount(json_t *elm) {
2019-03-10 06:35:06 +08:00
int res = 0;
const char *key;
json_t *value;
2019-03-10 06:35:06 +08:00
if (!json_is_object(elm))
return 0;
2019-03-10 06:35:06 +08:00
json_object_foreach(elm, key, value) {
if (strlen(key) > 0 && key[0] != '.')
res++;
}
2019-03-10 06:35:06 +08:00
return res;
2018-11-22 01:46:09 +08:00
}
int JsonToCbor(json_t *elm, CborEncoder *encoder) {
2019-03-10 06:35:06 +08:00
if (!elm || !encoder)
return 1;
int res;
// CBOR map == JSON object
if (json_is_object(elm)) {
CborEncoder map;
const char *key;
json_t *value;
res = cbor_encoder_create_map(encoder, &map, JsonObjElmCount(elm));
cbor_check(res);
json_object_foreach(elm, key, value) {
if (strlen(key) > 0 && key[0] != '.') {
res = cbor_encode_text_stringz(&map, key);
cbor_check(res);
// RECURSION!
JsonToCbor(value, &map);
}
}
res = cbor_encoder_close_container(encoder, &map);
cbor_check(res);
}
// CBOR array == JSON array
if (json_is_array(elm)) {
size_t index;
json_t *value;
CborEncoder array;
res = cbor_encoder_create_array(encoder, &array, json_array_size(elm));
cbor_check(res);
json_array_foreach(elm, index, value) {
// RECURSION!
JsonToCbor(value, &array);
}
res = cbor_encoder_close_container(encoder, &array);
cbor_check(res);
}
if (json_is_boolean(elm)) {
res = cbor_encode_boolean(encoder, json_is_true(elm));
cbor_check(res);
}
if (json_is_integer(elm)) {
res = cbor_encode_int(encoder, json_integer_value(elm));
cbor_check(res);
}
if (json_is_real(elm)) {
res = cbor_encode_float(encoder, json_real_value(elm));
cbor_check(res);
}
if (json_is_string(elm)) {
2019-03-10 07:00:59 +08:00
const char *val = json_string_value(elm);
2019-03-10 06:35:06 +08:00
if (CheckStringIsHEXValue(val)) {
size_t datalen = 0;
uint8_t data[4096] = {0};
res = JsonLoadBufAsHex(elm, "$", data, sizeof(data), &datalen);
if (res)
return 100;
res = cbor_encode_byte_string(encoder, data, datalen);
cbor_check(res);
} else {
res = cbor_encode_text_stringz(encoder, val);
cbor_check(res);
}
}
return 0;
2018-11-17 00:33:10 +08:00
}
2018-11-24 01:47:30 +08:00
int CborMapGetKeyById(CborParser *parser, CborValue *map, uint8_t *data, size_t dataLen, int key) {
2019-03-10 06:35:06 +08:00
CborValue cb;
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
CborError err = cbor_parser_init(data, dataLen, 0, parser, &cb);
cbor_check(err);
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
if (cbor_value_get_type(&cb) != CborMapType)
return 1;
2019-03-10 06:35:06 +08:00
err = cbor_value_enter_container(&cb, map);
cbor_check(err);
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
int64_t indx;
while (!cbor_value_at_end(map)) {
// check number
if (cbor_value_get_type(map) != CborIntegerType)
return 1;
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
cbor_value_get_int64(map, &indx);
2019-03-10 06:35:06 +08:00
err = cbor_value_advance(map);
cbor_check(err);
2019-03-10 06:35:06 +08:00
if (indx == key)
return 0;
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
// pass value
err = cbor_value_advance(map);
cbor_check(err);
}
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
err = cbor_value_leave_container(&cb, map);
cbor_check(err);
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
return 2;
2018-11-24 01:47:30 +08:00
}
CborError CborGetArrayBinStringValue(CborValue *elm, uint8_t *data, size_t maxdatalen, size_t *datalen) {
2019-03-10 06:35:06 +08:00
return CborGetArrayBinStringValueEx(elm, data, maxdatalen, datalen, NULL, 0);
}
CborError CborGetArrayBinStringValueEx(CborValue *elm, uint8_t *data, size_t maxdatalen, size_t *datalen, uint8_t *delimeter, size_t delimeterlen) {
2019-03-10 06:35:06 +08:00
CborValue array;
if (datalen)
*datalen = 0;
2019-03-10 06:35:06 +08:00
size_t slen = maxdatalen;
size_t totallen = 0;
2018-11-24 20:57:36 +08:00
2019-03-10 06:35:06 +08:00
CborError res = cbor_value_enter_container(elm, &array);
cbor_check(res);
2019-03-10 06:35:06 +08:00
while (!cbor_value_at_end(&array)) {
res = cbor_value_copy_byte_string(&array, &data[totallen], &slen, &array);
cbor_check(res);
2019-03-10 06:35:06 +08:00
totallen += slen;
if (delimeter) {
memcpy(&data[totallen], delimeter, delimeterlen);
totallen += delimeterlen;
}
slen = maxdatalen - totallen;
}
2018-11-24 20:57:36 +08:00
2019-03-10 06:35:06 +08:00
res = cbor_value_leave_container(elm, &array);
cbor_check(res);
2018-11-24 20:57:36 +08:00
2019-03-10 06:35:06 +08:00
if (datalen)
*datalen = totallen;
2018-11-24 20:57:36 +08:00
2019-03-10 06:35:06 +08:00
return CborNoError;
2018-11-24 20:57:36 +08:00
};
CborError CborGetBinStringValue(CborValue *elm, uint8_t *data, size_t maxdatalen, size_t *datalen) {
2019-03-10 06:35:06 +08:00
if (datalen)
*datalen = 0;
2019-03-10 06:35:06 +08:00
size_t slen = maxdatalen;
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
CborError res = cbor_value_copy_byte_string(elm, data, &slen, elm);
cbor_check(res);
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
if (datalen)
*datalen = slen;
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
return CborNoError;
2018-11-24 01:47:30 +08:00
};
CborError CborGetArrayStringValue(CborValue *elm, char *data, size_t maxdatalen, size_t *datalen, char *delimeter) {
2019-03-10 06:35:06 +08:00
CborValue array;
if (datalen)
*datalen = 0;
2019-03-10 06:35:06 +08:00
size_t slen = maxdatalen;
size_t totallen = 0;
2019-03-10 06:35:06 +08:00
CborError res = cbor_value_enter_container(elm, &array);
cbor_check(res);
2019-03-10 06:35:06 +08:00
while (!cbor_value_at_end(&array)) {
res = cbor_value_copy_text_string(&array, &data[totallen], &slen, &array);
cbor_check(res);
2019-03-10 06:35:06 +08:00
totallen += slen;
if (delimeter) {
strcat(data, delimeter);
totallen += strlen(delimeter);
}
slen = maxdatalen - totallen;
data[totallen] = 0x00;
}
2019-03-10 06:35:06 +08:00
res = cbor_value_leave_container(elm, &array);
cbor_check(res);
2019-03-10 06:35:06 +08:00
if (datalen)
*datalen = totallen;
2019-03-10 06:35:06 +08:00
return CborNoError;
};
CborError CborGetStringValue(CborValue *elm, char *data, size_t maxdatalen, size_t *datalen) {
2019-03-10 06:35:06 +08:00
if (datalen)
*datalen = 0;
2019-03-10 06:35:06 +08:00
size_t slen = maxdatalen;
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
CborError res = cbor_value_copy_text_string(elm, data, &slen, elm);
cbor_check(res);
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
if (datalen)
*datalen = slen;
2018-11-24 01:47:30 +08:00
2019-03-10 06:35:06 +08:00
return CborNoError;
2018-11-24 01:47:30 +08:00
};
CborError CborGetStringValueBuf(CborValue *elm) {
2019-03-10 06:35:06 +08:00
static char stringBuf[2048];
memset(stringBuf, 0x00, sizeof(stringBuf));
2019-03-10 06:35:06 +08:00
return CborGetStringValue(elm, stringBuf, sizeof(stringBuf), NULL);
2018-11-25 06:12:06 +08:00
};
2018-11-27 18:28:36 +08:00
int CBOREncodeElm(json_t *root, const char *rootElmId, CborEncoder *encoder) {
2019-03-10 06:35:06 +08:00
json_t *elm = NULL;
if (rootElmId && strlen(rootElmId) && rootElmId[0] == '$')
elm = json_path_get(root, rootElmId);
else
elm = json_object_get(root, rootElmId);
2019-03-10 06:35:06 +08:00
if (!elm)
return 1;
2019-03-10 06:35:06 +08:00
int res = JsonToCbor(elm, encoder);
2018-11-27 18:28:36 +08:00
2019-03-10 06:35:06 +08:00
return res;
2018-11-27 18:28:36 +08:00
}
2018-11-27 18:34:28 +08:00
CborError CBOREncodeClientDataHash(json_t *root, CborEncoder *encoder) {
2019-03-10 06:35:06 +08:00
uint8_t buf[100] = {0};
size_t jlen;
2018-11-27 18:34:28 +08:00
2019-03-10 06:35:06 +08:00
JsonLoadBufAsHex(root, "$.ClientDataHash", buf, sizeof(buf), &jlen);
2019-03-10 06:35:06 +08:00
// fill with 0x00 if not found
if (!jlen)
jlen = 32;
2019-03-10 06:35:06 +08:00
int res = cbor_encode_byte_string(encoder, buf, jlen);
cbor_check(res);
2018-11-27 18:34:28 +08:00
2019-03-10 06:35:06 +08:00
return 0;
2018-11-27 18:34:28 +08:00
}