fixed a memory leak in response_t
[strongswan.git] / src / libstrongswan / crypto / ocsp.c
1 /**
2 * @file ocsp.c
3 *
4 * @brief Implementation of ocsp_t.
5 *
6 */
7
8 /* Support of the Online Certificate Status Protocol (OCSP)
9 * Copyright (C) 2003 Christoph Gysin, Simon Zwahlen
10 * Zuercher Hochschule Winterthur
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 */
23
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32
33 #include <asn1/oid.h>
34 #include <asn1/asn1.h>
35 #include <utils/identification.h>
36 #include <utils/randomizer.h>
37 #include <utils/fetcher.h>
38 #include <debug.h>
39
40 #include "hashers/hasher.h"
41 #include "rsa/rsa_public_key.h"
42 #include "certinfo.h"
43 #include "x509.h"
44 #include "ocsp.h"
45
46 #define NONCE_LENGTH 16
47
48 typedef struct private_ocsp_t private_ocsp_t;
49
50 /**
51 * Private data of a ocsp_t object.
52 */
53 struct private_ocsp_t {
54 /**
55 * Public interface for this ocsp object.
56 */
57 ocsp_t public;
58
59 /**
60 * CA certificate.
61 */
62 x509_t *cacert;
63
64 /**
65 * Requestor certificate
66 */
67 x509_t *requestor_cert;
68
69 /**
70 * Linked list of ocsp uris
71 */
72 linked_list_t *uris;
73
74 /**
75 * Linked list of certinfos to be requested
76 */
77 linked_list_t *certinfos;
78
79 /**
80 * Nonce required for ocsp request and response
81 */
82 chunk_t nonce;
83
84 /**
85 * SHA-1 hash over issuer distinguished name
86 */
87 chunk_t authNameID;
88 };
89
90 ENUM(response_status_names, STATUS_SUCCESSFUL, STATUS_UNAUTHORIZED,
91 "successful",
92 "malformed request",
93 "internal error",
94 "try later",
95 "signature required",
96 "unauthorized"
97 );
98
99 /* response container */
100 typedef struct response_t response_t;
101
102 struct response_t {
103 chunk_t chunk;
104 chunk_t tbs;
105 identification_t *responder_id_name;
106 chunk_t responder_id_key;
107 time_t produced_at;
108 chunk_t responses;
109 chunk_t nonce;
110 int algorithm;
111 chunk_t signature;
112 x509_t *responder_cert;
113
114 /**
115 * @brief Destroys the response_t object
116 *
117 * @param this response_t to destroy
118 */
119 void (*destroy) (response_t *this);
120 };
121
122 /**
123 * Implements response_t.destroy.
124 */
125 static void response_destroy(response_t *this)
126 {
127 DESTROY_IF(this->responder_id_name);
128 DESTROY_IF(this->responder_cert);
129 free(this->chunk.ptr);
130 free(this);
131 }
132
133 /**
134 * Creates a response_t object
135 */
136 static response_t* response_create_from_chunk(chunk_t chunk)
137 {
138 response_t *this = malloc_thing(response_t);
139
140 this->chunk = chunk;
141 this->tbs = chunk_empty;
142 this->responder_id_name = NULL;
143 this->responder_id_key = chunk_empty;
144 this->produced_at = UNDEFINED_TIME;
145 this->responses = chunk_empty;
146 this->nonce = chunk_empty;
147 this->algorithm = OID_UNKNOWN;
148 this->signature = chunk_empty;
149 this->responder_cert = NULL;
150
151 this->destroy = (void (*) (response_t*))response_destroy;
152
153 return this;
154 }
155
156 /* single response container */
157 typedef struct single_response single_response_t;
158
159 struct single_response {
160 single_response_t *next;
161 int hash_algorithm;
162 chunk_t issuer_name_hash;
163 chunk_t issuer_key_hash;
164 chunk_t serialNumber;
165 cert_status_t status;
166 time_t revocationTime;
167 crl_reason_t revocationReason;
168 time_t thisUpdate;
169 time_t nextUpdate;
170 };
171
172 const single_response_t empty_single_response = {
173 NULL , /* *next */
174 OID_UNKNOWN , /* hash_algorithm */
175 { NULL, 0 } , /* issuer_name_hash */
176 { NULL, 0 } , /* issuer_key_hash */
177 { NULL, 0 } , /* serial_number */
178 CERT_UNDEFINED , /* status */
179 UNDEFINED_TIME , /* revocationTime */
180 REASON_UNSPECIFIED, /* revocationReason */
181 UNDEFINED_TIME , /* this_update */
182 UNDEFINED_TIME /* next_update */
183 };
184
185 /* some OCSP specific prefabricated ASN.1 constants */
186
187 static u_char ASN1_nonce_oid_str[] = {
188 0x06, 0x09,
189 0x2B, 0x06,
190 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x02
191 };
192
193 static u_char ASN1_response_oid_str[] = {
194 0x06, 0x09,
195 0x2B, 0x06,
196 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x04
197 };
198
199 static u_char ASN1_response_content_str[] = {
200 0x04, 0x0D,
201 0x30, 0x0B,
202 0x06, 0x09,
203 0x2B, 0x06,
204 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01
205 };
206
207 static const chunk_t ASN1_nonce_oid = chunk_from_buf(ASN1_nonce_oid_str);
208 static const chunk_t ASN1_response_oid = chunk_from_buf(ASN1_response_oid_str);
209 static const chunk_t ASN1_response_content = chunk_from_buf(ASN1_response_content_str);
210
211 /* asn.1 definitions for parsing */
212
213 static const asn1Object_t ocspResponseObjects[] = {
214 { 0, "OCSPResponse", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
215 { 1, "responseStatus", ASN1_ENUMERATED, ASN1_BODY }, /* 1 */
216 { 1, "responseBytesContext", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 2 */
217 { 2, "responseBytes", ASN1_SEQUENCE, ASN1_NONE }, /* 3 */
218 { 3, "responseType", ASN1_OID, ASN1_BODY }, /* 4 */
219 { 3, "response", ASN1_OCTET_STRING, ASN1_BODY }, /* 5 */
220 { 1, "end opt", ASN1_EOC, ASN1_END } /* 6 */
221 };
222
223 #define OCSP_RESPONSE_STATUS 1
224 #define OCSP_RESPONSE_TYPE 4
225 #define OCSP_RESPONSE 5
226 #define OCSP_RESPONSE_ROOF 7
227
228 static const asn1Object_t basicResponseObjects[] = {
229 { 0, "BasicOCSPResponse", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
230 { 1, "tbsResponseData", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
231 { 2, "versionContext", ASN1_CONTEXT_C_0, ASN1_NONE |
232 ASN1_DEF }, /* 2 */
233 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
234 { 2, "responderIdContext", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 4 */
235 { 3, "responderIdByName", ASN1_SEQUENCE, ASN1_OBJ }, /* 5 */
236 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
237 { 2, "responderIdContext", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 7 */
238 { 3, "responderIdByKey", ASN1_OCTET_STRING, ASN1_BODY }, /* 8 */
239 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
240 { 2, "producedAt", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 10 */
241 { 2, "responses", ASN1_SEQUENCE, ASN1_OBJ }, /* 11 */
242 { 2, "responseExtensionsContext", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
243 { 3, "responseExtensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 13 */
244 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 14 */
245 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 15 */
246 { 5, "critical", ASN1_BOOLEAN, ASN1_BODY |
247 ASN1_DEF }, /* 16 */
248 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 17 */
249 { 4, "end loop", ASN1_EOC, ASN1_END }, /* 18 */
250 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 19 */
251 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 20 */
252 { 1, "signature", ASN1_BIT_STRING, ASN1_BODY }, /* 21 */
253 { 1, "certsContext", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 22 */
254 { 2, "certs", ASN1_SEQUENCE, ASN1_LOOP }, /* 23 */
255 { 3, "certificate", ASN1_SEQUENCE, ASN1_RAW }, /* 24 */
256 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 25 */
257 { 1, "end opt", ASN1_EOC, ASN1_END } /* 26 */
258 };
259
260 #define BASIC_RESPONSE_TBS_DATA 1
261 #define BASIC_RESPONSE_VERSION 3
262 #define BASIC_RESPONSE_ID_BY_NAME 5
263 #define BASIC_RESPONSE_ID_BY_KEY 8
264 #define BASIC_RESPONSE_PRODUCED_AT 10
265 #define BASIC_RESPONSE_RESPONSES 11
266 #define BASIC_RESPONSE_EXT_ID 15
267 #define BASIC_RESPONSE_CRITICAL 16
268 #define BASIC_RESPONSE_EXT_VALUE 17
269 #define BASIC_RESPONSE_ALGORITHM 20
270 #define BASIC_RESPONSE_SIGNATURE 21
271 #define BASIC_RESPONSE_CERTIFICATE 24
272 #define BASIC_RESPONSE_ROOF 27
273
274 static const asn1Object_t responsesObjects[] = {
275 { 0, "responses", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
276 { 1, "singleResponse", ASN1_EOC, ASN1_RAW }, /* 1 */
277 { 0, "end loop", ASN1_EOC, ASN1_END } /* 2 */
278 };
279
280 #define RESPONSES_SINGLE_RESPONSE 1
281 #define RESPONSES_ROOF 3
282
283 static const asn1Object_t singleResponseObjects[] = {
284 { 0, "singleResponse", ASN1_SEQUENCE, ASN1_BODY }, /* 0 */
285 { 1, "certID", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
286 { 2, "algorithm", ASN1_EOC, ASN1_RAW }, /* 2 */
287 { 2, "issuerNameHash", ASN1_OCTET_STRING, ASN1_BODY }, /* 3 */
288 { 2, "issuerKeyHash", ASN1_OCTET_STRING, ASN1_BODY }, /* 4 */
289 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 5 */
290 { 1, "certStatusGood", ASN1_CONTEXT_S_0, ASN1_OPT }, /* 6 */
291 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
292 { 1, "certStatusRevoked", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 8 */
293 { 2, "revocationTime", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 9 */
294 { 2, "revocationReason", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 10 */
295 { 3, "crlReason", ASN1_ENUMERATED, ASN1_BODY }, /* 11 */
296 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 12 */
297 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
298 { 1, "certStatusUnknown", ASN1_CONTEXT_S_2, ASN1_OPT }, /* 14 */
299 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
300 { 1, "thisUpdate", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 16 */
301 { 1, "nextUpdateContext", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 17 */
302 { 2, "nextUpdate", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 18 */
303 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 19 */
304 { 1, "singleExtensionsContext", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 20 */
305 { 2, "singleExtensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 21 */
306 { 3, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 22 */
307 { 4, "extnID", ASN1_OID, ASN1_BODY }, /* 23 */
308 { 4, "critical", ASN1_BOOLEAN, ASN1_BODY |
309 ASN1_DEF }, /* 24 */
310 { 4, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 25 */
311 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 26 */
312 { 1, "end opt", ASN1_EOC, ASN1_END } /* 27 */
313 };
314
315 #define SINGLE_RESPONSE_ALGORITHM 2
316 #define SINGLE_RESPONSE_ISSUER_NAME_HASH 3
317 #define SINGLE_RESPONSE_ISSUER_KEY_HASH 4
318 #define SINGLE_RESPONSE_SERIAL_NUMBER 5
319 #define SINGLE_RESPONSE_CERT_STATUS_GOOD 6
320 #define SINGLE_RESPONSE_CERT_STATUS_REVOKED 8
321 #define SINGLE_RESPONSE_CERT_STATUS_REVOCATION_TIME 9
322 #define SINGLE_RESPONSE_CERT_STATUS_CRL_REASON 11
323 #define SINGLE_RESPONSE_CERT_STATUS_UNKNOWN 14
324 #define SINGLE_RESPONSE_THIS_UPDATE 16
325 #define SINGLE_RESPONSE_NEXT_UPDATE 18
326 #define SINGLE_RESPONSE_EXT_ID 23
327 #define SINGLE_RESPONSE_CRITICAL 24
328 #define SINGLE_RESPONSE_EXT_VALUE 25
329 #define SINGLE_RESPONSE_ROOF 28
330
331 /**
332 * build requestorName (into TBSRequest)
333 */
334 static chunk_t build_requestor_name(private_ocsp_t *this)
335 {
336 identification_t *requestor_name = this->requestor_cert->get_subject(this->requestor_cert);
337
338 return asn1_wrap(ASN1_CONTEXT_C_1, "m",
339 asn1_simple_object(ASN1_CONTEXT_C_4,
340 requestor_name->get_encoding(requestor_name)));
341 }
342
343 /**
344 * build request (into requestList)
345 * no singleRequestExtensions used
346 */
347 static chunk_t build_request(private_ocsp_t *this, certinfo_t *certinfo)
348 {
349 chunk_t authKeyID = this->cacert->get_subjectKeyID(this->cacert);
350 chunk_t serialNumber = certinfo->get_serialNumber(certinfo);
351
352 chunk_t reqCert = asn1_wrap(ASN1_SEQUENCE, "cmmm",
353 ASN1_sha1_id,
354 asn1_simple_object(ASN1_OCTET_STRING, this->authNameID),
355 asn1_simple_object(ASN1_OCTET_STRING, authKeyID),
356 asn1_simple_object(ASN1_INTEGER, serialNumber));
357
358 return asn1_wrap(ASN1_SEQUENCE, "m", reqCert);
359 }
360
361 /**
362 * build requestList (into TBSRequest)
363 */
364 static chunk_t build_request_list(private_ocsp_t *this)
365 {
366 chunk_t requestList;
367 size_t datalen = 0;
368 linked_list_t *request_list = linked_list_create();
369
370 {
371 iterator_t *iterator = this->certinfos->create_iterator(this->certinfos, TRUE);
372 certinfo_t *certinfo;
373
374 while (iterator->iterate(iterator, (void**)&certinfo))
375 {
376 chunk_t *request = malloc_thing(chunk_t);
377
378 *request = build_request(this, certinfo);
379 request_list->insert_last(request_list, (void*)request);
380 datalen += request->len;
381 }
382 iterator->destroy(iterator);
383 }
384 {
385 iterator_t *iterator = request_list->create_iterator(request_list, TRUE);
386 chunk_t *request;
387
388 u_char *pos = build_asn1_object(&requestList, ASN1_SEQUENCE, datalen);
389
390 while (iterator->iterate(iterator, (void**)&request))
391 {
392 memcpy(pos, request->ptr, request->len);
393 pos += request->len;
394 free(request->ptr);
395 free(request);
396 }
397 iterator->destroy(iterator);
398 request_list->destroy(request_list);
399 }
400 return requestList;
401 }
402
403 /**
404 * build nonce extension (into requestExtensions)
405 */
406 static chunk_t build_nonce_extension(private_ocsp_t *this)
407 {
408 randomizer_t *randomizer = randomizer_create();
409
410 /* generate a random nonce */
411 randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_LENGTH, &this->nonce);
412 randomizer->destroy(randomizer);
413
414 return asn1_wrap(ASN1_SEQUENCE, "cm",
415 ASN1_nonce_oid,
416 asn1_simple_object(ASN1_OCTET_STRING, this->nonce));
417 }
418
419 /**
420 * build requestExtensions (into TBSRequest)
421 */
422 static chunk_t build_request_ext(private_ocsp_t *this)
423 {
424 return asn1_wrap(ASN1_CONTEXT_C_2, "m",
425 asn1_wrap(ASN1_SEQUENCE, "mm",
426 build_nonce_extension(this),
427 asn1_wrap(ASN1_SEQUENCE, "cc",
428 ASN1_response_oid,
429 ASN1_response_content
430 )
431 )
432 );
433 }
434
435 /**
436 * build TBSRequest (into OCSPRequest)
437 */
438 static chunk_t build_tbs_request(private_ocsp_t *this, bool has_requestor_cert)
439 {
440 /* version is skipped since the default is ok */
441 return asn1_wrap(ASN1_SEQUENCE, "mmm",
442 (has_requestor_cert)? build_requestor_name(this): chunk_empty,
443 build_request_list(this),
444 build_request_ext(this));
445 }
446
447 /**
448 * build signature into ocsp request
449 * gets built only if a request cert with a corresponding private key is found
450 */
451 static chunk_t build_signature(private_ocsp_t *this, chunk_t tbsRequest)
452 {
453 /* TODO */
454 return chunk_empty;
455 }
456
457 /**
458 * assembles an ocsp request and sets the nonce field in private_ocsp_t to the sent nonce
459 */
460 static chunk_t ocsp_build_request(private_ocsp_t *this)
461 {
462 bool has_requestor_cert;
463 chunk_t keyid = this->cacert->get_keyid(this->cacert);
464 chunk_t tbsRequest, signature;
465
466 DBG2("assembling ocsp request");
467 DBG2("issuer: '%D'", this->cacert->get_subject(this->cacert));
468 DBG2("keyid: %#B", &keyid);
469
470 /* looks for requestor cert and matching private key */
471 has_requestor_cert = FALSE;
472
473 /* TODO has_requestor_cert = get_ocsp_requestor_cert(location); */
474
475 /* build content */
476 tbsRequest = build_tbs_request(this, has_requestor_cert);
477
478 /* sign tbsReuqest */
479 signature = (has_requestor_cert)? build_signature(this, tbsRequest): chunk_empty;
480
481 return asn1_wrap(ASN1_SEQUENCE, "mm",
482 tbsRequest,
483 signature);
484
485 return signature;
486 }
487
488 /**
489 * parse a basic OCSP response
490 */
491 static bool ocsp_parse_basic_response(chunk_t blob, int level0, response_t *res)
492 {
493 u_int level, version;
494 u_int extn_oid = OID_UNKNOWN;
495 u_char buf[BUF_LEN];
496 asn1_ctx_t ctx;
497 bool critical;
498 chunk_t object;
499 int objectID = 0;
500
501 asn1_init(&ctx, blob, level0, FALSE, FALSE);
502
503 while (objectID < BASIC_RESPONSE_ROOF)
504 {
505 if (!extract_object(basicResponseObjects, &objectID, &object, &level, &ctx))
506 {
507 return FALSE;
508 }
509
510 switch (objectID)
511 {
512 case BASIC_RESPONSE_TBS_DATA:
513 res->tbs = object;
514 break;
515 case BASIC_RESPONSE_VERSION:
516 version = (object.len)? (1 + (u_int)*object.ptr) : 1;
517 if (version != OCSP_BASIC_RESPONSE_VERSION)
518 {
519 DBG1("wrong ocsp basic response version (version= %i)", version);
520 return FALSE;
521 }
522 break;
523 case BASIC_RESPONSE_ID_BY_NAME:
524 res->responder_id_name = identification_create_from_encoding(ID_DER_ASN1_DN, object);
525 DBG2(" '%D'", res->responder_id_name);
526 break;
527 case BASIC_RESPONSE_ID_BY_KEY:
528 res->responder_id_key = object;
529 break;
530 case BASIC_RESPONSE_PRODUCED_AT:
531 res->produced_at = asn1totime(&object, ASN1_GENERALIZEDTIME);
532 break;
533 case BASIC_RESPONSE_RESPONSES:
534 res->responses = object;
535 break;
536 case BASIC_RESPONSE_EXT_ID:
537 extn_oid = known_oid(object);
538 break;
539 case BASIC_RESPONSE_CRITICAL:
540 critical = object.len && *object.ptr;
541 DBG2(" %s", critical? "TRUE" : "FALSE");
542 break;
543 case BASIC_RESPONSE_EXT_VALUE:
544 if (extn_oid == OID_NONCE)
545 res->nonce = object;
546 break;
547 case BASIC_RESPONSE_ALGORITHM:
548 res->algorithm = parse_algorithmIdentifier(object, level+1, NULL);
549 break;
550 case BASIC_RESPONSE_SIGNATURE:
551 res->signature = object;
552 break;
553 case BASIC_RESPONSE_CERTIFICATE:
554 {
555 chunk_t blob = chunk_clone(object);
556
557 res->responder_cert = x509_create_from_chunk(blob, level+1);
558 }
559 break;
560 }
561 objectID++;
562 }
563 return TRUE;
564 }
565
566 /**
567 * parse an ocsp response and return the result as a response_t struct
568 */
569 static response_status ocsp_parse_response(response_t *res)
570 {
571 asn1_ctx_t ctx;
572 chunk_t object;
573 u_int level;
574 int objectID = 0;
575
576 response_status rStatus = STATUS_INTERNALERROR;
577 u_int ocspResponseType = OID_UNKNOWN;
578
579 asn1_init(&ctx, res->chunk, 0, FALSE, FALSE);
580
581 while (objectID < OCSP_RESPONSE_ROOF)
582 {
583 if (!extract_object(ocspResponseObjects, &objectID, &object, &level, &ctx))
584 {
585 return STATUS_INTERNALERROR;
586 }
587
588 switch (objectID)
589 {
590 case OCSP_RESPONSE_STATUS:
591 rStatus = (response_status) *object.ptr;
592 DBG2(" '%N'", response_status_names, rStatus);
593
594 switch (rStatus)
595 {
596 case STATUS_SUCCESSFUL:
597 break;
598 case STATUS_MALFORMEDREQUEST:
599 case STATUS_INTERNALERROR:
600 case STATUS_TRYLATER:
601 case STATUS_SIGREQUIRED:
602 case STATUS_UNAUTHORIZED:
603 DBG1("unsuccessful ocsp response: server said '%N'",
604 response_status_names, rStatus);
605 return rStatus;
606 default:
607 return STATUS_INTERNALERROR;
608 }
609 break;
610 case OCSP_RESPONSE_TYPE:
611 ocspResponseType = known_oid(object);
612 break;
613 case OCSP_RESPONSE:
614 {
615 switch (ocspResponseType)
616 {
617 case OID_BASIC:
618 if (!ocsp_parse_basic_response(object, level+1, res))
619 {
620 return STATUS_INTERNALERROR;
621 }
622 break;
623 default:
624 DBG1("ocsp response is not of type BASIC");
625 DBG1("ocsp response OID: %#B", &object);
626 return STATUS_INTERNALERROR;
627 }
628 }
629 break;
630 }
631 objectID++;
632 }
633 return rStatus;
634 }
635
636 /**
637 * Check if the OCSP response has a valid signature
638 */
639 static bool ocsp_valid_response(response_t *res, x509_t *ocsp_cert)
640 {
641 rsa_public_key_t *public_key;
642 time_t until = UNDEFINED_TIME;
643 err_t ugh;
644
645 DBG2("verifying ocsp response signature:");
646 DBG2("signer: '%D'", ocsp_cert->get_subject(ocsp_cert));
647 DBG2("issuer: '%D'", ocsp_cert->get_issuer(ocsp_cert));
648
649 ugh = ocsp_cert->is_valid(ocsp_cert, &until);
650 if (ugh != NULL)
651 {
652 DBG1("ocsp signer certificate %s", ugh);
653 return FALSE;
654 }
655 public_key = ocsp_cert->get_public_key(ocsp_cert);
656
657 return public_key->verify_emsa_pkcs1_signature(public_key, res->tbs, res->signature) == SUCCESS;
658 }
659
660 /**
661 * parse a single OCSP response
662 */
663 static bool ocsp_parse_single_response(chunk_t blob, int level0, single_response_t *sres)
664 {
665 u_int level, extn_oid;
666 asn1_ctx_t ctx;
667 bool critical;
668 chunk_t object;
669 int objectID = 0;
670
671 asn1_init(&ctx, blob, level0, FALSE, FALSE);
672
673 while (objectID < SINGLE_RESPONSE_ROOF)
674 {
675 if (!extract_object(singleResponseObjects, &objectID, &object, &level, &ctx))
676 {
677 return FALSE;
678 }
679
680 switch (objectID)
681 {
682 case SINGLE_RESPONSE_ALGORITHM:
683 sres->hash_algorithm = parse_algorithmIdentifier(object, level+1, NULL);
684 break;
685 case SINGLE_RESPONSE_ISSUER_NAME_HASH:
686 sres->issuer_name_hash = object;
687 break;
688 case SINGLE_RESPONSE_ISSUER_KEY_HASH:
689 sres->issuer_key_hash = object;
690 break;
691 case SINGLE_RESPONSE_SERIAL_NUMBER:
692 sres->serialNumber = object;
693 break;
694 case SINGLE_RESPONSE_CERT_STATUS_GOOD:
695 sres->status = CERT_GOOD;
696 break;
697 case SINGLE_RESPONSE_CERT_STATUS_REVOKED:
698 sres->status = CERT_REVOKED;
699 break;
700 case SINGLE_RESPONSE_CERT_STATUS_REVOCATION_TIME:
701 sres->revocationTime = asn1totime(&object, ASN1_GENERALIZEDTIME);
702 break;
703 case SINGLE_RESPONSE_CERT_STATUS_CRL_REASON:
704 sres->revocationReason = (object.len == 1)
705 ? *object.ptr : REASON_UNSPECIFIED;
706 break;
707 case SINGLE_RESPONSE_CERT_STATUS_UNKNOWN:
708 sres->status = CERT_UNKNOWN;
709 break;
710 case SINGLE_RESPONSE_THIS_UPDATE:
711 sres->thisUpdate = asn1totime(&object, ASN1_GENERALIZEDTIME);
712 break;
713 case SINGLE_RESPONSE_NEXT_UPDATE:
714 sres->nextUpdate = asn1totime(&object, ASN1_GENERALIZEDTIME);
715 break;
716 case SINGLE_RESPONSE_EXT_ID:
717 extn_oid = known_oid(object);
718 break;
719 case SINGLE_RESPONSE_CRITICAL:
720 critical = object.len && *object.ptr;
721 DBG2(" %s", critical ? "TRUE" : "FALSE");
722 case SINGLE_RESPONSE_EXT_VALUE:
723 break;
724 }
725 objectID++;
726 }
727 return TRUE;
728 }
729
730 /**
731 * process received ocsp single response and add it to ocsp cache
732 */
733 static void process_single_response(private_ocsp_t *this, single_response_t *sres)
734 {
735 /* TODO */
736 }
737
738 /**
739 * verify and process ocsp response and update the ocsp cache
740 */
741 static void ocsp_process_response(private_ocsp_t *this, response_t *res, credential_store_t *credentials)
742 {
743 x509_t *ocsp_cert = NULL;
744
745 /* parse the ocsp response without looking at the single responses yet */
746 response_status status = ocsp_parse_response(res);
747
748 if (status != STATUS_SUCCESSFUL)
749 {
750 DBG1("error in ocsp response");
751 return;
752 }
753
754 /* check if there was a nonce in the request */
755 if (this->nonce.ptr != NULL && res->nonce.ptr == NULL)
756 {
757 DBG1("ocsp response contains no nonce, replay attack possible");
758 }
759
760 /* check if the nonces are identical */
761 if (res->nonce.ptr != NULL && !chunk_equals(res->nonce, this->nonce))
762 {
763 DBG1("invalid nonce in ocsp response");
764 return;
765 }
766
767 /* check if we received a trusted responder certificate */
768 if (res->responder_cert)
769 {
770 if (res->responder_cert->is_ocsp_signer(res->responder_cert))
771 {
772 DBG2("received certificate is ocsp signer");
773 if (credentials->is_trusted(credentials, res->responder_cert))
774 {
775 DBG2("received ocsp signer certificate is trusted");
776 ocsp_cert = credentials->add_auth_certificate(credentials,
777 res->responder_cert, AUTH_OCSP);
778 res->responder_cert = NULL;
779 }
780 else
781 {
782 DBG1("received ocsp signer certificate is not trusted - rejected");
783 }
784 }
785 else
786 {
787 DBG1("received certificate is no ocsp signer - rejected");
788 }
789 }
790
791 /* if we didn't receive a trusted responder cert, search the credential store */
792 if (ocsp_cert == NULL)
793 {
794 ocsp_cert = credentials->get_auth_certificate(credentials,
795 AUTH_OCSP|AUTH_CA, res->responder_id_name);
796 if (ocsp_cert == NULL)
797 {
798 DBG1("no ocsp signer certificate found");
799 return;
800 }
801 }
802
803 /* check the response signature */
804 if (!ocsp_valid_response(res, ocsp_cert))
805 {
806 DBG1("ocsp response signature is invalid");
807 return;
808 }
809 DBG2("ocsp response signature is valid");
810
811 /* now parse the single responses one at a time */
812 {
813 u_int level;
814 asn1_ctx_t ctx;
815 chunk_t object;
816 int objectID = 0;
817
818 asn1_init(&ctx, res->responses, 0, FALSE, FALSE);
819
820 while (objectID < RESPONSES_ROOF)
821 {
822 if (!extract_object(responsesObjects, &objectID, &object, &level, &ctx))
823 {
824 return;
825 }
826 if (objectID == RESPONSES_SINGLE_RESPONSE)
827 {
828 single_response_t sres = empty_single_response;
829
830 if (ocsp_parse_single_response(object, level+1, &sres))
831 {
832 process_single_response(this, &sres);
833 }
834 }
835 objectID++;
836 }
837 }
838 }
839
840 /**
841 * Implements ocsp_t.fetch.
842 */
843 static void fetch(private_ocsp_t *this, certinfo_t *certinfo, credential_store_t *credentials)
844 {
845 chunk_t request;
846 response_t *response = NULL;
847
848 if (this->uris->get_count(this->uris) == 0)
849 {
850 return;
851 }
852 this->certinfos->insert_last(this->certinfos, (void*)certinfo);
853
854 request = ocsp_build_request(this);
855 DBG3("ocsp request: %B", &request);
856 {
857 iterator_t *iterator = this->uris->create_iterator(this->uris, TRUE);
858 identification_t *uri;
859
860 while (iterator->iterate(iterator, (void**)&uri))
861 {
862 fetcher_t *fetcher;
863 char uri_string[BUF_LEN];
864 chunk_t uri_chunk = uri->get_encoding(uri);
865 chunk_t response_chunk;
866
867 snprintf(uri_string, BUF_LEN, "%.*s", uri_chunk.len, uri_chunk.ptr);
868 fetcher = fetcher_create(uri_string);
869
870 response_chunk = fetcher->post(fetcher, "application/ocsp-request", request);
871 fetcher->destroy(fetcher);
872 if (response_chunk.ptr != NULL)
873 {
874 response = response_create_from_chunk(response_chunk);
875 break;
876 }
877 }
878 iterator->destroy(iterator);
879 }
880 free(request.ptr);
881
882 if (response == NULL)
883 {
884 return;
885 }
886 DBG3("ocsp response: %B", &response->chunk);
887 ocsp_process_response(this, response, credentials);
888 response->destroy(response);
889 }
890
891 /**
892 * Implements ocsp_t.destroy.
893 */
894 static void destroy(private_ocsp_t *this)
895 {
896 this->certinfos->destroy(this->certinfos);
897 free(this->authNameID.ptr);
898 free(this->nonce.ptr);
899 free(this);
900 }
901
902 /*
903 * Described in header.
904 */
905 ocsp_t *ocsp_create(x509_t *cacert, linked_list_t *uris)
906 {
907 private_ocsp_t *this = malloc_thing(private_ocsp_t);
908
909 /* initialize */
910 this->cacert = cacert;
911 this->uris = uris;
912 this->certinfos = linked_list_create();
913 this->nonce = chunk_empty;
914 {
915 hasher_t *hasher = hasher_create(HASH_SHA1);
916 identification_t *issuer = cacert->get_subject(cacert);
917
918 hasher->allocate_hash(hasher, issuer->get_encoding(issuer),
919 &this->authNameID);
920 hasher->destroy(hasher);
921 }
922
923 /* public functions */
924 this->public.fetch = (void (*) (ocsp_t*,certinfo_t*,credential_store_t*))fetch;
925 this->public.destroy = (void (*) (ocsp_t*))destroy;
926
927 return &this->public;
928 }