support of cert payloads
[strongswan.git] / src / libstrongswan / crypto / x509.c
1 /**
2 * @file x509.c
3 *
4 * @brief Implementation of x509_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <gmp.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <string.h>
27
28 #include "x509.h"
29
30 #include <types.h>
31 #include <definitions.h>
32 #include <asn1/oid.h>
33 #include <asn1/asn1.h>
34 #include <asn1/pem.h>
35 #include <utils/logger_manager.h>
36 #include <utils/linked_list.h>
37 #include <utils/identification.h>
38
39 #define CERT_WARNING_INTERVAL 30 /* days */
40
41 static logger_t *logger;
42
43 /**
44 * Different kinds of generalNames
45 */
46 typedef enum generalNames_t generalNames_t;
47
48 enum generalNames_t {
49 GN_OTHER_NAME = 0,
50 GN_RFC822_NAME = 1,
51 GN_DNS_NAME = 2,
52 GN_X400_ADDRESS = 3,
53 GN_DIRECTORY_NAME = 4,
54 GN_EDI_PARTY_NAME = 5,
55 GN_URI = 6,
56 GN_IP_ADDRESS = 7,
57 GN_REGISTERED_ID = 8,
58 };
59
60 typedef struct private_x509_t private_x509_t;
61
62 /**
63 * Private data of a x509_t object.
64 */
65 struct private_x509_t {
66 /**
67 * Public interface for this certificate.
68 */
69 x509_t public;
70
71 /**
72 * Time when certificate was installed
73 */
74 time_t installed;
75
76 /**
77 * Time until certificate can be trusted
78 */
79 time_t until;
80
81 /**
82 * Certificate status
83 */
84 cert_status_t status;
85
86 /**
87 * X.509 Certificate in DER format
88 */
89 chunk_t certificate;
90
91 /**
92 * X.509 certificate body over which signature is computed
93 */
94 chunk_t tbsCertificate;
95
96 /**
97 * Version of the X.509 certificate
98 */
99 u_int version;
100
101 /**
102 * Serial number of the X.509 certificate
103 */
104 chunk_t serialNumber;
105
106 /**
107 * Signature algorithm
108 */
109 int sigAlg;
110
111 /**
112 * ID representing the certificate issuer
113 */
114 identification_t *issuer;
115
116 /**
117 * Start time of certificate validity
118 */
119 time_t notBefore;
120
121 /**
122 * End time of certificate validity
123 */
124 time_t notAfter;
125
126 /**
127 * ID representing the certificate subject
128 */
129 identification_t *subject;
130
131 /**
132 * List of identification_t's representing subjectAltNames
133 */
134 linked_list_t *subjectAltNames;
135
136 /**
137 * List of identification_t's representing crlDistributionPoints
138 */
139 linked_list_t *crlDistributionPoints;
140
141 /**
142 * Subject RSA public key, if subjectPublicKeyAlgorithm == RSA
143 */
144 rsa_public_key_t *public_key;
145
146 /**
147 * Subject Key Identifier
148 */
149 chunk_t subjectKeyID;
150
151 /**
152 * Authority Key Identifier
153 */
154 chunk_t authKeyID;
155
156 /**
157 * Authority Key Serial Number
158 */
159 chunk_t authKeySerialNumber;
160
161 /**
162 * CA basic constraints flag
163 */
164 bool isCA;
165
166 /**
167 * Signature algorithm (must be identical to sigAlg)
168 */
169 int algorithm;
170
171 /**
172 * Signature
173 */
174 chunk_t signature;
175
176 u_char authority_flags;
177 chunk_t subjectPublicKey;
178 bool isOcspSigner; /* ocsp */
179 chunk_t accessLocation; /* ocsp */
180 };
181
182 /**
183 * ASN.1 definition of generalName
184 */
185 static const asn1Object_t generalNameObjects[] = {
186 { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */
187 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */
188 { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */
189 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */
190 { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */
191 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
192 { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */
193 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
194 { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */
195 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
196 { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */
197 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
198 { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */
199 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */
200 { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */
201 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */
202 { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */
203 { 0, "end choice", ASN1_EOC, ASN1_END } /* 17 */
204 };
205 #define GN_OBJ_OTHER_NAME 0
206 #define GN_OBJ_RFC822_NAME 2
207 #define GN_OBJ_DNS_NAME 4
208 #define GN_OBJ_X400_ADDRESS 6
209 #define GN_OBJ_DIRECTORY_NAME 8
210 #define GN_OBJ_EDI_PARTY_NAME 10
211 #define GN_OBJ_URI 12
212 #define GN_OBJ_IP_ADDRESS 14
213 #define GN_OBJ_REGISTERED_ID 16
214 #define GN_OBJ_ROOF 18
215
216 /**
217 * ASN.1 definition of otherName
218 */
219 static const asn1Object_t otherNameObjects[] = {
220 {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */
221 {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY } /* 1 */
222 };
223 #define ON_OBJ_ID_TYPE 0
224 #define ON_OBJ_VALUE 1
225 #define ON_OBJ_ROOF 2
226 /**
227 * ASN.1 definition of a basicConstraints extension
228 */
229 static const asn1Object_t basicConstraintsObjects[] = {
230 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
231 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
232 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
233 { 1, "end opt", ASN1_EOC, ASN1_END } /* 3 */
234 };
235 #define BASIC_CONSTRAINTS_CA 1
236 #define BASIC_CONSTRAINTS_ROOF 4
237
238 /**
239 * ASN.1 definition of time
240 */
241 static const asn1Object_t timeObjects[] = {
242 { 0, "utcTime", ASN1_UTCTIME, ASN1_OPT|ASN1_BODY }, /* 0 */
243 { 0, "end opt", ASN1_EOC, ASN1_END }, /* 1 */
244 { 0, "generalizeTime",ASN1_GENERALIZEDTIME, ASN1_OPT|ASN1_BODY }, /* 2 */
245 { 0, "end opt", ASN1_EOC, ASN1_END } /* 3 */
246 };
247 #define TIME_UTC 0
248 #define TIME_GENERALIZED 2
249 #define TIME_ROOF 4
250
251 /**
252 * ASN.1 definition of a keyIdentifier
253 */
254 static const asn1Object_t keyIdentifierObjects[] = {
255 { 0, "keyIdentifier", ASN1_OCTET_STRING, ASN1_BODY } /* 0 */
256 };
257
258 /**
259 * ASN.1 definition of a authorityKeyIdentifier extension
260 */
261 static const asn1Object_t authorityKeyIdentifierObjects[] = {
262 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
263 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_OBJ }, /* 1 */
264 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
265 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
266 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
267 { 1, "authorityCertSerialNumber",ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
268 { 1, "end opt", ASN1_EOC, ASN1_END } /* 6 */
269 };
270 #define AUTH_KEY_ID_KEY_ID 1
271 #define AUTH_KEY_ID_CERT_ISSUER 3
272 #define AUTH_KEY_ID_CERT_SERIAL 5
273 #define AUTH_KEY_ID_ROOF 7
274
275 /**
276 * ASN.1 definition of a authorityInfoAccess extension
277 */
278 static const asn1Object_t authorityInfoAccessObjects[] = {
279 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
280 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
281 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
282 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
283 { 0, "end loop", ASN1_EOC, ASN1_END } /* 4 */
284 };
285 #define AUTH_INFO_ACCESS_METHOD 2
286 #define AUTH_INFO_ACCESS_LOCATION 3
287 #define AUTH_INFO_ACCESS_ROOF 5
288
289 /**
290 * ASN.1 definition of a extendedKeyUsage extension
291 */
292 static const asn1Object_t extendedKeyUsageObjects[] = {
293 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
294 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
295 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
296 };
297
298 #define EXT_KEY_USAGE_PURPOSE_ID 1
299 #define EXT_KEY_USAGE_ROOF 3
300
301 /**
302 * ASN.1 definition of generalNames
303 */
304 static const asn1Object_t generalNamesObjects[] = {
305 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
306 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
307 { 0, "end loop", ASN1_EOC, ASN1_END } /* 2 */
308 };
309 #define GENERAL_NAMES_GN 1
310 #define GENERAL_NAMES_ROOF 3
311
312
313 /**
314 * ASN.1 definition of crlDistributionPoints
315 */
316 static const asn1Object_t crlDistributionPointsObjects[] = {
317 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
318 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
319 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
320 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
321 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
322 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
323 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
324 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
325 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
326 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
327 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
328 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
329 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
330 };
331 #define CRL_DIST_POINTS_FULLNAME 3
332 #define CRL_DIST_POINTS_ROOF 13
333
334 /**
335 * ASN.1 definition of an X.509v3 x509
336 */
337 static const asn1Object_t certObjects[] = {
338 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
339 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
340 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
341 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
342 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
343 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
344 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
345 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
346 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
347 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
348 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
349 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_NONE }, /* 11 */
350 { 3, "algorithm", ASN1_EOC, ASN1_RAW }, /* 12 */
351 { 3, "subjectPublicKey", ASN1_BIT_STRING, ASN1_NONE }, /* 13 */
352 { 4, "RSAPublicKey", ASN1_SEQUENCE, ASN1_RAW }, /* 14 */
353 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 15 */
354 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 16 */
355 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 17 */
356 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 18 */
357 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 19 */
358 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 20 */
359 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 21 */
360 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 22 */
361 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 23 */
362 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 24 */
363 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 25 */
364 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 26 */
365 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 27 */
366 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY } /* 28 */
367 };
368 #define X509_OBJ_CERTIFICATE 0
369 #define X509_OBJ_TBS_CERTIFICATE 1
370 #define X509_OBJ_VERSION 3
371 #define X509_OBJ_SERIAL_NUMBER 4
372 #define X509_OBJ_SIG_ALG 5
373 #define X509_OBJ_ISSUER 6
374 #define X509_OBJ_NOT_BEFORE 8
375 #define X509_OBJ_NOT_AFTER 9
376 #define X509_OBJ_SUBJECT 10
377 #define X509_OBJ_SUBJECT_PUBLIC_KEY_ALGORITHM 12
378 #define X509_OBJ_SUBJECT_PUBLIC_KEY 13
379 #define X509_OBJ_RSA_PUBLIC_KEY 14
380 #define X509_OBJ_EXTN_ID 22
381 #define X509_OBJ_CRITICAL 23
382 #define X509_OBJ_EXTN_VALUE 24
383 #define X509_OBJ_ALGORITHM 27
384 #define X509_OBJ_SIGNATURE 28
385 #define X509_OBJ_ROOF 29
386
387
388 static u_char ASN1_subjectAltName_oid_str[] = {
389 0x06, 0x03, 0x55, 0x1D, 0x11
390 };
391
392 static const chunk_t ASN1_subjectAltName_oid = chunk_from_buf(ASN1_subjectAltName_oid_str);
393
394
395 /**
396 * compare two X.509 x509s by comparing their signatures
397 */
398 static bool equals(const private_x509_t *this, const private_x509_t *other)
399 {
400 return chunk_equals(this->signature, other->signature);
401 }
402
403 /**
404 * extracts the basicConstraints extension
405 */
406 static bool parse_basicConstraints(chunk_t blob, int level0)
407 {
408 asn1_ctx_t ctx;
409 chunk_t object;
410 u_int level;
411 int objectID = 0;
412 bool isCA = FALSE;
413
414 asn1_init(&ctx, blob, level0, FALSE);
415
416 while (objectID < BASIC_CONSTRAINTS_ROOF) {
417
418 if (!extract_object(basicConstraintsObjects, &objectID, &object,&level, &ctx))
419 {
420 break;
421 }
422 if (objectID == BASIC_CONSTRAINTS_CA)
423 {
424 isCA = object.len && *object.ptr;
425 logger->log(logger, CONTROL|LEVEL2, " %s", isCA ? "TRUE" : "FALSE");
426 }
427 objectID++;
428 }
429 return isCA;
430 }
431
432 /*
433 * extracts an otherName
434 */
435 static bool
436 parse_otherName(chunk_t blob, int level0)
437 {
438 asn1_ctx_t ctx;
439 chunk_t object;
440 int objectID = 0;
441 u_int level;
442 int oid = OID_UNKNOWN;
443
444 asn1_init(&ctx, blob, level0, FALSE);
445
446 while (objectID < ON_OBJ_ROOF)
447 {
448 if (!extract_object(otherNameObjects, &objectID, &object, &level, &ctx))
449 return FALSE;
450
451 switch (objectID)
452 {
453 case ON_OBJ_ID_TYPE:
454 oid = known_oid(object);
455 break;
456 case ON_OBJ_VALUE:
457 if (oid == OID_XMPP_ADDR)
458 {
459 if (!parse_asn1_simple_object(&object, ASN1_UTF8STRING, level + 1, "xmppAddr"))
460 return FALSE;
461 }
462 break;
463 default:
464 break;
465 }
466 objectID++;
467 }
468 return TRUE;
469 }
470
471 /*
472 * extracts a generalName
473 */
474 static identification_t *parse_generalName(chunk_t blob, int level0)
475 {
476 u_char buf[BUF_LEN];
477 asn1_ctx_t ctx;
478 chunk_t object;
479 int objectID = 0;
480 u_int level;
481
482 asn1_init(&ctx, blob, level0, FALSE);
483
484 while (objectID < GN_OBJ_ROOF)
485 {
486 id_type_t id_type = ID_ANY;
487
488 if (!extract_object(generalNameObjects, &objectID, &object, &level, &ctx))
489 return NULL;
490
491 switch (objectID)
492 {
493 case GN_OBJ_RFC822_NAME:
494 id_type = ID_RFC822_ADDR;
495 break;
496 case GN_OBJ_DNS_NAME:
497 id_type = ID_FQDN;
498 break;
499 case GN_OBJ_URI:
500 id_type = ID_DER_ASN1_GN_URI;
501 break;
502 case GN_OBJ_DIRECTORY_NAME:
503 id_type = ID_DER_ASN1_DN;
504 break;
505 case GN_OBJ_IP_ADDRESS:
506 id_type = ID_IPV4_ADDR;
507 break;
508 case GN_OBJ_OTHER_NAME:
509 if (!parse_otherName(object, level + 1))
510 return NULL;
511 break;
512 case GN_OBJ_X400_ADDRESS:
513 case GN_OBJ_EDI_PARTY_NAME:
514 case GN_OBJ_REGISTERED_ID:
515 break;
516 default:
517 break;
518 }
519
520 if (id_type != ID_ANY)
521 {
522 identification_t *gn = identification_create_from_encoding(id_type, object);
523 logger->log(logger, CONTROL|LEVEL2, " '%s'", gn->get_string(gn));
524 return gn;
525 }
526 objectID++;
527 }
528 return NULL;
529 }
530
531
532 /**
533 * extracts one or several GNs and puts them into a chained list
534 */
535 static void parse_generalNames(chunk_t blob, int level0, bool implicit, linked_list_t *list)
536 {
537 asn1_ctx_t ctx;
538 chunk_t object;
539 u_int level;
540 int objectID = 0;
541
542 asn1_init(&ctx, blob, level0, implicit);
543
544 while (objectID < GENERAL_NAMES_ROOF)
545 {
546 if (!extract_object(generalNamesObjects, &objectID, &object, &level, &ctx))
547 return;
548
549 if (objectID == GENERAL_NAMES_GN)
550 {
551 identification_t *gn = parse_generalName(object, level+1);
552
553 if (gn != NULL)
554 list->insert_last(list, (void *)gn);
555 }
556 objectID++;
557 }
558 return;
559 }
560
561 /**
562 * extracts and converts a UTCTIME or GENERALIZEDTIME object
563 */
564 time_t parse_time(chunk_t blob, int level0)
565 {
566 asn1_ctx_t ctx;
567 chunk_t object;
568 u_int level;
569 int objectID = 0;
570
571 asn1_init(&ctx, blob, level0, FALSE);
572
573 while (objectID < TIME_ROOF)
574 {
575 if (!extract_object(timeObjects, &objectID, &object, &level, &ctx))
576 return 0;
577
578 if (objectID == TIME_UTC || objectID == TIME_GENERALIZED)
579 {
580 return asn1totime(&object, (objectID == TIME_UTC)
581 ? ASN1_UTCTIME : ASN1_GENERALIZEDTIME);
582 }
583 objectID++;
584 }
585 return 0;
586 }
587
588 /**
589 * extracts a keyIdentifier
590 */
591 static chunk_t parse_keyIdentifier(chunk_t blob, int level0, bool implicit)
592 {
593 asn1_ctx_t ctx;
594 chunk_t object;
595 u_int level;
596 int objectID = 0;
597
598 asn1_init(&ctx, blob, level0, implicit);
599
600 extract_object(keyIdentifierObjects, &objectID, &object, &level, &ctx);
601 return object;
602 }
603
604 /**
605 * extracts an authoritykeyIdentifier
606 */
607 void parse_authorityKeyIdentifier(chunk_t blob, int level0 , chunk_t *authKeyID, chunk_t *authKeySerialNumber)
608 {
609 asn1_ctx_t ctx;
610 chunk_t object;
611 u_int level;
612 int objectID = 0;
613
614 asn1_init(&ctx, blob, level0, FALSE);
615 while (objectID < AUTH_KEY_ID_ROOF)
616 {
617 if (!extract_object(authorityKeyIdentifierObjects, &objectID, &object, &level, &ctx))
618 {
619 return;
620 }
621 switch (objectID)
622 {
623 case AUTH_KEY_ID_KEY_ID:
624 *authKeyID = parse_keyIdentifier(object, level+1, TRUE);
625 break;
626 case AUTH_KEY_ID_CERT_ISSUER:
627 {
628 /* TODO: parse_generalNames(object, level+1, TRUE); */
629 break;
630 }
631 case AUTH_KEY_ID_CERT_SERIAL:
632 *authKeySerialNumber = object;
633 break;
634 default:
635 break;
636 }
637 objectID++;
638 }
639 }
640
641 /**
642 * extracts an authorityInfoAcess location
643 */
644 static void parse_authorityInfoAccess(chunk_t blob, int level0, chunk_t *accessLocation)
645 {
646 asn1_ctx_t ctx;
647 chunk_t object;
648 u_int level;
649 int objectID = 0;
650
651 u_int accessMethod = OID_UNKNOWN;
652
653 asn1_init(&ctx, blob, level0, FALSE);
654 while (objectID < AUTH_INFO_ACCESS_ROOF)
655 {
656 if (!extract_object(authorityInfoAccessObjects, &objectID, &object, &level, &ctx))
657 {
658 return;
659 }
660 switch (objectID)
661 {
662 case AUTH_INFO_ACCESS_METHOD:
663 accessMethod = known_oid(object);
664 break;
665 case AUTH_INFO_ACCESS_LOCATION:
666 {
667 switch (accessMethod)
668 {
669 case OID_OCSP:
670 if (*object.ptr == ASN1_CONTEXT_S_6)
671 {
672 if (asn1_length(&object) == ASN1_INVALID_LENGTH)
673 return;
674 logger->log(logger, CONTROL|LEVEL2, " '%.*s'",(int)object.len, object.ptr);
675 /* only HTTP(S) URIs accepted */
676 if (strncasecmp(object.ptr, "http", 4) == 0)
677 {
678 *accessLocation = object;
679 return;
680 }
681 }
682 logger->log(logger, ERROR|LEVEL2, "ignoring OCSP InfoAccessLocation with unkown protocol");
683 break;
684 default:
685 /* unkown accessMethod, ignoring */
686 break;
687 }
688 break;
689 }
690 default:
691 break;
692 }
693 objectID++;
694 }
695 }
696
697 /**
698 * extracts extendedKeyUsage OIDs
699 */
700 static bool parse_extendedKeyUsage(chunk_t blob, int level0)
701 {
702 asn1_ctx_t ctx;
703 chunk_t object;
704 u_int level;
705 int objectID = 0;
706
707 asn1_init(&ctx, blob, level0, FALSE);
708 while (objectID < EXT_KEY_USAGE_ROOF)
709 {
710 if (!extract_object(extendedKeyUsageObjects, &objectID, &object, &level, &ctx))
711 {
712 return FALSE;
713 }
714 if (objectID == EXT_KEY_USAGE_PURPOSE_ID &&
715 known_oid(object) == OID_OCSP_SIGNING)
716 {
717 return TRUE;
718 }
719 objectID++;
720 }
721 return FALSE;
722 }
723
724 /**
725 * extracts one or several crlDistributionPoints and puts them into
726 * a chained list
727 */
728 static void parse_crlDistributionPoints(chunk_t blob, int level0, linked_list_t *list)
729 {
730 asn1_ctx_t ctx;
731 chunk_t object;
732 u_int level;
733 int objectID = 0;
734
735 asn1_init(&ctx, blob, level0, FALSE);
736 while (objectID < CRL_DIST_POINTS_ROOF)
737 {
738 if (!extract_object(crlDistributionPointsObjects, &objectID, &object, &level, &ctx))
739 {
740 return;
741 }
742 if (objectID == CRL_DIST_POINTS_FULLNAME)
743 {
744 /* append extracted generalNames to existing chained list */
745 parse_generalNames(object, level+1, TRUE, list);
746
747 }
748 objectID++;
749 }
750 }
751
752
753 /**
754 * Parses an X.509v3 certificate
755 */
756 bool parse_x509cert(chunk_t blob, u_int level0, private_x509_t *cert)
757 {
758 asn1_ctx_t ctx;
759 bool critical;
760 chunk_t object;
761 u_int level;
762 u_int extn_oid = OID_UNKNOWN;
763 int objectID = 0;
764
765 asn1_init(&ctx, blob, level0, FALSE);
766 while (objectID < X509_OBJ_ROOF)
767 {
768 if (!extract_object(certObjects, &objectID, &object, &level, &ctx))
769 {
770 return FALSE;
771 }
772 /* those objects which will parsed further need the next higher level */
773 level++;
774 switch (objectID) {
775 case X509_OBJ_CERTIFICATE:
776 cert->certificate = object;
777 break;
778 case X509_OBJ_TBS_CERTIFICATE:
779 cert->tbsCertificate = object;
780 break;
781 case X509_OBJ_VERSION:
782 cert->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
783 logger->log(logger, CONTROL|LEVEL2, " v%d", cert->version);
784 break;
785 case X509_OBJ_SERIAL_NUMBER:
786 cert->serialNumber = object;
787 break;
788 case X509_OBJ_SIG_ALG:
789 cert->sigAlg = parse_algorithmIdentifier(object, level, NULL);
790 break;
791 case X509_OBJ_ISSUER:
792 cert->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
793 logger->log(logger, CONTROL|LEVEL1, " '%s'", cert->issuer->get_string(cert->issuer));
794 break;
795 case X509_OBJ_NOT_BEFORE:
796 cert->notBefore = parse_time(object, level);
797 break;
798 case X509_OBJ_NOT_AFTER:
799 cert->notAfter = parse_time(object, level);
800 break;
801 case X509_OBJ_SUBJECT:
802 cert->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
803 logger->log(logger, CONTROL|LEVEL1, " '%s'", cert->subject->get_string(cert->subject));
804 break;
805 case X509_OBJ_SUBJECT_PUBLIC_KEY_ALGORITHM:
806 if (parse_algorithmIdentifier(object, level, NULL) != OID_RSA_ENCRYPTION)
807 {
808 logger->log(logger, ERROR|LEVEL1, " unsupported public key algorithm");
809 return FALSE;
810 }
811 break;
812 case X509_OBJ_SUBJECT_PUBLIC_KEY:
813 if (ctx.blobs[4].len > 0 && *ctx.blobs[4].ptr == 0x00)
814 {
815 /* skip initial bit string octet defining 0 unused bits */
816 ctx.blobs[4].ptr++; ctx.blobs[4].len--;
817 }
818 else
819 {
820 logger->log(logger, ERROR|LEVEL1, " invalid RSA public key format");
821 return FALSE;
822 }
823 break;
824 case X509_OBJ_RSA_PUBLIC_KEY:
825 cert->subjectPublicKey = object;
826 break;
827 case X509_OBJ_EXTN_ID:
828 extn_oid = known_oid(object);
829 break;
830 case X509_OBJ_CRITICAL:
831 critical = object.len && *object.ptr;
832 logger->log(logger, ERROR|LEVEL2, " %s", critical ? "TRUE" : "FALSE");
833 break;
834 case X509_OBJ_EXTN_VALUE:
835 {
836 switch (extn_oid) {
837 case OID_SUBJECT_KEY_ID:
838 cert->subjectKeyID = parse_keyIdentifier(object, level, FALSE);
839 break;
840 case OID_SUBJECT_ALT_NAME:
841 parse_generalNames(object, level, FALSE, cert->subjectAltNames);
842 break;
843 case OID_BASIC_CONSTRAINTS:
844 cert->isCA = parse_basicConstraints(object, level);
845 break;
846 case OID_CRL_DISTRIBUTION_POINTS:
847 parse_crlDistributionPoints(object, level, cert->crlDistributionPoints);
848 break;
849 case OID_AUTHORITY_KEY_ID:
850 parse_authorityKeyIdentifier(object, level , &cert->authKeyID, &cert->authKeySerialNumber);
851 break;
852 case OID_AUTHORITY_INFO_ACCESS:
853 parse_authorityInfoAccess(object, level, &cert->accessLocation);
854 break;
855 case OID_EXTENDED_KEY_USAGE:
856 cert->isOcspSigner = parse_extendedKeyUsage(object, level);
857 break;
858 case OID_NS_REVOCATION_URL:
859 case OID_NS_CA_REVOCATION_URL:
860 case OID_NS_CA_POLICY_URL:
861 case OID_NS_COMMENT:
862 if (!parse_asn1_simple_object(&object, ASN1_IA5STRING , level, oid_names[extn_oid].name))
863 return FALSE;
864 break;
865 default:
866 break;
867 }
868 break;
869 }
870 case X509_OBJ_ALGORITHM:
871 cert->algorithm = parse_algorithmIdentifier(object, level, NULL);
872 break;
873 case X509_OBJ_SIGNATURE:
874 cert->signature = object;
875 break;
876 default:
877 break;
878 }
879 objectID++;
880 }
881 time(&cert->installed);
882 return TRUE;
883 }
884
885 /**
886 * Implements x509_t.is_valid
887 */
888 static err_t is_valid(const private_x509_t *this, time_t *until)
889 {
890 char buf[TIMETOA_BUF];
891
892 time_t current_time = time(NULL);
893
894 timetoa(buf, BUF_LEN, &this->notBefore, TRUE);
895 logger->log(logger, CONTROL|LEVEL1, " not before : %s", buf);
896 timetoa(buf, BUF_LEN, &current_time, TRUE);
897 logger->log(logger, CONTROL|LEVEL1, " current time: %s", buf);
898 timetoa(buf, BUF_LEN, &this->notAfter, TRUE);
899 logger->log(logger, CONTROL|LEVEL1, " not after : %s", buf);
900
901 if (until != NULL
902 && (*until == UNDEFINED_TIME || this->notAfter < *until))
903 {
904 *until = this->notAfter;
905 }
906 if (current_time < this->notBefore)
907 return "is not valid yet";
908 if (current_time > this->notAfter)
909 return "has expired";
910 logger->log(logger, CONTROL|LEVEL1, " certificate is valid", buf);
911 return NULL;
912 }
913
914 /**
915 * Implements x509_t.is_ca
916 */
917 static bool is_ca(const private_x509_t *this)
918 {
919 return this->isCA;
920 }
921
922 /**
923 * Implements x509_t.is_self_signed
924 */
925 static bool is_self_signed(const private_x509_t *this)
926 {
927 return this->subject->equals(this->subject, this->issuer);
928 }
929
930 /**
931 * Implements x509_t.equals_subjectAltName
932 */
933 static bool equals_subjectAltName(const private_x509_t *this, identification_t *id)
934 {
935 bool found = FALSE;
936 iterator_t *iterator = this->subjectAltNames->create_iterator(this->subjectAltNames, TRUE);
937
938 while (iterator->has_next(iterator))
939 {
940 identification_t *subjectAltName;
941
942 iterator->current(iterator, (void**)&subjectAltName);
943 if (id->equals(id, subjectAltName))
944 {
945 found = TRUE;
946 break;
947 }
948 }
949 iterator->destroy(iterator);
950 return found;
951 }
952
953 /**
954 * Implements x509_t.is_issuer
955 */
956 static bool is_issuer(const private_x509_t *this, const private_x509_t *issuer)
957 {
958 return (this->authKeyID.ptr)
959 ? chunk_equals(this->authKeyID, issuer->subjectKeyID)
960 : (this->issuer->equals(this->issuer, issuer->subject)
961 && chunk_equals_or_null(this->authKeySerialNumber, issuer->serialNumber));
962 }
963
964 /**
965 * Implements x509_t.get_certificate
966 */
967 static chunk_t get_certificate(const private_x509_t *this)
968 {
969 return this->certificate;
970 }
971
972 /**
973 * Implements x509_t.get_public_key
974 */
975 static rsa_public_key_t *get_public_key(const private_x509_t *this)
976 {
977 return this->public_key;
978 }
979
980 /**
981 * Implements x509_t.get_serialNumber
982 */
983 static chunk_t get_serialNumber(const private_x509_t *this)
984 {
985 return this->serialNumber;
986 }
987
988 /**
989 * Implements x509_t.get_subjectKeyID
990 */
991 static chunk_t get_subjectKeyID(const private_x509_t *this)
992 {
993 return this->subjectKeyID;
994 }
995
996 /**
997 * Implements x509_t.get_issuer
998 */
999 static identification_t *get_issuer(const private_x509_t *this)
1000 {
1001 return this->issuer;
1002 }
1003
1004 /**
1005 * Implements x509_t.get_subject
1006 */
1007 static identification_t *get_subject(const private_x509_t *this)
1008 {
1009 return this->subject;
1010 }
1011
1012 /**
1013 * Implements x509_t.set_until
1014 */
1015 static void set_until(private_x509_t *this, time_t until)
1016 {
1017 this->until = until;
1018 }
1019
1020 /**
1021 * Implements x509_t.get_until
1022 */
1023 static time_t get_until(const private_x509_t *this)
1024 {
1025 return this->until;
1026 }
1027
1028 /**
1029 * Implements x509_t.set_status
1030 */
1031 static void set_status(private_x509_t *this, cert_status_t status)
1032 {
1033 this->status = status;
1034 }
1035
1036 /**
1037 * Implements x509_t.get_status
1038 */
1039 static cert_status_t get_status(const private_x509_t *this)
1040 {
1041 return this->status;
1042 }
1043
1044 /**
1045 * Implements x509_t.verify
1046 */
1047 static bool verify(const private_x509_t *this, const rsa_public_key_t *signer)
1048 {
1049 return signer->verify_emsa_pkcs1_signature(signer, this->tbsCertificate, this->signature);
1050 }
1051
1052 /**
1053 * destroy
1054 */
1055 static void destroy(private_x509_t *this)
1056 {
1057 identification_t *id;
1058 while (this->subjectAltNames->remove_last(this->subjectAltNames, (void**)&id) == SUCCESS)
1059 {
1060 id->destroy(id);
1061 }
1062 this->subjectAltNames->destroy(this->subjectAltNames);
1063
1064 while (this->crlDistributionPoints->remove_last(this->crlDistributionPoints, (void**)&id) == SUCCESS)
1065 {
1066 id->destroy(id);
1067 }
1068 this->crlDistributionPoints->destroy(this->crlDistributionPoints);
1069
1070 if (this->issuer)
1071 this->issuer->destroy(this->issuer);
1072
1073 if (this->subject)
1074 this->subject->destroy(this->subject);
1075
1076 if (this->public_key)
1077 this->public_key->destroy(this->public_key);
1078
1079 free(this->certificate.ptr);
1080 free(this);
1081 }
1082
1083 /**
1084 * checks if the expiration date has been reached and warns during the
1085 * warning_interval of the imminent expiration.
1086 * strict=TRUE declares a fatal error, strict=FALSE issues a warning upon expiry.
1087 */
1088 char* check_expiry(time_t expiration_date, int warning_interval, bool strict)
1089 {
1090 time_t now;
1091 int time_left;
1092
1093 if (expiration_date == UNDEFINED_TIME)
1094 return "ok (expires never)";
1095
1096 time_left = (expiration_date - time(NULL));
1097 if (time_left < 0)
1098 return strict? "fatal (expired)" : "warning (expired)";
1099
1100 {
1101 static char buf[35]; /* temporary storage */
1102 const char* unit = "second";
1103
1104 if (time_left > 86400*warning_interval)
1105 return "ok";
1106
1107 if (time_left > 172800)
1108 {
1109 time_left /= 86400;
1110 unit = "day";
1111 }
1112 else if (time_left > 7200)
1113 {
1114 time_left /= 3600;
1115 unit = "hour";
1116 }
1117 else if (time_left > 120)
1118 {
1119 time_left /= 60;
1120 unit = "minute";
1121 }
1122 snprintf(buf, sizeof(buf), "warning (expires in %d %s%s)", time_left, unit, (time_left == 1)?"":"s");
1123 }
1124 }
1125
1126 /**
1127 * log certificate
1128 */
1129 static void log_certificate(const private_x509_t *this, logger_t *logger, bool utc, bool has_key)
1130 {
1131 identification_t *subject = this->subject;
1132 identification_t *issuer = this->issuer;
1133 rsa_public_key_t *pubkey = this->public_key;
1134
1135 char buf[BUF_LEN];
1136 char time_buf[TIMETOA_BUF];
1137
1138 /* determine the current time */
1139 time_t now = time(NULL);
1140
1141 timetoa(time_buf, TIMETOA_BUF, &this->installed, utc);
1142 logger->log(logger, CONTROL, "%s", time_buf);
1143 logger->log(logger, CONTROL, " subject: '%s'", subject->get_string(subject));
1144 logger->log(logger, CONTROL, " issuer: '%s'", issuer->get_string(issuer));
1145
1146 chunk_to_hex(buf, BUF_LEN, this->serialNumber);
1147 logger->log(logger, CONTROL, " serial: %s", buf);
1148
1149 timetoa(time_buf, TIMETOA_BUF, &this->notBefore, utc);
1150 logger->log(logger, CONTROL, " validity: not before %s %s", time_buf,
1151 (this->notBefore < now)? "ok":"fatal (not valid yet)");
1152
1153 timetoa(time_buf, TIMETOA_BUF, &this->notAfter, utc);
1154 logger->log(logger, CONTROL, " not after %s %s", time_buf,
1155 check_expiry(this->notAfter, CERT_WARNING_INTERVAL, TRUE));
1156
1157 timetoa(time_buf, TIMETOA_BUF, &this->until, utc);
1158 switch (this->status)
1159 {
1160 case CERT_GOOD:
1161 snprintf(buf, BUF_LEN, " until %s", time_buf);
1162 break;
1163 case CERT_REVOKED:
1164 snprintf(buf, BUF_LEN, " on %s", time_buf);
1165 break;
1166 case CERT_UNKNOWN:
1167 case CERT_UNDEFINED:
1168 case CERT_UNTRUSTED:
1169 default:
1170 *buf = '\0';
1171 }
1172 logger->log(logger, CONTROL, " pubkey: RSA %d bits%s, status %s%s",
1173 BITS_PER_BYTE * pubkey->get_keysize(pubkey),
1174 has_key? ", has private key":"",
1175 enum_name(&cert_status_names, this->status), buf);
1176
1177 chunk_to_hex(buf, BUF_LEN, pubkey->get_keyid(pubkey));
1178 logger->log(logger, CONTROL, " keyid: %s", buf);
1179
1180 if (this->subjectKeyID.ptr != NULL)
1181 {
1182 chunk_to_hex(buf, BUF_LEN, this->subjectKeyID);
1183 logger->log(logger, CONTROL, " subjkey: %s", buf);
1184 }
1185 if (this->authKeyID.ptr != NULL)
1186 {
1187 chunk_to_hex(buf, BUF_LEN, this->authKeyID);
1188 logger->log(logger, CONTROL, " authkey: %s", buf);
1189 }
1190 if (this->authKeySerialNumber.ptr != NULL)
1191 {
1192 chunk_to_hex(buf, BUF_LEN, this->authKeySerialNumber);
1193 logger->log(logger, CONTROL, " aserial: %s", buf);
1194 }
1195 }
1196
1197 /*
1198 * Described in header.
1199 */
1200 x509_t *x509_create_from_chunk(chunk_t chunk)
1201 {
1202 private_x509_t *this = malloc_thing(private_x509_t);
1203
1204 /* initialize */
1205 this->subjectPublicKey = CHUNK_INITIALIZER;
1206 this->public_key = NULL;
1207 this->subject = NULL;
1208 this->issuer = NULL;
1209 this->subjectAltNames = linked_list_create();
1210 this->crlDistributionPoints = linked_list_create();
1211 this->subjectKeyID = CHUNK_INITIALIZER;
1212 this->authKeyID = CHUNK_INITIALIZER;
1213 this->authKeySerialNumber = CHUNK_INITIALIZER;
1214
1215 /* public functions */
1216 this->public.equals = (bool (*) (const x509_t*,const x509_t*))equals;
1217 this->public.equals_subjectAltName = (bool (*) (const x509_t*,identification_t*))equals_subjectAltName;
1218 this->public.is_issuer = (bool (*) (const x509_t*,const x509_t*))is_issuer;
1219 this->public.is_valid = (err_t (*) (const x509_t*,time_t*))is_valid;
1220 this->public.is_ca = (bool (*) (const x509_t*))is_ca;
1221 this->public.is_self_signed = (bool (*) (const x509_t*))is_self_signed;
1222 this->public.get_certificate = (chunk_t (*) (const x509_t*))get_certificate;
1223 this->public.get_public_key = (rsa_public_key_t* (*) (const x509_t*))get_public_key;
1224 this->public.get_serialNumber = (chunk_t (*) (const x509_t*))get_serialNumber;
1225 this->public.get_subjectKeyID = (chunk_t (*) (const x509_t*))get_subjectKeyID;
1226 this->public.get_issuer = (identification_t* (*) (const x509_t*))get_issuer;
1227 this->public.get_subject = (identification_t* (*) (const x509_t*))get_subject;
1228 this->public.set_until = (void (*) (x509_t*,time_t))set_until;
1229 this->public.get_until = (time_t (*) (const x509_t*))get_until;
1230 this->public.set_status = (void (*) (x509_t*,cert_status_t))set_status;
1231 this->public.get_status = (cert_status_t (*) (const x509_t*))get_status;
1232 this->public.verify = (bool (*) (const x509_t*,const rsa_public_key_t*))verify;
1233 this->public.destroy = (void (*) (x509_t*))destroy;
1234 this->public.log_certificate = (void (*) (const x509_t*,logger_t*,bool,bool))log_certificate;
1235
1236 /* we do not use a per-instance logger right now, since its not always accessible */
1237 logger = logger_manager->get_logger(logger_manager, ASN1);
1238
1239 if (!parse_x509cert(chunk, 0, this))
1240 {
1241 destroy(this);
1242 return NULL;
1243 }
1244
1245 /* extract public key from certificate */
1246 this->public_key = rsa_public_key_create_from_chunk(this->subjectPublicKey);
1247 if (this->public_key == NULL)
1248 {
1249 destroy(this);
1250 return NULL;
1251 }
1252 /* set trusted lifetime of public key to notAfter */
1253 this->status = is_self_signed(this)? CERT_GOOD:CERT_UNDEFINED;
1254 this->until = this->notAfter;
1255 return &this->public;
1256 }
1257
1258 /*
1259 * Described in header.
1260 */
1261 x509_t *x509_create_from_file(const char *filename, const char *label)
1262 {
1263 bool pgp = FALSE;
1264 chunk_t chunk = CHUNK_INITIALIZER;
1265 x509_t *cert = NULL;
1266
1267 if (!pem_asn1_load_file(filename, "", label, &chunk, &pgp))
1268 return NULL;
1269
1270 cert = x509_create_from_chunk(chunk);
1271
1272 if (cert == NULL)
1273 free(chunk.ptr);
1274 return cert;
1275 }