Skip to content

Commit

Permalink
Merge pull request #1752 from greenbone/enum-error-handling
Browse files Browse the repository at this point in the history
Error handling for builtin functions
  • Loading branch information
Tehforsch authored Dec 5, 2024
2 parents 2f613e5 + e1d20c6 commit 22b614e
Show file tree
Hide file tree
Showing 82 changed files with 1,650 additions and 1,687 deletions.
4 changes: 2 additions & 2 deletions rust/src/feed/transpile/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::nasl::syntax::{LoadError, Statement};
use super::verify;
use super::Replace;

#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[derive(Debug, Clone, PartialEq, Error)]
/// Error during transpiling
pub enum TranspileError {
/// Loader is unable to handle operation
Expand All @@ -19,7 +19,7 @@ pub enum TranspileError {
Replace(#[from] ReplaceError),
}

#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[derive(Debug, Clone, PartialEq, Error)]
/// Error cases on a replace operation
pub enum ReplaceError {
/// The replace operation is invalid on statement
Expand Down
4 changes: 2 additions & 2 deletions rust/src/feed/update/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use thiserror::Error;

use crate::feed::{verify, VerifyError};

#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[derive(Debug, Error)]
/// Errors within feed handling
pub enum ErrorKind {
/// An InterpretError occurred while interpreting
Expand All @@ -32,7 +32,7 @@ pub enum ErrorKind {
VerifyError(#[from] verify::Error),
}

#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[derive(Debug, Error)]
#[error("Error with key '{key}': {kind}")]
/// ErrorKind and key of error
pub struct Error {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/feed/verify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use sequoia_ipc::keybox::{Keybox, KeyboxRecord};
use sequoia_openpgp as openpgp;
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[derive(Debug, Clone, PartialEq, Error)]
/// Defines error cases that can happen while verifying
pub enum Error {
#[error("Incorrect feed.")]
Expand Down
56 changes: 25 additions & 31 deletions rust/src/nasl/builtin/cert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ use std::collections::HashMap;
use std::sync::RwLock;

use nasl_function_proc_macro::nasl_function;
use thiserror::Error;
use x509_certificate::X509Certificate;
use x509_parser::prelude::GeneralName;

use crate::{
function_set,
nasl::{FunctionErrorKind, NaslValue},
};
use crate::nasl::prelude::*;

use super::string::encode_hex;

#[derive(Debug, Error)]
pub enum CertError {
#[error("Unable to calculate SHA256 fingerprint")]
UnableToCalculateSHA256Fingerprint,
#[error("Unable to calculate SHA1 fingerprint")]
UnableToCalculateSHA1Fingerprint,
#[error("Query parameter 'all' not implemented yet.")]
QueryParamAllNotImplemented,
}

fn sign_alg_oid_to_name(oid: &str) -> &str {
match oid {
"1.2.840.10040.4.1" => "id-dsa",
Expand Down Expand Up @@ -118,7 +126,7 @@ pub enum CertCommands {
}

impl TryFrom<&str> for CertCommands {
type Error = FunctionErrorKind;
type Error = FnError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
Expand All @@ -138,9 +146,9 @@ impl TryFrom<&str> for CertCommands {
"modulus" => Ok(Self::Modulus),
"exponent" => Ok(Self::Exponent),
"key-size" => Ok(Self::KeySize),
_ => Err(FunctionErrorKind::WrongArgument(
"The given query is not valid.".to_string(),
)),
_ => Err(
ArgumentError::WrongArgument("The given query is not valid.".to_string()).into(),
),
}
}
}
Expand Down Expand Up @@ -176,7 +184,7 @@ impl NaslCerts {
/// On success the function returns a cert identifier that can be used
/// for further operations.
#[nasl_function]
fn cert_open(&self, cert: &[u8]) -> Result<usize, FunctionErrorKind> {
fn cert_open(&self, cert: &[u8]) -> Result<usize, FnError> {
if let Ok(cert) = X509Certificate::from_der(cert) {
return Ok(self.insert(cert));
}
Expand All @@ -187,10 +195,11 @@ impl NaslCerts {
return Ok(self.insert(cert));
}

Err(FunctionErrorKind::WrongArgument(
Err(ArgumentError::WrongArgument(
"The given string is not a valid DER, BER or PEM encoded X.509 certificate."
.to_string(),
))
)
.into())
}

/// Release a certificate object.
Expand Down Expand Up @@ -358,12 +367,12 @@ impl NaslCerts {
cert_handle: usize,
query: &str,
idx: Option<usize>,
) -> Result<NaslValue, FunctionErrorKind> {
) -> Result<NaslValue, FnError> {
let idx = idx.unwrap_or(0);
let handle = self.0.read().unwrap();

let cert = handle.certs.get(&cert_handle).ok_or_else(|| {
FunctionErrorKind::WrongArgument("The given file descriptor is not valid.".to_string())
ArgumentError::WrongArgument("The given file descriptor is not valid.".to_string())
})?;
let result = match CertCommands::try_from(query)? {
CertCommands::Serial => {
Expand All @@ -387,27 +396,12 @@ impl NaslCerts {
CertCommands::FprSha256 => cert
.sha256_fingerprint()
.map(|fpr| NaslValue::String(encode_hex(fpr.as_ref())))
.map_err(|_| {
FunctionErrorKind::Diagnostic(
"Unable to calculate SHA256 fingerprint".to_string(),
None,
)
})?,
.map_err(|_| CertError::UnableToCalculateSHA256Fingerprint)?,
CertCommands::FprSha1 => cert
.sha1_fingerprint()
.map(|fpr| NaslValue::String(encode_hex(fpr.as_ref())))
.map_err(|_| {
FunctionErrorKind::Diagnostic(
"Unable to calculate SHA1 fingerprint".to_string(),
None,
)
})?,
CertCommands::All => {
return Err(FunctionErrorKind::Diagnostic(
"Query parameter 'all' is not implemented yet".to_string(),
None,
))
}
.map_err(|_| CertError::UnableToCalculateSHA1Fingerprint)?,
CertCommands::All => return Err(CertError::QueryParamAllNotImplemented.into()),
CertCommands::Hostnames => NaslValue::Array(
Self::hostnames(cert)
.into_iter()
Expand Down
30 changes: 14 additions & 16 deletions rust/src/nasl/builtin/cryptographic/aes_cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ use aes::{
};
use cbc::{Decryptor, Encryptor};

use crate::function_set;
use crate::nasl::syntax::NaslValue;
use crate::nasl::utils::error::FunctionErrorKind;
use crate::nasl::utils::{Context, Register};
use crate::nasl::prelude::*;

use super::{get_data, get_iv, get_key, get_len, Crypt};

/// Base function for en- and decrypting Cipher Block Chaining (CBC) mode
fn cbc<D>(register: &Register, crypt: Crypt) -> Result<NaslValue, FunctionErrorKind>
fn cbc<D>(register: &Register, crypt: Crypt) -> Result<NaslValue, FnError>
where
D: BlockCipher + BlockEncrypt + BlockDecrypt + KeyInit,
{
Expand All @@ -35,7 +32,7 @@ where
let res = Encryptor::<D>::new_from_slices(key, iv);
match res {
Ok(encryptor) => Ok(encryptor.encrypt_padded_vec_mut::<ZeroPadding>(data).into()),
Err(e) => Err(FunctionErrorKind::WrongArgument(e.to_string())),
Err(e) => Err(ArgumentError::WrongArgument(e.to_string()).into()),
}
}
Crypt::Decrypt => {
Expand All @@ -47,20 +44,21 @@ where

// len should not be more than the length of the data
if len > data.len() {
return Err(FunctionErrorKind::wrong_argument(
return Err(ArgumentError::wrong_argument(
"len",
format!("<={:?}", data.len()).as_str(),
len.to_string().as_str(),
));
)
.into());
}
let res = Decryptor::<D>::new_from_slices(key, iv);
match res {
Ok(decryptor) => Ok(decryptor
.decrypt_padded_vec_mut::<NoPadding>(data)
.map_err(|e| FunctionErrorKind::WrongArgument(e.to_string()))?[..len]
.map_err(|e| ArgumentError::WrongArgument(e.to_string()))?[..len]
.to_vec()
.into()),
Err(e) => Err(FunctionErrorKind::WrongArgument(e.to_string())),
Err(e) => Err(ArgumentError::WrongArgument(e.to_string()).into()),
}
}
}
Expand All @@ -73,7 +71,7 @@ where
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
cbc::<Aes128>(register, Crypt::Encrypt)
}

Expand All @@ -85,7 +83,7 @@ fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
cbc::<Aes128>(register, Crypt::Decrypt)
}

Expand All @@ -96,7 +94,7 @@ fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
cbc::<Aes192>(register, Crypt::Encrypt)
}

Expand All @@ -108,7 +106,7 @@ fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
cbc::<Aes192>(register, Crypt::Decrypt)
}

Expand All @@ -119,7 +117,7 @@ fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
cbc::<Aes256>(register, Crypt::Encrypt)
}

Expand All @@ -131,7 +129,7 @@ fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result<NaslValue, Fun
/// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be
/// known for decryption. If no length is given, the last block is decrypted as a whole.
/// - The iv must have a length of 16 bytes
fn aes256_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FunctionErrorKind> {
fn aes256_cbc_decrypt(register: &Register, _: &Context) -> Result<NaslValue, FnError> {
cbc::<Aes256>(register, Crypt::Decrypt)
}

Expand Down
Loading

0 comments on commit 22b614e

Please sign in to comment.