Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(core_public_header
include/alibabacloud/core/Error.h
include/alibabacloud/core/Global.h
include/alibabacloud/core/HmacSha1Signer.h
include/alibabacloud/core/HmacSha256Signer.h
include/alibabacloud/core/HttpClient.h
include/alibabacloud/core/HttpMessage.h
include/alibabacloud/core/HttpResponse.h
Expand Down Expand Up @@ -103,6 +104,7 @@ set(core_src
src/Executor.cc
src/Executor.h
src/HmacSha1Signer.cc
src/HmacSha256Signer.cc
src/HttpClient.cc
src/HttpMessage.cc
src/HttpResponse.cc
Expand Down
34 changes: 34 additions & 0 deletions core/include/alibabacloud/core/HmacSha256Signer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 1999-2019 Alibaba Cloud All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CORE_INCLUDE_ALIBABACLOUD_CORE_HMACSHA256SIGNER_H_
#define CORE_INCLUDE_ALIBABACLOUD_CORE_HMACSHA256SIGNER_H_

#include "Signer.h"
#include <string>

namespace AlibabaCloud {
class ALIBABACLOUD_CORE_EXPORT HmacSha256Signer : public Signer {
public:
HmacSha256Signer();
~HmacSha256Signer();

std::string generate(const std::string &src,
const std::string &secret) const override;
};
} // namespace AlibabaCloud

#endif // CORE_INCLUDE_ALIBABACLOUD_CORE_HMACSHA256SIGNER_H_
3 changes: 2 additions & 1 deletion core/include/alibabacloud/core/RoaServiceClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "CoreClient.h"
#include "CredentialsProvider.h"
#include "HmacSha1Signer.h"
#include "HmacSha256Signer.h"
#include "HttpRequest.h"
#include "RoaServiceRequest.h"
#include <map>
Expand All @@ -37,7 +38,7 @@ class ALIBABACLOUD_CORE_EXPORT RoaServiceClient : public CoreClient {
const std::shared_ptr<CredentialsProvider> &credentialsProvider,
const ClientConfiguration &configuration,
const std::shared_ptr<Signer> &signer =
std::make_shared<HmacSha1Signer>());
std::make_shared<HmacSha256Signer>());
virtual ~RoaServiceClient();

protected:
Expand Down
3 changes: 2 additions & 1 deletion core/include/alibabacloud/core/RpcServiceClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "CoreClient.h"
#include "CredentialsProvider.h"
#include "HmacSha1Signer.h"
#include "HmacSha256Signer.h"
#include "HttpRequest.h"
#include "Outcome.h"
#include "RpcServiceRequest.h"
Expand All @@ -39,7 +40,7 @@ class ALIBABACLOUD_CORE_EXPORT RpcServiceClient : public CoreClient {
const std::shared_ptr<CredentialsProvider> &credentialsProvider,
const ClientConfiguration &configuration,
const std::shared_ptr<Signer> &signer =
std::make_shared<HmacSha1Signer>());
std::make_shared<HmacSha256Signer>());
virtual ~RpcServiceClient();

std::shared_ptr<CredentialsProvider> credentialsProvider() {
Expand Down
1 change: 1 addition & 0 deletions core/include/alibabacloud/core/Signer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ALIBABACLOUD_CORE_EXPORT Signer {
public:
enum Type {
HmacSha1,
HmacSha256,
};
virtual ~Signer();

Expand Down
7 changes: 4 additions & 3 deletions core/src/CommonClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <algorithm>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/core/CommonClient.h>
#include <alibabacloud/core/HmacSha256Signer.h>
#include <alibabacloud/core/SimpleCredentialsProvider.h>
#include <alibabacloud/core/location/LocationClient.h>
#include <ctime>
Expand All @@ -36,22 +37,22 @@ CommonClient::CommonClient(const Credentials &credentials,
: CoreClient(SERVICE_NAME, configuration),
credentialsProvider_(
std::make_shared<SimpleCredentialsProvider>(credentials)),
signer_(std::make_shared<HmacSha1Signer>()) {}
signer_(std::make_shared<HmacSha256Signer>()) {}

CommonClient::CommonClient(
const std::shared_ptr<CredentialsProvider> &credentialsProvider,
const ClientConfiguration &configuration)
: CoreClient(SERVICE_NAME, configuration),
credentialsProvider_(credentialsProvider),
signer_(std::make_shared<HmacSha1Signer>()) {}
signer_(std::make_shared<HmacSha256Signer>()) {}

CommonClient::CommonClient(const std::string &accessKeyId,
const std::string &accessKeySecret,
const ClientConfiguration &configuration)
: CoreClient(SERVICE_NAME, configuration),
credentialsProvider_(std::make_shared<SimpleCredentialsProvider>(
accessKeyId, accessKeySecret)),
signer_(std::make_shared<HmacSha1Signer>()) {}
signer_(std::make_shared<HmacSha256Signer>()) {}

CommonClient::~CommonClient() {}

Expand Down
105 changes: 105 additions & 0 deletions core/src/HmacSha256Signer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 1999-2019 Alibaba Cloud All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <alibabacloud/core/HmacSha256Signer.h>
#include <limits>
#ifdef _WIN32
#include <windows.h>
#include <wincrypt.h>
#include <cstring>
#include <vector>
#else
#include <openssl/hmac.h>
#endif

namespace AlibabaCloud {

HmacSha256Signer::HmacSha256Signer()
: Signer(HmacSha256, "HMAC-SHA256", "1.0") {}

HmacSha256Signer::~HmacSha256Signer() {}

std::string HmacSha256Signer::generate(const std::string &src,
const std::string &secret) const {
if (src.empty() || secret.size() > (std::numeric_limits<int>::max)())
return std::string();

#ifdef _WIN32
if (src.size() > MAXDWORD ||
secret.size() > MAXDWORD - sizeof(BLOBHEADER) - sizeof(DWORD))
return std::string();

std::vector<BYTE> keyBlob(sizeof(BLOBHEADER) + sizeof(DWORD) + secret.size());
auto header = reinterpret_cast<BLOBHEADER *>(keyBlob.data());
header->bType = PLAINTEXTKEYBLOB;
header->bVersion = CUR_BLOB_VERSION;
header->aiKeyAlg = CALG_RC2;
DWORD keySize = static_cast<DWORD>(secret.size());
std::memcpy(keyBlob.data() + sizeof(BLOBHEADER), &keySize, sizeof(keySize));
std::memcpy(keyBlob.data() + sizeof(BLOBHEADER) + sizeof(DWORD),
secret.data(), secret.size());

HCRYPTPROV provider = 0;
HCRYPTKEY key = 0;
HCRYPTHASH hash = 0;
BYTE digest[32];
DWORD digestSize = sizeof(digest);
HMAC_INFO info = {};
info.HashAlgid = CALG_SHA_256;
bool success =
CryptAcquireContext(&provider, nullptr, MS_ENH_RSA_AES_PROV, PROV_RSA_AES,
CRYPT_VERIFYCONTEXT) &&
CryptImportKey(provider, keyBlob.data(), static_cast<DWORD>(keyBlob.size()),
0, CRYPT_IPSEC_HMAC_KEY, &key) &&
CryptCreateHash(provider, CALG_HMAC, key, 0, &hash) &&
CryptSetHashParam(hash, HP_HMAC_INFO, reinterpret_cast<BYTE *>(&info), 0) &&
CryptHashData(hash, reinterpret_cast<const BYTE *>(src.data()),
static_cast<DWORD>(src.size()), 0) &&
CryptGetHashParam(hash, HP_HASHVAL, digest, &digestSize, 0);

if (hash)
CryptDestroyHash(hash);
if (key)
CryptDestroyKey(key);
if (provider)
CryptReleaseContext(provider, 0);
if (!success)
return std::string();

DWORD encodedSize = 0;
DWORD flags = CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF;
if (!CryptBinaryToStringA(digest, digestSize, flags, nullptr, &encodedSize))
return std::string();
std::string encoded(encodedSize, '\0');
if (!CryptBinaryToStringA(digest, digestSize, flags, &encoded[0], &encodedSize))
return std::string();
encoded.resize(std::strlen(encoded.c_str()));
return encoded;
#else
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int digestSize = sizeof(digest);
if (HMAC(EVP_sha256(), secret.data(), secret.size(),
reinterpret_cast<const unsigned char *>(src.data()), src.size(),
digest, &digestSize) == nullptr)
return std::string();

unsigned char encoded[4 * ((EVP_MAX_MD_SIZE + 2) / 3) + 1];
int encodedSize = EVP_EncodeBlock(encoded, digest, digestSize);
return std::string(reinterpret_cast<char *>(encoded), encodedSize);
#endif
}

} // namespace AlibabaCloud
1 change: 1 addition & 0 deletions test/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ add_executable(core_ut
httpresponse_ut.cc
httpmessage_ut.cc
hmacsha1signer_ut.cc
signaturev2_ut.cc
networkproxy_ut.cc
roaserviceclient_ut.cc
roaservicerequest_ut.cc
Expand Down
2 changes: 1 addition & 1 deletion test/core/commonclient_ut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace {
string signature_nonce = "fb62c51d-f735-4ec7-870d-c6ce03368214";
EXPECT_TRUE(rr.header("x-acs-signature-nonce").length() == signature_nonce.length());

EXPECT_TRUE(rr.header("x-acs-signature-method") == "HMAC-SHA1");
EXPECT_TRUE(rr.header("x-acs-signature-method") == "HMAC-SHA256");
EXPECT_TRUE(rr.header("x-acs-signature-version") == "1.0");
EXPECT_TRUE(rr.header("x-acs-version") == "");
EXPECT_TRUE(rr.header("x-sdk-client") == string("CPP/").append(ALIBABACLOUD_VERSION_STR));
Expand Down
Loading
Loading