Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging levels. #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const char* CertName = "quiccat";
void
PrintHexBuffer(const char* const Label, const uint8_t*const Buf, uint32_t Len)
{
Log() << Label << ": ";
Log(LogInfo) << Label << ": ";
for(unsigned i = 0; i < Len; i++) {
printf("%02x", (unsigned char)Buf[i]);
}
Log() << std::endl;
Log(LogInfo) << std::endl;
}

EVP_PKEY*
Expand All @@ -38,14 +38,14 @@ QcGenerateSigningKey(
uint8_t SigningKeyBytes[ED448_KEYLEN];
int Ret = PKCS5_PBKDF2_HMAC(Password.c_str(), (int)Password.length(), Salt, SaltLen, PBKDFIterations, EVP_sha512(), sizeof(SigningKeyBytes), SigningKeyBytes);
if (Ret != 1) {
Log() << "Failed to run PBKDF2!\n";
Log(LogError) << "Failed to run PBKDF2!\n";
goto Error;
}

SigningKey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED448, nullptr, SigningKeyBytes, sizeof(SigningKeyBytes));
if (SigningKey == nullptr) {
Log() << "Failed to create signing key!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log() << str << std::endl; return 1;}, nullptr);
Log(LogError) << "Failed to create signing key!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log(LogError) << str << std::endl; return 1;}, nullptr);
goto Error;
}

Expand Down Expand Up @@ -78,55 +78,55 @@ QcGenerateAuthCertificate(

EVP_PKEY_CTX *KeyContext = EVP_PKEY_CTX_new_id(EVP_PKEY_ED448, NULL);
if (KeyContext == nullptr) {
Log() << "Failed to allocate Key context!\n";
Log(LogError) << "Failed to allocate Key context!\n";
goto Error;
}

Ret = EVP_PKEY_keygen_init(KeyContext);
if (Ret != 1) {
Log() << "Keygen init failed!\n";
Log(LogError) << "Keygen init failed!\n";
goto Error;
}

Ret = EVP_PKEY_keygen(KeyContext, &PrivateKey);
if (Ret != 1) {
Log() << "Keygen failed!\n";
Log(LogError) << "Keygen failed!\n";
goto Error;
}

Ret = RAND_bytes(Salt, sizeof(Salt));
if (Ret != 1) {
Log() << "Failed to get random bytes!\n";
Log(LogError) << "Failed to get random bytes!\n";
goto Error;
}

Cert = X509_new();
if (Cert == nullptr) {
Log() << "Failed to allocate X509!\n";
Log(LogError) << "Failed to allocate X509!\n";
goto Error;
}

Ret = X509_set_version(Cert, 2);
if (Ret != 1) {
Log() << "Failed to set certificate version!\n";
Log(LogError) << "Failed to set certificate version!\n";
goto Error;
}

SaltBn = BN_bin2bn(Salt, sizeof(Salt), nullptr);
if (SaltBn == nullptr) {
Log() << "Failed to convert Salt to BIGNUM!\n";
Log(LogError) << "Failed to convert Salt to BIGNUM!\n";
goto Error;
}

SerialNumber = BN_to_ASN1_INTEGER(SaltBn, nullptr);
if (SerialNumber == nullptr) {
Log() << "Failed to allocate serial number!\n";
Log(LogError) << "Failed to allocate serial number!\n";
goto Error;
}

Ret = X509_set_serialNumber(Cert, SerialNumber);
if (Ret != 1) {
Log() << "Failed to set serial number!\n";
Log(LogError) << "Failed to set serial number!\n";
goto Error;
}

Expand All @@ -135,24 +135,24 @@ QcGenerateAuthCertificate(

Ret = X509_set_pubkey(Cert, PrivateKey);
if (Ret != 1) {
Log() << "Failed to set public key on cert!\n";
Log(LogError) << "Failed to set public key on cert!\n";
goto Error;
}

Name = X509_get_subject_name(Cert);
if (Name == nullptr) {
Log() << "Failed to allocate subject name!\n";
Log(LogError) << "Failed to allocate subject name!\n";
goto Error;
}
Ret = X509_NAME_add_entry_by_txt(Name, "CN", MBSTRING_ASC, (unsigned char*)CertName, -1, -1, 0);
if (Ret != 1) {
Log() << "Failed to set subject name!\n";
Log(LogError) << "Failed to set subject name!\n";
goto Error;
}

Ret = X509_set_issuer_name(Cert, Name);
if (Ret != 1) {
Log() << "Failed to set issuer name!\n";
Log(LogError) << "Failed to set issuer name!\n";
goto Error;
}

Expand All @@ -163,41 +163,41 @@ QcGenerateAuthCertificate(

Ret = X509_sign(Cert, SigningKey, nullptr);
if (Ret == 0) {
Log() << "Failed to sign certificate!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log() << str << std::endl; return 1;}, nullptr);
Log(LogError) << "Failed to sign certificate!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log(LogError) << str << std::endl; return 1;}, nullptr);
goto Error;
}

NewPkcs12 = PKCS12_create("", CertName, PrivateKey, Cert, nullptr, -1, -1, 0, 0, 0);
if (NewPkcs12 == nullptr) {
Log() << "Failed to create new PKCS12!\n";
Log(LogError) << "Failed to create new PKCS12!\n";
goto Error;
}

Ret = i2d_PKCS12(NewPkcs12, nullptr);
if (Ret <= 0) {
Log() << "Failed to get export buffer size of NewPkcs12!\n";
Log(LogError) << "Failed to get export buffer size of NewPkcs12!\n";
goto Error;
}

Pkcs12Length = Ret;

Pkcs12Buffer = new (std::nothrow) uint8_t[Pkcs12Length];
if (Pkcs12Buffer == nullptr) {
Log() << "Failed to allocate " << Pkcs12Length << " bytes for Pkcs12!\n";
Log(LogError) << "Failed to allocate " << Pkcs12Length << " bytes for Pkcs12!\n";
goto Error;
}

Pkcs12BufferPtr = Pkcs12Buffer;

Ret = i2d_PKCS12(NewPkcs12, &Pkcs12BufferPtr);
if (Ret < 0) {
Log() << "Failed to export NewPkcs12!\n";
Log(LogError) << "Failed to export NewPkcs12!\n";
goto Error;
}

if ((uint32_t)Ret != Pkcs12Length) {
Log() << "Pkcs12 export length changed between calls!\n";
Log(LogError) << "Pkcs12 export length changed between calls!\n";
goto Error;
}

Expand Down Expand Up @@ -259,18 +259,18 @@ QcVerifyCertificate(

SaltBn = ASN1_INTEGER_to_BN(SerialNumber, nullptr);
if (SaltBn == nullptr) {
Log() << "Failed to convert ASN SerialNumber to BIGNUM Salt!\n";
Log(LogError) << "Failed to convert ASN SerialNumber to BIGNUM Salt!\n";
goto Error;
}

if (BN_num_bytes(SaltBn) > (int)sizeof(Salt)) {
Log() << "Serial number is not correct size! " << BN_num_bytes(SaltBn) << " vs " << sizeof(Salt) << std::endl;
Log(LogError) << "Serial number is not correct size! " << BN_num_bytes(SaltBn) << " vs " << sizeof(Salt) << std::endl;
goto Error;
}

Ret = BN_bn2binpad(SaltBn, Salt, sizeof(Salt));
if (Ret != sizeof(Salt)) {
Log() << "BIGNUM conversion to binary is wrong size! " << Ret << " vs " << sizeof(Salt) << std::endl;
Log(LogError) << "BIGNUM conversion to binary is wrong size! " << Ret << " vs " << sizeof(Salt) << std::endl;
goto Error;
}

Expand All @@ -283,15 +283,15 @@ QcVerifyCertificate(
if (Ret == 1) {
Result = true;
} else if (Ret == 0) {
Log() << "Certificate failed signature verification!\n";
Log(LogError) << "Certificate failed signature verification!\n";
goto Error;
} else if (Ret == -1) {
Log() << "Certificate signature is malformed!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log() << str << std::endl; return 1;}, nullptr);
Log(LogError) << "Certificate signature is malformed!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log(LogError) << str << std::endl; return 1;}, nullptr);
goto Error;
} else {
Log() << "Certificate failed validation for another reason!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log() << str << std::endl; return 1;}, nullptr);
Log(LogError) << "Certificate failed validation for another reason!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log(LogError) << str << std::endl; return 1;}, nullptr);
goto Error;
}

Expand Down
19 changes: 16 additions & 3 deletions log.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#pragma once

enum QcLogLevel : uint8_t {
LogFatal,
LogError,
LogInfo
};

extern QcLogLevel CurrentLogLevel;
extern std::ofstream NullLogger;

static
inline
std::ostream&
Log() {
return std::cerr;
}
Log(QcLogLevel Level = LogFatal) {
if (Level <= CurrentLogLevel) {
return std::cerr;
} else {
return NullLogger;
}
}
Loading