| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/ssl/qsslkey_p.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
| 16 | ** | - | ||||||||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
| 25 | ** | - | ||||||||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
| 29 | ** | - | ||||||||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 31 | ** | - | ||||||||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||||||||
| 33 | - | |||||||||||||||||||
| 34 | - | |||||||||||||||||||
| 35 | /*! | - | ||||||||||||||||||
| 36 | \class QSslKey | - | ||||||||||||||||||
| 37 | \brief The QSslKey class provides an interface for private and public keys. | - | ||||||||||||||||||
| 38 | \since 4.3 | - | ||||||||||||||||||
| 39 | - | |||||||||||||||||||
| 40 | \reentrant | - | ||||||||||||||||||
| 41 | \ingroup network | - | ||||||||||||||||||
| 42 | \ingroup ssl | - | ||||||||||||||||||
| 43 | \ingroup shared | - | ||||||||||||||||||
| 44 | \inmodule QtNetwork | - | ||||||||||||||||||
| 45 | - | |||||||||||||||||||
| 46 | QSslKey provides a simple API for managing keys. | - | ||||||||||||||||||
| 47 | - | |||||||||||||||||||
| 48 | \sa QSslSocket, QSslCertificate, QSslCipher | - | ||||||||||||||||||
| 49 | */ | - | ||||||||||||||||||
| 50 | - | |||||||||||||||||||
| 51 | #include "qsslkey.h" | - | ||||||||||||||||||
| 52 | #include "qsslkey_p.h" | - | ||||||||||||||||||
| 53 | #include "qsslsocket.h" | - | ||||||||||||||||||
| 54 | #include "qsslsocket_p.h" | - | ||||||||||||||||||
| 55 | - | |||||||||||||||||||
| 56 | #include <QtCore/qatomic.h> | - | ||||||||||||||||||
| 57 | #include <QtCore/qbytearray.h> | - | ||||||||||||||||||
| 58 | #include <QtCore/qbytearraymatcher.h> | - | ||||||||||||||||||
| 59 | #include <QtCore/qiodevice.h> | - | ||||||||||||||||||
| 60 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||||||||
| 61 | #include <QtCore/qdebug.h> | - | ||||||||||||||||||
| 62 | #endif | - | ||||||||||||||||||
| 63 | - | |||||||||||||||||||
| 64 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 65 | - | |||||||||||||||||||
| 66 | /*! | - | ||||||||||||||||||
| 67 | \fn void QSslKeyPrivate::clear(bool deep) | - | ||||||||||||||||||
| 68 | \internal | - | ||||||||||||||||||
| 69 | */ | - | ||||||||||||||||||
| 70 | - | |||||||||||||||||||
| 71 | /*! | - | ||||||||||||||||||
| 72 | \fn void QSslKeyPrivate::decodePem(const QByteArray &pem, const QByteArray &passPhrase, | - | ||||||||||||||||||
| 73 | bool deepClear) | - | ||||||||||||||||||
| 74 | \internal | - | ||||||||||||||||||
| 75 | - | |||||||||||||||||||
| 76 | Allocates a new rsa or dsa struct and decodes \a pem into it | - | ||||||||||||||||||
| 77 | according to the current algorithm and type. | - | ||||||||||||||||||
| 78 | - | |||||||||||||||||||
| 79 | If \a deepClear is true, the rsa/dsa struct is freed if it is was | - | ||||||||||||||||||
| 80 | already allocated, otherwise we "leak" memory (which is exactly | - | ||||||||||||||||||
| 81 | what we want for copy construction). | - | ||||||||||||||||||
| 82 | - | |||||||||||||||||||
| 83 | If \a passPhrase is non-empty, it will be used for decrypting | - | ||||||||||||||||||
| 84 | \a pem. | - | ||||||||||||||||||
| 85 | */ | - | ||||||||||||||||||
| 86 | - | |||||||||||||||||||
| 87 | /*! | - | ||||||||||||||||||
| 88 | Constructs a null key. | - | ||||||||||||||||||
| 89 | - | |||||||||||||||||||
| 90 | \sa isNull() | - | ||||||||||||||||||
| 91 | */ | - | ||||||||||||||||||
| 92 | QSslKey::QSslKey() | - | ||||||||||||||||||
| 93 | : d(new QSslKeyPrivate) | - | ||||||||||||||||||
| 94 | { | - | ||||||||||||||||||
| 95 | } executed 6636 times by 17 tests: end of blockExecuted by:
| 6636 | ||||||||||||||||||
| 96 | - | |||||||||||||||||||
| 97 | /*! | - | ||||||||||||||||||
| 98 | \internal | - | ||||||||||||||||||
| 99 | */ | - | ||||||||||||||||||
| 100 | QByteArray QSslKeyPrivate::pemHeader() const | - | ||||||||||||||||||
| 101 | { | - | ||||||||||||||||||
| 102 | if (type == QSsl::PublicKey)
| 576-590 | ||||||||||||||||||
| 103 | return QByteArrayLiteral("-----BEGIN PUBLIC KEY-----"); executed 590 times by 2 tests: 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; }());Executed by:
executed 590 times by 2 tests: return ba;Executed by:
| 590 | ||||||||||||||||||
| 104 | else if (algorithm == QSsl::Rsa)
| 287-289 | ||||||||||||||||||
| 105 | return QByteArrayLiteral("-----BEGIN RSA PRIVATE KEY-----"); executed 289 times by 2 tests: 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; }());Executed by:
executed 289 times by 2 tests: return ba;Executed by:
| 289 | ||||||||||||||||||
| 106 | else if (algorithm == QSsl::Dsa)
| 123-164 | ||||||||||||||||||
| 107 | return QByteArrayLiteral("-----BEGIN DSA PRIVATE KEY-----"); executed 164 times by 1 test: 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; }());Executed by:
executed 164 times by 1 test: return ba;Executed by:
| 164 | ||||||||||||||||||
| 108 | else if (algorithm == QSsl::Ec)
| 0-123 | ||||||||||||||||||
| 109 | return QByteArrayLiteral("-----BEGIN EC PRIVATE KEY-----"); executed 123 times by 1 test: 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; }());Executed by:
executed 123 times by 1 test: return ba;Executed by:
| 123 | ||||||||||||||||||
| 110 | - | |||||||||||||||||||
| 111 | Q_UNREACHABLE(); | - | ||||||||||||||||||
| 112 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||||||||
| 113 | } | - | ||||||||||||||||||
| 114 | - | |||||||||||||||||||
| 115 | /*! | - | ||||||||||||||||||
| 116 | \internal | - | ||||||||||||||||||
| 117 | */ | - | ||||||||||||||||||
| 118 | QByteArray QSslKeyPrivate::pemFooter() const | - | ||||||||||||||||||
| 119 | { | - | ||||||||||||||||||
| 120 | if (type == QSsl::PublicKey)
| 576-590 | ||||||||||||||||||
| 121 | return QByteArrayLiteral("-----END PUBLIC KEY-----"); executed 590 times by 2 tests: 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; }());Executed by:
executed 590 times by 2 tests: return ba;Executed by:
| 590 | ||||||||||||||||||
| 122 | else if (algorithm == QSsl::Rsa)
| 287-289 | ||||||||||||||||||
| 123 | return QByteArrayLiteral("-----END RSA PRIVATE KEY-----"); executed 289 times by 2 tests: 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; }());Executed by:
executed 289 times by 2 tests: return ba;Executed by:
| 289 | ||||||||||||||||||
| 124 | else if (algorithm == QSsl::Dsa)
| 123-164 | ||||||||||||||||||
| 125 | return QByteArrayLiteral("-----END DSA PRIVATE KEY-----"); executed 164 times by 1 test: 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; }());Executed by:
executed 164 times by 1 test: return ba;Executed by:
| 164 | ||||||||||||||||||
| 126 | else if (algorithm == QSsl::Ec)
| 0-123 | ||||||||||||||||||
| 127 | return QByteArrayLiteral("-----END EC PRIVATE KEY-----"); executed 123 times by 1 test: 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; }());Executed by:
executed 123 times by 1 test: return ba;Executed by:
| 123 | ||||||||||||||||||
| 128 | - | |||||||||||||||||||
| 129 | Q_UNREACHABLE(); | - | ||||||||||||||||||
| 130 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||||||||
| 131 | } | - | ||||||||||||||||||
| 132 | - | |||||||||||||||||||
| 133 | /*! | - | ||||||||||||||||||
| 134 | \internal | - | ||||||||||||||||||
| 135 | - | |||||||||||||||||||
| 136 | Returns a DER key formatted as PEM. | - | ||||||||||||||||||
| 137 | */ | - | ||||||||||||||||||
| 138 | QByteArray QSslKeyPrivate::pemFromDer(const QByteArray &der, const QMap<QByteArray, QByteArray> &headers) const | - | ||||||||||||||||||
| 139 | { | - | ||||||||||||||||||
| 140 | QByteArray pem(der.toBase64()); | - | ||||||||||||||||||
| 141 | - | |||||||||||||||||||
| 142 | const int lineWidth = 64; // RFC 1421 | - | ||||||||||||||||||
| 143 | const int newLines = pem.size() / lineWidth; | - | ||||||||||||||||||
| 144 | const bool rem = pem.size() % lineWidth; | - | ||||||||||||||||||
| 145 | - | |||||||||||||||||||
| 146 | // ### optimize | - | ||||||||||||||||||
| 147 | for (int i = 0; i < newLines; ++i)
| 226-1254 | ||||||||||||||||||
| 148 | pem.insert((i + 1) * lineWidth + i, '\n'); executed 1254 times by 2 tests: pem.insert((i + 1) * lineWidth + i, '\n');Executed by:
| 1254 | ||||||||||||||||||
| 149 | if (rem)
| 8-218 | ||||||||||||||||||
| 150 | pem.append('\n'); // ### executed 218 times by 2 tests: pem.append('\n');Executed by:
| 218 | ||||||||||||||||||
| 151 | - | |||||||||||||||||||
| 152 | QByteArray extra; | - | ||||||||||||||||||
| 153 | if (!headers.isEmpty()) {
| 0-226 | ||||||||||||||||||
| 154 | QMap<QByteArray, QByteArray>::const_iterator it = headers.constEnd(); | - | ||||||||||||||||||
| 155 | do { | - | ||||||||||||||||||
| 156 | --it; | - | ||||||||||||||||||
| 157 | extra += it.key() + ": " + it.value() + '\n'; | - | ||||||||||||||||||
| 158 | } while (it != headers.constBegin()); never executed: end of block
| 0 | ||||||||||||||||||
| 159 | extra += '\n'; | - | ||||||||||||||||||
| 160 | } never executed: end of block | 0 | ||||||||||||||||||
| 161 | pem.prepend(pemHeader() + '\n' + extra); | - | ||||||||||||||||||
| 162 | pem.append(pemFooter() + '\n'); | - | ||||||||||||||||||
| 163 | - | |||||||||||||||||||
| 164 | return pem; executed 226 times by 2 tests: return pem;Executed by:
| 226 | ||||||||||||||||||
| 165 | } | - | ||||||||||||||||||
| 166 | - | |||||||||||||||||||
| 167 | /*! | - | ||||||||||||||||||
| 168 | \internal | - | ||||||||||||||||||
| 169 | - | |||||||||||||||||||
| 170 | Returns a PEM key formatted as DER. | - | ||||||||||||||||||
| 171 | */ | - | ||||||||||||||||||
| 172 | QByteArray QSslKeyPrivate::derFromPem(const QByteArray &pem, QMap<QByteArray, QByteArray> *headers) const | - | ||||||||||||||||||
| 173 | { | - | ||||||||||||||||||
| 174 | const QByteArray header = pemHeader(); | - | ||||||||||||||||||
| 175 | const QByteArray footer = pemFooter(); | - | ||||||||||||||||||
| 176 | - | |||||||||||||||||||
| 177 | QByteArray der(pem); | - | ||||||||||||||||||
| 178 | - | |||||||||||||||||||
| 179 | const int headerIndex = der.indexOf(header); | - | ||||||||||||||||||
| 180 | const int footerIndex = der.indexOf(footer); | - | ||||||||||||||||||
| 181 | if (headerIndex == -1 || footerIndex == -1)
| 0-940 | ||||||||||||||||||
| 182 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||||||||
| 183 | - | |||||||||||||||||||
| 184 | der = der.mid(headerIndex + header.size(), footerIndex - (headerIndex + header.size())); | - | ||||||||||||||||||
| 185 | - | |||||||||||||||||||
| 186 | if (der.contains("Proc-Type:")) {
| 0-940 | ||||||||||||||||||
| 187 | // taken from QHttpNetworkReplyPrivate::parseHeader | - | ||||||||||||||||||
| 188 | const QByteArrayMatcher lf("\n"); | - | ||||||||||||||||||
| 189 | const QByteArrayMatcher colon(":"); | - | ||||||||||||||||||
| 190 | int i = 0; | - | ||||||||||||||||||
| 191 | while (i < der.count()) {
| 0 | ||||||||||||||||||
| 192 | int j = colon.indexIn(der, i); // field-name | - | ||||||||||||||||||
| 193 | if (j == -1)
| 0 | ||||||||||||||||||
| 194 | break; never executed: break; | 0 | ||||||||||||||||||
| 195 | const QByteArray field = der.mid(i, j - i).trimmed(); | - | ||||||||||||||||||
| 196 | j++; | - | ||||||||||||||||||
| 197 | // any number of LWS is allowed before and after the value | - | ||||||||||||||||||
| 198 | QByteArray value; | - | ||||||||||||||||||
| 199 | do { | - | ||||||||||||||||||
| 200 | i = lf.indexIn(der, j); | - | ||||||||||||||||||
| 201 | if (i == -1)
| 0 | ||||||||||||||||||
| 202 | break; never executed: break; | 0 | ||||||||||||||||||
| 203 | if (!value.isEmpty())
| 0 | ||||||||||||||||||
| 204 | value += ' '; never executed: value += ' '; | 0 | ||||||||||||||||||
| 205 | // check if we have CRLF or only LF | - | ||||||||||||||||||
| 206 | bool hasCR = (i && der[i-1] == '\r');
| 0 | ||||||||||||||||||
| 207 | int length = i -(hasCR ? 1: 0) - j;
| 0 | ||||||||||||||||||
| 208 | value += der.mid(j, length).trimmed(); | - | ||||||||||||||||||
| 209 | j = ++i; | - | ||||||||||||||||||
| 210 | } while (i < der.count() && (der.at(i) == ' ' || der.at(i) == '\t')); never executed: end of block
| 0 | ||||||||||||||||||
| 211 | if (i == -1)
| 0 | ||||||||||||||||||
| 212 | break; // something is wrong never executed: break; | 0 | ||||||||||||||||||
| 213 | - | |||||||||||||||||||
| 214 | headers->insert(field, value); | - | ||||||||||||||||||
| 215 | } never executed: end of block | 0 | ||||||||||||||||||
| 216 | der = der.mid(i); | - | ||||||||||||||||||
| 217 | } never executed: end of block | 0 | ||||||||||||||||||
| 218 | - | |||||||||||||||||||
| 219 | return QByteArray::fromBase64(der); // ignores newlines executed 940 times by 2 tests: return QByteArray::fromBase64(der);Executed by:
| 940 | ||||||||||||||||||
| 220 | } | - | ||||||||||||||||||
| 221 | - | |||||||||||||||||||
| 222 | /*! | - | ||||||||||||||||||
| 223 | Constructs a QSslKey by decoding the string in the byte array | - | ||||||||||||||||||
| 224 | \a encoded using a specified \a algorithm and \a encoding format. | - | ||||||||||||||||||
| 225 | \a type specifies whether the key is public or private. | - | ||||||||||||||||||
| 226 | - | |||||||||||||||||||
| 227 | If the key is encoded as PEM and encrypted, \a passPhrase is used | - | ||||||||||||||||||
| 228 | to decrypt it. | - | ||||||||||||||||||
| 229 | - | |||||||||||||||||||
| 230 | After construction, use isNull() to check if \a encoded contained | - | ||||||||||||||||||
| 231 | a valid key. | - | ||||||||||||||||||
| 232 | */ | - | ||||||||||||||||||
| 233 | QSslKey::QSslKey(const QByteArray &encoded, QSsl::KeyAlgorithm algorithm, | - | ||||||||||||||||||
| 234 | QSsl::EncodingFormat encoding, QSsl::KeyType type, const QByteArray &passPhrase) | - | ||||||||||||||||||
| 235 | : d(new QSslKeyPrivate) | - | ||||||||||||||||||
| 236 | { | - | ||||||||||||||||||
| 237 | d->type = type; | - | ||||||||||||||||||
| 238 | d->algorithm = algorithm; | - | ||||||||||||||||||
| 239 | if (encoding == QSsl::Der)
| 226-415 | ||||||||||||||||||
| 240 | d->decodeDer(encoded); executed 226 times by 2 tests: d->decodeDer(encoded);Executed by:
| 226 | ||||||||||||||||||
| 241 | else | - | ||||||||||||||||||
| 242 | d->decodePem(encoded, passPhrase); executed 415 times by 4 tests: d->decodePem(encoded, passPhrase);Executed by:
| 415 | ||||||||||||||||||
| 243 | } | - | ||||||||||||||||||
| 244 | - | |||||||||||||||||||
| 245 | /*! | - | ||||||||||||||||||
| 246 | Constructs a QSslKey by reading and decoding data from a | - | ||||||||||||||||||
| 247 | \a device using a specified \a algorithm and \a encoding format. | - | ||||||||||||||||||
| 248 | \a type specifies whether the key is public or private. | - | ||||||||||||||||||
| 249 | - | |||||||||||||||||||
| 250 | If the key is encoded as PEM and encrypted, \a passPhrase is used | - | ||||||||||||||||||
| 251 | to decrypt it. | - | ||||||||||||||||||
| 252 | - | |||||||||||||||||||
| 253 | After construction, use isNull() to check if \a device provided | - | ||||||||||||||||||
| 254 | a valid key. | - | ||||||||||||||||||
| 255 | */ | - | ||||||||||||||||||
| 256 | QSslKey::QSslKey(QIODevice *device, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat encoding, | - | ||||||||||||||||||
| 257 | QSsl::KeyType type, const QByteArray &passPhrase) | - | ||||||||||||||||||
| 258 | : d(new QSslKeyPrivate) | - | ||||||||||||||||||
| 259 | { | - | ||||||||||||||||||
| 260 | QByteArray encoded; | - | ||||||||||||||||||
| 261 | if (device)
| 0-16 | ||||||||||||||||||
| 262 | encoded = device->readAll(); executed 16 times by 2 tests: encoded = device->readAll();Executed by:
| 16 | ||||||||||||||||||
| 263 | d->type = type; | - | ||||||||||||||||||
| 264 | d->algorithm = algorithm; | - | ||||||||||||||||||
| 265 | if (encoding == QSsl::Der)
| 0-16 | ||||||||||||||||||
| 266 | d->decodeDer(encoded); never executed: d->decodeDer(encoded); | 0 | ||||||||||||||||||
| 267 | else | - | ||||||||||||||||||
| 268 | d->decodePem(encoded, passPhrase); executed 16 times by 2 tests: d->decodePem(encoded, passPhrase);Executed by:
| 16 | ||||||||||||||||||
| 269 | } | - | ||||||||||||||||||
| 270 | - | |||||||||||||||||||
| 271 | /*! | - | ||||||||||||||||||
| 272 | \since 5.0 | - | ||||||||||||||||||
| 273 | Constructs a QSslKey from a valid native key \a handle. | - | ||||||||||||||||||
| 274 | \a type specifies whether the key is public or private. | - | ||||||||||||||||||
| 275 | - | |||||||||||||||||||
| 276 | QSslKey will take ownership for this key and you must not | - | ||||||||||||||||||
| 277 | free the key using the native library. The algorithm used | - | ||||||||||||||||||
| 278 | when creating a key from a handle will always be QSsl::Opaque. | - | ||||||||||||||||||
| 279 | */ | - | ||||||||||||||||||
| 280 | QSslKey::QSslKey(Qt::HANDLE handle, QSsl::KeyType type) | - | ||||||||||||||||||
| 281 | : d(new QSslKeyPrivate) | - | ||||||||||||||||||
| 282 | { | - | ||||||||||||||||||
| 283 | #ifndef QT_NO_OPENSSL | - | ||||||||||||||||||
| 284 | d->opaque = reinterpret_cast<EVP_PKEY *>(handle); | - | ||||||||||||||||||
| 285 | #else | - | ||||||||||||||||||
| 286 | d->opaque = handle; | - | ||||||||||||||||||
| 287 | #endif | - | ||||||||||||||||||
| 288 | d->algorithm = QSsl::Opaque; | - | ||||||||||||||||||
| 289 | d->type = type; | - | ||||||||||||||||||
| 290 | d->isNull = !d->opaque; | - | ||||||||||||||||||
| 291 | } executed 5 times by 1 test: end of blockExecuted by:
| 5 | ||||||||||||||||||
| 292 | - | |||||||||||||||||||
| 293 | /*! | - | ||||||||||||||||||
| 294 | Constructs an identical copy of \a other. | - | ||||||||||||||||||
| 295 | */ | - | ||||||||||||||||||
| 296 | QSslKey::QSslKey(const QSslKey &other) : d(other.d) | - | ||||||||||||||||||
| 297 | { | - | ||||||||||||||||||
| 298 | } executed 1588 times by 9 tests: end of blockExecuted by:
| 1588 | ||||||||||||||||||
| 299 | - | |||||||||||||||||||
| 300 | /*! | - | ||||||||||||||||||
| 301 | Destroys the QSslKey object. | - | ||||||||||||||||||
| 302 | */ | - | ||||||||||||||||||
| 303 | QSslKey::~QSslKey() | - | ||||||||||||||||||
| 304 | { | - | ||||||||||||||||||
| 305 | } | - | ||||||||||||||||||
| 306 | - | |||||||||||||||||||
| 307 | /*! | - | ||||||||||||||||||
| 308 | Copies the contents of \a other into this key, making the two keys | - | ||||||||||||||||||
| 309 | identical. | - | ||||||||||||||||||
| 310 | - | |||||||||||||||||||
| 311 | Returns a reference to this QSslKey. | - | ||||||||||||||||||
| 312 | */ | - | ||||||||||||||||||
| 313 | QSslKey &QSslKey::operator=(const QSslKey &other) | - | ||||||||||||||||||
| 314 | { | - | ||||||||||||||||||
| 315 | d = other.d; | - | ||||||||||||||||||
| 316 | return *this; executed 796 times by 8 tests: return *this;Executed by:
| 796 | ||||||||||||||||||
| 317 | } | - | ||||||||||||||||||
| 318 | - | |||||||||||||||||||
| 319 | /*! | - | ||||||||||||||||||
| 320 | \fn void QSslKey::swap(QSslKey &other) | - | ||||||||||||||||||
| 321 | \since 5.0 | - | ||||||||||||||||||
| 322 | - | |||||||||||||||||||
| 323 | Swaps this ssl key with \a other. This function is very fast and | - | ||||||||||||||||||
| 324 | never fails. | - | ||||||||||||||||||
| 325 | */ | - | ||||||||||||||||||
| 326 | - | |||||||||||||||||||
| 327 | /*! | - | ||||||||||||||||||
| 328 | Returns \c true if this is a null key; otherwise false. | - | ||||||||||||||||||
| 329 | - | |||||||||||||||||||
| 330 | \sa clear() | - | ||||||||||||||||||
| 331 | */ | - | ||||||||||||||||||
| 332 | bool QSslKey::isNull() const | - | ||||||||||||||||||
| 333 | { | - | ||||||||||||||||||
| 334 | return d->isNull; executed 1319 times by 6 tests: return d->isNull;Executed by:
| 1319 | ||||||||||||||||||
| 335 | } | - | ||||||||||||||||||
| 336 | - | |||||||||||||||||||
| 337 | /*! | - | ||||||||||||||||||
| 338 | Clears the contents of this key, making it a null key. | - | ||||||||||||||||||
| 339 | - | |||||||||||||||||||
| 340 | \sa isNull() | - | ||||||||||||||||||
| 341 | */ | - | ||||||||||||||||||
| 342 | void QSslKey::clear() | - | ||||||||||||||||||
| 343 | { | - | ||||||||||||||||||
| 344 | d = new QSslKeyPrivate; | - | ||||||||||||||||||
| 345 | } never executed: end of block | 0 | ||||||||||||||||||
| 346 | - | |||||||||||||||||||
| 347 | /*! | - | ||||||||||||||||||
| 348 | Returns the length of the key in bits, or -1 if the key is null. | - | ||||||||||||||||||
| 349 | */ | - | ||||||||||||||||||
| 350 | int QSslKey::length() const | - | ||||||||||||||||||
| 351 | { | - | ||||||||||||||||||
| 352 | return d->length(); executed 745 times by 2 tests: return d->length();Executed by:
| 745 | ||||||||||||||||||
| 353 | } | - | ||||||||||||||||||
| 354 | - | |||||||||||||||||||
| 355 | /*! | - | ||||||||||||||||||
| 356 | Returns the type of the key (i.e., PublicKey or PrivateKey). | - | ||||||||||||||||||
| 357 | */ | - | ||||||||||||||||||
| 358 | QSsl::KeyType QSslKey::type() const | - | ||||||||||||||||||
| 359 | { | - | ||||||||||||||||||
| 360 | return d->type; executed 688 times by 2 tests: return d->type;Executed by:
| 688 | ||||||||||||||||||
| 361 | } | - | ||||||||||||||||||
| 362 | - | |||||||||||||||||||
| 363 | /*! | - | ||||||||||||||||||
| 364 | Returns the key algorithm. | - | ||||||||||||||||||
| 365 | */ | - | ||||||||||||||||||
| 366 | QSsl::KeyAlgorithm QSslKey::algorithm() const | - | ||||||||||||||||||
| 367 | { | - | ||||||||||||||||||
| 368 | return d->algorithm; executed 1174 times by 4 tests: return d->algorithm;Executed by:
| 1174 | ||||||||||||||||||
| 369 | } | - | ||||||||||||||||||
| 370 | - | |||||||||||||||||||
| 371 | /*! | - | ||||||||||||||||||
| 372 | Returns the key in DER encoding. | - | ||||||||||||||||||
| 373 | - | |||||||||||||||||||
| 374 | The \a passPhrase argument should be omitted as DER cannot be | - | ||||||||||||||||||
| 375 | encrypted. It will be removed in a future version of Qt. | - | ||||||||||||||||||
| 376 | */ | - | ||||||||||||||||||
| 377 | QByteArray QSslKey::toDer(const QByteArray &passPhrase) const | - | ||||||||||||||||||
| 378 | { | - | ||||||||||||||||||
| 379 | if (d->isNull || d->algorithm == QSsl::Opaque)
| 0-1052 | ||||||||||||||||||
| 380 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||||||||
| 381 | - | |||||||||||||||||||
| 382 | // Encrypted DER is nonsense, see QTBUG-41038. | - | ||||||||||||||||||
| 383 | if (d->type == QSsl::PrivateKey && !passPhrase.isEmpty())
| 112-576 | ||||||||||||||||||
| 384 | return QByteArray(); executed 112 times by 1 test: return QByteArray();Executed by:
| 112 | ||||||||||||||||||
| 385 | - | |||||||||||||||||||
| 386 | #ifndef QT_NO_OPENSSL | - | ||||||||||||||||||
| 387 | QMap<QByteArray, QByteArray> headers; | - | ||||||||||||||||||
| 388 | return d->derFromPem(toPem(passPhrase), &headers); executed 940 times by 2 tests: return d->derFromPem(toPem(passPhrase), &headers);Executed by:
| 940 | ||||||||||||||||||
| 389 | #else | - | ||||||||||||||||||
| 390 | return d->derData; | - | ||||||||||||||||||
| 391 | #endif | - | ||||||||||||||||||
| 392 | } | - | ||||||||||||||||||
| 393 | - | |||||||||||||||||||
| 394 | /*! | - | ||||||||||||||||||
| 395 | Returns the key in PEM encoding. The result is encrypted with | - | ||||||||||||||||||
| 396 | \a passPhrase if the key is a private key and \a passPhrase is | - | ||||||||||||||||||
| 397 | non-empty. | - | ||||||||||||||||||
| 398 | */ | - | ||||||||||||||||||
| 399 | QByteArray QSslKey::toPem(const QByteArray &passPhrase) const | - | ||||||||||||||||||
| 400 | { | - | ||||||||||||||||||
| 401 | return d->toPem(passPhrase); executed 1752 times by 2 tests: return d->toPem(passPhrase);Executed by:
| 1752 | ||||||||||||||||||
| 402 | } | - | ||||||||||||||||||
| 403 | - | |||||||||||||||||||
| 404 | /*! | - | ||||||||||||||||||
| 405 | Returns a pointer to the native key handle, if it is available; | - | ||||||||||||||||||
| 406 | otherwise a null pointer is returned. | - | ||||||||||||||||||
| 407 | - | |||||||||||||||||||
| 408 | You can use this handle together with the native API to access | - | ||||||||||||||||||
| 409 | extended information about the key. | - | ||||||||||||||||||
| 410 | - | |||||||||||||||||||
| 411 | \warning Use of this function has a high probability of being | - | ||||||||||||||||||
| 412 | non-portable, and its return value may vary across platforms, and | - | ||||||||||||||||||
| 413 | between minor Qt releases. | - | ||||||||||||||||||
| 414 | */ | - | ||||||||||||||||||
| 415 | Qt::HANDLE QSslKey::handle() const | - | ||||||||||||||||||
| 416 | { | - | ||||||||||||||||||
| 417 | return d->handle(); executed 91 times by 2 tests: return d->handle();Executed by:
| 91 | ||||||||||||||||||
| 418 | } | - | ||||||||||||||||||
| 419 | - | |||||||||||||||||||
| 420 | /*! | - | ||||||||||||||||||
| 421 | Returns \c true if this key is equal to \a other; otherwise returns \c false. | - | ||||||||||||||||||
| 422 | */ | - | ||||||||||||||||||
| 423 | bool QSslKey::operator==(const QSslKey &other) const | - | ||||||||||||||||||
| 424 | { | - | ||||||||||||||||||
| 425 | if (isNull())
| 63-232 | ||||||||||||||||||
| 426 | return other.isNull(); executed 63 times by 4 tests: return other.isNull();Executed by:
| 63 | ||||||||||||||||||
| 427 | if (other.isNull())
| 0-232 | ||||||||||||||||||
| 428 | return isNull(); never executed: return isNull(); | 0 | ||||||||||||||||||
| 429 | if (algorithm() != other.algorithm())
| 0-232 | ||||||||||||||||||
| 430 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 431 | if (type() != other.type())
| 0-232 | ||||||||||||||||||
| 432 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 433 | if (length() != other.length())
| 0-232 | ||||||||||||||||||
| 434 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 435 | if (algorithm() == QSsl::Opaque)
| 0-232 | ||||||||||||||||||
| 436 | return handle() == other.handle(); never executed: return handle() == other.handle(); | 0 | ||||||||||||||||||
| 437 | return toDer() == other.toDer(); executed 232 times by 2 tests: return toDer() == other.toDer();Executed by:
| 232 | ||||||||||||||||||
| 438 | } | - | ||||||||||||||||||
| 439 | - | |||||||||||||||||||
| 440 | /*! \fn bool QSslKey::operator!=(const QSslKey &other) const | - | ||||||||||||||||||
| 441 | - | |||||||||||||||||||
| 442 | Returns \c true if this key is not equal to key \a other; otherwise | - | ||||||||||||||||||
| 443 | returns \c false. | - | ||||||||||||||||||
| 444 | */ | - | ||||||||||||||||||
| 445 | - | |||||||||||||||||||
| 446 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||||||||
| 447 | QDebug operator<<(QDebug debug, const QSslKey &key) | - | ||||||||||||||||||
| 448 | { | - | ||||||||||||||||||
| 449 | QDebugStateSaver saver(debug); | - | ||||||||||||||||||
| 450 | debug.resetFormat().nospace(); | - | ||||||||||||||||||
| 451 | debug << "QSslKey(" | - | ||||||||||||||||||
| 452 | << (key.type() == QSsl::PublicKey ? "PublicKey" : "PrivateKey") | - | ||||||||||||||||||
| 453 | << ", " << (key.algorithm() == QSsl::Opaque ? "OPAQUE" : | - | ||||||||||||||||||
| 454 | (key.algorithm() == QSsl::Rsa ? "RSA" : ((key.algorithm() == QSsl::Dsa) ? "DSA" : "EC"))) | - | ||||||||||||||||||
| 455 | << ", " << key.length() | - | ||||||||||||||||||
| 456 | << ')'; | - | ||||||||||||||||||
| 457 | return debug; never executed: return debug; | 0 | ||||||||||||||||||
| 458 | } | - | ||||||||||||||||||
| 459 | #endif | - | ||||||||||||||||||
| 460 | - | |||||||||||||||||||
| 461 | QT_END_NAMESPACE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |