1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 /////////////////////////////////////////////////////////////////////////////// 16 17 #include "tink/aead/kms_envelope_aead_key_manager.h" 18 19 #include <memory> 20 #include <string> 21 #include <utility> 22 23 #include "tink/aead.h" 24 #include "tink/aead/kms_envelope_aead.h" 25 #include "tink/kms_client.h" 26 #include "tink/kms_clients.h" 27 #include "tink/util/statusor.h" 28 #include "proto/kms_envelope.pb.h" 29 30 namespace crypto { 31 namespace tink { 32 33 using ::crypto::tink::util::StatusOr; 34 using ::google::crypto::tink::KmsEnvelopeAeadKey; 35 Create(const KmsEnvelopeAeadKey & key) const36StatusOr<std::unique_ptr<Aead>> KmsEnvelopeAeadKeyManager::AeadFactory::Create( 37 const KmsEnvelopeAeadKey& key) const { 38 const auto& kek_uri = key.params().kek_uri(); 39 auto kms_client_result = KmsClients::Get(kek_uri); 40 if (!kms_client_result.ok()) return kms_client_result.status(); 41 auto aead_result = kms_client_result.value()->GetAead(kek_uri); 42 if (!aead_result.ok()) return aead_result.status(); 43 return KmsEnvelopeAead::New(key.params().dek_template(), 44 std::move(aead_result.value())); 45 } 46 47 } // namespace tink 48 } // namespace crypto 49