moved definition of generalNames_t to identification.h; initialized subjectKeyID...
[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 <asn1/oid.h>
31 #include <asn1/asn1.h>
32 #include <asn1/pem.h>
33 #include <utils/logger_manager.h>
34 #include <utils/linked_list.h>
35 #include <utils/identification.h>
36
37 #define BUF_LEN 512
38 #define BITS_PER_BYTE 8
39 #define RSA_MIN_OCTETS (1024 / BITS_PER_BYTE)
40 #define RSA_MIN_OCTETS_UGH "RSA modulus too small for security: less than 1024 bits"
41 #define RSA_MAX_OCTETS (8192 / BITS_PER_BYTE)
42 #define RSA_MAX_OCTETS_UGH "RSA modulus too large: more than 8192 bits"
43
44 logger_t *logger;
45
46 typedef struct generalName_t generalName_t;
47
48 /**
49 * A generalName, chainable in a list
50 */
51 struct generalName_t {
52 generalName_t *next;
53 generalNames_t kind;
54 chunk_t name;
55 };
56
57 typedef struct private_x509_t private_x509_t;
58
59 /**
60 * Private data of a x509_t object.
61 */
62 struct private_x509_t {
63 /**
64 * Public interface for this certificate.
65 */
66 x509_t public;
67
68 /**
69 * Time when certificate was installed
70 */
71 time_t installed;
72
73 /**
74 * X.509 Certificate in DER format
75 */
76 chunk_t certificate;
77
78 /**
79 * Version of the X.509 certificate
80 */
81 u_int version;
82
83 /**
84 * Serial number of the X.509 certificate
85 */
86 chunk_t serialNumber;
87
88 /**
89 * ID representing the certificate issuer
90 */
91 identification_t *issuer;
92
93 /**
94 * Start time of certificate validity
95 */
96 time_t notBefore;
97
98 /**
99 * End time of certificate validity
100 */
101 time_t notAfter;
102
103 /**
104 * ID representing the certificate subject
105 */
106 identification_t *subject;
107
108 /**
109 * List of identification_t's representing subjectAltNames
110 */
111 linked_list_t *subjectAltNames;
112
113 /**
114 * List of identification_t's representing issuerAltNames
115 */
116 linked_list_t *issuerAltNames;
117
118 /**
119 * List of identification_t's representing crlDistributionPoints
120 */
121 linked_list_t *crlDistributionPoints;
122
123 /**
124 * Subject RSA public key, if subjectPublicKeyAlgorithm == RSA
125 */
126 rsa_public_key_t *public_key;
127
128 /**
129 * Subject Key Identifier
130 */
131 chunk_t subjectKeyID;
132
133 /**
134 * Authority Key Identifier
135 */
136 chunk_t authKeyID;
137
138 /**
139 * Authority Key Serial Number
140 */
141 chunk_t authKeySerialNumber;
142
143 u_char authority_flags;
144 chunk_t tbsCertificate;
145 /* signature */
146 int sigAlg;
147 chunk_t subjectPublicKey;
148 bool isCA;
149 bool isOcspSigner; /* ocsp */
150 chunk_t accessLocation; /* ocsp */
151 /* signatureAlgorithm */
152 int algorithm;
153 chunk_t signature;
154 };
155
156 /**
157 * ASN.1 definition of a basicConstraints extension
158 */
159 static const asn1Object_t basicConstraintsObjects[] = {
160 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
161 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
162 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
163 { 1, "end opt", ASN1_EOC, ASN1_END } /* 3 */
164 };
165 #define BASIC_CONSTRAINTS_CA 1
166 #define BASIC_CONSTRAINTS_ROOF 4
167
168 /**
169 * ASN.1 definition of time
170 */
171 static const asn1Object_t timeObjects[] = {
172 { 0, "utcTime", ASN1_UTCTIME, ASN1_OPT|ASN1_BODY }, /* 0 */
173 { 0, "end opt", ASN1_EOC, ASN1_END }, /* 1 */
174 { 0, "generalizeTime",ASN1_GENERALIZEDTIME, ASN1_OPT|ASN1_BODY }, /* 2 */
175 { 0, "end opt", ASN1_EOC, ASN1_END } /* 3 */
176 };
177 #define TIME_UTC 0
178 #define TIME_GENERALIZED 2
179 #define TIME_ROOF 4
180
181 /**
182 * ASN.1 definition of a keyIdentifier
183 */
184 static const asn1Object_t keyIdentifierObjects[] = {
185 { 0, "keyIdentifier", ASN1_OCTET_STRING, ASN1_BODY } /* 0 */
186 };
187
188 /**
189 * ASN.1 definition of a authorityKeyIdentifier extension
190 */
191 static const asn1Object_t authorityKeyIdentifierObjects[] = {
192 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
193 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_OBJ }, /* 1 */
194 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
195 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
196 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
197 { 1, "authorityCertSerialNumber",ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
198 { 1, "end opt", ASN1_EOC, ASN1_END } /* 6 */
199 };
200 #define AUTH_KEY_ID_KEY_ID 1
201 #define AUTH_KEY_ID_CERT_ISSUER 3
202 #define AUTH_KEY_ID_CERT_SERIAL 5
203 #define AUTH_KEY_ID_ROOF 7
204
205 /**
206 * ASN.1 definition of a authorityInfoAccess extension
207 */
208 static const asn1Object_t authorityInfoAccessObjects[] = {
209 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
210 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
211 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
212 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
213 { 0, "end loop", ASN1_EOC, ASN1_END } /* 4 */
214 };
215 #define AUTH_INFO_ACCESS_METHOD 2
216 #define AUTH_INFO_ACCESS_LOCATION 3
217 #define AUTH_INFO_ACCESS_ROOF 5
218
219 /**
220 * ASN.1 definition of a extendedKeyUsage extension
221 */
222 static const asn1Object_t extendedKeyUsageObjects[] = {
223 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
224 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
225 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
226 };
227
228 #define EXT_KEY_USAGE_PURPOSE_ID 1
229 #define EXT_KEY_USAGE_ROOF 3
230
231 /**
232 * ASN.1 definition of generalNames
233 */
234 static const asn1Object_t generalNamesObjects[] = {
235 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
236 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
237 { 0, "end loop", ASN1_EOC, ASN1_END } /* 2 */
238 };
239 #define GENERAL_NAMES_GN 1
240 #define GENERAL_NAMES_ROOF 3
241
242
243 /**
244 * ASN.1 definition of crlDistributionPoints
245 */
246 static const asn1Object_t crlDistributionPointsObjects[] = {
247 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
248 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
249 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
250 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
251 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
252 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
253 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
254 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
255 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
256 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
257 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
258 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
259 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
260 };
261 #define CRL_DIST_POINTS_FULLNAME 3
262 #define CRL_DIST_POINTS_ROOF 13
263
264 /**
265 * ASN.1 definition of an X.509v3 x509
266 */
267 static const asn1Object_t certObjects[] = {
268 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
269 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
270 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
271 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
272 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
273 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
274 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
275 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
276 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
277 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
278 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
279 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_NONE }, /* 11 */
280 { 3, "algorithm", ASN1_EOC, ASN1_RAW }, /* 12 */
281 { 3, "subjectPublicKey", ASN1_BIT_STRING, ASN1_NONE }, /* 13 */
282 { 4, "RSAPublicKey", ASN1_SEQUENCE, ASN1_RAW }, /* 14 */
283 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 15 */
284 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 16 */
285 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 17 */
286 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 18 */
287 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 19 */
288 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 20 */
289 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 21 */
290 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 22 */
291 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 23 */
292 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 24 */
293 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 25 */
294 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 26 */
295 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 27 */
296 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY } /* 28 */
297 };
298 #define X509_OBJ_CERTIFICATE 0
299 #define X509_OBJ_TBS_CERTIFICATE 1
300 #define X509_OBJ_VERSION 3
301 #define X509_OBJ_SERIAL_NUMBER 4
302 #define X509_OBJ_SIG_ALG 5
303 #define X509_OBJ_ISSUER 6
304 #define X509_OBJ_NOT_BEFORE 8
305 #define X509_OBJ_NOT_AFTER 9
306 #define X509_OBJ_SUBJECT 10
307 #define X509_OBJ_SUBJECT_PUBLIC_KEY_ALGORITHM 12
308 #define X509_OBJ_SUBJECT_PUBLIC_KEY 13
309 #define X509_OBJ_RSA_PUBLIC_KEY 14
310 #define X509_OBJ_EXTN_ID 22
311 #define X509_OBJ_CRITICAL 23
312 #define X509_OBJ_EXTN_VALUE 24
313 #define X509_OBJ_ALGORITHM 27
314 #define X509_OBJ_SIGNATURE 28
315 #define X509_OBJ_ROOF 29
316
317
318 static u_char ASN1_subjectAltName_oid_str[] = {
319 0x06, 0x03, 0x55, 0x1D, 0x11
320 };
321
322 static const chunk_t ASN1_subjectAltName_oid = chunk_from_buf(ASN1_subjectAltName_oid_str);
323
324
325 /**
326 * compare two X.509 x509s by comparing their signatures
327 */
328 static bool equals(private_x509_t *this, private_x509_t *other)
329 {
330 return chunk_equals(this->signature, other->signature);
331 }
332
333 /**
334 * encode a linked list of subjectAltNames
335 */
336 chunk_t build_subjectAltNames(generalName_t *subjectAltNames)
337 {
338 u_char *pos;
339 chunk_t names;
340 size_t len = 0;
341 generalName_t *gn = subjectAltNames;
342
343 /* compute the total size of the ASN.1 attributes object */
344 while (gn != NULL)
345 {
346 len += gn->name.len;
347 gn = gn->next;
348 }
349
350 pos = build_asn1_object(&names, ASN1_SEQUENCE, len);
351
352 gn = subjectAltNames;
353 while (gn != NULL)
354 {
355 memcpy(pos, gn->name.ptr, gn->name.len);
356 pos += gn->name.len;
357 gn = gn->next;
358 }
359
360 return asn1_wrap(ASN1_SEQUENCE, "cm",
361 ASN1_subjectAltName_oid,
362 asn1_wrap(ASN1_OCTET_STRING, "m", names)
363 );
364 }
365
366 /**
367 * free the dynamic memory used to store generalNames
368 */
369 void free_generalNames(generalName_t* gn, bool free_name)
370 {
371 while (gn != NULL)
372 {
373 generalName_t *gn_top = gn;
374 if (free_name)
375 {
376 free(gn->name.ptr);
377 }
378 gn = gn->next;
379 free(gn_top);
380 }
381 }
382
383 /**
384 * extracts the basicConstraints extension
385 */
386 static bool parse_basicConstraints(chunk_t blob, int level0)
387 {
388 asn1_ctx_t ctx;
389 chunk_t object;
390 u_int level;
391 int objectID = 0;
392 bool isCA = FALSE;
393
394 asn1_init(&ctx, blob, level0, FALSE);
395
396 while (objectID < BASIC_CONSTRAINTS_ROOF) {
397
398 if (!extract_object(basicConstraintsObjects, &objectID, &object,&level, &ctx))
399 {
400 break;
401 }
402 if (objectID == BASIC_CONSTRAINTS_CA)
403 {
404 isCA = object.len && *object.ptr;
405 logger->log(logger, RAW|LEVEL1, " %s", isCA ? "TRUE" : "FALSE");
406 }
407 objectID++;
408 }
409 return isCA;
410 }
411
412 /**
413 * extracts one or several GNs and puts them into a chained list
414 */
415 static void parse_generalNames(chunk_t blob, int level0, bool implicit, linked_list_t *list)
416 {
417 asn1_ctx_t ctx;
418 chunk_t object;
419 u_int level;
420 int objectID = 0;
421
422 asn1_init(&ctx, blob, level0, implicit);
423
424 while (objectID < GENERAL_NAMES_ROOF)
425 {
426 if (!extract_object(generalNamesObjects, &objectID, &object, &level, &ctx))
427 return;
428
429 if (objectID == GENERAL_NAMES_GN)
430 {
431 list->insert_last(list, identification_create_from_encoding(ID_DER_ASN1_GN, object));
432 }
433 objectID++;
434 }
435 return;
436 }
437
438 /**
439 * extracts and converts a UTCTIME or GENERALIZEDTIME object
440 */
441 time_t parse_time(chunk_t blob, int level0)
442 {
443 asn1_ctx_t ctx;
444 chunk_t object;
445 u_int level;
446 int objectID = 0;
447
448 asn1_init(&ctx, blob, level0, FALSE);
449
450 while (objectID < TIME_ROOF)
451 {
452 if (!extract_object(timeObjects, &objectID, &object, &level, &ctx))
453 return 0;
454
455 if (objectID == TIME_UTC || objectID == TIME_GENERALIZED)
456 {
457 return asn1totime(&object, (objectID == TIME_UTC)
458 ? ASN1_UTCTIME : ASN1_GENERALIZEDTIME);
459 }
460 objectID++;
461 }
462 return 0;
463 }
464
465 /**
466 * extracts a keyIdentifier
467 */
468 static chunk_t parse_keyIdentifier(chunk_t blob, int level0, bool implicit)
469 {
470 asn1_ctx_t ctx;
471 chunk_t object;
472 u_int level;
473 int objectID = 0;
474
475 asn1_init(&ctx, blob, level0, implicit);
476
477 extract_object(keyIdentifierObjects, &objectID, &object, &level, &ctx);
478 return object;
479 }
480
481 /**
482 * extracts an authoritykeyIdentifier
483 */
484 void parse_authorityKeyIdentifier(chunk_t blob, int level0 , chunk_t *authKeyID, chunk_t *authKeySerialNumber)
485 {
486 asn1_ctx_t ctx;
487 chunk_t object;
488 u_int level;
489 int objectID = 0;
490
491 asn1_init(&ctx, blob, level0, FALSE);
492 while (objectID < AUTH_KEY_ID_ROOF)
493 {
494 if (!extract_object(authorityKeyIdentifierObjects, &objectID, &object, &level, &ctx))
495 {
496 return;
497 }
498 switch (objectID)
499 {
500 case AUTH_KEY_ID_KEY_ID:
501 *authKeyID = parse_keyIdentifier(object, level+1, TRUE);
502 break;
503 case AUTH_KEY_ID_CERT_ISSUER:
504 {
505 /* TODO: parse_generalNames(object, level+1, TRUE); */
506 break;
507 }
508 case AUTH_KEY_ID_CERT_SERIAL:
509 *authKeySerialNumber = object;
510 break;
511 default:
512 break;
513 }
514 objectID++;
515 }
516 }
517
518 /**
519 * extracts an authorityInfoAcess location
520 */
521 static void parse_authorityInfoAccess(chunk_t blob, int level0, chunk_t *accessLocation)
522 {
523 asn1_ctx_t ctx;
524 chunk_t object;
525 u_int level;
526 int objectID = 0;
527
528 u_int accessMethod = OID_UNKNOWN;
529
530 asn1_init(&ctx, blob, level0, FALSE);
531 while (objectID < AUTH_INFO_ACCESS_ROOF)
532 {
533 if (!extract_object(authorityInfoAccessObjects, &objectID, &object, &level, &ctx))
534 {
535 return;
536 }
537 switch (objectID)
538 {
539 case AUTH_INFO_ACCESS_METHOD:
540 accessMethod = known_oid(object);
541 break;
542 case AUTH_INFO_ACCESS_LOCATION:
543 {
544 switch (accessMethod)
545 {
546 case OID_OCSP:
547 if (*object.ptr == ASN1_CONTEXT_S_6)
548 {
549 if (asn1_length(&object) == ASN1_INVALID_LENGTH)
550 {
551 return;
552 }
553 logger->log(logger, RAW|LEVEL1, " '%.*s'",(int)object.len, object.ptr);
554 /* only HTTP(S) URIs accepted */
555 if (strncasecmp(object.ptr, "http", 4) == 0)
556 {
557 *accessLocation = object;
558 return;
559 }
560 }
561 logger->log(logger, ERROR|LEVEL2, "ignoring OCSP InfoAccessLocation with unkown protocol");
562 break;
563 default:
564 /* unkown accessMethod, ignoring */
565 break;
566 }
567 break;
568 }
569 default:
570 break;
571 }
572 objectID++;
573 }
574 }
575
576 /**
577 * extracts extendedKeyUsage OIDs
578 */
579 static bool parse_extendedKeyUsage(chunk_t blob, int level0)
580 {
581 asn1_ctx_t ctx;
582 chunk_t object;
583 u_int level;
584 int objectID = 0;
585
586 asn1_init(&ctx, blob, level0, FALSE);
587 while (objectID < EXT_KEY_USAGE_ROOF)
588 {
589 if (!extract_object(extendedKeyUsageObjects, &objectID, &object, &level, &ctx))
590 {
591 return FALSE;
592 }
593 if (objectID == EXT_KEY_USAGE_PURPOSE_ID &&
594 known_oid(object) == OID_OCSP_SIGNING)
595 {
596 return TRUE;
597 }
598 objectID++;
599 }
600 return FALSE;
601 }
602
603 /**
604 * extracts one or several crlDistributionPoints and puts them into
605 * a chained list
606 */
607 static void parse_crlDistributionPoints(chunk_t blob, int level0, linked_list_t *list)
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 < CRL_DIST_POINTS_ROOF)
616 {
617 if (!extract_object(crlDistributionPointsObjects, &objectID, &object, &level, &ctx))
618 {
619 return;
620 }
621 if (objectID == CRL_DIST_POINTS_FULLNAME)
622 {
623 /* append extracted generalNames to existing chained list */
624 parse_generalNames(object, level+1, TRUE, list);
625
626 }
627 objectID++;
628 }
629 }
630
631
632 /**
633 * Parses an X.509v3 x509
634 */
635 bool parse_x509cert(chunk_t blob, u_int level0, private_x509_t *cert)
636 {
637 asn1_ctx_t ctx;
638 bool critical;
639 chunk_t object;
640 u_int level;
641 u_int extn_oid = OID_UNKNOWN;
642 int objectID = 0;
643
644 asn1_init(&ctx, blob, level0, FALSE);
645 while (objectID < X509_OBJ_ROOF)
646 {
647 if (!extract_object(certObjects, &objectID, &object, &level, &ctx))
648 {
649 return FALSE;
650 }
651 /* those objects which will parsed further need the next higher level */
652 level++;
653 switch (objectID) {
654 case X509_OBJ_CERTIFICATE:
655 cert->certificate = object;
656 break;
657 case X509_OBJ_TBS_CERTIFICATE:
658 cert->tbsCertificate = object;
659 break;
660 case X509_OBJ_VERSION:
661 cert->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
662 logger->log(logger, RAW|LEVEL1, " v%d", cert->version);
663 break;
664 case X509_OBJ_SERIAL_NUMBER:
665 cert->serialNumber = object;
666 break;
667 case X509_OBJ_SIG_ALG:
668 cert->sigAlg = parse_algorithmIdentifier(object, level, NULL);
669 break;
670 case X509_OBJ_ISSUER:
671 cert->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
672 break;
673 case X509_OBJ_NOT_BEFORE:
674 cert->notBefore = parse_time(object, level);
675 break;
676 case X509_OBJ_NOT_AFTER:
677 cert->notAfter = parse_time(object, level);
678 break;
679 case X509_OBJ_SUBJECT:
680 cert->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
681 break;
682 case X509_OBJ_SUBJECT_PUBLIC_KEY_ALGORITHM:
683 if (parse_algorithmIdentifier(object, level, NULL) != OID_RSA_ENCRYPTION)
684 {
685 logger->log(logger, ERROR|LEVEL1, " unsupported public key algorithm");
686 return FALSE;
687 }
688 break;
689 case X509_OBJ_SUBJECT_PUBLIC_KEY:
690 if (ctx.blobs[4].len > 0 && *ctx.blobs[4].ptr == 0x00)
691 {
692 /* skip initial bit string octet defining 0 unused bits */
693 ctx.blobs[4].ptr++; ctx.blobs[4].len--;
694 }
695 else
696 {
697 logger->log(logger, ERROR|LEVEL1, " invalid RSA public key format");
698 return FALSE;
699 }
700 break;
701 case X509_OBJ_RSA_PUBLIC_KEY:
702 cert->subjectPublicKey = object;
703 break;
704 case X509_OBJ_EXTN_ID:
705 extn_oid = known_oid(object);
706 break;
707 case X509_OBJ_CRITICAL:
708 critical = object.len && *object.ptr;
709 logger->log(logger, ERROR|LEVEL1, " %s", critical ? "TRUE" : "FALSE");
710 break;
711 case X509_OBJ_EXTN_VALUE:
712 {
713 switch (extn_oid) {
714 case OID_SUBJECT_KEY_ID:
715 cert->subjectKeyID = parse_keyIdentifier(object, level, FALSE);
716 break;
717 case OID_SUBJECT_ALT_NAME:
718 parse_generalNames(object, level, FALSE, cert->subjectAltNames);
719 break;
720 case OID_BASIC_CONSTRAINTS:
721 cert->isCA = parse_basicConstraints(object, level);
722 break;
723 case OID_CRL_DISTRIBUTION_POINTS:
724 parse_crlDistributionPoints(object, level, cert->crlDistributionPoints);
725 break;
726 case OID_AUTHORITY_KEY_ID:
727 parse_authorityKeyIdentifier(object, level , &cert->authKeyID, &cert->authKeySerialNumber);
728 break;
729 case OID_AUTHORITY_INFO_ACCESS:
730 parse_authorityInfoAccess(object, level, &cert->accessLocation);
731 break;
732 case OID_EXTENDED_KEY_USAGE:
733 cert->isOcspSigner = parse_extendedKeyUsage(object, level);
734 break;
735 case OID_NS_REVOCATION_URL:
736 case OID_NS_CA_REVOCATION_URL:
737 case OID_NS_CA_POLICY_URL:
738 case OID_NS_COMMENT:
739 if (!parse_asn1_simple_object(&object, ASN1_IA5STRING , level, oid_names[extn_oid].name))
740 {
741 return FALSE;
742 }
743 break;
744 default:
745 break;
746 }
747 break;
748 }
749 case X509_OBJ_ALGORITHM:
750 cert->algorithm = parse_algorithmIdentifier(object, level, NULL);
751 break;
752 case X509_OBJ_SIGNATURE:
753 cert->signature = object;
754 break;
755 default:
756 break;
757 }
758 objectID++;
759 }
760 time(&cert->installed);
761 return TRUE;
762 }
763
764 /**
765 * verify the validity of a x509 by
766 * checking the notBefore and notAfter dates
767 */
768 err_t check_validity(const private_x509_t *cert, time_t *until)
769 {
770 time_t current_time;
771
772 time(&current_time);
773
774 if (cert->notAfter < *until)
775 {
776 *until = cert->notAfter;
777 }
778 if (current_time < cert->notBefore)
779 {
780 return "x509 is not valid yet";
781 }
782 if (current_time > cert->notAfter)
783 {
784 return "x509 has expired";
785 }
786 else
787 {
788 return NULL;
789 }
790 }
791
792 /**
793 * Implements x509_t.get_public_key
794 */
795 static rsa_public_key_t *get_public_key(private_x509_t *this)
796 {
797 return this->public_key->clone(this->public_key);;
798 }
799
800 /**
801 * Implements x509_t.get_subject
802 */
803 static identification_t *get_subject(private_x509_t *this)
804 {
805 return this->subject;
806 }
807
808 /**
809 * Implements x509_t.get_issuer
810 */
811 static identification_t *get_issuer(private_x509_t *this)
812 {
813 return this->issuer;
814 }
815
816 /**
817 * destroy
818 */
819 static void destroy(private_x509_t *this)
820 {
821 identification_t *id;
822 while (this->subjectAltNames->remove_last(this->subjectAltNames, (void**)&id) == SUCCESS)
823 {
824 id->destroy(id);
825 }
826 this->subjectAltNames->destroy(this->subjectAltNames);
827 while (this->issuerAltNames->remove_last(this->issuerAltNames, (void**)&id) == SUCCESS)
828 {
829 id->destroy(id);
830 }
831 this->issuerAltNames->destroy(this->issuerAltNames);
832 while (this->crlDistributionPoints->remove_last(this->crlDistributionPoints, (void**)&id) == SUCCESS)
833 {
834 id->destroy(id);
835 }
836 this->crlDistributionPoints->destroy(this->crlDistributionPoints);
837 if (this->issuer)
838 {
839 this->issuer->destroy(this->issuer);
840 }
841 if (this->subject)
842 {
843 this->subject->destroy(this->subject);
844 }
845 if (this->public_key)
846 {
847 this->public_key->destroy(this->public_key);
848 }
849 free(this->certificate.ptr);
850 free(this);
851 }
852
853 /**
854 * log certificate
855 */
856 static void log_certificate(private_x509_t *this, logger_t *logger, bool utc)
857 {
858 identification_t *subject = this->subject;
859 identification_t *issuer = this->issuer;
860
861 rsa_public_key_t *rsa_key = this->public_key;
862
863 char buf[BUF_LEN];
864
865 timetoa(buf, BUF_LEN, &this->installed, utc);
866 logger->log(logger, CONTROL, "%s", buf);
867 logger->log(logger, CONTROL, " subject: '%s'", subject->get_string(subject));
868 logger->log(logger, CONTROL, " issuer: '%s'", issuer->get_string(issuer));
869 chunk_to_hex(buf, BUF_LEN, this->serialNumber);
870 logger->log(logger, CONTROL, " serial: %s", buf);
871 timetoa(buf, BUF_LEN, &this->notBefore, utc);
872 logger->log(logger, CONTROL, " validity: not before %s", buf);
873 timetoa(buf, BUF_LEN, &this->notAfter, utc);
874 logger->log(logger, CONTROL, " not after %s", buf);
875 logger->log(logger, CONTROL, " pubkey: RSA %d bits", BITS_PER_BYTE * rsa_key->get_keysize(rsa_key));
876 if (this->subjectKeyID.ptr != NULL)
877 {
878 chunk_to_hex(buf, BUF_LEN, this->subjectKeyID);
879 logger->log(logger, CONTROL, " subjkey: %s", buf);
880 }
881 if (this->authKeyID.ptr != NULL)
882 {
883 chunk_to_hex(buf, BUF_LEN, this->authKeyID);
884 logger->log(logger, CONTROL, " authkey: %s", buf);
885 }
886 if (this->authKeySerialNumber.ptr != NULL)
887 {
888 chunk_to_hex(buf, BUF_LEN, this->authKeySerialNumber);
889 logger->log(logger, CONTROL, " aserial: %s", buf);
890 }
891 }
892
893 /*
894 * Described in header.
895 */
896 x509_t *x509_create_from_chunk(chunk_t chunk)
897 {
898 private_x509_t *this = malloc_thing(private_x509_t);
899
900 /* public functions */
901 this->public.equals = (bool (*) (x509_t*,x509_t*))equals;
902 this->public.destroy = (void (*) (x509_t*))destroy;
903 this->public.get_public_key = (rsa_public_key_t* (*) (x509_t*))get_public_key;
904 this->public.get_subject = (identification_t* (*) (x509_t*))get_subject;
905 this->public.get_issuer = (identification_t* (*) (x509_t*))get_issuer;
906 this->public.log_certificate = (void (*) (x509_t*,logger_t*,bool))log_certificate;
907
908 /* initialize */
909 this->subjectPublicKey = CHUNK_INITIALIZER;
910 this->public_key = NULL;
911 this->subject = NULL;
912 this->issuer = NULL;
913 this->subjectAltNames = linked_list_create();
914 this->issuerAltNames = linked_list_create();
915 this->crlDistributionPoints = linked_list_create();
916 this->subjectKeyID = CHUNK_INITIALIZER;
917 this->authKeyID = CHUNK_INITIALIZER;
918 this->authKeySerialNumber = CHUNK_INITIALIZER;
919
920 /* we do not use a per-instance logger right now, since its not always accessible */
921 logger = logger_manager->get_logger(logger_manager, ASN1);
922
923 if (!is_asn1(chunk) ||
924 !parse_x509cert(chunk, 0, this))
925 {
926 destroy(this);
927 return NULL;
928 }
929
930 this->public_key = rsa_public_key_create_from_chunk(this->subjectPublicKey);
931 if (this->public_key == NULL)
932 {
933 destroy(this);
934 return NULL;
935 }
936
937 return &this->public;
938 }
939
940 /*
941 * Described in header.
942 */
943 x509_t *x509_create_from_file(const char *filename)
944 {
945 bool pgp = FALSE;
946 chunk_t chunk = CHUNK_INITIALIZER;
947 x509_t *cert = NULL;
948
949 if (!pem_asn1_load_file(filename, "", "certificate", &chunk, &pgp))
950 return NULL;
951
952 cert = x509_create_from_chunk(chunk);
953 if (cert == NULL)
954 {
955 free(chunk.ptr);
956 }
957 return cert;
958 }