]> git.zerfleddert.de Git - proxmark3-svn/blob - client/emv/emv_pki_priv.c
"hf mf ekeyprn d" doesn't works properly (#904)
[proxmark3-svn] / client / emv / emv_pki_priv.c
1 /*
2 * libopenemv - a library to work with EMV family of smart cards
3 * Copyright (C) 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_pki_priv.h"
21 #include "crypto.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26
27 struct emv_pk *emv_pki_make_ca(const struct crypto_pk *cp,
28 const unsigned char *rid, unsigned char index,
29 unsigned int expire, enum crypto_algo_hash hash_algo)
30 {
31 size_t modlen, explen;
32 unsigned char *mod, *exp;
33
34 if (!rid)
35 return NULL;
36
37 mod = crypto_pk_get_parameter(cp, 0, &modlen);
38 exp = crypto_pk_get_parameter(cp, 1, &explen);
39
40 if (!mod || !modlen || !exp || !explen) {
41 free(mod);
42 free(exp);
43
44 return NULL;
45 }
46
47 struct emv_pk *pk = emv_pk_new(modlen, explen);
48 memcpy(pk->rid, rid, 5);
49 pk->index = index;
50 pk->expire = expire;
51 pk->pk_algo = crypto_pk_get_algo(cp);
52 pk->hash_algo = hash_algo;
53 memcpy(pk->modulus, mod, modlen);
54 memcpy(pk->exp, exp, explen);
55
56 free(mod);
57 free(exp);
58
59 struct crypto_hash *ch = crypto_hash_open(pk->hash_algo);
60 if (!ch) {
61 emv_pk_free(pk);
62 return false;
63 }
64
65 crypto_hash_write(ch, pk->rid, sizeof(pk->rid));
66 crypto_hash_write(ch, &pk->index, 1);
67 crypto_hash_write(ch, pk->modulus, pk->mlen);
68 crypto_hash_write(ch, pk->exp, pk->elen);
69
70 unsigned char *h = crypto_hash_read(ch);
71 if (!h) {
72 crypto_hash_close(ch);
73 emv_pk_free(pk);
74
75 return NULL;
76 }
77
78 memcpy(pk->hash, h, crypto_hash_get_size(ch));
79 crypto_hash_close(ch);
80
81 return pk;
82 }
83
84 static struct tlvdb *emv_pki_sign_message(const struct crypto_pk *cp,
85 tlv_tag_t cert_tag, tlv_tag_t rem_tag,
86 const unsigned char *msg, size_t msg_len,
87 ... /* A list of tlv pointers, end with NULL */
88 )
89 {
90 size_t tmp_len = (crypto_pk_get_nbits(cp) + 7) / 8;
91 unsigned char *tmp = malloc(tmp_len);
92 if (!tmp)
93 return NULL;
94
95 // XXX
96 struct crypto_hash *ch = crypto_hash_open(HASH_SHA_1);
97 if (!ch) {
98 free(tmp);
99
100 return NULL;
101 }
102
103 tmp[0] = 0x6a;
104 tmp[tmp_len - 1] = 0xbc;
105
106 const unsigned char *rem;
107 size_t rem_len;
108 size_t hash_len = crypto_hash_get_size(ch);
109 size_t part_len = tmp_len - 2 - hash_len;
110 if (part_len < msg_len) {
111 memcpy(tmp + 1, msg, part_len);
112 rem = msg + part_len;
113 rem_len = msg_len - part_len;
114 } else {
115 memcpy(tmp + 1, msg, msg_len);
116 memset(tmp + 1 + msg_len, 0xbb, part_len - msg_len);
117 rem = NULL;
118 rem_len = 0;
119 }
120 crypto_hash_write(ch, tmp + 1, part_len);
121 crypto_hash_write(ch, rem, rem_len);
122
123 va_list vl;
124 va_start(vl, msg_len);
125 while (true) {
126 const struct tlv *add_tlv = va_arg(vl, const struct tlv *);
127 if (!add_tlv)
128 break;
129
130 crypto_hash_write(ch, add_tlv->value, add_tlv->len);
131 }
132 va_end(vl);
133
134 unsigned char *h = crypto_hash_read(ch);
135 if (!h) {
136 crypto_hash_close(ch);
137 free(tmp);
138
139 return NULL;
140 }
141
142 memcpy(tmp + 1 + part_len, h, hash_len);
143 crypto_hash_close(ch);
144
145 size_t cert_len;
146 unsigned char *cert = crypto_pk_decrypt(cp, tmp, tmp_len, &cert_len);
147 free(tmp);
148
149 if (!cert)
150 return NULL;
151
152 struct tlvdb *db = tlvdb_fixed(cert_tag, cert_len, cert);
153 free(cert);
154 if (!db)
155 return NULL;
156
157 if (rem) {
158 struct tlvdb *rdb = tlvdb_fixed(rem_tag, rem_len, rem);
159 if (!rdb) {
160 tlvdb_free(db);
161
162 return NULL;
163 }
164 tlvdb_add(db, rdb);
165 }
166
167 return db;
168 }
169
170 static struct tlvdb *emv_pki_sign_key(const struct crypto_pk *cp,
171 struct emv_pk *ipk,
172 unsigned char msgtype,
173 size_t pan_len,
174 tlv_tag_t cert_tag,
175 tlv_tag_t exp_tag,
176 tlv_tag_t rem_tag,
177 const struct tlv *add_tlv
178 )
179 {
180 unsigned pos = 0;
181 unsigned char *msg = malloc(1 + pan_len + 2 + 3 + 1 + 1 + 1 + 1 + ipk->mlen);
182
183 if (!msg)
184 return NULL;
185
186 msg[pos++] = msgtype;
187 memcpy(msg + pos, ipk->pan, pan_len); pos += pan_len;
188 msg[pos++] = (ipk->expire >> 8) & 0xff;
189 msg[pos++] = (ipk->expire >> 16) & 0xff;
190 memcpy(msg + pos, ipk->serial, 3); pos += 3;
191 msg[pos++] = ipk->hash_algo;
192 msg[pos++] = ipk->pk_algo;
193 msg[pos++] = ipk->mlen;
194 msg[pos++] = ipk->elen;
195 memcpy(msg + pos, ipk->modulus, ipk->mlen);
196 pos += ipk->mlen;
197
198 struct tlvdb *exp_db = tlvdb_fixed(exp_tag, ipk->elen, ipk->exp);
199 if (!exp_db) {
200 free(msg);
201
202 return NULL;
203 }
204
205 struct tlvdb *db = emv_pki_sign_message(cp,
206 cert_tag, rem_tag,
207 msg, pos,
208 tlvdb_get(exp_db, exp_tag, NULL),
209 add_tlv,
210 NULL);
211 free(msg);
212 if (!db)
213 return NULL;
214
215 tlvdb_add(db, exp_db);
216
217 return db;
218 }
219
220 struct tlvdb *emv_pki_sign_issuer_cert(const struct crypto_pk *cp, struct emv_pk *issuer_pk)
221 {
222 return emv_pki_sign_key(cp, issuer_pk, 2, 4, 0x90, 0x9f32, 0x92, NULL);
223 }
224
225 struct tlvdb *emv_pki_sign_icc_cert(const struct crypto_pk *cp, struct emv_pk *icc_pk, const struct tlv *sda_tlv)
226 {
227 return emv_pki_sign_key(cp, icc_pk, 4, 10, 0x9f46, 0x9f47, 0x9f48, sda_tlv);
228 }
229
230 struct tlvdb *emv_pki_sign_icc_pe_cert(const struct crypto_pk *cp, struct emv_pk *icc_pe_pk)
231 {
232 return emv_pki_sign_key(cp, icc_pe_pk, 4, 10, 0x9f2d, 0x9f2e, 0x9f2f, NULL);
233 }
234
235 struct tlvdb *emv_pki_sign_dac(const struct crypto_pk *cp, const struct tlv *dac_tlv, const struct tlv *sda_tlv)
236 {
237 unsigned pos = 0;
238 unsigned char *msg = malloc(1+1+dac_tlv->len);
239
240 if (!msg)
241 return NULL;
242
243 msg[pos++] = 3;
244 msg[pos++] = HASH_SHA_1;
245 memcpy(msg+pos, dac_tlv->value, dac_tlv->len);
246 pos += dac_tlv->len;
247
248 struct tlvdb *db = emv_pki_sign_message(cp,
249 0x93, 0,
250 msg, pos,
251 sda_tlv,
252 NULL);
253
254 free(msg);
255
256 return db;
257 }
258
259 struct tlvdb *emv_pki_sign_idn(const struct crypto_pk *cp, const struct tlv *idn_tlv, const struct tlv *dyn_tlv)
260 {
261 unsigned pos = 0;
262 unsigned char *msg = malloc(1+1+1+1+idn_tlv->len);
263
264 if (!msg)
265 return NULL;
266
267 msg[pos++] = 5;
268 msg[pos++] = HASH_SHA_1;
269 msg[pos++] = idn_tlv->len + 1;
270 msg[pos++] = idn_tlv->len;
271 memcpy(msg+pos, idn_tlv->value, idn_tlv->len);
272 pos += idn_tlv->len;
273
274 struct tlvdb *db = emv_pki_sign_message(cp,
275 0x9f4b, 0,
276 msg, pos,
277 dyn_tlv,
278 NULL);
279
280 free(msg);
281
282 return db;
283 }
Impressum, Datenschutz