Fido U2F complete (#716)
[proxmark3-svn] / client / crypto / asn1utils.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 Merlok
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // asn.1 utils
9 //-----------------------------------------------------------------------------
10
11 #include "asn1utils.h"
12 #include <ctype.h>
13 #include <stdlib.h>
14 #include <mbedtls/asn1.h>
15 #include "emv/tlv.h"
16 #include "emv/dump.h"
17 #include "asn1dump.h"
18 #include "util.h"
19 #include "ui.h" // PrintAndLog
20
21 int ecdsa_asn1_get_signature(uint8_t *signature, size_t signaturelen, uint8_t *rval, uint8_t *sval) {
22 if (!signature || !signaturelen || !rval || !sval)
23 return 1;
24
25 int res = 0;
26 unsigned char *p = signature;
27 const unsigned char *end = p + signaturelen;
28 size_t len;
29 mbedtls_mpi xmpi;
30
31 if ((res = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) == 0) {
32 mbedtls_mpi_init(&xmpi);
33 res = mbedtls_asn1_get_mpi(&p, end, &xmpi);
34 if (res) {
35 mbedtls_mpi_free(&xmpi);
36 goto exit;
37 }
38
39 res = mbedtls_mpi_write_binary(&xmpi, rval, 32);
40 mbedtls_mpi_free(&xmpi);
41 if (res)
42 goto exit;
43
44 mbedtls_mpi_init(&xmpi);
45 res = mbedtls_asn1_get_mpi(&p, end, &xmpi);
46 if (res) {
47 mbedtls_mpi_free(&xmpi);
48 goto exit;
49 }
50
51 res = mbedtls_mpi_write_binary(&xmpi, sval, 32);
52 mbedtls_mpi_free(&xmpi);
53 if (res)
54 goto exit;
55
56 // check size
57 if (end != p)
58 return 2;
59 }
60
61 exit:
62 return res;
63 }
64
65 static bool print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) {
66 bool candump = true;
67 asn1_tag_dump(tlv, stdout, level, &candump);
68 if (is_leaf && candump) {
69 dump_buffer(tlv->value, tlv->len, stdout, level);
70 }
71
72 return true;
73 }
74
75 int asn1_print(uint8_t *asn1buf, size_t asn1buflen, char *indent) {
76
77 struct tlvdb *t = NULL;
78 t = tlvdb_parse_multi(asn1buf, asn1buflen);
79 if (t) {
80 tlvdb_visit(t, print_cb, NULL, 0);
81 tlvdb_free(t);
82 } else {
83 PrintAndLogEx(ERR, "Can't parse data as TLV tree.");
84 return 1;
85 }
86
87 return 0;
88 }
89
90
Impressum, Datenschutz