aboutsummaryrefslogtreecommitdiffstats
path: root/src/crypto/tls_openssl_ocsp.c
blob: 37c87f4090b9f849fb4c35530ffcea887435bc89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*
 * SSL/TLS interface functions for OpenSSL - BoringSSL OCSP
 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */

#include "includes.h"

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/x509v3.h>
#ifdef OPENSSL_IS_BORINGSSL
#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#endif /* OPENSSL_IS_BORINGSSL */

#include "common.h"
#include "tls_openssl.h"


#ifdef OPENSSL_IS_BORINGSSL

static void tls_show_errors(int level, const char *func, const char *txt)
{
	unsigned long err;

	wpa_printf(level, "OpenSSL: %s - %s %s",
		   func, txt, ERR_error_string(ERR_get_error(), NULL));

	while ((err = ERR_get_error())) {
		wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
			   ERR_error_string(err, NULL));
	}
}


/*
 * CertID ::= SEQUENCE {
 *     hashAlgorithm      AlgorithmIdentifier,
 *     issuerNameHash     OCTET STRING, -- Hash of Issuer's DN
 *     issuerKeyHash      OCTET STRING, -- Hash of Issuer's public key
 *     serialNumber       CertificateSerialNumber }
 */
typedef struct {
	X509_ALGOR *hashAlgorithm;
	ASN1_OCTET_STRING *issuerNameHash;
	ASN1_OCTET_STRING *issuerKeyHash;
	ASN1_INTEGER *serialNumber;
} CertID;

/*
 * ResponseBytes ::=       SEQUENCE {
 *     responseType   OBJECT IDENTIFIER,
 *     response       OCTET STRING }
 */
typedef struct {
	ASN1_OBJECT *responseType;
	ASN1_OCTET_STRING *response;
} ResponseBytes;

/*
 * OCSPResponse ::= SEQUENCE {
 *    responseStatus         OCSPResponseStatus,
 *    responseBytes          [0] EXPLICIT ResponseBytes OPTIONAL }
 */
typedef struct {
	ASN1_ENUMERATED *responseStatus;
	ResponseBytes *responseBytes;
} OCSPResponse;

ASN1_SEQUENCE(ResponseBytes) = {
	ASN1_SIMPLE(ResponseBytes, responseType, ASN1_OBJECT),
	ASN1_SIMPLE(ResponseBytes, response, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(ResponseBytes);

ASN1_SEQUENCE(OCSPResponse) = {
	ASN1_SIMPLE(OCSPResponse, responseStatus, ASN1_ENUMERATED),
	ASN1_EXP_OPT(OCSPResponse, responseBytes, ResponseBytes, 0)
} ASN1_SEQUENCE_END(OCSPResponse);

IMPLEMENT_ASN1_FUNCTIONS(OCSPResponse);

/*
 * ResponderID ::= CHOICE {
 *    byName               [1] Name,
 *    byKey                [2] KeyHash }
 */
typedef struct {
	int type;
	union {
		X509_NAME *byName;
		ASN1_OCTET_STRING *byKey;
	} value;
} ResponderID;

/*
 * RevokedInfo ::= SEQUENCE {
 *     revocationTime              GeneralizedTime,
 *     revocationReason    [0]     EXPLICIT CRLReason OPTIONAL }
 */
typedef struct {
	ASN1_GENERALIZEDTIME *revocationTime;
	ASN1_ENUMERATED *revocationReason;
} RevokedInfo;

/*
 * CertStatus ::= CHOICE {
 *     good        [0]     IMPLICIT NULL,
 *     revoked     [1]     IMPLICIT RevokedInfo,
 *     unknown     [2]     IMPLICIT UnknownInfo }
 */
typedef struct {
	int type;
	union {
		ASN1_NULL *good;
		RevokedInfo *revoked;
		ASN1_NULL *unknown;
	} value;
} CertStatus;

/*
 * SingleResponse ::= SEQUENCE {
 *    certID                       CertID,
 *    certStatus                   CertStatus,
 *    thisUpdate                   GeneralizedTime,
 *    nextUpdate         [0]       EXPLICIT GeneralizedTime OPTIONAL,
 *    singleExtensions   [1]       EXPLICIT Extensions OPTIONAL }
 */
typedef struct {
	CertID *certID;
	CertStatus *certStatus;
	ASN1_GENERALIZEDTIME *thisUpdate;
	ASN1_GENERALIZEDTIME *nextUpdate;
	STACK_OF(X509_EXTENSION) *singleExtensions;
} SingleResponse;

/*
 * ResponseData ::= SEQUENCE {
 *   version              [0] EXPLICIT Version DEFAULT v1,
 *   responderID              ResponderID,
 *   producedAt               GeneralizedTime,
 *   responses                SEQUENCE OF SingleResponse,
 *   responseExtensions   [1] EXPLICIT Extensions OPTIONAL }
 */
typedef struct {
	ASN1_INTEGER *version;
	ResponderID *responderID;
	ASN1_GENERALIZEDTIME *producedAt;
	STACK_OF(SingleResponse) *responses;
	STACK_OF(X509_EXTENSION) *responseExtensions;
} ResponseData;

/*
 * BasicOCSPResponse       ::= SEQUENCE {
 *   tbsResponseData      ResponseData,
 *   signatureAlgorithm   AlgorithmIdentifier,
 *   signature            BIT STRING,
 *   certs                [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
 */
typedef struct {
	ResponseData *tbsResponseData;
	X509_ALGOR *signatureAlgorithm;
	ASN1_BIT_STRING *signature;
	STACK_OF(X509) *certs;
} BasicOCSPResponse;

ASN1_SEQUENCE(CertID) = {
	ASN1_SIMPLE(CertID, hashAlgorithm, X509_ALGOR),
	ASN1_SIMPLE(CertID, issuerNameHash, ASN1_OCTET_STRING),
	ASN1_SIMPLE(CertID, issuerKeyHash, ASN1_OCTET_STRING),
	ASN1_SIMPLE(CertID, serialNumber, ASN1_INTEGER)
} ASN1_SEQUENCE_END(CertID);

ASN1_CHOICE(ResponderID) = {
	ASN1_EXP(ResponderID, value.byName, X509_NAME, 1),
	ASN1_EXP(ResponderID, value.byKey, ASN1_OCTET_STRING, 2)
} ASN1_CHOICE_END(ResponderID);

ASN1_SEQUENCE(RevokedInfo) = {
	ASN1_SIMPLE(RevokedInfo, revocationTime, ASN1_GENERALIZEDTIME),
	ASN1_EXP_OPT(RevokedInfo, revocationReason, ASN1_ENUMERATED, 0)
} ASN1_SEQUENCE_END(RevokedInfo);

ASN1_CHOICE(CertStatus) = {
	ASN1_IMP(CertStatus, value.good, ASN1_NULL, 0),
	ASN1_IMP(CertStatus, value.revoked, RevokedInfo, 1),
	ASN1_IMP(CertStatus, value.unknown, ASN1_NULL, 2)
} ASN1_CHOICE_END(CertStatus);

ASN1_SEQUENCE(SingleResponse) = {
	ASN1_SIMPLE(SingleResponse, certID, CertID),
	ASN1_SIMPLE(SingleResponse, certStatus, CertStatus),
	ASN1_SIMPLE(SingleResponse, thisUpdate, ASN1_GENERALIZEDTIME),
	ASN1_EXP_OPT(SingleResponse, nextUpdate, ASN1_GENERALIZEDTIME, 0),
	ASN1_EXP_SEQUENCE_OF_OPT(SingleResponse, singleExtensions,
				 X509_EXTENSION, 1)
} ASN1_SEQUENCE_END(SingleResponse);

ASN1_SEQUENCE(ResponseData) = {
	ASN1_EXP_OPT(ResponseData, version, ASN1_INTEGER, 0),
	ASN1_SIMPLE(ResponseData, responderID, ResponderID),
	ASN1_SIMPLE(ResponseData, producedAt, ASN1_GENERALIZEDTIME),
	ASN1_SEQUENCE_OF(ResponseData, responses, SingleResponse),
	ASN1_EXP_SEQUENCE_OF_OPT(ResponseData, responseExtensions,
				 X509_EXTENSION, 1)
} ASN1_SEQUENCE_END(ResponseData);

ASN1_SEQUENCE(BasicOCSPResponse) = {
	ASN1_SIMPLE(BasicOCSPResponse, tbsResponseData, ResponseData),
	ASN1_SIMPLE(BasicOCSPResponse, signatureAlgorithm, X509_ALGOR),
	ASN1_SIMPLE(BasicOCSPResponse, signature, ASN1_BIT_STRING),
	ASN1_EXP_SEQUENCE_OF_OPT(BasicOCSPResponse, certs, X509, 0)
} ASN1_SEQUENCE_END(BasicOCSPResponse);

IMPLEMENT_ASN1_FUNCTIONS(BasicOCSPResponse);

#define sk_SingleResponse_num(sk) \
sk_num(CHECKED_CAST(_STACK *, STACK_OF(SingleResponse) *, sk))

#define sk_SingleResponse_value(sk, i) \
	((SingleResponse *)						\
	 sk_value(CHECKED_CAST(_STACK *, STACK_OF(SingleResponse) *, sk), (i)))


static char * mem_bio_to_str(BIO *out)
{
	char *txt;
	size_t rlen;
	int res;

	rlen = BIO_ctrl_pending(out);
	txt = os_malloc(rlen + 1);
	if (!txt) {
		BIO_free(out);
		return NULL;
	}

	res = BIO_read(out, txt, rlen);
	BIO_free(out);
	if (res < 0) {
		os_free(txt);
		return NULL;
	}

	txt[res] = '\0';
	return txt;
}


static char * generalizedtime_str(ASN1_GENERALIZEDTIME *t)
{
	BIO *out;

	out = BIO_new(BIO_s_mem());
	if (!out)
		return NULL;

	if (!ASN1_GENERALIZEDTIME_print(out, t)) {
		BIO_free(out);
		return NULL;
	}

	return mem_bio_to_str(out);
}


static char * responderid_str(ResponderID *rid)
{
	BIO *out;

	out = BIO_new(BIO_s_mem());
	if (!out)
		return NULL;

	switch (rid->type) {
	case 0:
		X509_NAME_print_ex(out, rid->value.byName, 0, XN_FLAG_ONELINE);
		break;
	case 1:
		i2a_ASN1_STRING(out, rid->value.byKey, V_ASN1_OCTET_STRING);
		break;
	default:
		BIO_free(out);
		return NULL;
	}

	return mem_bio_to_str(out);
}


static char * octet_string_str(ASN1_OCTET_STRING *o)
{
	BIO *out;

	out = BIO_new(BIO_s_mem());
	if (!out)
		return NULL;

	i2a_ASN1_STRING(out, o, V_ASN1_OCTET_STRING);
	return mem_bio_to_str(out);
}


static char * integer_str(ASN1_INTEGER *i)
{
	BIO *out;

	out = BIO_new(BIO_s_mem());
	if (!out)
		return NULL;

	i2a_ASN1_INTEGER(out, i);
	return mem_bio_to_str(out);
}


static char * algor_str(X509_ALGOR *alg)
{
	BIO *out;

	out = BIO_new(BIO_s_mem());
	if (!out)
		return NULL;

	i2a_ASN1_OBJECT(out, alg->algorithm);
	return mem_bio_to_str(out);
}


static char * extensions_str(const char *title, STACK_OF(X509_EXTENSION) *ext)
{
	BIO *out;

	if (!ext)
		return NULL;

	out = BIO_new(BIO_s_mem());
	if (!out)
		return NULL;

	if (!X509V3_extensions_print(out, title, ext, 0, 0)) {
		BIO_free(out);
		return NULL;
	}
	return mem_bio_to_str(out);
}


static int ocsp_resp_valid(ASN1_GENERALIZEDTIME *thisupd,
			   ASN1_GENERALIZEDTIME *nextupd)
{
	time_t now, tmp;

	if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: Invalid OCSP response thisUpdate");
		return 0;
	}

	time(&now);
	tmp = now + 5 * 60; /* allow five minute clock difference */
	if (X509_cmp_time(thisupd, &tmp) > 0) {
		wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response not yet valid");
		return 0;
	}

	if (!nextupd)
		return 1; /* OK - no limit on response age */

	if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: Invalid OCSP response nextUpdate");
		return 0;
	}

	tmp = now - 5 * 60; /* allow five minute clock difference */
	if (X509_cmp_time(nextupd, &tmp) < 0) {
		wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response expired");
		return 0;
	}

	if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: OCSP response nextUpdate before thisUpdate");
		return 0;
	}

	/* Both thisUpdate and nextUpdate are valid */
	return -1;
}


static int issuer_match(X509 *cert, X509 *issuer, CertID *certid)
{
	X509_NAME *iname;
	ASN1_BIT_STRING *ikey;
	const EVP_MD *dgst;
	unsigned int len;
	unsigned char md[EVP_MAX_MD_SIZE];
	ASN1_OCTET_STRING *hash;
	char *txt;

	dgst = EVP_get_digestbyobj(certid->hashAlgorithm->algorithm);
	if (!dgst) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: Could not find matching hash algorithm for OCSP");
		return -1;
	}

	iname = X509_get_issuer_name(cert);
	if (!X509_NAME_digest(iname, dgst, md, &len))
		return -1;
	hash = ASN1_OCTET_STRING_new();
	if (!hash)
		return -1;
	if (!ASN1_OCTET_STRING_set(hash, md, len)) {
		ASN1_OCTET_STRING_free(hash);
		return -1;
	}

	txt = octet_string_str(hash);
	if (txt) {
		wpa_printf(MSG_DEBUG, "OpenSSL: calculated issuerNameHash: %s",
			   txt);
		os_free(txt);
	}

	if (ASN1_OCTET_STRING_cmp(certid->issuerNameHash, hash)) {
		ASN1_OCTET_STRING_free(hash);
		return -1;
	}

	ikey = X509_get0_pubkey_bitstr(issuer);
	if (!EVP_Digest(ikey->data, ikey->length, md, &len, dgst, NULL) ||
	    !ASN1_OCTET_STRING_set(hash, md, len)) {
		ASN1_OCTET_STRING_free(hash);
		return -1;
	}

	txt = octet_string_str(hash);
	if (txt) {
		wpa_printf(MSG_DEBUG, "OpenSSL: calculated issuerKeyHash: %s",
			   txt);
		os_free(txt);
	}

	if (ASN1_OCTET_STRING_cmp(certid->issuerKeyHash, hash)) {
		ASN1_OCTET_STRING_free(hash);
		return -1;
	}

	ASN1_OCTET_STRING_free(hash);
	return 0;
}


static X509 * ocsp_find_signer(STACK_OF(X509) *certs, ResponderID *rid)
{
	unsigned int i;
	unsigned char hash[SHA_DIGEST_LENGTH];

	if (rid->type == 0) {
		/* byName */
		return X509_find_by_subject(certs, rid->value.byName);
	}

	/* byKey */
	if (rid->value.byKey->length != SHA_DIGEST_LENGTH)
		return NULL;
	for (i = 0; i < sk_X509_num(certs); i++) {
		X509 *x = sk_X509_value(certs, i);

		X509_pubkey_digest(x, EVP_sha1(), hash, NULL);
		if (os_memcmp(rid->value.byKey->data, hash,
			      SHA_DIGEST_LENGTH) == 0)
			return x;
	}

	return NULL;
}


enum ocsp_result check_ocsp_resp(SSL_CTX *ssl_ctx, SSL *ssl, X509 *cert,
				 X509 *issuer, X509 *issuer_issuer)
{
	const uint8_t *resp_data;
	size_t resp_len;
	OCSPResponse *resp;
	int status;
	ResponseBytes *bytes;
	const u8 *basic_data;
	size_t basic_len;
	BasicOCSPResponse *basic;
	ResponseData *rd;
	char *txt;
	int i, num;
	unsigned int j, num_resp;
	SingleResponse *matching_resp = NULL, *cmp_sresp;
	enum ocsp_result result = OCSP_INVALID;
	X509_STORE *store;
	STACK_OF(X509) *untrusted = NULL, *certs = NULL, *chain = NULL;
	X509_STORE_CTX ctx;
	X509 *signer, *tmp_cert;
	int signer_trusted = 0;
	EVP_PKEY *skey;
	int ret;
	char buf[256];

	txt = integer_str(X509_get_serialNumber(cert));
	if (txt) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: Searching OCSP response for peer certificate serialNumber: %s", txt);
		os_free(txt);
	}

	SSL_get0_ocsp_response(ssl, &resp_data, &resp_len);
	if (resp_data == NULL || resp_len == 0) {
		wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
		return OCSP_NO_RESPONSE;
	}

	wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", resp_data, resp_len);

	resp = d2i_OCSPResponse(NULL, &resp_data, resp_len);
	if (!resp) {
		wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSPResponse");
		return OCSP_INVALID;
	}

	status = ASN1_ENUMERATED_get(resp->responseStatus);
	if (status != 0) {
		wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d",
			   status);
		return OCSP_INVALID;
	}

	bytes = resp->responseBytes;

	if (!bytes ||
	    OBJ_obj2nid(bytes->responseType) != NID_id_pkix_OCSP_basic) {
		wpa_printf(MSG_INFO,
			   "OpenSSL: Could not find BasicOCSPResponse");
		return OCSP_INVALID;
	}

	basic_data = ASN1_STRING_data(bytes->response);
	basic_len = ASN1_STRING_length(bytes->response);
	wpa_hexdump(MSG_DEBUG, "OpenSSL: BasicOCSPResponse",
		    basic_data, basic_len);

	basic = d2i_BasicOCSPResponse(NULL, &basic_data, basic_len);
	if (!basic) {
		wpa_printf(MSG_INFO,
			   "OpenSSL: Could not parse BasicOCSPResponse");
		OCSPResponse_free(resp);
		return OCSP_INVALID;
	}

	rd = basic->tbsResponseData;

	if (basic->certs) {
		untrusted = sk_X509_dup(basic->certs);

		num = sk_X509_num(basic->certs);
		for (i = 0; i < num; i++) {
			X509 *extra_cert;

			extra_cert = sk_X509_value(basic->certs, i);
			X509_NAME_oneline(X509_get_subject_name(extra_cert),
					  buf, sizeof(buf));
			wpa_printf(MSG_DEBUG,
				   "OpenSSL: BasicOCSPResponse cert %s", buf);

			if (!sk_X509_push(untrusted, extra_cert)) {
				wpa_printf(MSG_DEBUG,
					   "OpenSSL: Could not add certificate to the untrusted stack");
			}
		}
	}

	store = SSL_CTX_get_cert_store(ssl_ctx);
	if (issuer) {
		if (X509_STORE_add_cert(store, issuer) != 1) {
			tls_show_errors(MSG_INFO, __func__,
					"OpenSSL: Could not add issuer to certificate store");
		}
		certs = sk_X509_new_null();
		if (certs) {
			tmp_cert = X509_dup(issuer);
			if (tmp_cert && !sk_X509_push(certs, tmp_cert)) {
				tls_show_errors(
					MSG_INFO, __func__,
					"OpenSSL: Could not add issuer to OCSP responder trust store");
				X509_free(tmp_cert);
				sk_X509_free(certs);
				certs = NULL;
			}
			if (certs && issuer_issuer) {
				tmp_cert = X509_dup(issuer_issuer);
				if (tmp_cert &&
				    !sk_X509_push(certs, tmp_cert)) {
					tls_show_errors(
						MSG_INFO, __func__,
						"OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
					X509_free(tmp_cert);
				}
			}
		}
	}

	signer = ocsp_find_signer(certs, rd->responderID);
	if (!signer)
		signer = ocsp_find_signer(untrusted, rd->responderID);
	else
		signer_trusted = 1;
	if (!signer) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: Could not find OCSP signer certificate");
		goto fail;
	}

	skey = X509_get_pubkey(signer);
	if (!skey) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: Could not get OCSP signer public key");
		goto fail;
	}
	if (ASN1_item_verify(ASN1_ITEM_rptr(ResponseData),
			     basic->signatureAlgorithm, basic->signature,
			     basic->tbsResponseData, skey) <= 0) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: BasicOCSPResponse signature is invalid");
		goto fail;
	}

	X509_NAME_oneline(X509_get_subject_name(signer), buf, sizeof(buf));
	wpa_printf(MSG_DEBUG,
		   "OpenSSL: Found OCSP signer certificate %s and verified BasicOCSPResponse signature",
		   buf);

	if (!X509_STORE_CTX_init(&ctx, store, signer, untrusted))
		goto fail;
	X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_OCSP_HELPER);
	ret = X509_verify_cert(&ctx);
	chain = X509_STORE_CTX_get1_chain(&ctx);
	X509_STORE_CTX_cleanup(&ctx);
	if (ret <= 0) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: Could not validate OCSP signer certificate");
		goto fail;
	}

	if (!chain || sk_X509_num(chain) <= 0) {
		wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP signer chain found");
		goto fail;
	}

	if (!signer_trusted) {
		X509_check_purpose(signer, -1, 0);
		if ((signer->ex_flags & EXFLAG_XKUSAGE) &&
		    (signer->ex_xkusage & XKU_OCSP_SIGN)) {
			wpa_printf(MSG_DEBUG,
				   "OpenSSL: OCSP signer certificate delegation OK");
		} else {
			tmp_cert = sk_X509_value(chain, sk_X509_num(chain) - 1);
			if (X509_check_trust(tmp_cert, NID_OCSP_sign, 0) !=
			    X509_TRUST_TRUSTED) {
				wpa_printf(MSG_DEBUG,
					   "OpenSSL: OCSP signer certificate not trusted");
				result = OCSP_NO_RESPONSE;
				goto fail;
			}
		}
	}

	wpa_printf(MSG_DEBUG, "OpenSSL: OCSP version: %lu",
		   ASN1_INTEGER_get(rd->version));

	txt = responderid_str(rd->responderID);
	if (txt) {
		wpa_printf(MSG_DEBUG, "OpenSSL: OCSP responderID: %s",
			   txt);
		os_free(txt);
	}

	txt = generalizedtime_str(rd->producedAt);
	if (txt) {
		wpa_printf(MSG_DEBUG, "OpenSSL: OCSP producedAt: %s",
			   txt);
		os_free(txt);
	}

	num_resp = sk_SingleResponse_num(rd->responses);
	if (num_resp == 0) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: No OCSP SingleResponse within BasicOCSPResponse");
		result = OCSP_NO_RESPONSE;
		goto fail;
	}
	cmp_sresp = sk_SingleResponse_value(rd->responses, 0);
	for (j = 0; j < num_resp; j++) {
		SingleResponse *sresp;
		CertID *cid1, *cid2;

		sresp = sk_SingleResponse_value(rd->responses, j);
		wpa_printf(MSG_DEBUG, "OpenSSL: OCSP SingleResponse %u/%u",
			   j + 1, num_resp);

		txt = algor_str(sresp->certID->hashAlgorithm);
		if (txt) {
			wpa_printf(MSG_DEBUG,
				   "OpenSSL: certID hashAlgorithm: %s", txt);
			os_free(txt);
		}

		txt = octet_string_str(sresp->certID->issuerNameHash);
		if (txt) {
			wpa_printf(MSG_DEBUG,
				   "OpenSSL: certID issuerNameHash: %s", txt);
			os_free(txt);
		}

		txt = octet_string_str(sresp->certID->issuerKeyHash);
		if (txt) {
			wpa_printf(MSG_DEBUG,
				   "OpenSSL: certID issuerKeyHash: %s", txt);
			os_free(txt);
		}

		txt = integer_str(sresp->certID->serialNumber);
		if (txt) {
			wpa_printf(MSG_DEBUG,
				   "OpenSSL: certID serialNumber: %s", txt);
			os_free(txt);
		}

		switch (sresp->certStatus->type) {
		case 0:
			wpa_printf(MSG_DEBUG, "OpenSSL: certStatus: good");
			break;
		case 1:
			wpa_printf(MSG_DEBUG, "OpenSSL: certStatus: revoked");
			break;
		default:
			wpa_printf(MSG_DEBUG, "OpenSSL: certStatus: unknown");
			break;
		}

		txt = generalizedtime_str(sresp->thisUpdate);
		if (txt) {
			wpa_printf(MSG_DEBUG, "OpenSSL: thisUpdate: %s", txt);
			os_free(txt);
		}

		if (sresp->nextUpdate) {
			txt = generalizedtime_str(sresp->nextUpdate);
			if (txt) {
				wpa_printf(MSG_DEBUG, "OpenSSL: nextUpdate: %s",
					   txt);
				os_free(txt);
			}
		}

		txt = extensions_str("singleExtensions",
				     sresp->singleExtensions);
		if (txt) {
			wpa_printf(MSG_DEBUG, "OpenSSL: %s", txt);
			os_free(txt);
		}

		cid1 = cmp_sresp->certID;
		cid2 = sresp->certID;
		if (j > 0 &&
		    (OBJ_cmp(cid1->hashAlgorithm->algorithm,
			     cid2->hashAlgorithm->algorithm) != 0 ||
		     ASN1_OCTET_STRING_cmp(cid1->issuerNameHash,
					   cid2->issuerNameHash) != 0 ||
		     ASN1_OCTET_STRING_cmp(cid1->issuerKeyHash,
					   cid2->issuerKeyHash) != 0)) {
			wpa_printf(MSG_DEBUG,
				   "OpenSSL: Different OCSP response issuer information between SingleResponse values within BasicOCSPResponse");
			goto fail;
		}

		if (!matching_resp && issuer &&
		    ASN1_INTEGER_cmp(sresp->certID->serialNumber,
				     X509_get_serialNumber(cert)) == 0 &&
		    issuer_match(cert, issuer, sresp->certID) == 0) {
			wpa_printf(MSG_DEBUG,
				   "OpenSSL: This response matches peer certificate");
			matching_resp = sresp;
		}
	}

	txt = extensions_str("responseExtensions", rd->responseExtensions);
	if (txt) {
		wpa_printf(MSG_DEBUG, "OpenSSL: %s", txt);
		os_free(txt);
	}

	if (!matching_resp) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: Could not find OCSP response that matches the peer certificate");
		result = OCSP_NO_RESPONSE;
		goto fail;
	}

	if (!ocsp_resp_valid(matching_resp->thisUpdate,
			     matching_resp->nextUpdate)) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: OCSP response not valid at this time");
		goto fail;
	}

	if (matching_resp->certStatus->type == 1) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: OCSP response indicated that the peer certificate has been revoked");
		result = OCSP_REVOKED;
		goto fail;
	}

	if (matching_resp->certStatus->type != 0) {
		wpa_printf(MSG_DEBUG,
			   "OpenSSL: OCSP response did not indicate good status");
		result = OCSP_NO_RESPONSE;
		goto fail;
	}

	/* OCSP response indicated the certificate is good. */
	result = OCSP_GOOD;
fail:
	sk_X509_pop_free(chain, X509_free);
	sk_X509_free(untrusted);
	sk_X509_pop_free(certs, X509_free);
	BasicOCSPResponse_free(basic);
	OCSPResponse_free(resp);

	return result;
}

#endif /* OPENSSL_IS_BORINGSSL */