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