Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "net/cert/ct_objects_extractor.h" | 5 #include "net/cert/ct_objects_extractor.h" |
| 6 | 6 |
| 7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <secasn1.h> | 8 #include <secasn1.h> |
| 9 #include <secitem.h> | 9 #include <secitem.h> |
| 10 #include <secoid.h> | 10 #include <secoid.h> |
| 11 | 11 |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/sha1.h" | |
| 13 #include "crypto/scoped_nss_types.h" | 14 #include "crypto/scoped_nss_types.h" |
| 14 #include "crypto/sha2.h" | 15 #include "crypto/sha2.h" |
| 15 #include "net/cert/asn1_util.h" | 16 #include "net/cert/asn1_util.h" |
| 16 #include "net/cert/scoped_nss_types.h" | 17 #include "net/cert/scoped_nss_types.h" |
| 17 #include "net/cert/signed_certificate_timestamp.h" | 18 #include "net/cert/signed_certificate_timestamp.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 | 21 |
| 21 namespace ct { | 22 namespace ct { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 26 // NSS black magic to get the address of externally defined template at runtime. | |
| 27 SEC_ASN1_MKSUB(SEC_IntegerTemplate) | |
| 28 SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate) | |
| 29 SEC_ASN1_MKSUB(SEC_GeneralizedTimeTemplate) | |
| 30 SEC_ASN1_MKSUB(CERT_SequenceOfCertExtensionTemplate) | |
| 31 | |
| 25 // Wrapper class to convert a X509Certificate::OSCertHandle directly | 32 // Wrapper class to convert a X509Certificate::OSCertHandle directly |
| 26 // into a CERTCertificate* usable with other NSS functions. This is used for | 33 // into a CERTCertificate* usable with other NSS functions. This is used for |
| 27 // platforms where X509Certificate::OSCertHandle refers to a different type | 34 // platforms where X509Certificate::OSCertHandle refers to a different type |
| 28 // than a CERTCertificate*. | 35 // than a CERTCertificate*. |
| 29 struct NSSCertWrapper { | 36 struct NSSCertWrapper { |
| 30 explicit NSSCertWrapper(X509Certificate::OSCertHandle cert_handle); | 37 explicit NSSCertWrapper(X509Certificate::OSCertHandle cert_handle); |
| 31 ~NSSCertWrapper() {} | 38 ~NSSCertWrapper() {} |
| 32 | 39 |
| 33 ScopedCERTCertificate cert; | 40 ScopedCERTCertificate cert; |
| 34 }; | 41 }; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 55 } | 62 } |
| 56 | 63 |
| 57 // The wire form of the OID 1.3.6.1.4.1.11129.2.4.2. See Section 3.3 of | 64 // The wire form of the OID 1.3.6.1.4.1.11129.2.4.2. See Section 3.3 of |
| 58 // RFC6962. | 65 // RFC6962. |
| 59 const unsigned char kEmbeddedSCTOid[] = {0x2B, 0x06, 0x01, 0x04, 0x01, | 66 const unsigned char kEmbeddedSCTOid[] = {0x2B, 0x06, 0x01, 0x04, 0x01, |
| 60 0xD6, 0x79, 0x02, 0x04, 0x02}; | 67 0xD6, 0x79, 0x02, 0x04, 0x02}; |
| 61 const char kEmbeddedSCTDescription[] = | 68 const char kEmbeddedSCTDescription[] = |
| 62 "X.509v3 Certificate Transparency Embedded Signed Certificate Timestamp " | 69 "X.509v3 Certificate Transparency Embedded Signed Certificate Timestamp " |
| 63 "List"; | 70 "List"; |
| 64 | 71 |
| 72 // The wire form of the OID 1.3.6.1.4.1.11129.2.4.5 - OCSP SingleExtension for | |
| 73 // X.509v3 Certificate Transparency Signed Certificate Timestamp List, see | |
| 74 // Section 3.3 of RFC6962. | |
| 75 const unsigned char kOCSPExtensionOid[] = {0x2B, 0x06, 0x01, 0x04, 0x01, | |
| 76 0xD6, 0x79, 0x02, 0x04, 0x05}; | |
| 77 | |
| 78 const SECItem kOCSPExtensionOidItem = { | |
| 79 siBuffer, const_cast<unsigned char*>(kOCSPExtensionOid), | |
| 80 sizeof(kOCSPExtensionOid) | |
| 81 }; | |
| 82 | |
| 83 // id-ad-ocsp: 1.3.6.1.5.5.7.48.1.1 | |
| 84 const unsigned char kBasicOCSPResponseOid[] = {0x2B, 0x06, 0x01, 0x05, 0x05, | |
| 85 0x07, 0x30, 0x01, 0x01}; | |
| 86 | |
| 87 const SECItem kBasicOCSPResponseOidItem = { | |
| 88 siBuffer, const_cast<unsigned char*>(kBasicOCSPResponseOid), | |
| 89 sizeof(kBasicOCSPResponseOid) | |
| 90 }; | |
| 91 | |
| 92 | |
| 65 // Initializes the necessary NSS internals for use with Certificate | 93 // Initializes the necessary NSS internals for use with Certificate |
| 66 // Transparency. | 94 // Transparency. |
| 67 class CTInitSingleton { | 95 class CTInitSingleton { |
| 68 public: | 96 public: |
| 69 SECOidTag embedded_oid() const { return embedded_oid_; } | 97 SECOidTag embedded_oid() const { return embedded_oid_; } |
| 70 | 98 |
| 71 private: | 99 private: |
| 72 friend struct base::DefaultLazyInstanceTraits<CTInitSingleton>; | 100 friend struct base::DefaultLazyInstanceTraits<CTInitSingleton>; |
| 73 | 101 |
| 74 CTInitSingleton() : embedded_oid_(SEC_OID_UNKNOWN) { | 102 CTInitSingleton() : embedded_oid_(SEC_OID_UNKNOWN) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 101 | 129 |
| 102 SECOidTag embedded_oid_; | 130 SECOidTag embedded_oid_; |
| 103 | 131 |
| 104 DISALLOW_COPY_AND_ASSIGN(CTInitSingleton); | 132 DISALLOW_COPY_AND_ASSIGN(CTInitSingleton); |
| 105 }; | 133 }; |
| 106 | 134 |
| 107 base::LazyInstance<CTInitSingleton>::Leaky g_ct_singleton = | 135 base::LazyInstance<CTInitSingleton>::Leaky g_ct_singleton = |
| 108 LAZY_INSTANCE_INITIALIZER; | 136 LAZY_INSTANCE_INITIALIZER; |
| 109 | 137 |
| 110 // Obtains the data for an X.509v3 certificate extension identified by |oid| | 138 // Obtains the data for an X.509v3 certificate extension identified by |oid| |
| 111 // and encoded as an OCTET STRING. Returns true if the extension was found, | 139 // and encoded as an OCTET STRING. Returns true if the extension was found in |
| 112 // updating |ext_data| to be the extension data after removing the DER | 140 // the certificate, updating |ext_data| to be the extension data after removing |
| 113 // encoding of OCTET STRING. | 141 // the DER encoding of OCTET STRING. |
| 114 bool GetOctetStringExtension(CERTCertificate* cert, | 142 bool GetCertOctetStringExtension(CERTCertificate* cert, |
| 115 SECOidTag oid, | 143 SECOidTag oid, |
| 116 std::string* extension_data) { | 144 std::string* extension_data) { |
| 117 SECItem extension; | 145 SECItem extension; |
| 118 SECStatus rv = CERT_FindCertExtension(cert, oid, &extension); | 146 SECStatus rv = CERT_FindCertExtension(cert, oid, &extension); |
| 119 if (rv != SECSuccess) | 147 if (rv != SECSuccess) |
| 120 return false; | 148 return false; |
| 121 | 149 |
| 122 base::StringPiece raw_data(reinterpret_cast<char*>(extension.data), | 150 base::StringPiece raw_data(reinterpret_cast<char*>(extension.data), |
| 123 extension.len); | 151 extension.len); |
| 124 base::StringPiece parsed_data; | 152 base::StringPiece parsed_data; |
| 125 if (!asn1::GetElement(&raw_data, asn1::kOCTETSTRING, &parsed_data) || | 153 if (!asn1::GetElement(&raw_data, asn1::kOCTETSTRING, &parsed_data) || |
| 126 raw_data.size() > 0) { // Decoding failure or raw data left | 154 raw_data.size() > 0) { // Decoding failure or raw data left |
| 127 rv = SECFailure; | 155 rv = SECFailure; |
| 128 } else { | 156 } else { |
| 129 parsed_data.CopyToString(extension_data); | 157 parsed_data.CopyToString(extension_data); |
| 130 } | 158 } |
| 131 | 159 |
| 132 SECITEM_FreeItem(&extension, PR_FALSE); | 160 SECITEM_FreeItem(&extension, PR_FALSE); |
| 133 return rv == SECSuccess; | 161 return rv == SECSuccess; |
| 134 } | 162 } |
| 135 | 163 |
| 164 // NSS offers CERT_FindCertExtension for certificates, but that only accepts | |
| 165 // CERTCertificate* inputs, so the method below extracts the SCT extension | |
| 166 // directly from the CERTCertExtension** of an OCSP response. | |
| 167 // | |
| 168 // Obtains the data for an OCSP extension identified by kOCSPExtensionOidItem | |
| 169 // and encoded as an OCTET STRING. Returns true if the extension was found in | |
| 170 // |extensions|, updating |extension_data| to be the extension data after | |
| 171 // removing the DER encoding of OCTET STRING. | |
| 172 bool GetSCTListFromOCSPExtension(PLArenaPool* arena, | |
| 173 const CERTCertExtension* const* extensions, | |
| 174 std::string* extension_data) { | |
| 175 if (!extensions) | |
| 176 return false; | |
| 177 | |
| 178 const CERTCertExtension* match = NULL; | |
| 179 | |
| 180 for (const CERTCertExtension* const* exts = extensions; *exts; ++exts) { | |
| 181 const CERTCertExtension* ext = *exts; | |
| 182 if (SECITEM_ItemsAreEqual(&kOCSPExtensionOidItem, &ext->id)) { | |
| 183 match = ext; | |
| 184 break; | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 if (!match) | |
| 189 return false; | |
| 190 | |
| 191 SECItem contents; | |
| 192 // SEC_QuickDERDecodeItem sets |contents| to point to |match|, so it is not | |
| 193 // necessary to free the contents of |contents|. | |
| 194 SECStatus rv = SEC_QuickDERDecodeItem(arena, &contents, | |
| 195 SEC_ASN1_GET(SEC_OctetStringTemplate), | |
| 196 &match->value); | |
| 197 if (rv != SECSuccess) | |
| 198 return false; | |
| 199 | |
| 200 base::StringPiece parsed_data(reinterpret_cast<char*>(contents.data), | |
| 201 contents.len); | |
| 202 parsed_data.CopyToString(extension_data); | |
| 203 return true; | |
| 204 } | |
| 205 | |
| 136 // Given a |cert|, extract the TBSCertificate from this certificate, also | 206 // Given a |cert|, extract the TBSCertificate from this certificate, also |
| 137 // removing the X.509 extension with OID 1.3.6.1.4.1.11129.2.4.2 (that is, | 207 // removing the X.509 extension with OID 1.3.6.1.4.1.11129.2.4.2 (that is, |
| 138 // the embedded SCT) | 208 // the embedded SCT) |
| 139 bool ExtractTBSCertWithoutSCTs(CERTCertificate* cert, | 209 bool ExtractTBSCertWithoutSCTs(CERTCertificate* cert, |
| 140 std::string* to_be_signed) { | 210 std::string* to_be_signed) { |
| 141 SECOidData* oid = SECOID_FindOIDByTag(g_ct_singleton.Get().embedded_oid()); | 211 SECOidData* oid = SECOID_FindOIDByTag(g_ct_singleton.Get().embedded_oid()); |
| 142 if (!oid) | 212 if (!oid) |
| 143 return false; | 213 return false; |
| 144 | 214 |
| 145 // This is a giant hack, due to the fact that NSS does not expose a good API | 215 // This is a giant hack, due to the fact that NSS does not expose a good API |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 173 &tbs_data, | 243 &tbs_data, |
| 174 &temp_cert, | 244 &temp_cert, |
| 175 SEC_ASN1_GET(CERT_CertificateTemplate)); | 245 SEC_ASN1_GET(CERT_CertificateTemplate)); |
| 176 if (!result) | 246 if (!result) |
| 177 return false; | 247 return false; |
| 178 | 248 |
| 179 to_be_signed->assign(reinterpret_cast<char*>(tbs_data.data), tbs_data.len); | 249 to_be_signed->assign(reinterpret_cast<char*>(tbs_data.data), tbs_data.len); |
| 180 return true; | 250 return true; |
| 181 } | 251 } |
| 182 | 252 |
| 253 // The following code is adapted from the NSS OCSP module, in order to expose | |
| 254 // the internal structure of an OCSP response. | |
| 255 | |
| 256 // ResponseBytes ::= SEQUENCE { | |
| 257 // responseType OBJECT IDENTIFIER, | |
| 258 // response OCTET STRING } | |
| 259 struct ResponseBytes { | |
| 260 SECItem response_type; | |
| 261 SECItem der_response; | |
| 262 }; | |
| 263 | |
| 264 const SEC_ASN1Template kResponseBytesTemplate[] = { | |
| 265 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(ResponseBytes) }, | |
| 266 { SEC_ASN1_OBJECT_ID, offsetof(ResponseBytes, response_type) }, | |
| 267 { SEC_ASN1_OCTET_STRING, offsetof(ResponseBytes, der_response) }, | |
| 268 { 0 } | |
| 269 }; | |
| 270 | |
| 271 // OCSPResponse ::= SEQUENCE { | |
| 272 // responseStatus OCSPResponseStatus, | |
| 273 // responseBytes [0] EXPLICIT ResponseBytes OPTIONAL } | |
| 274 struct OCSPResponse { | |
| 275 SECItem response_status; | |
| 276 // This indirection is needed because |response_bytes| is an optional | |
| 277 // component and we need a way to determine if it is missing. | |
| 278 ResponseBytes* response_bytes; | |
| 279 }; | |
| 280 | |
| 281 const SEC_ASN1Template kPointerToResponseBytesTemplate[] = { | |
| 282 { SEC_ASN1_POINTER, 0, kResponseBytesTemplate } | |
| 283 }; | |
| 284 | |
| 285 const SEC_ASN1Template kOCSPResponseTemplate[] = { | |
| 286 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(OCSPResponse) }, | |
| 287 { SEC_ASN1_ENUMERATED, offsetof(OCSPResponse, response_status) }, | |
| 288 { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | | |
| 289 SEC_ASN1_CONTEXT_SPECIFIC | 0, offsetof(OCSPResponse, response_bytes), | |
| 290 kPointerToResponseBytesTemplate }, | |
| 291 { 0 } | |
| 292 }; | |
| 293 | |
| 294 // CertID ::= SEQUENCE { | |
| 295 // hashAlgorithm AlgorithmIdentifier, | |
| 296 // issuerNameHash OCTET STRING, -- Hash of Issuer's DN | |
| 297 // issuerKeyHash OCTET STRING, -- Hash of Issuers public key | |
| 298 // serialNumber CertificateSerialNumber } | |
| 299 struct CertID { | |
| 300 SECAlgorithmID hash_algorithm; | |
| 301 SECItem issuer_name_hash; | |
| 302 SECItem issuer_key_hash; | |
| 303 SECItem serial_number; | |
| 304 }; | |
| 305 | |
| 306 const SEC_ASN1Template kCertIDTemplate[] = { | |
| 307 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CertID) }, | |
| 308 { SEC_ASN1_INLINE | SEC_ASN1_XTRN, offsetof(CertID, hash_algorithm), | |
| 309 SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, | |
| 310 { SEC_ASN1_OCTET_STRING, offsetof(CertID, issuer_name_hash) }, | |
| 311 { SEC_ASN1_OCTET_STRING, offsetof(CertID, issuer_key_hash) }, | |
| 312 { SEC_ASN1_INTEGER, offsetof(CertID, serial_number) }, | |
| 313 { 0 } | |
| 314 }; | |
| 315 | |
| 316 // SingleResponse ::= SEQUENCE { | |
| 317 // certID CertID, | |
| 318 // certStatus CertStatus, | |
| 319 // thisUpdate GeneralizedTime, | |
| 320 // nextUpdate [0] EXPLICIT GeneralizedTime OPTIONAL, | |
| 321 // singleExtensions [1] EXPLICIT Extensions OPTIONAL } | |
| 322 struct SingleResponse { | |
| 323 CertID cert_id; | |
| 324 // The following three fields are not used. | |
| 325 // TODO(ekasper): consider removing them and using SEC_ASN1_SKIP to | |
| 326 // skip the decoding. | |
| 327 SECItem der_cert_status; | |
| 328 SECItem this_update; | |
| 329 SECItem next_update; | |
| 330 CERTCertExtension** single_extensions; | |
| 331 }; | |
| 332 | |
| 333 const SEC_ASN1Template kSingleResponseTemplate[] = { | |
| 334 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SingleResponse) }, | |
| 335 { SEC_ASN1_INLINE, offsetof(SingleResponse, cert_id), kCertIDTemplate }, | |
| 336 // Really a CHOICE but we make it an ANY because we don't care about the | |
| 337 // contents of this field. | |
| 338 // TODO(ekasper): use SEC_ASN1_CHOICE. | |
| 339 { SEC_ASN1_ANY, offsetof(SingleResponse, der_cert_status) }, | |
| 340 { SEC_ASN1_GENERALIZED_TIME, offsetof(SingleResponse, this_update) }, | |
| 341 { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | | |
| 342 SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0, | |
| 343 offsetof(SingleResponse, next_update), | |
| 344 SEC_ASN1_SUB(SEC_GeneralizedTimeTemplate) }, | |
| 345 { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | | |
| 346 SEC_ASN1_CONTEXT_SPECIFIC | 1, | |
|
wtc
2013/12/13 03:32:51
The test failed because of an assertion failure in
| |
| 347 offsetof(SingleResponse, single_extensions), | |
| 348 SEC_ASN1_SUB(CERT_SequenceOfCertExtensionTemplate) }, | |
| 349 { 0 } | |
| 350 }; | |
| 351 | |
| 352 // ResponseData ::= SEQUENCE { | |
| 353 // version [0] EXPLICIT Version DEFAULT v1, | |
| 354 // responderID ResponderID, | |
| 355 // producedAt GeneralizedTime, | |
| 356 // responses SEQUENCE OF SingleResponse, | |
| 357 // responseExtensions [1] EXPLICIT Extensions OPTIONAL } | |
| 358 struct ResponseData { | |
| 359 // The first three fields are not used. | |
| 360 // TODO(ekasper): consider removing them and using SEC_ASN1_SKIP to | |
| 361 // skip the decoding. | |
| 362 SECItem version; | |
| 363 SECItem der_responder_id; | |
| 364 SECItem produced_at; | |
| 365 SingleResponse** single_responses; | |
| 366 // Skip extensions. | |
| 367 }; | |
| 368 | |
| 369 const SEC_ASN1Template kResponseDataTemplate[] = { | |
| 370 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(ResponseData) }, | |
| 371 { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | | |
| 372 SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0, | |
| 373 offsetof(ResponseData, version), SEC_ASN1_SUB(SEC_IntegerTemplate) }, | |
| 374 // Really a CHOICE but we make it an ANY because we don't care about the | |
| 375 // contents of this field. | |
| 376 // TODO(ekasper): use SEC_ASN1_CHOICE. | |
| 377 { SEC_ASN1_ANY, offsetof(ResponseData, der_responder_id) }, | |
| 378 { SEC_ASN1_GENERALIZED_TIME, offsetof(ResponseData, produced_at) }, | |
| 379 { SEC_ASN1_SEQUENCE_OF, offsetof(ResponseData, single_responses), | |
| 380 kSingleResponseTemplate }, | |
| 381 { SEC_ASN1_SKIP_REST }, | |
| 382 { 0 } | |
| 383 }; | |
| 384 | |
| 385 // BasicOCSPResponse ::= SEQUENCE { | |
| 386 // tbsResponseData ResponseData, | |
| 387 // signatureAlgorithm AlgorithmIdentifier, | |
| 388 // signature BIT STRING, | |
| 389 // certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL } | |
| 390 struct BasicOCSPResponse { | |
| 391 ResponseData tbs_response_data; | |
| 392 // We do not care about the rest. | |
| 393 }; | |
| 394 | |
| 395 const SEC_ASN1Template kBasicOCSPResponseTemplate[] = { | |
| 396 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(BasicOCSPResponse) }, | |
| 397 { SEC_ASN1_INLINE, offsetof(BasicOCSPResponse, tbs_response_data), | |
| 398 kResponseDataTemplate }, | |
| 399 { SEC_ASN1_SKIP_REST }, | |
| 400 { 0 } | |
| 401 }; | |
| 402 | |
| 403 bool StringEqualToSECItem(const std::string& value1, const SECItem& value2) { | |
| 404 if (value1.size() != value2.len) | |
| 405 return false; | |
| 406 return memcmp(value1.data(), value2.data, value2.len) == 0; | |
| 407 } | |
| 408 | |
| 409 // TODO(ekasper): also use the issuer name hash in matching. | |
| 410 bool CertIDsMatch(const CertID& cert_id, | |
| 411 const std::string& serial_number, | |
| 412 const std::string& issuer_key_sha1_hash, | |
| 413 const std::string& issuer_key_sha256_hash) { | |
| 414 if (!StringEqualToSECItem(serial_number, cert_id.serial_number)) | |
| 415 return false; | |
| 416 | |
| 417 SECOidTag hash_alg = SECOID_FindOIDTag(&cert_id.hash_algorithm.algorithm); | |
| 418 switch (hash_alg) { | |
| 419 case SEC_OID_SHA1: | |
| 420 return StringEqualToSECItem(issuer_key_sha1_hash, | |
| 421 cert_id.issuer_key_hash); | |
| 422 case SEC_OID_SHA256: | |
| 423 return StringEqualToSECItem(issuer_key_sha256_hash, | |
| 424 cert_id.issuer_key_hash); | |
| 425 default: | |
| 426 return false; | |
| 427 } | |
| 428 } | |
| 429 | |
| 183 } // namespace | 430 } // namespace |
| 184 | 431 |
| 185 bool ExtractEmbeddedSCTList(X509Certificate::OSCertHandle cert, | 432 bool ExtractEmbeddedSCTList(X509Certificate::OSCertHandle cert, |
| 186 std::string* sct_list) { | 433 std::string* sct_list) { |
| 187 DCHECK(cert); | 434 DCHECK(cert); |
| 188 | 435 |
| 189 NSSCertWrapper leaf_cert(cert); | 436 NSSCertWrapper leaf_cert(cert); |
| 190 if (!leaf_cert.cert) | 437 if (!leaf_cert.cert) |
| 191 return false; | 438 return false; |
| 192 | 439 |
| 193 return GetOctetStringExtension( | 440 return GetCertOctetStringExtension(leaf_cert.cert.get(), |
| 194 leaf_cert.cert.get(), g_ct_singleton.Get().embedded_oid(), sct_list); | 441 g_ct_singleton.Get().embedded_oid(), |
| 442 sct_list); | |
| 195 } | 443 } |
| 196 | 444 |
| 197 bool GetPrecertLogEntry(X509Certificate::OSCertHandle leaf, | 445 bool GetPrecertLogEntry(X509Certificate::OSCertHandle leaf, |
| 198 X509Certificate::OSCertHandle issuer, | 446 X509Certificate::OSCertHandle issuer, |
| 199 LogEntry* result) { | 447 LogEntry* result) { |
| 200 DCHECK(leaf); | 448 DCHECK(leaf); |
| 201 DCHECK(issuer); | 449 DCHECK(issuer); |
| 202 | 450 |
| 203 NSSCertWrapper leaf_cert(leaf); | 451 NSSCertWrapper leaf_cert(leaf); |
| 204 NSSCertWrapper issuer_cert(issuer); | 452 NSSCertWrapper issuer_cert(issuer); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 std::string encoded; | 510 std::string encoded; |
| 263 if (!X509Certificate::GetDEREncoded(leaf, &encoded)) | 511 if (!X509Certificate::GetDEREncoded(leaf, &encoded)) |
| 264 return false; | 512 return false; |
| 265 | 513 |
| 266 result->Reset(); | 514 result->Reset(); |
| 267 result->type = ct::LogEntry::LOG_ENTRY_TYPE_X509; | 515 result->type = ct::LogEntry::LOG_ENTRY_TYPE_X509; |
| 268 result->leaf_certificate.swap(encoded); | 516 result->leaf_certificate.swap(encoded); |
| 269 return true; | 517 return true; |
| 270 } | 518 } |
| 271 | 519 |
| 520 bool ExtractSCTListFromOCSPResponse(X509Certificate::OSCertHandle issuer, | |
| 521 const std::string& cert_serial_number, | |
| 522 const std::string& ocsp_response, | |
| 523 std::string* sct_list) { | |
| 524 DCHECK(issuer); | |
| 525 crypto::ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE)); | |
| 526 | |
| 527 OCSPResponse response; | |
| 528 memset(&response, 0, sizeof(response)); | |
| 529 | |
| 530 SECItem src = { siBuffer, | |
| 531 reinterpret_cast<unsigned char*>(const_cast<char*>( | |
| 532 ocsp_response.data())), ocsp_response.size() }; | |
| 533 | |
| 534 // |response| will point directly into |src|, so it's not necessary to | |
| 535 // free the |response| contents, but they may only be used while |src| | |
| 536 // is valid (i.e., in this method). | |
| 537 SECStatus rv = SEC_QuickDERDecodeItem(arena.get(), &response, | |
| 538 kOCSPResponseTemplate, &src); | |
| 539 if (rv != SECSuccess) | |
| 540 return false; | |
| 541 | |
| 542 if (!response.response_bytes) | |
| 543 return false; | |
| 544 | |
| 545 if (!SECITEM_ItemsAreEqual(&kBasicOCSPResponseOidItem, | |
| 546 &response.response_bytes->response_type)) { | |
| 547 return false; | |
| 548 } | |
| 549 | |
| 550 BasicOCSPResponse basic_response; | |
| 551 memset(&basic_response, 0, sizeof(basic_response)); | |
| 552 | |
| 553 rv = SEC_QuickDERDecodeItem(arena.get(), &basic_response, | |
| 554 kBasicOCSPResponseTemplate, | |
| 555 &response.response_bytes->der_response); | |
| 556 if (rv != SECSuccess) | |
| 557 return false; | |
| 558 | |
| 559 SingleResponse** responses = | |
| 560 basic_response.tbs_response_data.single_responses; | |
| 561 if (!responses) | |
| 562 return false; | |
| 563 | |
| 564 std::string issuer_der; | |
| 565 if (!X509Certificate::GetDEREncoded(issuer, &issuer_der)) | |
| 566 return false; | |
| 567 | |
| 568 base::StringPiece issuer_spki; | |
| 569 if (!asn1::ExtractSPKIFromDERCert(issuer_der, &issuer_spki)) | |
| 570 return false; | |
| 571 | |
| 572 // In OCSP, only the key itself is under hash. | |
| 573 base::StringPiece issuer_spk; | |
| 574 if (!asn1::ExtractSubjectPublicKeyFromSPKI(issuer_spki, &issuer_spk)) | |
| 575 return false; | |
| 576 | |
| 577 // ExtractSubjectPublicKey... does not remove the initial octet encoding the | |
| 578 // number of unused bits in the ASN.1 BIT STRING so we do it here. For public | |
| 579 // keys, the bitstring is in practice always byte-aligned. | |
| 580 if (issuer_spk.empty() || issuer_spk[0] != 0) | |
| 581 return false; | |
| 582 issuer_spk.remove_prefix(1); | |
| 583 | |
| 584 // NSS OCSP lib recognizes SHA1, MD5 and MD2; MD5 and MD2 are dead but | |
| 585 // https://bugzilla.mozilla.org/show_bug.cgi?id=663315 will add SHA-256 | |
| 586 // and SHA-384. | |
| 587 // TODO(ekasper): add SHA-384 to crypto/sha2.h and here if it proves | |
| 588 // necessary. | |
| 589 // TODO(ekasper): only compute the hashes on demand. | |
| 590 std::string issuer_key_sha256_hash = crypto::SHA256HashString(issuer_spk); | |
| 591 std::string issuer_key_sha1_hash = base::SHA1HashString( | |
| 592 issuer_spk.as_string()); | |
| 593 | |
| 594 const SingleResponse* match = NULL; | |
| 595 for (const SingleResponse* const* resps = responses; *resps; ++resps) { | |
| 596 const SingleResponse* resp = *resps; | |
| 597 if (CertIDsMatch(resp->cert_id, cert_serial_number, | |
| 598 issuer_key_sha1_hash, issuer_key_sha256_hash)) { | |
| 599 match = resp; | |
| 600 break; | |
| 601 } | |
| 602 } | |
| 603 | |
| 604 if (!match) | |
| 605 return false; | |
| 606 | |
| 607 return GetSCTListFromOCSPExtension(arena.get(), match->single_extensions, | |
| 608 sct_list); | |
| 609 } | |
| 610 | |
| 272 } // namespace ct | 611 } // namespace ct |
| 273 | 612 |
| 274 } // namespace net | 613 } // namespace net |
| OLD | NEW |