easyssl 0.50.142aaef
EasySSL is base back end library for your c++ Qt projects.
easysslutils.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2021-2025 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
15namespace EasySSL {
16
17
19 ERR_print_errors_fp(stderr);
20}
21
22QByteArray 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
31BIGNUM *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) {
37 }
38
39 return result;
40}
41
42QByteArray 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
54BIO* EasySSLUtils::byteArrayToBio(const QByteArray& byteArray) {
55 BIO* bio = BIO_new_mem_buf(byteArray.constData(), byteArray.length());
56 return bio;
57}
58
59QByteArray 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
75QByteArray 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}
static BIO * byteArrayToBio(const QByteArray &byteArray)
byteArrayToBio This method creates the BIO struct from the Qt QByteArray object.
static QByteArray bignumToArray(const BIGNUM *num)
bignumToArray This method converts openssl BIGNUM into byteArray
static void printlastOpenSSlError()
printlastOpenSSlError This method prints the latest ssl error message.
static QByteArray extractPublcKey(EVP_PKEY *ssl_keys)
extractPublcKey This method extracts the public key from the ssl (pem) structure.
static BIGNUM * bignumFromArray(const QByteArray &array)
bignumFromArray This method converts the Qt bytes array into the opensll big num.
static QByteArray bioToByteArray(BIO *bio)
bioToByteArray This method converts the openssl BIO to the QByteArry
static QByteArray extractPrivateKey(EVP_PKEY *ssl_keys)
extractPrivateKey This method extracts the private key from the ssl (pem) structure.