Skip to content

Commit

Permalink
Add CryptoException class
Browse files Browse the repository at this point in the history
  • Loading branch information
damianhxy committed May 16, 2024
1 parent 1fff68b commit 6538c5f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
26 changes: 19 additions & 7 deletions include/autolab/autolab.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
Expand All @@ -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.";
}
};
Expand All @@ -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();
}
};
Expand All @@ -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);
Expand Down
21 changes: 16 additions & 5 deletions src/context_manager/context_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../app_credentials.h"
#include "../file/file_utils.h"
#include "autolab/autolab.h"
#include "logger.h"
#include "../crypto/pseudocrypto.h"

Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand Down
27 changes: 13 additions & 14 deletions src/crypto/pseudocrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
#include <openssl/err.h>
#include <openssl/evp.h>

#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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 6538c5f

Please sign in to comment.