1 | /* $Id: RTCRestClientApiBaseOci.cpp 74122 2018-09-06 15:10:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - C++ REST, RTCRestClientApiBase implementation, OCI specific bits.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2018 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. 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/err.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 a 'Content-Length' header.
|
---|
47 | *
|
---|
48 | * @returns IPRT status code.
|
---|
49 | * @param hHttp The HTTP client handle.
|
---|
50 | * @param pvContent
|
---|
51 | */
|
---|
52 | static int ociSignRequestEnsureDateOrXDate(RTHTTP hHttp)
|
---|
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_RTC2822_LEN];
|
---|
62 | ssize_t cch = RTTimeToRfc2822(RTTimeExplode(&Now, RTTimeNow(&NowSpec)), szDate, sizeof(szDate));
|
---|
63 | AssertRCReturn((int)cch, (int)cch);
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * RFC2822 (RFC5322) marks "GMT" as obsolete,
|
---|
67 | * but RFC7231 explicitly specifies in the grammar:
|
---|
68 | *
|
---|
69 | * GMT = %x47.4D.54 ; "GMT", case-sensitive
|
---|
70 | */
|
---|
71 | if ( cch >= 5
|
---|
72 | && (szDate[cch-5] == '-' || szDate[cch-5] == '+')
|
---|
73 | && strcmp(&szDate[cch-5+1], "0000") == 0)
|
---|
74 | {
|
---|
75 | szDate[cch-5+0] = 'G';
|
---|
76 | szDate[cch-5+1] = 'M';
|
---|
77 | szDate[cch-5+2] = 'T';
|
---|
78 | szDate[cch-5+3] = '\0';
|
---|
79 | cch -= 2;
|
---|
80 | }
|
---|
81 |
|
---|
82 | return RTHttpAddHeader(hHttp, "x-date", szDate, cch, RTHTTPADDHDR_F_BACK);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Ensures that we've got a 'x-content-sha256' header.
|
---|
88 | *
|
---|
89 | * @returns IPRT status code.
|
---|
90 | * @param hHttp The HTTP client handle.
|
---|
91 | * @param pvContent
|
---|
92 | */
|
---|
93 | static int ociSignRequestEnsureXContentSha256(RTHTTP hHttp, void const *pvContent, size_t cbContent)
|
---|
94 | {
|
---|
95 | if (RTHttpGetHeader(hHttp, RT_STR_TUPLE("x-content-sha256")))
|
---|
96 | return VINF_SUCCESS;
|
---|
97 |
|
---|
98 | #ifdef RT_STRICT
|
---|
99 | const char *pszContentLength = RTHttpGetHeader(hHttp, RT_STR_TUPLE("Content-Length"));
|
---|
100 | Assert(pszContentLength);
|
---|
101 | AssertMsg(!pszContentLength || RTStrToUInt64(pszContentLength) == cbContent, ("'%s' vs %RU64\n", pszContentLength, cbContent));
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | uint8_t abHash[RTSHA256_HASH_SIZE];
|
---|
105 | RTSha256(pvContent, cbContent, abHash);
|
---|
106 |
|
---|
107 | char szBase64[RTSHA256_DIGEST_LEN + 1]; /* (base64 should be shorter) */
|
---|
108 | int rc = RTBase64EncodeEx(abHash, sizeof(abHash), RTBASE64_FLAGS_NO_LINE_BREAKS, szBase64, sizeof(szBase64), NULL);
|
---|
109 | AssertRCReturn(rc, rc);
|
---|
110 |
|
---|
111 | return RTHttpAddHeader(hHttp, "x-content-sha256", szBase64, RTSTR_MAX, RTHTTPADDHDR_F_BACK);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Ensures that we've got a 'Content-Length' header.
|
---|
117 | *
|
---|
118 | * @returns IPRT status code.
|
---|
119 | * @param hHttp The HTTP client handle.
|
---|
120 | * @param cbContent The content length.
|
---|
121 | */
|
---|
122 | static int ociSignRequestEnsureContentLength(RTHTTP hHttp, uint64_t cbContent)
|
---|
123 | {
|
---|
124 | if (RTHttpGetHeader(hHttp, RT_STR_TUPLE("Content-Length")))
|
---|
125 | return VINF_SUCCESS;
|
---|
126 | char szValue[64];
|
---|
127 | ssize_t cchValue = RTStrFormatU64(szValue, sizeof(szValue), cbContent, 10, 0, 0, 0);
|
---|
128 | AssertRCReturn((int)cchValue, (int)cchValue);
|
---|
129 | return RTHttpAddHeader(hHttp, "Content-Length", szValue, cchValue, RTHTTPADDHDR_F_BACK);
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Ensures that we've got a host header.
|
---|
135 | *
|
---|
136 | * @returns IPRT status code.
|
---|
137 | * @param hHttp The HTTP client handle.
|
---|
138 | * @param pszUrl The URL.
|
---|
139 | */
|
---|
140 | static int ociSignRequestEnsureHost(RTHTTP hHttp, const char *pszUrl)
|
---|
141 | {
|
---|
142 | if (RTHttpGetHeader(hHttp, RT_STR_TUPLE("host")))
|
---|
143 | return VINF_SUCCESS;
|
---|
144 |
|
---|
145 | RTURIPARSED ParsedUrl;
|
---|
146 | int rc = RTUriParse(pszUrl, &ParsedUrl);
|
---|
147 | AssertRCReturn(rc, rc);
|
---|
148 |
|
---|
149 | return RTHttpAddHeader(hHttp, "host", &pszUrl[ParsedUrl.offAuthorityHost], ParsedUrl.cchAuthorityHost, RTHTTPADDHDR_F_BACK);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | int RTCRestClientApiBase::ociSignRequest(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
|
---|
154 | RTCString const &a_rStrXmitBody, uint32_t a_fFlags,
|
---|
155 | RTCRKEY a_hKey, RTCString const &a_rStrKeyId)
|
---|
156 | {
|
---|
157 | /*
|
---|
158 | * First make sure required headers are present, adding them as needed.
|
---|
159 | */
|
---|
160 | int rc = ociSignRequestEnsureHost(a_hHttp, a_rStrFullUrl.c_str());
|
---|
161 | if (RT_SUCCESS(rc))
|
---|
162 | {
|
---|
163 | if ( a_rStrXmitBody.isNotEmpty()
|
---|
164 | || a_enmHttpMethod == RTHTTPMETHOD_POST
|
---|
165 | || a_enmHttpMethod == RTHTTPMETHOD_PUT)
|
---|
166 | rc = ociSignRequestEnsureContentLength(a_hHttp, a_rStrXmitBody.length());
|
---|
167 | if ( RT_SUCCESS(rc)
|
---|
168 | && a_rStrXmitBody.isNotEmpty())
|
---|
169 | rc = ociSignRequestEnsureXContentSha256(a_hHttp, a_rStrXmitBody.c_str(), a_rStrXmitBody.length());
|
---|
170 | if (RT_SUCCESS(rc))
|
---|
171 | rc = ociSignRequestEnsureDateOrXDate(a_hHttp);
|
---|
172 | if (RT_SUCCESS(rc))
|
---|
173 | {
|
---|
174 | /*
|
---|
175 | * Do the signing.
|
---|
176 | */
|
---|
177 | rc = RTHttpSignHeaders(a_hHttp, a_enmHttpMethod, a_rStrFullUrl.c_str(), a_hKey, a_rStrKeyId.c_str(), 0 /*fFlags*/);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | RT_NOREF_PV(a_fFlags); /* We don't need to use the kDoCall_OciReqSignExcludeBody flag it turns out. */
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|