Move more general parts of internal/cryptlib.h to new internal/common.h
[openssl.git] / crypto / cmp / cmp_http.c
1 /*
2  * Copyright 2007-2021 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 <string.h>
13 #include <stdio.h>
14
15 #include <openssl/asn1t.h>
16 #include <openssl/http.h>
17
18 #include <openssl/cmp.h>
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>
27 #include <openssl/cmp.h>
28 #include <openssl/err.h>
29
30 static int keep_alive(int keep_alive, int body_type)
31 {
32     if (keep_alive != 0
33         /* Ask for persistent connection only if may need more round trips */
34             && body_type != OSSL_CMP_PKIBODY_IR
35             && body_type != OSSL_CMP_PKIBODY_CR
36             && body_type != OSSL_CMP_PKIBODY_P10CR
37             && body_type != OSSL_CMP_PKIBODY_KUR
38             && body_type != OSSL_CMP_PKIBODY_POLLREQ)
39         keep_alive = 0;
40     return keep_alive;
41 }
42
43 /*
44  * Send the PKIMessage req and on success return the response, else NULL.
45  * Any previous error queue entries will likely be removed by ERR_clear_error().
46  */
47 OSSL_CMP_MSG *OSSL_CMP_MSG_http_perform(OSSL_CMP_CTX *ctx,
48                                         const OSSL_CMP_MSG *req)
49 {
50     char server_port[32] = { '\0' };
51     STACK_OF(CONF_VALUE) *headers = NULL;
52     const char content_type_pkix[] = "application/pkixcmp";
53     int tls_used;
54     const ASN1_ITEM *it = ASN1_ITEM_rptr(OSSL_CMP_MSG);
55     BIO *req_mem, *rsp;
56     OSSL_CMP_MSG *res = NULL;
57
58     if (ctx == NULL || req == NULL) {
59         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
60         return NULL;
61     }
62
63     if (!X509V3_add_value("Pragma", "no-cache", &headers))
64         return NULL;
65     if ((req_mem = ASN1_item_i2d_mem_bio(it, (const ASN1_VALUE *)req)) == NULL)
66         goto err;
67
68     if (ctx->serverPort != 0)
69         BIO_snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort);
70     tls_used = OSSL_CMP_CTX_get_http_cb_arg(ctx) != NULL;
71     if (ctx->http_ctx == NULL)
72         ossl_cmp_log3(DEBUG, ctx, "connecting to CMP server %s:%s%s",
73                       ctx->server, server_port, tls_used ? " using TLS" : "");
74
75     rsp = OSSL_HTTP_transfer(&ctx->http_ctx, ctx->server, server_port,
76                              ctx->serverPath, tls_used,
77                              ctx->proxy, ctx->no_proxy,
78                              NULL /* bio */, NULL /* rbio */,
79                              ctx->http_cb, OSSL_CMP_CTX_get_http_cb_arg(ctx),
80                              0 /* buf_size */, headers,
81                              content_type_pkix, req_mem,
82                              content_type_pkix, 1 /* expect_asn1 */,
83                              OSSL_HTTP_DEFAULT_MAX_RESP_LEN,
84                              ctx->msg_timeout,
85                              keep_alive(ctx->keep_alive, req->body->type));
86     BIO_free(req_mem);
87     res = (OSSL_CMP_MSG *)ASN1_item_d2i_bio(it, rsp, NULL);
88     BIO_free(rsp);
89
90     if (ctx->http_ctx == NULL)
91         ossl_cmp_debug(ctx, "disconnected from CMP server");
92     /*
93      * Note that on normal successful end of the transaction the connection
94      * is not closed at this level, but this will be done by the CMP client
95      * application via OSSL_CMP_CTX_free() or OSSL_CMP_CTX_reinit().
96      */
97     if (res != NULL)
98         ossl_cmp_debug(ctx, "finished reading response from CMP server");
99  err:
100     sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
101     return res;
102 }