moved very stroke specific x509 flag handling out of core library
[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 <asn1/pem.h>
37 #include <crypto/hashers/hasher.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 identification_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 identification_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 identification_t *authKeyIdentifier = NULL;
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 = identification_create_from_encoding(
443 ID_PUBKEY_SHA1, object);
444 break;
445 case AUTH_KEY_ID_CERT_ISSUER:
446 /* TODO: x509_parse_generalNames(object, level+1, TRUE); */
447 break;
448 case AUTH_KEY_ID_CERT_SERIAL:
449 *authKeySerialNumber = object;
450 break;
451 default:
452 break;
453 }
454 }
455 parser->destroy(parser);
456 return authKeyIdentifier;
457 }
458
459 /**
460 * ASN.1 definition of a authorityInfoAccess extension
461 */
462 static const asn1Object_t authInfoAccessObjects[] = {
463 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
464 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
465 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
466 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
467 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
468 { 0, "exit", ASN1_EOC, ASN1_EXIT }
469 };
470 #define AUTH_INFO_ACCESS_METHOD 2
471 #define AUTH_INFO_ACCESS_LOCATION 3
472
473 /**
474 * Extracts an authorityInfoAcess location
475 */
476 static void parse_authorityInfoAccess(chunk_t blob, int level0,
477 private_x509_cert_t *this)
478 {
479 asn1_parser_t *parser;
480 chunk_t object;
481 int objectID;
482 int accessMethod = OID_UNKNOWN;
483
484 parser = asn1_parser_create(authInfoAccessObjects, blob);
485 parser->set_top_level(parser, level0);
486
487 while (parser->iterate(parser, &objectID, &object))
488 {
489 switch (objectID)
490 {
491 case AUTH_INFO_ACCESS_METHOD:
492 accessMethod = asn1_known_oid(object);
493 break;
494 case AUTH_INFO_ACCESS_LOCATION:
495 {
496 switch (accessMethod)
497 {
498 case OID_OCSP:
499 case OID_CA_ISSUERS:
500 {
501 identification_t *id;
502 char *uri;
503
504 id = parse_generalName(object,
505 parser->get_level(parser)+1);
506 if (id == NULL)
507 {
508 /* parsing went wrong - abort */
509 goto end;
510 }
511 DBG2(" '%Y'", id);
512 if (accessMethod == OID_OCSP &&
513 asprintf(&uri, "%Y", id) > 0)
514 {
515 this->ocsp_uris->insert_last(this->ocsp_uris, uri);
516 }
517 id->destroy(id);
518 }
519 break;
520 default:
521 /* unkown accessMethod, ignoring */
522 break;
523 }
524 break;
525 }
526 default:
527 break;
528 }
529 }
530
531 end:
532 parser->destroy(parser);
533 }
534
535 /**
536 * ASN.1 definition of a extendedKeyUsage extension
537 */
538 static const asn1Object_t extendedKeyUsageObjects[] = {
539 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
540 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
541 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
542 { 0, "exit", ASN1_EOC, ASN1_EXIT }
543 };
544 #define EXT_KEY_USAGE_PURPOSE_ID 1
545
546 /**
547 * Extracts extendedKeyUsage OIDs - currently only OCSP_SIGING is returned
548 */
549 static bool parse_extendedKeyUsage(chunk_t blob, int level0)
550 {
551 asn1_parser_t *parser;
552 chunk_t object;
553 int objectID;
554 bool ocsp_signing = FALSE;
555
556 parser = asn1_parser_create(extendedKeyUsageObjects, blob);
557 parser->set_top_level(parser, level0);
558
559 while (parser->iterate(parser, &objectID, &object))
560 {
561 if (objectID == EXT_KEY_USAGE_PURPOSE_ID &&
562 asn1_known_oid(object) == OID_OCSP_SIGNING)
563 {
564 ocsp_signing = TRUE;
565 }
566 }
567 parser->destroy(parser);
568 return ocsp_signing;
569 }
570
571 /**
572 * ASN.1 definition of crlDistributionPoints
573 */
574 static const asn1Object_t crlDistributionPointsObjects[] = {
575 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
576 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
577 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
578 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
579 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
580 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
581 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
582 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
583 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
584 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
585 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
586 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
587 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
588 { 0, "exit", ASN1_EOC, ASN1_EXIT }
589 };
590 #define CRL_DIST_POINTS_FULLNAME 3
591
592 /**
593 * Extracts one or several crlDistributionPoints into a list
594 */
595 static void parse_crlDistributionPoints(chunk_t blob, int level0,
596 private_x509_cert_t *this)
597 {
598 asn1_parser_t *parser;
599 chunk_t object;
600 int objectID;
601 linked_list_t *list = linked_list_create();
602
603 parser = asn1_parser_create(crlDistributionPointsObjects, blob);
604 parser->set_top_level(parser, level0);
605
606 while (parser->iterate(parser, &objectID, &object))
607 {
608 if (objectID == CRL_DIST_POINTS_FULLNAME)
609 {
610 identification_t *id;
611
612 /* append extracted generalNames to existing chained list */
613 x509_parse_generalNames(object, parser->get_level(parser)+1,
614 TRUE, list);
615
616 while (list->remove_last(list, (void**)&id) == SUCCESS)
617 {
618 char *uri;
619
620 if (asprintf(&uri, "%Y", id) > 0)
621 {
622 this->crl_uris->insert_last(this->crl_uris, uri);
623 }
624 id->destroy(id);
625 }
626 }
627 }
628 parser->destroy(parser);
629 list->destroy(list);
630 }
631
632 /**
633 * ASN.1 definition of an X.509v3 x509_cert
634 */
635 static const asn1Object_t certObjects[] = {
636 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
637 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
638 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
639 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
640 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
641 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
642 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
643 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
644 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
645 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
646 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
647 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
648 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
649 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
650 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
651 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
652 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
653 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
654 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
655 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
656 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
657 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
658 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
659 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
660 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
661 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
662 { 0, "exit", ASN1_EOC, ASN1_EXIT }
663 };
664 #define X509_OBJ_TBS_CERTIFICATE 1
665 #define X509_OBJ_VERSION 3
666 #define X509_OBJ_SERIAL_NUMBER 4
667 #define X509_OBJ_SIG_ALG 5
668 #define X509_OBJ_ISSUER 6
669 #define X509_OBJ_NOT_BEFORE 8
670 #define X509_OBJ_NOT_AFTER 9
671 #define X509_OBJ_SUBJECT 10
672 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
673 #define X509_OBJ_EXTN_ID 19
674 #define X509_OBJ_CRITICAL 20
675 #define X509_OBJ_EXTN_VALUE 21
676 #define X509_OBJ_ALGORITHM 24
677 #define X509_OBJ_SIGNATURE 25
678
679 /**
680 * Parses an X.509v3 certificate
681 */
682 static bool parse_certificate(private_x509_cert_t *this)
683 {
684 asn1_parser_t *parser;
685 chunk_t object;
686 int objectID;
687 int extn_oid = OID_UNKNOWN;
688 int sig_alg = OID_UNKNOWN;
689 bool success = FALSE;
690 bool critical;
691
692 parser = asn1_parser_create(certObjects, this->encoding);
693
694 while (parser->iterate(parser, &objectID, &object))
695 {
696 u_int level = parser->get_level(parser)+1;
697
698 switch (objectID)
699 {
700 case X509_OBJ_TBS_CERTIFICATE:
701 this->tbsCertificate = object;
702 break;
703 case X509_OBJ_VERSION:
704 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
705 DBG2(" v%d", this->version);
706 break;
707 case X509_OBJ_SERIAL_NUMBER:
708 this->serialNumber = object;
709 break;
710 case X509_OBJ_SIG_ALG:
711 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
712 break;
713 case X509_OBJ_ISSUER:
714 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
715 DBG2(" '%Y'", this->issuer);
716 break;
717 case X509_OBJ_NOT_BEFORE:
718 this->notBefore = asn1_parse_time(object, level);
719 break;
720 case X509_OBJ_NOT_AFTER:
721 this->notAfter = asn1_parse_time(object, level);
722 break;
723 case X509_OBJ_SUBJECT:
724 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
725 DBG2(" '%Y'", this->subject);
726 break;
727 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
728 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
729 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
730 if (this->public_key == NULL)
731 {
732 DBG1("could not create public key");
733 goto end;
734 }
735 break;
736 case X509_OBJ_EXTN_ID:
737 extn_oid = asn1_known_oid(object);
738 break;
739 case X509_OBJ_CRITICAL:
740 critical = object.len && *object.ptr;
741 DBG2(" %s", critical ? "TRUE" : "FALSE");
742 break;
743 case X509_OBJ_EXTN_VALUE:
744 {
745 switch (extn_oid)
746 {
747 case OID_SUBJECT_KEY_ID:
748 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
749 level, "keyIdentifier"))
750 {
751 goto end;
752 }
753 this->subjectKeyID = object;
754 break;
755 case OID_SUBJECT_ALT_NAME:
756 x509_parse_generalNames(object, level, FALSE,
757 this->subjectAltNames);
758 break;
759 case OID_BASIC_CONSTRAINTS:
760 if (parse_basicConstraints(object, level))
761 {
762 this->flags |= X509_CA;
763 }
764 break;
765 case OID_CRL_DISTRIBUTION_POINTS:
766 parse_crlDistributionPoints(object, level, this);
767 break;
768 case OID_AUTHORITY_KEY_ID:
769 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
770 level, &this->authKeySerialNumber);
771 break;
772 case OID_AUTHORITY_INFO_ACCESS:
773 parse_authorityInfoAccess(object, level, this);
774 break;
775 case OID_EXTENDED_KEY_USAGE:
776 if (parse_extendedKeyUsage(object, level))
777 {
778 this->flags |= X509_OCSP_SIGNER;
779 }
780 break;
781 case OID_NS_REVOCATION_URL:
782 case OID_NS_CA_REVOCATION_URL:
783 case OID_NS_CA_POLICY_URL:
784 case OID_NS_COMMENT:
785 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING,
786 level, oid_names[extn_oid].name))
787 {
788 goto end;
789 }
790 break;
791 default:
792 break;
793 }
794 break;
795 }
796 case X509_OBJ_ALGORITHM:
797 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
798 if (this->algorithm != sig_alg)
799 {
800 DBG1(" signature algorithms do not agree");
801 goto end;
802 }
803 break;
804 case X509_OBJ_SIGNATURE:
805 this->signature = object;
806 break;
807 default:
808 break;
809 }
810 }
811 success = parser->success(parser);
812
813 end:
814 parser->destroy(parser);
815 return success;
816 }
817
818 /**
819 * Implementation of certificate_t.get_type
820 */
821 static certificate_type_t get_type(private_x509_cert_t *this)
822 {
823 return CERT_X509;
824 }
825
826 /**
827 * Implementation of certificate_t.get_subject
828 */
829 static identification_t* get_subject(private_x509_cert_t *this)
830 {
831 return this->subject;
832 }
833
834 /**
835 * Implementation of certificate_t.get_issuer
836 */
837 static identification_t* get_issuer(private_x509_cert_t *this)
838 {
839 return this->issuer;
840 }
841
842 /**
843 * Implementation of certificate_t.has_subject.
844 */
845 static id_match_t has_subject(private_x509_cert_t *this, identification_t *subject)
846 {
847 identification_t *current;
848 enumerator_t *enumerator;
849 id_match_t match, best;
850
851 if (this->encoding_hash.ptr && subject->get_type(subject) == ID_CERT_DER_SHA1 &&
852 chunk_equals(this->encoding_hash, subject->get_encoding(subject)))
853 {
854 return ID_MATCH_PERFECT;
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 /* TODO: generic OID to scheme mapper? */
913 switch (this->algorithm)
914 {
915 case OID_MD5_WITH_RSA:
916 scheme = SIGN_RSA_EMSA_PKCS1_MD5;
917 break;
918 case OID_SHA1_WITH_RSA:
919 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
920 break;
921 case OID_SHA256_WITH_RSA:
922 scheme = SIGN_RSA_EMSA_PKCS1_SHA256;
923 break;
924 case OID_SHA384_WITH_RSA:
925 scheme = SIGN_RSA_EMSA_PKCS1_SHA384;
926 break;
927 case OID_SHA512_WITH_RSA:
928 scheme = SIGN_RSA_EMSA_PKCS1_SHA512;
929 break;
930 case OID_ECDSA_WITH_SHA1:
931 scheme = SIGN_ECDSA_WITH_SHA1;
932 break;
933 default:
934 return FALSE;
935 }
936 key = issuer->get_public_key(issuer);
937 if (key == NULL)
938 {
939 return FALSE;
940 }
941 /* TODO: add a lightweight check option (comparing auth/subject keyids only) */
942 valid = key->verify(key, scheme, this->tbsCertificate, this->signature);
943 key->destroy(key);
944 return valid;
945 }
946
947 /**
948 * Implementation of certificate_t.get_public_key
949 */
950 static public_key_t* get_public_key(private_x509_cert_t *this)
951 {
952 this->public_key->get_ref(this->public_key);
953 return this->public_key;
954 }
955
956 /**
957 * Implementation of certificate_t.asdf
958 */
959 static private_x509_cert_t* get_ref(private_x509_cert_t *this)
960 {
961 ref_get(&this->ref);
962 return this;
963 }
964
965 /**
966 * Implementation of x509_cert_t.get_flags.
967 */
968 static x509_flag_t get_flags(private_x509_cert_t *this)
969 {
970 return this->flags;
971 }
972
973 /**
974 * Implementation of x509_cert_t.get_validity.
975 */
976 static bool get_validity(private_x509_cert_t *this, time_t *when,
977 time_t *not_before, time_t *not_after)
978 {
979 time_t t;
980
981 if (when)
982 {
983 t = *when;
984 }
985 else
986 {
987 t = time(NULL);
988 }
989 if (not_before)
990 {
991 *not_before = this->notBefore;
992 }
993 if (not_after)
994 {
995 *not_after = this->notAfter;
996 }
997 return (t >= this->notBefore && t <= this->notAfter);
998 }
999
1000 /**
1001 * Implementation of certificate_t.is_newer.
1002 */
1003 static bool is_newer(certificate_t *this, certificate_t *that)
1004 {
1005 time_t this_update, that_update, now = time(NULL);
1006 bool new;
1007
1008 this->get_validity(this, &now, &this_update, NULL);
1009 that->get_validity(that, &now, &that_update, NULL);
1010 new = this_update > that_update;
1011 DBG1(" certificate from %T is %s - existing certificate from %T %s",
1012 &this_update, FALSE, new ? "newer":"not newer",
1013 &that_update, FALSE, new ? "replaced":"retained");
1014 return new;
1015 }
1016
1017 /**
1018 * Implementation of certificate_t.get_encoding.
1019 */
1020 static chunk_t get_encoding(private_x509_cert_t *this)
1021 {
1022 return chunk_clone(this->encoding);
1023 }
1024
1025 /**
1026 * Implementation of certificate_t.equals.
1027 */
1028 static bool equals(private_x509_cert_t *this, certificate_t *other)
1029 {
1030 chunk_t encoding;
1031 bool equal;
1032
1033 if (this == (private_x509_cert_t*)other)
1034 {
1035 return TRUE;
1036 }
1037 if (other->get_type(other) != CERT_X509)
1038 {
1039 return FALSE;
1040 }
1041 if (other->equals == (void*)equals)
1042 { /* skip allocation if we have the same implementation */
1043 return chunk_equals(this->encoding, ((private_x509_cert_t*)other)->encoding);
1044 }
1045 encoding = other->get_encoding(other);
1046 equal = chunk_equals(this->encoding, encoding);
1047 free(encoding.ptr);
1048 return equal;
1049 }
1050
1051 /**
1052 * Implementation of x509_t.get_serial.
1053 */
1054 static chunk_t get_serial(private_x509_cert_t *this)
1055 {
1056 return this->serialNumber;
1057 }
1058
1059 /**
1060 * Implementation of x509_t.get_authKeyIdentifier.
1061 */
1062 static identification_t *get_authKeyIdentifier(private_x509_cert_t *this)
1063 {
1064 return this->authKeyIdentifier;
1065 }
1066
1067 /**
1068 * Implementation of x509_cert_t.create_subjectAltName_enumerator.
1069 */
1070 static enumerator_t* create_subjectAltName_enumerator(private_x509_cert_t *this)
1071 {
1072 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
1073 }
1074
1075 /**
1076 * Implementation of x509_cert_t.create_ocsp_uri_enumerator.
1077 */
1078 static enumerator_t* create_ocsp_uri_enumerator(private_x509_cert_t *this)
1079 {
1080 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
1081 }
1082
1083 /**
1084 * Implementation of x509_cert_t.create_crl_uri_enumerator.
1085 */
1086 static enumerator_t* create_crl_uri_enumerator(private_x509_cert_t *this)
1087 {
1088 return this->crl_uris->create_enumerator(this->crl_uris);
1089 }
1090
1091 /**
1092 * Implementation of certificate_t.asdf
1093 */
1094 static void destroy(private_x509_cert_t *this)
1095 {
1096 if (ref_put(&this->ref))
1097 {
1098 this->subjectAltNames->destroy_offset(this->subjectAltNames,
1099 offsetof(identification_t, destroy));
1100 this->crl_uris->destroy_function(this->crl_uris, free);
1101 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
1102 DESTROY_IF(this->issuer);
1103 DESTROY_IF(this->subject);
1104 DESTROY_IF(this->public_key);
1105 DESTROY_IF(this->authKeyIdentifier);
1106 chunk_free(&this->encoding);
1107 chunk_free(&this->encoding_hash);
1108 if (!this->parsed)
1109 { /* only parsed certificates point these fields to "encoded" */
1110 chunk_free(&this->signature);
1111 chunk_free(&this->serialNumber);
1112 chunk_free(&this->tbsCertificate);
1113 }
1114 free(this);
1115 }
1116 }
1117
1118 /**
1119 * create an empty but initialized X.509 certificate
1120 */
1121 static private_x509_cert_t* create_empty(void)
1122 {
1123 private_x509_cert_t *this = malloc_thing(private_x509_cert_t);
1124
1125 this->public.interface.interface.get_type = (certificate_type_t (*)(certificate_t *this))get_type;
1126 this->public.interface.interface.get_subject = (identification_t* (*)(certificate_t *this))get_subject;
1127 this->public.interface.interface.get_issuer = (identification_t* (*)(certificate_t *this))get_issuer;
1128 this->public.interface.interface.has_subject = (id_match_t (*)(certificate_t*, identification_t *subject))has_subject;
1129 this->public.interface.interface.has_issuer = (id_match_t (*)(certificate_t*, identification_t *issuer))has_issuer;
1130 this->public.interface.interface.issued_by = (bool (*)(certificate_t *this, certificate_t *issuer))issued_by;
1131 this->public.interface.interface.get_public_key = (public_key_t* (*)(certificate_t *this))get_public_key;
1132 this->public.interface.interface.get_validity = (bool (*)(certificate_t*, time_t *when, time_t *, time_t*))get_validity;
1133 this->public.interface.interface.is_newer = (bool (*)(certificate_t*,certificate_t*))is_newer;
1134 this->public.interface.interface.get_encoding = (chunk_t (*)(certificate_t*))get_encoding;
1135 this->public.interface.interface.equals = (bool (*)(certificate_t*, certificate_t *other))equals;
1136 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t *this))get_ref;
1137 this->public.interface.interface.destroy = (void (*)(certificate_t *this))destroy;
1138 this->public.interface.get_flags = (x509_flag_t (*)(x509_t*))get_flags;
1139 this->public.interface.get_serial = (chunk_t (*)(x509_t*))get_serial;
1140 this->public.interface.get_authKeyIdentifier = (identification_t* (*)(x509_t*))get_authKeyIdentifier;
1141 this->public.interface.create_subjectAltName_enumerator = (enumerator_t* (*)(x509_t*))create_subjectAltName_enumerator;
1142 this->public.interface.create_crl_uri_enumerator = (enumerator_t* (*)(x509_t*))create_crl_uri_enumerator;
1143 this->public.interface.create_ocsp_uri_enumerator = (enumerator_t* (*)(x509_t*))create_ocsp_uri_enumerator;
1144
1145 this->encoding = chunk_empty;
1146 this->encoding_hash = chunk_empty;
1147 this->tbsCertificate = chunk_empty;
1148 this->version = 3;
1149 this->serialNumber = chunk_empty;
1150 this->notBefore = 0;
1151 this->notAfter = 0;
1152 this->public_key = NULL;
1153 this->subject = NULL;
1154 this->issuer = NULL;
1155 this->subjectAltNames = linked_list_create();
1156 this->crl_uris = linked_list_create();
1157 this->ocsp_uris = linked_list_create();
1158 this->subjectKeyID = chunk_empty;
1159 this->authKeyIdentifier = NULL;
1160 this->authKeySerialNumber = chunk_empty;
1161 this->algorithm = 0;
1162 this->signature = chunk_empty;
1163 this->flags = 0;
1164 this->ref = 1;
1165 this->parsed = FALSE;
1166
1167 return this;
1168 }
1169
1170 /**
1171 * create an X.509 certificate from a chunk
1172 */
1173 static private_x509_cert_t *create_from_chunk(chunk_t chunk)
1174 {
1175 hasher_t *hasher;
1176 private_x509_cert_t *this = create_empty();
1177
1178 this->encoding = chunk;
1179 if (!parse_certificate(this))
1180 {
1181 destroy(this);
1182 return NULL;
1183 }
1184
1185 /* check if the certificate is self-signed */
1186 if (issued_by(this, &this->public.interface.interface))
1187 {
1188 this->flags |= X509_SELF_SIGNED;
1189 }
1190
1191 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1192 if (hasher != NULL)
1193 {
1194 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
1195 hasher->destroy(hasher);
1196 }
1197 else
1198 {
1199 DBG1(" unable to create hash of certificate, SHA1 not supported");
1200 }
1201
1202 this->parsed = TRUE;
1203 return this;
1204 }
1205
1206 /**
1207 * create an X.509 certificate from a file
1208 */
1209 static private_x509_cert_t *create_from_file(char *path)
1210 {
1211 bool pgp = FALSE;
1212 chunk_t chunk;
1213 private_x509_cert_t *this;
1214
1215 if (!pem_asn1_load_file(path, NULL, &chunk, &pgp))
1216 {
1217 return NULL;
1218 }
1219
1220 this = create_from_chunk(chunk);
1221
1222 if (this == NULL)
1223 {
1224 DBG1(" could not parse loaded certificate file '%s'",path);
1225 return NULL;
1226 }
1227 DBG1(" loaded certificate file '%s'", path);
1228 return this;
1229 }
1230
1231 typedef struct private_builder_t private_builder_t;
1232 /**
1233 * Builder implementation for certificate loading
1234 */
1235 struct private_builder_t {
1236 /** implements the builder interface */
1237 builder_t public;
1238 /** loaded certificate */
1239 private_x509_cert_t *cert;
1240 /** additional flags to enforce */
1241 x509_flag_t flags;
1242 /** certificate to sign, if we generate a new cert */
1243 certificate_t *sign_cert;
1244 /** private key to sign, if we generate a new cert */
1245 private_key_t *sign_key;
1246 };
1247
1248 /**
1249 * Generate and sign a new certificate
1250 */
1251 static bool generate(private_builder_t *this)
1252 {
1253 chunk_t extensions = chunk_empty;
1254 identification_t *issuer, *subject;
1255 chunk_t key_info, key;
1256 signature_scheme_t scheme;
1257 hasher_t *hasher;
1258
1259 subject = this->cert->subject;
1260 if (this->sign_cert)
1261 {
1262 issuer = this->sign_cert->get_subject(this->sign_cert);
1263 if (!this->cert->public_key)
1264 {
1265 return FALSE;
1266 }
1267 }
1268 else
1269 { /* self signed */
1270 issuer = subject;
1271 if (!this->cert->public_key)
1272 {
1273 this->cert->public_key = this->sign_key->get_public_key(this->sign_key);
1274 }
1275 this->flags |= X509_SELF_SIGNED;
1276 }
1277 this->cert->issuer = issuer->clone(issuer);
1278 if (!this->cert->notBefore)
1279 {
1280 this->cert->notBefore = time(NULL);
1281 }
1282 if (!this->cert->notAfter)
1283 { /* defaults to 1 years from now on */
1284 this->cert->notAfter = this->cert->notBefore + 60 * 60 * 24 * 365;
1285 }
1286 this->cert->flags = this->flags;
1287
1288 switch (this->sign_key->get_type(this->sign_key))
1289 {
1290 case KEY_RSA:
1291 this->cert->algorithm = OID_SHA1_WITH_RSA;
1292 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
1293 break;
1294 default:
1295 return FALSE;
1296 }
1297
1298 switch (this->cert->public_key->get_type(this->cert->public_key))
1299 {
1300 case KEY_RSA:
1301 key = this->cert->public_key->get_encoding(this->cert->public_key);
1302 key_info = asn1_wrap(ASN1_SEQUENCE, "cm",
1303 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
1304 asn1_bitstring("m", key));
1305 break;
1306 default:
1307 return FALSE;
1308 }
1309
1310 if (this->cert->subjectAltNames->get_count(this->cert->subjectAltNames))
1311 {
1312 /* TODO: encode subjectAltNames */
1313 }
1314
1315 this->cert->tbsCertificate = asn1_wrap(ASN1_SEQUENCE, "mmccmcmm",
1316 asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2),
1317 asn1_simple_object(ASN1_INTEGER, this->cert->serialNumber),
1318 asn1_algorithmIdentifier(this->cert->algorithm),
1319 issuer->get_encoding(issuer),
1320 asn1_wrap(ASN1_SEQUENCE, "mm",
1321 asn1_from_time(&this->cert->notBefore, ASN1_UTCTIME),
1322 asn1_from_time(&this->cert->notAfter, ASN1_UTCTIME)),
1323 subject->get_encoding(subject),
1324 key_info, extensions);
1325
1326 if (!this->sign_key->sign(this->sign_key, scheme,
1327 this->cert->tbsCertificate, &this->cert->signature))
1328 {
1329 return FALSE;
1330 }
1331 this->cert->encoding = asn1_wrap(ASN1_SEQUENCE, "ccm",
1332 this->cert->tbsCertificate,
1333 asn1_algorithmIdentifier(this->cert->algorithm),
1334 asn1_bitstring("c", this->cert->signature));
1335
1336 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1337 if (!hasher)
1338 {
1339 return FALSE;
1340 }
1341 hasher->allocate_hash(hasher, this->cert->encoding,
1342 &this->cert->encoding_hash);
1343 hasher->destroy(hasher);
1344 return TRUE;
1345 }
1346
1347 /**
1348 * Implementation of builder_t.build
1349 */
1350 static private_x509_cert_t *build(private_builder_t *this)
1351 {
1352 private_x509_cert_t *cert;
1353
1354 if (this->cert)
1355 {
1356 this->cert->flags |= this->flags;
1357 if (!this->cert->encoding.ptr)
1358 { /* generate a new certificate */
1359 if (!this->sign_key || !generate(this))
1360 {
1361 destroy(this->cert);
1362 free(this);
1363 return NULL;
1364 }
1365 }
1366 }
1367 cert = this->cert;
1368 free(this);
1369 return cert;
1370 }
1371
1372 /**
1373 * Implementation of builder_t.add
1374 */
1375 static void add(private_builder_t *this, builder_part_t part, ...)
1376 {
1377 va_list args;
1378 chunk_t chunk;
1379 bool handled = TRUE;
1380
1381 va_start(args, part);
1382 switch (part)
1383 {
1384 case BUILD_FROM_FILE:
1385 this->cert = create_from_file(va_arg(args, char*));
1386 break;
1387 case BUILD_BLOB_ASN1_DER:
1388 chunk = va_arg(args, chunk_t);
1389 this->cert = create_from_chunk(chunk_clone(chunk));
1390 break;
1391 case BUILD_X509_FLAG:
1392 this->flags = va_arg(args, x509_flag_t);
1393 break;
1394 case BUILD_SIGNING_KEY:
1395 this->sign_key = va_arg(args, private_key_t*);
1396 break;
1397 case BUILD_SIGNING_CERT:
1398 this->sign_cert = va_arg(args, certificate_t*);
1399 break;
1400 default:
1401 /* all other parts need an empty cert */
1402 if (!this->cert)
1403 {
1404 this->cert = create_empty();
1405 }
1406 handled = FALSE;
1407 break;
1408 }
1409 if (handled)
1410 {
1411 va_end(args);
1412 return;
1413 }
1414
1415 switch (part)
1416 {
1417 case BUILD_PUBLIC_KEY:
1418 {
1419 public_key_t *key = va_arg(args, public_key_t*);
1420 this->cert->public_key = key->get_ref(key);
1421 break;
1422 }
1423 case BUILD_SUBJECT:
1424 {
1425 identification_t *id = va_arg(args, identification_t*);
1426 this->cert->subject = id->clone(id);
1427 break;
1428 }
1429 case BUILD_SUBJECT_ALTNAME:
1430 {
1431 identification_t *id = va_arg(args, identification_t*);
1432 this->cert->subjectAltNames->insert_last(
1433 this->cert->subjectAltNames, id->clone(id));
1434 break;
1435 }
1436 case BUILD_NOT_BEFORE_TIME:
1437 this->cert->notBefore = va_arg(args, time_t);
1438 break;
1439 case BUILD_NOT_AFTER_TIME:
1440 this->cert->notAfter = va_arg(args, time_t);
1441 break;
1442 case BUILD_SERIAL:
1443 {
1444 chunk_t serial = va_arg(args, chunk_t);
1445 this->cert->serialNumber = chunk_clone(serial);
1446 break;
1447 }
1448 default:
1449 /* abort if unsupported option */
1450 if (this->cert)
1451 {
1452 destroy(this->cert);
1453 }
1454 builder_cancel(&this->public);
1455 break;
1456 }
1457 va_end(args);
1458 }
1459
1460 /**
1461 * Builder construction function
1462 */
1463 builder_t *x509_cert_builder(certificate_type_t type)
1464 {
1465 private_builder_t *this;
1466
1467 if (type != CERT_X509)
1468 {
1469 return NULL;
1470 }
1471
1472 this = malloc_thing(private_builder_t);
1473
1474 this->cert = NULL;
1475 this->flags = 0;
1476 this->sign_cert = NULL;
1477 this->sign_key = NULL;
1478 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
1479 this->public.build = (void*(*)(builder_t *this))build;
1480
1481 return &this->public;
1482 }
1483