Apache HTTPD
ssl_util_ocsp.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file implements an OCSP client including a toy HTTP/1.0
18 * client. Once httpd depends on a real HTTP client library, most of
19 * this can be thrown away. */
20
21#include "ssl_private.h"
22
23#ifndef OPENSSL_NO_OCSP
24
25#include "apr_buckets.h"
26#include "apr_uri.h"
27
28/* Serialize an OCSP request which will be sent to the responder at
29 * given URI to a memory BIO object, which is returned. */
31 const apr_uri_t *proxy_uri)
32{
33 BIO *bio;
34 int len;
35
37
39
40 BIO_printf(bio, "POST ");
41 /* Use full URL instead of URI in case of a request through a proxy */
42 if (proxy_uri) {
43 BIO_printf(bio, "http://%s:%d",
44 uri->hostname, uri->port);
45 }
46 BIO_printf(bio, "%s%s%s HTTP/1.0\r\n"
47 "Host: %s:%d\r\n"
48 "Content-Type: application/ocsp-request\r\n"
49 "Connection: close\r\n"
50 "Content-Length: %d\r\n"
51 "\r\n",
52 uri->path ? uri->path : "/",
53 uri->query ? "?" : "", uri->query ? uri->query : "",
54 uri->hostname, uri->port, len);
55
56 if (i2d_OCSP_REQUEST_bio(bio, req) != 1) {
58 return NULL;
59 }
60
61 return bio;
62}
63
64/* Send the OCSP request serialized into BIO 'request' to the
65 * responder at given server given by URI. Returns socket object or
66 * NULL on error. */
67static apr_socket_t *send_request(BIO *request, const apr_uri_t *uri,
70 const apr_uri_t *proxy_uri)
71{
72 apr_status_t rv;
74 apr_socket_t *sd;
75 char buf[HUGE_STRING_LEN];
76 int len;
78
79 if (proxy_uri) {
80 next_hop_uri = proxy_uri;
81 }
82 else {
84 }
85
87 next_hop_uri->port, 0, p);
88 if (rv) {
90 "could not resolve address of %s %s",
91 proxy_uri ? "proxy" : "OCSP responder",
92 next_hop_uri->hostinfo);
93 return NULL;
94 }
95
96 /* establish a connection to the OCSP responder */
98 "connecting to %s '%s'",
99 proxy_uri ? "proxy" : "OCSP responder",
100 uri->hostinfo);
101
102 /* Cycle through address until a connect() succeeds. */
103 for (; sa; sa = sa->next) {
105 if (rv == APR_SUCCESS) {
107
108 rv = apr_socket_connect(sd, sa);
109 if (rv == APR_SUCCESS) {
110 break;
111 }
113 }
114 }
115
116 if (sa == NULL) {
118 "could not connect to %s '%s'",
119 proxy_uri ? "proxy" : "OCSP responder",
120 next_hop_uri->hostinfo);
121 return NULL;
122 }
123
124 /* send the request and get a response */
126 "sending request to OCSP responder");
127
128 while ((len = BIO_read(request, buf, sizeof buf)) > 0) {
129 char *wbuf = buf;
131
132 do {
134
135 rv = apr_socket_send(sd, wbuf, &wlen);
136 wbuf += remain;
137 remain -= wlen;
138 } while (rv == APR_SUCCESS && remain > 0);
139
140 if (rv) {
143 "failed to send request to OCSP responder '%s'",
144 uri->hostinfo);
145 return NULL;
146 }
147 }
148
149 return sd;
150}
151
152/* Return a pool-allocated NUL-terminated line, with CRLF stripped,
153 * read from brigade 'bbin' using 'bbout' as temporary storage. */
156{
157 apr_status_t rv;
159 char *line;
160
162
164 if (rv) {
166 "failed reading line from OCSP server");
167 return NULL;
168 }
169
170 rv = apr_brigade_pflatten(bbout, &line, &len, p);
171 if (rv) {
173 "failed reading line from OCSP server");
174 return NULL;
175 }
176
177 if (len == 0) {
179 "empty response from OCSP server");
180 return NULL;
181 }
182
183 if (line[len-1] != APR_ASCII_LF) {
185 "response header line too long from OCSP server");
186 return NULL;
187 }
188
189 line[len-1] = '\0';
190 if (len > 1 && line[len-2] == APR_ASCII_CR) {
191 line[len-2] = '\0';
192 }
193
194 return line;
195}
196
197/* Maximum values to prevent eating RAM forever. */
198#define MAX_HEADERS (256)
199#define MAX_CONTENT (2048 * 1024)
200
201/* Read the OCSP response from the socket 'sd', using temporary memory
202 * BIO 'bio', and return the decoded OCSP response object, or NULL on
203 * error. */
205 apr_pool_t *p)
206{
207 apr_bucket_brigade *bb, *tmpbb;
208 OCSP_RESPONSE *response;
209 char *line;
212
213 /* Using brigades for response parsing is much simpler than using
214 * apr_socket_* directly. */
215 bb = apr_brigade_create(p, c->bucket_alloc);
216 tmpbb = apr_brigade_create(p, c->bucket_alloc);
217 APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_socket_create(sd, c->bucket_alloc));
218
219 line = get_line(tmpbb, bb, c, p);
220 if (!line || strncmp(line, "HTTP/", 5)
221 || (line = ap_strchr(line, ' ')) == NULL
222 || (code = apr_atoi64(++line)) < 200 || code > 299) {
224 "bad response from OCSP server: %s",
225 line ? line : "(none)");
226 return NULL;
227 }
228
229 /* Read till end of headers; don't have to even bother parsing the
230 * Content-Length since the server is obliged to close the
231 * connection after the response anyway for HTTP/1.0. */
232 count = 0;
233 while ((line = get_line(tmpbb, bb, c, p)) != NULL && line[0]
234 && ++count < MAX_HEADERS) {
236 "OCSP response header: %s", line);
237 }
238
239 if (count == MAX_HEADERS) {
241 "could not read response headers from OCSP server, "
242 "exceeded maximum count (%u)", MAX_HEADERS);
243 return NULL;
244 }
245 else if (!line) {
247 "could not read response header from OCSP server");
248 return NULL;
249 }
250
251 /* Read the response body into the memory BIO. */
252 count = 0;
253 while (!APR_BRIGADE_EMPTY(bb)) {
254 const char *data;
256 apr_status_t rv;
258
260 if (rv == APR_EOF) {
262 "OCSP response: got EOF");
263 break;
264 }
265 if (rv != APR_SUCCESS) {
267 "error reading response from OCSP server");
268 return NULL;
269 }
270 if (len == 0) {
271 /* Ignore zero-length buckets (possible side-effect of
272 * line splitting). */
274 continue;
275 }
276 count += len;
277 if (count > MAX_CONTENT) {
279 "OCSP response size exceeds %u byte limit",
281 return NULL;
282 }
284 "OCSP response: got %" APR_SIZE_T_FMT
285 " bytes, %" APR_SIZE_T_FMT " total", len, count);
286
287 BIO_write(bio, data, (int)len);
289 }
290
292 apr_brigade_destroy(tmpbb);
293
294 /* Finally decode the OCSP response from what's stored in the
295 * bio. */
296 response = d2i_OCSP_RESPONSE_bio(bio, NULL);
297 if (response == NULL) {
299 "failed to decode OCSP response data");
301 }
302
303 return response;
304}
305
308 OCSP_REQUEST *request,
310{
311 OCSP_RESPONSE *response = NULL;
312 apr_socket_t *sd;
313 BIO *bio;
314 const apr_uri_t *proxy_uri;
315
316 proxy_uri = (mySrvConfigFromConn(c))->server->proxy_uri;
317 bio = serialize_request(request, uri, proxy_uri);
318 if (bio == NULL) {
320 "could not serialize OCSP request");
322 return NULL;
323 }
324
325 sd = send_request(bio, uri, timeout, c, p, proxy_uri);
326 if (sd == NULL) {
327 /* Errors already logged. */
328 BIO_free(bio);
329 return NULL;
330 }
331
332 /* Clear the BIO contents, ready for the response. */
333 (void)BIO_reset(bio);
334
335 response = read_response(sd, bio, c, p);
336
338 BIO_free(bio);
339
340 return response;
341}
342
343/* _________________________________________________________________
344**
345** OCSP other certificate support
346** _________________________________________________________________
347*/
348
349/*
350 * Read a file that contains certificates in PEM format and
351 * return as a STACK.
352 */
353
355{
356 BIO *bio;
357 X509 *x509;
358 unsigned long err;
360
361 if ((bio = BIO_new(BIO_s_file())) == NULL)
362 return NULL;
363 if (BIO_read_filename(bio, file) <= 0) {
364 BIO_free(bio);
365 return NULL;
366 }
367
368 /* create new extra chain by loading the certs */
370 while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL)) != NULL) {
371 if (!other_certs) {
373 if (!other_certs) {
374 X509_free(x509);
375 BIO_free(bio);
376 return NULL;
377 }
378 }
379
380 if (!sk_X509_push(other_certs, x509)) {
381 X509_free(x509);
383 BIO_free(bio);
384 return NULL;
385 }
386 }
387 /* Make sure that only the error is just an EOF */
388 if ((err = ERR_peek_error()) > 0) {
389 if (!( ERR_GET_LIB(err) == ERR_LIB_PEM
391 BIO_free(bio);
393 return NULL;
394 }
395 while (ERR_get_error() > 0) ;
396 }
397 BIO_free(bio);
398 return other_certs;
399}
400
402{
403 /*
404 * Configure Trusted OCSP certificates.
405 */
406
407 if (!mctx->ocsp_certs_file) {
408 return;
409 }
410
412 "Configuring Trusted OCSP certificates");
413
414 mctx->ocsp_certs = modssl_read_ocsp_certificates(mctx->ocsp_certs_file);
415
416 if (!mctx->ocsp_certs) {
418 "Unable to configure OCSP Trusted Certificates");
420 ssl_die(s);
421 }
422 mctx->ocsp_verify_flags |= OCSP_TRUSTOTHER;
423}
424
425#endif /* HAVE_OCSP */
const char apr_size_t len
Definition ap_regex.h:187
APR-UTIL Buckets/Bucket Brigades.
APR-UTIL URI Routines.
#define HUGE_STRING_LEN
Definition httpd.h:303
#define APLOGNO(n)
Definition http_log.h:117
#define APLOG_ERR
Definition http_log.h:67
#define ap_log_error
Definition http_log.h:370
#define ap_log_cerror
Definition http_log.h:498
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_DEBUG
Definition http_log.h:71
const unsigned char * buf
Definition util_md5.h:50
#define APR_EOF
Definition apr_errno.h:461
unsigned int count
Definition apr_md5.h:152
#define APR_BRIGADE_INSERT_TAIL(b, e)
apr_bucket * e
#define APR_BRIGADE_EMPTY(b)
#define apr_bucket_delete(e)
#define APR_BRIGADE_FIRST(b)
#define apr_bucket_read(e, str, len, block)
@ APR_BLOCK_READ
Definition apr_buckets.h:58
apr_memcache_server_t * server
const char * uri
Definition apr_uri.h:159
#define APR_PROTO_TCP
apr_status_t ssl_die(server_rec *s)
#define SSLLOG_MARK
#define mySrvFromConn(c)
#define mySrvConfigFromConn(c)
OCSP_RESPONSE * modssl_dispatch_ocsp_request(const apr_uri_t *uri, apr_interval_time_t timeout, OCSP_REQUEST *request, conn_rec *c, apr_pool_t *p)
void ssl_log_ssl_error(const char *file, int line, int level, server_rec *s)
void ssl_init_ocsp_certificates(server_rec *s, modssl_ctx_t *mctx)
#define ap_strchr(s, c)
Definition httpd.h:2351
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
const char apr_file_t * file
#define APR_ASCII_CR
Definition apr_general.h:61
#define APR_ASCII_LF
Definition apr_general.h:63
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_sockaddr_t * sa
#define APR_UNSPEC
const char * s
Definition apr_strings.h:95
apr_int32_t apr_int32_t apr_int32_t err
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
Internal interfaces private to mod_ssl.
static STACK_OF(X509)
static BIO * serialize_request(OCSP_REQUEST *req, const apr_uri_t *uri, const apr_uri_t *proxy_uri)
#define MAX_CONTENT
static char * get_line(apr_bucket_brigade *bbout, apr_bucket_brigade *bbin, conn_rec *c, apr_pool_t *p)
#define MAX_HEADERS
static OCSP_RESPONSE * read_response(apr_socket_t *sd, BIO *bio, conn_rec *c, apr_pool_t *p)
static apr_socket_t * send_request(BIO *request, const apr_uri_t *uri, apr_interval_time_t timeout, conn_rec *c, apr_pool_t *p, const apr_uri_t *proxy_uri)
apr_sockaddr_t * next
apr_int32_t family
Structure to store things which are per connection.
Definition httpd.h:1152
A structure to store information for each virtual server.
Definition httpd.h:1322
apr_status_t apr_socket_send(apr_socket_t *sock, const char *buf, apr_size_t *len)
Definition sendrecv.c:30
apr_status_t apr_socket_close(apr_socket_t *thesocket)
Definition sockets.c:211
apr_status_t apr_socket_connect(apr_socket_t *sock, apr_sockaddr_t *sa)
Definition sockets.c:388
apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type, int protocol, apr_pool_t *cont)
Definition sockets.c:116
apr_status_t apr_socket_timeout_set(apr_socket_t *sock, apr_interval_time_t t)
Definition sockopt.c:75
IN ULONG IN INT timeout