Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/cmp/cmp_http.c
Line
Count
Source
1
/*
2
 * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright Nokia 2007-2019
4
 * Copyright Siemens AG 2015-2019
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include "cmp_local.h"
13
14
static int keep_alive(int keep_alive, int body_type, BIO **bios)
15
0
{
16
0
    if (keep_alive != 0 && bios == NULL
17
        /*
18
         * Ask for persistent connection only if may need more round trips.
19
         * Do so even with disableConfirm because polling might be needed.
20
         */
21
0
        && body_type != OSSL_CMP_PKIBODY_IR
22
0
        && body_type != OSSL_CMP_PKIBODY_CR
23
0
        && body_type != OSSL_CMP_PKIBODY_P10CR
24
0
        && body_type != OSSL_CMP_PKIBODY_KUR
25
0
        && body_type != OSSL_CMP_PKIBODY_POLLREQ)
26
0
        keep_alive = 0;
27
0
    return keep_alive;
28
0
}
29
30
/*
31
 * Send the PKIMessage req and on success return the response, else NULL.
32
 */
33
OSSL_CMP_MSG *OSSL_CMP_MSG_http_perform(OSSL_CMP_CTX *ctx,
34
    const OSSL_CMP_MSG *req)
35
0
{
36
0
    char server_port[32] = { '\0' };
37
0
    STACK_OF(CONF_VALUE) *headers = NULL;
38
0
    const char content_type_pkix[] = "application/pkixcmp";
39
0
    int tls_used;
40
0
    const ASN1_ITEM *it = ASN1_ITEM_rptr(OSSL_CMP_MSG);
41
0
    BIO *req_mem, *rsp;
42
0
    BIO **bios; /* optionally used as bio and rbio */
43
0
    OSSL_CMP_MSG *res = NULL;
44
45
0
    if (ctx == NULL || req == NULL) {
46
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
47
0
        return NULL;
48
0
    }
49
50
0
    if (!X509V3_add_value("Pragma", "no-cache", &headers))
51
0
        return NULL;
52
0
    if ((req_mem = ASN1_item_i2d_mem_bio(it, (const ASN1_VALUE *)req)) == NULL)
53
0
        goto err;
54
55
0
    bios = OSSL_CMP_CTX_get_transfer_cb_arg(ctx);
56
0
    if (ctx->serverPort != 0)
57
0
        BIO_snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort);
58
0
    tls_used = ctx->tls_used >= 0 ? ctx->tls_used != 0
59
0
                                  : OSSL_CMP_CTX_get_http_cb_arg(ctx) != NULL; /* backward compat */
60
0
    if (ctx->http_ctx == NULL) { /* using existing connection or yet not set up own connection */
61
0
        const char *path = ctx->serverPath;
62
63
0
        if (path == NULL)
64
0
            path = "";
65
0
        if (*path == '/')
66
0
            path++;
67
0
        if (bios == NULL)
68
0
            ossl_cmp_log4(DEBUG, ctx,
69
0
                "connecting to CMP server via http%s://%s:%s/%s",
70
0
                tls_used ? "s" : "", ctx->server, server_port, path);
71
0
        else
72
0
            ossl_cmp_log3(DEBUG, ctx,
73
0
                "using existing connection with CMP server %s:%s and HTTP path /%s",
74
0
                ctx->server, server_port, path);
75
0
    }
76
77
0
    rsp = OSSL_HTTP_transfer(&ctx->http_ctx, ctx->server, server_port,
78
0
        ctx->serverPath, tls_used,
79
0
        ctx->proxy, ctx->no_proxy,
80
0
        bios == NULL ? NULL : bios[0] /* bio */,
81
0
        bios == NULL ? NULL : bios[1] /* rbio */,
82
0
        ctx->http_cb, OSSL_CMP_CTX_get_http_cb_arg(ctx),
83
0
        0 /* buf_size */, headers,
84
0
        content_type_pkix, req_mem,
85
0
        content_type_pkix, 1 /* expect_asn1 */,
86
0
        OSSL_HTTP_DEFAULT_MAX_RESP_LEN,
87
0
        ctx->msg_timeout,
88
0
        keep_alive(ctx->keep_alive, req->body->type, bios));
89
0
    BIO_free(req_mem);
90
0
    res = (OSSL_CMP_MSG *)ASN1_item_d2i_bio(it, rsp, NULL);
91
0
    BIO_free(rsp);
92
93
0
    if (ctx->http_ctx == NULL)
94
0
        ossl_cmp_debug(ctx, "disconnected from CMP server");
95
    /*
96
     * Note that on normal successful end of the transaction the
97
     * HTTP connection is not closed at this level if keep_alive(...) != 0.
98
     * It should be closed by the CMP client application
99
     * using OSSL_CMP_CTX_free() or OSSL_CMP_CTX_reinit().
100
     * Any pre-existing bio (== ctx->transfer_cb_arg) is not freed.
101
     */
102
0
    if (res != NULL)
103
0
        ossl_cmp_debug(ctx, "finished reading response from CMP server");
104
0
err:
105
0
    sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
106
0
    return res;
107
0
}