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