forked from frankosterfeld/qtkeychain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keychain_android.cpp
182 lines (140 loc) · 6.25 KB
/
keychain_android.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
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
/******************************************************************************
* Copyright (C) 2016 Mathias Hasselmann <[email protected]> *
* *
* 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. For licensing and distribution *
* details, check the accompanying file 'COPYING'. *
*****************************************************************************/
#include "keychain_p.h"
#include "androidkeystore_p.h"
#include "plaintextstore_p.h"
#include <QtAndroid>
using namespace QKeychain;
using android::content::Context;
using android::security::KeyPairGeneratorSpec;
using java::io::ByteArrayInputStream;
using java::io::ByteArrayOutputStream;
using java::security::interfaces::RSAPrivateKey;
using java::security::interfaces::RSAPublicKey;
using java::security::KeyPair;
using java::security::KeyPairGenerator;
using java::security::KeyStore;
using java::util::Calendar;
using javax::crypto::Cipher;
using javax::crypto::CipherInputStream;
using javax::crypto::CipherOutputStream;
using javax::security::auth::x500::X500Principal;
namespace {
inline QString makeAlias(const QString &service, const QString &key)
{
return service + QLatin1Char('/') + key;
}
}
void ReadPasswordJobPrivate::scheduledStart()
{
PlainTextStore plainTextStore(q->service(), q->settings());
if (!plainTextStore.contains(q->key())) {
q->emitFinishedWithError(Error::EntryNotFound, tr("Entry not found"));
return;
}
const QByteArray &encryptedData = plainTextStore.readData(q->key());
const KeyStore keyStore = KeyStore::getInstance(QStringLiteral("AndroidKeyStore"));
if (!keyStore || !keyStore.load()) {
q->emitFinishedWithError(Error::AccessDenied, tr("Could not open keystore"));
return;
}
const auto &alias = makeAlias(q->service(), q->key());
const KeyStore::PrivateKeyEntry entry = keyStore.getEntry(alias);
if (!entry) {
q->emitFinishedWithError(Error::AccessDenied, tr("Could not retrieve private key from keystore"));
return;
}
const Cipher cipher = Cipher::getInstance(QStringLiteral("RSA/ECB/PKCS1Padding"));
if (!cipher || !cipher.init(Cipher::DECRYPT_MODE, entry.getPrivateKey())) {
q->emitFinishedWithError(Error::OtherError, tr("Could not create decryption cipher"));
return;
}
QByteArray plainData;
const CipherInputStream inputStream(ByteArrayInputStream(encryptedData), cipher);
for (int nextByte; (nextByte = inputStream.read()) != -1; )
plainData.append(nextByte);
mode = plainTextStore.readMode(q->key());
data = plainData;
q->emitFinished();
}
void WritePasswordJobPrivate::scheduledStart()
{
const KeyStore keyStore = KeyStore::getInstance(QStringLiteral("AndroidKeyStore"));
if (!keyStore || !keyStore.load()) {
q->emitFinishedWithError(Error::AccessDenied, tr("Could not open keystore"));
return;
}
const auto &alias = makeAlias(q->service(), q->key());
if (!keyStore.containsAlias(alias)) {
const Calendar start = Calendar::getInstance();
const Calendar end = Calendar::getInstance();
end.add(Calendar::YEAR, 99);
const KeyPairGeneratorSpec spec =
KeyPairGeneratorSpec::Builder(Context(QtAndroid::androidActivity())).
setAlias(alias).
setSubject(X500Principal(QStringLiteral("CN=QtKeychain, O=Android Authority"))).
setSerialNumber(java::math::BigInteger::ONE).
setStartDate(start.getTime()).
setEndDate(end.getTime()).
build();
const KeyPairGenerator generator = KeyPairGenerator::getInstance(QStringLiteral("RSA"),
QStringLiteral("AndroidKeyStore"));
if (!generator) {
q->emitFinishedWithError(Error::OtherError, tr("Could not create private key generator"));
return;
}
generator.initialize(spec);
if (!generator.generateKeyPair()) {
q->emitFinishedWithError(Error::OtherError, tr("Could not generate new private key"));
return;
}
}
const KeyStore::PrivateKeyEntry entry = keyStore.getEntry(alias);
if (!entry) {
q->emitFinishedWithError(Error::AccessDenied, tr("Could not retrieve private key from keystore"));
return;
}
const RSAPublicKey publicKey = entry.getCertificate().getPublicKey();
const Cipher cipher = Cipher::getInstance(QStringLiteral("RSA/ECB/PKCS1Padding"));
if (!cipher || !cipher.init(Cipher::ENCRYPT_MODE, publicKey)) {
q->emitFinishedWithError(Error::OtherError, tr("Could not create encryption cipher"));
return;
}
ByteArrayOutputStream outputStream;
CipherOutputStream cipherOutputStream(outputStream, cipher);
if (!cipherOutputStream.write(data) || !cipherOutputStream.close()) {
q->emitFinishedWithError(Error::OtherError, tr("Could not encrypt data"));
return;
}
PlainTextStore plainTextStore(q->service(), q->settings());
plainTextStore.write(q->key(), outputStream.toByteArray(), mode);
if (plainTextStore.error() != NoError)
q->emitFinishedWithError(plainTextStore.error(), plainTextStore.errorString());
else
q->emitFinished();
}
void DeletePasswordJobPrivate::scheduledStart()
{
const KeyStore keyStore = KeyStore::getInstance(QStringLiteral("AndroidKeyStore"));
if (!keyStore || !keyStore.load()) {
q->emitFinishedWithError(Error::AccessDenied, tr("Could not open keystore"));
return;
}
const auto &alias = makeAlias(q->service(), q->key());
if (!keyStore.deleteEntry(alias)) {
q->emitFinishedWithError(Error::OtherError, tr("Could not remove private key from keystore"));
return;
}
PlainTextStore plainTextStore(q->service(), q->settings());
plainTextStore.remove(q->key());
if (plainTextStore.error() != NoError)
q->emitFinishedWithError(plainTextStore.error(), plainTextStore.errorString());
else
q->emitFinished();
}