/src/opensc/src/libopensc/pkcs15-pteid.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * PKCS15 emulation layer for Portugal eID card. |
3 | | * |
4 | | * Copyright (C) 2016-2017, Nuno Goncalves <nunojpg@gmail.com> |
5 | | * Copyright (C) 2009, Joao Poupino <joao.poupino@ist.utl.pt> |
6 | | * Copyright (C) 2004, Martin Paljak <martin@martinpaljak.net> |
7 | | * |
8 | | * This library is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * This library is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with this library; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | * |
22 | | * Based on the PKCS#15 emulation layer for EstEID card by Martin Paljak |
23 | | * |
24 | | */ |
25 | | |
26 | | /* |
27 | | * The card has a valid PKCS#15 file system. However, the private keys |
28 | | * are missing the SC_PKCS15_CO_FLAG_PRIVATE flag and this causes problems |
29 | | * with some applications (i.e. they don't work). |
30 | | * |
31 | | * The three main objectives of the emulation layer are: |
32 | | * |
33 | | * 1. Add the necessary SC_PKCS15_CO_FLAG_PRIVATE flag to private keys. |
34 | | * 2. Hide "superfluous" PKCS#15 objects, e.g. PUKs (the user can't use them). |
35 | | * 3. Improve usability by providing more descriptive names for the PINs, Keys, etc. |
36 | | * |
37 | | */ |
38 | | |
39 | | #ifdef HAVE_CONFIG_H |
40 | | #include "config.h" |
41 | | #endif |
42 | | |
43 | | #include <stdlib.h> |
44 | | #include <string.h> |
45 | | #include <stdio.h> |
46 | | |
47 | | #include "common/compat_strlcpy.h" |
48 | | #include "internal.h" |
49 | | #include "pkcs15.h" |
50 | | |
51 | | static int pteid_detect_card(struct sc_card *card); |
52 | | |
53 | | static |
54 | | int dump_ef(sc_card_t * card, const char *path, u8 * buf, size_t * buf_len) |
55 | 322 | { |
56 | 322 | int rv; |
57 | 322 | sc_file_t *file = NULL; |
58 | 322 | sc_path_t scpath; |
59 | 322 | sc_format_path(path, &scpath); |
60 | 322 | rv = sc_select_file(card, &scpath, &file); |
61 | 322 | if (rv < 0) { |
62 | 107 | sc_file_free(file); |
63 | 107 | return rv; |
64 | 107 | } |
65 | 215 | if (file->size > *buf_len) { |
66 | 19 | sc_file_free(file); |
67 | 19 | return SC_ERROR_BUFFER_TOO_SMALL; |
68 | 19 | } |
69 | 196 | rv = sc_read_binary(card, 0, buf, file->size, 0); |
70 | 196 | sc_file_free(file); |
71 | 196 | if (rv < 0) |
72 | 4 | return rv; |
73 | 192 | *buf_len = rv; |
74 | | |
75 | 192 | return SC_SUCCESS; |
76 | 196 | } |
77 | | |
78 | | static const struct sc_asn1_entry c_asn1_odf[] = { |
79 | | {"privateKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, 0, NULL, NULL}, |
80 | | {"publicKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 1 | SC_ASN1_CONS, 0, NULL, NULL}, |
81 | | {"trustedPublicKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 2 | SC_ASN1_CONS, 0, NULL, NULL}, |
82 | | {"secretKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 3 | SC_ASN1_CONS, 0, NULL, NULL}, |
83 | | {"certificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 4 | SC_ASN1_CONS, 0, NULL, NULL}, |
84 | | {"trustedCertificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 5 | SC_ASN1_CONS, 0, NULL, NULL}, |
85 | | {"usefulCertificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 6 | SC_ASN1_CONS, 0, NULL, NULL}, |
86 | | {"dataObjects", SC_ASN1_STRUCT, SC_ASN1_CTX | 7 | SC_ASN1_CONS, 0, NULL, NULL}, |
87 | | {"authObjects", SC_ASN1_STRUCT, SC_ASN1_CTX | 8 | SC_ASN1_CONS, 0, NULL, NULL}, |
88 | | {NULL, 0, 0, 0, NULL, NULL} |
89 | | }; |
90 | | |
91 | | static const unsigned int odf_indexes[] = { |
92 | | SC_PKCS15_PRKDF, //0 |
93 | | SC_PKCS15_PUKDF, //1 |
94 | | SC_PKCS15_PUKDF_TRUSTED, //2 |
95 | | SC_PKCS15_SKDF, //3 |
96 | | SC_PKCS15_CDF, //4 |
97 | | SC_PKCS15_CDF_TRUSTED, //5 |
98 | | SC_PKCS15_CDF_USEFUL, //6 |
99 | | SC_PKCS15_DODF, //7 |
100 | | SC_PKCS15_AODF, //8 |
101 | | }; |
102 | | |
103 | | static |
104 | | int parse_odf(const u8 * buf, size_t buflen, struct sc_pkcs15_card *p15card) |
105 | 41 | { |
106 | 41 | const u8 *p = buf; |
107 | 41 | size_t left = buflen; |
108 | 41 | int r, i, type; |
109 | 41 | sc_path_t path; |
110 | 41 | struct sc_asn1_entry asn1_obj_or_path[] = { |
111 | 41 | {"path", SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_SEQUENCE, 0, |
112 | 41 | &path, NULL}, |
113 | 41 | {NULL, 0, 0, 0, NULL, NULL} |
114 | 41 | }; |
115 | 41 | struct sc_asn1_entry asn1_odf[10]; |
116 | | |
117 | 41 | sc_path_t path_prefix; |
118 | | |
119 | 41 | sc_format_path("3F004F00", &path_prefix); |
120 | | |
121 | 41 | sc_copy_asn1_entry(c_asn1_odf, asn1_odf); |
122 | 410 | for (i = 0; asn1_odf[i].name != NULL; i++) |
123 | 369 | sc_format_asn1_entry(asn1_odf + i, asn1_obj_or_path, NULL, 0); |
124 | 134 | while (left > 0) { |
125 | 111 | r = sc_asn1_decode_choice(p15card->card->ctx, asn1_odf, p, left, |
126 | 111 | &p, &left); |
127 | 111 | if (r == SC_ERROR_ASN1_END_OF_CONTENTS) |
128 | 10 | break; |
129 | 101 | if (r < 0) |
130 | 8 | return r; |
131 | 93 | type = r; |
132 | 93 | r = sc_pkcs15_make_absolute_path(&path_prefix, &path); |
133 | 93 | if (r < 0) |
134 | 0 | return r; |
135 | 93 | r = sc_pkcs15_add_df(p15card, odf_indexes[type], &path); |
136 | 93 | if (r) |
137 | 0 | return r; |
138 | 93 | } |
139 | 33 | return 0; |
140 | 41 | } |
141 | | |
142 | | static int sc_pkcs15emu_pteid_init(sc_pkcs15_card_t * p15card) |
143 | 378 | { |
144 | 378 | u8 buf[1024]; |
145 | 378 | sc_pkcs15_df_t *df; |
146 | 378 | sc_pkcs15_object_t *p15_obj; |
147 | 378 | sc_path_t path; |
148 | 378 | struct sc_file *file = NULL; |
149 | 378 | size_t len; |
150 | 378 | int rv; |
151 | 378 | int i; |
152 | | |
153 | 378 | sc_context_t *ctx = p15card->card->ctx; |
154 | 378 | LOG_FUNC_CALLED(ctx); |
155 | | |
156 | | /* Check for correct card atr */ |
157 | 378 | if (pteid_detect_card(p15card->card) != SC_SUCCESS) |
158 | 0 | return SC_ERROR_WRONG_CARD; |
159 | | |
160 | 378 | sc_log(p15card->card->ctx, "Selecting application DF"); |
161 | 378 | sc_format_path("4F00", &path); |
162 | 378 | rv = sc_select_file(p15card->card, &path, &file); |
163 | 378 | if (rv != SC_SUCCESS || !file) |
164 | 176 | return SC_ERROR_INTERNAL; |
165 | | /* set the application DF */ |
166 | 202 | sc_file_free(p15card->file_app); |
167 | 202 | p15card->file_app = file; |
168 | | |
169 | | /* Load TokenInfo */ |
170 | 202 | len = sizeof(buf); |
171 | 202 | rv = dump_ef(p15card->card, "4F005032", buf, &len); |
172 | 202 | if (rv != SC_SUCCESS) { |
173 | 51 | sc_log(ctx, "Reading of EF.TOKENINFO failed: %d", rv); |
174 | 51 | LOG_FUNC_RETURN(ctx, rv); |
175 | 51 | } |
176 | 151 | rv = sc_pkcs15_parse_tokeninfo(p15card->card->ctx, p15card->tokeninfo, |
177 | 151 | buf, len); |
178 | 151 | if (rv != SC_SUCCESS) { |
179 | 31 | sc_log(ctx, "Decoding of EF.TOKENINFO failed: %d", rv); |
180 | 31 | LOG_FUNC_RETURN(ctx, rv); |
181 | 31 | } |
182 | | |
183 | 120 | p15card->tokeninfo->flags |= SC_PKCS15_TOKEN_PRN_GENERATION |
184 | 120 | | SC_PKCS15_TOKEN_EID_COMPLIANT |
185 | 120 | | SC_PKCS15_TOKEN_READONLY; |
186 | | |
187 | | /* Load ODF */ |
188 | 120 | len = sizeof(buf); |
189 | 120 | rv = dump_ef(p15card->card, "4F005031", buf, &len); |
190 | 120 | if (rv != SC_SUCCESS) { |
191 | 79 | sc_log(ctx, "Reading of ODF failed: %d", rv); |
192 | 79 | LOG_FUNC_RETURN(ctx, rv); |
193 | 79 | } |
194 | 41 | rv = parse_odf(buf, len, p15card); |
195 | 41 | if (rv != SC_SUCCESS) { |
196 | 8 | sc_log(ctx, "Decoding of ODF failed: %d", rv); |
197 | 8 | sc_pkcs15_card_clear(p15card); |
198 | 8 | LOG_FUNC_RETURN(ctx, rv); |
199 | 8 | } |
200 | | |
201 | | /* Decode EF.PrKDF, EF.PuKDF, EF.CDF and EF.AODF */ |
202 | 121 | for (df = p15card->df_list; df != NULL; df = df->next) { |
203 | 88 | if (df->type == SC_PKCS15_PRKDF) { |
204 | 16 | rv = sc_pkcs15_parse_df(p15card, df); |
205 | 16 | if (rv != SC_SUCCESS) { |
206 | 15 | sc_log(ctx, |
207 | 15 | "Decoding of EF.PrKDF (%s) failed: %d", |
208 | 15 | sc_print_path(&df->path), rv); |
209 | 15 | } |
210 | 16 | } |
211 | 88 | if (df->type == SC_PKCS15_PUKDF) { |
212 | 6 | rv = sc_pkcs15_parse_df(p15card, df); |
213 | 6 | if (rv != SC_SUCCESS) { |
214 | 5 | sc_log(ctx, |
215 | 5 | "Decoding of EF.PuKDF (%s) failed: %d", |
216 | 5 | sc_print_path(&df->path), rv); |
217 | 5 | } |
218 | 6 | } |
219 | 88 | if (df->type == SC_PKCS15_CDF) { |
220 | 30 | rv = sc_pkcs15_parse_df(p15card, df); |
221 | 30 | if (rv != SC_SUCCESS) { |
222 | 28 | sc_log(ctx, |
223 | 28 | "Decoding of EF.CDF (%s) failed: %d", |
224 | 28 | sc_print_path(&df->path), rv); |
225 | 28 | } |
226 | 30 | } |
227 | 88 | if (df->type == SC_PKCS15_AODF) { |
228 | 19 | rv = sc_pkcs15_parse_df(p15card, df); |
229 | 19 | if (rv != SC_SUCCESS) { |
230 | 18 | sc_log(ctx, |
231 | 18 | "Decoding of EF.AODF (%s) failed: %d", |
232 | 18 | sc_print_path(&df->path), rv); |
233 | 18 | } |
234 | 19 | } |
235 | 88 | } |
236 | | |
237 | 33 | p15_obj = p15card->obj_list; |
238 | 33 | while (p15_obj != NULL) { |
239 | 0 | if ( p15_obj->df && (p15_obj->df->type == SC_PKCS15_PRKDF) ) { |
240 | 0 | struct sc_pkcs15_prkey_info *prkey_info = (sc_pkcs15_prkey_info_t *) p15_obj->data; |
241 | 0 | prkey_info->access_flags = SC_PKCS15_PRKEY_ACCESS_SENSITIVE |
242 | 0 | | SC_PKCS15_PRKEY_ACCESS_ALWAYSSENSITIVE |
243 | 0 | | SC_PKCS15_PRKEY_ACCESS_NEVEREXTRACTABLE |
244 | 0 | | SC_PKCS15_PRKEY_ACCESS_LOCAL; |
245 | 0 | p15_obj->flags = SC_PKCS15_CO_FLAG_PRIVATE; |
246 | 0 | } |
247 | | |
248 | |
|
249 | 0 | if ( p15_obj->df && (p15_obj->df->type == SC_PKCS15_AODF) ) { |
250 | 0 | static const char *pteid_pin_names[3] = { |
251 | 0 | "Auth PIN", |
252 | 0 | "Sign PIN", |
253 | 0 | "Address PIN" |
254 | 0 | }; |
255 | |
|
256 | 0 | struct sc_pin_cmd_data pin_cmd_data; |
257 | 0 | struct sc_pkcs15_auth_info *pin_info = (sc_pkcs15_auth_info_t *) p15_obj->data; |
258 | |
|
259 | 0 | strlcpy(p15_obj->label, pteid_pin_names[pin_info->auth_id.value[0]-1], sizeof(p15_obj->label)); |
260 | |
|
261 | 0 | pin_info->attrs.pin.flags |= SC_PKCS15_PIN_FLAG_NEEDS_PADDING; |
262 | 0 | pin_info->tries_left = -1; |
263 | 0 | pin_info->max_tries = 3; |
264 | 0 | pin_info->auth_method = SC_AC_CHV; |
265 | |
|
266 | 0 | memset(&pin_cmd_data, 0, sizeof(pin_cmd_data)); |
267 | 0 | pin_cmd_data.cmd = SC_PIN_CMD_GET_INFO; |
268 | 0 | pin_cmd_data.pin_type = pin_info->attrs.pin.type; |
269 | 0 | pin_cmd_data.pin_reference = pin_info->attrs.pin.reference; |
270 | 0 | rv = sc_pin_cmd(p15card->card, &pin_cmd_data, NULL); |
271 | 0 | if (rv == SC_SUCCESS) { |
272 | 0 | pin_info->tries_left = pin_cmd_data.pin1.tries_left; |
273 | 0 | pin_info->logged_in = pin_cmd_data.pin1.logged_in; |
274 | 0 | } |
275 | 0 | } |
276 | | /* Remove found public keys as cannot be read_binary()'d */ |
277 | 0 | if ( p15_obj->df && (p15_obj->df->type == SC_PKCS15_PUKDF) ) { |
278 | 0 | sc_pkcs15_object_t *puk = p15_obj; |
279 | 0 | p15_obj = p15_obj->next; |
280 | 0 | sc_pkcs15_remove_object(p15card, puk); |
281 | 0 | sc_pkcs15_free_object(puk); |
282 | 0 | } else { |
283 | 0 | p15_obj = p15_obj->next; |
284 | 0 | } |
285 | 0 | } |
286 | | |
287 | | /* Add data objects */ |
288 | 198 | for (i = 0; i < 5; i++) { |
289 | 165 | static const char *object_labels[5] = { |
290 | 165 | "Trace", |
291 | 165 | "Citizen Data", |
292 | 165 | "Citizen Address Data", |
293 | 165 | "SOd", |
294 | 165 | "Citizen Notepad", |
295 | 165 | }; |
296 | 165 | static const char *object_authids[5] = {NULL, NULL, "3", NULL, NULL}; |
297 | 165 | static const char *object_paths[5] = { |
298 | 165 | "3f000003", |
299 | 165 | "3f005f00ef02", |
300 | 165 | "3f005f00ef05", |
301 | 165 | "3f005f00ef06", |
302 | 165 | "3f005f00ef07", |
303 | 165 | }; |
304 | 165 | static const int object_flags[5] = { |
305 | 165 | 0, |
306 | 165 | 0, |
307 | 165 | SC_PKCS15_CO_FLAG_PRIVATE, |
308 | 165 | 0, |
309 | 165 | 0, |
310 | 165 | }; |
311 | 165 | struct sc_pkcs15_data_info obj_info; |
312 | 165 | struct sc_pkcs15_object obj_obj; |
313 | | |
314 | 165 | memset(&obj_info, 0, sizeof(obj_info)); |
315 | 165 | memset(&obj_obj, 0, sizeof(obj_obj)); |
316 | | |
317 | 165 | sc_format_path(object_paths[i], &obj_info.path); |
318 | 165 | strlcpy(obj_info.app_label, object_labels[i], SC_PKCS15_MAX_LABEL_SIZE); |
319 | 165 | if (object_authids[i] != NULL) |
320 | 33 | sc_pkcs15_format_id(object_authids[i], &obj_obj.auth_id); |
321 | 165 | strlcpy(obj_obj.label, object_labels[i], SC_PKCS15_MAX_LABEL_SIZE); |
322 | 165 | obj_obj.flags = object_flags[i]; |
323 | | |
324 | 165 | rv = sc_pkcs15emu_object_add(p15card, SC_PKCS15_TYPE_DATA_OBJECT, &obj_obj, &obj_info); |
325 | 165 | if (rv != SC_SUCCESS){ |
326 | 0 | sc_log(ctx, "Object add failed: %d", rv); |
327 | 0 | break; |
328 | 0 | } |
329 | 165 | } |
330 | | |
331 | 33 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
332 | 33 | } |
333 | | |
334 | | static int pteid_detect_card(struct sc_card *card) |
335 | 8.51k | { |
336 | 8.51k | if (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID) |
337 | 756 | return SC_SUCCESS; |
338 | 7.75k | return SC_ERROR_WRONG_CARD; |
339 | 8.51k | } |
340 | | |
341 | | int sc_pkcs15emu_pteid_init_ex(sc_pkcs15_card_t *p15card, struct sc_aid *aid) |
342 | 8.13k | { |
343 | 8.13k | int r=SC_SUCCESS; |
344 | 8.13k | sc_context_t *ctx = p15card->card->ctx; |
345 | 8.13k | LOG_FUNC_CALLED(ctx); |
346 | | |
347 | | /* check for proper card */ |
348 | 8.13k | r = pteid_detect_card(p15card->card); |
349 | 8.13k | if (r == SC_ERROR_WRONG_CARD) |
350 | 8.13k | LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_CARD); |
351 | | /* ok: initialize and return */ |
352 | 378 | LOG_FUNC_RETURN(ctx, sc_pkcs15emu_pteid_init(p15card)); |
353 | 378 | } |