Skip to content

Commit

Permalink
Better handling for cache key errors (#35)
Browse files Browse the repository at this point in the history
* Add CryptoException class

* Remove grading deadline

* Discard .arcache on load_tokens failure

* Use LogDebug instead of Logger::fatal for load_tokens
  • Loading branch information
damianhxy authored May 22, 2024
1 parent 14965a9 commit 5c510f3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
28 changes: 19 additions & 9 deletions include/autolab/autolab.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ struct Assessment {
std::time_t start_at;
std::time_t due_at;
std::time_t end_at;
// available only if user is an instructor of the course
std::time_t grading_deadline;
};

struct DetailedAssessment {
Expand Down Expand Up @@ -128,8 +126,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 +136,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 +147,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 +161,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
1 change: 0 additions & 1 deletion lib/autolab/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ void assessment_from_json(Assessment &asmt, rapidjson::Value &asmt_json) {
asmt.start_at = Utility::string_to_time(get_string_force(asmt_json, "start_at"));
asmt.due_at = Utility::string_to_time(get_string_force(asmt_json, "due_at"));
asmt.end_at = Utility::string_to_time(get_string_force(asmt_json, "end_at"));
asmt.grading_deadline = Utility::string_to_time(get_string(asmt_json, "grading_deadline"));
}

void enrollment_from_json(Enrollment &enrollment, rapidjson::Value &enrollment_json) {
Expand Down
25 changes: 20 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,16 @@ 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." << Logger::endl;
Logger::fatal << e.what() << Logger::endl;
exit(-1);
}
LogDebug("[ContextManager] tokens stored" << Logger::endl);
}

Expand All @@ -88,7 +95,15 @@ 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) {
LogDebug("OpenSSL error in load_tokens." << Logger::endl);
LogDebug(e.what() << Logger::endl);
LogDebug("Removing token cache file." << Logger::endl);
remove(get_token_cache_file_full_path().c_str());
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 5c510f3

Please sign in to comment.