/src/opensc/src/libopensc/pkcs15-dnie.c
Line | Count | Source |
1 | | /** |
2 | | * PKCS15 emulation layer for DNIe card. |
3 | | * |
4 | | * Copyright (C) 2011, Andre Zepezauer <andre.zepezauer@student.uni-halle.de> |
5 | | * Copyright (C) 2011, Juan Antonio Martinez <jonsito@terra.es> |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with this library; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | #ifdef HAVE_CONFIG_H |
25 | | #include "config.h" |
26 | | #endif |
27 | | #include "libopensc/log.h" |
28 | | #include "libopensc/asn1.h" |
29 | | #include "libopensc/pkcs15.h" |
30 | | #include "libopensc/cwa14890.h" |
31 | | #include "libopensc/cwa-dnie.h" |
32 | | |
33 | | /* Card driver related */ |
34 | | #if defined(ENABLE_OPENSSL) && defined(ENABLE_SM) |
35 | | |
36 | | extern int dnie_match_card(struct sc_card *card); |
37 | | |
38 | | /* Helper functions to get the pkcs15 stuff bound. */ |
39 | | |
40 | | static |
41 | | int dump_ef(sc_card_t * card, const char *path, u8 * buf, size_t * buf_len) |
42 | 467 | { |
43 | 467 | int rv; |
44 | 467 | sc_file_t *file = NULL; |
45 | 467 | sc_path_t scpath; |
46 | 467 | sc_format_path(path, &scpath); |
47 | 467 | rv = sc_select_file(card, &scpath, &file); |
48 | 467 | if (rv < 0) { |
49 | 420 | sc_file_free(file); |
50 | 420 | return rv; |
51 | 420 | } |
52 | 47 | if (file->size > *buf_len) { |
53 | 17 | sc_file_free(file); |
54 | 17 | return SC_ERROR_BUFFER_TOO_SMALL; |
55 | 17 | } |
56 | 30 | rv = sc_read_binary(card, 0, buf, file->size, 0); |
57 | 30 | sc_file_free(file); |
58 | 30 | if (rv < 0) |
59 | 1 | return rv; |
60 | 29 | *buf_len = rv; |
61 | | |
62 | 29 | return SC_SUCCESS; |
63 | 30 | } |
64 | | |
65 | | static const struct sc_asn1_entry c_asn1_odf[] = { |
66 | | {"privateKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, 0, NULL, |
67 | | NULL}, |
68 | | {"publicKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 1 | SC_ASN1_CONS, 0, NULL, |
69 | | NULL}, |
70 | | {"trustedPublicKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 2 | SC_ASN1_CONS, 0, |
71 | | NULL, NULL}, |
72 | | {"secretKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 3 | SC_ASN1_CONS, 0, NULL, |
73 | | NULL}, |
74 | | {"certificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 4 | SC_ASN1_CONS, 0, |
75 | | NULL, NULL}, |
76 | | {"trustedCertificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 5 | SC_ASN1_CONS, |
77 | | 0, NULL, NULL}, |
78 | | {"usefulCertificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 6 | SC_ASN1_CONS, |
79 | | 0, NULL, NULL}, |
80 | | {"dataObjects", SC_ASN1_STRUCT, SC_ASN1_CTX | 7 | SC_ASN1_CONS, 0, NULL, |
81 | | NULL}, |
82 | | {"authObjects", SC_ASN1_STRUCT, SC_ASN1_CTX | 8 | SC_ASN1_CONS, 0, NULL, |
83 | | NULL}, |
84 | | {NULL, 0, 0, 0, NULL, NULL} |
85 | | }; |
86 | | |
87 | | static const unsigned int odf_indexes[] = { |
88 | | SC_PKCS15_PRKDF, |
89 | | SC_PKCS15_PUKDF, |
90 | | SC_PKCS15_PUKDF_TRUSTED, |
91 | | SC_PKCS15_SKDF, |
92 | | SC_PKCS15_CDF, |
93 | | SC_PKCS15_CDF_TRUSTED, |
94 | | SC_PKCS15_CDF_USEFUL, |
95 | | SC_PKCS15_DODF, |
96 | | SC_PKCS15_AODF, |
97 | | }; |
98 | | |
99 | | static |
100 | | int parse_odf(const u8 * buf, size_t buflen, struct sc_pkcs15_card *p15card) |
101 | 0 | { |
102 | 0 | const u8 *p = buf; |
103 | 0 | size_t left = buflen; |
104 | 0 | int r, i, type; |
105 | 0 | sc_path_t path; |
106 | 0 | struct sc_asn1_entry asn1_obj_or_path[] = { |
107 | 0 | {"path", SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_SEQUENCE, 0, |
108 | 0 | &path, NULL}, |
109 | 0 | {NULL, 0, 0, 0, NULL, NULL} |
110 | 0 | }; |
111 | 0 | struct sc_asn1_entry asn1_odf[10]; |
112 | |
|
113 | 0 | sc_path_t path_prefix; |
114 | |
|
115 | 0 | sc_format_path("3F005015", &path_prefix); |
116 | |
|
117 | 0 | sc_copy_asn1_entry(c_asn1_odf, asn1_odf); |
118 | 0 | for (i = 0; asn1_odf[i].name != NULL; i++) |
119 | 0 | sc_format_asn1_entry(asn1_odf + i, asn1_obj_or_path, NULL, 0); |
120 | 0 | while (left > 0) { |
121 | 0 | r = sc_asn1_decode_choice(p15card->card->ctx, asn1_odf, p, left, |
122 | 0 | &p, &left); |
123 | 0 | if (r == SC_ERROR_ASN1_END_OF_CONTENTS) |
124 | 0 | break; |
125 | 0 | if (r < 0) |
126 | 0 | return r; |
127 | 0 | type = r; |
128 | 0 | r = sc_pkcs15_make_absolute_path(&path_prefix, &path); |
129 | 0 | if (r < 0) |
130 | 0 | return r; |
131 | 0 | r = sc_pkcs15_add_df(p15card, odf_indexes[type], &path); |
132 | 0 | if (r) |
133 | 0 | return r; |
134 | 0 | } |
135 | 0 | return 0; |
136 | 0 | } |
137 | | |
138 | | static int sc_pkcs15emu_dnie_init(sc_pkcs15_card_t * p15card) |
139 | 477 | { |
140 | 477 | u8 buf[1024]; |
141 | 477 | sc_pkcs15_df_t *df; |
142 | 477 | sc_pkcs15_object_t *p15_obj; |
143 | 477 | size_t len = sizeof(buf); |
144 | 477 | int rv; |
145 | 477 | struct sc_pkcs15_cert_info *p15_info = NULL; |
146 | 477 | int use_pin_cache_backup = p15card->opts.use_pin_cache; |
147 | | |
148 | 477 | sc_context_t *ctx = p15card->card->ctx; |
149 | 477 | LOG_FUNC_CALLED(ctx); |
150 | | |
151 | | /* Check for correct card driver (i.e. iso7816) */ |
152 | 477 | if (strcmp(p15card->card->driver->short_name, "dnie") != 0) |
153 | 10 | return SC_ERROR_WRONG_CARD; |
154 | | |
155 | | /* Check for correct card atr */ |
156 | 467 | if (dnie_match_card(p15card->card) != 1) |
157 | 0 | return SC_ERROR_WRONG_CARD; |
158 | | |
159 | | /* The two keys inside DNIe 3.0 needs login before performing any signature. |
160 | | * They are CKA_ALWAYS_AUTHENTICATE although they are not tagged like that. |
161 | | * For the moment caching is forced if 3.0 is detected to make it work properly. */ |
162 | 467 | if (p15card->card->atr.value[15] >= DNIE_30_VERSION) { |
163 | 363 | p15card->opts.use_pin_cache = 1; |
164 | 363 | p15card->opts.pin_cache_counter = DNIE_30_CACHE_COUNTER; |
165 | 363 | sc_log(ctx, "DNIe 3.0 detected - PKCS#15 options reset: use_file_cache=%d use_pin_cache=%d pin_cache_counter=%d pin_cache_ignore_user_consent=%d", |
166 | 363 | p15card->opts.use_file_cache, |
167 | 363 | p15card->opts.use_pin_cache, |
168 | 363 | p15card->opts.pin_cache_counter, |
169 | 363 | p15card->opts.pin_cache_ignore_user_consent); |
170 | 363 | } |
171 | | |
172 | | /* Set root path of this application */ |
173 | 467 | sc_file_free(p15card->file_app); |
174 | 467 | p15card->file_app = sc_file_new(); |
175 | 467 | if (NULL == p15card->file_app) { |
176 | 0 | rv = SC_ERROR_NOT_ENOUGH_MEMORY; |
177 | 0 | LOG_TEST_GOTO_ERR(ctx, rv, "Can not create file."); |
178 | 0 | } |
179 | 467 | sc_format_path("3F00", &p15card->file_app->path); |
180 | | |
181 | | /* Load TokenInfo */ |
182 | 467 | rv = dump_ef(p15card->card, "3F0050155032", buf, &len); |
183 | 467 | LOG_TEST_GOTO_ERR(ctx, rv, "Reading of EF.TOKENINFO failed."); |
184 | | |
185 | 29 | rv = sc_pkcs15_parse_tokeninfo(p15card->card->ctx, p15card->tokeninfo, |
186 | 29 | buf, len); |
187 | 29 | LOG_TEST_GOTO_ERR(ctx, rv, "Decoding of EF.TOKENINFO failed."); |
188 | | |
189 | | /* Only accept the original stuff */ |
190 | 0 | if (strcmp(p15card->tokeninfo->manufacturer_id, "DGP-FNMT") != 0) { |
191 | 0 | rv = SC_ERROR_WRONG_CARD; |
192 | 0 | LOG_TEST_GOTO_ERR(ctx, rv, "Wrong card."); |
193 | 0 | } |
194 | | |
195 | | /* Load ODF */ |
196 | 0 | rv = dump_ef(p15card->card, "3F0050155031", buf, &len); |
197 | 0 | LOG_TEST_GOTO_ERR(ctx, rv, "Reading of ODF failed."); |
198 | | |
199 | 0 | rv = parse_odf(buf, len, p15card); |
200 | 0 | LOG_TEST_GOTO_ERR(ctx, rv, "Decoding of ODF failed."); |
201 | | |
202 | | /* Decode EF.PrKDF, EF.PuKDF and EF.CDF */ |
203 | 0 | for (df = p15card->df_list; df != NULL; df = df->next) { |
204 | 0 | if (df->type == SC_PKCS15_PRKDF) { |
205 | 0 | rv = sc_pkcs15_parse_df(p15card, df); |
206 | 0 | if (rv != SC_SUCCESS) { |
207 | 0 | sc_log(ctx, |
208 | 0 | "Decoding of EF.PrKDF (%s) failed: %d", |
209 | 0 | sc_print_path(&df->path), rv); |
210 | 0 | } |
211 | 0 | } |
212 | 0 | if (df->type == SC_PKCS15_PUKDF) { |
213 | 0 | rv = sc_pkcs15_parse_df(p15card, df); |
214 | 0 | if (rv != SC_SUCCESS) { |
215 | 0 | sc_log(ctx, |
216 | 0 | "Decoding of EF.PuKDF (%s) failed: %d", |
217 | 0 | sc_print_path(&df->path), rv); |
218 | 0 | } |
219 | 0 | } |
220 | 0 | if (df->type == SC_PKCS15_CDF) { |
221 | 0 | rv = sc_pkcs15_parse_df(p15card, df); |
222 | 0 | if (rv != SC_SUCCESS) { |
223 | 0 | sc_log(ctx, |
224 | 0 | "Decoding of EF.CDF (%s) failed: %d", |
225 | 0 | sc_print_path(&df->path), rv); |
226 | 0 | } |
227 | 0 | } |
228 | 0 | if (df->type == SC_PKCS15_DODF) { |
229 | 0 | rv = sc_pkcs15_parse_df(p15card, df); |
230 | 0 | if (rv != SC_SUCCESS) { |
231 | 0 | sc_log(ctx, |
232 | 0 | "Decoding of EF.DODF (%s) failed: %d", |
233 | 0 | sc_print_path(&df->path), rv); |
234 | 0 | } |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | /* Perform required fixes */ |
239 | 0 | p15_obj = p15card->obj_list; |
240 | 0 | while (p15_obj != NULL) { |
241 | | /* Add missing 'auth_id' to private objects */ |
242 | 0 | if ((p15_obj->flags & SC_PKCS15_CO_FLAG_PRIVATE) |
243 | 0 | && (p15_obj->auth_id.len == 0)) { |
244 | 0 | p15_obj->auth_id.value[0] = 0x01; |
245 | 0 | p15_obj->auth_id.len = 1; |
246 | 0 | }; |
247 | | /* Set path count to -1 for public certificates, as they |
248 | | will need to be decompressed and read_binary()'d, so |
249 | | we make sure we end up reading the file->size and not the |
250 | | path->count which is the compressed size on newer |
251 | | DNIe versions */ |
252 | 0 | if ( p15_obj->df && (p15_obj->df->type == SC_PKCS15_CDF) ) { |
253 | 0 | p15_info = (struct sc_pkcs15_cert_info *) p15_obj ->data; |
254 | 0 | p15_info ->path.count = -1; |
255 | 0 | } |
256 | | /* Remove found public keys as cannot be read_binary()'d */ |
257 | 0 | if ( p15_obj->df && (p15_obj->df->type == SC_PKCS15_PUKDF) ) { |
258 | 0 | sc_pkcs15_object_t *puk = p15_obj; |
259 | 0 | p15_obj = p15_obj->next; |
260 | 0 | sc_pkcs15_remove_object(p15card, puk); |
261 | 0 | sc_pkcs15_free_object(puk); |
262 | 0 | } else { |
263 | 0 | p15_obj = p15_obj->next; |
264 | 0 | } |
265 | 0 | } |
266 | | |
267 | 0 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
268 | | |
269 | 467 | err: |
270 | 467 | sc_pkcs15_card_clear(p15card); |
271 | 467 | p15card->opts.use_pin_cache = use_pin_cache_backup; |
272 | 467 | LOG_FUNC_RETURN(ctx, rv); |
273 | 467 | } |
274 | | #endif |
275 | | |
276 | | /****************************************/ |
277 | | /* public functions for in-built module */ |
278 | | /****************************************/ |
279 | | int sc_pkcs15emu_dnie_init_ex(sc_pkcs15_card_t * p15card, |
280 | | struct sc_aid *aid) |
281 | 8.62k | { |
282 | 8.62k | int r=SC_SUCCESS; |
283 | 8.62k | sc_context_t *ctx = p15card->card->ctx; |
284 | 8.62k | LOG_FUNC_CALLED(ctx); |
285 | | |
286 | 8.62k | #if defined(ENABLE_OPENSSL) && defined(ENABLE_SM) |
287 | | /* check for proper card */ |
288 | 8.62k | r = dnie_match_card(p15card->card); |
289 | 8.62k | if (r == 0) |
290 | 8.62k | LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_CARD); |
291 | | /* ok: initialize and return */ |
292 | 477 | LOG_FUNC_RETURN(ctx, sc_pkcs15emu_dnie_init(p15card)); |
293 | | #else |
294 | | r = SC_ERROR_WRONG_CARD; |
295 | | LOG_FUNC_RETURN(ctx, r); |
296 | | #endif |
297 | 477 | } |