| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_CERT_INTERNAL_PARSE_CERTIFICATE_H_ | 5 #ifndef NET_CERT_INTERNAL_PARSE_CERTIFICATE_H_ |
| 6 #define NET_CERT_INTERNAL_PARSE_CERTIFICATE_H_ | 6 #define NET_CERT_INTERNAL_PARSE_CERTIFICATE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 // Given the uniqueness requirements above, serial numbers can be | 38 // Given the uniqueness requirements above, serial numbers can be |
| 39 // expected to contain long integers. Certificate users MUST be able to | 39 // expected to contain long integers. Certificate users MUST be able to |
| 40 // handle serialNumber values up to 20 octets. Conforming CAs MUST NOT | 40 // handle serialNumber values up to 20 octets. Conforming CAs MUST NOT |
| 41 // use serialNumber values longer than 20 octets. | 41 // use serialNumber values longer than 20 octets. |
| 42 // | 42 // |
| 43 // Note: Non-conforming CAs may issue certificates with serial numbers | 43 // Note: Non-conforming CAs may issue certificates with serial numbers |
| 44 // that are negative or zero. Certificate users SHOULD be prepared to | 44 // that are negative or zero. Certificate users SHOULD be prepared to |
| 45 // gracefully handle such certificates. | 45 // gracefully handle such certificates. |
| 46 NET_EXPORT bool VerifySerialNumber(const der::Input& value) WARN_UNUSED_RESULT; | 46 NET_EXPORT bool VerifySerialNumber(const der::Input& value) WARN_UNUSED_RESULT; |
| 47 | 47 |
| 48 struct NET_EXPORT ParseCertificateOptions { |
| 49 // If set to true, then parsing will skip checks on the certificate's serial |
| 50 // number. The only requirement will be that the serial number is an INTEGER, |
| 51 // however it is not required to be a valid DER-encoding (i.e. minimal |
| 52 // encoding), nor is it required to be constrained to any particular length. |
| 53 bool allow_invalid_serial_numbers = false; |
| 54 }; |
| 55 |
| 48 // Parses a DER-encoded "Certificate" as specified by RFC 5280. Returns true on | 56 // Parses a DER-encoded "Certificate" as specified by RFC 5280. Returns true on |
| 49 // success and sets the results in the |out_*| parameters. | 57 // success and sets the results in the |out_*| parameters. |
| 50 // | 58 // |
| 51 // Note that on success the out parameters alias data from the input | 59 // Note that on success the out parameters alias data from the input |
| 52 // |certificate_tlv|. Hence the output values are only valid as long as | 60 // |certificate_tlv|. Hence the output values are only valid as long as |
| 53 // |certificate_tlv| remains valid. | 61 // |certificate_tlv| remains valid. |
| 54 // | 62 // |
| 55 // On failure the out parameters have an undefined state. Some of them may have | 63 // On failure the out parameters have an undefined state. Some of them may have |
| 56 // been updated during parsing, whereas others may not have been changed. | 64 // been updated during parsing, whereas others may not have been changed. |
| 57 // | 65 // |
| (...skipping 21 matching lines...) Expand all Loading... |
| 79 // signatureValue BIT STRING } | 87 // signatureValue BIT STRING } |
| 80 // | 88 // |
| 81 // Parsing guarantees that this is a valid BIT STRING. | 89 // Parsing guarantees that this is a valid BIT STRING. |
| 82 NET_EXPORT bool ParseCertificate(const der::Input& certificate_tlv, | 90 NET_EXPORT bool ParseCertificate(const der::Input& certificate_tlv, |
| 83 der::Input* out_tbs_certificate_tlv, | 91 der::Input* out_tbs_certificate_tlv, |
| 84 der::Input* out_signature_algorithm_tlv, | 92 der::Input* out_signature_algorithm_tlv, |
| 85 der::BitString* out_signature_value) | 93 der::BitString* out_signature_value) |
| 86 WARN_UNUSED_RESULT; | 94 WARN_UNUSED_RESULT; |
| 87 | 95 |
| 88 // Parses a DER-encoded "TBSCertificate" as specified by RFC 5280. Returns true | 96 // Parses a DER-encoded "TBSCertificate" as specified by RFC 5280. Returns true |
| 89 // on success and sets the results in |out|. | 97 // on success and sets the results in |out|. Certain invalid inputs may |
| 98 // be accepted based on the provided |options|. |
| 90 // | 99 // |
| 91 // Note that on success |out| aliases data from the input |tbs_tlv|. | 100 // Note that on success |out| aliases data from the input |tbs_tlv|. |
| 92 // Hence the fields of the ParsedTbsCertificate are only valid as long as | 101 // Hence the fields of the ParsedTbsCertificate are only valid as long as |
| 93 // |tbs_tlv| remains valid. | 102 // |tbs_tlv| remains valid. |
| 94 // | 103 // |
| 95 // On failure |out| has an undefined state. Some of its fields may have been | 104 // On failure |out| has an undefined state. Some of its fields may have been |
| 96 // updated during parsing, whereas others may not have been changed. | 105 // updated during parsing, whereas others may not have been changed. |
| 97 // | 106 // |
| 98 // Refer to the per-field documentation of ParsedTbsCertificate for details on | 107 // Refer to the per-field documentation of ParsedTbsCertificate for details on |
| 99 // what validity checks parsing performs. | 108 // what validity checks parsing performs. |
| 100 // | 109 // |
| 101 // TBSCertificate ::= SEQUENCE { | 110 // TBSCertificate ::= SEQUENCE { |
| 102 // version [0] EXPLICIT Version DEFAULT v1, | 111 // version [0] EXPLICIT Version DEFAULT v1, |
| 103 // serialNumber CertificateSerialNumber, | 112 // serialNumber CertificateSerialNumber, |
| 104 // signature AlgorithmIdentifier, | 113 // signature AlgorithmIdentifier, |
| 105 // issuer Name, | 114 // issuer Name, |
| 106 // validity Validity, | 115 // validity Validity, |
| 107 // subject Name, | 116 // subject Name, |
| 108 // subjectPublicKeyInfo SubjectPublicKeyInfo, | 117 // subjectPublicKeyInfo SubjectPublicKeyInfo, |
| 109 // issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, | 118 // issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
| 110 // -- If present, version MUST be v2 or v3 | 119 // -- If present, version MUST be v2 or v3 |
| 111 // subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, | 120 // subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
| 112 // -- If present, version MUST be v2 or v3 | 121 // -- If present, version MUST be v2 or v3 |
| 113 // extensions [3] EXPLICIT Extensions OPTIONAL | 122 // extensions [3] EXPLICIT Extensions OPTIONAL |
| 114 // -- If present, version MUST be v3 | 123 // -- If present, version MUST be v3 |
| 115 // } | 124 // } |
| 116 NET_EXPORT bool ParseTbsCertificate(const der::Input& tbs_tlv, | 125 NET_EXPORT bool ParseTbsCertificate(const der::Input& tbs_tlv, |
| 126 const ParseCertificateOptions& options, |
| 117 ParsedTbsCertificate* out) | 127 ParsedTbsCertificate* out) |
| 118 WARN_UNUSED_RESULT; | 128 WARN_UNUSED_RESULT; |
| 119 | 129 |
| 120 // Represents a "Version" from RFC 5280: | 130 // Represents a "Version" from RFC 5280: |
| 121 // Version ::= INTEGER { v1(0), v2(1), v3(2) } | 131 // Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 122 enum class CertificateVersion { | 132 enum class CertificateVersion { |
| 123 V1, | 133 V1, |
| 124 V2, | 134 V2, |
| 125 V3, | 135 V3, |
| 126 }; | 136 }; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 141 // Parsing guarantees that the version is one of v1, v2, or v3. | 151 // Parsing guarantees that the version is one of v1, v2, or v3. |
| 142 CertificateVersion version = CertificateVersion::V1; | 152 CertificateVersion version = CertificateVersion::V1; |
| 143 | 153 |
| 144 // Corresponds with "serialNumber" from RFC 5280: | 154 // Corresponds with "serialNumber" from RFC 5280: |
| 145 // serialNumber CertificateSerialNumber, | 155 // serialNumber CertificateSerialNumber, |
| 146 // | 156 // |
| 147 // This field specifically contains the content bytes of the INTEGER. So for | 157 // This field specifically contains the content bytes of the INTEGER. So for |
| 148 // instance if the serial number was 1000 then this would contain bytes | 158 // instance if the serial number was 1000 then this would contain bytes |
| 149 // {0x03, 0xE8}. | 159 // {0x03, 0xE8}. |
| 150 // | 160 // |
| 151 // In addition to being a valid DER-encoded INTEGER, parsing guarantees that | 161 // The serial number may or may not be a valid DER-encoded INTEGER: |
| 162 // |
| 163 // If the option |allow_invalid_serial_numbers=true| was used during |
| 164 // parsing, then nothing further can be assumed about these bytes. |
| 165 // |
| 166 // Otherwise if |allow_invalid_serial_numbers=false| then in addition |
| 167 // to being a valid DER-encoded INTEGER, parsing guarantees that |
| 152 // the serial number is at most 20 bytes long. Parsing does NOT guarantee | 168 // the serial number is at most 20 bytes long. Parsing does NOT guarantee |
| 153 // that the integer is positive (might be zero or negative). | 169 // that the integer is positive (might be zero or negative). |
| 154 der::Input serial_number; | 170 der::Input serial_number; |
| 155 | 171 |
| 156 // Corresponds with "signatureAlgorithm" from RFC 5280: | 172 // Corresponds with "signatureAlgorithm" from RFC 5280: |
| 157 // signatureAlgorithm AlgorithmIdentifier, | 173 // signatureAlgorithm AlgorithmIdentifier, |
| 158 // | 174 // |
| 159 // This contains the full (unverified) Tag-Length-Value for a SEQUENCE. No | 175 // This contains the full (unverified) Tag-Length-Value for a SEQUENCE. No |
| 160 // guarantees are made regarding the value of this SEQUENCE. | 176 // guarantees are made regarding the value of this SEQUENCE. |
| 161 // | 177 // |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 // be set. | 395 // be set. |
| 380 // | 396 // |
| 381 // To test if a particular key usage is set, call, e.g.: | 397 // To test if a particular key usage is set, call, e.g.: |
| 382 // key_usage->AssertsBit(KEY_USAGE_BIT_DIGITAL_SIGNATURE); | 398 // key_usage->AssertsBit(KEY_USAGE_BIT_DIGITAL_SIGNATURE); |
| 383 NET_EXPORT bool ParseKeyUsage(const der::Input& key_usage_tlv, | 399 NET_EXPORT bool ParseKeyUsage(const der::Input& key_usage_tlv, |
| 384 der::BitString* key_usage) WARN_UNUSED_RESULT; | 400 der::BitString* key_usage) WARN_UNUSED_RESULT; |
| 385 | 401 |
| 386 } // namespace net | 402 } // namespace net |
| 387 | 403 |
| 388 #endif // NET_CERT_INTERNAL_PARSE_CERTIFICATE_H_ | 404 #endif // NET_CERT_INTERNAL_PARSE_CERTIFICATE_H_ |
| OLD | NEW |