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