9f27f23064ea4c5dcb6b9c1209f7b00362142f86
[strongswan.git] / src / libstrongswan / plugins / x509 / x509_ocsp_request.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2007 Andreas Steffen
4 * Hochschule fuer Technik Rapperswil
5 * Copyright (C) 2003 Christoph Gysin, Simon Zwahlen
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 * $Id$
18 */
19
20 #include "x509_ocsp_request.h"
21
22 #include <library.h>
23 #include <asn1/oid.h>
24 #include <asn1/asn1.h>
25 #include <utils/identification.h>
26 #include <utils/linked_list.h>
27 #include <debug.h>
28 #include <credentials/certificates/x509.h>
29
30 #define NONCE_LEN 16
31
32 typedef struct private_x509_ocsp_request_t private_x509_ocsp_request_t;
33
34 /**
35 * private data of x509_ocsp_request
36 */
37 struct private_x509_ocsp_request_t {
38
39 /**
40 * public functions
41 */
42 x509_ocsp_request_t public;
43
44 /**
45 * CA the candidates belong to
46 */
47 x509_t *ca;
48
49 /**
50 * Requestor name, subject of cert used if not set
51 */
52 identification_t *requestor;
53
54 /**
55 * Requestor certificate, included in request
56 */
57 certificate_t *cert;
58
59 /**
60 * Requestor private key to sign request
61 */
62 private_key_t *key;
63
64 /**
65 * list of certificates to check, x509_t
66 */
67 linked_list_t *candidates;
68
69 /**
70 * nonce used in request
71 */
72 chunk_t nonce;
73
74 /**
75 * encoded OCSP request
76 */
77 chunk_t encoding;
78
79 /**
80 * reference count
81 */
82 refcount_t ref;
83 };
84
85 static u_char ASN1_nonce_oid_str[] = {
86 0x06, 0x09,
87 0x2B, 0x06,
88 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x02
89 };
90
91 static u_char ASN1_response_oid_str[] = {
92 0x06, 0x09,
93 0x2B, 0x06,
94 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x04
95 };
96
97 static u_char ASN1_response_content_str[] = {
98 0x04, 0x0D,
99 0x30, 0x0B,
100 0x06, 0x09,
101 0x2B, 0x06,
102 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01
103 };
104
105 static const chunk_t ASN1_nonce_oid = chunk_from_buf(ASN1_nonce_oid_str);
106 static const chunk_t ASN1_response_oid = chunk_from_buf(ASN1_response_oid_str);
107 static const chunk_t ASN1_response_content = chunk_from_buf(ASN1_response_content_str);
108
109 /**
110 * build requestorName
111 */
112 static chunk_t build_requestorName(private_x509_ocsp_request_t *this)
113 {
114 if (this->requestor || this->cert)
115 { /* use requestor name, fallback to his cert subject */
116 if (!this->requestor)
117 {
118 this->requestor = this->cert->get_subject(this->cert);
119 this->requestor = this->requestor->clone(this->requestor);
120 }
121 return asn1_wrap(ASN1_CONTEXT_C_1, "m",
122 asn1_simple_object(ASN1_CONTEXT_C_4,
123 this->requestor->get_encoding(this->requestor)));
124
125 }
126 return chunk_empty;
127 }
128
129 /**
130 * build Request, not using singleRequestExtensions
131 */
132 static chunk_t build_Request(private_x509_ocsp_request_t *this,
133 chunk_t issuerNameHash, chunk_t issuerKeyHash,
134 chunk_t serialNumber)
135 {
136 return asn1_wrap(ASN1_SEQUENCE, "m",
137 asn1_wrap(ASN1_SEQUENCE, "cmmm",
138 asn1_algorithmIdentifier(OID_SHA1),
139 asn1_simple_object(ASN1_OCTET_STRING, issuerNameHash),
140 asn1_simple_object(ASN1_OCTET_STRING, issuerKeyHash),
141 asn1_simple_object(ASN1_INTEGER, serialNumber)));
142 }
143
144 /**
145 * build requestList
146 */
147 static chunk_t build_requestList(private_x509_ocsp_request_t *this)
148 {
149 chunk_t issuerNameHash, issuerKeyHash;
150 identification_t *issuer;
151 x509_t *x509;
152 certificate_t *cert;
153 chunk_t list = chunk_empty;
154 public_key_t *public;
155
156 cert = (certificate_t*)this->ca;
157 public = cert->get_public_key(cert);
158 if (public)
159 {
160 hasher_t *hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
161 if (hasher)
162 {
163 identification_t *keyid = public->get_id(public, ID_PUBKEY_SHA1);
164 if (keyid)
165 {
166 enumerator_t *enumerator;
167
168 issuerKeyHash = keyid->get_encoding(keyid);
169
170 issuer = cert->get_subject(cert);
171 hasher->allocate_hash(hasher, issuer->get_encoding(issuer),
172 &issuerNameHash);
173 hasher->destroy(hasher);
174
175 enumerator = this->candidates->create_enumerator(this->candidates);
176 while (enumerator->enumerate(enumerator, &x509))
177 {
178 chunk_t request, serialNumber;
179
180 serialNumber = x509->get_serial(x509);
181 request = build_Request(this, issuerNameHash, issuerKeyHash,
182 serialNumber);
183 list = chunk_cat("mm", list, request);
184 }
185 enumerator->destroy(enumerator);
186 chunk_free(&issuerNameHash);
187 }
188 }
189 else
190 {
191 DBG1("creating OCSP request failed, SHA1 not supported");
192 }
193 public->destroy(public);
194 }
195 else
196 {
197 DBG1("creating OCSP request failed, CA certificate has no public key");
198 }
199 return asn1_wrap(ASN1_SEQUENCE, "m", list);
200 }
201
202 /**
203 * build nonce extension
204 */
205 static chunk_t build_nonce(private_x509_ocsp_request_t *this)
206 {
207 rng_t *rng;
208
209 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
210 if (rng)
211 {
212 rng->allocate_bytes(rng, NONCE_LEN, &this->nonce);
213 rng->destroy(rng);
214 return asn1_wrap(ASN1_SEQUENCE, "cm", ASN1_nonce_oid,
215 asn1_simple_object(ASN1_OCTET_STRING, this->nonce));
216 }
217 DBG1("creating OCSP request nonce failed, no RNG found");
218 return chunk_empty;
219 }
220
221 /**
222 * build acceptableResponses extension
223 */
224 static chunk_t build_acceptableResponses(private_x509_ocsp_request_t *this)
225 {
226 return asn1_wrap(ASN1_SEQUENCE, "cc",
227 ASN1_response_oid,
228 ASN1_response_content);
229 }
230
231 /**
232 * build requestExtensions
233 */
234 static chunk_t build_requestExtensions(private_x509_ocsp_request_t *this)
235 {
236 return asn1_wrap(ASN1_CONTEXT_C_2, "m",
237 asn1_wrap(ASN1_SEQUENCE, "mm",
238 build_nonce(this),
239 build_acceptableResponses(this)));
240 }
241
242 /**
243 * build tbsRequest
244 */
245 static chunk_t build_tbsRequest(private_x509_ocsp_request_t *this)
246 {
247 return asn1_wrap(ASN1_SEQUENCE, "mmm",
248 build_requestorName(this),
249 build_requestList(this),
250 build_requestExtensions(this));
251 }
252
253 /**
254 * Build the optionalSignature
255 */
256 static chunk_t build_optionalSignature(private_x509_ocsp_request_t *this,
257 chunk_t tbsRequest)
258 {
259 int oid;
260 signature_scheme_t scheme;
261 chunk_t certs, signature;
262
263 switch (this->key->get_type(this->key))
264 {
265 /* TODO: use a generic mapping function */
266 case KEY_RSA:
267 oid = OID_SHA1_WITH_RSA;
268 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
269 break;
270 default:
271 DBG1("unable to sign OCSP request, %N signature not supported",
272 key_type_names, this->key->get_type(this->key));
273 return chunk_empty;
274 }
275
276 if (!this->key->sign(this->key, scheme, tbsRequest, &signature))
277 {
278 DBG1("creating OCSP signature failed, skipped");
279 return chunk_empty;
280 }
281 if (this->cert)
282 {
283 certs = asn1_wrap(ASN1_CONTEXT_C_0, "m",
284 asn1_wrap(ASN1_SEQUENCE, "m",
285 this->cert->get_encoding(this->cert)));
286 }
287 return asn1_wrap(ASN1_CONTEXT_C_0, "m",
288 asn1_wrap(ASN1_SEQUENCE, "cmm",
289 asn1_algorithmIdentifier(oid),
290 asn1_bitstring("m", signature),
291 certs));
292 }
293
294 /**
295 * Build the OCSPRequest data
296 *
297 */
298 static chunk_t build_OCSPRequest(private_x509_ocsp_request_t *this)
299 {
300 chunk_t tbsRequest, optionalSignature = chunk_empty;
301
302 tbsRequest = build_tbsRequest(this);
303 if (this->key)
304 {
305 optionalSignature = build_optionalSignature(this, tbsRequest);
306 }
307 return asn1_wrap(ASN1_SEQUENCE, "mm", tbsRequest, optionalSignature);
308 }
309
310
311 /**
312 * Implementation of certificate_t.get_type
313 */
314 static certificate_type_t get_type(private_x509_ocsp_request_t *this)
315 {
316 return CERT_X509_OCSP_REQUEST;
317 }
318
319 /**
320 * Implementation of certificate_t.get_subject
321 */
322 static identification_t* get_subject(private_x509_ocsp_request_t *this)
323 {
324 certificate_t *ca = (certificate_t*)this->ca;
325
326 if (this->requestor)
327 {
328 return this->requestor;
329 }
330 if (this->cert)
331 {
332 return this->cert->get_subject(this->cert);
333 }
334 return ca->get_subject(ca);
335 }
336
337 /**
338 * Implementation of certificate_t.get_issuer
339 */
340 static identification_t* get_issuer(private_x509_ocsp_request_t *this)
341 {
342 certificate_t *ca = (certificate_t*)this->ca;
343
344 return ca->get_subject(ca);
345 }
346
347 /**
348 * Implementation of certificate_t.has_subject.
349 */
350 static id_match_t has_subject(private_x509_ocsp_request_t *this,
351 identification_t *subject)
352 {
353 certificate_t *current;
354 enumerator_t *enumerator;
355 id_match_t match, best = ID_MATCH_NONE;
356
357 enumerator = this->candidates->create_enumerator(this->candidates);
358 while (enumerator->enumerate(enumerator, &current))
359 {
360 match = current->has_subject(current, subject);
361 if (match > best)
362 {
363 best = match;
364 }
365 }
366 enumerator->destroy(enumerator);
367 return best;
368 }
369
370 /**
371 * Implementation of certificate_t.has_subject.
372 */
373 static id_match_t has_issuer(private_x509_ocsp_request_t *this,
374 identification_t *issuer)
375 {
376 certificate_t *ca = (certificate_t*)this->ca;
377
378 return ca->has_subject(ca, issuer);
379 }
380
381 /**
382 * Implementation of certificate_t.issued_by
383 */
384 static bool issued_by(private_x509_ocsp_request_t *this, certificate_t *issuer)
385 {
386 DBG1("OCSP request validation not implemented!");
387 return FALSE;
388 }
389
390 /**
391 * Implementation of certificate_t.get_public_key
392 */
393 static public_key_t* get_public_key(private_x509_ocsp_request_t *this)
394 {
395 return NULL;
396 }
397
398 /**
399 * Implementation of x509_cert_t.get_validity.
400 */
401 static bool get_validity(private_x509_ocsp_request_t *this, time_t *when,
402 time_t *not_before, time_t *not_after)
403 {
404 certificate_t *cert;
405
406 if (this->cert)
407 {
408 cert = this->cert;
409 }
410 else
411 {
412 cert = (certificate_t*)this->ca;
413 }
414 return cert->get_validity(cert, when, not_before, not_after);
415 }
416
417 /**
418 * Implementation of certificate_t.get_encoding.
419 */
420 static chunk_t get_encoding(private_x509_ocsp_request_t *this)
421 {
422 return chunk_clone(this->encoding);
423 }
424
425 /**
426 * Implementation of certificate_t.equals.
427 */
428 static bool equals(private_x509_ocsp_request_t *this, certificate_t *other)
429 {
430 chunk_t encoding;
431 bool equal;
432
433 if (this == (private_x509_ocsp_request_t*)other)
434 {
435 return TRUE;
436 }
437 if (other->get_type(other) != CERT_X509_OCSP_REQUEST)
438 {
439 return FALSE;
440 }
441 if (other->equals == (void*)equals)
442 { /* skip allocation if we have the same implementation */
443 return chunk_equals(this->encoding, ((private_x509_ocsp_request_t*)other)->encoding);
444 }
445 encoding = other->get_encoding(other);
446 equal = chunk_equals(this->encoding, encoding);
447 free(encoding.ptr);
448 return equal;
449 }
450
451 /**
452 * Implementation of certificate_t.asdf
453 */
454 static private_x509_ocsp_request_t* get_ref(private_x509_ocsp_request_t *this)
455 {
456 ref_get(&this->ref);
457 return this;
458 }
459
460 /**
461 * Implementation of x509_ocsp_request_t.destroy
462 */
463 static void destroy(private_x509_ocsp_request_t *this)
464 {
465 if (ref_put(&this->ref))
466 {
467 DESTROY_IF((certificate_t*)this->ca);
468 DESTROY_IF(this->requestor);
469 DESTROY_IF(this->cert);
470 DESTROY_IF(this->key);
471 this->candidates->destroy_offset(this->candidates, offsetof(certificate_t, destroy));
472 chunk_free(&this->nonce);
473 chunk_free(&this->encoding);
474 free(this);
475 }
476 }
477
478 /**
479 * create an empty but initialized OCSP request
480 */
481 static private_x509_ocsp_request_t *create_empty()
482 {
483 private_x509_ocsp_request_t *this = malloc_thing(private_x509_ocsp_request_t);
484
485 this->public.interface.interface.get_type = (certificate_type_t (*)(certificate_t *this))get_type;
486 this->public.interface.interface.get_subject = (identification_t* (*)(certificate_t *this))get_subject;
487 this->public.interface.interface.get_issuer = (identification_t* (*)(certificate_t *this))get_issuer;
488 this->public.interface.interface.has_subject = (id_match_t(*)(certificate_t*, identification_t *subject))has_subject;
489 this->public.interface.interface.has_issuer = (id_match_t(*)(certificate_t*, identification_t *issuer))has_issuer;
490 this->public.interface.interface.issued_by = (bool (*)(certificate_t *this, certificate_t *issuer))issued_by;
491 this->public.interface.interface.get_public_key = (public_key_t* (*)(certificate_t *this))get_public_key;
492 this->public.interface.interface.get_validity = (bool(*)(certificate_t*, time_t *when, time_t *, time_t*))get_validity;
493 this->public.interface.interface.get_encoding = (chunk_t(*)(certificate_t*))get_encoding;
494 this->public.interface.interface.equals = (bool(*)(certificate_t*, certificate_t *other))equals;
495 this->public.interface.interface.get_ref = (certificate_t* (*)(certificate_t *this))get_ref;
496 this->public.interface.interface.destroy = (void (*)(certificate_t *this))destroy;
497
498 this->ca = NULL;
499 this->requestor = NULL;
500 this->cert = NULL;
501 this->key = NULL;
502 this->nonce = chunk_empty;
503 this->encoding = chunk_empty;
504 this->candidates = linked_list_create();
505 this->ref = 1;
506
507 return this;
508 }
509
510 typedef struct private_builder_t private_builder_t;
511 /**
512 * Builder implementation for certificate loading
513 */
514 struct private_builder_t {
515 /** implements the builder interface */
516 builder_t public;
517 /** OCSP request to build */
518 private_x509_ocsp_request_t *req;
519 };
520
521 /**
522 * Implementation of builder_t.build
523 */
524 static x509_ocsp_request_t *build(private_builder_t *this)
525 {
526 private_x509_ocsp_request_t *req;
527
528 req = this->req;
529 free(this);
530 if (req->ca)
531 {
532 req->encoding = build_OCSPRequest(req);
533 return &req->public;
534 }
535 destroy(req);
536 return NULL;
537 }
538
539 /**
540 * Implementation of builder_t.add
541 */
542 static void add(private_builder_t *this, builder_part_t part, ...)
543 {
544 va_list args;
545 certificate_t *cert;
546
547 va_start(args, part);
548 switch (part)
549 {
550 case BUILD_CA_CERT:
551 cert = va_arg(args, certificate_t*);
552 if (cert->get_type(cert) == CERT_X509)
553 {
554 this->req->ca = (x509_t*)cert;
555 }
556 else
557 {
558 cert->destroy(cert);
559 }
560 break;
561 case BUILD_CERT:
562 cert = va_arg(args, certificate_t*);
563 if (cert->get_type(cert) == CERT_X509)
564 {
565 this->req->candidates->insert_last(this->req->candidates, cert);
566 }
567 else
568 {
569 cert->destroy(cert);
570 }
571 break;
572 case BUILD_SIGNING_CERT:
573 this->req->cert = va_arg(args, certificate_t*);
574 break;
575 case BUILD_SIGNING_KEY:
576 this->req->key = va_arg(args, private_key_t*);
577 break;
578 case BUILD_SUBJECT:
579 this->req->requestor = va_arg(args, identification_t*);
580 break;
581 default:
582 DBG1("ignoring unsupported build part %N", builder_part_names, part);
583 break;
584 }
585 va_end(args);
586 }
587
588 /**
589 * Builder construction function
590 */
591 builder_t *x509_ocsp_request_builder(certificate_type_t type)
592 {
593 private_builder_t *this;
594
595 if (type != CERT_X509_OCSP_REQUEST)
596 {
597 return NULL;
598 }
599
600 this = malloc_thing(private_builder_t);
601
602 this->req = create_empty();
603 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
604 this->public.build = (void*(*)(builder_t *this))build;
605
606 return &this->public;
607 }
608