eliminated TIMETOA_BUF definition
[strongswan.git] / src / pluto / ocsp.c
1 /* Support of the Online Certificate Status Protocol (OCSP)
2 * Copyright (C) 2003 Christoph Gysin, Simon Zwahlen
3 * Zuercher Hochschule Winterthur
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * RCSID $Id$
16 */
17
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <time.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <freeswan.h>
27 #include <ipsec_policy.h>
28
29 #include "constants.h"
30 #include "defs.h"
31 #include "log.h"
32 #include "x509.h"
33 #include "crl.h"
34 #include "ca.h"
35 #include "rnd.h"
36 #include "asn1.h"
37 #include "certs.h"
38 #include "smartcard.h"
39 #include <asn1/oid.h>
40 #include "whack.h"
41 #include "pkcs1.h"
42 #include "keys.h"
43 #include "fetch.h"
44 #include "ocsp.h"
45
46 #define NONCE_LENGTH 16
47
48 static const char *const cert_status_names[] = {
49 "good",
50 "revoked",
51 "unknown",
52 "undefined"
53 };
54
55
56 static const char *const response_status_names[] = {
57 "successful",
58 "malformed request",
59 "internal error",
60 "try later",
61 "status #4",
62 "signature required",
63 "unauthorized"
64 };
65
66 /* response container */
67 typedef struct response response_t;
68
69 struct response {
70 chunk_t tbs;
71 chunk_t responder_id_name;
72 chunk_t responder_id_key;
73 time_t produced_at;
74 chunk_t responses;
75 chunk_t nonce;
76 int algorithm;
77 chunk_t signature;
78 };
79
80 const response_t empty_response = {
81 { NULL, 0 } , /* tbs */
82 { NULL, 0 } , /* responder_id_name */
83 { NULL, 0 } , /* responder_id_key */
84 UNDEFINED_TIME, /* produced_at */
85 { NULL, 0 } , /* single_response */
86 { NULL, 0 } , /* nonce */
87 OID_UNKNOWN , /* signature_algorithm */
88 { NULL, 0 } /* signature */
89 };
90
91 /* single response container */
92 typedef struct single_response single_response_t;
93
94 struct single_response {
95 single_response_t *next;
96 int hash_algorithm;
97 chunk_t issuer_name_hash;
98 chunk_t issuer_key_hash;
99 chunk_t serialNumber;
100 cert_status_t status;
101 time_t revocationTime;
102 crl_reason_t revocationReason;
103 time_t thisUpdate;
104 time_t nextUpdate;
105 };
106
107 const single_response_t empty_single_response = {
108 NULL , /* *next */
109 OID_UNKNOWN , /* hash_algorithm */
110 { NULL, 0 } , /* issuer_name_hash */
111 { NULL, 0 } , /* issuer_key_hash */
112 { NULL, 0 } , /* serial_number */
113 CERT_UNDEFINED , /* status */
114 UNDEFINED_TIME , /* revocationTime */
115 REASON_UNSPECIFIED, /* revocationReason */
116 UNDEFINED_TIME , /* this_update */
117 UNDEFINED_TIME /* next_update */
118 };
119
120
121 /* list of single requests */
122 typedef struct request_list request_list_t;
123 struct request_list {
124 chunk_t request;
125 request_list_t *next;
126 };
127
128 /* some OCSP specific prefabricated ASN.1 constants */
129
130 static u_char ASN1_nonce_oid_str[] = {
131 0x06, 0x09, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x02
132 };
133
134 static const chunk_t ASN1_nonce_oid = chunk_from_buf(ASN1_nonce_oid_str);
135
136 static u_char ASN1_response_oid_str[] = {
137 0x06, 0x09, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x04
138 };
139
140 static const chunk_t ASN1_response_oid = chunk_from_buf(ASN1_response_oid_str);
141
142 static u_char ASN1_response_content_str[] = {
143 0x04, 0x0D,
144 0x30, 0x0B,
145 0x06, 0x09, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01
146 };
147
148 static const chunk_t ASN1_response_content = chunk_from_buf(ASN1_response_content_str);
149
150 /* default OCSP uri */
151 static chunk_t ocsp_default_uri;
152
153 /* ocsp cache: pointer to first element */
154 static ocsp_location_t *ocsp_cache = NULL;
155
156 /* static temporary storage for ocsp requestor information */
157 static x509cert_t *ocsp_requestor_cert = NULL;
158
159 static smartcard_t *ocsp_requestor_sc = NULL;
160
161 static const struct RSA_private_key *ocsp_requestor_pri = NULL;
162
163 /* asn.1 definitions for parsing */
164
165 static const asn1Object_t ocspResponseObjects[] = {
166 { 0, "OCSPResponse", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
167 { 1, "responseStatus", ASN1_ENUMERATED, ASN1_BODY }, /* 1 */
168 { 1, "responseBytesContext", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 2 */
169 { 2, "responseBytes", ASN1_SEQUENCE, ASN1_NONE }, /* 3 */
170 { 3, "responseType", ASN1_OID, ASN1_BODY }, /* 4 */
171 { 3, "response", ASN1_OCTET_STRING, ASN1_BODY }, /* 5 */
172 { 1, "end opt", ASN1_EOC, ASN1_END } /* 6 */
173 };
174
175 #define OCSP_RESPONSE_STATUS 1
176 #define OCSP_RESPONSE_TYPE 4
177 #define OCSP_RESPONSE 5
178 #define OCSP_RESPONSE_ROOF 7
179
180 static const asn1Object_t basicResponseObjects[] = {
181 { 0, "BasicOCSPResponse", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
182 { 1, "tbsResponseData", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
183 { 2, "versionContext", ASN1_CONTEXT_C_0, ASN1_NONE |
184 ASN1_DEF }, /* 2 */
185 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 3 */
186 { 2, "responderIdContext", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 4 */
187 { 3, "responderIdByName", ASN1_SEQUENCE, ASN1_OBJ }, /* 5 */
188 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 6 */
189 { 2, "responderIdContext", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 7 */
190 { 3, "responderIdByKey", ASN1_OCTET_STRING, ASN1_BODY }, /* 8 */
191 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
192 { 2, "producedAt", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 10 */
193 { 2, "responses", ASN1_SEQUENCE, ASN1_OBJ }, /* 11 */
194 { 2, "responseExtensionsContext", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 12 */
195 { 3, "responseExtensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 13 */
196 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 14 */
197 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 15 */
198 { 5, "critical", ASN1_BOOLEAN, ASN1_BODY |
199 ASN1_DEF }, /* 16 */
200 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 17 */
201 { 4, "end loop", ASN1_EOC, ASN1_END }, /* 18 */
202 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 19 */
203 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 20 */
204 { 1, "signature", ASN1_BIT_STRING, ASN1_BODY }, /* 21 */
205 { 1, "certsContext", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 22 */
206 { 2, "certs", ASN1_SEQUENCE, ASN1_LOOP }, /* 23 */
207 { 3, "certificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 24 */
208 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 25 */
209 { 1, "end opt", ASN1_EOC, ASN1_END } /* 26 */
210 };
211
212 #define BASIC_RESPONSE_TBS_DATA 1
213 #define BASIC_RESPONSE_VERSION 3
214 #define BASIC_RESPONSE_ID_BY_NAME 5
215 #define BASIC_RESPONSE_ID_BY_KEY 8
216 #define BASIC_RESPONSE_PRODUCED_AT 10
217 #define BASIC_RESPONSE_RESPONSES 11
218 #define BASIC_RESPONSE_EXT_ID 15
219 #define BASIC_RESPONSE_CRITICAL 16
220 #define BASIC_RESPONSE_EXT_VALUE 17
221 #define BASIC_RESPONSE_ALGORITHM 20
222 #define BASIC_RESPONSE_SIGNATURE 21
223 #define BASIC_RESPONSE_CERTIFICATE 24
224 #define BASIC_RESPONSE_ROOF 27
225
226 static const asn1Object_t responsesObjects[] = {
227 { 0, "responses", ASN1_SEQUENCE, ASN1_LOOP }, /* 0 */
228 { 1, "singleResponse", ASN1_EOC, ASN1_RAW }, /* 1 */
229 { 0, "end loop", ASN1_EOC, ASN1_END } /* 2 */
230 };
231
232 #define RESPONSES_SINGLE_RESPONSE 1
233 #define RESPONSES_ROOF 3
234
235 static const asn1Object_t singleResponseObjects[] = {
236 { 0, "singleResponse", ASN1_SEQUENCE, ASN1_BODY }, /* 0 */
237 { 1, "certID", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
238 { 2, "algorithm", ASN1_EOC, ASN1_RAW }, /* 2 */
239 { 2, "issuerNameHash", ASN1_OCTET_STRING, ASN1_BODY }, /* 3 */
240 { 2, "issuerKeyHash", ASN1_OCTET_STRING, ASN1_BODY }, /* 4 */
241 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 5 */
242 { 1, "certStatusGood", ASN1_CONTEXT_S_0, ASN1_OPT }, /* 6 */
243 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 7 */
244 { 1, "certStatusRevoked", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 8 */
245 { 2, "revocationTime", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 9 */
246 { 2, "revocationReason", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 10 */
247 { 3, "crlReason", ASN1_ENUMERATED, ASN1_BODY }, /* 11 */
248 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 12 */
249 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 13 */
250 { 1, "certStatusUnknown", ASN1_CONTEXT_S_2, ASN1_OPT }, /* 14 */
251 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
252 { 1, "thisUpdate", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 16 */
253 { 1, "nextUpdateContext", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 17 */
254 { 2, "nextUpdate", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 18 */
255 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 19 */
256 { 1, "singleExtensionsContext", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 20 */
257 { 2, "singleExtensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 21 */
258 { 3, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 22 */
259 { 4, "extnID", ASN1_OID, ASN1_BODY }, /* 23 */
260 { 4, "critical", ASN1_BOOLEAN, ASN1_BODY |
261 ASN1_DEF }, /* 24 */
262 { 4, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 25 */
263 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 26 */
264 { 1, "end opt", ASN1_EOC, ASN1_END } /* 27 */
265 };
266
267 #define SINGLE_RESPONSE_ALGORITHM 2
268 #define SINGLE_RESPONSE_ISSUER_NAME_HASH 3
269 #define SINGLE_RESPONSE_ISSUER_KEY_HASH 4
270 #define SINGLE_RESPONSE_SERIAL_NUMBER 5
271 #define SINGLE_RESPONSE_CERT_STATUS_GOOD 6
272 #define SINGLE_RESPONSE_CERT_STATUS_REVOKED 8
273 #define SINGLE_RESPONSE_CERT_STATUS_REVOCATION_TIME 9
274 #define SINGLE_RESPONSE_CERT_STATUS_CRL_REASON 11
275 #define SINGLE_RESPONSE_CERT_STATUS_UNKNOWN 14
276 #define SINGLE_RESPONSE_THIS_UPDATE 16
277 #define SINGLE_RESPONSE_NEXT_UPDATE 18
278 #define SINGLE_RESPONSE_EXT_ID 23
279 #define SINGLE_RESPONSE_CRITICAL 24
280 #define SINGLE_RESPONSE_EXT_VALUE 25
281 #define SINGLE_RESPONSE_ROOF 28
282
283 /* build an ocsp location from certificate information
284 * without unsharing its contents
285 */
286 static bool
287 build_ocsp_location(const x509cert_t *cert, ocsp_location_t *location)
288 {
289 static u_char digest[SHA1_DIGEST_SIZE]; /* temporary storage */
290
291 location->uri = cert->accessLocation;
292
293 if (location->uri.ptr == NULL)
294 {
295 ca_info_t *ca = get_ca_info(cert->issuer, cert->authKeySerialNumber
296 , cert->authKeyID);
297 if (ca != NULL && ca->ocspuri != NULL)
298 {
299 location->uri = chunk_create(ca->ocspuri, strlen(ca->ocspuri));
300 }
301 else
302 { /* abort if no ocsp location uri is defined */
303 return FALSE;
304 }
305 }
306
307 location->authNameID = chunk_create(digest, SHA1_DIGEST_SIZE);
308 compute_digest(cert->issuer, OID_SHA1, &location->authNameID);
309
310 location->next = NULL;
311 location->issuer = cert->issuer;
312 location->authKeyID = cert->authKeyID;
313 location->authKeySerialNumber = cert->authKeySerialNumber;
314
315 if (cert->authKeyID.ptr == NULL)
316 {
317 x509cert_t *authcert = get_authcert(cert->issuer
318 , cert->authKeySerialNumber, cert->authKeyID, AUTH_CA);
319
320 if (authcert != NULL)
321 {
322 location->authKeyID = authcert->subjectKeyID;
323 location->authKeySerialNumber = authcert->serialNumber;
324 }
325 }
326
327 location->nonce = chunk_empty;
328 location->certinfo = NULL;
329
330 return TRUE;
331 }
332
333 /*
334 * compare two ocsp locations for equality
335 */
336 static bool
337 same_ocsp_location(const ocsp_location_t *a, const ocsp_location_t *b)
338 {
339 return ((a->authKeyID.ptr != NULL)
340 ? same_keyid(a->authKeyID, b->authKeyID)
341 : (same_dn(a->issuer, b->issuer)
342 && same_serial(a->authKeySerialNumber, b->authKeySerialNumber)))
343 && chunk_equals(a->uri, b->uri);
344 }
345
346 /*
347 * find an existing ocsp location in a chained list
348 */
349 ocsp_location_t*
350 get_ocsp_location(const ocsp_location_t * loc, ocsp_location_t *chain)
351 {
352
353 while (chain != NULL)
354 {
355 if (same_ocsp_location(loc, chain))
356 return chain;
357 chain = chain->next;
358 }
359 return NULL;
360 }
361
362 /* retrieves the status of a cert from the ocsp cache
363 * returns CERT_UNDEFINED if no status is found
364 */
365 static cert_status_t
366 get_ocsp_status(const ocsp_location_t *loc, chunk_t serialNumber
367 ,time_t *nextUpdate, time_t *revocationTime, crl_reason_t *revocationReason)
368 {
369 ocsp_certinfo_t *certinfo, **certinfop;
370 int cmp = -1;
371
372 /* find location */
373 ocsp_location_t *location = get_ocsp_location(loc, ocsp_cache);
374
375 if (location == NULL)
376 return CERT_UNDEFINED;
377
378 /* traverse list of certinfos in increasing order */
379 certinfop = &location->certinfo;
380 certinfo = *certinfop;
381
382 while (certinfo != NULL)
383 {
384 cmp = chunk_compare(serialNumber, certinfo->serialNumber);
385 if (cmp <= 0)
386 break;
387 certinfop = &certinfo->next;
388 certinfo = *certinfop;
389 }
390
391 if (cmp == 0)
392 {
393 *nextUpdate = certinfo->nextUpdate;
394 *revocationTime = certinfo->revocationTime;
395 *revocationReason = certinfo->revocationReason;
396 return certinfo->status;
397 }
398
399 return CERT_UNDEFINED;
400 }
401
402 /*
403 * verify the ocsp status of a certificate
404 */
405 cert_status_t
406 verify_by_ocsp(const x509cert_t *cert, time_t *until
407 , time_t *revocationDate, crl_reason_t *revocationReason)
408 {
409 cert_status_t status;
410 ocsp_location_t location;
411 time_t nextUpdate = 0;
412
413 *revocationDate = UNDEFINED_TIME;
414 *revocationReason = REASON_UNSPECIFIED;
415
416 /* is an ocsp location defined? */
417 if (!build_ocsp_location(cert, &location))
418 return CERT_UNDEFINED;
419
420 lock_ocsp_cache("verify_by_ocsp");
421 status = get_ocsp_status(&location, cert->serialNumber, &nextUpdate
422 , revocationDate, revocationReason);
423 unlock_ocsp_cache("verify_by_ocsp");
424
425 if (status == CERT_UNDEFINED || nextUpdate < time(NULL))
426 {
427 plog("ocsp status is stale or not in cache");
428 add_ocsp_fetch_request(&location, cert->serialNumber);
429
430 /* inititate fetching of ocsp status */
431 wake_fetch_thread("verify_by_ocsp");
432 }
433 *until = nextUpdate;
434 return status;
435 }
436
437 /*
438 * check if an ocsp status is about to expire
439 */
440 void
441 check_ocsp(void)
442 {
443 ocsp_location_t *location;
444
445 lock_ocsp_cache("check_ocsp");
446 location = ocsp_cache;
447
448 while (location != NULL)
449 {
450 char buf[BUF_LEN];
451 bool first = TRUE;
452 ocsp_certinfo_t *certinfo = location->certinfo;
453
454 while (certinfo != NULL)
455 {
456 if (!certinfo->once)
457 {
458 time_t time_left = certinfo->nextUpdate - time(NULL);
459
460 DBG(DBG_CONTROL,
461 if (first)
462 {
463 dntoa(buf, BUF_LEN, location->issuer);
464 DBG_log("issuer: '%s'", buf);
465 if (location->authKeyID.ptr != NULL)
466 {
467 datatot(location->authKeyID.ptr, location->authKeyID.len
468 , ':', buf, BUF_LEN);
469 DBG_log("authkey: %s", buf);
470 }
471 first = FALSE;
472 }
473 datatot(certinfo->serialNumber.ptr, certinfo->serialNumber.len
474 , ':', buf, BUF_LEN);
475 DBG_log("serial: %s, %ld seconds left", buf, time_left)
476 )
477
478 if (time_left < 2*crl_check_interval)
479 add_ocsp_fetch_request(location, certinfo->serialNumber);
480 }
481 certinfo = certinfo->next;
482 }
483 location = location->next;
484 }
485 unlock_ocsp_cache("check_ocsp");
486 }
487
488 /*
489 * frees the allocated memory of a certinfo struct
490 */
491 static void
492 free_certinfo(ocsp_certinfo_t *certinfo)
493 {
494 free(certinfo->serialNumber.ptr);
495 free(certinfo);
496 }
497
498 /*
499 * frees all certinfos in a chained list
500 */
501 static void
502 free_certinfos(ocsp_certinfo_t *chain)
503 {
504 ocsp_certinfo_t *certinfo;
505
506 while (chain != NULL)
507 {
508 certinfo = chain;
509 chain = chain->next;
510 free_certinfo(certinfo);
511 }
512 }
513
514 /*
515 * frees the memory allocated to an ocsp location including all certinfos
516 */
517 static void
518 free_ocsp_location(ocsp_location_t* location)
519 {
520 free(location->issuer.ptr);
521 free(location->authNameID.ptr);
522 free(location->authKeyID.ptr);
523 free(location->authKeySerialNumber.ptr);
524 free(location->uri.ptr);
525 free_certinfos(location->certinfo);
526 free(location);
527 }
528
529 /*
530 * free a chained list of ocsp locations
531 */
532 void
533 free_ocsp_locations(ocsp_location_t **chain)
534 {
535 while (*chain != NULL)
536 {
537 ocsp_location_t *location = *chain;
538 *chain = location->next;
539 free_ocsp_location(location);
540 }
541 }
542
543 /*
544 * free the ocsp cache
545 */
546 void
547 free_ocsp_cache(void)
548 {
549 lock_ocsp_cache("free_ocsp_cache");
550 free_ocsp_locations(&ocsp_cache);
551 unlock_ocsp_cache("free_ocsp_cache");
552 }
553
554 /*
555 * frees the ocsp cache and global variables
556 */
557 void
558 free_ocsp(void)
559 {
560 free(ocsp_default_uri.ptr);
561 free_ocsp_cache();
562 }
563
564 /*
565 * list a chained list of ocsp_locations
566 */
567 void
568 list_ocsp_locations(ocsp_location_t *location, bool requests, bool utc
569 , bool strict)
570 {
571 bool first = TRUE;
572
573 while (location != NULL)
574 {
575 ocsp_certinfo_t *certinfo = location->certinfo;
576
577 if (certinfo != NULL)
578 {
579 u_char buf[BUF_LEN];
580
581 if (first)
582 {
583 whack_log(RC_COMMENT, " ");
584 whack_log(RC_COMMENT, "List of OCSP %s:", requests?
585 "fetch requests":"responses");
586 first = FALSE;
587 }
588 whack_log(RC_COMMENT, " ");
589 if (location->issuer.ptr != NULL)
590 {
591 dntoa(buf, BUF_LEN, location->issuer);
592 whack_log(RC_COMMENT, " issuer: '%s'", buf);
593 }
594 whack_log(RC_COMMENT, " uri: '%.*s'", (int)location->uri.len
595 , location->uri.ptr);
596 if (location->authNameID.ptr != NULL)
597 {
598 datatot(location->authNameID.ptr, location->authNameID.len, ':'
599 , buf, BUF_LEN);
600 whack_log(RC_COMMENT, " authname: %s", buf);
601 }
602 if (location->authKeyID.ptr != NULL)
603 {
604 datatot(location->authKeyID.ptr, location->authKeyID.len, ':'
605 , buf, BUF_LEN);
606 whack_log(RC_COMMENT, " authkey: %s", buf);
607 }
608 if (location->authKeySerialNumber.ptr != NULL)
609 {
610 datatot(location->authKeySerialNumber.ptr
611 , location->authKeySerialNumber.len, ':', buf, BUF_LEN);
612 whack_log(RC_COMMENT, " aserial: %s", buf);
613 }
614 while (certinfo != NULL)
615 {
616 char thisUpdate[BUF_LEN];
617
618 snprintf(thisUpdate, BUF_LEN, "%T", &certinfo->thisUpdate, utc);
619
620 if (requests)
621 {
622 whack_log(RC_COMMENT, "%s, trials: %d", thisUpdate
623 , certinfo->trials);
624 }
625 else if (certinfo->once)
626 {
627 whack_log(RC_COMMENT, "%s, onetime use%s", thisUpdate
628 , (certinfo->nextUpdate < time(NULL))? " (expired)": "");
629 }
630 else
631 {
632 whack_log(RC_COMMENT, "%s, until %T %s", thisUpdate
633 , &certinfo->nextUpdate, utc
634 , check_expiry(certinfo->nextUpdate, OCSP_WARNING_INTERVAL, strict));
635 }
636 datatot(certinfo->serialNumber.ptr, certinfo->serialNumber.len, ':'
637 , buf, BUF_LEN);
638 whack_log(RC_COMMENT, " serial: %s, %s", buf
639 , cert_status_names[certinfo->status]);
640 certinfo = certinfo->next;
641 }
642 }
643 location = location->next;
644 }
645 }
646
647 /*
648 * list the ocsp cache
649 */
650 void
651 list_ocsp_cache(bool utc, bool strict)
652 {
653 lock_ocsp_cache("list_ocsp_cache");
654 list_ocsp_locations(ocsp_cache, FALSE, utc, strict);
655 unlock_ocsp_cache("list_ocsp_cache");
656 }
657
658 static bool
659 get_ocsp_requestor_cert(ocsp_location_t *location)
660 {
661 x509cert_t *cert = NULL;
662
663 /* initialize temporary static storage */
664 ocsp_requestor_cert = NULL;
665 ocsp_requestor_sc = NULL;
666 ocsp_requestor_pri = NULL;
667
668 for (;;)
669 {
670 char buf[BUF_LEN];
671
672 /* looking for a certificate from the same issuer */
673 cert = get_x509cert(location->issuer, location->authKeySerialNumber
674 ,location->authKeyID, cert);
675 if (cert == NULL)
676 break;
677
678 DBG(DBG_CONTROL,
679 dntoa(buf, BUF_LEN, cert->subject);
680 DBG_log("candidate: '%s'", buf);
681 )
682
683 if (cert->smartcard)
684 {
685 /* look for a matching private key on a smartcard */
686 smartcard_t *sc = scx_get(cert);
687
688 if (sc != NULL)
689 {
690 DBG(DBG_CONTROL,
691 DBG_log("matching smartcard found")
692 )
693 if (sc->valid)
694 {
695 ocsp_requestor_cert = cert;
696 ocsp_requestor_sc = sc;
697 return TRUE;
698 }
699 plog("unable to sign ocsp request without PIN");
700 }
701 }
702 else
703 {
704 /* look for a matching private key in the chained list */
705 const struct RSA_private_key *pri = get_x509_private_key(cert);
706
707 if (pri != NULL)
708 {
709 DBG(DBG_CONTROL,
710 DBG_log("matching private key found")
711 )
712 ocsp_requestor_cert = cert;
713 ocsp_requestor_pri = pri;
714 return TRUE;
715 }
716 }
717 }
718 return FALSE;
719 }
720
721 static chunk_t
722 generate_signature(chunk_t digest, smartcard_t *sc
723 , const RSA_private_key_t *pri)
724 {
725 chunk_t sigdata;
726 u_char *pos;
727 size_t siglen = 0;
728
729 if (sc != NULL)
730 {
731 /* RSA signature is done on smartcard */
732
733 if (!scx_establish_context(sc) || !scx_login(sc))
734 {
735 scx_release_context(sc);
736 return chunk_empty;
737 }
738
739 siglen = scx_get_keylength(sc);
740
741 if (siglen == 0)
742 {
743 plog("failed to get keylength from smartcard");
744 scx_release_context(sc);
745 return chunk_empty;
746 }
747
748 DBG(DBG_CONTROL | DBG_CRYPT,
749 DBG_log("signing hash with RSA key from smartcard (slot: %d, id: %s)"
750 , (int)sc->slot, sc->id)
751 )
752
753 pos = build_asn1_object(&sigdata, ASN1_BIT_STRING, 1 + siglen);
754 *pos++ = 0x00;
755 scx_sign_hash(sc, digest.ptr, digest.len, pos, siglen);
756 if (!pkcs11_keep_state)
757 scx_release_context(sc);
758 }
759 else
760 {
761 /* RSA signature is done in software */
762 siglen = pri->pub.k;
763 pos = build_asn1_object(&sigdata, ASN1_BIT_STRING, 1 + siglen);
764 *pos++ = 0x00;
765 sign_hash(pri, digest.ptr, digest.len, pos, siglen);
766 }
767 return sigdata;
768 }
769
770 /*
771 * build signature into ocsp request
772 * gets built only if a request cert with
773 * a corresponding private key is found
774 */
775 static chunk_t
776 build_signature(chunk_t tbsRequest)
777 {
778 chunk_t sigdata, certs;
779 chunk_t digest_info;
780
781 u_char digest_buf[MAX_DIGEST_LEN];
782 chunk_t digest_raw = { digest_buf, MAX_DIGEST_LEN };
783
784 if (!compute_digest(tbsRequest, OID_SHA1, &digest_raw))
785 return chunk_empty;
786
787 /* according to PKCS#1 v2.1 digest must be packaged into
788 * an ASN.1 structure for encryption
789 */
790 digest_info = asn1_wrap(ASN1_SEQUENCE, "cm"
791 , ASN1_sha1_id
792 , asn1_simple_object(ASN1_OCTET_STRING, digest_raw));
793
794 /* generate the RSA signature */
795 sigdata = generate_signature(digest_info
796 , ocsp_requestor_sc
797 , ocsp_requestor_pri);
798 free(digest_info.ptr);
799
800 /* has the RSA signature generation been successful? */
801 if (sigdata.ptr == NULL)
802 return chunk_empty;
803
804 /* include our certificate */
805 certs = asn1_wrap(ASN1_CONTEXT_C_0, "m"
806 , asn1_simple_object(ASN1_SEQUENCE
807 , ocsp_requestor_cert->certificate
808 )
809 );
810
811 /* build signature comprising algorithm, signature and cert */
812 return asn1_wrap(ASN1_CONTEXT_C_0, "m"
813 , asn1_wrap(ASN1_SEQUENCE, "cmm"
814 , ASN1_sha1WithRSA_id
815 , sigdata
816 , certs
817 )
818 );
819 }
820
821 /* build request (into requestList)
822 * no singleRequestExtensions used
823 */
824 static chunk_t
825 build_request(ocsp_location_t *location, ocsp_certinfo_t *certinfo)
826 {
827 chunk_t reqCert = asn1_wrap(ASN1_SEQUENCE, "cmmm"
828 , ASN1_sha1_id
829 , asn1_simple_object(ASN1_OCTET_STRING, location->authNameID)
830 , asn1_simple_object(ASN1_OCTET_STRING, location->authKeyID)
831 , asn1_simple_object(ASN1_INTEGER, certinfo->serialNumber));
832
833 return asn1_wrap(ASN1_SEQUENCE, "m", reqCert);
834 }
835
836 /*
837 * build requestList (into TBSRequest)
838 */
839 static chunk_t
840 build_request_list(ocsp_location_t *location)
841 {
842 chunk_t requestList;
843 request_list_t *reqs = NULL;
844 ocsp_certinfo_t *certinfo = location->certinfo;
845 u_char *pos;
846
847 size_t datalen = 0;
848
849 /* build content */
850 while (certinfo != NULL)
851 {
852 /* build request for every certificate in list
853 * and store them in a chained list
854 */
855 request_list_t *req = malloc_thing(request_list_t);
856
857 req->request = build_request(location, certinfo);
858 req->next = reqs;
859 reqs = req;
860
861 datalen += req->request.len;
862 certinfo = certinfo->next;
863 }
864
865 pos = build_asn1_object(&requestList, ASN1_SEQUENCE
866 , datalen);
867
868 /* copy all in chained list, free list afterwards */
869 while (reqs != NULL)
870 {
871 request_list_t *req = reqs;
872
873 mv_chunk(&pos, req->request);
874 reqs = reqs->next;
875 free(req);
876 }
877
878 return requestList;
879 }
880
881 /*
882 * build requestorName (into TBSRequest)
883 */
884 static chunk_t
885 build_requestor_name(void)
886 {
887 return asn1_wrap(ASN1_CONTEXT_C_1, "m"
888 , asn1_simple_object(ASN1_CONTEXT_C_4
889 , ocsp_requestor_cert->subject));
890 }
891
892 /*
893 * build nonce extension (into requestExtensions)
894 */
895 static chunk_t
896 build_nonce_extension(ocsp_location_t *location)
897 {
898 /* generate a random nonce */
899 location->nonce.ptr = malloc(NONCE_LENGTH),
900 location->nonce.len = NONCE_LENGTH;
901 get_rnd_bytes(location->nonce.ptr, NONCE_LENGTH);
902
903 return asn1_wrap(ASN1_SEQUENCE, "cm"
904 , ASN1_nonce_oid
905 , asn1_simple_object(ASN1_OCTET_STRING, location->nonce));
906 }
907
908 /*
909 * build requestExtensions (into TBSRequest)
910 */
911 static chunk_t
912 build_request_ext(ocsp_location_t *location)
913 {
914 return asn1_wrap(ASN1_CONTEXT_C_2, "m"
915 , asn1_wrap(ASN1_SEQUENCE, "mm"
916 , build_nonce_extension(location)
917 , asn1_wrap(ASN1_SEQUENCE, "cc"
918 , ASN1_response_oid
919 , ASN1_response_content
920 )
921 )
922 );
923 }
924
925 /*
926 * build TBSRequest (into OCSPRequest)
927 */
928 static chunk_t
929 build_tbs_request(ocsp_location_t *location, bool has_requestor_cert)
930 {
931 /* version is skipped since the default is ok */
932 return asn1_wrap(ASN1_SEQUENCE, "mmm"
933 , (has_requestor_cert)
934 ? build_requestor_name()
935 : chunk_empty
936 , build_request_list(location)
937 , build_request_ext(location));
938 }
939
940 /* assembles an ocsp request to given location
941 * and sets nonce field in location to the sent nonce
942 */
943 chunk_t
944 build_ocsp_request(ocsp_location_t *location)
945 {
946 bool has_requestor_cert;
947 chunk_t tbsRequest, signature;
948 char buf[BUF_LEN];
949
950 DBG(DBG_CONTROL,
951 DBG_log("assembling ocsp request");
952 dntoa(buf, BUF_LEN, location->issuer);
953 DBG_log("issuer: '%s'", buf);
954 if (location->authKeyID.ptr != NULL)
955 {
956 datatot(location->authKeyID.ptr, location->authKeyID.len, ':'
957 , buf, BUF_LEN);
958 DBG_log("authkey: %s", buf);
959 }
960 )
961 lock_certs_and_keys("build_ocsp_request");
962
963 /* looks for requestor cert and matching private key */
964 has_requestor_cert = get_ocsp_requestor_cert(location);
965
966 /* build content */
967 tbsRequest = build_tbs_request(location, has_requestor_cert);
968
969 /* sign tbsReuqest */
970 signature = (has_requestor_cert)? build_signature(tbsRequest)
971 : chunk_empty;
972
973 unlock_certs_and_keys("build_ocsp_request");
974
975 return asn1_wrap(ASN1_SEQUENCE, "mm"
976 , tbsRequest
977 , signature);
978 }
979
980 /*
981 * check if the OCSP response has a valid signature
982 */
983 static bool
984 valid_ocsp_response(response_t *res)
985 {
986 int pathlen;
987 x509cert_t *authcert;
988
989 lock_authcert_list("valid_ocsp_response");
990
991 authcert = get_authcert(res->responder_id_name, chunk_empty
992 , res->responder_id_key, AUTH_OCSP | AUTH_CA);
993
994 if (authcert == NULL)
995 {
996 plog("no matching ocsp signer cert found");
997 unlock_authcert_list("valid_ocsp_response");
998 return FALSE;
999 }
1000 DBG(DBG_CONTROL,
1001 DBG_log("ocsp signer cert found")
1002 )
1003
1004 if (!check_signature(res->tbs, res->signature, res->algorithm
1005 , res->algorithm, authcert))
1006 {
1007 plog("signature of ocsp response is invalid");
1008 unlock_authcert_list("valid_ocsp_response");
1009 return FALSE;
1010 }
1011 DBG(DBG_CONTROL,
1012 DBG_log("signature of ocsp response is valid")
1013 )
1014
1015
1016 for (pathlen = 0; pathlen < MAX_CA_PATH_LEN; pathlen++)
1017 {
1018 u_char buf[BUF_LEN];
1019 err_t ugh = NULL;
1020 time_t until;
1021
1022 x509cert_t *cert = authcert;
1023
1024 DBG(DBG_CONTROL,
1025 dntoa(buf, BUF_LEN, cert->subject);
1026 DBG_log("subject: '%s'",buf);
1027 dntoa(buf, BUF_LEN, cert->issuer);
1028 DBG_log("issuer: '%s'",buf);
1029 if (cert->authKeyID.ptr != NULL)
1030 {
1031 datatot(cert->authKeyID.ptr, cert->authKeyID.len, ':'
1032 , buf, BUF_LEN);
1033 DBG_log("authkey: %s", buf);
1034 }
1035 )
1036
1037 ugh = check_validity(authcert, &until);
1038
1039 if (ugh != NULL)
1040 {
1041 plog("%s", ugh);
1042 unlock_authcert_list("valid_ocsp_response");
1043 return FALSE;
1044 }
1045
1046 DBG(DBG_CONTROL,
1047 DBG_log("certificate is valid")
1048 )
1049
1050 authcert = get_authcert(cert->issuer, cert->authKeySerialNumber
1051 , cert->authKeyID, AUTH_CA);
1052
1053 if (authcert == NULL)
1054 {
1055 plog("issuer cacert not found");
1056 unlock_authcert_list("valid_ocsp_response");
1057 return FALSE;
1058 }
1059 DBG(DBG_CONTROL,
1060 DBG_log("issuer cacert found")
1061 )
1062
1063 if (!check_signature(cert->tbsCertificate, cert->signature
1064 , cert->algorithm, cert->algorithm, authcert))
1065 {
1066 plog("certificate signature is invalid");
1067 unlock_authcert_list("valid_ocsp_response");
1068 return FALSE;
1069 }
1070 DBG(DBG_CONTROL,
1071 DBG_log("certificate signature is valid")
1072 )
1073
1074 /* check if cert is self-signed */
1075 if (same_dn(cert->issuer, cert->subject))
1076 {
1077 DBG(DBG_CONTROL,
1078 DBG_log("reached self-signed root ca")
1079 )
1080 unlock_authcert_list("valid_ocsp_response");
1081 return TRUE;
1082 }
1083 }
1084 plog("maximum ca path length of %d levels exceeded", MAX_CA_PATH_LEN);
1085 unlock_authcert_list("valid_ocsp_response");
1086 return FALSE;
1087 }
1088
1089 /*
1090 * parse a basic OCSP response
1091 */
1092 static bool
1093 parse_basic_ocsp_response(chunk_t blob, int level0, response_t *res)
1094 {
1095 asn1_ctx_t ctx;
1096 bool critical;
1097 chunk_t object;
1098 u_int level, version;
1099 u_char buf[BUF_LEN];
1100 int objectID = 0;
1101 int extn_oid = OID_UNKNOWN;
1102
1103 asn1_init(&ctx, blob, level0, FALSE, DBG_RAW);
1104
1105 while (objectID < BASIC_RESPONSE_ROOF)
1106 {
1107 if (!extract_object(basicResponseObjects, &objectID, &object, &level, &ctx))
1108 return FALSE;
1109
1110 switch (objectID)
1111 {
1112 case BASIC_RESPONSE_TBS_DATA:
1113 res->tbs = object;
1114 break;
1115 case BASIC_RESPONSE_VERSION:
1116 version = (object.len)? (1 + (u_int)*object.ptr) : 1;
1117 if (version != OCSP_BASIC_RESPONSE_VERSION)
1118 {
1119 plog("wrong ocsp basic response version (version= %i)", version);
1120 return FALSE;
1121 }
1122 break;
1123 case BASIC_RESPONSE_ID_BY_NAME:
1124 res->responder_id_name = object;
1125 DBG(DBG_PARSING,
1126 dntoa(buf, BUF_LEN, object);
1127 DBG_log(" '%s'",buf)
1128 )
1129 break;
1130 case BASIC_RESPONSE_ID_BY_KEY:
1131 res->responder_id_key = object;
1132 break;
1133 case BASIC_RESPONSE_PRODUCED_AT:
1134 res->produced_at = asn1totime(&object, ASN1_GENERALIZEDTIME);
1135 break;
1136 case BASIC_RESPONSE_RESPONSES:
1137 res->responses = object;
1138 break;
1139 case BASIC_RESPONSE_EXT_ID:
1140 extn_oid = asn1_known_oid(object);
1141 break;
1142 case BASIC_RESPONSE_CRITICAL:
1143 critical = object.len && *object.ptr;
1144 DBG(DBG_PARSING,
1145 DBG_log(" %s",(critical)?"TRUE":"FALSE");
1146 )
1147 break;
1148 case BASIC_RESPONSE_EXT_VALUE:
1149 if (extn_oid == OID_NONCE)
1150 res->nonce = object;
1151 break;
1152 case BASIC_RESPONSE_ALGORITHM:
1153 res->algorithm = parse_algorithmIdentifier(object, level+1, NULL);
1154 break;
1155 case BASIC_RESPONSE_SIGNATURE:
1156 res->signature = object;
1157 break;
1158 case BASIC_RESPONSE_CERTIFICATE:
1159 {
1160 chunk_t blob = chunk_clone(object);
1161 x509cert_t *cert = malloc_thing(x509cert_t);
1162
1163 *cert = empty_x509cert;
1164
1165 if (parse_x509cert(blob, level+1, cert)
1166 && cert->isOcspSigner
1167 && trust_authcert_candidate(cert, NULL))
1168 {
1169 add_authcert(cert, AUTH_OCSP);
1170 }
1171 else
1172 {
1173 DBG(DBG_CONTROL | DBG_PARSING,
1174 DBG_log("embedded ocsp certificate rejected")
1175 )
1176 free_x509cert(cert);
1177 }
1178 }
1179 break;
1180 }
1181 objectID++;
1182 }
1183 return TRUE;
1184 }
1185
1186
1187 /*
1188 * parse an ocsp response and return the result as a response_t struct
1189 */
1190 static response_status
1191 parse_ocsp_response(chunk_t blob, response_t * res)
1192 {
1193 asn1_ctx_t ctx;
1194 chunk_t object;
1195 u_int level;
1196 int objectID = 0;
1197 int ocspResponseType = OID_UNKNOWN;
1198 response_status rStatus = STATUS_INTERNALERROR;
1199
1200 asn1_init(&ctx, blob, 0, FALSE, DBG_RAW);
1201
1202 while (objectID < OCSP_RESPONSE_ROOF)
1203 {
1204 if (!extract_object(ocspResponseObjects, &objectID, &object, &level, &ctx))
1205 return STATUS_INTERNALERROR;
1206
1207 switch (objectID) {
1208 case OCSP_RESPONSE_STATUS:
1209 rStatus = (response_status) *object.ptr;
1210
1211 switch (rStatus)
1212 {
1213 case STATUS_SUCCESSFUL:
1214 break;
1215 case STATUS_MALFORMEDREQUEST:
1216 case STATUS_INTERNALERROR:
1217 case STATUS_TRYLATER:
1218 case STATUS_SIGREQUIRED:
1219 case STATUS_UNAUTHORIZED:
1220 plog("ocsp response: server said '%s'"
1221 , response_status_names[rStatus]);
1222 return rStatus;
1223 default:
1224 return STATUS_INTERNALERROR;
1225 }
1226 break;
1227 case OCSP_RESPONSE_TYPE:
1228 ocspResponseType = asn1_known_oid(object);
1229 break;
1230 case OCSP_RESPONSE:
1231 {
1232 switch (ocspResponseType) {
1233 case OID_BASIC:
1234 if (!parse_basic_ocsp_response(object, level+1, res))
1235 return STATUS_INTERNALERROR;
1236 break;
1237 default:
1238 DBG(DBG_CONTROL,
1239 DBG_log("ocsp response is not of type BASIC");
1240 DBG_dump_chunk("ocsp response OID: ", object);
1241 )
1242 return STATUS_INTERNALERROR;
1243 }
1244 }
1245 break;
1246 }
1247 objectID++;
1248 }
1249 return rStatus;
1250 }
1251
1252 /*
1253 * parse a basic OCSP response
1254 */
1255 static bool
1256 parse_ocsp_single_response(chunk_t blob, int level0, single_response_t *sres)
1257 {
1258 u_int level, extn_oid;
1259 asn1_ctx_t ctx;
1260 bool critical;
1261 chunk_t object;
1262 int objectID = 0;
1263
1264 asn1_init(&ctx, blob, level0, FALSE, DBG_RAW);
1265
1266 while (objectID < SINGLE_RESPONSE_ROOF)
1267 {
1268 if (!extract_object(singleResponseObjects, &objectID, &object, &level, &ctx))
1269 return FALSE;
1270
1271 switch (objectID)
1272 {
1273 case SINGLE_RESPONSE_ALGORITHM:
1274 sres->hash_algorithm = parse_algorithmIdentifier(object, level+1, NULL);
1275 break;
1276 case SINGLE_RESPONSE_ISSUER_NAME_HASH:
1277 sres->issuer_name_hash = object;
1278 break;
1279 case SINGLE_RESPONSE_ISSUER_KEY_HASH:
1280 sres->issuer_key_hash = object;
1281 break;
1282 case SINGLE_RESPONSE_SERIAL_NUMBER:
1283 sres->serialNumber = object;
1284 break;
1285 case SINGLE_RESPONSE_CERT_STATUS_GOOD:
1286 sres->status = CERT_GOOD;
1287 break;
1288 case SINGLE_RESPONSE_CERT_STATUS_REVOKED:
1289 sres->status = CERT_REVOKED;
1290 break;
1291 case SINGLE_RESPONSE_CERT_STATUS_REVOCATION_TIME:
1292 sres->revocationTime = asn1totime(&object, ASN1_GENERALIZEDTIME);
1293 break;
1294 case SINGLE_RESPONSE_CERT_STATUS_CRL_REASON:
1295 sres->revocationReason = (object.len == 1)
1296 ? *object.ptr : REASON_UNSPECIFIED;
1297 break;
1298 case SINGLE_RESPONSE_CERT_STATUS_UNKNOWN:
1299 sres->status = CERT_UNKNOWN;
1300 break;
1301 case SINGLE_RESPONSE_THIS_UPDATE:
1302 sres->thisUpdate = asn1totime(&object, ASN1_GENERALIZEDTIME);
1303 break;
1304 case SINGLE_RESPONSE_NEXT_UPDATE:
1305 sres->nextUpdate = asn1totime(&object, ASN1_GENERALIZEDTIME);
1306 break;
1307 case SINGLE_RESPONSE_EXT_ID:
1308 extn_oid = asn1_known_oid(object);
1309 break;
1310 case SINGLE_RESPONSE_CRITICAL:
1311 critical = object.len && *object.ptr;
1312 DBG(DBG_PARSING,
1313 DBG_log(" %s",(critical)?"TRUE":"FALSE");
1314 )
1315 case SINGLE_RESPONSE_EXT_VALUE:
1316 break;
1317 }
1318 objectID++;
1319 }
1320 return TRUE;
1321 }
1322
1323 /*
1324 * add an ocsp location to a chained list
1325 */
1326 ocsp_location_t*
1327 add_ocsp_location(const ocsp_location_t *loc, ocsp_location_t **chain)
1328 {
1329 ocsp_location_t *location = malloc_thing(ocsp_location_t);
1330
1331 /* unshare location fields */
1332 location->issuer = chunk_clone(loc->issuer);
1333 location->authNameID = chunk_clone(loc->authNameID);
1334 location->authKeyID = chunk_clone(loc->authKeyID);
1335 location->authKeySerialNumber = chunk_clone(loc->authKeySerialNumber);
1336 location->uri = chunk_clone(loc->uri);
1337 location->certinfo = NULL;
1338
1339 /* insert new ocsp location in front of chain */
1340 location->next = *chain;
1341 *chain = location;
1342
1343 DBG(DBG_CONTROL,
1344 DBG_log("new ocsp location added")
1345 )
1346
1347 return location;
1348 }
1349
1350 /*
1351 * add a certinfo struct to a chained list
1352 */
1353 void
1354 add_certinfo(ocsp_location_t *loc, ocsp_certinfo_t *info, ocsp_location_t **chain
1355 , bool request)
1356 {
1357 ocsp_location_t *location;
1358 ocsp_certinfo_t *certinfo, **certinfop;
1359 char buf[BUF_LEN];
1360 time_t now;
1361 int cmp = -1;
1362
1363 location = get_ocsp_location(loc, *chain);
1364 if (location == NULL)
1365 location = add_ocsp_location(loc, chain);
1366
1367 /* traverse list of certinfos in increasing order */
1368 certinfop = &location->certinfo;
1369 certinfo = *certinfop;
1370
1371 while (certinfo != NULL)
1372 {
1373 cmp = chunk_compare(info->serialNumber, certinfo->serialNumber);
1374 if (cmp <= 0)
1375 break;
1376 certinfop = &certinfo->next;
1377 certinfo = *certinfop;
1378 }
1379
1380 if (cmp != 0)
1381 {
1382 /* add a new certinfo entry */
1383 ocsp_certinfo_t *cnew = malloc_thing(ocsp_certinfo_t);
1384
1385 cnew->serialNumber = chunk_clone(info->serialNumber);
1386 cnew->next = certinfo;
1387 *certinfop = cnew;
1388 certinfo = cnew;
1389 }
1390
1391 DBG(DBG_CONTROL,
1392 datatot(info->serialNumber.ptr, info->serialNumber.len, ':'
1393 , buf, BUF_LEN);
1394 DBG_log("ocsp %s for serial %s %s"
1395 , request?"fetch request":"certinfo"
1396 , buf
1397 , (cmp == 0)? (request?"already exists":"updated"):"added")
1398 )
1399
1400 time(&now);
1401
1402 if (request)
1403 {
1404 certinfo->status = CERT_UNDEFINED;
1405
1406 if (cmp != 0)
1407 certinfo->thisUpdate = now;
1408
1409 certinfo->nextUpdate = UNDEFINED_TIME;
1410 }
1411 else
1412 {
1413 certinfo->status = info->status;
1414 certinfo->revocationTime = info->revocationTime;
1415 certinfo->revocationReason = info->revocationReason;
1416
1417 certinfo->thisUpdate = (info->thisUpdate != UNDEFINED_TIME)?
1418 info->thisUpdate : now;
1419
1420 certinfo->once = (info->nextUpdate == UNDEFINED_TIME);
1421
1422 certinfo->nextUpdate = (certinfo->once)?
1423 (now + OCSP_DEFAULT_VALID_TIME) : info->nextUpdate;
1424 }
1425 }
1426
1427 /*
1428 * process received ocsp single response and add it to ocsp cache
1429 */
1430 static void
1431 process_single_response(ocsp_location_t *location, single_response_t *sres)
1432 {
1433 ocsp_certinfo_t *certinfo, **certinfop;
1434 int cmp = -1;
1435
1436 if (sres->hash_algorithm != OID_SHA1)
1437 {
1438 plog("only SHA-1 hash supported in OCSP single response");
1439 return;
1440 }
1441 if (!(chunk_equals(sres->issuer_name_hash, location->authNameID)
1442 && chunk_equals(sres->issuer_key_hash, location->authKeyID)))
1443 {
1444 plog("ocsp single response has wrong issuer");
1445 return;
1446 }
1447
1448 /* traverse list of certinfos in increasing order */
1449 certinfop = &location->certinfo;
1450 certinfo = *certinfop;
1451
1452 while (certinfo != NULL)
1453 {
1454 cmp = chunk_compare(sres->serialNumber, certinfo->serialNumber);
1455 if (cmp <= 0)
1456 break;
1457 certinfop = &certinfo->next;
1458 certinfo = *certinfop;
1459 }
1460
1461 if (cmp != 0)
1462 {
1463 plog("received unrequested cert status from ocsp server");
1464 return;
1465 }
1466
1467 /* unlink cert from ocsp fetch request list */
1468 *certinfop = certinfo->next;
1469
1470 /* update certinfo using the single response information */
1471 certinfo->thisUpdate = sres->thisUpdate;
1472 certinfo->nextUpdate = sres->nextUpdate;
1473 certinfo->status = sres->status;
1474 certinfo->revocationTime = sres->revocationTime;
1475 certinfo->revocationReason = sres->revocationReason;
1476
1477 /* add or update certinfo in ocsp cache */
1478 lock_ocsp_cache("process_single_response");
1479 add_certinfo(location, certinfo, &ocsp_cache, FALSE);
1480 unlock_ocsp_cache("process_single_response");
1481
1482 /* free certinfo unlinked from ocsp fetch request list */
1483 free_certinfo(certinfo);
1484
1485 }
1486
1487 /*
1488 * parse and verify ocsp response and update the ocsp cache
1489 */
1490 void
1491 parse_ocsp(ocsp_location_t *location, chunk_t blob)
1492 {
1493 response_t res = empty_response;
1494
1495 /* parse the ocsp response without looking at the single responses yet */
1496 response_status status = parse_ocsp_response(blob, &res);
1497
1498 if (status != STATUS_SUCCESSFUL)
1499 {
1500 plog("error in ocsp response");
1501 return;
1502 }
1503 /* check if there was a nonce in the request */
1504 if (location->nonce.ptr != NULL && res.nonce.ptr == NULL)
1505 {
1506 plog("ocsp response contains no nonce, replay attack possible");
1507 }
1508 /* check if the nonce is identical */
1509 if (res.nonce.ptr != NULL && !chunk_equals(res.nonce, location->nonce))
1510 {
1511 plog("invalid nonce in ocsp response");
1512 return;
1513 }
1514 /* check if the response is signed by a trusted key */
1515 if (!valid_ocsp_response(&res))
1516 {
1517 plog("invalid ocsp response");
1518 return;
1519 }
1520 DBG(DBG_CONTROL,
1521 DBG_log("valid ocsp response")
1522 )
1523
1524 /* now parse the single responses one at a time */
1525 {
1526 u_int level;
1527 asn1_ctx_t ctx;
1528 chunk_t object;
1529 int objectID = 0;
1530
1531 asn1_init(&ctx, res.responses, 0, FALSE, DBG_RAW);
1532
1533 while (objectID < RESPONSES_ROOF)
1534 {
1535 if (!extract_object(responsesObjects, &objectID, &object, &level, &ctx))
1536 return;
1537
1538 if (objectID == RESPONSES_SINGLE_RESPONSE)
1539 {
1540 single_response_t sres = empty_single_response;
1541
1542 if (parse_ocsp_single_response(object, level+1, &sres))
1543 {
1544 process_single_response(location, &sres);
1545 }
1546 }
1547 objectID++;
1548 }
1549 }
1550 }