| 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 | #ifndef BSSL_PKI_PARSED_CERTIFICATE_H_ |
| 16 | #define BSSL_PKI_PARSED_CERTIFICATE_H_ |
| 17 |
| 18 | #include <map> |
| 19 | #include <memory> |
| 20 | #include <optional> |
| 21 | #include <vector> |
| 22 |
| 23 | #include <openssl/base.h> |
| 24 |
| 25 | #include "certificate_policies.h" |
| 26 | #include "input.h" |
| 27 | #include "parse_certificate.h" |
| 28 | #include "signature_algorithm.h" |
| 29 |
| 30 | BSSL_NAMESPACE_BEGIN |
| 31 |
| 32 | struct GeneralNames; |
| 33 | class NameConstraints; |
| 34 | class ParsedCertificate; |
| 35 | class CertErrors; |
| 36 |
| 37 | using ParsedCertificateList = |
| 38 | std::vector<std::shared_ptr<const ParsedCertificate>>; |
| 39 |
| 40 | // Represents an X.509 certificate, including Certificate, TBSCertificate, and |
| 41 | // standard extensions. |
| 42 | // Creating a ParsedCertificate does not completely parse and validate the |
| 43 | // certificate data. Presence of a member in this class implies the DER was |
| 44 | // parsed successfully to that level, but does not imply the contents of that |
| 45 | // member are valid, unless otherwise specified. See the documentation for each |
| 46 | // member or the documentation of the type it returns. |
| 47 | class OPENSSL_EXPORT ParsedCertificate { |
| 48 | private: |
| 49 | // Used to make constructors private while still being compatible with |
| 50 | // |std::make_shared|. |
| 51 | class PrivateConstructor { |
| 52 | private: |
| 53 | friend ParsedCertificate; |
| 54 | PrivateConstructor() = default; |
| 55 | }; |
| 56 |
| 57 | public: |
| 58 | ~ParsedCertificate(); |
| 59 | // Map from OID to ParsedExtension. |
| 60 | using ExtensionsMap = std::map<der::Input, ParsedExtension>; |
| 61 |
| 62 | // Creates a ParsedCertificate given a DER-encoded Certificate. Returns |
| 63 | // nullptr on failure. Failure will occur if the standard certificate fields |
| 64 | // and supported extensions cannot be parsed. |
| 65 | // On either success or failure, if |errors| is non-null it may have error |
| 66 | // information added to it. |
| 67 | static std::shared_ptr<const ParsedCertificate> Create( |
| 68 | bssl::UniquePtr<CRYPTO_BUFFER> cert_data, |
| 69 | const ParseCertificateOptions &options, CertErrors *errors); |
| 70 |
| 71 | // Creates a ParsedCertificate by copying the provided |data|, and appends it |
| 72 | // to |chain|. Returns true if the certificate was successfully parsed and |
| 73 | // added. If false is return, |chain| is unmodified. |
| 74 | // |
| 75 | // On either success or failure, if |errors| is non-null it may have error |
| 76 | // information added to it. |
| 77 | static bool CreateAndAddToVector( |
| 78 | bssl::UniquePtr<CRYPTO_BUFFER> cert_data, |
| 79 | const ParseCertificateOptions &options, |
| 80 | std::vector<std::shared_ptr<const bssl::ParsedCertificate>> *chain, |
| 81 | CertErrors *errors); |
| 82 |
| 83 | explicit ParsedCertificate(PrivateConstructor); |
| 84 |
| 85 | ParsedCertificate(const ParsedCertificate &) = delete; |
| 86 | ParsedCertificate &operator=(const ParsedCertificate &) = delete; |
| 87 |
| 88 | // Returns the DER-encoded certificate data for this cert. |
| 89 | der::Input der_cert() const { return cert_; } |
| 90 |
| 91 | // Returns the CRYPTO_BUFFER backing this object. |
| 92 | CRYPTO_BUFFER *cert_buffer() const { return cert_data_.get(); } |
| 93 |
| 94 | // Accessors for raw fields of the Certificate. |
| 95 | der::Input tbs_certificate_tlv() const { return tbs_certificate_tlv_; } |
| 96 |
| 97 | der::Input signature_algorithm_tlv() const { |
| 98 | return signature_algorithm_tlv_; |
| 99 | } |
| 100 |
| 101 | const der::BitString &signature_value() const { return signature_value_; } |
| 102 |
| 103 | // Accessor for struct containing raw fields of the TbsCertificate. |
| 104 | const ParsedTbsCertificate &tbs() const { return tbs_; } |
| 105 |
| 106 | // Returns the signatureAlgorithm of the Certificate (not the tbsCertificate). |
| 107 | // If the signature algorithm is unknown/unsupported, this returns nullopt. |
| 108 | std::optional<SignatureAlgorithm> signature_algorithm() const { |
| 109 | return signature_algorithm_; |
| 110 | } |
| 111 |
| 112 | // Returns the DER-encoded raw subject value (including the outer sequence |
| 113 | // tag). This is guaranteed to be valid DER, though the contents of unhandled |
| 114 | // string types are treated as raw bytes. |
| 115 | der::Input subject_tlv() const { return tbs_.subject_tlv; } |
| 116 | // Returns the DER-encoded normalized subject value (not including outer |
| 117 | // Sequence tag). This is guaranteed to be valid DER, though the contents of |
| 118 | // unhandled string types are treated as raw bytes. |
| 119 | der::Input normalized_subject() const { |
| 120 | return der::Input(normalized_subject_); |
| 121 | } |
| 122 | // Returns the DER-encoded raw issuer value (including the outer sequence |
| 123 | // tag). This is guaranteed to be valid DER, though the contents of unhandled |
| 124 | // string types are treated as raw bytes. |
| 125 | der::Input issuer_tlv() const { return tbs_.issuer_tlv; } |
| 126 | // Returns the DER-encoded normalized issuer value (not including outer |
| 127 | // Sequence tag). This is guaranteed to be valid DER, though the contents of |
| 128 | // unhandled string types are treated as raw bytes. |
| 129 | der::Input normalized_issuer() const { |
| 130 | return der::Input(normalized_issuer_); |
| 131 | } |
| 132 |
| 133 | // Returns true if the certificate has a BasicConstraints extension. |
| 134 | bool has_basic_constraints() const { return has_basic_constraints_; } |
| 135 |
| 136 | // Returns the ParsedBasicConstraints struct. Caller must check |
| 137 | // has_basic_constraints() before accessing this. |
| 138 | const ParsedBasicConstraints &basic_constraints() const { |
| 139 | BSSL_CHECK(has_basic_constraints_); |
| 140 | return basic_constraints_; |
| 141 | } |
| 142 |
| 143 | // Returns true if the certificate has a KeyUsage extension. |
| 144 | bool has_key_usage() const { return has_key_usage_; } |
| 145 |
| 146 | // Returns the KeyUsage BitString. Caller must check |
| 147 | // has_key_usage() before accessing this. |
| 148 | const der::BitString &key_usage() const { |
| 149 | BSSL_CHECK(has_key_usage_); |
| 150 | return key_usage_; |
| 151 | } |
| 152 |
| 153 | // Returns true if the certificate has a ExtendedKeyUsage extension. |
| 154 | bool has_extended_key_usage() const { return has_extended_key_usage_; } |
| 155 |
| 156 | // Returns the ExtendedKeyUsage key purpose OIDs. Caller must check |
| 157 | // has_extended_key_usage() before accessing this. |
| 158 | const std::vector<der::Input> &extended_key_usage() const { |
| 159 | BSSL_CHECK(has_extended_key_usage_); |
| 160 | return extended_key_usage_; |
| 161 | } |
| 162 |
| 163 | // Returns true if the certificate has a SubjectAltName extension. |
| 164 | bool has_subject_alt_names() const { return subject_alt_names_ != nullptr; } |
| 165 |
| 166 | // Returns the ParsedExtension struct for the SubjectAltName extension. |
| 167 | // If the cert did not have a SubjectAltName extension, this will be a |
| 168 | // default-initialized ParsedExtension struct. |
| 169 | const ParsedExtension &subject_alt_names_extension() const { |
| 170 | return subject_alt_names_extension_; |
| 171 | } |
| 172 |
| 173 | // Returns the GeneralNames class parsed from SubjectAltName extension, or |
| 174 | // nullptr if no SubjectAltName extension was present. |
| 175 | const GeneralNames *subject_alt_names() const { |
| 176 | return subject_alt_names_.get(); |
| 177 | } |
| 178 |
| 179 | // Returns true if the certificate has a NameConstraints extension. |
| 180 | bool has_name_constraints() const { return name_constraints_ != nullptr; } |
| 181 |
| 182 | // Returns the parsed NameConstraints extension. Must not be called if |
| 183 | // has_name_constraints() is false. |
| 184 | const NameConstraints &name_constraints() const { |
| 185 | BSSL_CHECK(name_constraints_); |
| 186 | return *name_constraints_; |
| 187 | } |
| 188 |
| 189 | // Returns true if the certificate has an AuthorityInfoAccess extension. |
| 190 | bool has_authority_info_access() const { return has_authority_info_access_; } |
| 191 |
| 192 | // Returns the ParsedExtension struct for the AuthorityInfoAccess extension. |
| 193 | const ParsedExtension &authority_info_access_extension() const { |
| 194 | return authority_info_access_extension_; |
| 195 | } |
| 196 |
| 197 | // Returns any caIssuers URIs from the AuthorityInfoAccess extension. |
| 198 | const std::vector<std::string_view> &ca_issuers_uris() const { |
| 199 | return ca_issuers_uris_; |
| 200 | } |
| 201 |
| 202 | // Returns any OCSP URIs from the AuthorityInfoAccess extension. |
| 203 | const std::vector<std::string_view> &ocsp_uris() const { return ocsp_uris_; } |
| 204 |
| 205 | // Returns true if the certificate has a Policies extension. |
| 206 | bool has_policy_oids() const { return has_policy_oids_; } |
| 207 |
| 208 | // Returns the policy OIDs. Caller must check has_policy_oids() before |
| 209 | // accessing this. |
| 210 | const std::vector<der::Input> &policy_oids() const { |
| 211 | BSSL_CHECK(has_policy_oids()); |
| 212 | return policy_oids_; |
| 213 | } |
| 214 |
| 215 | // Returns true if the certificate has a PolicyConstraints extension. |
| 216 | bool has_policy_constraints() const { return has_policy_constraints_; } |
| 217 |
| 218 | // Returns the ParsedPolicyConstraints struct. Caller must check |
| 219 | // has_policy_constraints() before accessing this. |
| 220 | const ParsedPolicyConstraints &policy_constraints() const { |
| 221 | BSSL_CHECK(has_policy_constraints_); |
| 222 | return policy_constraints_; |
| 223 | } |
| 224 |
| 225 | // Returns true if the certificate has a PolicyMappings extension. |
| 226 | bool has_policy_mappings() const { return has_policy_mappings_; } |
| 227 |
| 228 | // Returns the PolicyMappings extension. Caller must check |
| 229 | // has_policy_mappings() before accessing this. |
| 230 | const std::vector<ParsedPolicyMapping> &policy_mappings() const { |
| 231 | BSSL_CHECK(has_policy_mappings_); |
| 232 | return policy_mappings_; |
| 233 | } |
| 234 |
| 235 | // Returns the Inhibit Any Policy extension. |
| 236 | const std::optional<uint8_t> &inhibit_any_policy() const { |
| 237 | return inhibit_any_policy_; |
| 238 | } |
| 239 |
| 240 | // Returns the AuthorityKeyIdentifier extension, or nullopt if there wasn't |
| 241 | // one. |
| 242 | const std::optional<ParsedAuthorityKeyIdentifier> &authority_key_identifier() |
| 243 | const { |
| 244 | return authority_key_identifier_; |
| 245 | } |
| 246 |
| 247 | // Returns the SubjectKeyIdentifier extension, or nullopt if there wasn't |
| 248 | // one. |
| 249 | const std::optional<der::Input> &subject_key_identifier() const { |
| 250 | return subject_key_identifier_; |
| 251 | } |
| 252 |
| 253 | // Returns a map of all the extensions in the certificate. |
| 254 | const ExtensionsMap &extensions() const { return extensions_; } |
| 255 |
| 256 | // Gets the value for extension matching |extension_oid|. Returns false if the |
| 257 | // extension is not present. |
| 258 | bool GetExtension(der::Input extension_oid, |
| 259 | ParsedExtension *parsed_extension) const; |
| 260 |
| 261 | private: |
| 262 | // The backing store for the certificate data. |
| 263 | bssl::UniquePtr<CRYPTO_BUFFER> cert_data_; |
| 264 |
| 265 | // Points to the raw certificate DER. |
| 266 | der::Input cert_; |
| 267 |
| 268 | der::Input tbs_certificate_tlv_; |
| 269 | der::Input signature_algorithm_tlv_; |
| 270 | der::BitString signature_value_; |
| 271 | ParsedTbsCertificate tbs_; |
| 272 |
| 273 | // The signatureAlgorithm from the Certificate. |
| 274 | std::optional<SignatureAlgorithm> signature_algorithm_; |
| 275 |
| 276 | // Normalized DER-encoded Subject (not including outer Sequence tag). |
| 277 | std::string normalized_subject_; |
| 278 | // Normalized DER-encoded Issuer (not including outer Sequence tag). |
| 279 | std::string normalized_issuer_; |
| 280 |
| 281 | // BasicConstraints extension. |
| 282 | bool has_basic_constraints_ = false; |
| 283 | ParsedBasicConstraints basic_constraints_; |
| 284 |
| 285 | // KeyUsage extension. |
| 286 | bool has_key_usage_ = false; |
| 287 | der::BitString key_usage_; |
| 288 |
| 289 | // ExtendedKeyUsage extension. |
| 290 | bool has_extended_key_usage_ = false; |
| 291 | std::vector<der::Input> extended_key_usage_; |
| 292 |
| 293 | // Raw SubjectAltName extension. |
| 294 | ParsedExtension subject_alt_names_extension_; |
| 295 | // Parsed SubjectAltName extension. |
| 296 | std::unique_ptr<GeneralNames> subject_alt_names_; |
| 297 |
| 298 | // NameConstraints extension. |
| 299 | std::unique_ptr<NameConstraints> name_constraints_; |
| 300 |
| 301 | // AuthorityInfoAccess extension. |
| 302 | bool has_authority_info_access_ = false; |
| 303 | ParsedExtension authority_info_access_extension_; |
| 304 | // CaIssuers and Ocsp URIs parsed from the AuthorityInfoAccess extension. Note |
| 305 | // that the AuthorityInfoAccess may have contained other AccessDescriptions |
| 306 | // which are not represented here. |
| 307 | std::vector<std::string_view> ca_issuers_uris_; |
| 308 | std::vector<std::string_view> ocsp_uris_; |
| 309 |
| 310 | // Policies extension. This list will already have been checked for |
| 311 | // duplicates. |
| 312 | bool has_policy_oids_ = false; |
| 313 | std::vector<der::Input> policy_oids_; |
| 314 |
| 315 | // Policy constraints extension. |
| 316 | bool has_policy_constraints_ = false; |
| 317 | ParsedPolicyConstraints policy_constraints_; |
| 318 |
| 319 | // Policy mappings extension. |
| 320 | bool has_policy_mappings_ = false; |
| 321 | std::vector<ParsedPolicyMapping> policy_mappings_; |
| 322 |
| 323 | // Inhibit Any Policy extension. |
| 324 | std::optional<uint8_t> inhibit_any_policy_; |
| 325 |
| 326 | // AuthorityKeyIdentifier extension. |
| 327 | std::optional<ParsedAuthorityKeyIdentifier> authority_key_identifier_; |
| 328 |
| 329 | // SubjectKeyIdentifier extension. |
| 330 | std::optional<der::Input> subject_key_identifier_; |
| 331 |
| 332 | // All of the extensions. |
| 333 | ExtensionsMap extensions_; |
| 334 | }; |
| 335 |
| 336 | BSSL_NAMESPACE_END |
| 337 |
| 338 | #endif // BSSL_PKI_PARSED_CERTIFICATE_H_ |
| 339 |