generated from QuasarApp/CMakeProject
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from QuasarApp/task_1
Support RSA and x509 formats
- Loading branch information
Showing
29 changed files
with
1,256 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//# | ||
//# Copyright (C) 2021-2023 QuasarApp. | ||
//# Distributed under the GPLv3 software license, see the accompanying | ||
//# Everyone is permitted to copy and distribute verbatim copies | ||
//# of this license document, but changing it is not allowed. | ||
//# | ||
|
||
#include "easysslutils.h" | ||
#include <openssl/bn.h> | ||
#include <openssl/err.h> | ||
#include <openssl/pem.h> | ||
#include <openssl/types.h> | ||
#include <QVector> | ||
|
||
namespace EasySSL { | ||
|
||
|
||
void EasySSLUtils::printlastOpenSSlError() { | ||
ERR_print_errors_fp(stderr); | ||
} | ||
|
||
QByteArray EasySSLUtils::bignumToArray(const BIGNUM *num) { | ||
int length = BN_bn2mpi(num, nullptr); | ||
QVector<unsigned char> data(length); | ||
BN_bn2mpi(num, data.data()); | ||
QByteArray result; | ||
result.insert(0, reinterpret_cast<char*>(data.data()), data.length()); | ||
return result; | ||
} | ||
|
||
BIGNUM *EasySSLUtils::bignumFromArray(const QByteArray &array) { | ||
auto d = reinterpret_cast<const unsigned char*>(array.data()); | ||
BIGNUM* result = BN_mpi2bn(d, | ||
array.length(), nullptr); | ||
if (!result) { | ||
printlastOpenSSlError(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
QByteArray EasySSLUtils::bioToByteArray(BIO* bio) { | ||
QByteArray byteArray; | ||
|
||
int dataSize = BIO_ctrl_pending(bio); | ||
byteArray.resize(dataSize); | ||
if (BIO_read(bio, byteArray.data(), dataSize) != dataSize) { | ||
return {}; | ||
} | ||
|
||
return byteArray; | ||
} | ||
|
||
BIO* EasySSLUtils::byteArrayToBio(const QByteArray& byteArray) { | ||
BIO* bio = BIO_new_mem_buf(byteArray.constData(), byteArray.length()); | ||
return bio; | ||
} | ||
|
||
QByteArray EasySSLUtils::extractPublcKey(EVP_PKEY *ssl_keys) { | ||
if (!ssl_keys) | ||
return {}; | ||
|
||
BIO* bio = BIO_new(BIO_s_mem()); | ||
if (PEM_write_bio_PUBKEY(bio, ssl_keys) != 1) { | ||
BIO_free(bio); | ||
return {}; | ||
} | ||
|
||
QByteArray pubKey = bioToByteArray(bio); | ||
BIO_free(bio); | ||
|
||
return pubKey; | ||
} | ||
|
||
QByteArray EasySSLUtils::extractPrivateKey(EVP_PKEY *ssl_keys) { | ||
if (!ssl_keys) | ||
return {}; | ||
|
||
BIO* bio = BIO_new(BIO_s_mem()); | ||
if (PEM_write_bio_PrivateKey(bio, ssl_keys, nullptr, nullptr, 0, nullptr, nullptr) != 1) { | ||
BIO_free(bio); | ||
return {}; | ||
} | ||
|
||
QByteArray pKey = bioToByteArray(bio); | ||
BIO_free(bio); | ||
|
||
return pKey; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//# | ||
//# Copyright (C) 2021-2023 QuasarApp. | ||
//# Distributed under the GPLv3 software license, see the accompanying | ||
//# Everyone is permitted to copy and distribute verbatim copies | ||
//# of this license document, but changing it is not allowed. | ||
//# | ||
|
||
#include <openssl/types.h> | ||
|
||
#include <QByteArray> | ||
namespace EasySSL { | ||
|
||
/** | ||
* @brief The EasySSLUtils class These are basic utils for work with the opwnssl library. | ||
*/ | ||
class EasySSLUtils { | ||
|
||
public: | ||
|
||
/** | ||
* @brief printlastOpenSSlError This method prints the latest ssl error message. | ||
*/ | ||
static void printlastOpenSSlError(); | ||
|
||
/** | ||
* @brief bignumToArray This method converts openssl BIGNUM into byteArray | ||
* @param num This is a big num of the openssl library | ||
* @return bytes array. | ||
*/ | ||
static QByteArray bignumToArray(const BIGNUM* num); | ||
|
||
/** | ||
* @brief bignumFromArray This method converts the Qt bytes array into the opensll big num. | ||
* @param array This is an input array. | ||
* @return big num pointer. | ||
* @note This result pointer will not be free automatically. Please free the returned pointer after use. | ||
*/ | ||
[[nodiscard("The result pointer will not be free automatically. Please free the returned pointer after using.")]] | ||
static BIGNUM* bignumFromArray(const QByteArray& array); | ||
|
||
/** | ||
* @brief bioToByteArray This method converts the openssl BIO to the QByteArry | ||
* @param bio input arrary. | ||
* @return Qt Array | ||
*/ | ||
static QByteArray bioToByteArray(BIO *bio); | ||
|
||
/** | ||
* @brief byteArrayToBio This method creates the BIO struct from the Qt QByteArray object. | ||
* @param byteArray This is an input Qt byte array. | ||
* @return pointer to the BIO struct of OpenSLL library. | ||
* @note Don't forget to free the result pointer. | ||
*/ | ||
[[nodiscard("This pointer will not free automatically. Please free returned pointer after using.")]] | ||
static BIO *byteArrayToBio(const QByteArray &byteArray); | ||
|
||
/** | ||
* @brief extractPublcKey This method extracts the public key from the ssl (pem) structure. | ||
* @param ssl_keys These are objects of the ssl keys. | ||
* @return bytes array of the extracted key. | ||
*/ | ||
static QByteArray extractPublcKey(EVP_PKEY* ssl_keys); | ||
|
||
/** | ||
* @brief extractPrivateKey This method extracts the private key from the ssl (pem) structure. | ||
* @param ssl_keys These are objects of the ssl keys. | ||
* @return bytes array of the extracted key. | ||
*/ | ||
static QByteArray extractPrivateKey(EVP_PKEY* ssl_keys); | ||
|
||
}; | ||
|
||
|
||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
namespace EasySSL { | ||
|
||
bool init() { | ||
initeasysslResources(); | ||
return true; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.