ipsec pki --issue supports --flag ocspSigning option
[strongswan.git] / src / libstrongswan / plugins / x509 / x509_cert.c
1 /*
2 * Copyright (C) 2000 Andreas Hess, Patric Lichtsteiner, Roger Wegmann
3 * Copyright (C) 2001 Marco Bertossa, Andreas Schleiss
4 * Copyright (C) 2002 Mario Strasser
5 * Copyright (C) 2000-2006 Andreas Steffen
6 * Copyright (C) 2006-2009 Martin Willi
7 * Copyright (C) 2008 Tobias Brunner
8 * Hochschule fuer Technik Rapperswil
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 */
20
21 #define _GNU_SOURCE
22
23 #include "x509_cert.h"
24
25 #include <sys/stat.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include <library.h>
32 #include <debug.h>
33 #include <asn1/oid.h>
34 #include <asn1/asn1.h>
35 #include <asn1/asn1_parser.h>
36 #include <crypto/hashers/hasher.h>
37 #include <credentials/keys/private_key.h>
38 #include <utils/linked_list.h>
39 #include <utils/identification.h>
40
41 /**
42 * Different kinds of generalNames
43 */
44 typedef enum {
45 GN_OTHER_NAME = 0,
46 GN_RFC822_NAME = 1,
47 GN_DNS_NAME = 2,
48 GN_X400_ADDRESS = 3,
49 GN_DIRECTORY_NAME = 4,
50 GN_EDI_PARTY_NAME = 5,
51 GN_URI = 6,
52 GN_IP_ADDRESS = 7,
53 GN_REGISTERED_ID = 8,
54 } generalNames_t;
55
56
57 typedef struct private_x509_cert_t private_x509_cert_t;
58
59 /**
60 * Private data of a x509_cert_t object.
61 */
62 struct private_x509_cert_t {
63 /**
64 * Public interface for this certificate.
65 */
66 x509_cert_t public;
67
68 /**
69 * X.509 certificate encoding in ASN.1 DER format
70 */
71 chunk_t encoding;
72
73 /**
74 * SHA1 hash of the DER encoding of this X.509 certificate
75 */
76 chunk_t encoding_hash;
77
78 /**
79 * X.509 certificate body over which signature is computed
80 */
81 chunk_t tbsCertificate;
82
83 /**
84 * Version of the X.509 certificate
85 */
86 u_int version;
87
88 /**
89 * Serial number of the X.509 certificate
90 */
91 chunk_t serialNumber;
92
93 /**
94 * ID representing the certificate issuer
95 */
96 identification_t *issuer;
97
98 /**
99 * Start time of certificate validity
100 */
101 time_t notBefore;
102
103 /**
104 * End time of certificate validity
105 */
106 time_t notAfter;
107
108 /**
109 * ID representing the certificate subject
110 */
111 identification_t *subject;
112
113 /**
114 * List of subjectAltNames as identification_t
115 */
116 linked_list_t *subjectAltNames;
117
118 /**
119 * List of crlDistributionPoints as allocated char*
120 */
121 linked_list_t *crl_uris;
122
123 /**
124 * List ocspAccessLocations as allocated char*
125 */
126 linked_list_t *ocsp_uris;
127
128 /**
129 * certificate's embedded public key
130 */
131 public_key_t *public_key;
132
133 /**
134 * Subject Key Identifier
135 */
136 chunk_t subjectKeyIdentifier;
137
138 /**
139 * Authority Key Identifier
140 */
141 chunk_t authKeyIdentifier;
142
143 /**
144 * Authority Key Serial Number
145 */
146 chunk_t authKeySerialNumber;
147
148 /**
149 * x509 constraints and other flags
150 */
151 x509_flag_t flags;
152
153 /**
154 * Signature algorithm
155 */
156 int algorithm;
157
158 /**
159 * Signature
160 */
161 chunk_t signature;
162
163 /**
164 * Certificate parsed from blob/file?
165 */
166 bool parsed;
167
168 /**
169 * reference count
170 */
171 refcount_t ref;
172 };
173
174 static const chunk_t ASN1_subjectAltName_oid = chunk_from_chars(
175 0x06, 0x03, 0x55, 0x1D, 0x11
176 );
177
178 /**
179 * ASN.1 definition of a basicConstraints extension
180 */
181 static const asn1Object_t basicConstraintsObjects[] = {
182 { 0, "basicConstraints", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
183 { 1, "CA", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 1 */
184 { 1, "pathLenConstraint", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 2 */
185 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
186 { 0, "exit", ASN1_EOC, ASN1_EXIT }
187 };
188 #define BASIC_CONSTRAINTS_CA 1
189
190 /**
191 * Extracts the basicConstraints extension
192 */
193 static bool parse_basicConstraints(chunk_t blob, int level0)
194 {
195 asn1_parser_t *parser;
196 chunk_t object;
197 int objectID;
198 bool isCA = FALSE;
199
200 parser = asn1_parser_create(basicConstraintsObjects, blob);
201 parser->set_top_level(parser, level0);
202
203 while (parser->iterate(parser, &objectID, &object))
204 {
205 if (objectID == BASIC_CONSTRAINTS_CA)
206 {
207 isCA = object.len && *object.ptr;
208 DBG2(" %s", isCA ? "TRUE" : "FALSE");
209 }
210 }
211 parser->destroy(parser);
212
213 return isCA;
214 }
215
216 /**
217 * ASN.1 definition of otherName
218 */
219 static const asn1Object_t otherNameObjects[] = {
220 {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */
221 {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY }, /* 1 */
222 {0, "exit", ASN1_EOC, ASN1_EXIT }
223 };
224 #define ON_OBJ_ID_TYPE 0
225 #define ON_OBJ_VALUE 1
226
227 /**
228 * Extracts an otherName
229 */
230 static bool parse_otherName(chunk_t blob, int level0)
231 {
232 asn1_parser_t *parser;
233 chunk_t object;
234 int objectID;
235 int oid = OID_UNKNOWN;
236 bool success = FALSE;
237
238 parser = asn1_parser_create(otherNameObjects, blob);
239 parser->set_top_level(parser, level0);
240
241 while (parser->iterate(parser, &objectID, &object))
242 {
243 switch (objectID)
244 {
245 case ON_OBJ_ID_TYPE:
246 oid = asn1_known_oid(object);
247 break;
248 case ON_OBJ_VALUE:
249 if (oid == OID_XMPP_ADDR)
250 {
251 if (!asn1_parse_simple_object(&object, ASN1_UTF8STRING,
252 parser->get_level(parser)+1, "xmppAddr"))
253 {
254 goto end;
255 }
256 }
257 break;
258 default:
259 break;
260 }
261 }
262 success = parser->success(parser);
263
264 end:
265 parser->destroy(parser);
266 return success;
267 }
268
269 /**
270 * ASN.1 definition of generalName
271 */
272 static const asn1Object_t generalNameObjects[] = {
273 { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */
274 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */
275 { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */
276 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */
277 { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */
278 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
279 { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */
280 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
281 { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */
282 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
283 { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */
284 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */
285 { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */
286 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */
287 { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */
288 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */
289 { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */
290 { 0, "end choice", ASN1_EOC, ASN1_END }, /* 17 */
291 { 0, "exit", ASN1_EOC, ASN1_EXIT }
292 };
293 #define GN_OBJ_OTHER_NAME 0
294 #define GN_OBJ_RFC822_NAME 2
295 #define GN_OBJ_DNS_NAME 4
296 #define GN_OBJ_X400_ADDRESS 6
297 #define GN_OBJ_DIRECTORY_NAME 8
298 #define GN_OBJ_EDI_PARTY_NAME 10
299 #define GN_OBJ_URI 12
300 #define GN_OBJ_IP_ADDRESS 14
301 #define GN_OBJ_REGISTERED_ID 16
302
303 /**
304 * Extracts a generalName
305 */
306 static identification_t *parse_generalName(chunk_t blob, int level0)
307 {
308 asn1_parser_t *parser;
309 chunk_t object;
310 int objectID ;
311
312 identification_t *gn = NULL;
313
314 parser = asn1_parser_create(generalNameObjects, blob);
315 parser->set_top_level(parser, level0);
316
317 while (parser->iterate(parser, &objectID, &object))
318 {
319 id_type_t id_type = ID_ANY;
320
321 switch (objectID)
322 {
323 case GN_OBJ_RFC822_NAME:
324 id_type = ID_RFC822_ADDR;
325 break;
326 case GN_OBJ_DNS_NAME:
327 id_type = ID_FQDN;
328 break;
329 case GN_OBJ_URI:
330 id_type = ID_DER_ASN1_GN_URI;
331 break;
332 case GN_OBJ_DIRECTORY_NAME:
333 id_type = ID_DER_ASN1_DN;
334 break;
335 case GN_OBJ_IP_ADDRESS:
336 id_type = ID_IPV4_ADDR;
337 break;
338 case GN_OBJ_OTHER_NAME:
339 if (!parse_otherName(object, parser->get_level(parser)+1))
340 {
341 goto end;
342 }
343 break;
344 case GN_OBJ_X400_ADDRESS:
345 case GN_OBJ_EDI_PARTY_NAME:
346 case GN_OBJ_REGISTERED_ID:
347 default:
348 break;
349 }
350 if (id_type != ID_ANY)
351 {
352 gn = identification_create_from_encoding(id_type, object);
353 DBG2(" '%Y'", gn);
354 goto end;
355 }
356 }
357
358 end:
359 parser->destroy(parser);
360 return gn;
361 }
362
363 /**
364 * ASN.1 definition of generalNames
365 */
366 static const asn1Object_t generalNamesObjects[] = {
367 { 0, "generalNames", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
368 { 1, "generalName", ASN1_EOC, ASN1_RAW }, /* 1 */
369 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
370 { 0, "exit", ASN1_EOC, ASN1_EXIT }
371 };
372 #define GENERAL_NAMES_GN 1
373
374 /**
375 * Extracts one or several GNs and puts them into a chained list
376 */
377 void x509_parse_generalNames(chunk_t blob, int level0, bool implicit, linked_list_t *list)
378 {
379 asn1_parser_t *parser;
380 chunk_t object;
381 int objectID;
382
383 parser = asn1_parser_create(generalNamesObjects, blob);
384 parser->set_top_level(parser, level0);
385 parser->set_flags(parser, implicit, FALSE);
386
387 while (parser->iterate(parser, &objectID, &object))
388 {
389 if (objectID == GENERAL_NAMES_GN)
390 {
391 identification_t *gn = parse_generalName(object,
392 parser->get_level(parser)+1);
393
394 if (gn)
395 {
396 list->insert_last(list, (void *)gn);
397 }
398 }
399 }
400 parser->destroy(parser);
401 }
402
403 /**
404 * ASN.1 definition of a authorityKeyIdentifier extension
405 */
406 static const asn1Object_t authKeyIdentifierObjects[] = {
407 { 0, "authorityKeyIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
408 { 1, "keyIdentifier", ASN1_CONTEXT_S_0, ASN1_OPT|ASN1_BODY }, /* 1 */
409 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
410 { 1, "authorityCertIssuer", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_OBJ }, /* 3 */
411 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
412 { 1, "authorityCertSerialNumber", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 5 */
413 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 6 */
414 { 0, "exit", ASN1_EOC, ASN1_EXIT }
415 };
416 #define AUTH_KEY_ID_KEY_ID 1
417 #define AUTH_KEY_ID_CERT_ISSUER 3
418 #define AUTH_KEY_ID_CERT_SERIAL 5
419
420 /**
421 * Extracts an authoritykeyIdentifier
422 */
423 chunk_t x509_parse_authorityKeyIdentifier(chunk_t blob, int level0,
424 chunk_t *authKeySerialNumber)
425 {
426 asn1_parser_t *parser;
427 chunk_t object;
428 int objectID;
429 chunk_t authKeyIdentifier = chunk_empty;
430
431 *authKeySerialNumber = chunk_empty;
432
433 parser = asn1_parser_create(authKeyIdentifierObjects, blob);
434 parser->set_top_level(parser, level0);
435
436 while (parser->iterate(parser, &objectID, &object))
437 {
438 switch (objectID)
439 {
440 case AUTH_KEY_ID_KEY_ID:
441 authKeyIdentifier = chunk_clone(object);
442 break;
443 case AUTH_KEY_ID_CERT_ISSUER:
444 /* TODO: x509_parse_generalNames(object, level+1, TRUE); */
445 break;
446 case AUTH_KEY_ID_CERT_SERIAL:
447 *authKeySerialNumber = object;
448 break;
449 default:
450 break;
451 }
452 }
453 parser->destroy(parser);
454 return authKeyIdentifier;
455 }
456
457 /**
458 * ASN.1 definition of a authorityInfoAccess extension
459 */
460 static const asn1Object_t authInfoAccessObjects[] = {
461 { 0, "authorityInfoAccess", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
462 { 1, "accessDescription", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
463 { 2, "accessMethod", ASN1_OID, ASN1_BODY }, /* 2 */
464 { 2, "accessLocation", ASN1_EOC, ASN1_RAW }, /* 3 */
465 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
466 { 0, "exit", ASN1_EOC, ASN1_EXIT }
467 };
468 #define AUTH_INFO_ACCESS_METHOD 2
469 #define AUTH_INFO_ACCESS_LOCATION 3
470
471 /**
472 * Extracts an authorityInfoAcess location
473 */
474 static void parse_authorityInfoAccess(chunk_t blob, int level0,
475 private_x509_cert_t *this)
476 {
477 asn1_parser_t *parser;
478 chunk_t object;
479 int objectID;
480 int accessMethod = OID_UNKNOWN;
481
482 parser = asn1_parser_create(authInfoAccessObjects, blob);
483 parser->set_top_level(parser, level0);
484
485 while (parser->iterate(parser, &objectID, &object))
486 {
487 switch (objectID)
488 {
489 case AUTH_INFO_ACCESS_METHOD:
490 accessMethod = asn1_known_oid(object);
491 break;
492 case AUTH_INFO_ACCESS_LOCATION:
493 {
494 switch (accessMethod)
495 {
496 case OID_OCSP:
497 case OID_CA_ISSUERS:
498 {
499 identification_t *id;
500 char *uri;
501
502 id = parse_generalName(object,
503 parser->get_level(parser)+1);
504 if (id == NULL)
505 {
506 /* parsing went wrong - abort */
507 goto end;
508 }
509 DBG2(" '%Y'", id);
510 if (accessMethod == OID_OCSP &&
511 asprintf(&uri, "%Y", id) > 0)
512 {
513 this->ocsp_uris->insert_last(this->ocsp_uris, uri);
514 }
515 id->destroy(id);
516 }
517 break;
518 default:
519 /* unkown accessMethod, ignoring */
520 break;
521 }
522 break;
523 }
524 default:
525 break;
526 }
527 }
528
529 end:
530 parser->destroy(parser);
531 }
532
533 /**
534 * ASN.1 definition of a extendedKeyUsage extension
535 */
536 static const asn1Object_t extendedKeyUsageObjects[] = {
537 { 0, "extendedKeyUsage", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
538 { 1, "keyPurposeID", ASN1_OID, ASN1_BODY }, /* 1 */
539 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 2 */
540 { 0, "exit", ASN1_EOC, ASN1_EXIT }
541 };
542 #define EXT_KEY_USAGE_PURPOSE_ID 1
543
544 /**
545 * Extracts extendedKeyUsage OIDs - currently only OCSP_SIGING is returned
546 */
547 static bool parse_extendedKeyUsage(chunk_t blob, int level0)
548 {
549 asn1_parser_t *parser;
550 chunk_t object;
551 int objectID;
552 bool ocsp_signing = FALSE;
553
554 parser = asn1_parser_create(extendedKeyUsageObjects, blob);
555 parser->set_top_level(parser, level0);
556
557 while (parser->iterate(parser, &objectID, &object))
558 {
559 if (objectID == EXT_KEY_USAGE_PURPOSE_ID &&
560 asn1_known_oid(object) == OID_OCSP_SIGNING)
561 {
562 ocsp_signing = TRUE;
563 }
564 }
565 parser->destroy(parser);
566 return ocsp_signing;
567 }
568
569 /**
570 * ASN.1 definition of crlDistributionPoints
571 */
572 static const asn1Object_t crlDistributionPointsObjects[] = {
573 { 0, "crlDistributionPoints", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
574 { 1, "DistributionPoint", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
575 { 2, "distributionPoint", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 2 */
576 { 3, "fullName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 3 */
577 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 4 */
578 { 3, "nameRelToCRLIssuer",ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 5 */
579 { 3, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
580 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
581 { 2, "reasons", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_BODY }, /* 8 */
582 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
583 { 2, "crlIssuer", ASN1_CONTEXT_C_2, ASN1_OPT|ASN1_BODY }, /* 10 */
584 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
585 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 12 */
586 { 0, "exit", ASN1_EOC, ASN1_EXIT }
587 };
588 #define CRL_DIST_POINTS_FULLNAME 3
589
590 /**
591 * Extracts one or several crlDistributionPoints into a list
592 */
593 static void parse_crlDistributionPoints(chunk_t blob, int level0,
594 private_x509_cert_t *this)
595 {
596 asn1_parser_t *parser;
597 chunk_t object;
598 int objectID;
599 linked_list_t *list = linked_list_create();
600
601 parser = asn1_parser_create(crlDistributionPointsObjects, blob);
602 parser->set_top_level(parser, level0);
603
604 while (parser->iterate(parser, &objectID, &object))
605 {
606 if (objectID == CRL_DIST_POINTS_FULLNAME)
607 {
608 identification_t *id;
609
610 /* append extracted generalNames to existing chained list */
611 x509_parse_generalNames(object, parser->get_level(parser)+1,
612 TRUE, list);
613
614 while (list->remove_last(list, (void**)&id) == SUCCESS)
615 {
616 char *uri;
617
618 if (asprintf(&uri, "%Y", id) > 0)
619 {
620 this->crl_uris->insert_last(this->crl_uris, uri);
621 }
622 id->destroy(id);
623 }
624 }
625 }
626 parser->destroy(parser);
627 list->destroy(list);
628 }
629
630 /**
631 * ASN.1 definition of an X.509v3 x509_cert
632 */
633 static const asn1Object_t certObjects[] = {
634 { 0, "x509", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
635 { 1, "tbsCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
636 { 2, "DEFAULT v1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 2 */
637 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
638 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 4 */
639 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 5 */
640 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
641 { 2, "validity", ASN1_SEQUENCE, ASN1_NONE }, /* 7 */
642 { 3, "notBefore", ASN1_EOC, ASN1_RAW }, /* 8 */
643 { 3, "notAfter", ASN1_EOC, ASN1_RAW }, /* 9 */
644 { 2, "subject", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
645 { 2, "subjectPublicKeyInfo",ASN1_SEQUENCE, ASN1_RAW }, /* 11 */
646 { 2, "issuerUniqueID", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
647 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
648 { 2, "subjectUniqueID", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 14 */
649 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
650 { 2, "optional extensions", ASN1_CONTEXT_C_3, ASN1_OPT }, /* 16 */
651 { 3, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 17 */
652 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 18 */
653 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 19 */
654 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF|ASN1_BODY }, /* 20 */
655 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 21 */
656 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 22 */
657 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 23 */
658 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 24 */
659 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 25 */
660 { 0, "exit", ASN1_EOC, ASN1_EXIT }
661 };
662 #define X509_OBJ_TBS_CERTIFICATE 1
663 #define X509_OBJ_VERSION 3
664 #define X509_OBJ_SERIAL_NUMBER 4
665 #define X509_OBJ_SIG_ALG 5
666 #define X509_OBJ_ISSUER 6
667 #define X509_OBJ_NOT_BEFORE 8
668 #define X509_OBJ_NOT_AFTER 9
669 #define X509_OBJ_SUBJECT 10
670 #define X509_OBJ_SUBJECT_PUBLIC_KEY_INFO 11
671 #define X509_OBJ_OPTIONAL_EXTENSIONS 16
672 #define X509_OBJ_EXTN_ID 19
673 #define X509_OBJ_CRITICAL 20
674 #define X509_OBJ_EXTN_VALUE 21
675 #define X509_OBJ_ALGORITHM 24
676 #define X509_OBJ_SIGNATURE 25
677
678 /**
679 * forward declaration
680 */
681 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer);
682
683 /**
684 * Parses an X.509v3 certificate
685 */
686 static bool parse_certificate(private_x509_cert_t *this)
687 {
688 asn1_parser_t *parser;
689 chunk_t object;
690 int objectID;
691 int extn_oid = OID_UNKNOWN;
692 int sig_alg = OID_UNKNOWN;
693 bool success = FALSE;
694 bool critical;
695
696 parser = asn1_parser_create(certObjects, this->encoding);
697
698 while (parser->iterate(parser, &objectID, &object))
699 {
700 u_int level = parser->get_level(parser)+1;
701
702 switch (objectID)
703 {
704 case X509_OBJ_TBS_CERTIFICATE:
705 this->tbsCertificate = object;
706 break;
707 case X509_OBJ_VERSION:
708 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
709 if (this->version < 1 || this->version > 3)
710 {
711 DBG1("X.509v%d not supported", this->version);
712 goto end;
713 }
714 else
715 {
716 DBG2(" X.509v%d", this->version);
717 }
718 break;
719 case X509_OBJ_SERIAL_NUMBER:
720 this->serialNumber = object;
721 break;
722 case X509_OBJ_SIG_ALG:
723 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
724 break;
725 case X509_OBJ_ISSUER:
726 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
727 DBG2(" '%Y'", this->issuer);
728 break;
729 case X509_OBJ_NOT_BEFORE:
730 this->notBefore = asn1_parse_time(object, level);
731 break;
732 case X509_OBJ_NOT_AFTER:
733 this->notAfter = asn1_parse_time(object, level);
734 break;
735 case X509_OBJ_SUBJECT:
736 this->subject = identification_create_from_encoding(ID_DER_ASN1_DN, object);
737 DBG2(" '%Y'", this->subject);
738 break;
739 case X509_OBJ_SUBJECT_PUBLIC_KEY_INFO:
740 DBG2("-- > --");
741 this->public_key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
742 KEY_ANY, BUILD_BLOB_ASN1_DER, object, BUILD_END);
743 DBG2("-- < --");
744 if (this->public_key == NULL)
745 {
746 goto end;
747 }
748 break;
749 case X509_OBJ_OPTIONAL_EXTENSIONS:
750 if (this->version != 3)
751 {
752 DBG1("Only X.509v3 certificates have extensions");
753 goto end;
754 }
755 break;
756 case X509_OBJ_EXTN_ID:
757 extn_oid = asn1_known_oid(object);
758 break;
759 case X509_OBJ_CRITICAL:
760 critical = object.len && *object.ptr;
761 DBG2(" %s", critical ? "TRUE" : "FALSE");
762 break;
763 case X509_OBJ_EXTN_VALUE:
764 {
765 switch (extn_oid)
766 {
767 case OID_SUBJECT_KEY_ID:
768 if (!asn1_parse_simple_object(&object, ASN1_OCTET_STRING,
769 level, "keyIdentifier"))
770 {
771 goto end;
772 }
773 this->subjectKeyIdentifier = object;
774 break;
775 case OID_SUBJECT_ALT_NAME:
776 x509_parse_generalNames(object, level, FALSE,
777 this->subjectAltNames);
778 break;
779 case OID_BASIC_CONSTRAINTS:
780 if (parse_basicConstraints(object, level))
781 {
782 this->flags |= X509_CA;
783 }
784 break;
785 case OID_CRL_DISTRIBUTION_POINTS:
786 parse_crlDistributionPoints(object, level, this);
787 break;
788 case OID_AUTHORITY_KEY_ID:
789 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
790 level, &this->authKeySerialNumber);
791 break;
792 case OID_AUTHORITY_INFO_ACCESS:
793 parse_authorityInfoAccess(object, level, this);
794 break;
795 case OID_EXTENDED_KEY_USAGE:
796 if (parse_extendedKeyUsage(object, level))
797 {
798 this->flags |= X509_OCSP_SIGNER;
799 }
800 break;
801 case OID_NS_REVOCATION_URL:
802 case OID_NS_CA_REVOCATION_URL:
803 case OID_NS_CA_POLICY_URL:
804 case OID_NS_COMMENT:
805 if (!asn1_parse_simple_object(&object, ASN1_IA5STRING,
806 level, oid_names[extn_oid].name))
807 {
808 goto end;
809 }
810 break;
811 default:
812 break;
813 }
814 break;
815 }
816 case X509_OBJ_ALGORITHM:
817 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
818 if (this->algorithm != sig_alg)
819 {
820 DBG1(" signature algorithms do not agree");
821 goto end;
822 }
823 break;
824 case X509_OBJ_SIGNATURE:
825 this->signature = object;
826 break;
827 default:
828 break;
829 }
830 }
831 success = parser->success(parser);
832
833 end:
834 parser->destroy(parser);
835 if (success)
836 {
837 hasher_t *hasher;
838
839 /* check if the certificate is self-signed */
840 if (issued_by(this, &this->public.interface.interface))
841 {
842 this->flags |= X509_SELF_SIGNED;
843 }
844 /* create certificate hash */
845 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
846 if (hasher == NULL)
847 {
848 DBG1(" unable to create hash of certificate, SHA1 not supported");
849 return NULL;
850 }
851 hasher->allocate_hash(hasher, this->encoding, &this->encoding_hash);
852 hasher->destroy(hasher);
853 }
854 return success;
855 }
856
857 /**
858 * Implementation of certificate_t.get_type
859 */
860 static certificate_type_t get_type(private_x509_cert_t *this)
861 {
862 return CERT_X509;
863 }
864
865 /**
866 * Implementation of certificate_t.get_subject
867 */
868 static identification_t* get_subject(private_x509_cert_t *this)
869 {
870 return this->subject;
871 }
872
873 /**
874 * Implementation of certificate_t.get_issuer
875 */
876 static identification_t* get_issuer(private_x509_cert_t *this)
877 {
878 return this->issuer;
879 }
880
881 /**
882 * Implementation of certificate_t.has_subject.
883 */
884 static id_match_t has_subject(private_x509_cert_t *this, identification_t *subject)
885 {
886 identification_t *current;
887 enumerator_t *enumerator;
888 id_match_t match, best;
889
890 if (this->encoding_hash.ptr && subject->get_type(subject) == ID_KEY_ID)
891 {
892 if (chunk_equals(this->encoding_hash, subject->get_encoding(subject)))
893 {
894 return ID_MATCH_PERFECT;
895 }
896 }
897
898 best = this->subject->matches(this->subject, subject);
899 enumerator = this->subjectAltNames->create_enumerator(this->subjectAltNames);
900 while (enumerator->enumerate(enumerator, &current))
901 {
902 match = current->matches(current, subject);
903 if (match > best)
904 {
905 best = match;
906 }
907 }
908 enumerator->destroy(enumerator);
909 return best;
910 }
911
912 /**
913 * Implementation of certificate_t.has_issuer.
914 */
915 static id_match_t has_issuer(private_x509_cert_t *this, identification_t *issuer)
916 {
917 /* issuerAltNames currently not supported */
918 return this->issuer->matches(this->issuer, issuer);
919 }
920
921 /**
922 * Implementation of certificate_t.issued_by.
923 */
924 static bool issued_by(private_x509_cert_t *this, certificate_t *issuer)
925 {
926 public_key_t *key;
927 signature_scheme_t scheme;
928 bool valid;
929 x509_t *x509 = (x509_t*)issuer;
930
931 if (&this->public.interface.interface == issuer)
932 {
933 if (this->flags & X509_SELF_SIGNED)
934 {
935 return TRUE;
936 }
937 }
938 else
939 {
940 if (issuer->get_type(issuer) != CERT_X509)
941 {
942 return FALSE;
943 }
944 if (!(x509->get_flags(x509) & X509_CA))
945 {
946 return FALSE;
947 }
948 }
949 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
950 {
951 return FALSE;
952 }
953
954 /* determine signature scheme */
955 scheme = signature_scheme_from_oid(this->algorithm);
956 if (scheme == SIGN_UNKNOWN)
957 {
958 return FALSE;
959 }
960 /* get the public key of the issuer */
961 key = issuer->get_public_key(issuer);
962 if (!key)
963 {
964 return FALSE;
965 }
966 valid = key->verify(key, scheme, this->tbsCertificate, this->signature);
967 key->destroy(key);
968 return valid;
969 }
970
971 /**
972 * Implementation of certificate_t.get_public_key
973 */
974 static public_key_t* get_public_key(private_x509_cert_t *this)
975 {
976 this->public_key->get_ref(this->public_key);
977 return this->public_key;
978 }
979
980 /**
981 * Implementation of certificate_t.get_ref
982 */
983 static private_x509_cert_t* get_ref(private_x509_cert_t *this)
984 {
985 ref_get(&this->ref);
986 return this;
987 }
988
989 /**
990 * Implementation of x509_cert_t.get_flags.
991 */
992 static x509_flag_t get_flags(private_x509_cert_t *this)
993 {
994 return this->flags;
995 }
996
997 /**
998 * Implementation of x509_cert_t.get_validity.
999 */
1000 static bool get_validity(private_x509_cert_t *this, time_t *when,
1001 time_t *not_before, time_t *not_after)
1002 {
1003 time_t t = when ? *when : time(NULL);
1004
1005 if (not_before)
1006 {
1007 *not_before = this->notBefore;
1008 }
1009 if (not_after)
1010 {
1011 *not_after = this->notAfter;
1012 }
1013 return (t >= this->notBefore && t <= this->notAfter);
1014 }
1015
1016 /**
1017 * Implementation of certificate_t.is_newer.
1018 */
1019 static bool is_newer(certificate_t *this, certificate_t *that)
1020 {
1021 time_t this_update, that_update, now = time(NULL);
1022 bool new;
1023
1024 this->get_validity(this, &now, &this_update, NULL);
1025 that->get_validity(that, &now, &that_update, NULL);
1026 new = this_update > that_update;
1027 DBG1(" certificate from %T is %s - existing certificate from %T %s",
1028 &this_update, FALSE, new ? "newer":"not newer",
1029 &that_update, FALSE, new ? "replaced":"retained");
1030 return new;
1031 }
1032
1033 /**
1034 * Implementation of certificate_t.get_encoding.
1035 */
1036 static chunk_t get_encoding(private_x509_cert_t *this)
1037 {
1038 return chunk_clone(this->encoding);
1039 }
1040
1041 /**
1042 * Implementation of certificate_t.equals.
1043 */
1044 static bool equals(private_x509_cert_t *this, certificate_t *other)
1045 {
1046 chunk_t encoding;
1047 bool equal;
1048
1049 if (this == (private_x509_cert_t*)other)
1050 {
1051 return TRUE;
1052 }
1053 if (other->get_type(other) != CERT_X509)
1054 {
1055 return FALSE;
1056 }
1057 if (other->equals == (void*)equals)
1058 { /* skip allocation if we have the same implementation */
1059 return chunk_equals(this->encoding, ((private_x509_cert_t*)other)->encoding);
1060 }
1061 encoding = other->get_encoding(other);
1062 equal = chunk_equals(this->encoding, encoding);
1063 free(encoding.ptr);
1064 return equal;
1065 }
1066
1067 /**
1068 * Implementation of x509_t.get_serial.
1069 */
1070 static chunk_t get_serial(private_x509_cert_t *this)
1071 {
1072 return this->serialNumber;
1073 }
1074
1075 /**
1076 * Implementation of x509_t.get_subjectKeyIdentifier.
1077 */
1078 static chunk_t get_subjectKeyIdentifier(private_x509_cert_t *this)
1079 {
1080 if (this->subjectKeyIdentifier.ptr)
1081 {
1082 return this->subjectKeyIdentifier;
1083 }
1084 else
1085 {
1086 chunk_t fingerprint;
1087
1088 if (this->public_key->get_fingerprint(this->public_key,
1089 KEY_ID_PUBKEY_SHA1, &fingerprint))
1090 {
1091 return fingerprint;
1092 }
1093 else
1094 {
1095 return chunk_empty;
1096 }
1097 }
1098 }
1099
1100 /**
1101 * Implementation of x509_t.get_authKeyIdentifier.
1102 */
1103 static chunk_t get_authKeyIdentifier(private_x509_cert_t *this)
1104 {
1105 return this->authKeyIdentifier;
1106 }
1107
1108 /**
1109 * Implementation of x509_cert_t.create_subjectAltName_enumerator.
1110 */
1111 static enumerator_t* create_subjectAltName_enumerator(private_x509_cert_t *this)
1112 {
1113 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
1114 }
1115
1116 /**
1117 * Implementation of x509_cert_t.create_ocsp_uri_enumerator.
1118 */
1119 static enumerator_t* create_ocsp_uri_enumerator(private_x509_cert_t *this)
1120 {
1121 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
1122 }
1123
1124 /**
1125 * Implementation of x509_cert_t.create_crl_uri_enumerator.
1126 */
1127 static enumerator_t* create_crl_uri_enumerator(private_x509_cert_t *this)
1128 {
1129 return this->crl_uris->create_enumerator(this->crl_uris);
1130 }
1131
1132 /**
1133 * Implementation of certificate_t.destroy.
1134 */
1135 static void destroy(private_x509_cert_t *this)
1136 {
1137 if (ref_put(&this->ref))
1138 {
1139 this->subjectAltNames->destroy_offset(this->subjectAltNames,
1140 offsetof(identification_t, destroy));
1141 this->crl_uris->destroy_function(this->crl_uris, free);
1142 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
1143 DESTROY_IF(this->issuer);
1144 DESTROY_IF(this->subject);
1145 DESTROY_IF(this->public_key);
1146 chunk_free(&this->authKeyIdentifier);
1147 chunk_free(&this->encoding);
1148 chunk_free(&this->encoding_hash);
1149 if (!this->parsed)
1150 { /* only parsed certificates point these fields to "encoded" */
1151 chunk_free(&this->signature);
1152 chunk_free(&this->serialNumber);
1153 chunk_free(&this->tbsCertificate);
1154 }
1155 free(this);
1156 }
1157 }
1158
1159 /**
1160 * create an empty but initialized X.509 certificate
1161 */
1162 static private_x509_cert_t* create_empty(void)
1163 {
1164 private_x509_cert_t *this = malloc_thing(private_x509_cert_t);
1165
1166 this->public.interface.interface.get_type = (certificate_type_t (*) (certificate_t*))get_type;
1167 this->public.interface.interface.get_subject = (identification_t* (*) (certificate_t*))get_subject;
1168 this->public.interface.interface.get_issuer = (identification_t* (*) (certificate_t*))get_issuer;
1169 this->public.interface.interface.has_subject = (id_match_t (*) (certificate_t*, identification_t*))has_subject;
1170 this->public.interface.interface.has_issuer = (id_match_t (*) (certificate_t*, identification_t*))has_issuer;
1171 this->public.interface.interface.issued_by = (bool (*) (certificate_t*, certificate_t*))issued_by;
1172 this->public.interface.interface.get_public_key = (public_key_t* (*) (certificate_t*))get_public_key;
1173 this->public.interface.interface.get_validity = (bool (*) (certificate_t*, time_t*, time_t*, time_t*))get_validity;
1174 this->public.interface.interface.is_newer = (bool (*) (certificate_t*,certificate_t*))is_newer;
1175 this->public.interface.interface.get_encoding = (chunk_t (*) (certificate_t*))get_encoding;
1176 this->public.interface.interface.equals = (bool (*)(certificate_t*, certificate_t*))equals;
1177 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t*))get_ref;
1178 this->public.interface.interface.destroy = (void (*)(certificate_t*))destroy;
1179 this->public.interface.get_flags = (x509_flag_t (*)(x509_t*))get_flags;
1180 this->public.interface.get_serial = (chunk_t (*)(x509_t*))get_serial;
1181 this->public.interface.get_subjectKeyIdentifier = (chunk_t (*)(x509_t*))get_subjectKeyIdentifier;
1182 this->public.interface.get_authKeyIdentifier = (chunk_t (*)(x509_t*))get_authKeyIdentifier;
1183 this->public.interface.create_subjectAltName_enumerator = (enumerator_t* (*)(x509_t*))create_subjectAltName_enumerator;
1184 this->public.interface.create_crl_uri_enumerator = (enumerator_t* (*)(x509_t*))create_crl_uri_enumerator;
1185 this->public.interface.create_ocsp_uri_enumerator = (enumerator_t* (*)(x509_t*))create_ocsp_uri_enumerator;
1186
1187 this->encoding = chunk_empty;
1188 this->encoding_hash = chunk_empty;
1189 this->tbsCertificate = chunk_empty;
1190 this->version = 1;
1191 this->serialNumber = chunk_empty;
1192 this->notBefore = 0;
1193 this->notAfter = 0;
1194 this->public_key = NULL;
1195 this->subject = NULL;
1196 this->issuer = NULL;
1197 this->subjectAltNames = linked_list_create();
1198 this->crl_uris = linked_list_create();
1199 this->ocsp_uris = linked_list_create();
1200 this->subjectKeyIdentifier = chunk_empty;
1201 this->authKeyIdentifier = chunk_empty;
1202 this->authKeySerialNumber = chunk_empty;
1203 this->algorithm = 0;
1204 this->signature = chunk_empty;
1205 this->flags = 0;
1206 this->ref = 1;
1207 this->parsed = FALSE;
1208
1209 return this;
1210 }
1211
1212 /**
1213 * Encode a linked list of subjectAltNames
1214 */
1215 chunk_t x509_build_subjectAltNames(linked_list_t *list)
1216 {
1217 chunk_t subjectAltNames = chunk_empty;
1218 enumerator_t *enumerator;
1219 identification_t *id;
1220
1221 if (list->get_count(list) == 0)
1222 {
1223 return chunk_empty;
1224 }
1225
1226 enumerator = list->create_enumerator(list);
1227 while (enumerator->enumerate(enumerator, &id))
1228 {
1229 int context;
1230 chunk_t name;
1231
1232 switch (id->get_type(id))
1233 {
1234 case ID_RFC822_ADDR:
1235 context = ASN1_CONTEXT_S_1;
1236 break;
1237 case ID_FQDN:
1238 context = ASN1_CONTEXT_S_2;
1239 break;
1240 case ID_IPV4_ADDR:
1241 case ID_IPV6_ADDR:
1242 context = ASN1_CONTEXT_S_7;
1243 break;
1244 default:
1245 DBG1("encoding %N as subjectAltName not supported",
1246 id_type_names, id->get_type(id));
1247 enumerator->destroy(enumerator);
1248 free(subjectAltNames.ptr);
1249 return chunk_empty;
1250 }
1251 name = asn1_wrap(context, "c", id->get_encoding(id));
1252 subjectAltNames = chunk_cat("mm", subjectAltNames, name);
1253 }
1254 enumerator->destroy(enumerator);
1255
1256 return asn1_wrap(ASN1_SEQUENCE, "mm",
1257 asn1_build_known_oid(OID_SUBJECT_ALT_NAME),
1258 asn1_wrap(ASN1_OCTET_STRING, "m",
1259 asn1_wrap(ASN1_SEQUENCE, "m", subjectAltNames)
1260 )
1261 );
1262 }
1263
1264 /**
1265 * Generate and sign a new certificate
1266 */
1267 static bool generate(private_x509_cert_t *cert, certificate_t *sign_cert,
1268 private_key_t *sign_key, int digest_alg)
1269 {
1270 chunk_t extensions = chunk_empty, extendedKeyUsage = chunk_empty;
1271 chunk_t basicConstraints = chunk_empty, subjectAltNames = chunk_empty;
1272 chunk_t subjectKeyIdentifier = chunk_empty, authKeyIdentifier = chunk_empty;
1273 chunk_t crlDistributionPoints = chunk_empty, authorityInfoAccess = chunk_empty;
1274 identification_t *issuer, *subject;
1275 chunk_t key_info;
1276 signature_scheme_t scheme;
1277 hasher_t *hasher;
1278 enumerator_t *enumerator;
1279 char *uri;
1280
1281 subject = cert->subject;
1282 if (sign_cert)
1283 {
1284 issuer = sign_cert->get_subject(sign_cert);
1285 if (!cert->public_key)
1286 {
1287 return FALSE;
1288 }
1289 }
1290 else
1291 { /* self signed */
1292 issuer = subject;
1293 if (!cert->public_key)
1294 {
1295 cert->public_key = sign_key->get_public_key(sign_key);
1296 }
1297 cert->flags |= X509_SELF_SIGNED;
1298 }
1299 cert->issuer = issuer->clone(issuer);
1300 if (!cert->notBefore)
1301 {
1302 cert->notBefore = time(NULL);
1303 }
1304 if (!cert->notAfter)
1305 { /* defaults to 1 year from now */
1306 cert->notAfter = cert->notBefore + 60 * 60 * 24 * 365;
1307 }
1308
1309 /* select signature scheme */
1310 cert->algorithm = hasher_signature_algorithm_to_oid(digest_alg,
1311 sign_key->get_type(sign_key));
1312 if (cert->algorithm == OID_UNKNOWN)
1313 {
1314 return FALSE;
1315 }
1316 scheme = signature_scheme_from_oid(cert->algorithm);
1317
1318 if (!cert->public_key->get_encoding(cert->public_key,
1319 KEY_PUB_SPKI_ASN1_DER, &key_info))
1320 {
1321 return FALSE;
1322 }
1323
1324 /* encode subjectAltNames */
1325 subjectAltNames = x509_build_subjectAltNames(cert->subjectAltNames);
1326
1327 /* encode CRL distribution points extension */
1328 enumerator = cert->crl_uris->create_enumerator(cert->crl_uris);
1329 while (enumerator->enumerate(enumerator, &uri))
1330 {
1331 chunk_t distributionPoint;
1332
1333 distributionPoint = asn1_wrap(ASN1_SEQUENCE, "m",
1334 asn1_wrap(ASN1_CONTEXT_C_0, "m",
1335 asn1_wrap(ASN1_CONTEXT_C_0, "m",
1336 asn1_wrap(ASN1_CONTEXT_S_6, "c",
1337 chunk_create(uri, strlen(uri))))));
1338
1339 crlDistributionPoints = chunk_cat("mm", crlDistributionPoints,
1340 distributionPoint);
1341 }
1342 enumerator->destroy(enumerator);
1343 if (crlDistributionPoints.ptr)
1344 {
1345 crlDistributionPoints = asn1_wrap(ASN1_SEQUENCE, "mm",
1346 asn1_build_known_oid(OID_CRL_DISTRIBUTION_POINTS),
1347 asn1_wrap(ASN1_OCTET_STRING, "m",
1348 asn1_wrap(ASN1_SEQUENCE, "m", crlDistributionPoints)));
1349 }
1350
1351 /* encode OCSP URIs in authorityInfoAccess extension */
1352 enumerator = cert->ocsp_uris->create_enumerator(cert->ocsp_uris);
1353 while (enumerator->enumerate(enumerator, &uri))
1354 {
1355 chunk_t accessDescription;
1356
1357 accessDescription = asn1_wrap(ASN1_SEQUENCE, "mm",
1358 asn1_build_known_oid(OID_OCSP),
1359 asn1_wrap(ASN1_CONTEXT_S_6, "c",
1360 chunk_create(uri, strlen(uri))));
1361 authorityInfoAccess = chunk_cat("mm", authorityInfoAccess,
1362 accessDescription);
1363 }
1364 enumerator->destroy(enumerator);
1365 if (authorityInfoAccess.ptr)
1366 {
1367 authorityInfoAccess = asn1_wrap(ASN1_SEQUENCE, "mm",
1368 asn1_build_known_oid(OID_AUTHORITY_INFO_ACCESS),
1369 asn1_wrap(ASN1_OCTET_STRING, "m",
1370 asn1_wrap(ASN1_SEQUENCE, "m", authorityInfoAccess)));
1371 }
1372
1373 /* build CA basicConstraint for CA certificates */
1374 if (cert->flags & X509_CA)
1375 {
1376 basicConstraints = asn1_wrap(ASN1_SEQUENCE, "mmm",
1377 asn1_build_known_oid(OID_BASIC_CONSTRAINTS),
1378 asn1_wrap(ASN1_BOOLEAN, "c",
1379 chunk_from_chars(0xFF)),
1380 asn1_wrap(ASN1_OCTET_STRING, "m",
1381 asn1_wrap(ASN1_SEQUENCE, "m",
1382 asn1_wrap(ASN1_BOOLEAN, "c",
1383 chunk_from_chars(0xFF)))));
1384 }
1385
1386 /* add ocspSigning extendedKeyUsage */
1387 if (cert->flags & X509_OCSP_SIGNER)
1388 {
1389 extendedKeyUsage = asn1_wrap(ASN1_SEQUENCE, "mm ",
1390 asn1_build_known_oid(OID_EXTENDED_KEY_USAGE),
1391 asn1_wrap(ASN1_OCTET_STRING, "m",
1392 asn1_wrap(ASN1_SEQUENCE, "m",
1393 asn1_build_known_oid(OID_OCSP_SIGNING))));
1394 }
1395
1396 /* add subjectKeyIdentifier to CA and OCSP signer certificates */
1397 if (cert->flags & (X509_CA | X509_OCSP_SIGNER))
1398 {
1399 chunk_t keyid;
1400
1401 if (cert->public_key->get_fingerprint(cert->public_key,
1402 KEY_ID_PUBKEY_SHA1, &keyid))
1403 {
1404 subjectKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1405 asn1_build_known_oid(OID_SUBJECT_KEY_ID),
1406 asn1_wrap(ASN1_OCTET_STRING, "m",
1407 asn1_wrap(ASN1_OCTET_STRING, "c", keyid)));
1408 }
1409 }
1410
1411 /* add the keyid authKeyIdentifier for non self-signed certificates */
1412 if (sign_key)
1413 {
1414 chunk_t keyid;
1415
1416 if (sign_key->get_fingerprint(sign_key, KEY_ID_PUBKEY_SHA1, &keyid))
1417 {
1418 authKeyIdentifier = asn1_wrap(ASN1_SEQUENCE, "mm",
1419 asn1_build_known_oid(OID_AUTHORITY_KEY_ID),
1420 asn1_wrap(ASN1_OCTET_STRING, "m",
1421 asn1_wrap(ASN1_SEQUENCE, "m",
1422 asn1_wrap(ASN1_CONTEXT_S_0, "c", keyid))));
1423 }
1424 }
1425 if (basicConstraints.ptr || subjectAltNames.ptr || authKeyIdentifier.ptr ||
1426 crlDistributionPoints.ptr)
1427 {
1428 extensions = asn1_wrap(ASN1_CONTEXT_C_3, "m",
1429 asn1_wrap(ASN1_SEQUENCE, "mmmmmmm",
1430 basicConstraints, subjectKeyIdentifier,
1431 authKeyIdentifier, subjectAltNames,
1432 extendedKeyUsage, crlDistributionPoints,
1433 authorityInfoAccess));
1434 }
1435
1436 cert->tbsCertificate = asn1_wrap(ASN1_SEQUENCE, "mmmcmcmm",
1437 asn1_simple_object(ASN1_CONTEXT_C_0, ASN1_INTEGER_2),
1438 asn1_integer("c", cert->serialNumber),
1439 asn1_algorithmIdentifier(cert->algorithm),
1440 issuer->get_encoding(issuer),
1441 asn1_wrap(ASN1_SEQUENCE, "mm",
1442 asn1_from_time(&cert->notBefore, ASN1_UTCTIME),
1443 asn1_from_time(&cert->notAfter, ASN1_UTCTIME)),
1444 subject->get_encoding(subject),
1445 key_info, extensions);
1446
1447 if (!sign_key->sign(sign_key, scheme, cert->tbsCertificate, &cert->signature))
1448 {
1449 return FALSE;
1450 }
1451 cert->encoding = asn1_wrap(ASN1_SEQUENCE, "cmm", cert->tbsCertificate,
1452 asn1_algorithmIdentifier(cert->algorithm),
1453 asn1_bitstring("c", cert->signature));
1454
1455 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1456 if (!hasher)
1457 {
1458 return FALSE;
1459 }
1460 hasher->allocate_hash(hasher, cert->encoding, &cert->encoding_hash);
1461 hasher->destroy(hasher);
1462 return TRUE;
1463 }
1464
1465 /**
1466 * See header.
1467 */
1468 x509_cert_t *x509_cert_load(certificate_type_t type, va_list args)
1469 {
1470 x509_flag_t flags = 0;
1471 chunk_t blob = chunk_empty;
1472
1473 while (TRUE)
1474 {
1475 switch (va_arg(args, builder_part_t))
1476 {
1477 case BUILD_BLOB_ASN1_DER:
1478 blob = va_arg(args, chunk_t);
1479 continue;
1480 case BUILD_X509_FLAG:
1481 flags |= va_arg(args, x509_flag_t);
1482 continue;
1483 case BUILD_END:
1484 break;
1485 default:
1486 return NULL;
1487 }
1488 break;
1489 }
1490
1491 if (blob.ptr)
1492 {
1493 private_x509_cert_t *cert = create_empty();
1494
1495 cert->encoding = chunk_clone(blob);
1496 cert->parsed = TRUE;
1497 if (parse_certificate(cert))
1498 {
1499 cert->flags |= flags;
1500 return &cert->public;
1501 }
1502 destroy(cert);
1503 }
1504 return NULL;
1505 }
1506
1507 /**
1508 * See header.
1509 */
1510 x509_cert_t *x509_cert_gen(certificate_type_t type, va_list args)
1511 {
1512 private_x509_cert_t *cert;
1513 certificate_t *sign_cert = NULL;
1514 private_key_t *sign_key = NULL;
1515 hash_algorithm_t digest_alg = HASH_SHA1;
1516
1517 cert = create_empty();
1518 while (TRUE)
1519 {
1520 switch (va_arg(args, builder_part_t))
1521 {
1522 case BUILD_X509_FLAG:
1523 cert->flags |= va_arg(args, x509_flag_t);
1524 continue;
1525 case BUILD_SIGNING_KEY:
1526 sign_key = va_arg(args, private_key_t*);
1527 continue;
1528 case BUILD_SIGNING_CERT:
1529 sign_cert = va_arg(args, certificate_t*);
1530 continue;
1531 case BUILD_PUBLIC_KEY:
1532 cert->public_key = va_arg(args, public_key_t*);
1533 cert->public_key->get_ref(cert->public_key);
1534 continue;
1535 case BUILD_SUBJECT:
1536 cert->subject = va_arg(args, identification_t*);
1537 cert->subject = cert->subject->clone(cert->subject);
1538 continue;
1539 case BUILD_SUBJECT_ALTNAMES:
1540 {
1541 enumerator_t *enumerator;
1542 identification_t *id;
1543 linked_list_t *list;
1544
1545 list = va_arg(args, linked_list_t*);
1546 enumerator = list->create_enumerator(list);
1547 while (enumerator->enumerate(enumerator, &id))
1548 {
1549 cert->subjectAltNames->insert_last(cert->subjectAltNames,
1550 id->clone(id));
1551 }
1552 enumerator->destroy(enumerator);
1553 continue;
1554 }
1555 case BUILD_CRL_DISTRIBUTION_POINTS:
1556 {
1557 enumerator_t *enumerator;
1558 linked_list_t *list;
1559 char *uri;
1560
1561 list = va_arg(args, linked_list_t*);
1562 enumerator = list->create_enumerator(list);
1563 while (enumerator->enumerate(enumerator, &uri))
1564 {
1565 cert->crl_uris->insert_last(cert->crl_uris, strdup(uri));
1566 }
1567 enumerator->destroy(enumerator);
1568 continue;
1569 }
1570 case BUILD_OCSP_ACCESS_LOCATIONS:
1571 {
1572 enumerator_t *enumerator;
1573 linked_list_t *list;
1574 char *uri;
1575
1576 list = va_arg(args, linked_list_t*);
1577 enumerator = list->create_enumerator(list);
1578 while (enumerator->enumerate(enumerator, &uri))
1579 {
1580 cert->ocsp_uris->insert_last(cert->ocsp_uris, strdup(uri));
1581 }
1582 enumerator->destroy(enumerator);
1583 continue;
1584 }
1585 case BUILD_NOT_BEFORE_TIME:
1586 cert->notBefore = va_arg(args, time_t);
1587 continue;
1588 case BUILD_NOT_AFTER_TIME:
1589 cert->notAfter = va_arg(args, time_t);
1590 continue;
1591 case BUILD_SERIAL:
1592 cert->serialNumber = chunk_clone(va_arg(args, chunk_t));
1593 continue;
1594 case BUILD_DIGEST_ALG:
1595 digest_alg = va_arg(args, int);
1596 continue;
1597 case BUILD_END:
1598 break;
1599 default:
1600 destroy(cert);
1601 return NULL;
1602 }
1603 break;
1604 }
1605
1606 if (sign_key && generate(cert, sign_cert, sign_key, digest_alg))
1607 {
1608 return &cert->public;
1609 }
1610 destroy(cert);
1611 return NULL;
1612 }
1613