Added openssl-dev dependency
[froff-onlinedoc] / encryptsupport.cpp
1 #include "encryptsupport.h"
2
3 #include <QString>
4 #include <QByteArray>
5 #include <QSettings>
6 #include <QInputDialog>
7 #include <QLineEdit>
8 #include <QLabel>
9 #include <QCryptographicHash>
10 #include <QFile>
11 #include <QMaemo5InformationBox>
12
13 #include  <openssl/evp.h>
14
15 encryptSupport::encryptSupport(QWidget *parent):
16         QWidget(parent)
17 {
18     QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope, "/tmp/");
19     getDetails();
20 }
21
22 void encryptSupport::getDetails()
23 {
24     QSettings passphraseConf("freoffice", "plugin-encryption-support");
25     QSettings passphraseTemp(QSettings::SystemScope, "freoffice-encryption-support-temp.conf");
26     if (!passphraseTemp.contains("key")) {
27         enterPassphraseDialog();
28         return;
29     }
30     key = passphraseTemp.value("key").toByteArray();
31     iv = passphraseConf.value("iv").toByteArray();
32 }
33
34 void encryptSupport::enterPassphraseDialog()
35 {
36     QSettings passphraseConf("freoffice", "plugin-encryption-support");
37     if(!passphraseConf.contains("hash")) {
38         newPassphraseDialog();
39         return;
40     }
41     QString hash = passphraseConf.value("hash").toString();
42     QString passphrase;
43     while(true) {
44         passphrase = QInputDialog::getText(this,"Enter Passphrase", "Enter the passphrase you used to encrypt.\n This will be done once every session only", QLineEdit::Normal,"");
45         if(QCryptographicHash::hash(passphrase.toUtf8(), QCryptographicHash::Sha1).toHex() == hash.toUtf8())
46             break;
47         QMaemo5InformationBox::information(this, "Wrong passphrase.\nEnter again.", QMaemo5InformationBox::NoTimeout);
48     }
49     QSettings passphraseTemp(QSettings::SystemScope, "freoffice-encryption-support-temp.conf");
50     passphraseTemp.setValue("key", passphrase);
51     passphraseTemp.sync();
52     getDetails();
53 }
54
55 void encryptSupport::newPassphraseDialog()
56 {
57     QString passphrase;
58     while("" == passphrase) {
59         passphrase = QInputDialog::getText(this,"New Passphrase", "Please enter a phrase which is long.\nThis phrase will be used to encrypt your passwords and details", QLineEdit::Normal,"");
60     }
61     QSettings passphraseConf("freoffice","plugin-encryption-support");
62     QString hash(QCryptographicHash::hash(passphrase.toUtf8(), QCryptographicHash::Sha1).toHex());
63     passphraseConf.setValue("hash",hash);
64     QFile f("/dev/urandom");
65     f.open(QFile::ReadOnly);
66     QByteArray ivInit = f.read(8);
67     f.close();
68     passphraseConf.setValue("iv", ivInit);
69     passphraseConf.sync();
70     QSettings passphraseTemp(QSettings::SystemScope,"freoffice-encryption-support-temp.conf");
71     passphraseTemp.setValue("key", passphrase);
72     passphraseTemp.sync();
73     getDetails();
74 }
75
76 QString encryptSupport::encrypt(const QString & dataString)
77 {
78     QByteArray data = dataString.toUtf8();
79     EVP_CIPHER_CTX ctx;
80     EVP_CIPHER_CTX_init(&ctx);
81     EVP_EncryptInit(&ctx, EVP_bf_cbc(), (unsigned char*)key.constData(), (unsigned char*)iv.constData());
82     unsigned char outbuf[1024];
83     int len = data.length();
84     int outlen, tmplen;
85     EVP_EncryptUpdate(&ctx, outbuf, &outlen, (unsigned char*)data.constData(), len);
86     EVP_EncryptFinal_ex(&ctx, outbuf+len, &tmplen);
87     outlen += tmplen;
88     EVP_CIPHER_CTX_cleanup(&ctx);
89     QByteArray encData((const char*)outbuf, outlen);
90     return QString(encData.toHex());
91 }
92
93 QString encryptSupport::decrypt(const QString &dataString)
94 {
95     QByteArray data = QByteArray::fromHex(dataString.toUtf8());
96     EVP_CIPHER_CTX ctx;
97     EVP_CIPHER_CTX_init(&ctx);
98     EVP_DecryptInit(&ctx, EVP_bf_cbc(), (unsigned char*)key.constData(), (unsigned char*)iv.constData());
99     unsigned char outbuf[1024];
100     int len = data.length();
101     int outlen, tmplen;
102     EVP_DecryptUpdate(&ctx, outbuf, &outlen, (unsigned char*)data.constData(), len);
103     EVP_DecryptFinal(&ctx, outbuf+outlen, &tmplen);
104     EVP_CIPHER_CTX_cleanup(&ctx);
105     QByteArray decData((const char*)outbuf, outlen);
106     return QString(decData);
107 }