1
2
3
4
5
6
7
8
9
10
11
12
13
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
26
27
28
29
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
53
54
55
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
85
86
87
88
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 }