VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rest/RTCRestClientApiBaseOci.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/* $Id: RTCRestClientApiBaseOci.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - C++ REST, RTCRestClientApiBase implementation, OCI specific bits.
4 */
5
6/*
7 * Copyright (C) 2018-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_REST
42#include <iprt/cpp/restclient.h>
43
44#include <iprt/assert.h>
45#include <iprt/base64.h>
46#include <iprt/errcore.h>
47#include <iprt/http.h>
48#include <iprt/log.h>
49#include <iprt/sha.h>
50#include <iprt/time.h>
51#include <iprt/uri.h>
52
53
54
55/**
56 * Ensures that we've got an 'X-Date' or 'Date' header.
57 *
58 * @returns IPRT status code.
59 * @param hHttp The HTTP client handle.
60 * @param pvContent
61 */
62static int ociSignRequestEnsureDateOrXDate(RTHTTP hHttp) RT_NOEXCEPT
63{
64 if (RTHttpGetHeader(hHttp, RT_STR_TUPLE("x-date")))
65 return VINF_SUCCESS;
66 if (RTHttpGetHeader(hHttp, RT_STR_TUPLE("date")))
67 return VINF_SUCCESS;
68
69 RTTIMESPEC NowSpec;
70 RTTIME Now;
71 char szDate[RTTIME_RFC2822_LEN];
72 ssize_t cch = RTTimeToRfc2822(RTTimeExplode(&Now, RTTimeNow(&NowSpec)), szDate, sizeof(szDate), RTTIME_RFC2822_F_GMT);
73 AssertRCReturn((int)cch, (int)cch);
74
75 return RTHttpAddHeader(hHttp, "x-date", szDate, cch, RTHTTPADDHDR_F_BACK);
76}
77
78
79/**
80 * Ensures that we've got a 'x-content-sha256' header.
81 *
82 * @returns IPRT status code.
83 * @param hHttp The HTTP client handle.
84 * @param pvContent
85 */
86static int ociSignRequestEnsureXContentSha256(RTHTTP hHttp, void const *pvContent, size_t cbContent) RT_NOEXCEPT
87{
88 if (RTHttpGetHeader(hHttp, RT_STR_TUPLE("x-content-sha256")))
89 return VINF_SUCCESS;
90
91#ifdef RT_STRICT
92 if (cbContent != 0)
93 {
94 const char *pszContentLength = RTHttpGetHeader(hHttp, RT_STR_TUPLE("Content-Length"));
95 Assert(pszContentLength);
96 AssertMsg(!pszContentLength || RTStrToUInt64(pszContentLength) == cbContent,
97 ("'%s' vs %RU64\n", pszContentLength, cbContent));
98 }
99#endif
100
101 uint8_t abHash[RTSHA256_HASH_SIZE];
102 RTSha256(pvContent, cbContent, abHash);
103
104 char szBase64[RTSHA256_DIGEST_LEN + 1]; /* (base64 should be shorter) */
105 int rc = RTBase64EncodeEx(abHash, sizeof(abHash), RTBASE64_FLAGS_NO_LINE_BREAKS, szBase64, sizeof(szBase64), NULL);
106 AssertRCReturn(rc, rc);
107
108 return RTHttpAddHeader(hHttp, "x-content-sha256", szBase64, RTSTR_MAX, RTHTTPADDHDR_F_BACK);
109}
110
111
112/**
113 * Ensures that we've got a 'Content-Length' header.
114 *
115 * @returns IPRT status code.
116 * @param hHttp The HTTP client handle.
117 * @param cbContent The content length.
118 */
119static int ociSignRequestEnsureContentLength(RTHTTP hHttp, uint64_t cbContent) RT_NOEXCEPT
120{
121 if (RTHttpGetHeader(hHttp, RT_STR_TUPLE("Content-Length")))
122 return VINF_SUCCESS;
123 char szValue[64];
124 ssize_t cchValue = RTStrFormatU64(szValue, sizeof(szValue), cbContent, 10, 0, 0, 0);
125 AssertRCReturn((int)cchValue, (int)cchValue);
126 return RTHttpAddHeader(hHttp, "Content-Length", szValue, cchValue, RTHTTPADDHDR_F_BACK);
127}
128
129
130/**
131 * Ensures that we've got a host header.
132 *
133 * @returns IPRT status code.
134 * @param hHttp The HTTP client handle.
135 * @param pszUrl The URL.
136 */
137static int ociSignRequestEnsureHost(RTHTTP hHttp, const char *pszUrl) RT_NOEXCEPT
138{
139 if (RTHttpGetHeader(hHttp, RT_STR_TUPLE("host")))
140 return VINF_SUCCESS;
141
142 RTURIPARSED ParsedUrl;
143 int rc = RTUriParse(pszUrl, &ParsedUrl);
144 AssertRCReturn(rc, rc);
145
146 return RTHttpAddHeader(hHttp, "host", &pszUrl[ParsedUrl.offAuthorityHost], ParsedUrl.cchAuthorityHost, RTHTTPADDHDR_F_BACK);
147}
148
149
150int RTCRestClientApiBase::ociSignRequest(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
151 RTCString const &a_rStrXmitBody, uint32_t a_fFlags,
152 RTCRKEY a_hKey, RTCString const &a_rStrKeyId) RT_NOEXCEPT
153{
154 /*
155 * First make sure required headers are present, adding them as needed.
156 */
157 int rc = ociSignRequestEnsureHost(a_hHttp, a_rStrFullUrl.c_str());
158 if (RT_SUCCESS(rc))
159 {
160 bool fHasBody
161 = a_rStrXmitBody.isNotEmpty()
162 /* but sometimes we need an empty body signed too */
163 || (a_fFlags & kDoCall_RequireBody)
164 || a_enmHttpMethod == RTHTTPMETHOD_POST
165 || a_enmHttpMethod == RTHTTPMETHOD_PUT;
166
167 if (fHasBody)
168 {
169 rc = ociSignRequestEnsureContentLength(a_hHttp, a_rStrXmitBody.length());
170 if (RT_SUCCESS(rc))
171 rc = ociSignRequestEnsureXContentSha256(a_hHttp, a_rStrXmitBody.c_str(), a_rStrXmitBody.length());
172 }
173 if (RT_SUCCESS(rc))
174 rc = ociSignRequestEnsureDateOrXDate(a_hHttp);
175 if (RT_SUCCESS(rc))
176 {
177 /*
178 * Do the signing.
179 */
180 rc = RTHttpSignHeaders(a_hHttp, a_enmHttpMethod, a_rStrFullUrl.c_str(), a_hKey, a_rStrKeyId.c_str(), 0 /*fFlags*/);
181 }
182 }
183 return rc;
184}
185
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use