1/*
2 * Copyright 2001-2021 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
13#ifndef OPENSSL_NO_OCSP
14
15OSSL_HTTP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path,
16                                    const OCSP_REQUEST *req, int buf_size)
17{
18    OSSL_HTTP_REQ_CTX *rctx = OSSL_HTTP_REQ_CTX_new(io, io, buf_size);
19
20    if (rctx == NULL)
21        return NULL;
22    /*-
23     * by default:
24     * no bio_update_fn (and consequently no arg)
25     * no ssl
26     * no proxy
27     * no timeout (blocking indefinitely)
28     * no expected content type
29     * max_resp_len = 100 KiB
30     */
31    if (!OSSL_HTTP_REQ_CTX_set_request_line(rctx, 1 /* POST */,
32                                            NULL, NULL, path))
33        goto err;
34    /* by default, no extra headers */
35    if (!OSSL_HTTP_REQ_CTX_set_expected(rctx,
36                                        NULL /* content_type */, 1 /* asn1 */,
37                                        0 /* timeout */, 0 /* keep_alive */))
38        goto err;
39    if (req != NULL
40        && !OSSL_HTTP_REQ_CTX_set1_req(rctx, "application/ocsp-request",
41                                       ASN1_ITEM_rptr(OCSP_REQUEST),
42                                       (const ASN1_VALUE *)req))
43        goto err;
44    return rctx;
45
46 err:
47    OSSL_HTTP_REQ_CTX_free(rctx);
48    return NULL;
49}
50
51OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req)
52{
53    OCSP_RESPONSE *resp = NULL;
54    OSSL_HTTP_REQ_CTX *ctx;
55    BIO *mem;
56
57    ctx = OCSP_sendreq_new(b, path, req, 0 /* default buf_size */);
58    if (ctx == NULL)
59        return NULL;
60    mem = OSSL_HTTP_REQ_CTX_exchange(ctx);
61    /* ASN1_item_d2i_bio handles NULL bio gracefully */
62    resp = (OCSP_RESPONSE *)ASN1_item_d2i_bio(ASN1_ITEM_rptr(OCSP_RESPONSE),
63                                              mem, NULL);
64
65    OSSL_HTTP_REQ_CTX_free(ctx);
66    return resp;
67}
68#endif /* !defined(OPENSSL_NO_OCSP) */
69