got rid of deprecated create_iterator_locked()
[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-2008 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 * $Id$
21 */
22
23 #define _GNU_SOURCE
24
25 #include "x509_cert.h"
26
27 #include <sys/stat.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include <library.h>
34 #include <debug.h>
35 #include <asn1/oid.h>
36 #include <asn1/asn1.h>
37 #include <asn1/asn1_parser.h>
38 #include <asn1/pem.h>
39 #include <crypto/hashers/hasher.h>
40 #include <utils/linked_list.h>
41 #include <utils/identification.h>
42
43 /**
44 * Different kinds of generalNames
45 */
46 typedef enum {
47 GN_OTHER_NAME = 0,
48 GN_RFC822_NAME = 1,
49 GN_DNS_NAME = 2,
50 GN_X400_ADDRESS = 3,
51 GN_DIRECTORY_NAME = 4,
52 GN_EDI_PARTY_NAME = 5,
53 GN_URI = 6,
54 GN_IP_ADDRESS = 7,
55 GN_REGISTERED_ID = 8,
56 } generalNames_t;
57
58
59 typedef struct private_x509_cert_t private_x509_cert_t;
60
61 /**
62 * Private data of a x509_cert_t object.
63 */
64 struct private_x509_cert_t {
65 /**
66 * Public interface for this certificate.
67 */
68 x509_cert_t public;
69
70 /**
71 * X.509 certificate encoding in ASN.1 DER format
72 */
73 chunk_t encoding;
74
75 /**
76 * SHA1 hash of the DER encoding of this X.509 certificate
77 */
78 chunk_t encoding_hash;
79
80 /**
81 * X.509 certificate body over which signature is computed
82 */
83 chunk_t tbsCertificate;
84
85 /**
86 * Version of the X.509 certificate
87 */
88 u_int version;
89
90 /**
91 * Serial number of the X.509 certificate
92 */
93 chunk_t serialNumber;
94
95 /**
96 * ID representing the certificate issuer
97 */
98 identification_t *issuer;
99
100 /**
101 * Start time of certificate validity
102 */
103 time_t notBefore;
104
105 /**
106 * End time of certificate validity
107 */
108 time_t notAfter;
109
110 /**
111 * ID representing the certificate subject
112 */
113 identification_t *subject;
114
115 /**
116 * List of subjectAltNames as identification_t
117 */
118 linked_list_t *subjectAltNames;
119
120 /**
121 * List of crlDistributionPoints as allocated char*
122 */
123 linked_list_t *crl_uris;
124
125 /**
126 * List ocspAccessLocations as identification_t
127 */
128 linked_list_t *ocsp_uris;
129
130 /**
131 * certificates embedded public key
132 */
133 public_key_t *public_key;
134
135 /**
136 * Subject Key Identifier
137 */
138 chunk_t subjectKeyID;
139
140 /**
141 * Authority Key Identifier
142 */
143 identification_t *authKeyIdentifier;
144
145 /**
146 * Authority Key Serial Number
147 */
148 chunk_t authKeySerialNumber;
149
150 /**
151 * x509 constraints and other flags
152 */
153 x509_flag_t flags;
154
155 /**
156 * Signature algorithm
157 */
158 int algorithm;
159
160 /**
161 * Signature
162 */
163 chunk_t signature;
164
165 /**
166 * reference count
167 */
168 refcount_t ref;
169 };
170
171 static u_char ASN1_sAN_oid_buf[] = {
172 0x06, 0x03, 0x55, 0x1D, 0x11
173 };
174 static const chunk_t ASN1_subjectAltName_oid = chunk_from_buf(ASN1_sAN_oid_buf);
175
176 /**
177 * ASN.1 definition of a basicConstraints extension
178 */
179 static const asn1Object_t basicConstraintsObjects[] = {
180 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
181 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
182 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
183 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
184 { 0, "exit", ASN1_EOC, ASN1_EXIT }
185 };
186 #define BASIC_CONSTRAINTS_CA 1
187
188 /**
189 * Extracts the basicConstraints extension
190 */
191 static bool parse_basicConstraints(chunk_t blob, int level0)
192 {
193 asn1_parser_t *parser;
194 chunk_t object;
195 int objectID;
196 bool isCA = FALSE;
197
198 parser = asn1_parser_create(basicConstraintsObjects, blob);
199 parser->set_top_level(parser, level0);
200
201 while (parser->iterate(parser, &objectID, &object))
202 {
203 if (objectID == BASIC_CONSTRAINTS_CA)
204 {
205 isCA = object.len && *object.ptr;
206 DBG2(" %s", isCA ? "TRUE" : "FALSE");
207 }
208 }
209 parser->destroy(parser);
210
211 return isCA;
212 }
213
214 /**
215 * ASN.1 definition of otherName
216 */
217 static const asn1Object_t otherNameObjects[] = {
218 {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */
219 {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY }, /* 1 */
220 {0, "exit", ASN1_EOC, ASN1_EXIT }
221 };
222 #define ON_OBJ_ID_TYPE 0
223 #define ON_OBJ_VALUE 1
224
225 /**
226 * Extracts an otherName
227 */
228 static bool parse_otherName(chunk_t blob, int level0)
229 {
230 asn1_parser_t *parser;
231 chunk_t object;
232 int objectID;
233 int oid = OID_UNKNOWN;
234 bool success = FALSE;
235
236 parser = asn1_parser_create(otherNameObjects, blob);
237 parser->set_top_level(parser, level0);
238
239 while (parser->iterate(parser, &objectID, &object))
240 {
241 switch (objectID)
242 {
243 case ON_OBJ_ID_TYPE:
244 oid = asn1_known_oid(object);
245 break;
246 case ON_OBJ_VALUE:
247 if (oid == OID_XMPP_ADDR)
248 {
249 if (!asn1_parse_simple_object(&object, ASN1_UTF8STRING,
250 parser->get_level(parser)+1, "xmppAddr"))
251 {
252 goto end;
253 }
254 }
255 break;
256 default:
257 break;
258 }
259 }
260 success = parser->success(parser);
261
262 end:
263 parser->destroy(parser);
264 return success;
265 }
266
267 /**
268 * ASN.1 definition of generalName
269 */
270 static const asn1Object_t generalNameObjects[] = {
271 { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */
272 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */
273 { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */
274 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */
275 { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */
276 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
277 { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */
278 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
279 { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */
280 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
281 { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */
282 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
283 { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */
284 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */
285 { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */
286 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */
287 { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */
288 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 17 */
289 { 0, "exit", ASN1_EOC, ASN1_EXIT }
290 };
291 #define GN_OBJ_OTHER_NAME 0
292 #define GN_OBJ_RFC822_NAME 2
293 #define GN_OBJ_DNS_NAME 4
294 #define GN_OBJ_X400_ADDRESS 6
295 #define GN_OBJ_DIRECTORY_NAME 8
296 #define GN_OBJ_EDI_PARTY_NAME 10
297 #define GN_OBJ_URI 12
298 #define GN_OBJ_IP_ADDRESS 14
299 #define GN_OBJ_REGISTERED_ID 16
300
301 /**
302 * Extracts a generalName
303 */
304 static identification_t *parse_generalName(chunk_t blob, int level0)
305 {
306 asn1_parser_t *parser;
307 chunk_t object;
308 int objectID ;
309
310 identification_t *gn = NULL;
311
312 parser = asn1_parser_create(generalNameObjects, blob);
313 parser->set_top_level(parser, level0);
314
315 while (parser->iterate(parser, &objectID, &object))
316 {
317 id_type_t id_type = ID_ANY;
318
319 switch (objectID)
320 {
321 case GN_OBJ_RFC822_NAME:
322 id_type = ID_RFC822_ADDR;
323 break;
324 case GN_OBJ_DNS_NAME:
325 id_type = ID_FQDN;
326 break;
327 case GN_OBJ_URI:
328 id_type = ID_DER_ASN1_GN_URI;
329 break;
330 case GN_OBJ_DIRECTORY_NAME:
331 id_type = ID_DER_ASN1_DN;
332 break;
333 case GN_OBJ_IP_ADDRESS:
334 id_type = ID_IPV4_ADDR;
335 break;
336 case GN_OBJ_OTHER_NAME:
337 if (!parse_otherName(object, parser->get_level(parser)+1))
338 {
339 goto end;
340 }
341 break;
342 case GN_OBJ_X400_ADDRESS:
343 case GN_OBJ_EDI_PARTY_NAME:
344 case GN_OBJ_REGISTERED_ID:
345 default:
346 break;
347 }
348 if (id_type != ID_ANY)
349 {
350 gn = identification_create_from_encoding(id_type, object);
351 DBG2(" '%D'", gn);
352 goto end;
353 }
354 }
355
356 end:
357 parser->destroy(parser);
358 return gn;
359 }
360
361 /**
362 * ASN.1 definition of generalNames
363 */
364 static const asn1Object_t generalNamesObjects[] = {
365 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
366 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
367 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
368 { 0, "exit", ASN1_EOC, ASN1_EXIT }
369 };
370 #define GENERAL_NAMES_GN 1
371
372 /**
373 * Extracts one or several GNs and puts them into a chained list
374 */
375 void x509_parse_generalNames(chunk_t blob, int level0, bool implicit, linked_list_t *list)
376 {
377 asn1_parser_t *parser;
378 chunk_t object;
379 int objectID;
380
381 parser = asn1_parser_create(generalNamesObjects, blob);
382 parser->set_top_level(parser, level0);
383 parser->set_flags(parser, implicit, FALSE);
384
385 while (parser->iterate(parser, &objectID, &object))
386 {
387 if (objectID == GENERAL_NAMES_GN)
388 {
389 identification_t *gn = parse_generalName(object,
390 parser->get_level(parser)+1);
391
392 if (gn)
393 {
394 list->insert_last(list, (void *)gn);
395 }
396 }
397 }
398 parser->destroy(parser);
399 }
400
401 /**
402 * ASN.1 definition of a authorityKeyIdentifier extension
403 */
404 static const asn1Object_t authKeyIdentifierObjects[] = {
405 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
406 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_BODY }, /* 1 */
407 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
408 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
409 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
410 { 1, "authorityCertSerialNumber", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
411 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 6 */
412 { 0, "exit", ASN1_EOC, ASN1_EXIT }
413 };
414 #define AUTH_KEY_ID_KEY_ID 1
415 #define AUTH_KEY_ID_CERT_ISSUER 3
416 #define AUTH_KEY_ID_CERT_SERIAL 5
417
418 /**
419 * Extracts an authoritykeyIdentifier
420 */
421 identification_t* x509_parse_authorityKeyIdentifier(chunk_t blob, int level0,
422 chunk_t *authKeySerialNumber)
423 {
424 asn1_parser_t *parser;
425 chunk_t object;
426 int objectID;
427 identification_t *authKeyIdentifier = NULL;
428
429 *authKeySerialNumber = chunk_empty;
430
431 parser = asn1_parser_create(authKeyIdentifierObjects, blob);
432 parser->set_top_level(parser, level0);
433
434 while (parser->iterate(parser, &objectID, &object))
435 {
436 switch (objectID)
437 {
438 case AUTH_KEY_ID_KEY_ID:
439 authKeyIdentifier = identification_create_from_encoding(
440 ID_PUBKEY_SHA1, object);
441 break;
442 case AUTH_KEY_ID_CERT_ISSUER:
443 /* TODO: x509_parse_generalNames(object, level+1, TRUE); */
444 break;
445 case AUTH_KEY_ID_CERT_SERIAL:
446 *authKeySerialNumber = object;
447 break;
448 default:
449 break;
450 }
451 }
452 parser->destroy(parser);
453 return authKeyIdentifier;
454 }
455
456 /**
457 * ASN.1 definition of a authorityInfoAccess extension
458 */
459 static const asn1Object_t authInfoAccessObjects[] = {
460 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
461 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
462 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
463 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
464 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
465 { 0, "exit", ASN1_EOC, ASN1_EXIT }
466 };
467 #define AUTH_INFO_ACCESS_METHOD 2
468 #define AUTH_INFO_ACCESS_LOCATION 3
469
470 /**
471 * Extracts an authorityInfoAcess location
472 */
473 static void parse_authorityInfoAccess(chunk_t blob, int level0,
474 private_x509_cert_t *this)
475 {
476 asn1_parser_t *parser;
477 chunk_t object;
478 int objectID;
479 int accessMethod = OID_UNKNOWN;
480
481 parser = asn1_parser_create(authInfoAccessObjects, blob);
482 parser->set_top_level(parser, level0);
483
484 while (parser->iterate(parser, &objectID, &object))
485 {
486 switch (objectID)
487 {
488 case AUTH_INFO_ACCESS_METHOD:
489 accessMethod = asn1_known_oid(object);
490 break;
491 case AUTH_INFO_ACCESS_LOCATION:
492 {
493 switch (accessMethod)
494 {
495 case OID_OCSP:
496 case OID_CA_ISSUERS:
497 {
498 identification_t *id;
499 char *uri;
500
501 id = parse_generalName(object,
502 parser->get_level(parser)+1);
503 if (id == NULL)
504 {
505 /* parsing went wrong - abort */
506 goto end;
507 }
508 DBG2(" '%D'", id);
509 if (accessMethod == OID_OCSP &&
510 asprintf(&uri, "%D", id) > 0)
511 {
512 this->ocsp_uris->insert_last(this->ocsp_uris, uri);
513 }
514 id->destroy(id);
515 }
516 break;
517 default:
518 /* unkown accessMethod, ignoring */
519 break;
520 }
521 break;
522 }
523 default:
524 break;
525 }
526 }
527
528 end:
529 parser->destroy(parser);
530 }
531
532 /**
533 * ASN.1 definition of a extendedKeyUsage extension
534 */
535 static const asn1Object_t extendedKeyUsageObjects[] = {
536 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
537 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
538 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
539 { 0, "exit", ASN1_EOC, ASN1_EXIT }
540 };
541 #define EXT_KEY_USAGE_PURPOSE_ID 1
542
543 /**
544 * Extracts extendedKeyUsage OIDs - currently only OCSP_SIGING is returned
545 */
546 static bool parse_extendedKeyUsage(chunk_t blob, int level0)
547 {
548 asn1_parser_t *parser;
549 chunk_t object;
550 int objectID;
551 bool ocsp_signing = FALSE;
552
553 parser = asn1_parser_create(extendedKeyUsageObjects, blob);
554 parser->set_top_level(parser, level0);
555
556 while (parser->iterate(parser, &objectID, &object))
557 {
558 if (objectID == EXT_KEY_USAGE_PURPOSE_ID &&
559 asn1_known_oid(object) == OID_OCSP_SIGNING)
560 {
561 ocsp_signing = TRUE;
562 }
563 }
564 parser->destroy(parser);
565 return ocsp_signing;
566 }
567
568 /**
569 * ASN.1 definition of crlDistributionPoints
570 */
571 static const asn1Object_t crlDistributionPointsObjects[] = {
572 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
573 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
574 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
575 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
576 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
577 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
578 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
579 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
580 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
581 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
582 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
583 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
584 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
585 { 0, "exit", ASN1_EOC, ASN1_EXIT }
586 };
587 #define CRL_DIST_POINTS_FULLNAME 3
588
589 /**
590 * Extracts one or several crlDistributionPoints into a list
591 */
592 static void parse_crlDistributionPoints(chunk_t blob, int level0,
593 private_x509_cert_t *this)
594 {
595 asn1_parser_t *parser;
596 chunk_t object;
597 int objectID;
598 linked_list_t *list = linked_list_create();
599
600 parser = asn1_parser_create(crlDistributionPointsObjects, blob);
601 parser->set_top_level(parser, level0);
602
603 while (parser->iterate(parser, &objectID, &object))
604 {
605 if (objectID == CRL_DIST_POINTS_FULLNAME)
606 {
607 identification_t *id;
608
609 /* append extracted generalNames to existing chained list */
610 x509_parse_generalNames(object, parser->get_level(parser)+1,
611 TRUE, list);
612
613 while (list->remove_last(list, (void**)&id) == SUCCESS)
614 {
615 char *uri;
616
617 if (asprintf(&uri, "%D", id) > 0)
618 {
619 this->crl_uris->insert_last(this->crl_uris, uri);
620 }
621 id->destroy(id);
622 }
623 }
624 }
625 parser->destroy(parser);
626 list->destroy(list);
627 }
628
629 /**
630 * ASN.1 definition of an X.509v3 x509_cert
631 */
632 static const asn1Object_t certObjects[] = {
633 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
634 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
635 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
636 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
637 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
638 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
639 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
640 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
641 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
642 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
643 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
644 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
645 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
646 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
647 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
648 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
649 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
650 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
651 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
652 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
653 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
654 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
655 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
656 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
657 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
658 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
659 { 0, "exit", ASN1_EOC, ASN1_EXIT }
660 };
661 #define X509_OBJ_TBS_CERTIFICATE 1
662 #define X509_OBJ_VERSION 3
663 #define X509_OBJ_SERIAL_NUMBER 4
664 #define X509_OBJ_SIG_ALG 5
665 #define X509_OBJ_ISSUER 6
666 #define X509_OBJ_NOT_BEFORE 8
667 #define X509_OBJ_NOT_AFTER 9
668 #define X509_OBJ_SUBJECT 10
669 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
670 #define X509_OBJ_EXTN_ID 19
671 #define X509_OBJ_CRITICAL 20
672 #define X509_OBJ_EXTN_VALUE 21
673 #define X509_OBJ_ALGORITHM 24
674 #define X509_OBJ_SIGNATURE 25
675
676 /**
677 * Parses an X.509v3 certificate
678 */
679 static bool parse_certificate(private_x509_cert_t *this)
680 {
681 asn1_parser_t *parser;
682 chunk_t object;
683 int objectID;
684 int extn_oid = OID_UNKNOWN;
685 int sig_alg = OID_UNKNOWN;
686 bool success = FALSE;
687 bool critical;
688
689 parser = asn1_parser_create(certObjects, this->encoding);
690
691 while (parser->iterate(parser, &objectID, &object))
692 {
693 u_int level = parser->get_level(parser)+1;
694
695 switch (objectID)
696 {
697 case X509_OBJ_TBS_CERTIFICATE:
698 this->tbsCertificate = object;
699 break;
700 case X509_OBJ_VERSION:
701 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
702 DBG2(" v%d", this->version);
703 break;
704 case X509_OBJ_SERIAL_NUMBER:
705 this->serialNumber = object;
706 break;
707 case X509_OBJ_SIG_ALG:
708 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
709 break;
710 case X509_OBJ_ISSUER:
711 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
712 DBG2(" '%D'", this->issuer);
713 break;
714 case X509_OBJ_NOT_BEFORE:
715 this->notBefore = asn1_parse_time(object, level);
716 break;
717 case X509_OBJ_NOT_AFTER:
718 this->notAfter = asn1_parse_time(object, level);
719 break;
720 case X509_OBJ_SUBJECT:
721 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
722 DBG2(" '%D'", this->subject);
723 break;
724 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
725 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
726 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
727 if (this->public_key == NULL)
728 {
729 DBG1("could not create public key");
730 goto end;
731 }
732 break;
733 case X509_OBJ_EXTN_ID:
734 extn_oid = asn1_known_oid(object);
735 break;
736 case X509_OBJ_CRITICAL:
737 critical = object.len && *object.ptr;
738 DBG2(" %s", critical ? "TRUE" : "FALSE");
739 break;
740 case X509_OBJ_EXTN_VALUE:
741 {
742 switch (extn_oid)
743 {
744 case OID_SUBJECT_KEY_ID:
745 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
746 level, "keyIdentifier"))
747 {
748 goto end;
749 }
750 this->subjectKeyID = object;
751 break;
752 case OID_SUBJECT_ALT_NAME:
753 x509_parse_generalNames(object, level, FALSE,
754 this->subjectAltNames);
755 break;
756 case OID_BASIC_CONSTRAINTS:
757 if (parse_basicConstraints(object, level))
758 {
759 this->flags |= X509_CA;
760 }
761 break;
762 case OID_CRL_DISTRIBUTION_POINTS:
763 parse_crlDistributionPoints(object, level, this);
764 break;
765 case OID_AUTHORITY_KEY_ID:
766 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
767 level, &this->authKeySerialNumber);
768 break;
769 case OID_AUTHORITY_INFO_ACCESS:
770 parse_authorityInfoAccess(object, level, this);
771 break;
772 case OID_EXTENDED_KEY_USAGE:
773 if (parse_extendedKeyUsage(object, level))
774 {
775 this->flags |= X509_OCSP_SIGNER;
776 }
777 break;
778 case OID_NS_REVOCATION_URL:
779 case OID_NS_CA_REVOCATION_URL:
780 case OID_NS_CA_POLICY_URL:
781 case OID_NS_COMMENT:
782 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING,
783 level, oid_names[extn_oid].name))
784 {
785 goto end;
786 }
787 break;
788 default:
789 break;
790 }
791 break;
792 }
793 case X509_OBJ_ALGORITHM:
794 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
795 if (this->algorithm != sig_alg)
796 {
797 DBG1(" signature algorithms do not agree");
798 goto end;
799 }
800 break;
801 case X509_OBJ_SIGNATURE:
802 this->signature = object;
803 break;
804 default:
805 break;
806 }
807 }
808 success = parser->success(parser);
809
810 end:
811 parser->destroy(parser);
812 return success;
813 }
814
815 /**
816 * Implementation of certificate_t.get_type
817 */
818 static certificate_type_t get_type(private_x509_cert_t *this)
819 {
820 return CERT_X509;
821 }
822
823 /**
824 * Implementation of certificate_t.get_subject
825 */
826 static identification_t* get_subject(private_x509_cert_t *this)
827 {
828 return this->subject;
829 }
830
831 /**
832 * Implementation of certificate_t.get_issuer
833 */
834 static identification_t* get_issuer(private_x509_cert_t *this)
835 {
836 return this->issuer;
837 }
838
839 /**
840 * Implementation of certificate_t.has_subject.
841 */
842 static id_match_t has_subject(private_x509_cert_t *this, identification_t *subject)
843 {
844 identification_t *current;
845 enumerator_t *enumerator;
846 id_match_t match, best;
847
848 if (this->encoding_hash.ptr && subject->get_type(subject) == ID_CERT_DER_SHA1 &&
849 chunk_equals(this->encoding_hash, subject->get_encoding(subject)))
850 {
851 return ID_MATCH_PERFECT;
852 }
853
854 best = this->subject->matches(this->subject, subject);
855 enumerator = this->subjectAltNames->create_enumerator(this->subjectAltNames);
856 while (enumerator->enumerate(enumerator, &current))
857 {
858 match = current->matches(current, subject);
859 if (match > best)
860 {
861 best = match;
862 }
863 }
864 enumerator->destroy(enumerator);
865 return best;
866 }
867
868 /**
869 * Implementation of certificate_t.has_subject.
870 */
871 static id_match_t has_issuer(private_x509_cert_t *this, identification_t *issuer)
872 {
873 /* issuerAltNames currently not supported */
874 return this->issuer->matches(this->issuer, issuer);
875 }
876
877 /**
878 * Implementation of certificate_t.issued_by
879 */
880 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer)
881 {
882 public_key_t *key;
883 signature_scheme_t scheme;
884 bool valid;
885 x509_t *x509 = (x509_t*)issuer;
886
887 if (&this->public.interface.interface == issuer)
888 {
889 if (this->flags & X509_SELF_SIGNED)
890 {
891 return TRUE;
892 }
893 }
894 else
895 {
896 if (issuer->get_type(issuer) != CERT_X509)
897 {
898 return FALSE;
899 }
900 if (!(x509->get_flags(x509) & X509_CA))
901 {
902 return FALSE;
903 }
904 }
905 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
906 {
907 return FALSE;
908 }
909 /* TODO: generic OID to scheme mapper? */
910 switch (this->algorithm)
911 {
912 case OID_MD5_WITH_RSA:
913 scheme = SIGN_RSA_EMSA_PKCS1_MD5;
914 break;
915 case OID_SHA1_WITH_RSA:
916 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
917 break;
918 case OID_SHA256_WITH_RSA:
919 scheme = SIGN_RSA_EMSA_PKCS1_SHA256;
920 break;
921 case OID_SHA384_WITH_RSA:
922 scheme = SIGN_RSA_EMSA_PKCS1_SHA384;
923 break;
924 case OID_SHA512_WITH_RSA:
925 scheme = SIGN_RSA_EMSA_PKCS1_SHA512;
926 break;
927 case OID_ECDSA_WITH_SHA1:
928 scheme = SIGN_ECDSA_WITH_SHA1;
929 break;
930 default:
931 return FALSE;
932 }
933 key = issuer->get_public_key(issuer);
934 if (key == NULL)
935 {
936 return FALSE;
937 }
938 /* TODO: add a lightweight check option (comparing auth/subject keyids only) */
939 valid = key->verify(key, scheme, this->tbsCertificate, this->signature);
940 key->destroy(key);
941 return valid;
942 }
943
944 /**
945 * Implementation of certificate_t.get_public_key
946 */
947 static public_key_t* get_public_key(private_x509_cert_t *this)
948 {
949 this->public_key->get_ref(this->public_key);
950 return this->public_key;
951 }
952
953 /**
954 * Implementation of certificate_t.asdf
955 */
956 static private_x509_cert_t* get_ref(private_x509_cert_t *this)
957 {
958 ref_get(&this->ref);
959 return this;
960 }
961
962 /**
963 * Implementation of x509_cert_t.get_flags.
964 */
965 static x509_flag_t get_flags(private_x509_cert_t *this)
966 {
967 return this->flags;
968 }
969
970 /**
971 * Implementation of x509_cert_t.get_validity.
972 */
973 static bool get_validity(private_x509_cert_t *this, time_t *when,
974 time_t *not_before, time_t *not_after)
975 {
976 time_t t;
977
978 if (when)
979 {
980 t = *when;
981 }
982 else
983 {
984 t = time(NULL);
985 }
986 if (not_before)
987 {
988 *not_before = this->notBefore;
989 }
990 if (not_after)
991 {
992 *not_after = this->notAfter;
993 }
994 return (t >= this->notBefore && t <= this->notAfter);
995 }
996
997 /**
998 * Implementation of certificate_t.is_newer.
999 */
1000 static bool is_newer(certificate_t *this, certificate_t *that)
1001 {
1002 time_t this_update, that_update, now = time(NULL);
1003 bool new;
1004
1005 this->get_validity(this, &now, &this_update, NULL);
1006 that->get_validity(that, &now, &that_update, NULL);
1007 new = this_update > that_update;
1008 DBG1(" certificate from %#T is %s - existing certificate from %#T %s",
1009 &this_update, FALSE, new ? "newer":"not newer",
1010 &that_update, FALSE, new ? "replaced":"retained");
1011 return new;
1012 }
1013
1014 /**
1015 * Implementation of certificate_t.get_encoding.
1016 */
1017 static chunk_t get_encoding(private_x509_cert_t *this)
1018 {
1019 return chunk_clone(this->encoding);
1020 }
1021
1022 /**
1023 * Implementation of certificate_t.equals.
1024 */
1025 static bool equals(private_x509_cert_t *this, certificate_t *other)
1026 {
1027 chunk_t encoding;
1028 bool equal;
1029
1030 if (this == (private_x509_cert_t*)other)
1031 {
1032 return TRUE;
1033 }
1034 if (other->get_type(other) != CERT_X509)
1035 {
1036 return FALSE;
1037 }
1038 if (other->equals == (void*)equals)
1039 { /* skip allocation if we have the same implementation */
1040 return chunk_equals(this->encoding, ((private_x509_cert_t*)other)->encoding);
1041 }
1042 encoding = other->get_encoding(other);
1043 equal = chunk_equals(this->encoding, encoding);
1044 free(encoding.ptr);
1045 return equal;
1046 }
1047
1048 /**
1049 * Implementation of x509_t.get_serial.
1050 */
1051 static chunk_t get_serial(private_x509_cert_t *this)
1052 {
1053 return this->serialNumber;
1054 }
1055
1056 /**
1057 * Implementation of x509_t.get_authKeyIdentifier.
1058 */
1059 static identification_t *get_authKeyIdentifier(private_x509_cert_t *this)
1060 {
1061 return this->authKeyIdentifier;
1062 }
1063
1064 /**
1065 * Implementation of x509_cert_t.create_subjectAltName_enumerator.
1066 */
1067 static enumerator_t* create_subjectAltName_enumerator(private_x509_cert_t *this)
1068 {
1069 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
1070 }
1071
1072 /**
1073 * Implementation of x509_cert_t.create_ocsp_uri_enumerator.
1074 */
1075 static enumerator_t* create_ocsp_uri_enumerator(private_x509_cert_t *this)
1076 {
1077 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
1078 }
1079
1080 /**
1081 * Implementation of x509_cert_t.create_crl_uri_enumerator.
1082 */
1083 static enumerator_t* create_crl_uri_enumerator(private_x509_cert_t *this)
1084 {
1085 return this->crl_uris->create_enumerator(this->crl_uris);
1086 }
1087
1088 /**
1089 * Implementation of certificate_t.asdf
1090 */
1091 static void destroy(private_x509_cert_t *this)
1092 {
1093 if (ref_put(&this->ref))
1094 {
1095 this->subjectAltNames->destroy_offset(this->subjectAltNames,
1096 offsetof(identification_t, destroy));
1097 this->crl_uris->destroy_function(this->crl_uris, free);
1098 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
1099 DESTROY_IF(this->issuer);
1100 DESTROY_IF(this->subject);
1101 DESTROY_IF(this->public_key);
1102 DESTROY_IF(this->authKeyIdentifier);
1103 chunk_free(&this->encoding);
1104 chunk_free(&this->encoding_hash);
1105 free(this);
1106 }
1107 }
1108
1109 /**
1110 * create an empty but initialized X.509 certificate
1111 */
1112 static private_x509_cert_t* create_empty(void)
1113 {
1114 private_x509_cert_t *this = malloc_thing(private_x509_cert_t);
1115
1116 this->public.interface.interface.get_type = (certificate_type_t (*)(certificate_t *this))get_type;
1117 this->public.interface.interface.get_subject = (identification_t* (*)(certificate_t *this))get_subject;
1118 this->public.interface.interface.get_issuer = (identification_t* (*)(certificate_t *this))get_issuer;
1119 this->public.interface.interface.has_subject = (id_match_t (*)(certificate_t*, identification_t *subject))has_subject;
1120 this->public.interface.interface.has_issuer = (id_match_t (*)(certificate_t*, identification_t *issuer))has_issuer;
1121 this->public.interface.interface.issued_by = (bool (*)(certificate_t *this, certificate_t *issuer))issued_by;
1122 this->public.interface.interface.get_public_key = (public_key_t* (*)(certificate_t *this))get_public_key;
1123 this->public.interface.interface.get_validity = (bool (*)(certificate_t*, time_t *when, time_t *, time_t*))get_validity;
1124 this->public.interface.interface.is_newer = (bool (*)(certificate_t*,certificate_t*))is_newer;
1125 this->public.interface.interface.get_encoding = (chunk_t (*)(certificate_t*))get_encoding;
1126 this->public.interface.interface.equals = (bool (*)(certificate_t*, certificate_t *other))equals;
1127 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t *this))get_ref;
1128 this->public.interface.interface.destroy = (void (*)(certificate_t *this))destroy;
1129 this->public.interface.get_flags = (x509_flag_t (*)(x509_t*))get_flags;
1130 this->public.interface.get_serial = (chunk_t (*)(x509_t*))get_serial;
1131 this->public.interface.get_authKeyIdentifier = (identification_t* (*)(x509_t*))get_authKeyIdentifier;
1132 this->public.interface.create_subjectAltName_enumerator = (enumerator_t* (*)(x509_t*))create_subjectAltName_enumerator;
1133 this->public.interface.create_crl_uri_enumerator = (enumerator_t* (*)(x509_t*))create_crl_uri_enumerator;
1134 this->public.interface.create_ocsp_uri_enumerator = (enumerator_t* (*)(x509_t*))create_ocsp_uri_enumerator;
1135
1136 this->encoding = chunk_empty;
1137 this->encoding_hash = chunk_empty;
1138 this->public_key = NULL;
1139 this->subject = NULL;
1140 this->issuer = NULL;
1141 this->subjectAltNames = linked_list_create();
1142 this->crl_uris = linked_list_create();
1143 this->ocsp_uris = linked_list_create();
1144 this->subjectKeyID = chunk_empty;
1145 this->authKeyIdentifier = NULL;
1146 this->authKeySerialNumber = chunk_empty;
1147 this->flags = 0;
1148 this->ref = 1;
1149
1150 return this;
1151 }
1152
1153 /**
1154 * create an X.509 certificate from a chunk
1155 */
1156 static private_x509_cert_t *create_from_chunk(chunk_t chunk)
1157 {
1158 private_x509_cert_t *this = create_empty();
1159
1160 this->encoding = chunk;
1161 if (!parse_certificate(this))
1162 {
1163 destroy(this);
1164 return NULL;
1165 }
1166
1167 /* check if the certificate is self-signed */
1168 if (issued_by(this, &this->public.interface.interface))
1169 {
1170 this->flags |= X509_SELF_SIGNED;
1171 }
1172
1173 hasher_t *hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1174 if (hasher != NULL)
1175 {
1176 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
1177 hasher->destroy(hasher);
1178 }
1179 else
1180 {
1181 DBG1(" unable to create hash of certificate, SHA1 not supported");
1182 }
1183
1184 return this;
1185 }
1186
1187 /**
1188 * create an X.509 certificate from a file
1189 */
1190 static private_x509_cert_t *create_from_file(char *path)
1191 {
1192 bool pgp = FALSE;
1193 chunk_t chunk;
1194 private_x509_cert_t *this;
1195
1196 if (!pem_asn1_load_file(path, NULL, &chunk, &pgp))
1197 {
1198 return NULL;
1199 }
1200
1201 this = create_from_chunk(chunk);
1202
1203 if (this == NULL)
1204 {
1205 DBG1(" could not parse loaded certificate file '%s'",path);
1206 return NULL;
1207 }
1208 DBG1(" loaded certificate file '%s'", path);
1209 return this;
1210
1211 }
1212
1213 typedef struct private_builder_t private_builder_t;
1214 /**
1215 * Builder implementation for certificate loading
1216 */
1217 struct private_builder_t {
1218 /** implements the builder interface */
1219 builder_t public;
1220 /** loaded certificate */
1221 private_x509_cert_t *cert;
1222 /** additional flags to enforce */
1223 x509_flag_t flags;
1224 };
1225
1226 /**
1227 * Implementation of builder_t.build
1228 */
1229 static private_x509_cert_t *build(private_builder_t *this)
1230 {
1231 private_x509_cert_t *cert = this->cert;
1232 x509_flag_t flags = this->flags;
1233
1234 free(this);
1235 if (cert == NULL)
1236 {
1237 return NULL;
1238 }
1239 if ((flags & X509_CA) && !(cert->flags & X509_CA))
1240 {
1241 DBG1(" ca certificate must have ca basic constraint set, discarded");
1242 destroy(cert);
1243 return NULL;
1244 }
1245 cert->flags |= flags;
1246 return cert;
1247 }
1248
1249 /**
1250 * Implementation of builder_t.add
1251 */
1252 static void add(private_builder_t *this, builder_part_t part, ...)
1253 {
1254 va_list args;
1255 chunk_t chunk;
1256
1257 va_start(args, part);
1258 switch (part)
1259 {
1260 case BUILD_FROM_FILE:
1261 this->cert = create_from_file(va_arg(args, char*));
1262 break;
1263 case BUILD_BLOB_ASN1_DER:
1264 chunk = va_arg(args, chunk_t);
1265 this->cert = create_from_chunk(chunk_clone(chunk));
1266 break;
1267 case BUILD_X509_FLAG:
1268 this->flags = va_arg(args, x509_flag_t);
1269 break;
1270 default:
1271 /* abort if unsupported option */
1272 if (this->cert)
1273 {
1274 destroy(this->cert);
1275 }
1276 builder_cancel(&this->public);
1277 break;
1278 }
1279 va_end(args);
1280 }
1281
1282 /**
1283 * Builder construction function
1284 */
1285 builder_t *x509_cert_builder(certificate_type_t type)
1286 {
1287 private_builder_t *this;
1288
1289 if (type != CERT_X509)
1290 {
1291 return NULL;
1292 }
1293
1294 this = malloc_thing(private_builder_t);
1295
1296 this->cert = NULL;
1297 this->flags = 0;
1298 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
1299 this->public.build = (void*(*)(builder_t *this))build;
1300
1301 return &this->public;
1302 }
1303