1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

fix(ext/crypto): use DataError in importKey() (#12071)

This commit is contained in:
Divy Srivastava 2021-09-16 13:28:29 +05:30 committed by GitHub
parent 9270cad67c
commit 868f38d452
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 28 deletions

View file

@ -1007,8 +1007,10 @@ pub async fn op_crypto_import_key(
// 7. // 7.
if let Some(pk_hash) = pk_hash { if let Some(pk_hash) = pk_hash {
if pk_hash != hash { if pk_hash != hash {
// TODO(@littledivy): DataError return Err(custom_error(
return Err(type_error("Hash mismatch".to_string())); "DOMExceptionDataError",
"Hash mismatch".to_string(),
));
} }
} }
@ -1019,15 +1021,16 @@ pub async fn op_crypto_import_key(
)?; )?;
let bytes_consumed = private_key.encoded_len().map_err(|e| { let bytes_consumed = private_key.encoded_len().map_err(|e| {
// TODO(@littledivy): DataError custom_error("DOMExceptionDataError", e.to_string())
custom_error("DOMExceptionOperationError", e.to_string())
})?; })?;
if bytes_consumed if bytes_consumed
!= rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16) != rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16)
{ {
// TODO(@littledivy): DataError return Err(custom_error(
return Err(type_error("Some bytes were not consumed".to_string())); "DOMExceptionDataError",
"Some bytes were not consumed".to_string(),
));
} }
Ok(ImportKeyResult { Ok(ImportKeyResult {
@ -1083,9 +1086,9 @@ pub async fn op_crypto_import_key(
ID_SHA384_OID => Some(CryptoHash::Sha384), ID_SHA384_OID => Some(CryptoHash::Sha384),
// id-sha256 // id-sha256
ID_SHA512_OID => Some(CryptoHash::Sha512), ID_SHA512_OID => Some(CryptoHash::Sha512),
// TODO(@littledivy): DataError
_ => { _ => {
return Err(type_error( return Err(custom_error(
"DOMExceptionDataError",
"Unsupported hash algorithm".to_string(), "Unsupported hash algorithm".to_string(),
)) ))
} }
@ -1100,15 +1103,21 @@ pub async fn op_crypto_import_key(
hash hash
} }
// TODO(@littledivy): DataError _ => {
_ => return Err(type_error("Unsupported algorithm".to_string())), return Err(custom_error(
"DOMExceptionDataError",
"Unsupported algorithm".to_string(),
))
}
}; };
// 7. // 7.
if let Some(pk_hash) = pk_hash { if let Some(pk_hash) = pk_hash {
if pk_hash != hash { if pk_hash != hash {
// TODO(@littledivy): DataError return Err(custom_error(
return Err(type_error("Hash mismatch".to_string())); "DOMExceptionDataError",
"Hash mismatch".to_string(),
));
} }
} }
@ -1118,16 +1127,17 @@ pub async fn op_crypto_import_key(
|e| custom_error("DOMExceptionOperationError", e.to_string()), |e| custom_error("DOMExceptionOperationError", e.to_string()),
)?; )?;
let bytes_consumed = private_key.encoded_len().map_err(|e| { let bytes_consumed = private_key
// TODO(@littledivy): DataError .encoded_len()
custom_error("DOMExceptionOperationError", e.to_string()) .map_err(|e| custom_error("DataError", e.to_string()))?;
})?;
if bytes_consumed if bytes_consumed
!= rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16) != rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16)
{ {
// TODO(@littledivy): DataError return Err(custom_error(
return Err(type_error("Some bytes were not consumed".to_string())); "DOMExceptionDataError",
"Some bytes were not consumed".to_string(),
));
} }
Ok(ImportKeyResult { Ok(ImportKeyResult {
@ -1183,9 +1193,9 @@ pub async fn op_crypto_import_key(
ID_SHA384_OID => Some(CryptoHash::Sha384), ID_SHA384_OID => Some(CryptoHash::Sha384),
// id-sha256 // id-sha256
ID_SHA512_OID => Some(CryptoHash::Sha512), ID_SHA512_OID => Some(CryptoHash::Sha512),
// TODO(@littledivy): DataError
_ => { _ => {
return Err(type_error( return Err(custom_error(
"DOMExceptionDataError",
"Unsupported hash algorithm".to_string(), "Unsupported hash algorithm".to_string(),
)) ))
} }
@ -1200,15 +1210,21 @@ pub async fn op_crypto_import_key(
hash hash
} }
// TODO(@littledivy): DataError _ => {
_ => return Err(type_error("Unsupported algorithm".to_string())), return Err(custom_error(
"DOMExceptionDataError",
"Unsupported algorithm".to_string(),
))
}
}; };
// 7. // 7.
if let Some(pk_hash) = pk_hash { if let Some(pk_hash) = pk_hash {
if pk_hash != hash { if pk_hash != hash {
// TODO(@littledivy): DataError return Err(custom_error(
return Err(type_error("Hash mismatch".to_string())); "DOMExceptionDataError",
"Hash mismatch".to_string(),
));
} }
} }
@ -1219,15 +1235,16 @@ pub async fn op_crypto_import_key(
)?; )?;
let bytes_consumed = private_key.encoded_len().map_err(|e| { let bytes_consumed = private_key.encoded_len().map_err(|e| {
// TODO(@littledivy): DataError custom_error("DOMExceptionDataError", e.to_string())
custom_error("DOMExceptionOperationError", e.to_string())
})?; })?;
if bytes_consumed if bytes_consumed
!= rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16) != rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16)
{ {
// TODO(@littledivy): DataError return Err(custom_error(
return Err(type_error("Some bytes were not consumed".to_string())); "DOMExceptionDataError",
"Some bytes were not consumed".to_string(),
));
} }
Ok(ImportKeyResult { Ok(ImportKeyResult {

View file

@ -289,6 +289,12 @@ delete Object.prototype.__proto__;
return new domException.DOMException(msg, "InvalidCharacterError"); return new domException.DOMException(msg, "InvalidCharacterError");
}, },
); );
core.registerErrorBuilder(
"DOMExceptionDataError",
function DOMExceptionDataError(msg) {
return new domException.DOMException(msg, "DataError");
},
);
} }
class Navigator { class Navigator {