/src/opensc/src/libopensc/pkcs15-algo.c
Line | Count | Source |
1 | | /* |
2 | | * pkc15-algo.c: ASN.1 handling for algorithm IDs and parameters |
3 | | * |
4 | | * Copyright (C) 2001, 2002 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 | | #ifdef HAVE_CONFIG_H |
22 | | #include "config.h" |
23 | | #endif |
24 | | |
25 | | #include <stdio.h> |
26 | | #include <string.h> |
27 | | #include <ctype.h> |
28 | | #include <stdlib.h> |
29 | | |
30 | | #include "internal.h" |
31 | | #include "asn1.h" |
32 | | |
33 | | /* |
34 | | * AlgorithmIdentifier handling |
35 | | */ |
36 | | static struct sc_asn1_entry c_asn1_des_iv[] = { |
37 | | { "iv", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL }, |
38 | | { NULL, 0, 0, 0, NULL, NULL } |
39 | | }; |
40 | | |
41 | | static int |
42 | | asn1_decode_des_params(sc_context_t *ctx, void **paramp, |
43 | | const u8 *buf, size_t buflen, int depth) |
44 | 0 | { |
45 | 0 | struct sc_asn1_entry asn1_des_iv[2]; |
46 | 0 | u8 iv[8]; |
47 | 0 | size_t ivlen = 8; |
48 | 0 | int r; |
49 | |
|
50 | 0 | sc_copy_asn1_entry(c_asn1_des_iv, asn1_des_iv); |
51 | 0 | sc_format_asn1_entry(asn1_des_iv + 0, iv, &ivlen, 0); |
52 | 0 | r = _sc_asn1_decode(ctx, asn1_des_iv, buf, buflen, NULL, NULL, 0, depth + 1); |
53 | 0 | if (r < 0) |
54 | 0 | return r; |
55 | 0 | if (ivlen != 8) |
56 | 0 | return SC_ERROR_INVALID_ASN1_OBJECT; |
57 | 0 | *paramp = malloc(8); |
58 | 0 | if (!*paramp) |
59 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
60 | 0 | memcpy(*paramp, iv, 8); |
61 | 0 | return 0; |
62 | 0 | } |
63 | | |
64 | | static int |
65 | | asn1_encode_des_params(sc_context_t *ctx, void *params, |
66 | | u8 **buf, size_t *buflen, int depth) |
67 | 0 | { |
68 | 0 | struct sc_asn1_entry asn1_des_iv[2]; |
69 | 0 | int ivlen = 8; |
70 | |
|
71 | 0 | sc_copy_asn1_entry(c_asn1_des_iv, asn1_des_iv); |
72 | 0 | sc_format_asn1_entry(asn1_des_iv + 0, params, &ivlen, 1); |
73 | 0 | return _sc_asn1_encode(ctx, asn1_des_iv, buf, buflen, depth + 1); |
74 | 0 | } |
75 | | |
76 | | static const struct sc_asn1_entry c_asn1_gostr3410_params0[] = { |
77 | | { "GOSTR3410Params", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
78 | | { NULL, 0, 0, 0, NULL, NULL } |
79 | | }; |
80 | | |
81 | | static const struct sc_asn1_entry c_asn1_gostr3410_params[] = { |
82 | | { "key_params", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, 0, NULL, NULL }, |
83 | | { "hash_params", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, 0, NULL, NULL }, |
84 | | { "cipher_params", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, SC_ASN1_OPTIONAL, NULL, NULL }, |
85 | | { NULL, 0, 0, 0, NULL, NULL } |
86 | | }; |
87 | | |
88 | | static int |
89 | | asn1_decode_gostr3410_params(sc_context_t *ctx, void **paramp, |
90 | | const u8 *buf, size_t buflen, int depth) |
91 | 0 | { |
92 | 0 | struct sc_asn1_entry asn1_gostr3410_params0[2], asn1_gostr3410_params[4]; |
93 | 0 | struct sc_object_id keyp, hashp, cipherp; |
94 | 0 | int r; |
95 | |
|
96 | 0 | sc_copy_asn1_entry(c_asn1_gostr3410_params0, asn1_gostr3410_params0); |
97 | 0 | sc_copy_asn1_entry(c_asn1_gostr3410_params, asn1_gostr3410_params); |
98 | |
|
99 | 0 | sc_format_asn1_entry(asn1_gostr3410_params0 + 0, asn1_gostr3410_params, NULL, 0); |
100 | 0 | sc_format_asn1_entry(asn1_gostr3410_params + 0, &keyp, NULL, 0); |
101 | 0 | sc_format_asn1_entry(asn1_gostr3410_params + 1, &hashp, NULL, 0); |
102 | 0 | sc_format_asn1_entry(asn1_gostr3410_params + 2, &cipherp, NULL, 0); |
103 | |
|
104 | 0 | r = _sc_asn1_decode(ctx, asn1_gostr3410_params0, buf, buflen, NULL, NULL, 0, depth + 1); |
105 | | /* TODO: store in paramp */ |
106 | 0 | (void)paramp; /* no warning */ |
107 | 0 | return r; |
108 | 0 | } |
109 | | |
110 | | static int |
111 | | asn1_encode_gostr3410_params(sc_context_t *ctx, void *params, |
112 | | u8 **buf, size_t *buflen, int depth) |
113 | 0 | { |
114 | 0 | struct sc_asn1_entry asn1_gostr3410_params0[2], asn1_gostr3410_params[4]; |
115 | 0 | struct sc_pkcs15_gost_parameters *gost_params = (struct sc_pkcs15_gost_parameters *)params; |
116 | 0 | int r; |
117 | |
|
118 | 0 | sc_copy_asn1_entry(c_asn1_gostr3410_params0, asn1_gostr3410_params0); |
119 | 0 | sc_copy_asn1_entry(c_asn1_gostr3410_params, asn1_gostr3410_params); |
120 | |
|
121 | 0 | sc_format_asn1_entry(asn1_gostr3410_params0 + 0, asn1_gostr3410_params, NULL, 1); |
122 | 0 | sc_format_asn1_entry(asn1_gostr3410_params + 0, &gost_params->key, NULL, 1); |
123 | 0 | sc_format_asn1_entry(asn1_gostr3410_params + 1, &gost_params->hash, NULL, 1); |
124 | | /* sc_format_asn1_entry(asn1_gostr3410_params + 2, &cipherp, NULL, 1); */ |
125 | |
|
126 | 0 | r = _sc_asn1_encode(ctx, asn1_gostr3410_params0, buf, buflen, depth + 1); |
127 | |
|
128 | 0 | sc_log(ctx, "encoded-params: %s", sc_dump_hex(*buf, *buflen)); |
129 | 0 | return r; |
130 | 0 | } |
131 | | |
132 | | static const struct sc_asn1_entry c_asn1_pbkdf2_params[] = { |
133 | | { "salt", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL }, |
134 | | { "count", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL }, |
135 | | { "keyLength", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, SC_ASN1_OPTIONAL, NULL, NULL }, |
136 | | { "prf", SC_ASN1_ALGORITHM_ID, SC_ASN1_TAG_SEQUENCE, SC_ASN1_OPTIONAL, NULL, NULL }, |
137 | | { NULL, 0, 0, 0, NULL, NULL } |
138 | | }; |
139 | | |
140 | | static int |
141 | | asn1_decode_pbkdf2_params(sc_context_t *ctx, void **paramp, |
142 | | const u8 *buf, size_t buflen, int depth) |
143 | 0 | { |
144 | 0 | struct sc_pbkdf2_params info; |
145 | 0 | struct sc_asn1_entry asn1_pbkdf2_params[5]; |
146 | 0 | int r; |
147 | |
|
148 | 0 | sc_copy_asn1_entry(c_asn1_pbkdf2_params, asn1_pbkdf2_params); |
149 | 0 | sc_format_asn1_entry(asn1_pbkdf2_params + 0, |
150 | 0 | info.salt, &info.salt_len, 0); |
151 | 0 | sc_format_asn1_entry(asn1_pbkdf2_params + 1, |
152 | 0 | &info.iterations, NULL, 0); |
153 | 0 | sc_format_asn1_entry(asn1_pbkdf2_params + 2, |
154 | 0 | &info.key_length, NULL, 0); |
155 | 0 | sc_format_asn1_entry(asn1_pbkdf2_params + 3, |
156 | 0 | &info.hash_alg, NULL, 0); |
157 | |
|
158 | 0 | memset(&info, 0, sizeof(info)); |
159 | 0 | info.salt_len = sizeof(info.salt); |
160 | 0 | info.hash_alg.algorithm = SC_ALGORITHM_SHA1; |
161 | |
|
162 | 0 | r = _sc_asn1_decode(ctx, asn1_pbkdf2_params, buf, buflen, NULL, NULL, 0, depth + 1); |
163 | 0 | if (r < 0) |
164 | 0 | return r; |
165 | | |
166 | 0 | *paramp = malloc(sizeof(info)); |
167 | 0 | if (!*paramp) |
168 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
169 | 0 | memcpy(*paramp, &info, sizeof(info)); |
170 | 0 | return 0; |
171 | 0 | } |
172 | | |
173 | | static int |
174 | | asn1_encode_pbkdf2_params(sc_context_t *ctx, void *params, |
175 | | u8 **buf, size_t *buflen, int depth) |
176 | 0 | { |
177 | 0 | struct sc_pbkdf2_params *info; |
178 | 0 | struct sc_asn1_entry asn1_pbkdf2_params[5]; |
179 | |
|
180 | 0 | info = (struct sc_pbkdf2_params *) params; |
181 | |
|
182 | 0 | sc_copy_asn1_entry(c_asn1_pbkdf2_params, asn1_pbkdf2_params); |
183 | 0 | sc_format_asn1_entry(asn1_pbkdf2_params + 0, |
184 | 0 | info->salt, &info->salt_len, 1); |
185 | 0 | sc_format_asn1_entry(asn1_pbkdf2_params + 1, |
186 | 0 | &info->iterations, NULL, 1); |
187 | 0 | if (info->key_length > 0) |
188 | 0 | sc_format_asn1_entry(asn1_pbkdf2_params + 2, |
189 | 0 | &info->key_length, NULL, 1); |
190 | 0 | if (info->hash_alg.algorithm != SC_ALGORITHM_SHA1) |
191 | 0 | sc_format_asn1_entry(asn1_pbkdf2_params + 3, |
192 | 0 | &info->hash_alg, NULL, 0); |
193 | |
|
194 | 0 | return _sc_asn1_encode(ctx, asn1_pbkdf2_params, buf, buflen, depth + 1); |
195 | 0 | } |
196 | | |
197 | | static const struct sc_asn1_entry c_asn1_pbes2_params[] = { |
198 | | { "keyDerivationAlg", SC_ASN1_ALGORITHM_ID, SC_ASN1_TAG_SEQUENCE, 0, NULL, NULL }, |
199 | | { "keyEcnryptionAlg", SC_ASN1_ALGORITHM_ID, SC_ASN1_TAG_SEQUENCE, 0, NULL, NULL }, |
200 | | { NULL, 0, 0, 0, NULL, NULL } |
201 | | }; |
202 | | |
203 | | static int |
204 | | asn1_decode_pbes2_params(sc_context_t *ctx, void **paramp, |
205 | | const u8 *buf, size_t buflen, int depth) |
206 | 0 | { |
207 | 0 | struct sc_asn1_entry asn1_pbes2_params[3]; |
208 | 0 | struct sc_pbes2_params info; |
209 | 0 | int r; |
210 | |
|
211 | 0 | sc_copy_asn1_entry(c_asn1_pbes2_params, asn1_pbes2_params); |
212 | 0 | sc_format_asn1_entry(asn1_pbes2_params + 0, |
213 | 0 | &info.derivation_alg, NULL, 0); |
214 | 0 | sc_format_asn1_entry(asn1_pbes2_params + 1, |
215 | 0 | &info.key_encr_alg, NULL, 0); |
216 | 0 | memset(&info, 0, sizeof(info)); |
217 | |
|
218 | 0 | r = _sc_asn1_decode(ctx, asn1_pbes2_params, buf, buflen, NULL, NULL, 0, depth + 1); |
219 | 0 | if (r < 0) |
220 | 0 | return r; |
221 | 0 | *paramp = malloc(sizeof(info)); |
222 | 0 | if (!*paramp) |
223 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
224 | 0 | memcpy(*paramp, &info, sizeof(info)); |
225 | 0 | return 0; |
226 | 0 | } |
227 | | |
228 | | static int |
229 | | asn1_encode_pbes2_params(sc_context_t *ctx, void *params, |
230 | | u8 **buf, size_t *buflen, int depth) |
231 | 0 | { |
232 | 0 | struct sc_asn1_entry asn1_pbes2_params[3]; |
233 | 0 | struct sc_pbes2_params *info; |
234 | |
|
235 | 0 | info = (struct sc_pbes2_params *) params; |
236 | 0 | sc_copy_asn1_entry(c_asn1_pbes2_params, asn1_pbes2_params); |
237 | 0 | sc_format_asn1_entry(asn1_pbes2_params + 0, |
238 | 0 | &info->derivation_alg, NULL, 0); |
239 | 0 | sc_format_asn1_entry(asn1_pbes2_params + 1, |
240 | 0 | &info->key_encr_alg, NULL, 0); |
241 | 0 | return _sc_asn1_encode(ctx, asn1_pbes2_params, buf, buflen, depth + 1); |
242 | 0 | } |
243 | | |
244 | | static void |
245 | | asn1_free_pbes2_params(void *ptr) |
246 | 0 | { |
247 | 0 | struct sc_pbes2_params *params = (struct sc_pbes2_params *) ptr; |
248 | |
|
249 | 0 | sc_asn1_clear_algorithm_id(¶ms->derivation_alg); |
250 | 0 | sc_asn1_clear_algorithm_id(¶ms->key_encr_alg); |
251 | 0 | free(params); |
252 | 0 | } |
253 | | |
254 | | static const struct sc_asn1_entry c_asn1_ec_params[] = { |
255 | | { "ecParameters", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
256 | | { "namedCurve", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, 0, NULL, NULL}, |
257 | | { "implicityCA", SC_ASN1_NULL, SC_ASN1_TAG_NULL, 0, NULL, NULL }, |
258 | | { NULL, 0, 0, 0, NULL, NULL } |
259 | | }; |
260 | | |
261 | | static int |
262 | | asn1_decode_ec_params(sc_context_t *ctx, void **paramp, |
263 | | const u8 *buf, size_t buflen, int depth) |
264 | 3.86k | { |
265 | 3.86k | int r; |
266 | 3.86k | struct sc_object_id curve; |
267 | 3.86k | struct sc_asn1_entry asn1_ec_params[4]; |
268 | 3.86k | struct sc_ec_parameters *ecp; |
269 | | |
270 | 3.86k | memset(&curve, 0, sizeof(curve)); |
271 | | |
272 | | /* We only want to copy the parms if they are a namedCurve |
273 | | * or ecParameters nullParam aka implicityCA is not to be |
274 | | * used with PKCS#11 2.20 */ |
275 | 3.86k | sc_copy_asn1_entry(c_asn1_ec_params, asn1_ec_params); |
276 | 3.86k | sc_format_asn1_entry(asn1_ec_params + 1, &curve, 0, 0); |
277 | | |
278 | | /* Some signature algorithms will not have any data */ |
279 | 3.86k | if (buflen == 0 || buf == NULL) |
280 | 34 | return 0; |
281 | | |
282 | 3.83k | r = sc_asn1_decode_choice(ctx, asn1_ec_params, buf, buflen, NULL, NULL); |
283 | | /* r = index in asn1_ec_params */ |
284 | 3.83k | sc_debug(ctx, SC_LOG_DEBUG_ASN1, "asn1_decode_ec_params r=%d", r); |
285 | 3.83k | if (r < 0) |
286 | 24 | return r; |
287 | | |
288 | 3.81k | ecp = calloc(1, sizeof(struct sc_ec_parameters)); |
289 | 3.81k | if (ecp == NULL) |
290 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
291 | | |
292 | 3.81k | if (r <= 1) { |
293 | 3.81k | ecp->der.value = malloc(buflen); |
294 | 3.81k | if (ecp->der.value == NULL) { |
295 | 0 | free(ecp); |
296 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
297 | 0 | } |
298 | 3.81k | ecp->der.len = buflen; |
299 | 3.81k | memcpy(ecp->der.value, buf, buflen); |
300 | 3.81k | } |
301 | 0 | else { |
302 | 0 | r = 0; |
303 | 0 | } |
304 | | |
305 | 3.81k | ecp->type = r; /* but 0 = ecparams if any, 1=named curve */ |
306 | 3.81k | *paramp = ecp; |
307 | 3.81k | return SC_SUCCESS; |
308 | 3.81k | }; |
309 | | |
310 | | static int |
311 | | asn1_encode_ec_params(sc_context_t *ctx, void *params, |
312 | | u8 **buf, size_t *buflen, int depth) |
313 | 2.68k | { |
314 | 2.68k | struct sc_ec_parameters *ecp = (struct sc_ec_parameters *) params; |
315 | | |
316 | | /* Only handle named curves. They may be absent too */ |
317 | 2.68k | sc_debug(ctx, SC_LOG_DEBUG_ASN1, "asn1_encode_ec_params() called"); |
318 | 2.68k | *buf = NULL; |
319 | 2.68k | *buflen = 0; |
320 | 2.68k | if (ecp && ecp->type == 1 && ecp->der.value) { /* named curve */ |
321 | 2.68k | *buf = malloc(ecp->der.len); |
322 | 2.68k | if (*buf == NULL) |
323 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
324 | | |
325 | 2.68k | memcpy(*buf, ecp->der.value, ecp->der.len); |
326 | 2.68k | *buflen = ecp->der.len; |
327 | 2.68k | } |
328 | 0 | else { |
329 | 0 | sc_debug(ctx, SC_LOG_DEBUG_ASN1, "Not named curve"); |
330 | 0 | } |
331 | | |
332 | 2.68k | return 0; |
333 | 2.68k | } |
334 | | |
335 | | static void |
336 | | asn1_free_ec_params(void *params) |
337 | 4.71k | { |
338 | 4.71k | struct sc_ec_parameters *ecp = (struct sc_ec_parameters *) params; |
339 | | |
340 | 4.71k | if (ecp) { |
341 | 4.71k | if (ecp->der.value) |
342 | 4.71k | free(ecp->der.value); |
343 | 4.71k | if (ecp->named_curve) |
344 | 0 | free(ecp->named_curve); |
345 | 4.71k | free(ecp); |
346 | 4.71k | } |
347 | 4.71k | } |
348 | | |
349 | | static struct sc_asn1_pkcs15_algorithm_info algorithm_table[] = { |
350 | | #ifdef SC_ALGORITHM_SHA1 |
351 | | /* hmacWithSHA1 */ |
352 | | {SC_ALGORITHM_SHA1, {{1, 2, 840, 113549, 2, 7, -1}}, NULL, NULL, NULL}, |
353 | | {SC_ALGORITHM_SHA1, {{1, 3, 6, 1, 5, 5, 8, 1, 2, -1}}, NULL, NULL, NULL}, |
354 | | /* SHA1 */ |
355 | | {SC_ALGORITHM_SHA1, {{1, 3, 14, 3, 2, 26, -1}}, NULL, NULL, NULL}, |
356 | | #endif |
357 | | #ifdef SC_ALGORITHM_MD5 |
358 | | {SC_ALGORITHM_MD5, {{1, 2, 840, 113549, 2, 5, -1}}, NULL, NULL, NULL}, |
359 | | #endif |
360 | | #ifdef SC_ALGORITHM_RSA /* really rsaEncryption and id-RSASSA-PSS */ |
361 | | {SC_ALGORITHM_RSA, {{1, 2, 840, 113549, 1, 1, 1, -1}}, NULL, NULL, NULL}, |
362 | | {SC_ALGORITHM_RSA, {{1, 2, 840, 113549, 1, 1, 10, -1}}, NULL, NULL, NULL}, |
363 | | #endif |
364 | | #ifdef SC_ALGORITHM_DH |
365 | | {SC_ALGORITHM_DH, {{1, 2, 840, 10046, 2, 1, -1}}, NULL, NULL, NULL}, |
366 | | #endif |
367 | | #ifdef SC_ALGORITHM_RC2_WRAP /* from CMS */ |
368 | | {SC_ALGORITHM_RC2_WRAP, {{1, 2, 840, 113549, 1, 9, 16, 3, 7, -1}}, NULL, NULL, NULL}, |
369 | | #endif |
370 | | #ifdef SC_ALGORITHM_RC2 /* CBC mode */ |
371 | | {SC_ALGORITHM_RC2, {{1, 2, 840, 113549, 3, 2, -1}}, |
372 | | asn1_decode_rc2_params, |
373 | | asn1_encode_rc2_params}, |
374 | | #endif |
375 | | #ifdef SC_ALGORITHM_DES /* CBC mode */ |
376 | | {SC_ALGORITHM_DES, {{1, 3, 14, 3, 2, 7, -1}}, |
377 | | asn1_decode_des_params, |
378 | | asn1_encode_des_params, |
379 | | free}, |
380 | | #endif |
381 | | #ifdef SC_ALGORITHM_3DES_WRAP /* from CMS */ |
382 | | {SC_ALGORITHM_3DES_WRAP, {{1, 2, 840, 113549, 1, 9, 16, 3, 6, -1}}, NULL, NULL, NULL}, |
383 | | #endif |
384 | | #ifdef SC_ALGORITHM_3DES /* EDE CBC mode */ |
385 | | {SC_ALGORITHM_3DES, {{1, 2, 840, 113549, 3, 7, -1}}, |
386 | | asn1_decode_des_params, |
387 | | asn1_encode_des_params, |
388 | | free}, |
389 | | #endif |
390 | | #ifdef SC_ALGORITHM_GOST /* EDE CBC mode */ |
391 | | {SC_ALGORITHM_GOST, {{1, 2, 4434, 66565, 3, 7, -1}}, NULL, NULL, NULL}, |
392 | | #endif |
393 | | #ifdef SC_ALGORITHM_GOSTR3410 |
394 | | {SC_ALGORITHM_GOSTR3410, {{1, 2, 643, 2, 2, 19, -1}}, |
395 | | asn1_decode_gostr3410_params, |
396 | | asn1_encode_gostr3410_params, |
397 | | NULL}, |
398 | | #endif |
399 | | /* We do not support PBES1 because the encryption is weak */ |
400 | | #ifdef SC_ALGORITHM_PBKDF2 |
401 | | {SC_ALGORITHM_PBKDF2, {{1, 2, 840, 113549, 1, 5, 12, -1}}, |
402 | | asn1_decode_pbkdf2_params, |
403 | | asn1_encode_pbkdf2_params, |
404 | | free}, |
405 | | #endif |
406 | | #ifdef SC_ALGORITHM_PBES2 |
407 | | {SC_ALGORITHM_PBES2, {{1, 2, 840, 113549, 1, 5, 13, -1}}, |
408 | | asn1_decode_pbes2_params, |
409 | | asn1_encode_pbes2_params, |
410 | | asn1_free_pbes2_params}, |
411 | | #endif |
412 | | #ifdef SC_ALGORITHM_EC |
413 | | {SC_ALGORITHM_EC, {{1, 2, 840, 10045, 2, 1, -1}}, |
414 | | asn1_decode_ec_params, |
415 | | asn1_encode_ec_params, |
416 | | asn1_free_ec_params}, |
417 | | #endif |
418 | | #ifdef SC_ALGORITHM_ECDSA_SHA1 |
419 | | /* Note RFC 3279 says no ecParameters */ |
420 | | {SC_ALGORITHM_ECDSA_SHA1, {{1, 2, 840, 10045, 4, 1, -1}}, NULL, NULL, NULL}, |
421 | | #endif |
422 | | #ifdef SC_ALGORITHM_ECDSA_SHA224 |
423 | | /* These next 4 are defined in RFC 5758 */ |
424 | | {SC_ALGORITHM_ECDSA_SHA224, {{1, 2, 840, 10045, 4, 3, 1, -1}}, |
425 | | asn1_decode_ec_params, |
426 | | asn1_encode_ec_params, |
427 | | asn1_free_ec_params}, |
428 | | #endif |
429 | | #ifdef SC_ALGORITHM_ECDSA_SHA256 |
430 | | {SC_ALGORITHM_ECDSA_SHA256, {{1, 2, 840, 10045, 4, 3, 2, -1}}, |
431 | | asn1_decode_ec_params, |
432 | | asn1_encode_ec_params, |
433 | | asn1_free_ec_params}, |
434 | | #endif |
435 | | #ifdef SC_ALGORITHM_ECDSA_SHA384 |
436 | | {SC_ALGORITHM_ECDSA_SHA384, {{1, 2, 840, 10045, 4, 3, 3, -1}}, |
437 | | asn1_decode_ec_params, |
438 | | asn1_encode_ec_params, |
439 | | asn1_free_ec_params}, |
440 | | #endif |
441 | | #ifdef SC_ALGORITHM_ECDSA_SHA512 |
442 | | {SC_ALGORITHM_ECDSA_SHA512, {{1, 2, 840, 10045, 4, 3, 4, -1}}, |
443 | | asn1_decode_ec_params, |
444 | | asn1_encode_ec_params, |
445 | | asn1_free_ec_params}, |
446 | | #endif |
447 | | #ifdef SC_ALGORITHM_EDDSA |
448 | | /* aka Ed25519 */ |
449 | | /* RFC 8410, needed to parse/create X509 certs/pubkeys */ |
450 | | {SC_ALGORITHM_EDDSA, {{1, 3, 101, 112, -1}}, |
451 | | asn1_decode_ec_params, |
452 | | asn1_encode_ec_params, |
453 | | asn1_free_ec_params}, /* Ed25119 */ |
454 | | {SC_ALGORITHM_EDDSA, {{1, 3, 6, 1, 4, 1, 11591, 15, 1, -1}}, |
455 | | asn1_decode_ec_params, |
456 | | asn1_encode_ec_params, |
457 | | asn1_free_ec_params}, /* Ed25519 OID used by OpenPGP */ |
458 | | {SC_ALGORITHM_EDDSA, {{1, 3, 101, 113, -1}}, |
459 | | asn1_decode_ec_params, |
460 | | asn1_encode_ec_params, |
461 | | asn1_free_ec_params}, /* Ed448 */ |
462 | | #endif |
463 | | #ifdef SC_ALGORITHM_XEDDSA |
464 | | /* aka curve25519 */ |
465 | | /* RFC 8410, needed to parse/create X509 certs/pubkeys ec_parms*/ |
466 | | {SC_ALGORITHM_XEDDSA, {{1, 3, 101, 110, -1}}, |
467 | | asn1_decode_ec_params, |
468 | | asn1_encode_ec_params, |
469 | | asn1_free_ec_params}, /* X25519 */ |
470 | | {SC_ALGORITHM_XEDDSA, {{1, 3, 6, 1, 4, 1, 3029, 1, 5, 1 - 1}}, |
471 | | asn1_decode_ec_params, |
472 | | asn1_encode_ec_params, |
473 | | asn1_free_ec_params}, /* X25519 OID used by OpenPGP */ |
474 | | {SC_ALGORITHM_XEDDSA, {{1, 3, 101, 111, -1}}, |
475 | | asn1_decode_ec_params, |
476 | | asn1_encode_ec_params, |
477 | | asn1_free_ec_params}, /* X448 */ |
478 | | #endif |
479 | | {-1, {{-1}}, NULL, NULL, NULL} |
480 | | }; |
481 | | |
482 | | |
483 | | static struct sc_asn1_pkcs15_algorithm_info * |
484 | | sc_asn1_get_algorithm_info(const struct sc_algorithm_id *id) |
485 | 14.8k | { |
486 | 14.8k | struct sc_asn1_pkcs15_algorithm_info *aip = NULL; |
487 | | |
488 | 199k | for (aip = algorithm_table; aip->id >= 0; aip++) { |
489 | 196k | if ((int) id->algorithm < 0 && sc_compare_oid(&id->oid, &aip->oid)) |
490 | 4.57k | return aip; |
491 | | |
492 | 192k | if (aip->id == (int)id->algorithm) |
493 | 7.91k | return aip; |
494 | 192k | } |
495 | | |
496 | 2.31k | return NULL; |
497 | 14.8k | } |
498 | | |
499 | | static const struct sc_asn1_entry c_asn1_alg_id[3] = { |
500 | | { "algorithm", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, 0, NULL, NULL }, |
501 | | { "nullParam", SC_ASN1_NULL, SC_ASN1_TAG_NULL, SC_ASN1_OPTIONAL, NULL, NULL }, |
502 | | { NULL, 0, 0, 0, NULL, NULL } |
503 | | }; |
504 | | |
505 | | int |
506 | | sc_asn1_decode_algorithm_id(struct sc_context *ctx, const unsigned char *in, |
507 | | size_t len, struct sc_algorithm_id *id, |
508 | | int depth) |
509 | 6.94k | { |
510 | 6.94k | struct sc_asn1_pkcs15_algorithm_info *alg_info = NULL; |
511 | 6.94k | struct sc_asn1_entry asn1_alg_id[3]; |
512 | 6.94k | int r; |
513 | | |
514 | 6.94k | LOG_FUNC_CALLED(ctx); |
515 | 6.94k | sc_copy_asn1_entry(c_asn1_alg_id, asn1_alg_id); |
516 | 6.94k | sc_format_asn1_entry(asn1_alg_id + 0, &id->oid, NULL, 0); |
517 | | |
518 | 6.94k | memset(id, 0, sizeof(*id)); |
519 | 6.94k | r = _sc_asn1_decode(ctx, asn1_alg_id, in, len, &in, &len, 0, depth + 1); |
520 | 6.94k | LOG_TEST_RET(ctx, r, "ASN.1 parsing of algo ID failed"); |
521 | | |
522 | 6.89k | sc_log(ctx, "decoded OID '%s'", sc_dump_oid(&(id->oid))); |
523 | | |
524 | | /* See if we understand the algorithm, and if we do, check |
525 | | * whether we know how to decode any additional parameters */ |
526 | 6.89k | id->algorithm = (unsigned int ) -1; |
527 | 6.89k | alg_info = sc_asn1_get_algorithm_info(id); |
528 | 6.89k | if (alg_info != NULL) { |
529 | 4.57k | id->algorithm = alg_info->id; |
530 | 4.57k | if (alg_info->decode) { |
531 | 3.87k | if (asn1_alg_id[1].flags & SC_ASN1_PRESENT) { |
532 | 7 | sc_log(ctx, "SC_ASN1_PRESENT was set, so invalid"); |
533 | 7 | LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ASN1_OBJECT); |
534 | 7 | } |
535 | 3.86k | r = alg_info->decode(ctx, &id->params, in, len, depth); |
536 | 3.86k | } |
537 | 4.57k | } |
538 | | |
539 | 6.88k | LOG_FUNC_RETURN(ctx, r); |
540 | 6.88k | } |
541 | | |
542 | | int |
543 | | sc_asn1_encode_algorithm_id(struct sc_context *ctx, u8 **buf, size_t *len, |
544 | | const struct sc_algorithm_id *id, |
545 | | int depth) |
546 | 3.20k | { |
547 | 3.20k | struct sc_asn1_pkcs15_algorithm_info *alg_info; |
548 | 3.20k | struct sc_algorithm_id temp_id; |
549 | 3.20k | struct sc_asn1_entry asn1_alg_id[3]; |
550 | 3.20k | u8 *obj = NULL; |
551 | 3.20k | size_t obj_len = 0; |
552 | 3.20k | int r; |
553 | 3.20k | u8 *tmp; |
554 | | |
555 | 3.20k | LOG_FUNC_CALLED(ctx); |
556 | 3.20k | sc_log(ctx, "type of algorithm to encode: %lu", id->algorithm); |
557 | 3.20k | alg_info = sc_asn1_get_algorithm_info(id); |
558 | 3.20k | if (alg_info == NULL) { |
559 | 0 | sc_log(ctx, "Cannot encode unknown algorithm %lu", id->algorithm); |
560 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS); |
561 | 0 | } |
562 | | |
563 | | /* Set the oid if not yet given */ |
564 | 3.20k | if (!sc_valid_oid(&id->oid)) { |
565 | 9 | temp_id = *id; |
566 | 9 | temp_id.oid = alg_info->oid; |
567 | 9 | id = &temp_id; |
568 | 9 | } |
569 | | |
570 | 3.20k | sc_log(ctx, "encode algo %s", sc_dump_oid(&(id->oid))); |
571 | 3.20k | sc_copy_asn1_entry(c_asn1_alg_id, asn1_alg_id); |
572 | 3.20k | sc_format_asn1_entry(asn1_alg_id + 0, (void *) &id->oid, NULL, 1); |
573 | | |
574 | | /* no parameters, write NULL tag */ |
575 | | /* If it's EDDSA/XEDDSA, according to RFC8410, params |
576 | | * MUST be absent */ |
577 | | /* PKCS11 3.0 list them under ec_params */ |
578 | 3.20k | if (id->algorithm != SC_ALGORITHM_EDDSA && |
579 | 3.19k | id->algorithm != SC_ALGORITHM_XEDDSA && |
580 | 3.18k | (!id->params || !alg_info->encode)) |
581 | 498 | asn1_alg_id[1].flags |= SC_ASN1_PRESENT; |
582 | | |
583 | 3.20k | r = _sc_asn1_encode(ctx, asn1_alg_id, buf, len, depth + 1); |
584 | 3.20k | LOG_TEST_RET(ctx, r, "ASN.1 encode of algorithm failed"); |
585 | | |
586 | | /* Encode any parameters */ |
587 | 3.20k | if (id->params && alg_info->encode) { |
588 | 2.68k | r = alg_info->encode(ctx, id->params, &obj, &obj_len, depth+1); |
589 | 2.68k | if (r < 0) { |
590 | 0 | if (obj) |
591 | 0 | free(obj); |
592 | 0 | LOG_FUNC_RETURN(ctx, r); |
593 | 0 | } |
594 | 2.68k | } |
595 | | |
596 | 3.20k | if (obj_len) { |
597 | 2.68k | tmp = (u8 *) realloc(*buf, *len + obj_len); |
598 | 2.68k | if (!tmp) { |
599 | 0 | free(*buf); |
600 | 0 | *buf = NULL; |
601 | 0 | free(obj); |
602 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); |
603 | 0 | } |
604 | 2.68k | *buf = tmp; |
605 | 2.68k | memcpy(*buf + *len, obj, obj_len); |
606 | 2.68k | *len += obj_len; |
607 | 2.68k | free(obj); |
608 | 2.68k | } |
609 | | |
610 | 3.20k | sc_log(ctx, "return encoded algorithm ID: %s", sc_dump_hex(*buf, *len)); |
611 | 3.20k | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
612 | 3.20k | } |
613 | | |
614 | | void |
615 | | sc_asn1_clear_algorithm_id(struct sc_algorithm_id *id) |
616 | 8.30k | { |
617 | 8.30k | struct sc_asn1_pkcs15_algorithm_info *aip; |
618 | | |
619 | 8.30k | if (id->params && (aip = sc_asn1_get_algorithm_info(id)) && aip->free) { |
620 | 4.71k | aip->free(id->params); |
621 | | id->params = NULL; |
622 | 4.71k | } |
623 | 8.30k | } |