Skip to content

Commit 53a113a

Browse files
committed
fix signature & update testcase.
1 parent a1623b3 commit 53a113a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+269
-76
lines changed

sdk/src/OssClientImpl.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "utils/SignUtils.h"
2727
#include "utils/ThreadExecutor.h"
2828
#include "signer/Signer.h"
29+
#include "signer/HmacSha1Signer.h"
2930
#include "OssClientImpl.h"
3031
#include "utils/LogUtils.h"
3132
#include "utils/FileSystemUtils.h"
@@ -173,7 +174,7 @@ void OssClientImpl::addSignInfo(const std::shared_ptr<HttpRequest> &httpRequest,
173174
}
174175

175176
if (httpRequest->hasHeader("x-oss-date")) {
176-
t = ToUnixTime(httpRequest->Header(Http::DATE), "%a, %d %b %Y %H:%M:%S GMT");
177+
t = ToUnixTime(httpRequest->Header("x-oss-date"), "%a, %d %b %Y %H:%M:%S GMT");
177178
}
178179

179180
SignerParam signerParam(std::move(region), std::move(product),
@@ -1716,7 +1717,7 @@ StringOutcome OssClientImpl::GenerateRTMPSignedUrl(const GenerateRTMPSignedUrlRe
17161717
SignUtils signUtils(signer_->version());
17171718
auto resource = std::string().append("/").append(request.Bucket()).append("/").append(request.ChannelName());
17181719
signUtils.build(expireStr, resource, parameters);
1719-
auto signature = signer_->generate(signUtils.CanonicalString(), credentials.AccessKeySecret());
1720+
auto signature = HmacSha1Signer::generate(signUtils.CanonicalString(), credentials.AccessKeySecret());
17201721
parameters["Expires"] = expireStr;
17211722
parameters["OSSAccessKeyId"] = credentials.AccessKeyId();
17221723
parameters["Signature"] = signature;

sdk/src/signer/HmacSha1Signer.cc

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2009-2017 Alibaba Cloud All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "HmacSha1Signer.h"
18+
#if 0//def _WIN32
19+
#include <windows.h>
20+
#include <wincrypt.h>
21+
#else
22+
#include <openssl/hmac.h>
23+
#ifdef OPENSSL_IS_BORINGSSL
24+
#include <openssl/base64.h>
25+
#endif
26+
#endif
27+
28+
using namespace AlibabaCloud::OSS;
29+
30+
HmacSha1Signer::HmacSha1Signer()
31+
{
32+
}
33+
34+
HmacSha1Signer::~HmacSha1Signer()
35+
{
36+
}
37+
38+
std::string HmacSha1Signer::generate(const std::string & src, const std::string & secret)
39+
{
40+
if (src.empty())
41+
return std::string();
42+
43+
#if 0//def _WIN32
44+
typedef struct _my_blob {
45+
BLOBHEADER hdr;
46+
DWORD dwKeySize;
47+
BYTE rgbKeyData[];
48+
}my_blob;
49+
50+
DWORD kbLen = sizeof(my_blob) + secret.size();
51+
my_blob * kb = (my_blob *)LocalAlloc(LPTR, kbLen);
52+
kb->hdr.bType = PLAINTEXTKEYBLOB;
53+
kb->hdr.bVersion = CUR_BLOB_VERSION;
54+
kb->hdr.reserved = 0;
55+
kb->hdr.aiKeyAlg = CALG_RC2;
56+
kb->dwKeySize = secret.size();
57+
memcpy(&kb->rgbKeyData, secret.c_str(), secret.size());
58+
59+
HCRYPTPROV hProv = 0;
60+
HCRYPTKEY hKey = 0;
61+
HCRYPTHASH hHmacHash = 0;
62+
BYTE pbHash[32];
63+
DWORD dwDataLen = 32;
64+
HMAC_INFO HmacInfo;
65+
ZeroMemory(&HmacInfo, sizeof(HmacInfo));
66+
HmacInfo.HashAlgid = CALG_SHA1;
67+
68+
CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET);
69+
CryptImportKey(hProv, (BYTE*)kb, kbLen, 0, CRYPT_IPSEC_HMAC_KEY, &hKey);
70+
CryptCreateHash(hProv, CALG_HMAC, hKey, 0, &hHmacHash);
71+
CryptSetHashParam(hHmacHash, HP_HMAC_INFO, (BYTE*)&HmacInfo, 0);
72+
CryptHashData(hHmacHash, (BYTE*)(src.c_str()), src.size(), 0);
73+
CryptGetHashParam(hHmacHash, HP_HASHVAL, pbHash, &dwDataLen, 0);
74+
75+
LocalFree(kb);
76+
CryptDestroyHash(hHmacHash);
77+
CryptDestroyKey(hKey);
78+
CryptReleaseContext(hProv, 0);
79+
80+
DWORD dlen = 0;
81+
CryptBinaryToString(pbHash, dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen);
82+
char* dest = new char[dlen];
83+
CryptBinaryToString(pbHash, dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen);
84+
85+
std::string ret = std::string(dest, dlen);
86+
delete[] dest;
87+
return ret;
88+
#else
89+
unsigned char md[32];
90+
unsigned int mdLen = 32;
91+
92+
if (HMAC(EVP_sha1(), secret.c_str(), static_cast<int>(secret.size()),
93+
reinterpret_cast<const unsigned char*>(src.c_str()), src.size(),
94+
md, &mdLen) == nullptr)
95+
return std::string();
96+
97+
char encodedData[100];
98+
EVP_EncodeBlock(reinterpret_cast<unsigned char*>(encodedData), md, mdLen);
99+
return encodedData;
100+
#endif
101+
}

sdk/src/signer/HmacSha1Signer.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2009-2017 Alibaba Cloud All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include "Signer.h"
20+
21+
22+
namespace AlibabaCloud
23+
{
24+
namespace OSS
25+
{
26+
27+
class HmacSha1Signer
28+
{
29+
public:
30+
HmacSha1Signer();
31+
~HmacSha1Signer();
32+
33+
static std::string generate(const std::string &src, const std::string &secret);
34+
};
35+
}
36+
}

sdk/src/signer/SignerV1.cc

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
#include <sstream>
1818
#include "Signer.h"
19+
#include "HmacSha1Signer.h"
1920
#include "../utils/SignUtils.h"
2021
#include "../utils/Utils.h"
2122
#include "../utils/LogUtils.h"
22-
#include <openssl/hmac.h>
23-
#ifdef OPENSSL_IS_BORINGSSL
24-
#include <openssl/base64.h>
25-
#endif
2623

2724
using namespace AlibabaCloud::OSS;
2825

@@ -56,20 +53,7 @@ static std::string buildResource(const std::string &bucket, const std::string &k
5653

5754
std::string SignerV1::generate(const std::string & src, const std::string & secret) const
5855
{
59-
if (src.empty())
60-
return std::string();
61-
62-
unsigned char md[32];
63-
unsigned int mdLen = 32;
64-
65-
if (HMAC(EVP_sha1(), secret.c_str(), static_cast<int>(secret.size()),
66-
reinterpret_cast<const unsigned char*>(src.c_str()), src.size(),
67-
md, &mdLen) == nullptr)
68-
return std::string();
69-
70-
char encodedData[100];
71-
EVP_EncodeBlock(reinterpret_cast<unsigned char*>(encodedData), md, mdLen);
72-
return encodedData;
56+
return HmacSha1Signer::generate(src, secret);
7357
}
7458

7559
void SignerV1::sign(const std::shared_ptr<HttpRequest> &httpRequest, ParameterCollection &parameters,

sdk/src/signer/SignerV4.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ static std::string buildCanonicalReuqest(const std::shared_ptr<HttpRequest> &htt
196196
for (const auto &header : httpRequest->Headers()) {
197197
std::string lowerKey = ToLower(header.first.c_str());
198198
std::string value = Trim(header.second.c_str());
199+
if (value.empty()) {
200+
continue;
201+
}
199202
if (lowerKey == "content-type" ||
200203
lowerKey == "content-md5" ||
201204
lowerKey.compare(0, 6, "x-oss-") == 0) {
@@ -288,6 +291,10 @@ void SignerV4::sign(const std::shared_ptr<HttpRequest> &httpRequest, ParameterCo
288291
httpRequest->addHeader(Http::DATE, ToGmtTime(requestTime));
289292
httpRequest->addHeader("x-oss-date", datetime);
290293

294+
if (!httpRequest->hasHeader("x-oss-content-sha256")) {
295+
httpRequest->addHeader("x-oss-content-sha256", "UNSIGNED-PAYLOAD");
296+
}
297+
291298
auto additionalHeaders = getCommonAdditionalHeaders(httpRequest->Headers(), signerParam.AdditionalHeaders());
292299

293300
auto canonicalReuqest = buildCanonicalReuqest(httpRequest, parameters, signerParam, additionalHeaders);

test/src/Bucket/BucketAclSettingsTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BucketAclSettingsTest : public ::testing::Test {
3535
// Sets up the stuff shared by all tests in this test case.
3636
static void SetUpTestCase()
3737
{
38-
Client = std::make_shared<OssClient>(Config::Endpoint, Config::AccessKeyId, Config::AccessKeySecret, ClientConfiguration());
38+
Client = TestUtils::GetOssClientDefault();
3939
BucketName = TestUtils::GetBucketName("cpp-sdk-bucketaclsettings");
4040
Client->CreateBucket(CreateBucketRequest(BucketName));
4141
}

test/src/Bucket/BucketBasicOperationTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BucketBasicOperationTest : public ::testing::Test {
3535
// Sets up the stuff shared by all tests in this test case.
3636
static void SetUpTestCase()
3737
{
38-
Client = std::make_shared<OssClient>(Config::Endpoint, Config::AccessKeyId, Config::AccessKeySecret, ClientConfiguration());
38+
Client = TestUtils::GetOssClientDefault();
3939
}
4040

4141
// Tears down the stuff shared by all tests in this test case.

test/src/Bucket/BucketCorsSettingsTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class BucketCorsSettingsTest : public ::testing::Test {
3939
// Sets up the stuff shared by all tests in this test case.
4040
static void SetUpTestCase()
4141
{
42-
Client = std::make_shared<OssClient>(Config::Endpoint, Config::AccessKeyId, Config::AccessKeySecret, ClientConfiguration());
42+
Client = TestUtils::GetOssClientDefault();
4343
BucketName = TestUtils::GetBucketName("cpp-sdk-bucketcorssettings");
4444
Client->CreateBucket(CreateBucketRequest(BucketName));
4545
}

test/src/Bucket/BucketEncryptionTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace OSS {
3535
// Sets up the stuff shared by all tests in this test case.
3636
static void SetUpTestCase()
3737
{
38-
Client = std::make_shared<OssClient>(Config::Endpoint, Config::AccessKeyId, Config::AccessKeySecret, ClientConfiguration());
38+
Client = TestUtils::GetOssClientDefault();
3939
BucketName = TestUtils::GetBucketName("cpp-sdk-bucketencryption");
4040
Client->CreateBucket(CreateBucketRequest(BucketName));
4141
}

test/src/Bucket/BucketInventoryConfigurationTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BucketInventoryConfigurationTest : public ::testing::Test {
3535
// Sets up the stuff shared by all tests in this test case.
3636
static void SetUpTestCase()
3737
{
38-
Client = std::make_shared<OssClient>(Config::Endpoint, Config::AccessKeyId, Config::AccessKeySecret, ClientConfiguration());
38+
Client = TestUtils::GetOssClientDefault();
3939
BucketName = TestUtils::GetBucketName("cpp-sdk-inventory");
4040
DstBucketName = TestUtils::GetBucketName("cpp-sdk-inventory-dst");
4141
Client->CreateBucket(CreateBucketRequest(BucketName));

0 commit comments

Comments
 (0)