1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! This is the documentation for the RABE library.
//!
//! * Developped by Georg Bramm, Martin Schanzenbach, Julian Schuette
//! * Type: encryption (attribute-based)
//! * Setting: bilinear groups (asymmetric), based on a modified bn library by zcash
//! * Date: 07/2020
//!
#![allow(dead_code)]
#[macro_use]
extern crate serde_derive;
extern crate base64;
extern crate blake2_rfc;
extern crate rabe_bn;
extern crate byteorder;
extern crate crypto;
extern crate libc;
extern crate rand;
extern crate serde;
extern crate serde_json;
extern crate pest;
#[macro_use]
extern crate pest_derive;

/// implemented schemes
pub mod schemes;
/// various utilities
pub mod utils;

use std::{fmt::{
    Display,
    Result,
    Formatter
}, error::Error, cmp};
use pest::error::{Error as PestError, LineColLocation};
use utils::policy::pest::json::Rule as jsonRule;
use utils::policy::pest::human::Rule as humanRule;
use crypto::symmetriccipher::SymmetricCipherError;

#[derive(Debug)]
pub struct RabeError {
    details: String,
}

impl RabeError {
    fn new(msg: &str) -> RabeError {
        RabeError { details: msg.to_string() }
    }
}

impl Display for RabeError {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "RabeError: {}", self.details)
    }
}

impl Error for RabeError {
    fn description(&self) -> &str {
        &self.details
    }
}

impl From<PestError<jsonRule>> for RabeError {
    fn from(error: PestError<jsonRule>) -> Self {
        let line = match error.line_col.to_owned() {
            LineColLocation::Pos((line, _)) => line,
            LineColLocation::Span((start_line, _), (end_line, _)) => cmp::max(start_line, end_line),
        };
        RabeError::new(
            format!("Json Policy Error in line {}\n", line).as_ref()
        )
    }
}

impl From<PestError<humanRule>> for RabeError {
    fn from(error: PestError<humanRule>) -> Self {
        let line = match error.line_col.to_owned() {
            LineColLocation::Pos((line, _)) => line,
            LineColLocation::Span((start_line, _), (end_line, _)) => cmp::max(start_line, end_line),
        };
        RabeError::new(
            format!("Json Policy Error in line {}\n", line).as_ref()
        )
    }
}

impl From<SymmetricCipherError> for RabeError {
    fn from(error: SymmetricCipherError) -> Self {
        match error {
            SymmetricCipherError::InvalidPadding => RabeError::new(
                format!("Error during decryption: Invalid Padding!").as_ref()
            ),
            SymmetricCipherError::InvalidLength=> RabeError::new(
                format!("Error during decryption: Invalid Length!").as_ref()
            )
        }
    }
}