-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptsupport.cpp
157 lines (146 loc) · 5.41 KB
/
encryptsupport.cpp
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
/*
* Copyright (c) 2010 Kaushal M <[email protected]>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "encryptsupport.h"
#include "passphrasedialog.h"
#include <QDebug>
#include <QString>
#include <QByteArray>
#include <QSettings>
#include <QInputDialog>
#include <QLineEdit>
#include <QLabel>
#include <QCryptographicHash>
#include <QFile>
#include <QMessageBox>
#include <QMaemo5InformationBox>
#include <openssl/evp.h>
encryptSupport::encryptSupport(QWidget *parent):
QWidget(parent)
{
QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope, "/tmp/");
getDetails();
}
void encryptSupport::getDetails()
{
QSettings passphraseConf("freoffice", "plugin-settings");
passphraseConf.beginGroup("encrypt-support");
QSettings passphraseTemp(QSettings::SystemScope, "freoffice-encryption-support-temp");
if (!passphraseTemp.contains("key")) {
enterPassphraseDialog();
return;
}
key = passphraseTemp.value("key").toByteArray();
iv = passphraseConf.value("iv").toByteArray();
}
void encryptSupport::enterPassphraseDialog()
{
QSettings passphraseConf("freoffice", "plugin-settings");
passphraseConf.beginGroup("encrypt-support");
if(!passphraseConf.contains("hash")) {
newPassphraseDialog();
return;
}
QString hash = passphraseConf.value("hash").toString();
QString passphrase;
while(true) {
int val = 0;
passphrase = passphraseDialog::getPassphrase(val, this);
if(val == 1) {
QMaemo5InformationBox::information(this,"All saved details will be lost", QMaemo5InformationBox::NoTimeout);
deleteSettings();
newPassphraseDialog();
return;
}
else if (val == 0) {
if(QCryptographicHash::hash(passphrase.toUtf8(), QCryptographicHash::Sha1).toHex() == hash.toUtf8()) {
break;
}
else {
QMaemo5InformationBox::information(this, "Wrong passphrase.\nEnter again.", QMaemo5InformationBox::NoTimeout);
}
}
else if (val == -1) {
emit cancelled();
return;
}
}
QSettings passphraseTemp(QSettings::SystemScope, "freoffice-encryption-support-temp");
passphraseTemp.setValue("key", passphrase);
passphraseTemp.sync();
getDetails();
}
void encryptSupport::deleteSettings() {
QSettings passphraseConf("freoffice", "plugin-settings");
passphraseConf.clear();
passphraseConf.sync();
QSettings passphraseTemp(QSettings::SystemScope, "freoffice-encryption-support-temp");
passphraseTemp.clear();
passphraseTemp.sync();
}
void encryptSupport::newPassphraseDialog()
{
QString passphrase;
while("" == passphrase) {
passphrase = QInputDialog::getText(this,"New Passphrase", "Please enter a phrase which is long.\nThis phrase will be used to encrypt your passwords", QLineEdit::Normal,"");
}
QSettings passphraseConf("freoffice","plugin-settings");
passphraseConf.beginGroup("encrypt-support");
QString hash(QCryptographicHash::hash(passphrase.toUtf8(), QCryptographicHash::Sha1).toHex());
passphraseConf.setValue("hash",hash);
QFile f("/dev/urandom");
f.open(QFile::ReadOnly);
QByteArray ivInit = f.read(8);
f.close();
passphraseConf.setValue("iv", ivInit);
passphraseConf.sync();
QSettings passphraseTemp(QSettings::SystemScope,"freoffice-encryption-support-temp");
passphraseTemp.setValue("key", passphrase);
passphraseTemp.sync();
getDetails();
}
QString encryptSupport::encrypt(const QString & dataString)
{
QByteArray data = dataString.toUtf8();
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit(&ctx, EVP_bf_cbc(), (unsigned char*)key.constData(), (unsigned char*)iv.constData());
unsigned char outbuf[1024];
int len = data.length();
int outlen, tmplen;
EVP_EncryptUpdate(&ctx, outbuf, &outlen, (unsigned char*)data.constData(), len);
EVP_EncryptFinal_ex(&ctx, outbuf+len, &tmplen);
outlen += tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
QByteArray encData((const char*)outbuf, outlen);
return QString(encData.toHex());
}
QString encryptSupport::decrypt(const QString &dataString)
{
QByteArray data = QByteArray::fromHex(dataString.toUtf8());
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_DecryptInit(&ctx, EVP_bf_cbc(), (unsigned char*)key.constData(), (unsigned char*)iv.constData());
unsigned char outbuf[1024];
int len = data.length();
int outlen, tmplen;
EVP_DecryptUpdate(&ctx, outbuf, &outlen, (unsigned char*)data.constData(), len);
EVP_DecryptFinal(&ctx, outbuf+outlen, &tmplen);
EVP_CIPHER_CTX_cleanup(&ctx);
QByteArray decData((const char*)outbuf, outlen);
return QString(decData);
}