|
14 | 14 | */ |
15 | 15 | package org.htmlunit.javascript.host.crypto; |
16 | 16 |
|
| 17 | +import java.nio.ByteBuffer; |
| 18 | +import java.security.GeneralSecurityException; |
| 19 | +import java.security.MessageDigest; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.htmlunit.corejs.javascript.EcmaError; |
17 | 24 | import org.htmlunit.corejs.javascript.NativePromise; |
| 25 | +import org.htmlunit.corejs.javascript.Scriptable; |
| 26 | +import org.htmlunit.corejs.javascript.ScriptableObject; |
| 27 | +import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer; |
| 28 | +import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBufferView; |
18 | 29 | import org.htmlunit.javascript.HtmlUnitScriptable; |
19 | 30 | import org.htmlunit.javascript.JavaScriptEngine; |
20 | 31 | import org.htmlunit.javascript.configuration.JsxClass; |
|
28 | 39 | * @author Ahmed Ashour |
29 | 40 | * @author Ronald Brill |
30 | 41 | * @author Atsushi Nakagawa |
| 42 | + * @author Lai Quang Duong |
31 | 43 | */ |
32 | 44 | @JsxClass |
33 | 45 | public class SubtleCrypto extends HtmlUnitScriptable { |
34 | 46 |
|
| 47 | + /** |
| 48 | + * Maps each crypto operation to its supported algorithm names. |
| 49 | + * @see <a href="https://w3c.github.io/webcrypto/#algorithm-overview">Algorithm Overview</a> |
| 50 | + */ |
| 51 | + private static final Map<String, Set<String>> OPERATION_TO_SUPPORTED_ALGORITHMS = Map.ofEntries( |
| 52 | + Map.entry("encrypt", Set.of("RSA-OAEP", "AES-CTR", "AES-CBC", "AES-GCM")), |
| 53 | + Map.entry("decrypt", Set.of("RSA-OAEP", "AES-CTR", "AES-CBC", "AES-GCM")), |
| 54 | + Map.entry("sign", Set.of("RSASSA-PKCS1-v1_5", "RSA-PSS", "ECDSA", "HMAC")), |
| 55 | + Map.entry("verify", Set.of("RSASSA-PKCS1-v1_5", "RSA-PSS", "ECDSA", "HMAC")), |
| 56 | + Map.entry("digest", Set.of("SHA-1", "SHA-256", "SHA-384", "SHA-512")), |
| 57 | + Map.entry("generateKey", Set.of("RSASSA-PKCS1-v1_5", "RSA-PSS", "RSA-OAEP", |
| 58 | + "ECDSA", "ECDH", "AES-CTR", "AES-CBC", "AES-GCM", "AES-KW", "HMAC")), |
| 59 | + Map.entry("importKey", Set.of("RSASSA-PKCS1-v1_5", "RSA-PSS", "RSA-OAEP", "ECDSA", "ECDH", |
| 60 | + "AES-CTR", "AES-CBC", "AES-GCM", "AES-KW", "HMAC", "HKDF", "PBKDF2")), |
| 61 | + Map.entry("wrapKey", Set.of("RSA-OAEP", "AES-CTR", "AES-CBC", "AES-GCM", "AES-KW")), |
| 62 | + Map.entry("unwrapKey", Set.of("RSA-OAEP", "AES-CTR", "AES-CBC", "AES-GCM", "AES-KW")), |
| 63 | + Map.entry("deriveBits", Set.of("ECDH", "HKDF", "PBKDF2")), |
| 64 | + Map.entry("deriveKey", Set.of("ECDH", "HKDF", "PBKDF2")) |
| 65 | + ); |
| 66 | + |
35 | 67 | /** |
36 | 68 | * Creates an instance. |
37 | 69 | */ |
@@ -86,13 +118,35 @@ public NativePromise verify() { |
86 | 118 | } |
87 | 119 |
|
88 | 120 | /** |
89 | | - * Not yet implemented. |
90 | | - * |
91 | | - * @return a Promise which will be fulfilled with the digest |
| 121 | + * Generates a digest of the given data. |
| 122 | + * @see <a href="https://w3c.github.io/webcrypto/#SubtleCrypto-method-digest">SubtleCrypto.digest()</a> |
| 123 | + * @param hashAlgorithm a string or an object with a single property name containing the hash algorithm to use |
| 124 | + * @param data an object containing the data to be digested |
| 125 | + * @return a Promise that fulfills with an ArrayBuffer containing the digest |
92 | 126 | */ |
93 | 127 | @JsxFunction |
94 | | - public NativePromise digest() { |
95 | | - return notImplemented(); |
| 128 | + public NativePromise digest(final Object hashAlgorithm, final Object data) { |
| 129 | + final byte[] digest; |
| 130 | + try { |
| 131 | + final ByteBuffer inputData = asByteBuffer(data); |
| 132 | + final String algorithm = resolveAlgorithmName(hashAlgorithm); |
| 133 | + ensureAlgorithmIsSupported("digest", algorithm); |
| 134 | + |
| 135 | + final MessageDigest messageDigest = MessageDigest.getInstance(algorithm); |
| 136 | + messageDigest.update(inputData); |
| 137 | + digest = messageDigest.digest(); |
| 138 | + } |
| 139 | + catch (final EcmaError e) { |
| 140 | + return setupRejectedPromise(() -> e); |
| 141 | + } |
| 142 | + catch (final IllegalArgumentException e) { |
| 143 | + return setupRejectedPromise(() -> new DOMException(e.getMessage(), DOMException.SYNTAX_ERR)); |
| 144 | + } |
| 145 | + catch (final GeneralSecurityException | UnsupportedOperationException e) { |
| 146 | + return setupRejectedPromise(() -> new DOMException("Operation is not supported: " + e.getMessage(), |
| 147 | + DOMException.NOT_SUPPORTED_ERR)); |
| 148 | + } |
| 149 | + return setupPromise(() -> createArrayBuffer(digest)); |
96 | 150 | } |
97 | 151 |
|
98 | 152 | /** |
@@ -164,4 +218,79 @@ public NativePromise wrapKey() { |
164 | 218 | public NativePromise unwrapKey() { |
165 | 219 | return notImplemented(); |
166 | 220 | } |
| 221 | + |
| 222 | + /** |
| 223 | + * Checks if the specified crypto operation supports the given algorithm. |
| 224 | + * @see <a href="https://w3c.github.io/webcrypto/#algorithm-overview">Algorithm Overview</a> |
| 225 | + * @param operation the crypto operation (e.g. "digest", "sign") |
| 226 | + * @param algorithm the algorithm name (e.g. "SHA-256", "HMAC") |
| 227 | + * @throws UnsupportedOperationException if the operation does not support the algorithm |
| 228 | + */ |
| 229 | + private static void ensureAlgorithmIsSupported(final String operation, final String algorithm) { |
| 230 | + final Set<String> supportedAlgorithms = OPERATION_TO_SUPPORTED_ALGORITHMS.get(operation); |
| 231 | + if (supportedAlgorithms == null || !supportedAlgorithms.contains(algorithm)) { |
| 232 | + throw new UnsupportedOperationException(operation + " " + algorithm); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Resolves the algorithm name from the given {@code AlgorithmIdentifier}. |
| 238 | + * @see <a href="https://w3c.github.io/webcrypto/#dfn-AlgorithmIdentifier"> |
| 239 | + * AlgorithmIdentifier</a> |
| 240 | + * @param algorithm the algorithm identifier (String or Scriptable with name property) |
| 241 | + * @return the resolved algorithm name |
| 242 | + * @throws IllegalArgumentException if the identifier cannot be resolved |
| 243 | + */ |
| 244 | + static String resolveAlgorithmName(final Object algorithm) { |
| 245 | + if (algorithm instanceof String str) { |
| 246 | + return str; |
| 247 | + } |
| 248 | + if (algorithm instanceof Scriptable obj) { |
| 249 | + final Object name = ScriptableObject.getProperty(obj, "name"); |
| 250 | + if (name instanceof String nameStr) { |
| 251 | + return nameStr; |
| 252 | + } |
| 253 | + } |
| 254 | + throw new IllegalArgumentException("An invalid or illegal string was specified"); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Converts ArrayBuffer or ArrayBufferView to a ByteBuffer. |
| 259 | + * @param data the buffer source object |
| 260 | + * @return the ByteBuffer wrapping the data |
| 261 | + * @throws IllegalArgumentException if data is not a Scriptable or is NOT_FOUND |
| 262 | + * @throws EcmaError if data is not an ArrayBuffer or ArrayBufferView |
| 263 | + */ |
| 264 | + static ByteBuffer asByteBuffer(final Object data) { |
| 265 | + if (!(data instanceof Scriptable)) { |
| 266 | + throw new IllegalArgumentException("An invalid or illegal string was specified"); |
| 267 | + } |
| 268 | + if (data == Scriptable.NOT_FOUND) { |
| 269 | + throw new IllegalArgumentException("An invalid or illegal string was specified"); |
| 270 | + } |
| 271 | + if (data instanceof NativeArrayBuffer nativeBuffer) { |
| 272 | + return ByteBuffer.wrap(nativeBuffer.getBuffer()); |
| 273 | + } |
| 274 | + else if (data instanceof NativeArrayBufferView arrayBufferView) { |
| 275 | + final NativeArrayBuffer arrayBuffer = arrayBufferView.getBuffer(); |
| 276 | + return ByteBuffer.wrap( |
| 277 | + arrayBuffer.getBuffer(), arrayBufferView.getByteOffset(), arrayBufferView.getByteLength()); |
| 278 | + } |
| 279 | + else { |
| 280 | + throw JavaScriptEngine.typeError("Argument could not be converted to any of: ArrayBufferView, ArrayBuffer."); |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * Creates a NativeArrayBuffer with proper scope and prototype from the given bytes. |
| 286 | + * @param data the byte array to wrap |
| 287 | + * @return the new NativeArrayBuffer |
| 288 | + */ |
| 289 | + NativeArrayBuffer createArrayBuffer(final byte[] data) { |
| 290 | + final NativeArrayBuffer buffer = new NativeArrayBuffer(data.length); |
| 291 | + System.arraycopy(data, 0, buffer.getBuffer(), 0, data.length); |
| 292 | + buffer.setParentScope(getParentScope()); |
| 293 | + buffer.setPrototype(ScriptableObject.getClassPrototype(getWindow(), buffer.getClassName())); |
| 294 | + return buffer; |
| 295 | + } |
167 | 296 | } |
0 commit comments