summaryrefslogtreecommitdiffstats
path: root/pica-client/dhparam.cpp
blob: 62f7fe0a2f16b59bcd365050624a687e1f76f7f7 (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
/*
	(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, either version 3 of the License, or
	(at your option) any later version.

	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 "dhparam.h"
#include <QFile>
#include <QDir>


DHParam *DHParam::dhparamgenerator = NULL;
QString DHParam::current_dhparam_file;

DHParam::DHParam(QObject *parent) :
	QObject(parent)
{
}


bool DHParam::VerifyDefault(QString *errormsg)
{
	if (!QFile::exists(config_defaultDHParam))
	{
		if (errormsg)
			*errormsg =
			    QString(tr("Diffie-Hellman parameters file \"%1\" does not exist"))
			    .arg(config_defaultDHParam);

		return false;
	}

	return true;
}

bool DHParam::VerifyGenerated(QString *errormsg)
{
	if (!QFile::exists(config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE))
		return false;

	return true;
}

bool DHParam::UseDefault()
{
	return QFile::copy(config_defaultDHParam, config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE);
}

bool DHParam::StartNewDHParamGeneration(QString *errormsg)
{
	if (dhparamgenerator)
	{
		if (errormsg)
			*errormsg = QString(tr("Diffie-Hellman parameter generation process is already running"));
		return false;
	}

	dhparamgenerator = new DHParam();

	if (!dhparamgenerator)
		return false;

	QString tempdhfile = config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE + ".temp";

	if (QFile::exists(tempdhfile))
		QFile::remove(tempdhfile);

	return dhparamgenerator -> osslt.GenDHParamSignal(PICA_CLIENT_DHPARAMBITS,
	        tempdhfile, dhparamgenerator, SLOT(DHParamGenFinished(int, QProcess::ExitStatus)));
}

QString DHParam::GetDHParamFilename()
{
	if (current_dhparam_file.isEmpty())
	{
		if (QFile::exists(config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE))
			current_dhparam_file = config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE;
		else
			current_dhparam_file = config_defaultDHParam;
	}

	return current_dhparam_file;
}

void DHParam::DHParamGenFinished(int retval, QProcess::ExitStatus)
{
	if (retval == 0)
	{
		if (QFile::exists(config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE))
			QFile::remove(config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE);

		QFile::rename(config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE + ".temp",
		              config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE);

		current_dhparam_file = config_dir + QDir::separator() + PICA_CLIENT_DHPARAMFILE;

		delete dhparamgenerator;

		dhparamgenerator = NULL;
	}
}