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