1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 Merlok
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
11 #include "asn1utils.h"
14 #include <mbedtls/asn1.h>
15 #include "ui.h" // Print...
20 int ecdsa_asn1_get_signature(uint8_t *signature
, size_t signaturelen
, uint8_t *rval
, uint8_t *sval
) {
21 if (!signature
|| !signaturelen
|| !rval
|| !sval
)
24 int res
= PM3_SUCCESS
;
25 unsigned char *p
= signature
;
26 const unsigned char *end
= p
+ signaturelen
;
30 if ((res
= mbedtls_asn1_get_tag(&p
, end
, &len
, MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
)) == 0) {
31 mbedtls_mpi_init(&xmpi
);
32 res
= mbedtls_asn1_get_mpi(&p
, end
, &xmpi
);
34 mbedtls_mpi_free(&xmpi
);
38 res
= mbedtls_mpi_write_binary(&xmpi
, rval
, 32);
39 mbedtls_mpi_free(&xmpi
);
43 mbedtls_mpi_init(&xmpi
);
44 res
= mbedtls_asn1_get_mpi(&p
, end
, &xmpi
);
46 mbedtls_mpi_free(&xmpi
);
50 res
= mbedtls_mpi_write_binary(&xmpi
, sval
, 32);
51 mbedtls_mpi_free(&xmpi
);
64 static void asn1_print_cb(void *data
, const struct tlv
*tlv
, int level
, bool is_leaf
) {
66 asn1_tag_dump(tlv
, level
, &candump
);
67 if (is_leaf
&& candump
) {
68 print_buffer(tlv
->value
, tlv
->len
, level
+ 1);
72 int asn1_print(uint8_t *asn1buf
, size_t asn1buflen
, const char *indent
) {
74 struct tlvdb
*t
= tlvdb_parse_multi(asn1buf
, asn1buflen
);
76 tlvdb_visit(t
, asn1_print_cb
, NULL
, 0);
79 PrintAndLogEx(ERR
, "Can't parse data as TLV tree");