Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/ocsp/ocsp_http.c

raw
1 // Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Modifications Copyright Amazon.com, Inc. or its affiliates.
5 // SPDX-License-Identifier: Apache-2.0 OR ISC
6
7 #include <ctype.h>
8 #include <string.h>
9 #include "openssl/mem.h"
10
11 #include "../internal.h"
12 #include "internal.h"
13
14 #define OCSP_MAX_LINE_LEN 4096;
15
16 // OCSP_REQ_CTX states
17
18 // If set no reading should be performed
19 #define OHS_NOREAD 0x1000
20 // Error condition
21 #define OHS_ERROR (0 | OHS_NOREAD)
22 // First line being read
23 #define OHS_FIRSTLINE 1
24 // MIME headers being read
25 #define OHS_HEADERS 2
26 // OCSP initial header (tag + length) being read
27 #define OHS_ASN1_HEADER 3
28 // OCSP content octets being read
29 #define OHS_ASN1_CONTENT 4
30 // First call: ready to start I/O
31 #define OHS_ASN1_WRITE_INIT (5 | OHS_NOREAD)
32 // Request being sent
33 #define OHS_ASN1_WRITE (6 | OHS_NOREAD)
34 // Request being flushed
35 #define OHS_ASN1_FLUSH (7 | OHS_NOREAD)
36 // Completed
37 #define OHS_DONE (8 | OHS_NOREAD)
38 // Headers set, no final \r\n included
39 #define OHS_HTTP_HEADER (9 | OHS_NOREAD)
40
41 static int check_protocol(char *line) {
42 if (strlen(line) >= 4 && strncmp(line, "HTTP", 4) == 0) {
43 return 1;
44 }
45 return 0;
46 }
47
48 // Parse the HTTP response. This will look like this: "HTTP/1.0 200 OK". We
49 // need to obtain the numeric code and (optional) informational message.
50 static int parse_http_line(char *line) {
51 int http_code;
52 char *code, *reason, *end;
53 if (!check_protocol(line)) {
54 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
55 return 0;
56 }
57 // Skip to first white space (passed protocol info)
58 for (code = line; *code != '\0' && !OPENSSL_isspace(*code); code++) {
59 continue;
60 }
61 if (*code == '\0') {
62 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
63 return 0;
64 }
65
66 // Skip past white space to start of response code.
67 while (*code != '\0' && OPENSSL_isspace(*code)) {
68 code++;
69 }
70 if (*code == '\0') {
71 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
72 return 0;
73 }
74
75 // Find end of response code: first whitespace after start of code.
76 for (reason = code; *reason != '\0' && !OPENSSL_isspace(*reason); reason++) {
77 continue;
78 }
79 if (*reason == '\0') {
80 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
81 return 0;
82 }
83
84 // Set end of response code and start of message.
85 *reason++ = '\0';
86 // Attempt to parse numeric code
87 http_code = (int)strtoul(code, &end, 10);
88 if (*end != '\0') {
89 return 0;
90 }
91
92 // Skip over any leading white space in message.
93 while (*reason != '\0' && OPENSSL_isspace(*reason)) {
94 reason++;
95 }
96 if (*reason != '\0') {
97 // Finally, zap any trailing white space in message (include CRLF).
98 // We know reason has a non-white space character so this is OK.
99 for (end = reason + strlen(reason) - 1; OPENSSL_isspace(*end); end--) {
100 *end = '\0';
101 }
102 }
103 if (http_code != 200) {
104 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SERVER_RESPONSE_PARSE_ERROR);
105 if (*reason == '\0') {
106 ERR_add_error_data(2, "Code=", code);
107 } else {
108 ERR_add_error_data(4, "Code=", code, ",Reason=", reason);
109 }
110 return 0;
111 }
112 return 1;
113 }
114
115 // |OCSP_REQUEST| sending:
116 // OHS_HTTP_HEADER should be initial state when first calling
117 // |OCSP_REQ_CTX_nbio|. OHS_HTTP_HEADER represents the state where OCSP request
118 // headers have finished being written, and we want to add the final "\r\n" to
119 // the ASN.1 OCSP request. This falls through to OHS_ASN1_WRITE_INIT and
120 // OHS_ASN1_WRITE, where we start writing the OCSP request from |rctx->mem| to
121 // |rctx->io|. |OHS_ASN1_WRITE| will continue writing the ASN.1 contents until
122 // all |OCSP_REQUEST| ASN.1 contents have been written. When OHS_ASN1_WRITE
123 // finishes writing, we will reset the BIO contents of |rctx->mem| and set the
124 // state to OHS_ASN1_FLUSH. OHS_ASN1_FLUSH will flush any buffered output that
125 // had been in |rctx->io|.
126 //
127 // |OCSP_RESPONSE| awaiting:
128 // Once OHS_ASN1_FLUSH has finished, we'll turn into a state of OHS_FIRSTLINE,
129 // and start expecting to read the first line of the HTTP OCSP response written
130 // back by the OCSP responder in |rctx->mem|. OHS_FIRSTLINE expects to parse the
131 // first line of the HTTP response, which contains the numeric code and
132 // (optional) informational message. The numeric code is parsed and verified
133 // with |parse_http_line|. Once the numeric code is parsed, OHS_FIRSTLINE will
134 // transtion to OHS_HEADERS. OHS_HEADERS parses any additional subsequent HTTP
135 // content headers in the OCSP HTTP response. Once a blank line is detected, we
136 // fallthrough to the state OHS_ASN1_HEADER and start expecting the ASN.1
137 // contents of the OCSP response. OHS_ASN1_HEADER first checks the ASN1 header
138 // contents, which should contain the length field. This then falls through to
139 // |OHS_ASN1_CONTENT| where we start reading in the actual contents of the
140 // ASN.1 OCSP response. Once all ASN.1 contents up to the length field have been
141 // read, |OCSP_REQ_CTX_nbio| will finish in the state of OHS_DONE.
142 // |OCSP_REQ_CTX_nbio| will not return 1 until we reach OHS_DONE.
143 int OCSP_REQ_CTX_nbio(OCSP_REQ_CTX *rctx) {
144 int ret, tmp_data_len;
145 size_t data_len;
146 const unsigned char *data;
147 next_io:
148 if (!(rctx->state & OHS_NOREAD)) {
149 tmp_data_len = BIO_read(rctx->io, rctx->iobuf, rctx->iobuflen);
150 if (tmp_data_len <= 0) {
151 if (BIO_should_retry(rctx->io)) {
152 return -1;
153 }
154 return 0;
155 }
156
157 // Write data to memory BIO.
158 if (BIO_write(rctx->mem, rctx->iobuf, tmp_data_len) != tmp_data_len) {
159 return 0;
160 }
161 data_len = (size_t)tmp_data_len;
162 }
163
164 switch (rctx->state) {
165 case OHS_HTTP_HEADER:
166 // Last operation was adding headers: need a final "\r\n".
167 if (BIO_write(rctx->mem, "\r\n", 2) != 2) {
168 rctx->state = OHS_ERROR;
169 return 0;
170 }
171 rctx->state = OHS_ASN1_WRITE_INIT;
172
173 OPENSSL_FALLTHROUGH;
174 case OHS_ASN1_WRITE_INIT:
175 if(!BIO_mem_contents(rctx->mem, NULL, &data_len)) {
176 rctx->state = OHS_ERROR;
177 return 0;
178 }
179 rctx->asn1_len = data_len;
180 rctx->state = OHS_ASN1_WRITE;
181
182 OPENSSL_FALLTHROUGH;
183 case OHS_ASN1_WRITE:
184 if(!BIO_mem_contents(rctx->mem, &data, &data_len)) {
185 rctx->state = OHS_ERROR;
186 return 0;
187 }
188
189 int write_len = BIO_write(rctx->io, data + (data_len - rctx->asn1_len),
190 (int)rctx->asn1_len);
191 if (write_len <= 0) {
192 if (BIO_should_retry(rctx->io)) {
193 return -1;
194 }
195 rctx->state = OHS_ERROR;
196 return 0;
197 }
198
199 rctx->asn1_len -= write_len;
200 if (rctx->asn1_len > 0) {
201 goto next_io;
202 }
203 rctx->state = OHS_ASN1_FLUSH;
204 if(!BIO_reset(rctx->mem)) {
205 return 0;
206 }
207
208 OPENSSL_FALLTHROUGH;
209 case OHS_ASN1_FLUSH:
210 ret = BIO_flush(rctx->io);
211 if (ret > 0) {
212 rctx->state = OHS_FIRSTLINE;
213 goto next_io;
214 }
215
216 if (BIO_should_retry(rctx->io)) {
217 return -1;
218 }
219 rctx->state = OHS_ERROR;
220 return 0;
221
222 case OHS_ERROR:
223 return 0;
224
225 case OHS_FIRSTLINE:
226 case OHS_HEADERS:
227
228 // Attempt to read a line in.
229 next_line:
230 // Due to strange memory BIO behaviour with BIO_gets we have to
231 // check there's a complete line in there before calling BIO_gets
232 // or we'll just get a partial read.
233 if(!BIO_mem_contents(rctx->mem, &data, &data_len)) {
234 rctx->state = OHS_ERROR;
235 return 0;
236 }
237 if ((data_len <= 0) || !memchr(data, '\n', data_len)) {
238 if (data_len >= (size_t)rctx->iobuflen) {
239 rctx->state = OHS_ERROR;
240 return 0;
241 }
242 goto next_io;
243 }
244 tmp_data_len = BIO_gets(rctx->mem, (char *)rctx->iobuf, rctx->iobuflen);
245
246 if (tmp_data_len <= 0) {
247 if (BIO_should_retry(rctx->mem)) {
248 goto next_io;
249 }
250 rctx->state = OHS_ERROR;
251 return 0;
252 }
253
254 // Don't allow excessive lines.
255 if (tmp_data_len >= rctx->iobuflen) {
256 rctx->state = OHS_ERROR;
257 return 0;
258 }
259 data_len = (size_t)tmp_data_len;
260
261 // First line.
262 if (rctx->state == OHS_FIRSTLINE) {
263 if (parse_http_line((char *)rctx->iobuf)) {
264 rctx->state = OHS_HEADERS;
265 goto next_line;
266 } else {
267 rctx->state = OHS_ERROR;
268 return 0;
269 }
270 } else {
271 // Look for blank line: end of headers.
272 for (data = rctx->iobuf; *data; data++) {
273 if ((*data != '\r') && (*data != '\n')) {
274 break;
275 }
276 }
277 if (*data != '\0') {
278 goto next_line;
279 }
280
281 rctx->state = OHS_ASN1_HEADER;
282 }
283
284 OPENSSL_FALLTHROUGH;
285 case OHS_ASN1_HEADER:
286 // Now reading ASN1 header: can read at least 2 bytes which is
287 // enough for ASN1 SEQUENCE header and either length field or at
288 // least the length of the length field.
289 if(!BIO_mem_contents(rctx->mem, &data, &data_len)) {
290 rctx->state = OHS_ERROR;
291 return 0;
292 }
293 if (data_len < 2) {
294 goto next_io;
295 }
296
297 // Check it is an ASN1 SEQUENCE.
298 if (*data++ != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
299 rctx->state = OHS_ERROR;
300 return 0;
301 }
302
303 // Check out length field. This is checking to see if the length is
304 // encoded as long-form (multiple bytes) versus being a length that
305 // can be encoded into 7 bits. "0x80" implies "0x80 + N", where N is
306 // the number of length bytes to follow.
307 if ((*data & 0x80) != 0) {
308 // If MSB set on initial length octet we can now always read 6
309 // octets: make sure we have them.
310 if (data_len < 6) {
311 goto next_io;
312 }
313 data_len = *data & 0x7F;
314 // Not NDEF or excessive length.
315 if (!data_len || (data_len > 4)) {
316 rctx->state = OHS_ERROR;
317 return 0;
318 }
319 data++;
320 rctx->asn1_len = 0;
321 for (size_t i = 0; i < data_len; i++) {
322 rctx->asn1_len <<= 8;
323 rctx->asn1_len |= *data++;
324 }
325 if (rctx->asn1_len > rctx->max_resp_len) {
326 rctx->state = OHS_ERROR;
327 return 0;
328 }
329 rctx->asn1_len += data_len + 2;
330 } else {
331 rctx->asn1_len = *data + 2;
332 }
333 rctx->state = OHS_ASN1_CONTENT;
334
335 OPENSSL_FALLTHROUGH;
336 case OHS_ASN1_CONTENT:
337 if(!BIO_mem_contents(rctx->mem, NULL, &data_len)) {
338 rctx->state = OHS_ERROR;
339 return 0;
340 }
341 if (data_len < rctx->asn1_len) {
342 goto next_io;
343 }
344 rctx->state = OHS_DONE;
345 return 1;
346
347 case OHS_DONE:
348 return 1;
349 }
350
351 return 0;
352 }
353
354
355 int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx) {
356 return OCSP_REQ_CTX_nbio_d2i(rctx, (ASN1_VALUE **)presp,
357 ASN1_ITEM_rptr(OCSP_RESPONSE));
358 }
359
360 OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req) {
361 OCSP_RESPONSE *resp = NULL;
362 OCSP_REQ_CTX *ctx;
363 int rv;
364
365 ctx = OCSP_sendreq_new(b, path, req, -1);
366 if (ctx == NULL) {
367 return NULL;
368 }
369
370 // This waits indefinitely on a response, if |BIO_should_retry| is on and
371 // the BIO persists.
372 do {
373 rv = OCSP_sendreq_nbio(&resp, ctx);
374 } while ((rv == -1) && BIO_should_retry(b));
375
376 OCSP_REQ_CTX_free(ctx);
377 if (rv) {
378 return resp;
379 }
380 return NULL;
381 }
382
383 int OCSP_REQ_CTX_nbio_d2i(OCSP_REQ_CTX *rctx, ASN1_VALUE **pval,
384 const ASN1_ITEM *it) {
385 int rv;
386 size_t len;
387 const unsigned char *p;
388
389 rv = OCSP_REQ_CTX_nbio(rctx);
390 if (rv != 1) {
391 return rv;
392 }
393
394 if (!BIO_mem_contents(rctx->mem, &p, &len)) {
395 goto err;
396 }
397 *pval = ASN1_item_d2i(NULL, &p, (long)len, it);
398 if (*pval == NULL) {
399 goto err;
400 }
401 return 1;
402
403 err:
404 rctx->state = OHS_ERROR;
405 return 0;
406 }
407
408 OCSP_REQ_CTX *OCSP_REQ_CTX_new(BIO *io, int maxline) {
409 OCSP_REQ_CTX *rctx = OPENSSL_malloc(sizeof(OCSP_REQ_CTX));
410
411 if (rctx == NULL) {
412 return NULL;
413 }
414 rctx->state = OHS_ERROR;
415 rctx->max_resp_len = OCSP_MAX_RESP_LENGTH;
416 rctx->mem = BIO_new(BIO_s_mem());
417 rctx->io = io;
418 if (maxline > 0) {
419 rctx->iobuflen = maxline;
420 } else {
421 rctx->iobuflen = OCSP_MAX_LINE_LEN;
422 }
423 rctx->iobuf = OPENSSL_malloc(rctx->iobuflen);
424 if (rctx->iobuf == NULL || rctx->mem == NULL) {
425 OCSP_REQ_CTX_free(rctx);
426 return NULL;
427 }
428 return rctx;
429 }
430
431 void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx) {
432 if (rctx == NULL) {
433 return;
434 }
435 BIO_free(rctx->mem);
436 OPENSSL_free(rctx->iobuf);
437 OPENSSL_free(rctx);
438 }
439
440 OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path, OCSP_REQUEST *req,
441 int maxline) {
442 OCSP_REQ_CTX *rctx = NULL;
443 rctx = OCSP_REQ_CTX_new(io, maxline);
444 if (rctx == NULL) {
445 return NULL;
446 }
447
448 if (!OCSP_REQ_CTX_http(rctx, "POST", path)) {
449 goto err;
450 }
451 if (req != NULL && !OCSP_REQ_CTX_set1_req(rctx, req)) {
452 goto err;
453 }
454 return rctx;
455
456 err:
457 OCSP_REQ_CTX_free(rctx);
458 return NULL;
459 }
460
461 int OCSP_REQ_CTX_http(OCSP_REQ_CTX *rctx, const char *op, const char *path) {
462 static const char http_hdr[] = "%s %s HTTP/1.0\r\n";
463
464 // Set to a default path, if path is NULL.
465 if (path == NULL) {
466 path = "/";
467 }
468
469 if (BIO_printf(rctx->mem, http_hdr, op, path) <= 0) {
470 return 0;
471 }
472 rctx->state = OHS_HTTP_HEADER;
473 return 1;
474 }
475
476 int OCSP_REQ_CTX_set1_req(OCSP_REQ_CTX *rctx, OCSP_REQUEST *req) {
477 return OCSP_REQ_CTX_i2d(rctx, ASN1_ITEM_rptr(OCSP_REQUEST),
478 (ASN1_VALUE *)req);
479 }
480
481 int OCSP_REQ_CTX_add1_header(OCSP_REQ_CTX *rctx, const char *name,
482 const char *value) {
483 if (name == NULL) {
484 return 0;
485 }
486 // The following being written conforms to the message-header field
487 // specification in https://www.rfc-editor.org/rfc/rfc2616#section-4.2.
488 // message-header = field-name ":" [ field-value ]
489 if (BIO_puts(rctx->mem, name) <= 0) {
490 return 0;
491 }
492 if (value != NULL) {
493 if (BIO_write(rctx->mem, ": ", 2) != 2) {
494 return 0;
495 }
496 if (BIO_puts(rctx->mem, value) <= 0) {
497 return 0;
498 }
499 }
500 if (BIO_write(rctx->mem, "\r\n", 2) != 2) {
501 return 0;
502 }
503 rctx->state = OHS_HTTP_HEADER;
504 return 1;
505 }
506
507 int OCSP_REQ_CTX_i2d(OCSP_REQ_CTX *rctx, const ASN1_ITEM *it, ASN1_VALUE *val) {
508 static const char req_hdr[] =
509 "Content-Type: application/ocsp-request\r\n"
510 "Content-Length: %d\r\n\r\n";
511 int reqlen = ASN1_item_i2d(val, NULL, it);
512 if (BIO_printf(rctx->mem, req_hdr, reqlen) <= 0) {
513 return 0;
514 }
515 if (ASN1_item_i2d_bio(it, rctx->mem, val) <= 0) {
516 return 0;
517 }
518 rctx->state = OHS_ASN1_WRITE_INIT;
519 return 1;
520 }
521
522 BIO *OCSP_REQ_CTX_get0_mem_bio(OCSP_REQ_CTX *rctx) { return rctx->mem; }
523
524 void OCSP_set_max_response_length(OCSP_REQ_CTX *rctx, unsigned long len) {
525 if (len == 0) {
526 rctx->max_resp_len = OCSP_MAX_RESP_LENGTH;
527 } else {
528 rctx->max_resp_len = len;
529 }
530 }