summaryrefslogtreecommitdiffstats
path: root/pica-client/openssltool.cpp
blob: ec7a547d455a968c6971449598335e79ffe11afd (plain) (blame)
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
/*
	(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 "openssltool.h"
#include "../PICA_security.h"
#include <openssl/rand.h>
#include <QProcess>
#include <QRegExp>
#include <QMessageBox>

#define REGEXOLD "CN = [0-9]+\\#([^\\#\\$&\"\'=\\(\\)\\\\/\\|`!<>\\{\\}\\[\\]\\+\r\n]+)"
#define REGEXNEW "CN = ([^\\#\\$&\"\'=\\(\\)\\\\/\\|`!<>\\{\\}\\[\\]\\+\r\n]+)"

OpenSSLTool::OpenSSLTool()
{
}

QString OpenSSLTool::NameFromCertFile(QString cert_file)
{
	//
	QProcess openssl;

	openssl.start("openssl", QStringList() << "x509" << "-in" << cert_file << "-subject" << "-nameopt" << "oneline,-esc_msb,utf8" << "-noout");
	if (!openssl.waitForStarted())
		return QString();

	if (!openssl.waitForFinished())
		return QString();

	QByteArray result = openssl.readAll();
	QRegExp rxOld(REGEXOLD);
	QRegExp rx(REGEXNEW);

	QString name = QString::fromUtf8( result.constData());

	if (name.contains(rxOld))
	{
		name = rxOld.cap(1);
	}
	else if (name.contains(rx))
	{
		name = rx.cap(1);
	}

	//
	return name;
}

QString OpenSSLTool::NameFromCertString(QString cert_pem)
{
	QProcess openssl;

	openssl.start("openssl", QStringList() << "x509" << "-subject" << "-nameopt" << "oneline,-esc_msb,utf8" << "-noout");
	if (!openssl.waitForStarted())
		return QString();

	openssl.write(cert_pem.toLatin1());

	if (!openssl.waitForFinished())
		return QString();

	QByteArray result = openssl.readAll();
	QRegExp rxOld(REGEXOLD);
	QRegExp rx(REGEXNEW);

	QString name = QString::fromUtf8( result.constData());

	if (name.contains(rxOld))
	{
		name = rxOld.cap(1);
	}
	else if (name.contains(rx))
	{
		name = rx.cap(1);
	}

	//
	return name;
}

QString OpenSSLTool::CertTextFromString(QString cert_pem)
{
	QProcess openssl;
	qint64 ret;
	char *data;
	QByteArray ba;

	openssl.start("openssl", QStringList() << "x509" << "-noout" << "-text" << "-nameopt" << "multiline,-esc_msb,utf8");

	if (!openssl.waitForStarted())
		return QString();

	ba = cert_pem.toLatin1();
	ret = openssl.write(data = (char*)ba.constData());
	openssl.closeWriteChannel();

	if (!openssl.waitForFinished())
		return QString();

	return QString::fromUtf8(openssl.readAllStandardOutput().constData());
}

bool OpenSSLTool::GenRSAKeySignal(QString keyfile, bool setpassword, QString password, QString rand, QObject *receiver, const char *finished_slot)
{
	QStringList args;
	quint32 numbits;
	unsigned char rndchar;

	args << "genrsa" << "-out" << keyfile;

	if (setpassword)
		args << "-passout" << "stdin" << PICA_PRIVKEYENCALGO;

	if (!rand.isEmpty())
		args << "-rand" << rand;

	/*
	 * 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;

	RAND_pseudo_bytes(&rndchar, 1);

	numbits += 8 * (rndchar % 100);

	args << QString::number(numbits);

	openssl_.start("openssl", args);


	if (!openssl_.waitForStarted())
		return false;

	if (setpassword)
	{
		openssl_.write((password + "\n").toLatin1().constData());
	}

	openssl_.disconnect(&openssl_, SIGNAL(finished(int, QProcess::ExitStatus)), 0, 0);
	openssl_.connect(&openssl_, SIGNAL(finished(int, QProcess::ExitStatus)), receiver, finished_slot);

	return true;
}

bool OpenSSLTool::GenCertSignal(QString cert_file, QString keyfile, QString keypassword,
                                QString subject, QObject *receiver, const char *finished_slot)
{
	openssl_.start("openssl",
	               QStringList()
	               << "req"
#ifdef WIN32
	               << "-config" << "openssl.cnf"
#endif
	               << "-out" << cert_file
	               << "-key" << keyfile
	               << "-subj" << "/CN=" + subject
	               << "-new"
	               << "-x509"
	               << "-days" << "3680"
	               << "-utf8"
	               << "-batch"
	               << PICA_CERTDIGESTALGO
	               << "-passin"
	               << "stdin");

	if (!openssl_.waitForStarted())
		return false;

	openssl_.write((keypassword + "\n").toLatin1().constData());

	openssl_.disconnect(&openssl_, SIGNAL(finished(int, QProcess::ExitStatus)), 0, 0);
	openssl_.connect(&openssl_, SIGNAL(finished(int, QProcess::ExitStatus)), receiver, finished_slot);

	return true;
}

QString OpenSSLTool::ReadStdErr()
{
	return QString::fromUtf8(openssl_.readAllStandardError().constData());
}

QString OpenSSLTool::ReadStdOut()
{
	return QString::fromUtf8(openssl_.readAllStandardOutput().constData());
}

bool OpenSSLTool::GenDHParamSignal(quint32 numbits, QString output_file, QObject *receiver, const char *finished_slot)
{
	//openssl dhparam -5 4096
	openssl_.start("openssl",
	               QStringList()
	               << "dhparam"
	               << "-out" << output_file
	               << "-5"
	               << QString::number(numbits));

	if (!openssl_.waitForStarted())
		return false;

	openssl_.disconnect(&openssl_, SIGNAL(finished(int, QProcess::ExitStatus)), 0, 0);
	openssl_.connect(&openssl_, SIGNAL(finished(int, QProcess::ExitStatus)), receiver, finished_slot);

	return true;
}