Skip to content

Commit

Permalink
Move GeneralErrorType to InternalError::Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 4, 2024
1 parent 735b3ef commit 4d824f9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
8 changes: 3 additions & 5 deletions rust/src/nasl/builtin/cryptographic/aes_ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<D, M, N>(
Expand Down Expand Up @@ -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()),
}
}

Expand Down
2 changes: 2 additions & 0 deletions rust/src/nasl/builtin/cryptographic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ mod tests;
pub enum CryptographicError {
#[error("Error in AesGcm: insufficient buffer size.")]
InsufficientBufferSize,
#[error("Error in AesCcm: unable to encrypt.")]
AesCcmUnableToEncrypt,
}

enum Crypt {
Expand Down
5 changes: 4 additions & 1 deletion rust/src/nasl/interpreter/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -230,7 +231,9 @@ impl From<LoadError> for InterpretError {
impl From<FunctionError> 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),
}
}
Expand Down
11 changes: 7 additions & 4 deletions rust/src/nasl/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StorageError> 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<NaslValue>),
/// 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}")]
Expand Down

0 comments on commit 4d824f9

Please sign in to comment.