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