Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 22, 2024
1 parent df94c4a commit b620062
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 68 deletions.
13 changes: 5 additions & 8 deletions demos/demo5/demo5.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <QtCore>

#include "../../src/SimpleMail"

#include <QtCore>

using namespace SimpleMail;

int main(int argc, char *argv[])
Expand All @@ -24,7 +24,6 @@ int main(int argc, char *argv[])
server.setUsername(QLatin1String("[email protected]"));
server.setPassword(QLatin1String("your_password"));


// Now we create a MimeMessage object. This is the email.

MimeMessage message;
Expand All @@ -50,10 +49,10 @@ int main(int argc, char *argv[])

// Add an attachment

auto document = std::make_shared<MimeAttachment>(std::make_shared<QFile>(QLatin1String("document.pdf")));
auto document =
std::make_shared<MimeAttachment>(std::make_shared<QFile>(QLatin1String("document.pdf")));
message.addPart(document);


// Now create an SMime object

auto smime = new SimpleMail::SMime(&message);
Expand All @@ -63,15 +62,13 @@ int main(int argc, char *argv[])
smime->setKeyFile("your_private_key.p12", "your_private_key_password");
smime->setPublicKey("recipient_public_key.cert");


// Sign the message. Only your private key is required.
// if(!smime->sign()) {
// qDebug() << "Failed to create signed email";
// delete smime;
// return -3;
// }


// Encrypt the message. Only the recipient's public key/certificate is required.
// if(!smime->encrypt()) {
// qDebug() << "Failed to create encrypted email";
Expand All @@ -80,7 +77,7 @@ int main(int argc, char *argv[])
// }

// Sign and encrypt the message
if(!smime->signAndEncrypt()) {
if (!smime->signAndEncrypt()) {
qDebug() << "Failed to create signed and encrypted email";
delete smime;
return -3;
Expand Down
10 changes: 4 additions & 6 deletions src/mimepart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ MimePart::MimePart()
{
}

MimePart::MimePart(const MimePart &other) : d_ptr(new MimePartPrivate)
MimePart::MimePart(const MimePart &other)
: d_ptr(new MimePartPrivate)
{
Q_D(MimePart);
d->contentCharset = other.charset();
Expand Down Expand Up @@ -314,14 +315,11 @@ bool MimePart::writeData(QIODevice *device)
}
break;
case MimePart::Base64:
if(!d->contentIsBase64)
{
if (!d->contentIsBase64) {
if (!d->writeBase64(input, device)) {
return false;
}
}
else
{
} else {
if (!d->writeRaw(input, device)) {
return false;
}
Expand Down
106 changes: 59 additions & 47 deletions src/smime.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#include "smime.h"
#include "mimepart_p.h"

#include "mimemultipart.h"
#include "mimepart_p.h"

#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/provider.h>

#include <QBuffer>
#include <QFile>
#include <QLoggingCategory>

#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/provider.h>

Q_LOGGING_CATEGORY(SIMPLEMAIL_SMIME, "simplemail.smime", QtInfoMsg)

using namespace SimpleMail;

SMime::SMime(MimeMessage *message)
: _privateKey(nullptr), _certificate(nullptr), _certificateCA(nullptr), _recipsReceiver(nullptr), _input(nullptr)
: _privateKey(nullptr)
, _certificate(nullptr)
, _certificateCA(nullptr)
, _recipsReceiver(nullptr)
, _input(nullptr)
{
initOpenSSL();
_mimeMessage = message;
Expand All @@ -35,7 +40,7 @@ SMime::~SMime()

void SMime::setKeyFile(const char *filename, const char *password)
{
_keyfile = QString::fromLatin1(filename);
_keyfile = QString::fromLatin1(filename);
_password = QString::fromLatin1(password);
loadPKCS12PrivateKey();
}
Expand All @@ -48,7 +53,7 @@ void SMime::setPublicKey(const char *filename)

bool SMime::sign()
{
bool ret = false;
bool ret = false;
PKCS7 *p7 = nullptr;

if (!_certificate || !_privateKey) {
Expand Down Expand Up @@ -76,10 +81,14 @@ bool SMime::sign()
goto err;

/* Write out S/MIME message */
if (!SMIME_write_PKCS7(out, p7, _input, flags | PKCS7_CRLFEOL)) // needed for intializing/finalizing SMIME structure
goto err;
if (!SMIME_write_PKCS7(out,
p7,
_input,
flags |
PKCS7_CRLFEOL)) // needed for intializing/finalizing SMIME structure
goto err;

if(!handleData(p7, nullptr, 0))
if (!handleData(p7, nullptr, 0))
goto err;

_mimeMessage->addPart(std::shared_ptr<SMime>(this));
Expand All @@ -96,7 +105,7 @@ bool SMime::sign()

bool SMime::encrypt()
{
bool ret = false;
bool ret = false;
PKCS7 *p7 = nullptr;

if (!_recipsReceiver) {
Expand All @@ -108,7 +117,7 @@ bool SMime::encrypt()

int flags = PKCS7_STREAM;

if(!writeInputBuffer())
if (!writeInputBuffer())
goto err;

/* encrypt content */
Expand All @@ -117,13 +126,13 @@ bool SMime::encrypt()
if (!p7)
goto err;

if(!handleData(p7, _input, flags))
if (!handleData(p7, _input, flags))
goto err;

_mimeMessage->setContent(std::shared_ptr<SMime>(this));

ret = true;
err:
err:
if (!ret) {
qCDebug(SIMPLEMAIL_SMIME) << "Error Encrypting Data";
}
Expand All @@ -133,8 +142,8 @@ bool SMime::encrypt()

bool SMime::signAndEncrypt()
{
bool ret = false;
PKCS7 *p7 = nullptr;
bool ret = false;
PKCS7 *p7 = nullptr;
BIO *signedContent = nullptr;
if (!_certificate || !_privateKey) {
qCDebug(SIMPLEMAIL_SMIME) << "no certificate or private key";
Expand All @@ -149,12 +158,12 @@ bool SMime::signAndEncrypt()

int flags = PKCS7_STREAM;

if(!writeInputBuffer())
if (!writeInputBuffer())
goto err;

/* Sign content */
p7 = PKCS7_sign(_certificate, _privateKey, NULL, _input, flags);
if(!p7)
if (!p7)
goto err;

signedContent = BIO_new(BIO_s_mem());
Expand All @@ -168,26 +177,26 @@ bool SMime::signAndEncrypt()
if (!p7)
goto err;

if(!handleData(p7, signedContent, flags))
if (!handleData(p7, signedContent, flags))
goto err;

_mimeMessage->setContent(std::shared_ptr<SMime>(this));

ret = true;
err:
if (!ret) {
qCDebug(SIMPLEMAIL_SMIME) << "Error Signing/Encrypting Data";
}
PKCS7_free(p7);
BIO_free(signedContent);
return ret;
if (!ret) {
qCDebug(SIMPLEMAIL_SMIME) << "Error Signing/Encrypting Data";
}
PKCS7_free(p7);
BIO_free(signedContent);
return ret;
}

void SMime::setSignedHeader()
{
Q_D(MimePart);
d->contentType = QByteArrayLiteral("application/pkcs7-signature; name=\"smime.p7s\"");
d->contentCharset = "";
d->contentType = QByteArrayLiteral("application/pkcs7-signature; name=\"smime.p7s\"");
d->contentCharset = "";
d->contentEncoding = Base64;
d->contentIsBase64 = true;

Expand All @@ -197,8 +206,9 @@ void SMime::setSignedHeader()
void SMime::setEncryptionHeader()
{
Q_D(MimePart);
d->contentType = QByteArrayLiteral("application/x-pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\"");
d->contentCharset = "";
d->contentType = QByteArrayLiteral(
"application/x-pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\"");
d->contentCharset = "";
d->contentEncoding = Base64;
d->contentIsBase64 = true;

Expand All @@ -208,7 +218,7 @@ void SMime::setEncryptionHeader()
void SMime::loadPKCS12PrivateKey()
{
QFile file(QString::fromLatin1(_keyfile.toStdString().c_str()));
if(!file.exists())
if (!file.exists())
return;
file.open(QFile::ReadOnly);
QByteArray buffer = file.readAll();
Expand All @@ -220,19 +230,23 @@ void SMime::loadPKCS12PrivateKey()
return;
}

BIO_write(keyBuffer, (void*)buffer.data(), buffer.length());
BIO_write(keyBuffer, (void *) buffer.data(), buffer.length());
PKCS12 *p12 = d2i_PKCS12_bio(keyBuffer, NULL);
BIO_free(keyBuffer);

if (p12 == NULL) {
qCDebug(SIMPLEMAIL_SMIME) << "Error reading PKCS#12 file";
}

if (!PKCS12_parse(p12, _password.toStdString().c_str(), &_privateKey, &_certificate, &_certificateCA)) {
if (!PKCS12_parse(
p12, _password.toStdString().c_str(), &_privateKey, &_certificate, &_certificateCA)) {
OSSL_PROVIDER_try_load(nullptr, "legacy", 1);

if (!PKCS12_parse(p12, _password.toStdString().c_str(), &_privateKey, &_certificate, &_certificateCA))
{
if (!PKCS12_parse(p12,
_password.toStdString().c_str(),
&_privateKey,
&_certificate,
&_certificateCA)) {
qCDebug(SIMPLEMAIL_SMIME) << "Error parsing PKCS#12 file";
}
}
Expand All @@ -243,7 +257,7 @@ void SMime::loadPKCS12PrivateKey()
void SMime::loadPKCS12PublicKey()
{
QFile file(QString::fromLatin1(_publicKeyfile.toStdString().c_str()));
if(!file.exists())
if (!file.exists())
return;
file.open(QFile::ReadOnly);
QByteArray buffer = file.readAll();
Expand All @@ -254,9 +268,9 @@ void SMime::loadPKCS12PublicKey()
qCDebug(SIMPLEMAIL_SMIME) << "Error opening file " << _publicKeyfile;
return;
}
BIO_write(keyBuffer, (void*)buffer.data(), buffer.length());
BIO_write(keyBuffer, (void *) buffer.data(), buffer.length());

X509* publicrcert = PEM_read_bio_X509(keyBuffer, NULL, 0, NULL);
X509 *publicrcert = PEM_read_bio_X509(keyBuffer, NULL, 0, NULL);
BIO_free(keyBuffer);

_recipsReceiver = sk_X509_new_null();
Expand Down Expand Up @@ -289,7 +303,7 @@ bool SMime::writeInputBuffer()
_input = BIO_new(BIO_s_mem());
if (!_input)
return false;
if (!BIO_write(_input, (void*)_message.data(), _message.length()))
if (!BIO_write(_input, (void *) _message.data(), _message.length()))
return false;
return true;
}
Expand All @@ -312,27 +326,25 @@ bool SMime::handleData(PKCS7 *p7, BIO *dataIn, int flags)
{
QByteArray contentBuffer;

//Buffer for signed and/or encrypted data;
// Buffer for signed and/or encrypted data;
BIO *encryptedData = BIO_new(BIO_s_mem());
if (!encryptedData)
return false;

BIO *b64 = BIO_new(BIO_f_base64());
BIO *tmp = BIO_push(b64, encryptedData);

i2d_ASN1_bio_stream(tmp, (ASN1_VALUE *)p7, dataIn, flags, ASN1_ITEM_rptr(PKCS7));
i2d_ASN1_bio_stream(tmp, (ASN1_VALUE *) p7, dataIn, flags, ASN1_ITEM_rptr(PKCS7));

BIO_flush(tmp);
BIO_pop(tmp);
BIO_free(b64);

char buffer[1024];
while(BIO_get_line(encryptedData, &buffer[0], sizeof(buffer)))
{
while (BIO_get_line(encryptedData, &buffer[0], sizeof(buffer))) {
qCDebug(SIMPLEMAIL_SMIME) << QString::fromLatin1(buffer);
if(strncmp(&buffer[0], "-----", strlen("-----"))!=0)
{
contentBuffer += QByteArray(&buffer[0], strlen(buffer)-1);
if (strncmp(&buffer[0], "-----", strlen("-----")) != 0) {
contentBuffer += QByteArray(&buffer[0], strlen(buffer) - 1);
contentBuffer += QByteArray("\r\n");
}
}
Expand Down
Loading

0 comments on commit b620062

Please sign in to comment.