1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/cert/ct_objects_extractor.h"
6
7 #include <string.h>
8
9 #include "base/hash/sha1.h"
10 #include "base/logging.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_util.h"
13 #include "crypto/sha2.h"
14 #include "net/cert/asn1_util.h"
15 #include "net/cert/signed_certificate_timestamp.h"
16 #include "net/cert/x509_util.h"
17 #include "third_party/boringssl/src/include/openssl/bytestring.h"
18 #include "third_party/boringssl/src/include/openssl/mem.h"
19
20 namespace net::ct {
21
22 namespace {
23
24 // The wire form of the OID 1.3.6.1.4.1.11129.2.4.5 - OCSP SingleExtension for
25 // X.509v3 Certificate Transparency Signed Certificate Timestamp List, see
26 // Section 3.3 of RFC6962.
27 const uint8_t kOCSPExtensionOid[] = {0x2B, 0x06, 0x01, 0x04, 0x01,
28 0xD6, 0x79, 0x02, 0x04, 0x05};
29
30 // The wire form of the OID 1.3.6.1.5.5.7.48.1.1. See RFC 6960.
31 const uint8_t kOCSPBasicResponseOid[] = {0x2b, 0x06, 0x01, 0x05, 0x05,
32 0x07, 0x30, 0x01, 0x01};
33
34 // The wire form of the OID 1.3.14.3.2.26.
35 const uint8_t kSHA1Oid[] = {0x2b, 0x0e, 0x03, 0x02, 0x1a};
36
37 // The wire form of the OID 2.16.840.1.101.3.4.2.1.
38 const uint8_t kSHA256Oid[] = {0x60, 0x86, 0x48, 0x01, 0x65,
39 0x03, 0x04, 0x02, 0x01};
40
StringEqualToCBS(const std::string & value1,const CBS * value2)41 bool StringEqualToCBS(const std::string& value1, const CBS* value2) {
42 if (CBS_len(value2) != value1.size())
43 return false;
44 return memcmp(value1.data(), CBS_data(value2), CBS_len(value2)) == 0;
45 }
46
SkipElements(CBS * cbs,int count)47 bool SkipElements(CBS* cbs, int count) {
48 for (int i = 0; i < count; i++) {
49 if (!CBS_get_any_asn1_element(cbs, nullptr, nullptr, nullptr))
50 return false;
51 }
52 return true;
53 }
54
SkipOptionalElement(CBS * cbs,unsigned tag)55 bool SkipOptionalElement(CBS* cbs, unsigned tag) {
56 CBS unused;
57 return !CBS_peek_asn1_tag(cbs, tag) || CBS_get_asn1(cbs, &unused, tag);
58 }
59
60 // Copies all the bytes in |outer| which are before |inner| to |out|. |inner|
61 // must be a subset of |outer|.
CopyBefore(const CBS & outer,const CBS & inner,CBB * out)62 bool CopyBefore(const CBS& outer, const CBS& inner, CBB* out) {
63 CHECK_LE(CBS_data(&outer), CBS_data(&inner));
64 CHECK_LE(CBS_data(&inner) + CBS_len(&inner),
65 CBS_data(&outer) + CBS_len(&outer));
66
67 return !!CBB_add_bytes(out, CBS_data(&outer),
68 CBS_data(&inner) - CBS_data(&outer));
69 }
70
71 // Copies all the bytes in |outer| which are after |inner| to |out|. |inner|
72 // must be a subset of |outer|.
CopyAfter(const CBS & outer,const CBS & inner,CBB * out)73 bool CopyAfter(const CBS& outer, const CBS& inner, CBB* out) {
74 CHECK_LE(CBS_data(&outer), CBS_data(&inner));
75 CHECK_LE(CBS_data(&inner) + CBS_len(&inner),
76 CBS_data(&outer) + CBS_len(&outer));
77
78 return !!CBB_add_bytes(
79 out, CBS_data(&inner) + CBS_len(&inner),
80 CBS_data(&outer) + CBS_len(&outer) - CBS_data(&inner) - CBS_len(&inner));
81 }
82
83 // Skips |tbs_cert|, which must be a TBSCertificate body, to just before the
84 // extensions element.
SkipTBSCertificateToExtensions(CBS * tbs_cert)85 bool SkipTBSCertificateToExtensions(CBS* tbs_cert) {
86 constexpr unsigned kVersionTag =
87 CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0;
88 constexpr unsigned kIssuerUniqueIDTag = CBS_ASN1_CONTEXT_SPECIFIC | 1;
89 constexpr unsigned kSubjectUniqueIDTag = CBS_ASN1_CONTEXT_SPECIFIC | 2;
90 return SkipOptionalElement(tbs_cert, kVersionTag) &&
91 SkipElements(tbs_cert,
92 6 /* serialNumber through subjectPublicKeyInfo */) &&
93 SkipOptionalElement(tbs_cert, kIssuerUniqueIDTag) &&
94 SkipOptionalElement(tbs_cert, kSubjectUniqueIDTag);
95 }
96
97 // Looks for the extension with the specified OID in |extensions|, which must
98 // contain the contents of a SEQUENCE of X.509 extension structures. If found,
99 // returns true and sets |*out| to the full extension element.
FindExtensionElement(const CBS & extensions,const uint8_t * oid,size_t oid_len,CBS * out)100 bool FindExtensionElement(const CBS& extensions,
101 const uint8_t* oid,
102 size_t oid_len,
103 CBS* out) {
104 CBS extensions_copy = extensions;
105 CBS result;
106 CBS_init(&result, nullptr, 0);
107 bool found = false;
108 while (CBS_len(&extensions_copy) > 0) {
109 CBS extension_element;
110 if (!CBS_get_asn1_element(&extensions_copy, &extension_element,
111 CBS_ASN1_SEQUENCE)) {
112 return false;
113 }
114
115 CBS copy = extension_element;
116 CBS extension, extension_oid;
117 if (!CBS_get_asn1(©, &extension, CBS_ASN1_SEQUENCE) ||
118 !CBS_get_asn1(&extension, &extension_oid, CBS_ASN1_OBJECT)) {
119 return false;
120 }
121
122 if (CBS_mem_equal(&extension_oid, oid, oid_len)) {
123 if (found)
124 return false;
125 found = true;
126 result = extension_element;
127 }
128 }
129 if (!found)
130 return false;
131
132 *out = result;
133 return true;
134 }
135
136 // Finds the SignedCertificateTimestampList in an extension with OID |oid| in
137 // |x509_exts|. If found, returns true and sets |*out_sct_list| to the encoded
138 // SCT list.
ParseSCTListFromExtensions(const CBS & extensions,const uint8_t * oid,size_t oid_len,std::string * out_sct_list)139 bool ParseSCTListFromExtensions(const CBS& extensions,
140 const uint8_t* oid,
141 size_t oid_len,
142 std::string* out_sct_list) {
143 CBS extension_element, extension, extension_oid, value, sct_list;
144 if (!FindExtensionElement(extensions, oid, oid_len, &extension_element) ||
145 !CBS_get_asn1(&extension_element, &extension, CBS_ASN1_SEQUENCE) ||
146 !CBS_get_asn1(&extension, &extension_oid, CBS_ASN1_OBJECT) ||
147 // Skip the optional critical element.
148 !SkipOptionalElement(&extension, CBS_ASN1_BOOLEAN) ||
149 // The extension value is stored in an OCTET STRING.
150 !CBS_get_asn1(&extension, &value, CBS_ASN1_OCTETSTRING) ||
151 CBS_len(&extension) != 0 ||
152 // The extension value itself is an OCTET STRING containing the
153 // serialized SCT list.
154 !CBS_get_asn1(&value, &sct_list, CBS_ASN1_OCTETSTRING) ||
155 CBS_len(&value) != 0) {
156 return false;
157 }
158
159 DCHECK(CBS_mem_equal(&extension_oid, oid, oid_len));
160 *out_sct_list = std::string(
161 reinterpret_cast<const char*>(CBS_data(&sct_list)), CBS_len(&sct_list));
162 return true;
163 }
164
165 // Finds the SingleResponse in |responses| which matches |issuer| and
166 // |cert_serial_number|. On success, returns true and sets
167 // |*out_single_response| to the body of the SingleResponse starting at the
168 // |certStatus| field.
FindMatchingSingleResponse(CBS * responses,const CRYPTO_BUFFER * issuer,const std::string & cert_serial_number,CBS * out_single_response)169 bool FindMatchingSingleResponse(CBS* responses,
170 const CRYPTO_BUFFER* issuer,
171 const std::string& cert_serial_number,
172 CBS* out_single_response) {
173 base::StringPiece issuer_spki;
174 if (!asn1::ExtractSPKIFromDERCert(
175 x509_util::CryptoBufferAsStringPiece(issuer), &issuer_spki))
176 return false;
177
178 // In OCSP, only the key itself is under hash.
179 base::StringPiece issuer_spk;
180 if (!asn1::ExtractSubjectPublicKeyFromSPKI(issuer_spki, &issuer_spk))
181 return false;
182
183 // ExtractSubjectPublicKeyFromSPKI does not remove the initial octet encoding
184 // the number of unused bits in the ASN.1 BIT STRING so we do it here. For
185 // public keys, the bitstring is in practice always byte-aligned.
186 if (issuer_spk.empty() || issuer_spk[0] != 0)
187 return false;
188 issuer_spk.remove_prefix(1);
189
190 // TODO(ekasper): add SHA-384 to crypto/sha2.h and here if it proves
191 // necessary.
192 // TODO(ekasper): only compute the hashes on demand.
193 std::string issuer_key_sha256_hash = crypto::SHA256HashString(issuer_spk);
194 std::string issuer_key_sha1_hash =
195 base::SHA1HashString(std::string(issuer_spk));
196
197 while (CBS_len(responses) > 0) {
198 CBS single_response, cert_id;
199 if (!CBS_get_asn1(responses, &single_response, CBS_ASN1_SEQUENCE) ||
200 !CBS_get_asn1(&single_response, &cert_id, CBS_ASN1_SEQUENCE)) {
201 return false;
202 }
203
204 CBS hash_algorithm, hash, serial_number, issuer_name_hash, issuer_key_hash;
205 if (!CBS_get_asn1(&cert_id, &hash_algorithm, CBS_ASN1_SEQUENCE) ||
206 !CBS_get_asn1(&hash_algorithm, &hash, CBS_ASN1_OBJECT) ||
207 !CBS_get_asn1(&cert_id, &issuer_name_hash, CBS_ASN1_OCTETSTRING) ||
208 !CBS_get_asn1(&cert_id, &issuer_key_hash, CBS_ASN1_OCTETSTRING) ||
209 !CBS_get_asn1(&cert_id, &serial_number, CBS_ASN1_INTEGER) ||
210 CBS_len(&cert_id) != 0) {
211 return false;
212 }
213
214 // Check the serial number matches.
215 if (!StringEqualToCBS(cert_serial_number, &serial_number))
216 continue;
217
218 // Check if the issuer_key_hash matches.
219 // TODO(ekasper): also use the issuer name hash in matching.
220 if (CBS_mem_equal(&hash, kSHA1Oid, sizeof(kSHA1Oid))) {
221 if (StringEqualToCBS(issuer_key_sha1_hash, &issuer_key_hash)) {
222 *out_single_response = single_response;
223 return true;
224 }
225 } else if (CBS_mem_equal(&hash, kSHA256Oid, sizeof(kSHA256Oid))) {
226 if (StringEqualToCBS(issuer_key_sha256_hash, &issuer_key_hash)) {
227 *out_single_response = single_response;
228 return true;
229 }
230 }
231 }
232
233 return false;
234 }
235
236 } // namespace
237
ExtractEmbeddedSCTList(const CRYPTO_BUFFER * cert,std::string * sct_list)238 bool ExtractEmbeddedSCTList(const CRYPTO_BUFFER* cert, std::string* sct_list) {
239 CBS cert_cbs;
240 CBS_init(&cert_cbs, CRYPTO_BUFFER_data(cert), CRYPTO_BUFFER_len(cert));
241 CBS cert_body, tbs_cert, extensions_wrap, extensions;
242 if (!CBS_get_asn1(&cert_cbs, &cert_body, CBS_ASN1_SEQUENCE) ||
243 CBS_len(&cert_cbs) != 0 ||
244 !CBS_get_asn1(&cert_body, &tbs_cert, CBS_ASN1_SEQUENCE) ||
245 !SkipTBSCertificateToExtensions(&tbs_cert) ||
246 // Extract the extensions list.
247 !CBS_get_asn1(&tbs_cert, &extensions_wrap,
248 CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 3) ||
249 !CBS_get_asn1(&extensions_wrap, &extensions, CBS_ASN1_SEQUENCE) ||
250 CBS_len(&extensions_wrap) != 0 || CBS_len(&tbs_cert) != 0) {
251 return false;
252 }
253
254 return ParseSCTListFromExtensions(extensions, kEmbeddedSCTOid,
255 sizeof(kEmbeddedSCTOid), sct_list);
256 }
257
GetPrecertSignedEntry(const CRYPTO_BUFFER * leaf,const CRYPTO_BUFFER * issuer,SignedEntryData * result)258 bool GetPrecertSignedEntry(const CRYPTO_BUFFER* leaf,
259 const CRYPTO_BUFFER* issuer,
260 SignedEntryData* result) {
261 result->Reset();
262
263 // Parse the TBSCertificate.
264 CBS cert_cbs;
265 CBS_init(&cert_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
266 CBS cert_body, tbs_cert;
267 if (!CBS_get_asn1(&cert_cbs, &cert_body, CBS_ASN1_SEQUENCE) ||
268 CBS_len(&cert_cbs) != 0 ||
269 !CBS_get_asn1(&cert_body, &tbs_cert, CBS_ASN1_SEQUENCE)) {
270 return false;
271 }
272
273 CBS tbs_cert_copy = tbs_cert;
274 if (!SkipTBSCertificateToExtensions(&tbs_cert))
275 return false;
276
277 // Start filling in a new TBSCertificate. Copy everything parsed or skipped
278 // so far to the |new_tbs_cert|.
279 bssl::ScopedCBB cbb;
280 CBB new_tbs_cert;
281 if (!CBB_init(cbb.get(), CBS_len(&tbs_cert_copy)) ||
282 !CBB_add_asn1(cbb.get(), &new_tbs_cert, CBS_ASN1_SEQUENCE) ||
283 !CopyBefore(tbs_cert_copy, tbs_cert, &new_tbs_cert)) {
284 return false;
285 }
286
287 // Parse the extensions list and find the SCT extension.
288 //
289 // XXX(rsleevi): We could generate precerts for certs without the extension
290 // by leaving the TBSCertificate as-is. The reference implementation does not
291 // do this.
292 constexpr unsigned kExtensionsTag =
293 CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 3;
294 CBS extensions_wrap, extensions, sct_extension;
295 if (!CBS_get_asn1(&tbs_cert, &extensions_wrap, kExtensionsTag) ||
296 !CBS_get_asn1(&extensions_wrap, &extensions, CBS_ASN1_SEQUENCE) ||
297 CBS_len(&extensions_wrap) != 0 || CBS_len(&tbs_cert) != 0 ||
298 !FindExtensionElement(extensions, kEmbeddedSCTOid,
299 sizeof(kEmbeddedSCTOid), &sct_extension)) {
300 return false;
301 }
302
303 // Add extensions to the TBSCertificate. Copy all extensions except the
304 // embedded SCT extension.
305 CBB new_extensions_wrap, new_extensions;
306 if (!CBB_add_asn1(&new_tbs_cert, &new_extensions_wrap, kExtensionsTag) ||
307 !CBB_add_asn1(&new_extensions_wrap, &new_extensions, CBS_ASN1_SEQUENCE) ||
308 !CopyBefore(extensions, sct_extension, &new_extensions) ||
309 !CopyAfter(extensions, sct_extension, &new_extensions)) {
310 return false;
311 }
312
313 uint8_t* new_tbs_cert_der;
314 size_t new_tbs_cert_len;
315 if (!CBB_finish(cbb.get(), &new_tbs_cert_der, &new_tbs_cert_len))
316 return false;
317 bssl::UniquePtr<uint8_t> scoped_new_tbs_cert_der(new_tbs_cert_der);
318
319 // Extract the issuer's public key.
320 base::StringPiece issuer_key;
321 if (!asn1::ExtractSPKIFromDERCert(
322 x509_util::CryptoBufferAsStringPiece(issuer), &issuer_key)) {
323 return false;
324 }
325
326 // Fill in the SignedEntryData.
327 result->type = ct::SignedEntryData::LOG_ENTRY_TYPE_PRECERT;
328 result->tbs_certificate.assign(
329 reinterpret_cast<const char*>(new_tbs_cert_der), new_tbs_cert_len);
330 crypto::SHA256HashString(issuer_key, result->issuer_key_hash.data,
331 sizeof(result->issuer_key_hash.data));
332
333 return true;
334 }
335
GetX509SignedEntry(const CRYPTO_BUFFER * leaf,SignedEntryData * result)336 bool GetX509SignedEntry(const CRYPTO_BUFFER* leaf, SignedEntryData* result) {
337 DCHECK(leaf);
338
339 result->Reset();
340 result->type = ct::SignedEntryData::LOG_ENTRY_TYPE_X509;
341 result->leaf_certificate =
342 std::string(x509_util::CryptoBufferAsStringPiece(leaf));
343 return true;
344 }
345
ExtractSCTListFromOCSPResponse(const CRYPTO_BUFFER * issuer,const std::string & cert_serial_number,base::StringPiece ocsp_response,std::string * sct_list)346 bool ExtractSCTListFromOCSPResponse(const CRYPTO_BUFFER* issuer,
347 const std::string& cert_serial_number,
348 base::StringPiece ocsp_response,
349 std::string* sct_list) {
350 // The input is an bssl::OCSPResponse. See RFC2560, section 4.2.1. The SCT
351 // list is in the extensions field of the SingleResponse which matches the
352 // input certificate.
353 CBS cbs;
354 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(ocsp_response.data()),
355 ocsp_response.size());
356
357 // Parse down to the ResponseBytes. The ResponseBytes is optional, but if it's
358 // missing, this can't include an SCT list.
359 CBS sequence, tagged_response_bytes, response_bytes, response_type, response;
360 if (!CBS_get_asn1(&cbs, &sequence, CBS_ASN1_SEQUENCE) || CBS_len(&cbs) != 0 ||
361 !SkipElements(&sequence, 1 /* responseStatus */) ||
362 !CBS_get_asn1(&sequence, &tagged_response_bytes,
363 CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
364 CBS_len(&sequence) != 0 ||
365 !CBS_get_asn1(&tagged_response_bytes, &response_bytes,
366 CBS_ASN1_SEQUENCE) ||
367 CBS_len(&tagged_response_bytes) != 0 ||
368 !CBS_get_asn1(&response_bytes, &response_type, CBS_ASN1_OBJECT) ||
369 !CBS_get_asn1(&response_bytes, &response, CBS_ASN1_OCTETSTRING) ||
370 CBS_len(&response_bytes) != 0) {
371 return false;
372 }
373
374 // The only relevant ResponseType is id-pkix-ocsp-basic.
375 if (!CBS_mem_equal(&response_type, kOCSPBasicResponseOid,
376 sizeof(kOCSPBasicResponseOid))) {
377 return false;
378 }
379
380 // Parse the ResponseData out of the BasicOCSPResponse. Ignore the rest.
381 constexpr unsigned kVersionTag =
382 CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0;
383 CBS basic_response, response_data, responses;
384 if (!CBS_get_asn1(&response, &basic_response, CBS_ASN1_SEQUENCE) ||
385 CBS_len(&response) != 0 ||
386 !CBS_get_asn1(&basic_response, &response_data, CBS_ASN1_SEQUENCE)) {
387 return false;
388 }
389
390 // Extract the list of SingleResponses from the ResponseData.
391 if (!SkipOptionalElement(&response_data, kVersionTag) ||
392 !SkipElements(&response_data, 2 /* responderID, producedAt */) ||
393 !CBS_get_asn1(&response_data, &responses, CBS_ASN1_SEQUENCE)) {
394 return false;
395 }
396
397 CBS single_response;
398 if (!FindMatchingSingleResponse(&responses, issuer, cert_serial_number,
399 &single_response)) {
400 return false;
401 }
402
403 // Parse the extensions out of the SingleResponse.
404 constexpr unsigned kNextUpdateTag =
405 CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0;
406 constexpr unsigned kSingleExtensionsTag =
407 CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1;
408 CBS extensions_wrap, extensions;
409 if (!SkipElements(&single_response, 2 /* certStatus, thisUpdate */) ||
410 !SkipOptionalElement(&single_response, kNextUpdateTag) ||
411 !CBS_get_asn1(&single_response, &extensions_wrap, kSingleExtensionsTag) ||
412 !CBS_get_asn1(&extensions_wrap, &extensions, CBS_ASN1_SEQUENCE) ||
413 CBS_len(&extensions_wrap) != 0) {
414 return false;
415 }
416
417 return ParseSCTListFromExtensions(extensions, kOCSPExtensionOid,
418 sizeof(kOCSPExtensionOid), sct_list);
419 }
420
421 } // namespace net::ct
422