11#include " smime.h"
2- # include " mimepart_p.h "
2+
33#include " mimemultipart.h"
4+ #include " mimepart_p.h"
5+
6+ #include < openssl/bio.h>
7+ #include < openssl/err.h>
8+ #include < openssl/pem.h>
9+ #include < openssl/provider.h>
410
511#include < QBuffer>
612#include < QFile>
713#include < QLoggingCategory>
814
9- #include < openssl/pem.h>
10- #include < openssl/err.h>
11- #include < openssl/bio.h>
12- #include < openssl/provider.h>
13-
1415Q_LOGGING_CATEGORY (SIMPLEMAIL_SMIME, " simplemail.smime" , QtInfoMsg)
1516
1617using namespace SimpleMail;
1718
1819SMime::SMime (MimeMessage *message)
19- : _privateKey(nullptr ), _certificate(nullptr ), _certificateCA(nullptr ), _recipsReceiver(nullptr ), _input(nullptr )
20+ : _privateKey(nullptr )
21+ , _certificate(nullptr )
22+ , _certificateCA(nullptr )
23+ , _recipsReceiver(nullptr )
24+ , _input(nullptr )
2025{
2126 initOpenSSL ();
2227 _mimeMessage = message;
@@ -35,7 +40,7 @@ SMime::~SMime()
3540
3641void SMime::setKeyFile (const char *filename, const char *password)
3742{
38- _keyfile = QString::fromLatin1 (filename);
43+ _keyfile = QString::fromLatin1 (filename);
3944 _password = QString::fromLatin1 (password);
4045 loadPKCS12PrivateKey ();
4146}
@@ -48,7 +53,7 @@ void SMime::setPublicKey(const char *filename)
4853
4954bool SMime::sign ()
5055{
51- bool ret = false ;
56+ bool ret = false ;
5257 PKCS7 *p7 = nullptr ;
5358
5459 if (!_certificate || !_privateKey) {
@@ -76,10 +81,14 @@ bool SMime::sign()
7681 goto err;
7782
7883 /* Write out S/MIME message */
79- if (!SMIME_write_PKCS7 (out, p7, _input, flags | PKCS7_CRLFEOL)) // needed for intializing/finalizing SMIME structure
80- goto err;
84+ if (!SMIME_write_PKCS7 (out,
85+ p7,
86+ _input,
87+ flags |
88+ PKCS7_CRLFEOL)) // needed for intializing/finalizing SMIME structure
89+ goto err;
8190
82- if (!handleData (p7, nullptr , 0 ))
91+ if (!handleData (p7, nullptr , 0 ))
8392 goto err;
8493
8594 _mimeMessage->addPart (std::shared_ptr<SMime>(this ));
@@ -96,7 +105,7 @@ bool SMime::sign()
96105
97106bool SMime::encrypt ()
98107{
99- bool ret = false ;
108+ bool ret = false ;
100109 PKCS7 *p7 = nullptr ;
101110
102111 if (!_recipsReceiver) {
@@ -108,7 +117,7 @@ bool SMime::encrypt()
108117
109118 int flags = PKCS7_STREAM;
110119
111- if (!writeInputBuffer ())
120+ if (!writeInputBuffer ())
112121 goto err;
113122
114123 /* encrypt content */
@@ -117,13 +126,13 @@ bool SMime::encrypt()
117126 if (!p7)
118127 goto err;
119128
120- if (!handleData (p7, _input, flags))
129+ if (!handleData (p7, _input, flags))
121130 goto err;
122131
123132 _mimeMessage->setContent (std::shared_ptr<SMime>(this ));
124133
125134 ret = true ;
126- err:
135+ err:
127136 if (!ret) {
128137 qCDebug (SIMPLEMAIL_SMIME) << " Error Encrypting Data" ;
129138 }
@@ -133,8 +142,8 @@ bool SMime::encrypt()
133142
134143bool SMime::signAndEncrypt ()
135144{
136- bool ret = false ;
137- PKCS7 *p7 = nullptr ;
145+ bool ret = false ;
146+ PKCS7 *p7 = nullptr ;
138147 BIO *signedContent = nullptr ;
139148 if (!_certificate || !_privateKey) {
140149 qCDebug (SIMPLEMAIL_SMIME) << " no certificate or private key" ;
@@ -149,12 +158,12 @@ bool SMime::signAndEncrypt()
149158
150159 int flags = PKCS7_STREAM;
151160
152- if (!writeInputBuffer ())
161+ if (!writeInputBuffer ())
153162 goto err;
154163
155164 /* Sign content */
156165 p7 = PKCS7_sign (_certificate, _privateKey, NULL , _input, flags);
157- if (!p7)
166+ if (!p7)
158167 goto err;
159168
160169 signedContent = BIO_new (BIO_s_mem ());
@@ -168,26 +177,26 @@ bool SMime::signAndEncrypt()
168177 if (!p7)
169178 goto err;
170179
171- if (!handleData (p7, signedContent, flags))
180+ if (!handleData (p7, signedContent, flags))
172181 goto err;
173182
174183 _mimeMessage->setContent (std::shared_ptr<SMime>(this ));
175184
176185 ret = true ;
177186err:
178- if (!ret) {
179- qCDebug (SIMPLEMAIL_SMIME) << " Error Signing/Encrypting Data" ;
180- }
181- PKCS7_free (p7);
182- BIO_free (signedContent);
183- return ret;
187+ if (!ret) {
188+ qCDebug (SIMPLEMAIL_SMIME) << " Error Signing/Encrypting Data" ;
189+ }
190+ PKCS7_free (p7);
191+ BIO_free (signedContent);
192+ return ret;
184193}
185194
186195void SMime::setSignedHeader ()
187196{
188197 Q_D (MimePart);
189- d->contentType = QByteArrayLiteral (" application/pkcs7-signature; name=\" smime.p7s\" " );
190- d->contentCharset = " " ;
198+ d->contentType = QByteArrayLiteral (" application/pkcs7-signature; name=\" smime.p7s\" " );
199+ d->contentCharset = " " ;
191200 d->contentEncoding = Base64;
192201 d->contentIsBase64 = true ;
193202
@@ -197,8 +206,9 @@ void SMime::setSignedHeader()
197206void SMime::setEncryptionHeader ()
198207{
199208 Q_D (MimePart);
200- d->contentType = QByteArrayLiteral (" application/x-pkcs7-mime; smime-type=enveloped-data; name=\" smime.p7m\" " );
201- d->contentCharset = " " ;
209+ d->contentType = QByteArrayLiteral (
210+ " application/x-pkcs7-mime; smime-type=enveloped-data; name=\" smime.p7m\" " );
211+ d->contentCharset = " " ;
202212 d->contentEncoding = Base64;
203213 d->contentIsBase64 = true ;
204214
@@ -208,7 +218,7 @@ void SMime::setEncryptionHeader()
208218void SMime::loadPKCS12PrivateKey ()
209219{
210220 QFile file (QString::fromLatin1 (_keyfile.toStdString ().c_str ()));
211- if (!file.exists ())
221+ if (!file.exists ())
212222 return ;
213223 file.open (QFile::ReadOnly);
214224 QByteArray buffer = file.readAll ();
@@ -220,19 +230,23 @@ void SMime::loadPKCS12PrivateKey()
220230 return ;
221231 }
222232
223- BIO_write (keyBuffer, (void *) buffer.data (), buffer.length ());
233+ BIO_write (keyBuffer, (void *) buffer.data (), buffer.length ());
224234 PKCS12 *p12 = d2i_PKCS12_bio (keyBuffer, NULL );
225235 BIO_free (keyBuffer);
226236
227237 if (p12 == NULL ) {
228238 qCDebug (SIMPLEMAIL_SMIME) << " Error reading PKCS#12 file" ;
229239 }
230240
231- if (!PKCS12_parse (p12, _password.toStdString ().c_str (), &_privateKey, &_certificate, &_certificateCA)) {
241+ if (!PKCS12_parse (
242+ p12, _password.toStdString ().c_str (), &_privateKey, &_certificate, &_certificateCA)) {
232243 OSSL_PROVIDER_try_load (nullptr , " legacy" , 1 );
233244
234- if (!PKCS12_parse (p12, _password.toStdString ().c_str (), &_privateKey, &_certificate, &_certificateCA))
235- {
245+ if (!PKCS12_parse (p12,
246+ _password.toStdString ().c_str (),
247+ &_privateKey,
248+ &_certificate,
249+ &_certificateCA)) {
236250 qCDebug (SIMPLEMAIL_SMIME) << " Error parsing PKCS#12 file" ;
237251 }
238252 }
@@ -243,7 +257,7 @@ void SMime::loadPKCS12PrivateKey()
243257void SMime::loadPKCS12PublicKey ()
244258{
245259 QFile file (QString::fromLatin1 (_publicKeyfile.toStdString ().c_str ()));
246- if (!file.exists ())
260+ if (!file.exists ())
247261 return ;
248262 file.open (QFile::ReadOnly);
249263 QByteArray buffer = file.readAll ();
@@ -254,9 +268,9 @@ void SMime::loadPKCS12PublicKey()
254268 qCDebug (SIMPLEMAIL_SMIME) << " Error opening file " << _publicKeyfile;
255269 return ;
256270 }
257- BIO_write (keyBuffer, (void *) buffer.data (), buffer.length ());
271+ BIO_write (keyBuffer, (void *) buffer.data (), buffer.length ());
258272
259- X509* publicrcert = PEM_read_bio_X509 (keyBuffer, NULL , 0 , NULL );
273+ X509 * publicrcert = PEM_read_bio_X509 (keyBuffer, NULL , 0 , NULL );
260274 BIO_free (keyBuffer);
261275
262276 _recipsReceiver = sk_X509_new_null ();
@@ -289,7 +303,7 @@ bool SMime::writeInputBuffer()
289303 _input = BIO_new (BIO_s_mem ());
290304 if (!_input)
291305 return false ;
292- if (!BIO_write (_input, (void *) _message.data (), _message.length ()))
306+ if (!BIO_write (_input, (void *) _message.data (), _message.length ()))
293307 return false ;
294308 return true ;
295309}
@@ -312,27 +326,25 @@ bool SMime::handleData(PKCS7 *p7, BIO *dataIn, int flags)
312326{
313327 QByteArray contentBuffer;
314328
315- // Buffer for signed and/or encrypted data;
329+ // Buffer for signed and/or encrypted data;
316330 BIO *encryptedData = BIO_new (BIO_s_mem ());
317331 if (!encryptedData)
318332 return false ;
319333
320334 BIO *b64 = BIO_new (BIO_f_base64 ());
321335 BIO *tmp = BIO_push (b64, encryptedData);
322336
323- i2d_ASN1_bio_stream (tmp, (ASN1_VALUE *)p7, dataIn, flags, ASN1_ITEM_rptr (PKCS7));
337+ i2d_ASN1_bio_stream (tmp, (ASN1_VALUE *) p7, dataIn, flags, ASN1_ITEM_rptr (PKCS7));
324338
325339 BIO_flush (tmp);
326340 BIO_pop (tmp);
327341 BIO_free (b64);
328342
329343 char buffer[1024 ];
330- while (BIO_get_line (encryptedData, &buffer[0 ], sizeof (buffer)))
331- {
344+ while (BIO_get_line (encryptedData, &buffer[0 ], sizeof (buffer))) {
332345 qCDebug (SIMPLEMAIL_SMIME) << QString::fromLatin1 (buffer);
333- if (strncmp (&buffer[0 ], " -----" , strlen (" -----" ))!=0 )
334- {
335- contentBuffer += QByteArray (&buffer[0 ], strlen (buffer)-1 );
346+ if (strncmp (&buffer[0 ], " -----" , strlen (" -----" )) != 0 ) {
347+ contentBuffer += QByteArray (&buffer[0 ], strlen (buffer) - 1 );
336348 contentBuffer += QByteArray (" \r\n " );
337349 }
338350 }
0 commit comments