View Javadoc
1   /*
2    * Copyright 2025 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 io.netty.util.internal.PlatformDependent;
19  
20  import javax.net.ssl.SSLParameters;
21  import java.lang.invoke.MethodHandle;
22  import java.lang.invoke.MethodHandles;
23  import java.lang.invoke.MethodType;
24  import java.security.AccessController;
25  import java.security.PrivilegedAction;
26  
27  final class OpenSslParametersUtil {
28  
29      private static final MethodHandle GET_NAMED_GROUPS;
30      private static final MethodHandle SET_NAMED_GROUPS;
31  
32      static {
33          MethodHandle getNamedGroups = null;
34          MethodHandle setNamedGroups = null;
35          if (PlatformDependent.javaVersion() >= 20) {
36              final MethodHandles.Lookup lookup = MethodHandles.lookup();
37              getNamedGroups = obtainHandle(lookup, "getNamedGroups",
38                      MethodType.methodType(String[].class));
39              setNamedGroups = obtainHandle(lookup, "setNamedGroups",
40                      MethodType.methodType(void.class, String[].class));
41          }
42          GET_NAMED_GROUPS = getNamedGroups;
43          SET_NAMED_GROUPS = setNamedGroups;
44      }
45  
46      private static MethodHandle obtainHandle(final MethodHandles.Lookup lookup,
47                                               final String methodName, final MethodType type) {
48          return AccessController.doPrivileged((PrivilegedAction<MethodHandle>) () -> {
49              try {
50                  return lookup.findVirtual(SSLParameters.class, methodName, type);
51              } catch (UnsupportedOperationException | SecurityException |
52                       NoSuchMethodException | IllegalAccessException e) {
53                  // Just ignore it.
54                  return null;
55              }
56          });
57      }
58  
59      static String[] getNamesGroups(SSLParameters parameters) {
60          if (GET_NAMED_GROUPS == null) {
61              return null;
62          }
63          try {
64              return (String[]) GET_NAMED_GROUPS.invoke(parameters);
65          } catch (Throwable t) {
66              // Just ignore it.
67              return null;
68          }
69      }
70  
71      static void setNamesGroups(SSLParameters parameters, String[] names) {
72          if (SET_NAMED_GROUPS == null) {
73              return;
74          }
75          try {
76              SET_NAMED_GROUPS.invoke(parameters, names);
77          } catch (Throwable ignore) {
78              // Ignore
79          }
80      }
81  
82      private OpenSslParametersUtil() { }
83  }