Skip to content

Commit

Permalink
Merge pull request #2 from QuasarApp/task_1
Browse files Browse the repository at this point in the history
Support RSA and x509 formats
  • Loading branch information
EndrII authored Jul 21, 2023
2 parents daa7705 + 6e60c3c commit 83cea81
Show file tree
Hide file tree
Showing 29 changed files with 1,256 additions and 340 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (ANDROID OR IOS)
if (ANDROID)
set(BUILD_SHARED_LIBS ON)
endif()

if (NOT QT_VERSION_MAJOR)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Test QUIET)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Network Test REQUIRED)
endif()
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Test QUIET)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Network Test REQUIRED)

include(submodules/CMake/QuasarApp.cmake)

Expand Down
26 changes: 18 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# Contributing in to EeasySSL

This is a wrap library for the Qt developers. So if you think that is a good library, and you use it in your projects - you can add new improvements and create a pull request with new features.

## What can you do for this Library ?

1. You can add a support of new encryption algorithms
2. You can implement new certificate generator.

## Adding new implementation of crypto algorithms

All Algorithms must be pass simple test. Encrypt, decrypt short and long data arrays. This simple test already implemented, and you just need to add it into main test file.

### Example

Adding supporting RSA algorithm to this library.

1. Create implementation of the iCrypto interface.

```cpp

#include "icrypto.h"
Expand All @@ -32,21 +37,25 @@ Adding supporting RSA algorithm to this library.

}
```
Full implementation of the RSA you can see here.

Full implementation of the RSA you can see [here](https://github.com/QuasarApp/easyssl/blob/main/src/lib/src/public/easyssl/rsassl.h).

2. Add your class to the tests Using The Template class [CryptoTest](https://github.com/QuasarApp/easyssl/blob/main/tests/units/cryptotest.h). See The [tstMain.cpp](https://github.com/QuasarApp/easyssl/blob/main/tests/tstMain.cpp) file

```cpp

2. Add your class to the tests Using The Template class "[CryptoTest](https://github.com/QuasarApp/easyssl/blob/main/tests/units/cryptotest.h)". See The [tstMain.cpp](https://github.com/QuasarApp/easyssl/blob/main/tests/tstMain.cpp) file
``` cpp
TestCase(cryptoTestRSA, CryptoTest<EasySSL::RSASSL>)
```
## Adding new implementation of Certificate generator.
1. Create implementation of the iCrypto interface. And override the create method.
```cpp
/**
* @brief The X509 class This is wrapper of the ssl objects.
*/
class EASYSSL_EXPORT X509: public ICertificate
class EASYSSL_EXPORT X509: public EasySSL::ICertificate
{
public:
X509(const QSharedPointer<ICrypto>& generator);
Expand All @@ -57,9 +66,9 @@ Full implementation of the RSA you can see here.
};
```

Full implementation of x509 certificate format you can see here.
Full implementation of x509 certificate format you can see [here](https://github.com/QuasarApp/easyssl/blob/main/src/lib/src/public/easyssl/x509.h).

2. Add your class to the tests Using The Template class "[CrtTest]()". See The [tstMain.cpp](https://github.com/QuasarApp/easyssl/blob/main/tests/tstMain.cpp) file
2. Add your class to the tests Using The Template class [CrtTest](https://github.com/QuasarApp/easyssl/blob/main/tests/units/crttest.h). See The [tstMain.cpp](https://github.com/QuasarApp/easyssl/blob/main/tests/tstMain.cpp) file

```cpp
#include "crttest.h"
Expand All @@ -70,9 +79,10 @@ Full implementation of x509 certificate format you can see here.
```
## Extra rools
1. All shared tools or useful functions located on the EasySSLUtils class.
2. All implementation must contains goxygen xml comments (documentation)
1. All shared tools or useful functions located on the [EasySSLUtils](https://github.com/QuasarApp/easyssl/blob/main/src/lib/src/private/easysslutils.h) class.
2. All implementation must contains goxygen xml comments (documentation)
3. All implementation must be inner EasySSL name space.
# Thank you
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# EasySSL
This is simple wrapper library that make using ssl simple.
This is wrapper library that make using OpenSSL library more simple.
This library contains interfaces for the signing and encription data.

### Supported encription alhorithms:
* ecdsa based on sll 1.1
* ECDSA
* RSA

### Supported features
* encription
Expand Down Expand Up @@ -33,7 +34,29 @@ This library contains interfaces for the signing and encription data.

## Usage

Authentication
### Encription

```cpp
#include "easyssl/rsassl.h"

// create a publick and private keys array.
int main() {
QByteArray pub, priv;
EasySSL::RSASSL crypto;
crypto.makeKeys(pub, priv)

auto siganture = crypto.signMessage(message, priv);
crypto.checkSign(message, siganture, pub);

auto encriptedMsg = crypto.encrypt(message, pub);
auto decryptedMsg = crypto.decrypt(encriptedMsg, priv);
}


```


### Authentication

```cpp
#include <easyssl/authecdsa.h>
Expand Down Expand Up @@ -73,4 +96,7 @@ edsa.auth(1000, &userID)

```

## Do not forget to help us make this library better...
See our main documentation about contributing to [EasySsl](https://github.com/QuasarApp/easyssl/blob/main/CONTRIBUTING.md)

Full documentation available [here](https://quasarapp.ddns.net:3031/docs/QuasarApp/easyssl/latest/index.html)
5 changes: 3 additions & 2 deletions doxygen.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ PROJECT_BRIEF = EasySSL is base back end library for your c++ Qt projec
# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy
# the logo to the output directory.

PROJECT_LOGO = res/Logo_Web_alpha.png
PROJECT_LOGO =

# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path
# into which the generated documentation will be written. If a relative path is
Expand Down Expand Up @@ -791,7 +791,8 @@ WARN_LOGFILE =
# Note: If this tag is empty the current directory is searched.

INPUT = src \
README.md
README.md \
CONTRIBUTING.md


# This tag can be used to specify the character encoding of the source files
Expand Down
21 changes: 17 additions & 4 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ cmake_minimum_required(VERSION 3.19)

get_filename_component(CURRENT_PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR} NAME)

set(CURRENT_PROJECT "${PROJECT_NAME}${CURRENT_PROJECT_DIR}")
set(CURRENT_PROJECT "${PROJECT_NAME}")
add_definitions(-DEASYSSL_LIBRARY)

list(APPEND CMAKE_FIND_ROOT_PATH "$ENV{OPENSSL_ROOT_DIR}")
find_package(OpenSSL REQUIRED)
find_package(OpenSSL 3.0 REQUIRED)

file(GLOB_RECURSE SOURCE_CPP
"src/*.cpp"
Expand All @@ -33,7 +33,7 @@ set(PRIVATE_INCUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/private")

add_library(${CURRENT_PROJECT} ${SOURCE_CPP} ${SOURCE_QRC})

target_link_libraries(${CURRENT_PROJECT} PUBLIC Qt${QT_VERSION_MAJOR}::Core )
target_link_libraries(${CURRENT_PROJECT} PUBLIC Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Core )

if (EASYSSL_STATIC_SSL)

Expand All @@ -42,9 +42,22 @@ if (EASYSSL_STATIC_SSL)
else()

message("Use shared ssl ")
target_link_libraries(${CURRENT_PROJECT} PRIVATE OpenSSL::Crypto OpenSSL::SSL)
target_link_libraries(${CURRENT_PROJECT} PUBLIC OpenSSL::Crypto OpenSSL::SSL)

if (ANDROID)
set(OPENSSL_ROOT_PATH "$ENV{OPENSSL_ROOT_DIR}")

set(ANDROID_EXTRA_LIBS
${OPENSSL_ROOT_PATH}/lib/libcrypto_android.so
${OPENSSL_ROOT_PATH}/lib/libssl_android.so
CACHE INTERNAL "")

message(ANDROID_EXTRA_LIBS = ${ANDROID_EXTRA_LIBS})
endif()
endif()

message("Use the OpenSSL libraries: ${OPENSSL_LIBRARIES}")

target_include_directories(${CURRENT_PROJECT} PUBLIC ${PUBLIC_INCUDE_DIR})
target_include_directories(${CURRENT_PROJECT} PRIVATE ${PRIVATE_INCUDE_DIR})

Expand Down
91 changes: 91 additions & 0 deletions src/lib/src/private/easysslutils.cpp
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;
}

}
75 changes: 75 additions & 0 deletions src/lib/src/private/easysslutils.h
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);

};



};
1 change: 0 additions & 1 deletion src/lib/src/public/easyssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace EasySSL {

bool init() {
initeasysslResources();
return true;
}

Expand Down
1 change: 0 additions & 1 deletion src/lib/src/public/easyssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "easyssl/global.h"
#include <QString>

inline void initeasysslResources() { Q_INIT_RESOURCE(easyssl); }

namespace EasySSL {

Expand Down
Loading

0 comments on commit 83cea81

Please sign in to comment.