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/subtle/rsa_ssa_pss_verify_boringssl.h"
18
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include "absl/memory/memory.h"
26 #include "absl/status/status.h"
27 #include "absl/strings/str_cat.h"
28 #include "absl/strings/string_view.h"
29 #include "openssl/evp.h"
30 #include "openssl/rsa.h"
31 #include "tink/internal/err_util.h"
32 #include "tink/internal/md_util.h"
33 #include "tink/internal/rsa_util.h"
34 #include "tink/internal/ssl_unique_ptr.h"
35 #include "tink/internal/util.h"
36 #include "tink/subtle/common_enums.h"
37 #include "tink/util/errors.h"
38 #include "tink/util/status.h"
39 #include "tink/util/statusor.h"
40
41 namespace crypto {
42 namespace tink {
43 namespace subtle {
44 namespace {
45
46 // Verifies an RSA-SSA PSS signature using `rsa_public_key` over
47 // `message_digest`; `message_digest` is the digest of the original message
48 // computed with `sig_md`, `mgf1_md` is the hash function for generating the
49 // mask (if nullptr, `sig_md` is used), and `salt_length` the salt length in
50 // bytes.
51 //
52 // This function is equivalent to BoringSSL's `RSA_verify_pss_mgf1`[1], and
53 // differs from it only in that it uses `RSA_public_decrypt` instead of
54 // `RSA_sign_raw`, because the latter is not defined in OpenSSL. In BoringSSL
55 // `RSA_public_decrypt` is essentially an alias for `RSA_verify_raw` [2].
56 //
57 // OpenSSL uses the same sequence of API calls [3].
58 //
59 // [1]https://github.com/google/boringssl/blob/master/crypto/fipsmodule/rsa/rsa.c#L633
60 // [2]https://github.com/google/boringssl/blob/master/crypto/fipsmodule/rsa/rsa.c#L354
61 // [3]https://github.com/openssl/openssl/blob/master/crypto/rsa/rsa_pmeth.c#L279
SslRsaSsaPssVerify(RSA * rsa_public_key,absl::string_view signature,absl::string_view message_digest,const EVP_MD * sig_md,const EVP_MD * mgf1_md,int32_t salt_length)62 util::Status SslRsaSsaPssVerify(RSA* rsa_public_key,
63 absl::string_view signature,
64 absl::string_view message_digest,
65 const EVP_MD* sig_md, const EVP_MD* mgf1_md,
66 int32_t salt_length) {
67 const int kHashSize = EVP_MD_size(sig_md);
68 // Make sure the size of the digest is correct.
69 if (message_digest.size() != kHashSize) {
70 return util::Status(
71 absl::StatusCode::kInvalidArgument,
72 absl::StrCat("Size of the digest doesn't match the one "
73 "of the hashing algorithm; expected ",
74 kHashSize, " got ", message_digest.size()));
75 }
76 const int kRsaModulusSize = RSA_size(rsa_public_key);
77 std::vector<uint8_t> recovered_message_digest(kRsaModulusSize);
78 int recovered_message_digest_size = RSA_public_decrypt(
79 /*flen=*/signature.size(),
80 /*from=*/reinterpret_cast<const uint8_t*>(signature.data()),
81 /*to=*/recovered_message_digest.data(), /*rsa=*/rsa_public_key,
82 /*padding=*/RSA_NO_PADDING);
83 if (recovered_message_digest_size != kRsaModulusSize) {
84 internal::GetSslErrors();
85 return util::Status(
86 absl::StatusCode::kInvalidArgument,
87 absl::StrCat("Invalid signature size (likely an incorrect key is "
88 "used); expected ",
89 kRsaModulusSize, " got ", recovered_message_digest_size));
90 }
91 if (RSA_verify_PKCS1_PSS_mgf1(
92 rsa_public_key,
93 reinterpret_cast<const uint8_t*>(message_digest.data()), sig_md,
94 mgf1_md, recovered_message_digest.data(), salt_length) != 1) {
95 internal::GetSslErrors();
96 return util::Status(absl::StatusCode::kInvalidArgument,
97 "PSS padding verification failed.");
98 }
99 return util::OkStatus();
100 }
101
102 } // namespace
103
104 util::StatusOr<std::unique_ptr<RsaSsaPssVerifyBoringSsl>>
New(const internal::RsaPublicKey & pub_key,const internal::RsaSsaPssParams & params)105 RsaSsaPssVerifyBoringSsl::New(const internal::RsaPublicKey& pub_key,
106 const internal::RsaSsaPssParams& params) {
107 util::Status res =
108 internal::CheckFipsCompatibility<RsaSsaPssVerifyBoringSsl>();
109 if (!res.ok()) {
110 return res;
111 }
112
113 // Check if the hash type is safe to use.
114 util::Status is_safe = internal::IsHashTypeSafeForSignature(params.sig_hash);
115 if (!is_safe.ok()) {
116 return is_safe;
117 }
118 util::StatusOr<const EVP_MD*> sig_hash =
119 internal::EvpHashFromHashType(params.sig_hash);
120 if (!sig_hash.ok()) {
121 return sig_hash.status();
122 }
123
124 // TODO(quannguyen): check mgf1_hash function and salt length.
125 util::StatusOr<const EVP_MD*> mgf1_hash =
126 internal::EvpHashFromHashType(params.mgf1_hash);
127 if (!mgf1_hash.ok()) {
128 return mgf1_hash.status();
129 }
130
131 // The RSA modulus and exponent are checked as part of the conversion to
132 // internal::SslUniquePtr<RSA>.
133 util::StatusOr<internal::SslUniquePtr<RSA>> rsa =
134 internal::RsaPublicKeyToRsa(pub_key);
135 if (!rsa.ok()) {
136 return rsa.status();
137 }
138
139 return {absl::WrapUnique(new RsaSsaPssVerifyBoringSsl(
140 *std::move(rsa), *sig_hash, *mgf1_hash, params.salt_length))};
141 }
142
Verify(absl::string_view signature,absl::string_view data) const143 util::Status RsaSsaPssVerifyBoringSsl::Verify(absl::string_view signature,
144 absl::string_view data) const {
145 // BoringSSL expects a non-null pointer for data,
146 // regardless of whether the size is 0.
147 data = internal::EnsureStringNonNull(data);
148 util::StatusOr<std::string> digest = internal::ComputeHash(data, *sig_hash_);
149 if (!digest.ok()) {
150 return digest.status();
151 }
152 return SslRsaSsaPssVerify(rsa_.get(), signature, *digest, sig_hash_,
153 mgf1_hash_, salt_length_);
154 }
155
156 } // namespace subtle
157 } // namespace tink
158 } // namespace crypto
159