-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathqgsauthpkcs12method.cpp
395 lines (334 loc) · 13.1 KB
/
qgsauthpkcs12method.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***************************************************************************
qgsauthpkcs12method.cpp
---------------------
begin : September 1, 2015
copyright : (C) 2015 by Boundless Spatial, Inc. USA
author : Larry Shaffer
email : lshaffer at boundlessgeo dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsauthpkcs12method.h"
#include "qgsauthcertutils.h"
#include "qgsauthmanager.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#ifdef HAVE_GUI
#include "qgsauthpkcs12edit.h"
#endif
#include <QDir>
#include <QFile>
#include <QRegularExpression>
#include <QUuid>
#ifndef QT_NO_SSL
#include <QtCrypto>
#include <QSslConfiguration>
#include <QSslError>
#endif
#include <QMutexLocker>
const QString QgsAuthPkcs12Method::AUTH_METHOD_KEY = QStringLiteral( "PKI-PKCS#12" );
const QString QgsAuthPkcs12Method::AUTH_METHOD_DESCRIPTION = QStringLiteral( "PKI PKCS#12 authentication" );
const QString QgsAuthPkcs12Method::AUTH_METHOD_DISPLAY_DESCRIPTION = tr( "PKI PKCS#12 authentication" );
#ifndef QT_NO_SSL
QMap<QString, QgsPkiConfigBundle *> QgsAuthPkcs12Method::sPkiConfigBundleCache = QMap<QString, QgsPkiConfigBundle *>();
#endif
QgsAuthPkcs12Method::QgsAuthPkcs12Method()
{
setVersion( 2 );
setExpansions( QgsAuthMethod::NetworkRequest | QgsAuthMethod::DataSourceUri );
setDataProviders( QStringList()
<< QStringLiteral( "ows" )
<< QStringLiteral( "wfs" ) // convert to lowercase
<< QStringLiteral( "wcs" )
<< QStringLiteral( "wms" )
<< QStringLiteral( "postgres" ) );
}
QgsAuthPkcs12Method::~QgsAuthPkcs12Method()
{
#ifndef QT_NO_SSL
qDeleteAll( sPkiConfigBundleCache );
sPkiConfigBundleCache.clear();
#endif
}
QString QgsAuthPkcs12Method::key() const
{
return AUTH_METHOD_KEY;
}
QString QgsAuthPkcs12Method::description() const
{
return AUTH_METHOD_DESCRIPTION;
}
QString QgsAuthPkcs12Method::displayDescription() const
{
return AUTH_METHOD_DISPLAY_DESCRIPTION;
}
bool QgsAuthPkcs12Method::updateNetworkRequest( QNetworkRequest &request, const QString &authcfg,
const QString &dataprovider )
{
#ifndef QT_NO_SSL
Q_UNUSED( dataprovider )
const QMutexLocker locker( &mMutex );
// TODO: is this too restrictive, to intercept only HTTPS connections?
if ( request.url().scheme().toLower() != QLatin1String( "https" ) )
{
QgsDebugMsgLevel( QStringLiteral( "Update request SSL config SKIPPED for authcfg %1: not HTTPS" ).arg( authcfg ), 2 );
return true;
}
QgsDebugMsgLevel( QStringLiteral( "Update request SSL config: HTTPS connection for authcfg: %1" ).arg( authcfg ), 2 );
QgsPkiConfigBundle *pkibundle = getPkiConfigBundle( authcfg );
if ( !pkibundle || !pkibundle->isValid() )
{
QgsDebugError( QStringLiteral( "Update request SSL config FAILED for authcfg: %1: PKI bundle invalid" ).arg( authcfg ) );
return false;
}
QgsDebugMsgLevel( QStringLiteral( "Update request SSL config: PKI bundle valid for authcfg: %1" ).arg( authcfg ), 2 );
QSslConfiguration sslConfig = request.sslConfiguration();
//QSslConfiguration sslConfig( QSslConfiguration::defaultConfiguration() );
sslConfig.setLocalCertificate( pkibundle->clientCert() );
sslConfig.setPrivateKey( pkibundle->clientCertKey() );
// add extra CAs from the bundle, QNAM will prepend the trusted CAs in createRequest()
if ( pkibundle->config().config( QStringLiteral( "addcas" ), QStringLiteral( "false" ) ) == QStringLiteral( "true" ) )
{
if ( pkibundle->config().config( QStringLiteral( "addrootca" ), QStringLiteral( "false" ) ) == QStringLiteral( "true" ) )
{
sslConfig.setCaCertificates( pkibundle->caChain() );
}
else
{
sslConfig.setCaCertificates( QgsAuthCertUtils::casRemoveSelfSigned( pkibundle->caChain() ) );
}
}
request.setSslConfiguration( sslConfig );
return true;
#else
return false;
#endif
}
bool QgsAuthPkcs12Method::updateDataSourceUriItems( QStringList &connectionItems, const QString &authcfg,
const QString &dataprovider )
{
#ifndef QT_NO_SSL
Q_UNUSED( dataprovider )
const QMutexLocker locker( &mMutex );
QgsDebugMsgLevel( QStringLiteral( "Update URI items for authcfg: %1" ).arg( authcfg ), 2 );
QgsPkiConfigBundle *pkibundle = getPkiConfigBundle( authcfg );
if ( !pkibundle || !pkibundle->isValid() )
{
QgsDebugError( QStringLiteral( "Update URI items FAILED: PKI bundle invalid" ) );
return false;
}
QgsDebugMsgLevel( QStringLiteral( "Update URI items: PKI bundle valid" ), 2 );
const QString pkiTempFileBase = QStringLiteral( "tmppki_%1.pem" );
// save client cert to temp file
const QString certFilePath = QgsAuthCertUtils::pemTextToTempFile(
pkiTempFileBase.arg( QUuid::createUuid().toString() ),
pkibundle->clientCert().toPem() );
if ( certFilePath.isEmpty() )
{
return false;
}
// save client cert key to temp file
const QString keyFilePath = QgsAuthCertUtils::pemTextToTempFile(
pkiTempFileBase.arg( QUuid::createUuid().toString() ),
pkibundle->clientCertKey().toPem() );
if ( keyFilePath.isEmpty() )
{
return false;
}
// add extra CAs from the bundle
QList<QSslCertificate> cas;
if ( pkibundle->config().config( QStringLiteral( "addcas" ), QStringLiteral( "false" ) ) == QStringLiteral( "true" ) )
{
if ( pkibundle->config().config( QStringLiteral( "addrootca" ), QStringLiteral( "false" ) ) == QStringLiteral( "true" ) )
{
cas = QgsAuthCertUtils::casMerge( QgsApplication::authManager()->trustedCaCerts(), pkibundle->caChain() );
}
else
{
cas = QgsAuthCertUtils::casMerge( QgsApplication::authManager()->trustedCaCerts(),
QgsAuthCertUtils::casRemoveSelfSigned( pkibundle->caChain() ) );
}
}
else
{
cas = QgsApplication::authManager()->trustedCaCerts();
}
// save CAs to temp file
const QString caFilePath = QgsAuthCertUtils::pemTextToTempFile(
pkiTempFileBase.arg( QUuid::createUuid().toString() ),
QgsAuthCertUtils::certsToPemText( cas ) );
if ( caFilePath.isEmpty() )
{
return false;
}
// get common name of the client certificate
const QString commonName = QgsAuthCertUtils::resolvedCertName( pkibundle->clientCert(), false );
// add uri parameters
const QString userparam = "user='" + commonName + "'";
const thread_local QRegularExpression userRegExp( "^user='.*" );
const int userindx = connectionItems.indexOf( userRegExp );
if ( userindx != -1 )
{
connectionItems.replace( userindx, userparam );
}
else
{
connectionItems.append( userparam );
}
const QString certparam = "sslcert='" + certFilePath + "'";
const thread_local QRegularExpression sslcertRegExp( "^sslcert='.*" );
const int sslcertindx = connectionItems.indexOf( sslcertRegExp );
if ( sslcertindx != -1 )
{
connectionItems.replace( sslcertindx, certparam );
}
else
{
connectionItems.append( certparam );
}
const QString keyparam = "sslkey='" + keyFilePath + "'";
const thread_local QRegularExpression sslkeyRegExp( "^sslkey='.*" );
const int sslkeyindx = connectionItems.indexOf( sslkeyRegExp );
if ( sslkeyindx != -1 )
{
connectionItems.replace( sslkeyindx, keyparam );
}
else
{
connectionItems.append( keyparam );
}
const QString caparam = "sslrootcert='" + caFilePath + "'";
const thread_local QRegularExpression sslcaRegExp( "^sslrootcert='.*" );
const int sslcaindx = connectionItems.indexOf( sslcaRegExp );
if ( sslcaindx != -1 )
{
connectionItems.replace( sslcaindx, caparam );
}
else
{
connectionItems.append( caparam );
}
return true;
#else
return false;
#endif
}
void QgsAuthPkcs12Method::clearCachedConfig( const QString &authcfg )
{
#ifndef QT_NO_SSL
const QMutexLocker locker( &mMutex );
removePkiConfigBundle( authcfg );
#endif
}
void QgsAuthPkcs12Method::updateMethodConfig( QgsAuthMethodConfig &mconfig )
{
const QMutexLocker locker( &mMutex );
if ( mconfig.hasConfig( QStringLiteral( "oldconfigstyle" ) ) )
{
QgsDebugMsgLevel( QStringLiteral( "Updating old style auth method config" ), 2 );
const QStringList conflist = mconfig.config( QStringLiteral( "oldconfigstyle" ) ).split( QStringLiteral( "|||" ) );
mconfig.setConfig( QStringLiteral( "bundlepath" ), conflist.at( 0 ) );
mconfig.setConfig( QStringLiteral( "bundlepass" ), conflist.at( 1 ) );
mconfig.removeConfig( QStringLiteral( "oldconfigstyle" ) );
}
// TODO: add updates as method version() increases due to config storage changes
}
#ifndef QT_NO_SSL
QgsPkiConfigBundle *QgsAuthPkcs12Method::getPkiConfigBundle( const QString &authcfg )
{
QMutexLocker locker( &mMutex );
QgsPkiConfigBundle *bundle = nullptr;
// check if it is cached
if ( sPkiConfigBundleCache.contains( authcfg ) )
{
bundle = sPkiConfigBundleCache.value( authcfg );
if ( bundle )
{
QgsDebugMsgLevel( QStringLiteral( "Retrieved PKI bundle for authcfg %1" ).arg( authcfg ), 2 );
return bundle;
}
}
// else build PKI bundle
QgsAuthMethodConfig mconfig;
if ( !QgsApplication::authManager()->loadAuthenticationConfig( authcfg, mconfig, true ) )
{
QgsDebugError( QStringLiteral( "PKI bundle for authcfg %1: FAILED to retrieve config" ).arg( authcfg ) );
return bundle;
}
const QStringList bundlelist = QgsAuthCertUtils::pkcs12BundleToPem( mconfig.config( QStringLiteral( "bundlepath" ) ),
mconfig.config( QStringLiteral( "bundlepass" ) ), false );
if ( bundlelist.isEmpty() || bundlelist.size() < 2 )
{
QgsDebugError( QStringLiteral( "PKI bundle for authcfg %1: insert FAILED, PKCS#12 bundle parsing failed" ).arg( authcfg ) );
return bundle;
}
// init client cert
// Note: if this is not valid, no sense continuing
const QSslCertificate clientcert( bundlelist.at( 0 ).toLatin1() );
if ( !QgsAuthCertUtils::certIsViable( clientcert ) )
{
QgsDebugError( QStringLiteral( "PKI bundle for authcfg %1: insert FAILED, client cert is not viable" ).arg( authcfg ) );
return bundle;
}
// !!! DON'T LEAVE THESE UNCOMMENTED !!!
// QgsDebugMsgLevel( QStringLiteral( "PKI bundle key for authcfg: \n%1" ).arg( bundlelist.at( 1 ) ), 2 );
// QgsDebugMsgLevel( QStringLiteral( "PKI bundle key pass for authcfg: \n%1" )
// .arg( !mconfig.config( QStringLiteral( "bundlepass" ) ).isNull() ? mconfig.config( QStringLiteral( "bundlepass" ) ) : QString() ), 2 );
// init key
const QSslKey clientkey( bundlelist.at( 1 ).toLatin1(),
QSsl::Rsa,
QSsl::Pem,
QSsl::PrivateKey,
!mconfig.config( QStringLiteral( "bundlepass" ) ).isNull() ? mconfig.config( QStringLiteral( "bundlepass" ) ).toUtf8() : QByteArray() );
if ( clientkey.isNull() )
{
QgsDebugError( QStringLiteral( "PKI bundle for authcfg %1: insert FAILED, cert key is null" ).arg( authcfg ) );
return bundle;
}
bundle = new QgsPkiConfigBundle( mconfig, clientcert, clientkey,
QgsAuthCertUtils::pkcs12BundleCas(
mconfig.config( QStringLiteral( "bundlepath" ) ),
mconfig.config( QStringLiteral( "bundlepass" ) ) ) );
locker.unlock();
// cache bundle
putPkiConfigBundle( authcfg, bundle );
return bundle;
}
void QgsAuthPkcs12Method::putPkiConfigBundle( const QString &authcfg, QgsPkiConfigBundle *pkibundle )
{
const QMutexLocker locker( &mMutex );
QgsDebugMsgLevel( QStringLiteral( "Putting PKI bundle for authcfg %1" ).arg( authcfg ), 2 );
sPkiConfigBundleCache.insert( authcfg, pkibundle );
}
void QgsAuthPkcs12Method::removePkiConfigBundle( const QString &authcfg )
{
const QMutexLocker locker( &mMutex );
if ( sPkiConfigBundleCache.contains( authcfg ) )
{
QgsPkiConfigBundle *pkibundle = sPkiConfigBundleCache.take( authcfg );
delete pkibundle;
pkibundle = nullptr;
QgsDebugMsgLevel( QStringLiteral( "Removed PKI bundle for authcfg: %1" ).arg( authcfg ), 2 );
}
}
#endif
#ifdef HAVE_GUI
QWidget *QgsAuthPkcs12Method::editWidget( QWidget *parent ) const
{
return new QgsAuthPkcs12Edit( parent );
}
#endif
//////////////////////////////////////////////
// Plugin externals
//////////////////////////////////////////////
#ifndef HAVE_STATIC_PROVIDERS
QGISEXTERN QgsAuthMethodMetadata *authMethodMetadataFactory()
{
return new QgsAuthPkcs12MethodMetadata();
}
#endif