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