x509: Correctly encode nonce in OCSP request
[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_wrap(ASN1_OCTET_STRING, "m",
213 asn1_simple_object(ASN1_OCTET_STRING, this->nonce)));
214 }
215
216 /**
217 * build acceptableResponses extension
218 */
219 static chunk_t build_acceptableResponses(private_x509_ocsp_request_t *this)
220 {
221 return asn1_wrap(ASN1_SEQUENCE, "cc",
222 ASN1_response_oid,
223 ASN1_response_content);
224 }
225
226 /**
227 * build requestExtensions
228 */
229 static chunk_t build_requestExtensions(private_x509_ocsp_request_t *this)
230 {
231 return asn1_wrap(ASN1_CONTEXT_C_2, "m",
232 asn1_wrap(ASN1_SEQUENCE, "mm",
233 build_nonce(this),
234 build_acceptableResponses(this)));
235 }
236
237 /**
238 * build tbsRequest
239 */
240 static chunk_t build_tbsRequest(private_x509_ocsp_request_t *this)
241 {
242 return asn1_wrap(ASN1_SEQUENCE, "mmm",
243 build_requestorName(this),
244 build_requestList(this),
245 build_requestExtensions(this));
246 }
247
248 /**
249 * Build the optionalSignature
250 */
251 static chunk_t build_optionalSignature(private_x509_ocsp_request_t *this,
252 chunk_t tbsRequest)
253 {
254 int oid;
255 signature_scheme_t scheme;
256 chunk_t certs = chunk_empty, signature, encoding;
257
258 switch (this->key->get_type(this->key))
259 {
260 /* TODO: use a generic mapping function */
261 case KEY_RSA:
262 oid = OID_SHA1_WITH_RSA;
263 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
264 break;
265 case KEY_ECDSA:
266 oid = OID_ECDSA_WITH_SHA1;
267 scheme = SIGN_ECDSA_WITH_SHA1_DER;
268 break;
269 case KEY_BLISS:
270 oid = OID_BLISS_WITH_SHA2_512;
271 scheme = SIGN_BLISS_WITH_SHA2_512;
272 break;
273 default:
274 DBG1(DBG_LIB, "unable to sign OCSP request, %N signature not "
275 "supported", key_type_names, this->key->get_type(this->key));
276 return chunk_empty;
277 }
278
279 if (!this->key->sign(this->key, scheme, tbsRequest, &signature))
280 {
281 DBG1(DBG_LIB, "creating OCSP signature failed, skipped");
282 return chunk_empty;
283 }
284 if (this->cert &&
285 this->cert->get_encoding(this->cert, CERT_ASN1_DER, &encoding))
286 {
287 certs = asn1_wrap(ASN1_CONTEXT_C_0, "m",
288 asn1_wrap(ASN1_SEQUENCE, "m", encoding));
289 }
290 return asn1_wrap(ASN1_CONTEXT_C_0, "m",
291 asn1_wrap(ASN1_SEQUENCE, "cmm",
292 asn1_algorithmIdentifier(oid),
293 asn1_bitstring("m", signature),
294 certs));
295 }
296
297 /**
298 * Build the OCSPRequest data
299 *
300 */
301 static chunk_t build_OCSPRequest(private_x509_ocsp_request_t *this)
302 {
303 chunk_t tbsRequest, optionalSignature = chunk_empty;
304
305 tbsRequest = build_tbsRequest(this);
306 if (this->key)
307 {
308 optionalSignature = build_optionalSignature(this, tbsRequest);
309 }
310 return asn1_wrap(ASN1_SEQUENCE, "mm", tbsRequest, optionalSignature);
311 }
312
313
314 METHOD(certificate_t, get_type, certificate_type_t,
315 private_x509_ocsp_request_t *this)
316 {
317 return CERT_X509_OCSP_REQUEST;
318 }
319
320 METHOD(certificate_t, get_subject, identification_t*,
321 private_x509_ocsp_request_t *this)
322 {
323 certificate_t *ca = (certificate_t*)this->ca;
324
325 if (this->requestor)
326 {
327 return this->requestor;
328 }
329 if (this->cert)
330 {
331 return this->cert->get_subject(this->cert);
332 }
333 return ca->get_subject(ca);
334 }
335
336 METHOD(certificate_t, get_issuer, identification_t*,
337 private_x509_ocsp_request_t *this)
338 {
339 certificate_t *ca = (certificate_t*)this->ca;
340
341 return ca->get_subject(ca);
342 }
343
344 METHOD(certificate_t, has_subject, id_match_t,
345 private_x509_ocsp_request_t *this, identification_t *subject)
346 {
347 certificate_t *current;
348 enumerator_t *enumerator;
349 id_match_t match, best = ID_MATCH_NONE;
350
351 enumerator = this->candidates->create_enumerator(this->candidates);
352 while (enumerator->enumerate(enumerator, &current))
353 {
354 match = current->has_subject(current, subject);
355 if (match > best)
356 {
357 best = match;
358 }
359 }
360 enumerator->destroy(enumerator);
361 return best;
362 }
363
364 METHOD(certificate_t, has_issuer, id_match_t,
365 private_x509_ocsp_request_t *this,
366 identification_t *issuer)
367 {
368 certificate_t *ca = (certificate_t*)this->ca;
369
370 return ca->has_subject(ca, issuer);
371 }
372
373 METHOD(certificate_t, issued_by, bool,
374 private_x509_ocsp_request_t *this, certificate_t *issuer,
375 signature_scheme_t *scheme)
376 {
377 DBG1(DBG_LIB, "OCSP request validation not implemented!");
378 return FALSE;
379 }
380
381 METHOD(certificate_t, get_public_key, public_key_t*,
382 private_x509_ocsp_request_t *this)
383 {
384 return NULL;
385 }
386
387 METHOD(certificate_t, get_validity, bool,
388 private_x509_ocsp_request_t *this, time_t *when, time_t *not_before,
389 time_t *not_after)
390 {
391 certificate_t *cert;
392
393 if (this->cert)
394 {
395 cert = this->cert;
396 }
397 else
398 {
399 cert = (certificate_t*)this->ca;
400 }
401 return cert->get_validity(cert, when, not_before, not_after);
402 }
403
404 METHOD(certificate_t, get_encoding, bool,
405 private_x509_ocsp_request_t *this, cred_encoding_type_t type,
406 chunk_t *encoding)
407 {
408 if (type == CERT_ASN1_DER)
409 {
410 *encoding = chunk_clone(this->encoding);
411 return TRUE;
412 }
413 return lib->encoding->encode(lib->encoding, type, NULL, encoding,
414 CRED_PART_X509_OCSP_REQ_ASN1_DER, this->encoding, CRED_PART_END);
415 }
416
417 METHOD(certificate_t, equals, bool,
418 private_x509_ocsp_request_t *this, certificate_t *other)
419 {
420 chunk_t encoding;
421 bool equal;
422
423 if (this == (private_x509_ocsp_request_t*)other)
424 {
425 return TRUE;
426 }
427 if (other->get_type(other) != CERT_X509_OCSP_REQUEST)
428 {
429 return FALSE;
430 }
431 if (other->equals == (void*)equals)
432 { /* skip allocation if we have the same implementation */
433 return chunk_equals(this->encoding, ((private_x509_ocsp_request_t*)other)->encoding);
434 }
435 if (!other->get_encoding(other, CERT_ASN1_DER, &encoding))
436 {
437 return FALSE;
438 }
439 equal = chunk_equals(this->encoding, encoding);
440 free(encoding.ptr);
441 return equal;
442 }
443
444 METHOD(certificate_t, get_ref, certificate_t*,
445 private_x509_ocsp_request_t *this)
446 {
447 ref_get(&this->ref);
448 return &this->public.interface.interface;
449 }
450
451 METHOD(certificate_t, destroy, void,
452 private_x509_ocsp_request_t *this)
453 {
454 if (ref_put(&this->ref))
455 {
456 DESTROY_IF((certificate_t*)this->ca);
457 DESTROY_IF(this->requestor);
458 DESTROY_IF(this->cert);
459 DESTROY_IF(this->key);
460 this->candidates->destroy_offset(this->candidates, offsetof(certificate_t, destroy));
461 chunk_free(&this->nonce);
462 chunk_free(&this->encoding);
463 free(this);
464 }
465 }
466
467 /**
468 * create an empty but initialized OCSP request
469 */
470 static private_x509_ocsp_request_t *create_empty()
471 {
472 private_x509_ocsp_request_t *this;
473
474 INIT(this,
475 .public = {
476 .interface = {
477 .interface = {
478 .get_type = _get_type,
479 .get_subject = _get_subject,
480 .get_issuer = _get_issuer,
481 .has_subject = _has_subject,
482 .has_issuer = _has_issuer,
483 .issued_by = _issued_by,
484 .get_public_key = _get_public_key,
485 .get_validity = _get_validity,
486 .get_encoding = _get_encoding,
487 .equals = _equals,
488 .get_ref = _get_ref,
489 .destroy = _destroy,
490 },
491 },
492 },
493 .candidates = linked_list_create(),
494 .ref = 1,
495 );
496
497 return this;
498 }
499
500 /**
501 * See header.
502 */
503 x509_ocsp_request_t *x509_ocsp_request_gen(certificate_type_t type, va_list args)
504 {
505 private_x509_ocsp_request_t *req;
506 certificate_t *cert;
507 private_key_t *private;
508 identification_t *subject;
509
510 req = create_empty();
511 while (TRUE)
512 {
513 switch (va_arg(args, builder_part_t))
514 {
515 case BUILD_CA_CERT:
516 cert = va_arg(args, certificate_t*);
517 if (cert->get_type(cert) == CERT_X509)
518 {
519 req->ca = (x509_t*)cert->get_ref(cert);
520 }
521 continue;
522 case BUILD_CERT:
523 cert = va_arg(args, certificate_t*);
524 if (cert->get_type(cert) == CERT_X509)
525 {
526 req->candidates->insert_last(req->candidates,
527 cert->get_ref(cert));
528 }
529 continue;
530 case BUILD_SIGNING_CERT:
531 cert = va_arg(args, certificate_t*);
532 req->cert = cert->get_ref(cert);
533 continue;
534 case BUILD_SIGNING_KEY:
535 private = va_arg(args, private_key_t*);
536 req->key = private->get_ref(private);
537 continue;
538 case BUILD_SUBJECT:
539 subject = va_arg(args, identification_t*);
540 req->requestor = subject->clone(subject);
541 continue;
542 case BUILD_END:
543 break;
544 default:
545 destroy(req);
546 return NULL;
547 }
548 break;
549 }
550 if (req->ca)
551 {
552 req->encoding = build_OCSPRequest(req);
553 return &req->public;
554 }
555 destroy(req);
556 return NULL;
557 }
558