1 // Copyright 2017 Google Inc.
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/signature/public_key_verify_factory.h"
18
19 #include <string>
20 #include <utility>
21
22 #include "gtest/gtest.h"
23 #include "tink/crypto_format.h"
24 #include "tink/keyset_handle.h"
25 #include "tink/public_key_verify.h"
26 #include "tink/registry.h"
27 #include "tink/signature/ecdsa_verify_key_manager.h"
28 #include "tink/signature/signature_config.h"
29 #include "tink/util/status.h"
30 #include "tink/util/test_keyset_handle.h"
31 #include "tink/util/test_util.h"
32 #include "proto/ecdsa.pb.h"
33 #include "proto/tink.pb.h"
34
35 using crypto::tink::test::AddTinkKey;
36 using google::crypto::tink::EcdsaPublicKey;
37 using google::crypto::tink::EcdsaSignatureEncoding;
38 using google::crypto::tink::EllipticCurveType;
39 using google::crypto::tink::HashType;
40 using google::crypto::tink::KeyData;
41 using google::crypto::tink::Keyset;
42 using google::crypto::tink::KeyStatusType;
43
44 namespace crypto {
45 namespace tink {
46 namespace {
47
48 class PublicKeyVerifyFactoryTest : public ::testing::Test {
49 protected:
SetUp()50 void SetUp() override {
51 auto status = SignatureConfig::Register();
52 ASSERT_TRUE(status.ok()) << status;
53 }
54 };
55
GetNewEcdsaPublicKey()56 EcdsaPublicKey GetNewEcdsaPublicKey() {
57 auto ecdsa_key = test::GetEcdsaTestPrivateKey(EllipticCurveType::NIST_P256,
58 HashType::SHA256,
59 EcdsaSignatureEncoding::DER);
60 return ecdsa_key.public_key();
61 }
62
TEST_F(PublicKeyVerifyFactoryTest,testBasic)63 TEST_F(PublicKeyVerifyFactoryTest, testBasic) {
64 Keyset keyset;
65 auto public_key_verify_result = PublicKeyVerifyFactory::GetPrimitive(
66 *TestKeysetHandle::GetKeysetHandle(keyset));
67 EXPECT_FALSE(public_key_verify_result.ok());
68 EXPECT_EQ(absl::StatusCode::kInvalidArgument,
69 public_key_verify_result.status().code());
70 EXPECT_PRED_FORMAT2(testing::IsSubstring, "at least one key",
71 std::string(public_key_verify_result.status().message()));
72 }
73
TEST_F(PublicKeyVerifyFactoryTest,testPrimitive)74 TEST_F(PublicKeyVerifyFactoryTest, testPrimitive) {
75 // Prepare a Keyset.
76 Keyset keyset;
77 std::string key_type =
78 "type.googleapis.com/google.crypto.tink.EcdsaPublicKey";
79
80 uint32_t key_id_1 = 1234543;
81 AddTinkKey(key_type, key_id_1, GetNewEcdsaPublicKey(),
82 KeyStatusType::ENABLED, KeyData::ASYMMETRIC_PUBLIC, &keyset);
83
84 uint32_t key_id_2 = 726329;
85 AddTinkKey(key_type, key_id_2, GetNewEcdsaPublicKey(),
86 KeyStatusType::ENABLED, KeyData::ASYMMETRIC_PUBLIC, &keyset);
87
88 uint32_t key_id_3 = 7213743;
89 AddTinkKey(key_type, key_id_3, GetNewEcdsaPublicKey(),
90 KeyStatusType::ENABLED, KeyData::ASYMMETRIC_PUBLIC, &keyset);
91
92 keyset.set_primary_key_id(key_id_3);
93
94 // Create a KeysetHandle and use it with the factory.
95 auto public_key_verify_result = PublicKeyVerifyFactory::GetPrimitive(
96 *TestKeysetHandle::GetKeysetHandle(keyset));
97 EXPECT_TRUE(public_key_verify_result.ok())
98 << public_key_verify_result.status();
99 auto public_key_verify = std::move(public_key_verify_result.value());
100 }
101
102 } // namespace
103 } // namespace tink
104 } // namespace crypto
105