1 // Copyright 2017 The Chromium Authors
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 //     https://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 #include "simple_path_builder_delegate.h"
16 
17 #include <openssl/bn.h>
18 #include <openssl/bytestring.h>
19 #include <openssl/digest.h>
20 #include <openssl/ec.h>
21 #include <openssl/ec_key.h>
22 #include <openssl/evp.h>
23 #include <openssl/nid.h>
24 #include <openssl/pki/signature_verify_cache.h>
25 #include <openssl/rsa.h>
26 
27 #include "cert_error_params.h"
28 #include "cert_errors.h"
29 #include "signature_algorithm.h"
30 #include "verify_signed_data.h"
31 
32 BSSL_NAMESPACE_BEGIN
33 
34 DEFINE_CERT_ERROR_ID(SimplePathBuilderDelegate::kRsaModulusTooSmall,
35                      "RSA modulus too small");
36 
37 namespace {
38 
39 DEFINE_CERT_ERROR_ID(kUnacceptableCurveForEcdsa,
40                      "Only P-256, P-384, P-521 are supported for ECDSA");
41 
IsAcceptableCurveForEcdsa(int curve_nid)42 bool IsAcceptableCurveForEcdsa(int curve_nid) {
43   switch (curve_nid) {
44     case NID_X9_62_prime256v1:
45     case NID_secp384r1:
46     case NID_secp521r1:
47       return true;
48   }
49 
50   return false;
51 }
52 
53 }  // namespace
54 
SimplePathBuilderDelegate(size_t min_rsa_modulus_length_bits,DigestPolicy digest_policy)55 SimplePathBuilderDelegate::SimplePathBuilderDelegate(
56     size_t min_rsa_modulus_length_bits, DigestPolicy digest_policy)
57     : min_rsa_modulus_length_bits_(min_rsa_modulus_length_bits),
58       digest_policy_(digest_policy) {}
59 
CheckPathAfterVerification(const CertPathBuilder & path_builder,CertPathBuilderResultPath * path)60 void SimplePathBuilderDelegate::CheckPathAfterVerification(
61     const CertPathBuilder &path_builder, CertPathBuilderResultPath *path) {
62   // Do nothing - consider all candidate paths valid.
63 }
64 
IsDeadlineExpired()65 bool SimplePathBuilderDelegate::IsDeadlineExpired() { return false; }
66 
IsDebugLogEnabled()67 bool SimplePathBuilderDelegate::IsDebugLogEnabled() { return false; }
68 
AcceptPreCertificates()69 bool SimplePathBuilderDelegate::AcceptPreCertificates() { return false; }
70 
DebugLog(std::string_view msg)71 void SimplePathBuilderDelegate::DebugLog(std::string_view msg) {}
72 
GetVerifyCache()73 SignatureVerifyCache *SimplePathBuilderDelegate::GetVerifyCache() {
74   return nullptr;
75 }
76 
IsSignatureAlgorithmAcceptable(SignatureAlgorithm algorithm,CertErrors * errors)77 bool SimplePathBuilderDelegate::IsSignatureAlgorithmAcceptable(
78     SignatureAlgorithm algorithm, CertErrors *errors) {
79   switch (algorithm) {
80     case SignatureAlgorithm::kRsaPkcs1Sha1:
81     case SignatureAlgorithm::kEcdsaSha1:
82       return digest_policy_ == DigestPolicy::kWeakAllowSha1;
83 
84     case SignatureAlgorithm::kRsaPkcs1Sha256:
85     case SignatureAlgorithm::kRsaPkcs1Sha384:
86     case SignatureAlgorithm::kRsaPkcs1Sha512:
87     case SignatureAlgorithm::kEcdsaSha256:
88     case SignatureAlgorithm::kEcdsaSha384:
89     case SignatureAlgorithm::kEcdsaSha512:
90     case SignatureAlgorithm::kRsaPssSha256:
91     case SignatureAlgorithm::kRsaPssSha384:
92     case SignatureAlgorithm::kRsaPssSha512:
93       return true;
94   }
95   return false;
96 }
97 
IsPublicKeyAcceptable(EVP_PKEY * public_key,CertErrors * errors)98 bool SimplePathBuilderDelegate::IsPublicKeyAcceptable(EVP_PKEY *public_key,
99                                                       CertErrors *errors) {
100   int pkey_id = EVP_PKEY_id(public_key);
101   if (pkey_id == EVP_PKEY_RSA) {
102     // Extract the modulus length from the key.
103     RSA *rsa = EVP_PKEY_get0_RSA(public_key);
104     if (!rsa) {
105       return false;
106     }
107     unsigned int modulus_length_bits = RSA_bits(rsa);
108 
109     if (modulus_length_bits < min_rsa_modulus_length_bits_) {
110       errors->AddWarning(
111           kRsaModulusTooSmall,
112           CreateCertErrorParams2SizeT("actual", modulus_length_bits, "minimum",
113                                       min_rsa_modulus_length_bits_));
114       return false;
115     }
116 
117     return true;
118   }
119 
120   if (pkey_id == EVP_PKEY_EC) {
121     if (!IsAcceptableCurveForEcdsa(EVP_PKEY_get_ec_curve_nid(public_key))) {
122       errors->AddWarning(kUnacceptableCurveForEcdsa);
123       return false;
124     }
125 
126     return true;
127   }
128 
129   // Unexpected key type.
130   return false;
131 }
132 
133 BSSL_NAMESPACE_END
134