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