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 implements KeyAlgorithm {
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          length_ = length;
45      }
46  
47      /**
48       * Parses AES key algorithm parameters from a JS object.
49       *
50       * @param keyGenParams the JS algorithm parameters object
51       * @return the parsed AesKeyAlgorithm
52       */
53      static AesKeyAlgorithm from(final Scriptable keyGenParams) {
54          final String name = SubtleCrypto.resolveAlgorithmName(keyGenParams);
55  
56          final int length = (int) SubtleCrypto.getRequiredNumber(keyGenParams, "length");
57          return new AesKeyAlgorithm(name, length);
58      }
59  
60      static boolean isSupported(final String name) {
61          return SUPPORTED_NAMES.contains(name);
62      }
63  
64      @Override
65      public String getName() {
66          return name_;
67      }
68  
69      int getLength() {
70          return length_;
71      }
72  
73      /**
74       * Converts to a JS object matching the {@code AesKeyAlgorithm} dictionary:
75       * {@code {name: "AES-GCM", length: 256}}.
76       *
77       * @param scope the JS scope for prototype/parent setup
78       * @return the JS algorithm object
79       */
80      @Override
81      public Scriptable toScriptableObject(final VarScope scope) {
82          final Scriptable algorithm = JavaScriptEngine.newObject(scope);
83          ScriptableObject.putProperty(algorithm, "name", getName());
84          ScriptableObject.putProperty(algorithm, "length", getLength());
85          return algorithm;
86      }
87  }