Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying usages when importing public ECDH key, deriveBits/deriveKey refactor #415

Merged
merged 1 commit into from
Mar 3, 2023
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
28 changes: 15 additions & 13 deletions src/workerd/api/crypto-impl-asymmetric.c++
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,18 @@ ImportAsymmetricResult importAsymmetric(kj::StringPtr format,
JSG_REQUIRE(keyDataJwk.oth == nullptr, DOMNotSupportedError,
"Multi-prime private keys not supported.");
} else {
// Public key.
// Public key. If the given key is an ECDH key allow usages to contain deriveBits and
// deriveKey. While these usages appear to not be needed in the public key for the
// operations, users may be confused when they can't import a public key to be used alongside
// a private key for derive*() operations when such a usage is specified.
// The derive* functions check that baseKey is a private key, so imported public keys can't
// be mistakenly used as private keys regardless of the usages field.
keyType = "public";
usages =
CryptoKeyUsageSet::validate(normalizedName, CryptoKeyUsageSet::Context::importPublic,
keyUsages, allowedUsages & CryptoKeyUsageSet::publicKeyMask());
keyUsages, allowedUsages & (normalizedName == "ECDH" ?
CryptoKeyUsageSet::derivationKeyMask() :
CryptoKeyUsageSet::publicKeyMask()));
}

if (keyUsages.size() > 0) {
Expand Down Expand Up @@ -332,7 +339,9 @@ ImportAsymmetricResult importAsymmetric(kj::StringPtr format,
}
usages =
CryptoKeyUsageSet::validate(normalizedName, CryptoKeyUsageSet::Context::importPublic,
keyUsages, allowedUsages & CryptoKeyUsageSet::publicKeyMask());
keyUsages, allowedUsages & (normalizedName == "ECDH" ?
CryptoKeyUsageSet::derivationKeyMask() :
CryptoKeyUsageSet::publicKeyMask()));
return { kj::mv(evpPkey), "public"_kj, usages };
} else if (format == "pkcs8") {
kj::ArrayPtr<const kj::byte> keyBytes = JSG_REQUIRE_NONNULL(
Expand Down Expand Up @@ -1549,13 +1558,6 @@ ImportAsymmetricResult importEllipticRaw(SubtleCrypto::ImportKeyData keyData, in

const auto& raw = keyData.get<kj::Array<kj::byte>>();

if (normalizedName == "ECDH"_kj) {
// ECDH publicKeys only support deriveBits and deriveKey.
KJ_ASSERT(allowedUsages <= (CryptoKeyUsageSet::deriveBits() |
CryptoKeyUsageSet::deriveKey()));
} else {
KJ_ASSERT(allowedUsages <= CryptoKeyUsageSet::publicKeyMask());
}
auto usages = CryptoKeyUsageSet::validate(normalizedName,
CryptoKeyUsageSet::Context::importPublic, keyUsages, allowedUsages);
// TODO(revisit once this is standardized): NodeJS appears to support importing raw for private
Expand Down Expand Up @@ -1659,7 +1661,7 @@ kj::OneOf<jsg::Ref<CryptoKey>, CryptoKeyPair> CryptoKey::Impl::generateEcdh(
kj::ArrayPtr<const kj::String> keyUsages) {
auto usages =
CryptoKeyUsageSet::validate(normalizedName, CryptoKeyUsageSet::Context::generate, keyUsages,
CryptoKeyUsageSet::deriveKey() | CryptoKeyUsageSet::deriveBits());
CryptoKeyUsageSet::derivationKeyMask());
return EllipticKey::generateElliptic(normalizedName, kj::mv(algorithm), extractable, usages, {});
}

Expand All @@ -1680,10 +1682,10 @@ kj::Own<CryptoKey::Impl> CryptoKey::Impl::importEcdh(
// Verbose lambda capture needed because: https://bugs.llvm.org/show_bug.cgi?id=35984
[curveId = curveId](SubtleCrypto::JsonWebKey keyDataJwk) -> kj::Own<EVP_PKEY> {
return ellipticJwkReader(curveId, kj::mv(keyDataJwk));
}, CryptoKeyUsageSet::deriveKey() | CryptoKeyUsageSet::deriveBits());
}, CryptoKeyUsageSet::derivationKeyMask());
} else {
return importEllipticRaw(kj::mv(keyData), curveId, normalizedName, keyUsages,
CryptoKeyUsageSet::deriveKey() | CryptoKeyUsageSet::deriveBits());
CryptoKeyUsageSet::derivationKeyMask());
}
}();

Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/crypto-impl-hkdf.c++
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ kj::Own<CryptoKey::Impl> CryptoKey::Impl::importHkdf(
kj::ArrayPtr<const kj::String> keyUsages) {
auto usages =
CryptoKeyUsageSet::validate(normalizedName, CryptoKeyUsageSet::Context::importSecret,
keyUsages, CryptoKeyUsageSet::deriveBits() | CryptoKeyUsageSet::deriveKey());
keyUsages, CryptoKeyUsageSet::derivationKeyMask());

JSG_REQUIRE(!extractable, DOMSyntaxError, "HKDF key cannot be extractable.");
JSG_REQUIRE(format == "raw", DOMNotSupportedError, "HKDF key must be imported "
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/crypto-impl-pbkdf2.c++
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ kj::Own<CryptoKey::Impl> CryptoKey::Impl::importPbkdf2(
kj::ArrayPtr<const kj::String> keyUsages) {
auto usages =
CryptoKeyUsageSet::validate(normalizedName, CryptoKeyUsageSet::Context::importSecret,
keyUsages, CryptoKeyUsageSet::deriveKey() | CryptoKeyUsageSet::deriveBits());
keyUsages, CryptoKeyUsageSet::derivationKeyMask());

JSG_REQUIRE(!extractable, DOMSyntaxError, "PBKDF2 key cannot be extractable.");
JSG_REQUIRE(format == "raw", DOMNotSupportedError,
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/api/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class CryptoKeyUsageSet {
return decrypt() | sign() | unwrapKey() | deriveKey() | deriveBits();
}

static constexpr CryptoKeyUsageSet derivationKeyMask() {
return deriveKey() | deriveBits();
}

CryptoKeyUsageSet() : set(0) {}

CryptoKeyUsageSet operator&(CryptoKeyUsageSet other) const { return set & other.set; }
Expand Down