pluto and scepclient use private and public key plugins of libstrongswan
[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 <credentials/keys/private_key.h>
39 #include <utils/linked_list.h>
40 #include <utils/identification.h>
41
42 /**
43 * Different kinds of generalNames
44 */
45 typedef enum {
46 GN_OTHER_NAME = 0,
47 GN_RFC822_NAME = 1,
48 GN_DNS_NAME = 2,
49 GN_X400_ADDRESS = 3,
50 GN_DIRECTORY_NAME = 4,
51 GN_EDI_PARTY_NAME = 5,
52 GN_URI = 6,
53 GN_IP_ADDRESS = 7,
54 GN_REGISTERED_ID = 8,
55 } generalNames_t;
56
57
58 typedef struct private_x509_cert_t private_x509_cert_t;
59
60 /**
61 * Private data of a x509_cert_t object.
62 */
63 struct private_x509_cert_t {
64 /**
65 * Public interface for this certificate.
66 */
67 x509_cert_t public;
68
69 /**
70 * X.509 certificate encoding in ASN.1 DER format
71 */
72 chunk_t encoding;
73
74 /**
75 * SHA1 hash of the DER encoding of this X.509 certificate
76 */
77 chunk_t encoding_hash;
78
79 /**
80 * X.509 certificate body over which signature is computed
81 */
82 chunk_t tbsCertificate;
83
84 /**
85 * Version of the X.509 certificate
86 */
87 u_int version;
88
89 /**
90 * Serial number of the X.509 certificate
91 */
92 chunk_t serialNumber;
93
94 /**
95 * ID representing the certificate issuer
96 */
97 identification_t *issuer;
98
99 /**
100 * Start time of certificate validity
101 */
102 time_t notBefore;
103
104 /**
105 * End time of certificate validity
106 */
107 time_t notAfter;
108
109 /**
110 * ID representing the certificate subject
111 */
112 identification_t *subject;
113
114 /**
115 * List of subjectAltNames as identification_t
116 */
117 linked_list_t *subjectAltNames;
118
119 /**
120 * List of crlDistributionPoints as allocated char*
121 */
122 linked_list_t *crl_uris;
123
124 /**
125 * List ocspAccessLocations as identification_t
126 */
127 linked_list_t *ocsp_uris;
128
129 /**
130 * certificates embedded public key
131 */
132 public_key_t *public_key;
133
134 /**
135 * Subject Key Identifier
136 */
137 chunk_t subjectKeyID;
138
139 /**
140 * Authority Key Identifier
141 */
142 identification_t *authKeyIdentifier;
143
144 /**
145 * Authority Key Serial Number
146 */
147 chunk_t authKeySerialNumber;
148
149 /**
150 * x509 constraints and other flags
151 */
152 x509_flag_t flags;
153
154 /**
155 * Signature algorithm
156 */
157 int algorithm;
158
159 /**
160 * Signature
161 */
162 chunk_t signature;
163
164 /**
165 * Certificate parsed from blob/file?
166 */
167 bool parsed;
168
169 /**
170 * reference count
171 */
172 refcount_t ref;
173 };
174
175 static u_char ASN1_sAN_oid_buf[] = {
176 0x06, 0x03, 0x55, 0x1D, 0x11
177 };
178 static const chunk_t ASN1_subjectAltName_oid = chunk_from_buf(ASN1_sAN_oid_buf);
179
180 /**
181 * ASN.1 definition of a basicConstraints extension
182 */
183 static const asn1Object_t basicConstraintsObjects[] = {
184 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
185 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
186 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
187 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
188 { 0, "exit", ASN1_EOC, ASN1_EXIT }
189 };
190 #define BASIC_CONSTRAINTS_CA 1
191
192 /**
193 * Extracts the basicConstraints extension
194 */
195 static bool parse_basicConstraints(chunk_t blob, int level0)
196 {
197 asn1_parser_t *parser;
198 chunk_t object;
199 int objectID;
200 bool isCA = FALSE;
201
202 parser = asn1_parser_create(basicConstraintsObjects, blob);
203 parser->set_top_level(parser, level0);
204
205 while (parser->iterate(parser, &objectID, &object))
206 {
207 if (objectID == BASIC_CONSTRAINTS_CA)
208 {
209 isCA = object.len && *object.ptr;
210 DBG2(" %s", isCA ? "TRUE" : "FALSE");
211 }
212 }
213 parser->destroy(parser);
214
215 return isCA;
216 }
217
218 /**
219 * ASN.1 definition of otherName
220 */
221 static const asn1Object_t otherNameObjects[] = {
222 {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */
223 {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY }, /* 1 */
224 {0, "exit", ASN1_EOC, ASN1_EXIT }
225 };
226 #define ON_OBJ_ID_TYPE 0
227 #define ON_OBJ_VALUE 1
228
229 /**
230 * Extracts an otherName
231 */
232 static bool parse_otherName(chunk_t blob, int level0)
233 {
234 asn1_parser_t *parser;
235 chunk_t object;
236 int objectID;
237 int oid = OID_UNKNOWN;
238 bool success = FALSE;
239
240 parser = asn1_parser_create(otherNameObjects, blob);
241 parser->set_top_level(parser, level0);
242
243 while (parser->iterate(parser, &objectID, &object))
244 {
245 switch (objectID)
246 {
247 case ON_OBJ_ID_TYPE:
248 oid = asn1_known_oid(object);
249 break;
250 case ON_OBJ_VALUE:
251 if (oid == OID_XMPP_ADDR)
252 {
253 if (!asn1_parse_simple_object(&object, ASN1_UTF8STRING,
254 parser->get_level(parser)+1, "xmppAddr"))
255 {
256 goto end;
257 }
258 }
259 break;
260 default:
261 break;
262 }
263 }
264 success = parser->success(parser);
265
266 end:
267 parser->destroy(parser);
268 return success;
269 }
270
271 /**
272 * ASN.1 definition of generalName
273 */
274 static const asn1Object_t generalNameObjects[] = {
275 { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */
276 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */
277 { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */
278 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */
279 { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */
280 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
281 { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */
282 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
283 { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */
284 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
285 { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */
286 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
287 { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */
288 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */
289 { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */
290 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */
291 { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */
292 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 17 */
293 { 0, "exit", ASN1_EOC, ASN1_EXIT }
294 };
295 #define GN_OBJ_OTHER_NAME 0
296 #define GN_OBJ_RFC822_NAME 2
297 #define GN_OBJ_DNS_NAME 4
298 #define GN_OBJ_X400_ADDRESS 6
299 #define GN_OBJ_DIRECTORY_NAME 8
300 #define GN_OBJ_EDI_PARTY_NAME 10
301 #define GN_OBJ_URI 12
302 #define GN_OBJ_IP_ADDRESS 14
303 #define GN_OBJ_REGISTERED_ID 16
304
305 /**
306 * Extracts a generalName
307 */
308 static identification_t *parse_generalName(chunk_t blob, int level0)
309 {
310 asn1_parser_t *parser;
311 chunk_t object;
312 int objectID ;
313
314 identification_t *gn = NULL;
315
316 parser = asn1_parser_create(generalNameObjects, blob);
317 parser->set_top_level(parser, level0);
318
319 while (parser->iterate(parser, &objectID, &object))
320 {
321 id_type_t id_type = ID_ANY;
322
323 switch (objectID)
324 {
325 case GN_OBJ_RFC822_NAME:
326 id_type = ID_RFC822_ADDR;
327 break;
328 case GN_OBJ_DNS_NAME:
329 id_type = ID_FQDN;
330 break;
331 case GN_OBJ_URI:
332 id_type = ID_DER_ASN1_GN_URI;
333 break;
334 case GN_OBJ_DIRECTORY_NAME:
335 id_type = ID_DER_ASN1_DN;
336 break;
337 case GN_OBJ_IP_ADDRESS:
338 id_type = ID_IPV4_ADDR;
339 break;
340 case GN_OBJ_OTHER_NAME:
341 if (!parse_otherName(object, parser->get_level(parser)+1))
342 {
343 goto end;
344 }
345 break;
346 case GN_OBJ_X400_ADDRESS:
347 case GN_OBJ_EDI_PARTY_NAME:
348 case GN_OBJ_REGISTERED_ID:
349 default:
350 break;
351 }
352 if (id_type != ID_ANY)
353 {
354 gn = identification_create_from_encoding(id_type, object);
355 DBG2(" '%Y'", gn);
356 goto end;
357 }
358 }
359
360 end:
361 parser->destroy(parser);
362 return gn;
363 }
364
365 /**
366 * ASN.1 definition of generalNames
367 */
368 static const asn1Object_t generalNamesObjects[] = {
369 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
370 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
371 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
372 { 0, "exit", ASN1_EOC, ASN1_EXIT }
373 };
374 #define GENERAL_NAMES_GN 1
375
376 /**
377 * Extracts one or several GNs and puts them into a chained list
378 */
379 void x509_parse_generalNames(chunk_t blob, int level0, bool implicit, linked_list_t *list)
380 {
381 asn1_parser_t *parser;
382 chunk_t object;
383 int objectID;
384
385 parser = asn1_parser_create(generalNamesObjects, blob);
386 parser->set_top_level(parser, level0);
387 parser->set_flags(parser, implicit, FALSE);
388
389 while (parser->iterate(parser, &objectID, &object))
390 {
391 if (objectID == GENERAL_NAMES_GN)
392 {
393 identification_t *gn = parse_generalName(object,
394 parser->get_level(parser)+1);
395
396 if (gn)
397 {
398 list->insert_last(list, (void *)gn);
399 }
400 }
401 }
402 parser->destroy(parser);
403 }
404
405 /**
406 * ASN.1 definition of a authorityKeyIdentifier extension
407 */
408 static const asn1Object_t authKeyIdentifierObjects[] = {
409 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
410 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_BODY }, /* 1 */
411 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
412 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
413 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
414 { 1, "authorityCertSerialNumber", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
415 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 6 */
416 { 0, "exit", ASN1_EOC, ASN1_EXIT }
417 };
418 #define AUTH_KEY_ID_KEY_ID 1
419 #define AUTH_KEY_ID_CERT_ISSUER 3
420 #define AUTH_KEY_ID_CERT_SERIAL 5
421
422 /**
423 * Extracts an authoritykeyIdentifier
424 */
425 identification_t* x509_parse_authorityKeyIdentifier(chunk_t blob, int level0,
426 chunk_t *authKeySerialNumber)
427 {
428 asn1_parser_t *parser;
429 chunk_t object;
430 int objectID;
431 identification_t *authKeyIdentifier = NULL;
432
433 *authKeySerialNumber = chunk_empty;
434
435 parser = asn1_parser_create(authKeyIdentifierObjects, blob);
436 parser->set_top_level(parser, level0);
437
438 while (parser->iterate(parser, &objectID, &object))
439 {
440 switch (objectID)
441 {
442 case AUTH_KEY_ID_KEY_ID:
443 authKeyIdentifier = identification_create_from_encoding(
444 ID_PUBKEY_SHA1, object);
445 break;
446 case AUTH_KEY_ID_CERT_ISSUER:
447 /* TODO: x509_parse_generalNames(object, level+1, TRUE); */
448 break;
449 case AUTH_KEY_ID_CERT_SERIAL:
450 *authKeySerialNumber = object;
451 break;
452 default:
453 break;
454 }
455 }
456 parser->destroy(parser);
457 return authKeyIdentifier;
458 }
459
460 /**
461 * ASN.1 definition of a authorityInfoAccess extension
462 */
463 static const asn1Object_t authInfoAccessObjects[] = {
464 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
465 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
466 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
467 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
468 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
469 { 0, "exit", ASN1_EOC, ASN1_EXIT }
470 };
471 #define AUTH_INFO_ACCESS_METHOD 2
472 #define AUTH_INFO_ACCESS_LOCATION 3
473
474 /**
475 * Extracts an authorityInfoAcess location
476 */
477 static void parse_authorityInfoAccess(chunk_t blob, int level0,
478 private_x509_cert_t *this)
479 {
480 asn1_parser_t *parser;
481 chunk_t object;
482 int objectID;
483 int accessMethod = OID_UNKNOWN;
484
485 parser = asn1_parser_create(authInfoAccessObjects, blob);
486 parser->set_top_level(parser, level0);
487
488 while (parser->iterate(parser, &objectID, &object))
489 {
490 switch (objectID)
491 {
492 case AUTH_INFO_ACCESS_METHOD:
493 accessMethod = asn1_known_oid(object);
494 break;
495 case AUTH_INFO_ACCESS_LOCATION:
496 {
497 switch (accessMethod)
498 {
499 case OID_OCSP:
500 case OID_CA_ISSUERS:
501 {
502 identification_t *id;
503 char *uri;
504
505 id = parse_generalName(object,
506 parser->get_level(parser)+1);
507 if (id == NULL)
508 {
509 /* parsing went wrong - abort */
510 goto end;
511 }
512 DBG2(" '%Y'", id);
513 if (accessMethod == OID_OCSP &&
514 asprintf(&uri, "%Y", id) > 0)
515 {
516 this->ocsp_uris->insert_last(this->ocsp_uris, uri);
517 }
518 id->destroy(id);
519 }
520 break;
521 default:
522 /* unkown accessMethod, ignoring */
523 break;
524 }
525 break;
526 }
527 default:
528 break;
529 }
530 }
531
532 end:
533 parser->destroy(parser);
534 }
535
536 /**
537 * ASN.1 definition of a extendedKeyUsage extension
538 */
539 static const asn1Object_t extendedKeyUsageObjects[] = {
540 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
541 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
542 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
543 { 0, "exit", ASN1_EOC, ASN1_EXIT }
544 };
545 #define EXT_KEY_USAGE_PURPOSE_ID 1
546
547 /**
548 * Extracts extendedKeyUsage OIDs - currently only OCSP_SIGING is returned
549 */
550 static bool parse_extendedKeyUsage(chunk_t blob, int level0)
551 {
552 asn1_parser_t *parser;
553 chunk_t object;
554 int objectID;
555 bool ocsp_signing = FALSE;
556
557 parser = asn1_parser_create(extendedKeyUsageObjects, blob);
558 parser->set_top_level(parser, level0);
559
560 while (parser->iterate(parser, &objectID, &object))
561 {
562 if (objectID == EXT_KEY_USAGE_PURPOSE_ID &&
563 asn1_known_oid(object) == OID_OCSP_SIGNING)
564 {
565 ocsp_signing = TRUE;
566 }
567 }
568 parser->destroy(parser);
569 return ocsp_signing;
570 }
571
572 /**
573 * ASN.1 definition of crlDistributionPoints
574 */
575 static const asn1Object_t crlDistributionPointsObjects[] = {
576 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
577 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
578 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
579 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
580 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
581 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
582 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
583 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
584 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
585 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
586 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
587 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
588 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
589 { 0, "exit", ASN1_EOC, ASN1_EXIT }
590 };
591 #define CRL_DIST_POINTS_FULLNAME 3
592
593 /**
594 * Extracts one or several crlDistributionPoints into a list
595 */
596 static void parse_crlDistributionPoints(chunk_t blob, int level0,
597 private_x509_cert_t *this)
598 {
599 asn1_parser_t *parser;
600 chunk_t object;
601 int objectID;
602 linked_list_t *list = linked_list_create();
603
604 parser = asn1_parser_create(crlDistributionPointsObjects, blob);
605 parser->set_top_level(parser, level0);
606
607 while (parser->iterate(parser, &objectID, &object))
608 {
609 if (objectID == CRL_DIST_POINTS_FULLNAME)
610 {
611 identification_t *id;
612
613 /* append extracted generalNames to existing chained list */
614 x509_parse_generalNames(object, parser->get_level(parser)+1,
615 TRUE, list);
616
617 while (list->remove_last(list, (void**)&id) == SUCCESS)
618 {
619 char *uri;
620
621 if (asprintf(&uri, "%Y", id) > 0)
622 {
623 this->crl_uris->insert_last(this->crl_uris, uri);
624 }
625 id->destroy(id);
626 }
627 }
628 }
629 parser->destroy(parser);
630 list->destroy(list);
631 }
632
633 /**
634 * ASN.1 definition of an X.509v3 x509_cert
635 */
636 static const asn1Object_t certObjects[] = {
637 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
638 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
639 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
640 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
641 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
642 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
643 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
644 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
645 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
646 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
647 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
648 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
649 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
650 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
651 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
652 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
653 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
654 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
655 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
656 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
657 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
658 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
659 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
660 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
661 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
662 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
663 { 0, "exit", ASN1_EOC, ASN1_EXIT }
664 };
665 #define X509_OBJ_TBS_CERTIFICATE 1
666 #define X509_OBJ_VERSION 3
667 #define X509_OBJ_SERIAL_NUMBER 4
668 #define X509_OBJ_SIG_ALG 5
669 #define X509_OBJ_ISSUER 6
670 #define X509_OBJ_NOT_BEFORE 8
671 #define X509_OBJ_NOT_AFTER 9
672 #define X509_OBJ_SUBJECT 10
673 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
674 #define X509_OBJ_EXTN_ID 19
675 #define X509_OBJ_CRITICAL 20
676 #define X509_OBJ_EXTN_VALUE 21
677 #define X509_OBJ_ALGORITHM 24
678 #define X509_OBJ_SIGNATURE 25
679
680 /**
681 * Parses an X.509v3 certificate
682 */
683 static bool parse_certificate(private_x509_cert_t *this)
684 {
685 asn1_parser_t *parser;
686 chunk_t object;
687 int objectID;
688 int extn_oid = OID_UNKNOWN;
689 int sig_alg = OID_UNKNOWN;
690 bool success = FALSE;
691 bool critical;
692
693 parser = asn1_parser_create(certObjects, this->encoding);
694
695 while (parser->iterate(parser, &objectID, &object))
696 {
697 u_int level = parser->get_level(parser)+1;
698
699 switch (objectID)
700 {
701 case X509_OBJ_TBS_CERTIFICATE:
702 this->tbsCertificate = object;
703 break;
704 case X509_OBJ_VERSION:
705 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
706 DBG2(" v%d", this->version);
707 break;
708 case X509_OBJ_SERIAL_NUMBER:
709 this->serialNumber = object;
710 break;
711 case X509_OBJ_SIG_ALG:
712 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
713 break;
714 case X509_OBJ_ISSUER:
715 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
716 DBG2(" '%Y'", this->issuer);
717 break;
718 case X509_OBJ_NOT_BEFORE:
719 this->notBefore = asn1_parse_time(object, level);
720 break;
721 case X509_OBJ_NOT_AFTER:
722 this->notAfter = asn1_parse_time(object, level);
723 break;
724 case X509_OBJ_SUBJECT:
725 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
726 DBG2(" '%Y'", this->subject);
727 break;
728 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
729 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
730 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
731 if (this->public_key == NULL)
732 {
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*))get_type;
1126 this->public.interface.interface.get_subject = (identification_t* (*) (certificate_t*))get_subject;
1127 this->public.interface.interface.get_issuer = (identification_t* (*) (certificate_t*))get_issuer;
1128 this->public.interface.interface.has_subject = (id_match_t (*) (certificate_t*, identification_t*))has_subject;
1129 this->public.interface.interface.has_issuer = (id_match_t (*) (certificate_t*, identification_t*))has_issuer;
1130 this->public.interface.interface.issued_by = (bool (*) (certificate_t*, certificate_t*))issued_by;
1131 this->public.interface.interface.get_public_key = (public_key_t* (*) (certificate_t*))get_public_key;
1132 this->public.interface.interface.get_validity = (bool (*) (certificate_t*, time_t*, 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*))equals;
1136 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t*))get_ref;
1137 this->public.interface.interface.destroy = (void (*)(certificate_t*))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 this->parsed = TRUE;
1180 if (!parse_certificate(this))
1181 {
1182 destroy(this);
1183 return NULL;
1184 }
1185
1186 /* check if the certificate is self-signed */
1187 if (issued_by(this, &this->public.interface.interface))
1188 {
1189 this->flags |= X509_SELF_SIGNED;
1190 }
1191
1192 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1193 if (hasher == NULL)
1194 {
1195 DBG1(" unable to create hash of certificate, SHA1 not supported");
1196 destroy(this);
1197 return NULL;
1198 }
1199 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
1200 hasher->destroy(hasher);
1201
1202 return this;
1203 }
1204
1205 /**
1206 * create an X.509 certificate from a file
1207 */
1208 static private_x509_cert_t *create_from_file(char *path)
1209 {
1210 bool pgp = FALSE;
1211 chunk_t chunk;
1212 private_x509_cert_t *this;
1213
1214 if (!pem_asn1_load_file(path, NULL, &chunk, &pgp))
1215 {
1216 return NULL;
1217 }
1218
1219 this = create_from_chunk(chunk);
1220
1221 if (this == NULL)
1222 {
1223 DBG1(" could not parse loaded certificate file '%s'",path);
1224 return NULL;
1225 }
1226 DBG1(" loaded certificate file '%s'", path);
1227 return this;
1228 }
1229
1230 typedef struct private_builder_t private_builder_t;
1231 /**
1232 * Builder implementation for certificate loading
1233 */
1234 struct private_builder_t {
1235 /** implements the builder interface */
1236 builder_t public;
1237 /** loaded certificate */
1238 private_x509_cert_t *cert;
1239 /** additional flags to enforce */
1240 x509_flag_t flags;
1241 /** certificate to sign, if we generate a new cert */
1242 certificate_t *sign_cert;
1243 /** private key to sign, if we generate a new cert */
1244 private_key_t *sign_key;
1245 };
1246
1247 /**
1248 * Generate and sign a new certificate
1249 */
1250 static bool generate(private_builder_t *this)
1251 {
1252 chunk_t extensions = chunk_empty;
1253 identification_t *issuer, *subject;
1254 chunk_t key_info, key;
1255 signature_scheme_t scheme;
1256 hasher_t *hasher;
1257
1258 subject = this->cert->subject;
1259 if (this->sign_cert)
1260 {
1261 issuer = this->sign_cert->get_subject(this->sign_cert);
1262 if (!this->cert->public_key)
1263 {
1264 return FALSE;
1265 }
1266 }
1267 else
1268 { /* self signed */
1269 issuer = subject;
1270 if (!this->cert->public_key)
1271 {
1272 this->cert->public_key = this->sign_key->get_public_key(this->sign_key);
1273 }
1274 this->flags |= X509_SELF_SIGNED;
1275 }
1276 this->cert->issuer = issuer->clone(issuer);
1277 if (!this->cert->notBefore)
1278 {
1279 this->cert->notBefore = time(NULL);
1280 }
1281 if (!this->cert->notAfter)
1282 { /* defaults to 1 years from now on */
1283 this->cert->notAfter = this->cert->notBefore + 60 * 60 * 24 * 365;
1284 }
1285 this->cert->flags = this->flags;
1286
1287 switch (this->sign_key->get_type(this->sign_key))
1288 {
1289 case KEY_RSA:
1290 this->cert->algorithm = OID_SHA1_WITH_RSA;
1291 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
1292 break;
1293 default:
1294 return FALSE;
1295 }
1296
1297 switch (this->cert->public_key->get_type(this->cert->public_key))
1298 {
1299 case KEY_RSA:
1300 key = this->cert->public_key->get_encoding(this->cert->public_key);
1301 key_info = asn1_wrap(ASN1_SEQUENCE, "cm",
1302 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
1303 asn1_bitstring("m", key));
1304 break;
1305 default:
1306 return FALSE;
1307 }
1308
1309 if (this->cert->subjectAltNames->get_count(this->cert->subjectAltNames))
1310 {
1311 /* TODO: encode subjectAltNames */
1312 }
1313
1314 this->cert->tbsCertificate = asn1_wrap(ASN1_SEQUENCE, "mmccmcmm",
1315 asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2),
1316 asn1_integer("c", this->cert->serialNumber),
1317 asn1_algorithmIdentifier(this->cert->algorithm),
1318 issuer->get_encoding(issuer),
1319 asn1_wrap(ASN1_SEQUENCE, "mm",
1320 asn1_from_time(&this->cert->notBefore, ASN1_UTCTIME),
1321 asn1_from_time(&this->cert->notAfter, ASN1_UTCTIME)),
1322 subject->get_encoding(subject),
1323 key_info, extensions);
1324
1325 if (!this->sign_key->sign(this->sign_key, scheme,
1326 this->cert->tbsCertificate, &this->cert->signature))
1327 {
1328 return FALSE;
1329 }
1330 this->cert->encoding = asn1_wrap(ASN1_SEQUENCE, "ccm",
1331 this->cert->tbsCertificate,
1332 asn1_algorithmIdentifier(this->cert->algorithm),
1333 asn1_bitstring("c", this->cert->signature));
1334
1335 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1336 if (!hasher)
1337 {
1338 return FALSE;
1339 }
1340 hasher->allocate_hash(hasher, this->cert->encoding,
1341 &this->cert->encoding_hash);
1342 hasher->destroy(hasher);
1343 return TRUE;
1344 }
1345
1346 /**
1347 * Implementation of builder_t.build
1348 */
1349 static private_x509_cert_t *build(private_builder_t *this)
1350 {
1351 private_x509_cert_t *cert;
1352
1353 if (this->cert)
1354 {
1355 this->cert->flags |= this->flags;
1356 if (!this->cert->encoding.ptr)
1357 { /* generate a new certificate */
1358 if (!this->sign_key || !generate(this))
1359 {
1360 destroy(this->cert);
1361 free(this);
1362 return NULL;
1363 }
1364 }
1365 }
1366 cert = this->cert;
1367 free(this);
1368 return cert;
1369 }
1370
1371 /**
1372 * Implementation of builder_t.add
1373 */
1374 static void add(private_builder_t *this, builder_part_t part, ...)
1375 {
1376 va_list args;
1377 chunk_t chunk;
1378 bool handled = TRUE;
1379
1380 va_start(args, part);
1381 switch (part)
1382 {
1383 case BUILD_FROM_FILE:
1384 this->cert = create_from_file(va_arg(args, char*));
1385 break;
1386 case BUILD_BLOB_ASN1_DER:
1387 chunk = va_arg(args, chunk_t);
1388 this->cert = create_from_chunk(chunk_clone(chunk));
1389 break;
1390 case BUILD_X509_FLAG:
1391 this->flags = va_arg(args, x509_flag_t);
1392 break;
1393 case BUILD_SIGNING_KEY:
1394 this->sign_key = va_arg(args, private_key_t*);
1395 break;
1396 case BUILD_SIGNING_CERT:
1397 this->sign_cert = va_arg(args, certificate_t*);
1398 break;
1399 default:
1400 /* all other parts need an empty cert */
1401 if (!this->cert)
1402 {
1403 this->cert = create_empty();
1404 }
1405 handled = FALSE;
1406 break;
1407 }
1408 if (handled)
1409 {
1410 va_end(args);
1411 return;
1412 }
1413
1414 switch (part)
1415 {
1416 case BUILD_PUBLIC_KEY:
1417 {
1418 public_key_t *key = va_arg(args, public_key_t*);
1419 this->cert->public_key = key->get_ref(key);
1420 break;
1421 }
1422 case BUILD_SUBJECT:
1423 {
1424 identification_t *id = va_arg(args, identification_t*);
1425 this->cert->subject = id->clone(id);
1426 break;
1427 }
1428 case BUILD_SUBJECT_ALTNAME:
1429 {
1430 identification_t *id = va_arg(args, identification_t*);
1431 this->cert->subjectAltNames->insert_last(
1432 this->cert->subjectAltNames, id->clone(id));
1433 break;
1434 }
1435 case BUILD_NOT_BEFORE_TIME:
1436 this->cert->notBefore = va_arg(args, time_t);
1437 break;
1438 case BUILD_NOT_AFTER_TIME:
1439 this->cert->notAfter = va_arg(args, time_t);
1440 break;
1441 case BUILD_SERIAL:
1442 {
1443 chunk_t serial = va_arg(args, chunk_t);
1444 this->cert->serialNumber = chunk_clone(serial);
1445 break;
1446 }
1447 default:
1448 /* abort if unsupported option */
1449 if (this->cert)
1450 {
1451 destroy(this->cert);
1452 }
1453 builder_cancel(&this->public);
1454 break;
1455 }
1456 va_end(args);
1457 }
1458
1459 /**
1460 * Builder construction function
1461 */
1462 builder_t *x509_cert_builder(certificate_type_t type)
1463 {
1464 private_builder_t *this;
1465
1466 if (type != CERT_X509)
1467 {
1468 return NULL;
1469 }
1470
1471 this = malloc_thing(private_builder_t);
1472
1473 this->cert = NULL;
1474 this->flags = 0;
1475 this->sign_cert = NULL;
1476 this->sign_key = NULL;
1477 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
1478 this->public.build = (void*(*)(builder_t *this))build;
1479
1480 return &this->public;
1481 }
1482