VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTHttp-1.cpp@ 83743

Last change on this file since 83743 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1/* $Id: tstRTHttp-1.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Testcase for the RTHttp API.
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#include <iprt/http.h>
32
33#include <iprt/test.h>
34#include <iprt/message.h>
35#include <iprt/string.h>
36#include <iprt/crypto/key.h>
37
38
39/* Test message:
40 * POST /foo?param=value&pet=dog HTTP/1.1
41 * Host: example.com
42 * Date: Sun, 05 Jan 2014 21:31:40 GMT
43 * Content-Type: application/json
44 * Digest: SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=
45 * Content-Length: 18
46 *
47 * {"hello": "world"}
48 */
49
50void testHeaderSigning()
51{
52 static const char s_szPublicKey1[] =
53 "-----BEGIN PUBLIC KEY-----\n"
54 "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBi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KaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI\n"
73 "7U1yQXnTAEFYM560yJlzUpOb1V4cScGd365tiSMvxLOvTA==\n"
74 "-----END RSA PRIVATE KEY-----\n";
75 static const char s_szKeyId1[] = "Test";
76 static const char s_szUrl1[] = "https://example.com/foo?param=value&pet=dog";
77 static const char s_szHost1[] = "example.com";
78 static const char s_szDate1[] = "Sun, 05 Jan 2014 21:31:40 GMT";
79
80 RTTestISub("RTHttpSignHeaders");
81
82
83 /*
84 * Load the key pair used in the reference examples.
85 */
86 RTCRKEY hPublicKey;
87 RTTESTI_CHECK_RC_RETV(RTCrKeyCreateFromBuffer(&hPublicKey, 0, RT_STR_TUPLE(s_szPublicKey1), NULL /*pszPassword*/,
88 NULL /*pErrInfo*/, NULL /*pszErrorTag*/), VINF_SUCCESS);
89 RTCRKEY hPrivateKey;
90 RTTESTI_CHECK_RC_RETV(RTCrKeyCreateFromBuffer(&hPrivateKey, 0, RT_STR_TUPLE(s_szPrivateKey1), NULL /*pszPassword*/,
91 NULL /*pErrInfo*/, NULL /*pszErrorTag*/), VINF_SUCCESS);
92
93 /*
94 * C.2 Basic Test - tweaked a little with 'version="1"'.
95 */
96 RTHTTP hHttp;
97 RTTESTI_CHECK_RC_RETV(RTHttpCreate(&hHttp), VINF_SUCCESS);
98 RTTESTI_CHECK_RC_RETV(RTHttpAddHeader(hHttp, "Host", RT_STR_TUPLE(s_szHost1), RTHTTPADDHDR_F_BACK), VINF_SUCCESS);
99 RTTESTI_CHECK_RC_RETV(RTHttpAddHeader(hHttp, "Date", RT_STR_TUPLE(s_szDate1), RTHTTPADDHDR_F_BACK), VINF_SUCCESS);
100 RTTESTI_CHECK_RC_RETV(RTHttpSignHeaders(hHttp, RTHTTPMETHOD_POST, s_szUrl1, hPrivateKey, s_szKeyId1, 0), VINF_SUCCESS);
101 const char *pszAuth = RTHttpGetHeader(hHttp, RT_STR_TUPLE("Authorization"));
102 RTTESTI_CHECK_RETV(pszAuth);
103 //const char *pszExpect = "Signature keyId=\"Test\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date\",signature=\"qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0=\"";
104 const char *pszExpect = "Signature version=\"1\",keyId=\"Test\",algorithm=\"rsa-sha256\","
105 "headers=\"(request-target) host date\","
106 "signature=\"qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0=\"";
107 if (strcmp(pszAuth, pszExpect) != 0)
108 {
109 RTTestIFailed("Test C.2 failed");
110 RTTestIFailureDetails("Got auth: %s\n", pszAuth);
111 RTTestIFailureDetails("Expected: %s\n", pszExpect);
112 }
113 RTTESTI_CHECK_RC(RTHttpDestroy(hHttp), VINF_SUCCESS);
114
115 /*
116 * C.3 All Headers Test - tweaked a little with 'version="1"'.
117 *
118 * Note! Draft #10 has an incorrect signed digest. The decrypting digest
119 * does not match the documented plaintext.
120 * Decrypted sha-256: 407954c106c7e9aa1644fc4764cbfb481cc178dec9142bf62e3cac97251e1953
121 * Plain text sha-256: 53cd4050ff72e3a6383091186168f3df4ca2e6b3a77cbed60a02ba00c9cd8078
122 */
123 hHttp = NIL_RTHTTP;
124 RTTESTI_CHECK_RC_RETV(RTHttpCreate(&hHttp), VINF_SUCCESS);
125 RTTESTI_CHECK_RC_RETV(RTHttpAddHeader(hHttp, "Host", RT_STR_TUPLE(s_szHost1), RTHTTPADDHDR_F_BACK), VINF_SUCCESS);
126 RTTESTI_CHECK_RC_RETV(RTHttpAddHeader(hHttp, "Date", RT_STR_TUPLE(s_szDate1), RTHTTPADDHDR_F_BACK), VINF_SUCCESS);
127 RTTESTI_CHECK_RC_RETV(RTHttpAddHeader(hHttp, "Content-Type", RT_STR_TUPLE("application/json"), RTHTTPADDHDR_F_BACK), VINF_SUCCESS);
128 RTTESTI_CHECK_RC_RETV(RTHttpAddHeader(hHttp, "Digest", RT_STR_TUPLE("SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE="), RTHTTPADDHDR_F_BACK), VINF_SUCCESS);
129 RTTESTI_CHECK_RC_RETV(RTHttpAddHeader(hHttp, "Content-Length", RT_STR_TUPLE("18"), RTHTTPADDHDR_F_BACK), VINF_SUCCESS);
130
131 RTTESTI_CHECK_RC_RETV(RTHttpSignHeaders(hHttp, RTHTTPMETHOD_POST, s_szUrl1, hPrivateKey, s_szKeyId1, 0), VINF_SUCCESS);
132 pszAuth = RTHttpGetHeader(hHttp, RT_STR_TUPLE("Authorization"));
133 RTTESTI_CHECK_RETV(pszAuth);
134 //pszExpect = "Signature keyId=\"Test\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date content-type digest content-length\",signature=\"jgSqYK0yKclIHfF9zdApVEbDp5eqj8C4i4X76pE+XHoxugXv7qnVrGR+30bmBgtpR39I4utq17s9ghz/2QFVxlnToYAvbSVZJ9ulLd1HQBugO0jOyn9sXOtcN7uNHBjqNCqUsnt0sw/cJA6B6nJZpyNqNyAXKdxZZItOuhIs78w=\"";
135 pszExpect = "Signature version=\"1\",keyId=\"Test\",algorithm=\"rsa-sha256\","
136 "headers=\"(request-target) host date content-type digest content-length\","
137 // bad rfc draft #10? "signature=\"jgSqYK0yKclIHfF9zdApVEbDp5eqj8C4i4X76pE+XHoxugXv7qnVrGR+30bmBgtpR39I4utq17s9ghz/2QFVxlnToYAvbSVZJ9ulLd1HQBugO0jOyn9sXOtcN7uNHBjqNCqUsnt0sw/cJA6B6nJZpyNqNyAXKdxZZItOuhIs78w=\"";
138 "signature=\"vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZFukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE=\"";
139 if (strcmp(pszAuth, pszExpect) != 0)
140 {
141 RTTestIFailed("Test C.3 failed");
142 RTTestIFailureDetails("Got auth: %s\n", pszAuth);
143 RTTestIFailureDetails("Expected: %s\n", pszExpect);
144 }
145}
146
147
148int main()
149{
150 RTTEST hTest;
151 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTHttp-1", &hTest);
152 if (rcExit != RTEXITCODE_SUCCESS)
153 return rcExit;
154
155 testHeaderSigning();
156
157 return RTTestSummaryAndDestroy(hTest);
158}
159
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette