• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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 #include "tink/jwt/internal/jwt_ecdsa_sign_key_manager.h"
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 #include "tink/jwt/internal/jwt_ecdsa_verify_key_manager.h"
23 #include "tink/jwt/internal/jwt_public_key_sign_impl.h"
24 
25 namespace crypto {
26 namespace tink {
27 namespace jwt_internal {
28 
29 using crypto::tink::util::Status;
30 using crypto::tink::util::StatusOr;
31 using google::crypto::tink::JwtEcdsaKeyFormat;
32 using google::crypto::tink::JwtEcdsaPrivateKey;
33 using google::crypto::tink::JwtEcdsaPublicKey;
34 
35 StatusOr<std::unique_ptr<JwtPublicKeySignInternal>>
Create(const JwtEcdsaPrivateKey & jwt_ecdsa_private_key) const36 JwtEcdsaSignKeyManager::PublicKeySignFactory::Create(
37     const JwtEcdsaPrivateKey& jwt_ecdsa_private_key) const {
38   StatusOr<std::string> name = JwtEcdsaVerifyKeyManager::AlgorithmName(
39       jwt_ecdsa_private_key.public_key().algorithm());
40   if (!name.ok()) {
41     return name.status();
42   }
43   util::StatusOr<std::unique_ptr<PublicKeySign>> sign =
44       raw_key_manager_.GetPrimitive<PublicKeySign>(jwt_ecdsa_private_key);
45   if (!sign.ok()) {
46     return sign.status();
47   }
48   absl::optional<absl::string_view> custom_kid = absl::nullopt;
49   if (jwt_ecdsa_private_key.public_key().has_custom_kid()) {
50     custom_kid = jwt_ecdsa_private_key.public_key().custom_kid().value();
51   }
52   std::unique_ptr<JwtPublicKeySignInternal> jwt_public_key_sign =
53       absl::make_unique<jwt_internal::JwtPublicKeySignImpl>(*std::move(sign),
54                                                             *name, custom_kid);
55   return std::move(jwt_public_key_sign);
56 }
57 
get_version() const58 uint32_t JwtEcdsaSignKeyManager::get_version() const {
59   return raw_key_manager_.get_version();
60 }
61 
62 google::crypto::tink::KeyData::KeyMaterialType
key_material_type() const63 JwtEcdsaSignKeyManager::key_material_type() const {
64   return raw_key_manager_.key_material_type();
65 }
66 
get_key_type() const67 const std::string& JwtEcdsaSignKeyManager::get_key_type() const {
68   return raw_key_manager_.get_key_type();
69 }
70 
CreateKey(const JwtEcdsaKeyFormat & key_format) const71 StatusOr<JwtEcdsaPrivateKey> JwtEcdsaSignKeyManager::CreateKey(
72     const JwtEcdsaKeyFormat& key_format) const {
73   return raw_key_manager_.CreateKey(key_format);
74 }
75 
ValidateKey(const JwtEcdsaPrivateKey & key) const76 Status JwtEcdsaSignKeyManager::ValidateKey(
77     const JwtEcdsaPrivateKey& key) const {
78   return raw_key_manager_.ValidateKey(key);
79 }
80 
ValidateKeyFormat(const JwtEcdsaKeyFormat & key_format) const81 Status JwtEcdsaSignKeyManager::ValidateKeyFormat(
82     const JwtEcdsaKeyFormat& key_format) const {
83   return raw_key_manager_.ValidateKeyFormat(key_format);
84 }
85 
GetPublicKey(const JwtEcdsaPrivateKey & private_key) const86 StatusOr<JwtEcdsaPublicKey> JwtEcdsaSignKeyManager::GetPublicKey(
87     const JwtEcdsaPrivateKey& private_key) const {
88   return raw_key_manager_.GetPublicKey(private_key);
89 }
90 
91 }  // namespace jwt_internal
92 }  // namespace tink
93 }  // namespace crypto
94