]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
3c7d0945 | 2 | * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. |
5782ceb2 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
5782ceb2 DSH |
8 | */ |
9 | ||
3e41ac35 MC |
10 | #include <openssl/opensslconf.h> |
11 | ||
12 | #ifdef OPENSSL_NO_OCSP | |
13 | NON_EMPTY_TRANSLATION_UNIT | |
14 | #else | |
0f113f3e MC |
15 | # ifdef OPENSSL_SYS_VMS |
16 | # define _XOPEN_SOURCE_EXTENDED/* So fd_set and friends get properly defined | |
17 | * on OpenVMS */ | |
18 | # endif | |
19 | ||
0f113f3e MC |
20 | # include <stdio.h> |
21 | # include <stdlib.h> | |
22 | # include <string.h> | |
23 | # include <time.h> | |
98cd49db | 24 | # include <ctype.h> |
3e41ac35 MC |
25 | |
26 | /* Needs to be included before the openssl headers */ | |
27 | # include "apps.h" | |
0f113f3e MC |
28 | # include <openssl/e_os2.h> |
29 | # include <openssl/crypto.h> | |
30 | # include <openssl/err.h> | |
31 | # include <openssl/ssl.h> | |
32 | # include <openssl/evp.h> | |
33 | # include <openssl/bn.h> | |
34 | # include <openssl/x509v3.h> | |
35 | ||
0f113f3e | 36 | /* Maximum leeway in validity period: default 5 minutes */ |
7e1b7485 | 37 | # define MAX_VALIDITY_PERIOD (5 * 60) |
0f113f3e MC |
38 | |
39 | static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, | |
40 | const EVP_MD *cert_id_md, X509 *issuer, | |
41 | STACK_OF(OCSP_CERTID) *ids); | |
42 | static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, | |
43 | const EVP_MD *cert_id_md, X509 *issuer, | |
44 | STACK_OF(OCSP_CERTID) *ids); | |
7e1b7485 | 45 | static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, |
0f113f3e MC |
46 | STACK_OF(OPENSSL_STRING) *names, |
47 | STACK_OF(OCSP_CERTID) *ids, long nsec, | |
48 | long maxage); | |
7e1b7485 | 49 | static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, |
3bb0f989 | 50 | CA_DB *db, STACK_OF(X509) *ca, X509 *rcert, |
0f113f3e MC |
51 | EVP_PKEY *rkey, const EVP_MD *md, |
52 | STACK_OF(X509) *rother, unsigned long flags, | |
53 | int nmin, int ndays, int badsig); | |
ee306a13 | 54 | |
f85b68cd | 55 | static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser); |
c45a48c1 | 56 | static BIO *init_responder(const char *port); |
a773b52a | 57 | static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio); |
ee306a13 | 58 | static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp); |
f9e55034 MC |
59 | |
60 | # ifndef OPENSSL_NO_SOCK | |
76e0cd12 DSH |
61 | static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host, |
62 | const char *path, | |
0f113f3e MC |
63 | const STACK_OF(CONF_VALUE) *headers, |
64 | OCSP_REQUEST *req, int req_timeout); | |
f9e55034 | 65 | # endif |
ee306a13 | 66 | |
7e1b7485 RS |
67 | typedef enum OPTION_choice { |
68 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
69 | OPT_OUTFILE, OPT_TIMEOUT, OPT_URL, OPT_HOST, OPT_PORT, | |
70 | OPT_IGNORE_ERR, OPT_NOVERIFY, OPT_NONCE, OPT_NO_NONCE, | |
71 | OPT_RESP_NO_CERTS, OPT_RESP_KEY_ID, OPT_NO_CERTS, | |
72 | OPT_NO_SIGNATURE_VERIFY, OPT_NO_CERT_VERIFY, OPT_NO_CHAIN, | |
73 | OPT_NO_CERT_CHECKS, OPT_NO_EXPLICIT, OPT_TRUST_OTHER, | |
74 | OPT_NO_INTERN, OPT_BADSIG, OPT_TEXT, OPT_REQ_TEXT, OPT_RESP_TEXT, | |
75 | OPT_REQIN, OPT_RESPIN, OPT_SIGNER, OPT_VAFILE, OPT_SIGN_OTHER, | |
2b6bcb70 | 76 | OPT_VERIFY_OTHER, OPT_CAFILE, OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH, |
7e1b7485 | 77 | OPT_VALIDITY_PERIOD, OPT_STATUS_AGE, OPT_SIGNKEY, OPT_REQOUT, |
c6724060 | 78 | OPT_RESPOUT, OPT_PATH, OPT_ISSUER, OPT_CERT, OPT_SERIAL, |
7e1b7485 RS |
79 | OPT_INDEX, OPT_CA, OPT_NMIN, OPT_REQUEST, OPT_NDAYS, OPT_RSIGNER, |
80 | OPT_RKEY, OPT_ROTHER, OPT_RMD, OPT_HEADER, | |
81 | OPT_V_ENUM, | |
82 | OPT_MD | |
83 | } OPTION_CHOICE; | |
84 | ||
44c83ebd | 85 | const OPTIONS ocsp_options[] = { |
7e1b7485 RS |
86 | {"help", OPT_HELP, '-', "Display this summary"}, |
87 | {"out", OPT_OUTFILE, '>', "Output filename"}, | |
9a13bb38 RS |
88 | {"timeout", OPT_TIMEOUT, 'p', |
89 | "Connection timeout (in seconds) to the OCSP responder"}, | |
7e1b7485 | 90 | {"url", OPT_URL, 's', "Responder URL"}, |
ceab33e2 | 91 | {"host", OPT_HOST, 's', "TCP/IP hostname:port to connect to"}, |
7e1b7485 | 92 | {"port", OPT_PORT, 'p', "Port to run responder on"}, |
12d56b29 | 93 | {"ignore_err", OPT_IGNORE_ERR, '-', |
bbe9c3d5 | 94 | "Ignore error on OCSP request or response and continue running"}, |
7e1b7485 RS |
95 | {"noverify", OPT_NOVERIFY, '-', "Don't verify response at all"}, |
96 | {"nonce", OPT_NONCE, '-', "Add OCSP nonce to request"}, | |
97 | {"no_nonce", OPT_NO_NONCE, '-', "Don't add OCSP nonce to request"}, | |
98 | {"resp_no_certs", OPT_RESP_NO_CERTS, '-', | |
99 | "Don't include any certificates in response"}, | |
100 | {"resp_key_id", OPT_RESP_KEY_ID, '-', | |
ceab33e2 | 101 | "Identify response by signing certificate key ID"}, |
7e1b7485 RS |
102 | {"no_certs", OPT_NO_CERTS, '-', |
103 | "Don't include any certificates in signed request"}, | |
104 | {"no_signature_verify", OPT_NO_SIGNATURE_VERIFY, '-', | |
105 | "Don't check signature on response"}, | |
106 | {"no_cert_verify", OPT_NO_CERT_VERIFY, '-', | |
107 | "Don't check signing certificate"}, | |
108 | {"no_chain", OPT_NO_CHAIN, '-', "Don't chain verify response"}, | |
109 | {"no_cert_checks", OPT_NO_CERT_CHECKS, '-', | |
110 | "Don't do additional checks on signing certificate"}, | |
12d56b29 F |
111 | {"no_explicit", OPT_NO_EXPLICIT, '-', |
112 | "Do not explicitly check the chain, just verify the root"}, | |
7e1b7485 RS |
113 | {"trust_other", OPT_TRUST_OTHER, '-', |
114 | "Don't verify additional certificates"}, | |
115 | {"no_intern", OPT_NO_INTERN, '-', | |
116 | "Don't search certificates contained in response for signer"}, | |
16e1b281 F |
117 | {"badsig", OPT_BADSIG, '-', |
118 | "Corrupt last byte of loaded OSCP response signature (for test)"}, | |
7e1b7485 RS |
119 | {"text", OPT_TEXT, '-', "Print text form of request and response"}, |
120 | {"req_text", OPT_REQ_TEXT, '-', "Print text form of request"}, | |
121 | {"resp_text", OPT_RESP_TEXT, '-', "Print text form of response"}, | |
122 | {"reqin", OPT_REQIN, 's', "File with the DER-encoded request"}, | |
123 | {"respin", OPT_RESPIN, 's', "File with the DER-encoded response"}, | |
124 | {"signer", OPT_SIGNER, '<', "Certificate to sign OCSP request with"}, | |
125 | {"VAfile", OPT_VAFILE, '<', "Validator certificates file"}, | |
126 | {"sign_other", OPT_SIGN_OTHER, '<', | |
127 | "Additional certificates to include in signed request"}, | |
128 | {"verify_other", OPT_VERIFY_OTHER, '<', | |
129 | "Additional certificates to search for signer"}, | |
130 | {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"}, | |
131 | {"CApath", OPT_CAPATH, '<', "Trusted certificates directory"}, | |
2b6bcb70 MC |
132 | {"no-CAfile", OPT_NOCAFILE, '-', |
133 | "Do not load the default certificates file"}, | |
134 | {"no-CApath", OPT_NOCAPATH, '-', | |
135 | "Do not load certificates from the default certificates directory"}, | |
7e1b7485 RS |
136 | {"validity_period", OPT_VALIDITY_PERIOD, 'u', |
137 | "Maximum validity discrepancy in seconds"}, | |
138 | {"status_age", OPT_STATUS_AGE, 'p', "Maximum status age in seconds"}, | |
139 | {"signkey", OPT_SIGNKEY, 's', "Private key to sign OCSP request with"}, | |
140 | {"reqout", OPT_REQOUT, 's', "Output file for the DER-encoded request"}, | |
141 | {"respout", OPT_RESPOUT, 's', "Output file for the DER-encoded response"}, | |
142 | {"path", OPT_PATH, 's', "Path to use in OCSP request"}, | |
c6724060 | 143 | {"issuer", OPT_ISSUER, '<', "Issuer certificate"}, |
7e1b7485 | 144 | {"cert", OPT_CERT, '<', "Certificate to check"}, |
16e1b281 | 145 | {"serial", OPT_SERIAL, 's', "Serial number to check"}, |
7e1b7485 RS |
146 | {"index", OPT_INDEX, '<', "Certificate status index file"}, |
147 | {"CA", OPT_CA, '<', "CA certificate"}, | |
148 | {"nmin", OPT_NMIN, 'p', "Number of minutes before next update"}, | |
149 | {"nrequest", OPT_REQUEST, 'p', | |
150 | "Number of requests to accept (default unlimited)"}, | |
151 | {"ndays", OPT_NDAYS, 'p', "Number of days before next update"}, | |
152 | {"rsigner", OPT_RSIGNER, '<', | |
0ad69cd6 | 153 | "Responder certificate to sign responses with"}, |
7e1b7485 RS |
154 | {"rkey", OPT_RKEY, '<', "Responder key to sign responses with"}, |
155 | {"rother", OPT_ROTHER, '<', "Other certificates to include in response"}, | |
16e1b281 | 156 | {"rmd", OPT_RMD, 's', "Digest Algorithm to use in signature of OCSP response"}, |
7e1b7485 | 157 | {"header", OPT_HEADER, 's', "key=value header to add"}, |
16e1b281 | 158 | {"", OPT_MD, '-', "Any supported digest algorithm (sha1,sha256, ... )"}, |
7e1b7485 RS |
159 | OPT_V_OPTIONS, |
160 | {NULL} | |
161 | }; | |
162 | ||
163 | int ocsp_main(int argc, char **argv) | |
0f113f3e | 164 | { |
7e1b7485 RS |
165 | BIO *acbio = NULL, *cbio = NULL, *derbio = NULL, *out = NULL; |
166 | const EVP_MD *cert_id_md = NULL, *rsign_md = NULL; | |
6302bbd2 | 167 | int trailing_md = 0; |
7e1b7485 RS |
168 | CA_DB *rdb = NULL; |
169 | EVP_PKEY *key = NULL, *rkey = NULL; | |
170 | OCSP_BASICRESP *bs = NULL; | |
0f113f3e MC |
171 | OCSP_REQUEST *req = NULL; |
172 | OCSP_RESPONSE *resp = NULL; | |
7e1b7485 RS |
173 | STACK_OF(CONF_VALUE) *headers = NULL; |
174 | STACK_OF(OCSP_CERTID) *ids = NULL; | |
175 | STACK_OF(OPENSSL_STRING) *reqnames = NULL; | |
176 | STACK_OF(X509) *sign_other = NULL, *verify_other = NULL, *rother = NULL; | |
88806cfc | 177 | STACK_OF(X509) *issuers = NULL; |
3bb0f989 TS |
178 | X509 *issuer = NULL, *cert = NULL; |
179 | STACK_OF(X509) *rca_cert = NULL; | |
0f113f3e | 180 | X509 *signer = NULL, *rsigner = NULL; |
0f113f3e MC |
181 | X509_STORE *store = NULL; |
182 | X509_VERIFY_PARAM *vpm = NULL; | |
cc696296 F |
183 | const char *CAfile = NULL, *CApath = NULL; |
184 | char *header, *value; | |
7e1b7485 RS |
185 | char *host = NULL, *port = NULL, *path = "/", *outfile = NULL; |
186 | char *rca_filename = NULL, *reqin = NULL, *respin = NULL; | |
187 | char *reqout = NULL, *respout = NULL, *ridx_filename = NULL; | |
188 | char *rsignfile = NULL, *rkeyfile = NULL; | |
0f113f3e | 189 | char *sign_certfile = NULL, *verify_certfile = NULL, *rcertfile = NULL; |
7e1b7485 RS |
190 | char *signfile = NULL, *keyfile = NULL; |
191 | char *thost = NULL, *tport = NULL, *tpath = NULL; | |
2b6bcb70 | 192 | int noCAfile = 0, noCApath = 0; |
7e1b7485 RS |
193 | int accept_count = -1, add_nonce = 1, noverify = 0, use_ssl = -1; |
194 | int vpmtouched = 0, badsig = 0, i, ignore_err = 0, nmin = 0, ndays = -1; | |
f9e55034 MC |
195 | int req_text = 0, resp_text = 0, ret = 1; |
196 | #ifndef OPENSSL_NO_SOCK | |
197 | int req_timeout = -1; | |
198 | #endif | |
7e1b7485 | 199 | long nsec = MAX_VALIDITY_PERIOD, maxage = -1; |
0f113f3e | 200 | unsigned long sign_flags = 0, verify_flags = 0, rflags = 0; |
7e1b7485 RS |
201 | OPTION_CHOICE o; |
202 | char *prog; | |
0f113f3e | 203 | |
0f113f3e | 204 | reqnames = sk_OPENSSL_STRING_new_null(); |
2234212c | 205 | if (reqnames == NULL) |
7e1b7485 | 206 | goto end; |
0f113f3e | 207 | ids = sk_OCSP_CERTID_new_null(); |
2234212c | 208 | if (ids == NULL) |
7e1b7485 RS |
209 | goto end; |
210 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) | |
211 | return 1; | |
212 | ||
213 | prog = opt_init(argc, argv, ocsp_options); | |
214 | while ((o = opt_next()) != OPT_EOF) { | |
215 | switch (o) { | |
216 | case OPT_EOF: | |
217 | case OPT_ERR: | |
218 | opthelp: | |
219 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
220 | goto end; | |
221 | case OPT_HELP: | |
222 | ret = 0; | |
223 | opt_help(ocsp_options); | |
224 | goto end; | |
225 | case OPT_OUTFILE: | |
226 | outfile = opt_arg(); | |
227 | break; | |
228 | case OPT_TIMEOUT: | |
f9e55034 | 229 | #ifndef OPENSSL_NO_SOCK |
7e1b7485 | 230 | req_timeout = atoi(opt_arg()); |
f9e55034 | 231 | #endif |
7e1b7485 RS |
232 | break; |
233 | case OPT_URL: | |
b548a1f1 RS |
234 | OPENSSL_free(thost); |
235 | OPENSSL_free(tport); | |
236 | OPENSSL_free(tpath); | |
4b8d8e2a | 237 | thost = tport = tpath = NULL; |
7e1b7485 RS |
238 | if (!OCSP_parse_url(opt_arg(), &host, &port, &path, &use_ssl)) { |
239 | BIO_printf(bio_err, "%s Error parsing URL\n", prog); | |
240 | goto end; | |
241 | } | |
242 | thost = host; | |
243 | tport = port; | |
244 | tpath = path; | |
245 | break; | |
246 | case OPT_HOST: | |
247 | host = opt_arg(); | |
248 | break; | |
249 | case OPT_PORT: | |
250 | port = opt_arg(); | |
251 | break; | |
252 | case OPT_IGNORE_ERR: | |
0f113f3e | 253 | ignore_err = 1; |
7e1b7485 RS |
254 | break; |
255 | case OPT_NOVERIFY: | |
0f113f3e | 256 | noverify = 1; |
7e1b7485 RS |
257 | break; |
258 | case OPT_NONCE: | |
0f113f3e | 259 | add_nonce = 2; |
7e1b7485 RS |
260 | break; |
261 | case OPT_NO_NONCE: | |
0f113f3e | 262 | add_nonce = 0; |
7e1b7485 RS |
263 | break; |
264 | case OPT_RESP_NO_CERTS: | |
0f113f3e | 265 | rflags |= OCSP_NOCERTS; |
7e1b7485 RS |
266 | break; |
267 | case OPT_RESP_KEY_ID: | |
0f113f3e | 268 | rflags |= OCSP_RESPID_KEY; |
7e1b7485 RS |
269 | break; |
270 | case OPT_NO_CERTS: | |
0f113f3e | 271 | sign_flags |= OCSP_NOCERTS; |
7e1b7485 RS |
272 | break; |
273 | case OPT_NO_SIGNATURE_VERIFY: | |
0f113f3e | 274 | verify_flags |= OCSP_NOSIGS; |
7e1b7485 RS |
275 | break; |
276 | case OPT_NO_CERT_VERIFY: | |
0f113f3e | 277 | verify_flags |= OCSP_NOVERIFY; |
7e1b7485 RS |
278 | break; |
279 | case OPT_NO_CHAIN: | |
0f113f3e | 280 | verify_flags |= OCSP_NOCHAIN; |
7e1b7485 RS |
281 | break; |
282 | case OPT_NO_CERT_CHECKS: | |
0f113f3e | 283 | verify_flags |= OCSP_NOCHECKS; |
7e1b7485 RS |
284 | break; |
285 | case OPT_NO_EXPLICIT: | |
0f113f3e | 286 | verify_flags |= OCSP_NOEXPLICIT; |
7e1b7485 RS |
287 | break; |
288 | case OPT_TRUST_OTHER: | |
0f113f3e | 289 | verify_flags |= OCSP_TRUSTOTHER; |
7e1b7485 RS |
290 | break; |
291 | case OPT_NO_INTERN: | |
0f113f3e | 292 | verify_flags |= OCSP_NOINTERN; |
7e1b7485 RS |
293 | break; |
294 | case OPT_BADSIG: | |
0f113f3e | 295 | badsig = 1; |
7e1b7485 RS |
296 | break; |
297 | case OPT_TEXT: | |
298 | req_text = resp_text = 1; | |
299 | break; | |
300 | case OPT_REQ_TEXT: | |
0f113f3e | 301 | req_text = 1; |
7e1b7485 RS |
302 | break; |
303 | case OPT_RESP_TEXT: | |
0f113f3e | 304 | resp_text = 1; |
7e1b7485 RS |
305 | break; |
306 | case OPT_REQIN: | |
307 | reqin = opt_arg(); | |
308 | break; | |
309 | case OPT_RESPIN: | |
310 | respin = opt_arg(); | |
311 | break; | |
312 | case OPT_SIGNER: | |
313 | signfile = opt_arg(); | |
314 | break; | |
315 | case OPT_VAFILE: | |
316 | verify_certfile = opt_arg(); | |
317 | verify_flags |= OCSP_TRUSTOTHER; | |
318 | break; | |
319 | case OPT_SIGN_OTHER: | |
320 | sign_certfile = opt_arg(); | |
321 | break; | |
322 | case OPT_VERIFY_OTHER: | |
323 | verify_certfile = opt_arg(); | |
324 | break; | |
325 | case OPT_CAFILE: | |
326 | CAfile = opt_arg(); | |
327 | break; | |
328 | case OPT_CAPATH: | |
329 | CApath = opt_arg(); | |
330 | break; | |
2b6bcb70 MC |
331 | case OPT_NOCAFILE: |
332 | noCAfile = 1; | |
333 | break; | |
334 | case OPT_NOCAPATH: | |
335 | noCApath = 1; | |
336 | break; | |
7e1b7485 RS |
337 | case OPT_V_CASES: |
338 | if (!opt_verify(o, vpm)) | |
0f113f3e | 339 | goto end; |
7e1b7485 RS |
340 | vpmtouched++; |
341 | break; | |
342 | case OPT_VALIDITY_PERIOD: | |
343 | opt_long(opt_arg(), &nsec); | |
344 | break; | |
345 | case OPT_STATUS_AGE: | |
346 | opt_long(opt_arg(), &maxage); | |
347 | break; | |
348 | case OPT_SIGNKEY: | |
349 | keyfile = opt_arg(); | |
350 | break; | |
351 | case OPT_REQOUT: | |
352 | reqout = opt_arg(); | |
353 | break; | |
354 | case OPT_RESPOUT: | |
355 | respout = opt_arg(); | |
356 | break; | |
357 | case OPT_PATH: | |
358 | path = opt_arg(); | |
359 | break; | |
c6724060 | 360 | case OPT_ISSUER: |
a773b52a | 361 | issuer = load_cert(opt_arg(), FORMAT_PEM, "issuer certificate"); |
c6724060 RS |
362 | if (issuer == NULL) |
363 | goto end; | |
bb7fc98c DSH |
364 | if (issuers == NULL) { |
365 | if ((issuers = sk_X509_new_null()) == NULL) | |
366 | goto end; | |
367 | } | |
c6724060 RS |
368 | sk_X509_push(issuers, issuer); |
369 | break; | |
7e1b7485 RS |
370 | case OPT_CERT: |
371 | X509_free(cert); | |
a773b52a | 372 | cert = load_cert(opt_arg(), FORMAT_PEM, "certificate"); |
7e1b7485 RS |
373 | if (cert == NULL) |
374 | goto end; | |
375 | if (cert_id_md == NULL) | |
376 | cert_id_md = EVP_sha1(); | |
377 | if (!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids)) | |
378 | goto end; | |
379 | if (!sk_OPENSSL_STRING_push(reqnames, opt_arg())) | |
380 | goto end; | |
6302bbd2 | 381 | trailing_md = 0; |
7e1b7485 RS |
382 | break; |
383 | case OPT_SERIAL: | |
384 | if (cert_id_md == NULL) | |
385 | cert_id_md = EVP_sha1(); | |
386 | if (!add_ocsp_serial(&req, opt_arg(), cert_id_md, issuer, ids)) | |
387 | goto end; | |
388 | if (!sk_OPENSSL_STRING_push(reqnames, opt_arg())) | |
389 | goto end; | |
6302bbd2 | 390 | trailing_md = 0; |
7e1b7485 RS |
391 | break; |
392 | case OPT_INDEX: | |
393 | ridx_filename = opt_arg(); | |
394 | break; | |
395 | case OPT_CA: | |
396 | rca_filename = opt_arg(); | |
397 | break; | |
398 | case OPT_NMIN: | |
399 | opt_int(opt_arg(), &nmin); | |
0f113f3e MC |
400 | if (ndays == -1) |
401 | ndays = 0; | |
7e1b7485 RS |
402 | break; |
403 | case OPT_REQUEST: | |
404 | opt_int(opt_arg(), &accept_count); | |
405 | break; | |
406 | case OPT_NDAYS: | |
407 | ndays = atoi(opt_arg()); | |
408 | break; | |
409 | case OPT_RSIGNER: | |
410 | rsignfile = opt_arg(); | |
411 | break; | |
412 | case OPT_RKEY: | |
413 | rkeyfile = opt_arg(); | |
414 | break; | |
415 | case OPT_ROTHER: | |
416 | rcertfile = opt_arg(); | |
417 | break; | |
16e1b281 | 418 | case OPT_RMD: /* Response MessageDigest */ |
7e1b7485 RS |
419 | if (!opt_md(opt_arg(), &rsign_md)) |
420 | goto end; | |
421 | break; | |
422 | case OPT_HEADER: | |
423 | header = opt_arg(); | |
424 | value = strchr(header, '='); | |
425 | if (value == NULL) { | |
426 | BIO_printf(bio_err, "Missing = in header key=value\n"); | |
427 | goto opthelp; | |
428 | } | |
429 | *value++ = '\0'; | |
430 | if (!X509V3_add_value(header, value, &headers)) | |
431 | goto end; | |
432 | break; | |
433 | case OPT_MD: | |
6302bbd2 | 434 | if (trailing_md) { |
7e1b7485 RS |
435 | BIO_printf(bio_err, |
436 | "%s: Digest must be before -cert or -serial\n", | |
437 | prog); | |
438 | goto opthelp; | |
439 | } | |
440 | if (!opt_md(opt_unknown(), &cert_id_md)) | |
441 | goto opthelp; | |
6302bbd2 | 442 | trailing_md = 1; |
7e1b7485 | 443 | break; |
0f113f3e | 444 | } |
0f113f3e | 445 | } |
6302bbd2 DSH |
446 | |
447 | if (trailing_md) { | |
448 | BIO_printf(bio_err, "%s: Digest must be before -cert or -serial\n", | |
449 | prog); | |
450 | goto opthelp; | |
451 | } | |
7e1b7485 | 452 | argc = opt_num_rest(); |
03358517 KR |
453 | if (argc != 0) |
454 | goto opthelp; | |
0f113f3e MC |
455 | |
456 | /* Have we anything to do? */ | |
2234212c PY |
457 | if (req == NULL&& reqin == NULL |
458 | && respin == NULL && !(port != NULL && ridx_filename != NULL)) | |
7e1b7485 | 459 | goto opthelp; |
0f113f3e | 460 | |
bdd58d98 | 461 | out = bio_open_default(outfile, 'w', FORMAT_TEXT); |
7e1b7485 | 462 | if (out == NULL) |
0f113f3e | 463 | goto end; |
0f113f3e | 464 | |
2234212c | 465 | if (req == NULL && (add_nonce != 2)) |
0f113f3e MC |
466 | add_nonce = 0; |
467 | ||
2234212c | 468 | if (req == NULL && reqin != NULL) { |
bdd58d98 | 469 | derbio = bio_open_default(reqin, 'r', FORMAT_ASN1); |
7e1b7485 | 470 | if (derbio == NULL) |
0f113f3e | 471 | goto end; |
0f113f3e MC |
472 | req = d2i_OCSP_REQUEST_bio(derbio, NULL); |
473 | BIO_free(derbio); | |
2234212c | 474 | if (req == NULL) { |
0f113f3e MC |
475 | BIO_printf(bio_err, "Error reading OCSP request\n"); |
476 | goto end; | |
477 | } | |
478 | } | |
479 | ||
2234212c | 480 | if (req == NULL && port != NULL) { |
0f113f3e | 481 | acbio = init_responder(port); |
2234212c | 482 | if (acbio == NULL) |
0f113f3e MC |
483 | goto end; |
484 | } | |
485 | ||
2234212c PY |
486 | if (rsignfile != NULL) { |
487 | if (rkeyfile == NULL) | |
0f113f3e | 488 | rkeyfile = rsignfile; |
a773b52a | 489 | rsigner = load_cert(rsignfile, FORMAT_PEM, "responder certificate"); |
2234212c | 490 | if (rsigner == NULL) { |
0f113f3e MC |
491 | BIO_printf(bio_err, "Error loading responder certificate\n"); |
492 | goto end; | |
493 | } | |
3bb0f989 TS |
494 | if (!load_certs(rca_filename, &rca_cert, FORMAT_PEM, |
495 | NULL, "CA certificate")) | |
496 | goto end; | |
2234212c | 497 | if (rcertfile != NULL) { |
a773b52a | 498 | if (!load_certs(rcertfile, &rother, FORMAT_PEM, NULL, |
0996dc54 | 499 | "responder other certificates")) |
0f113f3e MC |
500 | goto end; |
501 | } | |
7e1b7485 | 502 | rkey = load_key(rkeyfile, FORMAT_PEM, 0, NULL, NULL, |
0f113f3e | 503 | "responder private key"); |
2234212c | 504 | if (rkey == NULL) |
0f113f3e MC |
505 | goto end; |
506 | } | |
2234212c | 507 | if (acbio != NULL) |
0f113f3e MC |
508 | BIO_printf(bio_err, "Waiting for OCSP client connections...\n"); |
509 | ||
2234212c | 510 | redo_accept: |
0f113f3e | 511 | |
2234212c | 512 | if (acbio != NULL) { |
a773b52a | 513 | if (!do_responder(&req, &cbio, acbio)) |
0f113f3e | 514 | goto end; |
2234212c | 515 | if (req == NULL) { |
0f113f3e MC |
516 | resp = |
517 | OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, | |
518 | NULL); | |
519 | send_ocsp_response(cbio, resp); | |
520 | goto done_resp; | |
521 | } | |
522 | } | |
523 | ||
2234212c PY |
524 | if (req == NULL |
525 | && (signfile != NULL || reqout != NULL | |
526 | || host != NULL || add_nonce || ridx_filename != NULL)) { | |
0f113f3e MC |
527 | BIO_printf(bio_err, "Need an OCSP request for this operation!\n"); |
528 | goto end; | |
529 | } | |
530 | ||
2234212c | 531 | if (req != NULL && add_nonce) |
0f113f3e MC |
532 | OCSP_request_add1_nonce(req, NULL, -1); |
533 | ||
2234212c PY |
534 | if (signfile != NULL) { |
535 | if (keyfile == NULL) | |
0f113f3e | 536 | keyfile = signfile; |
a773b52a | 537 | signer = load_cert(signfile, FORMAT_PEM, "signer certificate"); |
2234212c | 538 | if (signer == NULL) { |
0f113f3e MC |
539 | BIO_printf(bio_err, "Error loading signer certificate\n"); |
540 | goto end; | |
541 | } | |
2234212c | 542 | if (sign_certfile != NULL) { |
a773b52a | 543 | if (!load_certs(sign_certfile, &sign_other, FORMAT_PEM, NULL, |
0996dc54 | 544 | "signer certificates")) |
0f113f3e MC |
545 | goto end; |
546 | } | |
7e1b7485 | 547 | key = load_key(keyfile, FORMAT_PEM, 0, NULL, NULL, |
0f113f3e | 548 | "signer private key"); |
2234212c | 549 | if (key == NULL) |
0f113f3e MC |
550 | goto end; |
551 | ||
552 | if (!OCSP_request_sign | |
553 | (req, signer, key, NULL, sign_other, sign_flags)) { | |
554 | BIO_printf(bio_err, "Error signing OCSP request\n"); | |
555 | goto end; | |
556 | } | |
557 | } | |
558 | ||
2234212c | 559 | if (req_text && req != NULL) |
0f113f3e MC |
560 | OCSP_REQUEST_print(out, req, 0); |
561 | ||
2234212c | 562 | if (reqout != NULL) { |
bdd58d98 | 563 | derbio = bio_open_default(reqout, 'w', FORMAT_ASN1); |
7e1b7485 | 564 | if (derbio == NULL) |
0f113f3e | 565 | goto end; |
0f113f3e MC |
566 | i2d_OCSP_REQUEST_bio(derbio, req); |
567 | BIO_free(derbio); | |
568 | } | |
569 | ||
2234212c PY |
570 | if (ridx_filename != NULL |
571 | && (rkey == NULL || rsigner == NULL || rca_cert == NULL)) { | |
0f113f3e MC |
572 | BIO_printf(bio_err, |
573 | "Need a responder certificate, key and CA for this operation!\n"); | |
574 | goto end; | |
575 | } | |
576 | ||
2234212c | 577 | if (ridx_filename != NULL && rdb == NULL) { |
0f113f3e | 578 | rdb = load_index(ridx_filename, NULL); |
2234212c | 579 | if (rdb == NULL) |
0f113f3e MC |
580 | goto end; |
581 | if (!index_index(rdb)) | |
582 | goto end; | |
583 | } | |
584 | ||
2234212c | 585 | if (rdb != NULL) { |
7e1b7485 | 586 | make_ocsp_response(&resp, req, rdb, rca_cert, rsigner, rkey, |
0f113f3e | 587 | rsign_md, rother, rflags, nmin, ndays, badsig); |
2234212c | 588 | if (cbio != NULL) |
0f113f3e | 589 | send_ocsp_response(cbio, resp); |
2234212c | 590 | } else if (host != NULL) { |
0f113f3e | 591 | # ifndef OPENSSL_NO_SOCK |
7e1b7485 | 592 | resp = process_responder(req, host, path, |
0f113f3e | 593 | port, use_ssl, headers, req_timeout); |
2234212c | 594 | if (resp == NULL) |
0f113f3e MC |
595 | goto end; |
596 | # else | |
597 | BIO_printf(bio_err, | |
598 | "Error creating connect BIO - sockets not supported.\n"); | |
599 | goto end; | |
600 | # endif | |
2234212c | 601 | } else if (respin != NULL) { |
bdd58d98 | 602 | derbio = bio_open_default(respin, 'r', FORMAT_ASN1); |
7e1b7485 | 603 | if (derbio == NULL) |
0f113f3e | 604 | goto end; |
0f113f3e MC |
605 | resp = d2i_OCSP_RESPONSE_bio(derbio, NULL); |
606 | BIO_free(derbio); | |
2234212c | 607 | if (resp == NULL) { |
0f113f3e MC |
608 | BIO_printf(bio_err, "Error reading OCSP response\n"); |
609 | goto end; | |
610 | } | |
0f113f3e MC |
611 | } else { |
612 | ret = 0; | |
613 | goto end; | |
614 | } | |
615 | ||
616 | done_resp: | |
617 | ||
2234212c | 618 | if (respout != NULL) { |
bdd58d98 | 619 | derbio = bio_open_default(respout, 'w', FORMAT_ASN1); |
7e1b7485 | 620 | if (derbio == NULL) |
0f113f3e | 621 | goto end; |
0f113f3e MC |
622 | i2d_OCSP_RESPONSE_bio(derbio, resp); |
623 | BIO_free(derbio); | |
624 | } | |
625 | ||
626 | i = OCSP_response_status(resp); | |
0f113f3e MC |
627 | if (i != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
628 | BIO_printf(out, "Responder Error: %s (%d)\n", | |
629 | OCSP_response_status_str(i), i); | |
630 | if (ignore_err) | |
631 | goto redo_accept; | |
632 | ret = 0; | |
633 | goto end; | |
634 | } | |
635 | ||
636 | if (resp_text) | |
637 | OCSP_RESPONSE_print(out, resp, 0); | |
638 | ||
639 | /* If running as responder don't verify our own response */ | |
2234212c | 640 | if (cbio != NULL) { |
e46bcca2 AE |
641 | /* If not unlimited, see if we took all we should. */ |
642 | if (accept_count != -1 && --accept_count <= 0) { | |
7e1b7485 RS |
643 | ret = 0; |
644 | goto end; | |
0f113f3e | 645 | } |
7e1b7485 RS |
646 | BIO_free_all(cbio); |
647 | cbio = NULL; | |
648 | OCSP_REQUEST_free(req); | |
649 | req = NULL; | |
650 | OCSP_RESPONSE_free(resp); | |
651 | resp = NULL; | |
652 | goto redo_accept; | |
653 | } | |
2234212c | 654 | if (ridx_filename != NULL) { |
0f113f3e MC |
655 | ret = 0; |
656 | goto end; | |
657 | } | |
658 | ||
2234212c | 659 | if (store == NULL) { |
2b6bcb70 | 660 | store = setup_verify(CAfile, CApath, noCAfile, noCApath); |
7e1b7485 RS |
661 | if (!store) |
662 | goto end; | |
663 | } | |
664 | if (vpmtouched) | |
0f113f3e | 665 | X509_STORE_set1_param(store, vpm); |
2234212c | 666 | if (verify_certfile != NULL) { |
a773b52a | 667 | if (!load_certs(verify_certfile, &verify_other, FORMAT_PEM, NULL, |
0996dc54 | 668 | "validator certificate")) |
0f113f3e MC |
669 | goto end; |
670 | } | |
671 | ||
672 | bs = OCSP_response_get1_basic(resp); | |
2234212c | 673 | if (bs == NULL) { |
0f113f3e MC |
674 | BIO_printf(bio_err, "Error parsing response\n"); |
675 | goto end; | |
676 | } | |
677 | ||
678 | ret = 0; | |
679 | ||
680 | if (!noverify) { | |
2234212c | 681 | if (req != NULL && ((i = OCSP_check_nonce(req, bs)) <= 0)) { |
0f113f3e MC |
682 | if (i == -1) |
683 | BIO_printf(bio_err, "WARNING: no nonce in response\n"); | |
684 | else { | |
685 | BIO_printf(bio_err, "Nonce Verify error\n"); | |
686 | ret = 1; | |
687 | goto end; | |
688 | } | |
689 | } | |
690 | ||
691 | i = OCSP_basic_verify(bs, verify_other, store, verify_flags); | |
c6724060 RS |
692 | if (i <= 0 && issuers) { |
693 | i = OCSP_basic_verify(bs, issuers, store, OCSP_TRUSTOTHER); | |
694 | if (i > 0) | |
695 | ERR_clear_error(); | |
696 | } | |
0f113f3e MC |
697 | if (i <= 0) { |
698 | BIO_printf(bio_err, "Response Verify Failure\n"); | |
699 | ERR_print_errors(bio_err); | |
700 | ret = 1; | |
2234212c | 701 | } else { |
0f113f3e | 702 | BIO_printf(bio_err, "Response verify OK\n"); |
2234212c | 703 | } |
0f113f3e MC |
704 | } |
705 | ||
7e1b7485 | 706 | print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage); |
0f113f3e MC |
707 | |
708 | end: | |
709 | ERR_print_errors(bio_err); | |
710 | X509_free(signer); | |
711 | X509_STORE_free(store); | |
222561fe | 712 | X509_VERIFY_PARAM_free(vpm); |
0f113f3e MC |
713 | EVP_PKEY_free(key); |
714 | EVP_PKEY_free(rkey); | |
0f113f3e | 715 | X509_free(cert); |
bb7fc98c | 716 | sk_X509_pop_free(issuers, X509_free); |
0f113f3e | 717 | X509_free(rsigner); |
3bb0f989 | 718 | sk_X509_pop_free(rca_cert, X509_free); |
0f113f3e MC |
719 | free_index(rdb); |
720 | BIO_free_all(cbio); | |
721 | BIO_free_all(acbio); | |
722 | BIO_free(out); | |
723 | OCSP_REQUEST_free(req); | |
724 | OCSP_RESPONSE_free(resp); | |
725 | OCSP_BASICRESP_free(bs); | |
726 | sk_OPENSSL_STRING_free(reqnames); | |
727 | sk_OCSP_CERTID_free(ids); | |
728 | sk_X509_pop_free(sign_other, X509_free); | |
729 | sk_X509_pop_free(verify_other, X509_free); | |
730 | sk_CONF_VALUE_pop_free(headers, X509V3_conf_free); | |
b548a1f1 RS |
731 | OPENSSL_free(thost); |
732 | OPENSSL_free(tport); | |
733 | OPENSSL_free(tpath); | |
0f113f3e | 734 | |
26a7d938 | 735 | return ret; |
5782ceb2 DSH |
736 | } |
737 | ||
0f113f3e MC |
738 | static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, |
739 | const EVP_MD *cert_id_md, X509 *issuer, | |
740 | STACK_OF(OCSP_CERTID) *ids) | |
741 | { | |
742 | OCSP_CERTID *id; | |
2234212c PY |
743 | |
744 | if (issuer == NULL) { | |
0f113f3e MC |
745 | BIO_printf(bio_err, "No issuer certificate specified\n"); |
746 | return 0; | |
747 | } | |
96487cdd | 748 | if (*req == NULL) |
0f113f3e | 749 | *req = OCSP_REQUEST_new(); |
96487cdd | 750 | if (*req == NULL) |
0f113f3e MC |
751 | goto err; |
752 | id = OCSP_cert_to_id(cert_id_md, cert, issuer); | |
2234212c | 753 | if (id == NULL || !sk_OCSP_CERTID_push(ids, id)) |
0f113f3e MC |
754 | goto err; |
755 | if (!OCSP_request_add0_id(*req, id)) | |
756 | goto err; | |
757 | return 1; | |
758 | ||
759 | err: | |
760 | BIO_printf(bio_err, "Error Creating OCSP request\n"); | |
761 | return 0; | |
762 | } | |
763 | ||
764 | static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, | |
765 | const EVP_MD *cert_id_md, X509 *issuer, | |
766 | STACK_OF(OCSP_CERTID) *ids) | |
767 | { | |
768 | OCSP_CERTID *id; | |
769 | X509_NAME *iname; | |
770 | ASN1_BIT_STRING *ikey; | |
771 | ASN1_INTEGER *sno; | |
2234212c PY |
772 | |
773 | if (issuer == NULL) { | |
0f113f3e MC |
774 | BIO_printf(bio_err, "No issuer certificate specified\n"); |
775 | return 0; | |
776 | } | |
96487cdd | 777 | if (*req == NULL) |
0f113f3e | 778 | *req = OCSP_REQUEST_new(); |
96487cdd | 779 | if (*req == NULL) |
0f113f3e MC |
780 | goto err; |
781 | iname = X509_get_subject_name(issuer); | |
782 | ikey = X509_get0_pubkey_bitstr(issuer); | |
783 | sno = s2i_ASN1_INTEGER(NULL, serial); | |
2234212c | 784 | if (sno == NULL) { |
0f113f3e MC |
785 | BIO_printf(bio_err, "Error converting serial number %s\n", serial); |
786 | return 0; | |
787 | } | |
788 | id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno); | |
789 | ASN1_INTEGER_free(sno); | |
96487cdd | 790 | if (id == NULL || !sk_OCSP_CERTID_push(ids, id)) |
0f113f3e MC |
791 | goto err; |
792 | if (!OCSP_request_add0_id(*req, id)) | |
793 | goto err; | |
794 | return 1; | |
795 | ||
796 | err: | |
797 | BIO_printf(bio_err, "Error Creating OCSP request\n"); | |
798 | return 0; | |
799 | } | |
73758d43 | 800 | |
7e1b7485 | 801 | static void print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req, |
0f113f3e MC |
802 | STACK_OF(OPENSSL_STRING) *names, |
803 | STACK_OF(OCSP_CERTID) *ids, long nsec, | |
804 | long maxage) | |
805 | { | |
806 | OCSP_CERTID *id; | |
cc696296 | 807 | const char *name; |
7e1b7485 | 808 | int i, status, reason; |
0f113f3e MC |
809 | ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd; |
810 | ||
2234212c | 811 | if (bs == NULL || req == NULL || !sk_OPENSSL_STRING_num(names) |
0f113f3e | 812 | || !sk_OCSP_CERTID_num(ids)) |
7e1b7485 | 813 | return; |
0f113f3e MC |
814 | |
815 | for (i = 0; i < sk_OCSP_CERTID_num(ids); i++) { | |
816 | id = sk_OCSP_CERTID_value(ids, i); | |
817 | name = sk_OPENSSL_STRING_value(names, i); | |
818 | BIO_printf(out, "%s: ", name); | |
819 | ||
820 | if (!OCSP_resp_find_status(bs, id, &status, &reason, | |
821 | &rev, &thisupd, &nextupd)) { | |
822 | BIO_puts(out, "ERROR: No Status found.\n"); | |
823 | continue; | |
824 | } | |
825 | ||
826 | /* | |
827 | * Check validity: if invalid write to output BIO so we know which | |
828 | * response this refers to. | |
829 | */ | |
830 | if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) { | |
831 | BIO_puts(out, "WARNING: Status times invalid.\n"); | |
832 | ERR_print_errors(out); | |
833 | } | |
834 | BIO_printf(out, "%s\n", OCSP_cert_status_str(status)); | |
835 | ||
836 | BIO_puts(out, "\tThis Update: "); | |
837 | ASN1_GENERALIZEDTIME_print(out, thisupd); | |
838 | BIO_puts(out, "\n"); | |
839 | ||
840 | if (nextupd) { | |
841 | BIO_puts(out, "\tNext Update: "); | |
842 | ASN1_GENERALIZEDTIME_print(out, nextupd); | |
843 | BIO_puts(out, "\n"); | |
844 | } | |
845 | ||
846 | if (status != V_OCSP_CERTSTATUS_REVOKED) | |
847 | continue; | |
848 | ||
849 | if (reason != -1) | |
850 | BIO_printf(out, "\tReason: %s\n", OCSP_crl_reason_str(reason)); | |
851 | ||
852 | BIO_puts(out, "\tRevocation Time: "); | |
853 | ASN1_GENERALIZEDTIME_print(out, rev); | |
854 | BIO_puts(out, "\n"); | |
855 | } | |
0f113f3e MC |
856 | } |
857 | ||
7e1b7485 | 858 | static void make_ocsp_response(OCSP_RESPONSE **resp, OCSP_REQUEST *req, |
3bb0f989 | 859 | CA_DB *db, STACK_OF(X509) *ca, X509 *rcert, |
0f113f3e MC |
860 | EVP_PKEY *rkey, const EVP_MD *rmd, |
861 | STACK_OF(X509) *rother, unsigned long flags, | |
862 | int nmin, int ndays, int badsig) | |
863 | { | |
864 | ASN1_TIME *thisupd = NULL, *nextupd = NULL; | |
3bb0f989 | 865 | OCSP_CERTID *cid; |
0f113f3e | 866 | OCSP_BASICRESP *bs = NULL; |
7e1b7485 | 867 | int i, id_count; |
0f113f3e MC |
868 | |
869 | id_count = OCSP_request_onereq_count(req); | |
870 | ||
871 | if (id_count <= 0) { | |
872 | *resp = | |
873 | OCSP_response_create(OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, NULL); | |
874 | goto end; | |
875 | } | |
876 | ||
877 | bs = OCSP_BASICRESP_new(); | |
878 | thisupd = X509_gmtime_adj(NULL, 0); | |
879 | if (ndays != -1) | |
9aa00b18 | 880 | nextupd = X509_time_adj_ex(NULL, ndays, nmin * 60, NULL); |
0f113f3e MC |
881 | |
882 | /* Examine each certificate id in the request */ | |
883 | for (i = 0; i < id_count; i++) { | |
884 | OCSP_ONEREQ *one; | |
885 | ASN1_INTEGER *serial; | |
886 | char **inf; | |
3bb0f989 TS |
887 | int jj; |
888 | int found = 0; | |
0f113f3e MC |
889 | ASN1_OBJECT *cert_id_md_oid; |
890 | const EVP_MD *cert_id_md; | |
891 | one = OCSP_request_onereq_get0(req, i); | |
892 | cid = OCSP_onereq_get0_id(one); | |
893 | ||
894 | OCSP_id_get0_info(NULL, &cert_id_md_oid, NULL, NULL, cid); | |
895 | ||
896 | cert_id_md = EVP_get_digestbyobj(cert_id_md_oid); | |
2234212c | 897 | if (cert_id_md == NULL) { |
0f113f3e MC |
898 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_INTERNALERROR, |
899 | NULL); | |
900 | goto end; | |
901 | } | |
3bb0f989 TS |
902 | for (jj = 0; jj < sk_X509_num(ca) && !found; jj++) { |
903 | X509 *ca_cert = sk_X509_value(ca, jj); | |
904 | OCSP_CERTID *ca_id = OCSP_cert_to_id(cert_id_md, NULL, ca_cert); | |
905 | ||
906 | if (OCSP_id_issuer_cmp(ca_id, cid) == 0) | |
907 | found = 1; | |
908 | ||
909 | OCSP_CERTID_free(ca_id); | |
910 | } | |
0f113f3e | 911 | |
3bb0f989 | 912 | if (!found) { |
0f113f3e MC |
913 | OCSP_basic_add1_status(bs, cid, |
914 | V_OCSP_CERTSTATUS_UNKNOWN, | |
915 | 0, NULL, thisupd, nextupd); | |
916 | continue; | |
917 | } | |
918 | OCSP_id_get0_info(NULL, NULL, NULL, &serial, cid); | |
919 | inf = lookup_serial(db, serial); | |
2234212c | 920 | if (inf == NULL) { |
0f113f3e MC |
921 | OCSP_basic_add1_status(bs, cid, |
922 | V_OCSP_CERTSTATUS_UNKNOWN, | |
923 | 0, NULL, thisupd, nextupd); | |
2234212c | 924 | } else if (inf[DB_type][0] == DB_TYPE_VAL) { |
0f113f3e MC |
925 | OCSP_basic_add1_status(bs, cid, |
926 | V_OCSP_CERTSTATUS_GOOD, | |
927 | 0, NULL, thisupd, nextupd); | |
2234212c | 928 | } else if (inf[DB_type][0] == DB_TYPE_REV) { |
0f113f3e MC |
929 | ASN1_OBJECT *inst = NULL; |
930 | ASN1_TIME *revtm = NULL; | |
931 | ASN1_GENERALIZEDTIME *invtm = NULL; | |
932 | OCSP_SINGLERESP *single; | |
933 | int reason = -1; | |
934 | unpack_revinfo(&revtm, &reason, &inst, &invtm, inf[DB_rev_date]); | |
935 | single = OCSP_basic_add1_status(bs, cid, | |
936 | V_OCSP_CERTSTATUS_REVOKED, | |
937 | reason, revtm, thisupd, nextupd); | |
2234212c | 938 | if (invtm != NULL) |
0f113f3e MC |
939 | OCSP_SINGLERESP_add1_ext_i2d(single, NID_invalidity_date, |
940 | invtm, 0, 0); | |
2234212c | 941 | else if (inst != NULL) |
0f113f3e MC |
942 | OCSP_SINGLERESP_add1_ext_i2d(single, |
943 | NID_hold_instruction_code, inst, | |
944 | 0, 0); | |
945 | ASN1_OBJECT_free(inst); | |
946 | ASN1_TIME_free(revtm); | |
947 | ASN1_GENERALIZEDTIME_free(invtm); | |
948 | } | |
949 | } | |
950 | ||
951 | OCSP_copy_nonce(bs, req); | |
952 | ||
953 | OCSP_basic_sign(bs, rcert, rkey, rmd, rother, flags); | |
954 | ||
6ef869d7 | 955 | if (badsig) { |
a0754084 DSH |
956 | const ASN1_OCTET_STRING *sig = OCSP_resp_get0_signature(bs); |
957 | corrupt_signature(sig); | |
6ef869d7 | 958 | } |
0f113f3e MC |
959 | |
960 | *resp = OCSP_response_create(OCSP_RESPONSE_STATUS_SUCCESSFUL, bs); | |
961 | ||
962 | end: | |
963 | ASN1_TIME_free(thisupd); | |
964 | ASN1_TIME_free(nextupd); | |
0f113f3e | 965 | OCSP_BASICRESP_free(bs); |
0f113f3e | 966 | } |
ee306a13 | 967 | |
f85b68cd | 968 | static char **lookup_serial(CA_DB *db, ASN1_INTEGER *ser) |
0f113f3e MC |
969 | { |
970 | int i; | |
971 | BIGNUM *bn = NULL; | |
972 | char *itmp, *row[DB_NUMBER], **rrow; | |
973 | for (i = 0; i < DB_NUMBER; i++) | |
974 | row[i] = NULL; | |
975 | bn = ASN1_INTEGER_to_BN(ser, NULL); | |
976 | OPENSSL_assert(bn); /* FIXME: should report an error at this | |
977 | * point and abort */ | |
978 | if (BN_is_zero(bn)) | |
7644a9ae | 979 | itmp = OPENSSL_strdup("00"); |
0f113f3e MC |
980 | else |
981 | itmp = BN_bn2hex(bn); | |
982 | row[DB_serial] = itmp; | |
983 | BN_free(bn); | |
984 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
985 | OPENSSL_free(itmp); | |
986 | return rrow; | |
987 | } | |
ee306a13 DSH |
988 | |
989 | /* Quick and dirty OCSP server: read in and parse input request */ | |
990 | ||
c45a48c1 | 991 | static BIO *init_responder(const char *port) |
0f113f3e | 992 | { |
366e2a60 | 993 | # ifdef OPENSSL_NO_SOCK |
0f113f3e MC |
994 | BIO_printf(bio_err, |
995 | "Error setting up accept BIO - sockets not supported.\n"); | |
366e2a60 | 996 | return NULL; |
f863ad0c MC |
997 | # else |
998 | BIO *acbio = NULL, *bufbio = NULL; | |
999 | ||
366e2a60 | 1000 | bufbio = BIO_new(BIO_f_buffer()); |
96487cdd | 1001 | if (bufbio == NULL) |
0f113f3e | 1002 | goto err; |
366e2a60 RS |
1003 | acbio = BIO_new(BIO_s_accept()); |
1004 | if (acbio == NULL | |
1005 | || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0 | |
1006 | || BIO_set_accept_port(acbio, port) < 0) { | |
1007 | BIO_printf(bio_err, "Error setting up accept BIO\n"); | |
1008 | ERR_print_errors(bio_err); | |
1009 | goto err; | |
1010 | } | |
1011 | ||
0f113f3e MC |
1012 | BIO_set_accept_bios(acbio, bufbio); |
1013 | bufbio = NULL; | |
0f113f3e | 1014 | if (BIO_do_accept(acbio) <= 0) { |
366e2a60 | 1015 | BIO_printf(bio_err, "Error starting accept\n"); |
0f113f3e MC |
1016 | ERR_print_errors(bio_err); |
1017 | goto err; | |
1018 | } | |
1019 | ||
1020 | return acbio; | |
1021 | ||
1022 | err: | |
1023 | BIO_free_all(acbio); | |
1024 | BIO_free(bufbio); | |
1025 | return NULL; | |
f863ad0c | 1026 | # endif |
0f113f3e | 1027 | } |
ee306a13 | 1028 | |
f863ad0c | 1029 | # ifndef OPENSSL_NO_SOCK |
fc3cec53 RS |
1030 | /* |
1031 | * Decode %xx URL-decoding in-place. Ignores mal-formed sequences. | |
1032 | */ | |
1033 | static int urldecode(char *p) | |
995101d6 RS |
1034 | { |
1035 | unsigned char *out = (unsigned char *)p; | |
fc3cec53 | 1036 | unsigned char *save = out; |
995101d6 RS |
1037 | |
1038 | for (; *p; p++) { | |
1039 | if (*p != '%') | |
1040 | *out++ = *p; | |
18295f0c | 1041 | else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) { |
14f051a0 RS |
1042 | /* Don't check, can't fail because of ixdigit() call. */ |
1043 | *out++ = (OPENSSL_hexchar2int(p[1]) << 4) | |
1044 | | OPENSSL_hexchar2int(p[2]); | |
995101d6 RS |
1045 | p += 2; |
1046 | } | |
fc3cec53 RS |
1047 | else |
1048 | return -1; | |
995101d6 | 1049 | } |
fc3cec53 RS |
1050 | *out = '\0'; |
1051 | return (int)(out - save); | |
995101d6 | 1052 | } |
f863ad0c | 1053 | # endif |
995101d6 | 1054 | |
a773b52a | 1055 | static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio) |
0f113f3e | 1056 | { |
f863ad0c MC |
1057 | # ifdef OPENSSL_NO_SOCK |
1058 | return 0; | |
1059 | # else | |
7e1b7485 | 1060 | int len; |
0f113f3e | 1061 | OCSP_REQUEST *req = NULL; |
fc3cec53 | 1062 | char inbuf[2048], reqbuf[2048]; |
995101d6 RS |
1063 | char *p, *q; |
1064 | BIO *cbio = NULL, *getbio = NULL, *b64 = NULL; | |
0f113f3e MC |
1065 | |
1066 | if (BIO_do_accept(acbio) <= 0) { | |
1067 | BIO_printf(bio_err, "Error accepting connection\n"); | |
1068 | ERR_print_errors(bio_err); | |
1069 | return 0; | |
1070 | } | |
1071 | ||
1072 | cbio = BIO_pop(acbio); | |
1073 | *pcbio = cbio; | |
1074 | ||
7e1b7485 | 1075 | /* Read the request line. */ |
cbe29648 | 1076 | len = BIO_gets(cbio, reqbuf, sizeof(reqbuf)); |
7e1b7485 RS |
1077 | if (len <= 0) |
1078 | return 1; | |
fc3cec53 | 1079 | if (strncmp(reqbuf, "GET ", 4) == 0) { |
995101d6 | 1080 | /* Expecting GET {sp} /URL {sp} HTTP/1.x */ |
fc3cec53 | 1081 | for (p = reqbuf + 4; *p == ' '; ++p) |
995101d6 | 1082 | continue; |
fc3cec53 RS |
1083 | if (*p != '/') { |
1084 | BIO_printf(bio_err, "Invalid request -- bad URL\n"); | |
1085 | return 1; | |
995101d6 | 1086 | } |
fc3cec53 RS |
1087 | p++; |
1088 | ||
995101d6 RS |
1089 | /* Splice off the HTTP version identifier. */ |
1090 | for (q = p; *q; q++) | |
fc3cec53 | 1091 | if (*q == ' ') |
995101d6 | 1092 | break; |
fc3cec53 | 1093 | if (strncmp(q, " HTTP/1.", 8) != 0) { |
46f4e1be | 1094 | BIO_printf(bio_err, "Invalid request -- bad HTTP version\n"); |
995101d6 RS |
1095 | return 1; |
1096 | } | |
1097 | *q = '\0'; | |
fc3cec53 RS |
1098 | len = urldecode(p); |
1099 | if (len <= 0) { | |
1100 | BIO_printf(bio_err, "Invalid request -- bad URL encoding\n"); | |
1101 | return 1; | |
1102 | } | |
1103 | if ((getbio = BIO_new_mem_buf(p, len)) == NULL | |
1104 | || (b64 = BIO_new(BIO_f_base64())) == NULL) { | |
1105 | BIO_printf(bio_err, "Could not allocate memory\n"); | |
1106 | ERR_print_errors(bio_err); | |
1107 | return 1; | |
1108 | } | |
995101d6 RS |
1109 | BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
1110 | getbio = BIO_push(b64, getbio); | |
fc3cec53 RS |
1111 | } else if (strncmp(reqbuf, "POST ", 5) != 0) { |
1112 | BIO_printf(bio_err, "Invalid request -- bad HTTP verb\n"); | |
7e1b7485 RS |
1113 | return 1; |
1114 | } | |
fc3cec53 RS |
1115 | |
1116 | /* Read and skip past the headers. */ | |
0f113f3e | 1117 | for (;;) { |
cbe29648 | 1118 | len = BIO_gets(cbio, inbuf, sizeof(inbuf)); |
0f113f3e MC |
1119 | if (len <= 0) |
1120 | return 1; | |
0f113f3e MC |
1121 | if ((inbuf[0] == '\r') || (inbuf[0] == '\n')) |
1122 | break; | |
1123 | } | |
1124 | ||
1125 | /* Try to read OCSP request */ | |
2234212c | 1126 | if (getbio != NULL) { |
995101d6 RS |
1127 | req = d2i_OCSP_REQUEST_bio(getbio, NULL); |
1128 | BIO_free_all(getbio); | |
2234212c | 1129 | } else { |
995101d6 | 1130 | req = d2i_OCSP_REQUEST_bio(cbio, NULL); |
2234212c | 1131 | } |
0f113f3e | 1132 | |
2234212c | 1133 | if (req == NULL) { |
0f113f3e MC |
1134 | BIO_printf(bio_err, "Error parsing OCSP request\n"); |
1135 | ERR_print_errors(bio_err); | |
1136 | } | |
1137 | ||
1138 | *preq = req; | |
1139 | ||
1140 | return 1; | |
f863ad0c | 1141 | # endif |
0f113f3e | 1142 | } |
ee306a13 DSH |
1143 | |
1144 | static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp) | |
0f113f3e MC |
1145 | { |
1146 | char http_resp[] = | |
1147 | "HTTP/1.0 200 OK\r\nContent-type: application/ocsp-response\r\n" | |
1148 | "Content-Length: %d\r\n\r\n"; | |
2234212c | 1149 | if (cbio == NULL) |
0f113f3e MC |
1150 | return 0; |
1151 | BIO_printf(cbio, http_resp, i2d_OCSP_RESPONSE(resp, NULL)); | |
1152 | i2d_OCSP_RESPONSE_bio(cbio, resp); | |
1153 | (void)BIO_flush(cbio); | |
1154 | return 1; | |
1155 | } | |
ee306a13 | 1156 | |
f9e55034 | 1157 | # ifndef OPENSSL_NO_SOCK |
76e0cd12 DSH |
1158 | static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host, |
1159 | const char *path, | |
0f113f3e MC |
1160 | const STACK_OF(CONF_VALUE) *headers, |
1161 | OCSP_REQUEST *req, int req_timeout) | |
1162 | { | |
1163 | int fd; | |
1164 | int rv; | |
1165 | int i; | |
76e0cd12 | 1166 | int add_host = 1; |
0f113f3e MC |
1167 | OCSP_REQ_CTX *ctx = NULL; |
1168 | OCSP_RESPONSE *rsp = NULL; | |
1169 | fd_set confds; | |
1170 | struct timeval tv; | |
1171 | ||
1172 | if (req_timeout != -1) | |
1173 | BIO_set_nbio(cbio, 1); | |
1174 | ||
1175 | rv = BIO_do_connect(cbio); | |
1176 | ||
1177 | if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(cbio))) { | |
7e1b7485 | 1178 | BIO_puts(bio_err, "Error connecting BIO\n"); |
0f113f3e MC |
1179 | return NULL; |
1180 | } | |
1181 | ||
4428c7db | 1182 | if (BIO_get_fd(cbio, &fd) < 0) { |
7e1b7485 | 1183 | BIO_puts(bio_err, "Can't get connection fd\n"); |
0f113f3e MC |
1184 | goto err; |
1185 | } | |
1186 | ||
1187 | if (req_timeout != -1 && rv <= 0) { | |
1188 | FD_ZERO(&confds); | |
1189 | openssl_fdset(fd, &confds); | |
1190 | tv.tv_usec = 0; | |
1191 | tv.tv_sec = req_timeout; | |
1192 | rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); | |
1193 | if (rv == 0) { | |
7e1b7485 | 1194 | BIO_puts(bio_err, "Timeout on connect\n"); |
0f113f3e MC |
1195 | return NULL; |
1196 | } | |
1197 | } | |
1198 | ||
1199 | ctx = OCSP_sendreq_new(cbio, path, NULL, -1); | |
96487cdd | 1200 | if (ctx == NULL) |
0f113f3e MC |
1201 | return NULL; |
1202 | ||
1203 | for (i = 0; i < sk_CONF_VALUE_num(headers); i++) { | |
1204 | CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i); | |
76e0cd12 DSH |
1205 | if (add_host == 1 && strcasecmp("host", hdr->name) == 0) |
1206 | add_host = 0; | |
0f113f3e MC |
1207 | if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value)) |
1208 | goto err; | |
1209 | } | |
1210 | ||
76e0cd12 DSH |
1211 | if (add_host == 1 && OCSP_REQ_CTX_add1_header(ctx, "Host", host) == 0) |
1212 | goto err; | |
1213 | ||
0f113f3e MC |
1214 | if (!OCSP_REQ_CTX_set1_req(ctx, req)) |
1215 | goto err; | |
1216 | ||
1217 | for (;;) { | |
1218 | rv = OCSP_sendreq_nbio(&rsp, ctx); | |
1219 | if (rv != -1) | |
1220 | break; | |
1221 | if (req_timeout == -1) | |
1222 | continue; | |
1223 | FD_ZERO(&confds); | |
1224 | openssl_fdset(fd, &confds); | |
1225 | tv.tv_usec = 0; | |
1226 | tv.tv_sec = req_timeout; | |
2234212c | 1227 | if (BIO_should_read(cbio)) { |
0f113f3e | 1228 | rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv); |
2234212c | 1229 | } else if (BIO_should_write(cbio)) { |
0f113f3e | 1230 | rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv); |
2234212c | 1231 | } else { |
7e1b7485 | 1232 | BIO_puts(bio_err, "Unexpected retry condition\n"); |
0f113f3e MC |
1233 | goto err; |
1234 | } | |
1235 | if (rv == 0) { | |
7e1b7485 | 1236 | BIO_puts(bio_err, "Timeout on request\n"); |
0f113f3e MC |
1237 | break; |
1238 | } | |
1239 | if (rv == -1) { | |
7e1b7485 | 1240 | BIO_puts(bio_err, "Select error\n"); |
0f113f3e MC |
1241 | break; |
1242 | } | |
1243 | ||
1244 | } | |
1245 | err: | |
895cba19 | 1246 | OCSP_REQ_CTX_free(ctx); |
0f113f3e MC |
1247 | |
1248 | return rsp; | |
1249 | } | |
454dbbc5 | 1250 | |
7e1b7485 | 1251 | OCSP_RESPONSE *process_responder(OCSP_REQUEST *req, |
0f113f3e MC |
1252 | const char *host, const char *path, |
1253 | const char *port, int use_ssl, | |
82c49427 | 1254 | STACK_OF(CONF_VALUE) *headers, |
0f113f3e MC |
1255 | int req_timeout) |
1256 | { | |
1257 | BIO *cbio = NULL; | |
1258 | SSL_CTX *ctx = NULL; | |
1259 | OCSP_RESPONSE *resp = NULL; | |
ff4a9394 | 1260 | |
0f113f3e | 1261 | cbio = BIO_new_connect(host); |
2234212c | 1262 | if (cbio == NULL) { |
7e1b7485 | 1263 | BIO_printf(bio_err, "Error creating connect BIO\n"); |
0f113f3e MC |
1264 | goto end; |
1265 | } | |
2234212c | 1266 | if (port != NULL) |
0f113f3e MC |
1267 | BIO_set_conn_port(cbio, port); |
1268 | if (use_ssl == 1) { | |
1269 | BIO *sbio; | |
13c9bb3e | 1270 | ctx = SSL_CTX_new(TLS_client_method()); |
0f113f3e | 1271 | if (ctx == NULL) { |
7e1b7485 | 1272 | BIO_printf(bio_err, "Error creating SSL context.\n"); |
0f113f3e MC |
1273 | goto end; |
1274 | } | |
1275 | SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY); | |
1276 | sbio = BIO_new_ssl(ctx, 1); | |
1277 | cbio = BIO_push(sbio, cbio); | |
1278 | } | |
ff4a9394 | 1279 | |
76e0cd12 | 1280 | resp = query_responder(cbio, host, path, headers, req, req_timeout); |
2234212c | 1281 | if (resp == NULL) |
0f113f3e MC |
1282 | BIO_printf(bio_err, "Error querying OCSP responder\n"); |
1283 | end: | |
ca3a82c3 | 1284 | BIO_free_all(cbio); |
62adbcee | 1285 | SSL_CTX_free(ctx); |
0f113f3e MC |
1286 | return resp; |
1287 | } | |
f9e55034 | 1288 | # endif |
67c8e7f4 | 1289 | |
85d686e7 | 1290 | #endif |