/
qgsauthpkcs12method.cpp
249 lines (202 loc) · 7.23 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
/***************************************************************************
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 "qgsauthpkcs12edit.h"
#ifndef QT_NO_OPENSSL
#include <QtCrypto>
#include <QSslConfiguration>
#include <QSslError>
#endif
#include "qgsauthcertutils.h"
#include "qgsauthmanager.h"
#include "qgslogger.h"
static const QString AUTH_METHOD_KEY = "PKI-PKCS#12";
static const QString AUTH_METHOD_DESCRIPTION = "PKI PKCS#12 authentication";
QMap<QString, QgsPkiConfigBundle *> QgsAuthPkcs12Method::mPkiConfigBundleCache = QMap<QString, QgsPkiConfigBundle *>();
QgsAuthPkcs12Method::QgsAuthPkcs12Method()
: QgsAuthMethod()
{
setVersion( 2 );
setExpansions( QgsAuthMethod::NetworkRequest );
setDataProviders( QStringList()
<< "ows"
<< "wfs" // convert to lowercase
<< "wcs"
<< "wms" );
}
QgsAuthPkcs12Method::~QgsAuthPkcs12Method()
{
qDeleteAll( mPkiConfigBundleCache.values() );
mPkiConfigBundleCache.clear();
}
QString QgsAuthPkcs12Method::key() const
{
return AUTH_METHOD_KEY;
}
QString QgsAuthPkcs12Method::description() const
{
return AUTH_METHOD_DESCRIPTION;
}
QString QgsAuthPkcs12Method::displayDescription() const
{
return tr( "PKI PKCS#12 authentication" );
}
bool QgsAuthPkcs12Method::updateNetworkRequest( QNetworkRequest &request, const QString &authcfg,
const QString &dataprovider )
{
Q_UNUSED( dataprovider )
// TODO: is this too restrictive, to intercept only HTTPS connections?
if ( request.url().scheme().toLower() != QLatin1String( "https" ) )
{
QgsDebugMsg( QString( "Update request SSL config SKIPPED for authcfg %1: not HTTPS" ).arg( authcfg ) );
return true;
}
QgsDebugMsg( QString( "Update request SSL config: HTTPS connection for authcfg: %1" ).arg( authcfg ) );
QgsPkiConfigBundle * pkibundle = getPkiConfigBundle( authcfg );
if ( !pkibundle || !pkibundle->isValid() )
{
QgsDebugMsg( QString( "Update request SSL config FAILED for authcfg: %1: PKI bundle invalid" ).arg( authcfg ) );
return false;
}
QgsDebugMsg( QString( "Update request SSL config: PKI bundle valid for authcfg: %1" ).arg( authcfg ) );
QSslConfiguration sslConfig = request.sslConfiguration();
//QSslConfiguration sslConfig( QSslConfiguration::defaultConfiguration() );
sslConfig.setLocalCertificate( pkibundle->clientCert() );
sslConfig.setPrivateKey( pkibundle->clientCertKey() );
request.setSslConfiguration( sslConfig );
return true;
}
void QgsAuthPkcs12Method::clearCachedConfig( const QString &authcfg )
{
removePkiConfigBundle( authcfg );
}
void QgsAuthPkcs12Method::updateMethodConfig( QgsAuthMethodConfig &mconfig )
{
if ( mconfig.hasConfig( "oldconfigstyle" ) )
{
QgsDebugMsg( "Updating old style auth method config" );
QStringList conflist = mconfig.config( "oldconfigstyle" ).split( "|||" );
mconfig.setConfig( "bundlepath", conflist.at( 0 ) );
mconfig.setConfig( "bundlepass", conflist.at( 1 ) );
mconfig.removeConfig( "oldconfigstyle" );
}
// TODO: add updates as method version() increases due to config storage changes
}
QgsPkiConfigBundle *QgsAuthPkcs12Method::getPkiConfigBundle( const QString &authcfg )
{
QgsPkiConfigBundle * bundle = 0;
// check if it is cached
if ( mPkiConfigBundleCache.contains( authcfg ) )
{
bundle = mPkiConfigBundleCache.value( authcfg );
if ( bundle )
{
QgsDebugMsg( QString( "Retrieved PKI bundle for authcfg %1" ).arg( authcfg ) );
return bundle;
}
}
// else build PKI bundle
QgsAuthMethodConfig mconfig;
if ( !QgsAuthManager::instance()->loadAuthenticationConfig( authcfg, mconfig, true ) )
{
QgsDebugMsg( QString( "PKI bundle for authcfg %1: FAILED to retrieve config" ).arg( authcfg ) );
return bundle;
}
QStringList bundlelist = QgsAuthCertUtils::pkcs12BundleToPem( mconfig.config( "bundlepath" ),
mconfig.config( "bundlepass" ), false );
// init client cert
// Note: if this is not valid, no sense continuing
QSslCertificate clientcert( bundlelist.at( 0 ).toAscii() );
if ( !clientcert.isValid() )
{
QgsDebugMsg( QString( "PKI bundle for authcfg %1: insert FAILED, client cert is not valid" ).arg( authcfg ) );
return bundle;
}
// init key
QSslKey clientkey( bundlelist.at( 1 ).toAscii(),
QSsl::Rsa,
QSsl::Pem,
QSsl::PrivateKey,
!mconfig.config( "bundlepass" ).isNull() ? mconfig.config( "bundlepass" ).toUtf8() : QByteArray() );
if ( clientkey.isNull() )
{
QgsDebugMsg( QString( "PKI bundle for authcfg %1: insert FAILED, cert key is null" ).arg( authcfg ) );
return bundle;
}
bundle = new QgsPkiConfigBundle( mconfig, clientcert, clientkey );
// cache bundle
putPkiConfigBundle( authcfg, bundle );
return bundle;
}
void QgsAuthPkcs12Method::putPkiConfigBundle( const QString &authcfg, QgsPkiConfigBundle *pkibundle )
{
QgsDebugMsg( QString( "Putting PKI bundle for authcfg %1" ).arg( authcfg ) );
mPkiConfigBundleCache.insert( authcfg, pkibundle );
}
void QgsAuthPkcs12Method::removePkiConfigBundle( const QString &authcfg )
{
if ( mPkiConfigBundleCache.contains( authcfg ) )
{
QgsPkiConfigBundle * pkibundle = mPkiConfigBundleCache.take( authcfg );
delete pkibundle;
pkibundle = 0;
QgsDebugMsg( QString( "Removed PKI bundle for authcfg: %1" ).arg( authcfg ) );
}
}
//////////////////////////////////////////////
// Plugin externals
//////////////////////////////////////////////
/**
* Required class factory to return a pointer to a newly created object
*/
QGISEXTERN QgsAuthPkcs12Method *classFactory()
{
return new QgsAuthPkcs12Method();
}
/** Required key function (used to map the plugin to a data store type)
*/
QGISEXTERN QString authMethodKey()
{
return AUTH_METHOD_KEY;
}
/**
* Required description function
*/
QGISEXTERN QString description()
{
return AUTH_METHOD_DESCRIPTION;
}
/**
* Required isAuthMethod function. Used to determine if this shared library
* is an authentication method plugin
*/
QGISEXTERN bool isAuthMethod()
{
return true;
}
/**
* Optional class factory to return a pointer to a newly created edit widget
*/
QGISEXTERN QgsAuthPkcs12Edit *editWidget( QWidget *parent )
{
return new QgsAuthPkcs12Edit( parent );
}
/**
* Required cleanup function
*/
QGISEXTERN void cleanupAuthMethod() // pass QgsAuthMethod *method, then delete method ?
{
}