]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/emv/test/sda_test.c
Merge pull request #503 from merlokk/travis
[proxmark3-svn] / client / emv / test / sda_test.c
1 /*
2 * emv-tools - a set of tools to work with EMV family of smart cards
3 * Copyright (C) 2012, 2015 Dmitry Eremin-Solenikov
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include "../emv_pk.h"
21 #include "../crypto.h"
22 #include "../dump.h"
23 #include "../tlv.h"
24 #include "../emv_pki.h"
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 struct emv_pk vsdc_01 = {
31 .rid = { 0xa0, 0x00, 0x00, 0x00, 0x03, },
32 .index = 1,
33 .hash_algo = HASH_SHA_1,
34 .pk_algo = PK_RSA,
35 .hash = {
36 0xd3, 0x4a, 0x6a, 0x77,
37 0x60, 0x11, 0xc7, 0xe7,
38 0xce, 0x3a, 0xec, 0x5f,
39 0x03, 0xad, 0x2f, 0x8c,
40 0xfc, 0x55, 0x03, 0xcc, },
41 .exp = { 0x03, },
42 .elen = 1,
43 .mlen = 1024 / 8,
44 .modulus = (unsigned char[]){
45 0xc6, 0x96, 0x03, 0x42, 0x13, 0xd7, 0xd8, 0x54, 0x69, 0x84, 0x57, 0x9d, 0x1d, 0x0f, 0x0e, 0xa5,
46 0x19, 0xcf, 0xf8, 0xde, 0xff, 0xc4, 0x29, 0x35, 0x4c, 0xf3, 0xa8, 0x71, 0xa6, 0xf7, 0x18, 0x3f,
47 0x12, 0x28, 0xda, 0x5c, 0x74, 0x70, 0xc0, 0x55, 0x38, 0x71, 0x00, 0xcb, 0x93, 0x5a, 0x71, 0x2c,
48 0x4e, 0x28, 0x64, 0xdf, 0x5d, 0x64, 0xba, 0x93, 0xfe, 0x7e, 0x63, 0xe7, 0x1f, 0x25, 0xb1, 0xe5,
49 0xf5, 0x29, 0x85, 0x75, 0xeb, 0xe1, 0xc6, 0x3a, 0xa6, 0x17, 0x70, 0x69, 0x17, 0x91, 0x1d, 0xc2,
50 0xa7, 0x5a, 0xc2, 0x8b, 0x25, 0x1c, 0x7e, 0xf4, 0x0f, 0x23, 0x65, 0x91, 0x24, 0x90, 0xb9, 0x39,
51 0xbc, 0xa2, 0x12, 0x4a, 0x30, 0xa2, 0x8f, 0x54, 0x40, 0x2c, 0x34, 0xae, 0xca, 0x33, 0x1a, 0xb6,
52 0x7e, 0x1e, 0x79, 0xb2, 0x85, 0xdd, 0x57, 0x71, 0xb5, 0xd9, 0xff, 0x79, 0xea, 0x63, 0x0b, 0x75,
53 },
54 };
55
56 const unsigned char issuer_cert[] = {
57 0x3c, 0x5f, 0xea, 0xd4, 0xdd, 0x7b, 0xca, 0x44, 0xf9, 0x3e, 0x90, 0xc4, 0x4f, 0x76, 0xed, 0xe5,
58 0x4a, 0x32, 0x88, 0xec, 0xdc, 0x78, 0x46, 0x9f, 0xcb, 0x12, 0x25, 0xc0, 0x3b, 0x2c, 0x04, 0xf2,
59 0xc2, 0xf4, 0x12, 0x28, 0x1a, 0x08, 0x22, 0xdf, 0x14, 0x64, 0x92, 0x30, 0x98, 0x9f, 0xb1, 0x49,
60 0x40, 0x70, 0xda, 0xf8, 0xc9, 0x53, 0x4a, 0x78, 0x81, 0x96, 0x01, 0x48, 0x61, 0x6a, 0xce, 0x58,
61 0x17, 0x88, 0x12, 0x0d, 0x35, 0x06, 0xac, 0xe4, 0xce, 0xe5, 0x64, 0xfb, 0x27, 0xee, 0x53, 0x34,
62 0x1c, 0x22, 0xf0, 0xb4, 0x5b, 0x31, 0x87, 0x3d, 0x05, 0xde, 0x54, 0x5e, 0xfe, 0x33, 0xbc, 0xd2,
63 0x9b, 0x21, 0x85, 0xd0, 0x35, 0xa8, 0x06, 0xad, 0x08, 0xc6, 0x97, 0x6f, 0x35, 0x05, 0xa1, 0x99,
64 0x99, 0x93, 0x0c, 0xa8, 0xa0, 0x3e, 0xfa, 0x32, 0x1c, 0x48, 0x60, 0x61, 0xf7, 0xdc, 0xec, 0x9f,
65 };
66
67 const unsigned char issuer_rem[] = {
68 0x1e, 0xbc, 0xa3, 0x0f, 0x00, 0xce, 0x59, 0x62, 0xa8, 0xc6, 0xe1, 0x30, 0x54, 0x4b, 0x82, 0x89,
69 0x1b, 0x23, 0x6c, 0x65, 0xde, 0x29, 0x31, 0x7f, 0x36, 0x47, 0x35, 0xde, 0xe6, 0x3f, 0x65, 0x98,
70 0x97, 0x58, 0x35, 0xd5
71 };
72
73 const unsigned char issuer_exp[] = {
74 0x03,
75 };
76
77 const unsigned char ssad_cr[] = {
78 0x99, 0xa5, 0x58, 0xb6, 0x2b, 0x67, 0x4a, 0xa5, 0xe7, 0xd2, 0xa5, 0x7e, 0x5e, 0xf6, 0xa6, 0xf2,
79 0x25, 0x8e, 0x5d, 0xa0, 0x52, 0xd0, 0x5b, 0x54, 0xe5, 0xc1, 0x15, 0xff, 0x1c, 0xec, 0xf9, 0x4a,
80 0xa2, 0xdf, 0x8f, 0x39, 0xa0, 0x1d, 0x71, 0xc6, 0x19, 0xeb, 0x81, 0x9d, 0xa5, 0x2e, 0xf3, 0x81,
81 0xe8, 0x49, 0x79, 0x58, 0x6a, 0xea, 0x78, 0x55, 0xff, 0xbe, 0xf4, 0x0a, 0xa3, 0xa7, 0x1c, 0xd3,
82 0xb0, 0x4c, 0xfd, 0xf2, 0x70, 0xae, 0xc8, 0x15, 0x8a, 0x27, 0x97, 0xf2, 0x4f, 0xd6, 0x13, 0xb7,
83 0x48, 0x13, 0x46, 0x61, 0x13, 0x5c, 0xd2, 0x90, 0xe4, 0x5b, 0x04, 0xa8, 0xe0, 0xcc, 0xc7, 0x11,
84 0xae, 0x04, 0x2f, 0x15, 0x9e, 0x73, 0xc8, 0x9c, 0x2a, 0x7e, 0x65, 0xa4, 0xc2, 0xfd, 0x1d, 0x61,
85 0x06, 0x02, 0x4a, 0xa2, 0x71, 0x30, 0xb0, 0xec, 0xec, 0x02, 0x38, 0xf9, 0x16, 0x59, 0xde, 0x96,
86 };
87
88 const unsigned char ssd1[] = {
89 0x5f, 0x24, 0x03, 0x08, 0x12, 0x31, 0x5a, 0x08, 0x42, 0x76, 0x55, 0x00, 0x13, 0x23, 0x45, 0x99, 0x5f, 0x34, 0x01, 0x01, 0x9f, 0x07, 0x02, 0xff, 0x00, 0x9f, 0x0d, 0x05, 0xd0, 0x40, 0xac, 0xa8, 0x00, 0x9f, 0x0e, 0x05, 0x00, 0x10, 0x00, 0x00, 0x00, 0x9f, 0x0f, 0x05, 0xd0, 0x68, 0xbc, 0xf8, 0x00,
90 0x5c, 0x00,
91 };
92 static const struct tlv ssd1_tlv = {
93 .len = sizeof(ssd1),
94 .value = ssd1,
95 };
96
97 const unsigned char pan[] = {
98 0x42, 0x76, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00,
99 };
100
101 static int sda_test_raw(bool verbose)
102 {
103 const struct emv_pk *pk = &vsdc_01;
104
105 struct crypto_pk *kcp = crypto_pk_open(PK_RSA,
106 pk->modulus, pk->mlen,
107 pk->exp, pk->elen);
108 if (!kcp)
109 return 1;
110
111 unsigned char *ipk_data;
112 size_t ipk_data_len;
113 ipk_data = crypto_pk_encrypt(kcp, issuer_cert, sizeof(issuer_cert), &ipk_data_len);
114 crypto_pk_close(kcp);
115
116 if (!ipk_data)
117 return 1;
118
119 if (verbose) {
120 printf("issuer cert:\n");
121 dump_buffer(ipk_data, ipk_data_len, stdout, 0);
122 }
123
124 size_t ipk_pk_len = ipk_data[13];
125 unsigned char *ipk_pk = malloc(ipk_pk_len);
126 memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36);
127 memcpy(ipk_pk + ipk_data_len - 36, issuer_rem, sizeof(issuer_rem));
128
129 struct crypto_hash *ch;
130 ch = crypto_hash_open(HASH_SHA_1);
131 if (!ch) {
132 free(ipk_pk);
133 free(ipk_data);
134 return 1;
135 }
136
137 crypto_hash_write(ch, ipk_data + 1, 14);
138 crypto_hash_write(ch, ipk_pk, ipk_pk_len);
139 crypto_hash_write(ch, issuer_exp, sizeof(issuer_exp));
140
141 unsigned char *h = crypto_hash_read(ch);
142 if (!h) {
143 crypto_hash_close(ch);
144 free(ipk_pk);
145 free(ipk_data);
146 return 1;
147 }
148
149 if (verbose) {
150 printf("crypto hash:\n");
151 dump_buffer(h, 20, stdout, 0);
152 }
153
154 if (memcmp(ipk_data + ipk_data_len - 21, h, 20)) {
155 crypto_hash_close(ch);
156 free(ipk_pk);
157 free(ipk_data);
158 return 1;
159 }
160
161 crypto_hash_close(ch);
162 free(ipk_data);
163
164 struct crypto_pk *ikcp = crypto_pk_open(PK_RSA, ipk_pk, (int) ipk_pk_len,
165 issuer_exp, (int) sizeof(issuer_exp));
166 free(ipk_pk);
167 if (!ikcp)
168 return 1;
169
170 size_t ssad_len;
171 unsigned char *ssad = crypto_pk_encrypt(ikcp, ssad_cr, sizeof(ssad_cr), &ssad_len);
172 crypto_pk_close(ikcp);
173 if (!ssad)
174 return 1;
175
176 if (verbose) {
177 printf("ssad:\n");
178 dump_buffer(ssad, ssad_len, stdout, 0);
179 }
180
181 ch = crypto_hash_open(HASH_SHA_1);
182 if (!ch) {
183 free(ssad);
184 return 1;
185 }
186
187 crypto_hash_write(ch, ssad + 1, ssad_len - 22);
188 crypto_hash_write(ch, ssd1, sizeof(ssd1));
189
190 unsigned char *h2 = crypto_hash_read(ch);
191 if (!h2) {
192 crypto_hash_close(ch);
193 free(ssad);
194 return 1;
195 }
196
197 if (verbose) {
198 printf("crypto hash2:\n");
199 dump_buffer(h2, 20, stdout, 0);
200 }
201
202 crypto_hash_close(ch);
203
204 free(ssad);
205
206 return 0;
207 }
208
209 static int sda_test_pk(bool verbose)
210 {
211 const struct emv_pk *pk = &vsdc_01;
212 struct tlvdb *db;
213
214 db = tlvdb_external(0x90, sizeof(issuer_cert), issuer_cert);
215 tlvdb_add(db, tlvdb_external(0x9f32, sizeof(issuer_exp), issuer_exp));
216 tlvdb_add(db, tlvdb_external(0x92, sizeof(issuer_rem), issuer_rem));
217 tlvdb_add(db, tlvdb_external(0x5a, sizeof(pan), pan));
218
219 struct emv_pk *ipk = emv_pki_recover_issuer_cert(pk, db);
220 if (!ipk) {
221 fprintf(stderr, "Could not recover Issuer certificate!\n");
222 tlvdb_free(db);
223 return 2;
224 }
225
226 tlvdb_add(db, tlvdb_external(0x93, sizeof(ssad_cr), ssad_cr));
227
228 struct tlvdb *dacdb = emv_pki_recover_dac(ipk, db, &ssd1_tlv);
229 if (!dacdb) {
230 fprintf(stderr, "Could not recover DAC!\n");
231 emv_pk_free(ipk);
232 tlvdb_free(db);
233 return 2;
234 }
235
236 const struct tlv *dac = tlvdb_get(dacdb, 0x9f45, NULL);
237 if (!dac) {
238 fprintf(stderr, "DAC not found!\n");
239 tlvdb_free(dacdb);
240 emv_pk_free(ipk);
241 tlvdb_free(db);
242 return 2;
243 }
244
245 if (verbose) {
246 printf("dac:\n");
247 dump_buffer(dac->value, dac->len, stdout, 0);
248 }
249
250 tlvdb_free(dacdb);
251 emv_pk_free(ipk);
252 tlvdb_free(db);
253
254 return 0;
255 }
256
257 int exec_sda_test(bool verbose)
258 {
259 int ret;
260 fprintf(stdout, "\n");
261
262 ret = sda_test_raw(verbose);
263 if (ret) {
264 fprintf(stderr, "SDA raw test: failed\n");
265 return ret;
266 }
267 fprintf(stdout, "SDA raw test: passed\n");
268
269 ret = sda_test_pk(verbose);
270 if (ret) {
271 fprintf(stderr, "SDA test pk: failed\n");
272 return ret;
273 }
274 fprintf(stdout, "SDA test pk: passed\n");
275
276 return 0;
277 }
Impressum, Datenschutz