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