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.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 Scriptable algorithm_;
51      private Set<String> usages_;
52  
53      /**
54       * JavaScript constructor.
55       */
56      @JsxConstructor
57      public void jsConstructor() {
58          throw JavaScriptEngine.typeErrorIllegalConstructor();
59      }
60  
61      /**
62       * Creates a properly scoped CryptoKey from the given parameters.
63       *
64       * @param scope the JS scope
65       * @param internalKey the Java key (SecretKey, PublicKey, or PrivateKey)
66       * @param isExtractable whether the key can be exported
67       * @param algorithm the JS algorithm descriptor object
68       * @param usages the permitted key usages
69       * @return the new CryptoKey
70       */
71      static CryptoKey create(final VarScope scope, final Key internalKey, final boolean isExtractable,
72              final Scriptable algorithm, final Collection<String> usages) {
73          if (internalKey == null) {
74              throw new NullPointerException("The provided key can't be null");
75          }
76  
77          final CryptoKey key = new CryptoKey();
78          key.internalKey_ = internalKey;
79  
80          if (internalKey instanceof PublicKey) {
81              key.type_ = "public";
82          }
83          else if (internalKey instanceof PrivateKey) {
84              key.type_ = "private";
85          }
86          else if (internalKey instanceof SecretKey) {
87              key.type_ = "secret";
88          }
89          else {
90              throw new IllegalStateException("Unsupported key type: " + internalKey.getClass());
91          }
92  
93          key.isExtractable_ = isExtractable;
94          key.algorithm_ = algorithm;
95          key.usages_ = new LinkedHashSet<>(usages);
96  
97          key.setParentScope(scope);
98          key.setPrototype(getWindow(key).getPrototype(CryptoKey.class));
99          return key;
100     }
101 
102     /**
103      * Returns the underlying Java key.
104      * @return the Java key (opaque {@code [[handle]]} internal slot)
105      */
106     public Key getInternalKey() {
107         return internalKey_;
108     }
109 
110     /**
111      * Returns the type of key this represents.
112      * @return the key type: "public", "private", or "secret"
113      */
114     @JsxGetter
115     public String getType() {
116         return type_;
117     }
118 
119     /**
120      * Returns whether the key material may be exported.
121      * @return whether the key material may be exported
122      */
123     @JsxGetter
124     public boolean getExtractable() {
125         return isExtractable_;
126     }
127 
128     /**
129      * Returns the algorithm descriptor object.
130      * @return the algorithm descriptor object
131      */
132     @JsxGetter
133     public Scriptable getAlgorithm() {
134         return algorithm_;
135     }
136 
137     /**
138      * Returns the permitted key usages.
139      * @return the permitted key usages as a JS array
140      */
141     @JsxGetter
142     public Scriptable getUsages() {
143         return JavaScriptEngine.newArray(getParentScope(), usages_.toArray());
144     }
145 
146     /**
147      * Returns the permitted key usages for internal use.
148      * @return the permitted key usages as a Java set (for internal use)
149      */
150     public Set<String> getUsagesInternal() {
151         return usages_;
152     }
153 }