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