View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.crypto;
16  
17  import java.util.Set;
18  
19  import org.htmlunit.corejs.javascript.Scriptable;
20  import org.htmlunit.corejs.javascript.ScriptableObject;
21  import org.htmlunit.corejs.javascript.VarScope;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  
24  /**
25   * Internal helper representing AES key algorithm parameters.
26   * Used by {@link SubtleCrypto} for AES key operations.
27   *
28   * @author Lai Quang Duong
29   * @author Ronald Brill
30   */
31  final class AesKeyAlgorithm {
32  
33      static final Set<String> SUPPORTED_NAMES = Set.of("AES-CBC", "AES-CTR", "AES-GCM", "AES-KW");
34      static final Set<Integer> SUPPORTED_LENGTHS = Set.of(128, 192, 256);
35  
36      private final String name_;
37      private final int length_;
38  
39      AesKeyAlgorithm(final String name, final int length) {
40          if (!SUPPORTED_NAMES.contains(name)) {
41              throw new UnsupportedOperationException("AES " + name);
42          }
43          name_ = name;
44  
45          if (!SUPPORTED_LENGTHS.contains(length)) {
46              throw new IllegalArgumentException("Data provided to an operation does not meet requirements");
47          }
48          length_ = length;
49      }
50  
51      /**
52       * Parse AES key algorithm parameters from a JS object.
53       *
54       * @param keyGenParams the JS algorithm parameters object
55       * @return the parsed AesKeyAlgorithm
56       */
57      static AesKeyAlgorithm from(final Scriptable keyGenParams) {
58          final Object nameProp = ScriptableObject.getProperty(keyGenParams, "name");
59          if (!(nameProp instanceof String name)) {
60              throw new IllegalArgumentException("An invalid or illegal string was specified");
61          }
62  
63          final Object lengthProp = ScriptableObject.getProperty(keyGenParams, "length");
64          if (!(lengthProp instanceof Number numLength)) {
65              throw new IllegalArgumentException("An invalid or illegal string was specified");
66          }
67  
68          return new AesKeyAlgorithm(name, numLength.intValue());
69      }
70  
71      static boolean isSupported(final String name) {
72          return SUPPORTED_NAMES.contains(name);
73      }
74  
75      String getName() {
76          return name_;
77      }
78  
79      int getLength() {
80          return length_;
81      }
82  
83      /**
84       * Converts to a JS object matching the {@code AesKeyAlgorithm} dictionary:
85       * {@code {name: "AES-GCM", length: 256}}
86       *
87       * @param scope the JS scope for prototype/parent setup
88       * @return the JS algorithm object
89       */
90      Scriptable toScriptableObject(final VarScope scope) {
91          final Scriptable algorithm = JavaScriptEngine.newObject(scope);
92          ScriptableObject.putProperty(algorithm, "name", getName());
93          ScriptableObject.putProperty(algorithm, "length", getLength());
94          return algorithm;
95      }
96  }