View Javadoc
1   /*
2    * Copyright 2022 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version
5    * 2.0 (the "License"); you may not use this file except in compliance with the
6    * 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 under
14   * the License.
15   */
16  package io.netty.handler.ssl;
17  
18  import io.netty.util.internal.ObjectUtil;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  /**
27   * Configuration for TLS1.3 certificate compression extension.
28   * When no configuration is specified, zlib compression is enabled in both directions with BoringSSL and AWS-LC.
29   * Use an empty configuration to disable it. The {@code jdk.tls.client.disableExtensions} and
30   * {@code jdk.tls.server.disableExtensions} system properties can disable the default for the corresponding side,
31   * but an explicit configuration takes precedence.
32   */
33  public final class OpenSslCertificateCompressionConfig implements
34          Iterable<OpenSslCertificateCompressionConfig.AlgorithmConfig> {
35      private final List<AlgorithmConfig> pairList;
36  
37      private OpenSslCertificateCompressionConfig(AlgorithmConfig... pairs) {
38          pairList = Collections.unmodifiableList(Arrays.asList(pairs));
39      }
40  
41      @Override
42      public Iterator<AlgorithmConfig> iterator() {
43          return pairList.iterator();
44      }
45  
46      /**
47       * Creates a new {@link Builder} for a config.
48       *
49       * @return a bulder
50       */
51      public static Builder newBuilder() {
52          return new Builder();
53      }
54  
55      /**
56       * Builder for an {@link OpenSslCertificateCompressionAlgorithm}.
57       */
58      public static final class Builder {
59          private final List<AlgorithmConfig> algorithmList = new ArrayList<AlgorithmConfig>();
60  
61          private Builder() { }
62  
63          /**
64           * Adds a certificate compression algorithm.
65           * For servers, algorithm preference order is dictated by the order of algorithm registration.
66           * Most preferred algorithm should be registered first.
67           *
68           * @param algorithm implementation of the compression and or decompression algorithm as a
69           * {@link OpenSslCertificateCompressionAlgorithm}
70           * @param mode indicates whether decompression support should be advertized, compression should be applied
71           *                  for peers which support it, or both. This allows the caller to support one way compression
72           *                  only.
73           * @return self.
74           */
75          public Builder addAlgorithm(OpenSslCertificateCompressionAlgorithm algorithm, AlgorithmMode mode) {
76              algorithmList.add(new AlgorithmConfig(algorithm, mode));
77              return this;
78          }
79  
80          /**
81           * Build a new {@link OpenSslCertificateCompressionConfig} based on the previous
82           * added {@link OpenSslCertificateCompressionAlgorithm}s.
83           *
84           * @return a new config.
85           */
86          public OpenSslCertificateCompressionConfig build() {
87              return new OpenSslCertificateCompressionConfig(algorithmList.toArray(new AlgorithmConfig[0]));
88          }
89      }
90  
91      /**
92       * The configuration for algorithm.
93       */
94      public static final class AlgorithmConfig {
95          private final OpenSslCertificateCompressionAlgorithm algorithm;
96          private final AlgorithmMode mode;
97  
98          private AlgorithmConfig(OpenSslCertificateCompressionAlgorithm algorithm, AlgorithmMode mode) {
99              this.algorithm = ObjectUtil.checkNotNull(algorithm, "algorithm");
100             this.mode = ObjectUtil.checkNotNull(mode, "mode");
101         }
102 
103         /**
104          * The {@link AlgorithmMode}
105          *
106          * @return the usage mode.
107          */
108         public AlgorithmMode mode() {
109             return mode;
110         }
111 
112         /**
113          * The configured {@link OpenSslCertificateCompressionAlgorithm}.
114          *
115          * @return the algorithm
116          */
117         public OpenSslCertificateCompressionAlgorithm algorithm() {
118             return algorithm;
119         }
120     }
121 
122     /**
123      * The usage mode of the {@link OpenSslCertificateCompressionAlgorithm}.
124      */
125     public enum AlgorithmMode {
126         /**
127          * Compression supported and should be advertized.
128          */
129         Compress,
130 
131         /**
132          * Decompression supported and should be advertized.
133          */
134         Decompress,
135 
136         /**
137          * Compression and Decompression are supported and both should be advertized.
138          */
139         Both
140     }
141 }