View Javadoc
1   /*
2    * Copyright 2026 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.ssl;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.util.zip.DataFormatException;
20  import java.util.zip.Deflater;
21  import java.util.zip.Inflater;
22  import javax.net.ssl.SSLEngine;
23  
24  /**
25   * The zlib certificate compression algorithm (ID 1) as defined by
26   * <a href="https://datatracker.ietf.org/doc/html/rfc8879">RFC 8879</a>.
27   * <p>
28   * This is the default algorithm used by {@link OpenSslCertificateCompressionConfig}. Use it to keep zlib when
29   * supplying a custom configuration. The singleton is thread-safe.
30   */
31  public final class ZlibCertificateCompressionAlgorithm implements OpenSslCertificateCompressionAlgorithm {
32      /**
33       * The shared instance.
34       */
35      public static final ZlibCertificateCompressionAlgorithm INSTANCE = new ZlibCertificateCompressionAlgorithm();
36  
37      // The zlib algorithm identifier assigned by RFC 8879.
38      private static final int ALGORITHM_ID = 1;
39      private static final int BUFFER_SIZE = 8192;
40      private static final int MAX_UNCOMPRESSED_CERTIFICATE_LENGTH = 0xFFFFFF;
41  
42      private ZlibCertificateCompressionAlgorithm() { }
43  
44      @Override
45      public byte[] compress(SSLEngine engine, byte[] uncompressedCertificate) {
46          Deflater deflater = new Deflater();
47          try {
48              deflater.setInput(uncompressedCertificate);
49              deflater.finish();
50              ByteArrayOutputStream output = new ByteArrayOutputStream(uncompressedCertificate.length);
51              byte[] buffer = new byte[BUFFER_SIZE];
52              while (!deflater.finished()) {
53                  int length = deflater.deflate(buffer);
54                  output.write(buffer, 0, length);
55              }
56              return output.toByteArray();
57          } finally {
58              deflater.end();
59          }
60      }
61  
62      @Override
63      public byte[] decompress(SSLEngine engine, int uncompressedLen, byte[] compressedCertificate)
64              throws DataFormatException {
65          if (uncompressedLen < 0 || uncompressedLen > MAX_UNCOMPRESSED_CERTIFICATE_LENGTH) {
66              throw new DataFormatException("uncompressed length must be between 0 and " +
67                      MAX_UNCOMPRESSED_CERTIFICATE_LENGTH);
68          }
69  
70          Inflater inflater = new Inflater();
71          try {
72              inflater.setInput(compressedCertificate);
73              byte[] output = new byte[uncompressedLen];
74              int offset = 0;
75              while (!inflater.finished() && offset < output.length) {
76                  int length = inflater.inflate(output, offset, output.length - offset);
77                  if (length == 0) {
78                      break;
79                  }
80                  offset += length;
81              }
82              if (!inflater.finished() || offset != uncompressedLen || inflater.getRemaining() != 0) {
83                  throw new DataFormatException("decompressed certificate length does not match uncompressed length");
84              }
85              return output;
86          } finally {
87              inflater.end();
88          }
89      }
90  
91      @Override
92      public int algorithmId() {
93          return ALGORITHM_ID;
94      }
95  }