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