|
14 | 14 | */ |
15 | 15 | package org.htmlunit.javascript.host.crypto; |
16 | 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.Objects; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import javax.crypto.SecretKey; |
| 26 | + |
| 27 | +import org.htmlunit.corejs.javascript.Scriptable; |
17 | 28 | import org.htmlunit.javascript.HtmlUnitScriptable; |
| 29 | +import org.htmlunit.javascript.JavaScriptEngine; |
18 | 30 | import org.htmlunit.javascript.configuration.JsxClass; |
19 | 31 | import org.htmlunit.javascript.configuration.JsxConstructor; |
| 32 | +import org.htmlunit.javascript.configuration.JsxGetter; |
| 33 | +import org.htmlunit.javascript.host.Window; |
20 | 34 |
|
21 | 35 | /** |
22 | 36 | * A JavaScript object for {@code CryptoKey}. |
23 | 37 | * |
| 38 | + * @see <a href="https://w3c.github.io/webcrypto/#dfn-CryptoKey">CryptoKey</a> |
| 39 | + * |
24 | 40 | * @author Ahmed Ashour |
25 | 41 | * @author Ronald Brill |
| 42 | + * @author Lai Quang Duong |
26 | 43 | */ |
27 | 44 | @JsxClass |
28 | 45 | public class CryptoKey extends HtmlUnitScriptable { |
29 | 46 |
|
| 47 | + private Key internalKey_; |
| 48 | + private String type_; |
| 49 | + private boolean isExtractable_; |
| 50 | + private Scriptable algorithm_; |
| 51 | + private Set<String> usages_; |
| 52 | + |
30 | 53 | /** |
31 | 54 | * JavaScript constructor. |
32 | 55 | */ |
33 | 56 | @JsxConstructor |
34 | 57 | public void jsConstructor() { |
35 | | - // nothing to do |
| 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 Scriptable scope, final Key internalKey, final boolean isExtractable, |
| 72 | + final Scriptable algorithm, final Collection<String> usages) { |
| 73 | + final CryptoKey key = new CryptoKey(); |
| 74 | + key.internalKey_ = Objects.requireNonNull(internalKey); |
| 75 | + |
| 76 | + if (internalKey instanceof PublicKey) { |
| 77 | + key.type_ = "public"; |
| 78 | + } |
| 79 | + else if (internalKey instanceof PrivateKey) { |
| 80 | + key.type_ = "private"; |
| 81 | + } |
| 82 | + else if (internalKey instanceof SecretKey) { |
| 83 | + key.type_ = "secret"; |
| 84 | + } |
| 85 | + else { |
| 86 | + throw new IllegalStateException("Unsupported key type: " + internalKey.getClass()); |
| 87 | + } |
| 88 | + |
| 89 | + key.isExtractable_ = isExtractable; |
| 90 | + key.algorithm_ = algorithm; |
| 91 | + key.usages_ = new LinkedHashSet<>(usages); |
| 92 | + |
| 93 | + final Window window = getWindow(Objects.requireNonNull(scope)); |
| 94 | + key.setParentScope(window); |
| 95 | + key.setPrototype(window.getPrototype(CryptoKey.class)); |
| 96 | + return key; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return the Java key (opaque {@code [[handle]]} internal slot) |
| 101 | + */ |
| 102 | + public Key getInternalKey() { |
| 103 | + return internalKey_; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return the key type: "public", "private", or "secret" |
| 108 | + */ |
| 109 | + @JsxGetter |
| 110 | + public String getType() { |
| 111 | + return type_; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return whether the key material may be exported |
| 116 | + */ |
| 117 | + @JsxGetter |
| 118 | + public boolean getExtractable() { |
| 119 | + return isExtractable_; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return the algorithm descriptor object |
| 124 | + */ |
| 125 | + @JsxGetter |
| 126 | + public Scriptable getAlgorithm() { |
| 127 | + return algorithm_; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @return the permitted key usages as a JS array |
| 132 | + */ |
| 133 | + @JsxGetter |
| 134 | + public Scriptable getUsages() { |
| 135 | + return JavaScriptEngine.newArray(this, usages_.toArray()); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @return the permitted key usages as a Java set (for internal use) |
| 140 | + */ |
| 141 | + public Set<String> getUsagesInternal() { |
| 142 | + return usages_; |
36 | 143 | } |
37 | 144 | } |
0 commit comments