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