/src/opensc/src/libopensc/pkcs15-cert.c
Line | Count | Source |
1 | | /* |
2 | | * pkcs15-cert.c: PKCS #15 certificate functions |
3 | | * |
4 | | * Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi> |
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 <stdlib.h> |
26 | | #include <string.h> |
27 | | #include <stdio.h> |
28 | | #include <sys/stat.h> |
29 | | #ifdef HAVE_UNISTD_H |
30 | | #include <unistd.h> |
31 | | #endif |
32 | | |
33 | | #include "internal.h" |
34 | | #include "asn1.h" |
35 | | #include "pkcs15.h" |
36 | | |
37 | | static int |
38 | | parse_x509_cert(sc_context_t *ctx, struct sc_pkcs15_der *der, struct sc_pkcs15_cert *cert) |
39 | 3.40k | { |
40 | 3.40k | int r; |
41 | 3.40k | struct sc_algorithm_id sig_alg = {0}; |
42 | 3.40k | struct sc_pkcs15_pubkey *pubkey = NULL; |
43 | 3.40k | unsigned char *serial = NULL, *issuer = NULL, *subject = NULL, *buf = der->value; |
44 | 3.40k | size_t serial_len = 0, issuer_len = 0, subject_len = 0, data_len = 0, buflen = der->len; |
45 | 3.40k | struct sc_asn1_entry asn1_version[] = { |
46 | 3.40k | { "version", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, &cert->version, NULL }, |
47 | 3.40k | { NULL, 0, 0, 0, NULL, NULL } |
48 | 3.40k | }; |
49 | 3.40k | struct sc_asn1_entry asn1_extensions[] = { |
50 | 3.40k | { "x509v3", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL| SC_ASN1_ALLOC, &cert->extensions, &cert->extensions_len }, |
51 | 3.40k | { NULL, 0, 0, 0, NULL, NULL } |
52 | 3.40k | }; |
53 | 3.40k | struct sc_asn1_entry asn1_tbscert[] = { |
54 | 3.40k | { "version", SC_ASN1_STRUCT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, asn1_version, NULL }, |
55 | 3.40k | { "serialNumber", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_INTEGER, SC_ASN1_ALLOC, &serial, &serial_len }, |
56 | 3.40k | { "signature", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
57 | 3.40k | { "issuer", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_ALLOC, &issuer, &issuer_len }, |
58 | 3.40k | { "validity", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
59 | 3.40k | { "subject", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_ALLOC, &subject, &subject_len }, |
60 | | /* Use a callback to get the algorithm, parameters and pubkey into sc_pkcs15_pubkey */ |
61 | 3.40k | { "subjectPublicKeyInfo",SC_ASN1_CALLBACK, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, sc_pkcs15_pubkey_from_spki_fields, &pubkey }, |
62 | 3.40k | { "extensions", SC_ASN1_STRUCT, SC_ASN1_CTX | 3 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, asn1_extensions, NULL }, |
63 | 3.40k | { NULL, 0, 0, 0, NULL, NULL } |
64 | 3.40k | }; |
65 | 3.40k | struct sc_asn1_entry asn1_cert[] = { |
66 | 3.40k | { "tbsCertificate", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, asn1_tbscert, NULL }, |
67 | 3.40k | { "signatureAlgorithm", SC_ASN1_ALGORITHM_ID, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, &sig_alg, NULL }, |
68 | 3.40k | { "signatureValue", SC_ASN1_BIT_STRING, SC_ASN1_TAG_BIT_STRING, 0, NULL, NULL }, |
69 | 3.40k | { NULL, 0, 0, 0, NULL, NULL } |
70 | 3.40k | }; |
71 | 3.40k | struct sc_asn1_entry asn1_serial_number[] = { |
72 | 3.40k | { "serialNumber", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_INTEGER, SC_ASN1_ALLOC, NULL, NULL }, |
73 | 3.40k | { NULL, 0, 0, 0, NULL, NULL } |
74 | 3.40k | }; |
75 | 3.40k | struct sc_asn1_entry asn1_subject[] = { |
76 | 3.40k | { "subject", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_ALLOC, NULL, NULL }, |
77 | 3.40k | { NULL, 0, 0, 0, NULL, NULL } |
78 | 3.40k | }; |
79 | 3.40k | struct sc_asn1_entry asn1_issuer[] = { |
80 | 3.40k | { "issuer", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_ALLOC, NULL, NULL }, |
81 | 3.40k | { NULL, 0, 0, 0, NULL, NULL } |
82 | 3.40k | }; |
83 | | |
84 | 3.40k | const u8 *obj; |
85 | 3.40k | size_t objlen; |
86 | | |
87 | 3.40k | LOG_FUNC_CALLED(ctx); |
88 | | |
89 | 3.40k | memset(cert, 0, sizeof(*cert)); |
90 | 3.40k | obj = sc_asn1_verify_tag(ctx, buf, buflen, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, &objlen); |
91 | 3.40k | if (obj == NULL) |
92 | 3.40k | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "X.509 certificate not found"); |
93 | | |
94 | 2.62k | data_len = objlen + (obj - buf); |
95 | 2.62k | cert->data.value = malloc(data_len); |
96 | 2.62k | if (!cert->data.value) |
97 | 2.62k | LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); |
98 | 2.62k | memcpy(cert->data.value, buf, data_len); |
99 | 2.62k | cert->data.len = data_len; |
100 | | |
101 | 2.62k | r = sc_asn1_decode(ctx, asn1_cert, obj, objlen, NULL, NULL); |
102 | 2.62k | cert->key = pubkey; |
103 | 2.62k | cert->version++; |
104 | | |
105 | 2.62k | LOG_TEST_GOTO_ERR(ctx, r, "ASN.1 parsing of certificate failed"); |
106 | | |
107 | 2.15k | if (!pubkey) { |
108 | 0 | r = SC_ERROR_INVALID_ASN1_OBJECT; |
109 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Unable to decode subjectPublicKeyInfo from cert"); |
110 | 0 | } |
111 | | |
112 | | |
113 | 2.15k | if (serial && serial_len) { |
114 | 2.15k | sc_format_asn1_entry(asn1_serial_number + 0, serial, &serial_len, 1); |
115 | 2.15k | r = sc_asn1_encode(ctx, asn1_serial_number, &cert->serial, &cert->serial_len); |
116 | 2.15k | LOG_TEST_GOTO_ERR(ctx, r, "ASN.1 encoding of serial failed"); |
117 | 2.15k | } |
118 | | |
119 | 2.15k | if (subject && subject_len) { |
120 | 2.15k | sc_format_asn1_entry(asn1_subject + 0, subject, &subject_len, 1); |
121 | 2.15k | r = sc_asn1_encode(ctx, asn1_subject, &cert->subject, &cert->subject_len); |
122 | 2.15k | LOG_TEST_GOTO_ERR(ctx, r, "ASN.1 encoding of subject"); |
123 | 2.15k | } |
124 | | |
125 | 2.15k | if (issuer && issuer_len) { |
126 | 2.08k | sc_format_asn1_entry(asn1_issuer + 0, issuer, &issuer_len, 1); |
127 | 2.08k | r = sc_asn1_encode(ctx, asn1_issuer, &cert->issuer, &cert->issuer_len); |
128 | 2.08k | LOG_TEST_GOTO_ERR(ctx, r, "ASN.1 encoding of issuer"); |
129 | 2.08k | } |
130 | | |
131 | 2.62k | err: |
132 | | /* not used for anything */ |
133 | 2.62k | sc_asn1_clear_algorithm_id(&sig_alg); |
134 | 2.62k | free(serial); |
135 | 2.62k | free(subject); |
136 | 2.62k | free(issuer); |
137 | | |
138 | 2.62k | LOG_FUNC_RETURN(ctx, r); |
139 | 2.62k | } |
140 | | |
141 | | |
142 | | /* Get a component of Distinguished Name (e.i. subject or issuer) USING the oid tag. |
143 | | * dn can be either cert->subject or cert->issuer. |
144 | | * dn_len would be cert->subject_len or cert->issuer_len. |
145 | | * |
146 | | * Common types: |
147 | | * CN: struct sc_object_id type = {{2, 5, 4, 3, -1}}; |
148 | | * Country: struct sc_object_id type = {{2, 5, 4, 6, -1}}; |
149 | | * L: struct sc_object_id type = {{2, 5, 4, 7, -1}}; |
150 | | * S: struct sc_object_id type = {{2, 5, 4, 8, -1}}; |
151 | | * O: struct sc_object_id type = {{2, 5, 4, 10, -1}}; |
152 | | * OU: struct sc_object_id type = {{2, 5, 4, 11, -1}}; |
153 | | * |
154 | | * if *name is NULL, sc_pkcs15_get_name_from_dn will allocate space for name. |
155 | | */ |
156 | | int |
157 | | sc_pkcs15_get_name_from_dn(struct sc_context *ctx, const u8 *dn, size_t dn_len, |
158 | | const struct sc_object_id *type, u8 **name, size_t *name_len) |
159 | 1.04k | { |
160 | 1.04k | const u8 *rdn = NULL; |
161 | 1.04k | const u8 *next_ava = NULL; |
162 | 1.04k | size_t rdn_len = 0; |
163 | 1.04k | size_t next_ava_len = 0; |
164 | 1.04k | int rv; |
165 | | |
166 | 1.04k | rdn = sc_asn1_skip_tag(ctx, &dn, &dn_len, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, &rdn_len); |
167 | 1.04k | if (rdn == NULL) |
168 | 1.04k | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of Distinguished Name"); |
169 | | |
170 | 1.12k | for (next_ava = rdn, next_ava_len = rdn_len; next_ava_len; ) { |
171 | 1.04k | const u8 *ava, *dummy, *oidp; |
172 | 1.04k | struct sc_object_id oid; |
173 | 1.04k | size_t ava_len = 0, dummy_len, oid_len = 0; |
174 | | |
175 | | /* unwrap the set and point to the next ava */ |
176 | 1.04k | ava = sc_asn1_skip_tag(ctx, &next_ava, &next_ava_len, SC_ASN1_TAG_SET | SC_ASN1_CONS, &ava_len); |
177 | 1.04k | if (ava == NULL) |
178 | 1.04k | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA"); |
179 | | |
180 | | /* It would be nice to use sc_asn1_decode here to parse the entire AVA, but we are missing 1 critical |
181 | | * function in the templates: the ability to accept any tag for value. This prevents us from just |
182 | | * grabbing the value as is out of the template. AVA's can have tags of PRINTABLE_STRING, |
183 | | * TELETEXSTRING, T61STRING or UTF8_STRING with PRINTABLE_STRING and UTF8_STRING being the most common. |
184 | | * The other feature that would be nice is returning a pointer to our requested data using the space |
185 | | * of the parent (basically what this code is doing here), rather than allocating and copying. |
186 | | */ |
187 | | |
188 | | /* unwrap the sequence */ |
189 | 760 | dummy = ava; dummy_len = ava_len; |
190 | 760 | ava = sc_asn1_skip_tag(ctx, &dummy, &dummy_len, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, &ava_len); |
191 | 760 | if (ava == NULL) |
192 | 760 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA"); |
193 | | |
194 | | /* unwrap the oid */ |
195 | 507 | oidp = sc_asn1_skip_tag(ctx, &ava, &ava_len, SC_ASN1_TAG_OBJECT, &oid_len); |
196 | 507 | if (ava == NULL || ava_len < 1) |
197 | 507 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA OID"); |
198 | | |
199 | | /* Convert to OID */ |
200 | 496 | rv = sc_asn1_decode_object_id(oidp, oid_len, &oid); |
201 | 496 | if (rv != SC_SUCCESS) |
202 | 496 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA OID"); |
203 | | |
204 | 113 | if (sc_compare_oid(&oid, type) == 0) |
205 | 78 | continue; |
206 | | |
207 | | /* Yes, then return the name */ |
208 | 35 | dummy = sc_asn1_skip_tag(ctx, &ava, &ava_len, ava[0] & SC_ASN1_TAG_PRIMITIVE, &dummy_len); |
209 | 35 | if (dummy == NULL) |
210 | 35 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA name"); |
211 | 13 | if (*name == NULL) { |
212 | 13 | *name = malloc(dummy_len); |
213 | 13 | if (*name == NULL) |
214 | 13 | LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); |
215 | 13 | *name_len = dummy_len; |
216 | 13 | } |
217 | | |
218 | 13 | *name_len = MIN(dummy_len, *name_len); |
219 | 13 | memcpy(*name, dummy, *name_len); |
220 | 13 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
221 | 13 | } |
222 | | |
223 | 78 | LOG_FUNC_RETURN(ctx, SC_ERROR_ASN1_OBJECT_NOT_FOUND); |
224 | 78 | } |
225 | | |
226 | | |
227 | | /* Get a specific extension from the cert. |
228 | | * The extension is identified by it's oid value. |
229 | | * NOTE: extensions can occur in any number or any order, which is why we |
230 | | * can't parse them with a single pass of the asn1 decoder. |
231 | | * If is_critical is supplied, then it is set to 1 if the extension is critical |
232 | | * and 0 if it is not. |
233 | | * The data in the extension is extension specific. |
234 | | * The following are common extension values: |
235 | | * Subject Key ID: struct sc_object_id type = {{2, 5, 29, 14, -1}}; |
236 | | * Key Usage: struct sc_object_id type = {{2, 5, 29, 15, -1}}; |
237 | | * Subject Alt Name: struct sc_object_id type = {{2, 5, 29, 17, -1}}; |
238 | | * Basic Constraints: struct sc_object_id type = {{2, 5, 29, 19, -1}}; |
239 | | * CRL Distribution Points: struct sc_object_id type = {{2, 5, 29, 31, -1}}; |
240 | | * Certificate Policies: struct sc_object_id type = {{2, 5, 29, 32, -1}}; |
241 | | * Extended Key Usage: struct sc_object_id type = {{2, 5, 29, 37, -1}}; |
242 | | * |
243 | | * if *ext_val is NULL, sc_pkcs15_get_extension will allocate space for ext_val. |
244 | | */ |
245 | | int |
246 | | sc_pkcs15_get_extension(struct sc_context *ctx, struct sc_pkcs15_cert *cert, |
247 | | const struct sc_object_id *type, u8 **ext_val, |
248 | | size_t *ext_val_len, int *is_critical) |
249 | 1.06k | { |
250 | 1.06k | const u8 *ext = NULL; |
251 | 1.06k | const u8 *next_ext = NULL; |
252 | 1.06k | size_t ext_len = 0; |
253 | 1.06k | size_t next_ext_len = 0; |
254 | 1.06k | struct sc_object_id oid; |
255 | 1.06k | u8 *val = NULL; |
256 | 1.06k | size_t val_len = 0; |
257 | 1.06k | int critical; |
258 | 1.06k | int r; |
259 | 1.06k | struct sc_asn1_entry asn1_cert_ext[] = { |
260 | 1.06k | { "x509v3 entry OID", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, 0, &oid, 0 }, |
261 | 1.06k | { "criticalFlag", SC_ASN1_BOOLEAN, SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, &critical, NULL }, |
262 | 1.06k | { "extensionValue",SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_ALLOC, &val, &val_len }, |
263 | 1.06k | { NULL, 0, 0, 0, NULL, NULL } |
264 | 1.06k | }; |
265 | | |
266 | 1.06k | LOG_FUNC_CALLED(ctx); |
267 | | |
268 | 1.06k | for (next_ext = cert->extensions, next_ext_len = cert->extensions_len; next_ext_len; ) { |
269 | | /* unwrap the set and point to the next ava */ |
270 | 0 | ext = sc_asn1_skip_tag(ctx, &next_ext, &next_ext_len, |
271 | 0 | SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, &ext_len); |
272 | 0 | if (ext == NULL) |
273 | 0 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "ASN.1 decoding of AVA"); |
274 | | |
275 | | /* |
276 | | * use the sc_asn1_decoder for clarity. NOTE it would be more efficient to do this by hand |
277 | | * so we avoid the many malloc/frees here, but one hopes that one day the asn1_decode will allow |
278 | | * a 'static pointer' flag that returns a const pointer to the actual asn1 space so we only need |
279 | | * to make a final copy of the extension value before we return */ |
280 | 0 | critical = 0; |
281 | 0 | r = sc_asn1_decode(ctx, asn1_cert_ext, ext, ext_len, NULL, NULL); |
282 | 0 | if (r < 0) |
283 | 0 | LOG_FUNC_RETURN(ctx, r); |
284 | | |
285 | | /* is it the RN we are looking for */ |
286 | 0 | if (sc_compare_oid(&oid, type) != 0) { |
287 | 0 | if (*ext_val == NULL) { |
288 | 0 | *ext_val = val; |
289 | 0 | val = NULL; |
290 | 0 | *ext_val_len = val_len; |
291 | | /* do not free here -- return the allocated value to caller */ |
292 | 0 | } |
293 | 0 | else { |
294 | 0 | *ext_val_len = MIN(*ext_val_len, val_len); |
295 | 0 | if (val) { |
296 | 0 | memcpy(*ext_val, val, *ext_val_len); |
297 | 0 | free(val); |
298 | 0 | } |
299 | 0 | } |
300 | |
|
301 | 0 | if (is_critical) |
302 | 0 | *is_critical = critical; |
303 | |
|
304 | 0 | r = (int)val_len; |
305 | 0 | LOG_FUNC_RETURN(ctx, r); |
306 | 0 | } |
307 | 0 | if (val) { |
308 | 0 | free(val); |
309 | 0 | val = NULL; |
310 | 0 | } |
311 | 0 | } |
312 | 1.06k | if (val) |
313 | 0 | free(val); |
314 | | |
315 | 1.06k | LOG_FUNC_RETURN(ctx, SC_ERROR_ASN1_OBJECT_NOT_FOUND); |
316 | 1.06k | } |
317 | | |
318 | | /* |
319 | | * Get an extension whose value is a bit string. These include keyUsage and extendedKeyUsage. |
320 | | * See above for the other parameters. |
321 | | */ |
322 | | int |
323 | | sc_pkcs15_get_bitstring_extension(struct sc_context *ctx, |
324 | | struct sc_pkcs15_cert *cert, const struct sc_object_id *type, |
325 | | unsigned int *value, int *is_critical) |
326 | 1.06k | { |
327 | 1.06k | int r; |
328 | 1.06k | u8 *bit_string = NULL; |
329 | 1.06k | size_t bit_string_len=0, val_len = sizeof(*value); |
330 | 1.06k | struct sc_asn1_entry asn1_bit_string[] = { |
331 | 1.06k | { "bitString", SC_ASN1_BIT_FIELD, SC_ASN1_TAG_BIT_STRING, 0, value, &val_len }, |
332 | 1.06k | { NULL, 0, 0, 0, NULL, NULL } |
333 | 1.06k | }; |
334 | | |
335 | 1.06k | LOG_FUNC_CALLED(ctx); |
336 | | |
337 | 1.06k | r = sc_pkcs15_get_extension(ctx, cert, type, &bit_string, &bit_string_len, is_critical); |
338 | 1.06k | LOG_TEST_RET(ctx, r, "Get extension error"); |
339 | | |
340 | 0 | r = sc_asn1_decode(ctx, asn1_bit_string, bit_string, bit_string_len, NULL, NULL); |
341 | 0 | free(bit_string); |
342 | 0 | LOG_TEST_RET(ctx, r, "Decoding extension bit string"); |
343 | | |
344 | 0 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
345 | 0 | } |
346 | | |
347 | | |
348 | | int |
349 | | sc_pkcs15_pubkey_from_cert(struct sc_context *ctx, |
350 | | struct sc_pkcs15_der *cert_blob, struct sc_pkcs15_pubkey **out) |
351 | 29 | { |
352 | 29 | int rv; |
353 | 29 | struct sc_pkcs15_cert * cert; |
354 | | |
355 | 29 | cert = calloc(1, sizeof(struct sc_pkcs15_cert)); |
356 | 29 | if (cert == NULL) |
357 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
358 | | |
359 | 29 | rv = parse_x509_cert(ctx, cert_blob, cert); |
360 | | |
361 | 29 | *out = cert->key; |
362 | 29 | cert->key = NULL; |
363 | 29 | sc_pkcs15_free_certificate(cert); |
364 | | |
365 | 29 | LOG_FUNC_RETURN(ctx, rv); |
366 | 29 | } |
367 | | |
368 | | |
369 | | int |
370 | | sc_pkcs15_read_certificate(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_cert_info *info, |
371 | | int private_obj, struct sc_pkcs15_cert **cert_out) |
372 | 4.36k | { |
373 | 4.36k | struct sc_context *ctx = NULL; |
374 | 4.36k | struct sc_pkcs15_cert *cert = NULL; |
375 | 4.36k | struct sc_pkcs15_der der; |
376 | 4.36k | int r; |
377 | | |
378 | 4.36k | if (p15card == NULL || info == NULL || cert_out == NULL) { |
379 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
380 | 0 | } |
381 | 4.36k | ctx = p15card->card->ctx; |
382 | 4.36k | LOG_FUNC_CALLED(ctx); |
383 | | |
384 | 4.36k | if (info->value.len && info->value.value) { |
385 | 3.05k | sc_der_copy(&der, &info->value); |
386 | 3.05k | } |
387 | 1.30k | else if (info->path.len) { |
388 | 1.30k | r = sc_pkcs15_read_file(p15card, &info->path, &der.value, &der.len, private_obj); |
389 | 1.30k | LOG_TEST_RET(ctx, r, "Unable to read certificate file."); |
390 | 1.30k | } |
391 | 3 | else { |
392 | 3 | LOG_FUNC_RETURN(ctx, SC_ERROR_OBJECT_NOT_FOUND); |
393 | 3 | } |
394 | | |
395 | 3.37k | cert = malloc(sizeof(struct sc_pkcs15_cert)); |
396 | 3.37k | if (cert == NULL) { |
397 | 0 | free(der.value); |
398 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); |
399 | 0 | } |
400 | 3.37k | memset(cert, 0, sizeof(struct sc_pkcs15_cert)); |
401 | 3.37k | if (parse_x509_cert(ctx, &der, cert)) { |
402 | 1.25k | free(der.value); |
403 | 1.25k | sc_pkcs15_free_certificate(cert); |
404 | 1.25k | LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ASN1_OBJECT); |
405 | 1.25k | } |
406 | 2.12k | free(der.value); |
407 | | |
408 | 2.12k | *cert_out = cert; |
409 | 2.12k | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
410 | 2.12k | } |
411 | | |
412 | | |
413 | | static const struct sc_asn1_entry c_asn1_cred_ident[] = { |
414 | | { "idType", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL }, |
415 | | { "idValue", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL }, |
416 | | { NULL, 0, 0, 0, NULL, NULL } |
417 | | }; |
418 | | static const struct sc_asn1_entry c_asn1_com_cert_attr[] = { |
419 | | { "iD", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL }, |
420 | | { "authority", SC_ASN1_BOOLEAN, SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, NULL, NULL }, |
421 | | { "identifier", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
422 | | /* FIXME: Add rest of the optional fields */ |
423 | | { NULL, 0, 0, 0, NULL, NULL } |
424 | | }; |
425 | | static const struct sc_asn1_entry c_asn1_x509_cert_value_choice[] = { |
426 | | { "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
427 | | { "direct", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 0 | SC_ASN1_CONS, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL }, |
428 | | { NULL, 0, 0, 0, NULL, NULL } |
429 | | }; |
430 | | static const struct sc_asn1_entry c_asn1_x509_cert_attr[] = { |
431 | | { "value", SC_ASN1_CHOICE, 0, 0, NULL, NULL }, |
432 | | { NULL, 0, 0, 0, NULL, NULL } |
433 | | }; |
434 | | static const struct sc_asn1_entry c_asn1_type_cert_attr[] = { |
435 | | { "x509CertificateAttributes", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
436 | | { NULL, 0, 0, 0, NULL, NULL } |
437 | | }; |
438 | | static const struct sc_asn1_entry c_asn1_cert[] = { |
439 | | { "x509Certificate", SC_ASN1_PKCS15_OBJECT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
440 | | { NULL, 0, 0, 0, NULL, NULL } |
441 | | }; |
442 | | |
443 | | |
444 | | int |
445 | | sc_pkcs15_decode_cdf_entry(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *obj, |
446 | | const u8 ** buf, size_t *buflen) |
447 | 5 | { |
448 | 5 | sc_context_t *ctx = p15card->card->ctx; |
449 | 5 | struct sc_pkcs15_cert_info info; |
450 | 5 | struct sc_asn1_entry asn1_cred_ident[3], asn1_com_cert_attr[4], |
451 | 5 | asn1_x509_cert_attr[2], asn1_type_cert_attr[2], |
452 | 5 | asn1_cert[2], asn1_x509_cert_value_choice[3]; |
453 | 5 | struct sc_asn1_pkcs15_object cert_obj = { |
454 | 5 | obj, asn1_com_cert_attr, NULL, |
455 | 5 | asn1_type_cert_attr }; |
456 | 5 | sc_pkcs15_der_t *der = &info.value; |
457 | 5 | u8 id_value[128]; |
458 | 5 | int id_type; |
459 | 5 | size_t id_value_len = sizeof(id_value); |
460 | 5 | int r; |
461 | | |
462 | 5 | sc_copy_asn1_entry(c_asn1_cred_ident, asn1_cred_ident); |
463 | 5 | sc_copy_asn1_entry(c_asn1_com_cert_attr, asn1_com_cert_attr); |
464 | 5 | sc_copy_asn1_entry(c_asn1_x509_cert_attr, asn1_x509_cert_attr); |
465 | 5 | sc_copy_asn1_entry(c_asn1_x509_cert_value_choice, asn1_x509_cert_value_choice); |
466 | 5 | sc_copy_asn1_entry(c_asn1_type_cert_attr, asn1_type_cert_attr); |
467 | 5 | sc_copy_asn1_entry(c_asn1_cert, asn1_cert); |
468 | | |
469 | 5 | sc_format_asn1_entry(asn1_cred_ident + 0, &id_type, NULL, 0); |
470 | 5 | sc_format_asn1_entry(asn1_cred_ident + 1, &id_value, &id_value_len, 0); |
471 | 5 | sc_format_asn1_entry(asn1_com_cert_attr + 0, &info.id, NULL, 0); |
472 | 5 | sc_format_asn1_entry(asn1_com_cert_attr + 1, &info.authority, NULL, 0); |
473 | 5 | sc_format_asn1_entry(asn1_com_cert_attr + 2, asn1_cred_ident, NULL, 0); |
474 | 5 | sc_format_asn1_entry(asn1_x509_cert_attr + 0, asn1_x509_cert_value_choice, NULL, 0); |
475 | 5 | sc_format_asn1_entry(asn1_x509_cert_value_choice + 0, &info.path, NULL, 0); |
476 | 5 | sc_format_asn1_entry(asn1_x509_cert_value_choice + 1, &der->value, &der->len, 0); |
477 | 5 | sc_format_asn1_entry(asn1_type_cert_attr + 0, asn1_x509_cert_attr, NULL, 0); |
478 | 5 | sc_format_asn1_entry(asn1_cert + 0, &cert_obj, NULL, 0); |
479 | | |
480 | | /* Fill in defaults */ |
481 | 5 | memset(&info, 0, sizeof(info)); |
482 | 5 | info.authority = 0; |
483 | | |
484 | 5 | r = sc_asn1_decode(ctx, asn1_cert, *buf, *buflen, buf, buflen); |
485 | | /* In case of error, trash the cert value (direct coding) */ |
486 | 5 | if (r < 0 && der->value) |
487 | 0 | free(der->value); |
488 | 5 | if (r == SC_ERROR_ASN1_END_OF_CONTENTS) |
489 | 1 | return r; |
490 | 4 | LOG_TEST_RET(ctx, r, "ASN.1 decoding failed"); |
491 | | |
492 | 0 | if (!p15card->app || !p15card->app->ddo.aid.len) { |
493 | 0 | if (!p15card->file_app) { |
494 | 0 | free(der->value); |
495 | 0 | return SC_ERROR_INTERNAL; |
496 | 0 | } |
497 | 0 | r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &info.path); |
498 | 0 | LOG_TEST_RET(ctx, r, "Cannot make absolute path"); |
499 | 0 | } |
500 | 0 | else { |
501 | 0 | info.path.aid = p15card->app->ddo.aid; |
502 | 0 | } |
503 | 0 | sc_log(ctx, "Certificate path '%s'", sc_print_path(&info.path)); |
504 | |
|
505 | 0 | switch (p15card->opts.pin_protected_certificate) { |
506 | 0 | case SC_PKCS15_CARD_OPTS_PRIV_CERT_DECLASSIFY: |
507 | 0 | sc_log(ctx, "Declassifying certificate"); |
508 | 0 | obj->flags &= ~SC_PKCS15_CO_FLAG_PRIVATE; |
509 | 0 | break; |
510 | 0 | case SC_PKCS15_CARD_OPTS_PRIV_CERT_IGNORE: |
511 | 0 | sc_log(ctx, "Ignoring certificate"); |
512 | 0 | free(der->value); |
513 | 0 | return 0; |
514 | 0 | } |
515 | | |
516 | 0 | obj->type = SC_PKCS15_TYPE_CERT_X509; |
517 | 0 | obj->data = malloc(sizeof(info)); |
518 | 0 | if (obj->data == NULL) |
519 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); |
520 | 0 | memcpy(obj->data, &info, sizeof(info)); |
521 | |
|
522 | 0 | return 0; |
523 | 0 | } |
524 | | |
525 | | |
526 | | int |
527 | | sc_pkcs15_encode_cdf_entry(sc_context_t *ctx, const struct sc_pkcs15_object *obj, |
528 | | u8 **buf, size_t *bufsize) |
529 | 0 | { |
530 | 0 | struct sc_asn1_entry asn1_cred_ident[3], asn1_com_cert_attr[4], |
531 | 0 | asn1_x509_cert_attr[2], asn1_type_cert_attr[2], |
532 | 0 | asn1_cert[2], asn1_x509_cert_value_choice[3]; |
533 | 0 | struct sc_pkcs15_cert_info *infop = (sc_pkcs15_cert_info_t *) obj->data; |
534 | 0 | sc_pkcs15_der_t *der = &infop->value; |
535 | 0 | struct sc_asn1_pkcs15_object cert_obj = { (struct sc_pkcs15_object *) obj, |
536 | 0 | asn1_com_cert_attr, NULL, |
537 | 0 | asn1_type_cert_attr }; |
538 | 0 | int r; |
539 | |
|
540 | 0 | sc_copy_asn1_entry(c_asn1_cred_ident, asn1_cred_ident); |
541 | 0 | sc_copy_asn1_entry(c_asn1_com_cert_attr, asn1_com_cert_attr); |
542 | 0 | sc_copy_asn1_entry(c_asn1_x509_cert_attr, asn1_x509_cert_attr); |
543 | 0 | sc_copy_asn1_entry(c_asn1_x509_cert_value_choice, asn1_x509_cert_value_choice); |
544 | 0 | sc_copy_asn1_entry(c_asn1_type_cert_attr, asn1_type_cert_attr); |
545 | 0 | sc_copy_asn1_entry(c_asn1_cert, asn1_cert); |
546 | |
|
547 | 0 | sc_format_asn1_entry(asn1_com_cert_attr + 0, (void *) &infop->id, NULL, 1); |
548 | 0 | if (infop->authority) |
549 | 0 | sc_format_asn1_entry(asn1_com_cert_attr + 1, (void *) &infop->authority, NULL, 1); |
550 | 0 | if (infop->path.len || !der->value) { |
551 | 0 | sc_format_asn1_entry(asn1_x509_cert_value_choice + 0, &infop->path, NULL, 1); |
552 | 0 | } else { |
553 | 0 | sc_format_asn1_entry(asn1_x509_cert_value_choice + 1, der->value, &der->len, 1); |
554 | 0 | } |
555 | 0 | sc_format_asn1_entry(asn1_type_cert_attr + 0, &asn1_x509_cert_value_choice, NULL, 1); |
556 | 0 | sc_format_asn1_entry(asn1_cert + 0, (void *) &cert_obj, NULL, 1); |
557 | |
|
558 | 0 | r = sc_asn1_encode(ctx, asn1_cert, buf, bufsize); |
559 | |
|
560 | 0 | return r; |
561 | 0 | } |
562 | | |
563 | | /* Only certain usages are valid for a given algorithm, return all the usages |
564 | | * that the algorithm supports so we can use it as a filter for all |
565 | | * the public and private key usages |
566 | | */ |
567 | | static unsigned int |
568 | | sc_pkcs15_alg_flags_from_algorithm(unsigned long algorithm) |
569 | 0 | { |
570 | 0 | switch (algorithm) { |
571 | 0 | case SC_ALGORITHM_RSA: |
572 | 0 | return SC_PKCS15_PRKEY_USAGE_ENCRYPT | SC_PKCS15_PRKEY_USAGE_WRAP | |
573 | 0 | SC_PKCS15_PRKEY_USAGE_VERIFY | SC_PKCS15_PRKEY_USAGE_VERIFYRECOVER | |
574 | 0 | SC_PKCS15_PRKEY_USAGE_DECRYPT | SC_PKCS15_PRKEY_USAGE_UNWRAP | |
575 | 0 | SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_SIGNRECOVER | |
576 | 0 | SC_PKCS15_PRKEY_USAGE_NONREPUDIATION; |
577 | | #ifdef SC_ALGORITHM_DH |
578 | | case SC_ALGORITHM_DH: |
579 | | return SC_PKCS15_PRKEY_USAGE_DERIVE ; |
580 | | #endif |
581 | 0 | case SC_ALGORITHM_EC: |
582 | 0 | return SC_PKCS15_PRKEY_USAGE_DERIVE | SC_PKCS15_PRKEY_USAGE_VERIFY| |
583 | 0 | SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_NONREPUDIATION; |
584 | 0 | case SC_ALGORITHM_GOSTR3410: |
585 | 0 | return SC_PKCS15_PRKEY_USAGE_DERIVE | SC_PKCS15_PRKEY_USAGE_VERIFY| |
586 | 0 | SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_NONREPUDIATION; |
587 | 0 | } |
588 | 0 | return 0; |
589 | 0 | } |
590 | | |
591 | | /* These are the cert key usage bits that map to various PKCS #11 (and thus PKCS #15) flags */ |
592 | | #define SC_PKCS15_X509_USAGE_SIGNATURE \ |
593 | 0 | (SC_X509_DIGITAL_SIGNATURE | \ |
594 | 0 | SC_X509_NON_REPUDIATION | \ |
595 | 0 | SC_X509_KEY_CERT_SIGN | \ |
596 | 0 | SC_X509_CRL_SIGN) |
597 | | #define SC_PKCS15_X509_USAGE_DERIVE \ |
598 | 0 | (SC_X509_KEY_ENCIPHERMENT | \ |
599 | 0 | SC_X509_KEY_AGREEMENT) |
600 | | #define SC_PKCS15_X509_USAGE_UNWRAP \ |
601 | 0 | (SC_X509_KEY_ENCIPHERMENT | \ |
602 | 0 | SC_X509_KEY_AGREEMENT) |
603 | | #define SC_PKCS15_X509_USAGE_DECRYPT \ |
604 | 0 | (SC_X509_DATA_ENCIPHERMENT | \ |
605 | 0 | SC_X509_ENCIPHER_ONLY) |
606 | | #define SC_PKCS15_X509_USAGE_NONREPUDIATION \ |
607 | 0 | SC_X509_NON_REPUDIATION |
608 | | |
609 | | /* map a cert usage and algorithm to public and private key usages */ |
610 | | int |
611 | | sc_pkcs15_map_usage(unsigned int cert_usage, unsigned long algorithm, |
612 | | unsigned int *pub_usage_ptr, unsigned int *pr_usage_ptr, |
613 | | int allow_nonrepudiation) |
614 | 0 | { |
615 | 0 | unsigned int pub_usage = 0, pr_usage = 0; |
616 | 0 | unsigned int alg_flags = sc_pkcs15_alg_flags_from_algorithm(algorithm); |
617 | |
|
618 | 0 | if (cert_usage & SC_PKCS15_X509_USAGE_SIGNATURE) { |
619 | 0 | pub_usage |= SC_PKCS15_PRKEY_USAGE_VERIFY|SC_PKCS15_PRKEY_USAGE_VERIFYRECOVER; |
620 | 0 | pr_usage |= SC_PKCS15_PRKEY_USAGE_SIGN|SC_PKCS15_PRKEY_USAGE_SIGNRECOVER; |
621 | 0 | } |
622 | 0 | if (cert_usage & SC_PKCS15_X509_USAGE_DERIVE) { |
623 | 0 | pub_usage |= SC_PKCS15_PRKEY_USAGE_DERIVE; |
624 | 0 | pr_usage |= SC_PKCS15_PRKEY_USAGE_DERIVE; |
625 | 0 | } |
626 | 0 | if (cert_usage & (SC_PKCS15_X509_USAGE_DECRYPT|SC_PKCS15_X509_USAGE_UNWRAP)) { |
627 | 0 | pub_usage |= SC_PKCS15_PRKEY_USAGE_ENCRYPT; |
628 | 0 | pr_usage |= SC_PKCS15_PRKEY_USAGE_DECRYPT; |
629 | 0 | } |
630 | 0 | if (allow_nonrepudiation && (cert_usage & SC_PKCS15_X509_USAGE_NONREPUDIATION)) { |
631 | 0 | pub_usage |= SC_PKCS15_PRKEY_USAGE_NONREPUDIATION; |
632 | 0 | pr_usage |= SC_PKCS15_PRKEY_USAGE_NONREPUDIATION; |
633 | 0 | } |
634 | | /* filter usages algorithm */ |
635 | 0 | if (pub_usage_ptr) { |
636 | 0 | *pub_usage_ptr = pub_usage & alg_flags; |
637 | 0 | } |
638 | 0 | if (pr_usage_ptr) { |
639 | 0 | *pr_usage_ptr = pr_usage & alg_flags; |
640 | 0 | } |
641 | 0 | return SC_SUCCESS; |
642 | 0 | } |
643 | | |
644 | | void |
645 | | sc_pkcs15_free_certificate(struct sc_pkcs15_cert *cert) |
646 | 3.40k | { |
647 | 3.40k | if (cert == NULL) { |
648 | 0 | return; |
649 | 0 | } |
650 | | |
651 | 3.40k | sc_pkcs15_free_pubkey(cert->key); |
652 | 3.40k | free(cert->subject); |
653 | 3.40k | free(cert->issuer); |
654 | 3.40k | free(cert->serial); |
655 | 3.40k | free(cert->data.value); |
656 | 3.40k | free(cert->extensions); |
657 | 3.40k | free(cert); |
658 | 3.40k | } |
659 | | |
660 | | |
661 | | void |
662 | | sc_pkcs15_free_cert_info(sc_pkcs15_cert_info_t *cert) |
663 | 2.36k | { |
664 | 2.36k | if (!cert) |
665 | 0 | return; |
666 | 2.36k | free(cert->value.value); |
667 | 2.36k | free(cert); |
668 | 2.36k | } |