qsslkey_p.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/network/ssl/qsslkey_p.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6-
7-
8QSslKey::QSslKey()-
9 : d(new QSslKeyPrivate)-
10{-
11}-
12-
13-
14-
15-
16QByteArray QSslKeyPrivate::pemHeader() const-
17{-
18 if (type == QSsl::PublicKey)-
19 return ([]() -> QByteArray { enum { Size = sizeof("-----BEGIN PUBLIC KEY-----") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "-----BEGIN PUBLIC KEY-----" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());-
20 else if (algorithm == QSsl::Rsa)-
21 return ([]() -> QByteArray { enum { Size = sizeof("-----BEGIN RSA PRIVATE KEY-----") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "-----BEGIN RSA PRIVATE KEY-----" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());-
22 else if (algorithm == QSsl::Dsa)-
23 return ([]() -> QByteArray { enum { Size = sizeof("-----BEGIN DSA PRIVATE KEY-----") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "-----BEGIN DSA PRIVATE KEY-----" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());-
24 else if (algorithm == QSsl::Ec)-
25 return ([]() -> QByteArray { enum { Size = sizeof("-----BEGIN EC PRIVATE KEY-----") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "-----BEGIN EC PRIVATE KEY-----" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());-
26-
27 do { ((!(false)) ? qt_assert_x("Q_UNREACHABLE()", "Q_UNREACHABLE was reached",__FILE__,111116) : qt_noop()); __builtin_unreachable(); } while (0);-
28 return QByteArray();-
29}-
30-
31-
32-
33-
34QByteArray QSslKeyPrivate::pemFooter() const-
35{-
36 if (type == QSsl::PublicKey)-
37 return ([]() -> QByteArray { enum { Size = sizeof("-----END PUBLIC KEY-----") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "-----END PUBLIC KEY-----" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());-
38 else if (algorithm == QSsl::Rsa)-
39 return ([]() -> QByteArray { enum { Size = sizeof("-----END RSA PRIVATE KEY-----") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "-----END RSA PRIVATE KEY-----" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());-
40 else if (algorithm == QSsl::Dsa)-
41 return ([]() -> QByteArray { enum { Size = sizeof("-----END DSA PRIVATE KEY-----") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "-----END DSA PRIVATE KEY-----" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());-
42 else if (algorithm == QSsl::Ec)-
43 return ([]() -> QByteArray { enum { Size = sizeof("-----END EC PRIVATE KEY-----") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "-----END EC PRIVATE KEY-----" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());-
44-
45 do { ((!(false)) ? qt_assert_x("Q_UNREACHABLE()", "Q_UNREACHABLE was reached",__FILE__,129134) : qt_noop()); __builtin_unreachable(); } while (0);-
46 return QByteArray();-
47}-
48-
49-
50-
51-
52-
53-
54QByteArray QSslKeyPrivate::pemFromDer(const QByteArray &der, const QMap<QByteArray, QByteArray> &headers) const-
55{-
56 QByteArray pem(der.toBase64());-
57-
58 const int lineWidth = 64;-
59 const int newLines = pem.size() / lineWidth;-
60 const bool rem = pem.size() % lineWidth;-
61-
62-
63 for (int i = 0; i < newLines; ++i)-
64 pem.insert((i + 1) * lineWidth + i, '\n');-
65 if (rem)-
66 pem.append('\n');-
67-
68 QByteArray extra;-
69 if (!headers.isEmpty()) {-
70 QMap<QByteArray, QByteArray>::const_iterator it = headers.constEnd();-
71 do {-
72 --it;-
73 extra += it.key() + ": " + it.value() + '\n';-
74 } while (it != headers.constBegin());-
75 extra += '\n';-
76 }-
77 pem.prepend(pemHeader() + '\n' + extra);-
78 pem.append(pemFooter() + '\n');-
79-
80 return pem;-
81}-
82-
83-
84-
85-
86-
87-
88QByteArray QSslKeyPrivate::derFromPem(const QByteArray &pem, QMap<QByteArray, QByteArray> *headers) const-
89{-
90 const QByteArray header = pemHeader();-
91 const QByteArray footer = pemFooter();-
92-
93 QByteArray der(pem);-
94-
95 const int headerIndex = der.indexOf(header);-
96 const int footerIndex = der.indexOf(footer);-
97 if (headerIndex == -1
headerIndex == -1Description
TRUEnever evaluated
FALSEevaluated 940 times by 2 tests
Evaluated by:
  • tst_qsslcertificate - unknown status
  • tst_qsslkey - unknown status
|| footerIndex == -1
footerIndex == -1Description
TRUEnever evaluated
FALSEevaluated 940 times by 2 tests
Evaluated by:
  • tst_qsslcertificate - unknown status
  • tst_qsslkey - unknown status
)
0-940
98 return
never executed: return QByteArray();
QByteArray();
never executed: return QByteArray();
0
99-
100 der = der.mid(headerIndex + header.size(), footerIndex - (headerIndex + header.size()));-
101-
102 if (der.contains("Proc-Type:")
der.contains("Proc-Type:")Description
TRUEnever evaluated
FALSEevaluated 940 times by 2 tests
Evaluated by:
  • tst_qsslcertificate - unknown status
  • tst_qsslkey - unknown status
) {
0-940
103-
104 const QByteArrayMatcher lf("\n");-
const QByteArrayMatcher colon(":");int i = 0;
105 while (i < der.count()
i < der.count()Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
106 int j = colonder.indexInindexOf(der':', i);-
107 if (j == -1
j == -1Description
TRUEnever evaluated
FALSEnever evaluated
)
0
108 break;
never executed: break;
0
109 const QByteArray field = der.mid(i, j - i).trimmed();-
110 j++;-
111-
112 QByteArray value;-
113 do {-
114 i = lfder.indexInindexOf(der'\n', j);-
115 if (i == -1
i == -1Description
TRUEnever evaluated
FALSEnever evaluated
)
0
116 break;
never executed: break;
0
117 if (!value.isEmpty()
!value.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
118 value += ' ';
never executed: value += ' ';
0
119-
120 bool hasCR = (i
iDescription
TRUEnever evaluated
FALSEnever evaluated
&& der[i-1] == '\r'
der[i-1] == '\r'Description
TRUEnever evaluated
FALSEnever evaluated
);
0
121 int length = i -(hasCR
hasCRDescription
TRUEnever evaluated
FALSEnever evaluated
? 1: 0) - j;
0
122 value += der.mid(j, length).trimmed();-
123 j = ++i;-
124 }
never executed: end of block
while (i < der.count()
i < der.count()Description
TRUEnever evaluated
FALSEnever evaluated
&& (der.at(i) == ' '
der.at(i) == ' 'Description
TRUEnever evaluated
FALSEnever evaluated
|| der.at(i) == '\t'
der.at(i) == '\t'Description
TRUEnever evaluated
FALSEnever evaluated
));
0
125 if (i == -1
i == -1Description
TRUEnever evaluated
FALSEnever evaluated
)
0
126 break;
never executed: break;
0
127-
128 headers->insert(field, value);-
129 }
never executed: end of block
0
130 der = der.mid(i);-
131 }
never executed: end of block
0
132-
133 return
executed 940 times by 2 tests: return QByteArray::fromBase64(der);
Executed by:
  • tst_qsslcertificate - unknown status
  • tst_qsslkey - unknown status
QByteArray::fromBase64(der);
executed 940 times by 2 tests: return QByteArray::fromBase64(der);
Executed by:
  • tst_qsslcertificate - unknown status
  • tst_qsslkey - unknown status
940
134}-
135QSslKey::QSslKey(const QByteArray &encoded, QSsl::KeyAlgorithm algorithm,-
136 QSsl::EncodingFormat encoding, QSsl::KeyType type, const QByteArray &passPhrase)-
137 : d(new QSslKeyPrivate)-
138{-
139 d->type = type;-
140 d->algorithm = algorithm;-
141 if (encoding == QSsl::Der)-
142 d->decodeDer(encoded);-
143 else-
144 d->decodePem(encoded, passPhrase);-
145}-
146QSslKey::QSslKey(QIODevice *device, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat encoding,-
147 QSsl::KeyType type, const QByteArray &passPhrase)-
148 : d(new QSslKeyPrivate)-
149{-
150 QByteArray encoded;-
151 if (device)-
152 encoded = device->readAll();-
153 d->type = type;-
154 d->algorithm = algorithm;-
155 if (encoding == QSsl::Der)-
156 d->decodeDer(encoded);-
157 else-
158 d->decodePem(encoded, passPhrase);-
159}-
160QSslKey::QSslKey(Qt::HANDLE handle, QSsl::KeyType type)-
161 : d(new QSslKeyPrivate)-
162{-
163-
164 d->opaque = reinterpret_cast<EVP_PKEY *>(handle);-
165-
166-
167-
168 d->algorithm = QSsl::Opaque;-
169 d->type = type;-
170 d->isNull = !d->opaque;-
171}-
172-
173-
174-
175-
176QSslKey::QSslKey(const QSslKey &other) : d(other.d)-
177{-
178}-
179-
180-
181-
182-
183QSslKey::~QSslKey()-
184{-
185}-
186-
187-
188-
189-
190-
191-
192-
193QSslKey &QSslKey::operator=(const QSslKey &other)-
194{-
195 d = other.d;-
196 return *this;-
197}-
198bool QSslKey::isNull() const-
199{-
200 return d->isNull;-
201}-
202-
203-
204-
205-
206-
207-
208void QSslKey::clear()-
209{-
210 d = new QSslKeyPrivate;-
211}-
212-
213-
214-
215-
216int QSslKey::length() const-
217{-
218 return d->length();-
219}-
220-
221-
222-
223-
224QSsl::KeyType QSslKey::type() const-
225{-
226 return d->type;-
227}-
228-
229-
230-
231-
232QSsl::KeyAlgorithm QSslKey::algorithm() const-
233{-
234 return d->algorithm;-
235}-
236-
237-
238-
239-
240-
241-
242-
243QByteArray QSslKey::toDer(const QByteArray &passPhrase) const-
244{-
245 if (d->isNull || d->algorithm == QSsl::Opaque)-
246 return QByteArray();-
247-
248-
249 if (d->type == QSsl::PrivateKey && !passPhrase.isEmpty())-
250 return QByteArray();-
251-
252-
253 QMap<QByteArray, QByteArray> headers;-
254 return d->derFromPem(toPem(passPhrase), &headers);-
255-
256-
257-
258}-
259-
260-
261-
262-
263-
264-
265QByteArray QSslKey::toPem(const QByteArray &passPhrase) const-
266{-
267 return d->toPem(passPhrase);-
268}-
269Qt::HANDLE QSslKey::handle() const-
270{-
271 return d->handle();-
272}-
273-
274-
275-
276-
277bool QSslKey::operator==(const QSslKey &other) const-
278{-
279 if (isNull())-
280 return other.isNull();-
281 if (other.isNull())-
282 return isNull();-
283 if (algorithm() != other.algorithm())-
284 return false;-
285 if (type() != other.type())-
286 return false;-
287 if (length() != other.length())-
288 return false;-
289 if (algorithm() == QSsl::Opaque)-
290 return handle() == other.handle();-
291 return toDer() == other.toDer();-
292}-
293QDebug operator<<(QDebug debug, const QSslKey &key)-
294{-
295 QDebugStateSaver saver(debug);-
296 debug.resetFormat().nospace();-
297 debug << "QSslKey("-
298 << (key.type() == QSsl::PublicKey ? "PublicKey" : "PrivateKey")-
299 << ", " << (key.algorithm() == QSsl::Opaque ? "OPAQUE" :-
300 (key.algorithm() == QSsl::Rsa ? "RSA" : ((key.algorithm() == QSsl::Dsa) ? "DSA" : "EC")))-
301 << ", " << key.length()-
302 << ')';-
303 return debug;-
304}-
305-
306-
307-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9