/src/openssl41/crypto/cmp/cmp_http.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2007-2026 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 <stdio.h> |
13 | | |
14 | | #include "cmp_local.h" |
15 | | |
16 | | static int keep_alive(int want_keep_alive, int body_type, BIO **bios) |
17 | 0 | { |
18 | 0 | if (want_keep_alive != 0 && bios == NULL |
19 | | /* |
20 | | * Ask for persistent connection only if may need more round trips. |
21 | | * Do so even with disableConfirm because polling might be needed. |
22 | | */ |
23 | 0 | && body_type != OSSL_CMP_PKIBODY_IR |
24 | 0 | && body_type != OSSL_CMP_PKIBODY_CR |
25 | 0 | && body_type != OSSL_CMP_PKIBODY_P10CR |
26 | 0 | && body_type != OSSL_CMP_PKIBODY_KUR |
27 | 0 | && body_type != OSSL_CMP_PKIBODY_POLLREQ) |
28 | 0 | want_keep_alive = 0; |
29 | 0 | return want_keep_alive; |
30 | 0 | } |
31 | | |
32 | | /* |
33 | | * Send the PKIMessage req and on success return the response, else NULL. |
34 | | */ |
35 | | OSSL_CMP_MSG *OSSL_CMP_MSG_http_perform(OSSL_CMP_CTX *ctx, |
36 | | const OSSL_CMP_MSG *req) |
37 | 0 | { |
38 | 0 | char server_port[32] = { '\0' }; |
39 | 0 | STACK_OF(CONF_VALUE) *headers = NULL; |
40 | 0 | const char content_type_pkix[] = "application/pkixcmp"; |
41 | 0 | int tls_used; |
42 | 0 | const ASN1_ITEM *it = ASN1_ITEM_rptr(OSSL_CMP_MSG); |
43 | 0 | BIO *req_mem, *rsp; |
44 | 0 | BIO **bios; /* optionally used as bio and rbio */ |
45 | 0 | OSSL_CMP_MSG *res = NULL; |
46 | |
|
47 | 0 | if (ctx == NULL || req == NULL) { |
48 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
49 | 0 | return NULL; |
50 | 0 | } |
51 | | |
52 | 0 | if (!X509V3_add_value("Pragma", "no-cache", &headers)) |
53 | 0 | return NULL; |
54 | 0 | if ((req_mem = ASN1_item_i2d_mem_bio(it, (const ASN1_VALUE *)req)) == NULL) |
55 | 0 | goto err; |
56 | | |
57 | 0 | bios = OSSL_CMP_CTX_get_transfer_cb_arg(ctx); |
58 | 0 | if (ctx->serverPort != 0) |
59 | 0 | snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort); |
60 | 0 | tls_used = ctx->tls_used >= 0 ? ctx->tls_used != 0 |
61 | 0 | : OSSL_CMP_CTX_get_http_cb_arg(ctx) != NULL; /* backward compat */ |
62 | 0 | if (ctx->http_ctx == NULL) { /* using existing connection or yet not set up own connection */ |
63 | 0 | const char *path = ctx->serverPath; |
64 | |
|
65 | 0 | if (path == NULL) |
66 | 0 | path = ""; |
67 | 0 | if (*path == '/') |
68 | 0 | path++; |
69 | 0 | if (bios == NULL) |
70 | 0 | ossl_cmp_log4(DEBUG, ctx, |
71 | 0 | "connecting to CMP server via http%s://%s:%s/%s", |
72 | 0 | tls_used ? "s" : "", ctx->server, server_port, path); |
73 | 0 | else |
74 | 0 | ossl_cmp_log3(DEBUG, ctx, |
75 | 0 | "using existing connection with CMP server %s:%s and HTTP path /%s", |
76 | 0 | ctx->server, server_port, path); |
77 | 0 | } |
78 | |
|
79 | 0 | rsp = OSSL_HTTP_transfer(&ctx->http_ctx, ctx->server, server_port, |
80 | 0 | ctx->serverPath, tls_used, |
81 | 0 | ctx->proxy, ctx->no_proxy, |
82 | 0 | bios == NULL ? NULL : bios[0] /* bio */, |
83 | 0 | bios == NULL ? NULL : bios[1] /* rbio */, |
84 | 0 | ctx->http_cb, OSSL_CMP_CTX_get_http_cb_arg(ctx), |
85 | 0 | 0 /* buf_size */, headers, |
86 | 0 | content_type_pkix, req_mem, |
87 | 0 | content_type_pkix, 1 /* expect_asn1 */, |
88 | 0 | OSSL_HTTP_DEFAULT_MAX_RESP_LEN, |
89 | 0 | ctx->msg_timeout, |
90 | 0 | keep_alive(ctx->keep_alive, req->body->type, bios)); |
91 | 0 | BIO_free(req_mem); |
92 | 0 | res = (OSSL_CMP_MSG *)ASN1_item_d2i_bio(it, rsp, NULL); |
93 | 0 | BIO_free(rsp); |
94 | |
|
95 | 0 | if (ctx->http_ctx == NULL) |
96 | 0 | ossl_cmp_debug(ctx, "disconnected from CMP server"); |
97 | | /* |
98 | | * Note that on normal successful end of the transaction the |
99 | | * HTTP connection is not closed at this level if keep_alive(...) != 0. |
100 | | * It should be closed by the CMP client application |
101 | | * using OSSL_CMP_CTX_free() or OSSL_CMP_CTX_reinit(). |
102 | | * Any pre-existing bio (== ctx->transfer_cb_arg) is not freed. |
103 | | */ |
104 | 0 | if (res != NULL) |
105 | 0 | ossl_cmp_debug(ctx, "finished reading response from CMP server"); |
106 | 0 | err: |
107 | 0 | sk_CONF_VALUE_pop_free(headers, X509V3_conf_free); |
108 | 0 | return res; |
109 | 0 | } |