easyssl 0.91.3c571d2
EasySSL is base back end library for your c++ Qt projects.
x509.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 "x509.h"
9#include <openssl/evp.h>
10#include <openssl/pem.h>
11#include <openssl/x509.h>
12#include <easysslutils.h>
13namespace EasySSL {
14
15X509::X509(const QSharedPointer<ICrypto>& generator): ICertificate(generator) {
16
17}
18
19SelfSignedSertificate X509::create(const SslSrtData &certificateData) const {
21 if (!(keyGenerator()->supportedFeatures() & ICrypto::Features::Signing)) {
22 return {};
23 }
24
25 EVP_PKEY *pkey = static_cast<EVP_PKEY *>(keyGenerator()->makeRawKeys());
26
27 ::X509 * x509 = nullptr;
28 X509_NAME * name = nullptr;
29
30 x509 = X509_new();
31 q_check_ptr(x509);
32 ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
33 X509_gmtime_adj(X509_get_notBefore(x509), 0); // not before current time
34 X509_gmtime_adj(X509_get_notAfter(x509), certificateData.endTime); // not after a year from this point
35 X509_set_pubkey(x509, pkey);
36
37 // The subject name of the certificate is filled with a separate
38 // X509_NAME object: the X509_get_subject_name method returns a const
39 // pointer in the OpenSSL 4.x releases, so the name of the certificate
40 // can not be changed in place. The X509_set_subject_name and
41 // X509_set_issuer_name methods copy the name, so it can be freed
42 // after the calls.
43 name = X509_NAME_new();
44 q_check_ptr(name);
45
46 unsigned char *C = reinterpret_cast<unsigned char *>(certificateData.country.toLatin1().data());
47 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, C, -1, -1, 0);
48
49 unsigned char *O = reinterpret_cast<unsigned char *>(certificateData.organization.toLatin1().data());
50 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, O, -1, -1, 0);
51
52 unsigned char *CN = reinterpret_cast<unsigned char *>(certificateData.commonName.toLatin1().data());
53 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, CN, -1, -1, 0);
54
55 X509_set_subject_name(x509, name);
56 X509_set_issuer_name(x509, name); // the issuer of the self-signed certificate is the same as the subject
57 X509_NAME_free(name);
58 X509_sign(x509, pkey, EVP_sha256());
59
60 result.key = QSslKey(EasySSLUtils::extractPrivateKey(pkey), keyGenerator()->keyAlgorithm());
61 if(result.key.isNull()) {
62 EVP_PKEY_free(pkey);
63 X509_free(x509);
64 qCritical("Failed to generate a random private key");
65 return {};
66 }
67 EVP_PKEY_free(pkey);
68
69 BIO * bp_public = BIO_new(BIO_s_mem());
70 q_check_ptr(bp_public);
71 if(PEM_write_bio_X509(bp_public, x509) != 1){
72 X509_free(x509);
73 BIO_free_all(bp_public);
74 qCritical("PEM_write_bio_PrivateKey");
75 return {};
76 }
77
78 result.crt = QSslCertificate(EasySSLUtils::bioToByteArray(bp_public));
79 if(result.crt.isNull()) {
80 X509_free(x509);
81 BIO_free_all(bp_public);
82 qCritical("Failed to generate a random client certificate");
83 return {};
84 }
85
86 X509_free(x509);
87 BIO_free_all(bp_public);
88
89 return result;
90}
91
92}
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.
The ICertificate class is base interface for all certificate generators classes.
const QSharedPointer< ICrypto > & keyGenerator() const
generator This method return private key generator.
@ Signing
Signin and check sign of the data.
Definition icrypto.h:31
X509(const QSharedPointer< ICrypto > &generator)
Definition x509.cpp:15
SelfSignedSertificate create(const SslSrtData &certificateData) const override
create This method create a self signed certificate.
Definition x509.cpp:19
The SelfSignedSertificate struct contains qt certificate object and private key of them.
The SslSrtData struct This structure contains base information to generate self-signed SSL certificat...