ipsec pki --self|issue supports --pathlen option setting a path length constraint
[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 allocated char*
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 * certificate's embedded public key
136 */
137 public_key_t *public_key;
138
139 /**
140 * Subject Key Identifier
141 */
142 chunk_t subjectKeyIdentifier;
143
144 /**
145 * Authority Key Identifier
146 */
147 chunk_t authKeyIdentifier;
148
149 /**
150 * Authority Key Serial Number
151 */
152 chunk_t authKeySerialNumber;
153
154 /**
155 * Path Length Constraint
156 */
157 int pathLenConstraint;
158
159 /**
160 * x509 constraints and other flags
161 */
162 x509_flag_t flags;
163
164 /**
165 * Signature algorithm
166 */
167 int algorithm;
168
169 /**
170 * Signature
171 */
172 chunk_t signature;
173
174 /**
175 * Certificate parsed from blob/file?
176 */
177 bool parsed;
178
179 /**
180 * reference count
181 */
182 refcount_t ref;
183 };
184
185 static const chunk_t ASN1_subjectAltName_oid = chunk_from_chars(
186 0x06, 0x03, 0x55, 0x1D, 0x11
187 );
188
189 /**
190 * ASN.1 definition of a basicConstraints extension
191 */
192 static const asn1Object_t basicConstraintsObjects[] = {
193 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
194 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
195 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
196 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
197 { 0, "exit", ASN1_EOC, ASN1_EXIT }
198 };
199 #define BASIC_CONSTRAINTS_CA 1
200 #define BASIC_CONSTRAINTS_PATH_LEN 2
201
202 /**
203 * Extracts the basicConstraints extension
204 */
205 static void parse_basicConstraints(chunk_t blob, int level0,
206 private_x509_cert_t *this)
207 {
208 asn1_parser_t *parser;
209 chunk_t object;
210 int objectID;
211 bool isCA = FALSE;
212
213 parser = asn1_parser_create(basicConstraintsObjects, blob);
214 parser->set_top_level(parser, level0);
215
216 while (parser->iterate(parser, &objectID, &object))
217 {
218 switch (objectID)
219 {
220 case BASIC_CONSTRAINTS_CA:
221 isCA = object.len && *object.ptr;
222 DBG2(" %s", isCA ? "TRUE" : "FALSE");
223 if (isCA)
224 {
225 this->flags |= X509_CA;
226 }
227 break;
228 case BASIC_CONSTRAINTS_PATH_LEN:
229 if (isCA)
230 {
231 if (object.len == 0)
232 {
233 this->pathLenConstraint = 0;
234 }
235 else if (object.len == 1)
236 {
237 this->pathLenConstraint = *object.ptr;
238 }
239 /* we ignore path length constraints > 127 */
240 }
241 break;
242 default:
243 break;
244 }
245 }
246 parser->destroy(parser);
247 }
248
249 /**
250 * ASN.1 definition of otherName
251 */
252 static const asn1Object_t otherNameObjects[] = {
253 {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */
254 {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY }, /* 1 */
255 {0, "exit", ASN1_EOC, ASN1_EXIT }
256 };
257 #define ON_OBJ_ID_TYPE 0
258 #define ON_OBJ_VALUE 1
259
260 /**
261 * Extracts an otherName
262 */
263 static bool parse_otherName(chunk_t blob, int level0)
264 {
265 asn1_parser_t *parser;
266 chunk_t object;
267 int objectID;
268 int oid = OID_UNKNOWN;
269 bool success = FALSE;
270
271 parser = asn1_parser_create(otherNameObjects, blob);
272 parser->set_top_level(parser, level0);
273
274 while (parser->iterate(parser, &objectID, &object))
275 {
276 switch (objectID)
277 {
278 case ON_OBJ_ID_TYPE:
279 oid = asn1_known_oid(object);
280 break;
281 case ON_OBJ_VALUE:
282 if (oid == OID_XMPP_ADDR)
283 {
284 if (!asn1_parse_simple_object(&object, ASN1_UTF8STRING,
285 parser->get_level(parser)+1, "xmppAddr"))
286 {
287 goto end;
288 }
289 }
290 break;
291 default:
292 break;
293 }
294 }
295 success = parser->success(parser);
296
297 end:
298 parser->destroy(parser);
299 return success;
300 }
301
302 /**
303 * ASN.1 definition of generalName
304 */
305 static const asn1Object_t generalNameObjects[] = {
306 { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */
307 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */
308 { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */
309 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */
310 { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */
311 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
312 { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */
313 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
314 { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */
315 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
316 { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */
317 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
318 { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */
319 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */
320 { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */
321 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */
322 { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */
323 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 17 */
324 { 0, "exit", ASN1_EOC, ASN1_EXIT }
325 };
326 #define GN_OBJ_OTHER_NAME 0
327 #define GN_OBJ_RFC822_NAME 2
328 #define GN_OBJ_DNS_NAME 4
329 #define GN_OBJ_X400_ADDRESS 6
330 #define GN_OBJ_DIRECTORY_NAME 8
331 #define GN_OBJ_EDI_PARTY_NAME 10
332 #define GN_OBJ_URI 12
333 #define GN_OBJ_IP_ADDRESS 14
334 #define GN_OBJ_REGISTERED_ID 16
335
336 /**
337 * Extracts a generalName
338 */
339 static identification_t *parse_generalName(chunk_t blob, int level0)
340 {
341 asn1_parser_t *parser;
342 chunk_t object;
343 int objectID ;
344
345 identification_t *gn = NULL;
346
347 parser = asn1_parser_create(generalNameObjects, blob);
348 parser->set_top_level(parser, level0);
349
350 while (parser->iterate(parser, &objectID, &object))
351 {
352 id_type_t id_type = ID_ANY;
353
354 switch (objectID)
355 {
356 case GN_OBJ_RFC822_NAME:
357 id_type = ID_RFC822_ADDR;
358 break;
359 case GN_OBJ_DNS_NAME:
360 id_type = ID_FQDN;
361 break;
362 case GN_OBJ_URI:
363 id_type = ID_DER_ASN1_GN_URI;
364 break;
365 case GN_OBJ_DIRECTORY_NAME:
366 id_type = ID_DER_ASN1_DN;
367 break;
368 case GN_OBJ_IP_ADDRESS:
369 id_type = ID_IPV4_ADDR;
370 break;
371 case GN_OBJ_OTHER_NAME:
372 if (!parse_otherName(object, parser->get_level(parser)+1))
373 {
374 goto end;
375 }
376 break;
377 case GN_OBJ_X400_ADDRESS:
378 case GN_OBJ_EDI_PARTY_NAME:
379 case GN_OBJ_REGISTERED_ID:
380 default:
381 break;
382 }
383 if (id_type != ID_ANY)
384 {
385 gn = identification_create_from_encoding(id_type, object);
386 DBG2(" '%Y'", gn);
387 goto end;
388 }
389 }
390
391 end:
392 parser->destroy(parser);
393 return gn;
394 }
395
396 /**
397 * ASN.1 definition of generalNames
398 */
399 static const asn1Object_t generalNamesObjects[] = {
400 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
401 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
402 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
403 { 0, "exit", ASN1_EOC, ASN1_EXIT }
404 };
405 #define GENERAL_NAMES_GN 1
406
407 /**
408 * Extracts one or several GNs and puts them into a chained list
409 */
410 void x509_parse_generalNames(chunk_t blob, int level0, bool implicit, linked_list_t *list)
411 {
412 asn1_parser_t *parser;
413 chunk_t object;
414 int objectID;
415
416 parser = asn1_parser_create(generalNamesObjects, blob);
417 parser->set_top_level(parser, level0);
418 parser->set_flags(parser, implicit, FALSE);
419
420 while (parser->iterate(parser, &objectID, &object))
421 {
422 if (objectID == GENERAL_NAMES_GN)
423 {
424 identification_t *gn = parse_generalName(object,
425 parser->get_level(parser)+1);
426
427 if (gn)
428 {
429 list->insert_last(list, (void *)gn);
430 }
431 }
432 }
433 parser->destroy(parser);
434 }
435
436 /**
437 * ASN.1 definition of a authorityKeyIdentifier extension
438 */
439 static const asn1Object_t authKeyIdentifierObjects[] = {
440 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
441 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_BODY }, /* 1 */
442 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
443 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
444 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
445 { 1, "authorityCertSerialNumber", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
446 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 6 */
447 { 0, "exit", ASN1_EOC, ASN1_EXIT }
448 };
449 #define AUTH_KEY_ID_KEY_ID 1
450 #define AUTH_KEY_ID_CERT_ISSUER 3
451 #define AUTH_KEY_ID_CERT_SERIAL 5
452
453 /**
454 * Extracts an authoritykeyIdentifier
455 */
456 chunk_t x509_parse_authorityKeyIdentifier(chunk_t blob, int level0,
457 chunk_t *authKeySerialNumber)
458 {
459 asn1_parser_t *parser;
460 chunk_t object;
461 int objectID;
462 chunk_t authKeyIdentifier = chunk_empty;
463
464 *authKeySerialNumber = chunk_empty;
465
466 parser = asn1_parser_create(authKeyIdentifierObjects, blob);
467 parser->set_top_level(parser, level0);
468
469 while (parser->iterate(parser, &objectID, &object))
470 {
471 switch (objectID)
472 {
473 case AUTH_KEY_ID_KEY_ID:
474 authKeyIdentifier = chunk_clone(object);
475 break;
476 case AUTH_KEY_ID_CERT_ISSUER:
477 /* TODO: x509_parse_generalNames(object, level+1, TRUE); */
478 break;
479 case AUTH_KEY_ID_CERT_SERIAL:
480 *authKeySerialNumber = object;
481 break;
482 default:
483 break;
484 }
485 }
486 parser->destroy(parser);
487 return authKeyIdentifier;
488 }
489
490 /**
491 * ASN.1 definition of a authorityInfoAccess extension
492 */
493 static const asn1Object_t authInfoAccessObjects[] = {
494 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
495 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
496 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
497 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
498 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
499 { 0, "exit", ASN1_EOC, ASN1_EXIT }
500 };
501 #define AUTH_INFO_ACCESS_METHOD 2
502 #define AUTH_INFO_ACCESS_LOCATION 3
503
504 /**
505 * Extracts an authorityInfoAcess location
506 */
507 static void parse_authorityInfoAccess(chunk_t blob, int level0,
508 private_x509_cert_t *this)
509 {
510 asn1_parser_t *parser;
511 chunk_t object;
512 int objectID;
513 int accessMethod = OID_UNKNOWN;
514
515 parser = asn1_parser_create(authInfoAccessObjects, blob);
516 parser->set_top_level(parser, level0);
517
518 while (parser->iterate(parser, &objectID, &object))
519 {
520 switch (objectID)
521 {
522 case AUTH_INFO_ACCESS_METHOD:
523 accessMethod = asn1_known_oid(object);
524 break;
525 case AUTH_INFO_ACCESS_LOCATION:
526 {
527 switch (accessMethod)
528 {
529 case OID_OCSP:
530 case OID_CA_ISSUERS:
531 {
532 identification_t *id;
533 char *uri;
534
535 id = parse_generalName(object,
536 parser->get_level(parser)+1);
537 if (id == NULL)
538 {
539 /* parsing went wrong - abort */
540 goto end;
541 }
542 DBG2(" '%Y'", id);
543 if (accessMethod == OID_OCSP &&
544 asprintf(&uri, "%Y", id) > 0)
545 {
546 this->ocsp_uris->insert_last(this->ocsp_uris, uri);
547 }
548 id->destroy(id);
549 }
550 break;
551 default:
552 /* unkown accessMethod, ignoring */
553 break;
554 }
555 break;
556 }
557 default:
558 break;
559 }
560 }
561
562 end:
563 parser->destroy(parser);
564 }
565
566 /**
567 * ASN.1 definition of a extendedKeyUsage extension
568 */
569 static const asn1Object_t extendedKeyUsageObjects[] = {
570 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
571 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
572 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
573 { 0, "exit", ASN1_EOC, ASN1_EXIT }
574 };
575 #define EXT_KEY_USAGE_PURPOSE_ID 1
576
577 /**
578 * Extracts extendedKeyUsage OIDs - currently only OCSP_SIGING is returned
579 */
580 static void parse_extendedKeyUsage(chunk_t blob, int level0,
581 private_x509_cert_t *this)
582 {
583 asn1_parser_t *parser;
584 chunk_t object;
585 int objectID;
586
587 parser = asn1_parser_create(extendedKeyUsageObjects, blob);
588 parser->set_top_level(parser, level0);
589
590 while (parser->iterate(parser, &objectID, &object))
591 {
592 if (objectID == EXT_KEY_USAGE_PURPOSE_ID)
593 {
594 switch (asn1_known_oid(object))
595 {
596 case OID_SERVER_AUTH:
597 this->flags |= X509_SERVER_AUTH;
598 break;
599 case OID_OCSP_SIGNING:
600 this->flags |= X509_OCSP_SIGNER;
601 break;
602 default:
603 break;
604 }
605 }
606 }
607 parser->destroy(parser);
608 }
609
610 /**
611 * ASN.1 definition of crlDistributionPoints
612 */
613 static const asn1Object_t crlDistributionPointsObjects[] = {
614 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
615 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
616 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
617 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
618 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
619 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
620 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
621 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
622 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
623 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
624 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
625 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
626 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
627 { 0, "exit", ASN1_EOC, ASN1_EXIT }
628 };
629 #define CRL_DIST_POINTS_FULLNAME 3
630
631 /**
632 * Extracts one or several crlDistributionPoints into a list
633 */
634 static void parse_crlDistributionPoints(chunk_t blob, int level0,
635 private_x509_cert_t *this)
636 {
637 asn1_parser_t *parser;
638 chunk_t object;
639 int objectID;
640 linked_list_t *list = linked_list_create();
641
642 parser = asn1_parser_create(crlDistributionPointsObjects, blob);
643 parser->set_top_level(parser, level0);
644
645 while (parser->iterate(parser, &objectID, &object))
646 {
647 if (objectID == CRL_DIST_POINTS_FULLNAME)
648 {
649 identification_t *id;
650
651 /* append extracted generalNames to existing chained list */
652 x509_parse_generalNames(object, parser->get_level(parser)+1,
653 TRUE, list);
654
655 while (list->remove_last(list, (void**)&id) == SUCCESS)
656 {
657 char *uri;
658
659 if (asprintf(&uri, "%Y", id) > 0)
660 {
661 this->crl_uris->insert_last(this->crl_uris, uri);
662 }
663 id->destroy(id);
664 }
665 }
666 }
667 parser->destroy(parser);
668 list->destroy(list);
669 }
670
671 /**
672 * ASN.1 definition of ipAddrBlocks according to RFC 3779
673 */
674 static const asn1Object_t ipAddrBlocksObjects[] = {
675 { 0, "ipAddrBlocks", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
676 { 1, "ipAddressFamily", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
677 { 2, "addressFamily", ASN1_OCTET_STRING, ASN1_BODY }, /* 2 */
678 { 2, "inherit", ASN1_NULL, ASN1_OPT|ASN1_NONE }, /* 3 */
679 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
680 { 2, "addressesOrRanges", ASN1_SEQUENCE, ASN1_OPT|ASN1_LOOP }, /* 5 */
681 { 3, "addressPrefix", ASN1_BIT_STRING, ASN1_OPT|ASN1_BODY }, /* 6 */
682 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
683 { 3, "addressRange", ASN1_SEQUENCE, ASN1_OPT|ASN1_NONE }, /* 8 */
684 { 4, "min", ASN1_BIT_STRING, ASN1_BODY }, /* 9 */
685 { 4, "max", ASN1_BIT_STRING, ASN1_BODY }, /* 10 */
686 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
687 { 2, "end choice/loop", ASN1_EOC, ASN1_END }, /* 12 */
688 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 13 */
689 { 0, "exit", ASN1_EOC, ASN1_EXIT }
690 };
691 #define IP_ADDR_BLOCKS_FAMILY 2
692 #define IP_ADDR_BLOCKS_INHERIT 3
693 #define IP_ADDR_BLOCKS_PREFIX 6
694 #define IP_ADDR_BLOCKS_MIN 9
695 #define IP_ADDR_BLOCKS_MAX 10
696
697 static bool check_address_object(ts_type_t ts_type, chunk_t object)
698 {
699 switch (ts_type)
700 {
701 case TS_IPV4_ADDR_RANGE:
702 if (object.len > 5)
703 {
704 DBG1("IPv4 address object is larger than 5 octets");
705 return FALSE;
706 }
707 break;
708 case TS_IPV6_ADDR_RANGE:
709 if (object.len > 17)
710 {
711 DBG1("IPv6 address object is larger than 17 octets");
712 return FALSE;
713 }
714 break;
715 default:
716 DBG1("unknown address family");
717 return FALSE;
718 }
719 if (object.len == 0)
720 {
721 DBG1("An ASN.1 bit string must contain at least the initial octet");
722 return FALSE;
723 }
724 if (object.len == 1 && object.ptr[0] != 0)
725 {
726 DBG1("An empty ASN.1 bit string must contain a zero initial octet");
727 return FALSE;
728 }
729 if (object.ptr[0] > 7)
730 {
731 DBG1("number of unused bits is too large");
732 return FALSE;
733 }
734 return TRUE;
735 }
736
737 static void parse_ipAddrBlocks(chunk_t blob, int level0,
738 private_x509_cert_t *this)
739 {
740 asn1_parser_t *parser;
741 chunk_t object, min_object;
742 ts_type_t ts_type;
743 traffic_selector_t *ts;
744 int objectID;
745
746 parser = asn1_parser_create(ipAddrBlocksObjects, blob);
747 parser->set_top_level(parser, level0);
748
749 while (parser->iterate(parser, &objectID, &object))
750 {
751 switch (objectID)
752 {
753 case IP_ADDR_BLOCKS_FAMILY:
754 ts_type = 0;
755 if (object.len == 2 && object.ptr[0] == 0)
756 {
757 if (object.ptr[1] == 1)
758 {
759 ts_type = TS_IPV4_ADDR_RANGE;
760 }
761 else if (object.ptr[1] == 2)
762 {
763 ts_type = TS_IPV6_ADDR_RANGE;
764 }
765 else
766 {
767 break;
768 }
769 DBG2(" %N", ts_type_name, ts_type);
770 }
771 break;
772 case IP_ADDR_BLOCKS_INHERIT:
773 DBG1("inherit choice is not supported");
774 break;
775 case IP_ADDR_BLOCKS_PREFIX:
776 if (!check_address_object(ts_type, object))
777 {
778 goto end;
779 }
780 ts = traffic_selector_create_from_rfc3779_format(ts_type,
781 object, object);
782 DBG2(" %R", ts);
783 this->ipAddrBlocks->insert_last(this->ipAddrBlocks, ts);
784 break;
785 case IP_ADDR_BLOCKS_MIN:
786 if (!check_address_object(ts_type, object))
787 {
788 goto end;
789 }
790 min_object = object;
791 break;
792 case IP_ADDR_BLOCKS_MAX:
793 if (!check_address_object(ts_type, object))
794 {
795 goto end;
796 }
797 ts = traffic_selector_create_from_rfc3779_format(ts_type,
798 min_object, object);
799 DBG2(" %R", ts);
800 this->ipAddrBlocks->insert_last(this->ipAddrBlocks, ts);
801 break;
802 default:
803 break;
804 }
805 }
806 this->flags |= X509_IP_ADDR_BLOCKS;
807
808 end:
809 parser->destroy(parser);
810 }
811
812 /**
813 * ASN.1 definition of an X.509v3 x509_cert
814 */
815 static const asn1Object_t certObjects[] = {
816 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
817 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
818 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
819 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
820 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
821 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
822 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
823 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
824 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
825 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
826 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
827 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
828 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
829 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
830 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
831 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
832 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
833 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
834 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
835 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
836 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
837 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
838 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
839 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
840 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
841 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
842 { 0, "exit", ASN1_EOC, ASN1_EXIT }
843 };
844 #define X509_OBJ_TBS_CERTIFICATE 1
845 #define X509_OBJ_VERSION 3
846 #define X509_OBJ_SERIAL_NUMBER 4
847 #define X509_OBJ_SIG_ALG 5
848 #define X509_OBJ_ISSUER 6
849 #define X509_OBJ_NOT_BEFORE 8
850 #define X509_OBJ_NOT_AFTER 9
851 #define X509_OBJ_SUBJECT 10
852 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
853 #define X509_OBJ_OPTIONAL_EXTENSIONS 16
854 #define X509_OBJ_EXTN_ID 19
855 #define X509_OBJ_CRITICAL 20
856 #define X509_OBJ_EXTN_VALUE 21
857 #define X509_OBJ_ALGORITHM 24
858 #define X509_OBJ_SIGNATURE 25
859
860 /**
861 * forward declaration
862 */
863 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer);
864
865 /**
866 * Parses an X.509v3 certificate
867 */
868 static bool parse_certificate(private_x509_cert_t *this)
869 {
870 asn1_parser_t *parser;
871 chunk_t object;
872 int objectID;
873 int extn_oid = OID_UNKNOWN;
874 int sig_alg = OID_UNKNOWN;
875 bool success = FALSE;
876 bool critical;
877
878 parser = asn1_parser_create(certObjects, this->encoding);
879
880 while (parser->iterate(parser, &objectID, &object))
881 {
882 u_int level = parser->get_level(parser)+1;
883
884 switch (objectID)
885 {
886 case X509_OBJ_TBS_CERTIFICATE:
887 this->tbsCertificate = object;
888 break;
889 case X509_OBJ_VERSION:
890 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
891 if (this->version < 1 || this->version > 3)
892 {
893 DBG1("X.509v%d not supported", this->version);
894 goto end;
895 }
896 else
897 {
898 DBG2(" X.509v%d", this->version);
899 }
900 break;
901 case X509_OBJ_SERIAL_NUMBER:
902 this->serialNumber = object;
903 break;
904 case X509_OBJ_SIG_ALG:
905 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
906 break;
907 case X509_OBJ_ISSUER:
908 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
909 DBG2(" '%Y'", this->issuer);
910 break;
911 case X509_OBJ_NOT_BEFORE:
912 this->notBefore = asn1_parse_time(object, level);
913 break;
914 case X509_OBJ_NOT_AFTER:
915 this->notAfter = asn1_parse_time(object, level);
916 break;
917 case X509_OBJ_SUBJECT:
918 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
919 DBG2(" '%Y'", this->subject);
920 break;
921 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
922 DBG2("-- > --");
923 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
924 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
925 DBG2("-- < --");
926 if (this->public_key == NULL)
927 {
928 goto end;
929 }
930 break;
931 case X509_OBJ_OPTIONAL_EXTENSIONS:
932 if (this->version != 3)
933 {
934 DBG1("Only X.509v3 certificates have extensions");
935 goto end;
936 }
937 break;
938 case X509_OBJ_EXTN_ID:
939 extn_oid = asn1_known_oid(object);
940 break;
941 case X509_OBJ_CRITICAL:
942 critical = object.len && *object.ptr;
943 DBG2(" %s", critical ? "TRUE" : "FALSE");
944 break;
945 case X509_OBJ_EXTN_VALUE:
946 {
947 switch (extn_oid)
948 {
949 case OID_SUBJECT_KEY_ID:
950 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
951 level, "keyIdentifier"))
952 {
953 goto end;
954 }
955 this->subjectKeyIdentifier = object;
956 break;
957 case OID_SUBJECT_ALT_NAME:
958 x509_parse_generalNames(object, level, FALSE,
959 this->subjectAltNames);
960 break;
961 case OID_BASIC_CONSTRAINTS:
962 parse_basicConstraints(object, level, this);
963 break;
964 case OID_CRL_DISTRIBUTION_POINTS:
965 parse_crlDistributionPoints(object, level, this);
966 break;
967 case OID_AUTHORITY_KEY_ID:
968 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
969 level, &this->authKeySerialNumber);
970 break;
971 case OID_AUTHORITY_INFO_ACCESS:
972 parse_authorityInfoAccess(object, level, this);
973 break;
974 case OID_EXTENDED_KEY_USAGE:
975 parse_extendedKeyUsage(object, level, this);
976 break;
977 case OID_IP_ADDR_BLOCKS:
978 parse_ipAddrBlocks(object, level, this);
979 break;
980 case OID_NS_REVOCATION_URL:
981 case OID_NS_CA_REVOCATION_URL:
982 case OID_NS_CA_POLICY_URL:
983 case OID_NS_COMMENT:
984 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING,
985 level, oid_names[extn_oid].name))
986 {
987 goto end;
988 }
989 break;
990 default:
991 if (critical && lib->settings->get_bool(lib->settings,
992 "libstrongswan.plugins.x509.enforce_critical", FALSE))
993 {
994 DBG1("critical %s extension not supported",
995 (extn_oid == OID_UNKNOWN) ? "unknown" :
996 (char*)oid_names[extn_oid].name);
997 goto end;
998 }
999 break;
1000 }
1001 break;
1002 }
1003 case X509_OBJ_ALGORITHM:
1004 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
1005 if (this->algorithm != sig_alg)
1006 {
1007 DBG1(" signature algorithms do not agree");
1008 goto end;
1009 }
1010 break;
1011 case X509_OBJ_SIGNATURE:
1012 this->signature = object;
1013 break;
1014 default:
1015 break;
1016 }
1017 }
1018 success = parser->success(parser);
1019
1020 end:
1021 parser->destroy(parser);
1022 if (success)
1023 {
1024 hasher_t *hasher;
1025
1026 /* check if the certificate is self-signed */
1027 if (issued_by(this, &this->public.interface.interface))
1028 {
1029 this->flags |= X509_SELF_SIGNED;
1030 }
1031 /* create certificate hash */
1032 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1033 if (hasher == NULL)
1034 {
1035 DBG1(" unable to create hash of certificate, SHA1 not supported");
1036 return NULL;
1037 }
1038 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
1039 hasher->destroy(hasher);
1040 }
1041 return success;
1042 }
1043
1044 /**
1045 * Implementation of certificate_t.get_type
1046 */
1047 static certificate_type_t get_type(private_x509_cert_t *this)
1048 {
1049 return CERT_X509;
1050 }
1051
1052 /**
1053 * Implementation of certificate_t.get_subject
1054 */
1055 static identification_t* get_subject(private_x509_cert_t *this)
1056 {
1057 return this->subject;
1058 }
1059
1060 /**
1061 * Implementation of certificate_t.get_issuer
1062 */
1063 static identification_t* get_issuer(private_x509_cert_t *this)
1064 {
1065 return this->issuer;
1066 }
1067
1068 /**
1069 * Implementation of certificate_t.has_subject.
1070 */
1071 static id_match_t has_subject(private_x509_cert_t *this, identification_t *subject)
1072 {
1073 identification_t *current;
1074 enumerator_t *enumerator;
1075 id_match_t match, best;
1076
1077 if (this->encoding_hash.ptr && subject->get_type(subject) == ID_KEY_ID)
1078 {
1079 if (chunk_equals(this->encoding_hash, subject->get_encoding(subject)))
1080 {
1081 return ID_MATCH_PERFECT;
1082 }
1083 }
1084
1085 best = this->subject->matches(this->subject, subject);
1086 enumerator = this->subjectAltNames->create_enumerator(this->subjectAltNames);
1087 while (enumerator->enumerate(enumerator, &current))
1088 {
1089 match = current->matches(current, subject);
1090 if (match > best)
1091 {
1092 best = match;
1093 }
1094 }
1095 enumerator->destroy(enumerator);
1096 return best;
1097 }
1098
1099 /**
1100 * Implementation of certificate_t.has_issuer.
1101 */
1102 static id_match_t has_issuer(private_x509_cert_t *this, identification_t *issuer)
1103 {
1104 /* issuerAltNames currently not supported */
1105 return this->issuer->matches(this->issuer, issuer);
1106 }
1107
1108 /**
1109 * Implementation of certificate_t.issued_by.
1110 */
1111 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer)
1112 {
1113 public_key_t *key;
1114 signature_scheme_t scheme;
1115 bool valid;
1116 x509_t *x509 = (x509_t*)issuer;
1117
1118 if (&this->public.interface.interface == issuer)
1119 {
1120 if (this->flags & X509_SELF_SIGNED)
1121 {
1122 return TRUE;
1123 }
1124 }
1125 else
1126 {
1127 if (issuer->get_type(issuer) != CERT_X509)
1128 {
1129 return FALSE;
1130 }
1131 if (!(x509->get_flags(x509) & X509_CA))
1132 {
1133 return FALSE;
1134 }
1135 }
1136 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
1137 {
1138 return FALSE;
1139 }
1140
1141 /* determine signature scheme */
1142 scheme = signature_scheme_from_oid(this->algorithm);
1143 if (scheme == SIGN_UNKNOWN)
1144 {
1145 return FALSE;
1146 }
1147 /* get the public key of the issuer */
1148 key = issuer->get_public_key(issuer);
1149 if (!key)
1150 {
1151 return FALSE;
1152 }
1153 valid = key->verify(key, scheme, this->tbsCertificate, this->signature);
1154 key->destroy(key);
1155 return valid;
1156 }
1157
1158 /**
1159 * Implementation of certificate_t.get_public_key
1160 */
1161 static public_key_t* get_public_key(private_x509_cert_t *this)
1162 {
1163 this->public_key->get_ref(this->public_key);
1164 return this->public_key;
1165 }
1166
1167 /**
1168 * Implementation of certificate_t.get_ref
1169 */
1170 static private_x509_cert_t* get_ref(private_x509_cert_t *this)
1171 {
1172 ref_get(&this->ref);
1173 return this;
1174 }
1175
1176 /**
1177 * Implementation of x509_cert_t.get_flags.
1178 */
1179 static x509_flag_t get_flags(private_x509_cert_t *this)
1180 {
1181 return this->flags;
1182 }
1183
1184 /**
1185 * Implementation of x509_cert_t.get_validity.
1186 */
1187 static bool get_validity(private_x509_cert_t *this, time_t *when,
1188 time_t *not_before, time_t *not_after)
1189 {
1190 time_t t = when ? *when : time(NULL);
1191
1192 if (not_before)
1193 {
1194 *not_before = this->notBefore;
1195 }
1196 if (not_after)
1197 {
1198 *not_after = this->notAfter;
1199 }
1200 return (t >= this->notBefore && t <= this->notAfter);
1201 }
1202
1203 /**
1204 * Implementation of certificate_t.is_newer.
1205 */
1206 static bool is_newer(certificate_t *this, certificate_t *that)
1207 {
1208 time_t this_update, that_update, now = time(NULL);
1209 bool new;
1210
1211 this->get_validity(this, &now, &this_update, NULL);
1212 that->get_validity(that, &now, &that_update, NULL);
1213 new = this_update > that_update;
1214 DBG1(" certificate from %T is %s - existing certificate from %T %s",
1215 &this_update, FALSE, new ? "newer":"not newer",
1216 &that_update, FALSE, new ? "replaced":"retained");
1217 return new;
1218 }
1219
1220 /**
1221 * Implementation of certificate_t.get_encoding.
1222 */
1223 static chunk_t get_encoding(private_x509_cert_t *this)
1224 {
1225 return chunk_clone(this->encoding);
1226 }
1227
1228 /**
1229 * Implementation of certificate_t.equals.
1230 */
1231 static bool equals(private_x509_cert_t *this, certificate_t *other)
1232 {
1233 chunk_t encoding;
1234 bool equal;
1235
1236 if (this == (private_x509_cert_t*)other)
1237 {
1238 return TRUE;
1239 }
1240 if (other->get_type(other) != CERT_X509)
1241 {
1242 return FALSE;
1243 }
1244 if (other->equals == (void*)equals)
1245 { /* skip allocation if we have the same implementation */
1246 return chunk_equals(this->encoding, ((private_x509_cert_t*)other)->encoding);
1247 }
1248 encoding = other->get_encoding(other);
1249 equal = chunk_equals(this->encoding, encoding);
1250 free(encoding.ptr);
1251 return equal;
1252 }
1253
1254 /**
1255 * Implementation of x509_t.get_serial.
1256 */
1257 static chunk_t get_serial(private_x509_cert_t *this)
1258 {
1259 return this->serialNumber;
1260 }
1261
1262 /**
1263 * Implementation of x509_t.get_subjectKeyIdentifier.
1264 */
1265 static chunk_t get_subjectKeyIdentifier(private_x509_cert_t *this)
1266 {
1267 if (this->subjectKeyIdentifier.ptr)
1268 {
1269 return this->subjectKeyIdentifier;
1270 }
1271 else
1272 {
1273 chunk_t fingerprint;
1274
1275 if (this->public_key->get_fingerprint(this->public_key,
1276 KEY_ID_PUBKEY_SHA1, &fingerprint))
1277 {
1278 return fingerprint;
1279 }
1280 else
1281 {
1282 return chunk_empty;
1283 }
1284 }
1285 }
1286
1287 /**
1288 * Implementation of x509_t.get_authKeyIdentifier.
1289 */
1290 static chunk_t get_authKeyIdentifier(private_x509_cert_t *this)
1291 {
1292 return this->authKeyIdentifier;
1293 }
1294
1295 /**
1296 * Implementation of x509_t.get_pathLenConstraint.
1297 */
1298 static int get_pathLenConstraint(private_x509_cert_t *this)
1299 {
1300 return this->pathLenConstraint;
1301 }
1302
1303 /**
1304 * Implementation of x509_cert_t.create_subjectAltName_enumerator.
1305 */
1306 static enumerator_t* create_subjectAltName_enumerator(private_x509_cert_t *this)
1307 {
1308 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
1309 }
1310
1311 /**
1312 * Implementation of x509_cert_t.create_ocsp_uri_enumerator.
1313 */
1314 static enumerator_t* create_ocsp_uri_enumerator(private_x509_cert_t *this)
1315 {
1316 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
1317 }
1318
1319 /**
1320 * Implementation of x509_cert_t.create_crl_uri_enumerator.
1321 */
1322 static enumerator_t* create_crl_uri_enumerator(private_x509_cert_t *this)
1323 {
1324 return this->crl_uris->create_enumerator(this->crl_uris);
1325 }
1326
1327 /**
1328 * Implementation of x509_cert_t.create_ipAddrBlock_enumerator.
1329 */
1330 static enumerator_t* create_ipAddrBlock_enumerator(private_x509_cert_t *this)
1331 {
1332 return this->ipAddrBlocks->create_enumerator(this->ipAddrBlocks);
1333 }
1334
1335 /**
1336 * Implementation of certificate_t.destroy.
1337 */
1338 static void destroy(private_x509_cert_t *this)
1339 {
1340 if (ref_put(&this->ref))
1341 {
1342 this->subjectAltNames->destroy_offset(this->subjectAltNames,
1343 offsetof(identification_t, destroy));
1344 this->crl_uris->destroy_function(this->crl_uris, free);
1345 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
1346 this->ipAddrBlocks->destroy_offset(this->ipAddrBlocks, offsetof(traffic_selector_t, destroy));
1347 DESTROY_IF(this->issuer);
1348 DESTROY_IF(this->subject);
1349 DESTROY_IF(this->public_key);
1350 chunk_free(&this->authKeyIdentifier);
1351 chunk_free(&this->encoding);
1352 chunk_free(&this->encoding_hash);
1353 if (!this->parsed)
1354 { /* only parsed certificates point these fields to "encoded" */
1355 chunk_free(&this->signature);
1356 chunk_free(&this->serialNumber);
1357 chunk_free(&this->tbsCertificate);
1358 }
1359 free(this);
1360 }
1361 }
1362
1363 /**
1364 * create an empty but initialized X.509 certificate
1365 */
1366 static private_x509_cert_t* create_empty(void)
1367 {
1368 private_x509_cert_t *this = malloc_thing(private_x509_cert_t);
1369
1370 this->public.interface.interface.get_type = (certificate_type_t (*) (certificate_t*))get_type;
1371 this->public.interface.interface.get_subject = (identification_t* (*) (certificate_t*))get_subject;
1372 this->public.interface.interface.get_issuer = (identification_t* (*) (certificate_t*))get_issuer;
1373 this->public.interface.interface.has_subject = (id_match_t (*) (certificate_t*, identification_t*))has_subject;
1374 this->public.interface.interface.has_issuer = (id_match_t (*) (certificate_t*, identification_t*))has_issuer;
1375 this->public.interface.interface.issued_by = (bool (*) (certificate_t*, certificate_t*))issued_by;
1376 this->public.interface.interface.get_public_key = (public_key_t* (*) (certificate_t*))get_public_key;
1377 this->public.interface.interface.get_validity = (bool (*) (certificate_t*, time_t*, time_t*, time_t*))get_validity;
1378 this->public.interface.interface.is_newer = (bool (*) (certificate_t*,certificate_t*))is_newer;
1379 this->public.interface.interface.get_encoding = (chunk_t (*) (certificate_t*))get_encoding;
1380 this->public.interface.interface.equals = (bool (*)(certificate_t*, certificate_t*))equals;
1381 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t*))get_ref;
1382 this->public.interface.interface.destroy = (void (*)(certificate_t*))destroy;
1383 this->public.interface.get_flags = (x509_flag_t (*)(x509_t*))get_flags;
1384 this->public.interface.get_serial = (chunk_t (*)(x509_t*))get_serial;
1385 this->public.interface.get_subjectKeyIdentifier = (chunk_t (*)(x509_t*))get_subjectKeyIdentifier;
1386 this->public.interface.get_authKeyIdentifier = (chunk_t (*)(x509_t*))get_authKeyIdentifier;
1387 this->public.interface.get_pathLenConstraint = (int (*)(x509_t*))get_pathLenConstraint;
1388 this->public.interface.create_subjectAltName_enumerator = (enumerator_t* (*)(x509_t*))create_subjectAltName_enumerator;
1389 this->public.interface.create_crl_uri_enumerator = (enumerator_t* (*)(x509_t*))create_crl_uri_enumerator;
1390 this->public.interface.create_ocsp_uri_enumerator = (enumerator_t* (*)(x509_t*))create_ocsp_uri_enumerator;
1391 this->public.interface.create_ipAddrBlock_enumerator = (enumerator_t* (*)(x509_t*))create_ipAddrBlock_enumerator;
1392
1393 this->encoding = chunk_empty;
1394 this->encoding_hash = chunk_empty;
1395 this->tbsCertificate = chunk_empty;
1396 this->version = 1;
1397 this->serialNumber = chunk_empty;
1398 this->notBefore = 0;
1399 this->notAfter = 0;
1400 this->public_key = NULL;
1401 this->subject = NULL;
1402 this->issuer = NULL;
1403 this->subjectAltNames = linked_list_create();
1404 this->crl_uris = linked_list_create();
1405 this->ocsp_uris = linked_list_create();
1406 this->ipAddrBlocks = linked_list_create();
1407 this->subjectKeyIdentifier = chunk_empty;
1408 this->authKeyIdentifier = chunk_empty;
1409 this->authKeySerialNumber = chunk_empty;
1410 this->pathLenConstraint = X509_NO_PATH_LEN_CONSTRAINT;
1411 this->algorithm = 0;
1412 this->signature = chunk_empty;
1413 this->flags = 0;
1414 this->ref = 1;
1415 this->parsed = FALSE;
1416
1417 return this;
1418 }
1419
1420 /**
1421 * Encode a linked list of subjectAltNames
1422 */
1423 chunk_t x509_build_subjectAltNames(linked_list_t *list)
1424 {
1425 chunk_t subjectAltNames = chunk_empty;
1426 enumerator_t *enumerator;
1427 identification_t *id;
1428
1429 if (list->get_count(list) == 0)
1430 {
1431 return chunk_empty;
1432 }
1433
1434 enumerator = list->create_enumerator(list);
1435 while (enumerator->enumerate(enumerator, &id))
1436 {
1437 int context;
1438 chunk_t name;
1439
1440 switch (id->get_type(id))
1441 {
1442 case ID_RFC822_ADDR:
1443 context = ASN1_CONTEXT_S_1;
1444 break;
1445 case ID_FQDN:
1446 context = ASN1_CONTEXT_S_2;
1447 break;
1448 case ID_IPV4_ADDR:
1449 case ID_IPV6_ADDR:
1450 context = ASN1_CONTEXT_S_7;
1451 break;
1452 default:
1453 DBG1("encoding %N as subjectAltName not supported",
1454 id_type_names, id->get_type(id));
1455 enumerator->destroy(enumerator);
1456 free(subjectAltNames.ptr);
1457 return chunk_empty;
1458 }
1459 name = asn1_wrap(context, "c", id->get_encoding(id));
1460 subjectAltNames = chunk_cat("mm", subjectAltNames, name);
1461 }
1462 enumerator->destroy(enumerator);
1463
1464 return asn1_wrap(ASN1_SEQUENCE, "mm",
1465 asn1_build_known_oid(OID_SUBJECT_ALT_NAME),
1466 asn1_wrap(ASN1_OCTET_STRING, "m",
1467 asn1_wrap(ASN1_SEQUENCE, "m", subjectAltNames)
1468 )
1469 );
1470 }
1471
1472 /**
1473 * Generate and sign a new certificate
1474 */
1475 static bool generate(private_x509_cert_t *cert, certificate_t *sign_cert,
1476 private_key_t *sign_key, int digest_alg)
1477 {
1478 chunk_t extensions = chunk_empty, extendedKeyUsage = chunk_empty;
1479 chunk_t serverAuth = chunk_empty, ocspSigning = chunk_empty;
1480 chunk_t basicConstraints = chunk_empty, subjectAltNames = chunk_empty;
1481 chunk_t subjectKeyIdentifier = chunk_empty, authKeyIdentifier = chunk_empty;
1482 chunk_t crlDistributionPoints = chunk_empty, authorityInfoAccess = chunk_empty;
1483 identification_t *issuer, *subject;
1484 chunk_t key_info;
1485 signature_scheme_t scheme;
1486 hasher_t *hasher;
1487 enumerator_t *enumerator;
1488 char *uri;
1489
1490 subject = cert->subject;
1491 if (sign_cert)
1492 {
1493 issuer = sign_cert->get_subject(sign_cert);
1494 if (!cert->public_key)
1495 {
1496 return FALSE;
1497 }
1498 }
1499 else
1500 { /* self signed */
1501 issuer = subject;
1502 if (!cert->public_key)
1503 {
1504 cert->public_key = sign_key->get_public_key(sign_key);
1505 }
1506 cert->flags |= X509_SELF_SIGNED;
1507 }
1508 cert->issuer = issuer->clone(issuer);
1509 if (!cert->notBefore)
1510 {
1511 cert->notBefore = time(NULL);
1512 }
1513 if (!cert->notAfter)
1514 { /* defaults to 1 year from now */
1515 cert->notAfter = cert->notBefore + 60 * 60 * 24 * 365;
1516 }
1517
1518 /* select signature scheme */
1519 cert->algorithm = hasher_signature_algorithm_to_oid(digest_alg,
1520 sign_key->get_type(sign_key));
1521 if (cert->algorithm == OID_UNKNOWN)
1522 {
1523 return FALSE;
1524 }
1525 scheme = signature_scheme_from_oid(cert->algorithm);
1526
1527 if (!cert->public_key->get_encoding(cert->public_key,
1528 KEY_PUB_SPKI_ASN1_DER, &key_info))
1529 {
1530 return FALSE;
1531 }
1532
1533 /* encode subjectAltNames */
1534 subjectAltNames = x509_build_subjectAltNames(cert->subjectAltNames);
1535
1536 /* encode CRL distribution points extension */
1537 enumerator = cert->crl_uris->create_enumerator(cert->crl_uris);
1538 while (enumerator->enumerate(enumerator, &uri))
1539 {
1540 chunk_t distributionPoint;
1541
1542 distributionPoint = asn1_wrap(ASN1_SEQUENCE, "m",
1543 asn1_wrap(ASN1_CONTEXT_C_0, "m",
1544 asn1_wrap(ASN1_CONTEXT_C_0, "m",
1545 asn1_wrap(ASN1_CONTEXT_S_6, "c",
1546 chunk_create(uri, strlen(uri))))));
1547
1548 crlDistributionPoints = chunk_cat("mm", crlDistributionPoints,
1549 distributionPoint);
1550 }
1551 enumerator->destroy(enumerator);
1552 if (crlDistributionPoints.ptr)
1553 {
1554 crlDistributionPoints = asn1_wrap(ASN1_SEQUENCE, "mm",
1555 asn1_build_known_oid(OID_CRL_DISTRIBUTION_POINTS),
1556 asn1_wrap(ASN1_OCTET_STRING, "m",
1557 asn1_wrap(ASN1_SEQUENCE, "m", crlDistributionPoints)));
1558 }
1559
1560 /* encode OCSP URIs in authorityInfoAccess extension */
1561 enumerator = cert->ocsp_uris->create_enumerator(cert->ocsp_uris);
1562 while (enumerator->enumerate(enumerator, &uri))
1563 {
1564 chunk_t accessDescription;
1565
1566 accessDescription = asn1_wrap(ASN1_SEQUENCE, "mm",
1567 asn1_build_known_oid(OID_OCSP),
1568 asn1_wrap(ASN1_CONTEXT_S_6, "c",
1569 chunk_create(uri, strlen(uri))));
1570 authorityInfoAccess = chunk_cat("mm", authorityInfoAccess,
1571 accessDescription);
1572 }
1573 enumerator->destroy(enumerator);
1574 if (authorityInfoAccess.ptr)
1575 {
1576 authorityInfoAccess = asn1_wrap(ASN1_SEQUENCE, "mm",
1577 asn1_build_known_oid(OID_AUTHORITY_INFO_ACCESS),
1578 asn1_wrap(ASN1_OCTET_STRING, "m",
1579 asn1_wrap(ASN1_SEQUENCE, "m", authorityInfoAccess)));
1580 }
1581
1582 /* build CA basicConstraint for CA certificates */
1583 if (cert->flags & X509_CA)
1584 {
1585 chunk_t pathLenConstraint = chunk_empty;
1586
1587 if (cert->pathLenConstraint != X509_NO_PATH_LEN_CONSTRAINT)
1588 {
1589 char pathlen = (char)cert->pathLenConstraint;
1590
1591 pathLenConstraint = asn1_integer("c", chunk_from_thing(pathlen));
1592 }
1593 basicConstraints = asn1_wrap(ASN1_SEQUENCE, "mmm",
1594 asn1_build_known_oid(OID_BASIC_CONSTRAINTS),
1595 asn1_wrap(ASN1_BOOLEAN, "c",
1596 chunk_from_chars(0xFF)),
1597 asn1_wrap(ASN1_OCTET_STRING, "m",
1598 asn1_wrap(ASN1_SEQUENCE, "mm",
1599 asn1_wrap(ASN1_BOOLEAN, "c",
1600 chunk_from_chars(0xFF)),
1601 pathLenConstraint)));
1602 }
1603
1604 /* add serverAuth extendedKeyUsage flag */
1605 if (cert->flags & X509_SERVER_AUTH)
1606 {
1607 serverAuth = asn1_build_known_oid(OID_SERVER_AUTH);
1608 }
1609
1610 /* add ocspSigning extendedKeyUsage flag */
1611 if (cert->flags & X509_OCSP_SIGNER)
1612 {
1613 ocspSigning = asn1_build_known_oid(OID_OCSP_SIGNING);
1614 }
1615
1616 if (serverAuth.ptr || ocspSigning.ptr)
1617 {
1618 extendedKeyUsage = asn1_wrap(ASN1_SEQUENCE, "mm",
1619 asn1_build_known_oid(OID_EXTENDED_KEY_USAGE),
1620 asn1_wrap(ASN1_OCTET_STRING, "m",
1621 asn1_wrap(ASN1_SEQUENCE, "mm",
1622 serverAuth, ocspSigning)));
1623 }
1624
1625 /* add subjectKeyIdentifier to CA and OCSP signer certificates */
1626 if (cert->flags & (X509_CA | X509_OCSP_SIGNER))
1627 {
1628 chunk_t keyid;
1629
1630 if (cert->public_key->get_fingerprint(cert->public_key,
1631 KEY_ID_PUBKEY_SHA1, &keyid))
1632 {
1633 subjectKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1634 asn1_build_known_oid(OID_SUBJECT_KEY_ID),
1635 asn1_wrap(ASN1_OCTET_STRING, "m",
1636 asn1_wrap(ASN1_OCTET_STRING, "c", keyid)));
1637 }
1638 }
1639
1640 /* add the keyid authKeyIdentifier for non self-signed certificates */
1641 if (sign_key)
1642 {
1643 chunk_t keyid;
1644
1645 if (sign_key->get_fingerprint(sign_key, KEY_ID_PUBKEY_SHA1, &keyid))
1646 {
1647 authKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1648 asn1_build_known_oid(OID_AUTHORITY_KEY_ID),
1649 asn1_wrap(ASN1_OCTET_STRING, "m",
1650 asn1_wrap(ASN1_SEQUENCE, "m",
1651 asn1_wrap(ASN1_CONTEXT_S_0, "c", keyid))));
1652 }
1653 }
1654 if (basicConstraints.ptr || subjectAltNames.ptr || authKeyIdentifier.ptr ||
1655 crlDistributionPoints.ptr)
1656 {
1657 extensions = asn1_wrap(ASN1_CONTEXT_C_3, "m",
1658 asn1_wrap(ASN1_SEQUENCE, "mmmmmmm",
1659 basicConstraints, subjectKeyIdentifier,
1660 authKeyIdentifier, subjectAltNames,
1661 extendedKeyUsage, crlDistributionPoints,
1662 authorityInfoAccess));
1663 }
1664
1665 cert->tbsCertificate = asn1_wrap(ASN1_SEQUENCE, "mmmcmcmm",
1666 asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2),
1667 asn1_integer("c", cert->serialNumber),
1668 asn1_algorithmIdentifier(cert->algorithm),
1669 issuer->get_encoding(issuer),
1670 asn1_wrap(ASN1_SEQUENCE, "mm",
1671 asn1_from_time(&cert->notBefore, ASN1_UTCTIME),
1672 asn1_from_time(&cert->notAfter, ASN1_UTCTIME)),
1673 subject->get_encoding(subject),
1674 key_info, extensions);
1675
1676 if (!sign_key->sign(sign_key, scheme, cert->tbsCertificate, &cert->signature))
1677 {
1678 return FALSE;
1679 }
1680 cert->encoding = asn1_wrap(ASN1_SEQUENCE, "cmm", cert->tbsCertificate,
1681 asn1_algorithmIdentifier(cert->algorithm),
1682 asn1_bitstring("c", cert->signature));
1683
1684 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1685 if (!hasher)
1686 {
1687 return FALSE;
1688 }
1689 hasher->allocate_hash(hasher, cert->encoding, &cert->encoding_hash);
1690 hasher->destroy(hasher);
1691 return TRUE;
1692 }
1693
1694 /**
1695 * See header.
1696 */
1697 x509_cert_t *x509_cert_load(certificate_type_t type, va_list args)
1698 {
1699 x509_flag_t flags = 0;
1700 chunk_t blob = chunk_empty;
1701
1702 while (TRUE)
1703 {
1704 switch (va_arg(args, builder_part_t))
1705 {
1706 case BUILD_BLOB_ASN1_DER:
1707 blob = va_arg(args, chunk_t);
1708 continue;
1709 case BUILD_X509_FLAG:
1710 flags |= va_arg(args, x509_flag_t);
1711 continue;
1712 case BUILD_END:
1713 break;
1714 default:
1715 return NULL;
1716 }
1717 break;
1718 }
1719
1720 if (blob.ptr)
1721 {
1722 private_x509_cert_t *cert = create_empty();
1723
1724 cert->encoding = chunk_clone(blob);
1725 cert->parsed = TRUE;
1726 if (parse_certificate(cert))
1727 {
1728 cert->flags |= flags;
1729 return &cert->public;
1730 }
1731 destroy(cert);
1732 }
1733 return NULL;
1734 }
1735
1736 /**
1737 * See header.
1738 */
1739 x509_cert_t *x509_cert_gen(certificate_type_t type, va_list args)
1740 {
1741 private_x509_cert_t *cert;
1742 certificate_t *sign_cert = NULL;
1743 private_key_t *sign_key = NULL;
1744 hash_algorithm_t digest_alg = HASH_SHA1;
1745
1746 cert = create_empty();
1747 while (TRUE)
1748 {
1749 switch (va_arg(args, builder_part_t))
1750 {
1751 case BUILD_X509_FLAG:
1752 cert->flags |= va_arg(args, x509_flag_t);
1753 continue;
1754 case BUILD_SIGNING_KEY:
1755 sign_key = va_arg(args, private_key_t*);
1756 continue;
1757 case BUILD_SIGNING_CERT:
1758 sign_cert = va_arg(args, certificate_t*);
1759 continue;
1760 case BUILD_PUBLIC_KEY:
1761 cert->public_key = va_arg(args, public_key_t*);
1762 cert->public_key->get_ref(cert->public_key);
1763 continue;
1764 case BUILD_SUBJECT:
1765 cert->subject = va_arg(args, identification_t*);
1766 cert->subject = cert->subject->clone(cert->subject);
1767 continue;
1768 case BUILD_SUBJECT_ALTNAMES:
1769 {
1770 enumerator_t *enumerator;
1771 identification_t *id;
1772 linked_list_t *list;
1773
1774 list = va_arg(args, linked_list_t*);
1775 enumerator = list->create_enumerator(list);
1776 while (enumerator->enumerate(enumerator, &id))
1777 {
1778 cert->subjectAltNames->insert_last(cert->subjectAltNames,
1779 id->clone(id));
1780 }
1781 enumerator->destroy(enumerator);
1782 continue;
1783 }
1784 case BUILD_CRL_DISTRIBUTION_POINTS:
1785 {
1786 enumerator_t *enumerator;
1787 linked_list_t *list;
1788 char *uri;
1789
1790 list = va_arg(args, linked_list_t*);
1791 enumerator = list->create_enumerator(list);
1792 while (enumerator->enumerate(enumerator, &uri))
1793 {
1794 cert->crl_uris->insert_last(cert->crl_uris, strdup(uri));
1795 }
1796 enumerator->destroy(enumerator);
1797 continue;
1798 }
1799 case BUILD_OCSP_ACCESS_LOCATIONS:
1800 {
1801 enumerator_t *enumerator;
1802 linked_list_t *list;
1803 char *uri;
1804
1805 list = va_arg(args, linked_list_t*);
1806 enumerator = list->create_enumerator(list);
1807 while (enumerator->enumerate(enumerator, &uri))
1808 {
1809 cert->ocsp_uris->insert_last(cert->ocsp_uris, strdup(uri));
1810 }
1811 enumerator->destroy(enumerator);
1812 continue;
1813 }
1814 case BUILD_PATHLEN:
1815 cert->pathLenConstraint = va_arg(args, int);
1816 if (cert->pathLenConstraint < 0 || cert->pathLenConstraint > 127)
1817 {
1818 cert->pathLenConstraint = X509_NO_PATH_LEN_CONSTRAINT;
1819 }
1820 continue;
1821 case BUILD_NOT_BEFORE_TIME:
1822 cert->notBefore = va_arg(args, time_t);
1823 continue;
1824 case BUILD_NOT_AFTER_TIME:
1825 cert->notAfter = va_arg(args, time_t);
1826 continue;
1827 case BUILD_SERIAL:
1828 cert->serialNumber = chunk_clone(va_arg(args, chunk_t));
1829 continue;
1830 case BUILD_DIGEST_ALG:
1831 digest_alg = va_arg(args, int);
1832 continue;
1833 case BUILD_END:
1834 break;
1835 default:
1836 destroy(cert);
1837 return NULL;
1838 }
1839 break;
1840 }
1841
1842 if (sign_key && generate(cert, sign_cert, sign_key, digest_alg))
1843 {
1844 return &cert->public;
1845 }
1846 destroy(cert);
1847 return NULL;
1848 }
1849