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