160e7b5c41d665e1cde72b2d318cdbaee09c3227
[strongswan.git] / src / libstrongswan / plugins / x509 / x509_cert.c
1 /*
2 * Copyright (C) 2000 Andreas Hess, Patric Lichtsteiner, Roger Wegmann
3 * Copyright (C) 2001 Marco Bertossa, Andreas Schleiss
4 * Copyright (C) 2002 Mario Strasser
5 * Copyright (C) 2000-2006 Andreas Steffen
6 * Copyright (C) 2006-2009 Martin Willi
7 * Copyright (C) 2008 Tobias Brunner
8 * Hochschule fuer Technik Rapperswil
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 */
20
21 #define _GNU_SOURCE
22
23 #include "x509_cert.h"
24
25 #include <sys/stat.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include <library.h>
32 #include <debug.h>
33 #include <asn1/oid.h>
34 #include <asn1/asn1.h>
35 #include <asn1/asn1_parser.h>
36 #include <crypto/hashers/hasher.h>
37 #include <credentials/keys/private_key.h>
38 #include <utils/linked_list.h>
39 #include <utils/identification.h>
40 #include <selectors/traffic_selector.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 crl_uri_t
121 */
122 linked_list_t *crl_uris;
123
124 /**
125 * List of ocspAccessLocations as allocated char*
126 */
127 linked_list_t *ocsp_uris;
128
129 /**
130 * List of ipAddrBlocks as traffic_selector_t
131 */
132 linked_list_t *ipAddrBlocks;
133
134 /**
135 * List of permitted name constraints
136 */
137 linked_list_t *permitted_names;
138
139 /**
140 * List of exluced name constraints
141 */
142 linked_list_t *excluded_names;
143
144 /**
145 * List of certificatePolicies, as x509_cert_policy_t
146 */
147 linked_list_t *cert_policies;
148
149 /**
150 * certificate's embedded public key
151 */
152 public_key_t *public_key;
153
154 /**
155 * Subject Key Identifier
156 */
157 chunk_t subjectKeyIdentifier;
158
159 /**
160 * Authority Key Identifier
161 */
162 chunk_t authKeyIdentifier;
163
164 /**
165 * Authority Key Serial Number
166 */
167 chunk_t authKeySerialNumber;
168
169 /**
170 * Path Length Constraint
171 */
172 int pathLenConstraint;
173
174 /**
175 * x509 constraints and other flags
176 */
177 x509_flag_t flags;
178
179 /**
180 * Signature algorithm
181 */
182 int algorithm;
183
184 /**
185 * Signature
186 */
187 chunk_t signature;
188
189 /**
190 * Certificate parsed from blob/file?
191 */
192 bool parsed;
193
194 /**
195 * reference count
196 */
197 refcount_t ref;
198 };
199
200 static const chunk_t ASN1_subjectAltName_oid = chunk_from_chars(
201 0x06, 0x03, 0x55, 0x1D, 0x11
202 );
203
204 /**
205 * CRL URIs with associated issuer
206 */
207 typedef struct {
208 identification_t *issuer;
209 linked_list_t *uris;
210 } crl_uri_t;
211
212 /**
213 * Create a new issuer entry
214 */
215 static crl_uri_t *crl_uri_create(identification_t *issuer)
216 {
217 crl_uri_t *this;
218
219 INIT(this,
220 .issuer = issuer ? issuer->clone(issuer) : NULL,
221 .uris = linked_list_create(),
222 );
223 return this;
224 }
225
226 /**
227 * Destroy a CRL URI struct
228 */
229 static void crl_uri_destroy(crl_uri_t *this)
230 {
231 this->uris->destroy_function(this->uris, free);
232 DESTROY_IF(this->issuer);
233 free(this);
234 }
235
236 /**
237 * Destroy a CertificatePolicy
238 */
239 static void cert_policy_destroy(x509_cert_policy_t *this)
240 {
241 free(this->oid.ptr);
242 free(this->cps_uri);
243 free(this->unotice_text);
244 free(this);
245 }
246
247 /**
248 * ASN.1 definition of a basicConstraints extension
249 */
250 static const asn1Object_t basicConstraintsObjects[] = {
251 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
252 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
253 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
254 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
255 { 0, "exit", ASN1_EOC, ASN1_EXIT }
256 };
257 #define BASIC_CONSTRAINTS_CA 1
258 #define BASIC_CONSTRAINTS_PATH_LEN 2
259
260 /**
261 * Extracts the basicConstraints extension
262 */
263 static void parse_basicConstraints(chunk_t blob, int level0,
264 private_x509_cert_t *this)
265 {
266 asn1_parser_t *parser;
267 chunk_t object;
268 int objectID;
269 bool isCA = FALSE;
270
271 parser = asn1_parser_create(basicConstraintsObjects, blob);
272 parser->set_top_level(parser, level0);
273
274 while (parser->iterate(parser, &objectID, &object))
275 {
276 switch (objectID)
277 {
278 case BASIC_CONSTRAINTS_CA:
279 isCA = object.len && *object.ptr;
280 DBG2(DBG_LIB, " %s", isCA ? "TRUE" : "FALSE");
281 if (isCA)
282 {
283 this->flags |= X509_CA;
284 }
285 break;
286 case BASIC_CONSTRAINTS_PATH_LEN:
287 if (isCA)
288 {
289 if (object.len == 0)
290 {
291 this->pathLenConstraint = 0;
292 }
293 else if (object.len == 1)
294 {
295 this->pathLenConstraint = *object.ptr;
296 }
297 /* we ignore path length constraints > 127 */
298 }
299 break;
300 default:
301 break;
302 }
303 }
304 parser->destroy(parser);
305 }
306
307 /**
308 * ASN.1 definition of otherName
309 */
310 static const asn1Object_t otherNameObjects[] = {
311 {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */
312 {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY }, /* 1 */
313 {0, "exit", ASN1_EOC, ASN1_EXIT }
314 };
315 #define ON_OBJ_ID_TYPE 0
316 #define ON_OBJ_VALUE 1
317
318 /**
319 * Extracts an otherName
320 */
321 static bool parse_otherName(chunk_t *blob, int level0, id_type_t *type)
322 {
323 asn1_parser_t *parser;
324 chunk_t object;
325 int objectID;
326 int oid = OID_UNKNOWN;
327 bool success = FALSE;
328
329 parser = asn1_parser_create(otherNameObjects, *blob);
330 parser->set_top_level(parser, level0);
331
332 while (parser->iterate(parser, &objectID, &object))
333 {
334 switch (objectID)
335 {
336 case ON_OBJ_ID_TYPE:
337 oid = asn1_known_oid(object);
338 break;
339 case ON_OBJ_VALUE:
340 switch (oid)
341 {
342 case OID_XMPP_ADDR:
343 if (!asn1_parse_simple_object(&object, ASN1_UTF8STRING,
344 parser->get_level(parser)+1, "xmppAddr"))
345 {
346 goto end;
347 }
348 break;
349 case OID_USER_PRINCIPAL_NAME:
350 if (asn1_parse_simple_object(&object, ASN1_UTF8STRING,
351 parser->get_level(parser)+1, "msUPN"))
352 { /* we handle UPNs as RFC822 addr */
353 *blob = object;
354 *type = ID_RFC822_ADDR;
355 }
356 else
357 {
358 goto end;
359 }
360 break;
361 }
362 break;
363 default:
364 break;
365 }
366 }
367 success = parser->success(parser);
368
369 end:
370 parser->destroy(parser);
371 return success;
372 }
373
374 /**
375 * ASN.1 definition of generalName
376 */
377 static const asn1Object_t generalNameObjects[] = {
378 { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */
379 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */
380 { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */
381 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */
382 { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */
383 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
384 { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */
385 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
386 { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */
387 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
388 { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */
389 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
390 { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */
391 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */
392 { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */
393 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */
394 { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */
395 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 17 */
396 { 0, "exit", ASN1_EOC, ASN1_EXIT }
397 };
398 #define GN_OBJ_OTHER_NAME 0
399 #define GN_OBJ_RFC822_NAME 2
400 #define GN_OBJ_DNS_NAME 4
401 #define GN_OBJ_X400_ADDRESS 6
402 #define GN_OBJ_DIRECTORY_NAME 8
403 #define GN_OBJ_EDI_PARTY_NAME 10
404 #define GN_OBJ_URI 12
405 #define GN_OBJ_IP_ADDRESS 14
406 #define GN_OBJ_REGISTERED_ID 16
407
408 /**
409 * Extracts a generalName
410 */
411 static identification_t *parse_generalName(chunk_t blob, int level0)
412 {
413 asn1_parser_t *parser;
414 chunk_t object;
415 int objectID ;
416
417 identification_t *gn = NULL;
418
419 parser = asn1_parser_create(generalNameObjects, blob);
420 parser->set_top_level(parser, level0);
421
422 while (parser->iterate(parser, &objectID, &object))
423 {
424 id_type_t id_type = ID_ANY;
425
426 switch (objectID)
427 {
428 case GN_OBJ_RFC822_NAME:
429 id_type = ID_RFC822_ADDR;
430 break;
431 case GN_OBJ_DNS_NAME:
432 id_type = ID_FQDN;
433 break;
434 case GN_OBJ_URI:
435 id_type = ID_DER_ASN1_GN_URI;
436 break;
437 case GN_OBJ_DIRECTORY_NAME:
438 id_type = ID_DER_ASN1_DN;
439 break;
440 case GN_OBJ_IP_ADDRESS:
441 switch (object.len)
442 {
443 case 4:
444 id_type = ID_IPV4_ADDR;
445 break;
446 case 16:
447 id_type = ID_IPV6_ADDR;
448 break;
449 default:
450 break;
451 }
452 break;
453 case GN_OBJ_OTHER_NAME:
454 if (!parse_otherName(&object, parser->get_level(parser)+1,
455 &id_type))
456 {
457 goto end;
458 }
459 break;
460 case GN_OBJ_X400_ADDRESS:
461 case GN_OBJ_EDI_PARTY_NAME:
462 case GN_OBJ_REGISTERED_ID:
463 default:
464 break;
465 }
466 if (id_type != ID_ANY)
467 {
468 gn = identification_create_from_encoding(id_type, object);
469 DBG2(DBG_LIB, " '%Y'", gn);
470 goto end;
471 }
472 }
473
474 end:
475 parser->destroy(parser);
476 return gn;
477 }
478
479 /**
480 * ASN.1 definition of generalNames
481 */
482 static const asn1Object_t generalNamesObjects[] = {
483 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
484 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
485 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
486 { 0, "exit", ASN1_EOC, ASN1_EXIT }
487 };
488 #define GENERAL_NAMES_GN 1
489
490 /**
491 * Extracts one or several GNs and puts them into a chained list
492 */
493 void x509_parse_generalNames(chunk_t blob, int level0, bool implicit, linked_list_t *list)
494 {
495 asn1_parser_t *parser;
496 chunk_t object;
497 int objectID;
498
499 parser = asn1_parser_create(generalNamesObjects, blob);
500 parser->set_top_level(parser, level0);
501 parser->set_flags(parser, implicit, FALSE);
502
503 while (parser->iterate(parser, &objectID, &object))
504 {
505 if (objectID == GENERAL_NAMES_GN)
506 {
507 identification_t *gn = parse_generalName(object,
508 parser->get_level(parser)+1);
509
510 if (gn)
511 {
512 list->insert_last(list, (void *)gn);
513 }
514 }
515 }
516 parser->destroy(parser);
517 }
518
519 /**
520 * ASN.1 definition of a authorityKeyIdentifier extension
521 */
522 static const asn1Object_t authKeyIdentifierObjects[] = {
523 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
524 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_BODY }, /* 1 */
525 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
526 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
527 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
528 { 1, "authorityCertSerialNumber", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
529 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 6 */
530 { 0, "exit", ASN1_EOC, ASN1_EXIT }
531 };
532 #define AUTH_KEY_ID_KEY_ID 1
533 #define AUTH_KEY_ID_CERT_ISSUER 3
534 #define AUTH_KEY_ID_CERT_SERIAL 5
535
536 /**
537 * Extracts an authoritykeyIdentifier
538 */
539 chunk_t x509_parse_authorityKeyIdentifier(chunk_t blob, int level0,
540 chunk_t *authKeySerialNumber)
541 {
542 asn1_parser_t *parser;
543 chunk_t object;
544 int objectID;
545 chunk_t authKeyIdentifier = chunk_empty;
546
547 *authKeySerialNumber = chunk_empty;
548
549 parser = asn1_parser_create(authKeyIdentifierObjects, blob);
550 parser->set_top_level(parser, level0);
551
552 while (parser->iterate(parser, &objectID, &object))
553 {
554 switch (objectID)
555 {
556 case AUTH_KEY_ID_KEY_ID:
557 authKeyIdentifier = chunk_clone(object);
558 break;
559 case AUTH_KEY_ID_CERT_ISSUER:
560 /* TODO: x509_parse_generalNames(object, level+1, TRUE); */
561 break;
562 case AUTH_KEY_ID_CERT_SERIAL:
563 *authKeySerialNumber = object;
564 break;
565 default:
566 break;
567 }
568 }
569 parser->destroy(parser);
570 return authKeyIdentifier;
571 }
572
573 /**
574 * ASN.1 definition of a authorityInfoAccess extension
575 */
576 static const asn1Object_t authInfoAccessObjects[] = {
577 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
578 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
579 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
580 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
581 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
582 { 0, "exit", ASN1_EOC, ASN1_EXIT }
583 };
584 #define AUTH_INFO_ACCESS_METHOD 2
585 #define AUTH_INFO_ACCESS_LOCATION 3
586
587 /**
588 * Extracts an authorityInfoAcess location
589 */
590 static void parse_authorityInfoAccess(chunk_t blob, int level0,
591 private_x509_cert_t *this)
592 {
593 asn1_parser_t *parser;
594 chunk_t object;
595 int objectID;
596 int accessMethod = OID_UNKNOWN;
597
598 parser = asn1_parser_create(authInfoAccessObjects, blob);
599 parser->set_top_level(parser, level0);
600
601 while (parser->iterate(parser, &objectID, &object))
602 {
603 switch (objectID)
604 {
605 case AUTH_INFO_ACCESS_METHOD:
606 accessMethod = asn1_known_oid(object);
607 break;
608 case AUTH_INFO_ACCESS_LOCATION:
609 {
610 switch (accessMethod)
611 {
612 case OID_OCSP:
613 case OID_CA_ISSUERS:
614 {
615 identification_t *id;
616 char *uri;
617
618 id = parse_generalName(object,
619 parser->get_level(parser)+1);
620 if (id == NULL)
621 {
622 /* parsing went wrong - abort */
623 goto end;
624 }
625 DBG2(DBG_LIB, " '%Y'", id);
626 if (accessMethod == OID_OCSP &&
627 asprintf(&uri, "%Y", id) > 0)
628 {
629 this->ocsp_uris->insert_last(this->ocsp_uris, uri);
630 }
631 id->destroy(id);
632 }
633 break;
634 default:
635 /* unkown accessMethod, ignoring */
636 break;
637 }
638 break;
639 }
640 default:
641 break;
642 }
643 }
644
645 end:
646 parser->destroy(parser);
647 }
648
649 /**
650 * Extract KeyUsage flags
651 */
652 static void parse_keyUsage(chunk_t blob, private_x509_cert_t *this)
653 {
654 enum {
655 KU_DIGITAL_SIGNATURE = 0,
656 KU_NON_REPUDIATION = 1,
657 KU_KEY_ENCIPHERMENT = 2,
658 KU_DATA_ENCIPHERMENT = 3,
659 KU_KEY_AGREEMENT = 4,
660 KU_KEY_CERT_SIGN = 5,
661 KU_CRL_SIGN = 6,
662 KU_ENCIPHER_ONLY = 7,
663 KU_DECIPHER_ONLY = 8,
664 };
665
666 if (asn1_unwrap(&blob, &blob) == ASN1_BIT_STRING && blob.len)
667 {
668 int bit, byte, unused = blob.ptr[0];
669
670 blob = chunk_skip(blob, 1);
671 for (byte = 0; byte < blob.len; byte++)
672 {
673 for (bit = 0; bit < 8; bit++)
674 {
675 if (byte == blob.len - 1 && bit > (7 - unused))
676 {
677 break;
678 }
679 if (blob.ptr[byte] & 1 << (7 - bit))
680 {
681 switch (byte * 8 + bit)
682 {
683 case KU_CRL_SIGN:
684 this->flags |= X509_CRL_SIGN;
685 break;
686 case KU_KEY_CERT_SIGN:
687 /* we use the caBasicConstraint, MUST be set */
688 case KU_DIGITAL_SIGNATURE:
689 case KU_NON_REPUDIATION:
690 case KU_KEY_ENCIPHERMENT:
691 case KU_DATA_ENCIPHERMENT:
692 case KU_KEY_AGREEMENT:
693 case KU_ENCIPHER_ONLY:
694 case KU_DECIPHER_ONLY:
695 break;
696 }
697 }
698 }
699 }
700 }
701 }
702
703 /**
704 * ASN.1 definition of a extendedKeyUsage extension
705 */
706 static const asn1Object_t extendedKeyUsageObjects[] = {
707 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
708 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
709 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
710 { 0, "exit", ASN1_EOC, ASN1_EXIT }
711 };
712 #define EXT_KEY_USAGE_PURPOSE_ID 1
713
714 /**
715 * Extracts extendedKeyUsage OIDs
716 */
717 static void parse_extendedKeyUsage(chunk_t blob, int level0,
718 private_x509_cert_t *this)
719 {
720 asn1_parser_t *parser;
721 chunk_t object;
722 int objectID;
723
724 parser = asn1_parser_create(extendedKeyUsageObjects, blob);
725 parser->set_top_level(parser, level0);
726
727 while (parser->iterate(parser, &objectID, &object))
728 {
729 if (objectID == EXT_KEY_USAGE_PURPOSE_ID)
730 {
731 switch (asn1_known_oid(object))
732 {
733 case OID_SERVER_AUTH:
734 this->flags |= X509_SERVER_AUTH;
735 break;
736 case OID_CLIENT_AUTH:
737 this->flags |= X509_CLIENT_AUTH;
738 break;
739 case OID_OCSP_SIGNING:
740 this->flags |= X509_OCSP_SIGNER;
741 break;
742 default:
743 break;
744 }
745 }
746 }
747 parser->destroy(parser);
748 }
749
750 /**
751 * ASN.1 definition of crlDistributionPoints
752 */
753 static const asn1Object_t crlDistributionPointsObjects[] = {
754 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
755 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
756 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
757 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
758 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
759 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
760 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
761 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
762 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
763 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
764 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_OBJ }, /* 10 */
765 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
766 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
767 { 0, "exit", ASN1_EOC, ASN1_EXIT }
768 };
769 #define CRL_DIST_POINTS 1
770 #define CRL_DIST_POINTS_FULLNAME 3
771 #define CRL_DIST_POINTS_ISSUER 10
772
773 /**
774 * Extracts one or several crlDistributionPoints into a list
775 */
776 static void parse_crlDistributionPoints(chunk_t blob, int level0,
777 private_x509_cert_t *this)
778 {
779 asn1_parser_t *parser;
780 chunk_t object;
781 int objectID;
782 crl_uri_t *entry = NULL;
783 identification_t *id;
784 char *uri;
785 linked_list_t *list = linked_list_create();
786
787 parser = asn1_parser_create(crlDistributionPointsObjects, blob);
788 parser->set_top_level(parser, level0);
789
790 while (parser->iterate(parser, &objectID, &object))
791 {
792 switch (objectID)
793 {
794 case CRL_DIST_POINTS:
795 entry = crl_uri_create(NULL);
796 this->crl_uris->insert_last(this->crl_uris, entry);
797 break;
798 case CRL_DIST_POINTS_FULLNAME:
799 if (entry)
800 {
801 x509_parse_generalNames(object, parser->get_level(parser)+1,
802 TRUE, list);
803 while (list->remove_last(list, (void**)&id) == SUCCESS)
804 {
805 if (asprintf(&uri, "%Y", id) > 0)
806 {
807 entry->uris->insert_last(entry->uris, uri);
808 }
809 id->destroy(id);
810 }
811 }
812 break;
813 case CRL_DIST_POINTS_ISSUER:
814 if (entry)
815 {
816 x509_parse_generalNames(object, parser->get_level(parser)+1,
817 TRUE, list);
818 while (list->remove_last(list, (void**)&id) == SUCCESS)
819 {
820 if (!entry->issuer)
821 {
822 entry->issuer = id;
823 }
824 else
825 {
826 id->destroy(id);
827 }
828 }
829 }
830 break;
831 }
832 }
833 parser->destroy(parser);
834 list->destroy(list);
835 }
836
837 /**
838 * ASN.1 definition of nameConstraints
839 */
840 static const asn1Object_t nameConstraintsObjects[] = {
841 { 0, "nameConstraints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
842 { 1, "permittedSubtrees", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 1 */
843 { 2, "generalSubtree", ASN1_SEQUENCE, ASN1_BODY }, /* 2 */
844 { 1, "end loop", ASN1_EOC, ASN1_END }, /* 3 */
845 { 1, "excludedSubtrees", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_LOOP }, /* 4 */
846 { 2, "generalSubtree", ASN1_SEQUENCE, ASN1_BODY }, /* 5 */
847 { 1, "end loop", ASN1_EOC, ASN1_END }, /* 6 */
848 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 7 */
849 { 0, "exit", ASN1_EOC, ASN1_EXIT }
850 };
851 #define NAME_CONSTRAINT_PERMITTED 2
852 #define NAME_CONSTRAINT_EXCLUDED 5
853
854 /**
855 * Parse permitted/excluded nameConstraints
856 */
857 static void parse_nameConstraints(chunk_t blob, int level0,
858 private_x509_cert_t *this)
859 {
860 asn1_parser_t *parser;
861 identification_t *id;
862 chunk_t object;
863 int objectID;
864
865 parser = asn1_parser_create(nameConstraintsObjects, blob);
866 parser->set_top_level(parser, level0);
867
868 while (parser->iterate(parser, &objectID, &object))
869 {
870 switch (objectID)
871 {
872 case NAME_CONSTRAINT_PERMITTED:
873 id = parse_generalName(object, parser->get_level(parser) + 1);
874 if (id)
875 {
876 this->permitted_names->insert_last(this->permitted_names, id);
877 }
878 break;
879 case NAME_CONSTRAINT_EXCLUDED:
880 id = parse_generalName(object, parser->get_level(parser) + 1);
881 if (id)
882 {
883 this->excluded_names->insert_last(this->excluded_names, id);
884 }
885 break;
886 default:
887 break;
888 }
889 }
890 parser->destroy(parser);
891 }
892
893 /**
894 * ASN.1 definition of a certificatePolicies extension
895 */
896 static const asn1Object_t certificatePoliciesObject[] = {
897 { 0, "certificatePolicies", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
898 { 1, "policyInformation", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
899 { 2, "policyId", ASN1_OID, ASN1_BODY }, /* 2 */
900 { 2, "qualifier", ASN1_SEQUENCE, ASN1_OPT|ASN1_BODY }, /* 3 */
901 { 3, "qualifierInfo", ASN1_SEQUENCE, ASN1_NONE }, /* 4 */
902 { 4, "qualifierId", ASN1_OID, ASN1_BODY }, /* 5 */
903 { 4, "cPSuri", ASN1_IA5STRING, ASN1_OPT|ASN1_BODY }, /* 6 */
904 { 4, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
905 { 4, "userNotice", ASN1_SEQUENCE, ASN1_OPT|ASN1_NONE }, /* 8 */
906 { 5, "explicitText", ASN1_EOC, ASN1_RAW }, /* 9 */
907 { 4, "end choice", ASN1_EOC, ASN1_END }, /* 10 */
908 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 12 */
909 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 13 */
910 { 0, "exit", ASN1_EOC, ASN1_EXIT }
911 };
912 #define CERT_POLICY_ID 2
913 #define CERT_POLICY_QUALIFIER_ID 5
914 #define CERT_POLICY_CPS_URI 6
915 #define CERT_POLICY_EXPLICIT_TEXT 9
916
917 /**
918 * Parse certificatePolicies
919 */
920 static void parse_certificatePolicies(chunk_t blob, int level0,
921 private_x509_cert_t *this)
922 {
923 x509_cert_policy_t *policy = NULL;
924 asn1_parser_t *parser;
925 chunk_t object;
926 int objectID, qualifier = OID_UNKNOWN;
927
928 parser = asn1_parser_create(certificatePoliciesObject, blob);
929 parser->set_top_level(parser, level0);
930
931 while (parser->iterate(parser, &objectID, &object))
932 {
933 switch (objectID)
934 {
935 case CERT_POLICY_ID:
936 INIT(policy,
937 .oid = chunk_clone(object),
938 );
939 this->cert_policies->insert_last(this->cert_policies, policy);
940 break;
941 case CERT_POLICY_QUALIFIER_ID:
942 qualifier = asn1_known_oid(object);
943 break;
944 case CERT_POLICY_CPS_URI:
945 if (policy && !policy->cps_uri && object.len &&
946 qualifier == OID_POLICY_QUALIFIER_CPS &&
947 chunk_printable(object, NULL, 0))
948 {
949 policy->cps_uri = strndup(object.ptr, object.len);
950 }
951 break;
952 case CERT_POLICY_EXPLICIT_TEXT:
953 /* TODO */
954 break;
955 default:
956 break;
957 }
958 }
959 parser->destroy(parser);
960 }
961
962 /**
963 * ASN.1 definition of ipAddrBlocks according to RFC 3779
964 */
965 static const asn1Object_t ipAddrBlocksObjects[] = {
966 { 0, "ipAddrBlocks", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
967 { 1, "ipAddressFamily", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
968 { 2, "addressFamily", ASN1_OCTET_STRING, ASN1_BODY }, /* 2 */
969 { 2, "inherit", ASN1_NULL, ASN1_OPT|ASN1_NONE }, /* 3 */
970 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
971 { 2, "addressesOrRanges", ASN1_SEQUENCE, ASN1_OPT|ASN1_LOOP }, /* 5 */
972 { 3, "addressPrefix", ASN1_BIT_STRING, ASN1_OPT|ASN1_BODY }, /* 6 */
973 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
974 { 3, "addressRange", ASN1_SEQUENCE, ASN1_OPT|ASN1_NONE }, /* 8 */
975 { 4, "min", ASN1_BIT_STRING, ASN1_BODY }, /* 9 */
976 { 4, "max", ASN1_BIT_STRING, ASN1_BODY }, /* 10 */
977 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
978 { 2, "end choice/loop", ASN1_EOC, ASN1_END }, /* 12 */
979 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 13 */
980 { 0, "exit", ASN1_EOC, ASN1_EXIT }
981 };
982 #define IP_ADDR_BLOCKS_FAMILY 2
983 #define IP_ADDR_BLOCKS_INHERIT 3
984 #define IP_ADDR_BLOCKS_PREFIX 6
985 #define IP_ADDR_BLOCKS_MIN 9
986 #define IP_ADDR_BLOCKS_MAX 10
987
988 static bool check_address_object(ts_type_t ts_type, chunk_t object)
989 {
990 switch (ts_type)
991 {
992 case TS_IPV4_ADDR_RANGE:
993 if (object.len > 5)
994 {
995 DBG1(DBG_LIB, "IPv4 address object is larger than 5 octets");
996 return FALSE;
997 }
998 break;
999 case TS_IPV6_ADDR_RANGE:
1000 if (object.len > 17)
1001 {
1002 DBG1(DBG_LIB, "IPv6 address object is larger than 17 octets");
1003 return FALSE;
1004 }
1005 break;
1006 default:
1007 DBG1(DBG_LIB, "unknown address family");
1008 return FALSE;
1009 }
1010 if (object.len == 0)
1011 {
1012 DBG1(DBG_LIB, "An ASN.1 bit string must contain at least the "
1013 "initial octet");
1014 return FALSE;
1015 }
1016 if (object.len == 1 && object.ptr[0] != 0)
1017 {
1018 DBG1(DBG_LIB, "An empty ASN.1 bit string must contain a zero "
1019 "initial octet");
1020 return FALSE;
1021 }
1022 if (object.ptr[0] > 7)
1023 {
1024 DBG1(DBG_LIB, "number of unused bits is too large");
1025 return FALSE;
1026 }
1027 return TRUE;
1028 }
1029
1030 static void parse_ipAddrBlocks(chunk_t blob, int level0,
1031 private_x509_cert_t *this)
1032 {
1033 asn1_parser_t *parser;
1034 chunk_t object, min_object;
1035 ts_type_t ts_type = 0;
1036 traffic_selector_t *ts;
1037 int objectID;
1038
1039 parser = asn1_parser_create(ipAddrBlocksObjects, blob);
1040 parser->set_top_level(parser, level0);
1041
1042 while (parser->iterate(parser, &objectID, &object))
1043 {
1044 switch (objectID)
1045 {
1046 case IP_ADDR_BLOCKS_FAMILY:
1047 ts_type = 0;
1048 if (object.len == 2 && object.ptr[0] == 0)
1049 {
1050 if (object.ptr[1] == 1)
1051 {
1052 ts_type = TS_IPV4_ADDR_RANGE;
1053 }
1054 else if (object.ptr[1] == 2)
1055 {
1056 ts_type = TS_IPV6_ADDR_RANGE;
1057 }
1058 else
1059 {
1060 break;
1061 }
1062 DBG2(DBG_LIB, " %N", ts_type_name, ts_type);
1063 }
1064 break;
1065 case IP_ADDR_BLOCKS_INHERIT:
1066 DBG1(DBG_LIB, "inherit choice is not supported");
1067 break;
1068 case IP_ADDR_BLOCKS_PREFIX:
1069 if (!check_address_object(ts_type, object))
1070 {
1071 goto end;
1072 }
1073 ts = traffic_selector_create_from_rfc3779_format(ts_type,
1074 object, object);
1075 DBG2(DBG_LIB, " %R", ts);
1076 this->ipAddrBlocks->insert_last(this->ipAddrBlocks, ts);
1077 break;
1078 case IP_ADDR_BLOCKS_MIN:
1079 if (!check_address_object(ts_type, object))
1080 {
1081 goto end;
1082 }
1083 min_object = object;
1084 break;
1085 case IP_ADDR_BLOCKS_MAX:
1086 if (!check_address_object(ts_type, object))
1087 {
1088 goto end;
1089 }
1090 ts = traffic_selector_create_from_rfc3779_format(ts_type,
1091 min_object, object);
1092 DBG2(DBG_LIB, " %R", ts);
1093 this->ipAddrBlocks->insert_last(this->ipAddrBlocks, ts);
1094 break;
1095 default:
1096 break;
1097 }
1098 }
1099 this->flags |= X509_IP_ADDR_BLOCKS;
1100
1101 end:
1102 parser->destroy(parser);
1103 }
1104
1105 /**
1106 * ASN.1 definition of an X.509v3 x509_cert
1107 */
1108 static const asn1Object_t certObjects[] = {
1109 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
1110 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
1111 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
1112 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
1113 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
1114 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
1115 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
1116 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
1117 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
1118 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
1119 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
1120 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
1121 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
1122 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
1123 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
1124 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
1125 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
1126 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
1127 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
1128 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
1129 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
1130 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
1131 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
1132 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
1133 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
1134 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
1135 { 0, "exit", ASN1_EOC, ASN1_EXIT }
1136 };
1137 #define X509_OBJ_TBS_CERTIFICATE 1
1138 #define X509_OBJ_VERSION 3
1139 #define X509_OBJ_SERIAL_NUMBER 4
1140 #define X509_OBJ_SIG_ALG 5
1141 #define X509_OBJ_ISSUER 6
1142 #define X509_OBJ_NOT_BEFORE 8
1143 #define X509_OBJ_NOT_AFTER 9
1144 #define X509_OBJ_SUBJECT 10
1145 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
1146 #define X509_OBJ_OPTIONAL_EXTENSIONS 16
1147 #define X509_OBJ_EXTN_ID 19
1148 #define X509_OBJ_CRITICAL 20
1149 #define X509_OBJ_EXTN_VALUE 21
1150 #define X509_OBJ_ALGORITHM 24
1151 #define X509_OBJ_SIGNATURE 25
1152
1153 /**
1154 * Parses an X.509v3 certificate
1155 */
1156 static bool parse_certificate(private_x509_cert_t *this)
1157 {
1158 asn1_parser_t *parser;
1159 chunk_t object;
1160 int objectID;
1161 int extn_oid = OID_UNKNOWN;
1162 int sig_alg = OID_UNKNOWN;
1163 bool success = FALSE;
1164 bool critical = FALSE;
1165
1166 parser = asn1_parser_create(certObjects, this->encoding);
1167
1168 while (parser->iterate(parser, &objectID, &object))
1169 {
1170 u_int level = parser->get_level(parser)+1;
1171
1172 switch (objectID)
1173 {
1174 case X509_OBJ_TBS_CERTIFICATE:
1175 this->tbsCertificate = object;
1176 break;
1177 case X509_OBJ_VERSION:
1178 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
1179 if (this->version < 1 || this->version > 3)
1180 {
1181 DBG1(DBG_LIB, "X.509v%d not supported", this->version);
1182 goto end;
1183 }
1184 else
1185 {
1186 DBG2(DBG_LIB, " X.509v%d", this->version);
1187 }
1188 break;
1189 case X509_OBJ_SERIAL_NUMBER:
1190 this->serialNumber = object;
1191 break;
1192 case X509_OBJ_SIG_ALG:
1193 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
1194 break;
1195 case X509_OBJ_ISSUER:
1196 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
1197 DBG2(DBG_LIB, " '%Y'", this->issuer);
1198 break;
1199 case X509_OBJ_NOT_BEFORE:
1200 this->notBefore = asn1_parse_time(object, level);
1201 break;
1202 case X509_OBJ_NOT_AFTER:
1203 this->notAfter = asn1_parse_time(object, level);
1204 break;
1205 case X509_OBJ_SUBJECT:
1206 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
1207 DBG2(DBG_LIB, " '%Y'", this->subject);
1208 break;
1209 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
1210 DBG2(DBG_LIB, "-- > --");
1211 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
1212 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
1213 DBG2(DBG_LIB, "-- < --");
1214 if (this->public_key == NULL)
1215 {
1216 goto end;
1217 }
1218 break;
1219 case X509_OBJ_OPTIONAL_EXTENSIONS:
1220 if (this->version != 3)
1221 {
1222 DBG1(DBG_LIB, "Only X.509v3 certificates have extensions");
1223 goto end;
1224 }
1225 break;
1226 case X509_OBJ_EXTN_ID:
1227 extn_oid = asn1_known_oid(object);
1228 break;
1229 case X509_OBJ_CRITICAL:
1230 critical = object.len && *object.ptr;
1231 DBG2(DBG_LIB, " %s", critical ? "TRUE" : "FALSE");
1232 break;
1233 case X509_OBJ_EXTN_VALUE:
1234 {
1235 switch (extn_oid)
1236 {
1237 case OID_SUBJECT_KEY_ID:
1238 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
1239 level, "keyIdentifier"))
1240 {
1241 goto end;
1242 }
1243 this->subjectKeyIdentifier = object;
1244 break;
1245 case OID_SUBJECT_ALT_NAME:
1246 x509_parse_generalNames(object, level, FALSE,
1247 this->subjectAltNames);
1248 break;
1249 case OID_BASIC_CONSTRAINTS:
1250 parse_basicConstraints(object, level, this);
1251 break;
1252 case OID_CRL_DISTRIBUTION_POINTS:
1253 parse_crlDistributionPoints(object, level, this);
1254 break;
1255 case OID_AUTHORITY_KEY_ID:
1256 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
1257 level, &this->authKeySerialNumber);
1258 break;
1259 case OID_AUTHORITY_INFO_ACCESS:
1260 parse_authorityInfoAccess(object, level, this);
1261 break;
1262 case OID_KEY_USAGE:
1263 parse_keyUsage(object, this);
1264 break;
1265 case OID_EXTENDED_KEY_USAGE:
1266 parse_extendedKeyUsage(object, level, this);
1267 break;
1268 case OID_IP_ADDR_BLOCKS:
1269 parse_ipAddrBlocks(object, level, this);
1270 break;
1271 case OID_NAME_CONSTRAINTS:
1272 parse_nameConstraints(object, level, this);
1273 break;
1274 case OID_CERTIFICATE_POLICIES:
1275 parse_certificatePolicies(object, level, this);
1276 break;
1277 case OID_NS_REVOCATION_URL:
1278 case OID_NS_CA_REVOCATION_URL:
1279 case OID_NS_CA_POLICY_URL:
1280 case OID_NS_COMMENT:
1281 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING,
1282 level, oid_names[extn_oid].name))
1283 {
1284 goto end;
1285 }
1286 break;
1287 default:
1288 if (critical && lib->settings->get_bool(lib->settings,
1289 "libstrongswan.plugins.x509.enforce_critical", FALSE))
1290 {
1291 DBG1(DBG_LIB, "critical %s extension not supported",
1292 (extn_oid == OID_UNKNOWN) ? "unknown" :
1293 (char*)oid_names[extn_oid].name);
1294 goto end;
1295 }
1296 break;
1297 }
1298 break;
1299 }
1300 case X509_OBJ_ALGORITHM:
1301 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
1302 if (this->algorithm != sig_alg)
1303 {
1304 DBG1(DBG_LIB, " signature algorithms do not agree");
1305 goto end;
1306 }
1307 break;
1308 case X509_OBJ_SIGNATURE:
1309 this->signature = object;
1310 break;
1311 default:
1312 break;
1313 }
1314 }
1315 success = parser->success(parser);
1316
1317 end:
1318 parser->destroy(parser);
1319 if (success)
1320 {
1321 hasher_t *hasher;
1322
1323 /* check if the certificate is self-signed */
1324 if (this->public.interface.interface.issued_by(
1325 &this->public.interface.interface,
1326 &this->public.interface.interface))
1327 {
1328 this->flags |= X509_SELF_SIGNED;
1329 }
1330 /* create certificate hash */
1331 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1332 if (hasher == NULL)
1333 {
1334 DBG1(DBG_LIB, " unable to create hash of certificate, SHA1 not supported");
1335 return NULL;
1336 }
1337 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
1338 hasher->destroy(hasher);
1339 }
1340 return success;
1341 }
1342
1343 METHOD(certificate_t, get_type, certificate_type_t,
1344 private_x509_cert_t *this)
1345 {
1346 return CERT_X509;
1347 }
1348
1349 METHOD(certificate_t, get_subject, identification_t*,
1350 private_x509_cert_t *this)
1351 {
1352 return this->subject;
1353 }
1354
1355 METHOD(certificate_t, get_issuer, identification_t*,
1356 private_x509_cert_t *this)
1357 {
1358 return this->issuer;
1359 }
1360
1361 METHOD(certificate_t, has_subject, id_match_t,
1362 private_x509_cert_t *this, identification_t *subject)
1363 {
1364 identification_t *current;
1365 enumerator_t *enumerator;
1366 id_match_t match, best;
1367 chunk_t encoding;
1368
1369 if (subject->get_type(subject) == ID_KEY_ID)
1370 {
1371 encoding = subject->get_encoding(subject);
1372
1373 if (this->encoding_hash.len &&
1374 chunk_equals(this->encoding_hash, encoding))
1375 {
1376 return ID_MATCH_PERFECT;
1377 }
1378 if (this->subjectKeyIdentifier.len &&
1379 chunk_equals(this->subjectKeyIdentifier, encoding))
1380 {
1381 return ID_MATCH_PERFECT;
1382 }
1383 if (this->public_key &&
1384 this->public_key->has_fingerprint(this->public_key, encoding))
1385 {
1386 return ID_MATCH_PERFECT;
1387 }
1388 }
1389 best = this->subject->matches(this->subject, subject);
1390 enumerator = this->subjectAltNames->create_enumerator(this->subjectAltNames);
1391 while (enumerator->enumerate(enumerator, &current))
1392 {
1393 match = current->matches(current, subject);
1394 if (match > best)
1395 {
1396 best = match;
1397 }
1398 }
1399 enumerator->destroy(enumerator);
1400 return best;
1401 }
1402
1403 METHOD(certificate_t, has_issuer, id_match_t,
1404 private_x509_cert_t *this, identification_t *issuer)
1405 {
1406 /* issuerAltNames currently not supported */
1407 return this->issuer->matches(this->issuer, issuer);
1408 }
1409
1410 METHOD(certificate_t, issued_by, bool,
1411 private_x509_cert_t *this, certificate_t *issuer)
1412 {
1413 public_key_t *key;
1414 signature_scheme_t scheme;
1415 bool valid;
1416 x509_t *x509 = (x509_t*)issuer;
1417
1418 if (&this->public.interface.interface == issuer)
1419 {
1420 if (this->flags & X509_SELF_SIGNED)
1421 {
1422 return TRUE;
1423 }
1424 }
1425 else
1426 {
1427 if (issuer->get_type(issuer) != CERT_X509)
1428 {
1429 return FALSE;
1430 }
1431 if (!(x509->get_flags(x509) & X509_CA))
1432 {
1433 return FALSE;
1434 }
1435 }
1436 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
1437 {
1438 return FALSE;
1439 }
1440
1441 /* determine signature scheme */
1442 scheme = signature_scheme_from_oid(this->algorithm);
1443 if (scheme == SIGN_UNKNOWN)
1444 {
1445 return FALSE;
1446 }
1447 /* get the public key of the issuer */
1448 key = issuer->get_public_key(issuer);
1449 if (!key)
1450 {
1451 return FALSE;
1452 }
1453 valid = key->verify(key, scheme, this->tbsCertificate, this->signature);
1454 key->destroy(key);
1455 return valid;
1456 }
1457
1458 METHOD(certificate_t, get_public_key, public_key_t*,
1459 private_x509_cert_t *this)
1460 {
1461 this->public_key->get_ref(this->public_key);
1462 return this->public_key;
1463 }
1464
1465 METHOD(certificate_t, get_ref, certificate_t*,
1466 private_x509_cert_t *this)
1467 {
1468 ref_get(&this->ref);
1469 return &this->public.interface.interface;
1470 }
1471
1472 METHOD(certificate_t, get_validity, bool,
1473 private_x509_cert_t *this, time_t *when, time_t *not_before,
1474 time_t *not_after)
1475 {
1476 time_t t = when ? *when : time(NULL);
1477
1478 if (not_before)
1479 {
1480 *not_before = this->notBefore;
1481 }
1482 if (not_after)
1483 {
1484 *not_after = this->notAfter;
1485 }
1486 return (t >= this->notBefore && t <= this->notAfter);
1487 }
1488
1489 METHOD(certificate_t, get_encoding, bool,
1490 private_x509_cert_t *this, cred_encoding_type_t type, chunk_t *encoding)
1491 {
1492 if (type == CERT_ASN1_DER)
1493 {
1494 *encoding = chunk_clone(this->encoding);
1495 return TRUE;
1496 }
1497 return lib->encoding->encode(lib->encoding, type, NULL, encoding,
1498 CRED_PART_X509_ASN1_DER, this->encoding, CRED_PART_END);
1499 }
1500
1501 METHOD(certificate_t, equals, bool,
1502 private_x509_cert_t *this, certificate_t *other)
1503 {
1504 chunk_t encoding;
1505 bool equal;
1506
1507 if (this == (private_x509_cert_t*)other)
1508 {
1509 return TRUE;
1510 }
1511 if (other->get_type(other) != CERT_X509)
1512 {
1513 return FALSE;
1514 }
1515 if (other->equals == (void*)equals)
1516 { /* skip allocation if we have the same implementation */
1517 return chunk_equals(this->encoding, ((private_x509_cert_t*)other)->encoding);
1518 }
1519 if (!other->get_encoding(other, CERT_ASN1_DER, &encoding))
1520 {
1521 return FALSE;
1522 }
1523 equal = chunk_equals(this->encoding, encoding);
1524 free(encoding.ptr);
1525 return equal;
1526 }
1527
1528 METHOD(x509_t, get_flags, x509_flag_t,
1529 private_x509_cert_t *this)
1530 {
1531 return this->flags;
1532 }
1533
1534 METHOD(x509_t, get_serial, chunk_t,
1535 private_x509_cert_t *this)
1536 {
1537 return this->serialNumber;
1538 }
1539
1540 METHOD(x509_t, get_subjectKeyIdentifier, chunk_t,
1541 private_x509_cert_t *this)
1542 {
1543 if (this->subjectKeyIdentifier.ptr)
1544 {
1545 return this->subjectKeyIdentifier;
1546 }
1547 else
1548 {
1549 chunk_t fingerprint;
1550
1551 if (this->public_key->get_fingerprint(this->public_key,
1552 KEYID_PUBKEY_SHA1, &fingerprint))
1553 {
1554 return fingerprint;
1555 }
1556 else
1557 {
1558 return chunk_empty;
1559 }
1560 }
1561 }
1562
1563 METHOD(x509_t, get_authKeyIdentifier, chunk_t,
1564 private_x509_cert_t *this)
1565 {
1566 return this->authKeyIdentifier;
1567 }
1568
1569 METHOD(x509_t, get_pathLenConstraint, int,
1570 private_x509_cert_t *this)
1571 {
1572 return this->pathLenConstraint;
1573 }
1574
1575 METHOD(x509_t, create_subjectAltName_enumerator, enumerator_t*,
1576 private_x509_cert_t *this)
1577 {
1578 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
1579 }
1580
1581 METHOD(x509_t, create_ocsp_uri_enumerator, enumerator_t*,
1582 private_x509_cert_t *this)
1583 {
1584 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
1585 }
1586
1587 /**
1588 * Convert enumerator value from entry to (uri, issuer)
1589 */
1590 static bool crl_enum_filter(identification_t *issuer_in,
1591 char **uri_in, char **uri_out,
1592 void *none_in, identification_t **issuer_out)
1593 {
1594 *uri_out = *uri_in;
1595 if (issuer_out)
1596 {
1597 *issuer_out = issuer_in;
1598 }
1599 return TRUE;
1600 }
1601
1602 /**
1603 * Create inner enumerator over URIs
1604 */
1605 static enumerator_t *crl_enum_create(crl_uri_t *entry)
1606 {
1607 return enumerator_create_filter(entry->uris->create_enumerator(entry->uris),
1608 (void*)crl_enum_filter, entry->issuer, NULL);
1609 }
1610
1611 METHOD(x509_t, create_crl_uri_enumerator, enumerator_t*,
1612 private_x509_cert_t *this)
1613 {
1614 return enumerator_create_nested(
1615 this->crl_uris->create_enumerator(this->crl_uris),
1616 (void*)crl_enum_create, NULL, NULL);
1617 }
1618
1619 METHOD(x509_t, create_ipAddrBlock_enumerator, enumerator_t*,
1620 private_x509_cert_t *this)
1621 {
1622 return this->ipAddrBlocks->create_enumerator(this->ipAddrBlocks);
1623 }
1624
1625 METHOD(x509_t, create_name_constraint_enumerator, enumerator_t*,
1626 private_x509_cert_t *this, bool perm)
1627 {
1628 if (perm)
1629 {
1630 return this->permitted_names->create_enumerator(this->permitted_names);
1631 }
1632 return this->excluded_names->create_enumerator(this->excluded_names);
1633 }
1634
1635 METHOD(x509_t, create_cert_policy_enumerator, enumerator_t*,
1636 private_x509_cert_t *this)
1637 {
1638 return this->cert_policies->create_enumerator(this->cert_policies);
1639 }
1640
1641 METHOD(certificate_t, destroy, void,
1642 private_x509_cert_t *this)
1643 {
1644 if (ref_put(&this->ref))
1645 {
1646 this->subjectAltNames->destroy_offset(this->subjectAltNames,
1647 offsetof(identification_t, destroy));
1648 this->crl_uris->destroy_function(this->crl_uris, (void*)crl_uri_destroy);
1649 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
1650 this->ipAddrBlocks->destroy_offset(this->ipAddrBlocks,
1651 offsetof(traffic_selector_t, destroy));
1652 this->permitted_names->destroy_offset(this->permitted_names,
1653 offsetof(identification_t, destroy));
1654 this->excluded_names->destroy_offset(this->excluded_names,
1655 offsetof(identification_t, destroy));
1656 this->cert_policies->destroy_function(this->cert_policies,
1657 (void*)cert_policy_destroy);
1658 DESTROY_IF(this->issuer);
1659 DESTROY_IF(this->subject);
1660 DESTROY_IF(this->public_key);
1661 chunk_free(&this->authKeyIdentifier);
1662 chunk_free(&this->encoding);
1663 chunk_free(&this->encoding_hash);
1664 if (!this->parsed)
1665 { /* only parsed certificates point these fields to "encoded" */
1666 chunk_free(&this->signature);
1667 chunk_free(&this->serialNumber);
1668 chunk_free(&this->tbsCertificate);
1669 }
1670 free(this);
1671 }
1672 }
1673
1674 /**
1675 * create an empty but initialized X.509 certificate
1676 */
1677 static private_x509_cert_t* create_empty(void)
1678 {
1679 private_x509_cert_t *this;
1680
1681 INIT(this,
1682 .public = {
1683 .interface = {
1684 .interface = {
1685 .get_type = _get_type,
1686 .get_subject = _get_subject,
1687 .get_issuer = _get_issuer,
1688 .has_subject = _has_subject,
1689 .has_issuer = _has_issuer,
1690 .issued_by = _issued_by,
1691 .get_public_key = _get_public_key,
1692 .get_validity = _get_validity,
1693 .get_encoding = _get_encoding,
1694 .equals = _equals,
1695 .get_ref = _get_ref,
1696 .destroy = _destroy,
1697 },
1698 .get_flags = _get_flags,
1699 .get_serial = _get_serial,
1700 .get_subjectKeyIdentifier = _get_subjectKeyIdentifier,
1701 .get_authKeyIdentifier = _get_authKeyIdentifier,
1702 .get_pathLenConstraint = _get_pathLenConstraint,
1703 .create_subjectAltName_enumerator = _create_subjectAltName_enumerator,
1704 .create_crl_uri_enumerator = _create_crl_uri_enumerator,
1705 .create_ocsp_uri_enumerator = _create_ocsp_uri_enumerator,
1706 .create_ipAddrBlock_enumerator = _create_ipAddrBlock_enumerator,
1707 .create_name_constraint_enumerator = _create_name_constraint_enumerator,
1708 .create_cert_policy_enumerator = _create_cert_policy_enumerator,
1709 },
1710 },
1711 .version = 1,
1712 .subjectAltNames = linked_list_create(),
1713 .crl_uris = linked_list_create(),
1714 .ocsp_uris = linked_list_create(),
1715 .ipAddrBlocks = linked_list_create(),
1716 .permitted_names = linked_list_create(),
1717 .excluded_names = linked_list_create(),
1718 .cert_policies = linked_list_create(),
1719 .pathLenConstraint = X509_NO_PATH_LEN_CONSTRAINT,
1720 .ref = 1,
1721 );
1722 return this;
1723 }
1724
1725 /**
1726 * Build a generalName from an id
1727 */
1728 chunk_t build_generalName(identification_t *id)
1729 {
1730 int context;
1731
1732 switch (id->get_type(id))
1733 {
1734 case ID_RFC822_ADDR:
1735 context = ASN1_CONTEXT_S_1;
1736 break;
1737 case ID_FQDN:
1738 context = ASN1_CONTEXT_S_2;
1739 break;
1740 case ID_DER_ASN1_DN:
1741 context = ASN1_CONTEXT_C_4;
1742 break;
1743 case ID_IPV4_ADDR:
1744 case ID_IPV6_ADDR:
1745 context = ASN1_CONTEXT_S_7;
1746 break;
1747 default:
1748 DBG1(DBG_LIB, "encoding %N as generalName not supported",
1749 id_type_names, id->get_type(id));
1750 return chunk_empty;
1751 }
1752 return asn1_wrap(context, "c", id->get_encoding(id));
1753 }
1754
1755 /**
1756 * Encode a linked list of subjectAltNames
1757 */
1758 chunk_t x509_build_subjectAltNames(linked_list_t *list)
1759 {
1760 chunk_t subjectAltNames = chunk_empty, name;
1761 enumerator_t *enumerator;
1762 identification_t *id;
1763
1764 if (list->get_count(list) == 0)
1765 {
1766 return chunk_empty;
1767 }
1768
1769 enumerator = list->create_enumerator(list);
1770 while (enumerator->enumerate(enumerator, &id))
1771 {
1772 name = build_generalName(id);
1773 subjectAltNames = chunk_cat("mm", subjectAltNames, name);
1774 }
1775 enumerator->destroy(enumerator);
1776
1777 return asn1_wrap(ASN1_SEQUENCE, "mm",
1778 asn1_build_known_oid(OID_SUBJECT_ALT_NAME),
1779 asn1_wrap(ASN1_OCTET_STRING, "m",
1780 asn1_wrap(ASN1_SEQUENCE, "m", subjectAltNames)
1781 )
1782 );
1783 }
1784
1785 /**
1786 * Generate and sign a new certificate
1787 */
1788 static bool generate(private_x509_cert_t *cert, certificate_t *sign_cert,
1789 private_key_t *sign_key, int digest_alg)
1790 {
1791 chunk_t extensions = chunk_empty, extendedKeyUsage = chunk_empty;
1792 chunk_t serverAuth = chunk_empty, clientAuth = chunk_empty;
1793 chunk_t ocspSigning = chunk_empty, certPolicies = chunk_empty;
1794 chunk_t basicConstraints = chunk_empty, nameConstraints = chunk_empty;
1795 chunk_t keyUsage = chunk_empty, keyUsageBits = chunk_empty;
1796 chunk_t subjectAltNames = chunk_empty;
1797 chunk_t subjectKeyIdentifier = chunk_empty, authKeyIdentifier = chunk_empty;
1798 chunk_t crlDistributionPoints = chunk_empty, authorityInfoAccess = chunk_empty;
1799 identification_t *issuer, *subject;
1800 crl_uri_t *entry;
1801 chunk_t key_info;
1802 signature_scheme_t scheme;
1803 hasher_t *hasher;
1804 enumerator_t *enumerator, *uris;
1805 char *uri;
1806
1807 subject = cert->subject;
1808 if (sign_cert)
1809 {
1810 issuer = sign_cert->get_subject(sign_cert);
1811 if (!cert->public_key)
1812 {
1813 return FALSE;
1814 }
1815 }
1816 else
1817 { /* self signed */
1818 issuer = subject;
1819 if (!cert->public_key)
1820 {
1821 cert->public_key = sign_key->get_public_key(sign_key);
1822 }
1823 cert->flags |= X509_SELF_SIGNED;
1824 }
1825 cert->issuer = issuer->clone(issuer);
1826 if (!cert->notBefore)
1827 {
1828 cert->notBefore = time(NULL);
1829 }
1830 if (!cert->notAfter)
1831 { /* defaults to 1 year from now */
1832 cert->notAfter = cert->notBefore + 60 * 60 * 24 * 365;
1833 }
1834
1835 /* select signature scheme */
1836 cert->algorithm = hasher_signature_algorithm_to_oid(digest_alg,
1837 sign_key->get_type(sign_key));
1838 if (cert->algorithm == OID_UNKNOWN)
1839 {
1840 return FALSE;
1841 }
1842 scheme = signature_scheme_from_oid(cert->algorithm);
1843
1844 if (!cert->public_key->get_encoding(cert->public_key,
1845 PUBKEY_SPKI_ASN1_DER, &key_info))
1846 {
1847 return FALSE;
1848 }
1849
1850 /* encode subjectAltNames */
1851 subjectAltNames = x509_build_subjectAltNames(cert->subjectAltNames);
1852
1853 /* encode CRL distribution points extension */
1854 enumerator = cert->crl_uris->create_enumerator(cert->crl_uris);
1855 while (enumerator->enumerate(enumerator, &entry))
1856 {
1857 chunk_t distributionPoint, gn;
1858 chunk_t crlIssuer = chunk_empty, gns = chunk_empty;
1859
1860 if (entry->issuer)
1861 {
1862 crlIssuer = asn1_wrap(ASN1_CONTEXT_C_2, "m",
1863 build_generalName(entry->issuer));
1864 }
1865 uris = entry->uris->create_enumerator(entry->uris);
1866 while (uris->enumerate(uris, &uri))
1867 {
1868 gn = asn1_wrap(ASN1_CONTEXT_S_6, "c", chunk_create(uri, strlen(uri)));
1869 gns = chunk_cat("mm", gns, gn);
1870 }
1871 uris->destroy(uris);
1872
1873 distributionPoint = asn1_wrap(ASN1_SEQUENCE, "mm",
1874 asn1_wrap(ASN1_CONTEXT_C_0, "m",
1875 asn1_wrap(ASN1_CONTEXT_C_0, "m", gns)),
1876 crlIssuer);
1877 crlDistributionPoints = chunk_cat("mm", crlDistributionPoints,
1878 distributionPoint);
1879 }
1880 enumerator->destroy(enumerator);
1881 if (crlDistributionPoints.ptr)
1882 {
1883 crlDistributionPoints = asn1_wrap(ASN1_SEQUENCE, "mm",
1884 asn1_build_known_oid(OID_CRL_DISTRIBUTION_POINTS),
1885 asn1_wrap(ASN1_OCTET_STRING, "m",
1886 asn1_wrap(ASN1_SEQUENCE, "m", crlDistributionPoints)));
1887 }
1888
1889 /* encode OCSP URIs in authorityInfoAccess extension */
1890 enumerator = cert->ocsp_uris->create_enumerator(cert->ocsp_uris);
1891 while (enumerator->enumerate(enumerator, &uri))
1892 {
1893 chunk_t accessDescription;
1894
1895 accessDescription = asn1_wrap(ASN1_SEQUENCE, "mm",
1896 asn1_build_known_oid(OID_OCSP),
1897 asn1_wrap(ASN1_CONTEXT_S_6, "c",
1898 chunk_create(uri, strlen(uri))));
1899 authorityInfoAccess = chunk_cat("mm", authorityInfoAccess,
1900 accessDescription);
1901 }
1902 enumerator->destroy(enumerator);
1903 if (authorityInfoAccess.ptr)
1904 {
1905 authorityInfoAccess = asn1_wrap(ASN1_SEQUENCE, "mm",
1906 asn1_build_known_oid(OID_AUTHORITY_INFO_ACCESS),
1907 asn1_wrap(ASN1_OCTET_STRING, "m",
1908 asn1_wrap(ASN1_SEQUENCE, "m", authorityInfoAccess)));
1909 }
1910
1911 /* build CA basicConstraint and keyUsage flags for CA certificates */
1912 if (cert->flags & X509_CA)
1913 {
1914 chunk_t pathLenConstraint = chunk_empty;
1915
1916 if (cert->pathLenConstraint != X509_NO_PATH_LEN_CONSTRAINT)
1917 {
1918 char pathlen = (char)cert->pathLenConstraint;
1919
1920 pathLenConstraint = asn1_integer("c", chunk_from_thing(pathlen));
1921 }
1922 basicConstraints = asn1_wrap(ASN1_SEQUENCE, "mmm",
1923 asn1_build_known_oid(OID_BASIC_CONSTRAINTS),
1924 asn1_wrap(ASN1_BOOLEAN, "c",
1925 chunk_from_chars(0xFF)),
1926 asn1_wrap(ASN1_OCTET_STRING, "m",
1927 asn1_wrap(ASN1_SEQUENCE, "mm",
1928 asn1_wrap(ASN1_BOOLEAN, "c",
1929 chunk_from_chars(0xFF)),
1930 pathLenConstraint)));
1931 /* set CertificateSign and implicitly CRLsign */
1932 keyUsageBits = chunk_from_chars(0x01, 0x06);
1933 }
1934 else if (cert->flags & X509_CRL_SIGN)
1935 {
1936 keyUsageBits = chunk_from_chars(0x01, 0x02);
1937 }
1938 if (keyUsageBits.len)
1939 {
1940 keyUsage = asn1_wrap(ASN1_SEQUENCE, "mmm",
1941 asn1_build_known_oid(OID_KEY_USAGE),
1942 asn1_wrap(ASN1_BOOLEAN, "c", chunk_from_chars(0xFF)),
1943 asn1_wrap(ASN1_OCTET_STRING, "m",
1944 asn1_wrap(ASN1_BIT_STRING, "c", keyUsageBits)));
1945 }
1946
1947 /* add serverAuth extendedKeyUsage flag */
1948 if (cert->flags & X509_SERVER_AUTH)
1949 {
1950 serverAuth = asn1_build_known_oid(OID_SERVER_AUTH);
1951 }
1952 if (cert->flags & X509_CLIENT_AUTH)
1953 {
1954 clientAuth = asn1_build_known_oid(OID_CLIENT_AUTH);
1955 }
1956
1957 /* add ocspSigning extendedKeyUsage flag */
1958 if (cert->flags & X509_OCSP_SIGNER)
1959 {
1960 ocspSigning = asn1_build_known_oid(OID_OCSP_SIGNING);
1961 }
1962
1963 if (serverAuth.ptr || clientAuth.ptr || ocspSigning.ptr)
1964 {
1965 extendedKeyUsage = asn1_wrap(ASN1_SEQUENCE, "mm",
1966 asn1_build_known_oid(OID_EXTENDED_KEY_USAGE),
1967 asn1_wrap(ASN1_OCTET_STRING, "m",
1968 asn1_wrap(ASN1_SEQUENCE, "mmm",
1969 serverAuth, clientAuth, ocspSigning)));
1970 }
1971
1972 /* add subjectKeyIdentifier to CA and OCSP signer certificates */
1973 if (cert->flags & (X509_CA | X509_OCSP_SIGNER | X509_CRL_SIGN))
1974 {
1975 chunk_t keyid;
1976
1977 if (cert->public_key->get_fingerprint(cert->public_key,
1978 KEYID_PUBKEY_SHA1, &keyid))
1979 {
1980 subjectKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1981 asn1_build_known_oid(OID_SUBJECT_KEY_ID),
1982 asn1_wrap(ASN1_OCTET_STRING, "m",
1983 asn1_wrap(ASN1_OCTET_STRING, "c", keyid)));
1984 }
1985 }
1986
1987 /* add the keyid authKeyIdentifier for non self-signed certificates */
1988 if (sign_key)
1989 {
1990 chunk_t keyid;
1991
1992 if (sign_key->get_fingerprint(sign_key, KEYID_PUBKEY_SHA1, &keyid))
1993 {
1994 authKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1995 asn1_build_known_oid(OID_AUTHORITY_KEY_ID),
1996 asn1_wrap(ASN1_OCTET_STRING, "m",
1997 asn1_wrap(ASN1_SEQUENCE, "m",
1998 asn1_wrap(ASN1_CONTEXT_S_0, "c", keyid))));
1999 }
2000 }
2001
2002 if (cert->permitted_names->get_count(cert->permitted_names) ||
2003 cert->excluded_names->get_count(cert->excluded_names))
2004 {
2005 chunk_t permitted = chunk_empty, excluded = chunk_empty, subtree;
2006 identification_t *id;
2007
2008 enumerator = create_name_constraint_enumerator(cert, TRUE);
2009 while (enumerator->enumerate(enumerator, &id))
2010 {
2011 subtree = asn1_wrap(ASN1_SEQUENCE, "m", build_generalName(id));
2012 permitted = chunk_cat("mm", permitted, subtree);
2013 }
2014 enumerator->destroy(enumerator);
2015 if (permitted.ptr)
2016 {
2017 permitted = asn1_wrap(ASN1_CONTEXT_C_0, "m", permitted);
2018 }
2019
2020 enumerator = create_name_constraint_enumerator(cert, FALSE);
2021 while (enumerator->enumerate(enumerator, &id))
2022 {
2023 subtree = asn1_wrap(ASN1_SEQUENCE, "m", build_generalName(id));
2024 excluded = chunk_cat("mm", excluded, subtree);
2025 }
2026 enumerator->destroy(enumerator);
2027 if (excluded.ptr)
2028 {
2029 excluded = asn1_wrap(ASN1_CONTEXT_C_1, "m", excluded);
2030 }
2031
2032 nameConstraints = asn1_wrap(ASN1_SEQUENCE, "mm",
2033 asn1_build_known_oid(OID_NAME_CONSTRAINTS),
2034 asn1_wrap(ASN1_OCTET_STRING, "m",
2035 asn1_wrap(ASN1_SEQUENCE, "mm",
2036 permitted, excluded)));
2037 }
2038
2039 if (cert->cert_policies->get_count(cert->cert_policies))
2040 {
2041 x509_cert_policy_t *policy;
2042
2043 enumerator = create_cert_policy_enumerator(cert);
2044 while (enumerator->enumerate(enumerator, &policy))
2045 {
2046 chunk_t chunk = chunk_empty, cps = chunk_empty, notice = chunk_empty;
2047
2048 if (policy->cps_uri)
2049 {
2050 cps = asn1_wrap(ASN1_SEQUENCE, "mm",
2051 asn1_build_known_oid(OID_POLICY_QUALIFIER_CPS),
2052 asn1_wrap(ASN1_IA5STRING, "c",
2053 chunk_create(policy->cps_uri,
2054 strlen(policy->cps_uri))));
2055 }
2056 if (policy->unotice_text)
2057 {
2058 notice = asn1_wrap(ASN1_SEQUENCE, "mm",
2059 asn1_build_known_oid(OID_POLICY_QUALIFIER_UNOTICE),
2060 asn1_wrap(ASN1_SEQUENCE, "m",
2061 asn1_wrap(ASN1_VISIBLESTRING, "c",
2062 chunk_create(policy->unotice_text,
2063 strlen(policy->unotice_text)))));
2064 }
2065 if (cps.len || notice.len)
2066 {
2067 chunk = asn1_wrap(ASN1_SEQUENCE, "mm", cps, notice);
2068 }
2069 chunk = asn1_wrap(ASN1_SEQUENCE, "mm",
2070 asn1_wrap(ASN1_OID, "c", policy->oid), chunk);
2071 certPolicies = chunk_cat("mm", certPolicies, chunk);
2072 }
2073 enumerator->destroy(enumerator);
2074
2075 certPolicies = asn1_wrap(ASN1_SEQUENCE, "mm",
2076 asn1_build_known_oid(OID_CERTIFICATE_POLICIES),
2077 asn1_wrap(ASN1_OCTET_STRING, "m",
2078 asn1_wrap(ASN1_SEQUENCE, "m", certPolicies)));
2079 }
2080
2081 if (basicConstraints.ptr || subjectAltNames.ptr || authKeyIdentifier.ptr ||
2082 crlDistributionPoints.ptr || nameConstraints.ptr)
2083 {
2084 extensions = asn1_wrap(ASN1_CONTEXT_C_3, "m",
2085 asn1_wrap(ASN1_SEQUENCE, "mmmmmmmmmm",
2086 basicConstraints, keyUsage, subjectKeyIdentifier,
2087 authKeyIdentifier, subjectAltNames,
2088 extendedKeyUsage, crlDistributionPoints,
2089 authorityInfoAccess, nameConstraints, certPolicies));
2090 }
2091
2092 cert->tbsCertificate = asn1_wrap(ASN1_SEQUENCE, "mmmcmcmm",
2093 asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2),
2094 asn1_integer("c", cert->serialNumber),
2095 asn1_algorithmIdentifier(cert->algorithm),
2096 issuer->get_encoding(issuer),
2097 asn1_wrap(ASN1_SEQUENCE, "mm",
2098 asn1_from_time(&cert->notBefore, ASN1_UTCTIME),
2099 asn1_from_time(&cert->notAfter, ASN1_UTCTIME)),
2100 subject->get_encoding(subject),
2101 key_info, extensions);
2102
2103 if (!sign_key->sign(sign_key, scheme, cert->tbsCertificate, &cert->signature))
2104 {
2105 return FALSE;
2106 }
2107 cert->encoding = asn1_wrap(ASN1_SEQUENCE, "cmm", cert->tbsCertificate,
2108 asn1_algorithmIdentifier(cert->algorithm),
2109 asn1_bitstring("c", cert->signature));
2110
2111 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
2112 if (!hasher)
2113 {
2114 return FALSE;
2115 }
2116 hasher->allocate_hash(hasher, cert->encoding, &cert->encoding_hash);
2117 hasher->destroy(hasher);
2118 return TRUE;
2119 }
2120
2121 /**
2122 * See header.
2123 */
2124 x509_cert_t *x509_cert_load(certificate_type_t type, va_list args)
2125 {
2126 x509_flag_t flags = 0;
2127 chunk_t blob = chunk_empty;
2128
2129 while (TRUE)
2130 {
2131 switch (va_arg(args, builder_part_t))
2132 {
2133 case BUILD_BLOB_ASN1_DER:
2134 blob = va_arg(args, chunk_t);
2135 continue;
2136 case BUILD_X509_FLAG:
2137 flags |= va_arg(args, x509_flag_t);
2138 continue;
2139 case BUILD_END:
2140 break;
2141 default:
2142 return NULL;
2143 }
2144 break;
2145 }
2146
2147 if (blob.ptr)
2148 {
2149 private_x509_cert_t *cert = create_empty();
2150
2151 cert->encoding = chunk_clone(blob);
2152 cert->parsed = TRUE;
2153 if (parse_certificate(cert))
2154 {
2155 cert->flags |= flags;
2156 return &cert->public;
2157 }
2158 destroy(cert);
2159 }
2160 return NULL;
2161 }
2162
2163 /**
2164 * See header.
2165 */
2166 x509_cert_t *x509_cert_gen(certificate_type_t type, va_list args)
2167 {
2168 private_x509_cert_t *cert;
2169 certificate_t *sign_cert = NULL;
2170 private_key_t *sign_key = NULL;
2171 identification_t *crl_issuer = NULL;
2172 hash_algorithm_t digest_alg = HASH_SHA1;
2173
2174 cert = create_empty();
2175 while (TRUE)
2176 {
2177 switch (va_arg(args, builder_part_t))
2178 {
2179 case BUILD_X509_FLAG:
2180 cert->flags |= va_arg(args, x509_flag_t);
2181 continue;
2182 case BUILD_SIGNING_KEY:
2183 sign_key = va_arg(args, private_key_t*);
2184 continue;
2185 case BUILD_SIGNING_CERT:
2186 sign_cert = va_arg(args, certificate_t*);
2187 continue;
2188 case BUILD_PUBLIC_KEY:
2189 cert->public_key = va_arg(args, public_key_t*);
2190 cert->public_key->get_ref(cert->public_key);
2191 continue;
2192 case BUILD_SUBJECT:
2193 cert->subject = va_arg(args, identification_t*);
2194 cert->subject = cert->subject->clone(cert->subject);
2195 continue;
2196 case BUILD_SUBJECT_ALTNAMES:
2197 {
2198 enumerator_t *enumerator;
2199 identification_t *id;
2200 linked_list_t *list;
2201
2202 list = va_arg(args, linked_list_t*);
2203 enumerator = list->create_enumerator(list);
2204 while (enumerator->enumerate(enumerator, &id))
2205 {
2206 cert->subjectAltNames->insert_last(cert->subjectAltNames,
2207 id->clone(id));
2208 }
2209 enumerator->destroy(enumerator);
2210 continue;
2211 }
2212 case BUILD_CRL_DISTRIBUTION_POINTS:
2213 {
2214 enumerator_t *enumerator;
2215 linked_list_t *list;
2216 crl_uri_t *entry;
2217 char *uri;
2218
2219 list = va_arg(args, linked_list_t*);
2220 if (list->get_count(list))
2221 {
2222 entry = crl_uri_create(crl_issuer);
2223 enumerator = list->create_enumerator(list);
2224 while (enumerator->enumerate(enumerator, &uri))
2225 {
2226 entry->uris->insert_last(entry->uris, strdup(uri));
2227 }
2228 enumerator->destroy(enumerator);
2229 cert->crl_uris->insert_last(cert->crl_uris, entry);
2230 }
2231 continue;
2232 }
2233 case BUILD_CRL_ISSUER:
2234 {
2235 crl_issuer = va_arg(args, identification_t*);
2236 continue;
2237 }
2238 case BUILD_OCSP_ACCESS_LOCATIONS:
2239 {
2240 enumerator_t *enumerator;
2241 linked_list_t *list;
2242 char *uri;
2243
2244 list = va_arg(args, linked_list_t*);
2245 enumerator = list->create_enumerator(list);
2246 while (enumerator->enumerate(enumerator, &uri))
2247 {
2248 cert->ocsp_uris->insert_last(cert->ocsp_uris, strdup(uri));
2249 }
2250 enumerator->destroy(enumerator);
2251 continue;
2252 }
2253 case BUILD_PATHLEN:
2254 cert->pathLenConstraint = va_arg(args, int);
2255 if (cert->pathLenConstraint < 0 || cert->pathLenConstraint > 127)
2256 {
2257 cert->pathLenConstraint = X509_NO_PATH_LEN_CONSTRAINT;
2258 }
2259 continue;
2260 case BUILD_PERMITTED_NAME_CONSTRAINTS:
2261 {
2262 enumerator_t *enumerator;
2263 linked_list_t *list;
2264 identification_t *constraint;
2265
2266 list = va_arg(args, linked_list_t*);
2267 enumerator = list->create_enumerator(list);
2268 while (enumerator->enumerate(enumerator, &constraint))
2269 {
2270 cert->permitted_names->insert_last(cert->permitted_names,
2271 constraint->clone(constraint));
2272 }
2273 enumerator->destroy(enumerator);
2274 continue;
2275 }
2276 case BUILD_EXCLUDED_NAME_CONSTRAINTS:
2277 {
2278 enumerator_t *enumerator;
2279 linked_list_t *list;
2280 identification_t *constraint;
2281
2282 list = va_arg(args, linked_list_t*);
2283 enumerator = list->create_enumerator(list);
2284 while (enumerator->enumerate(enumerator, &constraint))
2285 {
2286 cert->excluded_names->insert_last(cert->excluded_names,
2287 constraint->clone(constraint));
2288 }
2289 enumerator->destroy(enumerator);
2290 continue;
2291 }
2292 case BUILD_CERTIFICATE_POLICIES:
2293 {
2294 enumerator_t *enumerator;
2295 linked_list_t *list;
2296 x509_cert_policy_t *policy, *in;
2297
2298 list = va_arg(args, linked_list_t*);
2299 enumerator = list->create_enumerator(list);
2300 while (enumerator->enumerate(enumerator, &in))
2301 {
2302 INIT(policy,
2303 .oid = chunk_clone(in->oid),
2304 .cps_uri = strdupnull(in->cps_uri),
2305 .unotice_text = strdupnull(in->unotice_text),
2306 );
2307 cert->cert_policies->insert_last(cert->cert_policies, policy);
2308 }
2309 enumerator->destroy(enumerator);
2310 continue;
2311 }
2312 case BUILD_NOT_BEFORE_TIME:
2313 cert->notBefore = va_arg(args, time_t);
2314 continue;
2315 case BUILD_NOT_AFTER_TIME:
2316 cert->notAfter = va_arg(args, time_t);
2317 continue;
2318 case BUILD_SERIAL:
2319 cert->serialNumber = chunk_clone(va_arg(args, chunk_t));
2320 continue;
2321 case BUILD_DIGEST_ALG:
2322 digest_alg = va_arg(args, int);
2323 continue;
2324 case BUILD_END:
2325 break;
2326 default:
2327 destroy(cert);
2328 return NULL;
2329 }
2330 break;
2331 }
2332
2333 if (sign_key && generate(cert, sign_cert, sign_key, digest_alg))
2334 {
2335 return &cert->public;
2336 }
2337 destroy(cert);
2338 return NULL;
2339 }
2340