| 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 "parsed_certificate.h" |
| 16 |
| 17 | #include <openssl/bytestring.h> |
| 18 | #include <openssl/pool.h> |
| 19 |
| 20 | #include "cert_errors.h" |
| 21 | #include "certificate_policies.h" |
| 22 | #include "extended_key_usage.h" |
| 23 | #include "name_constraints.h" |
| 24 | #include "parser.h" |
| 25 | #include "signature_algorithm.h" |
| 26 | #include "verify_name_match.h" |
| 27 |
| 28 | BSSL_NAMESPACE_BEGIN |
| 29 |
| 30 | namespace { |
| 31 |
| 32 | DEFINE_CERT_ERROR_ID(kFailedParsingCertificate, "Failed parsing Certificate"); |
| 33 | DEFINE_CERT_ERROR_ID(kFailedParsingTbsCertificate, |
| 34 | "Failed parsing TBSCertificate"); |
| 35 | DEFINE_CERT_ERROR_ID(kFailedReadingIssuerOrSubject, |
| 36 | "Failed reading issuer or subject"); |
| 37 | DEFINE_CERT_ERROR_ID(kFailedNormalizingSubject, "Failed normalizing subject"); |
| 38 | DEFINE_CERT_ERROR_ID(kFailedNormalizingIssuer, "Failed normalizing issuer"); |
| 39 | DEFINE_CERT_ERROR_ID(kFailedParsingExtensions, "Failed parsing extensions"); |
| 40 | DEFINE_CERT_ERROR_ID(kFailedParsingBasicConstraints, |
| 41 | "Failed parsing basic constraints"); |
| 42 | DEFINE_CERT_ERROR_ID(kFailedParsingKeyUsage, "Failed parsing key usage"); |
| 43 | DEFINE_CERT_ERROR_ID(kFailedParsingEku, "Failed parsing extended key usage"); |
| 44 | DEFINE_CERT_ERROR_ID(kFailedParsingSubjectAltName, |
| 45 | "Failed parsing subjectAltName"); |
| 46 | DEFINE_CERT_ERROR_ID(kSubjectAltNameNotCritical, |
| 47 | "Empty subject and subjectAltName is not critical"); |
| 48 | DEFINE_CERT_ERROR_ID(kFailedParsingNameConstraints, |
| 49 | "Failed parsing name constraints"); |
| 50 | DEFINE_CERT_ERROR_ID(kFailedParsingAia, "Failed parsing authority info access"); |
| 51 | DEFINE_CERT_ERROR_ID(kFailedParsingPolicies, |
| 52 | "Failed parsing certificate policies"); |
| 53 | DEFINE_CERT_ERROR_ID(kFailedParsingPolicyConstraints, |
| 54 | "Failed parsing policy constraints"); |
| 55 | DEFINE_CERT_ERROR_ID(kFailedParsingPolicyMappings, |
| 56 | "Failed parsing policy mappings"); |
| 57 | DEFINE_CERT_ERROR_ID(kFailedParsingInhibitAnyPolicy, |
| 58 | "Failed parsing inhibit any policy"); |
| 59 | DEFINE_CERT_ERROR_ID(kFailedParsingAuthorityKeyIdentifier, |
| 60 | "Failed parsing authority key identifier"); |
| 61 | DEFINE_CERT_ERROR_ID(kFailedParsingSubjectKeyIdentifier, |
| 62 | "Failed parsing subject key identifier"); |
| 63 |
| 64 | [[nodiscard]] bool GetSequenceValue(der::Input tlv, der::Input *value) { |
| 65 | der::Parser parser(tlv); |
| 66 | return parser.ReadTag(CBS_ASN1_SEQUENCE, value) && !parser.HasMore(); |
| 67 | } |
| 68 |
| 69 | } // namespace |
| 70 |
| 71 | bool ParsedCertificate::GetExtension(der::Input extension_oid, |
| 72 | ParsedExtension *parsed_extension) const { |
| 73 | if (!tbs_.extensions_tlv) { |
| 74 | return false; |
| 75 | } |
| 76 |
| 77 | auto it = extensions_.find(extension_oid); |
| 78 | if (it == extensions_.end()) { |
| 79 | *parsed_extension = ParsedExtension(); |
| 80 | return false; |
| 81 | } |
| 82 |
| 83 | *parsed_extension = it->second; |
| 84 | return true; |
| 85 | } |
| 86 |
| 87 | ParsedCertificate::ParsedCertificate(PrivateConstructor) {} |
| 88 | ParsedCertificate::~ParsedCertificate() = default; |
| 89 |
| 90 | // static |
| 91 | std::shared_ptr<const ParsedCertificate> ParsedCertificate::Create( |
| 92 | bssl::UniquePtr<CRYPTO_BUFFER> backing_data, |
| 93 | const ParseCertificateOptions &options, CertErrors *errors) { |
| 94 | // |errors| is an optional parameter, but to keep the code simpler, use a |
| 95 | // dummy object when one wasn't provided. |
| 96 | CertErrors unused_errors; |
| 97 | if (!errors) { |
| 98 | errors = &unused_errors; |
| 99 | } |
| 100 |
| 101 | auto result = std::make_shared<ParsedCertificate>(PrivateConstructor{}); |
| 102 | result->cert_data_ = std::move(backing_data); |
| 103 | result->cert_ = der::Input(CRYPTO_BUFFER_data(result->cert_data_.get()), |
| 104 | CRYPTO_BUFFER_len(result->cert_data_.get())); |
| 105 |
| 106 | if (!ParseCertificate(result->cert_, &result->tbs_certificate_tlv_, |
| 107 | &result->signature_algorithm_tlv_, |
| 108 | &result->signature_value_, errors)) { |
| 109 | errors->AddError(kFailedParsingCertificate); |
| 110 | return nullptr; |
| 111 | } |
| 112 |
| 113 | if (!ParseTbsCertificate(result->tbs_certificate_tlv_, options, &result->tbs_, |
| 114 | errors)) { |
| 115 | errors->AddError(kFailedParsingTbsCertificate); |
| 116 | return nullptr; |
| 117 | } |
| 118 |
| 119 | // Attempt to parse the signature algorithm contained in the Certificate. |
| 120 | result->signature_algorithm_ = |
| 121 | ParseSignatureAlgorithm(result->signature_algorithm_tlv_); |
| 122 |
| 123 | der::Input subject_value; |
| 124 | if (!GetSequenceValue(result->tbs_.subject_tlv, &subject_value)) { |
| 125 | errors->AddError(kFailedReadingIssuerOrSubject); |
| 126 | return nullptr; |
| 127 | } |
| 128 | if (!NormalizeName(subject_value, &result->normalized_subject_, errors)) { |
| 129 | errors->AddError(kFailedNormalizingSubject); |
| 130 | return nullptr; |
| 131 | } |
| 132 | der::Input issuer_value; |
| 133 | if (!GetSequenceValue(result->tbs_.issuer_tlv, &issuer_value)) { |
| 134 | errors->AddError(kFailedReadingIssuerOrSubject); |
| 135 | return nullptr; |
| 136 | } |
| 137 | if (!NormalizeName(issuer_value, &result->normalized_issuer_, errors)) { |
| 138 | errors->AddError(kFailedNormalizingIssuer); |
| 139 | return nullptr; |
| 140 | } |
| 141 |
| 142 | // Parse the standard X.509 extensions. |
| 143 | if (result->tbs_.extensions_tlv) { |
| 144 | // ParseExtensions() ensures there are no duplicates, and maps the (unique) |
| 145 | // OID to the extension value. |
| 146 | if (!ParseExtensions(result->tbs_.extensions_tlv.value(), |
| 147 | &result->extensions_)) { |
| 148 | errors->AddError(kFailedParsingExtensions); |
| 149 | return nullptr; |
| 150 | } |
| 151 |
| 152 | ParsedExtension extension; |
| 153 |
| 154 | // Basic constraints. |
| 155 | if (result->GetExtension(der::Input(kBasicConstraintsOid), &extension)) { |
| 156 | result->has_basic_constraints_ = true; |
| 157 | if (!ParseBasicConstraints(extension.value, |
| 158 | &result->basic_constraints_)) { |
| 159 | errors->AddError(kFailedParsingBasicConstraints); |
| 160 | return nullptr; |
| 161 | } |
| 162 | } |
| 163 |
| 164 | // Key Usage. |
| 165 | if (result->GetExtension(der::Input(kKeyUsageOid), &extension)) { |
| 166 | result->has_key_usage_ = true; |
| 167 | if (!ParseKeyUsage(extension.value, &result->key_usage_)) { |
| 168 | errors->AddError(kFailedParsingKeyUsage); |
| 169 | return nullptr; |
| 170 | } |
| 171 | } |
| 172 |
| 173 | // Extended Key Usage. |
| 174 | if (result->GetExtension(der::Input(kExtKeyUsageOid), &extension)) { |
| 175 | result->has_extended_key_usage_ = true; |
| 176 | if (!ParseEKUExtension(extension.value, &result->extended_key_usage_)) { |
| 177 | errors->AddError(kFailedParsingEku); |
| 178 | return nullptr; |
| 179 | } |
| 180 | } |
| 181 |
| 182 | // Subject alternative name. |
| 183 | if (result->GetExtension(der::Input(kSubjectAltNameOid), |
| 184 | &result->subject_alt_names_extension_)) { |
| 185 | // RFC 5280 section 4.2.1.6: |
| 186 | // SubjectAltName ::= GeneralNames |
| 187 | result->subject_alt_names_ = GeneralNames::Create( |
| 188 | result->subject_alt_names_extension_.value, errors); |
| 189 | if (!result->subject_alt_names_) { |
| 190 | errors->AddError(kFailedParsingSubjectAltName); |
| 191 | return nullptr; |
| 192 | } |
| 193 | // RFC 5280 section 4.1.2.6: |
| 194 | // If subject naming information is present only in the subjectAltName |
| 195 | // extension (e.g., a key bound only to an email address or URI), then the |
| 196 | // subject name MUST be an empty sequence and the subjectAltName extension |
| 197 | // MUST be critical. |
| 198 | if (subject_value.empty() && |
| 199 | !result->subject_alt_names_extension_.critical) { |
| 200 | errors->AddError(kSubjectAltNameNotCritical); |
| 201 | return nullptr; |
| 202 | } |
| 203 | } |
| 204 |
| 205 | // Name constraints. |
| 206 | if (result->GetExtension(der::Input(kNameConstraintsOid), &extension)) { |
| 207 | result->name_constraints_ = |
| 208 | NameConstraints::Create(extension.value, extension.critical, errors); |
| 209 | if (!result->name_constraints_) { |
| 210 | errors->AddError(kFailedParsingNameConstraints); |
| 211 | return nullptr; |
| 212 | } |
| 213 | } |
| 214 |
| 215 | // Authority information access. |
| 216 | if (result->GetExtension(der::Input(kAuthorityInfoAccessOid), |
| 217 | &result->authority_info_access_extension_)) { |
| 218 | result->has_authority_info_access_ = true; |
| 219 | if (!ParseAuthorityInfoAccessURIs( |
| 220 | result->authority_info_access_extension_.value, |
| 221 | &result->ca_issuers_uris_, &result->ocsp_uris_)) { |
| 222 | errors->AddError(kFailedParsingAia); |
| 223 | return nullptr; |
| 224 | } |
| 225 | } |
| 226 |
| 227 | // Policies. |
| 228 | if (result->GetExtension(der::Input(kCertificatePoliciesOid), &extension)) { |
| 229 | result->has_policy_oids_ = true; |
| 230 | if (!ParseCertificatePoliciesExtensionOids( |
| 231 | extension.value, false /*fail_parsing_unknown_qualifier_oids*/, |
| 232 | &result->policy_oids_, errors)) { |
| 233 | errors->AddError(kFailedParsingPolicies); |
| 234 | return nullptr; |
| 235 | } |
| 236 | } |
| 237 |
| 238 | // Policy constraints. |
| 239 | if (result->GetExtension(der::Input(kPolicyConstraintsOid), &extension)) { |
| 240 | result->has_policy_constraints_ = true; |
| 241 | if (!ParsePolicyConstraints(extension.value, |
| 242 | &result->policy_constraints_)) { |
| 243 | errors->AddError(kFailedParsingPolicyConstraints); |
| 244 | return nullptr; |
| 245 | } |
| 246 | } |
| 247 |
| 248 | // Policy mappings. |
| 249 | if (result->GetExtension(der::Input(kPolicyMappingsOid), &extension)) { |
| 250 | result->has_policy_mappings_ = true; |
| 251 | if (!ParsePolicyMappings(extension.value, &result->policy_mappings_)) { |
| 252 | errors->AddError(kFailedParsingPolicyMappings); |
| 253 | return nullptr; |
| 254 | } |
| 255 | } |
| 256 |
| 257 | // Inhibit Any Policy. |
| 258 | if (result->GetExtension(der::Input(kInhibitAnyPolicyOid), &extension)) { |
| 259 | result->inhibit_any_policy_ = ParseInhibitAnyPolicy(extension.value); |
| 260 | if (!result->inhibit_any_policy_) { |
| 261 | errors->AddError(kFailedParsingInhibitAnyPolicy); |
| 262 | return nullptr; |
| 263 | } |
| 264 | } |
| 265 |
| 266 | // Subject Key Identifier. |
| 267 | if (result->GetExtension(der::Input(kSubjectKeyIdentifierOid), |
| 268 | &extension)) { |
| 269 | result->subject_key_identifier_ = std::make_optional<der::Input>(); |
| 270 | if (!ParseSubjectKeyIdentifier( |
| 271 | extension.value, &result->subject_key_identifier_.value())) { |
| 272 | errors->AddError(kFailedParsingSubjectKeyIdentifier); |
| 273 | return nullptr; |
| 274 | } |
| 275 | } |
| 276 |
| 277 | // Authority Key Identifier. |
| 278 | if (result->GetExtension(der::Input(kAuthorityKeyIdentifierOid), |
| 279 | &extension)) { |
| 280 | result->authority_key_identifier_ = |
| 281 | std::make_optional<ParsedAuthorityKeyIdentifier>(); |
| 282 | if (!ParseAuthorityKeyIdentifier( |
| 283 | extension.value, &result->authority_key_identifier_.value())) { |
| 284 | errors->AddError(kFailedParsingAuthorityKeyIdentifier); |
| 285 | return nullptr; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 |
| 290 | return result; |
| 291 | } |
| 292 |
| 293 | // static |
| 294 | bool ParsedCertificate::CreateAndAddToVector( |
| 295 | bssl::UniquePtr<CRYPTO_BUFFER> cert_data, |
| 296 | const ParseCertificateOptions &options, |
| 297 | std::vector<std::shared_ptr<const bssl::ParsedCertificate>> *chain, |
| 298 | CertErrors *errors) { |
| 299 | std::shared_ptr<const ParsedCertificate> cert( |
| 300 | Create(std::move(cert_data), options, errors)); |
| 301 | if (!cert) { |
| 302 | return false; |
| 303 | } |
| 304 | chain->push_back(std::move(cert)); |
| 305 | return true; |
| 306 | } |
| 307 |
| 308 | BSSL_NAMESPACE_END |
| 309 |