| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/ssl/qsslcertificate_openssl.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
| 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 https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at https://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 3 as published by the Free Software | - | ||||||||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
| 24 | ** | - | ||||||||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
| 35 | ** | - | ||||||||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 37 | ** | - | ||||||||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||||||||
| 39 | - | |||||||||||||||||||
| 40 | #include "qssl_p.h" | - | ||||||||||||||||||
| 41 | #include "qsslsocket_openssl_symbols_p.h" | - | ||||||||||||||||||
| 42 | #include "qsslcertificate_p.h" | - | ||||||||||||||||||
| 43 | #include "qsslkey_p.h" | - | ||||||||||||||||||
| 44 | #include "qsslcertificateextension_p.h" | - | ||||||||||||||||||
| 45 | - | |||||||||||||||||||
| 46 | #include <QtCore/private/qmutexpool_p.h> | - | ||||||||||||||||||
| 47 | - | |||||||||||||||||||
| 48 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 49 | - | |||||||||||||||||||
| 50 | // forward declaration | - | ||||||||||||||||||
| 51 | static QMap<QByteArray, QString> _q_mapFromX509Name(X509_NAME *name); | - | ||||||||||||||||||
| 52 | - | |||||||||||||||||||
| 53 | bool QSslCertificate::operator==(const QSslCertificate &other) const | - | ||||||||||||||||||
| 54 | { | - | ||||||||||||||||||
| 55 | if (d == other.d)
| 51-7118 | ||||||||||||||||||
| 56 | return true; executed 51 times by 4 tests: return true;Executed by:
| 51 | ||||||||||||||||||
| 57 | if (d->null && other.d->null)
| 27-7044 | ||||||||||||||||||
| 58 | return true; executed 47 times by 3 tests: return true;Executed by:
| 47 | ||||||||||||||||||
| 59 | if (d->x509 && other.d->x509)
| 4-7044 | ||||||||||||||||||
| 60 | return q_X509_cmp(d->x509, other.d->x509) == 0; executed 7040 times by 3 tests: return q_X509_cmp(d->x509, other.d->x509) == 0;Executed by:
| 7040 | ||||||||||||||||||
| 61 | return false; executed 31 times by 2 tests: return false;Executed by:
| 31 | ||||||||||||||||||
| 62 | } | - | ||||||||||||||||||
| 63 | - | |||||||||||||||||||
| 64 | uint qHash(const QSslCertificate &key, uint seed) Q_DECL_NOTHROW | - | ||||||||||||||||||
| 65 | { | - | ||||||||||||||||||
| 66 | if (X509 * const x509 = key.d->x509) {
| 0-4 | ||||||||||||||||||
| 67 | (void)q_X509_cmp(x509, x509); // populate x509->sha1_hash | - | ||||||||||||||||||
| 68 | // (if someone knows a better way...) | - | ||||||||||||||||||
| 69 | return qHashBits(x509->sha1_hash, SHA_DIGEST_LENGTH, seed); never executed: return qHashBits(x509->sha1_hash, 20, seed); | 0 | ||||||||||||||||||
| 70 | } else { | - | ||||||||||||||||||
| 71 | return seed; executed 4 times by 2 tests: return seed;Executed by:
| 4 | ||||||||||||||||||
| 72 | } | - | ||||||||||||||||||
| 73 | } | - | ||||||||||||||||||
| 74 | - | |||||||||||||||||||
| 75 | bool QSslCertificate::isNull() const | - | ||||||||||||||||||
| 76 | { | - | ||||||||||||||||||
| 77 | return d->null; executed 3707 times by 9 tests: return d->null;Executed by:
| 3707 | ||||||||||||||||||
| 78 | } | - | ||||||||||||||||||
| 79 | - | |||||||||||||||||||
| 80 | bool QSslCertificate::isSelfSigned() const | - | ||||||||||||||||||
| 81 | { | - | ||||||||||||||||||
| 82 | if (!d->x509)
| 1-2 | ||||||||||||||||||
| 83 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||||||||
| 84 | - | |||||||||||||||||||
| 85 | return (q_X509_check_issued(d->x509, d->x509) == X509_V_OK); executed 2 times by 1 test: return (q_X509_check_issued(d->x509, d->x509) == 0);Executed by:
| 2 | ||||||||||||||||||
| 86 | } | - | ||||||||||||||||||
| 87 | - | |||||||||||||||||||
| 88 | QByteArray QSslCertificate::version() const | - | ||||||||||||||||||
| 89 | { | - | ||||||||||||||||||
| 90 | QMutexLocker lock(QMutexPool::globalInstanceGet(d.data())); | - | ||||||||||||||||||
| 91 | if (d->versionString.isEmpty() && d->x509)
| 2-64 | ||||||||||||||||||
| 92 | d->versionString = executed 13 times by 1 test: d->versionString = QByteArray::number(qlonglong(q_ASN1_INTEGER_get(d->x509->cert_info->version)) + 1);Executed by:
| 13 | ||||||||||||||||||
| 93 | QByteArray::number(qlonglong(q_ASN1_INTEGER_get(d->x509->cert_info->version)) + 1); executed 13 times by 1 test: d->versionString = QByteArray::number(qlonglong(q_ASN1_INTEGER_get(d->x509->cert_info->version)) + 1);Executed by:
| 13 | ||||||||||||||||||
| 94 | - | |||||||||||||||||||
| 95 | return d->versionString; executed 79 times by 1 test: return d->versionString;Executed by:
| 79 | ||||||||||||||||||
| 96 | } | - | ||||||||||||||||||
| 97 | - | |||||||||||||||||||
| 98 | QByteArray QSslCertificate::serialNumber() const | - | ||||||||||||||||||
| 99 | { | - | ||||||||||||||||||
| 100 | QMutexLocker lock(QMutexPool::globalInstanceGet(d.data())); | - | ||||||||||||||||||
| 101 | if (d->serialNumberString.isEmpty() && d->x509) {
| 37-16564 | ||||||||||||||||||
| 102 | ASN1_INTEGER *serialNumber = d->x509->cert_info->serialNumber; | - | ||||||||||||||||||
| 103 | QByteArray hexString; | - | ||||||||||||||||||
| 104 | hexString.reserve(serialNumber->length * 3); | - | ||||||||||||||||||
| 105 | for (int a = 0; a < serialNumber->length; ++a) {
| 453-3564 | ||||||||||||||||||
| 106 | hexString += QByteArray::number(serialNumber->data[a], 16).rightJustified(2, '0'); | - | ||||||||||||||||||
| 107 | hexString += ':'; | - | ||||||||||||||||||
| 108 | } executed 3564 times by 8 tests: end of blockExecuted by:
| 3564 | ||||||||||||||||||
| 109 | hexString.chop(1); | - | ||||||||||||||||||
| 110 | d->serialNumberString = hexString; | - | ||||||||||||||||||
| 111 | } executed 453 times by 8 tests: end of blockExecuted by:
| 453 | ||||||||||||||||||
| 112 | return d->serialNumberString; executed 17054 times by 8 tests: return d->serialNumberString;Executed by:
| 17054 | ||||||||||||||||||
| 113 | } | - | ||||||||||||||||||
| 114 | - | |||||||||||||||||||
| 115 | QStringList QSslCertificate::issuerInfo(SubjectInfo info) const | - | ||||||||||||||||||
| 116 | { | - | ||||||||||||||||||
| 117 | QMutexLocker lock(QMutexPool::globalInstanceGet(d.data())); | - | ||||||||||||||||||
| 118 | // lazy init | - | ||||||||||||||||||
| 119 | if (d->issuerInfo.isEmpty() && d->x509)
| 1-213 | ||||||||||||||||||
| 120 | d->issuerInfo = executed 11 times by 1 test: d->issuerInfo = _q_mapFromX509Name(q_X509_get_issuer_name(d->x509));Executed by:
| 11 | ||||||||||||||||||
| 121 | _q_mapFromX509Name(q_X509_get_issuer_name(d->x509)); executed 11 times by 1 test: d->issuerInfo = _q_mapFromX509Name(q_X509_get_issuer_name(d->x509));Executed by:
| 11 | ||||||||||||||||||
| 122 | - | |||||||||||||||||||
| 123 | return d->issuerInfo.values(d->subjectInfoToString(info)); executed 225 times by 1 test: return d->issuerInfo.values(d->subjectInfoToString(info));Executed by:
| 225 | ||||||||||||||||||
| 124 | } | - | ||||||||||||||||||
| 125 | - | |||||||||||||||||||
| 126 | QStringList QSslCertificate::issuerInfo(const QByteArray &attribute) const | - | ||||||||||||||||||
| 127 | { | - | ||||||||||||||||||
| 128 | QMutexLocker lock(QMutexPool::globalInstanceGet(d.data())); | - | ||||||||||||||||||
| 129 | // lazy init | - | ||||||||||||||||||
| 130 | if (d->issuerInfo.isEmpty() && d->x509)
| 0-6 | ||||||||||||||||||
| 131 | d->issuerInfo = executed 1 time by 1 test: d->issuerInfo = _q_mapFromX509Name(q_X509_get_issuer_name(d->x509));Executed by:
| 1 | ||||||||||||||||||
| 132 | _q_mapFromX509Name(q_X509_get_issuer_name(d->x509)); executed 1 time by 1 test: d->issuerInfo = _q_mapFromX509Name(q_X509_get_issuer_name(d->x509));Executed by:
| 1 | ||||||||||||||||||
| 133 | - | |||||||||||||||||||
| 134 | return d->issuerInfo.values(attribute); executed 7 times by 1 test: return d->issuerInfo.values(attribute);Executed by:
| 7 | ||||||||||||||||||
| 135 | } | - | ||||||||||||||||||
| 136 | - | |||||||||||||||||||
| 137 | QStringList QSslCertificate::subjectInfo(SubjectInfo info) const | - | ||||||||||||||||||
| 138 | { | - | ||||||||||||||||||
| 139 | QMutexLocker lock(QMutexPool::globalInstanceGet(d.data())); | - | ||||||||||||||||||
| 140 | // lazy init | - | ||||||||||||||||||
| 141 | if (d->subjectInfo.isEmpty() && d->x509)
| 1-417 | ||||||||||||||||||
| 142 | d->subjectInfo = executed 416 times by 8 tests: d->subjectInfo = _q_mapFromX509Name(q_X509_get_subject_name(d->x509));Executed by:
| 416 | ||||||||||||||||||
| 143 | _q_mapFromX509Name(q_X509_get_subject_name(d->x509)); executed 416 times by 8 tests: d->subjectInfo = _q_mapFromX509Name(q_X509_get_subject_name(d->x509));Executed by:
| 416 | ||||||||||||||||||
| 144 | - | |||||||||||||||||||
| 145 | return d->subjectInfo.values(d->subjectInfoToString(info)); executed 631 times by 8 tests: return d->subjectInfo.values(d->subjectInfoToString(info));Executed by:
| 631 | ||||||||||||||||||
| 146 | } | - | ||||||||||||||||||
| 147 | - | |||||||||||||||||||
| 148 | QStringList QSslCertificate::subjectInfo(const QByteArray &attribute) const | - | ||||||||||||||||||
| 149 | { | - | ||||||||||||||||||
| 150 | QMutexLocker lock(QMutexPool::globalInstanceGet(d.data())); | - | ||||||||||||||||||
| 151 | // lazy init | - | ||||||||||||||||||
| 152 | if (d->subjectInfo.isEmpty() && d->x509)
| 0-9 | ||||||||||||||||||
| 153 | d->subjectInfo = executed 2 times by 1 test: d->subjectInfo = _q_mapFromX509Name(q_X509_get_subject_name(d->x509));Executed by:
| 2 | ||||||||||||||||||
| 154 | _q_mapFromX509Name(q_X509_get_subject_name(d->x509)); executed 2 times by 1 test: d->subjectInfo = _q_mapFromX509Name(q_X509_get_subject_name(d->x509));Executed by:
| 2 | ||||||||||||||||||
| 155 | - | |||||||||||||||||||
| 156 | return d->subjectInfo.values(attribute); executed 11 times by 1 test: return d->subjectInfo.values(attribute);Executed by:
| 11 | ||||||||||||||||||
| 157 | } | - | ||||||||||||||||||
| 158 | - | |||||||||||||||||||
| 159 | QList<QByteArray> QSslCertificate::subjectInfoAttributes() const | - | ||||||||||||||||||
| 160 | { | - | ||||||||||||||||||
| 161 | QMutexLocker lock(QMutexPool::globalInstanceGet(d.data())); | - | ||||||||||||||||||
| 162 | // lazy init | - | ||||||||||||||||||
| 163 | if (d->subjectInfo.isEmpty() && d->x509)
| 0-2 | ||||||||||||||||||
| 164 | d->subjectInfo = executed 2 times by 1 test: d->subjectInfo = _q_mapFromX509Name(q_X509_get_subject_name(d->x509));Executed by:
| 2 | ||||||||||||||||||
| 165 | _q_mapFromX509Name(q_X509_get_subject_name(d->x509)); executed 2 times by 1 test: d->subjectInfo = _q_mapFromX509Name(q_X509_get_subject_name(d->x509));Executed by:
| 2 | ||||||||||||||||||
| 166 | - | |||||||||||||||||||
| 167 | return d->subjectInfo.uniqueKeys(); executed 4 times by 1 test: return d->subjectInfo.uniqueKeys();Executed by:
| 4 | ||||||||||||||||||
| 168 | } | - | ||||||||||||||||||
| 169 | - | |||||||||||||||||||
| 170 | QList<QByteArray> QSslCertificate::issuerInfoAttributes() const | - | ||||||||||||||||||
| 171 | { | - | ||||||||||||||||||
| 172 | QMutexLocker lock(QMutexPool::globalInstanceGet(d.data())); | - | ||||||||||||||||||
| 173 | // lazy init | - | ||||||||||||||||||
| 174 | if (d->issuerInfo.isEmpty() && d->x509)
| 0-2 | ||||||||||||||||||
| 175 | d->issuerInfo = never executed: d->issuerInfo = _q_mapFromX509Name(q_X509_get_issuer_name(d->x509)); | 0 | ||||||||||||||||||
| 176 | _q_mapFromX509Name(q_X509_get_issuer_name(d->x509)); never executed: d->issuerInfo = _q_mapFromX509Name(q_X509_get_issuer_name(d->x509)); | 0 | ||||||||||||||||||
| 177 | - | |||||||||||||||||||
| 178 | return d->issuerInfo.uniqueKeys(); executed 2 times by 1 test: return d->issuerInfo.uniqueKeys();Executed by:
| 2 | ||||||||||||||||||
| 179 | } | - | ||||||||||||||||||
| 180 | - | |||||||||||||||||||
| 181 | QMultiMap<QSsl::AlternativeNameEntryType, QString> QSslCertificate::subjectAlternativeNames() const | - | ||||||||||||||||||
| 182 | { | - | ||||||||||||||||||
| 183 | QMultiMap<QSsl::AlternativeNameEntryType, QString> result; | - | ||||||||||||||||||
| 184 | - | |||||||||||||||||||
| 185 | if (!d->x509)
| 1-102 | ||||||||||||||||||
| 186 | return result; executed 1 time by 1 test: return result;Executed by:
| 1 | ||||||||||||||||||
| 187 | - | |||||||||||||||||||
| 188 | STACK_OF(GENERAL_NAME) *altNames = (STACK_OF(GENERAL_NAME)*)q_X509_get_ext_d2i(d->x509, NID_subject_alt_name, 0, 0); | - | ||||||||||||||||||
| 189 | - | |||||||||||||||||||
| 190 | if (altNames) {
| 11-91 | ||||||||||||||||||
| 191 | for (int i = 0; i < q_sk_GENERAL_NAME_num(altNames); ++i) {
| 11-31 | ||||||||||||||||||
| 192 | const GENERAL_NAME *genName = q_sk_GENERAL_NAME_value(altNames, i); | - | ||||||||||||||||||
| 193 | if (genName->type != GEN_DNS && genName->type != GEN_EMAIL)
| 0-20 | ||||||||||||||||||
| 194 | continue; never executed: continue; | 0 | ||||||||||||||||||
| 195 | - | |||||||||||||||||||
| 196 | int len = q_ASN1_STRING_length(genName->d.ia5); | - | ||||||||||||||||||
| 197 | if (len < 0 || len >= 8192) {
| 0-31 | ||||||||||||||||||
| 198 | // broken name | - | ||||||||||||||||||
| 199 | continue; never executed: continue; | 0 | ||||||||||||||||||
| 200 | } | - | ||||||||||||||||||
| 201 | - | |||||||||||||||||||
| 202 | const char *altNameStr = reinterpret_cast<const char *>(q_ASN1_STRING_data(genName->d.ia5)); | - | ||||||||||||||||||
| 203 | const QString altName = QString::fromLatin1(altNameStr, len); | - | ||||||||||||||||||
| 204 | if (genName->type == GEN_DNS)
| 11-20 | ||||||||||||||||||
| 205 | result.insert(QSsl::DnsEntry, altName); executed 11 times by 1 test: result.insert(QSsl::DnsEntry, altName);Executed by:
| 11 | ||||||||||||||||||
| 206 | else if (genName->type == GEN_EMAIL)
| 0-20 | ||||||||||||||||||
| 207 | result.insert(QSsl::EmailEntry, altName); executed 20 times by 1 test: result.insert(QSsl::EmailEntry, altName);Executed by:
| 20 | ||||||||||||||||||
| 208 | } executed 31 times by 1 test: end of blockExecuted by:
| 31 | ||||||||||||||||||
| 209 | q_sk_pop_free((STACK*)altNames, reinterpret_cast<void(*)(void*)>(q_sk_free)); | - | ||||||||||||||||||
| 210 | } executed 11 times by 1 test: end of blockExecuted by:
| 11 | ||||||||||||||||||
| 211 | - | |||||||||||||||||||
| 212 | return result; executed 102 times by 3 tests: return result;Executed by:
| 102 | ||||||||||||||||||
| 213 | } | - | ||||||||||||||||||
| 214 | - | |||||||||||||||||||
| 215 | QDateTime QSslCertificate::effectiveDate() const | - | ||||||||||||||||||
| 216 | { | - | ||||||||||||||||||
| 217 | return d->notValidBefore; executed 41 times by 1 test: return d->notValidBefore;Executed by:
| 41 | ||||||||||||||||||
| 218 | } | - | ||||||||||||||||||
| 219 | - | |||||||||||||||||||
| 220 | QDateTime QSslCertificate::expiryDate() const | - | ||||||||||||||||||
| 221 | { | - | ||||||||||||||||||
| 222 | return d->notValidAfter; executed 57430 times by 4 tests: return d->notValidAfter;Executed by:
| 57430 | ||||||||||||||||||
| 223 | } | - | ||||||||||||||||||
| 224 | - | |||||||||||||||||||
| 225 | Qt::HANDLE QSslCertificate::handle() const | - | ||||||||||||||||||
| 226 | { | - | ||||||||||||||||||
| 227 | return Qt::HANDLE(d->x509); executed 53884 times by 4 tests: return Qt::HANDLE(d->x509);Executed by:
| 53884 | ||||||||||||||||||
| 228 | } | - | ||||||||||||||||||
| 229 | - | |||||||||||||||||||
| 230 | QSslKey QSslCertificate::publicKey() const | - | ||||||||||||||||||
| 231 | { | - | ||||||||||||||||||
| 232 | if (!d->x509)
| 0-8 | ||||||||||||||||||
| 233 | return QSslKey(); never executed: return QSslKey(); | 0 | ||||||||||||||||||
| 234 | - | |||||||||||||||||||
| 235 | QSslKey key; | - | ||||||||||||||||||
| 236 | - | |||||||||||||||||||
| 237 | key.d->type = QSsl::PublicKey; | - | ||||||||||||||||||
| 238 | X509_PUBKEY *xkey = d->x509->cert_info->key; | - | ||||||||||||||||||
| 239 | EVP_PKEY *pkey = q_X509_PUBKEY_get(xkey); | - | ||||||||||||||||||
| 240 | Q_ASSERT(pkey); | - | ||||||||||||||||||
| 241 | - | |||||||||||||||||||
| 242 | if (q_EVP_PKEY_type(pkey->type) == EVP_PKEY_RSA) {
| 2-6 | ||||||||||||||||||
| 243 | key.d->rsa = q_EVP_PKEY_get1_RSA(pkey); | - | ||||||||||||||||||
| 244 | key.d->algorithm = QSsl::Rsa; | - | ||||||||||||||||||
| 245 | key.d->isNull = false; | - | ||||||||||||||||||
| 246 | } else if (q_EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA) { executed 6 times by 1 test: end of blockExecuted by:
| 1-6 | ||||||||||||||||||
| 247 | key.d->dsa = q_EVP_PKEY_get1_DSA(pkey); | - | ||||||||||||||||||
| 248 | key.d->algorithm = QSsl::Dsa; | - | ||||||||||||||||||
| 249 | key.d->isNull = false; | - | ||||||||||||||||||
| 250 | #ifndef OPENSSL_NO_EC | - | ||||||||||||||||||
| 251 | } else if (q_EVP_PKEY_type(pkey->type) == EVP_PKEY_EC) { executed 1 time by 1 test: end of blockExecuted by:
| 0-1 | ||||||||||||||||||
| 252 | key.d->ec = q_EVP_PKEY_get1_EC_KEY(pkey); | - | ||||||||||||||||||
| 253 | key.d->algorithm = QSsl::Ec; | - | ||||||||||||||||||
| 254 | key.d->isNull = false; | - | ||||||||||||||||||
| 255 | #endif | - | ||||||||||||||||||
| 256 | } else if (q_EVP_PKEY_type(pkey->type) == EVP_PKEY_DH) { executed 1 time by 1 test: end of blockExecuted by:
| 0-1 | ||||||||||||||||||
| 257 | // DH unsupported | - | ||||||||||||||||||
| 258 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 259 | // error? | - | ||||||||||||||||||
| 260 | } never executed: end of block | 0 | ||||||||||||||||||
| 261 | - | |||||||||||||||||||
| 262 | q_EVP_PKEY_free(pkey); | - | ||||||||||||||||||
| 263 | return key; executed 8 times by 1 test: return key;Executed by:
| 8 | ||||||||||||||||||
| 264 | } | - | ||||||||||||||||||
| 265 | - | |||||||||||||||||||
| 266 | /* | - | ||||||||||||||||||
| 267 | * Convert unknown extensions to a QVariant. | - | ||||||||||||||||||
| 268 | */ | - | ||||||||||||||||||
| 269 | static QVariant x509UnknownExtensionToValue(X509_EXTENSION *ext) | - | ||||||||||||||||||
| 270 | { | - | ||||||||||||||||||
| 271 | // Get the extension specific method object if available | - | ||||||||||||||||||
| 272 | // we cast away the const-ness here because some versions of openssl | - | ||||||||||||||||||
| 273 | // don't use const for the parameters in the functions pointers stored | - | ||||||||||||||||||
| 274 | // in the object. | - | ||||||||||||||||||
| 275 | X509V3_EXT_METHOD *meth = const_cast<X509V3_EXT_METHOD *>(q_X509V3_EXT_get(ext)); | - | ||||||||||||||||||
| 276 | if (!meth) {
| 1-9 | ||||||||||||||||||
| 277 | ASN1_OCTET_STRING *value = q_X509_EXTENSION_get_data(ext); | - | ||||||||||||||||||
| 278 | QByteArray result( reinterpret_cast<const char *>(q_ASN1_STRING_data(value)), | - | ||||||||||||||||||
| 279 | q_ASN1_STRING_length(value)); | - | ||||||||||||||||||
| 280 | return result; executed 1 time by 1 test: return result;Executed by:
| 1 | ||||||||||||||||||
| 281 | } | - | ||||||||||||||||||
| 282 | - | |||||||||||||||||||
| 283 | //const unsigned char *data = ext->value->data; | - | ||||||||||||||||||
| 284 | void *ext_internal = q_X509V3_EXT_d2i(ext); | - | ||||||||||||||||||
| 285 | - | |||||||||||||||||||
| 286 | // If this extension can be converted | - | ||||||||||||||||||
| 287 | if (meth->i2v && ext_internal) {
| 0-5 | ||||||||||||||||||
| 288 | STACK_OF(CONF_VALUE) *val = meth->i2v(meth, ext_internal, 0); | - | ||||||||||||||||||
| 289 | - | |||||||||||||||||||
| 290 | QVariantMap map; | - | ||||||||||||||||||
| 291 | QVariantList list; | - | ||||||||||||||||||
| 292 | bool isMap = false; | - | ||||||||||||||||||
| 293 | - | |||||||||||||||||||
| 294 | for (int j = 0; j < q_SKM_sk_num(CONF_VALUE, val); j++) {
| 5-12 | ||||||||||||||||||
| 295 | CONF_VALUE *nval = q_SKM_sk_value(CONF_VALUE, val, j); | - | ||||||||||||||||||
| 296 | if (nval->name && nval->value) {
| 2-6 | ||||||||||||||||||
| 297 | isMap = true; | - | ||||||||||||||||||
| 298 | map[QString::fromUtf8(nval->name)] = QString::fromUtf8(nval->value); | - | ||||||||||||||||||
| 299 | } else if (nval->name) { executed 2 times by 1 test: end of blockExecuted by:
| 2-6 | ||||||||||||||||||
| 300 | list << QString::fromUtf8(nval->name); | - | ||||||||||||||||||
| 301 | } else if (nval->value) { executed 4 times by 1 test: end of blockExecuted by:
| 0-6 | ||||||||||||||||||
| 302 | list << QString::fromUtf8(nval->value); | - | ||||||||||||||||||
| 303 | } executed 6 times by 1 test: end of blockExecuted by:
| 6 | ||||||||||||||||||
| 304 | } executed 12 times by 1 test: end of blockExecuted by:
| 12 | ||||||||||||||||||
| 305 | - | |||||||||||||||||||
| 306 | if (isMap)
| 1-4 | ||||||||||||||||||
| 307 | return map; executed 1 time by 1 test: return map;Executed by:
| 1 | ||||||||||||||||||
| 308 | else | - | ||||||||||||||||||
| 309 | return list; executed 4 times by 1 test: return list;Executed by:
| 4 | ||||||||||||||||||
| 310 | } else if (meth->i2s && ext_internal) {
| 0-4 | ||||||||||||||||||
| 311 | //qCDebug(lcSsl) << meth->i2s(meth, ext_internal); | - | ||||||||||||||||||
| 312 | QVariant result(QString::fromUtf8(meth->i2s(meth, ext_internal))); | - | ||||||||||||||||||
| 313 | return result; never executed: return result; | 0 | ||||||||||||||||||
| 314 | } else if (meth->i2r && ext_internal) {
| 0-4 | ||||||||||||||||||
| 315 | QByteArray result; | - | ||||||||||||||||||
| 316 | - | |||||||||||||||||||
| 317 | BIO *bio = q_BIO_new(q_BIO_s_mem()); | - | ||||||||||||||||||
| 318 | if (!bio)
| 0-4 | ||||||||||||||||||
| 319 | return result; never executed: return result; | 0 | ||||||||||||||||||
| 320 | - | |||||||||||||||||||
| 321 | meth->i2r(meth, ext_internal, bio, 0); | - | ||||||||||||||||||
| 322 | - | |||||||||||||||||||
| 323 | char *bio_buffer; | - | ||||||||||||||||||
| 324 | long bio_size = q_BIO_get_mem_data(bio, &bio_buffer); | - | ||||||||||||||||||
| 325 | result = QByteArray(bio_buffer, bio_size); | - | ||||||||||||||||||
| 326 | - | |||||||||||||||||||
| 327 | q_BIO_free(bio); | - | ||||||||||||||||||
| 328 | return result; executed 4 times by 1 test: return result;Executed by:
| 4 | ||||||||||||||||||
| 329 | } | - | ||||||||||||||||||
| 330 | - | |||||||||||||||||||
| 331 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||||||||
| 332 | } | - | ||||||||||||||||||
| 333 | - | |||||||||||||||||||
| 334 | /* | - | ||||||||||||||||||
| 335 | * Convert extensions to a variant. The naming of the keys of the map are | - | ||||||||||||||||||
| 336 | * taken from RFC 5280, however we decided the capitalisation in the RFC | - | ||||||||||||||||||
| 337 | * was too silly for the real world. | - | ||||||||||||||||||
| 338 | */ | - | ||||||||||||||||||
| 339 | static QVariant x509ExtensionToValue(X509_EXTENSION *ext) | - | ||||||||||||||||||
| 340 | { | - | ||||||||||||||||||
| 341 | ASN1_OBJECT *obj = q_X509_EXTENSION_get_object(ext); | - | ||||||||||||||||||
| 342 | int nid = q_OBJ_obj2nid(obj); | - | ||||||||||||||||||
| 343 | - | |||||||||||||||||||
| 344 | switch (nid) { | - | ||||||||||||||||||
| 345 | case NID_basic_constraints: executed 2 times by 1 test: case 87:Executed by:
| 2 | ||||||||||||||||||
| 346 | { | - | ||||||||||||||||||
| 347 | BASIC_CONSTRAINTS *basic = reinterpret_cast<BASIC_CONSTRAINTS *>(q_X509V3_EXT_d2i(ext)); | - | ||||||||||||||||||
| 348 | - | |||||||||||||||||||
| 349 | QVariantMap result; | - | ||||||||||||||||||
| 350 | result[QLatin1String("ca")] = basic->ca ? true : false;
| 0-2 | ||||||||||||||||||
| 351 | if (basic->pathlen)
| 0-2 | ||||||||||||||||||
| 352 | result[QLatin1String("pathLenConstraint")] = (qlonglong)q_ASN1_INTEGER_get(basic->pathlen); never executed: result[QLatin1String("pathLenConstraint")] = (qlonglong)q_ASN1_INTEGER_get(basic->pathlen); | 0 | ||||||||||||||||||
| 353 | - | |||||||||||||||||||
| 354 | q_BASIC_CONSTRAINTS_free(basic); | - | ||||||||||||||||||
| 355 | return result; executed 2 times by 1 test: return result;Executed by:
| 2 | ||||||||||||||||||
| 356 | } | - | ||||||||||||||||||
| 357 | break; dead code: break; | - | ||||||||||||||||||
| 358 | case NID_info_access: executed 2 times by 1 test: case 177:Executed by:
| 2 | ||||||||||||||||||
| 359 | { | - | ||||||||||||||||||
| 360 | AUTHORITY_INFO_ACCESS *info = reinterpret_cast<AUTHORITY_INFO_ACCESS *>(q_X509V3_EXT_d2i(ext)); | - | ||||||||||||||||||
| 361 | - | |||||||||||||||||||
| 362 | QVariantMap result; | - | ||||||||||||||||||
| 363 | for (int i=0; i < q_SKM_sk_num(ACCESS_DESCRIPTION, info); i++) {
| 2-4 | ||||||||||||||||||
| 364 | ACCESS_DESCRIPTION *ad = q_SKM_sk_value(ACCESS_DESCRIPTION, info, i); | - | ||||||||||||||||||
| 365 | - | |||||||||||||||||||
| 366 | GENERAL_NAME *name = ad->location; | - | ||||||||||||||||||
| 367 | if (name->type == GEN_URI) {
| 0-4 | ||||||||||||||||||
| 368 | int len = q_ASN1_STRING_length(name->d.uniformResourceIdentifier); | - | ||||||||||||||||||
| 369 | if (len < 0 || len >= 8192) {
| 0-4 | ||||||||||||||||||
| 370 | // broken name | - | ||||||||||||||||||
| 371 | continue; never executed: continue; | 0 | ||||||||||||||||||
| 372 | } | - | ||||||||||||||||||
| 373 | - | |||||||||||||||||||
| 374 | const char *uriStr = reinterpret_cast<const char *>(q_ASN1_STRING_data(name->d.uniformResourceIdentifier)); | - | ||||||||||||||||||
| 375 | const QString uri = QString::fromUtf8(uriStr, len); | - | ||||||||||||||||||
| 376 | - | |||||||||||||||||||
| 377 | result[QString::fromUtf8(QSslCertificatePrivate::asn1ObjectName(ad->method))] = uri; | - | ||||||||||||||||||
| 378 | } else { executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||||||||
| 379 | qCWarning(lcSsl) << "Strange location type" << name->type; never executed: QMessageLogger(__FILE__, 379, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning() << "Strange location type" << name->type;
| 0 | ||||||||||||||||||
| 380 | } never executed: end of block | 0 | ||||||||||||||||||
| 381 | } | - | ||||||||||||||||||
| 382 | - | |||||||||||||||||||
| 383 | #if OPENSSL_VERSION_NUMBER >= 0x10000000L | - | ||||||||||||||||||
| 384 | q_sk_pop_free((_STACK*)info, reinterpret_cast<void(*)(void*)>(q_sk_free)); | - | ||||||||||||||||||
| 385 | #else | - | ||||||||||||||||||
| 386 | q_sk_pop_free((STACK*)info, reinterpret_cast<void(*)(void*)>(q_sk_free)); | - | ||||||||||||||||||
| 387 | #endif | - | ||||||||||||||||||
| 388 | return result; executed 2 times by 1 test: return result;Executed by:
| 2 | ||||||||||||||||||
| 389 | } | - | ||||||||||||||||||
| 390 | break; dead code: break; | - | ||||||||||||||||||
| 391 | case NID_subject_key_identifier: executed 2 times by 1 test: case 82:Executed by:
| 2 | ||||||||||||||||||
| 392 | { | - | ||||||||||||||||||
| 393 | void *ext_internal = q_X509V3_EXT_d2i(ext); | - | ||||||||||||||||||
| 394 | - | |||||||||||||||||||
| 395 | // we cast away the const-ness here because some versions of openssl | - | ||||||||||||||||||
| 396 | // don't use const for the parameters in the functions pointers stored | - | ||||||||||||||||||
| 397 | // in the object. | - | ||||||||||||||||||
| 398 | X509V3_EXT_METHOD *meth = const_cast<X509V3_EXT_METHOD *>(q_X509V3_EXT_get(ext)); | - | ||||||||||||||||||
| 399 | - | |||||||||||||||||||
| 400 | return QVariant(QString::fromUtf8(meth->i2s(meth, ext_internal))); executed 2 times by 1 test: return QVariant(QString::fromUtf8(meth->i2s(meth, ext_internal)));Executed by:
| 2 | ||||||||||||||||||
| 401 | } | - | ||||||||||||||||||
| 402 | break; dead code: break; | - | ||||||||||||||||||
| 403 | case NID_authority_key_identifier: executed 2 times by 1 test: case 90:Executed by:
| 2 | ||||||||||||||||||
| 404 | { | - | ||||||||||||||||||
| 405 | AUTHORITY_KEYID *auth_key = reinterpret_cast<AUTHORITY_KEYID *>(q_X509V3_EXT_d2i(ext)); | - | ||||||||||||||||||
| 406 | - | |||||||||||||||||||
| 407 | QVariantMap result; | - | ||||||||||||||||||
| 408 | - | |||||||||||||||||||
| 409 | // keyid | - | ||||||||||||||||||
| 410 | if (auth_key->keyid) {
| 0-2 | ||||||||||||||||||
| 411 | QByteArray keyid(reinterpret_cast<const char *>(auth_key->keyid->data), | - | ||||||||||||||||||
| 412 | auth_key->keyid->length); | - | ||||||||||||||||||
| 413 | result[QLatin1String("keyid")] = keyid.toHex(); | - | ||||||||||||||||||
| 414 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||
| 415 | - | |||||||||||||||||||
| 416 | // issuer | - | ||||||||||||||||||
| 417 | // TODO: GENERAL_NAMES | - | ||||||||||||||||||
| 418 | - | |||||||||||||||||||
| 419 | // serial | - | ||||||||||||||||||
| 420 | if (auth_key->serial)
| 0-2 | ||||||||||||||||||
| 421 | result[QLatin1String("serial")] = (qlonglong)q_ASN1_INTEGER_get(auth_key->serial); never executed: result[QLatin1String("serial")] = (qlonglong)q_ASN1_INTEGER_get(auth_key->serial); | 0 | ||||||||||||||||||
| 422 | - | |||||||||||||||||||
| 423 | q_AUTHORITY_KEYID_free(auth_key); | - | ||||||||||||||||||
| 424 | return result; executed 2 times by 1 test: return result;Executed by:
| 2 | ||||||||||||||||||
| 425 | } | - | ||||||||||||||||||
| 426 | break; dead code: break; | - | ||||||||||||||||||
| 427 | } | - | ||||||||||||||||||
| 428 | - | |||||||||||||||||||
| 429 | return QVariant(); executed 10 times by 1 test: return QVariant();Executed by:
| 10 | ||||||||||||||||||
| 430 | } | - | ||||||||||||||||||
| 431 | - | |||||||||||||||||||
| 432 | QSslCertificateExtension QSslCertificatePrivate::convertExtension(X509_EXTENSION *ext) | - | ||||||||||||||||||
| 433 | { | - | ||||||||||||||||||
| 434 | QSslCertificateExtension result; | - | ||||||||||||||||||
| 435 | - | |||||||||||||||||||
| 436 | ASN1_OBJECT *obj = q_X509_EXTENSION_get_object(ext); | - | ||||||||||||||||||
| 437 | QByteArray oid = QSslCertificatePrivate::asn1ObjectId(obj); | - | ||||||||||||||||||
| 438 | QByteArray name = QSslCertificatePrivate::asn1ObjectName(obj); | - | ||||||||||||||||||
| 439 | - | |||||||||||||||||||
| 440 | result.d->oid = QString::fromUtf8(oid); | - | ||||||||||||||||||
| 441 | result.d->name = QString::fromUtf8(name); | - | ||||||||||||||||||
| 442 | - | |||||||||||||||||||
| 443 | bool critical = q_X509_EXTENSION_get_critical(ext); | - | ||||||||||||||||||
| 444 | result.d->critical = critical; | - | ||||||||||||||||||
| 445 | - | |||||||||||||||||||
| 446 | // Lets see if we have custom support for this one | - | ||||||||||||||||||
| 447 | QVariant extensionValue = x509ExtensionToValue(ext); | - | ||||||||||||||||||
| 448 | if (extensionValue.isValid()) {
| 8-10 | ||||||||||||||||||
| 449 | result.d->value = extensionValue; | - | ||||||||||||||||||
| 450 | result.d->supported = true; | - | ||||||||||||||||||
| 451 | - | |||||||||||||||||||
| 452 | return result; executed 8 times by 1 test: return result;Executed by:
| 8 | ||||||||||||||||||
| 453 | } | - | ||||||||||||||||||
| 454 | - | |||||||||||||||||||
| 455 | extensionValue = x509UnknownExtensionToValue(ext); | - | ||||||||||||||||||
| 456 | if (extensionValue.isValid()) {
| 0-10 | ||||||||||||||||||
| 457 | result.d->value = extensionValue; | - | ||||||||||||||||||
| 458 | result.d->supported = false; | - | ||||||||||||||||||
| 459 | return result; executed 10 times by 1 test: return result;Executed by:
| 10 | ||||||||||||||||||
| 460 | } | - | ||||||||||||||||||
| 461 | - | |||||||||||||||||||
| 462 | return result; never executed: return result; | 0 | ||||||||||||||||||
| 463 | } | - | ||||||||||||||||||
| 464 | - | |||||||||||||||||||
| 465 | QList<QSslCertificateExtension> QSslCertificate::extensions() const | - | ||||||||||||||||||
| 466 | { | - | ||||||||||||||||||
| 467 | QList<QSslCertificateExtension> result; | - | ||||||||||||||||||
| 468 | - | |||||||||||||||||||
| 469 | if (!d->x509)
| 0-4 | ||||||||||||||||||
| 470 | return result; never executed: return result; | 0 | ||||||||||||||||||
| 471 | - | |||||||||||||||||||
| 472 | int count = q_X509_get_ext_count(d->x509); | - | ||||||||||||||||||
| 473 | result.reserve(count); | - | ||||||||||||||||||
| 474 | - | |||||||||||||||||||
| 475 | for (int i = 0; i < count; i++) {
| 4-18 | ||||||||||||||||||
| 476 | X509_EXTENSION *ext = q_X509_get_ext(d->x509, i); | - | ||||||||||||||||||
| 477 | result << QSslCertificatePrivate::convertExtension(ext); | - | ||||||||||||||||||
| 478 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||
| 479 | - | |||||||||||||||||||
| 480 | return result; executed 4 times by 1 test: return result;Executed by:
| 4 | ||||||||||||||||||
| 481 | } | - | ||||||||||||||||||
| 482 | - | |||||||||||||||||||
| 483 | QByteArray QSslCertificate::toPem() const | - | ||||||||||||||||||
| 484 | { | - | ||||||||||||||||||
| 485 | if (!d->x509)
| 0-47 | ||||||||||||||||||
| 486 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||||||||
| 487 | return d->QByteArray_from_X509(d->x509, QSsl::Pem); executed 47 times by 1 test: return d->QByteArray_from_X509(d->x509, QSsl::Pem);Executed by:
| 47 | ||||||||||||||||||
| 488 | } | - | ||||||||||||||||||
| 489 | - | |||||||||||||||||||
| 490 | QByteArray QSslCertificate::toDer() const | - | ||||||||||||||||||
| 491 | { | - | ||||||||||||||||||
| 492 | if (!d->x509)
| 1-89 | ||||||||||||||||||
| 493 | return QByteArray(); executed 1 time by 1 test: return QByteArray();Executed by:
| 1 | ||||||||||||||||||
| 494 | return d->QByteArray_from_X509(d->x509, QSsl::Der); executed 89 times by 1 test: return d->QByteArray_from_X509(d->x509, QSsl::Der);Executed by:
| 89 | ||||||||||||||||||
| 495 | } | - | ||||||||||||||||||
| 496 | - | |||||||||||||||||||
| 497 | QString QSslCertificate::toText() const | - | ||||||||||||||||||
| 498 | { | - | ||||||||||||||||||
| 499 | if (!d->x509)
| 0-3 | ||||||||||||||||||
| 500 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
| 501 | return d->text_from_X509(d->x509); executed 3 times by 1 test: return d->text_from_X509(d->x509);Executed by:
| 3 | ||||||||||||||||||
| 502 | } | - | ||||||||||||||||||
| 503 | - | |||||||||||||||||||
| 504 | #define BEGINCERTSTRING "-----BEGIN CERTIFICATE-----" | - | ||||||||||||||||||
| 505 | #define ENDCERTSTRING "-----END CERTIFICATE-----" | - | ||||||||||||||||||
| 506 | - | |||||||||||||||||||
| 507 | void QSslCertificatePrivate::init(const QByteArray &data, QSsl::EncodingFormat format) | - | ||||||||||||||||||
| 508 | { | - | ||||||||||||||||||
| 509 | if (!data.isEmpty()) {
| 73-30192 | ||||||||||||||||||
| 510 | const QList<QSslCertificate> certs = (format == QSsl::Pem)
| 13-60 | ||||||||||||||||||
| 511 | ? certificatesFromPem(data, 1) | - | ||||||||||||||||||
| 512 | : certificatesFromDer(data, 1); | - | ||||||||||||||||||
| 513 | if (!certs.isEmpty()) {
| 3-70 | ||||||||||||||||||
| 514 | *this = *certs.first().d; | - | ||||||||||||||||||
| 515 | if (x509)
| 0-70 | ||||||||||||||||||
| 516 | x509 = q_X509_dup(x509); executed 70 times by 3 tests: x509 = q_X509_dup(x509);Executed by:
| 70 | ||||||||||||||||||
| 517 | } executed 70 times by 3 tests: end of blockExecuted by:
| 70 | ||||||||||||||||||
| 518 | } executed 73 times by 3 tests: end of blockExecuted by:
| 73 | ||||||||||||||||||
| 519 | } executed 30265 times by 16 tests: end of blockExecuted by:
| 30265 | ||||||||||||||||||
| 520 | - | |||||||||||||||||||
| 521 | // ### refactor against QSsl::pemFromDer() etc. (to avoid redundant implementations) | - | ||||||||||||||||||
| 522 | QByteArray QSslCertificatePrivate::QByteArray_from_X509(X509 *x509, QSsl::EncodingFormat format) | - | ||||||||||||||||||
| 523 | { | - | ||||||||||||||||||
| 524 | if (!x509) {
| 0-136 | ||||||||||||||||||
| 525 | qCWarning(lcSsl, "QSslSocketBackendPrivate::X509_to_QByteArray: null X509"); never executed: QMessageLogger(__FILE__, 525, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("QSslSocketBackendPrivate::X509_to_QByteArray: null X509");
| 0 | ||||||||||||||||||
| 526 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||||||||
| 527 | } | - | ||||||||||||||||||
| 528 | - | |||||||||||||||||||
| 529 | // Use i2d_X509 to convert the X509 to an array. | - | ||||||||||||||||||
| 530 | int length = q_i2d_X509(x509, 0); | - | ||||||||||||||||||
| 531 | QByteArray array; | - | ||||||||||||||||||
| 532 | array.resize(length); | - | ||||||||||||||||||
| 533 | char *data = array.data(); | - | ||||||||||||||||||
| 534 | char **dataP = &data; | - | ||||||||||||||||||
| 535 | unsigned char **dataPu = (unsigned char **)dataP; | - | ||||||||||||||||||
| 536 | if (q_i2d_X509(x509, dataPu) < 0)
| 0-136 | ||||||||||||||||||
| 537 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||||||||
| 538 | - | |||||||||||||||||||
| 539 | if (format == QSsl::Der)
| 47-89 | ||||||||||||||||||
| 540 | return array; executed 89 times by 1 test: return array;Executed by:
| 89 | ||||||||||||||||||
| 541 | - | |||||||||||||||||||
| 542 | // Convert to Base64 - wrap at 64 characters. | - | ||||||||||||||||||
| 543 | array = array.toBase64(); | - | ||||||||||||||||||
| 544 | QByteArray tmp; | - | ||||||||||||||||||
| 545 | for (int i = 0; i <= array.size() - 64; i += 64) {
| 47-518 | ||||||||||||||||||
| 546 | tmp += QByteArray::fromRawData(array.data() + i, 64); | - | ||||||||||||||||||
| 547 | tmp += '\n'; | - | ||||||||||||||||||
| 548 | } executed 518 times by 1 test: end of blockExecuted by:
| 518 | ||||||||||||||||||
| 549 | if (int remainder = array.size() % 64) {
| 1-46 | ||||||||||||||||||
| 550 | tmp += QByteArray::fromRawData(array.data() + array.size() - remainder, remainder); | - | ||||||||||||||||||
| 551 | tmp += '\n'; | - | ||||||||||||||||||
| 552 | } executed 46 times by 1 test: end of blockExecuted by:
| 46 | ||||||||||||||||||
| 553 | - | |||||||||||||||||||
| 554 | return BEGINCERTSTRING "\n" + tmp + ENDCERTSTRING "\n"; executed 47 times by 1 test: return "-----BEGIN CERTIFICATE-----" "\n" + tmp + "-----END CERTIFICATE-----" "\n";Executed by:
| 47 | ||||||||||||||||||
| 555 | } | - | ||||||||||||||||||
| 556 | - | |||||||||||||||||||
| 557 | QString QSslCertificatePrivate::text_from_X509(X509 *x509) | - | ||||||||||||||||||
| 558 | { | - | ||||||||||||||||||
| 559 | if (!x509) {
| 0-3 | ||||||||||||||||||
| 560 | qCWarning(lcSsl, "QSslSocketBackendPrivate::text_from_X509: null X509"); never executed: QMessageLogger(__FILE__, 560, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("QSslSocketBackendPrivate::text_from_X509: null X509");
| 0 | ||||||||||||||||||
| 561 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
| 562 | } | - | ||||||||||||||||||
| 563 | - | |||||||||||||||||||
| 564 | QByteArray result; | - | ||||||||||||||||||
| 565 | BIO *bio = q_BIO_new(q_BIO_s_mem()); | - | ||||||||||||||||||
| 566 | if (!bio)
| 0-3 | ||||||||||||||||||
| 567 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
| 568 | - | |||||||||||||||||||
| 569 | q_X509_print(bio, x509); | - | ||||||||||||||||||
| 570 | - | |||||||||||||||||||
| 571 | QVarLengthArray<char, 16384> data; | - | ||||||||||||||||||
| 572 | int count = q_BIO_read(bio, data.data(), 16384); | - | ||||||||||||||||||
| 573 | if ( count > 0 ) {
| 0-3 | ||||||||||||||||||
| 574 | result = QByteArray( data.data(), count ); | - | ||||||||||||||||||
| 575 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||||||||
| 576 | - | |||||||||||||||||||
| 577 | q_BIO_free(bio); | - | ||||||||||||||||||
| 578 | - | |||||||||||||||||||
| 579 | return QString::fromLatin1(result); executed 3 times by 1 test: return QString::fromLatin1(result);Executed by:
| 3 | ||||||||||||||||||
| 580 | } | - | ||||||||||||||||||
| 581 | - | |||||||||||||||||||
| 582 | QByteArray QSslCertificatePrivate::asn1ObjectId(ASN1_OBJECT *object) | - | ||||||||||||||||||
| 583 | { | - | ||||||||||||||||||
| 584 | char buf[80]; // The openssl docs a buffer length of 80 should be more than enough | - | ||||||||||||||||||
| 585 | q_OBJ_obj2txt(buf, sizeof(buf), object, 1); // the 1 says always use the oid not the long name | - | ||||||||||||||||||
| 586 | - | |||||||||||||||||||
| 587 | return QByteArray(buf); executed 20 times by 1 test: return QByteArray(buf);Executed by:
| 20 | ||||||||||||||||||
| 588 | } | - | ||||||||||||||||||
| 589 | - | |||||||||||||||||||
| 590 | - | |||||||||||||||||||
| 591 | QByteArray QSslCertificatePrivate::asn1ObjectName(ASN1_OBJECT *object) | - | ||||||||||||||||||
| 592 | { | - | ||||||||||||||||||
| 593 | int nid = q_OBJ_obj2nid(object); | - | ||||||||||||||||||
| 594 | if (nid != NID_undef)
| 2-2739 | ||||||||||||||||||
| 595 | return QByteArray(q_OBJ_nid2sn(nid)); executed 2739 times by 8 tests: return QByteArray(q_OBJ_nid2sn(nid));Executed by:
| 2739 | ||||||||||||||||||
| 596 | - | |||||||||||||||||||
| 597 | return asn1ObjectId(object); executed 2 times by 1 test: return asn1ObjectId(object);Executed by:
| 2 | ||||||||||||||||||
| 598 | } | - | ||||||||||||||||||
| 599 | - | |||||||||||||||||||
| 600 | static QMap<QByteArray, QString> _q_mapFromX509Name(X509_NAME *name) | - | ||||||||||||||||||
| 601 | { | - | ||||||||||||||||||
| 602 | QMap<QByteArray, QString> info; | - | ||||||||||||||||||
| 603 | for (int i = 0; i < q_X509_NAME_entry_count(name); ++i) {
| 432-2719 | ||||||||||||||||||
| 604 | X509_NAME_ENTRY *e = q_X509_NAME_get_entry(name, i); | - | ||||||||||||||||||
| 605 | - | |||||||||||||||||||
| 606 | QByteArray name = QSslCertificatePrivate::asn1ObjectName(q_X509_NAME_ENTRY_get_object(e)); | - | ||||||||||||||||||
| 607 | unsigned char *data = 0; | - | ||||||||||||||||||
| 608 | int size = q_ASN1_STRING_to_UTF8(&data, q_X509_NAME_ENTRY_get_data(e)); | - | ||||||||||||||||||
| 609 | info.insertMulti(name, QString::fromUtf8((char*)data, size)); | - | ||||||||||||||||||
| 610 | q_CRYPTO_free(data); | - | ||||||||||||||||||
| 611 | } executed 2719 times by 8 tests: end of blockExecuted by:
| 2719 | ||||||||||||||||||
| 612 | - | |||||||||||||||||||
| 613 | return info; executed 432 times by 8 tests: return info;Executed by:
| 432 | ||||||||||||||||||
| 614 | } | - | ||||||||||||||||||
| 615 | - | |||||||||||||||||||
| 616 | QSslCertificate QSslCertificatePrivate::QSslCertificate_from_X509(X509 *x509) | - | ||||||||||||||||||
| 617 | { | - | ||||||||||||||||||
| 618 | QSslCertificate certificate; | - | ||||||||||||||||||
| 619 | if (!x509 || !QSslSocket::supportsSsl())
| 0-20227 | ||||||||||||||||||
| 620 | return certificate; executed 44 times by 2 tests: return certificate;Executed by:
| 44 | ||||||||||||||||||
| 621 | - | |||||||||||||||||||
| 622 | ASN1_TIME *nbef = q_X509_get_notBefore(x509); | - | ||||||||||||||||||
| 623 | ASN1_TIME *naft = q_X509_get_notAfter(x509); | - | ||||||||||||||||||
| 624 | certificate.d->notValidBefore = q_getTimeFromASN1(nbef); | - | ||||||||||||||||||
| 625 | certificate.d->notValidAfter = q_getTimeFromASN1(naft); | - | ||||||||||||||||||
| 626 | certificate.d->null = false; | - | ||||||||||||||||||
| 627 | certificate.d->x509 = q_X509_dup(x509); | - | ||||||||||||||||||
| 628 | - | |||||||||||||||||||
| 629 | return certificate; executed 20227 times by 8 tests: return certificate;Executed by:
| 20227 | ||||||||||||||||||
| 630 | } | - | ||||||||||||||||||
| 631 | - | |||||||||||||||||||
| 632 | static bool matchLineFeed(const QByteArray &pem, int *offset) | - | ||||||||||||||||||
| 633 | { | - | ||||||||||||||||||
| 634 | char ch = 0; | - | ||||||||||||||||||
| 635 | - | |||||||||||||||||||
| 636 | // ignore extra whitespace at the end of the line | - | ||||||||||||||||||
| 637 | while (*offset < pem.size() && (ch = pem.at(*offset)) == ' ')
| 1-38312 | ||||||||||||||||||
| 638 | ++*offset; executed 4 times by 1 test: ++*offset;Executed by:
| 4 | ||||||||||||||||||
| 639 | - | |||||||||||||||||||
| 640 | if (ch == '\n') {
| 1-38308 | ||||||||||||||||||
| 641 | *offset += 1; | - | ||||||||||||||||||
| 642 | return true; executed 38308 times by 4 tests: return true;Executed by:
| 38308 | ||||||||||||||||||
| 643 | } | - | ||||||||||||||||||
| 644 | if (ch == '\r' && pem.size() > (*offset + 1) && pem.at(*offset + 1) == '\n') {
| 0-1 | ||||||||||||||||||
| 645 | *offset += 2; | - | ||||||||||||||||||
| 646 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 647 | } | - | ||||||||||||||||||
| 648 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||||||||
| 649 | } | - | ||||||||||||||||||
| 650 | - | |||||||||||||||||||
| 651 | QList<QSslCertificate> QSslCertificatePrivate::certificatesFromPem(const QByteArray &pem, int count) | - | ||||||||||||||||||
| 652 | { | - | ||||||||||||||||||
| 653 | QList<QSslCertificate> certificates; | - | ||||||||||||||||||
| 654 | QSslSocketPrivate::ensureInitialized(); | - | ||||||||||||||||||
| 655 | - | |||||||||||||||||||
| 656 | int offset = 0; | - | ||||||||||||||||||
| 657 | while (count == -1 || certificates.size() < count) {
| 58-28897 | ||||||||||||||||||
| 658 | int startPos = pem.indexOf(BEGINCERTSTRING, offset); | - | ||||||||||||||||||
| 659 | if (startPos == -1)
| 9801-19156 | ||||||||||||||||||
| 660 | break; executed 9801 times by 4 tests: break;Executed by:
| 9801 | ||||||||||||||||||
| 661 | startPos += sizeof(BEGINCERTSTRING) - 1; | - | ||||||||||||||||||
| 662 | if (!matchLineFeed(pem, &startPos))
| 1-19155 | ||||||||||||||||||
| 663 | break; executed 1 time by 1 test: break;Executed by:
| 1 | ||||||||||||||||||
| 664 | - | |||||||||||||||||||
| 665 | int endPos = pem.indexOf(ENDCERTSTRING, startPos); | - | ||||||||||||||||||
| 666 | if (endPos == -1)
| 1-19154 | ||||||||||||||||||
| 667 | break; executed 1 time by 1 test: break;Executed by:
| 1 | ||||||||||||||||||
| 668 | - | |||||||||||||||||||
| 669 | offset = endPos + sizeof(ENDCERTSTRING) - 1; | - | ||||||||||||||||||
| 670 | if (offset < pem.size() && !matchLineFeed(pem, &offset))
| 0-19153 | ||||||||||||||||||
| 671 | break; never executed: break; | 0 | ||||||||||||||||||
| 672 | - | |||||||||||||||||||
| 673 | QByteArray decoded = QByteArray::fromBase64( | - | ||||||||||||||||||
| 674 | QByteArray::fromRawData(pem.data() + startPos, endPos - startPos)); | - | ||||||||||||||||||
| 675 | const unsigned char *data = (const unsigned char *)decoded.data(); | - | ||||||||||||||||||
| 676 | - | |||||||||||||||||||
| 677 | if (X509 *x509 = q_d2i_X509(0, &data, decoded.size())) {
| 0-19154 | ||||||||||||||||||
| 678 | certificates << QSslCertificate_from_X509(x509); | - | ||||||||||||||||||
| 679 | q_X509_free(x509); | - | ||||||||||||||||||
| 680 | } executed 19154 times by 4 tests: end of blockExecuted by:
| 19154 | ||||||||||||||||||
| 681 | } executed 19154 times by 4 tests: end of blockExecuted by:
| 19154 | ||||||||||||||||||
| 682 | - | |||||||||||||||||||
| 683 | return certificates; executed 9861 times by 4 tests: return certificates;Executed by:
| 9861 | ||||||||||||||||||
| 684 | } | - | ||||||||||||||||||
| 685 | - | |||||||||||||||||||
| 686 | QList<QSslCertificate> QSslCertificatePrivate::certificatesFromDer(const QByteArray &der, int count) | - | ||||||||||||||||||
| 687 | { | - | ||||||||||||||||||
| 688 | QList<QSslCertificate> certificates; | - | ||||||||||||||||||
| 689 | QSslSocketPrivate::ensureInitialized(); | - | ||||||||||||||||||
| 690 | - | |||||||||||||||||||
| 691 | const unsigned char *data = (const unsigned char *)der.data(); | - | ||||||||||||||||||
| 692 | int size = der.size(); | - | ||||||||||||||||||
| 693 | - | |||||||||||||||||||
| 694 | while (size > 0 && (count == -1 || certificates.size() < count)) {
| 0-57 | ||||||||||||||||||
| 695 | if (X509 *x509 = q_d2i_X509(0, &data, size)) {
| 14-43 | ||||||||||||||||||
| 696 | certificates << QSslCertificate_from_X509(x509); | - | ||||||||||||||||||
| 697 | q_X509_free(x509); | - | ||||||||||||||||||
| 698 | } else { executed 14 times by 1 test: end of blockExecuted by:
| 14 | ||||||||||||||||||
| 699 | break; executed 43 times by 1 test: break;Executed by:
| 43 | ||||||||||||||||||
| 700 | } | - | ||||||||||||||||||
| 701 | size -= ((const char *)data - der.data()); | - | ||||||||||||||||||
| 702 | } executed 14 times by 1 test: end of blockExecuted by:
| 14 | ||||||||||||||||||
| 703 | - | |||||||||||||||||||
| 704 | return certificates; executed 57 times by 1 test: return certificates;Executed by:
| 57 | ||||||||||||||||||
| 705 | } | - | ||||||||||||||||||
| 706 | - | |||||||||||||||||||
| 707 | QT_END_NAMESPACE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |