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.security.Key;
18 import java.security.PrivateKey;
19 import java.security.PublicKey;
20 import java.util.Collection;
21 import java.util.LinkedHashSet;
22 import java.util.Set;
23
24 import javax.crypto.SecretKey;
25
26 import org.htmlunit.corejs.javascript.Scriptable;
27 import org.htmlunit.corejs.javascript.VarScope;
28 import org.htmlunit.javascript.HtmlUnitScriptable;
29 import org.htmlunit.javascript.JavaScriptEngine;
30 import org.htmlunit.javascript.configuration.JsxClass;
31 import org.htmlunit.javascript.configuration.JsxConstructor;
32 import org.htmlunit.javascript.configuration.JsxGetter;
33
34 /**
35 * A JavaScript object for {@code CryptoKey}.
36 *
37 * @see <a href="https://w3c.github.io/webcrypto/#dfn-CryptoKey">CryptoKey</a>
38 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey">MDN Documentation</a>
39 *
40 * @author Ahmed Ashour
41 * @author Ronald Brill
42 * @author Lai Quang Duong
43 */
44 @JsxClass
45 public class CryptoKey extends HtmlUnitScriptable {
46
47 private Key internalKey_;
48 private String type_;
49 private boolean isExtractable_;
50 private KeyAlgorithm algorithm_;
51 private Set<String> usages_;
52
53 // the JavaScript objects returned by the algorithm and usages properties, created on first access
54 private Scriptable algorithmObject_;
55 private Scriptable usagesObject_;
56
57 /**
58 * JavaScript constructor.
59 */
60 @JsxConstructor
61 public void jsConstructor() {
62 throw JavaScriptEngine.typeErrorIllegalConstructor();
63 }
64
65 /**
66 * Creates a properly scoped CryptoKey from the given parameters.
67 *
68 * @param scope the JS scope
69 * @param internalKey the Java key (SecretKey, PublicKey, or PrivateKey)
70 * @param isExtractable whether the key can be exported
71 * @param algorithm the key algorithm
72 * @param usages the permitted key usages
73 * @return the new CryptoKey
74 */
75 static CryptoKey create(final VarScope scope, final Key internalKey, final boolean isExtractable,
76 final KeyAlgorithm algorithm, final Collection<String> usages) {
77 if (internalKey == null) {
78 throw new IllegalArgumentException("The provided key can't be null");
79 }
80
81 final CryptoKey key = new CryptoKey();
82 key.internalKey_ = internalKey;
83
84 if (internalKey instanceof PublicKey) {
85 key.type_ = "public";
86 }
87 else if (internalKey instanceof PrivateKey) {
88 key.type_ = "private";
89 }
90 else if (internalKey instanceof SecretKey) {
91 key.type_ = "secret";
92 }
93 else {
94 throw new IllegalStateException("Unsupported key type: " + internalKey.getClass());
95 }
96
97 key.isExtractable_ = isExtractable;
98 key.algorithm_ = algorithm;
99 key.usages_ = new LinkedHashSet<>(usages);
100
101 key.setParentScope(scope);
102 key.setPrototype(getWindow(key).getPrototype(CryptoKey.class));
103 return key;
104 }
105
106 /**
107 * Returns the underlying Java key.
108 * @return the Java key (opaque {@code [[handle]]} internal slot)
109 */
110 public Key getInternalKey() {
111 return internalKey_;
112 }
113
114 /**
115 * Returns the type of key this represents.
116 * @return the key type: "public", "private", or "secret"
117 */
118 @JsxGetter
119 public String getType() {
120 return type_;
121 }
122
123 /**
124 * Returns whether the key material may be exported.
125 * @return whether the key material may be exported
126 */
127 @JsxGetter
128 public boolean getExtractable() {
129 return isExtractable_;
130 }
131
132 /**
133 * Returns the algorithm descriptor object. The object is created once for every key; changing it
134 * does not change the key.
135 * @return the algorithm descriptor object
136 */
137 @JsxGetter
138 public Scriptable getAlgorithm() {
139 if (algorithmObject_ == null) {
140 algorithmObject_ = algorithm_.toScriptableObject(getParentScope());
141 }
142 return algorithmObject_;
143 }
144
145 /**
146 * Returns the key algorithm for internal use.
147 * @return the key algorithm ({@code [[algorithm]]} internal slot)
148 */
149 KeyAlgorithm getKeyAlgorithm() {
150 return algorithm_;
151 }
152
153 /**
154 * Returns the permitted key usages. The array is created once for every key.
155 * @return the permitted key usages as a JS array
156 */
157 @JsxGetter
158 public Scriptable getUsages() {
159 if (usagesObject_ == null) {
160 usagesObject_ = JavaScriptEngine.newArray(getParentScope(), usages_.toArray());
161 }
162 return usagesObject_;
163 }
164
165 /**
166 * Returns the permitted key usages for internal use.
167 * @return the permitted key usages as a Java set (for internal use)
168 */
169 public Set<String> getUsagesInternal() {
170 return usages_;
171 }
172 }