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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <IceSSL/Config.h>
#include <IceSSL/UWPEngineF.h>
#include <IceSSL/UWPEngine.h>
#include <IceSSL/UWPTransceiverI.h>
#include <IceSSL/Util.h>
#include <Ice/Communicator.h>
#include <Ice/StringConverter.h>
#include <Ice/StringUtil.h>
#include <Ice/Properties.h>
#include <Ice/Network.h>
#include <IceUtil/Shared.h>
#include <string>
using namespace std;
using namespace Ice;
using namespace IceUtil;
using namespace IceSSL;
using namespace concurrency;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::Security::Cryptography;
using namespace Windows::Security::Cryptography::Core;
using namespace Windows::Security::Cryptography::Certificates;
IceUtil::Shared* UWP::upCast(UWP::SSLEngine* p) { return p; }
namespace
{
//
// Find a certificate in the Application Personal certificate store
// with the given friendly name. Returns the matching certificate or
// nullptr if none is found.
//
Certificates::Certificate^
findPersonalCertificate(String^ friendlyName)
{
CertificateQuery^ query = ref new CertificateQuery();
query->IncludeDuplicates = true;
query->IncludeExpiredCertificates = true;
query->FriendlyName = friendlyName;
query->StoreName = StandardCertificateStoreNames::Personal;
try
{
auto certificates = IceInternal::runSync(CertificateStores::FindAllAsync(query));
return certificates->Size > 0 ? certificates->GetAt(0) : nullptr;
}
catch(Platform::Exception^ ex)
{
throw PluginInitializationException(__FILE__, __LINE__,
"IceSSL: certificate error:\n" + wstringToString(ex->Message->Data()));
}
}
//
// Import a certificate in the Application Personal certificate store
// with the given friendly name. Returns true if there was a password
// error and false otherwise. If the import fails because a different
// error PluginInitializationException exception is throw.
//
bool
importPfxData(String^ friendlyName, String^ data, String^ password)
{
try
{
IceInternal::runSync(CertificateEnrollmentManager::ImportPfxDataAsync(
data,
password,
ExportOption::NotExportable,
KeyProtectionLevel::NoConsent,
InstallOptions::None,
friendlyName));
return false; // The import succcess
}
catch(Platform::Exception^ ex)
{
if(HRESULT_CODE(ex->HResult) == ERROR_DECRYPTION_FAILED)
{
return true; // Password error
}
else
{
throw PluginInitializationException(__FILE__, __LINE__,
"IceSSL: certificate error:\n" + wstringToString(ex->Message->Data()));
}
}
}
Certificates::Certificate^
importPersonalCertificate(const string& filename, function<string ()> password, bool passwordPrompt,
int passwordRetryMax)
{
auto uri = ref new Uri(ref new String(stringToWstring(filename).c_str()));
try
{
auto file = IceInternal::runSync(StorageFile::GetFileFromApplicationUriAsync(uri));
auto buffer = IceInternal::runSync(FileIO::ReadBufferAsync(file));
//
// Create a hash of the certificate to use as a friendly name, this will allow us
// to uniquely identify the certificate in the store.
//
auto hasher = HashAlgorithmProvider::OpenAlgorithm(HashAlgorithmNames::Sha1);
auto hash = hasher->CreateHash();
hash->Append(buffer);
String^ friendlyName = CryptographicBuffer::EncodeToBase64String(hash->GetValueAndReset());
//
// If the certificate is already in the store we avoid importing it.
//
Certificates::Certificate^ cert = findPersonalCertificate(friendlyName);
if(cert)
{
return cert;
}
else
{
String^ data = CryptographicBuffer::EncodeToBase64String(buffer);
int count = 0;
bool passwordErr = false;
do
{
passwordErr = importPfxData(friendlyName, data,
ref new String(stringToWstring(password()).c_str()));
}
while(passwordPrompt && passwordErr && ++count < passwordRetryMax);
if(passwordErr)
{
throw PluginInitializationException(__FILE__, __LINE__, "IceSSL: error decoding certificate");
}
return findPersonalCertificate(friendlyName);
}
}
catch(Platform::Exception^ ex)
{
if(HRESULT_CODE(ex->HResult) == ERROR_FILE_NOT_FOUND)
{
throw PluginInitializationException(__FILE__, __LINE__, "certificate file not found:\n" + filename);
}
else
{
throw PluginInitializationException(__FILE__, __LINE__,
"IceSSL: certificate error:\n" + wstringToString(ex->Message->Data()));
}
}
}
IVectorView<Certificates::Certificate^>^
findCertificates(const string& name, const string& value)
{
CertificateQuery^ query = ref new CertificateQuery();
query->StoreName = ref new String(stringToWstring(name).c_str());
query->IncludeDuplicates = true;
query->IncludeExpiredCertificates = true;
if(value != "*")
{
if(value.find(':', 0) == string::npos)
{
throw PluginInitializationException(__FILE__, __LINE__, "IceSSL: no key in `" + value + "'");
}
size_t start = 0;
size_t pos;
while((pos = value.find(':', start)) != string::npos)
{
string field = IceUtilInternal::toUpper(IceUtilInternal::trim(value.substr(start, pos - start)));
if(field != "ISSUER" && field != "THUMBPRINT" && field != "FRIENDLYNAME")
{
throw PluginInitializationException(__FILE__, __LINE__, "IceSSL: unknown key in `" + value + "'");
}
start = pos + 1;
while(start < value.size() && (value[start] == ' ' || value[start] == '\t'))
{
++start;
}
if(start == value.size())
{
throw PluginInitializationException(__FILE__, __LINE__, "IceSSL: missing argument in `" + value + "'");
}
string arg;
if(value[start] == '"' || value[start] == '\'')
{
size_t end = start;
++end;
while(end < value.size())
{
if(value[end] == value[start] && value[end - 1] != '\\')
{
break;
}
++end;
}
if(end == value.size() || value[end] != value[start])
{
throw PluginInitializationException(__FILE__, __LINE__, "IceSSL: unmatched quote in `" + value + "'");
}
++start;
arg = value.substr(start, end - start);
start = end + 1;
}
else
{
size_t end = value.find_first_of(" \t", start);
if(end == string::npos)
{
arg = value.substr(start);
start = value.size();
}
else
{
arg = value.substr(start, end - start);
start = end + 1;
}
}
if(field == "ISSUER")
{
query->IssuerName = ref new String(stringToWstring(arg).c_str());
}
else if(field == "FRIENDLYNAME")
{
query->FriendlyName = ref new String(stringToWstring(arg).c_str());
}
else if(field == "THUMBPRINT")
{
vector<BYTE> buffer;
if(!parseBytes(arg, buffer))
{
throw PluginInitializationException(__FILE__, __LINE__,
"IceSSL: invalid `IceSSL.FindCert' property: can't decode the value");
}
query->Thumbprint = ref new Array<unsigned char>(&buffer[0], static_cast<unsigned int>(buffer.size()));
}
}
}
try
{
return IceInternal::runSync(CertificateStores::FindAllAsync(query));
}
catch(Platform::Exception^ ex)
{
throw PluginInitializationException(__FILE__, __LINE__,
"IceSSL: certificate error:\n" + wstringToString(ex->Message->Data()));
}
}
} // anonymous namespace end
UWP::SSLEngine::SSLEngine(const Ice::CommunicatorPtr& communicator) : IceSSL::SSLEngine(communicator)
{
}
void
UWP::SSLEngine::initialize()
{
Mutex::Lock lock(_mutex);
if(_initialized)
{
return;
}
IceSSL::SSLEngine::initialize();
const auto properties = communicator()->getProperties();
//
// Load client certificate
//
const int passwordRetryMax = properties->getPropertyAsIntWithDefault("IceSSL.PasswordRetryMax", 3);
setPassword(properties->getProperty("IceSSL.Password"));
string certFile = properties->getProperty("IceSSL.CertFile");
string findCert = properties->getProperty("IceSSL.FindCert");
if(!certFile.empty())
{
_certificate = dynamic_pointer_cast<UWP::Certificate>(UWP::Certificate::create(importPersonalCertificate(
certFile,
[this]()
{
return password(false);
},
getPasswordPrompt() != nullptr,
passwordRetryMax)));
}
else if(!findCert.empty())
{
auto certs = findCertificates(properties->getPropertyWithDefault("IceSSL.CertStore", "My"), findCert);
if(certs->Size == 0)
{
throw Ice::PluginInitializationException(__FILE__, __LINE__, "IceSSL: no certificates found");
}
_certificate = dynamic_pointer_cast<UWP::Certificate>(UWP::Certificate::create(certs->GetAt(0)));
}
_initialized = true;
}
bool
UWP::SSLEngine::initialized() const
{
return _initialized;
}
shared_ptr<UWP::Certificate>
UWP::SSLEngine::certificate()
{
return _certificate;
}
void
UWP::SSLEngine::destroy()
{
}
IceInternal::TransceiverPtr
UWP::SSLEngine::createTransceiver(const InstancePtr& instance,
const IceInternal::TransceiverPtr& delegate,
const string& hostOrAdapterName,
bool incoming)
{
return new UWP::TransceiverI(instance, delegate, hostOrAdapterName, incoming);
}
|