Skip to content

Commit

Permalink
crypto: add separate error for INVALID_KEY_TYPE
Browse files Browse the repository at this point in the history
PR-URL: #37555
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
RaisinTen authored and danielleadams committed Mar 16, 2021
1 parent 05badcf commit 33180fa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
43 changes: 25 additions & 18 deletions src/crypto/crypto_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,31 @@ class CipherJob final : public CryptoJob<CipherTraits> {
WebCryptoCipherMode cipher_mode() const { return cipher_mode_; }

void DoThreadPoolWork() override {
switch (CipherTraits::DoCipher(
AsyncWrap::env(),
key(),
cipher_mode_,
*CryptoJob<CipherTraits>::params(),
in_,
&out_)) {
case WebCryptoCipherStatus::OK:
// Success!
break;
case WebCryptoCipherStatus::INVALID_KEY_TYPE:
// Fall through
// TODO(@jasnell): Separate error for this
case WebCryptoCipherStatus::FAILED: {
CryptoErrorVector* errors = CryptoJob<CipherTraits>::errors();
errors->Capture();
if (errors->empty())
errors->push_back(std::string("Cipher job failed."));
const WebCryptoCipherStatus status =
CipherTraits::DoCipher(
AsyncWrap::env(),
key(),
cipher_mode_,
*CryptoJob<CipherTraits>::params(),
in_,
&out_);
if (status == WebCryptoCipherStatus::OK) {
// Success!
return;
}
CryptoErrorVector* errors = CryptoJob<CipherTraits>::errors();
errors->Capture();
if (errors->empty()) {
switch (status) {
case WebCryptoCipherStatus::OK:
UNREACHABLE();
break;
case WebCryptoCipherStatus::INVALID_KEY_TYPE:
errors->emplace_back("Invalid key type.");
break;
case WebCryptoCipherStatus::FAILED:
errors->emplace_back("Cipher job failed.");
break;
}
}
}
Expand Down
39 changes: 23 additions & 16 deletions src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,29 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
WebCryptoKeyFormat format() const { return format_; }

void DoThreadPoolWork() override {
switch (KeyExportTraits::DoExport(
key_,
format_,
*CryptoJob<KeyExportTraits>::params(),
&out_)) {
case WebCryptoKeyExportStatus::OK:
// Success!
break;
case WebCryptoKeyExportStatus::INVALID_KEY_TYPE:
// Fall through
// TODO(@jasnell): Separate error for this
case WebCryptoKeyExportStatus::FAILED: {
CryptoErrorVector* errors = CryptoJob<KeyExportTraits>::errors();
errors->Capture();
if (errors->empty())
errors->push_back("Key export failed.");
const WebCryptoKeyExportStatus status =
KeyExportTraits::DoExport(
key_,
format_,
*CryptoJob<KeyExportTraits>::params(),
&out_);
if (status == WebCryptoKeyExportStatus::OK) {
// Success!
return;
}
CryptoErrorVector* errors = CryptoJob<KeyExportTraits>::errors();
errors->Capture();
if (errors->empty()) {
switch (status) {
case WebCryptoKeyExportStatus::OK:
UNREACHABLE();
break;
case WebCryptoKeyExportStatus::INVALID_KEY_TYPE:
errors->emplace_back("Invalid key type.");
break;
case WebCryptoKeyExportStatus::FAILED:
errors->emplace_back("Cipher job failed.");
break;
}
}
}
Expand Down

0 comments on commit 33180fa

Please sign in to comment.