1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <openssl/ocsp.h>
11e1051a39Sopenharmony_ci#include <openssl/http.h>
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_OCSP
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path,
16e1051a39Sopenharmony_ci                                    const OCSP_REQUEST *req, int buf_size)
17e1051a39Sopenharmony_ci{
18e1051a39Sopenharmony_ci    OSSL_HTTP_REQ_CTX *rctx = OSSL_HTTP_REQ_CTX_new(io, io, buf_size);
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ci    if (rctx == NULL)
21e1051a39Sopenharmony_ci        return NULL;
22e1051a39Sopenharmony_ci    /*-
23e1051a39Sopenharmony_ci     * by default:
24e1051a39Sopenharmony_ci     * no bio_update_fn (and consequently no arg)
25e1051a39Sopenharmony_ci     * no ssl
26e1051a39Sopenharmony_ci     * no proxy
27e1051a39Sopenharmony_ci     * no timeout (blocking indefinitely)
28e1051a39Sopenharmony_ci     * no expected content type
29e1051a39Sopenharmony_ci     * max_resp_len = 100 KiB
30e1051a39Sopenharmony_ci     */
31e1051a39Sopenharmony_ci    if (!OSSL_HTTP_REQ_CTX_set_request_line(rctx, 1 /* POST */,
32e1051a39Sopenharmony_ci                                            NULL, NULL, path))
33e1051a39Sopenharmony_ci        goto err;
34e1051a39Sopenharmony_ci    /* by default, no extra headers */
35e1051a39Sopenharmony_ci    if (!OSSL_HTTP_REQ_CTX_set_expected(rctx,
36e1051a39Sopenharmony_ci                                        NULL /* content_type */, 1 /* asn1 */,
37e1051a39Sopenharmony_ci                                        0 /* timeout */, 0 /* keep_alive */))
38e1051a39Sopenharmony_ci        goto err;
39e1051a39Sopenharmony_ci    if (req != NULL
40e1051a39Sopenharmony_ci        && !OSSL_HTTP_REQ_CTX_set1_req(rctx, "application/ocsp-request",
41e1051a39Sopenharmony_ci                                       ASN1_ITEM_rptr(OCSP_REQUEST),
42e1051a39Sopenharmony_ci                                       (const ASN1_VALUE *)req))
43e1051a39Sopenharmony_ci        goto err;
44e1051a39Sopenharmony_ci    return rctx;
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci err:
47e1051a39Sopenharmony_ci    OSSL_HTTP_REQ_CTX_free(rctx);
48e1051a39Sopenharmony_ci    return NULL;
49e1051a39Sopenharmony_ci}
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ciOCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req)
52e1051a39Sopenharmony_ci{
53e1051a39Sopenharmony_ci    OCSP_RESPONSE *resp = NULL;
54e1051a39Sopenharmony_ci    OSSL_HTTP_REQ_CTX *ctx;
55e1051a39Sopenharmony_ci    BIO *mem;
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci    ctx = OCSP_sendreq_new(b, path, req, 0 /* default buf_size */);
58e1051a39Sopenharmony_ci    if (ctx == NULL)
59e1051a39Sopenharmony_ci        return NULL;
60e1051a39Sopenharmony_ci    mem = OSSL_HTTP_REQ_CTX_exchange(ctx);
61e1051a39Sopenharmony_ci    /* ASN1_item_d2i_bio handles NULL bio gracefully */
62e1051a39Sopenharmony_ci    resp = (OCSP_RESPONSE *)ASN1_item_d2i_bio(ASN1_ITEM_rptr(OCSP_RESPONSE),
63e1051a39Sopenharmony_ci                                              mem, NULL);
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci    OSSL_HTTP_REQ_CTX_free(ctx);
66e1051a39Sopenharmony_ci    return resp;
67e1051a39Sopenharmony_ci}
68e1051a39Sopenharmony_ci#endif /* !defined(OPENSSL_NO_OCSP) */
69