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 5, 2024
1 parent 735b3ef commit 24a12a2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 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
16 changes: 5 additions & 11 deletions rust/src/nasl/builtin/cryptographic/aes_gmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<crate::nasl::syntax::NaslValue, crate::nasl::utils::NaslError> {
use super::{get_data, get_iv, get_key};
fn aes_gmac(register: &Register, _: &Context) -> Result<NaslValue, NaslError> {
use super::{get_data, get_iv, get_key, CryptographicError};
use nasl_c_lib::cryptographic::mac::aes_gmac;

let key = get_key(register)?;
Expand All @@ -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()),
}
}

Expand Down
4 changes: 4 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,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 {
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 24a12a2

Please sign in to comment.