diff --git a/webcrypto/aes.go b/webcrypto/aes.go index fe9f126..ec604d8 100644 --- a/webcrypto/aes.go +++ b/webcrypto/aes.go @@ -34,7 +34,7 @@ func newAesKeyGenParams(rt *goja.Runtime, normalized Algorithm, params goja.Valu // callback below could lead to a race condition. algorithmLengthValue, err := traverseObject(rt, params, "length") if err != nil { - return nil, NewError(0, SyntaxError, "could not get length from algorithm parameter") + return nil, NewError(SyntaxError, "could not get length from algorithm parameter") } algorithmLength := algorithmLengthValue.ToInteger() @@ -65,20 +65,20 @@ func (akgp *AesKeyGenParams) GenerateKey( continue } - return nil, NewError(0, SyntaxError, "invalid key usage") + return nil, NewError(SyntaxError, "invalid key usage") default: - return nil, NewError(0, SyntaxError, "invalid key usage") + return nil, NewError(SyntaxError, "invalid key usage") } } if akgp.Length != 128 && akgp.Length != 192 && akgp.Length != 256 { - return nil, NewError(0, OperationError, "invalid key length") + return nil, NewError(OperationError, "invalid key length") } randomKey := make([]byte, akgp.Length/8) if _, err := rand.Read(randomKey); err != nil { // 4. - return nil, NewError(0, OperationError, "could not generate random key") + return nil, NewError(OperationError, "could not generate random key") } // 5. 6. 7. 8. 9. @@ -119,25 +119,25 @@ type AesKeyAlgorithm struct { // TODO @oleiade: support JWK format. func exportAESKey(key *CryptoKey, format KeyFormat) ([]byte, error) { if !key.Extractable { - return nil, NewError(0, InvalidAccessError, "the key is not extractable") + return nil, NewError(InvalidAccessError, "the key is not extractable") } // 1. if key.handle == nil { - return nil, NewError(0, OperationError, "the key is not valid, no data") + return nil, NewError(OperationError, "the key is not valid, no data") } switch format { case RawKeyFormat: handle, ok := key.handle.([]byte) if !ok { - return nil, NewError(0, ImplementationError, "exporting key data's bytes failed") + return nil, NewError(ImplementationError, "exporting key data's bytes failed") } return handle, nil default: // FIXME: note that we do not support JWK format, yet. - return nil, NewError(0, NotSupportedError, "unsupported key format "+format) + return nil, NewError(NotSupportedError, "unsupported key format "+format) } } @@ -168,7 +168,7 @@ func (aip *aesImportParams) ImportKey( case EncryptCryptoKeyUsage, DecryptCryptoKeyUsage, WrapKeyCryptoKeyUsage, UnwrapKeyCryptoKeyUsage: continue default: - return nil, NewError(0, SyntaxError, "invalid key usage: "+usage) + return nil, NewError(SyntaxError, "invalid key usage: "+usage) } } @@ -181,10 +181,10 @@ func (aip *aesImportParams) ImportKey( ) if !has128Bits && !has192Bits && !has256Bits { - return nil, NewError(0, DataError, "invalid key length") + return nil, NewError(DataError, "invalid key length") } default: - return nil, NewError(0, NotSupportedError, "unsupported key format "+format) + return nil, NewError(NotSupportedError, "unsupported key format "+format) } key := &CryptoKey{ @@ -229,24 +229,24 @@ func (acp *AesCbcParams) Encrypt(plaintext []byte, key CryptoKey) ([]byte, error // 1. // Note that aes.BlockSize stands for the `k` variable as per the specification. if len(acp.Iv) != aes.BlockSize { - return nil, NewError(0, OperationError, "iv length is not 16 bytes") + return nil, NewError(OperationError, "iv length is not 16 bytes") } // 2. paddedPlainText, err := pKCS7Pad(plaintext, aes.BlockSize) if err != nil { - return nil, NewError(0, OperationError, "could not pad plaintext") + return nil, NewError(OperationError, "could not pad plaintext") } keyHandle, ok := key.handle.([]byte) if !ok { - return nil, NewError(0, ImplementationError, "could not get key handle") + return nil, NewError(ImplementationError, "could not get key handle") } // 3. block, err := aes.NewCipher(keyHandle) if err != nil { - return nil, NewError(0, OperationError, "could not create cipher") + return nil, NewError(OperationError, "could not create cipher") } ciphertext := make([]byte, len(paddedPlainText)) @@ -263,18 +263,18 @@ func (acp *AesCbcParams) Encrypt(plaintext []byte, key CryptoKey) ([]byte, error func (acp *AesCbcParams) Decrypt(ciphertext []byte, key CryptoKey) ([]byte, error) { // 1. if len(acp.Iv) != aes.BlockSize { - return nil, NewError(0, OperationError, "iv length is invalid, should be 16 bytes") + return nil, NewError(OperationError, "iv length is invalid, should be 16 bytes") } keyHandle, ok := key.handle.([]byte) if !ok { - return nil, NewError(0, OperationError, "invalid key handle") + return nil, NewError(OperationError, "invalid key handle") } // 2. block, err := aes.NewCipher(keyHandle) if err != nil { - return nil, NewError(0, OperationError, "could not create AES cipher") + return nil, NewError(OperationError, "could not create AES cipher") } paddedPlainText := make([]byte, len(ciphertext)) @@ -284,12 +284,12 @@ func (acp *AesCbcParams) Decrypt(ciphertext []byte, key CryptoKey) ([]byte, erro // 3. p := paddedPlainText[len(paddedPlainText)-1] if p == 0 || p > aes.BlockSize { - return nil, NewError(0, OperationError, "invalid padding") + return nil, NewError(OperationError, "invalid padding") } // 4. if !bytes.HasSuffix(paddedPlainText, bytes.Repeat([]byte{p}, int(p))) { - return nil, NewError(0, OperationError, "invalid padding") + return nil, NewError(OperationError, "invalid padding") } // 5. @@ -336,23 +336,23 @@ func (acp *AesCtrParams) Encrypt(plaintext []byte, key CryptoKey) ([]byte, error // 1. // Note that aes.BlockSize stands for the `k` variable as per the specification. if len(acp.Counter) != aes.BlockSize { - return nil, NewError(0, OperationError, "counter length is not 16 bytes") + return nil, NewError(OperationError, "counter length is not 16 bytes") } // 2. if acp.Length <= 0 || acp.Length > 128 { - return nil, NewError(0, OperationError, "invalid counter length, out of the 0 < x < 128 bounds") + return nil, NewError(OperationError, "invalid counter length, out of the 0 < x < 128 bounds") } keyHandle, ok := key.handle.([]byte) if !ok { - return nil, NewError(0, ImplementationError, "could not get key handle") + return nil, NewError(ImplementationError, "could not get key handle") } // 3. block, err := aes.NewCipher(keyHandle) if err != nil { - return nil, NewError(0, OperationError, "could not create cipher") + return nil, NewError(OperationError, "could not create cipher") } ciphertext := make([]byte, len(plaintext)) @@ -369,23 +369,23 @@ func (acp *AesCtrParams) Encrypt(plaintext []byte, key CryptoKey) ([]byte, error func (acp *AesCtrParams) Decrypt(ciphertext []byte, key CryptoKey) ([]byte, error) { // 1. if len(acp.Counter) != aes.BlockSize { - return nil, NewError(0, OperationError, "counter length is invalid, should be 16 bytes") + return nil, NewError(OperationError, "counter length is invalid, should be 16 bytes") } // 2. if acp.Length <= 0 || acp.Length > 128 { - return nil, NewError(0, OperationError, "invalid length, should be within 1 <= length <= 128 bounds") + return nil, NewError(OperationError, "invalid length, should be within 1 <= length <= 128 bounds") } keyHandle, ok := key.handle.([]byte) if !ok { - return nil, NewError(0, OperationError, "invalid key handle") + return nil, NewError(OperationError, "invalid key handle") } // 3. block, err := aes.NewCipher(keyHandle) if err != nil { - return nil, NewError(0, OperationError, "could not create AES cipher") + return nil, NewError(OperationError, "could not create AES cipher") } plaintext := make([]byte, len(ciphertext)) @@ -455,7 +455,7 @@ func (agp *AesGcmParams) Encrypt(plaintext []byte, key CryptoKey) ([]byte, error // As described in section 8 of AES-GCM [NIST SP800-38D]. // [NIST SP800-38D] https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf if len(plaintext) > maxAesGcmPlaintextLength { - return nil, NewError(0, OperationError, "plaintext length is too long") + return nil, NewError(OperationError, "plaintext length is too long") } // 2. @@ -466,14 +466,14 @@ func (agp *AesGcmParams) Encrypt(plaintext []byte, key CryptoKey) ([]byte, error // but go only supports 12 bytes IVs. We therefore are diverging from the // spec here, and have adjusted the test suite accordingly. if len(agp.Iv) != 12 { - return nil, NewError(0, NotSupportedError, "only 12 bytes long iv are supported") + return nil, NewError(NotSupportedError, "only 12 bytes long iv are supported") } // 3. // As described in section 8 of AES-GCM [NIST SP800-38D]. // [NIST SP800-38D] https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf if agp.AdditionalData != nil && (uint64(len(agp.AdditionalData)) > maxAesGcmAdditionalDataLength) { - return nil, NewError(0, OperationError, "additional data length is too long") + return nil, NewError(OperationError, "additional data length is too long") } // 4. @@ -486,32 +486,32 @@ func (agp *AesGcmParams) Encrypt(plaintext []byte, key CryptoKey) ([]byte, error tagLength = agp.TagLength case 32, 64: // Go's GCM implementation does not support 32 or 64 bit tag lengths. - return nil, NewError(0, NotSupportedError, "tag length 32 and 64 are not supported") + return nil, NewError(NotSupportedError, "tag length 32 and 64 are not supported") default: - return nil, NewError(0, OperationError, "invalid tag length, should be one of 96, 104, 112, 120, 128") + return nil, NewError(OperationError, "invalid tag length, should be one of 96, 104, 112, 120, 128") } } keyHandle, ok := key.handle.([]byte) if !ok { - return nil, NewError(0, ImplementationError, "could not get key data") + return nil, NewError(ImplementationError, "could not get key data") } // 6. block, err := aes.NewCipher(keyHandle) if err != nil { - return nil, NewError(0, OperationError, "could not create cipher") + return nil, NewError(OperationError, "could not create cipher") } gcm, err := cipher.NewGCMWithTagSize(block, tagLength/8) if err != nil { - return nil, NewError(0, ImplementationError, "could not create cipher") + return nil, NewError(ImplementationError, "could not create cipher") } // The Golang AES GCM cipher only supports a Nonce/Iv length of 12 bytes, // as opposed to the looser requirements of the Web Crypto API spec. if len(agp.Iv) != gcm.NonceSize() { - return nil, NewError(0, NotSupportedError, "only 12 bytes long iv are supported") + return nil, NewError(NotSupportedError, "only 12 bytes long iv are supported") } // 7. 8. @@ -535,9 +535,9 @@ func (agp *AesGcmParams) Decrypt(ciphertext []byte, key CryptoKey) ([]byte, erro tagLength = agp.TagLength case 32, 64: // Go's AES GCM implementation does not support 32 or 64 bit tag lengths. - return nil, NewError(0, OperationError, "invalid tag length, should be within 96 <= length <= 128 bounds") + return nil, NewError(OperationError, "invalid tag length, should be within 96 <= length <= 128 bounds") default: - return nil, NewError(0, OperationError, "invalid tag length, accepted values are 96, 104, 112, 120, 128") + return nil, NewError(OperationError, "invalid tag length, accepted values are 96, 104, 112, 120, 128") } } @@ -545,41 +545,41 @@ func (agp *AesGcmParams) Decrypt(ciphertext []byte, key CryptoKey) ([]byte, erro // Note that we multiply the length of the ciphertext by 8, in order // to get the length in bits. if len(ciphertext)*8 < tagLength { - return nil, NewError(0, OperationError, "ciphertext is too short") + return nil, NewError(OperationError, "ciphertext is too short") } // 3. if len(agp.Iv) < 1 || uint64(len(agp.Iv)) > maxAesGcmIvLength { - return nil, NewError(0, OperationError, "iv length is too long") + return nil, NewError(OperationError, "iv length is too long") } // 4. if agp.AdditionalData != nil && uint64(len(agp.AdditionalData)) > maxAesGcmAdditionalDataLength { - return nil, NewError(0, OperationError, "additional data is too long") + return nil, NewError(OperationError, "additional data is too long") } // 5. 6. are not necessary as Go's AES GCM implementation perform those steps for us keyHandle, ok := key.handle.([]byte) if !ok { - return nil, NewError(0, OperationError, "invalid key handle") + return nil, NewError(OperationError, "invalid key handle") } // 7. 8. block, err := aes.NewCipher(keyHandle) if err != nil { - return nil, NewError(0, OperationError, "could not create AES cipher") + return nil, NewError(OperationError, "could not create AES cipher") } gcm, err := cipher.NewGCMWithTagSize(block, tagLength/8) if err != nil { - return nil, NewError(0, OperationError, "could not create GCM cipher") + return nil, NewError(OperationError, "could not create GCM cipher") } // The Golang AES GCM cipher only supports a Nonce/Iv length of 12 bytes, plaintext, err := gcm.Open(nil, agp.Iv, ciphertext, agp.AdditionalData) if err != nil { - return nil, NewError(0, OperationError, "could not decrypt ciphertext") + return nil, NewError(OperationError, "could not decrypt ciphertext") } return plaintext, nil diff --git a/webcrypto/algorithm.go b/webcrypto/algorithm.go index 741dbca..a5b0ddc 100644 --- a/webcrypto/algorithm.go +++ b/webcrypto/algorithm.go @@ -130,19 +130,19 @@ func normalizeAlgorithm(rt *goja.Runtime, v goja.Value, op AlgorithmIdentifier) if v.ExportType().Kind() == reflect.String { algorithmString, ok := v.Export().(string) if !ok { - return Algorithm{}, NewError(0, ImplementationError, "algorithm cannot be interpreted as a string") + return Algorithm{}, NewError(ImplementationError, "algorithm cannot be interpreted as a string") } algorithmObject := rt.NewObject() if err := algorithmObject.Set("name", algorithmString); err != nil { - return Algorithm{}, NewError(0, ImplementationError, "unable to transform algorithm string into an object") + return Algorithm{}, NewError(ImplementationError, "unable to transform algorithm string into an object") } return normalizeAlgorithm(rt, algorithmObject, op) } if err := rt.ExportTo(v, &algorithm); err != nil { - return Algorithm{}, NewError(0, SyntaxError, "algorithm cannot be interpreted as a string or an object") + return Algorithm{}, NewError(SyntaxError, "algorithm cannot be interpreted as a string or an object") } // Algorithm identifers are always upper cased. @@ -151,7 +151,7 @@ func normalizeAlgorithm(rt *goja.Runtime, v goja.Value, op AlgorithmIdentifier) algorithm.Name = strings.ToUpper(algorithm.Name) if !isRegisteredAlgorithm(algorithm.Name, op) { - return Algorithm{}, NewError(0, NotSupportedError, "unsupported algorithm: "+algorithm.Name) + return Algorithm{}, NewError(NotSupportedError, "unsupported algorithm: "+algorithm.Name) } return algorithm, nil diff --git a/webcrypto/crypto.go b/webcrypto/crypto.go index 1ba7b8f..e662914 100644 --- a/webcrypto/crypto.go +++ b/webcrypto/crypto.go @@ -48,7 +48,7 @@ func (c *Crypto) GetRandomValues(typedArray goja.Value) goja.Value { // 1. if !IsInstanceOf(c.vu.Runtime(), typedArray, acceptedTypes...) { - common.Throw(c.vu.Runtime(), NewError(0, TypeMismatchError, "typedArray parameter isn't a TypedArray instance")) + common.Throw(c.vu.Runtime(), NewError(TypeMismatchError, "typedArray parameter isn't a TypedArray instance")) } // 2. @@ -58,16 +58,13 @@ func (c *Crypto) GetRandomValues(typedArray goja.Value) goja.Value { obj := typedArray.ToObject(c.vu.Runtime()) objLength, ok := obj.Get("length").ToNumber().Export().(int64) if !ok { - common.Throw(c.vu.Runtime(), NewError(0, TypeMismatchError, "typedArray parameter isn't a TypedArray instance")) + common.Throw(c.vu.Runtime(), NewError(TypeMismatchError, "typedArray parameter isn't a TypedArray instance")) } if objLength > maxRandomValuesLength { - // TODO: ideally we would prefer this to be an error that can be - // matched upon using something along the lines of `err isinstanceof QuotaExceededError`. common.Throw( c.vu.Runtime(), NewError( - 0, QuotaExceededError, fmt.Sprintf("typedArray parameter is too big; maximum length is %d", maxRandomValuesLength), ), diff --git a/webcrypto/encryption.go b/webcrypto/encryption.go index 8e60ca1..96323f8 100644 --- a/webcrypto/encryption.go +++ b/webcrypto/encryption.go @@ -47,12 +47,12 @@ func newEncryptDecrypter( ed = new(AesGcmParams) paramsObjectName = "AesGcmParams" default: - return nil, NewError(0, NotSupportedError, "unsupported algorithm") + return nil, NewError(NotSupportedError, "unsupported algorithm") } if err = rt.ExportTo(params, ed); err != nil { errMsg := fmt.Sprintf("invalid algorithm parameters, unable to interpret as %sParams object", paramsObjectName) - return nil, NewError(0, SyntaxError, errMsg) + return nil, NewError(SyntaxError, errMsg) } return ed, nil diff --git a/webcrypto/errors.go b/webcrypto/errors.go index 15844f6..5fb859f 100644 --- a/webcrypto/errors.go +++ b/webcrypto/errors.go @@ -44,9 +44,6 @@ const ( // Error represents a custom error emitted by the // Web Crypto API. type Error struct { - // Code is one of the legacy error code constants, or 0 if none match. - Code int `json:"code"` - // Name contains one of the strings associated with an error name. Name string `json:"name"` @@ -60,9 +57,8 @@ func (e *Error) Error() string { } // NewError returns a new WebCryptoError with the given name and message. -func NewError(code int, name, message string) *Error { +func NewError(name, message string) *Error { return &Error{ - Code: code, Name: name, Message: message, } diff --git a/webcrypto/goja.go b/webcrypto/goja.go index 8418e46..52140b4 100644 --- a/webcrypto/goja.go +++ b/webcrypto/goja.go @@ -11,7 +11,7 @@ import ( // and returns a copy of the underlying byte slice. func exportArrayBuffer(rt *goja.Runtime, v goja.Value) ([]byte, error) { if isNullish(v) { - return nil, NewError(0, TypeError, "data is null or undefined") + return nil, NewError(TypeError, "data is null or undefined") } asObject := v.ToObject(rt) @@ -22,12 +22,12 @@ func exportArrayBuffer(rt *goja.Runtime, v goja.Value) ([]byte, error) { if IsTypedArray(rt, v) { ab, ok = asObject.Get("buffer").Export().(goja.ArrayBuffer) if !ok { - return nil, NewError(0, TypeError, "TypedArray.buffer is not an ArrayBuffer") + return nil, NewError(TypeError, "TypedArray.buffer is not an ArrayBuffer") } } else { ab, ok = asObject.Export().(goja.ArrayBuffer) if !ok { - return nil, NewError(0, OperationError, "data is neither an ArrayBuffer, nor a TypedArray nor DataView") + return nil, NewError(OperationError, "data is neither an ArrayBuffer, nor a TypedArray nor DataView") } } @@ -46,19 +46,18 @@ func exportArrayBuffer(rt *goja.Runtime, v goja.Value) ([]byte, error) { // at the end of the traversal. It assumes that all the traversed fields are Objects. func traverseObject(rt *goja.Runtime, src goja.Value, fields ...string) (goja.Value, error) { if isNullish(src) { - return nil, NewError(0, TypeError, "Object is null or undefined") + return nil, NewError(TypeError, "Object is null or undefined") } obj := src.ToObject(rt) if isNullish(obj) { - return nil, NewError(0, TypeError, "Object is null or undefined") + return nil, NewError(TypeError, "Object is null or undefined") } for idx, field := range fields { src = obj.Get(field) if isNullish(src) { return nil, NewError( - 0, TypeError, fmt.Sprintf("field %s is null or undefined", strings.Join(fields[:idx+1], ".")), ) @@ -67,7 +66,6 @@ func traverseObject(rt *goja.Runtime, src goja.Value, fields ...string) (goja.Va obj = src.ToObject(rt) if isNullish(obj) { return nil, NewError( - 0, TypeError, fmt.Sprintf("field %s is not an Object", strings.Join(fields[:idx+1], ".")), ) diff --git a/webcrypto/hmac.go b/webcrypto/hmac.go index 5c504c3..055ec4f 100644 --- a/webcrypto/hmac.go +++ b/webcrypto/hmac.go @@ -45,7 +45,7 @@ func newHmacKeyGenParams(rt *goja.Runtime, normalized Algorithm, params goja.Val // and throw an error if it's not present. hashValue, err := traverseObject(rt, params, "hash") if err != nil { - return nil, NewError(0, SyntaxError, "could not get hash from algorithm parameter") + return nil, NewError(SyntaxError, "could not get hash from algorithm parameter") } // Although the specification doesn't explicitly ask us to do so, we @@ -89,7 +89,7 @@ func (hkgp *HmacKeyGenParams) GenerateKey( case SignCryptoKeyUsage, VerifyCryptoKeyUsage: continue default: - return nil, NewError(0, SyntaxError, "invalid key usage: "+usage) + return nil, NewError(SyntaxError, "invalid key usage: "+usage) } } @@ -110,19 +110,19 @@ func (hkgp *HmacKeyGenParams) GenerateKey( default: // This case should never happen, as the normalization algorithm // should have thrown an error if the hash algorithm is invalid. - return nil, NewError(0, ImplementationError, "invalid hash algorithm: "+hkgp.Hash.Name) + return nil, NewError(ImplementationError, "invalid hash algorithm: "+hkgp.Hash.Name) } } if hkgp.Length.Int64 == 0 { - return nil, NewError(0, OperationError, "algorithm's length cannot be 0") + return nil, NewError(OperationError, "algorithm's length cannot be 0") } // 3. randomKey := make([]byte, hkgp.Length.Int64/8) if _, err := rand.Read(randomKey); err != nil { // 4. - return nil, NewError(0, OperationError, "failed to generate random key; reason: "+err.Error()) + return nil, NewError(OperationError, "failed to generate random key; reason: "+err.Error()) } // 5. @@ -169,13 +169,13 @@ type HmacKeyAlgorithm struct { func exportHmacKey(ck *CryptoKey, format KeyFormat) ([]byte, error) { // 1. if ck.handle == nil { - return nil, NewError(0, OperationError, "key data is not accesible") + return nil, NewError(OperationError, "key data is not accesible") } // 2. bits, ok := ck.handle.([]byte) if !ok { - return nil, NewError(0, OperationError, "key underlying data is not of the correct type") + return nil, NewError(OperationError, "key underlying data is not of the correct type") } // 4. @@ -184,7 +184,7 @@ func exportHmacKey(ck *CryptoKey, format KeyFormat) ([]byte, error) { return bits, nil default: // FIXME: note that we do not support JWK format, yet #37. - return nil, NewError(0, NotSupportedError, "unsupported key format "+format) + return nil, NewError(NotSupportedError, "unsupported key format "+format) } } @@ -192,7 +192,7 @@ func exportHmacKey(ck *CryptoKey, format KeyFormat) ([]byte, error) { func (hka *HmacKeyAlgorithm) HashFn() (func() hash.Hash, error) { hashFn, ok := getHashFn(hka.Hash.Name) if !ok { - return nil, NewError(0, NotSupportedError, fmt.Sprintf("unsupported key hash algorithm %q", hka.Hash.Name)) + return nil, NewError(NotSupportedError, fmt.Sprintf("unsupported key hash algorithm %q", hka.Hash.Name)) } return hashFn, nil @@ -224,7 +224,7 @@ func newHmacImportParams(rt *goja.Runtime, normalized Algorithm, params goja.Val // and throw an error if it's not present. hashValue, err := traverseObject(rt, params, "hash") if err != nil { - return nil, NewError(0, SyntaxError, "could not get hash from algorithm parameter") + return nil, NewError(SyntaxError, "could not get hash from algorithm parameter") } // Although the specification doesn't explicitly ask us to do so, we @@ -269,7 +269,7 @@ func (hip *HmacImportParams) ImportKey( case SignCryptoKeyUsage, VerifyCryptoKeyUsage: continue default: - return nil, NewError(0, SyntaxError, "invalid key usage: "+usage) + return nil, NewError(SyntaxError, "invalid key usage: "+usage) } } @@ -281,23 +281,23 @@ func (hip *HmacImportParams) ImportKey( case RawKeyFormat: hash = KeyAlgorithm{Algorithm{Name: hip.Hash.Name}} default: - return nil, NewError(0, NotSupportedError, "unsupported key format "+format) + return nil, NewError(NotSupportedError, "unsupported key format "+format) } // 5. 6. length := int64(len(keyData) * 8) if length == 0 { - return nil, NewError(0, DataError, "key length cannot be 0") + return nil, NewError(DataError, "key length cannot be 0") } // 7. if hip.Length.Valid { if hip.Length.Int64 > length { - return nil, NewError(0, DataError, "key length cannot be greater than the length of the imported data") + return nil, NewError(DataError, "key length cannot be greater than the length of the imported data") } if hip.Length.Int64 < length { - return nil, NewError(0, DataError, "key length cannot be less than the length of the imported data") + return nil, NewError(DataError, "key length cannot be less than the length of the imported data") } length = hip.Length.Int64 diff --git a/webcrypto/subtle_crypto.go b/webcrypto/subtle_crypto.go index 19faa7f..a4356ca 100644 --- a/webcrypto/subtle_crypto.go +++ b/webcrypto/subtle_crypto.go @@ -56,7 +56,7 @@ func (sc *SubtleCrypto) Encrypt(algorithm, key, data goja.Value) *goja.Promise { var ck CryptoKey if err = rt.ExportTo(key, &ck); err != nil { - reject(NewError(0, TypeError, "key argument does hold not a valid CryptoKey object")) + reject(NewError(TypeError, "key argument does hold not a valid CryptoKey object")) return promise } @@ -68,7 +68,7 @@ func (sc *SubtleCrypto) Encrypt(algorithm, key, data goja.Value) *goja.Promise { // 8. if normalized.Name != keyAlgorithmNameValue.String() { - reject(NewError(0, InvalidAccessError, "algorithm name does not match key algorithm name")) + reject(NewError(InvalidAccessError, "algorithm name does not match key algorithm name")) return promise } @@ -81,7 +81,7 @@ func (sc *SubtleCrypto) Encrypt(algorithm, key, data goja.Value) *goja.Promise { go func() { // 9. if !ck.ContainsUsage(EncryptCryptoKeyUsage) { - reject(NewError(0, InvalidAccessError, "key does not contain the 'encrypt' usage")) + reject(NewError(InvalidAccessError, "key does not contain the 'encrypt' usage")) return } @@ -99,7 +99,7 @@ func (sc *SubtleCrypto) Encrypt(algorithm, key, data goja.Value) *goja.Promise { resolve(rt.NewArrayBuffer(ciphertext)) return default: - reject(NewError(0, NotSupportedError, fmt.Sprintf("unsupported algorithm %q", normalized.Name))) + reject(NewError(NotSupportedError, fmt.Sprintf("unsupported algorithm %q", normalized.Name))) return } }() @@ -148,7 +148,7 @@ func (sc *SubtleCrypto) Decrypt(algorithm, key, data goja.Value) *goja.Promise { var ck CryptoKey if err = rt.ExportTo(key, &ck); err != nil { - reject(NewError(0, InvalidAccessError, "key argument does hold not a valid CryptoKey object")) + reject(NewError(InvalidAccessError, "key argument does hold not a valid CryptoKey object")) return promise } @@ -160,7 +160,7 @@ func (sc *SubtleCrypto) Decrypt(algorithm, key, data goja.Value) *goja.Promise { // 8. if normalized.Name != keyAlgorithmNameValue.String() { - reject(NewError(0, InvalidAccessError, "algorithm name does not match key algorithm name")) + reject(NewError(InvalidAccessError, "algorithm name does not match key algorithm name")) return promise } @@ -173,7 +173,7 @@ func (sc *SubtleCrypto) Decrypt(algorithm, key, data goja.Value) *goja.Promise { go func() { // 9. if !ck.ContainsUsage(DecryptCryptoKeyUsage) { - reject(NewError(0, InvalidAccessError, "key does not contain the 'decrypt' usage")) + reject(NewError(InvalidAccessError, "key does not contain the 'decrypt' usage")) return } @@ -190,7 +190,7 @@ func (sc *SubtleCrypto) Decrypt(algorithm, key, data goja.Value) *goja.Promise { resolve(rt.NewArrayBuffer(plaintext)) default: - reject(NewError(0, NotSupportedError, fmt.Sprintf("unsupported algorithm %q", normalized.Name))) + reject(NewError(NotSupportedError, fmt.Sprintf("unsupported algorithm %q", normalized.Name))) } }() @@ -238,7 +238,7 @@ func (sc *SubtleCrypto) Sign(algorithm, key, data goja.Value) *goja.Promise { var ck CryptoKey if err = rt.ExportTo(key, &ck); err != nil { - reject(NewError(0, InvalidAccessError, "key argument does hold not a valid CryptoKey object")) + reject(NewError(InvalidAccessError, "key argument does hold not a valid CryptoKey object")) return promise } @@ -251,13 +251,13 @@ func (sc *SubtleCrypto) Sign(algorithm, key, data goja.Value) *goja.Promise { go func() { // 8. if normalized.Name != keyAlgorithmNameValue.String() { - reject(NewError(0, InvalidAccessError, "algorithm name does not match key algorithm name")) + reject(NewError(InvalidAccessError, "algorithm name does not match key algorithm name")) return } // 9. for !ck.ContainsUsage(SignCryptoKeyUsage) { - reject(NewError(0, InvalidAccessError, "key does not contain the 'sign' usage")) + reject(NewError(InvalidAccessError, "key does not contain the 'sign' usage")) return } @@ -266,13 +266,13 @@ func (sc *SubtleCrypto) Sign(algorithm, key, data goja.Value) *goja.Promise { case HMAC: keyAlgorithm, ok := ck.Algorithm.(HmacKeyAlgorithm) if !ok { - reject(NewError(0, InvalidAccessError, "key algorithm does not describe a HMAC key")) + reject(NewError(InvalidAccessError, "key algorithm does not describe a HMAC key")) return } keyHandle, ok := ck.handle.([]byte) if !ok { - reject(NewError(0, InvalidAccessError, "key handle is of incorrect type")) + reject(NewError(InvalidAccessError, "key handle is of incorrect type")) return } @@ -290,7 +290,7 @@ func (sc *SubtleCrypto) Sign(algorithm, key, data goja.Value) *goja.Promise { resolve(rt.NewArrayBuffer(mac)) default: - reject(NewError(0, NotSupportedError, fmt.Sprintf("unsupported algorithm %q", normalized.Name))) + reject(NewError(NotSupportedError, fmt.Sprintf("unsupported algorithm %q", normalized.Name))) } }() @@ -347,7 +347,7 @@ func (sc *SubtleCrypto) Verify(algorithm, key, signature, data goja.Value) *goja var ck CryptoKey if err = rt.ExportTo(key, &ck); err != nil { - reject(NewError(0, InvalidAccessError, "key argument does hold not a valid CryptoKey object")) + reject(NewError(InvalidAccessError, "key argument does hold not a valid CryptoKey object")) return promise } @@ -360,13 +360,13 @@ func (sc *SubtleCrypto) Verify(algorithm, key, signature, data goja.Value) *goja go func() { // 9. if normalizedAlgorithm.Name != keyAlgorithmNameValue.String() { - reject(NewError(0, InvalidAccessError, "algorithm name does not match key algorithm name")) + reject(NewError(InvalidAccessError, "algorithm name does not match key algorithm name")) return } // 10. for !ck.ContainsUsage(VerifyCryptoKeyUsage) { - reject(NewError(0, InvalidAccessError, "key does not contain the 'sign' usage")) + reject(NewError(InvalidAccessError, "key does not contain the 'sign' usage")) return } @@ -374,13 +374,13 @@ func (sc *SubtleCrypto) Verify(algorithm, key, signature, data goja.Value) *goja case HMAC: keyAlgorithm, ok := ck.Algorithm.(HmacKeyAlgorithm) if !ok { - reject(NewError(0, InvalidAccessError, "key algorithm does not describe a HMAC key")) + reject(NewError(InvalidAccessError, "key algorithm does not describe a HMAC key")) return } keyHandle, ok := ck.handle.([]byte) if !ok { - reject(NewError(0, InvalidAccessError, "key handle is of incorrect type")) + reject(NewError(InvalidAccessError, "key handle is of incorrect type")) return } @@ -395,7 +395,7 @@ func (sc *SubtleCrypto) Verify(algorithm, key, signature, data goja.Value) *goja resolve(hmac.Equal(signatureData, hasher.Sum(nil))) default: - reject(NewError(0, NotSupportedError, fmt.Sprintf("unsupported algorithm %q", normalizedAlgorithm.Name))) + reject(NewError(NotSupportedError, fmt.Sprintf("unsupported algorithm %q", normalizedAlgorithm.Name))) } }() @@ -453,7 +453,7 @@ func (sc *SubtleCrypto) Digest(algorithm goja.Value, data goja.Value) *goja.Prom hashFn, ok := getHashFn(normalized.Name) if !ok { // 7. - reject(NewError(0, NotSupportedError, "unsupported algorithm: "+normalized.Name)) + reject(NewError(NotSupportedError, "unsupported algorithm: "+normalized.Name)) return } @@ -515,7 +515,7 @@ func (sc *SubtleCrypto) GenerateKey(algorithm goja.Value, extractable bool, keyU isPrivateKey := result.Type == PrivateCryptoKeyType isUsagesEmpty := len(result.Usages) == 0 if (isSecretKey || isPrivateKey) && isUsagesEmpty { - reject(NewError(0, SyntaxError, "usages cannot not be empty for a secret or private CryptoKey")) + reject(NewError(SyntaxError, "usages cannot not be empty for a secret or private CryptoKey")) return } @@ -664,7 +664,7 @@ func (sc *SubtleCrypto) ImportKey( isPrivateKey := result.Type == PrivateCryptoKeyType isUsagesEmpty := len(keyUsages) == 0 if (isSecretKey || isPrivateKey) && isUsagesEmpty { - reject(NewError(0, SyntaxError, "usages cannot not be empty for a secret or private CryptoKey")) + reject(NewError(SyntaxError, "usages cannot not be empty for a secret or private CryptoKey")) return } @@ -704,32 +704,32 @@ func (sc *SubtleCrypto) ExportKey(format KeyFormat, key goja.Value) *goja.Promis var algorithm Algorithm algValue := key.ToObject(rt).Get("algorithm") if err := rt.ExportTo(algValue, &algorithm); err != nil { - reject(NewError(0, SyntaxError, "key is not a valid CryptoKey")) + reject(NewError(SyntaxError, "key is not a valid CryptoKey")) return promise } ck, ok := key.Export().(*CryptoKey) if !ok { - reject(NewError(0, ImplementationError, "unable to extract CryptoKey")) + reject(NewError(ImplementationError, "unable to extract CryptoKey")) return promise } keyAlgorithmName := key.ToObject(rt).Get("algorithm").ToObject(rt).Get("name").String() if algorithm.Name != keyAlgorithmName { - reject(NewError(0, InvalidAccessError, "algorithm name does not match key algorithm name")) + reject(NewError(InvalidAccessError, "algorithm name does not match key algorithm name")) return promise } go func() { // 5. if !isRegisteredAlgorithm(algorithm.Name, OperationIdentifierExportKey) { - reject(NewError(0, NotSupportedError, "unsupported algorithm "+algorithm.Name)) + reject(NewError(NotSupportedError, "unsupported algorithm "+algorithm.Name)) return } // 6. if !ck.Extractable { - reject(NewError(0, InvalidAccessError, "the key is not extractable")) + reject(NewError(InvalidAccessError, "the key is not extractable")) return } @@ -750,7 +750,7 @@ func (sc *SubtleCrypto) ExportKey(format KeyFormat, key goja.Value) *goja.Promis return } default: - reject(NewError(0, NotSupportedError, "unsupported algorithm "+keyAlgorithmName)) + reject(NewError(NotSupportedError, "unsupported algorithm "+keyAlgorithmName)) return }