| #include <optional> |
| 6 | #include <string> |
| 7 | #include <string_view> |
| 8 | #include <vector> |
| 9 |
| 10 | #include <openssl/pki/signature_verify_cache.h> |
| 11 | #include <openssl/pki/verify_error.h> |
| 12 |
| 13 | BSSL_NAMESPACE_BEGIN |
| 14 |
| 15 | class CertIssuerSourceStatic; |
| 16 | class TrustStoreInMemory; |
| 17 | class CertificateVerifyOptions; |
| 18 | class CertificateVerifyStatus; |
| 19 |
| 20 | class OPENSSL_EXPORT VerifyTrustStore { |
| 21 | public: |
| 22 | std::unique_ptr<TrustStoreInMemory> trust_store; |
| 23 |
| 24 | ~VerifyTrustStore(); |
| 25 |
| 26 | // FromDER returns a |TrustStore| derived from interpreting the |der_certs| as |
| 27 | // a bunch of DER-encoded certs, concatenated. In the event of a failure nullptr |
| 28 | // e is returned and a diagnostic string is placed in |out_diagnostic| |
| 29 | static std::unique_ptr<VerifyTrustStore> FromDER( |
| 30 | std::string_view der_certs, std::string *out_diagnostic); |
| 31 |
| 32 | // FromDER returns a |TrustStore| consisting of the supplied DER-encoded |
| 33 | // certs in |der_certs|. In the event of a failure nullptr is returned and a |
| 34 | // diagnostic string is placed in |out_diagnostic| |
| 35 | static std::unique_ptr<VerifyTrustStore> FromDER( |
| 36 | const std::vector<std::string_view> &der_certs, |
| 37 | std::string *out_diagnostic); |
| 38 | }; |
| 39 |
| 40 | class OPENSSL_EXPORT CertPool { |
| 41 | public: |
| 42 | CertPool(); |
| 43 | CertPool(const CertPool &) = delete; |
| 44 | CertPool &operator=(const CertPool &) = delete; |
| 45 | virtual ~CertPool(); |
| 46 |
| 47 | // FromCerts returns a |CertPool| consisting of the supplied DER-encoded |
| 48 | // certs in |der_certs|. In the event of a failure nullptr is returned and a |
| 49 | // diagnostic string is placed in |out_diagnostic| |
| 50 | static std::unique_ptr<CertPool> FromCerts( |
| 51 | const std::vector<std::string_view> &der_certs, |
| 52 | std::string *out_diagnostic); |
| 53 |
| 54 | private: |
| 55 | friend std::optional<std::vector<std::vector<std::string>>> |
| 56 | CertificateVerifyInternal(const CertificateVerifyOptions &opts, |
| 57 | VerifyError *out_error, |
| 58 | CertificateVerifyStatus *out_status, |
| 59 | bool all_paths); |
| 60 | std::unique_ptr<CertIssuerSourceStatic> impl_; |
| 61 | }; |
| 62 |
| 63 | // CertificateVerifyOptions contains all the options for a certificate verification. |
| 64 | class OPENSSL_EXPORT CertificateVerifyOptions { |
| 65 | public: |
| 66 | // The key purpose (extended key usage) to check for during verification. |
| 67 | enum class KeyPurpose { |
| 68 | ANY_EKU, |
| 69 | SERVER_AUTH, |
| 70 | CLIENT_AUTH, |
| 71 | SERVER_AUTH_STRICT, |
| 72 | CLIENT_AUTH_STRICT, |
| 73 | SERVER_AUTH_STRICT_LEAF, |
| 74 | CLIENT_AUTH_STRICT_LEAF, |
| 75 | RCS_MLS_CLIENT_AUTH, |
| 76 | }; |
| 77 |
| 78 | CertificateVerifyOptions(); |
| 79 | CertificateVerifyOptions(const CertificateVerifyOptions &) = delete; |
| 80 | CertificateVerifyOptions &operator=(const CertificateVerifyOptions &) = |
| 81 | delete; |
| 82 |
| 83 | KeyPurpose key_purpose = KeyPurpose::SERVER_AUTH; |
| 84 | std::string_view leaf_cert; |
| 85 | std::vector<std::string_view> intermediates; |
| 86 |
| 87 | // extra_intermediates optionally points to a pool of common intermediates. |
| 88 | const CertPool *extra_intermediates = nullptr; |
| 89 | // trust_store points to the set of root certificates to trust. |
| 90 | const VerifyTrustStore *trust_store = nullptr; |
| 91 | // min_rsa_modulus_length is the minimum acceptable RSA key size in a chain. |
| 92 | size_t min_rsa_modulus_length = 1024; |
| 93 | // time is the time in POSIX seconds since the POSIX epoch at which to |
| 94 | // validate the chain. It defaults to the current time if not set. |
| 95 | std::optional<int64_t> time; |
| 96 | // insecurely_allow_sha1 allows verification of signatures that use SHA-1 |
| 97 | // message digests. This option is insecure and should not be used. |
| 98 | bool insecurely_allow_sha1 = false; |
| 99 |
| 100 | // max_iteration_count, if not zero, limits the number of times path building |
| 101 | // will try to append an intermediate to a potential path. This bounds the |
| 102 | // amount of time that a verification attempt can take, at the risk of |
| 103 | // rejecting cases that would be solved if only more effort were used. |
| 104 | uint32_t max_iteration_count = 0; |
| 105 |
| 106 | // Sets an optional deadline for completing path building. It defaults |
| 107 | // to std::chrono::time_point::max() if it not set. If |deadline| has a |
| 108 | // value that has passed based on comparison to |
| 109 | // std::chrono::steady_clock::now(), and path building has not completed, |
| 110 | // path building will stop. Note that this is not a hard limit, there is no |
| 111 | // guarantee how far past |deadline| time will be when path building is |
| 112 | // aborted. |
| 113 | std::optional<std::chrono::time_point<std::chrono::steady_clock>> deadline; |
| 114 |
| 115 | // max_path_building_depth, if not zero, limits the depth of the path that the |
| 116 | // path building algorithm attempts to build between leafs and roots. Using |
| 117 | // this comes at the risk of rejecting cases that would be solved if only one |
| 118 | // more certificate is added to the path. |
| 119 | uint32_t max_path_building_depth = 0; |
| 120 |
| 121 | // signature_verify_cache, if not nullptr, points to an object implementing a |
| 122 | // signature verification cache derived from |
| 123 | // <openssl/pki/signature_verify_cache.h> |
| 124 | SignatureVerifyCache *signature_verify_cache = nullptr; |
| 125 | }; |
| 126 |
| 127 | // CertificateVerifyStatus describes the status of a certificate verification |
| 128 | // attempt. |
| 129 | class OPENSSL_EXPORT CertificateVerifyStatus { |
| 130 | public: |
| 131 | CertificateVerifyStatus(); |
| 132 |
| 133 | // IterationCount returns the total number of attempted certificate additions |
| 134 | // to any potential path while performing path building for verification. It |
| 135 | // is the same value which may be bound by max_iteration_count in |
| 136 | // CertificateVerifyOptions. |
| 137 | size_t IterationCount() const; |
| 138 |
| 139 | // MaxDepthSeen returns the maximum path depth seen during path building. |
| 140 | size_t MaxDepthSeen() const; |
| 141 |
| 142 | private: |
| 143 | friend std::optional<std::vector<std::vector<std::string>>> |
| 144 | CertificateVerifyInternal(const CertificateVerifyOptions &opts, |
| 145 | VerifyError *out_error, |
| 146 | CertificateVerifyStatus *out_status, |
| 147 | bool all_paths); |
| 148 | size_t iteration_count_ = 0; |
| 149 | size_t max_depth_seen_ = 0; |
| 150 | }; |
| 151 |
| 152 | // Verify verifies |opts.leaf_cert| using the other values in |opts|. It |
| 153 | // returns either an error, or else a validated chain from leaf to root. |
| 154 | // |
| 155 | // In the event of an error return, |out_error| will be updated with information |
| 156 | // about the error. It may be |nullptr|. |
| 157 | // |
| 158 | // Status information about the verification will be returned in |out_status|. |
| 159 | // It may be |nullptr|. |
| 160 | OPENSSL_EXPORT std::optional<std::vector<std::string>> CertificateVerify( |
| 161 | const CertificateVerifyOptions &opts, VerifyError *out_error = nullptr, |
| 162 | CertificateVerifyStatus *out_status = nullptr); |
| 163 |
| 164 | // VerifyAllPaths verifies |opts.leaf_cert| using the other values in |opts|, |
| 165 | // and returns all possible valid chains from the leaf to a root. If no chains |
| 166 | // exist, it returns an error. |
| 167 | OPENSSL_EXPORT std::optional<std::vector<std::vector<std::string>>> |
| 168 | CertificateVerifyAllPaths(const CertificateVerifyOptions &opts); |
| 169 |
| 170 | BSSL_NAMESPACE_END |
| 171 |
| 172 | #endif // BSSL_VERIFY_H_ |
| 173 |