View Javadoc
1   /*
2    * Copyright 2023 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  
17  package io.netty.handler.codec.quic;
18  
19  import java.util.Arrays;
20  
21  /**
22   * Session Ticket Key
23   */
24  public final class SslSessionTicketKey {
25      /**
26       * Size of session ticket key name
27       */
28      public static final int NAME_SIZE = 16;
29      /**
30       * Size of session ticket key HMAC key
31       */
32      public static final int HMAC_KEY_SIZE = 16;
33      /**
34       * Size of session ticket key AES key
35       */
36      public static final int AES_KEY_SIZE = 16;
37      /**
38       * Size of session ticket key
39       */
40      public static final int TICKET_KEY_SIZE = NAME_SIZE + HMAC_KEY_SIZE + AES_KEY_SIZE;
41  
42      // package private so we can access these in BoringSSLSessionTicketCallback without calling clone() on the byte[].
43      final byte[] name;
44      final byte[] hmacKey;
45      final byte[] aesKey;
46  
47      /**
48       * Construct SessionTicketKey.
49       * @param name the name of the session ticket key
50       * @param hmacKey the HMAC key of the session ticket key
51       * @param aesKey the AES key of the session ticket key
52       */
53      public SslSessionTicketKey(byte[] name, byte[] hmacKey, byte[] aesKey) {
54          if (name == null || name.length != NAME_SIZE) {
55              throw new IllegalArgumentException("Length of name must be " + NAME_SIZE);
56          }
57          if (hmacKey == null || hmacKey.length != HMAC_KEY_SIZE) {
58              throw new IllegalArgumentException("Length of hmacKey must be " + HMAC_KEY_SIZE);
59          }
60          if (aesKey == null || aesKey.length != AES_KEY_SIZE) {
61              throw new IllegalArgumentException("Length of aesKey must be " + AES_KEY_SIZE);
62          }
63          this.name = name.clone();
64          this.hmacKey = hmacKey.clone();
65          this.aesKey = aesKey.clone();
66      }
67  
68      /**
69       * Get name.
70       *
71       * @return the name of the session ticket key
72       */
73      public byte[] name() {
74          return name.clone();
75      }
76  
77      /**
78       * Get HMAC key.
79       * @return the HMAC key of the session ticket key
80       */
81      public byte[] hmacKey() {
82          return hmacKey.clone();
83      }
84  
85      /**
86       * Get AES Key.
87       * @return the AES key of the session ticket key
88       */
89      public byte[] aesKey() {
90          return aesKey.clone();
91      }
92  
93      @Override
94      public boolean equals(Object o) {
95          if (this == o) {
96              return true;
97          }
98          if (o == null || getClass() != o.getClass()) {
99              return false;
100         }
101 
102         SslSessionTicketKey that = (SslSessionTicketKey) o;
103 
104         if (!Arrays.equals(name, that.name)) {
105             return false;
106         }
107         if (!Arrays.equals(hmacKey, that.hmacKey)) {
108             return false;
109         }
110         return Arrays.equals(aesKey, that.aesKey);
111     }
112 
113     @Override
114     public int hashCode() {
115         int result = Arrays.hashCode(name);
116         result = 31 * result + Arrays.hashCode(hmacKey);
117         result = 31 * result + Arrays.hashCode(aesKey);
118         return result;
119     }
120 
121     @Override
122     public String toString() {
123         return "SessionTicketKey{" +
124                 "name=" + Arrays.toString(name) +
125                 ", hmacKey=" + Arrays.toString(hmacKey) +
126                 ", aesKey=" + Arrays.toString(aesKey) +
127                 '}';
128     }
129 }