|
34 | 34 | #include "builtins/cache-override.h"
|
35 | 35 | #include "builtins/compression-stream.h"
|
36 | 36 | #include "builtins/console.h"
|
| 37 | +#include "builtins/crypto.h" |
37 | 38 | #include "builtins/decompression-stream.h"
|
38 | 39 | #include "builtins/dictionary.h"
|
39 | 40 | #include "builtins/env.h"
|
@@ -427,65 +428,6 @@ JSObject *create(JSContext *cx, HandleObject self, HandleValue url_val, HandleOb
|
427 | 428 | JSObject *create(JSContext *cx, HandleObject self, HandleValue url_val, HandleValue base_val);
|
428 | 429 | } // namespace URL
|
429 | 430 |
|
430 |
| -bool is_int_typed_array(JSObject *obj) { |
431 |
| - return JS_IsInt8Array(obj) || JS_IsUint8Array(obj) || JS_IsInt16Array(obj) || |
432 |
| - JS_IsUint16Array(obj) || JS_IsInt32Array(obj) || JS_IsUint32Array(obj) || |
433 |
| - JS_IsUint8ClampedArray(obj) || JS_IsBigInt64Array(obj) || JS_IsBigUint64Array(obj); |
434 |
| -} |
435 |
| - |
436 |
| -namespace Crypto { |
437 |
| - |
438 |
| -#define MAX_BYTE_LENGTH 65536 |
439 |
| - |
440 |
| -/** |
441 |
| - * Implementation of |
442 |
| - * https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues |
443 |
| - * TODO: investigate ways to automatically wipe the buffer passed in here when |
444 |
| - * it is GC'd. Content can roughly approximate that using finalizers for views |
445 |
| - * of the buffer, but it's far from ideal. |
446 |
| - */ |
447 |
| -bool get_random_values(JSContext *cx, unsigned argc, Value *vp) { |
448 |
| - CallArgs args = CallArgsFromVp(argc, vp); |
449 |
| - if (!args.requireAtLeast(cx, "crypto.getRandomValues", 1)) |
450 |
| - return false; |
451 |
| - |
452 |
| - if (!args[0].isObject() || !is_int_typed_array(&args[0].toObject())) { |
453 |
| - JS_ReportErrorUTF8(cx, "crypto.getRandomValues: input must be an integer-typed TypedArray"); |
454 |
| - return false; |
455 |
| - } |
456 |
| - |
457 |
| - RootedObject typed_array(cx, &args[0].toObject()); |
458 |
| - size_t byte_length = JS_GetArrayBufferViewByteLength(typed_array); |
459 |
| - if (byte_length > MAX_BYTE_LENGTH) { |
460 |
| - JS_ReportErrorUTF8(cx, |
461 |
| - "crypto.getRandomValues: input byteLength must be at most %u, " |
462 |
| - "but is %zu", |
463 |
| - MAX_BYTE_LENGTH, byte_length); |
464 |
| - return false; |
465 |
| - } |
466 |
| - |
467 |
| - JS::AutoCheckCannotGC noGC(cx); |
468 |
| - bool is_shared; |
469 |
| - void *buffer = JS_GetArrayBufferViewData(typed_array, &is_shared, noGC); |
470 |
| - arc4random_buf(buffer, byte_length); |
471 |
| - |
472 |
| - args.rval().setObject(*typed_array); |
473 |
| - return true; |
474 |
| -} |
475 |
| - |
476 |
| -const JSFunctionSpec methods[] = {JS_FN("getRandomValues", get_random_values, 1, JSPROP_ENUMERATE), |
477 |
| - JS_FS_END}; |
478 |
| - |
479 |
| -static bool create(JSContext *cx, HandleObject global) { |
480 |
| - RootedObject crypto(cx, JS_NewPlainObject(cx)); |
481 |
| - if (!crypto) |
482 |
| - return false; |
483 |
| - if (!JS_DefineProperty(cx, global, "crypto", crypto, JSPROP_ENUMERATE)) |
484 |
| - return false; |
485 |
| - return JS_DefineFunctions(cx, crypto, methods); |
486 |
| -} |
487 |
| -} // namespace Crypto |
488 |
| - |
489 | 431 | enum class BodyReadResult {
|
490 | 432 | ArrayBuffer,
|
491 | 433 | JSON,
|
@@ -4947,7 +4889,7 @@ bool define_fastly_sys(JSContext *cx, HandleObject global) {
|
4947 | 4889 | return false;
|
4948 | 4890 | if (!Console::create(cx, global))
|
4949 | 4891 | return false;
|
4950 |
| - if (!Crypto::create(cx, global)) |
| 4892 | + if (!builtins::Crypto::create(cx, global)) |
4951 | 4893 | return false;
|
4952 | 4894 |
|
4953 | 4895 | if (!NativeStreamSource::init_class(cx, global))
|
|
0 commit comments