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