Skip to content

Commit

Permalink
refactor: Export key, more checks for nil, remove redundant check
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Apr 22, 2024
1 parent ae71f46 commit b51964d
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions webcrypto/subtle_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,23 +837,21 @@ func (sc *SubtleCrypto) ExportKey( //nolint:funlen // we have a lot of error han
)

err := func() error {
var algorithm Algorithm
algValue := key.ToObject(rt).Get("algorithm")
if err := rt.ExportTo(algValue, &algorithm); err != nil {
return NewError(SyntaxError, "key is not a valid CryptoKey")
keyObj := key.ToObject(rt)
if common.IsNullish(keyObj) {
return NewError(InvalidAccessError, "key is not an object")
}

var ok bool
ck, ok = key.Export().(*CryptoKey)
if !ok {
return NewError(ImplementationError, "unable to extract CryptoKey")
return NewError(ImplementationError, "unable to extract CryptoKey from key object")
}

inputAlgorithm := key.ToObject(rt).Get("algorithm").ToObject(rt)

keyAlgorithmName := inputAlgorithm.Get("name").String()
if algorithm.Name != keyAlgorithmName {
return NewError(InvalidAccessError, "algorithm name does not match key algorithm name")
var algorithm Algorithm
algObj := keyObj.Get("algorithm")
if err := rt.ExportTo(algObj, &algorithm); err != nil {
return NewError(SyntaxError, "key is not a valid Algorithm")
}

if !isRegisteredAlgorithm(algorithm.Name, OperationIdentifierExportKey) {
Expand All @@ -864,15 +862,15 @@ func (sc *SubtleCrypto) ExportKey( //nolint:funlen // we have a lot of error han
return NewError(InvalidAccessError, "the key is not extractable")
}

switch keyAlgorithmName {
switch algorithm.Name {
case AESCbc, AESCtr, AESGcm:
keyExporter = exportAESKey
case HMAC:
keyExporter = exportHMACKey
case ECDH, ECDSA:
keyExporter = exportECKey
default:
return NewError(NotSupportedError, "unsupported algorithm "+keyAlgorithmName)
return NewError(NotSupportedError, "unsupported algorithm "+algorithm.Name)
}

return nil
Expand Down

0 comments on commit b51964d

Please sign in to comment.