Debian Codegraph @

src/network/ssl/qsslsocket_mac_shared.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Copyright (C) 2015 ownCloud Inc
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the QtNetwork module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 //#define QSSLSOCKET_DEBUG
42 //#define QT_DECRYPT_SSL_TRAFFIC
43
44 #include "qssl_p.h"
45 #include "qsslsocket.h"
46
47 #ifndef QT_NO_OPENSSL
48 # include "qsslsocket_openssl_p.h"
49 # include "qsslsocket_openssl_symbols_p.h"
50 #endif
51
52 #include "qsslcertificate_p.h"
53
54 #ifdef Q_OS_DARWIN
55 # include <private/qcore_mac_p.h>
56 #endif
57
58 #include <QtCore/qdebug.h>
59
60 #ifdef Q_OS_MACOS
61 # include <Security/Security.h>
62 #endif
63
64
65 QT_BEGIN_NAMESPACE
66
67 #ifdef Q_OS_MACOS
68 namespace {
69
70 bool hasTrustedSslServerPolicy(SecPolicyRef policy, CFDictionaryRef props) {
71 QCFType<CFDictionaryRef> policyProps = SecPolicyCopyProperties(policy);
72 // only accept certificates with policies for SSL server validation for now
73 if (CFEqual(CFDictionaryGetValue(policyProps, kSecPolicyOid), kSecPolicyAppleSSL)) {
74 CFBooleanRef policyClient;
75 if (CFDictionaryGetValueIfPresent(policyProps, kSecPolicyClient, reinterpret_cast<const void**>(&policyClient)) &&
76 CFEqual(policyClient, kCFBooleanTrue)) {
77 return false; // no client certs
78 }
79 if (!CFDictionaryContainsKey(props, kSecTrustSettingsResult)) {
80 // as per the docs, no trust settings result implies full trust
81 return true;
82 }
83 CFNumberRef number = static_cast<CFNumberRef>(CFDictionaryGetValue(props, kSecTrustSettingsResult));
84 SecTrustSettingsResult settingsResult;
85 CFNumberGetValue(number, kCFNumberSInt32Type, &settingsResult);
86 switch (settingsResult) {
87 case kSecTrustSettingsResultTrustRoot:
88 case kSecTrustSettingsResultTrustAsRoot:
89 return true;
90 default:
91 return false;
92 }
93 }
94 return false;
95 }
96
97 bool isCaCertificateTrusted(SecCertificateRef cfCert, int domain)
98 {
99 QCFType<CFArrayRef> cfTrustSettings;
100 OSStatus status = SecTrustSettingsCopyTrustSettings(cfCert, SecTrustSettingsDomain(domain), &cfTrustSettings);
101 if (status == noErr) {
102 CFIndex size = CFArrayGetCount(cfTrustSettings);
103 // if empty, trust for everything (as per the Security Framework documentation)
104 if (size == 0) {
105 return true;
106 } else {
107 for (CFIndex i = 0; i < size; ++i) {
108 CFDictionaryRef props = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(cfTrustSettings, i));
109 if (CFDictionaryContainsKey(props, kSecTrustSettingsPolicy)) {
110 if (hasTrustedSslServerPolicy((SecPolicyRef)CFDictionaryGetValue(props, kSecTrustSettingsPolicy), props))
111 return true;
112 }
113 }
114 }
115 } else {
116 qCWarning(lcSsl, "Error receiving trust for a CA certificate");
117 }
118 return false;
119 }
120
121 } // anon namespace
122 #endif // Q_OS_MACOS
123
124 QList<QSslCertificate> QSslSocketPrivate::systemCaCertificates()
125 {
126 ensureInitialized();
127
128 QList<QSslCertificate> systemCerts;
129 // SecTrustSettingsCopyCertificates is not defined on iOS.
130 #ifdef Q_OS_MACOS
131 // iterate through all enum members, order:
132 // kSecTrustSettingsDomainUser, kSecTrustSettingsDomainAdmin, kSecTrustSettingsDomainSystem
133 for (int dom = kSecTrustSettingsDomainUser; dom <= int(kSecTrustSettingsDomainSystem); dom++) {
134 QCFType<CFArrayRef> cfCerts;
135 OSStatus status = SecTrustSettingsCopyCertificates(SecTrustSettingsDomain(dom), &cfCerts);
136 if (status == noErr) {
137 const CFIndex size = CFArrayGetCount(cfCerts);
138 for (CFIndex i = 0; i < size; ++i) {
139 SecCertificateRef cfCert = (SecCertificateRef)CFArrayGetValueAtIndex(cfCerts, i);
140 QCFType<CFDataRef> derData = SecCertificateCopyData(cfCert);
141 if (isCaCertificateTrusted(cfCert, dom)) {
142 if (derData == NULL) {
143 qCWarning(lcSsl, "Error retrieving a CA certificate from the system store");
144 } else {
145 systemCerts << QSslCertificate(QByteArray::fromCFData(derData), QSsl::Der);
146 }
147 }
148 }
149 }
150 }
151 #endif
152 return systemCerts;
153 }
154
155 QT_END_NAMESPACE