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
//! Client library for the Ubiq platform
//!
//! Provides client interfaces for encrypting and decrypting data
//! using the [`Ubiq`] [`platform`].
//!
//! # Example
//! ```rust
//! use ubiq::credentials::Credentials;
//! use ubiq::encryption::encrypt;
//! use ubiq::decryption::decrypt;
//!
//! let creds = Credentials::new(None, None).unwrap();
//!
//! let ct = encrypt(&creds, b"abc").unwrap();
//! let pt = decrypt(&creds, &ct).unwrap();
//!
//! assert!(pt != ct);
//! assert!(pt == b"abc");
//! ```
//!
//! [`Ubiq`]: https://www.ubiqsecurity.com/
//! [`platform`]: https://dashboard.ubiqsecurity.com/login

pub(crate) mod algorithm;
pub(crate) mod cipher;
pub(crate) mod client;
pub mod credentials;
pub mod decryption;
pub mod encryption;
pub(crate) mod header;
pub(crate) mod session;

pub(crate) mod base64 {
    use base64::Engine;

    pub fn decode(s: &str) -> crate::result::Result<Vec<u8>> {
        Ok(base64::engine::general_purpose::STANDARD.decode(s)?)
    }

    pub fn encode(v: &[u8]) -> String {
        base64::engine::general_purpose::STANDARD.encode(v)
    }
}

/// Common result interface used by the library
pub mod result {
    pub type Result<T> = std::result::Result<T, crate::error::Error>;
}

/// Errors returned by the Ubiq library
pub mod error {
    /// Structure used to convey errors to users of the library
    #[derive(Debug)]
    pub struct Error {
        why: String,
    }

    impl std::fmt::Display for Error {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            return write!(f, "{}", self.why);
        }
    }

    impl Error {
        /// Construct an error from a string reference
        pub fn new(why: &str) -> Error {
            return Error {
                why: why.to_string(),
            };
        }
    }

    impl From<openssl::error::ErrorStack> for Error {
        fn from(e: openssl::error::ErrorStack) -> Self {
            Error::new(&e.to_string())
        }
    }

    impl From<rsa::pkcs8::Error> for Error {
        fn from(e: rsa::pkcs8::Error) -> Self {
            Error::new(&e.to_string())
        }
    }

    impl From<rsa::Error> for Error {
        fn from(e: rsa::Error) -> Self {
            Error::new(&e.to_string())
        }
    }

    impl From<base64::DecodeError> for Error {
        fn from(e: base64::DecodeError) -> Self {
            Error::new(&e.to_string())
        }
    }
}