updated x509 plugin to public key/x509 API changes
[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-2008 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 * Parses an X.509v3 certificate
680 */
681 static bool parse_certificate(private_x509_cert_t *this)
682 {
683 asn1_parser_t *parser;
684 chunk_t object;
685 int objectID;
686 int extn_oid = OID_UNKNOWN;
687 int sig_alg = OID_UNKNOWN;
688 bool success = FALSE;
689 bool critical;
690
691 parser = asn1_parser_create(certObjects, this->encoding);
692
693 while (parser->iterate(parser, &objectID, &object))
694 {
695 u_int level = parser->get_level(parser)+1;
696
697 switch (objectID)
698 {
699 case X509_OBJ_TBS_CERTIFICATE:
700 this->tbsCertificate = object;
701 break;
702 case X509_OBJ_VERSION:
703 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
704 DBG2(" v%d", this->version);
705 break;
706 case X509_OBJ_SERIAL_NUMBER:
707 this->serialNumber = object;
708 break;
709 case X509_OBJ_SIG_ALG:
710 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
711 break;
712 case X509_OBJ_ISSUER:
713 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
714 DBG2(" '%Y'", this->issuer);
715 break;
716 case X509_OBJ_NOT_BEFORE:
717 this->notBefore = asn1_parse_time(object, level);
718 break;
719 case X509_OBJ_NOT_AFTER:
720 this->notAfter = asn1_parse_time(object, level);
721 break;
722 case X509_OBJ_SUBJECT:
723 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
724 DBG2(" '%Y'", this->subject);
725 break;
726 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
727 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
728 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
729 if (this->public_key == NULL)
730 {
731 goto end;
732 }
733 break;
734 case X509_OBJ_EXTN_ID:
735 extn_oid = asn1_known_oid(object);
736 break;
737 case X509_OBJ_CRITICAL:
738 critical = object.len && *object.ptr;
739 DBG2(" %s", critical ? "TRUE" : "FALSE");
740 break;
741 case X509_OBJ_EXTN_VALUE:
742 {
743 switch (extn_oid)
744 {
745 case OID_SUBJECT_KEY_ID:
746 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
747 level, "keyIdentifier"))
748 {
749 goto end;
750 }
751 this->subjectKeyID = object;
752 break;
753 case OID_SUBJECT_ALT_NAME:
754 x509_parse_generalNames(object, level, FALSE,
755 this->subjectAltNames);
756 break;
757 case OID_BASIC_CONSTRAINTS:
758 if (parse_basicConstraints(object, level))
759 {
760 this->flags |= X509_CA;
761 }
762 break;
763 case OID_CRL_DISTRIBUTION_POINTS:
764 parse_crlDistributionPoints(object, level, this);
765 break;
766 case OID_AUTHORITY_KEY_ID:
767 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
768 level, &this->authKeySerialNumber);
769 break;
770 case OID_AUTHORITY_INFO_ACCESS:
771 parse_authorityInfoAccess(object, level, this);
772 break;
773 case OID_EXTENDED_KEY_USAGE:
774 if (parse_extendedKeyUsage(object, level))
775 {
776 this->flags |= X509_OCSP_SIGNER;
777 }
778 break;
779 case OID_NS_REVOCATION_URL:
780 case OID_NS_CA_REVOCATION_URL:
781 case OID_NS_CA_POLICY_URL:
782 case OID_NS_COMMENT:
783 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING,
784 level, oid_names[extn_oid].name))
785 {
786 goto end;
787 }
788 break;
789 default:
790 break;
791 }
792 break;
793 }
794 case X509_OBJ_ALGORITHM:
795 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
796 if (this->algorithm != sig_alg)
797 {
798 DBG1(" signature algorithms do not agree");
799 goto end;
800 }
801 break;
802 case X509_OBJ_SIGNATURE:
803 this->signature = object;
804 break;
805 default:
806 break;
807 }
808 }
809 success = parser->success(parser);
810
811 end:
812 parser->destroy(parser);
813 return success;
814 }
815
816 /**
817 * Implementation of certificate_t.get_type
818 */
819 static certificate_type_t get_type(private_x509_cert_t *this)
820 {
821 return CERT_X509;
822 }
823
824 /**
825 * Implementation of certificate_t.get_subject
826 */
827 static identification_t* get_subject(private_x509_cert_t *this)
828 {
829 return this->subject;
830 }
831
832 /**
833 * Implementation of certificate_t.get_issuer
834 */
835 static identification_t* get_issuer(private_x509_cert_t *this)
836 {
837 return this->issuer;
838 }
839
840 /**
841 * Implementation of certificate_t.has_subject.
842 */
843 static id_match_t has_subject(private_x509_cert_t *this, identification_t *subject)
844 {
845 identification_t *current;
846 enumerator_t *enumerator;
847 id_match_t match, best;
848
849 if (this->encoding_hash.ptr && subject->get_type(subject) == ID_KEY_ID)
850 {
851 if (chunk_equals(this->encoding_hash, subject->get_encoding(subject)))
852 {
853 return ID_MATCH_PERFECT;
854 }
855 }
856
857 best = this->subject->matches(this->subject, subject);
858 enumerator = this->subjectAltNames->create_enumerator(this->subjectAltNames);
859 while (enumerator->enumerate(enumerator, &current))
860 {
861 match = current->matches(current, subject);
862 if (match > best)
863 {
864 best = match;
865 }
866 }
867 enumerator->destroy(enumerator);
868 return best;
869 }
870
871 /**
872 * Implementation of certificate_t.has_subject.
873 */
874 static id_match_t has_issuer(private_x509_cert_t *this, identification_t *issuer)
875 {
876 /* issuerAltNames currently not supported */
877 return this->issuer->matches(this->issuer, issuer);
878 }
879
880 /**
881 * Implementation of certificate_t.issued_by
882 */
883 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer)
884 {
885 public_key_t *key;
886 signature_scheme_t scheme;
887 bool valid;
888 x509_t *x509 = (x509_t*)issuer;
889
890 if (&this->public.interface.interface == issuer)
891 {
892 if (this->flags & X509_SELF_SIGNED)
893 {
894 return TRUE;
895 }
896 }
897 else
898 {
899 if (issuer->get_type(issuer) != CERT_X509)
900 {
901 return FALSE;
902 }
903 if (!(x509->get_flags(x509) & X509_CA))
904 {
905 return FALSE;
906 }
907 }
908 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
909 {
910 return FALSE;
911 }
912
913 /* get the public key of the issuer */
914 key = issuer->get_public_key(issuer);
915
916 /* determine signature scheme */
917 scheme = signature_scheme_from_oid(this->algorithm);
918
919 if (scheme == SIGN_UNKNOWN || key == NULL)
920 {
921 return FALSE;
922 }
923 /* TODO: add a lightweight check option (comparing auth/subject keyids only) */
924 valid = key->verify(key, scheme, this->tbsCertificate, this->signature);
925 key->destroy(key);
926 return valid;
927 }
928
929 /**
930 * Implementation of certificate_t.get_public_key
931 */
932 static public_key_t* get_public_key(private_x509_cert_t *this)
933 {
934 this->public_key->get_ref(this->public_key);
935 return this->public_key;
936 }
937
938 /**
939 * Implementation of certificate_t.asdf
940 */
941 static private_x509_cert_t* get_ref(private_x509_cert_t *this)
942 {
943 ref_get(&this->ref);
944 return this;
945 }
946
947 /**
948 * Implementation of x509_cert_t.get_flags.
949 */
950 static x509_flag_t get_flags(private_x509_cert_t *this)
951 {
952 return this->flags;
953 }
954
955 /**
956 * Implementation of x509_cert_t.get_validity.
957 */
958 static bool get_validity(private_x509_cert_t *this, time_t *when,
959 time_t *not_before, time_t *not_after)
960 {
961 time_t t;
962
963 if (when)
964 {
965 t = *when;
966 }
967 else
968 {
969 t = time(NULL);
970 }
971 if (not_before)
972 {
973 *not_before = this->notBefore;
974 }
975 if (not_after)
976 {
977 *not_after = this->notAfter;
978 }
979 return (t >= this->notBefore && t <= this->notAfter);
980 }
981
982 /**
983 * Implementation of certificate_t.is_newer.
984 */
985 static bool is_newer(certificate_t *this, certificate_t *that)
986 {
987 time_t this_update, that_update, now = time(NULL);
988 bool new;
989
990 this->get_validity(this, &now, &this_update, NULL);
991 that->get_validity(that, &now, &that_update, NULL);
992 new = this_update > that_update;
993 DBG1(" certificate from %T is %s - existing certificate from %T %s",
994 &this_update, FALSE, new ? "newer":"not newer",
995 &that_update, FALSE, new ? "replaced":"retained");
996 return new;
997 }
998
999 /**
1000 * Implementation of certificate_t.get_encoding.
1001 */
1002 static chunk_t get_encoding(private_x509_cert_t *this)
1003 {
1004 return chunk_clone(this->encoding);
1005 }
1006
1007 /**
1008 * Implementation of certificate_t.equals.
1009 */
1010 static bool equals(private_x509_cert_t *this, certificate_t *other)
1011 {
1012 chunk_t encoding;
1013 bool equal;
1014
1015 if (this == (private_x509_cert_t*)other)
1016 {
1017 return TRUE;
1018 }
1019 if (other->get_type(other) != CERT_X509)
1020 {
1021 return FALSE;
1022 }
1023 if (other->equals == (void*)equals)
1024 { /* skip allocation if we have the same implementation */
1025 return chunk_equals(this->encoding, ((private_x509_cert_t*)other)->encoding);
1026 }
1027 encoding = other->get_encoding(other);
1028 equal = chunk_equals(this->encoding, encoding);
1029 free(encoding.ptr);
1030 return equal;
1031 }
1032
1033 /**
1034 * Implementation of x509_t.get_serial.
1035 */
1036 static chunk_t get_serial(private_x509_cert_t *this)
1037 {
1038 return this->serialNumber;
1039 }
1040
1041 /**
1042 * Implementation of x509_t.get_authKeyIdentifier.
1043 */
1044 static chunk_t get_authKeyIdentifier(private_x509_cert_t *this)
1045 {
1046 return this->authKeyIdentifier;
1047 }
1048
1049 /**
1050 * Implementation of x509_cert_t.create_subjectAltName_enumerator.
1051 */
1052 static enumerator_t* create_subjectAltName_enumerator(private_x509_cert_t *this)
1053 {
1054 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
1055 }
1056
1057 /**
1058 * Implementation of x509_cert_t.create_ocsp_uri_enumerator.
1059 */
1060 static enumerator_t* create_ocsp_uri_enumerator(private_x509_cert_t *this)
1061 {
1062 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
1063 }
1064
1065 /**
1066 * Implementation of x509_cert_t.create_crl_uri_enumerator.
1067 */
1068 static enumerator_t* create_crl_uri_enumerator(private_x509_cert_t *this)
1069 {
1070 return this->crl_uris->create_enumerator(this->crl_uris);
1071 }
1072
1073 /**
1074 * Implementation of certificate_t.asdf
1075 */
1076 static void destroy(private_x509_cert_t *this)
1077 {
1078 if (ref_put(&this->ref))
1079 {
1080 this->subjectAltNames->destroy_offset(this->subjectAltNames,
1081 offsetof(identification_t, destroy));
1082 this->crl_uris->destroy_function(this->crl_uris, free);
1083 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
1084 DESTROY_IF(this->issuer);
1085 DESTROY_IF(this->subject);
1086 DESTROY_IF(this->public_key);
1087 chunk_free(&this->authKeyIdentifier);
1088 chunk_free(&this->encoding);
1089 chunk_free(&this->encoding_hash);
1090 if (!this->parsed)
1091 { /* only parsed certificates point these fields to "encoded" */
1092 chunk_free(&this->signature);
1093 chunk_free(&this->serialNumber);
1094 chunk_free(&this->tbsCertificate);
1095 }
1096 free(this);
1097 }
1098 }
1099
1100 /**
1101 * create an empty but initialized X.509 certificate
1102 */
1103 static private_x509_cert_t* create_empty(void)
1104 {
1105 private_x509_cert_t *this = malloc_thing(private_x509_cert_t);
1106
1107 this->public.interface.interface.get_type = (certificate_type_t (*) (certificate_t*))get_type;
1108 this->public.interface.interface.get_subject = (identification_t* (*) (certificate_t*))get_subject;
1109 this->public.interface.interface.get_issuer = (identification_t* (*) (certificate_t*))get_issuer;
1110 this->public.interface.interface.has_subject = (id_match_t (*) (certificate_t*, identification_t*))has_subject;
1111 this->public.interface.interface.has_issuer = (id_match_t (*) (certificate_t*, identification_t*))has_issuer;
1112 this->public.interface.interface.issued_by = (bool (*) (certificate_t*, certificate_t*))issued_by;
1113 this->public.interface.interface.get_public_key = (public_key_t* (*) (certificate_t*))get_public_key;
1114 this->public.interface.interface.get_validity = (bool (*) (certificate_t*, time_t*, time_t*, time_t*))get_validity;
1115 this->public.interface.interface.is_newer = (bool (*) (certificate_t*,certificate_t*))is_newer;
1116 this->public.interface.interface.get_encoding = (chunk_t (*) (certificate_t*))get_encoding;
1117 this->public.interface.interface.equals = (bool (*)(certificate_t*, certificate_t*))equals;
1118 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t*))get_ref;
1119 this->public.interface.interface.destroy = (void (*)(certificate_t*))destroy;
1120 this->public.interface.get_flags = (x509_flag_t (*)(x509_t*))get_flags;
1121 this->public.interface.get_serial = (chunk_t (*)(x509_t*))get_serial;
1122 this->public.interface.get_authKeyIdentifier = (chunk_t (*)(x509_t*))get_authKeyIdentifier;
1123 this->public.interface.create_subjectAltName_enumerator = (enumerator_t* (*)(x509_t*))create_subjectAltName_enumerator;
1124 this->public.interface.create_crl_uri_enumerator = (enumerator_t* (*)(x509_t*))create_crl_uri_enumerator;
1125 this->public.interface.create_ocsp_uri_enumerator = (enumerator_t* (*)(x509_t*))create_ocsp_uri_enumerator;
1126
1127 this->encoding = chunk_empty;
1128 this->encoding_hash = chunk_empty;
1129 this->tbsCertificate = chunk_empty;
1130 this->version = 3;
1131 this->serialNumber = chunk_empty;
1132 this->notBefore = 0;
1133 this->notAfter = 0;
1134 this->public_key = NULL;
1135 this->subject = NULL;
1136 this->issuer = NULL;
1137 this->subjectAltNames = linked_list_create();
1138 this->crl_uris = linked_list_create();
1139 this->ocsp_uris = linked_list_create();
1140 this->subjectKeyID = chunk_empty;
1141 this->authKeyIdentifier = chunk_empty;
1142 this->authKeySerialNumber = chunk_empty;
1143 this->algorithm = 0;
1144 this->signature = chunk_empty;
1145 this->flags = 0;
1146 this->ref = 1;
1147 this->parsed = FALSE;
1148
1149 return this;
1150 }
1151
1152 /**
1153 * create an X.509 certificate from a chunk
1154 */
1155 static private_x509_cert_t *create_from_chunk(chunk_t chunk)
1156 {
1157 hasher_t *hasher;
1158 private_x509_cert_t *this = create_empty();
1159
1160 this->encoding = chunk;
1161 this->parsed = TRUE;
1162 if (!parse_certificate(this))
1163 {
1164 destroy(this);
1165 return NULL;
1166 }
1167
1168 /* check if the certificate is self-signed */
1169 if (issued_by(this, &this->public.interface.interface))
1170 {
1171 this->flags |= X509_SELF_SIGNED;
1172 }
1173
1174 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1175 if (hasher == NULL)
1176 {
1177 DBG1(" unable to create hash of certificate, SHA1 not supported");
1178 destroy(this);
1179 return NULL;
1180 }
1181 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
1182 hasher->destroy(hasher);
1183
1184 return this;
1185 }
1186
1187 typedef struct private_builder_t private_builder_t;
1188 /**
1189 * Builder implementation for certificate loading
1190 */
1191 struct private_builder_t {
1192 /** implements the builder interface */
1193 builder_t public;
1194 /** loaded certificate */
1195 private_x509_cert_t *cert;
1196 /** additional flags to enforce */
1197 x509_flag_t flags;
1198 /** certificate to sign, if we generate a new cert */
1199 certificate_t *sign_cert;
1200 /** private key to sign, if we generate a new cert */
1201 private_key_t *sign_key;
1202 };
1203
1204 /**
1205 * Generate and sign a new certificate
1206 */
1207 static bool generate(private_builder_t *this)
1208 {
1209 chunk_t extensions = chunk_empty;
1210 identification_t *issuer, *subject;
1211 chunk_t key_info, key;
1212 signature_scheme_t scheme;
1213 hasher_t *hasher;
1214
1215 subject = this->cert->subject;
1216 if (this->sign_cert)
1217 {
1218 issuer = this->sign_cert->get_subject(this->sign_cert);
1219 if (!this->cert->public_key)
1220 {
1221 return FALSE;
1222 }
1223 }
1224 else
1225 { /* self signed */
1226 issuer = subject;
1227 if (!this->cert->public_key)
1228 {
1229 this->cert->public_key = this->sign_key->get_public_key(this->sign_key);
1230 }
1231 this->flags |= X509_SELF_SIGNED;
1232 }
1233 this->cert->issuer = issuer->clone(issuer);
1234 if (!this->cert->notBefore)
1235 {
1236 this->cert->notBefore = time(NULL);
1237 }
1238 if (!this->cert->notAfter)
1239 { /* defaults to 1 years from now on */
1240 this->cert->notAfter = this->cert->notBefore + 60 * 60 * 24 * 365;
1241 }
1242 this->cert->flags = this->flags;
1243
1244 switch (this->sign_key->get_type(this->sign_key))
1245 {
1246 case KEY_RSA:
1247 this->cert->algorithm = OID_SHA1_WITH_RSA;
1248 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
1249 break;
1250 default:
1251 return FALSE;
1252 }
1253
1254 switch (this->cert->public_key->get_type(this->cert->public_key))
1255 {
1256 case KEY_RSA:
1257 if (!this->cert->public_key->get_encoding(this->cert->public_key,
1258 KEY_PUB_ASN1_DER, &key))
1259 {
1260 return FALSE;
1261 }
1262 key_info = asn1_wrap(ASN1_SEQUENCE, "cm",
1263 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
1264 asn1_bitstring("m", key));
1265 break;
1266 default:
1267 return FALSE;
1268 }
1269
1270 if (this->cert->subjectAltNames->get_count(this->cert->subjectAltNames))
1271 {
1272 /* TODO: encode subjectAltNames */
1273 }
1274
1275 this->cert->tbsCertificate = asn1_wrap(ASN1_SEQUENCE, "mmccmcmm",
1276 asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2),
1277 asn1_integer("c", this->cert->serialNumber),
1278 asn1_algorithmIdentifier(this->cert->algorithm),
1279 issuer->get_encoding(issuer),
1280 asn1_wrap(ASN1_SEQUENCE, "mm",
1281 asn1_from_time(&this->cert->notBefore, ASN1_UTCTIME),
1282 asn1_from_time(&this->cert->notAfter, ASN1_UTCTIME)),
1283 subject->get_encoding(subject),
1284 key_info, extensions);
1285
1286 if (!this->sign_key->sign(this->sign_key, scheme,
1287 this->cert->tbsCertificate, &this->cert->signature))
1288 {
1289 return FALSE;
1290 }
1291 this->cert->encoding = asn1_wrap(ASN1_SEQUENCE, "ccm",
1292 this->cert->tbsCertificate,
1293 asn1_algorithmIdentifier(this->cert->algorithm),
1294 asn1_bitstring("c", this->cert->signature));
1295
1296 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1297 if (!hasher)
1298 {
1299 return FALSE;
1300 }
1301 hasher->allocate_hash(hasher, this->cert->encoding,
1302 &this->cert->encoding_hash);
1303 hasher->destroy(hasher);
1304 return TRUE;
1305 }
1306
1307 /**
1308 * Implementation of builder_t.build
1309 */
1310 static private_x509_cert_t *build(private_builder_t *this)
1311 {
1312 private_x509_cert_t *cert;
1313
1314 if (this->cert)
1315 {
1316 this->cert->flags |= this->flags;
1317 if (!this->cert->encoding.ptr)
1318 { /* generate a new certificate */
1319 if (!this->sign_key || !generate(this))
1320 {
1321 destroy(this->cert);
1322 free(this);
1323 return NULL;
1324 }
1325 }
1326 }
1327 cert = this->cert;
1328 free(this);
1329 return cert;
1330 }
1331
1332 /**
1333 * Implementation of builder_t.add
1334 */
1335 static void add(private_builder_t *this, builder_part_t part, ...)
1336 {
1337 va_list args;
1338 chunk_t chunk;
1339 bool handled = TRUE;
1340
1341 va_start(args, part);
1342 switch (part)
1343 {
1344 case BUILD_BLOB_ASN1_DER:
1345 chunk = va_arg(args, chunk_t);
1346 this->cert = create_from_chunk(chunk_clone(chunk));
1347 break;
1348 case BUILD_X509_FLAG:
1349 this->flags = va_arg(args, x509_flag_t);
1350 break;
1351 case BUILD_SIGNING_KEY:
1352 this->sign_key = va_arg(args, private_key_t*);
1353 break;
1354 case BUILD_SIGNING_CERT:
1355 this->sign_cert = va_arg(args, certificate_t*);
1356 break;
1357 default:
1358 /* all other parts need an empty cert */
1359 if (!this->cert)
1360 {
1361 this->cert = create_empty();
1362 }
1363 handled = FALSE;
1364 break;
1365 }
1366 if (handled)
1367 {
1368 va_end(args);
1369 return;
1370 }
1371
1372 switch (part)
1373 {
1374 case BUILD_PUBLIC_KEY:
1375 {
1376 public_key_t *key = va_arg(args, public_key_t*);
1377 this->cert->public_key = key->get_ref(key);
1378 break;
1379 }
1380 case BUILD_SUBJECT:
1381 {
1382 identification_t *id = va_arg(args, identification_t*);
1383 this->cert->subject = id->clone(id);
1384 break;
1385 }
1386 case BUILD_SUBJECT_ALTNAME:
1387 {
1388 identification_t *id = va_arg(args, identification_t*);
1389 this->cert->subjectAltNames->insert_last(
1390 this->cert->subjectAltNames, id->clone(id));
1391 break;
1392 }
1393 case BUILD_NOT_BEFORE_TIME:
1394 this->cert->notBefore = va_arg(args, time_t);
1395 break;
1396 case BUILD_NOT_AFTER_TIME:
1397 this->cert->notAfter = va_arg(args, time_t);
1398 break;
1399 case BUILD_SERIAL:
1400 {
1401 chunk_t serial = va_arg(args, chunk_t);
1402 this->cert->serialNumber = chunk_clone(serial);
1403 break;
1404 }
1405 default:
1406 /* abort if unsupported option */
1407 if (this->cert)
1408 {
1409 destroy(this->cert);
1410 }
1411 builder_cancel(&this->public);
1412 break;
1413 }
1414 va_end(args);
1415 }
1416
1417 /**
1418 * Builder construction function
1419 */
1420 builder_t *x509_cert_builder(certificate_type_t type)
1421 {
1422 private_builder_t *this;
1423
1424 if (type != CERT_X509)
1425 {
1426 return NULL;
1427 }
1428
1429 this = malloc_thing(private_builder_t);
1430
1431 this->cert = NULL;
1432 this->flags = 0;
1433 this->sign_cert = NULL;
1434 this->sign_key = NULL;
1435 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
1436 this->public.build = (void*(*)(builder_t *this))build;
1437
1438 return &this->public;
1439 }
1440