plugin name is x509
[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 ???
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_OBJ }, /* 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 void parse_ipAddrBlocks(chunk_t blob, int level0,
698 private_x509_cert_t *this)
699 {
700 asn1_parser_t *parser;
701 chunk_t object;
702 int objectID;
703
704 parser = asn1_parser_create(ipAddrBlocksObjects, blob);
705 parser->set_top_level(parser, level0);
706
707 while (parser->iterate(parser, &objectID, &object))
708 {
709 switch (objectID)
710 {
711 case IP_ADDR_BLOCKS_FAMILY:
712 break;
713 case IP_ADDR_BLOCKS_INHERIT:
714 DBG1("inherit choice is not supported");
715 break;
716 case IP_ADDR_BLOCKS_PREFIX:
717 break;
718 case IP_ADDR_BLOCKS_MIN:
719 break;
720 case IP_ADDR_BLOCKS_MAX:
721 break;
722 default:
723 break;
724 }
725 }
726 parser->destroy(parser);
727 }
728
729 /**
730 * ASN.1 definition of an X.509v3 x509_cert
731 */
732 static const asn1Object_t certObjects[] = {
733 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
734 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
735 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
736 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
737 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
738 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
739 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
740 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
741 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
742 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
743 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
744 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
745 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
746 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
747 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
748 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
749 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
750 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
751 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
752 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
753 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
754 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
755 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
756 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
757 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
758 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
759 { 0, "exit", ASN1_EOC, ASN1_EXIT }
760 };
761 #define X509_OBJ_TBS_CERTIFICATE 1
762 #define X509_OBJ_VERSION 3
763 #define X509_OBJ_SERIAL_NUMBER 4
764 #define X509_OBJ_SIG_ALG 5
765 #define X509_OBJ_ISSUER 6
766 #define X509_OBJ_NOT_BEFORE 8
767 #define X509_OBJ_NOT_AFTER 9
768 #define X509_OBJ_SUBJECT 10
769 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
770 #define X509_OBJ_OPTIONAL_EXTENSIONS 16
771 #define X509_OBJ_EXTN_ID 19
772 #define X509_OBJ_CRITICAL 20
773 #define X509_OBJ_EXTN_VALUE 21
774 #define X509_OBJ_ALGORITHM 24
775 #define X509_OBJ_SIGNATURE 25
776
777 /**
778 * forward declaration
779 */
780 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer);
781
782 /**
783 * Parses an X.509v3 certificate
784 */
785 static bool parse_certificate(private_x509_cert_t *this)
786 {
787 asn1_parser_t *parser;
788 chunk_t object;
789 int objectID;
790 int extn_oid = OID_UNKNOWN;
791 int sig_alg = OID_UNKNOWN;
792 bool success = FALSE;
793 bool critical;
794
795 parser = asn1_parser_create(certObjects, this->encoding);
796
797 while (parser->iterate(parser, &objectID, &object))
798 {
799 u_int level = parser->get_level(parser)+1;
800
801 switch (objectID)
802 {
803 case X509_OBJ_TBS_CERTIFICATE:
804 this->tbsCertificate = object;
805 break;
806 case X509_OBJ_VERSION:
807 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
808 if (this->version < 1 || this->version > 3)
809 {
810 DBG1("X.509v%d not supported", this->version);
811 goto end;
812 }
813 else
814 {
815 DBG2(" X.509v%d", this->version);
816 }
817 break;
818 case X509_OBJ_SERIAL_NUMBER:
819 this->serialNumber = object;
820 break;
821 case X509_OBJ_SIG_ALG:
822 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
823 break;
824 case X509_OBJ_ISSUER:
825 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
826 DBG2(" '%Y'", this->issuer);
827 break;
828 case X509_OBJ_NOT_BEFORE:
829 this->notBefore = asn1_parse_time(object, level);
830 break;
831 case X509_OBJ_NOT_AFTER:
832 this->notAfter = asn1_parse_time(object, level);
833 break;
834 case X509_OBJ_SUBJECT:
835 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
836 DBG2(" '%Y'", this->subject);
837 break;
838 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
839 DBG2("-- > --");
840 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
841 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
842 DBG2("-- < --");
843 if (this->public_key == NULL)
844 {
845 goto end;
846 }
847 break;
848 case X509_OBJ_OPTIONAL_EXTENSIONS:
849 if (this->version != 3)
850 {
851 DBG1("Only X.509v3 certificates have extensions");
852 goto end;
853 }
854 break;
855 case X509_OBJ_EXTN_ID:
856 extn_oid = asn1_known_oid(object);
857 break;
858 case X509_OBJ_CRITICAL:
859 critical = object.len && *object.ptr;
860 DBG2(" %s", critical ? "TRUE" : "FALSE");
861 break;
862 case X509_OBJ_EXTN_VALUE:
863 {
864 switch (extn_oid)
865 {
866 case OID_SUBJECT_KEY_ID:
867 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
868 level, "keyIdentifier"))
869 {
870 goto end;
871 }
872 this->subjectKeyIdentifier = object;
873 break;
874 case OID_SUBJECT_ALT_NAME:
875 x509_parse_generalNames(object, level, FALSE,
876 this->subjectAltNames);
877 break;
878 case OID_BASIC_CONSTRAINTS:
879 parse_basicConstraints(object, level, this);
880 break;
881 case OID_CRL_DISTRIBUTION_POINTS:
882 parse_crlDistributionPoints(object, level, this);
883 break;
884 case OID_AUTHORITY_KEY_ID:
885 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
886 level, &this->authKeySerialNumber);
887 break;
888 case OID_AUTHORITY_INFO_ACCESS:
889 parse_authorityInfoAccess(object, level, this);
890 break;
891 case OID_EXTENDED_KEY_USAGE:
892 parse_extendedKeyUsage(object, level, this);
893 break;
894 case OID_IP_ADDR_BLOCKS:
895 parse_ipAddrBlocks(object, level, this);
896 break;
897 case OID_NS_REVOCATION_URL:
898 case OID_NS_CA_REVOCATION_URL:
899 case OID_NS_CA_POLICY_URL:
900 case OID_NS_COMMENT:
901 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING,
902 level, oid_names[extn_oid].name))
903 {
904 goto end;
905 }
906 break;
907 default:
908 if (critical && lib->settings->get_bool(lib->settings,
909 "libstrongswan.plugins.x509.enforce_critical", FALSE))
910 {
911 DBG1("critical %s extension not supported",
912 (extn_oid == OID_UNKNOWN) ? "unknown" :
913 (char*)oid_names[extn_oid].name);
914 goto end;
915 }
916 break;
917 }
918 break;
919 }
920 case X509_OBJ_ALGORITHM:
921 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
922 if (this->algorithm != sig_alg)
923 {
924 DBG1(" signature algorithms do not agree");
925 goto end;
926 }
927 break;
928 case X509_OBJ_SIGNATURE:
929 this->signature = object;
930 break;
931 default:
932 break;
933 }
934 }
935 success = parser->success(parser);
936
937 end:
938 parser->destroy(parser);
939 if (success)
940 {
941 hasher_t *hasher;
942
943 /* check if the certificate is self-signed */
944 if (issued_by(this, &this->public.interface.interface))
945 {
946 this->flags |= X509_SELF_SIGNED;
947 }
948 /* create certificate hash */
949 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
950 if (hasher == NULL)
951 {
952 DBG1(" unable to create hash of certificate, SHA1 not supported");
953 return NULL;
954 }
955 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
956 hasher->destroy(hasher);
957 }
958 return success;
959 }
960
961 /**
962 * Implementation of certificate_t.get_type
963 */
964 static certificate_type_t get_type(private_x509_cert_t *this)
965 {
966 return CERT_X509;
967 }
968
969 /**
970 * Implementation of certificate_t.get_subject
971 */
972 static identification_t* get_subject(private_x509_cert_t *this)
973 {
974 return this->subject;
975 }
976
977 /**
978 * Implementation of certificate_t.get_issuer
979 */
980 static identification_t* get_issuer(private_x509_cert_t *this)
981 {
982 return this->issuer;
983 }
984
985 /**
986 * Implementation of certificate_t.has_subject.
987 */
988 static id_match_t has_subject(private_x509_cert_t *this, identification_t *subject)
989 {
990 identification_t *current;
991 enumerator_t *enumerator;
992 id_match_t match, best;
993
994 if (this->encoding_hash.ptr && subject->get_type(subject) == ID_KEY_ID)
995 {
996 if (chunk_equals(this->encoding_hash, subject->get_encoding(subject)))
997 {
998 return ID_MATCH_PERFECT;
999 }
1000 }
1001
1002 best = this->subject->matches(this->subject, subject);
1003 enumerator = this->subjectAltNames->create_enumerator(this->subjectAltNames);
1004 while (enumerator->enumerate(enumerator, &current))
1005 {
1006 match = current->matches(current, subject);
1007 if (match > best)
1008 {
1009 best = match;
1010 }
1011 }
1012 enumerator->destroy(enumerator);
1013 return best;
1014 }
1015
1016 /**
1017 * Implementation of certificate_t.has_issuer.
1018 */
1019 static id_match_t has_issuer(private_x509_cert_t *this, identification_t *issuer)
1020 {
1021 /* issuerAltNames currently not supported */
1022 return this->issuer->matches(this->issuer, issuer);
1023 }
1024
1025 /**
1026 * Implementation of certificate_t.issued_by.
1027 */
1028 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer)
1029 {
1030 public_key_t *key;
1031 signature_scheme_t scheme;
1032 bool valid;
1033 x509_t *x509 = (x509_t*)issuer;
1034
1035 if (&this->public.interface.interface == issuer)
1036 {
1037 if (this->flags & X509_SELF_SIGNED)
1038 {
1039 return TRUE;
1040 }
1041 }
1042 else
1043 {
1044 if (issuer->get_type(issuer) != CERT_X509)
1045 {
1046 return FALSE;
1047 }
1048 if (!(x509->get_flags(x509) & X509_CA))
1049 {
1050 return FALSE;
1051 }
1052 }
1053 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
1054 {
1055 return FALSE;
1056 }
1057
1058 /* determine signature scheme */
1059 scheme = signature_scheme_from_oid(this->algorithm);
1060 if (scheme == SIGN_UNKNOWN)
1061 {
1062 return FALSE;
1063 }
1064 /* get the public key of the issuer */
1065 key = issuer->get_public_key(issuer);
1066 if (!key)
1067 {
1068 return FALSE;
1069 }
1070 valid = key->verify(key, scheme, this->tbsCertificate, this->signature);
1071 key->destroy(key);
1072 return valid;
1073 }
1074
1075 /**
1076 * Implementation of certificate_t.get_public_key
1077 */
1078 static public_key_t* get_public_key(private_x509_cert_t *this)
1079 {
1080 this->public_key->get_ref(this->public_key);
1081 return this->public_key;
1082 }
1083
1084 /**
1085 * Implementation of certificate_t.get_ref
1086 */
1087 static private_x509_cert_t* get_ref(private_x509_cert_t *this)
1088 {
1089 ref_get(&this->ref);
1090 return this;
1091 }
1092
1093 /**
1094 * Implementation of x509_cert_t.get_flags.
1095 */
1096 static x509_flag_t get_flags(private_x509_cert_t *this)
1097 {
1098 return this->flags;
1099 }
1100
1101 /**
1102 * Implementation of x509_cert_t.get_validity.
1103 */
1104 static bool get_validity(private_x509_cert_t *this, time_t *when,
1105 time_t *not_before, time_t *not_after)
1106 {
1107 time_t t = when ? *when : time(NULL);
1108
1109 if (not_before)
1110 {
1111 *not_before = this->notBefore;
1112 }
1113 if (not_after)
1114 {
1115 *not_after = this->notAfter;
1116 }
1117 return (t >= this->notBefore && t <= this->notAfter);
1118 }
1119
1120 /**
1121 * Implementation of certificate_t.is_newer.
1122 */
1123 static bool is_newer(certificate_t *this, certificate_t *that)
1124 {
1125 time_t this_update, that_update, now = time(NULL);
1126 bool new;
1127
1128 this->get_validity(this, &now, &this_update, NULL);
1129 that->get_validity(that, &now, &that_update, NULL);
1130 new = this_update > that_update;
1131 DBG1(" certificate from %T is %s - existing certificate from %T %s",
1132 &this_update, FALSE, new ? "newer":"not newer",
1133 &that_update, FALSE, new ? "replaced":"retained");
1134 return new;
1135 }
1136
1137 /**
1138 * Implementation of certificate_t.get_encoding.
1139 */
1140 static chunk_t get_encoding(private_x509_cert_t *this)
1141 {
1142 return chunk_clone(this->encoding);
1143 }
1144
1145 /**
1146 * Implementation of certificate_t.equals.
1147 */
1148 static bool equals(private_x509_cert_t *this, certificate_t *other)
1149 {
1150 chunk_t encoding;
1151 bool equal;
1152
1153 if (this == (private_x509_cert_t*)other)
1154 {
1155 return TRUE;
1156 }
1157 if (other->get_type(other) != CERT_X509)
1158 {
1159 return FALSE;
1160 }
1161 if (other->equals == (void*)equals)
1162 { /* skip allocation if we have the same implementation */
1163 return chunk_equals(this->encoding, ((private_x509_cert_t*)other)->encoding);
1164 }
1165 encoding = other->get_encoding(other);
1166 equal = chunk_equals(this->encoding, encoding);
1167 free(encoding.ptr);
1168 return equal;
1169 }
1170
1171 /**
1172 * Implementation of x509_t.get_serial.
1173 */
1174 static chunk_t get_serial(private_x509_cert_t *this)
1175 {
1176 return this->serialNumber;
1177 }
1178
1179 /**
1180 * Implementation of x509_t.get_subjectKeyIdentifier.
1181 */
1182 static chunk_t get_subjectKeyIdentifier(private_x509_cert_t *this)
1183 {
1184 if (this->subjectKeyIdentifier.ptr)
1185 {
1186 return this->subjectKeyIdentifier;
1187 }
1188 else
1189 {
1190 chunk_t fingerprint;
1191
1192 if (this->public_key->get_fingerprint(this->public_key,
1193 KEY_ID_PUBKEY_SHA1, &fingerprint))
1194 {
1195 return fingerprint;
1196 }
1197 else
1198 {
1199 return chunk_empty;
1200 }
1201 }
1202 }
1203
1204 /**
1205 * Implementation of x509_t.get_authKeyIdentifier.
1206 */
1207 static chunk_t get_authKeyIdentifier(private_x509_cert_t *this)
1208 {
1209 return this->authKeyIdentifier;
1210 }
1211
1212 /**
1213 * Implementation of x509_t.get_pathLenConstraint.
1214 */
1215 static int get_pathLenConstraint(private_x509_cert_t *this)
1216 {
1217 return this->pathLenConstraint;
1218 }
1219
1220 /**
1221 * Implementation of x509_cert_t.create_subjectAltName_enumerator.
1222 */
1223 static enumerator_t* create_subjectAltName_enumerator(private_x509_cert_t *this)
1224 {
1225 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
1226 }
1227
1228 /**
1229 * Implementation of x509_cert_t.create_ocsp_uri_enumerator.
1230 */
1231 static enumerator_t* create_ocsp_uri_enumerator(private_x509_cert_t *this)
1232 {
1233 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
1234 }
1235
1236 /**
1237 * Implementation of x509_cert_t.create_crl_uri_enumerator.
1238 */
1239 static enumerator_t* create_crl_uri_enumerator(private_x509_cert_t *this)
1240 {
1241 return this->crl_uris->create_enumerator(this->crl_uris);
1242 }
1243
1244 /**
1245 * Implementation of certificate_t.destroy.
1246 */
1247 static void destroy(private_x509_cert_t *this)
1248 {
1249 if (ref_put(&this->ref))
1250 {
1251 this->subjectAltNames->destroy_offset(this->subjectAltNames,
1252 offsetof(identification_t, destroy));
1253 this->crl_uris->destroy_function(this->crl_uris, free);
1254 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
1255 this->ipAddrBlocks->destroy_offset(this->ipAddrBlocks, offsetof(traffic_selector_t, destroy));
1256 DESTROY_IF(this->issuer);
1257 DESTROY_IF(this->subject);
1258 DESTROY_IF(this->public_key);
1259 chunk_free(&this->authKeyIdentifier);
1260 chunk_free(&this->encoding);
1261 chunk_free(&this->encoding_hash);
1262 if (!this->parsed)
1263 { /* only parsed certificates point these fields to "encoded" */
1264 chunk_free(&this->signature);
1265 chunk_free(&this->serialNumber);
1266 chunk_free(&this->tbsCertificate);
1267 }
1268 free(this);
1269 }
1270 }
1271
1272 /**
1273 * create an empty but initialized X.509 certificate
1274 */
1275 static private_x509_cert_t* create_empty(void)
1276 {
1277 private_x509_cert_t *this = malloc_thing(private_x509_cert_t);
1278
1279 this->public.interface.interface.get_type = (certificate_type_t (*) (certificate_t*))get_type;
1280 this->public.interface.interface.get_subject = (identification_t* (*) (certificate_t*))get_subject;
1281 this->public.interface.interface.get_issuer = (identification_t* (*) (certificate_t*))get_issuer;
1282 this->public.interface.interface.has_subject = (id_match_t (*) (certificate_t*, identification_t*))has_subject;
1283 this->public.interface.interface.has_issuer = (id_match_t (*) (certificate_t*, identification_t*))has_issuer;
1284 this->public.interface.interface.issued_by = (bool (*) (certificate_t*, certificate_t*))issued_by;
1285 this->public.interface.interface.get_public_key = (public_key_t* (*) (certificate_t*))get_public_key;
1286 this->public.interface.interface.get_validity = (bool (*) (certificate_t*, time_t*, time_t*, time_t*))get_validity;
1287 this->public.interface.interface.is_newer = (bool (*) (certificate_t*,certificate_t*))is_newer;
1288 this->public.interface.interface.get_encoding = (chunk_t (*) (certificate_t*))get_encoding;
1289 this->public.interface.interface.equals = (bool (*)(certificate_t*, certificate_t*))equals;
1290 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t*))get_ref;
1291 this->public.interface.interface.destroy = (void (*)(certificate_t*))destroy;
1292 this->public.interface.get_flags = (x509_flag_t (*)(x509_t*))get_flags;
1293 this->public.interface.get_serial = (chunk_t (*)(x509_t*))get_serial;
1294 this->public.interface.get_subjectKeyIdentifier = (chunk_t (*)(x509_t*))get_subjectKeyIdentifier;
1295 this->public.interface.get_authKeyIdentifier = (chunk_t (*)(x509_t*))get_authKeyIdentifier;
1296 this->public.interface.get_pathLenConstraint = (int (*)(x509_t*))get_pathLenConstraint;
1297 this->public.interface.create_subjectAltName_enumerator = (enumerator_t* (*)(x509_t*))create_subjectAltName_enumerator;
1298 this->public.interface.create_crl_uri_enumerator = (enumerator_t* (*)(x509_t*))create_crl_uri_enumerator;
1299 this->public.interface.create_ocsp_uri_enumerator = (enumerator_t* (*)(x509_t*))create_ocsp_uri_enumerator;
1300
1301 this->encoding = chunk_empty;
1302 this->encoding_hash = chunk_empty;
1303 this->tbsCertificate = chunk_empty;
1304 this->version = 1;
1305 this->serialNumber = chunk_empty;
1306 this->notBefore = 0;
1307 this->notAfter = 0;
1308 this->public_key = NULL;
1309 this->subject = NULL;
1310 this->issuer = NULL;
1311 this->subjectAltNames = linked_list_create();
1312 this->crl_uris = linked_list_create();
1313 this->ocsp_uris = linked_list_create();
1314 this->ipAddrBlocks = linked_list_create();
1315 this->subjectKeyIdentifier = chunk_empty;
1316 this->authKeyIdentifier = chunk_empty;
1317 this->authKeySerialNumber = chunk_empty;
1318 this->pathLenConstraint = X509_NO_PATH_LEN_CONSTRAINT;
1319 this->algorithm = 0;
1320 this->signature = chunk_empty;
1321 this->flags = 0;
1322 this->ref = 1;
1323 this->parsed = FALSE;
1324
1325 return this;
1326 }
1327
1328 /**
1329 * Encode a linked list of subjectAltNames
1330 */
1331 chunk_t x509_build_subjectAltNames(linked_list_t *list)
1332 {
1333 chunk_t subjectAltNames = chunk_empty;
1334 enumerator_t *enumerator;
1335 identification_t *id;
1336
1337 if (list->get_count(list) == 0)
1338 {
1339 return chunk_empty;
1340 }
1341
1342 enumerator = list->create_enumerator(list);
1343 while (enumerator->enumerate(enumerator, &id))
1344 {
1345 int context;
1346 chunk_t name;
1347
1348 switch (id->get_type(id))
1349 {
1350 case ID_RFC822_ADDR:
1351 context = ASN1_CONTEXT_S_1;
1352 break;
1353 case ID_FQDN:
1354 context = ASN1_CONTEXT_S_2;
1355 break;
1356 case ID_IPV4_ADDR:
1357 case ID_IPV6_ADDR:
1358 context = ASN1_CONTEXT_S_7;
1359 break;
1360 default:
1361 DBG1("encoding %N as subjectAltName not supported",
1362 id_type_names, id->get_type(id));
1363 enumerator->destroy(enumerator);
1364 free(subjectAltNames.ptr);
1365 return chunk_empty;
1366 }
1367 name = asn1_wrap(context, "c", id->get_encoding(id));
1368 subjectAltNames = chunk_cat("mm", subjectAltNames, name);
1369 }
1370 enumerator->destroy(enumerator);
1371
1372 return asn1_wrap(ASN1_SEQUENCE, "mm",
1373 asn1_build_known_oid(OID_SUBJECT_ALT_NAME),
1374 asn1_wrap(ASN1_OCTET_STRING, "m",
1375 asn1_wrap(ASN1_SEQUENCE, "m", subjectAltNames)
1376 )
1377 );
1378 }
1379
1380 /**
1381 * Generate and sign a new certificate
1382 */
1383 static bool generate(private_x509_cert_t *cert, certificate_t *sign_cert,
1384 private_key_t *sign_key, int digest_alg)
1385 {
1386 chunk_t extensions = chunk_empty, extendedKeyUsage = chunk_empty;
1387 chunk_t serverAuth = chunk_empty, ocspSigning = chunk_empty;
1388 chunk_t basicConstraints = chunk_empty, subjectAltNames = chunk_empty;
1389 chunk_t subjectKeyIdentifier = chunk_empty, authKeyIdentifier = chunk_empty;
1390 chunk_t crlDistributionPoints = chunk_empty, authorityInfoAccess = chunk_empty;
1391 identification_t *issuer, *subject;
1392 chunk_t key_info;
1393 signature_scheme_t scheme;
1394 hasher_t *hasher;
1395 enumerator_t *enumerator;
1396 char *uri;
1397
1398 subject = cert->subject;
1399 if (sign_cert)
1400 {
1401 issuer = sign_cert->get_subject(sign_cert);
1402 if (!cert->public_key)
1403 {
1404 return FALSE;
1405 }
1406 }
1407 else
1408 { /* self signed */
1409 issuer = subject;
1410 if (!cert->public_key)
1411 {
1412 cert->public_key = sign_key->get_public_key(sign_key);
1413 }
1414 cert->flags |= X509_SELF_SIGNED;
1415 }
1416 cert->issuer = issuer->clone(issuer);
1417 if (!cert->notBefore)
1418 {
1419 cert->notBefore = time(NULL);
1420 }
1421 if (!cert->notAfter)
1422 { /* defaults to 1 year from now */
1423 cert->notAfter = cert->notBefore + 60 * 60 * 24 * 365;
1424 }
1425
1426 /* select signature scheme */
1427 cert->algorithm = hasher_signature_algorithm_to_oid(digest_alg,
1428 sign_key->get_type(sign_key));
1429 if (cert->algorithm == OID_UNKNOWN)
1430 {
1431 return FALSE;
1432 }
1433 scheme = signature_scheme_from_oid(cert->algorithm);
1434
1435 if (!cert->public_key->get_encoding(cert->public_key,
1436 KEY_PUB_SPKI_ASN1_DER, &key_info))
1437 {
1438 return FALSE;
1439 }
1440
1441 /* encode subjectAltNames */
1442 subjectAltNames = x509_build_subjectAltNames(cert->subjectAltNames);
1443
1444 /* encode CRL distribution points extension */
1445 enumerator = cert->crl_uris->create_enumerator(cert->crl_uris);
1446 while (enumerator->enumerate(enumerator, &uri))
1447 {
1448 chunk_t distributionPoint;
1449
1450 distributionPoint = asn1_wrap(ASN1_SEQUENCE, "m",
1451 asn1_wrap(ASN1_CONTEXT_C_0, "m",
1452 asn1_wrap(ASN1_CONTEXT_C_0, "m",
1453 asn1_wrap(ASN1_CONTEXT_S_6, "c",
1454 chunk_create(uri, strlen(uri))))));
1455
1456 crlDistributionPoints = chunk_cat("mm", crlDistributionPoints,
1457 distributionPoint);
1458 }
1459 enumerator->destroy(enumerator);
1460 if (crlDistributionPoints.ptr)
1461 {
1462 crlDistributionPoints = asn1_wrap(ASN1_SEQUENCE, "mm",
1463 asn1_build_known_oid(OID_CRL_DISTRIBUTION_POINTS),
1464 asn1_wrap(ASN1_OCTET_STRING, "m",
1465 asn1_wrap(ASN1_SEQUENCE, "m", crlDistributionPoints)));
1466 }
1467
1468 /* encode OCSP URIs in authorityInfoAccess extension */
1469 enumerator = cert->ocsp_uris->create_enumerator(cert->ocsp_uris);
1470 while (enumerator->enumerate(enumerator, &uri))
1471 {
1472 chunk_t accessDescription;
1473
1474 accessDescription = asn1_wrap(ASN1_SEQUENCE, "mm",
1475 asn1_build_known_oid(OID_OCSP),
1476 asn1_wrap(ASN1_CONTEXT_S_6, "c",
1477 chunk_create(uri, strlen(uri))));
1478 authorityInfoAccess = chunk_cat("mm", authorityInfoAccess,
1479 accessDescription);
1480 }
1481 enumerator->destroy(enumerator);
1482 if (authorityInfoAccess.ptr)
1483 {
1484 authorityInfoAccess = asn1_wrap(ASN1_SEQUENCE, "mm",
1485 asn1_build_known_oid(OID_AUTHORITY_INFO_ACCESS),
1486 asn1_wrap(ASN1_OCTET_STRING, "m",
1487 asn1_wrap(ASN1_SEQUENCE, "m", authorityInfoAccess)));
1488 }
1489
1490 /* build CA basicConstraint for CA certificates */
1491 if (cert->flags & X509_CA)
1492 {
1493 basicConstraints = asn1_wrap(ASN1_SEQUENCE, "mmm",
1494 asn1_build_known_oid(OID_BASIC_CONSTRAINTS),
1495 asn1_wrap(ASN1_BOOLEAN, "c",
1496 chunk_from_chars(0xFF)),
1497 asn1_wrap(ASN1_OCTET_STRING, "m",
1498 asn1_wrap(ASN1_SEQUENCE, "m",
1499 asn1_wrap(ASN1_BOOLEAN, "c",
1500 chunk_from_chars(0xFF)))));
1501 }
1502
1503 /* add serverAuth extendedKeyUsage flag */
1504 if (cert->flags & X509_SERVER_AUTH)
1505 {
1506 serverAuth = asn1_build_known_oid(OID_SERVER_AUTH);
1507 }
1508
1509 /* add ocspSigning extendedKeyUsage flag */
1510 if (cert->flags & X509_OCSP_SIGNER)
1511 {
1512 ocspSigning = asn1_build_known_oid(OID_OCSP_SIGNING);
1513 }
1514
1515 if (serverAuth.ptr || ocspSigning.ptr)
1516 {
1517 extendedKeyUsage = asn1_wrap(ASN1_SEQUENCE, "mm",
1518 asn1_build_known_oid(OID_EXTENDED_KEY_USAGE),
1519 asn1_wrap(ASN1_OCTET_STRING, "m",
1520 asn1_wrap(ASN1_SEQUENCE, "mm",
1521 serverAuth, ocspSigning)));
1522 }
1523
1524 /* add subjectKeyIdentifier to CA and OCSP signer certificates */
1525 if (cert->flags & (X509_CA | X509_OCSP_SIGNER))
1526 {
1527 chunk_t keyid;
1528
1529 if (cert->public_key->get_fingerprint(cert->public_key,
1530 KEY_ID_PUBKEY_SHA1, &keyid))
1531 {
1532 subjectKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1533 asn1_build_known_oid(OID_SUBJECT_KEY_ID),
1534 asn1_wrap(ASN1_OCTET_STRING, "m",
1535 asn1_wrap(ASN1_OCTET_STRING, "c", keyid)));
1536 }
1537 }
1538
1539 /* add the keyid authKeyIdentifier for non self-signed certificates */
1540 if (sign_key)
1541 {
1542 chunk_t keyid;
1543
1544 if (sign_key->get_fingerprint(sign_key, KEY_ID_PUBKEY_SHA1, &keyid))
1545 {
1546 authKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1547 asn1_build_known_oid(OID_AUTHORITY_KEY_ID),
1548 asn1_wrap(ASN1_OCTET_STRING, "m",
1549 asn1_wrap(ASN1_SEQUENCE, "m",
1550 asn1_wrap(ASN1_CONTEXT_S_0, "c", keyid))));
1551 }
1552 }
1553 if (basicConstraints.ptr || subjectAltNames.ptr || authKeyIdentifier.ptr ||
1554 crlDistributionPoints.ptr)
1555 {
1556 extensions = asn1_wrap(ASN1_CONTEXT_C_3, "m",
1557 asn1_wrap(ASN1_SEQUENCE, "mmmmmmm",
1558 basicConstraints, subjectKeyIdentifier,
1559 authKeyIdentifier, subjectAltNames,
1560 extendedKeyUsage, crlDistributionPoints,
1561 authorityInfoAccess));
1562 }
1563
1564 cert->tbsCertificate = asn1_wrap(ASN1_SEQUENCE, "mmmcmcmm",
1565 asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2),
1566 asn1_integer("c", cert->serialNumber),
1567 asn1_algorithmIdentifier(cert->algorithm),
1568 issuer->get_encoding(issuer),
1569 asn1_wrap(ASN1_SEQUENCE, "mm",
1570 asn1_from_time(&cert->notBefore, ASN1_UTCTIME),
1571 asn1_from_time(&cert->notAfter, ASN1_UTCTIME)),
1572 subject->get_encoding(subject),
1573 key_info, extensions);
1574
1575 if (!sign_key->sign(sign_key, scheme, cert->tbsCertificate, &cert->signature))
1576 {
1577 return FALSE;
1578 }
1579 cert->encoding = asn1_wrap(ASN1_SEQUENCE, "cmm", cert->tbsCertificate,
1580 asn1_algorithmIdentifier(cert->algorithm),
1581 asn1_bitstring("c", cert->signature));
1582
1583 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1584 if (!hasher)
1585 {
1586 return FALSE;
1587 }
1588 hasher->allocate_hash(hasher, cert->encoding, &cert->encoding_hash);
1589 hasher->destroy(hasher);
1590 return TRUE;
1591 }
1592
1593 /**
1594 * See header.
1595 */
1596 x509_cert_t *x509_cert_load(certificate_type_t type, va_list args)
1597 {
1598 x509_flag_t flags = 0;
1599 chunk_t blob = chunk_empty;
1600
1601 while (TRUE)
1602 {
1603 switch (va_arg(args, builder_part_t))
1604 {
1605 case BUILD_BLOB_ASN1_DER:
1606 blob = va_arg(args, chunk_t);
1607 continue;
1608 case BUILD_X509_FLAG:
1609 flags |= va_arg(args, x509_flag_t);
1610 continue;
1611 case BUILD_END:
1612 break;
1613 default:
1614 return NULL;
1615 }
1616 break;
1617 }
1618
1619 if (blob.ptr)
1620 {
1621 private_x509_cert_t *cert = create_empty();
1622
1623 cert->encoding = chunk_clone(blob);
1624 cert->parsed = TRUE;
1625 if (parse_certificate(cert))
1626 {
1627 cert->flags |= flags;
1628 return &cert->public;
1629 }
1630 destroy(cert);
1631 }
1632 return NULL;
1633 }
1634
1635 /**
1636 * See header.
1637 */
1638 x509_cert_t *x509_cert_gen(certificate_type_t type, va_list args)
1639 {
1640 private_x509_cert_t *cert;
1641 certificate_t *sign_cert = NULL;
1642 private_key_t *sign_key = NULL;
1643 hash_algorithm_t digest_alg = HASH_SHA1;
1644
1645 cert = create_empty();
1646 while (TRUE)
1647 {
1648 switch (va_arg(args, builder_part_t))
1649 {
1650 case BUILD_X509_FLAG:
1651 cert->flags |= va_arg(args, x509_flag_t);
1652 continue;
1653 case BUILD_SIGNING_KEY:
1654 sign_key = va_arg(args, private_key_t*);
1655 continue;
1656 case BUILD_SIGNING_CERT:
1657 sign_cert = va_arg(args, certificate_t*);
1658 continue;
1659 case BUILD_PUBLIC_KEY:
1660 cert->public_key = va_arg(args, public_key_t*);
1661 cert->public_key->get_ref(cert->public_key);
1662 continue;
1663 case BUILD_SUBJECT:
1664 cert->subject = va_arg(args, identification_t*);
1665 cert->subject = cert->subject->clone(cert->subject);
1666 continue;
1667 case BUILD_SUBJECT_ALTNAMES:
1668 {
1669 enumerator_t *enumerator;
1670 identification_t *id;
1671 linked_list_t *list;
1672
1673 list = va_arg(args, linked_list_t*);
1674 enumerator = list->create_enumerator(list);
1675 while (enumerator->enumerate(enumerator, &id))
1676 {
1677 cert->subjectAltNames->insert_last(cert->subjectAltNames,
1678 id->clone(id));
1679 }
1680 enumerator->destroy(enumerator);
1681 continue;
1682 }
1683 case BUILD_CRL_DISTRIBUTION_POINTS:
1684 {
1685 enumerator_t *enumerator;
1686 linked_list_t *list;
1687 char *uri;
1688
1689 list = va_arg(args, linked_list_t*);
1690 enumerator = list->create_enumerator(list);
1691 while (enumerator->enumerate(enumerator, &uri))
1692 {
1693 cert->crl_uris->insert_last(cert->crl_uris, strdup(uri));
1694 }
1695 enumerator->destroy(enumerator);
1696 continue;
1697 }
1698 case BUILD_OCSP_ACCESS_LOCATIONS:
1699 {
1700 enumerator_t *enumerator;
1701 linked_list_t *list;
1702 char *uri;
1703
1704 list = va_arg(args, linked_list_t*);
1705 enumerator = list->create_enumerator(list);
1706 while (enumerator->enumerate(enumerator, &uri))
1707 {
1708 cert->ocsp_uris->insert_last(cert->ocsp_uris, strdup(uri));
1709 }
1710 enumerator->destroy(enumerator);
1711 continue;
1712 }
1713 case BUILD_NOT_BEFORE_TIME:
1714 cert->notBefore = va_arg(args, time_t);
1715 continue;
1716 case BUILD_NOT_AFTER_TIME:
1717 cert->notAfter = va_arg(args, time_t);
1718 continue;
1719 case BUILD_SERIAL:
1720 cert->serialNumber = chunk_clone(va_arg(args, chunk_t));
1721 continue;
1722 case BUILD_DIGEST_ALG:
1723 digest_alg = va_arg(args, int);
1724 continue;
1725 case BUILD_END:
1726 break;
1727 default:
1728 destroy(cert);
1729 return NULL;
1730 }
1731 break;
1732 }
1733
1734 if (sign_key && generate(cert, sign_cert, sign_key, digest_alg))
1735 {
1736 return &cert->public;
1737 }
1738 destroy(cert);
1739 return NULL;
1740 }
1741