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  package io.netty.handler.codec.quic;
17  
18  import io.netty.util.internal.PlatformDependent;
19  import org.jetbrains.annotations.Nullable;
20  
21  final class BoringSSLSessionTicketCallback {
22  
23      // As we dont assume to have a lot of keys configured we will just use an array for now as a data store.
24      private volatile byte[][] sessionKeys;
25  
26      // Accessed via JNI.
27      byte @Nullable [] findSessionTicket(byte @Nullable [] keyname) {
28          byte[][] keys = this.sessionKeys;
29          if (keys == null || keys.length == 0) {
30              return null;
31          }
32          if (keyname == null) {
33              return keys[0];
34          }
35  
36          for (int i = 0; i < keys.length; i++) {
37              byte[] key = keys[i];
38              if (PlatformDependent.equals(keyname, 0, key, 1, keyname.length)) {
39                  return key;
40              }
41          }
42          return null;
43      }
44  
45      void setSessionTicketKeys(SslSessionTicketKey @Nullable [] keys) {
46          if (keys != null && keys.length != 0) {
47              byte[][] sessionKeys = new byte[keys.length][];
48              for (int i = 0; i < keys.length; ++i) {
49                  SslSessionTicketKey key = keys[i];
50                  byte[] binaryKey = new byte[49];
51                  // We mark the first key as preferred by using 1 as byte marker
52                  binaryKey[0] = i == 0 ? (byte) 1 : (byte) 0;
53                  int dstCurPos = 1;
54                  System.arraycopy(key.name, 0, binaryKey, dstCurPos, 16);
55                  dstCurPos += 16;
56                  System.arraycopy(key.hmacKey, 0, binaryKey, dstCurPos, 16);
57                  dstCurPos += 16;
58                  System.arraycopy(key.aesKey, 0, binaryKey, dstCurPos, 16);
59                  sessionKeys[i] = binaryKey;
60              }
61              this.sessionKeys = sessionKeys;
62          } else {
63              sessionKeys = null;
64          }
65      }
66  }