| 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 |
{ |
| 16 |
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 |
&& body_type != OSSL_CMP_PKIBODY_IR |
| 22 |
&& body_type != OSSL_CMP_PKIBODY_CR |
| 23 |
&& body_type != OSSL_CMP_PKIBODY_P10CR |
| 24 |
&& body_type != OSSL_CMP_PKIBODY_KUR |
| 25 |
&& body_type != OSSL_CMP_PKIBODY_POLLREQ) |
| 26 |
keep_alive = 0; |
| 27 |
return keep_alive; |
| 28 |
} |
| 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 |
{ |
| 36 |
char server_port[32] = { '\0' }; |
| 37 |
STACK_OF(CONF_VALUE) *headers = NULL; |
| 38 |
const char content_type_pkix[] = "application/pkixcmp"; |
| 39 |
int tls_used; |
| 40 |
const ASN1_ITEM *it = ASN1_ITEM_rptr(OSSL_CMP_MSG); |
| 41 |
BIO *req_mem, *rsp; |
| 42 |
BIO **bios; /* optionally used as bio and rbio */ |
| 43 |
OSSL_CMP_MSG *res = NULL; |
| 44 |
|
| 45 |
if (ctx == NULL || req == NULL) { |
| 46 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 47 |
return NULL; |
| 48 |
} |
| 49 |
|
| 50 |
if (!X509V3_add_value("Pragma", "no-cache", &headers)) |
| 51 |
return NULL; |
| 52 |
if ((req_mem = ASN1_item_i2d_mem_bio(it, (const ASN1_VALUE *)req)) == NULL) |
| 53 |
goto err; |
| 54 |
|
| 55 |
bios = OSSL_CMP_CTX_get_transfer_cb_arg(ctx); |
| 56 |
if (ctx->serverPort != 0) |
| 57 |
BIO_snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort); |
| 58 |
tls_used = ctx->tls_used >= 0 ? ctx->tls_used != 0 |
| 59 |
: OSSL_CMP_CTX_get_http_cb_arg(ctx) != NULL; /* backward compat */ |
| 60 |
if (ctx->http_ctx == NULL) { /* using existing connection or yet not set up own connection */ |
| 61 |
const char *path = ctx->serverPath; |
| 62 |
|
| 63 |
if (path == NULL) |
| 64 |
path = ""; |
| 65 |
if (*path == '/') |
| 66 |
path++; |
| 67 |
if (bios == NULL) |
| 68 |
ossl_cmp_log4(DEBUG, ctx, |
| 69 |
"connecting to CMP server via http%s://%s:%s/%s", |
| 70 |
tls_used ? "s" : "", ctx->server, server_port, path); |
| 71 |
else |
| 72 |
ossl_cmp_log3(DEBUG, ctx, |
| 73 |
"using existing connection with CMP server %s:%s and HTTP path /%s", |
| 74 |
ctx->server, server_port, path); |
| 75 |
} |
| 76 |
|
| 77 |
rsp = OSSL_HTTP_transfer(&ctx->http_ctx, ctx->server, server_port, |
| 78 |
ctx->serverPath, tls_used, |
| 79 |
ctx->proxy, ctx->no_proxy, |
| 80 |
bios == NULL ? NULL : bios[0] /* bio */, |
| 81 |
bios == NULL ? NULL : bios[1] /* rbio */, |
| 82 |
ctx->http_cb, OSSL_CMP_CTX_get_http_cb_arg(ctx), |
| 83 |
0 /* buf_size */, headers, |
| 84 |
content_type_pkix, req_mem, |
| 85 |
content_type_pkix, 1 /* expect_asn1 */, |
| 86 |
OSSL_HTTP_DEFAULT_MAX_RESP_LEN, |
| 87 |
ctx->msg_timeout, |
| 88 |
keep_alive(ctx->keep_alive, req->body->type, bios)); |
| 89 |
BIO_free(req_mem); |
| 90 |
res = (OSSL_CMP_MSG *)ASN1_item_d2i_bio(it, rsp, NULL); |
| 91 |
BIO_free(rsp); |
| 92 |
|
| 93 |
if (ctx->http_ctx == NULL) |
| 94 |
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 |
if (res != NULL) |
| 103 |
ossl_cmp_debug(ctx, "finished reading response from CMP server"); |
| 104 |
err: |
| 105 |
sk_CONF_VALUE_pop_free(headers, X509V3_conf_free); |
| 106 |
return res; |
| 107 |
} |