hooray, pluto and scepclient do not depend on libgmp anymore
[strongswan.git] / src / pluto / x509.c
1 /* Support of X.509 certificates
2 * Copyright (C) 2000 Andreas Hess, Patric Lichtsteiner, Roger Wegmann
3 * Copyright (C) 2001 Marco Bertossa, Andreas Schleiss
4 * Copyright (C) 2002 Mario Strasser
5 * Copyright (C) 2000-2009 Andreas Steffen - Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <dirent.h>
23 #include <time.h>
24 #include <sys/types.h>
25
26 #include <freeswan.h>
27
28 #include <asn1/asn1.h>
29 #include <asn1/asn1_parser.h>
30 #include <asn1/oid.h>
31 #include <crypto/hashers/hasher.h>
32
33 #include "constants.h"
34 #include "defs.h"
35 #include "log.h"
36 #include "id.h"
37 #include "x509.h"
38 #include "crl.h"
39 #include "ca.h"
40 #include "certs.h"
41 #include "keys.h"
42 #include "whack.h"
43 #include "fetch.h"
44 #include "ocsp.h"
45
46 /**
47 * Chained lists of X.509 end certificates
48 */
49 static x509cert_t *x509certs = NULL;
50
51 /**
52 * ASN.1 definition of a basicConstraints extension
53 */
54 static const asn1Object_t basicConstraintsObjects[] = {
55 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
56 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
57 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
58 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
59 { 0, "exit", ASN1_EOC, ASN1_EXIT }
60 };
61 #define BASIC_CONSTRAINTS_CA 1
62
63 /**
64 * ASN.1 definition of a authorityKeyIdentifier extension
65 */
66 static const asn1Object_t authKeyIdentifierObjects[] = {
67 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
68 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_BODY }, /* 1 */
69 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
70 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
71 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
72 { 1, "authorityCertSerialNumber", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
73 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 6 */
74 { 0, "exit", ASN1_EOC, ASN1_EXIT }
75 };
76 #define AUTH_KEY_ID_KEY_ID 1
77 #define AUTH_KEY_ID_CERT_ISSUER 3
78 #define AUTH_KEY_ID_CERT_SERIAL 5
79
80 /**
81 * ASN.1 definition of a authorityInfoAccess extension
82 */
83 static const asn1Object_t authInfoAccessObjects[] = {
84 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
85 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
86 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
87 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
88 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
89 { 0, "exit", ASN1_EOC, ASN1_EXIT }
90 };
91 #define AUTH_INFO_ACCESS_METHOD 2
92 #define AUTH_INFO_ACCESS_LOCATION 3
93
94 /**
95 * ASN.1 definition of a extendedKeyUsage extension
96 */
97 static const asn1Object_t extendedKeyUsageObjects[] = {
98 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
99 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
100 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
101 { 0, "exit", ASN1_EOC, ASN1_EXIT }
102 };
103 #define EXT_KEY_USAGE_PURPOSE_ID 1
104
105 /**
106 * ASN.1 definition of generalNames
107 */
108 static const asn1Object_t generalNamesObjects[] = {
109 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
110 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
111 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
112 { 0, "exit", ASN1_EOC, ASN1_EXIT }
113 };
114 #define GENERAL_NAMES_GN 1
115
116 /**
117 * ASN.1 definition of generalName
118 */
119 static const asn1Object_t generalNameObjects[] = {
120 { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */
121 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */
122 { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */
123 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */
124 { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */
125 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
126 { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */
127 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
128 { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */
129 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
130 { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */
131 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
132 { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */
133 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */
134 { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */
135 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */
136 { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */
137 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 17 */
138 { 0, "exit", ASN1_EOC, ASN1_EXIT }
139 };
140 #define GN_OBJ_OTHER_NAME 0
141 #define GN_OBJ_RFC822_NAME 2
142 #define GN_OBJ_DNS_NAME 4
143 #define GN_OBJ_X400_ADDRESS 6
144 #define GN_OBJ_DIRECTORY_NAME 8
145 #define GN_OBJ_EDI_PARTY_NAME 10
146 #define GN_OBJ_URI 12
147 #define GN_OBJ_IP_ADDRESS 14
148 #define GN_OBJ_REGISTERED_ID 16
149
150 /**
151 * ASN.1 definition of otherName
152 */
153 static const asn1Object_t otherNameObjects[] = {
154 {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */
155 {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY }, /* 1 */
156 {0, "exit", ASN1_EOC, ASN1_EXIT }
157 };
158 #define ON_OBJ_ID_TYPE 0
159 #define ON_OBJ_VALUE 1
160
161 /**
162 * ASN.1 definition of crlDistributionPoints
163 */
164 static const asn1Object_t crlDistributionPointsObjects[] = {
165 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
166 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
167 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
168 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
169 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
170 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
171 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
172 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
173 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
174 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
175 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
176 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
177 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
178 { 0, "exit", ASN1_EOC, ASN1_EXIT }
179 };
180 #define CRL_DIST_POINTS_FULLNAME 3
181
182 /**
183 * ASN.1 definition of RSApublicKey
184 */
185 static const asn1Object_t pubkeyObjects[] = {
186 { 0, "RSAPublicKey", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
187 { 1, "modulus", ASN1_INTEGER, ASN1_BODY }, /* 1 */
188 { 1, "publicExponent", ASN1_INTEGER, ASN1_BODY }, /* 2 */
189 { 0, "exit", ASN1_EOC, ASN1_EXIT }
190 };
191 #define PUB_KEY_RSA_PUBLIC_KEY 0
192 #define PUB_KEY_MODULUS 1
193 #define PUB_KEY_EXPONENT 2
194
195 /**
196 * ASN.1 definition of an X.509v3 x509_cert
197 */
198 static const asn1Object_t certObjects[] = {
199 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
200 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
201 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
202 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
203 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
204 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
205 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
206 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
207 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
208 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
209 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
210 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
211 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
212 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
213 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
214 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
215 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
216 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
217 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
218 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
219 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
220 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
221 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
222 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
223 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
224 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
225 { 0, "exit", ASN1_EOC, ASN1_EXIT }
226 };
227 #define X509_OBJ_CERTIFICATE 0
228 #define X509_OBJ_TBS_CERTIFICATE 1
229 #define X509_OBJ_VERSION 3
230 #define X509_OBJ_SERIAL_NUMBER 4
231 #define X509_OBJ_SIG_ALG 5
232 #define X509_OBJ_ISSUER 6
233 #define X509_OBJ_NOT_BEFORE 8
234 #define X509_OBJ_NOT_AFTER 9
235 #define X509_OBJ_SUBJECT 10
236 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
237 #define X509_OBJ_EXTN_ID 19
238 #define X509_OBJ_CRITICAL 20
239 #define X509_OBJ_EXTN_VALUE 21
240 #define X509_OBJ_ALGORITHM 24
241 #define X509_OBJ_SIGNATURE 25
242
243 const x509cert_t empty_x509cert = {
244 NULL , /* *next */
245 UNDEFINED_TIME, /* installed */
246 0 , /* count */
247 FALSE , /* smartcard */
248 AUTH_NONE , /* authority_flags */
249 { NULL, 0 } , /* certificate */
250 { NULL, 0 } , /* tbsCertificate */
251 1 , /* version */
252 { NULL, 0 } , /* serialNumber */
253 OID_UNKNOWN , /* sigAlg */
254 { NULL, 0 } , /* issuer */
255 /* validity */
256 0 , /* notBefore */
257 0 , /* notAfter */
258 { NULL, 0 } , /* subject */
259 NULL , /* public_key */
260 /* issuerUniqueID */
261 /* subjectUniqueID */
262 /* extensions */
263 /* extension */
264 /* extnID */
265 /* critical */
266 /* extnValue */
267 FALSE , /* isCA */
268 FALSE , /* isOcspSigner */
269 { NULL, 0 } , /* subjectKeyID */
270 { NULL, 0 } , /* authKeyID */
271 { NULL, 0 } , /* authKeySerialNumber */
272 { NULL, 0 } , /* accessLocation */
273 NULL , /* subjectAltName */
274 NULL , /* crlDistributionPoints */
275 OID_UNKNOWN , /* algorithm */
276 { NULL, 0 } /* signature */
277 };
278
279 /* coding of X.501 distinguished name */
280
281 typedef struct {
282 const u_char *name;
283 chunk_t oid;
284 u_char type;
285 } x501rdn_t;
286
287 /* X.501 acronyms for well known object identifiers (OIDs) */
288
289 static u_char oid_ND[] = {0x02, 0x82, 0x06, 0x01,
290 0x0A, 0x07, 0x14};
291 static u_char oid_UID[] = {0x09, 0x92, 0x26, 0x89, 0x93,
292 0xF2, 0x2C, 0x64, 0x01, 0x01};
293 static u_char oid_DC[] = {0x09, 0x92, 0x26, 0x89, 0x93,
294 0xF2, 0x2C, 0x64, 0x01, 0x19};
295 static u_char oid_CN[] = {0x55, 0x04, 0x03};
296 static u_char oid_S[] = {0x55, 0x04, 0x04};
297 static u_char oid_SN[] = {0x55, 0x04, 0x05};
298 static u_char oid_C[] = {0x55, 0x04, 0x06};
299 static u_char oid_L[] = {0x55, 0x04, 0x07};
300 static u_char oid_ST[] = {0x55, 0x04, 0x08};
301 static u_char oid_O[] = {0x55, 0x04, 0x0A};
302 static u_char oid_OU[] = {0x55, 0x04, 0x0B};
303 static u_char oid_T[] = {0x55, 0x04, 0x0C};
304 static u_char oid_D[] = {0x55, 0x04, 0x0D};
305 static u_char oid_N[] = {0x55, 0x04, 0x29};
306 static u_char oid_G[] = {0x55, 0x04, 0x2A};
307 static u_char oid_I[] = {0x55, 0x04, 0x2B};
308 static u_char oid_ID[] = {0x55, 0x04, 0x2D};
309 static u_char oid_EN[] = {0x60, 0x86, 0x48, 0x01, 0x86,
310 0xF8, 0x42, 0x03, 0x01, 0x03};
311 static u_char oid_E[] = {0x2A, 0x86, 0x48, 0x86, 0xF7,
312 0x0D, 0x01, 0x09, 0x01};
313 static u_char oid_UN[] = {0x2A, 0x86, 0x48, 0x86, 0xF7,
314 0x0D, 0x01, 0x09, 0x02};
315 static u_char oid_TCGID[] = {0x2B, 0x06, 0x01, 0x04, 0x01, 0x89,
316 0x31, 0x01, 0x01, 0x02, 0x02, 0x4B};
317
318 static const x501rdn_t x501rdns[] = {
319 {"ND" , {oid_ND, 7}, ASN1_PRINTABLESTRING},
320 {"UID" , {oid_UID, 10}, ASN1_PRINTABLESTRING},
321 {"DC" , {oid_DC, 10}, ASN1_PRINTABLESTRING},
322 {"CN" , {oid_CN, 3}, ASN1_PRINTABLESTRING},
323 {"S" , {oid_S, 3}, ASN1_PRINTABLESTRING},
324 {"SN" , {oid_SN, 3}, ASN1_PRINTABLESTRING},
325 {"serialNumber" , {oid_SN, 3}, ASN1_PRINTABLESTRING},
326 {"C" , {oid_C, 3}, ASN1_PRINTABLESTRING},
327 {"L" , {oid_L, 3}, ASN1_PRINTABLESTRING},
328 {"ST" , {oid_ST, 3}, ASN1_PRINTABLESTRING},
329 {"O" , {oid_O, 3}, ASN1_PRINTABLESTRING},
330 {"OU" , {oid_OU, 3}, ASN1_PRINTABLESTRING},
331 {"T" , {oid_T, 3}, ASN1_PRINTABLESTRING},
332 {"D" , {oid_D, 3}, ASN1_PRINTABLESTRING},
333 {"N" , {oid_N, 3}, ASN1_PRINTABLESTRING},
334 {"G" , {oid_G, 3}, ASN1_PRINTABLESTRING},
335 {"I" , {oid_I, 3}, ASN1_PRINTABLESTRING},
336 {"ID" , {oid_ID, 3}, ASN1_PRINTABLESTRING},
337 {"EN" , {oid_EN, 10}, ASN1_PRINTABLESTRING},
338 {"employeeNumber" , {oid_EN, 10}, ASN1_PRINTABLESTRING},
339 {"E" , {oid_E, 9}, ASN1_IA5STRING},
340 {"Email" , {oid_E, 9}, ASN1_IA5STRING},
341 {"emailAddress" , {oid_E, 9}, ASN1_IA5STRING},
342 {"UN" , {oid_UN, 9}, ASN1_IA5STRING},
343 {"unstructuredName", {oid_UN, 9}, ASN1_IA5STRING},
344 {"TCGID" , {oid_TCGID, 12}, ASN1_PRINTABLESTRING}
345 };
346
347 #define X501_RDN_ROOF 26
348
349 static u_char ASN1_subjectAltName_oid_str[] = {
350 0x06, 0x03, 0x55, 0x1D, 0x11
351 };
352
353 static const chunk_t ASN1_subjectAltName_oid = chunk_from_buf(ASN1_subjectAltName_oid_str);
354
355 static void update_chunk(chunk_t *ch, int n)
356 {
357 n = (n > -1 && n < (int)ch->len)? n : (int)ch->len-1;
358 ch->ptr += n; ch->len -= n;
359 }
360
361
362 /**
363 * Pointer is set to the first RDN in a DN
364 */
365 static err_t init_rdn(chunk_t dn, chunk_t *rdn, chunk_t *attribute, bool *next)
366 {
367 *rdn = chunk_empty;
368 *attribute = chunk_empty;
369
370 /* a DN is a SEQUENCE OF RDNs */
371
372 if (*dn.ptr != ASN1_SEQUENCE)
373 {
374 return "DN is not a SEQUENCE";
375 }
376
377 rdn->len = asn1_length(&dn);
378
379 if (rdn->len == ASN1_INVALID_LENGTH)
380 {
381 return "Invalid RDN length";
382 }
383 rdn->ptr = dn.ptr;
384
385 /* are there any RDNs ? */
386 *next = rdn->len > 0;
387
388 return NULL;
389 }
390
391 /**
392 * Fetches the next RDN in a DN
393 */
394 static err_t get_next_rdn(chunk_t *rdn, chunk_t * attribute, chunk_t *oid,
395 chunk_t *value, asn1_t *type, bool *next)
396 {
397 chunk_t body;
398
399 /* initialize return values */
400 *oid = chunk_empty;
401 *value = chunk_empty;
402
403 /* if all attributes have been parsed, get next rdn */
404 if (attribute->len <= 0)
405 {
406 /* an RDN is a SET OF attributeTypeAndValue */
407 if (*rdn->ptr != ASN1_SET)
408 {
409 return "RDN is not a SET";
410 }
411 attribute->len = asn1_length(rdn);
412
413 if (attribute->len == ASN1_INVALID_LENGTH)
414 {
415 return "Invalid attribute length";
416 }
417 attribute->ptr = rdn->ptr;
418
419 /* advance to start of next RDN */
420 rdn->ptr += attribute->len;
421 rdn->len -= attribute->len;
422 }
423
424 /* an attributeTypeAndValue is a SEQUENCE */
425 if (*attribute->ptr != ASN1_SEQUENCE)
426 {
427 return "attributeTypeAndValue is not a SEQUENCE";
428 }
429
430 /* extract the attribute body */
431 body.len = asn1_length(attribute);
432
433 if (body.len == ASN1_INVALID_LENGTH)
434 {
435 return "Invalid attribute body length";
436 }
437 body.ptr = attribute->ptr;
438
439 /* advance to start of next attribute */
440 attribute->ptr += body.len;
441 attribute->len -= body.len;
442
443 /* attribute type is an OID */
444 if (*body.ptr != ASN1_OID)
445 {
446 return "attributeType is not an OID";
447 }
448
449 /* extract OID */
450 oid->len = asn1_length(&body);
451
452 if (oid->len == ASN1_INVALID_LENGTH)
453 {
454 return "Invalid attribute OID length";
455 }
456 oid->ptr = body.ptr;
457
458 /* advance to the attribute value */
459 body.ptr += oid->len;
460 body.len -= oid->len;
461
462 /* extract string type */
463 *type = *body.ptr;
464
465 /* extract string value */
466 value->len = asn1_length(&body);
467
468 if (value->len == ASN1_INVALID_LENGTH)
469 {
470 return "Invalid attribute string length";
471 }
472 value->ptr = body.ptr;
473
474 /* are there any RDNs left? */
475 *next = rdn->len > 0 || attribute->len > 0;
476
477 return NULL;
478 }
479
480 /**
481 * Parses an ASN.1 distinguished name int its OID/value pairs
482 */
483 static err_t dn_parse(chunk_t dn, chunk_t *str)
484 {
485 chunk_t rdn, oid, attribute, value;
486 asn1_t type;
487 int oid_code;
488 bool next;
489 bool first = TRUE;
490
491 err_t ugh = init_rdn(dn, &rdn, &attribute, &next);
492
493 if (ugh != NULL) /* a parsing error has occured */
494 {
495 return ugh;
496 }
497
498 while (next)
499 {
500 ugh = get_next_rdn(&rdn, &attribute, &oid, &value, &type, &next);
501
502 if (ugh != NULL) /* a parsing error has occured */
503 {
504 return ugh;
505 }
506
507 if (first) /* first OID/value pair */
508 {
509 first = FALSE;
510 }
511 else /* separate OID/value pair by a comma */
512 {
513 update_chunk(str, snprintf(str->ptr,str->len,", "));
514 }
515
516 /* print OID */
517 oid_code = asn1_known_oid(oid);
518 if (oid_code == OID_UNKNOWN) /* OID not found in list */
519 {
520 hex_str(oid, str);
521 }
522 else
523 {
524 update_chunk(str, snprintf(str->ptr,str->len,"%s",
525 oid_names[oid_code].name));
526 }
527
528 /* print value */
529 update_chunk(str, snprintf(str->ptr,str->len,"=%.*s",
530 (int)value.len,value.ptr));
531 }
532 return NULL;
533 }
534
535 /**
536 * Count the number of wildcard RDNs in a distinguished name
537 */
538 int dn_count_wildcards(chunk_t dn)
539 {
540 chunk_t rdn, attribute, oid, value;
541 asn1_t type;
542 bool next;
543 int wildcards = 0;
544
545 err_t ugh = init_rdn(dn, &rdn, &attribute, &next);
546
547 if (ugh != NULL) /* a parsing error has occured */
548 {
549 return -1;
550 }
551
552 while (next)
553 {
554 ugh = get_next_rdn(&rdn, &attribute, &oid, &value, &type, &next);
555
556 if (ugh != NULL) /* a parsing error has occured */
557 {
558 return -1;
559 }
560 if (value.len == 1 && *value.ptr == '*')
561 {
562 wildcards++; /* we have found a wildcard RDN */
563 }
564 }
565 return wildcards;
566 }
567
568 /**
569 * Prints a binary string in hexadecimal form
570 */
571 void hex_str(chunk_t bin, chunk_t *str)
572 {
573 u_int i;
574 update_chunk(str, snprintf(str->ptr,str->len,"0x"));
575 for (i=0; i < bin.len; i++)
576 update_chunk(str, snprintf(str->ptr,str->len,"%02X",*bin.ptr++));
577 }
578
579
580 /** Converts a binary DER-encoded ASN.1 distinguished name
581 * into LDAP-style human-readable ASCII format
582 */
583 int dntoa(char *dst, size_t dstlen, chunk_t dn)
584 {
585 err_t ugh = NULL;
586 chunk_t str;
587
588 str.ptr = dst;
589 str.len = dstlen;
590 ugh = dn_parse(dn, &str);
591
592 if (ugh != NULL) /* error, print DN as hex string */
593 {
594 DBG(DBG_PARSING,
595 DBG_log("error in DN parsing: %s", ugh)
596 )
597 str.ptr = dst;
598 str.len = dstlen;
599 hex_str(dn, &str);
600 }
601 return (int)(dstlen - str.len);
602 }
603
604 /**
605 * Same as dntoa but prints a special string for a null dn
606 */
607 int dntoa_or_null(char *dst, size_t dstlen, chunk_t dn, const char* null_dn)
608 {
609 if (dn.ptr == NULL)
610 {
611 return snprintf(dst, dstlen, "%s", null_dn);
612 }
613 else
614 {
615 return dntoa(dst, dstlen, dn);
616 }
617 }
618
619
620 /**
621 * Codes ASN.1 lengths up to a size of 16'777'215 bytes
622 */
623 static void code_asn1_length(size_t length, chunk_t *code)
624 {
625 if (length < 128)
626 {
627 code->ptr[0] = length;
628 code->len = 1;
629 }
630 else if (length < 256)
631 {
632 code->ptr[0] = 0x81;
633 code->ptr[1] = (u_char) length;
634 code->len = 2;
635 }
636 else if (length < 65536)
637 {
638 code->ptr[0] = 0x82;
639 code->ptr[1] = length >> 8;
640 code->ptr[2] = length & 0x00ff;
641 code->len = 3;
642 }
643 else
644 {
645 code->ptr[0] = 0x83;
646 code->ptr[1] = length >> 16;
647 code->ptr[2] = (length >> 8) & 0x00ff;
648 code->ptr[3] = length & 0x0000ff;
649 code->len = 4;
650 }
651 }
652
653 /**
654 * Converts an LDAP-style human-readable ASCII-encoded
655 * ASN.1 distinguished name into binary DER-encoded format
656 */
657 err_t atodn(char *src, chunk_t *dn)
658 {
659 /* finite state machine for atodn */
660
661 typedef enum {
662 SEARCH_OID = 0,
663 READ_OID = 1,
664 SEARCH_NAME = 2,
665 READ_NAME = 3,
666 UNKNOWN_OID = 4
667 } state_t;
668
669 u_char oid_len_buf[3];
670 u_char name_len_buf[3];
671 u_char rdn_seq_len_buf[3];
672 u_char rdn_set_len_buf[3];
673 u_char dn_seq_len_buf[3];
674
675 chunk_t asn1_oid_len = { oid_len_buf, 0 };
676 chunk_t asn1_name_len = { name_len_buf, 0 };
677 chunk_t asn1_rdn_seq_len = { rdn_seq_len_buf, 0 };
678 chunk_t asn1_rdn_set_len = { rdn_set_len_buf, 0 };
679 chunk_t asn1_dn_seq_len = { dn_seq_len_buf, 0 };
680 chunk_t oid = chunk_empty;
681 chunk_t name = chunk_empty;
682
683 int whitespace = 0;
684 int rdn_seq_len = 0;
685 int rdn_set_len = 0;
686 int dn_seq_len = 0;
687 int pos = 0;
688
689 err_t ugh = NULL;
690
691 u_char *dn_ptr = dn->ptr + 4;
692
693 state_t state = SEARCH_OID;
694
695 do
696 {
697 switch (state)
698 {
699 case SEARCH_OID:
700 if (*src != ' ' && *src != '/' && *src != ',')
701 {
702 oid.ptr = src;
703 oid.len = 1;
704 state = READ_OID;
705 }
706 break;
707 case READ_OID:
708 if (*src != ' ' && *src != '=')
709 {
710 oid.len++;
711 }
712 else
713 {
714 for (pos = 0; pos < X501_RDN_ROOF; pos++)
715 {
716 if (strlen(x501rdns[pos].name) == oid.len &&
717 strncasecmp(x501rdns[pos].name, oid.ptr, oid.len) == 0)
718 {
719 break; /* found a valid OID */
720 }
721 }
722 if (pos == X501_RDN_ROOF)
723 {
724 ugh = "unknown OID in distinguished name";
725 state = UNKNOWN_OID;
726 break;
727 }
728 code_asn1_length(x501rdns[pos].oid.len, &asn1_oid_len);
729
730 /* reset oid and change state */
731 oid = chunk_empty;
732 state = SEARCH_NAME;
733 }
734 break;
735 case SEARCH_NAME:
736 if (*src != ' ' && *src != '=')
737 {
738 name.ptr = src;
739 name.len = 1;
740 whitespace = 0;
741 state = READ_NAME;
742 }
743 break;
744 case READ_NAME:
745 if (*src != ',' && *src != '/' && *src != '\0')
746 {
747 name.len++;
748 if (*src == ' ')
749 {
750 whitespace++;
751 }
752 else
753 {
754 whitespace = 0;
755 }
756 }
757 else
758 {
759 name.len -= whitespace;
760 code_asn1_length(name.len, &asn1_name_len);
761
762 /* compute the length of the relative distinguished name sequence */
763 rdn_seq_len = 1 + asn1_oid_len.len + x501rdns[pos].oid.len +
764 1 + asn1_name_len.len + name.len;
765 code_asn1_length(rdn_seq_len, &asn1_rdn_seq_len);
766
767 /* compute the length of the relative distinguished name set */
768 rdn_set_len = 1 + asn1_rdn_seq_len.len + rdn_seq_len;
769 code_asn1_length(rdn_set_len, &asn1_rdn_set_len);
770
771 /* encode the relative distinguished name */
772 *dn_ptr++ = ASN1_SET;
773 chunkcpy(dn_ptr, asn1_rdn_set_len);
774 *dn_ptr++ = ASN1_SEQUENCE;
775 chunkcpy(dn_ptr, asn1_rdn_seq_len);
776 *dn_ptr++ = ASN1_OID;
777 chunkcpy(dn_ptr, asn1_oid_len);
778 chunkcpy(dn_ptr, x501rdns[pos].oid);
779 /* encode the ASN.1 character string type of the name */
780 *dn_ptr++ = (x501rdns[pos].type == ASN1_PRINTABLESTRING
781 && !asn1_is_printablestring(name))? ASN1_T61STRING : x501rdns[pos].type;
782 chunkcpy(dn_ptr, asn1_name_len);
783 chunkcpy(dn_ptr, name);
784
785 /* accumulate the length of the distinguished name sequence */
786 dn_seq_len += 1 + asn1_rdn_set_len.len + rdn_set_len;
787
788 /* reset name and change state */
789 name = chunk_empty;
790 state = SEARCH_OID;
791 }
792 break;
793 case UNKNOWN_OID:
794 break;
795 }
796 } while (*src++ != '\0');
797
798 /* complete the distinguished name sequence*/
799 code_asn1_length(dn_seq_len, &asn1_dn_seq_len);
800 dn->ptr += 3 - asn1_dn_seq_len.len;
801 dn->len = 1 + asn1_dn_seq_len.len + dn_seq_len;
802 dn_ptr = dn->ptr;
803 *dn_ptr++ = ASN1_SEQUENCE;
804 chunkcpy(dn_ptr, asn1_dn_seq_len);
805 return ugh;
806 }
807
808 /**
809 * compare two distinguished names by comparing the individual RDNs
810 */
811 bool same_dn(chunk_t a, chunk_t b)
812 {
813 chunk_t rdn_a, rdn_b, attribute_a, attribute_b;
814 chunk_t oid_a, oid_b, value_a, value_b;
815 asn1_t type_a, type_b;
816 bool next_a, next_b;
817
818 /* same lengths for the DNs */
819 if (a.len != b.len)
820 {
821 return FALSE;
822 }
823
824 /* try a binary comparison first */
825 if (memeq(a.ptr, b.ptr, b.len))
826 {
827 return TRUE;
828 }
829
830 /* initialize DN parsing */
831 if (init_rdn(a, &rdn_a, &attribute_a, &next_a) != NULL
832 || init_rdn(b, &rdn_b, &attribute_b, &next_b) != NULL)
833 {
834 return FALSE;
835 }
836
837 /* fetch next RDN pair */
838 while (next_a && next_b)
839 {
840 /* parse next RDNs and check for errors */
841 if (get_next_rdn(&rdn_a, &attribute_a, &oid_a, &value_a, &type_a, &next_a) != NULL
842 || get_next_rdn(&rdn_b, &attribute_b, &oid_b, &value_b, &type_b, &next_b) != NULL)
843 {
844 return FALSE;
845 }
846
847 /* OIDs must agree */
848 if (oid_a.len != oid_b.len || memcmp(oid_a.ptr, oid_b.ptr, oid_b.len) != 0)
849 {
850 return FALSE;
851 }
852
853 /* same lengths for values */
854 if (value_a.len != value_b.len)
855 {
856 return FALSE;
857 }
858
859 /* printableStrings and email RDNs require uppercase comparison */
860 if (type_a == type_b && (type_a == ASN1_PRINTABLESTRING ||
861 (type_a == ASN1_IA5STRING && asn1_known_oid(oid_a) == OID_PKCS9_EMAIL)))
862 {
863 if (strncasecmp(value_a.ptr, value_b.ptr, value_b.len) != 0)
864 {
865 return FALSE;
866 }
867 }
868 else
869 {
870 if (strncmp(value_a.ptr, value_b.ptr, value_b.len) != 0)
871 {
872 return FALSE;
873 }
874 }
875 }
876 /* both DNs must have same number of RDNs */
877 if (next_a || next_b)
878 {
879 return FALSE;
880 }
881
882 /* the two DNs are equal! */
883 return TRUE;
884 }
885
886
887 /**
888 * Compare two distinguished names by comparing the individual RDNs.
889 * A single'*' character designates a wildcard RDN in DN b.
890 */
891 bool match_dn(chunk_t a, chunk_t b, int *wildcards)
892 {
893 chunk_t rdn_a, rdn_b, attribute_a, attribute_b;
894 chunk_t oid_a, oid_b, value_a, value_b;
895 asn1_t type_a, type_b;
896 bool next_a, next_b;
897
898 /* initialize wildcard counter */
899 *wildcards = 0;
900
901 /* initialize DN parsing */
902 if (init_rdn(a, &rdn_a, &attribute_a, &next_a) != NULL
903 || init_rdn(b, &rdn_b, &attribute_b, &next_b) != NULL)
904 {
905 return FALSE;
906 }
907
908 /* fetch next RDN pair */
909 while (next_a && next_b)
910 {
911 /* parse next RDNs and check for errors */
912 if (get_next_rdn(&rdn_a, &attribute_a, &oid_a, &value_a, &type_a, &next_a) != NULL
913 || get_next_rdn(&rdn_b, &attribute_b, &oid_b, &value_b, &type_b, &next_b) != NULL)
914 {
915 return FALSE;
916 }
917
918 /* OIDs must agree */
919 if (oid_a.len != oid_b.len || memcmp(oid_a.ptr, oid_b.ptr, oid_b.len) != 0)
920 {
921 return FALSE;
922 }
923
924 /* does rdn_b contain a wildcard? */
925 if (value_b.len == 1 && *value_b.ptr == '*')
926 {
927 (*wildcards)++;
928 continue;
929 }
930
931 /* same lengths for values */
932 if (value_a.len != value_b.len)
933 {
934 return FALSE;
935 }
936
937 /* printableStrings and email RDNs require uppercase comparison */
938 if (type_a == type_b && (type_a == ASN1_PRINTABLESTRING ||
939 (type_a == ASN1_IA5STRING && asn1_known_oid(oid_a) == OID_PKCS9_EMAIL)))
940 {
941 if (strncasecmp(value_a.ptr, value_b.ptr, value_b.len) != 0)
942 {
943 return FALSE;
944 }
945 }
946 else
947 {
948 if (strncmp(value_a.ptr, value_b.ptr, value_b.len) != 0)
949 {
950 return FALSE;
951 }
952 }
953 }
954
955 /* both DNs must have same number of RDNs */
956 if (next_a || next_b)
957 {
958 return FALSE;
959 }
960
961 /* the two DNs match! */
962 return TRUE;
963 }
964
965 /**
966 * Compare two X.509 certificates by comparing their signatures
967 */
968 bool same_x509cert(const x509cert_t *a, const x509cert_t *b)
969 {
970 return chunk_equals(a->signature, b->signature);
971 }
972
973 /**
974 * For each link pointing to the certificate increase the count by one
975 */
976 void share_x509cert(x509cert_t *cert)
977 {
978 if (cert != NULL)
979 {
980 cert->count++;
981 }
982 }
983
984 /**
985 * Add a X.509 user/host certificate to the chained list
986 */
987 x509cert_t* add_x509cert(x509cert_t *cert)
988 {
989 x509cert_t *c = x509certs;
990
991 while (c != NULL)
992 {
993 if (same_x509cert(c, cert)) /* already in chain, free cert */
994 {
995 free_x509cert(cert);
996 return c;
997 }
998 c = c->next;
999 }
1000
1001 /* insert new cert at the root of the chain */
1002 lock_certs_and_keys("add_x509cert");
1003 cert->next = x509certs;
1004 x509certs = cert;
1005 DBG(DBG_CONTROL | DBG_PARSING,
1006 DBG_log(" x509 cert inserted")
1007 )
1008 unlock_certs_and_keys("add_x509cert");
1009 return cert;
1010 }
1011
1012 /**
1013 * Choose either subject DN or a subjectAltName as connection end ID
1014 */
1015 void select_x509cert_id(x509cert_t *cert, struct id *end_id)
1016 {
1017 bool copy_subject_dn = TRUE; /* ID is subject DN */
1018
1019 if (end_id->kind != ID_ANY) /* check for matching subjectAltName */
1020 {
1021 generalName_t *gn = cert->subjectAltName;
1022
1023 while (gn != NULL)
1024 {
1025 struct id id = empty_id;
1026
1027 gntoid(&id, gn);
1028 if (same_id(&id, end_id))
1029 {
1030 copy_subject_dn = FALSE; /* take subjectAltName instead */
1031 break;
1032 }
1033 gn = gn->next;
1034 }
1035 }
1036
1037 if (copy_subject_dn)
1038 {
1039 if (end_id->kind != ID_ANY && end_id->kind != ID_DER_ASN1_DN)
1040 {
1041 char buf[BUF_LEN];
1042
1043 idtoa(end_id, buf, BUF_LEN);
1044 plog(" no subjectAltName matches ID '%s', replaced by subject DN", buf);
1045 }
1046 end_id->kind = ID_DER_ASN1_DN;
1047 end_id->name.len = cert->subject.len;
1048 end_id->name.ptr = temporary_cyclic_buffer();
1049 memcpy(end_id->name.ptr, cert->subject.ptr, cert->subject.len);
1050 }
1051 }
1052
1053 /**
1054 * Check for equality between two key identifiers
1055 */
1056 bool same_keyid(chunk_t a, chunk_t b)
1057 {
1058 if (a.ptr == NULL || b.ptr == NULL)
1059 {
1060 return FALSE;
1061 }
1062 return chunk_equals(a, b);
1063 }
1064
1065 /**
1066 * Check for equality between two serial numbers
1067 */
1068 bool same_serial(chunk_t a, chunk_t b)
1069 {
1070 /* do not compare serial numbers if one of them is not defined */
1071 if (a.ptr == NULL || b.ptr == NULL)
1072 {
1073 return TRUE;
1074 }
1075 return chunk_equals(a, b);
1076 }
1077
1078 /**
1079 * Get a X.509 certificate with a given issuer found at a certain position
1080 */
1081 x509cert_t* get_x509cert(chunk_t issuer, chunk_t serial, chunk_t keyid,
1082 x509cert_t *chain)
1083 {
1084 x509cert_t *cert = (chain != NULL)? chain->next : x509certs;
1085
1086 while (cert != NULL)
1087 {
1088 if ((keyid.ptr != NULL) ? same_keyid(keyid, cert->authKeyID)
1089 : (same_dn(issuer, cert->issuer)
1090 && same_serial(serial, cert->authKeySerialNumber)))
1091 {
1092 return cert;
1093 }
1094 cert = cert->next;
1095 }
1096 return NULL;
1097 }
1098
1099 /**
1100 * Encode a linked list of subjectAltNames
1101 */
1102 chunk_t build_subjectAltNames(generalName_t *subjectAltNames)
1103 {
1104 u_char *pos;
1105 chunk_t names;
1106 size_t len = 0;
1107 generalName_t *gn = subjectAltNames;
1108
1109 /* compute the total size of the ASN.1 attributes object */
1110 while (gn != NULL)
1111 {
1112 len += gn->name.len;
1113 gn = gn->next;
1114 }
1115
1116 pos = asn1_build_object(&names, ASN1_SEQUENCE, len);
1117
1118 gn = subjectAltNames;
1119 while (gn != NULL)
1120 {
1121 chunkcpy(pos, gn->name);
1122 gn = gn->next;
1123 }
1124
1125 return asn1_wrap(ASN1_SEQUENCE, "cm"
1126 , ASN1_subjectAltName_oid
1127 , asn1_wrap(ASN1_OCTET_STRING, "m", names));
1128 }
1129
1130 /**
1131 * Build a to-be-signed X.509 certificate body
1132 */
1133 static chunk_t build_tbs_x509cert(x509cert_t *cert, public_key_t *rsa)
1134 {
1135 /* version is always X.509v3 */
1136 chunk_t version = asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2);
1137
1138 chunk_t extensions = chunk_empty;
1139
1140 chunk_t key = rsa->get_encoding(rsa);
1141
1142 chunk_t keyInfo = asn1_wrap(ASN1_SEQUENCE, "cm",
1143 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
1144 asn1_bitstring("m", key));
1145
1146 if (cert->subjectAltName != NULL)
1147 {
1148 extensions = asn1_wrap(ASN1_CONTEXT_C_3, "m"
1149 , asn1_wrap(ASN1_SEQUENCE, "m"
1150 , build_subjectAltNames(cert->subjectAltName)));
1151 }
1152
1153 return asn1_wrap(ASN1_SEQUENCE, "mmccmcmm"
1154 , version
1155 , asn1_integer("c", cert->serialNumber)
1156 , asn1_algorithmIdentifier(cert->sigAlg)
1157 , cert->issuer
1158 , asn1_wrap(ASN1_SEQUENCE, "mm"
1159 , asn1_from_time(&cert->notBefore, ASN1_UTCTIME)
1160 , asn1_from_time(&cert->notAfter, ASN1_UTCTIME)
1161 )
1162 , cert->subject
1163 , keyInfo
1164 , extensions
1165 );
1166 }
1167
1168 /**
1169 * Build a DER-encoded X.509 certificate
1170 */
1171 void build_x509cert(x509cert_t *cert, public_key_t *cert_key,
1172 private_key_t *signer_key)
1173 {
1174 chunk_t tbs_cert = build_tbs_x509cert(cert, cert_key);
1175
1176 chunk_t signature = x509_build_signature(tbs_cert, cert->sigAlg
1177 , signer_key, TRUE);
1178
1179 cert->certificate = asn1_wrap(ASN1_SEQUENCE, "mcm"
1180 , tbs_cert
1181 , asn1_algorithmIdentifier(cert->sigAlg)
1182 , signature);
1183 }
1184
1185 /**
1186 * Free the dynamic memory used to store generalNames
1187 */
1188 void free_generalNames(generalName_t* gn, bool free_name)
1189 {
1190 while (gn != NULL)
1191 {
1192 generalName_t *gn_top = gn;
1193 if (free_name)
1194 {
1195 free(gn->name.ptr);
1196 }
1197 gn = gn->next;
1198 free(gn_top);
1199 }
1200 }
1201
1202 /**
1203 * Free a X.509 certificate
1204 */
1205 void free_x509cert(x509cert_t *cert)
1206 {
1207 if (cert != NULL)
1208 {
1209 DESTROY_IF(cert->public_key);
1210 free_generalNames(cert->subjectAltName, FALSE);
1211 free_generalNames(cert->crlDistributionPoints, FALSE);
1212 free(cert->certificate.ptr);
1213 free(cert);
1214 cert = NULL;
1215 }
1216 }
1217
1218 /**
1219 * Release of a certificate decreases the count by one
1220 * the certificate is freed when the counter reaches zero
1221 */
1222 void release_x509cert(x509cert_t *cert)
1223 {
1224 if (cert != NULL && --cert->count == 0)
1225 {
1226 x509cert_t **pp = &x509certs;
1227 while (*pp != cert)
1228 {
1229 pp = &(*pp)->next;
1230 }
1231 *pp = cert->next;
1232 free_x509cert(cert);
1233 }
1234 }
1235
1236 /**
1237 * Stores a chained list of end certs and CA certs
1238 */
1239 void store_x509certs(x509cert_t **firstcert, bool strict)
1240 {
1241 x509cert_t *cacerts = NULL;
1242 x509cert_t **pp = firstcert;
1243
1244 /* first extract CA certs, discarding root CA certs */
1245
1246 while (*pp != NULL)
1247 {
1248 x509cert_t *cert = *pp;
1249
1250 if (cert->isCA)
1251 {
1252 *pp = cert->next;
1253
1254 /* we don't accept self-signed CA certs */
1255 if (same_dn(cert->issuer, cert->subject))
1256 {
1257 plog("self-signed cacert rejected");
1258 free_x509cert(cert);
1259 }
1260 else
1261 {
1262 /* insertion into temporary chain of candidate CA certs */
1263 cert->next = cacerts;
1264 cacerts = cert;
1265 }
1266 }
1267 else
1268 {
1269 pp = &cert->next;
1270 }
1271 }
1272
1273 /* now verify the candidate CA certs */
1274
1275 while (cacerts != NULL)
1276 {
1277 x509cert_t *cert = cacerts;
1278
1279 cacerts = cacerts->next;
1280
1281 if (trust_authcert_candidate(cert, cacerts))
1282 {
1283 add_authcert(cert, AUTH_CA);
1284 }
1285 else
1286 {
1287 plog("intermediate cacert rejected");
1288 free_x509cert(cert);
1289 }
1290 }
1291
1292 /* now verify the end certificates */
1293
1294 pp = firstcert;
1295
1296 while (*pp != NULL)
1297 {
1298 time_t valid_until;
1299 x509cert_t *cert = *pp;
1300
1301 if (verify_x509cert(cert, strict, &valid_until))
1302 {
1303 DBG(DBG_CONTROL | DBG_PARSING,
1304 DBG_log("public key validated")
1305 )
1306 add_x509_public_key(cert, valid_until, DAL_SIGNED);
1307 }
1308 else
1309 {
1310 plog("X.509 certificate rejected");
1311 }
1312 *pp = cert->next;
1313 free_x509cert(cert);
1314 }
1315 }
1316
1317 /**
1318 * Check if a signature over binary blob is genuine
1319 */
1320 bool x509_check_signature(chunk_t tbs, chunk_t sig, int algorithm,
1321 const x509cert_t *issuer_cert)
1322 {
1323 public_key_t *key = issuer_cert->public_key;
1324 signature_scheme_t scheme = signature_scheme_from_oid(algorithm);
1325
1326 if (scheme == SIGN_UNKNOWN)
1327 {
1328 return FALSE;
1329 }
1330 return key->verify(key, scheme, tbs, sig);
1331 }
1332
1333 /**
1334 * Build an ASN.1 encoded PKCS#1 signature over a binary blob
1335 */
1336 chunk_t x509_build_signature(chunk_t tbs, int algorithm, private_key_t *key,
1337 bool bit_string)
1338 {
1339 chunk_t signature;
1340 signature_scheme_t scheme = signature_scheme_from_oid(algorithm);
1341
1342 if (scheme == SIGN_UNKNOWN || !key->sign(key, scheme, tbs, &signature))
1343 {
1344 return chunk_empty;
1345 }
1346 return (bit_string) ? asn1_bitstring("m", signature)
1347 : asn1_wrap(ASN1_OCTET_STRING, "m", signature);
1348 }
1349
1350 /**
1351 * Extracts the basicConstraints extension
1352 */
1353 static bool parse_basicConstraints(chunk_t blob, int level0)
1354 {
1355 asn1_parser_t *parser;
1356 chunk_t object;
1357 int objectID;
1358 bool isCA = FALSE;
1359
1360 parser = asn1_parser_create(basicConstraintsObjects, blob);
1361 parser->set_top_level(parser, level0);
1362
1363 while (parser->iterate(parser, &objectID, &object))
1364 {
1365 if (objectID == BASIC_CONSTRAINTS_CA)
1366 {
1367 isCA = object.len && *object.ptr;
1368 DBG(DBG_PARSING,
1369 DBG_log(" %s",(isCA)?"TRUE":"FALSE");
1370 )
1371 }
1372 }
1373 parser->destroy(parser);
1374
1375 return isCA;
1376 }
1377
1378 /**
1379 * Converts a X.500 generalName into an ID
1380 */
1381 void gntoid(struct id *id, const generalName_t *gn)
1382 {
1383 switch(gn->kind)
1384 {
1385 case GN_DNS_NAME: /* ID type: ID_FQDN */
1386 id->kind = ID_FQDN;
1387 id->name = gn->name;
1388 break;
1389 case GN_IP_ADDRESS: /* ID type: ID_IPV4_ADDR */
1390 {
1391 const struct af_info *afi = &af_inet4_info;
1392 err_t ugh = NULL;
1393
1394 id->kind = afi->id_addr;
1395 ugh = initaddr(gn->name.ptr, gn->name.len, afi->af, &id->ip_addr);
1396 }
1397 break;
1398 case GN_RFC822_NAME: /* ID type: ID_USER_FQDN */
1399 id->kind = ID_USER_FQDN;
1400 id->name = gn->name;
1401 break;
1402 default:
1403 id->kind = ID_ANY;
1404 id->name = chunk_empty;
1405 }
1406 }
1407
1408 /**
1409 * Compute the subjectKeyIdentifier according to section 4.2.1.2 of RFC 3280
1410 * as the 160 bit SHA-1 hash of the public key
1411 */
1412 bool compute_subjectKeyID(x509cert_t *cert, chunk_t subjectKeyID)
1413 {
1414 identification_t *keyid;
1415 chunk_t encoding;
1416
1417 keyid = cert->public_key->get_id(cert->public_key, ID_PUBKEY_SHA1);
1418 if (keyid == NULL)
1419 {
1420 plog(" unable to compute subjectKeyID");
1421 return FALSE;
1422 }
1423 encoding = keyid->get_encoding(keyid);
1424 memcpy(subjectKeyID.ptr, encoding.ptr, subjectKeyID.len);
1425 return TRUE;
1426 }
1427
1428 /**
1429 * Extracts an otherName
1430 */
1431 static bool parse_otherName(chunk_t blob, int level0)
1432 {
1433 asn1_parser_t *parser;
1434 chunk_t object;
1435 int objectID;
1436 int oid = OID_UNKNOWN;
1437 bool success = FALSE;
1438
1439 parser = asn1_parser_create(otherNameObjects, blob);
1440 parser->set_top_level(parser, level0);
1441
1442 while (parser->iterate(parser, &objectID, &object))
1443 {
1444 switch (objectID)
1445 {
1446 case ON_OBJ_ID_TYPE:
1447 oid = asn1_known_oid(object);
1448 break;
1449 case ON_OBJ_VALUE:
1450 if (oid == OID_XMPP_ADDR)
1451 {
1452 if (!asn1_parse_simple_object(&object, ASN1_UTF8STRING,
1453 parser->get_level(parser) + 1, "xmppAddr"))
1454 {
1455 goto end;
1456 }
1457 }
1458 break;
1459 default:
1460 break;
1461 }
1462 }
1463 success = parser->success(parser);
1464
1465 end:
1466 parser->destroy(parser);
1467 return success;
1468 }
1469
1470
1471 /**
1472 * Extracts a generalName
1473 */
1474 static generalName_t* parse_generalName(chunk_t blob, int level0)
1475 {
1476 u_char buf[BUF_LEN];
1477 asn1_parser_t *parser;
1478 chunk_t object;
1479 generalName_t *gn = NULL;
1480 int objectID;
1481
1482 parser = asn1_parser_create(generalNameObjects, blob);
1483 parser->set_top_level(parser, level0);
1484
1485 while (parser->iterate(parser, &objectID, &object))
1486 {
1487 bool valid_gn = FALSE;
1488
1489 switch (objectID) {
1490 case GN_OBJ_RFC822_NAME:
1491 case GN_OBJ_DNS_NAME:
1492 case GN_OBJ_URI:
1493 DBG(DBG_PARSING,
1494 DBG_log(" '%.*s'", (int)object.len, object.ptr);
1495 )
1496 valid_gn = TRUE;
1497 break;
1498 case GN_OBJ_DIRECTORY_NAME:
1499 DBG(DBG_PARSING,
1500 dntoa(buf, BUF_LEN, object);
1501 DBG_log(" '%s'", buf)
1502 )
1503 valid_gn = TRUE;
1504 break;
1505 case GN_OBJ_IP_ADDRESS:
1506 DBG(DBG_PARSING,
1507 DBG_log(" '%d.%d.%d.%d'", *object.ptr, *(object.ptr+1),
1508 *(object.ptr+2), *(object.ptr+3));
1509 )
1510 valid_gn = TRUE;
1511 break;
1512 case GN_OBJ_OTHER_NAME:
1513 if (!parse_otherName(object, parser->get_level(parser)+1))
1514 {
1515 goto end;
1516 }
1517 break;
1518 case GN_OBJ_X400_ADDRESS:
1519 case GN_OBJ_EDI_PARTY_NAME:
1520 case GN_OBJ_REGISTERED_ID:
1521 break;
1522 default:
1523 break;
1524 }
1525
1526 if (valid_gn)
1527 {
1528 gn = malloc_thing(generalName_t);
1529 gn->kind = (objectID - GN_OBJ_OTHER_NAME) / 2;
1530 gn->name = object;
1531 gn->next = NULL;
1532 goto end;
1533 }
1534 }
1535
1536 end:
1537 parser->destroy(parser);
1538 return gn;
1539 }
1540
1541 /**
1542 * Extracts one or several GNs and puts them into a chained list
1543 */
1544 static generalName_t* parse_generalNames(chunk_t blob, int level0, bool implicit)
1545 {
1546 asn1_parser_t *parser;
1547 chunk_t object;
1548 int objectID;
1549 generalName_t *top_gn = NULL;
1550
1551 parser = asn1_parser_create(generalNamesObjects, blob);
1552 parser->set_top_level(parser, level0);
1553 parser->set_flags(parser, implicit, FALSE);
1554
1555 while (parser->iterate(parser, &objectID, &object))
1556 {
1557 if (objectID == GENERAL_NAMES_GN)
1558 {
1559 generalName_t *gn = parse_generalName(object,
1560 parser->get_level(parser)+1);
1561 if (gn)
1562 {
1563 gn->next = top_gn;
1564 top_gn = gn;
1565 }
1566 }
1567 }
1568 parser->destroy(parser);
1569
1570 return top_gn;
1571 }
1572
1573 /**
1574 * Returns a directoryName
1575 */
1576 chunk_t get_directoryName(chunk_t blob, int level, bool implicit)
1577 {
1578 chunk_t name = chunk_empty;
1579 generalName_t * gn = parse_generalNames(blob, level, implicit);
1580
1581 if (gn != NULL && gn->kind == GN_DIRECTORY_NAME)
1582 {
1583 name= gn->name;
1584 }
1585 free_generalNames(gn, FALSE);
1586 return name;
1587 }
1588
1589 /**
1590 * Extracts an authoritykeyIdentifier
1591 */
1592 void parse_authorityKeyIdentifier(chunk_t blob, int level0,
1593 chunk_t *authKeyID,
1594 chunk_t *authKeySerialNumber)
1595 {
1596 asn1_parser_t *parser;
1597 chunk_t object;
1598 int objectID;
1599
1600 parser = asn1_parser_create(authKeyIdentifierObjects, blob);
1601 parser->set_top_level(parser, level0);
1602
1603 while (parser->iterate(parser, &objectID, &object))
1604 {
1605 switch (objectID)
1606 {
1607 case AUTH_KEY_ID_KEY_ID:
1608 *authKeyID = object;
1609 break;
1610 case AUTH_KEY_ID_CERT_ISSUER:
1611 {
1612 generalName_t * gn = parse_generalNames(object,
1613 parser->get_level(parser) + 1, TRUE);
1614
1615 free_generalNames(gn, FALSE);
1616 }
1617 break;
1618 case AUTH_KEY_ID_CERT_SERIAL:
1619 *authKeySerialNumber = object;
1620 break;
1621 default:
1622 break;
1623 }
1624 }
1625 parser->destroy(parser);
1626 }
1627
1628 /**
1629 * Extracts an authorityInfoAcess location
1630 */
1631 static void parse_authorityInfoAccess(chunk_t blob, int level0,
1632 chunk_t *accessLocation)
1633 {
1634 asn1_parser_t *parser;
1635 chunk_t object;
1636 int objectID;
1637 int accessMethod = OID_UNKNOWN;
1638
1639 parser = asn1_parser_create(authInfoAccessObjects, blob);
1640 parser->set_top_level(parser, level0);
1641
1642 while (parser->iterate(parser, &objectID, &object))
1643 {
1644 switch (objectID)
1645 {
1646 case AUTH_INFO_ACCESS_METHOD:
1647 accessMethod = asn1_known_oid(object);
1648 break;
1649 case AUTH_INFO_ACCESS_LOCATION:
1650 {
1651 switch (accessMethod)
1652 {
1653 case OID_OCSP:
1654 if (*object.ptr == ASN1_CONTEXT_S_6)
1655 {
1656 if (asn1_length(&object) == ASN1_INVALID_LENGTH)
1657 {
1658 goto end;
1659 }
1660 DBG(DBG_PARSING,
1661 DBG_log(" '%.*s'",(int)object.len, object.ptr)
1662 )
1663
1664 /* only HTTP(S) URIs accepted */
1665 if (strncasecmp(object.ptr, "http", 4) == 0)
1666 {
1667 *accessLocation = object;
1668 goto end;
1669 }
1670 }
1671 plog("warning: ignoring OCSP InfoAccessLocation with unkown protocol");
1672 break;
1673 default:
1674 /* unkown accessMethod, ignoring */
1675 break;
1676 }
1677 }
1678 break;
1679 default:
1680 break;
1681 }
1682 }
1683
1684 end:
1685 parser->destroy(parser);
1686 }
1687
1688 /**
1689 * Extracts extendedKeyUsage OIDs
1690 */
1691 static bool parse_extendedKeyUsage(chunk_t blob, int level0)
1692 {
1693 asn1_parser_t *parser;
1694 chunk_t object;
1695 int objectID;
1696 bool ocsp_signing = FALSE;
1697
1698 parser = asn1_parser_create(extendedKeyUsageObjects, blob);
1699 parser->set_top_level(parser, level0);
1700
1701 while (parser->iterate(parser, &objectID, &object))
1702 {
1703 if (objectID == EXT_KEY_USAGE_PURPOSE_ID
1704 && asn1_known_oid(object) == OID_OCSP_SIGNING)
1705 {
1706 ocsp_signing = TRUE;
1707 }
1708 }
1709 parser->destroy(parser);
1710
1711 return ocsp_signing;
1712 }
1713
1714 /**
1715 * Extracts one or several crlDistributionPoints
1716 * and puts them into a chained list
1717 */
1718 static generalName_t* parse_crlDistributionPoints(chunk_t blob, int level0)
1719 {
1720 asn1_parser_t *parser;
1721 chunk_t object;
1722 int objectID;
1723
1724 generalName_t *top_gn = NULL; /* top of the chained list */
1725 generalName_t **tail_gn = &top_gn; /* tail of the chained list */
1726
1727 parser = asn1_parser_create(crlDistributionPointsObjects, blob);
1728 parser->set_top_level(parser, level0);
1729
1730 while (parser->iterate(parser, &objectID, &object))
1731 {
1732 if (objectID == CRL_DIST_POINTS_FULLNAME)
1733 {
1734 generalName_t *gn;
1735
1736 gn = parse_generalNames(object, parser->get_level(parser)+1, TRUE);
1737 /* append extracted generalNames to existing chained list */
1738 *tail_gn = gn;
1739 /* find new tail of the chained list */
1740 while (gn != NULL)
1741 {
1742 tail_gn = &gn->next; gn = gn->next;
1743 }
1744 }
1745 }
1746 parser->destroy(parser);
1747
1748 return top_gn;
1749 }
1750
1751 /**
1752 * Parses an X.509v3 certificate
1753 */
1754 bool parse_x509cert(chunk_t blob, u_int level0, x509cert_t *cert)
1755 {
1756 u_char buf[BUF_LEN];
1757 asn1_parser_t *parser;
1758 chunk_t object;
1759 int objectID;
1760 int extn_oid = OID_UNKNOWN;
1761 bool critical;
1762 bool success = FALSE;
1763
1764 parser = asn1_parser_create(certObjects, blob);
1765 parser->set_top_level(parser, level0);
1766
1767 while (parser->iterate(parser, &objectID, &object))
1768 {
1769 u_int level = parser->get_level(parser) + 1;
1770
1771 switch (objectID) {
1772 case X509_OBJ_CERTIFICATE:
1773 cert->certificate = object;
1774 break;
1775 case X509_OBJ_TBS_CERTIFICATE:
1776 cert->tbsCertificate = object;
1777 break;
1778 case X509_OBJ_VERSION:
1779 cert->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
1780 DBG(DBG_PARSING,
1781 DBG_log(" v%d", cert->version);
1782 )
1783 break;
1784 case X509_OBJ_SERIAL_NUMBER:
1785 cert->serialNumber = object;
1786 break;
1787 case X509_OBJ_SIG_ALG:
1788 cert->sigAlg = asn1_parse_algorithmIdentifier(object, level, NULL);
1789 break;
1790 case X509_OBJ_ISSUER:
1791 cert->issuer = object;
1792 DBG(DBG_PARSING,
1793 dntoa(buf, BUF_LEN, object);
1794 DBG_log(" '%s'",buf)
1795 )
1796 break;
1797 case X509_OBJ_NOT_BEFORE:
1798 cert->notBefore = asn1_parse_time(object, level);
1799 break;
1800 case X509_OBJ_NOT_AFTER:
1801 cert->notAfter = asn1_parse_time(object, level);
1802 break;
1803 case X509_OBJ_SUBJECT:
1804 cert->subject = object;
1805 DBG(DBG_PARSING,
1806 dntoa(buf, BUF_LEN, object);
1807 DBG_log(" '%s'",buf)
1808 )
1809 break;
1810 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
1811 cert->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
1812 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
1813 if (cert->public_key == NULL)
1814 {
1815 goto end;
1816 }
1817 break;
1818 case X509_OBJ_EXTN_ID:
1819 extn_oid = asn1_known_oid(object);
1820 break;
1821 case X509_OBJ_CRITICAL:
1822 critical = object.len && *object.ptr;
1823 DBG(DBG_PARSING,
1824 DBG_log(" %s",(critical)?"TRUE":"FALSE");
1825 )
1826 break;
1827 case X509_OBJ_EXTN_VALUE:
1828 {
1829 switch (extn_oid) {
1830 case OID_SUBJECT_KEY_ID:
1831 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
1832 level, "keyIdentifier"))
1833 {
1834 goto end;
1835 }
1836 cert->subjectKeyID = object;
1837 break;
1838 case OID_SUBJECT_ALT_NAME:
1839 cert->subjectAltName =
1840 parse_generalNames(object, level, FALSE);
1841 break;
1842 case OID_BASIC_CONSTRAINTS:
1843 cert->isCA =
1844 parse_basicConstraints(object, level);
1845 break;
1846 case OID_CRL_DISTRIBUTION_POINTS:
1847 cert->crlDistributionPoints =
1848 parse_crlDistributionPoints(object, level);
1849 break;
1850 case OID_AUTHORITY_KEY_ID:
1851 parse_authorityKeyIdentifier(object, level
1852 , &cert->authKeyID, &cert->authKeySerialNumber);
1853 break;
1854 case OID_AUTHORITY_INFO_ACCESS:
1855 parse_authorityInfoAccess(object, level, &cert->accessLocation);
1856 break;
1857 case OID_EXTENDED_KEY_USAGE:
1858 cert->isOcspSigner = parse_extendedKeyUsage(object, level);
1859 break;
1860 case OID_NS_REVOCATION_URL:
1861 case OID_NS_CA_REVOCATION_URL:
1862 case OID_NS_CA_POLICY_URL:
1863 case OID_NS_COMMENT:
1864 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING
1865 , level, oid_names[extn_oid].name))
1866 {
1867 goto end;
1868 }
1869 break;
1870 default:
1871 break;
1872 }
1873 }
1874 break;
1875 case X509_OBJ_ALGORITHM:
1876 cert->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
1877 break;
1878 case X509_OBJ_SIGNATURE:
1879 cert->signature = object;
1880 break;
1881 default:
1882 break;
1883 }
1884 }
1885 success = parser->success(parser);
1886 time(&cert->installed);
1887
1888 end:
1889 parser->destroy(parser);
1890 return success;
1891 }
1892
1893 /**
1894 * Verify the validity of a certificate by
1895 * checking the notBefore and notAfter dates
1896 */
1897 err_t check_validity(const x509cert_t *cert, time_t *until)
1898 {
1899 time_t current_time;
1900
1901 time(&current_time);
1902 DBG(DBG_CONTROL | DBG_PARSING ,
1903 DBG_log(" not before : %T", &cert->notBefore, TRUE);
1904 DBG_log(" current time: %T", &current_time, TRUE);
1905 DBG_log(" not after : %T", &cert->notAfter, TRUE);
1906 )
1907
1908 if (cert->notAfter < *until)
1909 {
1910 *until = cert->notAfter;
1911 }
1912 if (current_time < cert->notBefore)
1913 {
1914 return "certificate is not valid yet";
1915 }
1916 if (current_time > cert->notAfter)
1917 {
1918 return "certificate has expired";
1919 }
1920 else
1921 {
1922 return NULL;
1923 }
1924 }
1925
1926 /**
1927 * Verifies a X.509 certificate
1928 */
1929 bool verify_x509cert(const x509cert_t *cert, bool strict, time_t *until)
1930 {
1931 int pathlen;
1932
1933 *until = cert->notAfter;
1934
1935 for (pathlen = 0; pathlen < MAX_CA_PATH_LEN; pathlen++)
1936 {
1937 x509cert_t *issuer_cert;
1938 u_char buf[BUF_LEN];
1939 err_t ugh = NULL;
1940
1941 DBG(DBG_CONTROL,
1942 dntoa(buf, BUF_LEN, cert->subject);
1943 DBG_log("subject: '%s'",buf);
1944 dntoa(buf, BUF_LEN, cert->issuer);
1945 DBG_log("issuer: '%s'",buf);
1946 if (cert->authKeyID.ptr != NULL)
1947 {
1948 datatot(cert->authKeyID.ptr, cert->authKeyID.len, ':'
1949 , buf, BUF_LEN);
1950 DBG_log("authkey: %s", buf);
1951 }
1952 )
1953
1954 ugh = check_validity(cert, until);
1955
1956 if (ugh != NULL)
1957 {
1958 plog("%s", ugh);
1959 return FALSE;
1960 }
1961
1962 DBG(DBG_CONTROL,
1963 DBG_log("certificate is valid")
1964 )
1965
1966 lock_authcert_list("verify_x509cert");
1967 issuer_cert = get_authcert(cert->issuer, cert->authKeySerialNumber
1968 , cert->authKeyID, AUTH_CA);
1969
1970 if (issuer_cert == NULL)
1971 {
1972 plog("issuer cacert not found");
1973 unlock_authcert_list("verify_x509cert");
1974 return FALSE;
1975 }
1976 DBG(DBG_CONTROL,
1977 DBG_log("issuer cacert found")
1978 )
1979
1980 if (!x509_check_signature(cert->tbsCertificate, cert->signature,
1981 cert->algorithm, issuer_cert))
1982 {
1983 plog("certificate signature is invalid");
1984 unlock_authcert_list("verify_x509cert");
1985 return FALSE;
1986 }
1987 DBG(DBG_CONTROL,
1988 DBG_log("certificate signature is valid")
1989 )
1990 unlock_authcert_list("verify_x509cert");
1991
1992 /* check if cert is a self-signed root ca */
1993 if (pathlen > 0 && same_dn(cert->issuer, cert->subject))
1994 {
1995 DBG(DBG_CONTROL,
1996 DBG_log("reached self-signed root ca")
1997 )
1998 return TRUE;
1999 }
2000 else
2001 {
2002 time_t nextUpdate = *until;
2003 time_t revocationDate = UNDEFINED_TIME;
2004 crl_reason_t revocationReason = REASON_UNSPECIFIED;
2005
2006 /* first check certificate revocation using ocsp */
2007 cert_status_t status = verify_by_ocsp(cert, &nextUpdate
2008 , &revocationDate, &revocationReason);
2009
2010 /* if ocsp service is not available then fall back to crl */
2011 if ((status == CERT_UNDEFINED)
2012 || (status == CERT_UNKNOWN && strict))
2013 {
2014 status = verify_by_crl(cert, &nextUpdate, &revocationDate
2015 , &revocationReason);
2016 }
2017
2018 switch (status)
2019 {
2020 case CERT_GOOD:
2021 /* if status information is stale */
2022 if (strict && nextUpdate < time(NULL))
2023 {
2024 DBG(DBG_CONTROL,
2025 DBG_log("certificate is good but status is stale")
2026 )
2027 remove_x509_public_key(cert);
2028 return FALSE;
2029 }
2030 DBG(DBG_CONTROL,
2031 DBG_log("certificate is good")
2032 )
2033
2034 /* with strict crl policy the public key must have the same
2035 * lifetime as the validity of the ocsp status or crl lifetime
2036 */
2037 if (strict && nextUpdate < *until)
2038 {
2039 *until = nextUpdate;
2040 }
2041 break;
2042 case CERT_REVOKED:
2043 plog("certificate was revoked on %T, reason: %N"
2044 , &revocationDate, TRUE
2045 , crl_reason_names, revocationReason);
2046 remove_x509_public_key(cert);
2047 return FALSE;
2048 case CERT_UNKNOWN:
2049 case CERT_UNDEFINED:
2050 default:
2051 plog("certificate status unknown");
2052 if (strict)
2053 {
2054 remove_x509_public_key(cert);
2055 return FALSE;
2056 }
2057 break;
2058 }
2059 }
2060
2061 /* go up one step in the trust chain */
2062 cert = issuer_cert;
2063 }
2064 plog("maximum ca path length of %d levels exceeded", MAX_CA_PATH_LEN);
2065 return FALSE;
2066 }
2067
2068 /**
2069 * List all X.509 certs in a chained list
2070 */
2071 void list_x509cert_chain(const char *caption, x509cert_t* cert,
2072 u_char auth_flags, bool utc)
2073 {
2074 bool first = TRUE;
2075 time_t now;
2076
2077 /* determine the current time */
2078 time(&now);
2079
2080 while (cert != NULL)
2081 {
2082 if (auth_flags == AUTH_NONE || (auth_flags & cert->authority_flags))
2083 {
2084 u_char buf[BUF_LEN];
2085 public_key_t *key = cert->public_key;
2086 cert_t c;
2087
2088 c.type = CERT_X509_SIGNATURE;
2089 c.u.x509 = cert;
2090
2091 if (first)
2092 {
2093 whack_log(RC_COMMENT, " ");
2094 whack_log(RC_COMMENT, "List of X.509 %s Certificates:", caption);
2095 whack_log(RC_COMMENT, " ");
2096 first = FALSE;
2097 }
2098
2099 whack_log(RC_COMMENT, "%T, count: %d", &cert->installed, utc,
2100 cert->count);
2101 dntoa(buf, BUF_LEN, cert->subject);
2102 whack_log(RC_COMMENT, " subject: '%s'", buf);
2103 dntoa(buf, BUF_LEN, cert->issuer);
2104 whack_log(RC_COMMENT, " issuer: '%s'", buf);
2105 datatot(cert->serialNumber.ptr, cert->serialNumber.len, ':',
2106 buf, BUF_LEN);
2107 whack_log(RC_COMMENT, " serial: %s", buf);
2108 whack_log(RC_COMMENT, " validity: not before %T %s",
2109 &cert->notBefore, utc,
2110 (cert->notBefore < now)?"ok":"fatal (not valid yet)");
2111 whack_log(RC_COMMENT, " not after %T %s",
2112 &cert->notAfter, utc,
2113 check_expiry(cert->notAfter, CA_CERT_WARNING_INTERVAL, TRUE));
2114 whack_log(RC_COMMENT, " pubkey: %N %4d bits%s",
2115 key_type_names, key->get_type(key),
2116 key->get_keysize(key) * BITS_PER_BYTE,
2117 cert->smartcard ? ", on smartcard" :
2118 (has_private_key(c)? ", has private key" : ""));
2119 whack_log(RC_COMMENT, " keyid: %Y",
2120 key->get_id(key, ID_PUBKEY_INFO_SHA1));
2121 if (cert->subjectKeyID.ptr != NULL)
2122 {
2123 datatot(cert->subjectKeyID.ptr, cert->subjectKeyID.len, ':',
2124 buf, BUF_LEN);
2125 whack_log(RC_COMMENT, " subjkey: %s", buf);
2126 }
2127 if (cert->authKeyID.ptr != NULL)
2128 {
2129 datatot(cert->authKeyID.ptr, cert->authKeyID.len, ':',
2130 buf, BUF_LEN);
2131 whack_log(RC_COMMENT, " authkey: %s", buf);
2132 }
2133 if (cert->authKeySerialNumber.ptr != NULL)
2134 {
2135 datatot(cert->authKeySerialNumber.ptr,
2136 cert->authKeySerialNumber.len, ':', buf, BUF_LEN);
2137 whack_log(RC_COMMENT, " aserial: %s", buf);
2138 }
2139 }
2140 cert = cert->next;
2141 }
2142 }
2143
2144 /**
2145 * List all X.509 end certificates in a chained list
2146 */
2147 void list_x509_end_certs(bool utc)
2148 {
2149 list_x509cert_chain("End", x509certs, AUTH_NONE, utc);
2150 }