xref: /aosp_15_r20/external/tink/cc/jwt/internal/raw_jwt_ecdsa_verify_key_manager_test.cc (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 
17 #include "tink/jwt/internal/raw_jwt_ecdsa_verify_key_manager.h"
18 
19 #include <memory>
20 #include <string>
21 
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "absl/status/status.h"
25 #include "tink/internal/ec_util.h"
26 #include "tink/jwt/internal/raw_jwt_ecdsa_sign_key_manager.h"
27 #include "tink/public_key_sign.h"
28 #include "tink/public_key_verify.h"
29 #include "tink/subtle/ecdsa_sign_boringssl.h"
30 #include "tink/util/enums.h"
31 #include "tink/util/secret_data.h"
32 #include "tink/util/status.h"
33 #include "tink/util/statusor.h"
34 #include "tink/util/test_matchers.h"
35 #include "tink/util/test_util.h"
36 #include "proto/ecdsa.pb.h"
37 
38 namespace crypto {
39 namespace tink {
40 namespace jwt_internal {
41 
42 using ::crypto::tink::test::IsOk;
43 using ::crypto::tink::test::StatusIs;
44 using ::crypto::tink::util::Enums;
45 using ::google::crypto::tink::JwtEcdsaKeyFormat;
46 using ::google::crypto::tink::JwtEcdsaPrivateKey;
47 using ::google::crypto::tink::JwtEcdsaPublicKey;
48 using ::google::crypto::tink::EllipticCurveType;
49 using ::google::crypto::tink::JwtEcdsaAlgorithm;
50 using ::google::crypto::tink::HashType;
51 using ::google::crypto::tink::KeyData;
52 using ::testing::Eq;
53 using ::testing::Not;
54 
55 namespace {
56 
TEST(RawJwtEcdsaVerifyKeyManagerTest,Basics)57 TEST(RawJwtEcdsaVerifyKeyManagerTest, Basics) {
58   EXPECT_THAT(RawJwtEcdsaVerifyKeyManager().get_version(), Eq(0));
59   EXPECT_THAT(RawJwtEcdsaVerifyKeyManager().key_material_type(),
60               Eq(KeyData::ASYMMETRIC_PUBLIC));
61   EXPECT_THAT(RawJwtEcdsaVerifyKeyManager().get_key_type(),
62               Eq("type.googleapis.com/google.crypto.tink.JwtEcdsaPublicKey"));
63 }
64 
TEST(RawJwtEcdsaVerifyKeyManagerTest,ValidateEmptyKey)65 TEST(RawJwtEcdsaVerifyKeyManagerTest, ValidateEmptyKey) {
66   EXPECT_THAT(RawJwtEcdsaVerifyKeyManager().ValidateKey(JwtEcdsaPublicKey()),
67               Not(IsOk()));
68 }
69 
CreateValidEs256PrivateKey()70 JwtEcdsaPrivateKey CreateValidEs256PrivateKey() {
71   JwtEcdsaKeyFormat key_format;
72   key_format.set_algorithm(JwtEcdsaAlgorithm::ES256);
73   return RawJwtEcdsaSignKeyManager().CreateKey(key_format).value();
74 }
75 
CreateValidPublicKey()76 JwtEcdsaPublicKey CreateValidPublicKey() {
77   return RawJwtEcdsaSignKeyManager()
78       .GetPublicKey(CreateValidEs256PrivateKey())
79       .value();
80 }
81 
82 // Checks that a public key generaed by the SignKeyManager is considered valid.
TEST(RawJwtEcdsaVerifyKeyManagerTest,PublicKeyValid)83 TEST(RawJwtEcdsaVerifyKeyManagerTest, PublicKeyValid) {
84   JwtEcdsaPublicKey key = CreateValidPublicKey();
85   EXPECT_THAT(RawJwtEcdsaVerifyKeyManager().ValidateKey(key), IsOk());
86 }
87 
TEST(EcdsaSignKeyManagerTest,ValidateKeyUnknownAlgorithm)88 TEST(EcdsaSignKeyManagerTest, ValidateKeyUnknownAlgorithm) {
89   JwtEcdsaPublicKey key = CreateValidPublicKey();
90   key.set_algorithm(JwtEcdsaAlgorithm::ES_UNKNOWN);
91   EXPECT_THAT(RawJwtEcdsaVerifyKeyManager().ValidateKey(key), Not(IsOk()));
92   EXPECT_THAT(RawJwtEcdsaVerifyKeyManager().ValidateKey(key),
93               StatusIs(absl::StatusCode::kInvalidArgument));
94 }
95 
96 
TEST(EcdsaSignKeyManagerTest,Create)97 TEST(EcdsaSignKeyManagerTest, Create) {
98   JwtEcdsaPrivateKey private_key = CreateValidEs256PrivateKey();
99   JwtEcdsaPublicKey public_key =
100       RawJwtEcdsaSignKeyManager().GetPublicKey(private_key).value();
101 
102   internal::EcKey ec_key;
103   ec_key.curve = Enums::ProtoToSubtle(EllipticCurveType::NIST_P256);
104   ec_key.pub_x = public_key.x();
105   ec_key.pub_y = public_key.y();
106   ec_key.priv = util::SecretDataFromStringView(private_key.key_value());
107 
108   util::StatusOr<std::unique_ptr<subtle::EcdsaSignBoringSsl>> direct_signer =
109       subtle::EcdsaSignBoringSsl::New(
110           ec_key, Enums::ProtoToSubtle(HashType::SHA256),
111           subtle::EcdsaSignatureEncoding::IEEE_P1363);
112   ASSERT_THAT(direct_signer, IsOk());
113 
114   util::StatusOr<std::unique_ptr<PublicKeyVerify>> verifier =
115       RawJwtEcdsaVerifyKeyManager().GetPrimitive<PublicKeyVerify>(public_key);
116   ASSERT_THAT(verifier, IsOk());
117 
118   std::string message = "Some message";
119   util::StatusOr<std::string> sig = (*direct_signer)->Sign(message);
120   ASSERT_THAT(sig, IsOk());
121   EXPECT_THAT((*verifier)->Verify(*sig, message), IsOk());
122 }
123 
TEST(EcdsaSignKeyManagerTest,CreateDifferentPrivateKey)124 TEST(EcdsaSignKeyManagerTest, CreateDifferentPrivateKey) {
125   JwtEcdsaPrivateKey private_key = CreateValidEs256PrivateKey();
126   // Note: we create a new key in the next line.
127   util::StatusOr<JwtEcdsaPublicKey> public_key =
128       RawJwtEcdsaSignKeyManager().GetPublicKey(CreateValidEs256PrivateKey());
129 
130   internal::EcKey ec_key;
131   ec_key.curve = Enums::ProtoToSubtle(EllipticCurveType::NIST_P256);
132   ec_key.pub_x = public_key->x();
133   ec_key.pub_y = public_key->y();
134   ec_key.priv = util::SecretDataFromStringView(private_key.key_value());
135 
136   util::StatusOr<std::unique_ptr<subtle::EcdsaSignBoringSsl>> direct_signer =
137       subtle::EcdsaSignBoringSsl::New(
138           ec_key, Enums::ProtoToSubtle(HashType::SHA256),
139           subtle::EcdsaSignatureEncoding::IEEE_P1363);
140   ASSERT_THAT(direct_signer, IsOk());
141 
142   util::StatusOr<std::unique_ptr<PublicKeyVerify>> verifier =
143       RawJwtEcdsaVerifyKeyManager().GetPrimitive<PublicKeyVerify>(*public_key);
144   ASSERT_THAT(verifier, IsOk());
145 
146   std::string message = "Some message";
147   util::StatusOr<std::string> sig = (*direct_signer)->Sign(message);
148   ASSERT_THAT(sig, IsOk());
149   EXPECT_THAT((*verifier)->Verify(*sig, message), Not(IsOk()));
150 }
151 
152 }  // namespace
153 }  // namespace jwt_internal
154 }  // namespace tink
155 }  // namespace crypto
156