Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/cmp/cmp_http.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2007-2022 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 <string.h>
13
#include <stdio.h>
14
15
#include <openssl/asn1t.h>
16
#include <openssl/http.h>
17
#include "internal/sockets.h"
18
19
#include <openssl/cmp.h>
20
#include "cmp_local.h"
21
22
/* explicit #includes not strictly needed since implied by the above: */
23
#include <ctype.h>
24
#include <fcntl.h>
25
#include <stdlib.h>
26
#include <openssl/bio.h>
27
#include <openssl/buffer.h>
28
#include <openssl/cmp.h>
29
#include <openssl/err.h>
30
31
static int keep_alive(int keep_alive, int body_type)
32
0
{
33
0
    if (keep_alive != 0
34
        /*
35
         * Ask for persistent connection only if may need more round trips.
36
         * Do so even with disableConfirm because polling might be needed.
37
         */
38
0
            && body_type != OSSL_CMP_PKIBODY_IR
39
0
            && body_type != OSSL_CMP_PKIBODY_CR
40
0
            && body_type != OSSL_CMP_PKIBODY_P10CR
41
0
            && body_type != OSSL_CMP_PKIBODY_KUR
42
0
            && body_type != OSSL_CMP_PKIBODY_POLLREQ)
43
0
        keep_alive = 0;
44
0
    return keep_alive;
45
0
}
46
47
/*
48
 * Send the PKIMessage req and on success return the response, else NULL.
49
 * Any previous error queue entries will likely be removed by ERR_clear_error().
50
 */
51
OSSL_CMP_MSG *OSSL_CMP_MSG_http_perform(OSSL_CMP_CTX *ctx,
52
                                        const OSSL_CMP_MSG *req)
53
0
{
54
0
    char server_port[32] = { '\0' };
55
0
    STACK_OF(CONF_VALUE) *headers = NULL;
56
0
    const char content_type_pkix[] = "application/pkixcmp";
57
0
    int tls_used;
58
0
    const ASN1_ITEM *it = ASN1_ITEM_rptr(OSSL_CMP_MSG);
59
0
    BIO *req_mem, *rsp;
60
0
    OSSL_CMP_MSG *res = NULL;
61
62
0
    if (ctx == NULL || req == NULL) {
63
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
64
0
        return NULL;
65
0
    }
66
67
0
    if (!X509V3_add_value("Pragma", "no-cache", &headers))
68
0
        return NULL;
69
0
    if ((req_mem = ASN1_item_i2d_mem_bio(it, (const ASN1_VALUE *)req)) == NULL)
70
0
        goto err;
71
72
0
    if (ctx->serverPort != 0)
73
0
        BIO_snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort);
74
0
    tls_used = OSSL_CMP_CTX_get_http_cb_arg(ctx) != NULL;
75
0
    if (ctx->http_ctx == NULL)
76
0
        ossl_cmp_log3(DEBUG, ctx, "connecting to CMP server %s:%s%s",
77
0
                      ctx->server, server_port, tls_used ? " using TLS" : "");
78
79
0
    rsp = OSSL_HTTP_transfer(&ctx->http_ctx, ctx->server, server_port,
80
0
                             ctx->serverPath, tls_used,
81
0
                             ctx->proxy, ctx->no_proxy,
82
0
                             NULL /* bio */, NULL /* rbio */,
83
0
                             ctx->http_cb, OSSL_CMP_CTX_get_http_cb_arg(ctx),
84
0
                             0 /* buf_size */, headers,
85
0
                             content_type_pkix, req_mem,
86
0
                             content_type_pkix, 1 /* expect_asn1 */,
87
0
                             OSSL_HTTP_DEFAULT_MAX_RESP_LEN,
88
0
                             ctx->msg_timeout,
89
0
                             keep_alive(ctx->keep_alive, req->body->type));
90
0
    BIO_free(req_mem);
91
0
    res = (OSSL_CMP_MSG *)ASN1_item_d2i_bio(it, rsp, NULL);
92
0
    BIO_free(rsp);
93
94
0
    if (ctx->http_ctx == NULL)
95
0
        ossl_cmp_debug(ctx, "disconnected from CMP server");
96
    /*
97
     * Note that on normal successful end of the transaction the connection
98
     * is not closed at this level, but this will be done by the CMP client
99
     * application via OSSL_CMP_CTX_free() or OSSL_CMP_CTX_reinit().
100
     */
101
0
    if (res != NULL)
102
0
        ossl_cmp_debug(ctx, "finished reading response from CMP server");
103
0
 err:
104
0
    sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
105
0
    return res;
106
0
}