1 // Copyright 2018 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/rsa_ssa_pkcs1_sign_key_manager.h"
18
19 #include <memory>
20 #include <string>
21
22 #include "absl/memory/memory.h"
23 #include "absl/status/status.h"
24 #include "absl/strings/string_view.h"
25 #include "tink/internal/bn_util.h"
26 #include "tink/internal/rsa_util.h"
27 #include "tink/internal/ssl_unique_ptr.h"
28 #include "tink/public_key_sign.h"
29 #include "tink/public_key_verify.h"
30 #include "tink/signature/rsa_ssa_pkcs1_verify_key_manager.h"
31 #include "tink/signature/sig_util.h"
32 #include "tink/subtle/rsa_ssa_pkcs1_sign_boringssl.h"
33 #include "tink/util/enums.h"
34 #include "tink/util/errors.h"
35 #include "tink/util/protobuf_helper.h"
36 #include "tink/util/secret_data.h"
37 #include "tink/util/status.h"
38 #include "tink/util/statusor.h"
39 #include "tink/util/validation.h"
40 #include "proto/rsa_ssa_pkcs1.pb.h"
41
42 namespace crypto {
43 namespace tink {
44
45 using crypto::tink::util::Enums;
46 using crypto::tink::util::Status;
47 using crypto::tink::util::StatusOr;
48 using google::crypto::tink::RsaSsaPkcs1KeyFormat;
49 using google::crypto::tink::RsaSsaPkcs1Params;
50 using google::crypto::tink::RsaSsaPkcs1PrivateKey;
51
52 namespace {
RsaPrivateKeySubtleToProto(const internal::RsaPrivateKey & private_key)53 RsaSsaPkcs1PrivateKey RsaPrivateKeySubtleToProto(
54 const internal::RsaPrivateKey& private_key) {
55 RsaSsaPkcs1PrivateKey key_proto;
56 key_proto.set_version(RsaSsaPkcs1SignKeyManager().get_version());
57 key_proto.set_d(std::string(util::SecretDataAsStringView(private_key.d)));
58 key_proto.set_p(std::string(util::SecretDataAsStringView(private_key.p)));
59 key_proto.set_q(std::string(util::SecretDataAsStringView(private_key.q)));
60 key_proto.set_dp(std::string(util::SecretDataAsStringView(private_key.dp)));
61 key_proto.set_dq(std::string(util::SecretDataAsStringView(private_key.dq)));
62 key_proto.set_crt(std::string(util::SecretDataAsStringView(private_key.crt)));
63 auto* public_key_proto = key_proto.mutable_public_key();
64 public_key_proto->set_version(RsaSsaPkcs1SignKeyManager().get_version());
65 public_key_proto->set_n(private_key.n);
66 public_key_proto->set_e(private_key.e);
67 return key_proto;
68 }
69
RsaPrivateKeyProtoToSubtle(const RsaSsaPkcs1PrivateKey & key_proto)70 internal::RsaPrivateKey RsaPrivateKeyProtoToSubtle(
71 const RsaSsaPkcs1PrivateKey& key_proto) {
72 internal::RsaPrivateKey key;
73 key.n = key_proto.public_key().n();
74 key.e = key_proto.public_key().e();
75 key.d = util::SecretDataFromStringView(key_proto.d());
76 key.p = util::SecretDataFromStringView(key_proto.p());
77 key.q = util::SecretDataFromStringView(key_proto.q());
78 key.dp = util::SecretDataFromStringView(key_proto.dp());
79 key.dq = util::SecretDataFromStringView(key_proto.dq());
80 key.crt = util::SecretDataFromStringView(key_proto.crt());
81 return key;
82 }
83
84 } // namespace
85
CreateKey(const RsaSsaPkcs1KeyFormat & rsa_ssa_pkcs1_key_format) const86 StatusOr<RsaSsaPkcs1PrivateKey> RsaSsaPkcs1SignKeyManager::CreateKey(
87 const RsaSsaPkcs1KeyFormat& rsa_ssa_pkcs1_key_format) const {
88 StatusOr<internal::SslUniquePtr<BIGNUM>> e =
89 internal::StringToBignum(rsa_ssa_pkcs1_key_format.public_exponent());
90 if (!e.ok()) {
91 return e.status();
92 }
93
94 internal::RsaPrivateKey private_key;
95 internal::RsaPublicKey public_key;
96 util::Status status =
97 internal::NewRsaKeyPair(rsa_ssa_pkcs1_key_format.modulus_size_in_bits(),
98 e->get(), &private_key, &public_key);
99 if (!status.ok()) {
100 return status;
101 }
102
103 RsaSsaPkcs1PrivateKey key_proto = RsaPrivateKeySubtleToProto(private_key);
104 auto* public_key_proto = key_proto.mutable_public_key();
105 *public_key_proto->mutable_params() = rsa_ssa_pkcs1_key_format.params();
106
107 return key_proto;
108 }
109
110 StatusOr<std::unique_ptr<PublicKeySign>>
Create(const RsaSsaPkcs1PrivateKey & private_key) const111 RsaSsaPkcs1SignKeyManager::PublicKeySignFactory::Create(
112 const RsaSsaPkcs1PrivateKey& private_key) const {
113 auto key = RsaPrivateKeyProtoToSubtle(private_key);
114 internal::RsaSsaPkcs1Params params;
115 const RsaSsaPkcs1Params& params_proto = private_key.public_key().params();
116 params.hash_type = Enums::ProtoToSubtle(params_proto.hash_type());
117 auto signer = subtle::RsaSsaPkcs1SignBoringSsl::New(key, params);
118 if (!signer.ok()) return signer.status();
119 // To check that the key is correct, we sign a test message with private key
120 // and verify with public key.
121 auto verifier = RsaSsaPkcs1VerifyKeyManager().GetPrimitive<PublicKeyVerify>(
122 private_key.public_key());
123 if (!verifier.ok()) return verifier.status();
124 auto sign_verify_result =
125 SignAndVerify(signer.value().get(), verifier.value().get());
126 if (!sign_verify_result.ok()) {
127 return util::Status(absl::StatusCode::kInternal,
128 "security bug: signing with private key followed by "
129 "verifying with public key failed");
130 }
131 return signer;
132 }
133
ValidateKey(const RsaSsaPkcs1PrivateKey & key) const134 Status RsaSsaPkcs1SignKeyManager::ValidateKey(
135 const RsaSsaPkcs1PrivateKey& key) const {
136 Status status = ValidateVersion(key.version(), get_version());
137 if (!status.ok()) return status;
138 return RsaSsaPkcs1VerifyKeyManager().ValidateKey(key.public_key());
139 }
140
ValidateKeyFormat(const RsaSsaPkcs1KeyFormat & key_format) const141 Status RsaSsaPkcs1SignKeyManager::ValidateKeyFormat(
142 const RsaSsaPkcs1KeyFormat& key_format) const {
143 Status modulus_status =
144 internal::ValidateRsaModulusSize(key_format.modulus_size_in_bits());
145 if (!modulus_status.ok()) {
146 return modulus_status;
147 }
148 Status exponent_status =
149 internal::ValidateRsaPublicExponent(key_format.public_exponent());
150 if (!exponent_status.ok()) {
151 return exponent_status;
152 }
153 return RsaSsaPkcs1VerifyKeyManager().ValidateParams(key_format.params());
154 }
155
156 } // namespace tink
157 } // namespace crypto
158