Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / net / cert / sha256_legacy_support_openssl_win.cc
blob2c5fc7087a8e893f8f23f48c0dc1e2692563a8ed
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <openssl/asn1.h>
6 #include <openssl/bytestring.h>
7 #include <openssl/evp.h>
8 #include <openssl/obj.h>
9 #include <openssl/x509.h>
11 #include "base/logging.h"
12 #include "crypto/scoped_openssl_types.h"
13 #include "net/cert/sha256_legacy_support_win.h"
15 namespace net {
17 namespace sha256_interception {
19 namespace {
21 using ScopedX509_ALGOR = crypto::ScopedOpenSSL<X509_ALGOR, X509_ALGOR_free>;
23 // Parses |subject_signature| and writes the components into |*out_tbs_data|,
24 // |*out_algor|, and |*out_signature|. The BIT STRING in the signature must be
25 // a multiple of 8 bits. |*out_signature| will have the padding byte removed.
26 // It returns true on success and false on failure.
27 bool ParseSubjectSignature(const base::StringPiece& subject_signature,
28 CBS* out_tbs_data,
29 ScopedX509_ALGOR* out_algor,
30 CBS* out_signature) {
31 CBS cbs, sequence, tbs_data, algorithm, signature;
32 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(subject_signature.data()),
33 subject_signature.size());
34 if (!CBS_get_asn1(&cbs, &sequence, CBS_ASN1_SEQUENCE) ||
35 CBS_len(&cbs) != 0 ||
36 !CBS_get_any_asn1_element(&sequence, &tbs_data, nullptr, nullptr) ||
37 !CBS_get_asn1_element(&sequence, &algorithm,
38 CBS_ASN1_SEQUENCE) ||
39 !CBS_get_asn1(&sequence, &signature, CBS_ASN1_BITSTRING) ||
40 CBS_len(&sequence) != 0) {
41 return false;
44 // Decode the algorithm.
45 const uint8_t* ptr = CBS_data(&algorithm);
46 ScopedX509_ALGOR algor(d2i_X509_ALGOR(NULL, &ptr, CBS_len(&algorithm)));
47 if (!algor || ptr != CBS_data(&algorithm) + CBS_len(&algorithm))
48 return false;
50 // An ASN.1 BIT STRING is encoded with a leading byte denoting the number of
51 // padding bits. All supported signature algorithms output octets, so the
52 // leading byte must be zero.
53 uint8_t padding;
54 if (!CBS_get_u8(&signature, &padding) || padding != 0)
55 return false;
57 *out_tbs_data = tbs_data;
58 *out_algor = algor.Pass();
59 *out_signature = signature;
60 return true;
63 } // namespace
65 BOOL CryptVerifyCertificateSignatureExHook(
66 CryptVerifyCertificateSignatureExFunc original_func,
67 HCRYPTPROV_LEGACY provider,
68 DWORD encoding_type,
69 DWORD subject_type,
70 void* subject_data,
71 DWORD issuer_type,
72 void* issuer_data,
73 DWORD flags,
74 void* extra) {
75 CHECK(original_func);
77 // Only intercept if the arguments are supported.
78 if (provider != NULL || (encoding_type != X509_ASN_ENCODING) ||
79 !IsSupportedSubjectType(subject_type) || subject_data == NULL ||
80 !IsSupportedIssuerType(issuer_type) || issuer_data == NULL) {
81 return original_func(provider, encoding_type, subject_type, subject_data,
82 issuer_type, issuer_data, flags, extra);
85 base::StringPiece subject_signature =
86 GetSubjectSignature(subject_type, subject_data);
87 bool should_intercept = false;
89 // Parse out the data, AlgorithmIdentifier, and signature.
90 CBS tbs_data, signature;
91 ScopedX509_ALGOR algor;
92 if (ParseSubjectSignature(subject_signature, &tbs_data, &algor,
93 &signature)) {
94 // If the signature algorithm is RSA with one of the SHA-2 algorithms
95 // supported by BoringSSL (excluding SHA-224, which is pointless), then
96 // defer to the BoringSSL implementation. Otherwise, fall back and let the
97 // OS handle it (e.g. in case there are any algorithm policies in effect).
98 int nid = OBJ_obj2nid(algor->algorithm);
99 if (nid == NID_sha256WithRSAEncryption ||
100 nid == NID_sha384WithRSAEncryption ||
101 nid == NID_sha512WithRSAEncryption) {
102 should_intercept = true;
106 if (!should_intercept) {
107 return original_func(provider, encoding_type, subject_type, subject_data,
108 issuer_type, issuer_data, flags, extra);
111 // Rather than attempting to synthesize an EVP_PKEY by hand, just force the
112 // OS to do an ASN.1 encoding and then decode it back into BoringSSL. This
113 // is silly for performance, but safest for consistency.
114 PCERT_PUBLIC_KEY_INFO issuer_public_key =
115 GetIssuerPublicKey(issuer_type, issuer_data);
116 if (!issuer_public_key) {
117 SetLastError(static_cast<DWORD>(NTE_BAD_ALGID));
118 return FALSE;
121 uint8_t* issuer_spki_data = NULL;
122 DWORD issuer_spki_len = 0;
123 if (!CryptEncodeObjectEx(X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO,
124 issuer_public_key, CRYPT_ENCODE_ALLOC_FLAG, NULL,
125 &issuer_spki_data, &issuer_spki_len)) {
126 return FALSE;
129 const uint8_t* ptr = issuer_spki_data;
130 crypto::ScopedEVP_PKEY pubkey(d2i_PUBKEY(NULL, &ptr, issuer_spki_len));
131 if (!pubkey.get() || ptr != issuer_spki_data + issuer_spki_len) {
132 ::LocalFree(issuer_spki_data);
133 SetLastError(static_cast<DWORD>(NTE_BAD_ALGID));
134 return FALSE;
136 ::LocalFree(issuer_spki_data);
138 crypto::ScopedEVP_MD_CTX md_ctx(EVP_MD_CTX_create());
139 if (!EVP_DigestVerifyInitFromAlgorithm(md_ctx.get(), algor.get(),
140 pubkey.get()) ||
141 !EVP_DigestVerifyUpdate(md_ctx.get(), CBS_data(&tbs_data),
142 CBS_len(&tbs_data)) ||
143 !EVP_DigestVerifyFinal(md_ctx.get(), CBS_data(&signature),
144 CBS_len(&signature))) {
145 SetLastError(static_cast<DWORD>(NTE_BAD_SIGNATURE));
146 return FALSE;
148 return TRUE;
151 } // namespace sha256_interception
153 } // namespace net