xref: /aosp_15_r20/external/tink/cc/jwt/internal/raw_jwt_rsa_ssa_pss_verify_key_manager.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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 #ifndef TINK_JWT_INTERNAL_RAW_JWT_RSA_SSA_PSS_VERIFY_KEY_MANAGER_H_
17 #define TINK_JWT_INTERNAL_RAW_JWT_RSA_SSA_PSS_VERIFY_KEY_MANAGER_H_
18 
19 #include <memory>
20 #include <string>
21 
22 #include "absl/memory/memory.h"
23 #include "absl/strings/str_cat.h"
24 #include "tink/core/key_type_manager.h"
25 #include "tink/public_key_verify.h"
26 #include "tink/util/constants.h"
27 #include "tink/util/errors.h"
28 #include "tink/util/protobuf_helper.h"
29 #include "tink/util/status.h"
30 #include "tink/util/statusor.h"
31 #include "proto/common.pb.h"
32 #include "proto/jwt_rsa_ssa_pss.pb.h"
33 #include "proto/tink.pb.h"
34 
35 namespace crypto {
36 namespace tink {
37 
38 class RawJwtRsaSsaPssVerifyKeyManager
39     : public KeyTypeManager<google::crypto::tink::JwtRsaSsaPssPublicKey, void,
40                             List<PublicKeyVerify>> {
41  public:
42   class PublicKeyVerifyFactory : public PrimitiveFactory<PublicKeyVerify> {
43     crypto::tink::util::StatusOr<std::unique_ptr<PublicKeyVerify>> Create(
44         const google::crypto::tink::JwtRsaSsaPssPublicKey&
45             rsa_ssa_pss_public_key) const override;
46   };
47 
RawJwtRsaSsaPssVerifyKeyManager()48   RawJwtRsaSsaPssVerifyKeyManager()
49       : KeyTypeManager(absl::make_unique<PublicKeyVerifyFactory>()) {}
50 
get_version()51   uint32_t get_version() const override { return 0; }
52 
key_material_type()53   google::crypto::tink::KeyData::KeyMaterialType key_material_type()
54       const override {
55     return google::crypto::tink::KeyData::ASYMMETRIC_PUBLIC;
56   }
57 
get_key_type()58   const std::string& get_key_type() const override { return key_type_; }
59 
60   crypto::tink::util::Status ValidateKey(
61       const google::crypto::tink::JwtRsaSsaPssPublicKey& key) const override;
62 
FipsStatus()63   internal::FipsCompatibility FipsStatus() const override {
64     return internal::FipsCompatibility::kRequiresBoringCrypto;
65   }
66 
67  private:
68   static crypto::tink::util::Status ValidateAlgorithm(
69       const google::crypto::tink::JwtRsaSsaPssAlgorithm& algorithm);
70 
71   static crypto::tink::util::StatusOr<google::crypto::tink::HashType>
72   HashForPssAlgorithm(
73       const google::crypto::tink::JwtRsaSsaPssAlgorithm& algorithm);
74 
75   static crypto::tink::util::StatusOr<int> SaltLengthForPssAlgorithm(
76       const google::crypto::tink::JwtRsaSsaPssAlgorithm& algorithm);
77 
78   const std::string key_type_ =
79       absl::StrCat(kTypeGoogleapisCom,
80                    google::crypto::tink::JwtRsaSsaPssPublicKey().GetTypeName());
81   friend class RawJwtRsaSsaPssSignKeyManager;
82 };
83 
84 }  // namespace tink
85 }  // namespace crypto
86 
87 #endif  // TINK_JWT_INTERNAL_RAW_JWT_RSA_SSA_PSS_VERIFY_KEY_MANAGER_H_
88