Invoke tear_down when exiting test_encode_tls_sct() prematurely
[openssl.git] / crypto / ocsp / ocsp_http.c
1 /*
2  * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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
8  */
9
10 #include <openssl/ocsp.h>
11 #include <openssl/http.h>
12 #include "../http/http_local.h"
13
14 #ifndef OPENSSL_NO_OCSP
15
16 OSSL_HTTP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path,
17                                     const OCSP_REQUEST *req, int maxline)
18 {
19     OSSL_HTTP_REQ_CTX *rctx = NULL;
20
21     if ((rctx = OSSL_HTTP_REQ_CTX_new(io, io, 1 /* POST */,
22                                       maxline, 0 /* default max_resp_len */,
23                                       0 /* no timeout, blocking indefinitely */,
24                                       NULL, 1 /* expect_asn1 */)) == NULL)
25         return NULL;
26
27     if (!OSSL_HTTP_REQ_CTX_set_request_line(rctx, NULL, NULL, path))
28         goto err;
29
30     if (req != NULL && !OSSL_HTTP_REQ_CTX_i2d(rctx, "application/ocsp-request",
31                                               ASN1_ITEM_rptr(OCSP_REQUEST),
32                                               (ASN1_VALUE *)req))
33         goto err;
34
35     return rctx;
36
37  err:
38     OSSL_HTTP_REQ_CTX_free(rctx);
39     return NULL;
40 }
41
42 int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OSSL_HTTP_REQ_CTX *rctx)
43 {
44     *presp = (OCSP_RESPONSE *)
45         OSSL_HTTP_REQ_CTX_sendreq_d2i(rctx, ASN1_ITEM_rptr(OCSP_RESPONSE));
46     return *presp != NULL;
47 }
48
49 OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req)
50 {
51     OCSP_RESPONSE *resp = NULL;
52     OSSL_HTTP_REQ_CTX *ctx;
53     int rv;
54
55     ctx = OCSP_sendreq_new(b, path, req, -1 /* default max resp line length */);
56     if (ctx == NULL)
57         return NULL;
58
59     rv = OCSP_sendreq_nbio(&resp, ctx);
60
61     /* this indirectly calls ERR_clear_error(): */
62     OSSL_HTTP_REQ_CTX_free(ctx);
63
64     return rv == 1 ? resp : NULL;
65 }
66 #endif /* !defined(OPENSSL_NO_OCSP) */