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