diff --git a/include/autolab/autolab.h b/include/autolab/autolab.h index 05bdb05..65217f0 100644 --- a/include/autolab/autolab.h +++ b/include/autolab/autolab.h @@ -128,8 +128,8 @@ class HttpException: public std::exception { private: std::string msg; public: - explicit HttpException(std::string m) : msg(m) {} - virtual const char* what() const throw() { + explicit HttpException(std::string m) : msg(std::move(m)) {} + const char* what() const noexcept override { return msg.c_str(); } }; @@ -138,7 +138,7 @@ class HttpException: public std::exception { // A new set of tokens should be acquired by re-preforming user authorization. class InvalidTokenException: public std::exception { public: - virtual const char* what() const throw() { + const char* what() const noexcept override { return "The provided access token is invalid and the refresh operation failed."; } }; @@ -149,8 +149,8 @@ class InvalidResponseException: public std::exception { private: std::string msg; public: - explicit InvalidResponseException(std::string m) : msg(m) {} - virtual const char* what() const throw() { + explicit InvalidResponseException(std::string m) : msg(std::move(m)) {} + const char* what() const noexcept override { return msg.c_str(); } }; @@ -163,12 +163,24 @@ class ErrorResponseException: public std::exception { private: std::string msg; public: - explicit ErrorResponseException(std::string m) : msg(m) {} - virtual const char* what() const throw() { + explicit ErrorResponseException(std::string m) : msg(std::move(m)) {} + const char* what() const noexcept override { return msg.c_str(); } }; +// Indicates that an error occurred while encrypting or decrypting data. +// This exception's msg will contain the error message returned by openssl. +class CryptoException: public std::exception { +private: + std::string msg; +public: + explicit CryptoException(std::string m) : msg(std::move(m)) {} + const char* what() const noexcept override { + return msg.c_str(); + } +}; + namespace Utility { // string conversions std::time_t string_to_time(std::string str); diff --git a/src/context_manager/context_manager.cpp b/src/context_manager/context_manager.cpp index 4f5eb45..493680b 100644 --- a/src/context_manager/context_manager.cpp +++ b/src/context_manager/context_manager.cpp @@ -2,6 +2,7 @@ #include "../app_credentials.h" #include "../file/file_utils.h" +#include "autolab/autolab.h" #include "logger.h" #include "../crypto/pseudocrypto.h" @@ -70,10 +71,15 @@ bool token_cache_file_exists() { /* interface */ void store_tokens(std::string at, std::string rt) { check_and_create_token_directory(); - std::string token_pair = token_pair_to_string(at, rt); - - write_file(get_token_cache_file_full_path().c_str(), - token_pair.c_str(), token_pair.length()); + try { + std::string token_pair = token_pair_to_string(at, rt); + + write_file(get_token_cache_file_full_path().c_str(), + token_pair.c_str(), token_pair.length()); + } catch (Autolab::CryptoException &e) { + Logger::fatal << "OpenSSL error in store_tokens " << e.what() << Logger::endl; + exit(-1); + } LogDebug("[ContextManager] tokens stored" << Logger::endl); } @@ -88,7 +94,12 @@ bool load_tokens(std::string &at, std::string &rt) { raw_result, TOKEN_CACHE_FILE_MAXSIZE); LogDebug("read size " << num_read << "\n"); - if (!token_pair_from_string(raw_result, num_read, at, rt)) return false; + try { + if (!token_pair_from_string(raw_result, num_read, at, rt)) return false; + } catch (Autolab::CryptoException &e) { + Logger::fatal << "OpenSSL error in load_tokens " << e.what() << Logger::endl; + return false; + } LogDebug("[ContextManager] tokens loaded" << Logger::endl); return true; } diff --git a/src/crypto/pseudocrypto.cpp b/src/crypto/pseudocrypto.cpp index d973805..051d3d0 100644 --- a/src/crypto/pseudocrypto.cpp +++ b/src/crypto/pseudocrypto.cpp @@ -7,14 +7,13 @@ #include #include +#include "autolab/autolab.h" #include "logger.h" #define MAX_CIPHERTEXT_LEN 256 -void exit_with_crypto_error() { - Logger::fatal << "OpenSSL error" << Logger::endl; - ERR_print_errors_fp(stderr); - exit(-1); +void raise_crypto_error() { + throw Autolab::CryptoException(ERR_error_string(ERR_get_error(), nullptr)); } void check_key_and_iv_lengths(unsigned char *key, unsigned char *iv) { @@ -42,17 +41,17 @@ std::string encrypt_string(std::string srctext, unsigned char *key, // create context if (!(ctx = EVP_CIPHER_CTX_new())) - exit_with_crypto_error(); + raise_crypto_error(); - if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) - exit_with_crypto_error(); + if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv)) + raise_crypto_error(); if (1 != EVP_EncryptUpdate(ctx, ciphertext, &temp_len, plaintext, input_len)) - exit_with_crypto_error(); + raise_crypto_error(); total_len = temp_len; if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + temp_len, &temp_len)) - exit_with_crypto_error(); + raise_crypto_error(); total_len += temp_len; EVP_CIPHER_CTX_free(ctx); @@ -74,17 +73,17 @@ std::string decrypt_string(char *srctext, size_t srclength, unsigned char *key, int input_len = (int)srclength; if (!(ctx = EVP_CIPHER_CTX_new())) - exit_with_crypto_error(); + raise_crypto_error(); - if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) - exit_with_crypto_error(); + if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv)) + raise_crypto_error(); if (1 != EVP_DecryptUpdate(ctx, plaintext, &temp_len, ciphertext, input_len)) - exit_with_crypto_error(); + raise_crypto_error(); total_len = temp_len; if (1 != EVP_DecryptFinal_ex(ctx, plaintext + temp_len, &temp_len)) - exit_with_crypto_error(); + raise_crypto_error(); total_len += temp_len; EVP_CIPHER_CTX_free(ctx);