1 // Copyright 2020 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/ssl/cert_compression.h"
6
7 #include <cstdint>
8
9 #include "third_party/boringssl/src/include/openssl/ssl.h"
10
11 #if !defined(NET_DISABLE_BROTLI)
12 #include "third_party/brotli/include/brotli/decode.h"
13 #endif
14
15 namespace net {
16 namespace {
17
18 #if !defined(NET_DISABLE_BROTLI)
DecompressBrotliCert(SSL * ssl,CRYPTO_BUFFER ** out,size_t uncompressed_len,const uint8_t * in,size_t in_len)19 int DecompressBrotliCert(SSL* ssl,
20 CRYPTO_BUFFER** out,
21 size_t uncompressed_len,
22 const uint8_t* in,
23 size_t in_len) {
24 uint8_t* data;
25 bssl::UniquePtr<CRYPTO_BUFFER> decompressed(
26 CRYPTO_BUFFER_alloc(&data, uncompressed_len));
27 if (!decompressed) {
28 return 0;
29 }
30
31 size_t output_size = uncompressed_len;
32 if (BrotliDecoderDecompress(in_len, in, &output_size, data) !=
33 BROTLI_DECODER_RESULT_SUCCESS ||
34 output_size != uncompressed_len) {
35 return 0;
36 }
37
38 *out = decompressed.release();
39 return 1;
40 }
41 #endif
42
43 } // namespace
44
ConfigureCertificateCompression(SSL_CTX * ctx)45 void ConfigureCertificateCompression(SSL_CTX* ctx) {
46 #if !defined(NET_DISABLE_BROTLI)
47 SSL_CTX_add_cert_compression_alg(ctx, TLSEXT_cert_compression_brotli,
48 nullptr /* compression not supported */,
49 DecompressBrotliCert);
50 #endif
51
52 // Avoid "unused argument" errors in case no algorithms are supported.
53 (void)(ctx);
54 }
55
56 } // namespace net
57