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 | |
65 // Initializes the necessary NSS internals for use with Certificate | 83 // Initializes the necessary NSS internals for use with Certificate |
66 // Transparency. | 84 // Transparency. |
67 class CTInitSingleton { | 85 class CTInitSingleton { |
68 public: | 86 public: |
69 SECOidTag embedded_oid() const { return embedded_oid_; } | 87 SECOidTag embedded_oid() const { return embedded_oid_; } |
70 | 88 |
71 private: | 89 private: |
72 friend struct base::DefaultLazyInstanceTraits<CTInitSingleton>; | 90 friend struct base::DefaultLazyInstanceTraits<CTInitSingleton>; |
73 | 91 |
74 CTInitSingleton() : embedded_oid_(SEC_OID_UNKNOWN) { | 92 CTInitSingleton() : embedded_oid_(SEC_OID_UNKNOWN) { |
(...skipping 26 matching lines...) Expand all Loading... | |
101 | 119 |
102 SECOidTag embedded_oid_; | 120 SECOidTag embedded_oid_; |
103 | 121 |
104 DISALLOW_COPY_AND_ASSIGN(CTInitSingleton); | 122 DISALLOW_COPY_AND_ASSIGN(CTInitSingleton); |
105 }; | 123 }; |
106 | 124 |
107 base::LazyInstance<CTInitSingleton>::Leaky g_ct_singleton = | 125 base::LazyInstance<CTInitSingleton>::Leaky g_ct_singleton = |
108 LAZY_INSTANCE_INITIALIZER; | 126 LAZY_INSTANCE_INITIALIZER; |
109 | 127 |
110 // Obtains the data for an X.509v3 certificate extension identified by |oid| | 128 // 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, | 129 // 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 | 130 // the certificate, updating |ext_data| to be the extension data after removing |
113 // encoding of OCTET STRING. | 131 // the DER encoding of OCTET STRING. |
114 bool GetOctetStringExtension(CERTCertificate* cert, | 132 bool GetCertOctetStringExtension(CERTCertificate* cert, |
115 SECOidTag oid, | 133 SECOidTag oid, |
116 std::string* extension_data) { | 134 std::string* extension_data) { |
117 SECItem extension; | 135 SECItem extension; |
118 SECStatus rv = CERT_FindCertExtension(cert, oid, &extension); | 136 SECStatus rv = CERT_FindCertExtension(cert, oid, &extension); |
119 if (rv != SECSuccess) | 137 if (rv != SECSuccess) |
120 return false; | 138 return false; |
121 | 139 |
122 base::StringPiece raw_data(reinterpret_cast<char*>(extension.data), | 140 base::StringPiece raw_data(reinterpret_cast<char*>(extension.data), |
123 extension.len); | 141 extension.len); |
124 base::StringPiece parsed_data; | 142 base::StringPiece parsed_data; |
125 if (!asn1::GetElement(&raw_data, asn1::kOCTETSTRING, &parsed_data) || | 143 if (!asn1::GetElement(&raw_data, asn1::kOCTETSTRING, &parsed_data) || |
126 raw_data.size() > 0) { // Decoding failure or raw data left | 144 raw_data.size() > 0) { // Decoding failure or raw data left |
127 rv = SECFailure; | 145 rv = SECFailure; |
128 } else { | 146 } else { |
129 parsed_data.CopyToString(extension_data); | 147 parsed_data.CopyToString(extension_data); |
130 } | 148 } |
131 | 149 |
132 SECITEM_FreeItem(&extension, PR_FALSE); | 150 SECITEM_FreeItem(&extension, PR_FALSE); |
133 return rv == SECSuccess; | 151 return rv == SECSuccess; |
134 } | 152 } |
135 | 153 |
154 // NSS offers CERT_FindCertExtension for certificates, but we also need to | |
155 // to extract extensions from OCSP responses, so we inspect the extensions | |
156 // manually. | |
Ryan Sleevi
2013/12/11 07:33:05
comment nit: avoid pronouns
// NSS offers CERT_Fi
ekasper
2013/12/11 15:40:51
I find the plural construct a little odd but I've
| |
157 // | |
158 // Obtains the data for an OCSP extension identified by kOCSPExtensionOidItem | |
159 // and encoded as an OCTET STRING. Returns true if the extension was found in | |
160 // |extensions|, updating |extension_data| to be the extension data after | |
161 // removing the DER encoding of OCTET STRING. | |
162 bool GetSCTListFromOCSPExtension(PLArenaPool* arena, | |
163 const CERTCertExtension* const* extensions, | |
164 std::string* extension_data) { | |
165 if (!extensions) | |
166 return false; | |
167 | |
168 const CERTCertExtension* match = NULL; | |
169 | |
170 for (const CERTCertExtension* const* exts = extensions; *exts; ++exts) { | |
171 const CERTCertExtension* ext = *exts; | |
172 if (SECITEM_ItemsAreEqual(&kOCSPExtensionOidItem, &ext->id)) { | |
173 match = ext; | |
174 break; | |
175 } | |
176 } | |
177 | |
178 if (!match) | |
179 return false; | |
180 | |
181 SECItem contents; | |
182 // QuickDER will point to |match|, so we don't have to free |contents|. | |
Ryan Sleevi
2013/12/11 07:33:05
comment nit: pronouns
// With QuickDER, |contents
ekasper
2013/12/11 15:40:51
Done.
| |
183 SECStatus rv = SEC_QuickDERDecodeItem(arena, &contents, | |
184 SEC_ASN1_GET(SEC_OctetStringTemplate), | |
185 &match->value); | |
186 if (rv != SECSuccess) | |
187 return false; | |
188 | |
189 base::StringPiece parsed_data(reinterpret_cast<char*>(contents.data), | |
190 contents.len); | |
191 parsed_data.CopyToString(extension_data); | |
192 return true; | |
193 } | |
194 | |
136 // Given a |cert|, extract the TBSCertificate from this certificate, also | 195 // 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, | 196 // removing the X.509 extension with OID 1.3.6.1.4.1.11129.2.4.2 (that is, |
138 // the embedded SCT) | 197 // the embedded SCT) |
139 bool ExtractTBSCertWithoutSCTs(CERTCertificate* cert, | 198 bool ExtractTBSCertWithoutSCTs(CERTCertificate* cert, |
140 std::string* to_be_signed) { | 199 std::string* to_be_signed) { |
141 SECOidData* oid = SECOID_FindOIDByTag(g_ct_singleton.Get().embedded_oid()); | 200 SECOidData* oid = SECOID_FindOIDByTag(g_ct_singleton.Get().embedded_oid()); |
142 if (!oid) | 201 if (!oid) |
143 return false; | 202 return false; |
144 | 203 |
145 // This is a giant hack, due to the fact that NSS does not expose a good API | 204 // 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, | 232 &tbs_data, |
174 &temp_cert, | 233 &temp_cert, |
175 SEC_ASN1_GET(CERT_CertificateTemplate)); | 234 SEC_ASN1_GET(CERT_CertificateTemplate)); |
176 if (!result) | 235 if (!result) |
177 return false; | 236 return false; |
178 | 237 |
179 to_be_signed->assign(reinterpret_cast<char*>(tbs_data.data), tbs_data.len); | 238 to_be_signed->assign(reinterpret_cast<char*>(tbs_data.data), tbs_data.len); |
180 return true; | 239 return true; |
181 } | 240 } |
182 | 241 |
242 // The following code is adapted from the NSS OCSP module, in order to expose | |
243 // the internal structure of an OCSP response. | |
244 | |
245 // ResponseBytes ::= SEQUENCE { | |
246 // responseType OBJECT IDENTIFIER, | |
247 // response OCTET STRING } | |
248 struct ResponseBytes { | |
249 SECItem response_type; | |
250 SECItem der_response; | |
251 }; | |
252 | |
253 const SEC_ASN1Template kResponseBytesTemplate[] = { | |
254 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(ResponseBytes) }, | |
255 { SEC_ASN1_OBJECT_ID, offsetof(ResponseBytes, response_type) }, | |
256 { SEC_ASN1_OCTET_STRING, offsetof(ResponseBytes, der_response) }, | |
257 { 0 } | |
258 }; | |
259 | |
260 // OCSPResponse ::= SEQUENCE { | |
261 // responseStatus OCSPResponseStatus, | |
262 // responseBytes [0] EXPLICIT ResponseBytes OPTIONAL } | |
263 struct OCSPResponse { | |
264 SECItem response_status; | |
265 // This indirection is needed because |response_bytes| is an optional | |
266 // component and we need a way to determine if it is missing. | |
267 ResponseBytes* response_bytes; | |
268 }; | |
269 | |
270 const SEC_ASN1Template kPointerToResponseBytesTemplate[] = { | |
271 { SEC_ASN1_POINTER, 0, kResponseBytesTemplate } | |
272 }; | |
273 | |
274 const SEC_ASN1Template kOCSPResponseTemplate[] = { | |
275 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(OCSPResponse) }, | |
276 { SEC_ASN1_ENUMERATED, offsetof(OCSPResponse, response_status) }, | |
277 { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | | |
278 SEC_ASN1_CONTEXT_SPECIFIC | 0, offsetof(OCSPResponse, response_bytes), | |
279 kPointerToResponseBytesTemplate }, | |
280 { 0 } | |
281 }; | |
282 | |
283 // CertID ::= SEQUENCE { | |
284 // hashAlgorithm AlgorithmIdentifier, | |
285 // issuerNameHash OCTET STRING, -- Hash of Issuer's DN | |
286 // issuerKeyHash OCTET STRING, -- Hash of Issuers public key | |
287 // serialNumber CertificateSerialNumber } | |
288 struct CertID { | |
289 SECAlgorithmID hash_algorithm; | |
290 SECItem issuer_name_hash; | |
291 SECItem issuer_key_hash; | |
292 SECItem serial_number; | |
293 }; | |
294 | |
295 const SEC_ASN1Template kCertIDTemplate[] = { | |
296 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CertID) }, | |
297 { SEC_ASN1_INLINE | SEC_ASN1_XTRN, offsetof(CertID, hash_algorithm), | |
298 SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, | |
299 { SEC_ASN1_OCTET_STRING, offsetof(CertID, issuer_name_hash) }, | |
300 { SEC_ASN1_OCTET_STRING, offsetof(CertID, issuer_key_hash) }, | |
301 { SEC_ASN1_INTEGER, offsetof(CertID, serial_number) }, | |
302 { 0 } | |
303 }; | |
304 | |
305 // SingleResponse ::= SEQUENCE { | |
306 // certID CertID, | |
307 // certStatus CertStatus, | |
308 // thisUpdate GeneralizedTime, | |
309 // nextUpdate [0] EXPLICIT GeneralizedTime OPTIONAL, | |
310 // singleExtensions [1] EXPLICIT Extensions OPTIONAL } | |
311 struct SingleResponse { | |
312 CertID cert_id; | |
313 // The following three fields are not used. | |
314 // TODO(ekasper): consider removing them and using SEC_ASN1_SKIP to | |
315 // skip the decoding. | |
316 SECItem der_cert_status; | |
317 SECItem this_update; | |
318 SECItem next_update; | |
319 CERTCertExtension** single_extensions; | |
320 }; | |
321 | |
322 const SEC_ASN1Template kSingleResponseTemplate[] = { | |
323 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SingleResponse) }, | |
324 { SEC_ASN1_INLINE, offsetof(SingleResponse, cert_id), kCertIDTemplate }, | |
325 // Really a CHOICE but we make it an ANY because we don't care about the | |
326 // contents of this field. | |
327 // TODO(ekasper): use SEC_ASN1_CHOICE. | |
328 { SEC_ASN1_ANY, offsetof(SingleResponse, der_cert_status) }, | |
329 { SEC_ASN1_GENERALIZED_TIME, offsetof(SingleResponse, this_update) }, | |
330 { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | | |
331 SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0, | |
332 offsetof(SingleResponse, next_update), | |
333 SEC_ASN1_SUB(SEC_GeneralizedTimeTemplate) }, | |
334 { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | | |
335 SEC_ASN1_CONTEXT_SPECIFIC | 1, | |
336 offsetof(SingleResponse, single_extensions), | |
337 SEC_ASN1_SUB(CERT_SequenceOfCertExtensionTemplate) }, | |
338 { 0 } | |
339 }; | |
340 | |
341 // ResponseData ::= SEQUENCE { | |
342 // version [0] EXPLICIT Version DEFAULT v1, | |
343 // responderID ResponderID, | |
344 // producedAt GeneralizedTime, | |
345 // responses SEQUENCE OF SingleResponse, | |
346 // responseExtensions [1] EXPLICIT Extensions OPTIONAL } | |
347 struct ResponseData { | |
348 // The first three fields are not used. | |
349 // TODO(ekasper): consider removing them and using SEC_ASN1_SKIP to | |
350 // skip the decoding. | |
351 SECItem version; | |
352 SECItem der_responder_id; | |
353 SECItem produced_at; | |
354 SingleResponse** single_responses; | |
355 // Skip extensions. | |
356 }; | |
357 | |
358 const SEC_ASN1Template kResponseDataTemplate[] = { | |
359 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(ResponseData) }, | |
360 { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | | |
361 SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0, | |
362 offsetof(ResponseData, version), SEC_ASN1_SUB(SEC_IntegerTemplate) }, | |
363 // Really a CHOICE but we make it an ANY because we don't care about the | |
364 // contents of this field. | |
365 // TODO(ekasper): use SEC_ASN1_CHOICE. | |
366 { SEC_ASN1_ANY, offsetof(ResponseData, der_responder_id) }, | |
367 { SEC_ASN1_GENERALIZED_TIME, offsetof(ResponseData, produced_at) }, | |
368 { SEC_ASN1_SEQUENCE_OF, offsetof(ResponseData, single_responses), | |
369 kSingleResponseTemplate }, | |
370 { SEC_ASN1_SKIP_REST }, | |
371 { 0 } | |
372 }; | |
373 | |
374 // BasicOCSPResponse ::= SEQUENCE { | |
375 // tbsResponseData ResponseData, | |
376 // signatureAlgorithm AlgorithmIdentifier, | |
377 // signature BIT STRING, | |
378 // certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL } | |
379 struct BasicOCSPResponse { | |
380 ResponseData tbs_response_data; | |
381 // We do not care about the rest. | |
382 }; | |
383 | |
384 const SEC_ASN1Template kBasicOCSPResponseTemplate[] = { | |
385 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(BasicOCSPResponse) }, | |
386 { SEC_ASN1_INLINE, offsetof(BasicOCSPResponse, tbs_response_data), | |
387 kResponseDataTemplate }, | |
388 { SEC_ASN1_SKIP_REST }, | |
389 { 0 } | |
390 }; | |
391 | |
392 bool StringEqualToSECItem(const std::string& value1, const SECItem& value2) { | |
393 if (value1.size() != value2.len) | |
394 return false; | |
395 return memcmp(value1.data(), value2.data, value2.len) == 0; | |
396 } | |
397 | |
398 // TODO(ekasper): also use the issuer name hash in matching. | |
399 bool CertIDsMatch(const CertID& cert_id, | |
400 const std::string& serial_number, | |
401 const std::string& issuer_key_sha1_hash, | |
402 const std::string& issuer_key_sha256_hash) { | |
403 if (!StringEqualToSECItem(serial_number, cert_id.serial_number)) | |
404 return false; | |
405 | |
406 SECOidTag hash_alg = SECOID_FindOIDTag(&cert_id.hash_algorithm.algorithm); | |
407 switch (hash_alg) { | |
408 case SEC_OID_SHA1: | |
409 return StringEqualToSECItem(issuer_key_sha1_hash, | |
410 cert_id.issuer_key_hash); | |
411 case SEC_OID_SHA256: | |
412 return StringEqualToSECItem(issuer_key_sha256_hash, | |
413 cert_id.issuer_key_hash); | |
414 default: | |
415 return false; | |
416 } | |
417 } | |
418 | |
183 } // namespace | 419 } // namespace |
184 | 420 |
185 bool ExtractEmbeddedSCTList(X509Certificate::OSCertHandle cert, | 421 bool ExtractEmbeddedSCTList(X509Certificate::OSCertHandle cert, |
186 std::string* sct_list) { | 422 std::string* sct_list) { |
187 DCHECK(cert); | 423 DCHECK(cert); |
188 | 424 |
189 NSSCertWrapper leaf_cert(cert); | 425 NSSCertWrapper leaf_cert(cert); |
190 if (!leaf_cert.cert) | 426 if (!leaf_cert.cert) |
191 return false; | 427 return false; |
192 | 428 |
193 return GetOctetStringExtension( | 429 return GetCertOctetStringExtension(leaf_cert.cert.get(), |
194 leaf_cert.cert.get(), g_ct_singleton.Get().embedded_oid(), sct_list); | 430 g_ct_singleton.Get().embedded_oid(), |
431 sct_list); | |
195 } | 432 } |
196 | 433 |
197 bool GetPrecertLogEntry(X509Certificate::OSCertHandle leaf, | 434 bool GetPrecertLogEntry(X509Certificate::OSCertHandle leaf, |
198 X509Certificate::OSCertHandle issuer, | 435 X509Certificate::OSCertHandle issuer, |
199 LogEntry* result) { | 436 LogEntry* result) { |
200 DCHECK(leaf); | 437 DCHECK(leaf); |
201 DCHECK(issuer); | 438 DCHECK(issuer); |
202 | 439 |
203 NSSCertWrapper leaf_cert(leaf); | 440 NSSCertWrapper leaf_cert(leaf); |
204 NSSCertWrapper issuer_cert(issuer); | 441 NSSCertWrapper issuer_cert(issuer); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 std::string encoded; | 499 std::string encoded; |
263 if (!X509Certificate::GetDEREncoded(leaf, &encoded)) | 500 if (!X509Certificate::GetDEREncoded(leaf, &encoded)) |
264 return false; | 501 return false; |
265 | 502 |
266 result->Reset(); | 503 result->Reset(); |
267 result->type = ct::LogEntry::LOG_ENTRY_TYPE_X509; | 504 result->type = ct::LogEntry::LOG_ENTRY_TYPE_X509; |
268 result->leaf_certificate.swap(encoded); | 505 result->leaf_certificate.swap(encoded); |
269 return true; | 506 return true; |
270 } | 507 } |
271 | 508 |
509 bool ExtractSCTListFromOCSPResponse(X509Certificate::OSCertHandle issuer, | |
510 const std::string& cert_serial_number, | |
511 const std::string& ocsp_response, | |
512 std::string* sct_list) { | |
513 DCHECK(issuer); | |
514 crypto::ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE)); | |
515 | |
516 // QuickDERDecodeItem returns data that points into |src|. This is | |
517 // ok for us as the decoded items never leave the method scope. | |
Ryan Sleevi
2013/12/11 07:33:05
This comment doesn't make much sense, because it's
ekasper
2013/12/11 15:40:51
Done.
| |
518 SECItem src = { siBuffer, | |
519 reinterpret_cast<unsigned char*>(const_cast<char*>( | |
520 ocsp_response.data())), ocsp_response.size() }; | |
521 | |
522 OCSPResponse response; | |
523 memset(&response, 0, sizeof(response)); | |
524 | |
525 SECStatus rv = SEC_QuickDERDecodeItem(arena.get(), &response, | |
526 kOCSPResponseTemplate, &src); | |
527 if (rv != SECSuccess) | |
528 return false; | |
529 | |
530 // id-ad-ocsp: 1.3.6.1.5.5.7.48.1.1 | |
531 static const uint8 kBasicOCSPResponseOID[] = { 0x2b, 0x06, 0x01, 0x05, 0x05, | |
532 0x07, 0x30, 0x01, 0x01 }; | |
533 | |
534 if (!response.response_bytes) | |
535 return false; | |
536 | |
537 if (response.response_bytes->response_type.len != | |
538 sizeof(kBasicOCSPResponseOID) || | |
539 memcmp(response.response_bytes->response_type.data, kBasicOCSPResponseOID, | |
540 sizeof(kBasicOCSPResponseOID)) != 0) { | |
Ryan Sleevi
2013/12/11 07:33:05
Would this make more sense as a synthetic SECITEM
ekasper
2013/12/11 15:40:51
Done.
| |
541 return false; | |
542 } | |
543 | |
544 BasicOCSPResponse basic_response; | |
545 memset(&basic_response, 0, sizeof(basic_response)); | |
546 | |
547 rv = SEC_QuickDERDecodeItem(arena.get(), &basic_response, | |
548 kBasicOCSPResponseTemplate, | |
549 &response.response_bytes->der_response); | |
550 if (rv != SECSuccess) | |
551 return false; | |
552 | |
553 SingleResponse** responses = | |
554 basic_response.tbs_response_data.single_responses; | |
555 if (!responses) | |
556 return false; | |
557 | |
558 std::string issuer_der; | |
559 if (!X509Certificate::GetDEREncoded(issuer, &issuer_der)) | |
560 return false; | |
561 | |
562 base::StringPiece issuer_spki; | |
563 if (!asn1::ExtractSPKIFromDERCert(issuer_der, &issuer_spki)) | |
564 return false; | |
565 | |
566 // In OCSP, only the key itself is under hash. | |
567 base::StringPiece issuer_spk; | |
568 if (!asn1::ExtractSubjectPublicKeyFromSPKI(issuer_spki, &issuer_spk)) | |
569 return false; | |
570 | |
571 // ExtractSubjectPublicKey... does not remove the initial octet encoding the | |
572 // number of unused bits in the ASN.1 BIT STRING so we do it here. For public | |
573 // keys, the bitstring is in practice always byte-aligned. | |
574 if (issuer_spk.empty() || issuer_spk[0] != 0) | |
575 return false; | |
576 issuer_spk.remove_prefix(1); | |
577 | |
578 // NSS OCSP lib recognizes SHA1, MD5 and MD2; MD5 and MD2 are dead but | |
579 // https://bugzilla.mozilla.org/show_bug.cgi?id=663315 will add SHA-256 | |
580 // and SHA-384. | |
581 // TODO(ekasper): add SHA-384 to crypto/sha2.h and here if it proves | |
582 // necessary. | |
583 // TODO(ekasper): only compute the hashes on demand. | |
584 std::string issuer_key_sha256_hash = crypto::SHA256HashString(issuer_spk); | |
585 std::string issuer_key_sha1_hash = base::SHA1HashString( | |
586 issuer_spk.as_string()); | |
587 | |
588 SingleResponse* match = NULL; | |
589 for (int i = 0; responses[i] != NULL; ++i) { | |
Ryan Sleevi
2013/12/11 07:33:05
s/int/size_t here
ekasper
2013/12/11 15:40:51
I've changed it to match the style of similar loop
| |
590 if (CertIDsMatch(responses[i]->cert_id, cert_serial_number, | |
591 issuer_key_sha1_hash, issuer_key_sha256_hash)) { | |
592 match = responses[i]; | |
593 break; | |
594 } | |
595 } | |
596 | |
597 if (!match) | |
598 return false; | |
599 | |
600 return GetSCTListFromOCSPExtension(arena.get(), match->single_extensions, | |
601 sct_list); | |
602 } | |
603 | |
272 } // namespace ct | 604 } // namespace ct |
273 | 605 |
274 } // namespace net | 606 } // namespace net |
OLD | NEW |