From 24a12a21c793153d199334a920b7009b5fde8849 Mon Sep 17 00:00:00 2001 From: Toni Peter Date: Mon, 4 Nov 2024 15:20:27 +0100 Subject: [PATCH] Move GeneralErrorType to InternalError::Storage --- rust/src/nasl/builtin/cryptographic/aes_ccm.rs | 8 +++----- rust/src/nasl/builtin/cryptographic/aes_gmac.rs | 16 +++++----------- rust/src/nasl/builtin/cryptographic/mod.rs | 4 ++++ rust/src/nasl/interpreter/error.rs | 5 ++++- rust/src/nasl/utils/error.rs | 11 +++++++---- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/rust/src/nasl/builtin/cryptographic/aes_ccm.rs b/rust/src/nasl/builtin/cryptographic/aes_ccm.rs index 1eb6fc718..846899ffd 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_ccm.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_ccm.rs @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception -use crate::nasl::utils::error::{GeneralErrorType, NaslError}; +use crate::nasl::utils::error::NaslError; use aes::cipher::{BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser}; use aes::{Aes128, Aes192, Aes256}; use ccm::{ @@ -17,7 +17,7 @@ use crate::nasl::utils::{Context, Register}; use crate::function_set; -use super::{get_aad, get_data, get_iv, get_key, get_len, Crypt}; +use super::{get_aad, get_data, get_iv, get_key, get_len, Crypt, CryptographicError}; /// Core function to en- and decrypt data. Throws error in case of failure. fn ccm_crypt( @@ -60,9 +60,7 @@ where // Error handling match res { Ok(x) => Ok(NaslValue::Data(x)), - Err(_) => Err(NaslError::GeneralError(GeneralErrorType::UnexpectedData( - "unable to en-/decrypt data".to_string(), - ))), + Err(_) => Err(CryptographicError::AesCcmUnableToEncrypt.into()), } } diff --git a/rust/src/nasl/builtin/cryptographic/aes_gmac.rs b/rust/src/nasl/builtin/cryptographic/aes_gmac.rs index 71d5022e3..5d9a9a39c 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_gmac.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_gmac.rs @@ -3,16 +3,15 @@ // SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception use crate::function_set; +#[cfg(feature = "nasl-c-lib")] +use crate::nasl::prelude::*; /// NASL function to calculate GMAC with AES128. /// /// This function expects 3 named arguments key, data and iv either in a string or data type. #[cfg(feature = "nasl-c-lib")] -fn aes_gmac( - register: &crate::nasl::utils::Register, - _: &crate::nasl::utils::Context, -) -> Result { - use super::{get_data, get_iv, get_key}; +fn aes_gmac(register: &Register, _: &Context) -> Result { + use super::{get_data, get_iv, get_key, CryptographicError}; use nasl_c_lib::cryptographic::mac::aes_gmac; let key = get_key(register)?; @@ -21,12 +20,7 @@ fn aes_gmac( match aes_gmac(data, key, iv) { Ok(val) => Ok(val.into()), - Err(code) => Err(crate::nasl::utils::NaslError::GeneralError( - crate::nasl::utils::error::GeneralErrorType::UnexpectedData(format!( - "Error code {}", - code - )), - )), + Err(msg) => Err(CryptographicError::AesGmacError(msg.into()).into()), } } diff --git a/rust/src/nasl/builtin/cryptographic/mod.rs b/rust/src/nasl/builtin/cryptographic/mod.rs index 382752f43..865211245 100644 --- a/rust/src/nasl/builtin/cryptographic/mod.rs +++ b/rust/src/nasl/builtin/cryptographic/mod.rs @@ -26,6 +26,10 @@ mod tests; pub enum CryptographicError { #[error("Error in AesGcm: insufficient buffer size.")] InsufficientBufferSize, + #[error("Error in AesCcm: unable to encrypt.")] + AesCcmUnableToEncrypt, + #[error("Error in AesGmac: {0}.")] + AesGmacError(String), } enum Crypt { diff --git a/rust/src/nasl/interpreter/error.rs b/rust/src/nasl/interpreter/error.rs index f3dbec932..670aa22c8 100644 --- a/rust/src/nasl/interpreter/error.rs +++ b/rust/src/nasl/interpreter/error.rs @@ -7,6 +7,7 @@ use std::io; use crate::nasl::syntax::LoadError; use crate::nasl::syntax::{Statement, SyntaxError, TokenCategory}; use crate::nasl::utils::error::NaslError; +use crate::nasl::InternalError; use crate::storage::StorageError; use thiserror::Error; @@ -230,7 +231,9 @@ impl From for InterpretError { impl From for InterpretError { fn from(fe: FunctionError) -> Self { match fe.kind { - NaslError::GeneralError(e) => Self::new(InterpretErrorKind::StorageError(e), None), + NaslError::Internal(InternalError::Storage(e)) => { + Self::new(InterpretErrorKind::StorageError(e), None) + } _ => Self::new(InterpretErrorKind::FunctionCallError(fe), None), } } diff --git a/rust/src/nasl/utils/error.rs b/rust/src/nasl/utils/error.rs index 705fbf39f..00350256e 100644 --- a/rust/src/nasl/utils/error.rs +++ b/rust/src/nasl/utils/error.rs @@ -32,20 +32,23 @@ pub enum ArgumentError { #[derive(Debug, Clone, PartialEq, Eq, Error)] pub enum InternalError { #[error("{0}")] - GeneralError(#[from] GeneralErrorType), + Storage(#[from] StorageError), #[error("{0}")] Dirty(String), } +impl From for NaslError { + fn from(value: StorageError) -> Self { + NaslError::Internal(InternalError::Storage(value)) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Error)] /// Descriptive kind of error that can occur while calling a function pub enum NaslError { /// Diagnostic string is informational and the second arg is the return value for the user #[error("{0}")] Diagnostic(String, Option), - /// Generic error - #[error("Generic error: {0}")] - GeneralError(#[from] GeneralErrorType), /// There is a deeper problem /// An example would be that there is no free memory left in the system #[error("{0}")]