Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/workerd/api/crypto/crypto.c++
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,7 @@ jsg::Promise<jsg::JsRef<jsg::JsArrayBuffer>> SubtleCrypto::wrapKey(jsg::Lock& js
size_t minimumLength;
};

JSG_REQUIRE(key.getExtractable(), DOMInvalidAccessError, "Attempt to export non-extractable ",
key.getAlgorithmName(), " key.");
key.impl->requireExportable();

auto exportedKey = key.impl->exportKey(js, kj::mv(format));

Expand Down Expand Up @@ -1027,10 +1026,7 @@ jsg::Promise<SubtleCrypto::ExportKeyData> SubtleCrypto::exportKey(

return js.evalNow([&] {
requireModernKeyFormat(js, format);
// TODO(someday): Throw a NotSupportedError? The Web Crypto API spec says InvalidAccessError,
// but Web IDL says that's deprecated.
JSG_REQUIRE(key.getExtractable(), DOMInvalidAccessError, "Attempt to export non-extractable ",
key.getAlgorithmName(), " key.");
key.impl->requireExportable();

return key.impl->exportKey(js, format);
});
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/api/crypto/hkdf.c++
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class HkdfKey final: public CryptoKey::Impl {
DOMOperationError, "HKDF deriveBits failed.");
}

bool supportsExport() const override {
return false;
}

kj::StringPtr getAlgorithmName() const override {
return "HKDF";
}
Expand Down
9 changes: 9 additions & 0 deletions src/workerd/api/crypto/impl.c++
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ kj::EncodingResult<kj::Array<kj::byte>> decodeBase64Url(kj::String text) {
return kj::decodeBase64(text);
}

void CryptoKey::Impl::requireExportable() const {
JSG_REQUIRE(supportsExport(), DOMNotSupportedError, "Unrecognized or unsupported export of \"",
getAlgorithmName(), "\" requested.");
// TODO(someday): Throw a NotSupportedError? The Web Crypto API spec says InvalidAccessError, but
// Web IDL says that's deprecated.
JSG_REQUIRE(isExtractable(), DOMInvalidAccessError, "Attempt to export non-extractable ",
getAlgorithmName(), " key.");
}

bool CryptoKey::Impl::equals(const kj::Array<kj::byte>&) const {
return false;
}
Expand Down
11 changes: 11 additions & 0 deletions src/workerd/api/crypto/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ class CryptoKey::Impl {
bool isExtractable() const {
return extractable;
}

// Throws the errors exportKey() and wrapKey() raise for a key they can't export, in the order
// the spec checks them: NotSupportedError, then InvalidAccessError.
void requireExportable() const;

CryptoKeyUsageSet getUsages() const {
return usages;
}
Expand Down Expand Up @@ -259,6 +264,12 @@ class CryptoKey::Impl {
return decrypt(js, kj::mv(algorithm), wrappedKey);
}

// Whether the algorithm supports the Web Crypto export key operation. Override it to return
// false in any Impl that doesn't override exportKey().
virtual bool supportsExport() const {
return true;
}

virtual SubtleCrypto::ExportKeyData exportKey(jsg::Lock& js, kj::StringPtr format) const {
JSG_FAIL_REQUIRE(DOMNotSupportedError, "Unrecognized or unsupported export of \"",
getAlgorithmName(), "\" requested.");
Expand Down
8 changes: 6 additions & 2 deletions src/workerd/api/crypto/pbkdf2.c++
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,18 @@ class Pbkdf2Key final: public CryptoKey::Impl {
// algorithms in exportKey(). Later, the allow list was removed, instead assuming that any
// algorithm which implemented this method must be allowed. To maintain exactly the
// preexisting behavior, then, this implementation had to be commented out. If disallowing this
// was a mistake, we can un-comment this method, but we would need to make sure to add tests
// when we do.
// was a mistake, we can un-comment this method and remove supportsExport() below, but we
// would need to make sure to add tests when we do.
// SubtleCrypto::ExportKeyData exportKey(kj::StringPtr format) const override {
// JSG_REQUIRE(format == "raw", DOMNotSupportedError,
// "Unimplemented key export format \"", format, "\".");
// return kj::heapArray(keyData.asPtr());
// }

bool supportsExport() const override {
return false;
}

kj::StringPtr getAlgorithmName() const override {
return "PBKDF2";
}
Expand Down
11 changes: 1 addition & 10 deletions src/wpt/WebCryptoAPI-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,7 @@ export default {
'encrypt_decrypt/rsa.js': supportFile,
'encrypt_decrypt/rsa_oaep.https.any.js': {},
'encrypt_decrypt/rsa_vectors.js': {},
'export-key-error-order.https.any.js': {
comment:
'exportKey() and wrapKey() check extractable before export support, so HKDF and PBKDF2 keys throw InvalidAccessError instead of NotSupportedError',
expectedFailures: [
'exportKey() with a HKDF key throws NotSupportedError',
'wrapKey() with a HKDF key throws NotSupportedError',
'exportKey() with a PBKDF2 key throws NotSupportedError',
'wrapKey() with a PBKDF2 key throws NotSupportedError',
],
},
'export-key-error-order.https.any.js': {},
'generateKey/algorithm_registry.js': {},
'generateKey/failures.js': {},
'generateKey/failures_AES-CBC.https.any.js': {},
Expand Down
Loading