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 <alibabacloud/oss/encryption/EncryptionMaterials.h>
18 #include <alibabacloud/oss/encryption/Cipher.h>
19 #include <map>
20 #include <string>
21 #include "utils/Utils.h"
22
23 using namespace AlibabaCloud::OSS;
24
~EncryptionMaterials()25 EncryptionMaterials::~EncryptionMaterials()
26 {
27 }
28
29 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
SimpleRSAEncryptionMaterials(const std::string & publicKey,const std::string & privateKey)30 SimpleRSAEncryptionMaterials::SimpleRSAEncryptionMaterials(const std::string& publicKey, const std::string& privateKey):
31 SimpleRSAEncryptionMaterials(publicKey, privateKey, std::map< std::string, std::string>())
32 {
33 }
34
SimpleRSAEncryptionMaterials(const std::string & publicKey,const std::string & privateKey,const std::map<std::string,std::string> & description)35 SimpleRSAEncryptionMaterials::SimpleRSAEncryptionMaterials(const std::string& publicKey, const std::string& privateKey,
36 const std::map<std::string, std::string>& description):
37 publicKey_(publicKey),
38 description_(description)
39 {
40 encryptionMaterials_.push_back(RSAEncryptionMaterial(std::pair<std::string, std::string>(publicKey, privateKey), description));
41 }
42
~SimpleRSAEncryptionMaterials()43 SimpleRSAEncryptionMaterials::~SimpleRSAEncryptionMaterials()
44 {
45 }
46
addEncryptionMaterial(const std::string & publicKey,const std::string & privateKey,const std::map<std::string,std::string> & description)47 void SimpleRSAEncryptionMaterials::addEncryptionMaterial(const std::string& publicKey, const std::string& privateKey,
48 const std::map<std::string, std::string>& description)
49 {
50 encryptionMaterials_.push_back(RSAEncryptionMaterial(std::pair<std::string, std::string>(publicKey, privateKey), description));
51 }
52
EncryptCEK(ContentCryptoMaterial & contentCryptoMaterial)53 int SimpleRSAEncryptionMaterials::EncryptCEK(ContentCryptoMaterial& contentCryptoMaterial)
54 {
55 auto cipher = AsymmetricCipher::CreateRSA_NONEImpl();
56 cipher->setPublicKey(publicKey_);
57
58 auto encrptedKey = cipher->Encrypt(contentCryptoMaterial.ContentKey());
59 auto encrptedKeyIV = cipher->Encrypt(contentCryptoMaterial.ContentIV());
60
61 if (encrptedKey.empty() || encrptedKeyIV.empty()) {
62 return -1;
63 }
64
65 contentCryptoMaterial.setDescription(description_);
66 contentCryptoMaterial.setKeyWrapAlgorithm(cipher->Name());
67 contentCryptoMaterial.setEncryptedContentKey(encrptedKey);
68 contentCryptoMaterial.setEncryptedContentIV(encrptedKeyIV);
69
70 return 0;
71 }
72
DecryptCEK(ContentCryptoMaterial & contentCryptoMaterial)73 int SimpleRSAEncryptionMaterials::DecryptCEK(ContentCryptoMaterial& contentCryptoMaterial)
74 {
75 auto cipher = AsymmetricCipher::CreateRSA_NONEImpl();
76
77 auto keyWarpAlgo = ToLower(contentCryptoMaterial.KeyWrapAlgorithm().c_str());
78 auto cipherName = ToLower(cipher->Name().c_str());
79
80 if (keyWarpAlgo != cipherName) {
81 return -1;
82 }
83
84 int index = findIndexByDescription(contentCryptoMaterial.Description());
85 if (index < 0) {
86 return -1;
87 }
88 const std::string& privateKey = encryptionMaterials_[index].first.second;
89
90 cipher->setPrivateKey(privateKey);
91
92 auto key = cipher->Decrypt(contentCryptoMaterial.EncryptedContentKey());
93 auto iv = cipher->Decrypt(contentCryptoMaterial.EncryptedContentIV());
94
95 if (key.empty() || iv.empty()) {
96 return -1;
97 }
98
99 contentCryptoMaterial.setContentKey(key);
100 contentCryptoMaterial.setContentIV(iv);
101
102 return 0;
103 }
104
findIndexByDescription(const std::map<std::string,std::string> & description)105 int SimpleRSAEncryptionMaterials::findIndexByDescription(const std::map<std::string, std::string>& description)
106 {
107 bool found = false;
108 int index = 0;
109 for (const auto& item : encryptionMaterials_) {
110 if (item.second == description) {
111 found = true;
112 break;
113 }
114 index++;
115 }
116 return found ? index: -1;
117 }
118