1 /*
2  * Copyright 1999-2019 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 <alibabacloud/core/HmacSha1Signer.h>
18 #ifdef _WIN32
19 #include <wincrypt.h>
20 #include <windows.h>
21 #elif defined(USE_CRYPTO_MBEDTLS)
22 #include "mbedtls/compat-1.3.h"
23 #include "mbedtls/sha1.h"
24 #include "mbedtls/base64.h"
25 #include "mbedtls/md.h"
26 #else
27 #include <openssl/hmac.h>
28 #endif
29 
30 namespace AlibabaCloud {
31 
HmacSha1Signer()32 HmacSha1Signer::HmacSha1Signer() : Signer(HmacSha1, "HMAC-SHA1", "1.0") {}
33 
~HmacSha1Signer()34 HmacSha1Signer::~HmacSha1Signer() {}
35 
generate(const std::string & src,const std::string & secret) const36 std::string HmacSha1Signer::generate(const std::string &src,
37                                      const std::string &secret) const {
38   if (src.empty())
39     return std::string();
40 
41 #ifdef _WIN32
42   typedef struct _my_blob {
43     BLOBHEADER hdr;
44     DWORD dwKeySize;
45     BYTE rgbKeyData[];
46   } my_blob;
47 
48   DWORD kbLen = sizeof(my_blob) + secret.size();
49   my_blob *kb = (my_blob *)LocalAlloc(LPTR, kbLen);
50   kb->hdr.bType = PLAINTEXTKEYBLOB;
51   kb->hdr.bVersion = CUR_BLOB_VERSION;
52   kb->hdr.reserved = 0;
53   kb->hdr.aiKeyAlg = CALG_RC2;
54   kb->dwKeySize = secret.size();
55   memcpy(&kb->rgbKeyData, secret.c_str(), secret.size());
56 
57   HCRYPTPROV hProv = 0;
58   HCRYPTKEY hKey = 0;
59   HCRYPTHASH hHmacHash = 0;
60   BYTE pbHash[32];
61   DWORD dwDataLen = 32;
62   HMAC_INFO HmacInfo;
63   ZeroMemory(&HmacInfo, sizeof(HmacInfo));
64   HmacInfo.HashAlgid = CALG_SHA1;
65 
66   CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL,
67                       CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET);
68   CryptImportKey(hProv, (BYTE *)kb, kbLen, 0, CRYPT_IPSEC_HMAC_KEY, &hKey);
69   CryptCreateHash(hProv, CALG_HMAC, hKey, 0, &hHmacHash);
70   CryptSetHashParam(hHmacHash, HP_HMAC_INFO, (BYTE *)&HmacInfo, 0);
71   CryptHashData(hHmacHash, (BYTE *)(src.c_str()), src.size(), 0);
72   CryptGetHashParam(hHmacHash, HP_HASHVAL, pbHash, &dwDataLen, 0);
73 
74   LocalFree(kb);
75   CryptDestroyHash(hHmacHash);
76   CryptDestroyKey(hKey);
77   CryptReleaseContext(hProv, 0);
78 
79   DWORD dlen = 0;
80   CryptBinaryToString(pbHash, dwDataLen,
81                       CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen);
82   char *dest = new char[dlen];
83   CryptBinaryToString(pbHash, dwDataLen,
84                       CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen);
85 
86   std::string ret = std::string(dest, dlen);
87   delete dest;
88   return ret;
89 
90 #elif defined(USE_CRYPTO_MBEDTLS)
91 	unsigned char md[20];
92   unsigned int mdLen = 20;
93 	size_t olen = 0;
94   mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
95                   reinterpret_cast<const unsigned char*>(secret.c_str()),static_cast<int>(secret.size()),
96                   reinterpret_cast<const unsigned char*>(src.c_str()), src.size(),
97                   (unsigned char *)md);
98   char encodedData[100];
99   mbedtls_base64_encode((unsigned char*)encodedData, sizeof(encodedData), &olen, md, mdLen);
100 
101   return encodedData;
102 #else
103   unsigned char md[EVP_MAX_BLOCK_LENGTH];
104   unsigned int mdLen = EVP_MAX_BLOCK_LENGTH;
105 
106   if (HMAC(EVP_sha1(), secret.c_str(), secret.size(),
107            reinterpret_cast<const unsigned char *>(src.c_str()), src.size(), md,
108            &mdLen) == nullptr)
109     return std::string();
110 
111   char encodedData[100];
112   EVP_EncodeBlock(reinterpret_cast<unsigned char *>(encodedData), md, mdLen);
113   return encodedData;
114 #endif
115 }
116 
117 } // namespace AlibabaCloud
118