/* * Copyright 2009-2017 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 #include #include #include #include "utils/Utils.h" using namespace AlibabaCloud::OSS; EncryptionMaterials::~EncryptionMaterials() { } ///////////////////////////////////////////////////////////////////////////////////////////////////////////// SimpleRSAEncryptionMaterials::SimpleRSAEncryptionMaterials(const std::string& publicKey, const std::string& privateKey): SimpleRSAEncryptionMaterials(publicKey, privateKey, std::map< std::string, std::string>()) { } SimpleRSAEncryptionMaterials::SimpleRSAEncryptionMaterials(const std::string& publicKey, const std::string& privateKey, const std::map& description): publicKey_(publicKey), description_(description) { encryptionMaterials_.push_back(RSAEncryptionMaterial(std::pair(publicKey, privateKey), description)); } SimpleRSAEncryptionMaterials::~SimpleRSAEncryptionMaterials() { } void SimpleRSAEncryptionMaterials::addEncryptionMaterial(const std::string& publicKey, const std::string& privateKey, const std::map& description) { encryptionMaterials_.push_back(RSAEncryptionMaterial(std::pair(publicKey, privateKey), description)); } int SimpleRSAEncryptionMaterials::EncryptCEK(ContentCryptoMaterial& contentCryptoMaterial) { auto cipher = AsymmetricCipher::CreateRSA_NONEImpl(); cipher->setPublicKey(publicKey_); auto encrptedKey = cipher->Encrypt(contentCryptoMaterial.ContentKey()); auto encrptedKeyIV = cipher->Encrypt(contentCryptoMaterial.ContentIV()); if (encrptedKey.empty() || encrptedKeyIV.empty()) { return -1; } contentCryptoMaterial.setDescription(description_); contentCryptoMaterial.setKeyWrapAlgorithm(cipher->Name()); contentCryptoMaterial.setEncryptedContentKey(encrptedKey); contentCryptoMaterial.setEncryptedContentIV(encrptedKeyIV); return 0; } int SimpleRSAEncryptionMaterials::DecryptCEK(ContentCryptoMaterial& contentCryptoMaterial) { auto cipher = AsymmetricCipher::CreateRSA_NONEImpl(); auto keyWarpAlgo = ToLower(contentCryptoMaterial.KeyWrapAlgorithm().c_str()); auto cipherName = ToLower(cipher->Name().c_str()); if (keyWarpAlgo != cipherName) { return -1; } int index = findIndexByDescription(contentCryptoMaterial.Description()); if (index < 0) { return -1; } const std::string& privateKey = encryptionMaterials_[index].first.second; cipher->setPrivateKey(privateKey); auto key = cipher->Decrypt(contentCryptoMaterial.EncryptedContentKey()); auto iv = cipher->Decrypt(contentCryptoMaterial.EncryptedContentIV()); if (key.empty() || iv.empty()) { return -1; } contentCryptoMaterial.setContentKey(key); contentCryptoMaterial.setContentIV(iv); return 0; } int SimpleRSAEncryptionMaterials::findIndexByDescription(const std::map& description) { bool found = false; int index = 0; for (const auto& item : encryptionMaterials_) { if (item.second == description) { found = true; break; } index++; } return found ? index: -1; }