View Javadoc
1   /*
2    * Copyright 2018 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.util.Locale;
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  /**
23   * Converts OpenSSL signature Algorithm names to
24   * <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Signature">
25   *     Java signature Algorithm names</a>.
26   */
27  final class SignatureAlgorithmConverter {
28  
29      private SignatureAlgorithmConverter() { }
30  
31      // OpenSSL has 3 different formats it uses at the moment we will match against all of these.
32      // For example:
33      //              ecdsa-with-SHA384
34      //              hmacWithSHA384
35      //              dsa_with_SHA224
36      //
37      // For more details see https://github.com/openssl/openssl/blob/OpenSSL_1_0_2p/crypto/objects/obj_dat.h
38      //
39      // BoringSSL uses a different format:
40      // https://github.com/google/boringssl/blob/8525ff3/ssl/ssl_privkey.cc#L436
41      //
42      private static final Pattern PATTERN = Pattern.compile(
43              // group 1 - 2
44              "(?:(^[a-zA-Z].+)With(.+)Encryption$)|" +
45              // group 3 - 4
46              "(?:(^[a-zA-Z].+)(?:_with_|-with-|_pkcs1_|_pss_rsae_)(.+$))|" +
47              // group 5 - 6
48              "(?:(^[a-zA-Z].+)_(.+$))");
49  
50      /**
51       * Converts an OpenSSL algorithm name to a Java algorithm name and return it,
52       * or return {@code null} if the conversion failed because the format is not known.
53       */
54      static String toJavaName(String opensslName) {
55          if (opensslName == null) {
56              return null;
57          }
58          // The names the native libraries report for the signature algorithms that TLS peers offer in practice.
59          // These are converted on every handshake, so they must not pay for the regular expression. Each result is
60          // what the regular expression produces for that name, anything else still goes through it.
61          switch (opensslName) {
62              // BoringSSL and AWS-LC, see SSL_get_signature_algorithm_name(...).
63              case "ecdsa_sha256":
64              // OpenSSL, see OBJ_nid2ln(...).
65              case "ecdsa-with-SHA256":
66                  return "SHA256withECDSA";
67              case "ecdsa_sha384":
68              case "ecdsa-with-SHA384":
69                  return "SHA384withECDSA";
70              case "ecdsa_sha512":
71              case "ecdsa-with-SHA512":
72                  return "SHA512withECDSA";
73              case "ecdsa-with-SHA224":
74                  return "SHA224withECDSA";
75              case "ecdsa_sha1":
76              case "ecdsa-with-SHA1":
77                  return "SHA1withECDSA";
78              case "rsa_pss_rsae_sha256":
79              case "rsa_pkcs1_sha256":
80              case "sha256WithRSAEncryption":
81                  return "SHA256withRSA";
82              case "rsa_pss_rsae_sha384":
83              case "rsa_pkcs1_sha384":
84              case "sha384WithRSAEncryption":
85                  return "SHA384withRSA";
86              case "rsa_pss_rsae_sha512":
87              case "rsa_pkcs1_sha512":
88              case "sha512WithRSAEncryption":
89                  return "SHA512withRSA";
90              case "sha224WithRSAEncryption":
91                  return "SHA224withRSA";
92              case "rsa_pkcs1_sha1":
93              case "sha1WithRSAEncryption":
94                  return "SHA1withRSA";
95              case "dsa_with_SHA256":
96                  return "SHA256withDSA";
97              // EdDSA names carry no hash, so the pattern can't match them. All spellings are listed as these names
98              // are matched without regard to case.
99              case "ed25519":
100             case "ED25519":
101             case "Ed25519":
102             case "eD25519":
103                 return "Ed25519";
104             case "ed448":
105             case "ED448":
106             case "Ed448":
107             case "eD448":
108                 return "Ed448";
109             // OpenSSL reports these for RSA-PSS, DSA and algorithms it has no name for. There is no Java name
110             // for them.
111             case "rsassaPss":
112             case "dsaEncryption":
113             case "undefined":
114                 return null;
115             default:
116                 return toJavaNameWithPattern(opensslName);
117         }
118     }
119 
120     static String toJavaNameWithPattern(String opensslName) {
121         Matcher matcher = PATTERN.matcher(opensslName);
122         if (matcher.matches()) {
123             String group1 = matcher.group(1);
124             if (group1 != null) {
125                 return group1.toUpperCase(Locale.ROOT) + "with" + matcher.group(2).toUpperCase(Locale.ROOT);
126             }
127             if (matcher.group(3) != null) {
128                 return matcher.group(4).toUpperCase(Locale.ROOT) + "with" + matcher.group(3).toUpperCase(Locale.ROOT);
129             }
130 
131             if (matcher.group(5) != null) {
132                 return matcher.group(6).toUpperCase(Locale.ROOT) + "with" + matcher.group(5).toUpperCase(Locale.ROOT);
133             }
134         }
135         return null;
136     }
137 }