Line | Source | Count |
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 | #include "qsslkey.h" | - |
58 | #include "qsslkey_p.h" | - |
59 | #include "qsslsocket.h" | - |
60 | #include "qsslsocket_p.h" | - |
61 | | - |
62 | #include <QtCore/qatomic.h> | - |
63 | #include <QtCore/qbytearray.h> | - |
| #include <QtCore/qbytearraymatcher.h> | |
64 | #include <QtCore/qiodevice.h> | - |
65 | #ifndef QT_NO_DEBUG_STREAM | - |
66 | #include <QtCore/qdebug.h> | - |
67 | #endif | - |
68 | | - |
69 | QT_BEGIN_NAMESPACE | - |
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 | QSslKey::QSslKey() | - |
98 | : d(new QSslKeyPrivate) | - |
99 | { | - |
100 | } | - |
101 | | - |
102 | | - |
103 | | - |
104 | | - |
105 | QByteArray QSslKeyPrivate::pemHeader() const | - |
106 | { | - |
107 | if (type == QSsl::PublicKey) | - |
108 | return QByteArrayLiteral("-----BEGIN PUBLIC KEY-----"); | - |
109 | else if (algorithm == QSsl::Rsa) | - |
110 | return QByteArrayLiteral("-----BEGIN RSA PRIVATE KEY-----"); | - |
111 | else if (algorithm == QSsl::Dsa) | - |
112 | return QByteArrayLiteral("-----BEGIN DSA PRIVATE KEY-----"); | - |
113 | else if (algorithm == QSsl::Ec) | - |
114 | return QByteArrayLiteral("-----BEGIN EC PRIVATE KEY-----"); | - |
115 | | - |
116 | Q_UNREACHABLE(); | - |
117 | return QByteArray(); | - |
118 | } | - |
119 | | - |
120 | | - |
121 | | - |
122 | | - |
123 | QByteArray QSslKeyPrivate::pemFooter() const | - |
124 | { | - |
125 | if (type == QSsl::PublicKey) | - |
126 | return QByteArrayLiteral("-----END PUBLIC KEY-----"); | - |
127 | else if (algorithm == QSsl::Rsa) | - |
128 | return QByteArrayLiteral("-----END RSA PRIVATE KEY-----"); | - |
129 | else if (algorithm == QSsl::Dsa) | - |
130 | return QByteArrayLiteral("-----END DSA PRIVATE KEY-----"); | - |
131 | else if (algorithm == QSsl::Ec) | - |
132 | return QByteArrayLiteral("-----END EC PRIVATE KEY-----"); | - |
133 | | - |
134 | Q_UNREACHABLE(); | - |
135 | return QByteArray(); | - |
136 | } | - |
137 | | - |
138 | | - |
139 | | - |
140 | | - |
141 | | - |
142 | | - |
143 | QByteArray QSslKeyPrivate::pemFromDer(const QByteArray &der, const QMap<QByteArray, QByteArray> &headers) const | - |
144 | { | - |
145 | QByteArray pem(der.toBase64()); | - |
146 | | - |
147 | const int lineWidth = 64; | - |
148 | const int newLines = pem.size() / lineWidth; | - |
149 | const bool rem = pem.size() % lineWidth; | - |
150 | | - |
151 | | - |
152 | for (int i = 0; i < newLines; ++i) | - |
153 | pem.insert((i + 1) * lineWidth + i, '\n'); | - |
154 | if (rem) | - |
155 | pem.append('\n'); | - |
156 | | - |
157 | QByteArray extra; | - |
158 | if (!headers.isEmpty()) { | - |
159 | QMap<QByteArray, QByteArray>::const_iterator it = headers.constEnd(); | - |
160 | do { | - |
161 | --it; | - |
162 | extra += it.key() + ": " + it.value() + '\n'; | - |
163 | } while (it != headers.constBegin()); | - |
164 | extra += '\n'; | - |
165 | } | - |
166 | pem.prepend(pemHeader() + '\n' + extra); | - |
167 | pem.append(pemFooter() + '\n'); | - |
168 | | - |
169 | return pem; | - |
170 | } | - |
171 | | - |
172 | | - |
173 | | - |
174 | | - |
175 | | - |
176 | | - |
177 | QByteArray QSslKeyPrivate::derFromPem(const QByteArray &pem, QMap<QByteArray, QByteArray> *headers) const | - |
178 | { | - |
179 | const QByteArray header = pemHeader(); | - |
180 | const QByteArray footer = pemFooter(); | - |
181 | | - |
182 | QByteArray der(pem); | - |
183 | | - |
184 | const int headerIndex = der.indexOf(header); | - |
185 | const int footerIndex = der.indexOf(footer); | - |
186 | if (headerIndex == -1 || footerIndex == -1)TRUE | never evaluated | FALSE | evaluated 940 times by 2 testsEvaluated by:- tst_qsslcertificate - unknown status
- tst_qsslkey - unknown status
|
TRUE | never evaluated | FALSE | evaluated 940 times by 2 testsEvaluated by:- tst_qsslcertificate - unknown status
- tst_qsslkey - unknown status
|
| 0-940 |
187 | return QByteArray(); never executed: return QByteArray(); | 0 |
188 | | - |
189 | der = der.mid(headerIndex + header.size(), footerIndex - (headerIndex + header.size())); | - |
190 | | - |
191 | if (der.contains("Proc-Type:")) {TRUE | never evaluated | FALSE | evaluated 940 times by 2 testsEvaluated by:- tst_qsslcertificate - unknown status
- tst_qsslkey - unknown status
|
| 0-940 |
192 | | - |
193 | const QByteArrayMatcher lf("\n"); | - |
| const QByteArrayMatcher colon(":");int i = 0; | |
194 | while (i < der.count()) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
195 | int j = colonder.indexInindexOf(der':', i); | - |
196 | if (j == -1)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
197 | break; never executed: break; | 0 |
198 | const QByteArray field = der.mid(i, j - i).trimmed(); | - |
199 | j++; | - |
200 | | - |
201 | QByteArray value; | - |
202 | do { | - |
203 | i = lfder.indexInindexOf(der'\n', j); | - |
204 | if (i == -1)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
205 | break; never executed: break; | 0 |
206 | if (!value.isEmpty())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
207 | value += ' '; never executed: value += ' '; | 0 |
208 | | - |
209 | bool hasCR = (i && der[i-1] == '\r');TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
210 | int length = i -(hasCR ? 1: 0) - j;TRUE | never evaluated | FALSE | never evaluated |
| 0 |
211 | value += der.mid(j, length).trimmed(); | - |
212 | j = ++i; | - |
213 | } while (i < der.count() && (der.at(i) == ' ' || der.at(i) == '\t')); never executed: end of block TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
214 | if (i == -1)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
215 | break; never executed: break; | 0 |
216 | | - |
217 | headers->insert(field, value); | - |
218 | } never executed: end of block | 0 |
219 | der = der.mid(i); | - |
220 | } never executed: end of block | 0 |
221 | | - |
222 | return QByteArray::fromBase64(der); executed 940 times by 2 tests: return QByteArray::fromBase64(der); Executed by:- tst_qsslcertificate - unknown status
- tst_qsslkey - unknown status
| 940 |
223 | } | - |
224 | | - |
225 | | - |
226 | | - |
227 | | - |
228 | | - |
229 | | - |
230 | | - |
231 | | - |
232 | | - |
233 | | - |
234 | | - |
235 | | - |
236 | QSslKey::QSslKey(const QByteArray &encoded, QSsl::KeyAlgorithm algorithm, | - |
237 | QSsl::EncodingFormat encoding, QSsl::KeyType type, const QByteArray &passPhrase) | - |
238 | : d(new QSslKeyPrivate) | - |
239 | { | - |
240 | d->type = type; | - |
241 | d->algorithm = algorithm; | - |
242 | if (encoding == QSsl::Der) | - |
243 | d->decodeDer(encoded); | - |
244 | else | - |
245 | d->decodePem(encoded, passPhrase); | - |
246 | } | - |
247 | | - |
248 | | - |
249 | | - |
250 | | - |
251 | | - |
252 | | - |
253 | | - |
254 | | - |
255 | | - |
256 | | - |
257 | | - |
258 | | - |
259 | QSslKey::QSslKey(QIODevice *device, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat encoding, | - |
260 | QSsl::KeyType type, const QByteArray &passPhrase) | - |
261 | : d(new QSslKeyPrivate) | - |
262 | { | - |
263 | QByteArray encoded; | - |
264 | if (device) | - |
265 | encoded = device->readAll(); | - |
266 | d->type = type; | - |
267 | d->algorithm = algorithm; | - |
268 | if (encoding == QSsl::Der) | - |
269 | d->decodeDer(encoded); | - |
270 | else | - |
271 | d->decodePem(encoded, passPhrase); | - |
272 | } | - |
273 | | - |
274 | | - |
275 | | - |
276 | | - |
277 | | - |
278 | | - |
279 | | - |
280 | | - |
281 | | - |
282 | | - |
283 | QSslKey::QSslKey(Qt::HANDLE handle, QSsl::KeyType type) | - |
284 | : d(new QSslKeyPrivate) | - |
285 | { | - |
286 | #ifndef QT_NO_OPENSSL | - |
287 | d->opaque = reinterpret_cast<EVP_PKEY *>(handle); | - |
288 | #else | - |
289 | d->opaque = handle; | - |
290 | #endif | - |
291 | d->algorithm = QSsl::Opaque; | - |
292 | d->type = type; | - |
293 | d->isNull = !d->opaque; | - |
294 | } | - |
295 | | - |
296 | | - |
297 | | - |
298 | | - |
299 | QSslKey::QSslKey(const QSslKey &other) : d(other.d) | - |
300 | { | - |
301 | } | - |
302 | | - |
303 | | - |
304 | | - |
305 | | - |
306 | QSslKey::~QSslKey() | - |
307 | { | - |
308 | } | - |
309 | | - |
310 | | - |
311 | | - |
312 | | - |
313 | | - |
314 | | - |
315 | | - |
316 | QSslKey &QSslKey::operator=(const QSslKey &other) | - |
317 | { | - |
318 | d = other.d; | - |
319 | return *this; | - |
320 | } | - |
321 | | - |
322 | | - |
323 | | - |
324 | | - |
325 | | - |
326 | | - |
327 | | - |
328 | | - |
329 | | - |
330 | | - |
331 | | - |
332 | | - |
333 | | - |
334 | | - |
335 | bool QSslKey::isNull() const | - |
336 | { | - |
337 | return d->isNull; | - |
338 | } | - |
339 | | - |
340 | | - |
341 | | - |
342 | | - |
343 | | - |
344 | | - |
345 | void QSslKey::clear() | - |
346 | { | - |
347 | d = new QSslKeyPrivate; | - |
348 | } | - |
349 | | - |
350 | | - |
351 | | - |
352 | | - |
353 | int QSslKey::length() const | - |
354 | { | - |
355 | return d->length(); | - |
356 | } | - |
357 | | - |
358 | | - |
359 | | - |
360 | | - |
361 | QSsl::KeyType QSslKey::type() const | - |
362 | { | - |
363 | return d->type; | - |
364 | } | - |
365 | | - |
366 | | - |
367 | | - |
368 | | - |
369 | QSsl::KeyAlgorithm QSslKey::algorithm() const | - |
370 | { | - |
371 | return d->algorithm; | - |
372 | } | - |
373 | | - |
374 | | - |
375 | | - |
376 | | - |
377 | | - |
378 | | - |
379 | | - |
380 | QByteArray QSslKey::toDer(const QByteArray &passPhrase) const | - |
381 | { | - |
382 | if (d->isNull || d->algorithm == QSsl::Opaque) | - |
383 | return QByteArray(); | - |
384 | | - |
385 | | - |
386 | if (d->type == QSsl::PrivateKey && !passPhrase.isEmpty()) | - |
387 | return QByteArray(); | - |
388 | | - |
389 | #ifndef QT_NO_OPENSSL | - |
390 | QMap<QByteArray, QByteArray> headers; | - |
391 | return d->derFromPem(toPem(passPhrase), &headers); | - |
392 | #else | - |
393 | return d->derData; | - |
394 | #endif | - |
395 | } | - |
396 | | - |
397 | | - |
398 | | - |
399 | | - |
400 | | - |
401 | | - |
402 | QByteArray QSslKey::toPem(const QByteArray &passPhrase) const | - |
403 | { | - |
404 | return d->toPem(passPhrase); | - |
405 | } | - |
406 | | - |
407 | | - |
408 | | - |
409 | | - |
410 | | - |
411 | | - |
412 | | - |
413 | | - |
414 | | - |
415 | | - |
416 | | - |
417 | | - |
418 | Qt::HANDLE QSslKey::handle() const | - |
419 | { | - |
420 | return d->handle(); | - |
421 | } | - |
422 | | - |
423 | | - |
424 | | - |
425 | | - |
426 | bool QSslKey::operator==(const QSslKey &other) const | - |
427 | { | - |
428 | if (isNull()) | - |
429 | return other.isNull(); | - |
430 | if (other.isNull()) | - |
431 | return isNull(); | - |
432 | if (algorithm() != other.algorithm()) | - |
433 | return false; | - |
434 | if (type() != other.type()) | - |
435 | return false; | - |
436 | if (length() != other.length()) | - |
437 | return false; | - |
438 | if (algorithm() == QSsl::Opaque) | - |
439 | return handle() == other.handle(); | - |
440 | return toDer() == other.toDer(); | - |
441 | } | - |
442 | | - |
443 | | - |
444 | | - |
445 | | - |
446 | | - |
447 | | - |
448 | | - |
449 | #ifndef QT_NO_DEBUG_STREAM | - |
450 | QDebug operator<<(QDebug debug, const QSslKey &key) | - |
451 | { | - |
452 | QDebugStateSaver saver(debug); | - |
453 | debug.resetFormat().nospace(); | - |
454 | debug << "QSslKey(" | - |
455 | << (key.type() == QSsl::PublicKey ? "PublicKey" : "PrivateKey") | - |
456 | << ", " << (key.algorithm() == QSsl::Opaque ? "OPAQUE" : | - |
457 | (key.algorithm() == QSsl::Rsa ? "RSA" : ((key.algorithm() == QSsl::Dsa) ? "DSA" : "EC"))) | - |
458 | << ", " << key.length() | - |
459 | << ')'; | - |
460 | return debug; | - |
461 | } | - |
462 | #endif | - |
463 | | - |
464 | QT_END_NAMESPACE | - |
| | |