author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 12:15:23 +0300 | |
branch | RCL_3 |
changeset 12 | cc75c76972ee |
parent 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <qsslkey.h> |
|
45 |
#include <qsslsocket.h> |
|
46 |
||
47 |
#include <QtNetwork/qhostaddress.h> |
|
48 |
#include <QtNetwork/qnetworkproxy.h> |
|
49 |
||
50 |
#ifdef Q_OS_SYMBIAN |
|
51 |
// In Symbian OS test data is located in applications private dir |
|
52 |
// Current path (C:\private\<UID>) contains only ascii chars |
|
53 |
#define SRCDIR QDir::currentPath().toAscii() |
|
54 |
#endif |
|
55 |
||
56 |
class tst_QSslKey : public QObject |
|
57 |
{ |
|
58 |
Q_OBJECT |
|
59 |
||
60 |
struct KeyInfo { |
|
61 |
QFileInfo fileInfo; |
|
62 |
QSsl::KeyAlgorithm algorithm; |
|
63 |
QSsl::KeyType type; |
|
64 |
int length; |
|
65 |
QSsl::EncodingFormat format; |
|
66 |
KeyInfo( |
|
67 |
const QFileInfo &fileInfo, QSsl::KeyAlgorithm algorithm, QSsl::KeyType type, |
|
68 |
int length, QSsl::EncodingFormat format) |
|
69 |
: fileInfo(fileInfo), algorithm(algorithm), type(type), length(length) |
|
70 |
, format(format) {} |
|
71 |
}; |
|
72 |
||
73 |
QList<KeyInfo> keyInfoList; |
|
74 |
||
75 |
void createPlainTestRows(); |
|
76 |
||
77 |
public: |
|
78 |
tst_QSslKey(); |
|
79 |
virtual ~tst_QSslKey(); |
|
80 |
||
81 |
public slots: |
|
82 |
void initTestCase_data(); |
|
83 |
void init(); |
|
84 |
void cleanup(); |
|
85 |
||
86 |
#ifndef QT_NO_OPENSSL |
|
87 |
||
88 |
private slots: |
|
89 |
void emptyConstructor(); |
|
90 |
void constructor_data(); |
|
91 |
void constructor(); |
|
92 |
void copyAndAssign_data(); |
|
93 |
void copyAndAssign(); |
|
94 |
void equalsOperator(); |
|
95 |
void length_data(); |
|
96 |
void length(); |
|
97 |
void toPemOrDer_data(); |
|
98 |
void toPemOrDer(); |
|
99 |
void toEncryptedPemOrDer_data(); |
|
100 |
void toEncryptedPemOrDer(); |
|
101 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
102 |
void passphraseChecks(); |
0 | 103 |
#endif |
104 |
}; |
|
105 |
||
106 |
tst_QSslKey::tst_QSslKey() |
|
107 |
{ |
|
108 |
#ifdef Q_WS_MAC |
|
109 |
// applicationDirPath() points to a path inside the app bundle on Mac. |
|
110 |
QDir dir(qApp->applicationDirPath() + QLatin1String("/../../../keys")); |
|
111 |
#elif defined(Q_OS_WIN) || defined (Q_OS_SYMBIAN) |
|
112 |
QDir dir(SRCDIR + QLatin1String("/keys")); // prefer this way to avoid ifdeffery and support shadow builds? |
|
113 |
#else |
|
114 |
QDir dir(qApp->applicationDirPath() + QLatin1String("/keys")); |
|
115 |
#endif |
|
116 |
QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Readable); |
|
117 |
QRegExp rx(QLatin1String("^(rsa|dsa)-(pub|pri)-(\\d+)\\.(pem|der)$")); |
|
118 |
foreach (QFileInfo fileInfo, fileInfoList) { |
|
119 |
if (rx.indexIn(fileInfo.fileName()) >= 0) |
|
120 |
keyInfoList << KeyInfo( |
|
121 |
fileInfo, |
|
122 |
rx.cap(1) == QLatin1String("rsa") ? QSsl::Rsa : QSsl::Dsa, |
|
123 |
rx.cap(2) == QLatin1String("pub") ? QSsl::PublicKey : QSsl::PrivateKey, |
|
124 |
rx.cap(3).toInt(), |
|
125 |
rx.cap(4) == QLatin1String("pem") ? QSsl::Pem : QSsl::Der); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
tst_QSslKey::~tst_QSslKey() |
|
130 |
{ |
|
131 |
} |
|
132 |
||
133 |
void tst_QSslKey::initTestCase_data() |
|
134 |
{ |
|
135 |
} |
|
136 |
||
137 |
void tst_QSslKey::init() |
|
138 |
{ |
|
139 |
} |
|
140 |
||
141 |
void tst_QSslKey::cleanup() |
|
142 |
{ |
|
143 |
} |
|
144 |
||
145 |
static QByteArray readFile(const QString &absFilePath) |
|
146 |
{ |
|
147 |
QFile file(absFilePath); |
|
148 |
if (!file.open(QIODevice::ReadOnly)) { |
|
149 |
QWARN("failed to open file"); |
|
150 |
return QByteArray(); |
|
151 |
} |
|
152 |
return file.readAll(); |
|
153 |
} |
|
154 |
||
155 |
#ifndef QT_NO_OPENSSL |
|
156 |
||
157 |
void tst_QSslKey::emptyConstructor() |
|
158 |
{ |
|
159 |
if (!QSslSocket::supportsSsl()) |
|
160 |
return; |
|
161 |
||
162 |
QSslKey key; |
|
163 |
QVERIFY(key.isNull()); |
|
164 |
QVERIFY(key.length() < 0); |
|
165 |
||
166 |
QSslKey key2; |
|
167 |
QCOMPARE(key, key2); |
|
168 |
} |
|
169 |
||
170 |
Q_DECLARE_METATYPE(QSsl::KeyAlgorithm); |
|
171 |
Q_DECLARE_METATYPE(QSsl::KeyType); |
|
172 |
Q_DECLARE_METATYPE(QSsl::EncodingFormat); |
|
173 |
||
174 |
void tst_QSslKey::createPlainTestRows() |
|
175 |
{ |
|
176 |
QTest::addColumn<QString>("absFilePath"); |
|
177 |
QTest::addColumn<QSsl::KeyAlgorithm>("algorithm"); |
|
178 |
QTest::addColumn<QSsl::KeyType>("type"); |
|
179 |
QTest::addColumn<int>("length"); |
|
180 |
QTest::addColumn<QSsl::EncodingFormat>("format"); |
|
181 |
foreach (KeyInfo keyInfo, keyInfoList) { |
|
182 |
QTest::newRow(keyInfo.fileInfo.fileName().toLatin1()) |
|
183 |
<< keyInfo.fileInfo.absoluteFilePath() << keyInfo.algorithm << keyInfo.type |
|
184 |
<< keyInfo.length << keyInfo.format; |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
void tst_QSslKey::constructor_data() |
|
189 |
{ |
|
190 |
createPlainTestRows(); |
|
191 |
} |
|
192 |
||
193 |
void tst_QSslKey::constructor() |
|
194 |
{ |
|
195 |
if (!QSslSocket::supportsSsl()) |
|
196 |
return; |
|
197 |
||
198 |
QFETCH(QString, absFilePath); |
|
199 |
QFETCH(QSsl::KeyAlgorithm, algorithm); |
|
200 |
QFETCH(QSsl::KeyType, type); |
|
201 |
QFETCH(QSsl::EncodingFormat, format); |
|
202 |
||
203 |
QByteArray encoded = readFile(absFilePath); |
|
204 |
QSslKey key(encoded, algorithm, format, type); |
|
205 |
QVERIFY(!key.isNull()); |
|
206 |
} |
|
207 |
||
208 |
void tst_QSslKey::copyAndAssign_data() |
|
209 |
{ |
|
210 |
createPlainTestRows(); |
|
211 |
} |
|
212 |
||
213 |
void tst_QSslKey::copyAndAssign() |
|
214 |
{ |
|
215 |
if (!QSslSocket::supportsSsl()) |
|
216 |
return; |
|
217 |
||
218 |
QFETCH(QString, absFilePath); |
|
219 |
QFETCH(QSsl::KeyAlgorithm, algorithm); |
|
220 |
QFETCH(QSsl::KeyType, type); |
|
221 |
QFETCH(QSsl::EncodingFormat, format); |
|
222 |
||
223 |
QByteArray encoded = readFile(absFilePath); |
|
224 |
QSslKey key(encoded, algorithm, format, type); |
|
225 |
||
226 |
QSslKey copied(key); |
|
227 |
QCOMPARE(key, copied); |
|
228 |
QCOMPARE(key.algorithm(), copied.algorithm()); |
|
229 |
QCOMPARE(key.type(), copied.type()); |
|
230 |
QCOMPARE(key.length(), copied.length()); |
|
231 |
QCOMPARE(key.toPem(), copied.toPem()); |
|
232 |
QCOMPARE(key.toDer(), copied.toDer()); |
|
233 |
||
234 |
QSslKey assigned = key; |
|
235 |
QCOMPARE(key, assigned); |
|
236 |
QCOMPARE(key.algorithm(), assigned.algorithm()); |
|
237 |
QCOMPARE(key.type(), assigned.type()); |
|
238 |
QCOMPARE(key.length(), assigned.length()); |
|
239 |
QCOMPARE(key.toPem(), assigned.toPem()); |
|
240 |
QCOMPARE(key.toDer(), assigned.toDer()); |
|
241 |
} |
|
242 |
||
243 |
void tst_QSslKey::equalsOperator() |
|
244 |
{ |
|
245 |
// ### unimplemented |
|
246 |
} |
|
247 |
||
248 |
void tst_QSslKey::length_data() |
|
249 |
{ |
|
250 |
createPlainTestRows(); |
|
251 |
} |
|
252 |
||
253 |
void tst_QSslKey::length() |
|
254 |
{ |
|
255 |
if (!QSslSocket::supportsSsl()) |
|
256 |
return; |
|
257 |
||
258 |
QFETCH(QString, absFilePath); |
|
259 |
QFETCH(QSsl::KeyAlgorithm, algorithm); |
|
260 |
QFETCH(QSsl::KeyType, type); |
|
261 |
QFETCH(int, length); |
|
262 |
QFETCH(QSsl::EncodingFormat, format); |
|
263 |
||
264 |
QByteArray encoded = readFile(absFilePath); |
|
265 |
QSslKey key(encoded, algorithm, format, type); |
|
266 |
QVERIFY(!key.isNull()); |
|
267 |
QCOMPARE(key.length(), length); |
|
268 |
} |
|
269 |
||
270 |
void tst_QSslKey::toPemOrDer_data() |
|
271 |
{ |
|
272 |
createPlainTestRows(); |
|
273 |
} |
|
274 |
||
275 |
void tst_QSslKey::toPemOrDer() |
|
276 |
{ |
|
277 |
if (!QSslSocket::supportsSsl()) |
|
278 |
return; |
|
279 |
||
280 |
QFETCH(QString, absFilePath); |
|
281 |
QFETCH(QSsl::KeyAlgorithm, algorithm); |
|
282 |
QFETCH(QSsl::KeyType, type); |
|
283 |
QFETCH(QSsl::EncodingFormat, format); |
|
284 |
||
285 |
QByteArray encoded = readFile(absFilePath); |
|
286 |
QSslKey key(encoded, algorithm, format, type); |
|
287 |
QVERIFY(!key.isNull()); |
|
288 |
if (format == QSsl::Pem) |
|
289 |
encoded.replace('\r', ""); |
|
290 |
QCOMPARE(format == QSsl::Pem ? key.toPem() : key.toDer(), encoded); |
|
291 |
} |
|
292 |
||
293 |
void tst_QSslKey::toEncryptedPemOrDer_data() |
|
294 |
{ |
|
295 |
QTest::addColumn<QString>("absFilePath"); |
|
296 |
QTest::addColumn<QSsl::KeyAlgorithm>("algorithm"); |
|
297 |
QTest::addColumn<QSsl::KeyType>("type"); |
|
298 |
QTest::addColumn<QSsl::EncodingFormat>("format"); |
|
299 |
QTest::addColumn<QString>("password"); |
|
300 |
||
301 |
QStringList passwords; |
|
302 |
passwords << " " << "foobar" << "foo bar" |
|
303 |
<< "aAzZ`1234567890-=~!@#$%^&*()_+[]{}\\|;:'\",.<>/?"; // ### add more (?) |
|
304 |
foreach (KeyInfo keyInfo, keyInfoList) { |
|
305 |
foreach (QString password, passwords) { |
|
306 |
QString testName = QString("%1-%2-%3-%4").arg(keyInfo.fileInfo.fileName()) |
|
307 |
.arg(keyInfo.algorithm == QSsl::Rsa ? "RSA" : "DSA") |
|
308 |
.arg(keyInfo.type == QSsl::PrivateKey ? "PrivateKey" : "PublicKey") |
|
309 |
.arg(keyInfo.format == QSsl::Pem ? "PEM" : "DER"); |
|
310 |
QTest::newRow(testName.toLatin1()) |
|
311 |
<< keyInfo.fileInfo.absoluteFilePath() << keyInfo.algorithm << keyInfo.type |
|
312 |
<< keyInfo.format << password; |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
void tst_QSslKey::toEncryptedPemOrDer() |
|
318 |
{ |
|
319 |
if (!QSslSocket::supportsSsl()) |
|
320 |
return; |
|
321 |
||
322 |
QFETCH(QString, absFilePath); |
|
323 |
QFETCH(QSsl::KeyAlgorithm, algorithm); |
|
324 |
QFETCH(QSsl::KeyType, type); |
|
325 |
QFETCH(QSsl::EncodingFormat, format); |
|
326 |
QFETCH(QString, password); |
|
327 |
||
328 |
QByteArray plain = readFile(absFilePath); |
|
329 |
QSslKey key(plain, algorithm, format, type); |
|
330 |
QVERIFY(!key.isNull()); |
|
331 |
||
332 |
QByteArray pwBytes(password.toLatin1()); |
|
333 |
||
334 |
if (type == QSsl::PrivateKey) { |
|
335 |
QByteArray encryptedPem = key.toPem(pwBytes); |
|
336 |
QVERIFY(!encryptedPem.isEmpty()); |
|
337 |
QSslKey keyPem(encryptedPem, algorithm, QSsl::Pem, type, pwBytes); |
|
338 |
QVERIFY(!keyPem.isNull()); |
|
339 |
QCOMPARE(keyPem, key); |
|
340 |
QCOMPARE(keyPem.toPem(), key.toPem()); |
|
341 |
} else { |
|
342 |
// verify that public keys are never encrypted by toPem() |
|
343 |
QByteArray encryptedPem = key.toPem(pwBytes); |
|
344 |
QVERIFY(!encryptedPem.isEmpty()); |
|
345 |
QByteArray plainPem = key.toPem(); |
|
346 |
QVERIFY(!plainPem.isEmpty()); |
|
347 |
QCOMPARE(encryptedPem, plainPem); |
|
348 |
} |
|
349 |
||
350 |
if (type == QSsl::PrivateKey) { |
|
351 |
QByteArray encryptedDer = key.toDer(pwBytes); |
|
352 |
// ### at this point, encryptedDer is invalid, hence the below QEXPECT_FAILs |
|
353 |
QVERIFY(!encryptedDer.isEmpty()); |
|
354 |
QSslKey keyDer(encryptedDer, algorithm, QSsl::Der, type, pwBytes); |
|
355 |
if (type == QSsl::PrivateKey) |
|
356 |
QEXPECT_FAIL( |
|
357 |
QTest::currentDataTag(), "We're not able to decrypt these yet...", Continue); |
|
358 |
QVERIFY(!keyDer.isNull()); |
|
359 |
if (type == QSsl::PrivateKey) |
|
360 |
QEXPECT_FAIL( |
|
361 |
QTest::currentDataTag(), "We're not able to decrypt these yet...", Continue); |
|
362 |
QCOMPARE(keyDer.toPem(), key.toPem()); |
|
363 |
} else { |
|
364 |
// verify that public keys are never encrypted by toDer() |
|
365 |
QByteArray encryptedDer = key.toDer(pwBytes); |
|
366 |
QVERIFY(!encryptedDer.isEmpty()); |
|
367 |
QByteArray plainDer = key.toDer(); |
|
368 |
QVERIFY(!plainDer.isEmpty()); |
|
369 |
QCOMPARE(encryptedDer, plainDer); |
|
370 |
} |
|
371 |
||
372 |
// ### add a test to verify that public keys are _decrypted_ correctly (by the ctor) |
|
373 |
} |
|
374 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
375 |
void tst_QSslKey::passphraseChecks() |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
376 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
377 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
378 |
QString fileName(SRCDIR "/rsa-with-passphrase.pem"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
379 |
QFile keyFile(fileName); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
380 |
QVERIFY(keyFile.exists()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
381 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
382 |
if (!keyFile.isOpen()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
383 |
keyFile.open(QIODevice::ReadOnly); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
384 |
else |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
385 |
keyFile.reset(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
386 |
QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
387 |
QVERIFY(key.isNull()); // null passphrase => should not be able to decode key |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
388 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
389 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
390 |
if (!keyFile.isOpen()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
391 |
keyFile.open(QIODevice::ReadOnly); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
392 |
else |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
393 |
keyFile.reset(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
394 |
QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, ""); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
395 |
QVERIFY(key.isNull()); // empty passphrase => should not be able to decode key |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
396 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
397 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
398 |
if (!keyFile.isOpen()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
399 |
keyFile.open(QIODevice::ReadOnly); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
400 |
else |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
401 |
keyFile.reset(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
402 |
QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, "WRONG!"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
403 |
QVERIFY(key.isNull()); // wrong passphrase => should not be able to decode key |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
404 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
405 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
406 |
if (!keyFile.isOpen()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
407 |
keyFile.open(QIODevice::ReadOnly); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
408 |
else |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
409 |
keyFile.reset(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
410 |
QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, "123"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
411 |
QVERIFY(!key.isNull()); // correct passphrase |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
412 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
413 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
414 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
415 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
416 |
// be sure and check a key without passphrase too |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
417 |
QString fileName(SRCDIR "/rsa-without-passphrase.pem"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
418 |
QFile keyFile(fileName); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
419 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
420 |
if (!keyFile.isOpen()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
421 |
keyFile.open(QIODevice::ReadOnly); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
422 |
else |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
423 |
keyFile.reset(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
424 |
QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
425 |
QVERIFY(!key.isNull()); // null passphrase => should be able to decode key |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
426 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
427 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
428 |
if (!keyFile.isOpen()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
429 |
keyFile.open(QIODevice::ReadOnly); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
430 |
else |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
431 |
keyFile.reset(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
432 |
QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, ""); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
433 |
QVERIFY(!key.isNull()); // empty passphrase => should be able to decode key |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
434 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
435 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
436 |
if (!keyFile.isOpen()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
437 |
keyFile.open(QIODevice::ReadOnly); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
438 |
else |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
439 |
keyFile.reset(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
440 |
QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, "xxx"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
441 |
QVERIFY(!key.isNull()); // passphrase given but key is not encrypted anyway => should work |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
442 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
443 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
444 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
445 |
|
0 | 446 |
#endif |
447 |
||
448 |
QTEST_MAIN(tst_QSslKey) |
|
449 |
#include "tst_qsslkey.moc" |