2 * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
15 #include <openssl/asn1t.h>
16 #include <openssl/http.h>
17 #include "internal/sockets.h"
19 #include <openssl/cmp.h>
20 #include "cmp_local.h"
22 /* explicit #includes not strictly needed since implied by the above: */
26 #include <openssl/bio.h>
27 #include <openssl/buffer.h>
28 #include <openssl/cmp.h>
29 #include <openssl/err.h>
31 static int keep_alive(int keep_alive, int body_type)
34 /* Ask for persistent connection only if may need more round trips */
35 && body_type != OSSL_CMP_PKIBODY_IR
36 && body_type != OSSL_CMP_PKIBODY_CR
37 && body_type != OSSL_CMP_PKIBODY_P10CR
38 && body_type != OSSL_CMP_PKIBODY_KUR
39 && body_type != OSSL_CMP_PKIBODY_POLLREQ)
45 * Send the PKIMessage req and on success return the response, else NULL.
46 * Any previous error queue entries will likely be removed by ERR_clear_error().
48 OSSL_CMP_MSG *OSSL_CMP_MSG_http_perform(OSSL_CMP_CTX *ctx,
49 const OSSL_CMP_MSG *req)
51 char server_port[32] = { '\0' };
52 STACK_OF(CONF_VALUE) *headers = NULL;
53 const char content_type_pkix[] = "application/pkixcmp";
55 const ASN1_ITEM *it = ASN1_ITEM_rptr(OSSL_CMP_MSG);
57 OSSL_CMP_MSG *res = NULL;
59 if (ctx == NULL || req == NULL) {
60 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
64 if (!X509V3_add_value("Pragma", "no-cache", &headers))
66 if ((req_mem = ASN1_item_i2d_mem_bio(it, (const ASN1_VALUE *)req)) == NULL)
69 if (ctx->serverPort != 0)
70 BIO_snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort);
71 tls_used = OSSL_CMP_CTX_get_http_cb_arg(ctx) != NULL;
72 if (ctx->http_ctx == NULL)
73 ossl_cmp_log3(DEBUG, ctx, "connecting to CMP server %s:%s%s",
74 ctx->server, server_port, tls_used ? " using TLS" : "");
76 rsp = OSSL_HTTP_transfer(&ctx->http_ctx, ctx->server, server_port,
77 ctx->serverPath, tls_used,
78 ctx->proxy, ctx->no_proxy,
79 NULL /* bio */, NULL /* rbio */,
80 ctx->http_cb, OSSL_CMP_CTX_get_http_cb_arg(ctx),
81 0 /* buf_size */, headers,
82 content_type_pkix, req_mem,
83 content_type_pkix, 1 /* expect_asn1 */,
84 OSSL_HTTP_DEFAULT_MAX_RESP_LEN,
86 keep_alive(ctx->keep_alive, req->body->type));
88 res = (OSSL_CMP_MSG *)ASN1_item_d2i_bio(it, rsp, NULL);
91 if (ctx->http_ctx == NULL)
92 ossl_cmp_debug(ctx, "disconnected from CMP server");
94 * Note that on normal successful end of the transaction the connection
95 * is not closed at this level, but this will be done by the CMP client
96 * application via OSSL_CMP_CTX_free() or OSSL_CMP_CTX_reinit().
99 ossl_cmp_debug(ctx, "finished reading response from CMP server");
101 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);