#include <string.h>
#include <stdio.h>
#include <openssl/asn1t.h>
#include <openssl/http.h>
#include <openssl/cmp.h>
#include "cmp_local.h"
#include <ctype.h>
#include <fcntl.h>
#include <stdlib.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/err.h>
static int keep_alive(int keep_alive, int body_type, BIO **bios)
{
if (keep_alive != 0 && bios == NULL
&& body_type != OSSL_CMP_PKIBODY_IR
&& body_type != OSSL_CMP_PKIBODY_CR
&& body_type != OSSL_CMP_PKIBODY_P10CR
&& body_type != OSSL_CMP_PKIBODY_KUR
&& body_type != OSSL_CMP_PKIBODY_POLLREQ)
keep_alive = 0;
return keep_alive;
}
OSSL_CMP_MSG *OSSL_CMP_MSG_http_perform(OSSL_CMP_CTX *ctx,
const OSSL_CMP_MSG *req)
{
char server_port[32] = { '\0' };
STACK_OF(CONF_VALUE) *headers = NULL;
const char content_type_pkix[] = "application/pkixcmp";
int tls_used;
const ASN1_ITEM *it = ASN1_ITEM_rptr(OSSL_CMP_MSG);
BIO *req_mem, *rsp;
BIO **bios;
OSSL_CMP_MSG *res = NULL;
if (ctx == NULL || req == NULL) {
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
return NULL;
}
if (!X509V3_add_value("Pragma", "no-cache", &headers))
return NULL;
if ((req_mem = ASN1_item_i2d_mem_bio(it, (const ASN1_VALUE *)req)) == NULL)
goto err;
bios = OSSL_CMP_CTX_get_transfer_cb_arg(ctx);
if (ctx->serverPort != 0)
BIO_snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort);
tls_used = ctx->tls_used >= 0 ? ctx->tls_used != 0
: OSSL_CMP_CTX_get_http_cb_arg(ctx) != NULL;
if (ctx->http_ctx == NULL) {
const char *path = ctx->serverPath;
if (path == NULL)
path = "";
if (*path == '/')
path++;
if (bios == NULL)
ossl_cmp_log4(DEBUG, ctx,
"connecting to CMP server via http%s://%s:%s/%s",
tls_used ? "s" : "", ctx->server, server_port, path);
else
ossl_cmp_log3(DEBUG, ctx,
"using existing connection with CMP server %s:%s and HTTP path /%s",
ctx->server, server_port, path);
}
rsp = OSSL_HTTP_transfer(&ctx->http_ctx, ctx->server, server_port,
ctx->serverPath, tls_used,
ctx->proxy, ctx->no_proxy,
bios == NULL ? NULL : bios[0] ,
bios == NULL ? NULL : bios[1] ,
ctx->http_cb, OSSL_CMP_CTX_get_http_cb_arg(ctx),
0 , headers,
content_type_pkix, req_mem,
content_type_pkix, 1 ,
OSSL_HTTP_DEFAULT_MAX_RESP_LEN,
ctx->msg_timeout,
keep_alive(ctx->keep_alive, req->body->type, bios));
BIO_free(req_mem);
res = (OSSL_CMP_MSG *)ASN1_item_d2i_bio(it, rsp, NULL);
BIO_free(rsp);
if (ctx->http_ctx == NULL)
ossl_cmp_debug(ctx, "disconnected from CMP server");
if (res != NULL)
ossl_cmp_debug(ctx, "finished reading response from CMP server");
err:
sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
return res;
}