Updated x509 plugin to the new builder API
[strongswan.git] / src / libstrongswan / plugins / x509 / x509_cert.c
1 /*
2 * Copyright (C) 2000 Andreas Hess, Patric Lichtsteiner, Roger Wegmann
3 * Copyright (C) 2001 Marco Bertossa, Andreas Schleiss
4 * Copyright (C) 2002 Mario Strasser
5 * Copyright (C) 2000-2006 Andreas Steffen
6 * Copyright (C) 2006-2009 Martin Willi
7 * Copyright (C) 2008 Tobias Brunner
8 * Hochschule fuer Technik Rapperswil
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 */
20
21 #define _GNU_SOURCE
22
23 #include "x509_cert.h"
24
25 #include <sys/stat.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include <library.h>
32 #include <debug.h>
33 #include <asn1/oid.h>
34 #include <asn1/asn1.h>
35 #include <asn1/asn1_parser.h>
36 #include <crypto/hashers/hasher.h>
37 #include <credentials/keys/private_key.h>
38 #include <utils/linked_list.h>
39 #include <utils/identification.h>
40
41 /**
42 * Different kinds of generalNames
43 */
44 typedef enum {
45 GN_OTHER_NAME = 0,
46 GN_RFC822_NAME = 1,
47 GN_DNS_NAME = 2,
48 GN_X400_ADDRESS = 3,
49 GN_DIRECTORY_NAME = 4,
50 GN_EDI_PARTY_NAME = 5,
51 GN_URI = 6,
52 GN_IP_ADDRESS = 7,
53 GN_REGISTERED_ID = 8,
54 } generalNames_t;
55
56
57 typedef struct private_x509_cert_t private_x509_cert_t;
58
59 /**
60 * Private data of a x509_cert_t object.
61 */
62 struct private_x509_cert_t {
63 /**
64 * Public interface for this certificate.
65 */
66 x509_cert_t public;
67
68 /**
69 * X.509 certificate encoding in ASN.1 DER format
70 */
71 chunk_t encoding;
72
73 /**
74 * SHA1 hash of the DER encoding of this X.509 certificate
75 */
76 chunk_t encoding_hash;
77
78 /**
79 * X.509 certificate body over which signature is computed
80 */
81 chunk_t tbsCertificate;
82
83 /**
84 * Version of the X.509 certificate
85 */
86 u_int version;
87
88 /**
89 * Serial number of the X.509 certificate
90 */
91 chunk_t serialNumber;
92
93 /**
94 * ID representing the certificate issuer
95 */
96 identification_t *issuer;
97
98 /**
99 * Start time of certificate validity
100 */
101 time_t notBefore;
102
103 /**
104 * End time of certificate validity
105 */
106 time_t notAfter;
107
108 /**
109 * ID representing the certificate subject
110 */
111 identification_t *subject;
112
113 /**
114 * List of subjectAltNames as identification_t
115 */
116 linked_list_t *subjectAltNames;
117
118 /**
119 * List of crlDistributionPoints as allocated char*
120 */
121 linked_list_t *crl_uris;
122
123 /**
124 * List ocspAccessLocations as identification_t
125 */
126 linked_list_t *ocsp_uris;
127
128 /**
129 * certificates embedded public key
130 */
131 public_key_t *public_key;
132
133 /**
134 * Subject Key Identifier
135 */
136 chunk_t subjectKeyID;
137
138 /**
139 * Authority Key Identifier
140 */
141 chunk_t authKeyIdentifier;
142
143 /**
144 * Authority Key Serial Number
145 */
146 chunk_t authKeySerialNumber;
147
148 /**
149 * x509 constraints and other flags
150 */
151 x509_flag_t flags;
152
153 /**
154 * Signature algorithm
155 */
156 int algorithm;
157
158 /**
159 * Signature
160 */
161 chunk_t signature;
162
163 /**
164 * Certificate parsed from blob/file?
165 */
166 bool parsed;
167
168 /**
169 * reference count
170 */
171 refcount_t ref;
172 };
173
174 static u_char ASN1_sAN_oid_buf[] = {
175 0x06, 0x03, 0x55, 0x1D, 0x11
176 };
177 static const chunk_t ASN1_subjectAltName_oid = chunk_from_buf(ASN1_sAN_oid_buf);
178
179 /**
180 * ASN.1 definition of a basicConstraints extension
181 */
182 static const asn1Object_t basicConstraintsObjects[] = {
183 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
184 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
185 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
186 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
187 { 0, "exit", ASN1_EOC, ASN1_EXIT }
188 };
189 #define BASIC_CONSTRAINTS_CA 1
190
191 /**
192 * Extracts the basicConstraints extension
193 */
194 static bool parse_basicConstraints(chunk_t blob, int level0)
195 {
196 asn1_parser_t *parser;
197 chunk_t object;
198 int objectID;
199 bool isCA = FALSE;
200
201 parser = asn1_parser_create(basicConstraintsObjects, blob);
202 parser->set_top_level(parser, level0);
203
204 while (parser->iterate(parser, &objectID, &object))
205 {
206 if (objectID == BASIC_CONSTRAINTS_CA)
207 {
208 isCA = object.len && *object.ptr;
209 DBG2(" %s", isCA ? "TRUE" : "FALSE");
210 }
211 }
212 parser->destroy(parser);
213
214 return isCA;
215 }
216
217 /**
218 * ASN.1 definition of otherName
219 */
220 static const asn1Object_t otherNameObjects[] = {
221 {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */
222 {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY }, /* 1 */
223 {0, "exit", ASN1_EOC, ASN1_EXIT }
224 };
225 #define ON_OBJ_ID_TYPE 0
226 #define ON_OBJ_VALUE 1
227
228 /**
229 * Extracts an otherName
230 */
231 static bool parse_otherName(chunk_t blob, int level0)
232 {
233 asn1_parser_t *parser;
234 chunk_t object;
235 int objectID;
236 int oid = OID_UNKNOWN;
237 bool success = FALSE;
238
239 parser = asn1_parser_create(otherNameObjects, blob);
240 parser->set_top_level(parser, level0);
241
242 while (parser->iterate(parser, &objectID, &object))
243 {
244 switch (objectID)
245 {
246 case ON_OBJ_ID_TYPE:
247 oid = asn1_known_oid(object);
248 break;
249 case ON_OBJ_VALUE:
250 if (oid == OID_XMPP_ADDR)
251 {
252 if (!asn1_parse_simple_object(&object, ASN1_UTF8STRING,
253 parser->get_level(parser)+1, "xmppAddr"))
254 {
255 goto end;
256 }
257 }
258 break;
259 default:
260 break;
261 }
262 }
263 success = parser->success(parser);
264
265 end:
266 parser->destroy(parser);
267 return success;
268 }
269
270 /**
271 * ASN.1 definition of generalName
272 */
273 static const asn1Object_t generalNameObjects[] = {
274 { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */
275 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */
276 { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */
277 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */
278 { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */
279 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
280 { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */
281 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
282 { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */
283 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
284 { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */
285 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
286 { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */
287 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */
288 { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */
289 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */
290 { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */
291 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 17 */
292 { 0, "exit", ASN1_EOC, ASN1_EXIT }
293 };
294 #define GN_OBJ_OTHER_NAME 0
295 #define GN_OBJ_RFC822_NAME 2
296 #define GN_OBJ_DNS_NAME 4
297 #define GN_OBJ_X400_ADDRESS 6
298 #define GN_OBJ_DIRECTORY_NAME 8
299 #define GN_OBJ_EDI_PARTY_NAME 10
300 #define GN_OBJ_URI 12
301 #define GN_OBJ_IP_ADDRESS 14
302 #define GN_OBJ_REGISTERED_ID 16
303
304 /**
305 * Extracts a generalName
306 */
307 static identification_t *parse_generalName(chunk_t blob, int level0)
308 {
309 asn1_parser_t *parser;
310 chunk_t object;
311 int objectID ;
312
313 identification_t *gn = NULL;
314
315 parser = asn1_parser_create(generalNameObjects, blob);
316 parser->set_top_level(parser, level0);
317
318 while (parser->iterate(parser, &objectID, &object))
319 {
320 id_type_t id_type = ID_ANY;
321
322 switch (objectID)
323 {
324 case GN_OBJ_RFC822_NAME:
325 id_type = ID_RFC822_ADDR;
326 break;
327 case GN_OBJ_DNS_NAME:
328 id_type = ID_FQDN;
329 break;
330 case GN_OBJ_URI:
331 id_type = ID_DER_ASN1_GN_URI;
332 break;
333 case GN_OBJ_DIRECTORY_NAME:
334 id_type = ID_DER_ASN1_DN;
335 break;
336 case GN_OBJ_IP_ADDRESS:
337 id_type = ID_IPV4_ADDR;
338 break;
339 case GN_OBJ_OTHER_NAME:
340 if (!parse_otherName(object, parser->get_level(parser)+1))
341 {
342 goto end;
343 }
344 break;
345 case GN_OBJ_X400_ADDRESS:
346 case GN_OBJ_EDI_PARTY_NAME:
347 case GN_OBJ_REGISTERED_ID:
348 default:
349 break;
350 }
351 if (id_type != ID_ANY)
352 {
353 gn = identification_create_from_encoding(id_type, object);
354 DBG2(" '%Y'", gn);
355 goto end;
356 }
357 }
358
359 end:
360 parser->destroy(parser);
361 return gn;
362 }
363
364 /**
365 * ASN.1 definition of generalNames
366 */
367 static const asn1Object_t generalNamesObjects[] = {
368 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
369 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
370 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
371 { 0, "exit", ASN1_EOC, ASN1_EXIT }
372 };
373 #define GENERAL_NAMES_GN 1
374
375 /**
376 * Extracts one or several GNs and puts them into a chained list
377 */
378 void x509_parse_generalNames(chunk_t blob, int level0, bool implicit, linked_list_t *list)
379 {
380 asn1_parser_t *parser;
381 chunk_t object;
382 int objectID;
383
384 parser = asn1_parser_create(generalNamesObjects, blob);
385 parser->set_top_level(parser, level0);
386 parser->set_flags(parser, implicit, FALSE);
387
388 while (parser->iterate(parser, &objectID, &object))
389 {
390 if (objectID == GENERAL_NAMES_GN)
391 {
392 identification_t *gn = parse_generalName(object,
393 parser->get_level(parser)+1);
394
395 if (gn)
396 {
397 list->insert_last(list, (void *)gn);
398 }
399 }
400 }
401 parser->destroy(parser);
402 }
403
404 /**
405 * ASN.1 definition of a authorityKeyIdentifier extension
406 */
407 static const asn1Object_t authKeyIdentifierObjects[] = {
408 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
409 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_BODY }, /* 1 */
410 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
411 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
412 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
413 { 1, "authorityCertSerialNumber", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
414 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 6 */
415 { 0, "exit", ASN1_EOC, ASN1_EXIT }
416 };
417 #define AUTH_KEY_ID_KEY_ID 1
418 #define AUTH_KEY_ID_CERT_ISSUER 3
419 #define AUTH_KEY_ID_CERT_SERIAL 5
420
421 /**
422 * Extracts an authoritykeyIdentifier
423 */
424 chunk_t x509_parse_authorityKeyIdentifier(chunk_t blob, int level0,
425 chunk_t *authKeySerialNumber)
426 {
427 asn1_parser_t *parser;
428 chunk_t object;
429 int objectID;
430 chunk_t authKeyIdentifier = chunk_empty;
431
432 *authKeySerialNumber = chunk_empty;
433
434 parser = asn1_parser_create(authKeyIdentifierObjects, blob);
435 parser->set_top_level(parser, level0);
436
437 while (parser->iterate(parser, &objectID, &object))
438 {
439 switch (objectID)
440 {
441 case AUTH_KEY_ID_KEY_ID:
442 authKeyIdentifier = chunk_clone(object);
443 break;
444 case AUTH_KEY_ID_CERT_ISSUER:
445 /* TODO: x509_parse_generalNames(object, level+1, TRUE); */
446 break;
447 case AUTH_KEY_ID_CERT_SERIAL:
448 *authKeySerialNumber = object;
449 break;
450 default:
451 break;
452 }
453 }
454 parser->destroy(parser);
455 return authKeyIdentifier;
456 }
457
458 /**
459 * ASN.1 definition of a authorityInfoAccess extension
460 */
461 static const asn1Object_t authInfoAccessObjects[] = {
462 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
463 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
464 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
465 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
466 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
467 { 0, "exit", ASN1_EOC, ASN1_EXIT }
468 };
469 #define AUTH_INFO_ACCESS_METHOD 2
470 #define AUTH_INFO_ACCESS_LOCATION 3
471
472 /**
473 * Extracts an authorityInfoAcess location
474 */
475 static void parse_authorityInfoAccess(chunk_t blob, int level0,
476 private_x509_cert_t *this)
477 {
478 asn1_parser_t *parser;
479 chunk_t object;
480 int objectID;
481 int accessMethod = OID_UNKNOWN;
482
483 parser = asn1_parser_create(authInfoAccessObjects, blob);
484 parser->set_top_level(parser, level0);
485
486 while (parser->iterate(parser, &objectID, &object))
487 {
488 switch (objectID)
489 {
490 case AUTH_INFO_ACCESS_METHOD:
491 accessMethod = asn1_known_oid(object);
492 break;
493 case AUTH_INFO_ACCESS_LOCATION:
494 {
495 switch (accessMethod)
496 {
497 case OID_OCSP:
498 case OID_CA_ISSUERS:
499 {
500 identification_t *id;
501 char *uri;
502
503 id = parse_generalName(object,
504 parser->get_level(parser)+1);
505 if (id == NULL)
506 {
507 /* parsing went wrong - abort */
508 goto end;
509 }
510 DBG2(" '%Y'", id);
511 if (accessMethod == OID_OCSP &&
512 asprintf(&uri, "%Y", id) > 0)
513 {
514 this->ocsp_uris->insert_last(this->ocsp_uris, uri);
515 }
516 id->destroy(id);
517 }
518 break;
519 default:
520 /* unkown accessMethod, ignoring */
521 break;
522 }
523 break;
524 }
525 default:
526 break;
527 }
528 }
529
530 end:
531 parser->destroy(parser);
532 }
533
534 /**
535 * ASN.1 definition of a extendedKeyUsage extension
536 */
537 static const asn1Object_t extendedKeyUsageObjects[] = {
538 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
539 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
540 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
541 { 0, "exit", ASN1_EOC, ASN1_EXIT }
542 };
543 #define EXT_KEY_USAGE_PURPOSE_ID 1
544
545 /**
546 * Extracts extendedKeyUsage OIDs - currently only OCSP_SIGING is returned
547 */
548 static bool parse_extendedKeyUsage(chunk_t blob, int level0)
549 {
550 asn1_parser_t *parser;
551 chunk_t object;
552 int objectID;
553 bool ocsp_signing = FALSE;
554
555 parser = asn1_parser_create(extendedKeyUsageObjects, blob);
556 parser->set_top_level(parser, level0);
557
558 while (parser->iterate(parser, &objectID, &object))
559 {
560 if (objectID == EXT_KEY_USAGE_PURPOSE_ID &&
561 asn1_known_oid(object) == OID_OCSP_SIGNING)
562 {
563 ocsp_signing = TRUE;
564 }
565 }
566 parser->destroy(parser);
567 return ocsp_signing;
568 }
569
570 /**
571 * ASN.1 definition of crlDistributionPoints
572 */
573 static const asn1Object_t crlDistributionPointsObjects[] = {
574 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
575 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
576 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
577 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
578 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
579 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
580 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
581 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
582 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
583 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
584 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
585 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
586 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
587 { 0, "exit", ASN1_EOC, ASN1_EXIT }
588 };
589 #define CRL_DIST_POINTS_FULLNAME 3
590
591 /**
592 * Extracts one or several crlDistributionPoints into a list
593 */
594 static void parse_crlDistributionPoints(chunk_t blob, int level0,
595 private_x509_cert_t *this)
596 {
597 asn1_parser_t *parser;
598 chunk_t object;
599 int objectID;
600 linked_list_t *list = linked_list_create();
601
602 parser = asn1_parser_create(crlDistributionPointsObjects, blob);
603 parser->set_top_level(parser, level0);
604
605 while (parser->iterate(parser, &objectID, &object))
606 {
607 if (objectID == CRL_DIST_POINTS_FULLNAME)
608 {
609 identification_t *id;
610
611 /* append extracted generalNames to existing chained list */
612 x509_parse_generalNames(object, parser->get_level(parser)+1,
613 TRUE, list);
614
615 while (list->remove_last(list, (void**)&id) == SUCCESS)
616 {
617 char *uri;
618
619 if (asprintf(&uri, "%Y", id) > 0)
620 {
621 this->crl_uris->insert_last(this->crl_uris, uri);
622 }
623 id->destroy(id);
624 }
625 }
626 }
627 parser->destroy(parser);
628 list->destroy(list);
629 }
630
631 /**
632 * ASN.1 definition of an X.509v3 x509_cert
633 */
634 static const asn1Object_t certObjects[] = {
635 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
636 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
637 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
638 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
639 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
640 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
641 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
642 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
643 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
644 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
645 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
646 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
647 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
648 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
649 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
650 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
651 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
652 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
653 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
654 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
655 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
656 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
657 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
658 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
659 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
660 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
661 { 0, "exit", ASN1_EOC, ASN1_EXIT }
662 };
663 #define X509_OBJ_TBS_CERTIFICATE 1
664 #define X509_OBJ_VERSION 3
665 #define X509_OBJ_SERIAL_NUMBER 4
666 #define X509_OBJ_SIG_ALG 5
667 #define X509_OBJ_ISSUER 6
668 #define X509_OBJ_NOT_BEFORE 8
669 #define X509_OBJ_NOT_AFTER 9
670 #define X509_OBJ_SUBJECT 10
671 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
672 #define X509_OBJ_EXTN_ID 19
673 #define X509_OBJ_CRITICAL 20
674 #define X509_OBJ_EXTN_VALUE 21
675 #define X509_OBJ_ALGORITHM 24
676 #define X509_OBJ_SIGNATURE 25
677
678 /**
679 * forward declaration
680 */
681 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer);
682
683 /**
684 * Parses an X.509v3 certificate
685 */
686 static bool parse_certificate(private_x509_cert_t *this)
687 {
688 asn1_parser_t *parser;
689 chunk_t object;
690 int objectID;
691 int extn_oid = OID_UNKNOWN;
692 int sig_alg = OID_UNKNOWN;
693 bool success = FALSE;
694 bool critical;
695
696 parser = asn1_parser_create(certObjects, this->encoding);
697
698 while (parser->iterate(parser, &objectID, &object))
699 {
700 u_int level = parser->get_level(parser)+1;
701
702 switch (objectID)
703 {
704 case X509_OBJ_TBS_CERTIFICATE:
705 this->tbsCertificate = object;
706 break;
707 case X509_OBJ_VERSION:
708 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
709 DBG2(" v%d", this->version);
710 break;
711 case X509_OBJ_SERIAL_NUMBER:
712 this->serialNumber = object;
713 break;
714 case X509_OBJ_SIG_ALG:
715 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
716 break;
717 case X509_OBJ_ISSUER:
718 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
719 DBG2(" '%Y'", this->issuer);
720 break;
721 case X509_OBJ_NOT_BEFORE:
722 this->notBefore = asn1_parse_time(object, level);
723 break;
724 case X509_OBJ_NOT_AFTER:
725 this->notAfter = asn1_parse_time(object, level);
726 break;
727 case X509_OBJ_SUBJECT:
728 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
729 DBG2(" '%Y'", this->subject);
730 break;
731 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
732 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
733 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
734 if (this->public_key == NULL)
735 {
736 goto end;
737 }
738 break;
739 case X509_OBJ_EXTN_ID:
740 extn_oid = asn1_known_oid(object);
741 break;
742 case X509_OBJ_CRITICAL:
743 critical = object.len && *object.ptr;
744 DBG2(" %s", critical ? "TRUE" : "FALSE");
745 break;
746 case X509_OBJ_EXTN_VALUE:
747 {
748 switch (extn_oid)
749 {
750 case OID_SUBJECT_KEY_ID:
751 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
752 level, "keyIdentifier"))
753 {
754 goto end;
755 }
756 this->subjectKeyID = object;
757 break;
758 case OID_SUBJECT_ALT_NAME:
759 x509_parse_generalNames(object, level, FALSE,
760 this->subjectAltNames);
761 break;
762 case OID_BASIC_CONSTRAINTS:
763 if (parse_basicConstraints(object, level))
764 {
765 this->flags |= X509_CA;
766 }
767 break;
768 case OID_CRL_DISTRIBUTION_POINTS:
769 parse_crlDistributionPoints(object, level, this);
770 break;
771 case OID_AUTHORITY_KEY_ID:
772 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
773 level, &this->authKeySerialNumber);
774 break;
775 case OID_AUTHORITY_INFO_ACCESS:
776 parse_authorityInfoAccess(object, level, this);
777 break;
778 case OID_EXTENDED_KEY_USAGE:
779 if (parse_extendedKeyUsage(object, level))
780 {
781 this->flags |= X509_OCSP_SIGNER;
782 }
783 break;
784 case OID_NS_REVOCATION_URL:
785 case OID_NS_CA_REVOCATION_URL:
786 case OID_NS_CA_POLICY_URL:
787 case OID_NS_COMMENT:
788 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING,
789 level, oid_names[extn_oid].name))
790 {
791 goto end;
792 }
793 break;
794 default:
795 break;
796 }
797 break;
798 }
799 case X509_OBJ_ALGORITHM:
800 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
801 if (this->algorithm != sig_alg)
802 {
803 DBG1(" signature algorithms do not agree");
804 goto end;
805 }
806 break;
807 case X509_OBJ_SIGNATURE:
808 this->signature = object;
809 break;
810 default:
811 break;
812 }
813 }
814 success = parser->success(parser);
815
816 end:
817 parser->destroy(parser);
818 if (success)
819 {
820 hasher_t *hasher;
821
822 /* check if the certificate is self-signed */
823 if (issued_by(this, &this->public.interface.interface))
824 {
825 this->flags |= X509_SELF_SIGNED;
826 }
827 /* create certificate hash */
828 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
829 if (hasher == NULL)
830 {
831 DBG1(" unable to create hash of certificate, SHA1 not supported");
832 return NULL;
833 }
834 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
835 hasher->destroy(hasher);
836 }
837 return success;
838 }
839
840 /**
841 * Implementation of certificate_t.get_type
842 */
843 static certificate_type_t get_type(private_x509_cert_t *this)
844 {
845 return CERT_X509;
846 }
847
848 /**
849 * Implementation of certificate_t.get_subject
850 */
851 static identification_t* get_subject(private_x509_cert_t *this)
852 {
853 return this->subject;
854 }
855
856 /**
857 * Implementation of certificate_t.get_issuer
858 */
859 static identification_t* get_issuer(private_x509_cert_t *this)
860 {
861 return this->issuer;
862 }
863
864 /**
865 * Implementation of certificate_t.has_subject.
866 */
867 static id_match_t has_subject(private_x509_cert_t *this, identification_t *subject)
868 {
869 identification_t *current;
870 enumerator_t *enumerator;
871 id_match_t match, best;
872
873 if (this->encoding_hash.ptr && subject->get_type(subject) == ID_KEY_ID)
874 {
875 if (chunk_equals(this->encoding_hash, subject->get_encoding(subject)))
876 {
877 return ID_MATCH_PERFECT;
878 }
879 }
880
881 best = this->subject->matches(this->subject, subject);
882 enumerator = this->subjectAltNames->create_enumerator(this->subjectAltNames);
883 while (enumerator->enumerate(enumerator, &current))
884 {
885 match = current->matches(current, subject);
886 if (match > best)
887 {
888 best = match;
889 }
890 }
891 enumerator->destroy(enumerator);
892 return best;
893 }
894
895 /**
896 * Implementation of certificate_t.has_subject.
897 */
898 static id_match_t has_issuer(private_x509_cert_t *this, identification_t *issuer)
899 {
900 /* issuerAltNames currently not supported */
901 return this->issuer->matches(this->issuer, issuer);
902 }
903
904 /**
905 * Implementation of certificate_t.issued_by
906 */
907 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer)
908 {
909 public_key_t *key;
910 signature_scheme_t scheme;
911 bool valid;
912 x509_t *x509 = (x509_t*)issuer;
913
914 if (&this->public.interface.interface == issuer)
915 {
916 if (this->flags & X509_SELF_SIGNED)
917 {
918 return TRUE;
919 }
920 }
921 else
922 {
923 if (issuer->get_type(issuer) != CERT_X509)
924 {
925 return FALSE;
926 }
927 if (!(x509->get_flags(x509) & X509_CA))
928 {
929 return FALSE;
930 }
931 }
932 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
933 {
934 return FALSE;
935 }
936
937 /* determine signature scheme */
938 scheme = signature_scheme_from_oid(this->algorithm);
939 if (scheme == SIGN_UNKNOWN)
940 {
941 return FALSE;
942 }
943 /* get the public key of the issuer */
944 key = issuer->get_public_key(issuer);
945 if (!key)
946 {
947 return FALSE;
948 }
949 valid = key->verify(key, scheme, this->tbsCertificate, this->signature);
950 key->destroy(key);
951 return valid;
952 }
953
954 /**
955 * Implementation of certificate_t.get_public_key
956 */
957 static public_key_t* get_public_key(private_x509_cert_t *this)
958 {
959 this->public_key->get_ref(this->public_key);
960 return this->public_key;
961 }
962
963 /**
964 * Implementation of certificate_t.asdf
965 */
966 static private_x509_cert_t* get_ref(private_x509_cert_t *this)
967 {
968 ref_get(&this->ref);
969 return this;
970 }
971
972 /**
973 * Implementation of x509_cert_t.get_flags.
974 */
975 static x509_flag_t get_flags(private_x509_cert_t *this)
976 {
977 return this->flags;
978 }
979
980 /**
981 * Implementation of x509_cert_t.get_validity.
982 */
983 static bool get_validity(private_x509_cert_t *this, time_t *when,
984 time_t *not_before, time_t *not_after)
985 {
986 time_t t;
987
988 if (when)
989 {
990 t = *when;
991 }
992 else
993 {
994 t = time(NULL);
995 }
996 if (not_before)
997 {
998 *not_before = this->notBefore;
999 }
1000 if (not_after)
1001 {
1002 *not_after = this->notAfter;
1003 }
1004 return (t >= this->notBefore && t <= this->notAfter);
1005 }
1006
1007 /**
1008 * Implementation of certificate_t.is_newer.
1009 */
1010 static bool is_newer(certificate_t *this, certificate_t *that)
1011 {
1012 time_t this_update, that_update, now = time(NULL);
1013 bool new;
1014
1015 this->get_validity(this, &now, &this_update, NULL);
1016 that->get_validity(that, &now, &that_update, NULL);
1017 new = this_update > that_update;
1018 DBG1(" certificate from %T is %s - existing certificate from %T %s",
1019 &this_update, FALSE, new ? "newer":"not newer",
1020 &that_update, FALSE, new ? "replaced":"retained");
1021 return new;
1022 }
1023
1024 /**
1025 * Implementation of certificate_t.get_encoding.
1026 */
1027 static chunk_t get_encoding(private_x509_cert_t *this)
1028 {
1029 return chunk_clone(this->encoding);
1030 }
1031
1032 /**
1033 * Implementation of certificate_t.equals.
1034 */
1035 static bool equals(private_x509_cert_t *this, certificate_t *other)
1036 {
1037 chunk_t encoding;
1038 bool equal;
1039
1040 if (this == (private_x509_cert_t*)other)
1041 {
1042 return TRUE;
1043 }
1044 if (other->get_type(other) != CERT_X509)
1045 {
1046 return FALSE;
1047 }
1048 if (other->equals == (void*)equals)
1049 { /* skip allocation if we have the same implementation */
1050 return chunk_equals(this->encoding, ((private_x509_cert_t*)other)->encoding);
1051 }
1052 encoding = other->get_encoding(other);
1053 equal = chunk_equals(this->encoding, encoding);
1054 free(encoding.ptr);
1055 return equal;
1056 }
1057
1058 /**
1059 * Implementation of x509_t.get_serial.
1060 */
1061 static chunk_t get_serial(private_x509_cert_t *this)
1062 {
1063 return this->serialNumber;
1064 }
1065
1066 /**
1067 * Implementation of x509_t.get_authKeyIdentifier.
1068 */
1069 static chunk_t get_authKeyIdentifier(private_x509_cert_t *this)
1070 {
1071 return this->authKeyIdentifier;
1072 }
1073
1074 /**
1075 * Implementation of x509_cert_t.create_subjectAltName_enumerator.
1076 */
1077 static enumerator_t* create_subjectAltName_enumerator(private_x509_cert_t *this)
1078 {
1079 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
1080 }
1081
1082 /**
1083 * Implementation of x509_cert_t.create_ocsp_uri_enumerator.
1084 */
1085 static enumerator_t* create_ocsp_uri_enumerator(private_x509_cert_t *this)
1086 {
1087 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
1088 }
1089
1090 /**
1091 * Implementation of x509_cert_t.create_crl_uri_enumerator.
1092 */
1093 static enumerator_t* create_crl_uri_enumerator(private_x509_cert_t *this)
1094 {
1095 return this->crl_uris->create_enumerator(this->crl_uris);
1096 }
1097
1098 /**
1099 * Implementation of certificate_t.asdf
1100 */
1101 static void destroy(private_x509_cert_t *this)
1102 {
1103 if (ref_put(&this->ref))
1104 {
1105 this->subjectAltNames->destroy_offset(this->subjectAltNames,
1106 offsetof(identification_t, destroy));
1107 this->crl_uris->destroy_function(this->crl_uris, free);
1108 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
1109 DESTROY_IF(this->issuer);
1110 DESTROY_IF(this->subject);
1111 DESTROY_IF(this->public_key);
1112 chunk_free(&this->authKeyIdentifier);
1113 chunk_free(&this->encoding);
1114 chunk_free(&this->encoding_hash);
1115 if (!this->parsed)
1116 { /* only parsed certificates point these fields to "encoded" */
1117 chunk_free(&this->signature);
1118 chunk_free(&this->serialNumber);
1119 chunk_free(&this->tbsCertificate);
1120 }
1121 free(this);
1122 }
1123 }
1124
1125 /**
1126 * create an empty but initialized X.509 certificate
1127 */
1128 static private_x509_cert_t* create_empty(void)
1129 {
1130 private_x509_cert_t *this = malloc_thing(private_x509_cert_t);
1131
1132 this->public.interface.interface.get_type = (certificate_type_t (*) (certificate_t*))get_type;
1133 this->public.interface.interface.get_subject = (identification_t* (*) (certificate_t*))get_subject;
1134 this->public.interface.interface.get_issuer = (identification_t* (*) (certificate_t*))get_issuer;
1135 this->public.interface.interface.has_subject = (id_match_t (*) (certificate_t*, identification_t*))has_subject;
1136 this->public.interface.interface.has_issuer = (id_match_t (*) (certificate_t*, identification_t*))has_issuer;
1137 this->public.interface.interface.issued_by = (bool (*) (certificate_t*, certificate_t*))issued_by;
1138 this->public.interface.interface.get_public_key = (public_key_t* (*) (certificate_t*))get_public_key;
1139 this->public.interface.interface.get_validity = (bool (*) (certificate_t*, time_t*, time_t*, time_t*))get_validity;
1140 this->public.interface.interface.is_newer = (bool (*) (certificate_t*,certificate_t*))is_newer;
1141 this->public.interface.interface.get_encoding = (chunk_t (*) (certificate_t*))get_encoding;
1142 this->public.interface.interface.equals = (bool (*)(certificate_t*, certificate_t*))equals;
1143 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t*))get_ref;
1144 this->public.interface.interface.destroy = (void (*)(certificate_t*))destroy;
1145 this->public.interface.get_flags = (x509_flag_t (*)(x509_t*))get_flags;
1146 this->public.interface.get_serial = (chunk_t (*)(x509_t*))get_serial;
1147 this->public.interface.get_authKeyIdentifier = (chunk_t (*)(x509_t*))get_authKeyIdentifier;
1148 this->public.interface.create_subjectAltName_enumerator = (enumerator_t* (*)(x509_t*))create_subjectAltName_enumerator;
1149 this->public.interface.create_crl_uri_enumerator = (enumerator_t* (*)(x509_t*))create_crl_uri_enumerator;
1150 this->public.interface.create_ocsp_uri_enumerator = (enumerator_t* (*)(x509_t*))create_ocsp_uri_enumerator;
1151
1152 this->encoding = chunk_empty;
1153 this->encoding_hash = chunk_empty;
1154 this->tbsCertificate = chunk_empty;
1155 this->version = 3;
1156 this->serialNumber = chunk_empty;
1157 this->notBefore = 0;
1158 this->notAfter = 0;
1159 this->public_key = NULL;
1160 this->subject = NULL;
1161 this->issuer = NULL;
1162 this->subjectAltNames = linked_list_create();
1163 this->crl_uris = linked_list_create();
1164 this->ocsp_uris = linked_list_create();
1165 this->subjectKeyID = chunk_empty;
1166 this->authKeyIdentifier = chunk_empty;
1167 this->authKeySerialNumber = chunk_empty;
1168 this->algorithm = 0;
1169 this->signature = chunk_empty;
1170 this->flags = 0;
1171 this->ref = 1;
1172 this->parsed = FALSE;
1173
1174 return this;
1175 }
1176
1177 /**
1178 * Generate and sign a new certificate
1179 */
1180 static bool generate(private_x509_cert_t *cert, certificate_t *sign_cert,
1181 private_key_t *sign_key, int digest_alg)
1182 {
1183 chunk_t extensions = chunk_empty;
1184 chunk_t basicConstraints = chunk_empty, subjectAltNames = chunk_empty;
1185 chunk_t subjectKeyIdentifier = chunk_empty, authKeyIdentifier = chunk_empty;
1186 identification_t *issuer, *subject;
1187 chunk_t key_info;
1188 signature_scheme_t scheme;
1189 hasher_t *hasher;
1190 enumerator_t *enumerator;
1191 identification_t *id;
1192
1193 subject = cert->subject;
1194 if (sign_cert)
1195 {
1196 issuer = sign_cert->get_subject(sign_cert);
1197 if (!cert->public_key)
1198 {
1199 return FALSE;
1200 }
1201 }
1202 else
1203 { /* self signed */
1204 issuer = subject;
1205 if (!cert->public_key)
1206 {
1207 cert->public_key = sign_key->get_public_key(sign_key);
1208 }
1209 cert->flags |= X509_SELF_SIGNED;
1210 }
1211 cert->issuer = issuer->clone(issuer);
1212 if (!cert->notBefore)
1213 {
1214 cert->notBefore = time(NULL);
1215 }
1216 if (!cert->notAfter)
1217 { /* defaults to 1 year from now */
1218 cert->notAfter = cert->notBefore + 60 * 60 * 24 * 365;
1219 }
1220
1221 /* select signature scheme */
1222 switch (sign_key->get_type(sign_key))
1223 {
1224 case KEY_RSA:
1225 switch (digest_alg)
1226 {
1227 case HASH_MD5:
1228 cert->algorithm = OID_MD5_WITH_RSA;
1229 break;
1230 case HASH_SHA1:
1231 cert->algorithm = OID_SHA1_WITH_RSA;
1232 break;
1233 case HASH_SHA224:
1234 cert->algorithm = OID_SHA224_WITH_RSA;
1235 break;
1236 case HASH_SHA256:
1237 cert->algorithm = OID_SHA256_WITH_RSA;
1238 break;
1239 case HASH_SHA384:
1240 cert->algorithm = OID_SHA384_WITH_RSA;
1241 break;
1242 case HASH_SHA512:
1243 cert->algorithm = OID_SHA512_WITH_RSA;
1244 break;
1245 default:
1246 return FALSE;
1247 }
1248 break;
1249 case KEY_ECDSA:
1250 switch (digest_alg)
1251 {
1252 case HASH_SHA1:
1253 cert->algorithm = OID_ECDSA_WITH_SHA1;
1254 break;
1255 case HASH_SHA256:
1256 cert->algorithm = OID_ECDSA_WITH_SHA256;
1257 break;
1258 case HASH_SHA384:
1259 cert->algorithm = OID_ECDSA_WITH_SHA384;
1260 break;
1261 case HASH_SHA512:
1262 cert->algorithm = OID_ECDSA_WITH_SHA512;
1263 break;
1264 default:
1265 return FALSE;
1266 }
1267 break;
1268 default:
1269 return FALSE;
1270 }
1271 scheme = signature_scheme_from_oid(cert->algorithm);
1272
1273 if (!cert->public_key->get_encoding(cert->public_key,
1274 KEY_PUB_SPKI_ASN1_DER, &key_info))
1275 {
1276 return FALSE;
1277 }
1278
1279 enumerator = cert->subjectAltNames->create_enumerator(cert->subjectAltNames);
1280 while (enumerator->enumerate(enumerator, &id))
1281 {
1282 int context;
1283 chunk_t name;
1284
1285 switch (id->get_type(id))
1286 {
1287 case ID_RFC822_ADDR:
1288 context = ASN1_CONTEXT_S_1;
1289 break;
1290 case ID_FQDN:
1291 context = ASN1_CONTEXT_S_2;
1292 break;
1293 case ID_IPV4_ADDR:
1294 case ID_IPV6_ADDR:
1295 context = ASN1_CONTEXT_S_7;
1296 break;
1297 default:
1298 DBG1("encoding %N as subjectAltName not supported",
1299 id_type_names, id->get_type(id));
1300 enumerator->destroy(enumerator);
1301 free(key_info.ptr);
1302 free(subjectAltNames.ptr);
1303 return FALSE;
1304 }
1305 name = asn1_wrap(context, "c", id->get_encoding(id));
1306 subjectAltNames = chunk_cat("mm", subjectAltNames, name);
1307 }
1308 enumerator->destroy(enumerator);
1309 if (subjectAltNames.ptr)
1310 {
1311 subjectAltNames = asn1_wrap(ASN1_SEQUENCE, "mm",
1312 asn1_build_known_oid(OID_SUBJECT_ALT_NAME),
1313 asn1_wrap(ASN1_OCTET_STRING, "m",
1314 asn1_wrap(ASN1_SEQUENCE, "m", subjectAltNames)));
1315 }
1316
1317 if (cert->flags & X509_CA)
1318 {
1319 chunk_t yes, keyid;
1320
1321 yes = chunk_alloca(1);
1322 yes.ptr[0] = 0xFF;
1323 basicConstraints = asn1_wrap(ASN1_SEQUENCE, "mmm",
1324 asn1_build_known_oid(OID_BASIC_CONSTRAINTS),
1325 asn1_wrap(ASN1_BOOLEAN, "c", yes),
1326 asn1_wrap(ASN1_OCTET_STRING, "m",
1327 asn1_wrap(ASN1_SEQUENCE, "m",
1328 asn1_wrap(ASN1_BOOLEAN, "c", yes))));
1329 /* add subjectKeyIdentifier to CA certificates */
1330 if (cert->public_key->get_fingerprint(cert->public_key,
1331 KEY_ID_PUBKEY_SHA1, &keyid))
1332 {
1333 subjectKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1334 asn1_build_known_oid(OID_SUBJECT_KEY_ID),
1335 asn1_wrap(ASN1_OCTET_STRING, "m",
1336 asn1_wrap(ASN1_OCTET_STRING, "c", keyid)));
1337 }
1338 }
1339 if (sign_key)
1340 { /* add the keyid authKeyIdentifier for non self-signed certificates */
1341 chunk_t keyid;
1342
1343 if (sign_key->get_fingerprint(sign_key, KEY_ID_PUBKEY_SHA1, &keyid))
1344 {
1345 authKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1346 asn1_build_known_oid(OID_AUTHORITY_KEY_ID),
1347 asn1_wrap(ASN1_OCTET_STRING, "m",
1348 asn1_wrap(ASN1_SEQUENCE, "m",
1349 asn1_wrap(ASN1_CONTEXT_S_0, "c", keyid))));
1350 }
1351 }
1352 if (basicConstraints.ptr || subjectAltNames.ptr || authKeyIdentifier.ptr)
1353 {
1354 extensions = asn1_wrap(ASN1_CONTEXT_C_3, "m",
1355 asn1_wrap(ASN1_SEQUENCE, "mmmm",
1356 basicConstraints, subjectKeyIdentifier,
1357 authKeyIdentifier, subjectAltNames));
1358 }
1359
1360 cert->tbsCertificate = asn1_wrap(ASN1_SEQUENCE, "mmmcmcmm",
1361 asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2),
1362 asn1_integer("c", cert->serialNumber),
1363 asn1_algorithmIdentifier(cert->algorithm),
1364 issuer->get_encoding(issuer),
1365 asn1_wrap(ASN1_SEQUENCE, "mm",
1366 asn1_from_time(&cert->notBefore, ASN1_UTCTIME),
1367 asn1_from_time(&cert->notAfter, ASN1_UTCTIME)),
1368 subject->get_encoding(subject),
1369 key_info, extensions);
1370
1371 if (!sign_key->sign(sign_key, scheme, cert->tbsCertificate, &cert->signature))
1372 {
1373 return FALSE;
1374 }
1375 cert->encoding = asn1_wrap(ASN1_SEQUENCE, "cmm", cert->tbsCertificate,
1376 asn1_algorithmIdentifier(cert->algorithm),
1377 asn1_bitstring("c", cert->signature));
1378
1379 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1380 if (!hasher)
1381 {
1382 return FALSE;
1383 }
1384 hasher->allocate_hash(hasher, cert->encoding, &cert->encoding_hash);
1385 hasher->destroy(hasher);
1386 return TRUE;
1387 }
1388
1389 /**
1390 * See header.
1391 */
1392 x509_cert_t *x509_cert_load(certificate_type_t type, va_list args)
1393 {
1394 chunk_t blob = chunk_empty;
1395
1396 while (TRUE)
1397 {
1398 switch (va_arg(args, builder_part_t))
1399 {
1400 case BUILD_BLOB_ASN1_DER:
1401 blob = va_arg(args, chunk_t);
1402 continue;
1403 case BUILD_END:
1404 break;
1405 default:
1406 return NULL;
1407 }
1408 break;
1409 }
1410
1411 if (blob.ptr)
1412 {
1413 private_x509_cert_t *cert = create_empty();
1414
1415 cert->encoding = chunk_clone(blob);
1416 if (parse_certificate(cert))
1417 {
1418 cert->parsed = TRUE;
1419 return &cert->public;
1420 }
1421 destroy(cert);
1422 }
1423 return NULL;
1424 }
1425
1426 /**
1427 * See header.
1428 */
1429 x509_cert_t *x509_cert_gen(certificate_type_t type, va_list args)
1430 {
1431 private_x509_cert_t *cert;
1432 certificate_t *sign_cert = NULL;
1433 private_key_t *sign_key = NULL;
1434 hash_algorithm_t digest_alg = HASH_SHA1;
1435
1436 cert = create_empty();
1437 while (TRUE)
1438 {
1439 switch (va_arg(args, builder_part_t))
1440 {
1441 case BUILD_X509_FLAG:
1442 cert->flags |= va_arg(args, x509_flag_t);
1443 continue;
1444 case BUILD_SIGNING_KEY:
1445 sign_key = va_arg(args, private_key_t*);
1446 continue;
1447 case BUILD_SIGNING_CERT:
1448 sign_cert = va_arg(args, certificate_t*);
1449 continue;
1450 case BUILD_PUBLIC_KEY:
1451 cert->public_key = va_arg(args, public_key_t*);
1452 cert->public_key->get_ref(cert->public_key);
1453 continue;
1454 case BUILD_SUBJECT:
1455 cert->subject = va_arg(args, identification_t*);
1456 cert->subject = cert->subject->clone(cert->subject);
1457 continue;
1458 case BUILD_SUBJECT_ALTNAMES:
1459 {
1460 enumerator_t *enumerator;
1461 identification_t *id;
1462 linked_list_t *list;
1463
1464 list = va_arg(args, linked_list_t*);
1465 enumerator = list->create_enumerator(list);
1466 while (enumerator->enumerate(enumerator, &id))
1467 {
1468 cert->subjectAltNames->insert_last(
1469 cert->subjectAltNames, id->clone(id));
1470 }
1471 enumerator->destroy(enumerator);
1472 continue;
1473 }
1474 case BUILD_NOT_BEFORE_TIME:
1475 cert->notBefore = va_arg(args, time_t);
1476 continue;
1477 case BUILD_NOT_AFTER_TIME:
1478 cert->notAfter = va_arg(args, time_t);
1479 continue;
1480 case BUILD_SERIAL:
1481 cert->serialNumber = chunk_clone(va_arg(args, chunk_t));
1482 continue;
1483 case BUILD_DIGEST_ALG:
1484 digest_alg = va_arg(args, int);
1485 continue;
1486 case BUILD_END:
1487 break;
1488 default:
1489 destroy(cert);
1490 return NULL;
1491 }
1492 break;
1493 }
1494
1495 if (sign_key && generate(cert, sign_cert, sign_key, digest_alg))
1496 {
1497 return &cert->public;
1498 }
1499 destroy(cert);
1500 return NULL;
1501 }
1502