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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * MIT License (MIT)
 * Copyright (c) 2019 Activeledger
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

//! # Key Error definitions

use std::error::Error;
use std::fmt;

/// KeyResult definition - Shorthand for: Result<T, KeyError>
pub type KeyResult<T> = Result<T, KeyError>;

/// KeyError data holder
#[derive(Debug)]
pub enum KeyError {
    GenerationError(u16), // 1000
    SigningError(u16),    // 2000
    StringifyError(u16),  // 3000
    ImportError(u16),     // 4000
    ExportError(u16),     // 5000
}

impl fmt::Display for KeyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            KeyError::GenerationError(ref code) => {
                let error = KeyErrorHandler::get_generation_error(code);
                write!(f, "Generation Error - {}: {}", code, error)
            }
            KeyError::SigningError(ref code) => {
                let error = KeyErrorHandler::get_signing_error(code);
                write!(f, "Signing Error - {}: {}", code, error)
            }
            KeyError::StringifyError(ref code) => {
                let error = KeyErrorHandler::get_stringify_error(code);
                write!(f, "Stringify Error - {}: {}", code, error)
            }
            KeyError::ImportError(ref code) => {
                let error = KeyErrorHandler::get_import_error(code);
                write!(f, "Import Error - {}: {}", code, error)
            }
            KeyError::ExportError(ref code) => {
                let error = KeyErrorHandler::get_export_error(code);
                write!(f, "Export Error - {}: {}", code, error)
            }
        }
    }
}

impl Error for KeyError {}

struct KeyErrorHandler;

impl KeyErrorHandler {
    fn get_generation_error(code: &u16) -> &str {
        match code {
            // EC
            1000 => "Error generating Elliptic Curve Group for keygen",
            1001 => "Error generating Elliptic Curve Keypair",
            1002 => "Error generating Elliptic Curve Private PEM",
            1003 => "Error generating Elliptic Curve Public PEM",

            // RSA
            1004 => "Error generating the RSA Key",
            1005 => "Error generating RSA Private PEM",
            1006 => "Error generating RSA Public PEM",
            _ => "Unknown Error",
        }
    }

    fn get_signing_error(code: &u16) -> &str {
        match code {
            2000 => "Error creating signer",
            2001 => "Error passing data to sign",
            2002 => "Error generating signature",
            2003 => "Error decoding signature for verification",
            2004 => "Error creating verifier",
            2005 => "Error passing data to verify",
            2006 => "Signature verification failed",
            2007 => "Error initialising private key",
            2008 => "Error initialising public key",
            _ => "Unknown Error",
        }
    }

    fn get_stringify_error(code: &u16) -> &str {
        match code {
            3000 => "Error converting private pem to string",
            3001 => "Error converting public pem to string",
            3007 => "Error initialising private key",
            3008 => "Error initialising public key",
            _ => "Unknown Error",
        }
    }

    fn get_import_error(code: &u16) -> &str {
        match code {
            4000 => "Error opening file for import",
            4001 => "Error reading file contents",
            4002 => "Key Type missmatch",
            _ => "Unknown Error",
        }
    }

    fn get_export_error(code: &u16) -> &str {
        match code {
            5000 => "Error generating JSON",
            5001 => "Error preparing the export file for writing",
            5002 => "Error writing to the export file",
            _ => "Unknown Error",
        }
    }
}