Skip to content

Commit 83cea81

Browse files
authored
Merge pull request #2 from QuasarApp/task_1
Support RSA and x509 formats
2 parents daa7705 + 6e60c3c commit 83cea81

29 files changed

+1256
-340
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ set(CMAKE_AUTOUIC ON)
2020
set(CMAKE_CXX_STANDARD 17)
2121
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2222

23-
if (ANDROID OR IOS)
23+
if (ANDROID)
2424
set(BUILD_SHARED_LIBS ON)
2525
endif()
2626

2727
if (NOT QT_VERSION_MAJOR)
28-
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Test QUIET)
28+
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Network Test REQUIRED)
2929
endif()
30-
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Test QUIET)
30+
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Network Test REQUIRED)
3131

3232
include(submodules/CMake/QuasarApp.cmake)
3333

CONTRIBUTING.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
# Contributing in to EeasySSL
2+
23
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.
34

45
## What can you do for this Library ?
6+
57
1. You can add a support of new encryption algorithms
68
2. You can implement new certificate generator.
79

810
## Adding new implementation of crypto algorithms
11+
912
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.
1013

1114
### Example
15+
1216
Adding supporting RSA algorithm to this library.
1317

1418
1. Create implementation of the iCrypto interface.
19+
1520
```cpp
1621

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

3338
}
3439
```
35-
Full implementation of the RSA you can see here.
40+
41+
Full implementation of the RSA you can see [here](https://github.com/QuasarApp/easyssl/blob/main/src/lib/src/public/easyssl/rsassl.h).
42+
43+
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
44+
45+
```cpp
3646

37-
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
38-
``` cpp
3947
TestCase(cryptoTestRSA, CryptoTest<EasySSL::RSASSL>)
4048
```
4149
4250
## Adding new implementation of Certificate generator.
4351
4452
1. Create implementation of the iCrypto interface. And override the create method.
53+
4554
```cpp
4655
/**
4756
* @brief The X509 class This is wrapper of the ssl objects.
4857
*/
49-
class EASYSSL_EXPORT X509: public ICertificate
58+
class EASYSSL_EXPORT X509: public EasySSL::ICertificate
5059
{
5160
public:
5261
X509(const QSharedPointer<ICrypto>& generator);
@@ -57,9 +66,9 @@ Full implementation of the RSA you can see here.
5766
};
5867
```
5968

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

62-
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
71+
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
6372

6473
```cpp
6574
#include "crttest.h"
@@ -70,9 +79,10 @@ Full implementation of x509 certificate format you can see here.
7079
```
7180
7281
## Extra rools
73-
1. All shared tools or useful functions located on the EasySSLUtils class.
74-
2. All implementation must contains goxygen xml comments (documentation)
7582
83+
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.
84+
2. All implementation must contains goxygen xml comments (documentation)
85+
3. All implementation must be inner EasySSL name space.
7686
7787
# Thank you
7888

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# EasySSL
2-
This is simple wrapper library that make using ssl simple.
2+
This is wrapper library that make using OpenSSL library more simple.
33
This library contains interfaces for the signing and encription data.
44

55
### Supported encription alhorithms:
6-
* ecdsa based on sll 1.1
6+
* ECDSA
7+
* RSA
78

89
### Supported features
910
* encription
@@ -33,7 +34,29 @@ This library contains interfaces for the signing and encription data.
3334
3435
## Usage
3536
36-
Authentication
37+
### Encription
38+
39+
```cpp
40+
#include "easyssl/rsassl.h"
41+
42+
// create a publick and private keys array.
43+
int main() {
44+
QByteArray pub, priv;
45+
EasySSL::RSASSL crypto;
46+
crypto.makeKeys(pub, priv)
47+
48+
auto siganture = crypto.signMessage(message, priv);
49+
crypto.checkSign(message, siganture, pub);
50+
51+
auto encriptedMsg = crypto.encrypt(message, pub);
52+
auto decryptedMsg = crypto.decrypt(encriptedMsg, priv);
53+
}
54+
55+
56+
```
57+
58+
59+
### Authentication
3760

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

7497
```
7598

99+
## Do not forget to help us make this library better...
100+
See our main documentation about contributing to [EasySsl](https://github.com/QuasarApp/easyssl/blob/main/CONTRIBUTING.md)
101+
76102
Full documentation available [here](https://quasarapp.ddns.net:3031/docs/QuasarApp/easyssl/latest/index.html)

doxygen.conf.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ PROJECT_BRIEF = EasySSL is base back end library for your c++ Qt projec
5151
# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy
5252
# the logo to the output directory.
5353

54-
PROJECT_LOGO = res/Logo_Web_alpha.png
54+
PROJECT_LOGO =
5555

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

793793
INPUT = src \
794-
README.md
794+
README.md \
795+
CONTRIBUTING.md
795796

796797

797798
# This tag can be used to specify the character encoding of the source files

src/lib/CMakeLists.txt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ cmake_minimum_required(VERSION 3.19)
99

1010
get_filename_component(CURRENT_PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR} NAME)
1111

12-
set(CURRENT_PROJECT "${PROJECT_NAME}${CURRENT_PROJECT_DIR}")
12+
set(CURRENT_PROJECT "${PROJECT_NAME}")
1313
add_definitions(-DEASYSSL_LIBRARY)
1414

1515
list(APPEND CMAKE_FIND_ROOT_PATH "$ENV{OPENSSL_ROOT_DIR}")
16-
find_package(OpenSSL REQUIRED)
16+
find_package(OpenSSL 3.0 REQUIRED)
1717

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

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

36-
target_link_libraries(${CURRENT_PROJECT} PUBLIC Qt${QT_VERSION_MAJOR}::Core )
36+
target_link_libraries(${CURRENT_PROJECT} PUBLIC Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Core )
3737

3838
if (EASYSSL_STATIC_SSL)
3939

@@ -42,9 +42,22 @@ if (EASYSSL_STATIC_SSL)
4242
else()
4343

4444
message("Use shared ssl ")
45-
target_link_libraries(${CURRENT_PROJECT} PRIVATE OpenSSL::Crypto OpenSSL::SSL)
45+
target_link_libraries(${CURRENT_PROJECT} PUBLIC OpenSSL::Crypto OpenSSL::SSL)
46+
47+
if (ANDROID)
48+
set(OPENSSL_ROOT_PATH "$ENV{OPENSSL_ROOT_DIR}")
49+
50+
set(ANDROID_EXTRA_LIBS
51+
${OPENSSL_ROOT_PATH}/lib/libcrypto_android.so
52+
${OPENSSL_ROOT_PATH}/lib/libssl_android.so
53+
CACHE INTERNAL "")
54+
55+
message(ANDROID_EXTRA_LIBS = ${ANDROID_EXTRA_LIBS})
56+
endif()
4657
endif()
4758

59+
message("Use the OpenSSL libraries: ${OPENSSL_LIBRARIES}")
60+
4861
target_include_directories(${CURRENT_PROJECT} PUBLIC ${PUBLIC_INCUDE_DIR})
4962
target_include_directories(${CURRENT_PROJECT} PRIVATE ${PRIVATE_INCUDE_DIR})
5063

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//#
2+
//# Copyright (C) 2021-2023 QuasarApp.
3+
//# Distributed under the GPLv3 software license, see the accompanying
4+
//# Everyone is permitted to copy and distribute verbatim copies
5+
//# of this license document, but changing it is not allowed.
6+
//#
7+
8+
#include "easysslutils.h"
9+
#include <openssl/bn.h>
10+
#include <openssl/err.h>
11+
#include <openssl/pem.h>
12+
#include <openssl/types.h>
13+
#include <QVector>
14+
15+
namespace EasySSL {
16+
17+
18+
void EasySSLUtils::printlastOpenSSlError() {
19+
ERR_print_errors_fp(stderr);
20+
}
21+
22+
QByteArray EasySSLUtils::bignumToArray(const BIGNUM *num) {
23+
int length = BN_bn2mpi(num, nullptr);
24+
QVector<unsigned char> data(length);
25+
BN_bn2mpi(num, data.data());
26+
QByteArray result;
27+
result.insert(0, reinterpret_cast<char*>(data.data()), data.length());
28+
return result;
29+
}
30+
31+
BIGNUM *EasySSLUtils::bignumFromArray(const QByteArray &array) {
32+
auto d = reinterpret_cast<const unsigned char*>(array.data());
33+
BIGNUM* result = BN_mpi2bn(d,
34+
array.length(), nullptr);
35+
if (!result) {
36+
printlastOpenSSlError();
37+
}
38+
39+
return result;
40+
}
41+
42+
QByteArray EasySSLUtils::bioToByteArray(BIO* bio) {
43+
QByteArray byteArray;
44+
45+
int dataSize = BIO_ctrl_pending(bio);
46+
byteArray.resize(dataSize);
47+
if (BIO_read(bio, byteArray.data(), dataSize) != dataSize) {
48+
return {};
49+
}
50+
51+
return byteArray;
52+
}
53+
54+
BIO* EasySSLUtils::byteArrayToBio(const QByteArray& byteArray) {
55+
BIO* bio = BIO_new_mem_buf(byteArray.constData(), byteArray.length());
56+
return bio;
57+
}
58+
59+
QByteArray EasySSLUtils::extractPublcKey(EVP_PKEY *ssl_keys) {
60+
if (!ssl_keys)
61+
return {};
62+
63+
BIO* bio = BIO_new(BIO_s_mem());
64+
if (PEM_write_bio_PUBKEY(bio, ssl_keys) != 1) {
65+
BIO_free(bio);
66+
return {};
67+
}
68+
69+
QByteArray pubKey = bioToByteArray(bio);
70+
BIO_free(bio);
71+
72+
return pubKey;
73+
}
74+
75+
QByteArray EasySSLUtils::extractPrivateKey(EVP_PKEY *ssl_keys) {
76+
if (!ssl_keys)
77+
return {};
78+
79+
BIO* bio = BIO_new(BIO_s_mem());
80+
if (PEM_write_bio_PrivateKey(bio, ssl_keys, nullptr, nullptr, 0, nullptr, nullptr) != 1) {
81+
BIO_free(bio);
82+
return {};
83+
}
84+
85+
QByteArray pKey = bioToByteArray(bio);
86+
BIO_free(bio);
87+
88+
return pKey;
89+
}
90+
91+
}

src/lib/src/private/easysslutils.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//#
2+
//# Copyright (C) 2021-2023 QuasarApp.
3+
//# Distributed under the GPLv3 software license, see the accompanying
4+
//# Everyone is permitted to copy and distribute verbatim copies
5+
//# of this license document, but changing it is not allowed.
6+
//#
7+
8+
#include <openssl/types.h>
9+
10+
#include <QByteArray>
11+
namespace EasySSL {
12+
13+
/**
14+
* @brief The EasySSLUtils class These are basic utils for work with the opwnssl library.
15+
*/
16+
class EasySSLUtils {
17+
18+
public:
19+
20+
/**
21+
* @brief printlastOpenSSlError This method prints the latest ssl error message.
22+
*/
23+
static void printlastOpenSSlError();
24+
25+
/**
26+
* @brief bignumToArray This method converts openssl BIGNUM into byteArray
27+
* @param num This is a big num of the openssl library
28+
* @return bytes array.
29+
*/
30+
static QByteArray bignumToArray(const BIGNUM* num);
31+
32+
/**
33+
* @brief bignumFromArray This method converts the Qt bytes array into the opensll big num.
34+
* @param array This is an input array.
35+
* @return big num pointer.
36+
* @note This result pointer will not be free automatically. Please free the returned pointer after use.
37+
*/
38+
[[nodiscard("The result pointer will not be free automatically. Please free the returned pointer after using.")]]
39+
static BIGNUM* bignumFromArray(const QByteArray& array);
40+
41+
/**
42+
* @brief bioToByteArray This method converts the openssl BIO to the QByteArry
43+
* @param bio input arrary.
44+
* @return Qt Array
45+
*/
46+
static QByteArray bioToByteArray(BIO *bio);
47+
48+
/**
49+
* @brief byteArrayToBio This method creates the BIO struct from the Qt QByteArray object.
50+
* @param byteArray This is an input Qt byte array.
51+
* @return pointer to the BIO struct of OpenSLL library.
52+
* @note Don't forget to free the result pointer.
53+
*/
54+
[[nodiscard("This pointer will not free automatically. Please free returned pointer after using.")]]
55+
static BIO *byteArrayToBio(const QByteArray &byteArray);
56+
57+
/**
58+
* @brief extractPublcKey This method extracts the public key from the ssl (pem) structure.
59+
* @param ssl_keys These are objects of the ssl keys.
60+
* @return bytes array of the extracted key.
61+
*/
62+
static QByteArray extractPublcKey(EVP_PKEY* ssl_keys);
63+
64+
/**
65+
* @brief extractPrivateKey This method extracts the private key from the ssl (pem) structure.
66+
* @param ssl_keys These are objects of the ssl keys.
67+
* @return bytes array of the extracted key.
68+
*/
69+
static QByteArray extractPrivateKey(EVP_PKEY* ssl_keys);
70+
71+
};
72+
73+
74+
75+
};

src/lib/src/public/easyssl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace EasySSL {
1111

1212
bool init() {
13-
initeasysslResources();
1413
return true;
1514
}
1615

src/lib/src/public/easyssl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "easyssl/global.h"
99
#include <QString>
1010

11-
inline void initeasysslResources() { Q_INIT_RESOURCE(easyssl); }
1211

1312
namespace EasySSL {
1413

0 commit comments

Comments
 (0)