/src/opensc/src/libopensc/card-openpgp.c
Line | Count | Source |
1 | | /* |
2 | | * card-openpgp.c: Support for OpenPGP card |
3 | | * |
4 | | * Copyright (C) 2003 Olaf Kirch <okir@suse.de> |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | /* |
22 | | * Specifications: |
23 | | * (all available from: https://gnupg.org/ftp/specs/) |
24 | | * https://gnupg.org/ftp/specs/openpgp-card-1.0.pdf (obsolete) |
25 | | * https://gnupg.org/ftp/specs/openpgp-card-1.1.pdf |
26 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-2.0.pdf |
27 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-2.1.pdf |
28 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-2.2.pdf |
29 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.0.pdf |
30 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.1.pdf |
31 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.2.pdf |
32 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.3.pdf |
33 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.3.0.pdf |
34 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.3.1.pdf |
35 | | * https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.4.pdf |
36 | | */ |
37 | | |
38 | | #ifdef HAVE_CONFIG_H |
39 | | #include "config.h" |
40 | | #endif |
41 | | |
42 | | #include <stdlib.h> |
43 | | #include <string.h> |
44 | | #include <ctype.h> |
45 | | #include <time.h> |
46 | | |
47 | | #include "internal.h" |
48 | | #include "asn1.h" |
49 | | #include "cardctl.h" |
50 | | #include "errors.h" |
51 | | #ifdef ENABLE_OPENSSL |
52 | | #include <openssl/evp.h> |
53 | | #include <openssl/sha.h> |
54 | | #endif /* ENABLE_OPENSSL */ |
55 | | |
56 | | #include "card-openpgp.h" |
57 | | |
58 | | |
59 | | static const char default_cardname[] = "OpenPGP card"; |
60 | | static const char default_cardname_v1[] = "OpenPGP card v1.x"; |
61 | | static const char default_cardname_v2[] = "OpenPGP card v2.x"; |
62 | | static const char default_cardname_v3[] = "OpenPGP card v3.x"; |
63 | | |
64 | | |
65 | | static const struct sc_atr_table pgp_atrs[] = { |
66 | | { "3b:fa:13:00:ff:81:31:80:45:00:31:c1:73:c0:01:00:00:90:00:b1", NULL, default_cardname_v1, SC_CARD_TYPE_OPENPGP_V1, 0, NULL }, |
67 | | { "3b:da:18:ff:81:b1:fe:75:1f:03:00:31:c5:73:c0:01:40:00:90:00:0c", NULL, default_cardname_v2, SC_CARD_TYPE_OPENPGP_V2, 0, NULL }, |
68 | | { |
69 | | "3b:da:11:ff:81:b1:fe:55:1f:03:00:31:84:73:80:01:80:00:90:00:e4", |
70 | | "ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:00:ff:ff:00", |
71 | | "Gnuk v1.x.x (OpenPGP v2.0)", SC_CARD_TYPE_OPENPGP_GNUK, 0, NULL |
72 | | }, |
73 | | { "3b:fc:13:00:00:81:31:fe:15:59:75:62:69:6b:65:79:4e:45:4f:72:33:e1", NULL, "Yubikey NEO (OpenPGP v2.0)", SC_CARD_TYPE_OPENPGP_V2, 0, NULL }, |
74 | | { "3b:f8:13:00:00:81:31:fe:15:59:75:62:69:6b:65:79:34:d4", NULL, "Yubikey 4 (OpenPGP v2.1)", SC_CARD_TYPE_OPENPGP_V2, 0, NULL }, |
75 | | { "3b:fd:13:00:00:81:31:fe:15:80:73:c0:21:c0:57:59:75:62:69:4b:65:79:40", NULL, "Yubikey 5 (OpenPGP v3.4)", SC_CARD_TYPE_OPENPGP_V3, 0, NULL }, |
76 | | { "3b:da:18:ff:81:b1:fe:75:1f:03:00:31:f5:73:c0:01:60:00:90:00:1c", NULL, default_cardname_v3, SC_CARD_TYPE_OPENPGP_V3, 0, NULL }, |
77 | | { NULL, NULL, NULL, 0, 0, NULL } |
78 | | }; |
79 | | |
80 | | |
81 | | static struct sc_card_operations *iso_ops; |
82 | | static struct sc_card_operations pgp_ops; |
83 | | static struct sc_card_driver pgp_drv = { |
84 | | "OpenPGP card", |
85 | | "openpgp", |
86 | | &pgp_ops, |
87 | | NULL, 0, NULL |
88 | | }; |
89 | | |
90 | | // clang-format off |
91 | | static pgp_ec_curves_t ec_curves_openpgp34[] = { |
92 | | /* OpenPGP 3.4+ Ed25519 and Curve25519 */ |
93 | | {{{1, 3, 6, 1, 4, 1, 3029, 1, 5, 1, -1}}, 255, SC_ALGORITHM_XEDDSA}, /* curve25519 for encryption => CKK_EC_MONTGOMERY */ |
94 | | {{{1, 3, 101, 110, -1}}, 255, SC_ALGORITHM_XEDDSA}, /* RFC8410 OID equivalent to curve25519 */ |
95 | | {{{1, 3, 6, 1, 4, 1, 11591, 15, 1, -1}}, 255, SC_ALGORITHM_EDDSA}, /* ed25519 for signatures => CKK_EC_EDWARDS */ |
96 | | {{{1, 3, 101, 112, -1}}, 255, SC_ALGORITHM_EDDSA}, /* RFC8410 OID equivalent to ed25519 */ |
97 | | |
98 | | /* v3.0+ supports: [RFC 4880 & 6637] 0x12 = ECDH, 0x13 = ECDSA */ |
99 | | {{{1, 2, 840, 10045, 3, 1, 7, -1}}, 256, SC_ALGORITHM_EC}, /* ansiX9p256r1 */ |
100 | | {{{1, 3, 132, 0, 34, -1}}, 384, SC_ALGORITHM_EC}, /* ansiX9p384r1 */ |
101 | | {{{1, 3, 132, 0, 35, -1}}, 521, SC_ALGORITHM_EC}, /* ansiX9p521r1 */ |
102 | | {{{1, 3, 36, 3, 3, 2, 8, 1, 1, 7, -1}}, 256, SC_ALGORITHM_EC}, /* brainpoolP256r1 */ |
103 | | {{{1, 3, 36, 3, 3, 2, 8, 1, 1, 11, -1}}, 384, SC_ALGORITHM_EC}, /* brainpoolP384r1 */ |
104 | | {{{1, 3, 36, 3, 3, 2, 8, 1, 1, 13, -1}}, 512, SC_ALGORITHM_EC}, /* brainpoolP512r1 */ |
105 | | {{{-1}}, 0, 0 } /* This entry must not be touched. */ |
106 | | }; |
107 | | |
108 | | #ifdef ENABLE_OPENSSL |
109 | | static pgp_ec_curves_alt_t ec_curves_alt[] = { |
110 | | {{{1, 3, 6, 1, 4, 1, 3029, 1, 5, 1, -1}}, {{1, 3, 101, 110, -1}}, 255}, /* curve25519 CKK_EC_MONTGOMERY X25519 */ |
111 | | {{{1, 3, 6, 1, 4, 1, 11591, 15, 1, -1}}, {{1, 3, 101, 112, -1}}, 255}, /* ed25519 CKK_EC_EDWARDS Ed25519 */ |
112 | | {{{-1}}, {{-1}}, 0 } /* This entry must not be touched. */ |
113 | | }; |
114 | | |
115 | | #endif /* ENABLE_OPENSSL */ |
116 | | |
117 | | static pgp_ec_curves_t *ec_curves_openpgp = ec_curves_openpgp34 + 4; |
118 | | |
119 | | struct sc_object_id curve25519_oid = {{1, 3, 6, 1, 4, 1, 3029, 1, 5, 1, -1}}; |
120 | | struct sc_object_id X25519_oid = {{1, 3, 101, 110, -1}}; /* need to check for RFC8410 version? */ |
121 | | |
122 | | /* Gnuk supports NIST, SECG and Curve25519 since version 1.2 */ |
123 | | static pgp_ec_curves_t ec_curves_gnuk[] = { |
124 | | {{{1, 2, 840, 10045, 3, 1, 7, -1}}, 256, SC_ALGORITHM_EC}, /* ansiX9p256r1 */ |
125 | | {{{1, 3, 132, 0, 10, -1}}, 256, SC_ALGORITHM_EC}, /* secp256k1 */ |
126 | | {{{1, 3, 6, 1, 4, 1, 3029, 1, 5, 1, -1}}, 255, SC_ALGORITHM_XEDDSA}, /* curve25519 for encryption => CKK_EC_MONTGOMERY */ |
127 | | {{{1, 3, 101, 110, -1}}, 255, SC_ALGORITHM_XEDDSA}, /* RFC8410 OID equivalent to curve25519 */ |
128 | | {{{1, 3, 6, 1, 4, 1, 11591, 15, 1, -1}}, 255, SC_ALGORITHM_EDDSA}, /* ed25519 for signatures => CKK_EC_EDWARDS */ |
129 | | {{{1, 3, 101, 112, -1}}, 255, SC_ALGORITHM_EDDSA}, /* RFC8410 OID equivalent to ed25519 */ |
130 | | {{{-1}}, 0, 0} /* This entry must not be touched. */ |
131 | | }; |
132 | | // clang-format on |
133 | | |
134 | | /* |
135 | | * The OpenPGP card doesn't have a file system, instead everything |
136 | | * is stored in data objects that are accessed through GET/PUT. |
137 | | * |
138 | | * However, much inside OpenSC's pkcs15 implementation is based on |
139 | | * the assumption that we have a file system. So we fake one here. |
140 | | * |
141 | | * Selecting the MF causes us to select the OpenPGP AID. |
142 | | * |
143 | | * Everything else is mapped to "file" IDs. |
144 | | */ |
145 | | |
146 | | static int pgp_get_card_features(sc_card_t *card); |
147 | | static int pgp_finish(sc_card_t *card); |
148 | | static void pgp_free_blobs(pgp_blob_t *); |
149 | | |
150 | | static int pgp_get_blob(sc_card_t *card, pgp_blob_t *blob, |
151 | | unsigned int id, pgp_blob_t **ret); |
152 | | static pgp_blob_t *pgp_new_blob(sc_card_t *, pgp_blob_t *, unsigned int, sc_file_t *); |
153 | | static void pgp_free_blob(pgp_blob_t *); |
154 | | static int pgp_get_pubkey(sc_card_t *, unsigned int, u8 *, size_t); |
155 | | static int pgp_get_pubkey_pem(sc_card_t *, unsigned int, u8 *, size_t); |
156 | | static int pgp_enumerate_blob(sc_card_t *card, pgp_blob_t *blob); |
157 | | |
158 | | // clang-format off |
159 | | static pgp_do_info_t pgp1x_objects[] = { /* OpenPGP card spec 1.1 */ |
160 | | { 0x004f, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
161 | | { 0x005b, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
162 | | { 0x005e, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
163 | | { 0x0065, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
164 | | { 0x006e, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
165 | | { 0x0073, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
166 | | { 0x007a, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
167 | | { 0x0081, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
168 | | { 0x0082, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
169 | | { 0x0093, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
170 | | { 0x00c0, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
171 | | { 0x00c1, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
172 | | { 0x00c2, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
173 | | { 0x00c3, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
174 | | { 0x00c4, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
175 | | { 0x00c5, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
176 | | { 0x00c6, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
177 | | { 0x00c7, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
178 | | { 0x00c8, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
179 | | { 0x00c9, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
180 | | { 0x00ca, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
181 | | { 0x00cb, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
182 | | { 0x00cc, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
183 | | { 0x00cd, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
184 | | { 0x00ce, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
185 | | { 0x00cf, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
186 | | { 0x00d0, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
187 | | { 0x00e0, CONSTRUCTED, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
188 | | { 0x00e1, CONSTRUCTED, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
189 | | { 0x00e2, CONSTRUCTED, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
190 | | { 0x0101, SIMPLE, READ_ALWAYS | WRITE_PIN2, sc_get_data, sc_put_data }, |
191 | | { 0x0102, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
192 | | { 0x0103, SIMPLE, READ_PIN2 | WRITE_PIN2, sc_get_data, sc_put_data }, |
193 | | { 0x0104, SIMPLE, READ_PIN3 | WRITE_PIN3, sc_get_data, sc_put_data }, |
194 | | { 0x3f00, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
195 | | { 0x5f2d, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
196 | | { 0x5f35, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
197 | | { 0x5f50, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
198 | | { 0x7f49, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
199 | | { DO_AUTH, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, pgp_get_pubkey, NULL }, |
200 | | { DO_AUTH_SYM, SIMPLE, READ_ALWAYS | WRITE_PIN3, pgp_get_pubkey_pem, NULL }, |
201 | | { DO_SIGN, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, pgp_get_pubkey, NULL }, |
202 | | { DO_SIGN_SYM, SIMPLE, READ_ALWAYS | WRITE_PIN3, pgp_get_pubkey_pem, NULL }, |
203 | | { DO_ENCR, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, pgp_get_pubkey, NULL }, |
204 | | { DO_ENCR_SYM, SIMPLE, READ_ALWAYS | WRITE_PIN3, pgp_get_pubkey_pem, NULL }, |
205 | | { 0, 0, 0, NULL, NULL }, |
206 | | }; |
207 | | |
208 | | static pgp_do_info_t pgp34_objects[] = { /**** OpenPGP card spec 3.4 ****/ |
209 | | { 0x00d9, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
210 | | { 0x00da, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
211 | | { 0x00db, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
212 | | { 0x00dc, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
213 | | { 0x00de, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
214 | | { 0x00de, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
215 | | /* DO FA is CONSTRUCTED in spec; we treat it as SIMPLE for the time being */ |
216 | | { 0x00fa, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
217 | | /* DO FB is CONSTRUCTED in spec; we treat it as SIMPLE for the time being */ |
218 | | { 0x00fb, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
219 | | /* DO FC is CONSTRUCTED in spec; we treat it as SIMPLE for the time being */ |
220 | | { 0x00fc, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
221 | | /**** OpenPGP card spec 3.3 ****/ |
222 | | { DO_KDF, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
223 | | /**** OpenPGP card spec 3.0 - 3.2 ****/ |
224 | | { 0x00d6, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
225 | | { 0x00d7, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
226 | | { 0x00d8, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
227 | | /* DO 7F66 is CONSTRUCTED in spec; we treat it as SIMPLE: no need to parse TLV */ |
228 | | { 0x7f66, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, sc_put_data }, |
229 | | /* DO 7F74 is CONSTRUCTED in spec; we treat it as SIMPLE for the time being */ |
230 | | { 0x7f74, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, sc_put_data }, |
231 | | /**** OpenPGP card spec 2.1 & 2.2 ****/ |
232 | | { 0x00d5, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
233 | | /**** OpenPGP card spec 2.0 ****/ |
234 | | { 0x004d, CONSTRUCTED, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
235 | | { 0x004f, SIMPLE, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
236 | | { 0x005b, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
237 | | { 0x005e, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
238 | | { 0x0065, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
239 | | { 0x006e, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
240 | | { 0x0073, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
241 | | { 0x007a, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
242 | | { 0x0081, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
243 | | { 0x0082, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
244 | | { 0x0093, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
245 | | { 0x00c0, SIMPLE, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
246 | | { 0x00c1, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
247 | | { 0x00c2, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
248 | | { 0x00c3, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
249 | | { 0x00c4, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
250 | | { 0x00c5, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
251 | | { 0x00c6, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
252 | | { 0x00c7, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
253 | | { 0x00c8, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
254 | | { 0x00c9, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
255 | | { 0x00ca, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
256 | | { 0x00cb, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
257 | | { 0x00cc, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
258 | | { 0x00cd, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
259 | | { 0x00ce, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
260 | | { 0x00cf, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
261 | | { 0x00d0, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
262 | | { 0x00d1, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
263 | | { 0x00d2, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
264 | | { 0x00d3, SIMPLE, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
265 | | { 0x00f4, CONSTRUCTED, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
266 | | { 0x0101, SIMPLE, READ_ALWAYS | WRITE_PIN2, sc_get_data, sc_put_data }, |
267 | | { 0x0102, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
268 | | { 0x0103, SIMPLE, READ_PIN2 | WRITE_PIN2, sc_get_data, sc_put_data }, |
269 | | { 0x0104, SIMPLE, READ_PIN3 | WRITE_PIN3, sc_get_data, sc_put_data }, |
270 | | { 0x3f00, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
271 | | { 0x5f2d, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
272 | | { 0x5f35, SIMPLE, READ_ALWAYS | WRITE_PIN3, NULL, sc_put_data }, |
273 | | { 0x5f48, CONSTRUCTED, READ_NEVER | WRITE_PIN3, NULL, sc_put_data }, |
274 | | { 0x5f50, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
275 | | { 0x5f52, SIMPLE, READ_ALWAYS | WRITE_NEVER, sc_get_data, NULL }, |
276 | | /* DO 7F21 is CONSTRUCTED in spec; we treat it as SIMPLE: no need to parse TLV */ |
277 | | { DO_CERT, SIMPLE, READ_ALWAYS | WRITE_PIN3, sc_get_data, sc_put_data }, |
278 | | { 0x7f48, CONSTRUCTED, READ_NEVER | WRITE_NEVER, NULL, NULL }, |
279 | | { 0x7f49, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, NULL, NULL }, |
280 | | { DO_AUTH, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, pgp_get_pubkey, NULL }, |
281 | | /* The DOs 0xA401, 0xB601, 0xB801 are virtual DOs, they do not represent any real DO. |
282 | | * However, their R/W access condition may block the process of importing key in pkcs15init. |
283 | | * So we set their accesses condition as WRITE_PIN3 (writable). */ |
284 | | { DO_AUTH_SYM, SIMPLE, READ_ALWAYS | WRITE_PIN3, pgp_get_pubkey_pem, NULL }, |
285 | | { DO_SIGN, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, pgp_get_pubkey, NULL }, |
286 | | { DO_SIGN_SYM, SIMPLE, READ_ALWAYS | WRITE_PIN3, pgp_get_pubkey_pem, NULL }, |
287 | | { DO_ENCR, CONSTRUCTED, READ_ALWAYS | WRITE_NEVER, pgp_get_pubkey, NULL }, |
288 | | { DO_ENCR_SYM, SIMPLE, READ_ALWAYS | WRITE_PIN3, pgp_get_pubkey_pem, NULL }, |
289 | | { 0, 0, 0, NULL, NULL }, |
290 | | }; |
291 | | // clang-format on |
292 | | |
293 | | static pgp_do_info_t *pgp33_objects = pgp34_objects + 9; |
294 | | static pgp_do_info_t *pgp30_objects = pgp34_objects + 10; |
295 | | static pgp_do_info_t *pgp21_objects = pgp34_objects + 15; |
296 | | static pgp_do_info_t *pgp20_objects = pgp34_objects + 16; |
297 | | |
298 | | |
299 | | /** |
300 | | * Internal: get OpenPGP application identifier from AID DO 004F |
301 | | */ |
302 | | static int |
303 | | get_full_pgp_aid(sc_card_t *card, sc_file_t *file) |
304 | 1.85k | { |
305 | 1.85k | int r = SC_ERROR_INVALID_ARGUMENTS; |
306 | | |
307 | 1.85k | if (file != NULL) { |
308 | | /* explicitly get the full aid */ |
309 | 1.85k | r = sc_get_data(card, 0x004F, file->name, sizeof file->name); |
310 | 1.85k | file->namelen = MAX(r, 0); |
311 | 1.85k | } |
312 | | |
313 | 1.85k | return r; |
314 | 1.85k | } |
315 | | |
316 | | |
317 | | /** |
318 | | * ABI: check if card's ATR matches one of driver's |
319 | | * or if the OpenPGP application is present on the card. |
320 | | */ |
321 | | static int |
322 | | pgp_match_card(sc_card_t *card) |
323 | 3.55k | { |
324 | 3.55k | int i; |
325 | | |
326 | 3.55k | LOG_FUNC_CALLED(card->ctx); |
327 | | |
328 | 3.55k | i = _sc_match_atr(card, pgp_atrs, &card->type); |
329 | 3.55k | if (i >= 0) { |
330 | 12 | card->name = pgp_atrs[i].name; |
331 | 12 | LOG_FUNC_RETURN(card->ctx, 1); |
332 | 12 | } |
333 | 3.54k | else { |
334 | 3.54k | sc_path_t partial_aid; |
335 | 3.54k | sc_file_t *file = NULL; |
336 | | |
337 | | /* select application "OpenPGP" */ |
338 | 3.54k | sc_format_path("D276:0001:2401", &partial_aid); |
339 | 3.54k | partial_aid.type = SC_PATH_TYPE_DF_NAME; |
340 | | /* OpenPGP card only supports selection *with* requested FCI */ |
341 | 3.54k | i = iso_ops->select_file(card, &partial_aid, &file); |
342 | 3.54k | if (SC_SUCCESS == i) { |
343 | 935 | card->type = SC_CARD_TYPE_OPENPGP_BASE; |
344 | 935 | card->name = default_cardname; |
345 | | |
346 | 935 | if (file->namelen != 16) |
347 | 934 | (void) get_full_pgp_aid(card, file); |
348 | 935 | if (file->namelen == 16) { |
349 | 80 | unsigned char major = BCD2UCHAR(file->name[6]); |
350 | | |
351 | 80 | switch (major) { |
352 | 4 | case 1: |
353 | 4 | card->type = SC_CARD_TYPE_OPENPGP_V1; |
354 | 4 | card->name = default_cardname_v1; |
355 | 4 | break; |
356 | 7 | case 2: |
357 | 7 | card->type = SC_CARD_TYPE_OPENPGP_V2; |
358 | 7 | card->name = default_cardname_v2; |
359 | 7 | break; |
360 | 29 | case 3: |
361 | 29 | card->type = SC_CARD_TYPE_OPENPGP_V3; |
362 | 29 | card->name = default_cardname_v3; |
363 | 29 | break; |
364 | 40 | default: |
365 | 40 | break; |
366 | 80 | } |
367 | 80 | } |
368 | 935 | sc_file_free(file); |
369 | 935 | LOG_FUNC_RETURN(card->ctx, 1); |
370 | 935 | } |
371 | 3.54k | } |
372 | 2.60k | LOG_FUNC_RETURN(card->ctx, 0); |
373 | 2.60k | } |
374 | | |
375 | | |
376 | | /* populate MF - add matching blobs listed in the pgp_objects table */ |
377 | | int populate_blobs_to_mf(sc_card_t *card, struct pgp_priv_data *priv) |
378 | 918 | { |
379 | 918 | pgp_do_info_t *info; |
380 | 57.4k | for (info = priv->pgp_objects; (info != NULL) && (info->id > 0); info++) { |
381 | 56.5k | if (((info->access & READ_MASK) != READ_NEVER) && (info->get_fn != NULL)) { |
382 | 18.0k | pgp_blob_t *child = NULL; |
383 | 18.0k | sc_file_t *file = sc_file_new(); |
384 | | |
385 | 18.0k | child = pgp_new_blob(card, priv->mf, info->id, file); |
386 | | |
387 | | /* catch out of memory condition */ |
388 | 18.0k | if (child == NULL) { |
389 | 0 | sc_file_free(file); |
390 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
391 | 0 | } |
392 | 18.0k | } |
393 | 56.5k | } |
394 | 918 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
395 | 918 | } |
396 | | |
397 | | /** |
398 | | * ABI: initialize driver & allocate private data. |
399 | | */ |
400 | | static int |
401 | | pgp_init(sc_card_t *card) |
402 | 947 | { |
403 | 947 | struct pgp_priv_data *priv; |
404 | 947 | sc_path_t path; |
405 | 947 | sc_file_t *file = NULL; |
406 | 947 | int r, i; |
407 | | |
408 | 947 | LOG_FUNC_CALLED(card->ctx); |
409 | | |
410 | 947 | priv = calloc (1, sizeof *priv); |
411 | 947 | if (!priv) |
412 | 947 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
413 | 947 | card->drv_data = priv; |
414 | | |
415 | 947 | card->cla = 0x00; |
416 | | |
417 | | /* select application "OpenPGP" */ |
418 | 947 | sc_format_path("D276:0001:2401", &path); |
419 | 947 | path.type = SC_PATH_TYPE_DF_NAME; |
420 | 947 | if ((r = iso_ops->select_file(card, &path, &file)) < 0) { |
421 | 22 | sc_file_free(file); |
422 | 22 | pgp_finish(card); |
423 | 22 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_CARD); |
424 | 22 | } |
425 | | |
426 | | /* defensive programming check */ |
427 | 925 | if (!file) { |
428 | 0 | pgp_finish(card); |
429 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_CARD); |
430 | 0 | } |
431 | | |
432 | 925 | if (file->namelen != 16) { |
433 | | /* explicitly get the full aid */ |
434 | 924 | r = get_full_pgp_aid(card, file); |
435 | 924 | if (r < 0) { |
436 | 7 | sc_file_free(file); |
437 | 7 | pgp_finish(card); |
438 | 7 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_CARD); |
439 | 7 | } |
440 | 924 | } |
441 | | |
442 | | /* read information from AID */ |
443 | 918 | if (file->namelen == 16) { |
444 | 747 | static char card_name[SC_MAX_APDU_BUFFER_SIZE] = "OpenPGP card"; |
445 | | |
446 | | /* OpenPGP card spec 1.1, 2.x & 3.x, section 4.2.1 & 4.1.2.1 */ |
447 | 747 | priv->bcd_version = bebytes2ushort(file->name + 6); |
448 | 747 | card->version.fw_major = card->version.hw_major = BCD2UCHAR(file->name[6]); |
449 | 747 | card->version.fw_minor = card->version.hw_minor = BCD2UCHAR(file->name[7]); |
450 | | |
451 | | /* for "standard" cards, include detailed card version & serial no. in card name */ |
452 | 747 | if (card->name == default_cardname_v1 || |
453 | 745 | card->name == default_cardname_v2 || |
454 | 742 | card->name == default_cardname_v3) { |
455 | 29 | snprintf(card_name, sizeof(card_name), |
456 | 29 | "OpenPGP card v%u.%u (%04X %08lX)", |
457 | 29 | card->version.hw_major, card->version.hw_minor, |
458 | 29 | bebytes2ushort(file->name + 8), |
459 | 29 | bebytes2ulong(file->name + 10)); |
460 | 29 | } |
461 | 718 | else if (card->name != NULL) { |
462 | | /* for other cards, append serial number to the card name */ |
463 | 718 | snprintf(card_name, sizeof(card_name), |
464 | 718 | "%s (%04X %08lX)", |
465 | 718 | card->name, |
466 | 718 | bebytes2ushort(file->name + 8), |
467 | 718 | bebytes2ulong(file->name + 10)); |
468 | 718 | } |
469 | 747 | card->name = card_name; |
470 | | |
471 | | /* GPG compatibility: set card's serial number to manufacturer ID + serial number */ |
472 | 747 | memcpy(card->serialnr.value, file->name + 8, 6); |
473 | 747 | card->serialnr.len = 6; |
474 | 747 | } else { |
475 | | /* set detailed card version */ |
476 | 171 | switch (card->type) { |
477 | 4 | case SC_CARD_TYPE_OPENPGP_V3: |
478 | 4 | priv->bcd_version = OPENPGP_CARD_3_0; |
479 | 4 | break; |
480 | 2 | case SC_CARD_TYPE_OPENPGP_GNUK: |
481 | 5 | case SC_CARD_TYPE_OPENPGP_V2: |
482 | 5 | priv->bcd_version = OPENPGP_CARD_2_0; |
483 | 5 | break; |
484 | 162 | default: |
485 | 162 | priv->bcd_version = OPENPGP_CARD_1_1; |
486 | 162 | break; |
487 | 171 | } |
488 | 171 | } |
489 | | |
490 | | /* set pointer to correct list of card objects */ |
491 | 918 | priv->pgp_objects = (priv->bcd_version < OPENPGP_CARD_2_0) ? pgp1x_objects |
492 | 918 | : (priv->bcd_version < OPENPGP_CARD_2_1) ? pgp20_objects |
493 | 721 | : (priv->bcd_version < OPENPGP_CARD_3_0) ? pgp21_objects |
494 | 714 | : (priv->bcd_version < OPENPGP_CARD_3_3) ? pgp30_objects |
495 | 684 | : (priv->bcd_version < OPENPGP_CARD_3_4) ? pgp33_objects |
496 | 677 | : pgp34_objects; |
497 | | |
498 | | /* With gnuk, we use different curves */ |
499 | 918 | if (card->type == SC_CARD_TYPE_OPENPGP_GNUK) { |
500 | 9 | priv->ec_curves = ec_curves_gnuk; |
501 | 909 | } else if (priv->bcd_version >= OPENPGP_CARD_3_4) { |
502 | 672 | priv->ec_curves = ec_curves_openpgp34; |
503 | 672 | } else { |
504 | 237 | priv->ec_curves = ec_curves_openpgp; |
505 | 237 | } |
506 | | |
507 | | /* change file path to MF for reuse in MF */ |
508 | 918 | sc_format_path("3f00", &file->path); |
509 | | |
510 | | /* set up the root of our fake file tree */ |
511 | | /* Transfers ownership of the file to the priv->mf structure */ |
512 | 918 | priv->mf = pgp_new_blob(card, NULL, 0x3f00, file); |
513 | 918 | if (!priv->mf) { |
514 | 0 | sc_file_free(file); |
515 | 0 | pgp_finish(card); |
516 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
517 | 0 | } |
518 | | |
519 | | /* select MF */ |
520 | 918 | priv->current = priv->mf; |
521 | | |
522 | 918 | r = populate_blobs_to_mf(card, priv); |
523 | 918 | if (r < 0) { |
524 | 0 | pgp_finish(card); |
525 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
526 | 0 | } |
527 | | |
528 | | /* get card_features from ATR & DOs */ |
529 | 918 | if (pgp_get_card_features(card)) { |
530 | 136 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
531 | 136 | } |
532 | | |
533 | | /* if algorithm attributes can be changed, |
534 | | * add supported algorithms based on specification for pkcs15-init */ |
535 | 782 | if (priv->ext_caps & EXT_CAP_ALG_ATTR_CHANGEABLE) { |
536 | 249 | unsigned long flags_rsa, flags_ecc, ext_flags; |
537 | 249 | unsigned long flags_eddsa, flags_xeddsa; |
538 | | |
539 | | /* OpenPGP card spec 1.1 & 2.x, section 7.2.9 & 7.2.10 / v3.x section 7.2.11 & 7.2.12 */ |
540 | 249 | flags_rsa = SC_ALGORITHM_RSA_PAD_PKCS1| |
541 | 249 | SC_ALGORITHM_RSA_HASH_NONE| |
542 | 249 | SC_ALGORITHM_ONBOARD_KEY_GEN; |
543 | 249 | flags_ecc = SC_ALGORITHM_ECDSA_RAW| |
544 | 249 | SC_ALGORITHM_ECDH_CDH_RAW| |
545 | 249 | SC_ALGORITHM_ECDSA_HASH_NONE| |
546 | 249 | SC_ALGORITHM_ONBOARD_KEY_GEN; |
547 | 249 | ext_flags = SC_ALGORITHM_EXT_EC_NAMEDCURVE; |
548 | | |
549 | 249 | flags_eddsa = SC_ALGORITHM_EDDSA_RAW | |
550 | 249 | SC_ALGORITHM_ONBOARD_KEY_GEN; |
551 | | /* xeddsa may allow signing at some time */ |
552 | 249 | flags_xeddsa = SC_ALGORITHM_ECDH_CDH_RAW | |
553 | 249 | SC_ALGORITHM_ONBOARD_KEY_GEN; |
554 | | |
555 | 249 | switch (card->type) { |
556 | 16 | case SC_CARD_TYPE_OPENPGP_V3: |
557 | | /* RSA 1024 was removed for v3+ */ |
558 | 16 | _sc_card_add_rsa_alg(card, 4096, flags_rsa, 0); |
559 | 16 | _sc_card_add_rsa_alg(card, 3072, flags_rsa, 0); |
560 | | /* fallthrough */ |
561 | 16 | case SC_CARD_TYPE_OPENPGP_GNUK: |
562 | 16 | _sc_card_add_rsa_alg(card, 2048, flags_rsa, 0); |
563 | 148 | for (i=0; priv->ec_curves[i].oid.value[0] >= 0; i++) |
564 | 132 | { |
565 | 132 | if (priv->ec_curves[i].key_type == SC_ALGORITHM_EC) |
566 | 96 | _sc_card_add_ec_alg(card, priv->ec_curves[i].size, |
567 | 96 | flags_ecc, ext_flags, &priv->ec_curves[i].oid); |
568 | 36 | else if (priv->ec_curves[i].key_type == SC_ALGORITHM_EDDSA) |
569 | 18 | _sc_card_add_eddsa_alg(card, priv->ec_curves[i].size, |
570 | 18 | flags_eddsa, ext_flags, &priv->ec_curves[i].oid); |
571 | 18 | else if (priv->ec_curves[i].key_type == SC_ALGORITHM_XEDDSA) |
572 | 18 | _sc_card_add_xeddsa_alg(card, priv->ec_curves[i].size, |
573 | 18 | flags_xeddsa, ext_flags, &priv->ec_curves[i].oid); |
574 | 0 | else |
575 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL); |
576 | 132 | } |
577 | 16 | break; |
578 | 16 | case SC_CARD_TYPE_OPENPGP_V2: |
579 | 233 | default: |
580 | 233 | _sc_card_add_rsa_alg(card, 1024, flags_rsa, 0); |
581 | 233 | _sc_card_add_rsa_alg(card, 2048, flags_rsa, 0); |
582 | 233 | _sc_card_add_rsa_alg(card, 3072, flags_rsa, 0); |
583 | 233 | _sc_card_add_rsa_alg(card, 4096, flags_rsa, 0); |
584 | 233 | break; |
585 | 249 | } |
586 | 249 | } |
587 | | |
588 | 782 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
589 | 782 | } |
590 | | |
591 | | |
592 | | /** |
593 | | * Internal: parse historic bytes to get card capabilities. |
594 | | */ |
595 | | static void |
596 | | pgp_parse_hist_bytes(sc_card_t *card, u8 *ctlv, size_t ctlv_len) |
597 | 95 | { |
598 | 95 | struct pgp_priv_data *priv = DRVDATA(card); |
599 | 95 | const u8 *ptr; |
600 | | |
601 | | /* IS07816-4 hist bytes: 3rd function table */ |
602 | 95 | if ((ptr = sc_compacttlv_find_tag(ctlv, ctlv_len, 0x73, NULL)) != NULL) { |
603 | | /* bit 0x40 in byte 3 of TL 0x73 means "extended Le/Lc" */ |
604 | 34 | if (ptr[2] & 0x40) { |
605 | 16 | card->caps |= SC_CARD_CAP_APDU_EXT; |
606 | 16 | priv->ext_caps |= EXT_CAP_APDU_EXT; |
607 | 16 | } |
608 | | /* bit 0x80 in byte 3 of TL 0x73 means "Command chaining" */ |
609 | 34 | if (ptr[2] & 0x80) { |
610 | 19 | priv->ext_caps |= EXT_CAP_CHAINING; |
611 | 19 | } |
612 | 34 | } |
613 | | |
614 | 95 | if ((priv->bcd_version >= OPENPGP_CARD_3_0) && |
615 | 77 | ((ptr = sc_compacttlv_find_tag(ctlv, ctlv_len, 0x31, NULL)) != NULL)) { |
616 | | // ToDo ... |
617 | 28 | } |
618 | 95 | } |
619 | | |
620 | | |
621 | | /** |
622 | | * Internal: parse an algorithm attributes DO |
623 | | **/ |
624 | | static int |
625 | | pgp_parse_algo_attr_blob(sc_card_t *card, const pgp_blob_t *blob, |
626 | | sc_cardctl_openpgp_key_gen_store_info_t *key_info) |
627 | 15.0k | { |
628 | 15.0k | struct pgp_priv_data *priv = DRVDATA(card); |
629 | 15.0k | struct sc_object_id oid; |
630 | 15.0k | unsigned int j, r; |
631 | | |
632 | 15.0k | LOG_FUNC_CALLED(card->ctx); |
633 | | |
634 | 15.0k | if (blob == NULL || blob->data == NULL || blob->len == 0 || |
635 | 11.7k | blob->id < 0x00c1 || blob->id > 0x00c3 || key_info == NULL) { |
636 | 3.36k | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCORRECT_PARAMETERS); |
637 | 3.36k | } |
638 | | |
639 | 11.7k | key_info->key_id = blob->id - 0x00c0; /* attribute algorithm blobs are C1 - C3 */ |
640 | | |
641 | 11.7k | switch (blob->data[0]) { |
642 | 1.76k | case SC_OPENPGP_KEYALGO_RSA: |
643 | 1.76k | if (blob->len < 5) { |
644 | 266 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCORRECT_PARAMETERS); |
645 | 266 | } |
646 | | |
647 | 1.49k | key_info->algorithm = SC_OPENPGP_KEYALGO_RSA; |
648 | 1.49k | key_info->u.rsa.modulus_len = bebytes2ushort(blob->data + 1); |
649 | 1.49k | key_info->u.rsa.exponent_len = bebytes2ushort(blob->data + 3); |
650 | | |
651 | 1.49k | key_info->u.rsa.keyformat = (blob->len > 5) |
652 | 1.49k | ? blob->data[5] |
653 | 1.49k | : SC_OPENPGP_KEYFORMAT_RSA_STD; |
654 | 1.49k | break; |
655 | 5.36k | case SC_OPENPGP_KEYALGO_ECDH: |
656 | 7.07k | case SC_OPENPGP_KEYALGO_ECDSA: |
657 | 8.59k | case SC_OPENPGP_KEYALGO_EDDSA: |
658 | | |
659 | | /* SC_OPENPGP_KEYALGO_ECDH || SC_OPENPGP_KEYALGO_ECDSA || SC_OPENPGP_KEYALGO_EDDSA */ |
660 | 8.59k | key_info->algorithm = blob->data[0]; |
661 | | |
662 | | /* last byte is set to 0xFF if pubkey import is supported */ |
663 | 8.59k | if (blob->data[blob->len-1] == SC_OPENPGP_KEYFORMAT_EC_STDPUB){ |
664 | 2.26k | if (blob->len < 3) |
665 | 212 | return SC_ERROR_INCORRECT_PARAMETERS; |
666 | 2.05k | key_info->u.ec.oidv_len = blob->len - 2; |
667 | 2.05k | key_info->u.ec.keyformat = SC_OPENPGP_KEYFORMAT_EC_STDPUB; |
668 | 2.05k | } |
669 | 6.32k | else { |
670 | | /* otherwise, last byte could be 00, so let's ignore it, as |
671 | | * it is not part of OID */ |
672 | 6.32k | if (blob->len < 2) |
673 | 2.64k | return SC_ERROR_INCORRECT_PARAMETERS; |
674 | 3.68k | if (blob->data[blob->len-1] == SC_OPENPGP_KEYFORMAT_EC_STD) |
675 | 674 | key_info->u.ec.oidv_len = blob->len - 2; |
676 | 3.00k | else |
677 | 3.00k | key_info->u.ec.oidv_len = blob->len - 1; |
678 | 3.68k | key_info->u.ec.keyformat = SC_OPENPGP_KEYFORMAT_EC_STD; |
679 | 3.68k | } |
680 | | |
681 | | /* Create copy of oid from blob */ |
682 | 5.73k | sc_init_oid(&oid); |
683 | 5.73k | r = sc_asn1_decode_object_id(&blob->data[1], key_info->u.ec.oidv_len, &oid); |
684 | | |
685 | | /* decoding failed, return sc_asn1_decode_object_id error code */ |
686 | 5.73k | if (r > 0){ |
687 | 1.96k | return r; |
688 | 1.96k | } |
689 | | /* compare with list of supported ec_curves */ |
690 | 18.4k | for (j = 0; priv->ec_curves[j].oid.value[0] >= 0; j++) { |
691 | 17.9k | if (sc_compare_oid(&priv->ec_curves[j].oid, &oid)) { |
692 | 3.17k | sc_log(card->ctx, "Matched EC oid %s (%d)", |
693 | 3.17k | sc_dump_oid(&oid), j); |
694 | 3.17k | key_info->u.ec.oid = priv->ec_curves[j].oid; |
695 | 3.17k | key_info->u.ec.key_length = priv->ec_curves[j].size; |
696 | 3.17k | break; |
697 | 3.17k | } |
698 | 17.9k | } |
699 | | /* We did not match the OID */ |
700 | 3.76k | if (priv->ec_curves[j].oid.value[0] < 0) { |
701 | 592 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED); |
702 | 592 | } |
703 | 3.17k | break; |
704 | 3.17k | default: |
705 | 1.36k | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
706 | 11.7k | } |
707 | | |
708 | 4.67k | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
709 | 4.67k | } |
710 | | |
711 | | int |
712 | | _pgp_handle_curve25519(sc_card_t *card, |
713 | | sc_cardctl_openpgp_key_gen_store_info_t *key_info, unsigned int do_num) |
714 | 1.80k | { |
715 | 1.80k | if (!card || !key_info) |
716 | 0 | return 0; |
717 | 1.80k | if (!sc_compare_oid(&key_info->u.ec.oid, &curve25519_oid) && |
718 | 1.80k | !sc_compare_oid(&key_info->u.ec.oid, &X25519_oid)) |
719 | 526 | return 0; |
720 | | |
721 | | /* CKM_XEDDSA supports both Sign and Derive, but |
722 | | * OpenPGP card supports only derivation using these |
723 | | * keys as far as I know */ |
724 | 1.28k | _sc_card_add_xeddsa_alg(card, key_info->u.ec.key_length, |
725 | 1.28k | SC_ALGORITHM_ECDH_CDH_RAW | SC_ALGORITHM_ONBOARD_KEY_GEN, 0, &key_info->u.ec.oid); |
726 | 1.28k | sc_log(card->ctx, "DO %uX: Added XEDDSA algorithm (%d), mod_len = %zu", |
727 | 1.28k | do_num, SC_ALGORITHM_XEDDSA, key_info->u.ec.key_length); |
728 | 1.28k | return 1; |
729 | 1.80k | } |
730 | | |
731 | | int |
732 | | _pgp_add_algo(sc_card_t *card, sc_cardctl_openpgp_key_gen_store_info_t *key_info, unsigned int do_num) |
733 | 4.66k | { |
734 | 4.66k | unsigned long flags = 0, ext_flags = 0; |
735 | | |
736 | 4.66k | if (!card || !key_info) |
737 | 0 | return 0; |
738 | | |
739 | | /* [RFC 4880], [draft-ietf-openpgp-crypto-refresh] */ |
740 | 4.66k | switch (key_info->algorithm) { |
741 | 1.49k | case SC_OPENPGP_KEYALGO_RSA: |
742 | | /* OpenPGP card spec 1.1 & 2.x, section 7.2.9 & 7.2.10 / |
743 | | * v3.x section 7.2.11 & 7.2.12 */ |
744 | 1.49k | flags = SC_ALGORITHM_RSA_PAD_PKCS1 | |
745 | 1.49k | SC_ALGORITHM_RSA_HASH_NONE | |
746 | 1.49k | SC_ALGORITHM_ONBOARD_KEY_GEN; /* key gen on card */ |
747 | | |
748 | 1.49k | _sc_card_add_rsa_alg(card, key_info->u.rsa.modulus_len, flags, 0); |
749 | 1.49k | sc_log(card->ctx, "DO %uX: Added RSA algorithm, mod_len = %" |
750 | 1.49k | SC_FORMAT_LEN_SIZE_T"u", |
751 | 1.49k | do_num, key_info->u.rsa.modulus_len); |
752 | 1.49k | break; |
753 | 601 | case SC_OPENPGP_KEYALGO_ECDH: |
754 | | /* The montgomery curve (curve25519) needs to go through |
755 | | * different paths, otherwise we handle it as a normal EC key */ |
756 | 601 | if (_pgp_handle_curve25519(card, key_info, do_num)) |
757 | 332 | break; |
758 | | /* fall through */ |
759 | 1.62k | case SC_OPENPGP_KEYALGO_ECDSA: |
760 | | /* v3.0+: ECC [RFC 4880 & 6637] */ |
761 | | |
762 | | /* Allow curve to be used by both ECDH and ECDSA. |
763 | | * pgp_init set these flags the same way */ |
764 | 1.62k | flags = SC_ALGORITHM_ECDH_CDH_RAW; |
765 | 1.62k | flags |= SC_ALGORITHM_ECDSA_RAW; |
766 | 1.62k | flags |= SC_ALGORITHM_ECDSA_HASH_NONE; |
767 | 1.62k | flags |= SC_ALGORITHM_ONBOARD_KEY_GEN; |
768 | 1.62k | ext_flags = SC_ALGORITHM_EXT_EC_NAMEDCURVE; |
769 | | |
770 | 1.62k | _sc_card_add_ec_alg(card, key_info->u.ec.key_length, flags, ext_flags, |
771 | 1.62k | &key_info->u.ec.oid); |
772 | 1.62k | sc_log(card->ctx, "DO %uX: Added EC algorithm (%d), mod_len = %zu" , |
773 | 1.62k | do_num, key_info->algorithm, key_info->u.ec.key_length); |
774 | 1.62k | break; |
775 | 1.20k | case SC_OPENPGP_KEYALGO_EDDSA: |
776 | | /* EdDSA from draft-ietf-openpgp-rfc4880bis-08 */ |
777 | | /* Handle Yubikey bug, that in DO FA curve25519 has EDDSA algo */ |
778 | 1.20k | if (_pgp_handle_curve25519(card, key_info, do_num)) |
779 | 950 | break; |
780 | 257 | _sc_card_add_eddsa_alg(card, key_info->u.ec.key_length, |
781 | 257 | SC_ALGORITHM_EDDSA_RAW | SC_ALGORITHM_ONBOARD_KEY_GEN, 0, &key_info->u.ec.oid); |
782 | | |
783 | 257 | sc_log(card->ctx, "DO %uX: Added EDDSA algorithm (%d), mod_len = %zu" , |
784 | 257 | do_num, key_info->algorithm, key_info->u.ec.key_length); |
785 | 257 | break; |
786 | 0 | default: |
787 | 0 | sc_log(card->ctx, "DO %uX: Unknown algorithm ID (%d)" , |
788 | 0 | do_num, key_info->algorithm); |
789 | | /* return "false" if we do not understand algo */ |
790 | 0 | return 0; |
791 | 4.66k | } |
792 | | /* return true */ |
793 | 4.66k | return 1; |
794 | 4.66k | } |
795 | | |
796 | | #ifdef ENABLE_OPENSSL |
797 | | static int |
798 | | pgp_decode_kdf_do(sc_card_t *card, struct pgp_priv_data *priv) |
799 | 48 | { |
800 | 48 | pgp_pin_kdf_info_t *pin_kdf_info = (pgp_pin_kdf_info_t *)calloc(1, sizeof(pgp_pin_kdf_info_t)); |
801 | 48 | int r = SC_ERROR_OUT_OF_MEMORY; |
802 | 48 | pgp_blob_t *kdf_blob; |
803 | 48 | const uint8_t *data; |
804 | 48 | size_t data_len; |
805 | 48 | size_t tag_len; |
806 | 48 | const uint8_t *p; |
807 | 48 | pgp_blob_t *blob; |
808 | | |
809 | 48 | if (!pin_kdf_info) { |
810 | 0 | return r; |
811 | 0 | } |
812 | | |
813 | 48 | r = SC_ERROR_INVALID_DATA; |
814 | 48 | priv->pin_kdf_info = NULL; |
815 | 48 | if ((pgp_get_blob(card, priv->mf, DO_KDF, &kdf_blob) != SC_SUCCESS) || (kdf_blob->len < 3)) { |
816 | 21 | sc_log(card->ctx, "KDF DO does not exists or contains invalid data but card claims to support it"); |
817 | 21 | goto out; |
818 | 21 | } |
819 | | |
820 | 27 | data = kdf_blob->data; |
821 | 27 | data_len = kdf_blob->len; |
822 | 27 | p = sc_asn1_find_tag(card->ctx, data, data_len, 0x81, &tag_len); |
823 | 27 | if (!p) { |
824 | 18 | sc_log(card->ctx, "KDF algorithm byte not found"); |
825 | 18 | goto out; |
826 | 18 | } |
827 | 9 | if (tag_len != 1) { |
828 | 1 | sc_log(card->ctx, "Unexpected KDF algorithm byte length, expects 1, got %" SC_FORMAT_LEN_SIZE_T "u", tag_len); |
829 | 1 | goto out; |
830 | 1 | } |
831 | | |
832 | 8 | if (*p != 0x3) { |
833 | 2 | if (*p == 0x0) { |
834 | 1 | r = SC_SUCCESS; |
835 | 1 | sc_log(card->ctx, "KDF derived PIN format disabled"); |
836 | 1 | } else { |
837 | 1 | sc_log(card->ctx, "Unknown KDF algorithm ID %x", *p); |
838 | 1 | } |
839 | 2 | goto out; |
840 | 2 | } |
841 | | |
842 | 6 | p = sc_asn1_find_tag(card->ctx, data, data_len, 0x82, &tag_len); |
843 | 6 | if (!p) { |
844 | 1 | sc_log(card->ctx, "KDF hash algorithm byte not found"); |
845 | 1 | goto out; |
846 | 1 | } |
847 | 5 | if (tag_len != 1) { |
848 | 1 | sc_log(card->ctx, "Unexpected KDF hash algorithm byte length, expects 1, got %" SC_FORMAT_LEN_SIZE_T "u", tag_len); |
849 | 1 | goto out; |
850 | 1 | } |
851 | | |
852 | 4 | switch (*p) { |
853 | 2 | case 0x08: |
854 | 2 | pin_kdf_info->hash_algo = "SHA256"; |
855 | 2 | break; |
856 | 1 | case 0x0A: |
857 | 1 | pin_kdf_info->hash_algo = "SHA512"; |
858 | 1 | break; |
859 | 1 | default: |
860 | 1 | sc_log(card->ctx, "Unknown KDF hash algorithm ID %x", *p); |
861 | 1 | goto out; |
862 | 4 | } |
863 | | |
864 | 3 | p = sc_asn1_find_tag(card->ctx, data, data_len, 0x83, &tag_len); |
865 | 3 | if (!p) { |
866 | 1 | sc_log(card->ctx, "KDF iteration count not found"); |
867 | 1 | goto out; |
868 | 1 | } |
869 | 2 | if (tag_len != 4) { |
870 | 1 | sc_log(card->ctx, "Unexpected KDF iteration count length, expects 4, got %" SC_FORMAT_LEN_SIZE_T "u", tag_len); |
871 | 1 | goto out; |
872 | 1 | } |
873 | 1 | pin_kdf_info->iterations = (uint32_t)bebytes2ulong(p); |
874 | | |
875 | 1 | r = pgp_get_blob(card, kdf_blob, 0x84, &blob); |
876 | 1 | if (r != SC_SUCCESS) { |
877 | 1 | sc_log(card->ctx, "Failed to get salt bytes of user password"); |
878 | 1 | goto out; |
879 | 1 | } |
880 | 0 | pin_kdf_info->userpw_salt = blob; |
881 | |
|
882 | 0 | r = pgp_get_blob(card, kdf_blob, 0x86, &blob); |
883 | 0 | if (r != SC_SUCCESS) { |
884 | 0 | sc_log(card->ctx, "Failed to get salt bytes of admin password"); |
885 | 0 | goto out; |
886 | 0 | } |
887 | 0 | pin_kdf_info->adminpw_salt = blob; |
888 | |
|
889 | 0 | sc_log(card->ctx, "KDF derived PIN format enabled"); |
890 | 0 | priv->pin_kdf_info = pin_kdf_info; |
891 | 0 | return r; |
892 | 48 | out: |
893 | 48 | free(pin_kdf_info); |
894 | 48 | return r; |
895 | 0 | } |
896 | | #endif /* ENABLE_OPENSSL */ |
897 | | |
898 | | /** |
899 | | * Internal: get features of the card: capabilities, ... |
900 | | */ |
901 | | static int |
902 | | pgp_get_card_features(sc_card_t *card) |
903 | 918 | { |
904 | 918 | struct pgp_priv_data *priv = DRVDATA(card); |
905 | 918 | u8 *hist_bytes = card->reader->atr_info.hist_bytes; |
906 | 918 | size_t hist_bytes_len = card->reader->atr_info.hist_bytes_len; |
907 | 918 | unsigned int i; |
908 | 918 | pgp_blob_t *blob, *blob6e, *blob73, *blobfa; |
909 | 918 | int handled_algos = 0; |
910 | | |
911 | 918 | LOG_FUNC_CALLED(card->ctx); |
912 | | |
913 | | /* parse card capabilities from historical bytes in ATR */ |
914 | 918 | if (hist_bytes_len > 0) { |
915 | | /* category indicator 0x00, 0x10 or 0x80 => compact TLV (ISO) */ |
916 | 293 | switch (hist_bytes[0]) { |
917 | 139 | case 0x00: |
918 | 139 | if (hist_bytes_len > 4) { |
919 | 35 | pgp_parse_hist_bytes(card, hist_bytes+1, hist_bytes_len-4); |
920 | 35 | } |
921 | 139 | break; |
922 | 13 | case 0x80: |
923 | 13 | if (hist_bytes_len > 1) { |
924 | 12 | pgp_parse_hist_bytes(card, hist_bytes+1, hist_bytes_len-1); |
925 | 12 | } |
926 | 13 | break; |
927 | 2 | case 0x10: |
928 | 2 | if (hist_bytes_len > 2) { |
929 | 1 | pgp_parse_hist_bytes(card, hist_bytes+2, hist_bytes_len-2); |
930 | 1 | } |
931 | 2 | break; |
932 | 293 | } |
933 | 293 | } |
934 | | |
935 | | /* v1.1 does not support lifecycle via ACTIVATE & TERMINATE: set default */ |
936 | 918 | priv->ext_caps &= ~EXT_CAP_LCS; |
937 | | |
938 | 918 | if (priv->bcd_version >= OPENPGP_CARD_2_0) { |
939 | | /* get card capabilities from "historical bytes" DO */ |
940 | 721 | if ((pgp_get_blob(card, priv->mf, 0x5f52, &blob) >= 0) && |
941 | 721 | (blob->data != NULL) && (blob->data[0] == 0x00)) { |
942 | | |
943 | 50 | if (blob->len > 4) { |
944 | 47 | pgp_parse_hist_bytes(card, blob->data+1, blob->len-4); |
945 | 47 | } |
946 | | |
947 | | /* get card status from historical bytes status indicator */ |
948 | 50 | if ((blob->data[0] == 0x00) && (blob->len >= 4)) { |
949 | 48 | priv->state = blob->data[blob->len-3]; |
950 | | /* state not CARD_STATE_UNKNOWN => LCS supported */ |
951 | 48 | if (priv->state != CARD_STATE_UNKNOWN) |
952 | 31 | priv->ext_caps |= EXT_CAP_LCS; |
953 | 48 | } |
954 | 50 | } |
955 | 721 | } |
956 | | |
957 | 918 | if (priv->bcd_version >= OPENPGP_CARD_3_1) { |
958 | 678 | card->caps |= SC_CARD_CAP_ISO7816_PIN_INFO; |
959 | 678 | } |
960 | | |
961 | 918 | if (priv->bcd_version >= OPENPGP_CARD_3_4) { |
962 | | /* Parse supported algorithms from Algorithm Information DO |
963 | | * see OpenPGP card spec 3.4 section 4.4.3.11 */ |
964 | 676 | if (pgp_get_blob(card, priv->mf, 0x00fa, &blobfa) >= 0) { |
965 | 676 | pgp_blob_t *child; |
966 | 676 | pgp_enumerate_blob(card, blobfa); |
967 | | /* There will be multiple children with the same ID, but |
968 | | * different algos, so we need to iterate over all of them */ |
969 | 25.2k | for (child = blobfa->files; child; child = child->next) { |
970 | 24.5k | if ((child->id < 0x00c1) || (child->id > 0x00c3)) |
971 | 9.58k | continue; |
972 | 14.9k | sc_cardctl_openpgp_key_gen_store_info_t key_info; |
973 | 14.9k | if (pgp_parse_algo_attr_blob(card, child, &key_info) >= 0) |
974 | 4.66k | handled_algos += _pgp_add_algo(card, &key_info, 0x00fa); |
975 | 14.9k | } |
976 | 676 | } |
977 | 676 | } |
978 | | |
979 | | /* v1.1 & v2.x: special DOs are limited to 254 bytes */ |
980 | 918 | priv->max_specialDO_size = 254; |
981 | | |
982 | 918 | if ((pgp_get_blob(card, priv->mf, 0x006e, &blob6e) >= 0) && |
983 | 918 | (pgp_get_blob(card, blob6e, 0x0073, &blob73) >= 0)) { |
984 | | |
985 | | /* get "extended capabilities" DO */ |
986 | 309 | if ((pgp_get_blob(card, blob73, 0x00c0, &blob) >= 0) && |
987 | 272 | (blob->data != NULL) && (blob->len > 0)) { |
988 | 269 | if ((blob->data[0] & 0x01) && (priv->bcd_version >= OPENPGP_CARD_3_3)) { |
989 | 48 | priv->ext_caps |= EXT_CAP_KDF_DO; |
990 | 48 | } |
991 | | /* v2.0+: bit 0x04 in first byte means "algorithm attributes changeable" */ |
992 | 269 | if ((blob->data[0] & 0x04) && |
993 | 254 | (priv->bcd_version >= OPENPGP_CARD_2_0)) |
994 | 249 | priv->ext_caps |= EXT_CAP_ALG_ATTR_CHANGEABLE; |
995 | | /* bit 0x08 in first byte means "support for private use DOs" */ |
996 | 269 | if (blob->data[0] & 0x08) |
997 | 63 | priv->ext_caps |= EXT_CAP_PRIVATE_DO; |
998 | | /* bit 0x10 in first byte means "support for CHV status byte changeable" */ |
999 | 269 | if (blob->data[0] & 0x10) |
1000 | 114 | priv->ext_caps |= EXT_CAP_C4_CHANGEABLE; |
1001 | | /* bit 0x20 in first byte means "support for Key Import" */ |
1002 | 269 | if (blob->data[0] & 0x20) |
1003 | 111 | priv->ext_caps |= EXT_CAP_KEY_IMPORT; |
1004 | | /* bit 0x40 in first byte means "support for Get Challenge" */ |
1005 | 269 | if (blob->data[0] & 0x40) { |
1006 | 52 | card->caps |= SC_CARD_CAP_RNG; |
1007 | 52 | priv->ext_caps |= EXT_CAP_GET_CHALLENGE; |
1008 | 52 | } |
1009 | | /* v2.0+: bit 0x80 in first byte means "support Secure Messaging" */ |
1010 | 269 | if ((blob->data[0] & 0x80) && |
1011 | 106 | (priv->bcd_version >= OPENPGP_CARD_2_0)) |
1012 | 101 | priv->ext_caps |= EXT_CAP_SM; |
1013 | | |
1014 | 269 | if ((priv->bcd_version >= OPENPGP_CARD_2_0) && (blob->len >= 10)) { |
1015 | | /* v2.0+: max. challenge size is at bytes 3-4 */ |
1016 | 260 | priv->max_challenge_size = bebytes2ushort(blob->data + 2); |
1017 | | /* v2.0+: max. cert size it at bytes 5-6 */ |
1018 | 260 | priv->max_cert_size = bebytes2ushort(blob->data + 4); |
1019 | | |
1020 | 260 | if (priv->bcd_version < OPENPGP_CARD_3_0) { |
1021 | | /* v2.x: SM algorithm is at byte 2: 0 == 3DES */ |
1022 | 15 | priv->sm_algo = blob->data[1]; |
1023 | 15 | if ((priv->sm_algo == SM_ALGO_NONE) && (priv->ext_caps & EXT_CAP_SM)) |
1024 | 6 | priv->sm_algo = SM_ALGO_3DES; |
1025 | | |
1026 | | /* v2.x: max. send/receive sizes are at bytes 7-8 resp. 9-10 */ |
1027 | 15 | card->max_send_size = bebytes2ushort(blob->data + 6); |
1028 | 15 | card->max_recv_size = bebytes2ushort(blob->data + 8); |
1029 | 15 | } |
1030 | 245 | else { |
1031 | | /* v3.0+: SM algorithm is at byte 2: 0 == UNKNOWN */ |
1032 | 245 | priv->sm_algo = blob->data[1]; |
1033 | 245 | if ((priv->sm_algo == SM_ALGO_NONE) && (priv->ext_caps & EXT_CAP_SM)) |
1034 | 33 | priv->sm_algo = SM_ALGO_UNKNOWN; |
1035 | | |
1036 | | /* v3.0+: max. size of special DOs is at bytes 7-8 */ |
1037 | 245 | priv->max_specialDO_size = bebytes2ushort(blob->data + 6); |
1038 | 245 | } |
1039 | 260 | if (priv->bcd_version >= OPENPGP_CARD_3_3 && (blob->len >= 10)) { |
1040 | | /* v3.3+: MSE for key numbers 2(DEC) and 3(AUT) supported */ |
1041 | 242 | if (blob->data[9]) |
1042 | 84 | priv->ext_caps |= EXT_CAP_MSE; |
1043 | 242 | } |
1044 | 260 | } |
1045 | 269 | } |
1046 | | |
1047 | | /* get max. PIN length from "CHV status bytes" DO */ |
1048 | 309 | if ((pgp_get_blob(card, blob73, 0x00c4, &blob) >= 0) && |
1049 | 140 | (blob->data != NULL) && (blob->len > 1)) { |
1050 | | /* 2nd byte in "CHV status bytes" DO means "max. PIN length" */ |
1051 | 82 | card->max_pin_len = blob->data[1]; |
1052 | 82 | } |
1053 | | |
1054 | 309 | if (priv->bcd_version >= OPENPGP_CARD_3_0) { |
1055 | | /* v3.0+: get length info from "extended length information" DO */ |
1056 | 269 | if ((pgp_get_blob(card, blob6e, 0x7f66, &blob) >= 0) && |
1057 | 3 | (blob->data != NULL) && (blob->len >= 8)) { |
1058 | | /* kludge: treat as SIMPLE DO and use appropriate offsets */ |
1059 | 1 | card->max_send_size = bebytes2ushort(blob->data + 2); |
1060 | 1 | card->max_recv_size = bebytes2ushort(blob->data + 6); |
1061 | 1 | } |
1062 | 269 | } |
1063 | | |
1064 | 309 | #ifdef ENABLE_OPENSSL |
1065 | 309 | if (priv->ext_caps & EXT_CAP_KDF_DO) { |
1066 | 48 | int r = pgp_decode_kdf_do(card, priv); |
1067 | 48 | if (r != SC_SUCCESS) { |
1068 | 47 | sc_log(card->ctx, "Failed to decode KDF DO: %s", sc_strerror(r)); |
1069 | 47 | } |
1070 | 48 | } |
1071 | 309 | #endif /* ENABLE_OPENSSL */ |
1072 | | |
1073 | | /* if we found at least one usable algo, let's skip other ways to find them */ |
1074 | 309 | if (handled_algos) { |
1075 | 3 | sc_log(card->ctx, "Algo list populated from Algorithm Information DO"); |
1076 | 3 | LOG_FUNC_RETURN(card->ctx, handled_algos); |
1077 | 3 | } |
1078 | | |
1079 | | /* get _current_ algorithms & key lengths from "algorithm attributes" DOs |
1080 | | * |
1081 | | * All available algorithms should be already provided by pgp_init. However, if another |
1082 | | * algorithm is found in the "algorithm attributes" DOs, it is supported by the card as |
1083 | | * well and therefore added |
1084 | | * see OpenPGP card spec 1.1 & 2.x section 4.3.3.6 / v3.x section 4.4.3.7 */ |
1085 | 1.22k | for (i = 0x00c1; i <= 0x00c3; i++) { |
1086 | 918 | sc_cardctl_openpgp_key_gen_store_info_t key_info; |
1087 | | |
1088 | 918 | sc_log(card->ctx, "Parsing algorithm attributes DO %uX" , i); |
1089 | | |
1090 | | /* OpenPGP card spec 1.1 & 2.x section 4.3.3.6 / v3.x section 4.4.3.7 */ |
1091 | 918 | if ((pgp_get_blob(card, blob73, i, &blob) >= 0) && |
1092 | 119 | (pgp_parse_algo_attr_blob(card, blob, &key_info) >= 0)) { |
1093 | 2 | if (!_pgp_add_algo(card, &key_info, i)) |
1094 | 2 | LOG_TEST_RET(card->ctx, SC_ERROR_INTERNAL, "Cannot add algorithm"); |
1095 | 2 | } |
1096 | 918 | } |
1097 | | |
1098 | 306 | } |
1099 | | |
1100 | 915 | LOG_FUNC_RETURN(card->ctx, handled_algos); |
1101 | 915 | } |
1102 | | |
1103 | | |
1104 | | /** |
1105 | | * ABI: terminate driver & free private data. |
1106 | | */ |
1107 | | static int |
1108 | | pgp_finish(sc_card_t *card) |
1109 | 947 | { |
1110 | 947 | if (card != NULL) { |
1111 | 947 | struct pgp_priv_data *priv = DRVDATA(card); |
1112 | | |
1113 | 947 | if (priv != NULL) { |
1114 | | /* delete fake file hierarchy */ |
1115 | 947 | pgp_free_blobs(priv->mf); |
1116 | | |
1117 | | /* delete kdf do related data if exists */ |
1118 | 947 | free(priv->pin_kdf_info); |
1119 | | |
1120 | | /* delete private data */ |
1121 | 947 | free(priv); |
1122 | 947 | } |
1123 | 947 | card->drv_data = NULL; |
1124 | 947 | } |
1125 | 947 | return SC_SUCCESS; |
1126 | 947 | } |
1127 | | |
1128 | | |
1129 | | /** |
1130 | | * Internal: fill a blob's data. |
1131 | | */ |
1132 | | static int |
1133 | | pgp_set_blob(pgp_blob_t *blob, const u8 *data, size_t len) |
1134 | 38.6k | { |
1135 | 38.6k | if (blob->data) |
1136 | 20 | free(blob->data); |
1137 | 38.6k | blob->data = NULL; |
1138 | 38.6k | blob->len = 0; |
1139 | 38.6k | blob->status = 0; |
1140 | | |
1141 | 38.6k | if (len > 0) { |
1142 | 19.7k | void *tmp = calloc(1, len); |
1143 | | |
1144 | 19.7k | if (tmp == NULL) |
1145 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
1146 | | |
1147 | 19.7k | blob->data = tmp; |
1148 | 19.7k | blob->len = (unsigned int)len; |
1149 | 19.7k | if (data != NULL) |
1150 | 19.7k | memcpy(blob->data, data, len); |
1151 | 19.7k | } |
1152 | | |
1153 | 38.6k | if (blob->file) |
1154 | 38.6k | blob->file->size = len; |
1155 | | |
1156 | 38.6k | return SC_SUCCESS; |
1157 | 38.6k | } |
1158 | | |
1159 | | |
1160 | | /** |
1161 | | * Internal: implement Access Control List for emulated file. |
1162 | | * The Access Control is derived from the DO access permission. |
1163 | | **/ |
1164 | | static void |
1165 | | pgp_attach_acl(sc_card_t *card, sc_file_t *file, pgp_do_info_t *info) |
1166 | 39.5k | { |
1167 | 39.5k | unsigned int method = SC_AC_NONE; |
1168 | 39.5k | unsigned long key_ref = SC_AC_KEY_REF_NONE; |
1169 | | |
1170 | | /* Write access */ |
1171 | 39.5k | switch (info->access & WRITE_MASK) { |
1172 | 10.6k | case WRITE_NEVER: |
1173 | 10.6k | method = SC_AC_NEVER; |
1174 | 10.6k | break; |
1175 | 0 | case WRITE_PIN1: |
1176 | 0 | method = SC_AC_CHV; |
1177 | 0 | key_ref = 0x01; |
1178 | 0 | break; |
1179 | 1.83k | case WRITE_PIN2: |
1180 | 1.83k | method = SC_AC_CHV; |
1181 | 1.83k | key_ref = 0x02; |
1182 | 1.83k | break; |
1183 | 27.0k | case WRITE_PIN3: |
1184 | 27.0k | method = SC_AC_CHV; |
1185 | 27.0k | key_ref = 0x03; |
1186 | 27.0k | break; |
1187 | 39.5k | } |
1188 | | |
1189 | 39.5k | if (method != SC_AC_NONE || key_ref != SC_AC_KEY_REF_NONE) { |
1190 | 39.5k | sc_file_add_acl_entry(file, SC_AC_OP_WRITE, method, key_ref); |
1191 | 39.5k | sc_file_add_acl_entry(file, SC_AC_OP_UPDATE, method, key_ref); |
1192 | 39.5k | sc_file_add_acl_entry(file, SC_AC_OP_DELETE, method, key_ref); |
1193 | 39.5k | sc_file_add_acl_entry(file, SC_AC_OP_CREATE, method, key_ref); |
1194 | 39.5k | } |
1195 | 0 | else { |
1196 | | /* When SC_AC_OP_DELETE is absent, we need to provide |
1197 | | * SC_AC_OP_DELETE_SELF for sc_pkcs15init_delete_by_path() */ |
1198 | 0 | sc_file_add_acl_entry(file, SC_AC_OP_DELETE_SELF, method, key_ref); |
1199 | 0 | } |
1200 | | |
1201 | 39.5k | method = SC_AC_NONE; |
1202 | 39.5k | key_ref = SC_AC_KEY_REF_NONE; |
1203 | | /* Read access */ |
1204 | 39.5k | switch (info->access & READ_MASK) { |
1205 | 1.82k | case READ_NEVER: |
1206 | 1.82k | method = SC_AC_NEVER; |
1207 | 1.82k | break; |
1208 | 0 | case READ_PIN1: |
1209 | 0 | method = SC_AC_CHV; |
1210 | 0 | key_ref = 0x01; |
1211 | 0 | break; |
1212 | 918 | case READ_PIN2: |
1213 | 918 | method = SC_AC_CHV; |
1214 | 918 | key_ref = 0x02; |
1215 | 918 | break; |
1216 | 918 | case READ_PIN3: |
1217 | 918 | method = SC_AC_CHV; |
1218 | 918 | key_ref = 0x03; |
1219 | 918 | break; |
1220 | 39.5k | } |
1221 | | |
1222 | 39.5k | if (method != SC_AC_NONE || key_ref != SC_AC_KEY_REF_NONE) { |
1223 | 3.66k | sc_file_add_acl_entry(file, SC_AC_OP_READ, method, key_ref); |
1224 | 3.66k | } |
1225 | 39.5k | } |
1226 | | |
1227 | | |
1228 | | /** |
1229 | | * Internal: append a blob to the list of children of a given parent blob. |
1230 | | */ |
1231 | | static pgp_blob_t * |
1232 | | pgp_new_blob(sc_card_t *card, pgp_blob_t *parent, unsigned int file_id, |
1233 | | sc_file_t *file) |
1234 | 55.4k | { |
1235 | 55.4k | pgp_blob_t *blob = NULL; |
1236 | | |
1237 | 55.4k | if (file == NULL) |
1238 | 0 | return NULL; |
1239 | | |
1240 | 55.4k | if ((blob = calloc(1, sizeof(pgp_blob_t))) != NULL) { |
1241 | 55.4k | struct pgp_priv_data *priv = DRVDATA(card); |
1242 | 55.4k | pgp_do_info_t *info; |
1243 | | |
1244 | 55.4k | blob->file = file; |
1245 | | |
1246 | 55.4k | blob->file->type = SC_FILE_TYPE_WORKING_EF; /* default */ |
1247 | 55.4k | blob->file->ef_structure = SC_FILE_EF_TRANSPARENT; |
1248 | 55.4k | blob->file->id = file_id; |
1249 | | |
1250 | 55.4k | blob->id = file_id; |
1251 | 55.4k | blob->parent = parent; |
1252 | | |
1253 | 55.4k | if (parent != NULL) { |
1254 | 54.5k | pgp_blob_t **p; |
1255 | | |
1256 | | /* set file's path = parent's path + file's id */ |
1257 | 54.5k | blob->file->path = parent->file->path; |
1258 | 54.5k | sc_append_file_id(&blob->file->path, file_id); |
1259 | | |
1260 | | /* append blob to list of parent's children */ |
1261 | 4.76M | for (p = &parent->files; *p != NULL; p = &(*p)->next) |
1262 | 4.70M | ; |
1263 | 54.5k | *p = blob; |
1264 | 54.5k | } |
1265 | 918 | else { |
1266 | 918 | char path[10] = "0000"; /* long enough */ |
1267 | | |
1268 | | /* no parent: set file's path = file's id */ |
1269 | 918 | if (4 != snprintf(path, sizeof(path), "%04X", file_id & 0xFFFF)) { |
1270 | 0 | free(blob); |
1271 | 0 | return NULL; |
1272 | 0 | } |
1273 | | |
1274 | 918 | sc_format_path(path, &blob->file->path); |
1275 | 918 | } |
1276 | | |
1277 | | /* find matching DO info: set file type depending on it */ |
1278 | 2.40M | for (info = priv->pgp_objects; (info != NULL) && (info->id > 0); info++) { |
1279 | 2.38M | if (info->id == file_id) { |
1280 | 39.5k | blob->info = info; |
1281 | 39.5k | blob->file->type = blob->info->type; |
1282 | 39.5k | pgp_attach_acl(card, blob->file, info); |
1283 | 39.5k | break; |
1284 | 39.5k | } |
1285 | 2.38M | } |
1286 | 55.4k | } |
1287 | | |
1288 | 55.4k | return blob; |
1289 | 55.4k | } |
1290 | | |
1291 | | |
1292 | | /** |
1293 | | * Internal: free a blob including its content. |
1294 | | */ |
1295 | | static void |
1296 | | pgp_free_blob(pgp_blob_t *blob) |
1297 | 55.4k | { |
1298 | 55.4k | if (blob) { |
1299 | 55.4k | if (blob->parent) { |
1300 | 54.5k | pgp_blob_t **p; |
1301 | | |
1302 | | /* remove blob from list of parent's children */ |
1303 | 54.5k | for (p = &blob->parent->files; *p != NULL && *p != blob; p = &(*p)->next) |
1304 | 0 | ; |
1305 | 54.5k | if (*p == blob) |
1306 | 54.5k | *p = blob->next; |
1307 | 54.5k | } |
1308 | | |
1309 | 55.4k | sc_file_free(blob->file); |
1310 | 55.4k | if (blob->data) |
1311 | 19.6k | free(blob->data); |
1312 | 55.4k | free(blob); |
1313 | 55.4k | } |
1314 | 55.4k | } |
1315 | | |
1316 | | |
1317 | | /** |
1318 | | * Internal: iterate through the blob tree, calling pgp_free_blob for each blob. |
1319 | | */ |
1320 | | static void |
1321 | | pgp_free_blobs(pgp_blob_t *blob) |
1322 | 55.5k | { |
1323 | 55.5k | if (blob) { |
1324 | 55.4k | pgp_blob_t *child = blob->files; |
1325 | | |
1326 | 110k | while (child != NULL) { |
1327 | 54.5k | pgp_blob_t *next = child->next; |
1328 | | |
1329 | 54.5k | pgp_free_blobs(child); |
1330 | 54.5k | child = next; |
1331 | 54.5k | } |
1332 | 55.4k | pgp_free_blob(blob); |
1333 | 55.4k | } |
1334 | 55.5k | } |
1335 | | |
1336 | | |
1337 | | /** |
1338 | | * Internal: read a blob's contents from card. |
1339 | | */ |
1340 | | static int |
1341 | | pgp_read_blob(sc_card_t *card, pgp_blob_t *blob) |
1342 | 18.1k | { |
1343 | 18.1k | struct pgp_priv_data *priv = DRVDATA(card); |
1344 | | |
1345 | 18.1k | if (blob->data != NULL) |
1346 | 7.73k | return SC_SUCCESS; |
1347 | 10.3k | if (blob->info == NULL) |
1348 | 3.58k | return blob->status; |
1349 | | |
1350 | 6.77k | if (blob->info->get_fn) { /* readable, top-level DO */ |
1351 | 5.80k | u8 buffer[MAX_OPENPGP_DO_SIZE]; |
1352 | 5.80k | size_t buf_len = sizeof(buffer); |
1353 | 5.80k | int r = SC_SUCCESS; |
1354 | | |
1355 | | /* buffer length for certificate */ |
1356 | 5.80k | if (blob->id == DO_CERT && priv->max_cert_size > 0) { |
1357 | 0 | buf_len = MIN(priv->max_cert_size, sizeof(buffer)); |
1358 | 0 | } |
1359 | | |
1360 | | /* buffer length for Gnuk pubkey */ |
1361 | 5.80k | if (card->type == SC_CARD_TYPE_OPENPGP_GNUK && |
1362 | 46 | (blob->id == DO_AUTH || |
1363 | 46 | blob->id == DO_SIGN || |
1364 | 46 | blob->id == DO_ENCR || |
1365 | 46 | blob->id == DO_AUTH_SYM || |
1366 | 46 | blob->id == DO_SIGN_SYM || |
1367 | 46 | blob->id == DO_ENCR_SYM)) { |
1368 | 0 | buf_len = MIN(MAXLEN_RESP_PUBKEY_GNUK, sizeof(buffer)); |
1369 | 0 | } |
1370 | | |
1371 | 5.80k | r = blob->info->get_fn(card, blob->id, buffer, buf_len); |
1372 | | |
1373 | 5.80k | if (r < 0) { /* an error occurred */ |
1374 | 3.89k | blob->status = r; |
1375 | 3.89k | return r; |
1376 | 3.89k | } |
1377 | | |
1378 | 1.91k | return pgp_set_blob(blob, buffer, r); |
1379 | 5.80k | } |
1380 | 965 | else { /* un-readable DO or part of a constructed DO */ |
1381 | 965 | return SC_SUCCESS; |
1382 | 965 | } |
1383 | 6.77k | } |
1384 | | |
1385 | | |
1386 | | /** |
1387 | | * Internal: enumerate contents of a data blob. |
1388 | | * The OpenPGP card has a TLV encoding according ASN.1 BER-encoding rules. |
1389 | | */ |
1390 | | static int |
1391 | | pgp_enumerate_blob(sc_card_t *card, pgp_blob_t *blob) |
1392 | 22.7k | { |
1393 | 22.7k | const u8 *in; |
1394 | 22.7k | int r; |
1395 | 22.7k | sc_file_t *file = NULL; |
1396 | | |
1397 | 22.7k | if (blob->files != NULL) |
1398 | 10.8k | return SC_SUCCESS; |
1399 | | |
1400 | 11.8k | if ((r = pgp_read_blob(card, blob)) < 0) |
1401 | 1.92k | return r; |
1402 | | |
1403 | 9.94k | in = blob->data; |
1404 | | |
1405 | 46.5k | while ((int) blob->len > (in - blob->data)) { |
1406 | 40.6k | unsigned int cla, tag, tmptag; |
1407 | 40.6k | size_t len; |
1408 | 40.6k | const u8 *data = in; |
1409 | 40.6k | pgp_blob_t *new; |
1410 | | |
1411 | 40.6k | if (!in) |
1412 | 0 | return SC_ERROR_OBJECT_NOT_VALID; |
1413 | | |
1414 | 40.6k | r = sc_asn1_read_tag(&data, blob->len - (in - blob->data), |
1415 | 40.6k | &cla, &tag, &len); |
1416 | 40.6k | if (r == SC_ERROR_INVALID_ASN1_OBJECT) { |
1417 | 1.25k | sc_log(card->ctx, "Invalid ASN.1 object"); |
1418 | 1.25k | return SC_ERROR_OBJECT_NOT_VALID; |
1419 | 1.25k | } |
1420 | | /* Check for unknown error, or empty data */ |
1421 | 39.4k | if (((r < 0) && (r != SC_ERROR_ASN1_END_OF_CONTENTS)) || |
1422 | 39.4k | (data == NULL)) { |
1423 | 517 | sc_log(card->ctx, "Unexpected end of contents"); |
1424 | 517 | return SC_ERROR_OBJECT_NOT_VALID; |
1425 | 517 | } |
1426 | | |
1427 | | /* undo ASN1's split of tag & class */ |
1428 | 41.1k | for (tmptag = tag; tmptag > 0x0FF; tmptag >>= 8) { |
1429 | 2.23k | cla <<= 8; |
1430 | 2.23k | } |
1431 | 38.8k | tag |= cla; |
1432 | | /* Check for length mismatch */ |
1433 | 38.8k | if ((r == SC_ERROR_ASN1_END_OF_CONTENTS) || |
1434 | 36.6k | (data + len > blob->data + blob->len)) { |
1435 | | // Check if it is not known Yubikey 5 issue |
1436 | 2.26k | if ((tag != blob->id) || (tag != 0xfa)) { |
1437 | 2.24k | sc_log(card->ctx, "Unexpected end of contents"); |
1438 | 2.24k | return SC_ERROR_OBJECT_NOT_VALID; |
1439 | 2.24k | } |
1440 | 2.26k | } |
1441 | | |
1442 | | /* Awful hack for composite DOs that have |
1443 | | * a TLV with the DO's id encompassing the |
1444 | | * entire blob. Example: Yubikey Neo */ |
1445 | 36.6k | if (tag == blob->id) { |
1446 | 240 | in = data; |
1447 | 240 | continue; |
1448 | 240 | } |
1449 | | |
1450 | | /* create fake file system hierarchy by |
1451 | | * using constructed DOs as DF */ |
1452 | 36.4k | file = sc_file_new(); |
1453 | 36.4k | if ((new = pgp_new_blob(card, blob, tag, file)) == NULL) { |
1454 | 0 | sc_file_free(file); |
1455 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
1456 | 0 | } |
1457 | 36.4k | if (pgp_set_blob(new, data, len) != SC_SUCCESS) { |
1458 | 0 | sc_file_free(file); |
1459 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
1460 | 0 | } |
1461 | 36.4k | in = data + len; |
1462 | 36.4k | } |
1463 | | |
1464 | 5.92k | return SC_SUCCESS; |
1465 | 9.94k | } |
1466 | | |
1467 | | |
1468 | | /** |
1469 | | * Internal: find a blob by ID below a given parent, filling its contents when necessary. |
1470 | | */ |
1471 | | static int |
1472 | | pgp_get_blob(sc_card_t *card, pgp_blob_t *blob, unsigned int id, |
1473 | | pgp_blob_t **ret) |
1474 | 22.0k | { |
1475 | 22.0k | pgp_blob_t *child; |
1476 | 22.0k | int r; |
1477 | | |
1478 | 22.0k | if ((r = pgp_enumerate_blob(card, blob)) < 0) |
1479 | 5.37k | return r; |
1480 | | |
1481 | 103k | for (child = blob->files; child; child = child->next) { |
1482 | 91.8k | if (child->id == id) { |
1483 | 5.26k | (void) pgp_read_blob(card, child); |
1484 | 5.26k | *ret = child; |
1485 | 5.26k | return SC_SUCCESS; |
1486 | 5.26k | } |
1487 | 91.8k | } |
1488 | | |
1489 | | /* This part is for "NOT FOUND" cases */ |
1490 | | |
1491 | | /* Special case: |
1492 | | * Gnuk does not have default value for children of DO 65 (DOs 5B, 5F2D, 5F35) |
1493 | | * So, if these blob was not found, we create it. */ |
1494 | 11.4k | if (blob->id == DO_CARDHOLDER && (id == DO_NAME || id == DO_LANG_PREF || id == DO_SEX)) { |
1495 | 159 | sc_log(card->ctx, "Create blob %X under %X", id, blob->id); |
1496 | 159 | child = pgp_new_blob(card, blob, id, sc_file_new()); |
1497 | 159 | if (child) { |
1498 | 159 | pgp_set_blob(child, NULL, 0); |
1499 | 159 | *ret = child; |
1500 | 159 | return SC_SUCCESS; |
1501 | 159 | } |
1502 | 0 | else |
1503 | 0 | sc_log(card->ctx, |
1504 | 159 | "Not enough memory to create blob for DO %X", |
1505 | 159 | id); |
1506 | 159 | } |
1507 | | |
1508 | 11.2k | return SC_ERROR_FILE_NOT_FOUND; |
1509 | 11.4k | } |
1510 | | |
1511 | | |
1512 | | /** |
1513 | | * Internal: search recursively for a blob by ID below a given root. |
1514 | | */ |
1515 | | static int |
1516 | | pgp_seek_blob(sc_card_t *card, pgp_blob_t *root, unsigned int id, |
1517 | | pgp_blob_t **ret) |
1518 | 12.5k | { |
1519 | 12.5k | pgp_blob_t *child; |
1520 | 12.5k | int r; |
1521 | | |
1522 | 12.5k | if ((r = pgp_get_blob(card, root, id, ret)) == 0) |
1523 | | /* the sought blob is right under root */ |
1524 | 372 | return r; |
1525 | | |
1526 | | /* not found, seek deeper */ |
1527 | 29.6k | for (child = root->files; child; child = child->next) { |
1528 | | /* The DO of SIMPLE type or the DO holding certificate |
1529 | | * does not contain children */ |
1530 | 18.3k | if ((child->info && child->info->type == SIMPLE) || child->id == DO_CERT) |
1531 | 6.35k | continue; |
1532 | 11.9k | r = pgp_seek_blob(card, child, id, ret); |
1533 | 11.9k | if (r == 0) |
1534 | 852 | return r; |
1535 | 11.9k | } |
1536 | | |
1537 | 11.3k | return SC_ERROR_FILE_NOT_FOUND; |
1538 | 12.1k | } |
1539 | | |
1540 | | |
1541 | | /** |
1542 | | * Internal: find a blob by tag - pgp_seek_blob with optimizations. |
1543 | | */ |
1544 | | static pgp_blob_t * |
1545 | | pgp_find_blob(sc_card_t *card, unsigned int tag) |
1546 | 386 | { |
1547 | 386 | struct pgp_priv_data *priv = DRVDATA(card); |
1548 | 386 | pgp_blob_t *blob = NULL; |
1549 | 386 | int r; |
1550 | | |
1551 | | /* check if current selected blob is which we want to test */ |
1552 | 386 | if (priv->current->id == tag) { |
1553 | 34 | return priv->current; |
1554 | 34 | } |
1555 | | /* look for the blob representing the DO */ |
1556 | 352 | r = pgp_seek_blob(card, priv->mf, tag, &blob); |
1557 | 352 | if (r < 0) { |
1558 | 166 | sc_log(card->ctx, "Failed to seek the blob representing the tag %04X. Error %d.", tag, r); |
1559 | 166 | return NULL; |
1560 | 166 | } |
1561 | 186 | return blob; |
1562 | 352 | } |
1563 | | |
1564 | | |
1565 | | /** |
1566 | | * Internal: get info for a specific tag. |
1567 | | */ |
1568 | | static pgp_do_info_t * |
1569 | | pgp_get_info_by_tag(sc_card_t *card, unsigned int tag) |
1570 | 155 | { |
1571 | 155 | struct pgp_priv_data *priv = DRVDATA(card); |
1572 | 155 | pgp_do_info_t *info; |
1573 | | |
1574 | 6.48k | for (info = priv->pgp_objects; (info != NULL) && (info->id > 0); info++) |
1575 | 6.48k | if (tag == info->id) |
1576 | 155 | return info; |
1577 | | |
1578 | 0 | return NULL; |
1579 | 155 | } |
1580 | | |
1581 | | |
1582 | | /** |
1583 | | * Internal: strip out the parts of PKCS15 file layout in the path. |
1584 | | * Get the reduced version which is understood by the OpenPGP card driver. |
1585 | | * Return the index whose preceding part will be ignored. |
1586 | | **/ |
1587 | | static unsigned int |
1588 | | pgp_strip_path(sc_card_t *card, const sc_path_t *path) |
1589 | 3.83k | { |
1590 | 3.83k | unsigned int start_point = 0; |
1591 | | /* start_point will move through the path string */ |
1592 | 3.83k | if (path->len == 0) |
1593 | 0 | return 0; |
1594 | | |
1595 | | /* ignore 3F00 (MF) at the beginning */ |
1596 | 3.83k | start_point = (memcmp(path->value, "\x3f\x00", 2) == 0) ? 2 : 0; |
1597 | | /* strip path of PKCS15-App DF (5015) */ |
1598 | 3.83k | start_point += (memcmp(path->value + start_point, "\x50\x15", 2) == 0) ? 2 : 0; |
1599 | 3.83k | return start_point; |
1600 | 3.83k | } |
1601 | | |
1602 | | |
1603 | | /** |
1604 | | * ABI: ISO 7816-4 SELECT FILE - search given file & make it the currently selected one. |
1605 | | */ |
1606 | | static int |
1607 | | pgp_select_file(sc_card_t *card, const sc_path_t *path, sc_file_t **ret) |
1608 | 5.35k | { |
1609 | 5.35k | struct pgp_priv_data *priv = DRVDATA(card); |
1610 | 5.35k | pgp_blob_t *blob; |
1611 | 5.35k | unsigned int path_start = 0; |
1612 | 5.35k | unsigned int n; |
1613 | 5.35k | sc_path_t dummy_path; |
1614 | | |
1615 | 5.35k | LOG_FUNC_CALLED(card->ctx); |
1616 | | |
1617 | 5.35k | if (path->type == SC_PATH_TYPE_DF_NAME) |
1618 | 5.35k | LOG_FUNC_RETURN(card->ctx, iso_ops->select_file(card, path, ret)); |
1619 | | |
1620 | 3.83k | if (path->len < 2 || (path->len & 1)) |
1621 | 3.83k | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
1622 | 3.83k | "invalid path length"); |
1623 | | |
1624 | 3.83k | if (path->type == SC_PATH_TYPE_FILE_ID && path->len != 2) |
1625 | 3.83k | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
1626 | 3.83k | "invalid path type"); |
1627 | | |
1628 | | /* Due to pkcs15init implementation, sometimes a file at path "11001101" |
1629 | | * need to be written (one use case is when importing key&cert from p12 file). |
1630 | | * This file does not exist in OpenPGP but pkcs15 requires that |
1631 | | * writing this file must be successful. |
1632 | | * So, we pretend that selecting & writing this file is successful. |
1633 | | * The "11001101"is defined in sc_pkcs15emu_get_df() function, pkcs15-sync.c file. */ |
1634 | 3.83k | sc_format_path("11001101", &dummy_path); |
1635 | 3.83k | if (sc_compare_path(path, &dummy_path)) { |
1636 | 0 | if (ret != NULL) { |
1637 | 0 | *ret = sc_file_new(); |
1638 | | /* One use case of this dummy file is after writing certificate in pkcs15init. |
1639 | | * So we set its size to be the same as max certificate size the card supports. */ |
1640 | 0 | (*ret)->size = priv->max_cert_size; |
1641 | 0 | } |
1642 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1643 | 0 | } |
1644 | | |
1645 | | /* ignore explicitly mentioned MF at the path's beginning */ |
1646 | 3.83k | path_start = pgp_strip_path(card, path); |
1647 | | |
1648 | | /* starting with the MF ... */ |
1649 | 3.83k | blob = priv->mf; |
1650 | | /* ... recurse through the tree following the path */ |
1651 | 5.67k | for (n = path_start; n < path->len; n += 2) { |
1652 | 4.39k | unsigned int id = bebytes2ushort(path->value + n); |
1653 | 4.39k | int r = pgp_get_blob(card, blob, id, &blob); |
1654 | | |
1655 | | /* This file ID is referred when importing key&certificate via pkcs15init, like above. |
1656 | | * We pretend to successfully find this inexistent file. */ |
1657 | 4.39k | if (id == 0x4402 || id == 0x5f48) { |
1658 | 0 | if (ret == NULL) |
1659 | | /* No need to return file */ |
1660 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1661 | | |
1662 | | /* Else, need to return file */ |
1663 | 0 | *ret = sc_file_new(); |
1664 | 0 | (*ret)->size = priv->max_cert_size; |
1665 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1666 | 0 | } |
1667 | | |
1668 | 4.39k | if (r < 0) { /* failure */ |
1669 | 2.55k | LOG_FUNC_RETURN(card->ctx, r); |
1670 | 2.55k | } |
1671 | 4.39k | } |
1672 | | |
1673 | | /* success: select file = set "current" pointer to blob found */ |
1674 | 1.27k | priv->current = blob; |
1675 | | |
1676 | 1.27k | if (ret) |
1677 | 683 | sc_file_dup(ret, blob->file); |
1678 | | |
1679 | 1.27k | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1680 | 1.27k | } |
1681 | | |
1682 | | |
1683 | | /** |
1684 | | * ABI: ISO 7816-4 LIST FILES - enumerate all files in current DF. |
1685 | | */ |
1686 | | static int |
1687 | | pgp_list_files(sc_card_t *card, u8 *buf, size_t buflen) |
1688 | 0 | { |
1689 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
1690 | 0 | pgp_blob_t *blob; |
1691 | 0 | unsigned int k; |
1692 | 0 | int r; |
1693 | |
|
1694 | 0 | LOG_FUNC_CALLED(card->ctx); |
1695 | | |
1696 | | /* jump to selected file */ |
1697 | 0 | blob = priv->current; |
1698 | |
|
1699 | 0 | if (blob->file->type != SC_FILE_TYPE_DF) |
1700 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_OBJECT_NOT_VALID, |
1701 | 0 | "invalid file type"); |
1702 | | |
1703 | 0 | if ((r = pgp_enumerate_blob(card, blob)) < 0) |
1704 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
1705 | | |
1706 | 0 | for (k = 0, blob = blob->files; blob != NULL; blob = blob->next) { |
1707 | 0 | if (blob->info != NULL && (blob->info->access & READ_MASK) != READ_NEVER) { |
1708 | 0 | if (k + 2 > buflen) |
1709 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_BUFFER_TOO_SMALL); |
1710 | | |
1711 | 0 | ushort2bebytes(buf + k, blob->id); |
1712 | 0 | k += 2; |
1713 | 0 | } |
1714 | 0 | } |
1715 | | |
1716 | 0 | LOG_FUNC_RETURN(card->ctx, k); |
1717 | 0 | } |
1718 | | |
1719 | | |
1720 | | /** |
1721 | | * ABI: ISO 7816-4 GET CHALLENGE - generate random byte sequence. |
1722 | | */ |
1723 | | static int |
1724 | | pgp_get_challenge(struct sc_card *card, u8 *rnd, size_t len) |
1725 | 0 | { |
1726 | 0 | struct pgp_priv_data *priv; |
1727 | |
|
1728 | 0 | LOG_FUNC_CALLED(card->ctx); |
1729 | |
|
1730 | 0 | priv = DRVDATA(card); |
1731 | 0 | if (0 == (priv->ext_caps & EXT_CAP_GET_CHALLENGE)) { |
1732 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
1733 | 0 | } |
1734 | | |
1735 | 0 | if (priv->max_challenge_size > 0 && len > priv->max_challenge_size) { |
1736 | 0 | len = priv->max_challenge_size; |
1737 | 0 | } |
1738 | |
|
1739 | 0 | LOG_FUNC_RETURN(card->ctx, iso_ops->get_challenge(card, rnd, len)); |
1740 | 0 | } |
1741 | | |
1742 | | |
1743 | | /** |
1744 | | * ABI: ISO 7816-4 READ BINARY - read data from currently selected EF. |
1745 | | */ |
1746 | | static int |
1747 | | pgp_read_binary(sc_card_t *card, unsigned int idx, |
1748 | | u8 *buf, size_t count, unsigned long *flags) |
1749 | 971 | { |
1750 | 971 | struct pgp_priv_data *priv = DRVDATA(card); |
1751 | 971 | pgp_blob_t *blob; |
1752 | 971 | int r; |
1753 | | |
1754 | 971 | LOG_FUNC_CALLED(card->ctx); |
1755 | | |
1756 | | /* jump to selected file */ |
1757 | 971 | blob = priv->current; |
1758 | | |
1759 | 971 | if (blob == NULL) |
1760 | 971 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_NOT_FOUND); |
1761 | | |
1762 | 971 | if (blob->file->type != SC_FILE_TYPE_WORKING_EF) |
1763 | 971 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_NOT_FOUND); |
1764 | | |
1765 | 971 | if ((r = pgp_read_blob(card, blob)) < 0) |
1766 | 971 | LOG_FUNC_RETURN(card->ctx, r); |
1767 | | |
1768 | 762 | if (idx > blob->len) |
1769 | 762 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCORRECT_PARAMETERS); |
1770 | | |
1771 | 762 | if (idx + count > blob->len) |
1772 | 89 | count = blob->len - idx; |
1773 | 762 | memcpy(buf, blob->data + idx, count); |
1774 | | |
1775 | 762 | LOG_FUNC_RETURN(card->ctx, (int)count); |
1776 | 762 | } |
1777 | | |
1778 | | |
1779 | | /** |
1780 | | * Internal: get public key from card - as DF + sub-wEFs. |
1781 | | */ |
1782 | | static int |
1783 | | pgp_get_pubkey(sc_card_t *card, unsigned int tag, u8 *buf, size_t buf_len) |
1784 | 589 | { |
1785 | 589 | sc_apdu_t apdu; |
1786 | 589 | u8 apdu_case = (card->type == SC_CARD_TYPE_OPENPGP_GNUK) |
1787 | 589 | ? SC_APDU_CASE_4_SHORT : SC_APDU_CASE_4; |
1788 | 589 | u8 idbuf[2]; |
1789 | 589 | int r; |
1790 | | |
1791 | 589 | sc_log(card->ctx, "called, tag=%04x\n", tag); |
1792 | | |
1793 | 589 | sc_format_apdu(card, &apdu, apdu_case, 0x47, 0x81, 0); |
1794 | 589 | apdu.lc = 2; |
1795 | 589 | apdu.data = ushort2bebytes(idbuf, tag); |
1796 | 589 | apdu.datalen = 2; |
1797 | 589 | apdu.le = ((buf_len >= 256) && !(card->caps & SC_CARD_CAP_APDU_EXT)) ? 256 : buf_len; |
1798 | 589 | apdu.resp = buf; |
1799 | 589 | apdu.resplen = buf_len; |
1800 | | |
1801 | 589 | r = sc_transmit_apdu(card, &apdu); |
1802 | 589 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1803 | | |
1804 | 565 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1805 | 565 | LOG_TEST_RET(card->ctx, r, "Card returned error"); |
1806 | | |
1807 | 74 | LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen); |
1808 | 74 | } |
1809 | | |
1810 | | |
1811 | | /** |
1812 | | * Internal: get public key from card - as one wEF. |
1813 | | */ |
1814 | | static int |
1815 | | pgp_get_pubkey_pem(sc_card_t *card, unsigned int tag, u8 *buf, size_t buf_len) |
1816 | 0 | { |
1817 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
1818 | 0 | pgp_blob_t *blob, *mod_blob, *exp_blob, *pubkey_blob, *blob6e, *blob73, *aa_blob; |
1819 | 0 | sc_pkcs15_pubkey_t p15pubkey; |
1820 | 0 | sc_cardctl_openpgp_key_gen_store_info_t key_info; |
1821 | 0 | unsigned int aa_tag = 0; |
1822 | 0 | u8 *data = NULL; |
1823 | 0 | size_t len = 0; |
1824 | 0 | int r; |
1825 | |
|
1826 | 0 | sc_log(card->ctx, "called, tag=%04x\n", tag); |
1827 | 0 | memset(&p15pubkey, 0, sizeof(p15pubkey)); |
1828 | |
|
1829 | 0 | if ((r = pgp_get_blob(card, priv->mf, tag & 0xFFFE, &blob)) < 0 |
1830 | 0 | || (r = pgp_get_blob(card, blob, 0x7F49, &blob)) < 0) |
1831 | 0 | LOG_TEST_RET(card->ctx, r, "error getting elements"); |
1832 | | |
1833 | | /* RSA */ |
1834 | 0 | if ((r = pgp_get_blob(card, blob, 0x0081, &mod_blob)) >= 0 |
1835 | 0 | && (r = pgp_get_blob(card, blob, 0x0082, &exp_blob)) >= 0 |
1836 | 0 | && (r = pgp_read_blob(card, mod_blob)) >= 0 |
1837 | 0 | && (r = pgp_read_blob(card, exp_blob)) >= 0) { |
1838 | |
|
1839 | 0 | p15pubkey.algorithm = SC_ALGORITHM_RSA; |
1840 | 0 | p15pubkey.u.rsa.modulus.data = mod_blob->data; |
1841 | 0 | p15pubkey.u.rsa.modulus.len = mod_blob->len; |
1842 | 0 | p15pubkey.u.rsa.exponent.data = exp_blob->data; |
1843 | 0 | p15pubkey.u.rsa.exponent.len = exp_blob->len; |
1844 | 0 | r = sc_pkcs15_encode_pubkey(card->ctx, &p15pubkey, &data, &len); |
1845 | 0 | } |
1846 | | /* ECC */ |
1847 | 0 | else if ((r = pgp_get_blob(card, blob, 0x0086, &pubkey_blob)) >= 0 |
1848 | 0 | && (r = pgp_read_blob(card, pubkey_blob)) >= 0) { |
1849 | |
|
1850 | 0 | switch(tag & 0xFFFE) { |
1851 | 0 | case DO_SIGN: aa_tag = 0x00C1; break; |
1852 | 0 | case DO_ENCR: aa_tag = 0x00C2; break; |
1853 | 0 | case DO_AUTH: aa_tag = 0x00C3; break; |
1854 | 0 | default: r = SC_ERROR_INCORRECT_PARAMETERS; |
1855 | 0 | } |
1856 | | |
1857 | | /* Get EC parameters from Algorithm Attribute if present */ |
1858 | | |
1859 | 0 | if (aa_tag && ((r = pgp_get_blob(card, priv->mf, 0x006e, &blob6e)) >= 0) && |
1860 | 0 | ((r = pgp_get_blob(card, blob6e, 0x0073, &blob73)) >= 0) && |
1861 | 0 | ((r = pgp_get_blob(card, blob73, aa_tag, &aa_blob)) >= 0) && |
1862 | 0 | ((r = pgp_parse_algo_attr_blob(card, aa_blob, &key_info)) >= 0)) { |
1863 | 0 | switch (key_info.algorithm) { |
1864 | 0 | case SC_OPENPGP_KEYALGO_EDDSA: |
1865 | | /* In EDDSA key case we do not have to care about OIDs |
1866 | | * as we support only one for now */ |
1867 | 0 | p15pubkey.algorithm = SC_ALGORITHM_EDDSA; |
1868 | 0 | p15pubkey.u.ec.ecpointQ.value = pubkey_blob->data; |
1869 | 0 | p15pubkey.u.ec.ecpointQ.len = pubkey_blob->len; |
1870 | | /* PKCS#11 3.0: 2.3.5 Edwards EC public keys only support the use |
1871 | | * of the curveName selection to specify a curve name as defined |
1872 | | * in [RFC 8032] */ |
1873 | 0 | r = sc_pkcs15_encode_pubkey_as_spki(card->ctx, &p15pubkey, &data, &len); |
1874 | 0 | break; |
1875 | 0 | case SC_OPENPGP_KEYALGO_ECDH: |
1876 | | /* This yields either EC(DSA) key or EC_MONTGOMERY (curve25519) key */ |
1877 | 0 | if (sc_compare_oid(&key_info.u.ec.oid, &curve25519_oid)) { |
1878 | 0 | p15pubkey.algorithm = SC_ALGORITHM_XEDDSA; |
1879 | 0 | p15pubkey.u.ec.ecpointQ.value = pubkey_blob->data; |
1880 | 0 | p15pubkey.u.ec.ecpointQ.len = pubkey_blob->len; |
1881 | | /* PKCS#11 3.0 2.3.7 Montgomery EC public keys only support |
1882 | | * the use of the curveName selection to specify a curve |
1883 | | * name as defined in [RFC7748] */ |
1884 | | /* XXX only curve25519 supported now. Theoretically could be |
1885 | | * also curve448 or OIDs */ |
1886 | |
|
1887 | 0 | r = sc_pkcs15_encode_pubkey_as_spki(card->ctx, &p15pubkey, &data, &len); |
1888 | 0 | break; |
1889 | 0 | } |
1890 | | /* fall through */ |
1891 | 0 | case SC_OPENPGP_KEYALGO_ECDSA: |
1892 | 0 | if ((r = sc_encode_oid(card->ctx, &key_info.u.ec.oid, |
1893 | 0 | &p15pubkey.u.ec.params.der.value, |
1894 | 0 | &p15pubkey.u.ec.params.der.len)) == 0) { |
1895 | 0 | p15pubkey.algorithm = SC_ALGORITHM_EC; |
1896 | 0 | p15pubkey.u.ec.ecpointQ.value = pubkey_blob->data; |
1897 | 0 | p15pubkey.u.ec.ecpointQ.len = pubkey_blob->len; |
1898 | 0 | p15pubkey.u.ec.params.type = 1; |
1899 | 0 | r = sc_pkcs15_encode_pubkey_as_spki(card->ctx, &p15pubkey, &data, &len); |
1900 | 0 | } else { |
1901 | 0 | sc_log(card->ctx, "Unable to encode EC curve OID from algorithm info"); |
1902 | 0 | } |
1903 | 0 | break; |
1904 | 0 | default: |
1905 | 0 | sc_log(card->ctx, "Unknown algorithm ID received (%d)", key_info.algorithm); |
1906 | 0 | break; |
1907 | 0 | } |
1908 | 0 | } else { |
1909 | 0 | sc_log(card->ctx, "Unable to find Algorithm Attribute for EC curve OID"); |
1910 | 0 | } |
1911 | 0 | } else { |
1912 | 0 | LOG_TEST_RET(card->ctx, r, "error getting elements"); |
1913 | 0 | } |
1914 | | |
1915 | | /* clean up anything we may have set in p15pubkey that can not be freed */ |
1916 | 0 | if (p15pubkey.algorithm == SC_ALGORITHM_RSA) { |
1917 | 0 | p15pubkey.u.rsa.modulus.data = NULL; |
1918 | 0 | p15pubkey.u.rsa.modulus.len = 0; |
1919 | 0 | p15pubkey.u.rsa.exponent.data = NULL; |
1920 | 0 | p15pubkey.u.rsa.exponent.len = 0; |
1921 | 0 | } else if (p15pubkey.algorithm == SC_ALGORITHM_EC || |
1922 | 0 | p15pubkey.algorithm == SC_ALGORITHM_EDDSA || |
1923 | 0 | p15pubkey.algorithm == SC_ALGORITHM_XEDDSA) { |
1924 | 0 | p15pubkey.u.ec.ecpointQ.value = NULL; |
1925 | 0 | p15pubkey.u.ec.ecpointQ.len = 0; |
1926 | | /* p15pubkey.u.ec.params.der and named_curve will be freed by sc_pkcs15_erase_pubkey */ |
1927 | 0 | } |
1928 | 0 | sc_pkcs15_erase_pubkey(&p15pubkey); |
1929 | |
|
1930 | 0 | LOG_TEST_RET(card->ctx, r, "public key encoding failed"); |
1931 | | |
1932 | 0 | if (len > buf_len) |
1933 | 0 | len = buf_len; |
1934 | 0 | memcpy(buf, data, len); |
1935 | 0 | free(data); |
1936 | |
|
1937 | 0 | LOG_FUNC_RETURN(card->ctx, (int)len); |
1938 | 0 | } |
1939 | | |
1940 | | |
1941 | | /** |
1942 | | * Internal: SELECT DATA - selects a DO within a DO tag with several instances |
1943 | | * (supported since OpenPGP Card v3 for DO 7F21 only, see section 7.2.5 of the specification; |
1944 | | * this enables us to store multiple Card holder certificates in DO 7F21) |
1945 | | * |
1946 | | * p1: number of an instance (DO 7F21: 0x00 for AUT, 0x01 for DEC and 0x02 for SIG) |
1947 | | */ |
1948 | | static int |
1949 | | pgp_select_data(sc_card_t *card, u8 p1) |
1950 | 0 | { |
1951 | 0 | sc_apdu_t apdu; |
1952 | 0 | u8 apdu_data[6]; |
1953 | 0 | int r; |
1954 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
1955 | |
|
1956 | 0 | LOG_FUNC_CALLED(card->ctx); |
1957 | |
|
1958 | 0 | if (priv->bcd_version < OPENPGP_CARD_3_0) |
1959 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
1960 | | |
1961 | 0 | sc_log(card->ctx, "select data with: %u", p1); |
1962 | | |
1963 | | // create apdu data (taken from spec: SELECT DATA 7.2.5.) |
1964 | 0 | apdu_data[0] = 0x60; |
1965 | 0 | apdu_data[1] = 0x04; |
1966 | 0 | apdu_data[2] = 0x5c; |
1967 | 0 | apdu_data[3] = 0x02; |
1968 | 0 | apdu_data[4] = 0x7f; |
1969 | 0 | apdu_data[5] = 0x21; |
1970 | | |
1971 | | // apdu, cla, ins, p1, p2, data, datalen, resp, resplen |
1972 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0xA5, p1, 0x04, apdu_data, sizeof(apdu_data), NULL, 0); |
1973 | | |
1974 | | // transmit apdu |
1975 | 0 | r = sc_transmit_apdu(card, &apdu); |
1976 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1977 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1978 | 0 | LOG_TEST_RET(card->ctx, r, "Card returned error"); |
1979 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
1980 | 0 | } |
1981 | | |
1982 | | |
1983 | | /** |
1984 | | * ABI: ISO 7816-4 GET DATA - get contents of a DO. |
1985 | | */ |
1986 | | static int |
1987 | | pgp_get_data(sc_card_t *card, unsigned int tag, u8 *buf, size_t buf_len) |
1988 | 7.11k | { |
1989 | 7.11k | sc_apdu_t apdu; |
1990 | 7.11k | int r; |
1991 | | |
1992 | 7.11k | LOG_FUNC_CALLED(card->ctx); |
1993 | | |
1994 | 7.11k | sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0xCA, tag >> 8, tag); |
1995 | 7.11k | apdu.le = ((buf_len >= 256) && !(card->caps & SC_CARD_CAP_APDU_EXT)) ? 256 : buf_len; |
1996 | 7.11k | apdu.resp = buf; |
1997 | 7.11k | apdu.resplen = buf_len; |
1998 | | |
1999 | 7.11k | r = sc_transmit_apdu(card, &apdu); |
2000 | 7.11k | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
2001 | | |
2002 | 7.03k | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
2003 | | |
2004 | | /* Gnuk returns an error instead of empty data if there is no certificate or private DO. |
2005 | | * So, for this case, we ignore error and consider success */ |
2006 | 7.03k | if (card->type == SC_CARD_TYPE_OPENPGP_GNUK && |
2007 | 52 | (tag == DO_CERT || |
2008 | 52 | tag == DO_PRIV1 || |
2009 | 49 | tag == DO_PRIV2 || |
2010 | 49 | tag == DO_PRIV3 || |
2011 | 49 | tag == DO_PRIV4)) { |
2012 | 3 | if (r == SC_ERROR_DATA_OBJECT_NOT_FOUND) { |
2013 | 0 | r = SC_SUCCESS; |
2014 | 0 | apdu.resplen = 0; |
2015 | 0 | } |
2016 | 3 | } |
2017 | 7.03k | LOG_TEST_RET(card->ctx, r, "Card returned error"); |
2018 | | |
2019 | 3.27k | LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen); |
2020 | 3.27k | } |
2021 | | |
2022 | | /** |
2023 | | * Internal: write certificate for Gnuk. |
2024 | | */ |
2025 | | static int |
2026 | | gnuk_write_certificate(sc_card_t *card, const u8 *buf, size_t length) |
2027 | 0 | { |
2028 | 0 | size_t i = 0; |
2029 | 0 | sc_apdu_t apdu; |
2030 | 0 | int r = SC_SUCCESS; |
2031 | |
|
2032 | 0 | LOG_FUNC_CALLED(card->ctx); |
2033 | | |
2034 | | /* If null data is passed, delete certificate */ |
2035 | 0 | if (buf == NULL || length == 0) { |
2036 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0xD6, 0x85, 0); |
2037 | 0 | r = sc_transmit_apdu(card, &apdu); |
2038 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
2039 | | /* Check response */ |
2040 | 0 | LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2)); |
2041 | 0 | } |
2042 | | |
2043 | | /* Ref: gnuk_put_binary_libusb.py and gnuk_token.py in Gnuk source tree */ |
2044 | | /* Split data to segments of 256 bytes. Send each segment via command chaining, |
2045 | | * with particular P1 byte for each segment */ |
2046 | 0 | for (i = 0; i*256 < length; i++) { |
2047 | 0 | u8 *part = (u8 *)buf + i*256; |
2048 | 0 | size_t plen = MIN(length - i*256, 256); |
2049 | 0 | u8 roundbuf[256]; /* space to build APDU data with even length for Gnuk */ |
2050 | |
|
2051 | 0 | sc_log(card->ctx, |
2052 | 0 | "Write part %"SC_FORMAT_LEN_SIZE_T"u from offset 0x%"SC_FORMAT_LEN_SIZE_T"X, len %"SC_FORMAT_LEN_SIZE_T"u", |
2053 | 0 | i+1, i*256, plen); |
2054 | | |
2055 | | /* 1st chunk: P1 = 0x85, further chunks: P1 = chunk no */ |
2056 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD6, (i == 0) ? 0x85 : (int)i, 0); |
2057 | 0 | apdu.flags |= SC_APDU_FLAGS_CHAINING; |
2058 | 0 | apdu.data = part; |
2059 | 0 | apdu.datalen = apdu.lc = plen; |
2060 | | |
2061 | | /* If the last part has odd length, we add zero padding to make it even. |
2062 | | * Gnuk does not allow data with odd length */ |
2063 | 0 | if (plen < 256 && (plen % 2) != 0) { |
2064 | 0 | memcpy(roundbuf, part, plen); |
2065 | 0 | roundbuf[plen++] = 0; |
2066 | 0 | apdu.data = roundbuf; |
2067 | 0 | apdu.datalen = apdu.lc = plen; |
2068 | 0 | } |
2069 | |
|
2070 | 0 | r = sc_transmit_apdu(card, &apdu); |
2071 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
2072 | | /* Check response */ |
2073 | 0 | LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "UPDATE BINARY returned error"); |
2074 | 0 | } |
2075 | | |
2076 | 0 | LOG_FUNC_RETURN(card->ctx, (int)length); |
2077 | 0 | } |
2078 | | |
2079 | | |
2080 | | /** |
2081 | | * Internal: use PUT DATA command to write. |
2082 | | */ |
2083 | | static int |
2084 | | pgp_put_data_plain(sc_card_t *card, unsigned int tag, const u8 *buf, size_t buf_len) |
2085 | 374 | { |
2086 | 374 | struct pgp_priv_data *priv = DRVDATA(card); |
2087 | 374 | sc_apdu_t apdu; |
2088 | 374 | u8 ins = 0xDA; |
2089 | 374 | u8 p1 = tag >> 8; |
2090 | 374 | u8 p2 = tag & 0xFF; |
2091 | 374 | u8 apdu_case = (card->type == SC_CARD_TYPE_OPENPGP_GNUK) |
2092 | 374 | ? SC_APDU_CASE_3_SHORT : SC_APDU_CASE_3; |
2093 | 374 | int r; |
2094 | | |
2095 | 374 | LOG_FUNC_CALLED(card->ctx); |
2096 | | |
2097 | | /* Extended Header list (DO 004D) needs a variant of PUT DATA command */ |
2098 | 374 | if (tag == 0x004D) { |
2099 | 0 | ins = 0xDB; |
2100 | 0 | p1 = 0x3F; |
2101 | 0 | p2 = 0xFF; |
2102 | 0 | } |
2103 | | |
2104 | | /* build APDU */ |
2105 | 374 | if (buf != NULL && buf_len > 0) { |
2106 | 374 | sc_format_apdu(card, &apdu, apdu_case, ins, p1, p2); |
2107 | | |
2108 | | /* if card/reader does not support extended APDUs, but chaining, then set it */ |
2109 | 374 | if (((card->caps & SC_CARD_CAP_APDU_EXT) == 0) && (priv->ext_caps & EXT_CAP_CHAINING)) |
2110 | 10 | apdu.flags |= SC_APDU_FLAGS_CHAINING; |
2111 | | |
2112 | 374 | apdu.data = (u8 *)buf; |
2113 | 374 | apdu.datalen = buf_len; |
2114 | 374 | apdu.lc = buf_len; |
2115 | 374 | } |
2116 | 0 | else { |
2117 | | /* This case is to empty DO */ |
2118 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, ins, p1, p2); |
2119 | 0 | } |
2120 | | |
2121 | | /* send APDU to card */ |
2122 | 374 | r = sc_transmit_apdu(card, &apdu); |
2123 | 374 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
2124 | | /* check response */ |
2125 | 371 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
2126 | 371 | LOG_TEST_RET(card->ctx, r, "Card returned error"); |
2127 | | |
2128 | 165 | LOG_FUNC_RETURN(card->ctx, (int)buf_len); |
2129 | 165 | } |
2130 | | |
2131 | | |
2132 | | /** |
2133 | | * ABI: ISO 7816-4 PUT DATA - write contents of a DO. |
2134 | | */ |
2135 | | static int |
2136 | | pgp_put_data(sc_card_t *card, unsigned int tag, const u8 *buf, size_t buf_len) |
2137 | 374 | { |
2138 | 374 | struct pgp_priv_data *priv = DRVDATA(card); |
2139 | 374 | pgp_blob_t *affected_blob = NULL; |
2140 | 374 | pgp_do_info_t *dinfo = NULL; |
2141 | 374 | int r; |
2142 | | |
2143 | 374 | LOG_FUNC_CALLED(card->ctx); |
2144 | | |
2145 | | /* Check if there is a blob for the given tag */ |
2146 | 374 | affected_blob = pgp_find_blob(card, tag); |
2147 | | |
2148 | | /* Non-readable DOs have no represented blob, we have to check from pgp_get_info_by_tag */ |
2149 | 374 | if (affected_blob == NULL) |
2150 | 155 | dinfo = pgp_get_info_by_tag(card, tag); |
2151 | 219 | else |
2152 | 219 | dinfo = affected_blob->info; |
2153 | | |
2154 | | /* Make sure the DO exists and is writeable */ |
2155 | 374 | if (dinfo == NULL) { |
2156 | 0 | sc_log(card->ctx, "The DO %04X does not exist.", tag); |
2157 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
2158 | 0 | } |
2159 | 374 | else if ((dinfo->access & WRITE_MASK) == WRITE_NEVER) { |
2160 | 0 | sc_log(card->ctx, "DO %04X is not writable.", tag); |
2161 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ALLOWED); |
2162 | 0 | } |
2163 | | |
2164 | | /* Check data size. |
2165 | | * We won't check other DOs than 7F21 (certificate), because their capacity |
2166 | | * is hard-coded and may change in various version of the card. |
2167 | | * If we check here, the driver may be stuck to a limit version number of card. |
2168 | | * 7F21 size is soft-coded, so we can check it. */ |
2169 | 374 | if (tag == DO_CERT && buf_len > priv->max_cert_size) { |
2170 | 0 | sc_log(card->ctx, |
2171 | 0 | "Data size %"SC_FORMAT_LEN_SIZE_T"u exceeds DO size limit %"SC_FORMAT_LEN_SIZE_T"u.", |
2172 | 0 | buf_len, priv->max_cert_size); |
2173 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_WRONG_LENGTH); |
2174 | 0 | } |
2175 | | |
2176 | 374 | if (tag == DO_CERT && card->type == SC_CARD_TYPE_OPENPGP_GNUK) { |
2177 | | /* Gnuk need a special way to write certificate. */ |
2178 | 0 | r = gnuk_write_certificate(card, buf, buf_len); |
2179 | 0 | } |
2180 | 374 | else { |
2181 | 374 | r = pgp_put_data_plain(card, tag, buf, buf_len); |
2182 | 374 | } |
2183 | | |
2184 | | /* instruct more in case of error */ |
2185 | 374 | if (r == SC_ERROR_SECURITY_STATUS_NOT_SATISFIED) { |
2186 | 1 | sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Please verify PIN first."); |
2187 | 1 | } |
2188 | 374 | LOG_TEST_RET(card->ctx, r, "PUT DATA returned error"); |
2189 | | |
2190 | 165 | if (affected_blob) { |
2191 | | /* update the corresponding file */ |
2192 | 143 | sc_log(card->ctx, "Updating the corresponding blob data"); |
2193 | 143 | r = pgp_set_blob(affected_blob, buf, buf_len); |
2194 | 143 | if (r < 0) |
2195 | 0 | sc_log(card->ctx, "Failed to update blob %04X. Error %d.", affected_blob->id, r); |
2196 | | /* pgp_set_blob()'s failures do not impact pgp_put_data()'s result */ |
2197 | 143 | } |
2198 | | |
2199 | 165 | LOG_FUNC_RETURN(card->ctx, (int)buf_len); |
2200 | 165 | } |
2201 | | |
2202 | | #ifdef ENABLE_OPENSSL |
2203 | | /* KDF_ITERSALTED_S2K algorithm, a simplified version of openpgp_s2k from libgcrypt[1] |
2204 | | * that supports one-pass only and outputs the same key length as the hash length of |
2205 | | * the specified hash algorithm. |
2206 | | * [1]: https://github.com/gpg/libgcrypt/blob/libgcrypt-1.11.0/cipher/kdf.c#L32-L109 |
2207 | | */ |
2208 | | static int |
2209 | | kdf_itersalted_s2k(struct sc_context *ctx, const char *hash, const uint8_t *pin, size_t pinlen, const uint8_t *salt, size_t saltlen, uint32_t iterations, uint8_t **out, size_t *outlen) |
2210 | 0 | { |
2211 | 0 | int r = SC_ERROR_INVALID_ARGUMENTS; |
2212 | 0 | EVP_MD *md = NULL; |
2213 | 0 | EVP_MD_CTX *hctx = NULL; |
2214 | 0 | int keysize; |
2215 | 0 | uint8_t *outkey = NULL; |
2216 | 0 | size_t count; |
2217 | 0 | int ret; |
2218 | 0 | unsigned int outsize; |
2219 | |
|
2220 | 0 | if (!ctx || !hash || !pin || pinlen == 0 || !salt || saltlen != 8 || !out || !outlen) { |
2221 | 0 | return r; |
2222 | 0 | } |
2223 | | |
2224 | 0 | md = sc_evp_md(ctx, hash); |
2225 | 0 | if (md == NULL) { |
2226 | 0 | return r; |
2227 | 0 | } |
2228 | | |
2229 | 0 | r = SC_ERROR_INTERNAL; |
2230 | 0 | hctx = EVP_MD_CTX_new(); |
2231 | 0 | if (hctx == NULL) { |
2232 | 0 | goto out; |
2233 | 0 | } |
2234 | | |
2235 | 0 | if (!EVP_DigestInit(hctx, md)) { |
2236 | 0 | goto out; |
2237 | 0 | } |
2238 | | |
2239 | 0 | keysize = EVP_MD_size(md); |
2240 | 0 | if (keysize <= 0) { |
2241 | 0 | goto out; |
2242 | 0 | } |
2243 | | |
2244 | 0 | outkey = (uint8_t *)sc_mem_secure_alloc(keysize); |
2245 | 0 | if (outkey == NULL) { |
2246 | 0 | r = SC_ERROR_NOT_ENOUGH_MEMORY; |
2247 | 0 | goto out; |
2248 | 0 | } |
2249 | | |
2250 | 0 | count = iterations; |
2251 | 0 | if (count < pinlen + saltlen) { |
2252 | 0 | count = pinlen + saltlen; |
2253 | 0 | } |
2254 | |
|
2255 | 0 | while (count >= pinlen + saltlen) { |
2256 | 0 | ret = EVP_DigestUpdate(hctx, salt, saltlen); |
2257 | 0 | if (!ret) { |
2258 | 0 | goto out; |
2259 | 0 | } |
2260 | 0 | ret = EVP_DigestUpdate(hctx, pin, pinlen); |
2261 | 0 | if (!ret) { |
2262 | 0 | goto out; |
2263 | 0 | } |
2264 | 0 | count -= pinlen + saltlen; |
2265 | 0 | } |
2266 | | |
2267 | 0 | if (count <= saltlen) { |
2268 | 0 | ret = EVP_DigestUpdate(hctx, salt, count); |
2269 | 0 | } else { |
2270 | 0 | ret = EVP_DigestUpdate(hctx, salt, saltlen); |
2271 | 0 | if (!ret) { |
2272 | 0 | goto out; |
2273 | 0 | } |
2274 | 0 | count -= saltlen; |
2275 | 0 | ret = EVP_DigestUpdate(hctx, pin, count); |
2276 | 0 | } |
2277 | 0 | if (!ret) { |
2278 | 0 | goto out; |
2279 | 0 | } |
2280 | | |
2281 | 0 | outsize = keysize; |
2282 | 0 | ret = EVP_DigestFinal(hctx, outkey, &outsize); |
2283 | 0 | if (ret && outsize == (unsigned int)keysize) { |
2284 | 0 | r = SC_SUCCESS; |
2285 | 0 | } |
2286 | 0 | out: |
2287 | 0 | if (r != SC_SUCCESS && outkey != NULL) { |
2288 | 0 | sc_mem_secure_clear_free(outkey, keysize); |
2289 | 0 | } |
2290 | 0 | EVP_MD_CTX_free(hctx); |
2291 | 0 | sc_evp_md_free(md); |
2292 | 0 | if (r == SC_SUCCESS) { |
2293 | 0 | *out = outkey; |
2294 | 0 | *outlen = outsize; |
2295 | 0 | } |
2296 | 0 | return r; |
2297 | 0 | } |
2298 | | |
2299 | | static int |
2300 | | pgp_kdf_do_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data, int *tries_left) |
2301 | 0 | { |
2302 | 0 | int r = SC_ERROR_INVALID_ARGUMENTS; |
2303 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
2304 | 0 | const pgp_pin_kdf_info_t *const info = priv->pin_kdf_info; |
2305 | 0 | uint8_t *pin1_derived = NULL; |
2306 | 0 | uint8_t *pin2_derived = NULL; |
2307 | 0 | size_t pin1_derived_len = 0; |
2308 | 0 | size_t pin2_derived_len = 0; |
2309 | 0 | const uint8_t *const pin1 = data->pin1.data; |
2310 | 0 | const size_t pin1len = data->pin1.len; |
2311 | 0 | const uint8_t *const pin2 = data->pin2.data; |
2312 | 0 | const size_t pin2len = data->pin2.len; |
2313 | 0 | const uint8_t *salt = NULL; |
2314 | 0 | size_t saltlen = 0; |
2315 | |
|
2316 | 0 | switch (data->cmd) { |
2317 | 0 | case SC_PIN_CMD_VERIFY: |
2318 | 0 | case SC_PIN_CMD_CHANGE: |
2319 | 0 | case SC_PIN_CMD_UNBLOCK: |
2320 | 0 | break; |
2321 | 0 | default: |
2322 | 0 | LOG_FUNC_RETURN(card->ctx, iso_ops->pin_cmd(card, data, tries_left)); |
2323 | 0 | } |
2324 | 0 | if (!info) { |
2325 | 0 | return r; |
2326 | 0 | } |
2327 | | |
2328 | 0 | switch (data->pin_reference) { |
2329 | 0 | case 0x81: |
2330 | 0 | case 0x82: |
2331 | 0 | salt = info->userpw_salt->data; |
2332 | 0 | saltlen = info->userpw_salt->len; |
2333 | 0 | break; |
2334 | 0 | case 0x83: |
2335 | 0 | salt = info->adminpw_salt->data; |
2336 | 0 | saltlen = info->adminpw_salt->len; |
2337 | 0 | break; |
2338 | 0 | } |
2339 | | |
2340 | 0 | switch (data->cmd) { |
2341 | 0 | case SC_PIN_CMD_UNBLOCK: |
2342 | 0 | case SC_PIN_CMD_CHANGE: |
2343 | 0 | r = kdf_itersalted_s2k(card->ctx, info->hash_algo, pin2, pin2len, salt, saltlen, info->iterations, &pin2_derived, &pin2_derived_len); |
2344 | 0 | if (r != SC_SUCCESS) { |
2345 | 0 | break; |
2346 | 0 | } |
2347 | 0 | data->pin2.data = pin2_derived; |
2348 | 0 | data->pin2.len = pin2_derived_len; |
2349 | 0 | if (data->cmd == SC_PIN_CMD_UNBLOCK) { |
2350 | 0 | salt = info->adminpw_salt->data; |
2351 | 0 | saltlen = info->adminpw_salt->len; |
2352 | 0 | } |
2353 | | // fallthrough |
2354 | 0 | case SC_PIN_CMD_VERIFY: |
2355 | 0 | r = kdf_itersalted_s2k(card->ctx, info->hash_algo, pin1, pin1len, salt, saltlen, info->iterations, &pin1_derived, &pin1_derived_len); |
2356 | 0 | if (r != SC_SUCCESS) { |
2357 | 0 | break; |
2358 | 0 | } |
2359 | 0 | data->pin1.data = pin1_derived; |
2360 | 0 | data->pin1.len = pin1_derived_len; |
2361 | 0 | break; |
2362 | 0 | } |
2363 | | |
2364 | 0 | if (r == SC_SUCCESS) { |
2365 | 0 | r = iso_ops->pin_cmd(card, data, tries_left); |
2366 | 0 | } |
2367 | 0 | if (pin1_derived) { |
2368 | 0 | data->pin1.data = pin1; |
2369 | 0 | data->pin1.len = pin1len; |
2370 | 0 | sc_mem_secure_clear_free(pin1_derived, pin1_derived_len); |
2371 | 0 | } |
2372 | 0 | if (pin2_derived) { |
2373 | 0 | data->pin2.data = pin2; |
2374 | 0 | data->pin2.len = pin2len; |
2375 | 0 | sc_mem_secure_clear_free(pin2_derived, pin2_derived_len); |
2376 | 0 | } |
2377 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
2378 | 0 | } |
2379 | | #endif /* ENABLE_OPENSSL */ |
2380 | | |
2381 | | /** |
2382 | | * ABI: ISO 7816-9 PIN CMD - verify/change/unblock a PIN. |
2383 | | */ |
2384 | | static int |
2385 | | pgp_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data, int *tries_left) |
2386 | 333 | { |
2387 | 333 | struct pgp_priv_data *priv = DRVDATA(card); |
2388 | 333 | struct sc_card_operations ops = {.pin_cmd = iso_ops->pin_cmd}; |
2389 | | |
2390 | 333 | LOG_FUNC_CALLED(card->ctx); |
2391 | | |
2392 | 333 | if (data->pin_type != SC_AC_CHV) |
2393 | 333 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2394 | 333 | "invalid PIN type"); |
2395 | | |
2396 | | /* In general, the PIN Reference is extracted from the key-id, |
2397 | | * for example, CHV0 -> Ref=0, CHV1 -> Ref=1. |
2398 | | * However, in the case of OpenPGP, the PIN Ref to compose APDU |
2399 | | * must be 81, 82, 83. |
2400 | | * So, if we receive Ref=1, Ref=2, we must convert to 81, 82... |
2401 | | * In OpenPGP v1, the PINs are named CHV1, CHV2, CHV3. |
2402 | | * In v2, they are named PW1, PW3 (PW1 operates in 2 modes). |
2403 | | * |
2404 | | * The PIN references (P2 in APDU) for "VERIFY" are the same in both versions: |
2405 | | * 81 (CHV1 or PW1), 82 (CHV2 or PW1-mode 2), 83 (CHV3 or PW3), |
2406 | | * On the other hand from version 2.0 "CHANGE REFERENCE DATA" and |
2407 | | * "RESET RETRY COUNTER" don't support PW1-mode 2 (82) and need this |
2408 | | * value changed to PW1 (81). |
2409 | | * Both of these commands also differ between card versions in that |
2410 | | * v1 cards can use only implicit old PIN or CHV3 test for both commands |
2411 | | * whereas v2 can use both implicit (for PW3) and explicit |
2412 | | * (for special "Resetting Code") PIN test for "RESET RETRY COUNTER" |
2413 | | * and only explicit test for "CHANGE REFERENCE DATA". |
2414 | | * |
2415 | | * Note that if this function is called from sc_pkcs15_verify_pin() in pkcs15-pin.c, |
2416 | | * the Ref is already 81, 82, 83. |
2417 | | */ |
2418 | | |
2419 | | /* convert the PIN Reference if needed */ |
2420 | 333 | data->pin_reference |= 0x80; |
2421 | | |
2422 | | /* check version-dependent constraints */ |
2423 | 333 | if (data->cmd == SC_PIN_CMD_CHANGE || data->cmd == SC_PIN_CMD_UNBLOCK) { |
2424 | 0 | if (priv->bcd_version >= OPENPGP_CARD_2_0) { |
2425 | 0 | if (data->pin_reference == 0x82) |
2426 | 0 | data->pin_reference = 0x81; |
2427 | |
|
2428 | 0 | if (data->cmd == SC_PIN_CMD_CHANGE) { |
2429 | 0 | if (data->pin1.len == 0 && |
2430 | 0 | !(data->flags & SC_PIN_CMD_USE_PINPAD)) |
2431 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2432 | 0 | "v2 cards don't support implicit old PIN for PIN change"); |
2433 | | |
2434 | 0 | data->flags &= ~SC_PIN_CMD_IMPLICIT_CHANGE; |
2435 | 0 | } |
2436 | 0 | } else { |
2437 | 0 | if (data->pin1.len != 0) { |
2438 | 0 | sc_log(card->ctx, |
2439 | 0 | "v1 cards don't support explicit old or CHV3 PIN, PIN ignored."); |
2440 | 0 | sc_log(card->ctx, |
2441 | 0 | "please make sure that you have verified the relevant PIN first."); |
2442 | 0 | data->pin1.len = 0; |
2443 | 0 | } |
2444 | |
|
2445 | 0 | data->flags |= SC_PIN_CMD_IMPLICIT_CHANGE; |
2446 | 0 | } |
2447 | 0 | } |
2448 | | |
2449 | 333 | if (data->cmd == SC_PIN_CMD_UNBLOCK && data->pin2.len == 0 && |
2450 | 0 | !(data->flags & SC_PIN_CMD_USE_PINPAD)) |
2451 | 333 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2452 | 333 | "new PIN must be provided for unblock operation"); |
2453 | | |
2454 | | /* ensure pin_reference is 81, 82, 83 */ |
2455 | 333 | if (data->pin_reference < 0x81 || data->pin_reference > 0x83) |
2456 | 333 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2457 | 333 | "Invalid key ID; must be 1, 2, or 3"); |
2458 | | |
2459 | | /* emulate SC_PIN_CMD_GET_INFO command for cards not supporting it */ |
2460 | 333 | if (data->cmd == SC_PIN_CMD_GET_INFO && (card->caps & SC_CARD_CAP_ISO7816_PIN_INFO) == 0) { |
2461 | 32 | u8 c4data[10]; |
2462 | 32 | int r; |
2463 | | |
2464 | 32 | r = sc_get_data(card, 0x00c4, c4data, sizeof(c4data)); |
2465 | 32 | LOG_TEST_RET(card->ctx, r, "reading CHV status bytes failed"); |
2466 | | |
2467 | 7 | if (r != 7) |
2468 | 7 | LOG_TEST_RET(card->ctx, SC_ERROR_OBJECT_NOT_VALID, |
2469 | 2 | "CHV status bytes have unexpected length"); |
2470 | | |
2471 | 2 | data->pin1.tries_left = c4data[3 + (data->pin_reference & 0x0F)]; |
2472 | 2 | data->pin1.max_tries = 3; |
2473 | 2 | data->pin1.logged_in = SC_PIN_STATE_UNKNOWN; |
2474 | 2 | if (tries_left != NULL) |
2475 | 0 | *tries_left = data->pin1.tries_left; |
2476 | | |
2477 | 2 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
2478 | 2 | } |
2479 | | |
2480 | 301 | #ifdef ENABLE_OPENSSL |
2481 | 301 | if (priv->pin_kdf_info) { |
2482 | 0 | ops.pin_cmd = &pgp_kdf_do_pin_cmd; |
2483 | 0 | } |
2484 | 301 | #endif /* ENABLE_OPENSSL */ |
2485 | | |
2486 | 301 | LOG_FUNC_RETURN(card->ctx, ops.pin_cmd(card, data, tries_left)); |
2487 | 301 | } |
2488 | | |
2489 | | |
2490 | | /** |
2491 | | * ABI: ISO 7816-8 LOGOUT - reset all access rights gained. |
2492 | | */ |
2493 | | int pgp_logout(struct sc_card *card) |
2494 | 0 | { |
2495 | 0 | int r = SC_SUCCESS; |
2496 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
2497 | |
|
2498 | 0 | LOG_FUNC_CALLED(card->ctx); |
2499 | |
|
2500 | 0 | if (priv->bcd_version >= OPENPGP_CARD_3_1) { |
2501 | 0 | unsigned char pin_reference; |
2502 | 0 | for (pin_reference = 0x81; pin_reference <= 0x83; pin_reference++) { |
2503 | 0 | int tmp = iso7816_logout(card, pin_reference); |
2504 | 0 | if (r == SC_SUCCESS) { |
2505 | 0 | r = tmp; |
2506 | 0 | } |
2507 | 0 | } |
2508 | 0 | } else { |
2509 | 0 | sc_path_t path; |
2510 | 0 | sc_file_t *file = NULL; |
2511 | | |
2512 | | /* select application "OpenPGP" */ |
2513 | 0 | sc_format_path("D276:0001:2401", &path); |
2514 | 0 | path.type = SC_PATH_TYPE_DF_NAME; |
2515 | 0 | r = iso_ops->select_file(card, &path, &file); |
2516 | 0 | sc_file_free(file); |
2517 | 0 | } |
2518 | |
|
2519 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
2520 | 0 | } |
2521 | | |
2522 | | |
2523 | | /** |
2524 | | * ABI: ISO 7816-8 SET SECURITY ENVIRONMENT. |
2525 | | * This is optional in the OpenPGP Card 3.4 specs |
2526 | | */ |
2527 | | static int |
2528 | | pgp_set_security_env(sc_card_t *card, |
2529 | | const sc_security_env_t *env, int se_num) |
2530 | 0 | { |
2531 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
2532 | |
|
2533 | 0 | LOG_FUNC_CALLED(card->ctx); |
2534 | | |
2535 | | /* The SC_SEC_ENV_ALG_PRESENT is set always so let it pass for GNUK */ |
2536 | 0 | if ((env->flags & SC_SEC_ENV_ALG_PRESENT) |
2537 | 0 | && (env->algorithm != SC_ALGORITHM_RSA) |
2538 | 0 | && (priv->bcd_version < OPENPGP_CARD_3_0) |
2539 | 0 | && (card->type != SC_CARD_TYPE_OPENPGP_GNUK)) |
2540 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2541 | 0 | "only RSA algorithm supported"); |
2542 | | |
2543 | 0 | if (!(env->flags & SC_SEC_ENV_KEY_REF_PRESENT) || (env->key_ref_len != 1)) |
2544 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2545 | 0 | "exactly one key reference required"); |
2546 | | |
2547 | 0 | if (env->flags & SC_SEC_ENV_FILE_REF_PRESENT) |
2548 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2549 | 0 | "passing file references not supported"); |
2550 | | |
2551 | 0 | sc_log(card->ctx, "Key ref %d", env->key_ref[0]); |
2552 | 0 | switch (env->operation) { |
2553 | 0 | case SC_SEC_OPERATION_SIGN: |
2554 | 0 | sc_log(card->ctx, "Operation: Sign."); |
2555 | 0 | if (env->key_ref[0] != 0x00 && env->key_ref[0] != 0x02) { |
2556 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_NOT_SUPPORTED, |
2557 | 0 | "Key reference not compatible with " |
2558 | 0 | "requested usage"); |
2559 | 0 | } |
2560 | 0 | break; |
2561 | 0 | case SC_SEC_OPERATION_DECIPHER: |
2562 | 0 | sc_log(card->ctx, "Operation: Decipher."); |
2563 | | /* we allow key ref 2 (auth key) to be used for deciphering */ |
2564 | 0 | if (env->key_ref[0] != 0x01 && env->key_ref[0] != 0x02) { |
2565 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_NOT_SUPPORTED, |
2566 | 0 | "Key reference not compatible with " |
2567 | 0 | "requested usage"); |
2568 | 0 | } |
2569 | 0 | break; |
2570 | 0 | case SC_SEC_OPERATION_DERIVE: |
2571 | 0 | sc_log(card->ctx, "Operation: Derive: No particular action needed"); |
2572 | 0 | break; |
2573 | 0 | default: |
2574 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2575 | 0 | "invalid operation"); |
2576 | 0 | } |
2577 | | |
2578 | 0 | priv->sec_env = *env; |
2579 | |
|
2580 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
2581 | 0 | } |
2582 | | |
2583 | | |
2584 | | /** |
2585 | | * set MANAGE SECURITY ENVIRONMENT as documented in 7.2.18 since OpenPGP Card v3.3 |
2586 | | * |
2587 | | * "This optional command (announced in Extended Capabilities) assigns a specific key to a |
2588 | | * command. The DEC-key (Key-Ref 2) can be assigned to the command INTERNAL AUTHENTICATE |
2589 | | * and the AUT-Key (Key.Ref 3) can be linked to the command PSO:DECIPHER also." |
2590 | | * |
2591 | | * key: Key-Ref to change (2 for DEC-Key or 3 for AUT-Key) |
2592 | | * p2: Usage to set (0xb8 for PSO:DECIPHER or 0xa4 for INTERNAL AUTHENTICATE) |
2593 | | **/ |
2594 | | static int |
2595 | | pgp_set_MSE(sc_card_t *card, int key, u8 p2) |
2596 | 0 | { |
2597 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
2598 | 0 | sc_apdu_t apdu; |
2599 | 0 | u8 apdu_case = SC_APDU_CASE_3; |
2600 | 0 | u8 apdu_data[3]; |
2601 | 0 | int r; |
2602 | |
|
2603 | 0 | LOG_FUNC_CALLED(card->ctx); |
2604 | | |
2605 | | // check if MSE is supported |
2606 | 0 | if (!(priv->ext_caps & EXT_CAP_MSE)) |
2607 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
2608 | | |
2609 | | // create apdu |
2610 | 0 | sc_format_apdu(card, &apdu, apdu_case, 0x22, 0x41, p2); |
2611 | 0 | apdu.lc = 3; |
2612 | 0 | apdu_data[0] = 0x83; |
2613 | 0 | apdu_data[1] = 0x01; |
2614 | 0 | apdu_data[2] = key; |
2615 | 0 | apdu.data = apdu_data; |
2616 | 0 | apdu.datalen = 3; |
2617 | | |
2618 | | // transmit apdu |
2619 | 0 | r = sc_transmit_apdu(card, &apdu); |
2620 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
2621 | | |
2622 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
2623 | 0 | LOG_TEST_RET(card->ctx, r, "Card returned error"); |
2624 | | |
2625 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
2626 | 0 | } |
2627 | | |
2628 | | |
2629 | | /** |
2630 | | * ABI: ISO 7816-8 COMPUTE DIGITAL SIGNATURE. |
2631 | | */ |
2632 | | static int |
2633 | | pgp_compute_signature(sc_card_t *card, const u8 *data, |
2634 | | size_t data_len, u8 * out, size_t outlen) |
2635 | 0 | { |
2636 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
2637 | 0 | sc_security_env_t *env = &priv->sec_env; |
2638 | 0 | sc_apdu_t apdu; |
2639 | 0 | u8 apdu_case = (card->type == SC_CARD_TYPE_OPENPGP_GNUK) |
2640 | 0 | ? SC_APDU_CASE_4_SHORT : SC_APDU_CASE_4; |
2641 | 0 | int r; |
2642 | |
|
2643 | 0 | LOG_FUNC_CALLED(card->ctx); |
2644 | |
|
2645 | 0 | if (env->operation != SC_SEC_OPERATION_SIGN) |
2646 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2647 | 0 | "invalid operation"); |
2648 | | |
2649 | 0 | switch (env->key_ref[0]) { |
2650 | 0 | case 0x00: /* signature key */ |
2651 | | /* PSO SIGNATURE */ |
2652 | 0 | sc_format_apdu(card, &apdu, apdu_case, 0x2A, 0x9E, 0x9A); |
2653 | 0 | break; |
2654 | 0 | case 0x02: /* authentication key */ |
2655 | | /* INTERNAL AUTHENTICATE */ |
2656 | 0 | sc_format_apdu(card, &apdu, apdu_case, 0x88, 0, 0); |
2657 | 0 | break; |
2658 | 0 | case 0x01: |
2659 | 0 | default: |
2660 | | /* From PKCS #11 point of view, we should be able to use |
2661 | | * curve25519 to do digital signature, but it is not how it |
2662 | | * is used in OpenPGP so we will not allow it here */ |
2663 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2664 | 0 | "invalid key reference"); |
2665 | 0 | } |
2666 | | |
2667 | | /* if card/reader does not support extended APDUs, but chaining, then set it */ |
2668 | 0 | if (((card->caps & SC_CARD_CAP_APDU_EXT) == 0) && (priv->ext_caps & EXT_CAP_CHAINING)) |
2669 | 0 | apdu.flags |= SC_APDU_FLAGS_CHAINING; |
2670 | |
|
2671 | 0 | apdu.lc = data_len; |
2672 | 0 | apdu.data = (u8 *)data; |
2673 | 0 | apdu.datalen = data_len; |
2674 | 0 | apdu.le = ((outlen >= 256) && !(card->caps & SC_CARD_CAP_APDU_EXT)) ? 256 : outlen; |
2675 | 0 | apdu.resp = out; |
2676 | 0 | apdu.resplen = outlen; |
2677 | |
|
2678 | 0 | r = sc_transmit_apdu(card, &apdu); |
2679 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
2680 | | |
2681 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
2682 | 0 | LOG_TEST_RET(card->ctx, r, "Card returned error"); |
2683 | | |
2684 | 0 | LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen); |
2685 | 0 | } |
2686 | | |
2687 | | |
2688 | | /** |
2689 | | * ABI: ISO 7816-8 DECIPHER - perform deciphering operation. |
2690 | | */ |
2691 | | static int |
2692 | | pgp_decipher(sc_card_t *card, const u8 *in, size_t inlen, |
2693 | | u8 *out, size_t outlen) |
2694 | 0 | { |
2695 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
2696 | 0 | sc_security_env_t *env = &priv->sec_env; |
2697 | 0 | sc_apdu_t apdu; |
2698 | 0 | u8 apdu_case = SC_APDU_CASE_4; |
2699 | 0 | u8 *temp = NULL, *p = NULL; |
2700 | 0 | size_t templen, pklen, dolen; |
2701 | 0 | int r; |
2702 | |
|
2703 | 0 | LOG_FUNC_CALLED(card->ctx); |
2704 | | |
2705 | | /* padding according to OpenPGP card spec 1.1 & 2.x section 7.2.9 / 3.x section 7.2.11 |
2706 | | * The longest possible prefix is 10 bytes for ECDH */ |
2707 | 0 | templen = inlen + 10; |
2708 | 0 | if (!(temp = malloc(templen))) |
2709 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
2710 | | |
2711 | | /* padding byte: 0xa6 = ECC; 0x00 = RSA; 0x02 = AES */ |
2712 | 0 | switch (env->algorithm) { |
2713 | 0 | case SC_ALGORITHM_RSA: |
2714 | | /* This is just PKCS#1.5 start byte and it should be already |
2715 | | * provided by the padding routines. But it lets put it here |
2716 | | * to make sure it does not conflict with following indicators */ |
2717 | 0 | temp[0] = 0x00; |
2718 | 0 | memcpy(temp + 1, in, inlen); |
2719 | 0 | inlen += 1; |
2720 | 0 | break; |
2721 | | |
2722 | 0 | case SC_ALGORITHM_EC: |
2723 | 0 | case SC_ALGORITHM_XEDDSA: |
2724 | | /* Calculate length of External Public Key (0x86) */ |
2725 | 0 | r = sc_asn1_put_tag(0x86, NULL, inlen, NULL, 0, NULL); |
2726 | 0 | if (r <= 0) { |
2727 | 0 | free(temp); |
2728 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
2729 | 0 | } |
2730 | 0 | pklen = r; |
2731 | | |
2732 | | /* Calculate length of Public Key DO (0x7F49) */ |
2733 | 0 | r = sc_asn1_put_tag(0x7f49, NULL, pklen, NULL, 0, NULL); |
2734 | 0 | if (r <= 0) { |
2735 | 0 | free(temp); |
2736 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
2737 | 0 | } |
2738 | 0 | dolen = r; |
2739 | |
|
2740 | 0 | p = temp; |
2741 | | /* This is 0xA6 Cipher DO with associated length field */ |
2742 | 0 | r = sc_asn1_put_tag(0xA6, NULL, dolen, p, templen - (p - temp), &p); |
2743 | 0 | if (r != SC_SUCCESS) { |
2744 | 0 | free(temp); |
2745 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
2746 | 0 | } |
2747 | | |
2748 | | /* Public Key DO (0x7F49) with associated length field */ |
2749 | 0 | r = sc_asn1_put_tag(0x7F49, NULL, pklen, p, templen - (p - temp), &p); |
2750 | 0 | if (r != SC_SUCCESS) { |
2751 | 0 | free(temp); |
2752 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
2753 | 0 | } |
2754 | | |
2755 | | /* External Public Key (0x86) with associated length */ |
2756 | 0 | r = sc_asn1_put_tag(0x86, in, inlen, p, templen - (p - temp), &p); |
2757 | 0 | if (r != SC_SUCCESS) { |
2758 | 0 | free(temp); |
2759 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
2760 | 0 | } |
2761 | 0 | inlen = (p - temp); |
2762 | 0 | break; |
2763 | 0 | case SC_ALGORITHM_AES: |
2764 | | /* not supported yet */ |
2765 | | /* |
2766 | | temp[0] = 0x02; |
2767 | | memcpy(temp + 1, in, inlen); |
2768 | | inlen += 1; |
2769 | | */ |
2770 | | /* fall through */ |
2771 | 0 | default: |
2772 | 0 | free(temp); |
2773 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
2774 | 0 | } |
2775 | | |
2776 | 0 | in = temp; |
2777 | |
|
2778 | 0 | if (env->operation != SC_SEC_OPERATION_DECIPHER && |
2779 | 0 | env->operation != SC_SEC_OPERATION_DERIVE) { |
2780 | 0 | free(temp); |
2781 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2782 | 0 | "invalid operation"); |
2783 | 0 | } |
2784 | | |
2785 | 0 | switch (env->key_ref[0]) { |
2786 | 0 | case 0x01: /* Decryption key */ |
2787 | 0 | case 0x02: /* authentication key */ |
2788 | | /* PSO DECIPHER */ |
2789 | 0 | sc_format_apdu(card, &apdu, apdu_case, 0x2A, 0x80, 0x86); |
2790 | 0 | break; |
2791 | 0 | case 0x00: /* signature key */ |
2792 | 0 | default: |
2793 | 0 | free(temp); |
2794 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2795 | 0 | "invalid key reference"); |
2796 | 0 | } |
2797 | | |
2798 | | /* Gnuk only supports short APDU, so we need to use command chaining */ |
2799 | 0 | if (card->type == SC_CARD_TYPE_OPENPGP_GNUK) { |
2800 | 0 | apdu.flags |= SC_APDU_FLAGS_CHAINING; |
2801 | 0 | } |
2802 | | /* if card/reader does not support extended APDUs, but chaining, then set it */ |
2803 | 0 | if (((card->caps & SC_CARD_CAP_APDU_EXT) == 0) && (priv->ext_caps & EXT_CAP_CHAINING)) |
2804 | 0 | apdu.flags |= SC_APDU_FLAGS_CHAINING; |
2805 | |
|
2806 | 0 | apdu.lc = inlen; |
2807 | 0 | apdu.data = (u8 *)in; |
2808 | 0 | apdu.datalen = inlen; |
2809 | 0 | apdu.le = ((outlen >= 256) && !(card->caps & SC_CARD_CAP_APDU_EXT)) ? 256 : outlen; |
2810 | 0 | apdu.resp = out; |
2811 | 0 | apdu.resplen = outlen; |
2812 | | |
2813 | | /* For OpenPGP Card >=v3.3, key slot 3 instead of 2 can be used for deciphering, |
2814 | | * but this has to be set via MSE beforehand on every usage (slot 2 is used by default) |
2815 | | * see section 7.2.18 of the specification of OpenPGP Card v3.3 */ |
2816 | 0 | if (priv->bcd_version >= OPENPGP_CARD_3_3 && env->key_ref[0] == 0x02){ |
2817 | 0 | pgp_set_MSE(card, 3, 0xb8); |
2818 | 0 | } |
2819 | |
|
2820 | 0 | r = sc_transmit_apdu(card, &apdu); |
2821 | 0 | free(temp); |
2822 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
2823 | | |
2824 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
2825 | 0 | LOG_TEST_RET(card->ctx, r, "Card returned error"); |
2826 | | |
2827 | | /* For OpenPGP Card >=v3.3, use key slot 2 for deciphering again (set to default) */ |
2828 | 0 | if (priv->bcd_version >= OPENPGP_CARD_3_3 && env->key_ref[0] == 0x02){ |
2829 | 0 | pgp_set_MSE(card, 2, 0xb8); |
2830 | 0 | } |
2831 | |
|
2832 | 0 | LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen); |
2833 | 0 | } |
2834 | | |
2835 | | |
2836 | | #ifdef ENABLE_OPENSSL |
2837 | | /** |
2838 | | * Internal: update algorithm attribute for new key size (before generating key). |
2839 | | **/ |
2840 | | static int |
2841 | | pgp_update_new_algo_attr(sc_card_t *card, sc_cardctl_openpgp_key_gen_store_info_t *key_info) |
2842 | 221 | { |
2843 | 221 | struct pgp_priv_data *priv = DRVDATA(card); |
2844 | 221 | pgp_blob_t *algo_blob = NULL; |
2845 | 221 | const unsigned int tag = 0x00C0 | key_info->key_id; |
2846 | 221 | int r = SC_SUCCESS; |
2847 | 221 | unsigned int i; |
2848 | | |
2849 | 221 | LOG_FUNC_CALLED(card->ctx); |
2850 | | |
2851 | 221 | r = pgp_seek_blob(card, priv->mf, tag, &algo_blob); |
2852 | 221 | LOG_TEST_RET(card->ctx, r, "Cannot get old algorithm attributes"); |
2853 | | |
2854 | 186 | if (priv->ext_caps & EXT_CAP_ALG_ATTR_CHANGEABLE) { |
2855 | | /* ECDSA and ECDH */ |
2856 | 179 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH |
2857 | 179 | || key_info->algorithm == SC_OPENPGP_KEYALGO_ECDSA |
2858 | 174 | || key_info->algorithm == SC_OPENPGP_KEYALGO_EDDSA){ |
2859 | | /* Note OpenPGP or current cards do not support 448 size keys yet */ |
2860 | 5 | unsigned char *aoid = NULL; /* ASN1 */ |
2861 | 5 | size_t aoid_len; |
2862 | 5 | struct sc_object_id *scoid = NULL; |
2863 | | |
2864 | 5 | scoid = &key_info->u.ec.oid; |
2865 | | /* |
2866 | | * Current OpenPGP cards use pre RFC8410 OIDs for ECDH and EdDSA |
2867 | | * so convert to older versions of the OIDs. |
2868 | | */ |
2869 | 15 | for (i = 0; ec_curves_alt[i].size > 0; i++) { |
2870 | 10 | if (sc_compare_oid(scoid, &ec_curves_alt[i].oid_alt)) { |
2871 | 0 | scoid = &ec_curves_alt[i].oid; |
2872 | 0 | break; |
2873 | 0 | } |
2874 | 10 | } |
2875 | | |
2876 | 5 | r = sc_encode_oid(card->ctx, scoid, &aoid, &aoid_len); |
2877 | 5 | LOG_TEST_RET(card->ctx, r, "invalid ec oid"); |
2878 | 5 | if (aoid == NULL || aoid_len < 3 || aoid[1] > 127) { |
2879 | 0 | free(aoid); |
2880 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL); |
2881 | 0 | } |
2882 | | |
2883 | 5 | key_info->data_len = aoid_len + 1 - 2; /* +1 for algorithm -2 drop 06 len */ |
2884 | 5 | key_info->data = malloc(key_info->data_len); |
2885 | 5 | if (!key_info->data) { |
2886 | 0 | free(aoid); |
2887 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ENOUGH_MEMORY); |
2888 | 0 | } |
2889 | | |
2890 | 5 | key_info->data[0] = key_info->algorithm; |
2891 | 45 | for (i = 0; i < aoid_len - 2; i++) |
2892 | 40 | key_info->data[i + 1] = aoid[i + 2]; |
2893 | | |
2894 | 5 | free(aoid); |
2895 | 5 | } |
2896 | | |
2897 | | /* RSA */ |
2898 | 174 | else if (key_info->algorithm == SC_OPENPGP_KEYALGO_RSA){ |
2899 | | |
2900 | | /* We can not rely on previous key attributes anymore, as it might be ECC */ |
2901 | 174 | if (key_info->u.rsa.exponent_len == 0 || key_info->u.rsa.modulus_len == 0) |
2902 | 174 | LOG_FUNC_RETURN(card->ctx,SC_ERROR_INVALID_ARGUMENTS); |
2903 | | |
2904 | 174 | key_info->data_len = 6; |
2905 | 174 | key_info->data = malloc(key_info->data_len); |
2906 | 174 | if (!key_info->data) |
2907 | 174 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ENOUGH_MEMORY); |
2908 | | |
2909 | 174 | key_info->data[0] = key_info->algorithm; |
2910 | 174 | ushort2bebytes(key_info->data + 1, key_info->u.rsa.modulus_len); |
2911 | | /* OpenPGP Card only accepts 32bit as exponent length field, |
2912 | | * although you can import keys with smaller exponent; |
2913 | | * thus we don't change rsa.exponent_len, but ignore it here */ |
2914 | 174 | ushort2bebytes(key_info->data + 3, SC_OPENPGP_MAX_EXP_BITS); |
2915 | | /* Import-Format of private key (e,p,q) */ |
2916 | 174 | key_info->data[5] = SC_OPENPGP_KEYFORMAT_RSA_STD; |
2917 | 174 | } |
2918 | 0 | else { |
2919 | 0 | sc_log(card->ctx, "Unknown algorithm id"); |
2920 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
2921 | 0 | } |
2922 | | |
2923 | 179 | r = pgp_put_data(card, tag, key_info->data, key_info->data_len); |
2924 | | /* Note: pgp_put_data calls pgp_set_blob */ |
2925 | 179 | LOG_TEST_RET(card->ctx, r, "Cannot set new algorithm attributes"); |
2926 | 179 | } else { |
2927 | 7 | sc_cardctl_openpgp_key_gen_store_info_t old_key_info; |
2928 | | |
2929 | 7 | if (pgp_parse_algo_attr_blob(card, algo_blob, &old_key_info) != SC_SUCCESS |
2930 | 6 | || old_key_info.algorithm != key_info->algorithm) |
2931 | 7 | LOG_TEST_RET(card->ctx, SC_ERROR_NO_CARD_SUPPORT, |
2932 | 7 | "Requested algorithm not supported"); |
2933 | | /* FIXME check whether the static parameters match the requested ones. */ |
2934 | 7 | } |
2935 | | |
2936 | 121 | LOG_FUNC_RETURN(card->ctx, r); |
2937 | 121 | } |
2938 | | |
2939 | | |
2940 | | /** |
2941 | | * Internal: store creation time of key. |
2942 | | * Pass non-zero outtime to use predefined time. |
2943 | | * Pass zero/null outtime to calculate current time. outtime then will be output. |
2944 | | * Pass null outtime to not receive output. |
2945 | | **/ |
2946 | | static int |
2947 | | pgp_store_creationtime(sc_card_t *card, u8 key_id, time_t *outtime) |
2948 | 112 | { |
2949 | 112 | int r; |
2950 | 112 | time_t createtime = 0; |
2951 | 112 | const size_t timestrlen = 64; |
2952 | 112 | char timestring[65]; |
2953 | 112 | u8 buf[4]; |
2954 | 112 | struct tm tm; |
2955 | | |
2956 | 112 | LOG_FUNC_CALLED(card->ctx); |
2957 | | |
2958 | 112 | if (key_id < 1 || key_id > 3) |
2959 | 112 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
2960 | 112 | "Invalid key ID; must be 1, 2, or 3"); |
2961 | | |
2962 | 112 | if (outtime != NULL && *outtime != 0) |
2963 | 0 | createtime = *outtime; |
2964 | 112 | else if (outtime != NULL) |
2965 | | /* set output */ |
2966 | 112 | *outtime = createtime = time(NULL); |
2967 | | |
2968 | | #ifdef _WIN32 |
2969 | | if (0 != gmtime_s(&tm, &createtime)) |
2970 | | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL); |
2971 | | #else |
2972 | 112 | if (NULL == gmtime_r(&createtime, &tm)) |
2973 | 112 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL); |
2974 | 112 | #endif |
2975 | 112 | strftime(timestring, timestrlen, "%c %Z", &tm); |
2976 | 112 | sc_log(card->ctx, "Creation time %s.", timestring); |
2977 | | /* Code borrowed from GnuPG */ |
2978 | 112 | ulong2bebytes(buf, (unsigned long)createtime); |
2979 | 112 | r = pgp_put_data(card, 0x00CD + key_id, buf, 4); |
2980 | 112 | LOG_TEST_RET(card->ctx, r, "Cannot write to DO"); |
2981 | 12 | LOG_FUNC_RETURN(card->ctx, r); |
2982 | 12 | } |
2983 | | |
2984 | | |
2985 | | /** |
2986 | | * Internal: calculate and store PGP fingerprints. |
2987 | | * Reference: GnuPG, app-openpgp.c. |
2988 | | **/ |
2989 | | static int |
2990 | | pgp_calculate_and_store_fingerprint(sc_card_t *card, time_t ctime, |
2991 | | sc_cardctl_openpgp_key_gen_store_info_t *key_info) |
2992 | 55 | { |
2993 | 55 | u8 fingerprint[SHA_DIGEST_LENGTH]; |
2994 | 55 | u8 *fp_buffer = NULL; /* fingerprint buffer, not hashed */ |
2995 | 55 | size_t fp_buffer_len; |
2996 | 55 | u8 *p; /* use this pointer to set fp_buffer content */ |
2997 | 55 | size_t pk_packet_len; |
2998 | 55 | unsigned int tag = 0x00C6 + key_info->key_id; |
2999 | 55 | pgp_blob_t *fpseq_blob = NULL; |
3000 | 55 | u8 *newdata = NULL; |
3001 | 55 | int r; |
3002 | | |
3003 | 55 | LOG_FUNC_CALLED(card->ctx); |
3004 | | |
3005 | | /* constructing public-key packet length */ |
3006 | | /* RSA */ |
3007 | 55 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_RSA) { |
3008 | | |
3009 | 51 | if (key_info->u.rsa.modulus == NULL |
3010 | 51 | || key_info->u.rsa.exponent == NULL |
3011 | 51 | || (key_info->u.rsa.modulus_len) == 0 |
3012 | 50 | || (key_info->u.rsa.exponent_len) == 0) { |
3013 | | |
3014 | 2 | sc_log(card->ctx, "Null data (modulus or exponent)"); |
3015 | 2 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
3016 | 2 | } |
3017 | | |
3018 | | /* https://tools.ietf.org/html/rfc4880 page 41, 72 */ |
3019 | 49 | pk_packet_len = 1 /* version number */ |
3020 | 49 | + 4 /* creation time */ |
3021 | 49 | + 1 /* algorithm */ |
3022 | 49 | + 2 /* algorithm-specific fields: RSA modulus+exponent */ |
3023 | 49 | + (BYTES4BITS(key_info->u.rsa.modulus_len)) |
3024 | 49 | + 2 |
3025 | 49 | + (BYTES4BITS(key_info->u.rsa.exponent_len)); |
3026 | | |
3027 | 49 | } |
3028 | | /* ECC */ |
3029 | 4 | else if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH || /* also includes XEDDSA */ |
3030 | 4 | key_info->algorithm == SC_OPENPGP_KEYALGO_ECDSA || |
3031 | 4 | key_info->algorithm == SC_OPENPGP_KEYALGO_EDDSA) { |
3032 | 4 | if (key_info->u.ec.ecpointQ == NULL || (key_info->u.ec.ecpointQ_len) == 0) { |
3033 | 4 | sc_log(card->ctx, "Error: ecpoint required!"); |
3034 | 4 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
3035 | 4 | } |
3036 | | |
3037 | | /* https://tools.ietf.org/html/rfc4880 page 41, 72 |
3038 | | * and https://tools.ietf.org/html/rfc6637 section 9 (page 8 and 9) */ |
3039 | 0 | pk_packet_len = 1 /* version number */ |
3040 | 0 | + 4 /* creation time */ |
3041 | 0 | + 1 /* algorithm */ |
3042 | 0 | + 1 /* oid len */ |
3043 | 0 | + key_info->u.ec.oidv_len /* oid */ |
3044 | 0 | + key_info->u.ec.ecpointQ_len; /* ecpoint */ |
3045 | | |
3046 | | /* KDF parameters for ECDH */ |
3047 | 0 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH) { |
3048 | | /* https://tools.ietf.org/html/rfc6637#section-8 */ |
3049 | 0 | pk_packet_len += 1 /* number of bytes */ |
3050 | 0 | + 1 /* version number */ |
3051 | 0 | + 1 /* KDF algo */ |
3052 | 0 | + 1; /* KEK algo */ |
3053 | 0 | } |
3054 | 0 | } else |
3055 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
3056 | 49 | sc_log(card->ctx, "pk_packet_len is %"SC_FORMAT_LEN_SIZE_T"u", pk_packet_len); |
3057 | | |
3058 | 49 | fp_buffer_len = 3 + pk_packet_len; |
3059 | 49 | p = fp_buffer = calloc(1, fp_buffer_len); |
3060 | 49 | if (p == NULL) |
3061 | 49 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ENOUGH_MEMORY); |
3062 | | |
3063 | | /* constructing public-key packet */ |
3064 | 49 | p[0] = 0x99; /* http://tools.ietf.org/html/rfc4880 page 71 */ |
3065 | 49 | ushort2bebytes(++p, (unsigned short)pk_packet_len); |
3066 | | /* start pk_packet */ |
3067 | 49 | p += 2; |
3068 | 49 | *p = 4; /* Version 4 key */ |
3069 | 49 | ulong2bebytes(++p, (unsigned long)ctime); /* Creation time */ |
3070 | 49 | p += 4; |
3071 | | |
3072 | | /* RSA */ |
3073 | 49 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_RSA) { |
3074 | 49 | unsigned short bytes_length = 0; |
3075 | | |
3076 | 49 | *p = 1; /* Algorithm ID, RSA */ |
3077 | 49 | p += 1; |
3078 | | |
3079 | | /* Modulus */ |
3080 | 49 | bytes_length = BYTES4BITS(key_info->u.rsa.modulus_len); |
3081 | 49 | ushort2bebytes(p, (unsigned short)key_info->u.rsa.modulus_len); |
3082 | 49 | p += 2; |
3083 | 49 | memcpy(p, key_info->u.rsa.modulus, bytes_length); |
3084 | 49 | p += bytes_length; |
3085 | | |
3086 | | /* Exponent */ |
3087 | 49 | bytes_length = BYTES4BITS(key_info->u.rsa.exponent_len); |
3088 | 49 | ushort2bebytes(p, (unsigned short)key_info->u.rsa.exponent_len); |
3089 | 49 | p += 2; |
3090 | 49 | memcpy(p, key_info->u.rsa.exponent, bytes_length); |
3091 | 49 | } |
3092 | | /* ECC */ |
3093 | 0 | else if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH || /* includes XEDDSA */ |
3094 | 0 | key_info->algorithm == SC_OPENPGP_KEYALGO_ECDSA || |
3095 | 0 | key_info->algorithm == SC_OPENPGP_KEYALGO_EDDSA) { |
3096 | | /* Algorithm ID, see https://tools.ietf.org/html/rfc6637#section-5 */ |
3097 | 0 | *p = key_info->algorithm + 6; |
3098 | 0 | p += 1; |
3099 | 0 | *p = key_info->u.ec.oidv_len; |
3100 | 0 | p += 1; |
3101 | 0 | memcpy(p, key_info->u.ec.oidv.value, key_info->u.ec.oidv_len); |
3102 | 0 | p += key_info->u.ec.oidv_len; |
3103 | 0 | memcpy(p, key_info->u.ec.ecpointQ, key_info->u.ec.ecpointQ_len); |
3104 | | |
3105 | | /* KDF parameters for ECDH */ |
3106 | 0 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH) { |
3107 | | /* https://tools.ietf.org/html/rfc6637#section-8 |
3108 | | * This is copied from GnuPG's ecdh_params() function in app-openpgp.c */ |
3109 | 0 | p += BYTES4BITS(key_info->u.ec.ecpointQ_len); |
3110 | 0 | *p = 0x03; /* number of bytes following */ |
3111 | 0 | p += 1; |
3112 | 0 | *p = 0x01; /* version of this format */ |
3113 | 0 | p += 1; |
3114 | 0 | if (key_info->u.ec.key_length <= 256) { /* ec bit size <= 256 */ |
3115 | 0 | *p = 0x08; /* KDF algo */ |
3116 | 0 | *(p+1) = 0x07; /* KEK algo */ |
3117 | 0 | } else if (key_info->u.ec.key_length <= 384) { /* ec bit size <= 384 */ |
3118 | 0 | *p = 0x09; /* KDF algo */ |
3119 | 0 | *(p+1) = 0x08; /* KEK algo */ |
3120 | 0 | } else { /* ec bit size = 512 or 521*/ |
3121 | 0 | *p = 0x0a; /* KDF algo */ |
3122 | 0 | *(p+1) = 0x09; /* KEK algo */ |
3123 | 0 | } |
3124 | 0 | } |
3125 | 0 | } else |
3126 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
3127 | | |
3128 | 49 | p = NULL; |
3129 | | |
3130 | | /* hash with SHA-1 */ |
3131 | 49 | SHA1(fp_buffer, fp_buffer_len, fingerprint); |
3132 | 49 | free(fp_buffer); |
3133 | | |
3134 | | /* store to DO */ |
3135 | 49 | sc_log(card->ctx, "Writing to DO %04X.", tag); |
3136 | 49 | r = pgp_put_data(card, tag, fingerprint, SHA_DIGEST_LENGTH); |
3137 | 49 | LOG_TEST_RET(card->ctx, r, "Cannot write to DO"); |
3138 | | |
3139 | | /* update the blob containing fingerprints (00C5) */ |
3140 | 12 | sc_log(card->ctx, "Updating fingerprint blob 00C5."); |
3141 | 12 | fpseq_blob = pgp_find_blob(card, 0x00C5); |
3142 | 12 | if (fpseq_blob == NULL) { |
3143 | 11 | r = SC_ERROR_OUT_OF_MEMORY; |
3144 | 11 | LOG_TEST_GOTO_ERR(card->ctx, r, "Cannot find blob 00C5"); |
3145 | 11 | } |
3146 | 1 | if (20U * key_info->key_id > fpseq_blob->len) { |
3147 | 1 | r = SC_ERROR_OBJECT_NOT_VALID; |
3148 | 1 | LOG_TEST_GOTO_ERR(card->ctx, r, "The 00C5 blob is not large enough"); |
3149 | 1 | } |
3150 | | |
3151 | | /* save the fingerprints sequence */ |
3152 | 0 | newdata = malloc(fpseq_blob->len); |
3153 | 0 | if (newdata == NULL) { |
3154 | 0 | r = SC_ERROR_OUT_OF_MEMORY; |
3155 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "Not enough memory to update fingerprint blob 00C5"); |
3156 | 0 | } |
3157 | | |
3158 | 0 | memcpy(newdata, fpseq_blob->data, fpseq_blob->len); |
3159 | | /* move p to the portion holding the fingerprint of the current key */ |
3160 | 0 | p = newdata + 20 * (key_info->key_id - 1); |
3161 | | /* copy new fingerprint value */ |
3162 | 0 | memcpy(p, fingerprint, 20); |
3163 | | /* set blob's data */ |
3164 | 0 | pgp_set_blob(fpseq_blob, newdata, fpseq_blob->len); |
3165 | 0 | free(newdata); |
3166 | |
|
3167 | 12 | err: |
3168 | 12 | LOG_FUNC_RETURN(card->ctx, r); |
3169 | 12 | } |
3170 | | |
3171 | | |
3172 | | /** |
3173 | | * Internal: update pubkey blob. |
3174 | | * Note that modulus_len, exponent_len is measured in bit. |
3175 | | **/ |
3176 | | static int |
3177 | | pgp_update_pubkey_blob(sc_card_t *card, sc_cardctl_openpgp_key_gen_store_info_t *key_info) |
3178 | 0 | { |
3179 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
3180 | 0 | pgp_blob_t *pk_blob; |
3181 | 0 | unsigned int blob_id = 0; |
3182 | 0 | sc_pkcs15_pubkey_t p15pubkey; |
3183 | 0 | u8 *data = NULL; |
3184 | 0 | size_t len; |
3185 | 0 | int r; |
3186 | |
|
3187 | 0 | LOG_FUNC_CALLED(card->ctx); |
3188 | |
|
3189 | 0 | if (key_info->key_id == SC_OPENPGP_KEY_SIGN) |
3190 | 0 | blob_id = DO_SIGN_SYM; |
3191 | 0 | else if (key_info->key_id == SC_OPENPGP_KEY_ENCR) |
3192 | 0 | blob_id = DO_ENCR_SYM; |
3193 | 0 | else if (key_info->key_id == SC_OPENPGP_KEY_AUTH) |
3194 | 0 | blob_id = DO_AUTH_SYM; |
3195 | 0 | else { |
3196 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
3197 | 0 | "Invalid key ID; must be 1, 2, or 3"); |
3198 | 0 | } |
3199 | | |
3200 | 0 | sc_log(card->ctx, "Retrieving blob %04X.", blob_id); |
3201 | 0 | r = pgp_get_blob(card, priv->mf, blob_id, &pk_blob); |
3202 | 0 | LOG_TEST_RET(card->ctx, r, "Cannot get the blob"); |
3203 | | |
3204 | | /* encode pubkey */ |
3205 | | /* RSA */ |
3206 | 0 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_RSA){ |
3207 | 0 | memset(&p15pubkey, 0, sizeof(p15pubkey)); |
3208 | 0 | p15pubkey.algorithm = SC_ALGORITHM_RSA; |
3209 | 0 | p15pubkey.u.rsa.modulus.data = key_info->u.rsa.modulus; |
3210 | 0 | p15pubkey.u.rsa.modulus.len = BYTES4BITS(key_info->u.rsa.modulus_len); |
3211 | 0 | p15pubkey.u.rsa.exponent.data = key_info->u.rsa.exponent; |
3212 | 0 | p15pubkey.u.rsa.exponent.len = BYTES4BITS(key_info->u.rsa.exponent_len); |
3213 | 0 | } |
3214 | | /* ECC */ |
3215 | 0 | else if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH || /* includes XEDDSA */ |
3216 | 0 | key_info->algorithm == SC_OPENPGP_KEYALGO_ECDSA || |
3217 | 0 | key_info->algorithm == SC_OPENPGP_KEYALGO_EDDSA) { |
3218 | 0 | memset(&p15pubkey, 0, sizeof(p15pubkey)); |
3219 | 0 | p15pubkey.algorithm = key_info->key_type; |
3220 | 0 | p15pubkey.u.ec.ecpointQ.value = key_info->u.ec.ecpointQ; |
3221 | 0 | p15pubkey.u.ec.ecpointQ.len = key_info->u.ec.ecpointQ_len; |
3222 | 0 | } else |
3223 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
3224 | | |
3225 | 0 | r = sc_pkcs15_encode_pubkey_as_spki(card->ctx, &p15pubkey, &data, &len); |
3226 | | /* |
3227 | | * key_info is missing an algo_id. sc_pkcs15_encode_pubkey_as_spki |
3228 | | * allocates one. Free it here. |
3229 | | */ |
3230 | 0 | free(p15pubkey.alg_id); |
3231 | 0 | p15pubkey.alg_id = NULL; |
3232 | 0 | LOG_TEST_RET(card->ctx, r, "Cannot encode pubkey"); |
3233 | | |
3234 | 0 | sc_log(card->ctx, "Updating blob %04X's content.", blob_id); |
3235 | 0 | r = pgp_set_blob(pk_blob, data, len); |
3236 | 0 | free(data); |
3237 | 0 | LOG_TEST_RET(card->ctx, r, "Cannot update blob content"); |
3238 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
3239 | 0 | } |
3240 | | |
3241 | | |
3242 | | /** |
3243 | | * Internal: parse response data and set output |
3244 | | **/ |
3245 | | static int |
3246 | | pgp_parse_and_set_pubkey_output(sc_card_t *card, u8 *data, size_t data_len, |
3247 | | sc_cardctl_openpgp_key_gen_store_info_t *key_info) |
3248 | 112 | { |
3249 | 112 | time_t ctime = 0; |
3250 | 112 | u8 *in = data; |
3251 | 112 | int r; |
3252 | 112 | LOG_FUNC_CALLED(card->ctx); |
3253 | | |
3254 | | /* store creation time */ |
3255 | 112 | r = pgp_store_creationtime(card, key_info->key_id, &ctime); |
3256 | 112 | if (r != SC_SUCCESS) { |
3257 | | /* TODO for now with GNUK at least, log but do not return error */ |
3258 | 112 | sc_log(card->ctx, "Cannot store creation time"); |
3259 | 112 | } |
3260 | | |
3261 | | /* parse response. Ref: pgp_enumerate_blob() */ |
3262 | 765 | while (data_len > (size_t) (in - data)) { |
3263 | 710 | unsigned int cla, tag, tmptag; |
3264 | 710 | size_t len; |
3265 | 710 | u8 *part = in; |
3266 | | |
3267 | | /* parse TLV structure */ |
3268 | 710 | r = sc_asn1_read_tag((const u8**)&part, data_len - (in - data), &cla, &tag, &len); |
3269 | 710 | if (part == NULL) |
3270 | 30 | r = SC_ERROR_ASN1_OBJECT_NOT_FOUND; |
3271 | 710 | LOG_TEST_RET(card->ctx, r, "Unexpected end of contents"); |
3272 | | /* undo ASN1's split of tag & class */ |
3273 | 898 | for (tmptag = tag; tmptag > 0x0FF; tmptag >>= 8) { |
3274 | 241 | cla <<= 8; |
3275 | 241 | } |
3276 | 657 | tag |= cla; |
3277 | | |
3278 | | /* RSA modulus */ |
3279 | 657 | if (tag == 0x0081) { |
3280 | 42 | if (key_info->algorithm != SC_OPENPGP_KEYALGO_RSA) { |
3281 | 1 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED); |
3282 | 1 | } |
3283 | 41 | if ((BYTES4BITS(key_info->u.rsa.modulus_len) < len) /* modulus_len is in bits */ |
3284 | 35 | || key_info->u.rsa.modulus == NULL) { |
3285 | | |
3286 | 6 | free(key_info->u.rsa.modulus); |
3287 | 6 | key_info->u.rsa.modulus = malloc(len); |
3288 | 6 | if (key_info->u.rsa.modulus == NULL) |
3289 | 6 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ENOUGH_MEMORY); |
3290 | 6 | } |
3291 | | |
3292 | | /* set values */ |
3293 | 41 | memcpy(key_info->u.rsa.modulus, part, len); |
3294 | 41 | key_info->u.rsa.modulus_len = len * 8; /* store length in bits */ |
3295 | 41 | } |
3296 | | /* RSA public exponent */ |
3297 | 615 | else if (tag == 0x0082) { |
3298 | 50 | if (key_info->algorithm != SC_OPENPGP_KEYALGO_RSA) { |
3299 | 1 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED); |
3300 | 1 | } |
3301 | 49 | if ((BYTES4BITS(key_info->u.rsa.exponent_len) < len) /* exponent_len is in bits */ |
3302 | 40 | || key_info->u.rsa.exponent == NULL) { |
3303 | | |
3304 | 9 | free(key_info->u.rsa.exponent); |
3305 | 9 | key_info->u.rsa.exponent = malloc(len); |
3306 | 9 | if (key_info->u.rsa.exponent == NULL) |
3307 | 9 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ENOUGH_MEMORY); |
3308 | 9 | } |
3309 | | |
3310 | | /* set values */ |
3311 | 49 | memcpy(key_info->u.rsa.exponent, part, len); |
3312 | 49 | key_info->u.rsa.exponent_len = len * 8; /* store length in bits */ |
3313 | 49 | } |
3314 | | /* ECC or EDDSA public key */ |
3315 | 565 | else if (tag == 0x0086) { |
3316 | | /* Note: SC_OPENPGP_KEYALGO_ECDH could be either EC or XEDDSA */ |
3317 | 2 | if (key_info->algorithm != SC_OPENPGP_KEYALGO_ECDSA && |
3318 | 1 | key_info->algorithm != SC_OPENPGP_KEYALGO_ECDH && |
3319 | 1 | key_info->algorithm != SC_OPENPGP_KEYALGO_EDDSA) { |
3320 | 1 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED); |
3321 | 1 | } |
3322 | | /* set the output data */ |
3323 | | /* key_info->u.ec.ecpoint_len is already set based on |
3324 | | * key_info->key_type |
3325 | | * EC is in 04||x||y format i.e. |
3326 | | * (field_length + 7)/8 * 2 + 1 in bytes |
3327 | | * len is ecpoint length + format byte |
3328 | | * see section 7.2.14 of 3.3.1 specs |
3329 | | * EDDSA and XEDDSA have no format byte and one number |
3330 | | * (field_length + 7)/8 in bytes |
3331 | | */ |
3332 | | /* GNUK returns 04||x||y */ |
3333 | | |
3334 | 1 | if (len != key_info->u.ec.ecpointQ_len) |
3335 | 1 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL); |
3336 | | |
3337 | 0 | switch (key_info->key_type) { |
3338 | 0 | case SC_ALGORITHM_EC: |
3339 | 0 | case SC_ALGORITHM_EDDSA: |
3340 | 0 | case SC_ALGORITHM_XEDDSA: |
3341 | 0 | key_info->u.ec.ecpointQ = malloc(len); |
3342 | 0 | if (key_info->u.ec.ecpointQ == NULL) |
3343 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ENOUGH_MEMORY); |
3344 | 0 | memcpy(key_info->u.ec.ecpointQ, part, len); |
3345 | 0 | key_info->u.ec.ecpointQ_len = len; |
3346 | 0 | break; |
3347 | 0 | default: |
3348 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL); |
3349 | 0 | } |
3350 | 0 | } |
3351 | | |
3352 | | /* go to next part to parse */ |
3353 | | /* This will be different from pgp_enumerate_blob() a bit */ |
3354 | 653 | in = part + ((tag != 0x7F49) ? len : 0); |
3355 | 653 | } |
3356 | | |
3357 | | /* calculate and store fingerprint */ |
3358 | 55 | sc_log(card->ctx, "Calculate and store fingerprint"); |
3359 | 55 | r = pgp_calculate_and_store_fingerprint(card, ctime, key_info); |
3360 | 55 | LOG_TEST_RET(card->ctx, r, "Cannot store fingerprint"); |
3361 | | /* TODO only try and update pubkey if present */ |
3362 | | |
3363 | | /* update pubkey blobs (B601, B801, A401) */ |
3364 | 0 | sc_log(card->ctx, "Update blobs holding pubkey info."); |
3365 | 0 | r = pgp_update_pubkey_blob(card, key_info); |
3366 | |
|
3367 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
3368 | 0 | } |
3369 | | |
3370 | | |
3371 | | /** |
3372 | | * Internal: update card->algorithms |
3373 | | */ |
3374 | | static int |
3375 | | pgp_update_card_algorithms(sc_card_t *card, sc_cardctl_openpgp_key_gen_store_info_t *key_info) |
3376 | 0 | { |
3377 | 0 | sc_algorithm_info_t *algo; |
3378 | 0 | u8 id = key_info->key_id; |
3379 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
3380 | |
|
3381 | 0 | LOG_FUNC_CALLED(card->ctx); |
3382 | | |
3383 | | /* protect incompatible cards against non-RSA */ |
3384 | 0 | if (key_info->algorithm != SC_OPENPGP_KEYALGO_RSA && |
3385 | 0 | priv->bcd_version < OPENPGP_CARD_3_0 && |
3386 | 0 | card->type != SC_CARD_TYPE_OPENPGP_GNUK) |
3387 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
3388 | | |
3389 | 0 | if (id > card->algorithm_count) { |
3390 | 0 | sc_log(card->ctx, |
3391 | 0 | "This key ID %u is out of the card's algorithm list.", |
3392 | 0 | (unsigned int)id); |
3393 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
3394 | 0 | } |
3395 | | |
3396 | | /* get the algorithm corresponding to the key ID */ |
3397 | 0 | algo = card->algorithms + (id - 1); |
3398 | | /* update new key attribute */ |
3399 | 0 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_RSA) { |
3400 | 0 | algo->algorithm = SC_ALGORITHM_RSA; |
3401 | 0 | algo->key_length = (unsigned int)key_info->u.rsa.modulus_len; |
3402 | 0 | } else if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH || /* includes XEDDSA */ |
3403 | 0 | key_info->algorithm == SC_OPENPGP_KEYALGO_ECDSA || |
3404 | 0 | key_info->algorithm == SC_OPENPGP_KEYALGO_EDDSA) { |
3405 | 0 | algo->algorithm = SC_ALGORITHM_EC; |
3406 | 0 | algo->key_length = (unsigned int)((key_info->u.ec.ecpointQ_len)); |
3407 | 0 | } else |
3408 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
3409 | | |
3410 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
3411 | 0 | } |
3412 | | |
3413 | | |
3414 | | /** |
3415 | | * ABI (card ctl): GENERATE ASYMMETRIC KEY PAIR |
3416 | | **/ |
3417 | | static int |
3418 | | pgp_gen_key(sc_card_t *card, sc_cardctl_openpgp_key_gen_store_info_t *key_info) |
3419 | 225 | { |
3420 | 225 | sc_apdu_t apdu; |
3421 | | /* temporary variables to hold APDU params */ |
3422 | 225 | u8 apdu_case; |
3423 | 225 | u8 apdu_data[2] = { 0x00, 0x00 }; |
3424 | 225 | size_t apdu_le; |
3425 | 225 | size_t resplen = 0; |
3426 | 225 | int r = SC_SUCCESS; |
3427 | 225 | struct pgp_priv_data *priv = DRVDATA(card); |
3428 | | |
3429 | 225 | LOG_FUNC_CALLED(card->ctx); |
3430 | | |
3431 | | /* protect incompatible cards against non-RSA */ |
3432 | 225 | if (key_info->algorithm != SC_OPENPGP_KEYALGO_RSA && |
3433 | 20 | priv->bcd_version < OPENPGP_CARD_3_0 && |
3434 | 4 | card->type != SC_CARD_TYPE_OPENPGP_GNUK) |
3435 | 225 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
3436 | | |
3437 | | /* set Control Reference Template for key */ |
3438 | 221 | if (key_info->key_id == SC_OPENPGP_KEY_SIGN) |
3439 | 0 | ushort2bebytes(apdu_data, DO_SIGN); |
3440 | 221 | else if (key_info->key_id == SC_OPENPGP_KEY_ENCR) |
3441 | 0 | ushort2bebytes(apdu_data, DO_ENCR); |
3442 | 221 | else if (key_info->key_id == SC_OPENPGP_KEY_AUTH) |
3443 | 221 | ushort2bebytes(apdu_data, DO_AUTH); |
3444 | 0 | else { |
3445 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
3446 | 0 | "Invalid key ID; must be 1, 2, or 3"); |
3447 | 0 | } |
3448 | | |
3449 | | /* set attributes for new-generated key */ |
3450 | 221 | r = pgp_update_new_algo_attr(card, key_info); |
3451 | 221 | LOG_TEST_RET(card->ctx, r, "Cannot set attributes for new-generated key"); |
3452 | | |
3453 | | /* Test whether we will need extended APDU. 1900 is an |
3454 | | * arbitrary modulus length which for sure fits into a short APDU. |
3455 | | * This idea is borrowed from GnuPG code. */ |
3456 | 121 | if (card->caps & SC_CARD_CAP_APDU_EXT |
3457 | 4 | && key_info->u.rsa.modulus_len > 1900 |
3458 | 1 | && card->type != SC_CARD_TYPE_OPENPGP_GNUK) { |
3459 | | /* We won't store to apdu variable yet, because it will be reset in |
3460 | | * sc_format_apdu() */ |
3461 | 1 | apdu_le = card->max_recv_size; |
3462 | 1 | apdu_case = SC_APDU_CASE_4_EXT; |
3463 | 1 | } |
3464 | 120 | else { |
3465 | 120 | apdu_case = SC_APDU_CASE_4_SHORT; |
3466 | 120 | apdu_le = 256; |
3467 | 120 | resplen = MAXLEN_RESP_PUBKEY; |
3468 | 120 | } |
3469 | 121 | if (card->type == SC_CARD_TYPE_OPENPGP_GNUK) { |
3470 | 0 | resplen = MAXLEN_RESP_PUBKEY_GNUK; |
3471 | 0 | } |
3472 | | |
3473 | | /* prepare APDU */ |
3474 | 121 | sc_format_apdu(card, &apdu, apdu_case, 0x47, 0x80, 0); |
3475 | 121 | apdu.data = apdu_data; |
3476 | 121 | apdu.datalen = sizeof(apdu_data); |
3477 | 121 | apdu.lc = sizeof(apdu_data); |
3478 | 121 | apdu.le = apdu_le; |
3479 | | |
3480 | | /* buffer to receive response */ |
3481 | 121 | apdu.resplen = (resplen > 0) ? resplen : apdu_le; |
3482 | 121 | apdu.resp = calloc(1, apdu.resplen); |
3483 | 121 | if (apdu.resp == NULL) { |
3484 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ENOUGH_MEMORY); |
3485 | 0 | } |
3486 | | |
3487 | | /* send */ |
3488 | 121 | sc_log(card->ctx, "Waiting for the card to generate key..."); |
3489 | 121 | r = sc_transmit_apdu(card, &apdu); |
3490 | 121 | sc_log(card->ctx, "Card has done key generation."); |
3491 | 121 | LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed"); |
3492 | | |
3493 | | /* check response */ |
3494 | 120 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
3495 | | /* instruct more in case of error */ |
3496 | 120 | if (r == SC_ERROR_SECURITY_STATUS_NOT_SATISFIED) { |
3497 | 1 | sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Please verify PIN first."); |
3498 | 1 | goto err; |
3499 | 1 | } |
3500 | 119 | LOG_TEST_GOTO_ERR(card->ctx, r, "Card returned error"); |
3501 | | |
3502 | | /* parse response data and set output */ |
3503 | 112 | r = pgp_parse_and_set_pubkey_output(card, apdu.resp, apdu.resplen, key_info); |
3504 | 112 | LOG_TEST_GOTO_ERR(card->ctx, r, "Failed to parse pubkey output"); |
3505 | 0 | r = pgp_update_card_algorithms(card, key_info); |
3506 | |
|
3507 | 121 | err: |
3508 | 121 | free(apdu.resp); |
3509 | 121 | LOG_FUNC_RETURN(card->ctx, r); |
3510 | 121 | } |
3511 | | |
3512 | | |
3513 | | /** |
3514 | | * Internal: build TLV. |
3515 | | * |
3516 | | * FIXME use `sc_asn1_put_tag` or similar instead |
3517 | | * |
3518 | | * @param[in] data The data ("value") part to build TLV. |
3519 | | * @param[in] len Data length |
3520 | | * @param[out] out The buffer of overall TLV. This buffer should be freed later. |
3521 | | * @param[out] outlen The length of buffer out. |
3522 | | **/ |
3523 | | static int |
3524 | | pgp_build_tlv(sc_context_t *ctx, unsigned int tag, u8 *data, size_t len, u8 **out, size_t *outlen) |
3525 | 0 | { |
3526 | 0 | u8 highest_order = 0; |
3527 | 0 | int r; |
3528 | |
|
3529 | 0 | r = sc_asn1_write_element(ctx, tag, data, len, out, outlen); |
3530 | 0 | LOG_TEST_RET(ctx, r, "Failed to write ASN.1 element"); |
3531 | | |
3532 | | /* Restore class bits stripped by sc_asn1_write_element */ |
3533 | | /* determine the leftmost byte of tag, which contains class bits */ |
3534 | 0 | while ((tag >> 8*highest_order) != 0) { |
3535 | 0 | highest_order++; |
3536 | 0 | } |
3537 | 0 | if (highest_order != 0) |
3538 | 0 | highest_order--; |
3539 | | |
3540 | | /* restore class bits in output */ |
3541 | 0 | if (highest_order < 4) |
3542 | 0 | *out[0] |= (tag >> 8*highest_order); |
3543 | |
|
3544 | 0 | return SC_SUCCESS; |
3545 | 0 | } |
3546 | | |
3547 | | |
3548 | | /** |
3549 | | * Internal: set Tag & Length components for TLV, store them in buffer. |
3550 | | * |
3551 | | * FIXME use `sc_asn1_put_tag` or similar instead |
3552 | | * |
3553 | | * Return the total length of Tag + Length. |
3554 | | * Note that the Value components is not counted. |
3555 | | * Ref: add_tlv() of GnuPG code. |
3556 | | **/ |
3557 | | static size_t |
3558 | | set_taglength_tlv(u8 *buffer, unsigned int tag, size_t length) |
3559 | 0 | { |
3560 | 0 | u8 *p = buffer; |
3561 | |
|
3562 | 0 | assert(tag <= 0xffff); |
3563 | 0 | if (tag > 0xff) |
3564 | 0 | *p++ = (tag >> 8) & 0xFF; |
3565 | 0 | *p++ = tag; |
3566 | 0 | if (length < 128) |
3567 | 0 | *p++ = (u8)length; |
3568 | 0 | else if (length < 256) { |
3569 | 0 | *p++ = 0x81; |
3570 | 0 | *p++ = (u8)length; |
3571 | 0 | } |
3572 | 0 | else { |
3573 | 0 | if (length > 0xffff) |
3574 | 0 | length = 0xffff; |
3575 | 0 | *p++ = 0x82; |
3576 | 0 | *p++ = (length >> 8) & 0xFF; |
3577 | 0 | *p++ = length & 0xFF; |
3578 | 0 | } |
3579 | |
|
3580 | 0 | return p - buffer; |
3581 | 0 | } |
3582 | | |
3583 | | |
3584 | | /** |
3585 | | * Internal: build Extended Header list (sec 4.3.3.9 - OpenPGP card spec v.3) |
3586 | | **/ |
3587 | | static int |
3588 | | pgp_build_extended_header_list(sc_card_t *card, sc_cardctl_openpgp_key_gen_store_info_t *key_info, |
3589 | | u8 **result, size_t *resultlen) |
3590 | 0 | { |
3591 | 0 | sc_context_t *ctx = card->ctx; |
3592 | | /* Cardholder private key template (7F48) part */ |
3593 | 0 | const size_t max_prtem_len = 7*(1 + 3); /* 7 components */ |
3594 | | /* 1 for tag name (91, 92... 97) |
3595 | | * 3 for storing length */ |
3596 | 0 | u8 pritemplate[7*(1 + 3)]; |
3597 | 0 | size_t tpl_len = 0; /* Actual size of pritemplate */ |
3598 | | /* Concatenation of key data */ |
3599 | 0 | u8 kdata[3 + 256 + 256 + 512]; /* Exponent is stored in 3 bytes |
3600 | | * With maximum 4096-bit key, |
3601 | | * p and q can be stored in 256 bytes (2048 bits). |
3602 | | * Maximum 4096-bit modulus is stored in 512 bytes */ |
3603 | 0 | size_t kdata_len = 0; /* Actual size of kdata */ |
3604 | 0 | u8 *tlvblock = NULL; |
3605 | 0 | size_t tlvlen = 0; |
3606 | 0 | u8 *tlv_5f48 = NULL; |
3607 | 0 | size_t tlvlen_5f48 = 0; |
3608 | 0 | u8 *tlv_7f48 = NULL; |
3609 | 0 | size_t tlvlen_7f48 = 0; |
3610 | 0 | u8 *data = NULL; |
3611 | 0 | size_t len = 0; |
3612 | 0 | u8 *p = NULL; |
3613 | 0 | u8 *components[4]; |
3614 | 0 | size_t componentlens[4]; |
3615 | 0 | unsigned int componenttags[4]; |
3616 | 0 | char *componentnames[4]; |
3617 | 0 | size_t comp_to_add; |
3618 | 0 | u8 i; |
3619 | 0 | int r; |
3620 | |
|
3621 | 0 | LOG_FUNC_CALLED(ctx); |
3622 | | |
3623 | | /* RSA */ |
3624 | 0 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_RSA){ |
3625 | |
|
3626 | 0 | components[0] = key_info->u.rsa.exponent; |
3627 | 0 | components[1] = key_info->u.rsa.p; |
3628 | 0 | components[2] = key_info->u.rsa.q; |
3629 | 0 | componentlens[0] = key_info->u.rsa.exponent_len; |
3630 | 0 | componentlens[1] = key_info->u.rsa.p_len; |
3631 | 0 | componentlens[2] = key_info->u.rsa.q_len; |
3632 | 0 | componenttags[0] = 0x91; |
3633 | 0 | componenttags[1] = 0x92; |
3634 | 0 | componenttags[2] = 0x93; |
3635 | 0 | componentnames[0] = "public exponent"; |
3636 | 0 | componentnames[1] = "prime p"; |
3637 | 0 | componentnames[2] = "prime q"; |
3638 | 0 | comp_to_add = 3; |
3639 | | |
3640 | | /* The maximum exponent length is 32 bit, as set on card |
3641 | | * we use this variable to check against actual exponent_len */ |
3642 | 0 | size_t max_e_len_bytes = BYTES4BITS(SC_OPENPGP_MAX_EXP_BITS); |
3643 | 0 | size_t e_len_bytes = BYTES4BITS(key_info->u.rsa.exponent_len); |
3644 | |
|
3645 | 0 | if (key_info->u.rsa.keyformat == SC_OPENPGP_KEYFORMAT_RSA_STDN |
3646 | 0 | || key_info->u.rsa.keyformat == SC_OPENPGP_KEYFORMAT_RSA_CRTN){ |
3647 | 0 | components[3] = key_info->u.rsa.modulus; |
3648 | 0 | componentlens[3] = key_info->u.rsa.modulus_len; |
3649 | 0 | componenttags[3] = 0x97; |
3650 | 0 | componentnames[3] = "modulus"; |
3651 | 0 | comp_to_add = 4; |
3652 | | |
3653 | | /* validate */ |
3654 | 0 | if (key_info->u.rsa.modulus == NULL || key_info->u.rsa.modulus_len == 0) { |
3655 | 0 | sc_log(ctx, "Error: Modulus required!"); |
3656 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS); |
3657 | 0 | } |
3658 | 0 | } |
3659 | | |
3660 | | /* Cardholder private key template's data part */ |
3661 | 0 | memset(pritemplate, 0, max_prtem_len); |
3662 | | |
3663 | | /* maximum 32 bit exponent length allowed on OpenPGP Card */ |
3664 | 0 | assert(key_info->u.rsa.exponent_len <= SC_OPENPGP_MAX_EXP_BITS); |
3665 | | |
3666 | | /* We need to right justify the exponent with allowed exponent length, |
3667 | | * e.g. from '01 00 01' to '00 01 00 01' */ |
3668 | 0 | if (key_info->u.rsa.exponent_len < SC_OPENPGP_MAX_EXP_BITS) { |
3669 | | /* create new buffer */ |
3670 | 0 | p = calloc(1, max_e_len_bytes); |
3671 | 0 | if (!p) |
3672 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_ENOUGH_MEMORY); |
3673 | | |
3674 | 0 | memcpy(p + (max_e_len_bytes - e_len_bytes), key_info->u.rsa.exponent, e_len_bytes); |
3675 | | /* set key_info->u.rsa.e to new buffer */ |
3676 | 0 | free(key_info->u.rsa.exponent); |
3677 | 0 | key_info->u.rsa.exponent = p; |
3678 | 0 | components[0] = p; |
3679 | 0 | key_info->u.rsa.exponent_len = SC_OPENPGP_MAX_EXP_BITS; /* we store info in bits */ |
3680 | 0 | componentlens[0] = max_e_len_bytes; /* ... but in bytes for header list */ |
3681 | 0 | } |
3682 | 0 | } |
3683 | | /* ECC */ |
3684 | 0 | else if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH || key_info->algorithm == SC_OPENPGP_KEYALGO_ECDSA || key_info->algorithm == SC_OPENPGP_KEYALGO_EDDSA) { |
3685 | 0 | components[0] = key_info->u.ec.privateD; |
3686 | 0 | componentlens[0] = key_info->u.ec.privateD_len; |
3687 | 0 | componenttags[0] = 0x92; |
3688 | 0 | componentnames[0] = "private key"; |
3689 | 0 | comp_to_add = 1; |
3690 | | |
3691 | | /* import public key as well */ |
3692 | 0 | if (key_info->u.ec.keyformat == SC_OPENPGP_KEYFORMAT_EC_STDPUB) { |
3693 | 0 | components[1] = key_info->u.ec.ecpointQ; |
3694 | 0 | componentlens[1] = key_info->u.ec.ecpointQ_len; |
3695 | 0 | componenttags[1] = 0x99; |
3696 | 0 | componentnames[1] = "public key"; |
3697 | 0 | comp_to_add = 2; |
3698 | | |
3699 | | /* validate */ |
3700 | 0 | if ((key_info->u.ec.ecpointQ == NULL || key_info->u.ec.ecpointQ_len == 0)) { |
3701 | 0 | sc_log(ctx, "Error: ecpointQ required!"); |
3702 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS); |
3703 | 0 | } |
3704 | 0 | } |
3705 | | |
3706 | | /* Cardholder private key template's data part */ |
3707 | 0 | memset(pritemplate, 0, max_prtem_len); |
3708 | 0 | } else |
3709 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
3710 | | |
3711 | | /* start from beginning of pritemplate */ |
3712 | 0 | p = pritemplate; |
3713 | |
|
3714 | 0 | for (i = 0; i < comp_to_add; i++) { |
3715 | 0 | sc_log(ctx, "Set Tag+Length for %s (%X).", componentnames[i], componenttags[i]); |
3716 | 0 | len = set_taglength_tlv(p, componenttags[i], componentlens[i]); |
3717 | 0 | tpl_len += len; |
3718 | | |
3719 | | /* |
3720 | | * <-- kdata_len --><-- Copy here --> |
3721 | | * kdata |===============|___________________ |
3722 | | */ |
3723 | 0 | memcpy(kdata + kdata_len, components[i], componentlens[i]); |
3724 | 0 | kdata_len += componentlens[i]; |
3725 | | |
3726 | | /* Move p to next part and build */ |
3727 | 0 | p += len; |
3728 | 0 | } |
3729 | | |
3730 | | /* TODO: Components for CRT format */ |
3731 | | |
3732 | | /* TLV block for 7F48 */ |
3733 | 0 | r = pgp_build_tlv(ctx, 0x7F48, pritemplate, tpl_len, &tlv_7f48, &tlvlen_7f48); |
3734 | 0 | LOG_TEST_RET(ctx, r, "Failed to build TLV for 7F48"); |
3735 | 0 | tlv_7f48[0] |= 0x7F; |
3736 | 0 | r = pgp_build_tlv(ctx, 0x5f48, kdata, kdata_len, &tlv_5f48, &tlvlen_5f48); |
3737 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Failed to build TLV for 5F48"); |
3738 | | |
3739 | | /* data part's length for Extended Header list */ |
3740 | 0 | len = 2 + tlvlen_7f48 + tlvlen_5f48; |
3741 | | /* set data part content */ |
3742 | 0 | data = calloc(1, len); |
3743 | 0 | if (data == NULL) |
3744 | 0 | LOG_TEST_GOTO_ERR(ctx, SC_ERROR_NOT_ENOUGH_MEMORY, "Not enough memory"); |
3745 | | |
3746 | 0 | switch (key_info->key_id) { |
3747 | 0 | case SC_OPENPGP_KEY_SIGN: |
3748 | 0 | ushort2bebytes(data, DO_SIGN); |
3749 | 0 | break; |
3750 | 0 | case SC_OPENPGP_KEY_ENCR: |
3751 | 0 | ushort2bebytes(data, DO_ENCR); |
3752 | 0 | break; |
3753 | 0 | case SC_OPENPGP_KEY_AUTH: |
3754 | 0 | ushort2bebytes(data, DO_AUTH); |
3755 | 0 | break; |
3756 | 0 | default: |
3757 | 0 | LOG_TEST_GOTO_ERR(ctx, SC_ERROR_INVALID_ARGUMENTS, |
3758 | 0 | "Invalid key ID; must be 1, 2, or 3"); |
3759 | 0 | } |
3760 | 0 | memcpy(data + 2, tlv_7f48, tlvlen_7f48); |
3761 | 0 | memcpy(data + 2 + tlvlen_7f48, tlv_5f48, tlvlen_5f48); |
3762 | 0 | r = pgp_build_tlv(ctx, 0x4D, data, len, &tlvblock, &tlvlen); |
3763 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Cannot build TLV for Extended Header list"); |
3764 | | |
3765 | | /* set output */ |
3766 | 0 | if (result != NULL) { |
3767 | 0 | *result = tlvblock; |
3768 | 0 | *resultlen = tlvlen; |
3769 | 0 | } else { |
3770 | 0 | free(tlvblock); |
3771 | 0 | } |
3772 | |
|
3773 | 0 | err: |
3774 | 0 | free(data); |
3775 | 0 | free(tlv_5f48); |
3776 | 0 | free(tlv_7f48); |
3777 | 0 | LOG_FUNC_RETURN(ctx, r); |
3778 | 0 | } |
3779 | | |
3780 | | |
3781 | | /** |
3782 | | * ABI (card ctl): store key |
3783 | | **/ |
3784 | | static int |
3785 | | pgp_store_key(sc_card_t *card, sc_cardctl_openpgp_key_gen_store_info_t *key_info) |
3786 | 0 | { |
3787 | 0 | u8 *data = NULL; |
3788 | 0 | size_t len = 0; |
3789 | 0 | int r = 0; |
3790 | 0 | int has_pubkey = 0, has_privkey = 0; |
3791 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
3792 | |
|
3793 | 0 | LOG_FUNC_CALLED(card->ctx); |
3794 | | |
3795 | | /* PKCS11 loads privkey separately from pubkey as two different operations |
3796 | | * So this routine will be called twice to create two different objects. |
3797 | | * pkcs15init only calls once, with both. |
3798 | | * OpenPGP 4.3.1 says modulus and ecpointQ are optional when |
3799 | | * creating the extended header. |
3800 | | * So we can tell the difference and only do appropriate parts of this |
3801 | | * routine. |
3802 | | */ |
3803 | | |
3804 | | /* protect incompatible cards against non-RSA */ |
3805 | 0 | if (key_info->algorithm != SC_OPENPGP_KEYALGO_RSA && |
3806 | 0 | priv->bcd_version < OPENPGP_CARD_3_0 && |
3807 | 0 | card->type != SC_CARD_TYPE_OPENPGP_GNUK) |
3808 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
3809 | | |
3810 | | /* Validate */ |
3811 | 0 | if (key_info->key_id < 1 || key_info->key_id > 3) |
3812 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, |
3813 | 0 | "Invalid key ID; must be 1, 2, or 3"); |
3814 | | |
3815 | | /* set algorithm attributes */ |
3816 | | /* RSA */ |
3817 | 0 | if (key_info->algorithm == SC_OPENPGP_KEYALGO_RSA){ |
3818 | 0 | has_pubkey = (key_info->u.rsa.modulus && key_info->u.rsa.modulus_len && |
3819 | 0 | key_info->u.rsa.exponent && key_info->u.rsa.exponent_len); |
3820 | 0 | has_privkey = (has_pubkey && |
3821 | 0 | key_info->u.rsa.p && key_info->u.rsa.p_len && |
3822 | 0 | key_info->u.rsa.q && key_info->u.rsa.q_len); |
3823 | |
|
3824 | 0 | if (!has_pubkey && !has_privkey) |
3825 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
3826 | | |
3827 | | /* we just support standard key format */ |
3828 | 0 | if (has_privkey) { |
3829 | 0 | switch (key_info->u.rsa.keyformat) { |
3830 | 0 | case SC_OPENPGP_KEYFORMAT_RSA_STD: |
3831 | 0 | case SC_OPENPGP_KEYFORMAT_RSA_STDN: |
3832 | 0 | break; |
3833 | | |
3834 | 0 | case SC_OPENPGP_KEYFORMAT_RSA_CRT: |
3835 | 0 | case SC_OPENPGP_KEYFORMAT_RSA_CRTN: |
3836 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
3837 | | |
3838 | 0 | default: |
3839 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
3840 | 0 | } |
3841 | 0 | } |
3842 | | |
3843 | | /* we only support exponent of maximum 32 bits */ |
3844 | 0 | if (key_info->u.rsa.exponent_len > SC_OPENPGP_MAX_EXP_BITS) { |
3845 | 0 | sc_log(card->ctx, |
3846 | 0 | "Exponent %" SC_FORMAT_LEN_SIZE_T "u-bit (>32) is not supported.", |
3847 | 0 | key_info->u.rsa.exponent_len); |
3848 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
3849 | 0 | } |
3850 | 0 | } |
3851 | | /* ECC */ |
3852 | 0 | else if (key_info->algorithm == SC_OPENPGP_KEYALGO_ECDSA || |
3853 | 0 | key_info->algorithm == SC_OPENPGP_KEYALGO_ECDH || /* includes XEDDSA */ |
3854 | 0 | key_info->algorithm == SC_OPENPGP_KEYALGO_EDDSA) { |
3855 | 0 | has_pubkey = (key_info->u.ec.ecpointQ && key_info->u.ec.ecpointQ_len); |
3856 | 0 | has_privkey = (key_info->u.ec.privateD && key_info->u.ec.privateD_len); |
3857 | |
|
3858 | 0 | if (!has_pubkey && !has_privkey) |
3859 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
3860 | | |
3861 | 0 | r = pgp_update_new_algo_attr(card, key_info); |
3862 | |
|
3863 | 0 | LOG_TEST_RET(card->ctx, r, "Failed to update new algorithm attributes"); |
3864 | 0 | } |
3865 | | |
3866 | 0 | if (has_privkey) { |
3867 | | /* build Extended Header list */ |
3868 | 0 | r = pgp_build_extended_header_list(card, key_info, &data, &len); |
3869 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "Failed to build Extended Header list"); |
3870 | | |
3871 | | /* write to DO */ |
3872 | 0 | r = pgp_put_data(card, 0x4D, data, len); |
3873 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "Failed to write to DO 004D"); |
3874 | | |
3875 | | /* store creation time */ |
3876 | 0 | r = pgp_store_creationtime(card, key_info->key_id, &key_info->creationtime); |
3877 | 0 | LOG_TEST_RET(card->ctx, r, "Cannot store creation time"); |
3878 | 0 | } |
3879 | | |
3880 | 0 | if (has_pubkey) { |
3881 | | /* calculate and store fingerprint */ |
3882 | 0 | sc_log(card->ctx, "Calculate and store fingerprint"); |
3883 | 0 | r = pgp_calculate_and_store_fingerprint(card, key_info->creationtime, key_info); |
3884 | 0 | LOG_TEST_RET(card->ctx, r, "Cannot store fingerprint"); |
3885 | | |
3886 | | /* update pubkey blobs (B601,B801, A401) */ |
3887 | 0 | sc_log(card->ctx, "Update blobs holding pubkey info."); |
3888 | 0 | r = pgp_update_pubkey_blob(card, key_info); |
3889 | |
|
3890 | 0 | sc_log(card->ctx, "Update card algorithms"); |
3891 | 0 | pgp_update_card_algorithms(card, key_info); |
3892 | 0 | } |
3893 | | |
3894 | 0 | err: |
3895 | 0 | free(data); |
3896 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
3897 | 0 | } |
3898 | | |
3899 | | #endif /* ENABLE_OPENSSL */ |
3900 | | |
3901 | | |
3902 | | /** |
3903 | | * ABI (card ctl): erase card |
3904 | | **/ |
3905 | | static int |
3906 | | pgp_erase_card(sc_card_t *card) |
3907 | 0 | { |
3908 | | /* Special series of commands to erase OpenPGP card, |
3909 | | * according to https://www.crypto-stick.com/en/faq |
3910 | | * (How to reset a Crypto Stick? question). |
3911 | | * Gnuk is known not to support this feature. */ |
3912 | 0 | const char *apdu_hex[] = { |
3913 | | /* block PIN1 */ |
3914 | 0 | "00:20:00:81:08:40:40:40:40:40:40:40:40", |
3915 | 0 | "00:20:00:81:08:40:40:40:40:40:40:40:40", |
3916 | 0 | "00:20:00:81:08:40:40:40:40:40:40:40:40", |
3917 | 0 | "00:20:00:81:08:40:40:40:40:40:40:40:40", |
3918 | | /* block PIN3 */ |
3919 | 0 | "00:20:00:83:08:40:40:40:40:40:40:40:40", |
3920 | 0 | "00:20:00:83:08:40:40:40:40:40:40:40:40", |
3921 | 0 | "00:20:00:83:08:40:40:40:40:40:40:40:40", |
3922 | 0 | "00:20:00:83:08:40:40:40:40:40:40:40:40", |
3923 | | /* TERMINATE */ |
3924 | 0 | "00:e6:00:00", |
3925 | 0 | NULL |
3926 | 0 | }; |
3927 | 0 | sc_apdu_t apdu; |
3928 | 0 | int i; |
3929 | 0 | int r = SC_SUCCESS; |
3930 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
3931 | |
|
3932 | 0 | LOG_FUNC_CALLED(card->ctx); |
3933 | |
|
3934 | 0 | if ((priv->ext_caps & EXT_CAP_LCS) == 0) { |
3935 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_NO_CARD_SUPPORT, |
3936 | 0 | "Card does not offer life cycle management"); |
3937 | 0 | } |
3938 | | |
3939 | 0 | switch (priv->state) { |
3940 | 0 | case CARD_STATE_ACTIVATED: |
3941 | | /* iterate over the commands above */ |
3942 | 0 | for (i = 0; apdu_hex[i] != NULL; i++) { |
3943 | 0 | u8 apdu_bin[25]; /* large enough to convert apdu_hex */ |
3944 | 0 | size_t apdu_bin_len = sizeof(apdu_bin); |
3945 | 0 | u8 rbuf[SC_MAX_APDU_BUFFER_SIZE]; |
3946 | | |
3947 | | /* convert hex array to bin array */ |
3948 | 0 | r = sc_hex_to_bin(apdu_hex[i], apdu_bin, &apdu_bin_len); |
3949 | 0 | LOG_TEST_RET(card->ctx, r, "Failed to convert APDU bytes"); |
3950 | | |
3951 | | /* build APDU from binary array */ |
3952 | 0 | r = sc_bytes2apdu(card->ctx, apdu_bin, apdu_bin_len, &apdu); |
3953 | 0 | if (r) |
3954 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INTERNAL, |
3955 | 0 | "Failed to build APDU"); |
3956 | | |
3957 | 0 | apdu.resp = rbuf; |
3958 | 0 | apdu.resplen = sizeof(rbuf); |
3959 | | |
3960 | | /* send APDU to card */ |
3961 | 0 | sc_log(card->ctx, "Sending APDU%d %s", i, apdu_hex[i]); |
3962 | 0 | r = sc_transmit_apdu(card, &apdu); |
3963 | 0 | LOG_TEST_RET(card->ctx, r, "Transmitting APDU failed"); |
3964 | 0 | } |
3965 | | /* fall through */ |
3966 | 0 | case CARD_STATE_INITIALIZATION: |
3967 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x44, 0, 0); |
3968 | 0 | r = sc_transmit_apdu(card, &apdu); |
3969 | 0 | LOG_TEST_RET(card->ctx, r, "Transmitting APDU failed"); |
3970 | 0 | break; |
3971 | 0 | default: |
3972 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_NO_CARD_SUPPORT, |
3973 | 0 | "Card does not offer life cycle management"); |
3974 | 0 | } |
3975 | | |
3976 | 0 | if (r == SC_SUCCESS && priv->mf) { |
3977 | 0 | pgp_blob_t *new_mf = pgp_new_blob(card, NULL, priv->mf->id, priv->mf->file); |
3978 | 0 | if (new_mf == NULL) { |
3979 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INTERNAL, "Failed to allocate the new MF blob"); |
3980 | 0 | } |
3981 | 0 | priv->mf->file = NULL; |
3982 | |
|
3983 | 0 | pgp_free_blobs(priv->mf); |
3984 | 0 | priv->mf = new_mf; |
3985 | 0 | populate_blobs_to_mf(card, priv); |
3986 | 0 | } |
3987 | | |
3988 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
3989 | 0 | } |
3990 | | |
3991 | | |
3992 | | /** |
3993 | | * ABI: ISO 7816-9 CARD CTL - perform special card-specific operations. |
3994 | | */ |
3995 | | static int |
3996 | | pgp_card_ctl(sc_card_t *card, unsigned long cmd, void *ptr) |
3997 | 1.43k | { |
3998 | 1.43k | int r; |
3999 | | |
4000 | 1.43k | LOG_FUNC_CALLED(card->ctx); |
4001 | | |
4002 | 1.43k | switch(cmd) { |
4003 | 293 | case SC_CARDCTL_GET_SERIALNR: |
4004 | 293 | memmove((sc_serial_number_t *) ptr, &card->serialnr, sizeof(card->serialnr)); |
4005 | 293 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
4006 | 0 | break; |
4007 | 0 | case SC_CARDCTL_OPENPGP_SELECT_DATA: |
4008 | 0 | r = pgp_select_data(card, *((u8 *) ptr)); |
4009 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
4010 | 0 | break; |
4011 | 0 | #ifdef ENABLE_OPENSSL |
4012 | 225 | case SC_CARDCTL_OPENPGP_GENERATE_KEY: |
4013 | 225 | r = pgp_gen_key(card, (sc_cardctl_openpgp_key_gen_store_info_t *)ptr); |
4014 | 225 | LOG_FUNC_RETURN(card->ctx, r); |
4015 | 0 | break; |
4016 | 0 | case SC_CARDCTL_OPENPGP_STORE_KEY: |
4017 | 0 | r = pgp_store_key(card, (sc_cardctl_openpgp_key_gen_store_info_t *)ptr); |
4018 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
4019 | 0 | break; |
4020 | 0 | #endif /* ENABLE_OPENSSL */ |
4021 | 0 | case SC_CARDCTL_ERASE_CARD: |
4022 | 0 | r = pgp_erase_card(card); |
4023 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
4024 | 0 | break; |
4025 | 1.43k | } |
4026 | | |
4027 | 918 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
4028 | 918 | } |
4029 | | |
4030 | | |
4031 | | /** |
4032 | | * Internal: delete key (GnuK only). |
4033 | | */ |
4034 | | static int |
4035 | | gnuk_delete_key(sc_card_t *card, u8 key_id) |
4036 | 0 | { |
4037 | 0 | sc_context_t *ctx = card->ctx; |
4038 | 0 | int r = SC_SUCCESS; |
4039 | 0 | u8 data[4] = { 0x4D, 0x02, 0x00, 0x00 }; |
4040 | |
|
4041 | 0 | LOG_FUNC_CALLED(ctx); |
4042 | |
|
4043 | 0 | if (key_id < 1 || key_id > 3) |
4044 | 0 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, |
4045 | 0 | "Invalid key ID; must be 1, 2, or 3"); |
4046 | | |
4047 | | /* delete fingerprint */ |
4048 | 0 | sc_log(ctx, "Delete fingerprints"); |
4049 | 0 | r = pgp_put_data(card, 0xC6 + key_id, NULL, 0); |
4050 | 0 | LOG_TEST_RET(ctx, r, "Failed to delete fingerprints"); |
4051 | | /* delete creation time */ |
4052 | 0 | sc_log(ctx, "Delete creation time"); |
4053 | 0 | r = pgp_put_data(card, 0xCD + key_id, NULL, 0); |
4054 | 0 | LOG_TEST_RET(ctx, r, "Failed to delete creation time"); |
4055 | | |
4056 | | /* rewrite Extended Header List */ |
4057 | 0 | sc_log(ctx, "Rewrite Extended Header List"); |
4058 | |
|
4059 | 0 | if (key_id == SC_OPENPGP_KEY_SIGN) |
4060 | 0 | ushort2bebytes(data+2, DO_SIGN); |
4061 | 0 | else if (key_id == SC_OPENPGP_KEY_ENCR) |
4062 | 0 | ushort2bebytes(data+2, DO_ENCR); |
4063 | 0 | else if (key_id == SC_OPENPGP_KEY_AUTH) |
4064 | 0 | ushort2bebytes(data+2, DO_AUTH); |
4065 | |
|
4066 | 0 | r = pgp_put_data(card, 0x4D, data, sizeof(data)); |
4067 | |
|
4068 | 0 | LOG_FUNC_RETURN(ctx, r); |
4069 | 0 | } |
4070 | | |
4071 | | |
4072 | | /** |
4073 | | * ABI: ISO 7816-9 DELETE FILE - delete EF or DF given. |
4074 | | */ |
4075 | | static int |
4076 | | pgp_delete_file(sc_card_t *card, const sc_path_t *path) |
4077 | 0 | { |
4078 | 0 | struct pgp_priv_data *priv = DRVDATA(card); |
4079 | 0 | pgp_blob_t *blob; |
4080 | 0 | sc_file_t *file; |
4081 | 0 | int r; |
4082 | |
|
4083 | 0 | LOG_FUNC_CALLED(card->ctx); |
4084 | | |
4085 | | /* sc_pkcs15init_delete_by_path() sets the path type to SC_PATH_TYPE_FILE_ID */ |
4086 | 0 | r = pgp_select_file(card, path, &file); |
4087 | 0 | LOG_TEST_RET(card->ctx, r, "Cannot select file"); |
4088 | | |
4089 | | /* save "current" blob */ |
4090 | 0 | blob = priv->current; |
4091 | | |
4092 | | /* don't try to delete MF */ |
4093 | 0 | if (blob == priv->mf) { |
4094 | 0 | sc_file_free(file); |
4095 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED); |
4096 | 0 | } |
4097 | | |
4098 | 0 | if (card->type != SC_CARD_TYPE_OPENPGP_GNUK && |
4099 | 0 | (file->id == DO_SIGN_SYM || file->id == DO_ENCR_SYM || file->id == DO_AUTH_SYM)) { |
4100 | | /* These tags are just symbolic. We don't really delete them. */ |
4101 | 0 | r = SC_SUCCESS; |
4102 | 0 | } |
4103 | 0 | else if (card->type == SC_CARD_TYPE_OPENPGP_GNUK && file->id == DO_SIGN_SYM) { |
4104 | 0 | r = gnuk_delete_key(card, 1); |
4105 | 0 | } |
4106 | 0 | else if (card->type == SC_CARD_TYPE_OPENPGP_GNUK && file->id == DO_ENCR_SYM) { |
4107 | 0 | r = gnuk_delete_key(card, 2); |
4108 | 0 | } |
4109 | 0 | else if (card->type == SC_CARD_TYPE_OPENPGP_GNUK && file->id == DO_AUTH_SYM) { |
4110 | 0 | r = gnuk_delete_key(card, 3); |
4111 | 0 | } |
4112 | 0 | else { |
4113 | | /* call pgp_put_data() with zero-sized NULL-buffer to zap the DO contents */ |
4114 | 0 | r = pgp_put_data(card, file->id, NULL, 0); |
4115 | 0 | } |
4116 | 0 | sc_file_free(file); |
4117 | | |
4118 | | /* set "current" blob to parent */ |
4119 | 0 | priv->current = blob->parent; |
4120 | |
|
4121 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
4122 | 0 | } |
4123 | | |
4124 | | |
4125 | | /** |
4126 | | * ABI: ISO 7816-4 UPDATE BINARY - update data in current EF. |
4127 | | */ |
4128 | | static int |
4129 | | pgp_update_binary(sc_card_t *card, unsigned int idx, |
4130 | | const u8 *buf, size_t count, unsigned long flags) |
4131 | 56 | { |
4132 | 56 | struct pgp_priv_data *priv = DRVDATA(card); |
4133 | 56 | pgp_blob_t *blob = priv->current; |
4134 | 56 | int r = SC_SUCCESS; |
4135 | | |
4136 | 56 | LOG_FUNC_CALLED(card->ctx); |
4137 | | |
4138 | | /* We will use PUT DATA to write to DO. |
4139 | | * As PUT DATA does not support idx, we don't either */ |
4140 | 56 | if (idx > 0) |
4141 | 56 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCORRECT_PARAMETERS); |
4142 | | |
4143 | | /* When a dummy file, e.g "11001101", is selected, the current blob |
4144 | | * is set to NULL. We don't really put data to dummy file. */ |
4145 | 34 | if (blob != NULL) { |
4146 | 34 | r = pgp_put_data(card, blob->id, buf, count); |
4147 | 34 | } |
4148 | | |
4149 | 34 | LOG_FUNC_RETURN(card->ctx, r); |
4150 | 34 | } |
4151 | | |
4152 | | |
4153 | | /** |
4154 | | * ABI: card reader lock obtained - re-select card applet if necessary. |
4155 | | */ |
4156 | | static int pgp_card_reader_lock_obtained(sc_card_t *card, int was_reset) |
4157 | 12.6k | { |
4158 | 12.6k | struct pgp_priv_data *priv = DRVDATA(card); /* may be null during initialization */ |
4159 | 12.6k | int r = SC_SUCCESS; |
4160 | | |
4161 | 12.6k | SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); |
4162 | | |
4163 | 12.6k | if (card->flags & SC_CARD_FLAG_KEEP_ALIVE |
4164 | 0 | && was_reset <= 0 |
4165 | 0 | && priv != NULL && priv->mf && priv->mf->file) { |
4166 | | /* check whether applet is still selected */ |
4167 | 0 | unsigned char aid[16]; |
4168 | |
|
4169 | 0 | r = sc_get_data(card, 0x004F, aid, sizeof aid); |
4170 | 0 | if ((size_t) r != priv->mf->file->namelen |
4171 | 0 | || 0 != memcmp(aid, priv->mf->file->name, r)) { |
4172 | | /* reselect is required */ |
4173 | 0 | was_reset = 1; |
4174 | 0 | } |
4175 | 0 | r = SC_SUCCESS; |
4176 | 0 | } |
4177 | | |
4178 | 12.6k | if (was_reset > 0) { |
4179 | 0 | sc_file_t *file = NULL; |
4180 | 0 | sc_path_t path; |
4181 | | /* select application "OpenPGP" */ |
4182 | 0 | sc_format_path("D276:0001:2401", &path); |
4183 | 0 | path.type = SC_PATH_TYPE_DF_NAME; |
4184 | 0 | r = iso_ops->select_file(card, &path, &file); |
4185 | 0 | sc_file_free(file); |
4186 | 0 | } |
4187 | | |
4188 | 12.6k | LOG_FUNC_RETURN(card->ctx, r); |
4189 | 12.6k | } |
4190 | | |
4191 | | |
4192 | | /** |
4193 | | * API: integrate OpenPGP driver into OpenSC's driver list. |
4194 | | */ |
4195 | | struct sc_card_driver * |
4196 | | sc_get_openpgp_driver(void) |
4197 | 19.3k | { |
4198 | 19.3k | struct sc_card_driver *iso_drv = sc_get_iso7816_driver(); |
4199 | | |
4200 | 19.3k | iso_ops = iso_drv->ops; |
4201 | | |
4202 | 19.3k | pgp_ops = *iso_ops; |
4203 | 19.3k | pgp_ops.match_card = pgp_match_card; |
4204 | 19.3k | pgp_ops.init = pgp_init; |
4205 | 19.3k | pgp_ops.finish = pgp_finish; |
4206 | 19.3k | pgp_ops.select_file = pgp_select_file; |
4207 | 19.3k | pgp_ops.list_files = pgp_list_files; |
4208 | 19.3k | pgp_ops.get_challenge = pgp_get_challenge; |
4209 | 19.3k | pgp_ops.read_binary = pgp_read_binary; |
4210 | 19.3k | pgp_ops.write_binary = NULL; |
4211 | 19.3k | pgp_ops.pin_cmd = pgp_pin_cmd; |
4212 | 19.3k | pgp_ops.logout = pgp_logout; |
4213 | 19.3k | pgp_ops.get_data = pgp_get_data; |
4214 | 19.3k | pgp_ops.put_data = pgp_put_data; |
4215 | 19.3k | pgp_ops.set_security_env= pgp_set_security_env; |
4216 | 19.3k | pgp_ops.compute_signature= pgp_compute_signature; |
4217 | 19.3k | pgp_ops.decipher = pgp_decipher; |
4218 | 19.3k | pgp_ops.card_ctl = pgp_card_ctl; |
4219 | 19.3k | pgp_ops.delete_file = pgp_delete_file; |
4220 | 19.3k | pgp_ops.update_binary = pgp_update_binary; |
4221 | 19.3k | pgp_ops.card_reader_lock_obtained = pgp_card_reader_lock_obtained; |
4222 | | |
4223 | 19.3k | return &pgp_drv; |
4224 | 19.3k | } |