1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
|
/*
(c) Copyright 2012 - 2018 Anton Sviridenko
https://picapica.im
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "openssllib.h"
#include "../PICA_security.h"
#include <openssl/asn1.h>
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/dh.h>
#include <QFile>
#include <QRegExp>
#include <QThread>
#define REGEXOLD "CN = [0-9]+\\#([^\\#\\$&\"\'=\\(\\)\\\\/\\|`!<>\\{\\}\\[\\]\\+\r\n]+)"
#define REGEXNEW "CN = ([^\\#\\$&\"\'=\\(\\)\\\\/\\|`!<>\\{\\}\\[\\]\\+\r\n]+)"
/* "-days 3680", as it was spelled on the openssl command line. */
#define PICA_CERT_VALIDITY_DAYS 3680
/*
* The serial number width "openssl req -x509" picks when it is not given one
* (SERIAL_RAND_BITS in apps/lib/apps.c).
*/
#define PICA_CERT_SERIAL_BITS 159
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define X509_getm_notBefore(x) X509_get_notBefore(x)
#define X509_getm_notAfter(x) X509_get_notAfter(x)
#endif
/*
* "oneline,-esc_msb,utf8" and "multiline,-esc_msb,utf8", the two -nameopt
* arguments OpenSSLTool used, worked out the way the command line program's
* option parser works them out: "oneline" and "multiline" clear every other
* bit before setting their own, the two that follow set and clear a single
* bit each.
*/
static const unsigned long ONELINE_NAMEOPT =
(XN_FLAG_ONELINE & ~ASN1_STRFLGS_ESC_MSB) | ASN1_STRFLGS_UTF8_CONVERT;
static const unsigned long MULTILINE_NAMEOPT =
(XN_FLAG_MULTILINE & ~ASN1_STRFLGS_ESC_MSB) | ASN1_STRFLGS_UTF8_CONVERT;
static QString BioToString(BIO *bio)
{
char *data = 0;
long len;
len = BIO_get_mem_data(bio, &data);
if (len <= 0 || !data)
return QString();
return QString::fromUtf8(data, (int)len);
}
static QByteArray BioToByteArray(BIO *bio)
{
char *data = 0;
long len;
len = BIO_get_mem_data(bio, &data);
if (len <= 0 || !data)
return QByteArray();
return QByteArray(data, (int)len);
}
static X509 *CertFromPEM(const QByteArray &cert_pem)
{
BIO *mem;
X509 *cert;
if (cert_pem.isEmpty())
return 0;
mem = BIO_new_mem_buf((void*)cert_pem.constData(), cert_pem.size());
if (!mem)
return 0;
cert = PEM_read_bio_X509(mem, NULL, NULL, NULL);
BIO_free(mem);
ERR_clear_error();
return cert;
}
/*
* The whole file is read through QFile rather than handed to BIO_new_file():
* config_dir can hold characters that do not survive the trip through the
* local 8 bit encoding a C file name would have to be, and Qt already knows
* how to open those paths on every platform we build for.
*/
static QByteArray ReadWholeFile(const QString &name)
{
QFile f(name);
if (!f.open(QIODevice::ReadOnly))
return QByteArray();
return f.readAll();
}
/*
* The name as the "-subject -nameopt oneline,-esc_msb,utf8" output of the
* command line program spelled it, prefix and trailing newline included, so
* that the two regular expressions below see exactly what they used to see.
*/
static QString SubjectLine(X509 *cert)
{
BIO *mem;
QString line;
mem = BIO_new(BIO_s_mem());
if (!mem)
return QString();
BIO_puts(mem, "subject=");
X509_NAME_print_ex(mem, X509_get_subject_name(cert), 0, ONELINE_NAMEOPT);
BIO_puts(mem, "\n");
line = BioToString(mem);
BIO_free(mem);
return line;
}
static QString NameFromSubjectLine(QString subject_line)
{
QRegExp rxOld(REGEXOLD);
QRegExp rx(REGEXNEW);
QString name = subject_line;
if (name.contains(rxOld))
{
name = rxOld.cap(1);
}
else if (name.contains(rx))
{
name = rx.cap(1);
}
return name;
}
/*
* The generating half of the class. RSA key generation takes seconds and DH
* parameter generation at PICA_CLIENT_DHPARAMBITS takes minutes, and the
* thread calling is the one drawing the dialog that shows the progress, so
* the work happens here instead.
*
* No Q_OBJECT: the only signal needed is QThread::finished(), which QThread
* already declares.
*/
class OpenSSLLibWorker : public QThread
{
public:
enum Job
{
JobGenRSAKey,
JobGenCert,
JobGenDHParam
};
OpenSSLLibWorker(Job job, QObject *parent)
: QThread(parent), setpassword_(false), numbits_(0), job_(job), retval_(1)
{
}
/* Set by the caller before start(), read by it after finished(). */
QString keyfile_;
QString certfile_;
QString outfile_;
QString password_;
QString rand_;
QString subject_;
bool setpassword_;
quint32 numbits_;
int retval() const
{
return retval_;
}
QString errors() const
{
return errors_;
}
protected:
void run();
private:
void RunGenRSAKey();
void RunGenCert();
void RunGenDHParam();
bool WriteFile(const QString &name, const QByteArray &data);
void Fail(const char *what);
Job job_;
QString errors_;
int retval_;
};
void OpenSSLLibWorker::Fail(const char *what)
{
BIO *mem = BIO_new(BIO_s_mem());
errors_ += QString::fromLatin1(what);
errors_ += QLatin1String("\n");
if (mem)
{
ERR_print_errors(mem);
errors_ += BioToString(mem);
BIO_free(mem);
}
ERR_clear_error();
retval_ = 1;
}
bool OpenSSLLibWorker::WriteFile(const QString &name, const QByteArray &data)
{
QFile f(name);
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
errors_ += QString("Failed to open %1 for writing: %2\n").arg(name).arg(f.errorString());
retval_ = 1;
return false;
}
if (f.write(data) != data.size() || !f.flush())
{
errors_ += QString("Failed to write %1: %2\n").arg(name).arg(f.errorString());
retval_ = 1;
return false;
}
return true;
}
void OpenSSLLibWorker::run()
{
retval_ = 1;
switch (job_)
{
case JobGenRSAKey:
RunGenRSAKey();
break;
case JobGenCert:
RunGenCert();
break;
case JobGenDHParam:
RunGenDHParam();
break;
}
}
void OpenSSLLibWorker::RunGenRSAKey()
{
EVP_PKEY_CTX *ctx = 0;
EVP_PKEY *pkey = 0;
BIO *mem = 0;
const EVP_CIPHER *cipher = 0;
QByteArray password;
unsigned char rndchar;
quint32 numbits;
/*
* Generate random-sized key with min size 4096 and multiply of 8.
* Max size is limited to 4096 + 99*8.
* See https://blog.josefsson.org/2016/11/03/why-i-dont-use-2048-or-4096-rsa-key-sizes/
* "Why I don’t Use 2048 or 4096 RSA Key Sizes"
*/
numbits = PICA_RSA_MINKEYSIZE;
if (RAND_bytes(&rndchar, 1) != 1)
{
Fail("Random number generator failure");
return;
}
numbits += 8 * (rndchar % 100);
/*
* What "-rand <file>" did: an extra seeding source, /dev/random when the
* account dialog offers it and the user wants it. RAND_load_file() reads
* a small fixed amount from anything that is not a regular file, so this
* does not sit on the device draining it.
*/
if (!rand_.isEmpty())
{
if (RAND_load_file(QFile::encodeName(rand_).constData(), -1) <= 0)
{
Fail("Failed to seed the random number generator from the requested source");
return;
}
}
if (setpassword_)
{
cipher = EVP_get_cipherbyname(PICA_PRIVKEYENCALGO + 1); /* skip the leading '-' */
if (!cipher)
{
Fail("Unknown private key encryption algorithm " PICA_PRIVKEYENCALGO);
return;
}
/*
* UTF-8 rather than the Latin-1 OpenSSLTool wrote down the pipe:
* AskPassword hands the passphrase to OpenSSL as UTF-8 when the
* account is opened later, so anything outside ASCII produced a key
* that could be created but never unlocked again.
*/
password = password_.toUtf8();
}
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (!ctx)
{
Fail("Failed to create an RSA key generation context");
goto end;
}
if (EVP_PKEY_keygen_init(ctx) <= 0)
{
Fail("Failed to initialize RSA key generation");
goto end;
}
if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, (int)numbits) <= 0)
{
Fail("Failed to set the RSA key size");
goto end;
}
if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
{
Fail("Failed to generate the RSA keypair");
goto end;
}
mem = BIO_new(BIO_s_mem());
if (!mem)
{
Fail("Out of memory");
goto end;
}
if (!PEM_write_bio_PrivateKey(mem, pkey, cipher, NULL, 0, NULL,
setpassword_ ? (void*)password.constData() : NULL))
{
Fail("Failed to encode the private key");
goto end;
}
if (WriteFile(keyfile_, BioToByteArray(mem)))
retval_ = 0;
end:
if (mem)
BIO_free(mem);
if (pkey)
EVP_PKEY_free(pkey);
if (ctx)
EVP_PKEY_CTX_free(ctx);
}
void OpenSSLLibWorker::RunGenCert()
{
EVP_PKEY *pkey = 0;
X509 *cert = 0;
X509_NAME *name = 0;
BIGNUM *serial = 0;
BIO *keymem = 0;
BIO *mem = 0;
const EVP_MD *digest;
X509V3_CTX extctx;
QByteArray keydata;
QByteArray password;
QByteArray subject;
/*
* The extensions "openssl req -x509" picked up from the [ v3_ca ] section
* of openssl.cnf. Spelled out here so that a certificate no longer
* depends on which openssl.cnf the machine happens to have - the Windows
* build used to have to ship one of its own next to the executable.
*
* Order matters: "keyid:always" reads the subject key identifier of the
* issuer, which for a self signed certificate is the extension added just
* above it.
*/
static const struct
{
int nid;
const char *value;
} extensions[] =
{
{ NID_subject_key_identifier, "hash" },
{ NID_authority_key_identifier, "keyid:always,issuer" },
{ NID_basic_constraints, "critical,CA:true" }
};
unsigned int i;
digest = EVP_get_digestbyname(PICA_CERTDIGESTALGO + 1); /* skip the leading '-' */
if (!digest)
{
Fail("Unknown certificate digest algorithm " PICA_CERTDIGESTALGO);
return;
}
keydata = ReadWholeFile(keyfile_);
if (keydata.isEmpty())
{
errors_ += QString("Failed to read the private key file %1\n").arg(keyfile_);
retval_ = 1;
return;
}
keymem = BIO_new_mem_buf((void*)keydata.constData(), keydata.size());
if (!keymem)
{
Fail("Out of memory");
goto end;
}
password = password_.toUtf8();
pkey = PEM_read_bio_PrivateKey(keymem, NULL, NULL, (void*)password.constData());
if (!pkey)
{
Fail("Failed to read the private key");
goto end;
}
cert = X509_new();
if (!cert)
{
Fail("Out of memory");
goto end;
}
if (!X509_set_version(cert, 2)) /* X.509 v3 */
{
Fail("Failed to set the certificate version");
goto end;
}
serial = BN_new();
if (!serial
|| !BN_rand(serial, PICA_CERT_SERIAL_BITS, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)
|| !BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(cert)))
{
Fail("Failed to generate the certificate serial number");
goto end;
}
name = X509_NAME_new();
subject = subject_.toUtf8();
if (!name
|| !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
(unsigned char*)subject.data(), subject.size(), -1, 0))
{
Fail("Failed to build the certificate subject name");
goto end;
}
if (!X509_set_subject_name(cert, name) || !X509_set_issuer_name(cert, name))
{
Fail("Failed to set the certificate subject name");
goto end;
}
if (!X509_gmtime_adj(X509_getm_notBefore(cert), 0)
|| !X509_time_adj_ex(X509_getm_notAfter(cert), PICA_CERT_VALIDITY_DAYS, 0, NULL))
{
Fail("Failed to set the certificate validity period");
goto end;
}
if (!X509_set_pubkey(cert, pkey))
{
Fail("Failed to set the certificate public key");
goto end;
}
X509V3_set_ctx_nodb(&extctx);
X509V3_set_ctx(&extctx, cert, cert, NULL, NULL, 0);
for (i = 0; i < sizeof(extensions) / sizeof(extensions[0]); i++)
{
X509_EXTENSION *ext;
QByteArray value(extensions[i].value);
ext = X509V3_EXT_conf_nid(NULL, &extctx, extensions[i].nid, value.data());
if (!ext)
{
Fail("Failed to build a certificate extension");
goto end;
}
if (!X509_add_ext(cert, ext, -1))
{
X509_EXTENSION_free(ext);
Fail("Failed to add a certificate extension");
goto end;
}
X509_EXTENSION_free(ext);
}
if (!X509_sign(cert, pkey, digest))
{
Fail("Failed to sign the certificate");
goto end;
}
mem = BIO_new(BIO_s_mem());
if (!mem)
{
Fail("Out of memory");
goto end;
}
if (!PEM_write_bio_X509(mem, cert))
{
Fail("Failed to encode the certificate");
goto end;
}
if (WriteFile(certfile_, BioToByteArray(mem)))
retval_ = 0;
end:
if (mem)
BIO_free(mem);
if (name)
X509_NAME_free(name);
if (serial)
BN_free(serial);
if (cert)
X509_free(cert);
if (pkey)
EVP_PKEY_free(pkey);
if (keymem)
BIO_free(keymem);
}
void OpenSSLLibWorker::RunGenDHParam()
{
EVP_PKEY_CTX *ctx = 0;
EVP_PKEY *pkey = 0;
BIO *mem = 0;
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
if (!ctx)
{
Fail("Failed to create a Diffie-Hellman parameter generation context");
goto end;
}
if (EVP_PKEY_paramgen_init(ctx) <= 0)
{
Fail("Failed to initialize Diffie-Hellman parameter generation");
goto end;
}
if (EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, (int)numbits_) <= 0)
{
Fail("Failed to set the Diffie-Hellman prime length");
goto end;
}
/* "-5", the generator OpenSSLTool asked the command line program for. */
if (EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, 5) <= 0)
{
Fail("Failed to set the Diffie-Hellman generator");
goto end;
}
if (EVP_PKEY_paramgen(ctx, &pkey) <= 0)
{
Fail("Failed to generate the Diffie-Hellman parameters");
goto end;
}
mem = BIO_new(BIO_s_mem());
if (!mem)
{
Fail("Out of memory");
goto end;
}
if (!PEM_write_bio_Parameters(mem, pkey))
{
Fail("Failed to encode the Diffie-Hellman parameters");
goto end;
}
if (WriteFile(outfile_, BioToByteArray(mem)))
retval_ = 0;
end:
if (mem)
BIO_free(mem);
if (pkey)
EVP_PKEY_free(pkey);
if (ctx)
EVP_PKEY_CTX_free(ctx);
}
OpenSSLLib::OpenSSLLib(QObject *parent) :
QObject(parent), worker_(0)
{
}
OpenSSLLib::~OpenSSLLib()
{
if (worker_)
{
worker_->disconnect(this);
worker_->wait();
}
}
static QString NameFromCertBytes(const QByteArray &cert_pem)
{
X509 *cert;
QString name;
cert = CertFromPEM(cert_pem);
if (!cert)
return QString();
name = NameFromSubjectLine(SubjectLine(cert));
X509_free(cert);
return name;
}
QString OpenSSLLib::NameFromCertFile(QString cert_file)
{
return NameFromCertBytes(ReadWholeFile(cert_file));
}
QString OpenSSLLib::NameFromCertString(QString cert_pem)
{
return NameFromCertBytes(cert_pem.toLatin1());
}
QString OpenSSLLib::CertTextFromString(QString cert_pem)
{
X509 *cert;
BIO *mem;
QString text;
cert = CertFromPEM(cert_pem.toLatin1());
if (!cert)
return QString();
mem = BIO_new(BIO_s_mem());
if (!mem)
{
X509_free(cert);
return QString();
}
if (X509_print_ex(mem, cert, MULTILINE_NAMEOPT, 0))
text = BioToString(mem);
BIO_free(mem);
X509_free(cert);
ERR_clear_error();
return text;
}
bool OpenSSLLib::StartWorker(OpenSSLLibWorker *worker, QObject *receiver, const char *finished_slot)
{
if (worker_)
{
delete worker;
return false;
}
worker_ = worker;
errors_.clear();
output_.clear();
disconnect(this, SIGNAL(finished(int)), 0, 0);
connect(this, SIGNAL(finished(int)), receiver, finished_slot);
/*
* Queued, because QThread::finished() is emitted on the worker thread and
* this object lives on the one that called - so WorkerFinished(), and the
* receiver's slot after it, run where the caller expects them to.
*/
connect(worker_, SIGNAL(finished()), this, SLOT(WorkerFinished()));
worker_->start();
return true;
}
void OpenSSLLib::WorkerFinished()
{
OpenSSLLibWorker *worker = worker_;
int retval;
if (!worker)
return;
retval = worker->retval();
errors_ = worker->errors();
worker_ = 0;
worker->wait(); /* returns at once - run() is over by the time this is delivered */
worker->deleteLater();
/*
* Last thing done here: the receiver is allowed to delete us from its
* slot, the way DHParam disposes of its generator.
*/
emit finished(retval);
}
bool OpenSSLLib::GenRSAKeySignal(QString keyfile, bool setpassword, QString password, QString rand, QObject *receiver, const char *finished_slot)
{
OpenSSLLibWorker *worker = new OpenSSLLibWorker(OpenSSLLibWorker::JobGenRSAKey, this);
worker->keyfile_ = keyfile;
worker->setpassword_ = setpassword;
worker->password_ = password;
worker->rand_ = rand;
return StartWorker(worker, receiver, finished_slot);
}
bool OpenSSLLib::GenCertSignal(QString cert_file, QString keyfile, QString keypassword,
QString subject, QObject *receiver, const char *finished_slot)
{
OpenSSLLibWorker *worker = new OpenSSLLibWorker(OpenSSLLibWorker::JobGenCert, this);
worker->certfile_ = cert_file;
worker->keyfile_ = keyfile;
worker->password_ = keypassword;
worker->subject_ = subject;
return StartWorker(worker, receiver, finished_slot);
}
bool OpenSSLLib::GenDHParamSignal(quint32 numbits, QString output_file, QObject *receiver, const char *finished_slot)
{
OpenSSLLibWorker *worker = new OpenSSLLibWorker(OpenSSLLibWorker::JobGenDHParam, this);
worker->numbits_ = numbits;
worker->outfile_ = output_file;
return StartWorker(worker, receiver, finished_slot);
}
QString OpenSSLLib::ReadStdErr()
{
QString errors = errors_;
errors_.clear();
return errors;
}
QString OpenSSLLib::ReadStdOut()
{
QString output = output_;
output_.clear();
return output;
}
|