6
6
JSONStringify,
7
7
ObjectDefineProperties,
8
8
ReflectApply,
9
+ ReflectConstruct,
9
10
SafeSet,
10
11
SymbolToStringTag,
11
12
StringPrototypeRepeat,
@@ -31,6 +32,7 @@ const { TextDecoder, TextEncoder } = require('internal/encoding');
31
32
32
33
const {
33
34
codes : {
35
+ ERR_ILLEGAL_CONSTRUCTOR ,
34
36
ERR_INVALID_ARG_TYPE ,
35
37
ERR_INVALID_THIS ,
36
38
}
@@ -70,12 +72,21 @@ const {
70
72
randomUUID : _randomUUID ,
71
73
} = require ( 'internal/crypto/random' ) ;
72
74
73
- const randomUUID = ( ) => _randomUUID ( ) ;
75
+ async function digest ( algorithm , data ) {
76
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
77
+ return ReflectApply ( asyncDigest , this , arguments ) ;
78
+ }
79
+
80
+ function randomUUID ( ) {
81
+ if ( this !== crypto ) throw new ERR_INVALID_THIS ( 'Crypto' ) ;
82
+ return _randomUUID ( ) ;
83
+ }
74
84
75
85
async function generateKey (
76
86
algorithm ,
77
87
extractable ,
78
88
keyUsages ) {
89
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
79
90
algorithm = normalizeAlgorithm ( algorithm ) ;
80
91
validateBoolean ( extractable , 'extractable' ) ;
81
92
validateArray ( keyUsages , 'keyUsages' ) ;
@@ -123,6 +134,7 @@ async function generateKey(
123
134
}
124
135
125
136
async function deriveBits ( algorithm , baseKey , length ) {
137
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
126
138
algorithm = normalizeAlgorithm ( algorithm ) ;
127
139
if ( ! isCryptoKey ( baseKey ) )
128
140
throw new ERR_INVALID_ARG_TYPE ( 'baseKey' , 'CryptoKey' , baseKey ) ;
@@ -194,6 +206,7 @@ async function deriveKey(
194
206
derivedKeyAlgorithm ,
195
207
extractable ,
196
208
keyUsages ) {
209
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
197
210
algorithm = normalizeAlgorithm ( algorithm ) ;
198
211
derivedKeyAlgorithm = normalizeAlgorithm ( derivedKeyAlgorithm ) ;
199
212
if ( ! isCryptoKey ( baseKey ) )
@@ -238,7 +251,11 @@ async function deriveKey(
238
251
throw lazyDOMException ( 'Unrecognized name.' ) ;
239
252
}
240
253
241
- return importKey ( 'raw' , bits , derivedKeyAlgorithm , extractable , keyUsages ) ;
254
+ return ReflectApply (
255
+ importKey ,
256
+ this ,
257
+ [ 'raw' , bits , derivedKeyAlgorithm , extractable , keyUsages ] ,
258
+ ) ;
242
259
}
243
260
244
261
async function exportKeySpki ( key ) {
@@ -415,6 +432,7 @@ async function exportKeyJWK(key) {
415
432
}
416
433
417
434
async function exportKey ( format , key ) {
435
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
418
436
validateString ( format , 'format' ) ;
419
437
validateOneOf ( format , 'format' , kExportFormats ) ;
420
438
if ( ! isCryptoKey ( key ) )
@@ -496,6 +514,7 @@ async function importKey(
496
514
algorithm ,
497
515
extractable ,
498
516
keyUsages ) {
517
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
499
518
validateString ( format , 'format' ) ;
500
519
validateOneOf ( format , 'format' , kExportFormats ) ;
501
520
if ( format !== 'node.keyObject' && format !== 'jwk' )
@@ -557,8 +576,9 @@ async function importKey(
557
576
// subtle.wrapKey() is essentially a subtle.exportKey() followed
558
577
// by a subtle.encrypt().
559
578
async function wrapKey ( format , key , wrappingKey , algorithm ) {
579
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
560
580
algorithm = normalizeAlgorithm ( algorithm ) ;
561
- let keyData = await exportKey ( format , key ) ;
581
+ let keyData = await ReflectApply ( exportKey , this , [ format , key ] ) ;
562
582
563
583
if ( format === 'jwk' ) {
564
584
if ( keyData == null || typeof keyData !== 'object' )
@@ -586,6 +606,7 @@ async function unwrapKey(
586
606
unwrappedKeyAlgo ,
587
607
extractable ,
588
608
keyUsages ) {
609
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
589
610
wrappedKey = getArrayBufferOrView ( wrappedKey , 'wrappedKey' ) ;
590
611
unwrapAlgo = normalizeAlgorithm ( unwrapAlgo ) ;
591
612
let keyData = await cipherOrWrap (
@@ -607,7 +628,11 @@ async function unwrapKey(
607
628
}
608
629
}
609
630
610
- return importKey ( format , keyData , unwrappedKeyAlgo , extractable , keyUsages ) ;
631
+ return ReflectApply (
632
+ importKey ,
633
+ this ,
634
+ [ format , keyData , unwrappedKeyAlgo , extractable , keyUsages ] ,
635
+ ) ;
611
636
}
612
637
613
638
function signVerify ( algorithm , key , data , signature ) {
@@ -654,10 +679,12 @@ function signVerify(algorithm, key, data, signature) {
654
679
}
655
680
656
681
async function sign ( algorithm , key , data ) {
682
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
657
683
return signVerify ( algorithm , key , data ) ;
658
684
}
659
685
660
686
async function verify ( algorithm , key , signature , data ) {
687
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
661
688
return signVerify ( algorithm , key , data , signature ) ;
662
689
}
663
690
@@ -707,30 +734,39 @@ async function cipherOrWrap(mode, algorithm, key, data, op) {
707
734
}
708
735
709
736
async function encrypt ( algorithm , key , data ) {
737
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
710
738
return cipherOrWrap ( kWebCryptoCipherEncrypt , algorithm , key , data , 'encrypt' ) ;
711
739
}
712
740
713
741
async function decrypt ( algorithm , key , data ) {
742
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
714
743
return cipherOrWrap ( kWebCryptoCipherDecrypt , algorithm , key , data , 'decrypt' ) ;
715
744
}
716
745
717
746
// The SubtleCrypto and Crypto classes are defined as part of the
718
747
// Web Crypto API standard: https://www.w3.org/TR/WebCryptoAPI/
719
748
720
- class SubtleCrypto { }
721
- const subtle = new SubtleCrypto ( ) ;
749
+ class SubtleCrypto {
750
+ constructor ( ) {
751
+ throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
752
+ }
753
+ }
754
+ const subtle = ReflectConstruct ( function ( ) { } , [ ] , SubtleCrypto ) ;
722
755
723
756
class Crypto {
757
+ constructor ( ) {
758
+ throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
759
+ }
760
+
724
761
get subtle ( ) {
762
+ if ( this !== crypto ) throw new ERR_INVALID_THIS ( 'Crypto' ) ;
725
763
return subtle ;
726
764
}
727
765
}
728
- const crypto = new Crypto ( ) ;
766
+ const crypto = ReflectConstruct ( function ( ) { } , [ ] , Crypto ) ;
729
767
730
768
function getRandomValues ( array ) {
731
- if ( ! ( this instanceof Crypto ) ) {
732
- throw new ERR_INVALID_THIS ( 'Crypto' ) ;
733
- }
769
+ if ( this !== crypto ) throw new ERR_INVALID_THIS ( 'Crypto' ) ;
734
770
return ReflectApply ( _getRandomValues , this , arguments ) ;
735
771
}
736
772
@@ -799,7 +835,7 @@ ObjectDefineProperties(
799
835
enumerable : true ,
800
836
configurable : true ,
801
837
writable : true ,
802
- value : asyncDigest ,
838
+ value : digest ,
803
839
} ,
804
840
generateKey : {
805
841
enumerable : true ,
0 commit comments