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
|
// **********************************************************************
//
// Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceSSL/Config.h>
#include <IceSSL/UWPTransceiverI.h>
#include <IceSSL/Instance.h>
#include <IceSSL/UWPEngine.h>
#include <IceSSL/ConnectionInfo.h>
#include <Ice/Logger.h>
#include <ppltasks.h>
using namespace std;
using namespace Ice;
using namespace IceSSL;
using namespace concurrency;
using namespace Platform;
using namespace Windows::Networking;
using namespace Windows::Networking::Sockets;
using namespace Windows::Foundation::Collections;
using namespace Windows::Security::Cryptography::Certificates;
namespace
{
std::string
validationResultToString(ChainValidationResult result)
{
switch (result)
{
case ChainValidationResult::Success:
{
return "The certificate chain was verified.";
}
case ChainValidationResult::Untrusted:
{
return "A certificate in the chain is not trusted.";
}
case ChainValidationResult::Revoked:
{
return "A certificate in the chain has been revoked.";
}
case ChainValidationResult::Expired:
{
return "A certificate in the chain has expired.";
}
case ChainValidationResult::IncompleteChain:
{
return "The certificate chain is missing one or more certificates.";
}
case ChainValidationResult::InvalidSignature:
{
return "The signature of a certificate in the chain cannot be verified.";
}
case ChainValidationResult::WrongUsage:
{
return "A certificate in the chain is being used for a purpose other than one specified by its CA.";
}
case ChainValidationResult::InvalidName:
{
return "A certificate in the chain has a name that is not valid. The name is either not included in "
"the permitted list or is explicitly excluded.";
}
case ChainValidationResult::InvalidCertificateAuthorityPolicy:
{
return "A certificate in the chain has a policy that is not valid.";
}
case ChainValidationResult::BasicConstraintsError:
{
return "The basic constraint extension of a certificate in the chain has not been observed.";
}
case ChainValidationResult::UnknownCriticalExtension:
{
return "A certificate in the chain contains an unknown extension that is marked \"critical\".";
}
case ChainValidationResult::RevocationInformationMissing:
{
return "No installed or registered DLL was found to verify revocation.";
}
case ChainValidationResult::RevocationFailure:
{
return "Unable to connect to the revocation server.";
}
case ChainValidationResult::OtherErrors:
{
return "An unexpected error occurred while validating the certificate chain.";
}
default:
{
assert(false);
return "";
}
}
}
}
IceInternal::NativeInfoPtr
UWP::TransceiverI::getNativeInfo()
{
return _delegate->getNativeInfo();
}
IceInternal::SocketOperation
UWP::TransceiverI::initialize(IceInternal::Buffer& readBuffer, IceInternal::Buffer& writeBuffer)
{
if(!_connected)
{
IceInternal::SocketOperation status = _delegate->initialize(readBuffer, writeBuffer);
if(status != IceInternal::SocketOperationNone)
{
return status;
}
_connected = true;
//
// Continue connecting, this will call startWrite/finishWrite to upgrade the stream
// to SSL.
//
return IceInternal::SocketOperationConnect;
}
else if(!_upgraded)
{
_upgraded = true;
try
{
auto fd = safe_cast<StreamSocket^>(_delegate->getNativeInfo()->fd());
if(fd->Information->ServerCertificate)
{
//
// Build the certificate chain
//
auto params = ref new ChainBuildingParameters();
params->AuthorityInformationAccessEnabled = false;
params->CurrentTimeValidationEnabled = true;
params->NetworkRetrievalEnabled = false;
params->RevocationCheckEnabled = false;
//
// BUGFIX: It is currently not possible to set ExclusiveTrustRoots programatically
// it is causing a read access exception see:https://goo.gl/B6OaNx
//
//if(_engine->ca())
//{
// params->ExclusiveTrustRoots->Append(_engine->ca()->getCert());
//}
try
{
_chain = create_task(fd->Information->ServerCertificate->BuildChainAsync(
fd->Information->ServerIntermediateCertificates, params)).get();
}
catch(Platform::Exception^ ex)
{
throw SyscallException(__FILE__, __LINE__, ex->HResult);
}
ChainValidationResult result = _chain->Validate();
if(result != ChainValidationResult::Success)
{
if(_engine->getVerifyPeer() == 0)
{
if(_instance->traceLevel() >= 1)
{
_instance->logger()->trace(_instance->traceCategory(),
"IceSSL: ignoring certificate verification failure\n" +
validationResultToString(result));
}
}
else
{
throw SecurityException(__FILE__, __LINE__,
"IceSSL: certificate validation error:\n" + validationResultToString(result));
}
}
else
{
_verified = true;
}
}
else if((!_incoming && _engine->getVerifyPeer() > 0) || (_incoming && _engine->getVerifyPeer() == 2))
{
//
// Clients require server certificate if VerifyPeer > 0 and servers require client
// certificate if VerifyPeer == 2
//
throw SecurityException(__FILE__, __LINE__, "IceSSL: certificate required");
}
if(_chain)
{
auto certs = _chain->GetCertificates(true);
for(auto iter = certs->First(); iter->HasCurrent; iter->MoveNext())
{
auto cert = UWP::Certificate::create(iter->Current);
_certs.push_back(cert);
}
}
_engine->verifyPeer(_host, dynamic_pointer_cast<IceSSL::ConnectionInfo>(getInfo()), toString());
}
catch(Platform::Exception^ ex)
{
ostringstream os;
os << "IceSSL: certificate verification failure:\n" << wstringToString(ex->Message->Data());
throw SecurityException(__FILE__, __LINE__, os.str());
}
}
return IceInternal::SocketOperationNone;
}
IceInternal::SocketOperation
UWP::TransceiverI::closing(bool initiator, const Ice::LocalException& ex)
{
return _delegate->closing(initiator, ex);
}
void
UWP::TransceiverI::close()
{
_delegate->close();
}
IceInternal::SocketOperation
UWP::TransceiverI::write(IceInternal::Buffer& buf)
{
return _delegate->write(buf);
}
IceInternal::SocketOperation
UWP::TransceiverI::read(IceInternal::Buffer& buf)
{
return _delegate->read(buf);
}
bool
UWP::TransceiverI::startWrite(IceInternal::Buffer& buf)
{
if(_connected && !_upgraded)
{
StreamSocket^ stream = safe_cast<StreamSocket^>(_delegate->getNativeInfo()->fd());
HostName^ host = ref new HostName(ref new String(IceUtil::stringToWstring(_host).c_str()));
//
// We ignore SSL Certificate errors at this point, the certificate chain will be validated
// when the chain is constructed in Transceiver::initialize
//
stream->Control->IgnorableServerCertificateErrors->Append(ChainValidationResult::Expired);
stream->Control->IgnorableServerCertificateErrors->Append(ChainValidationResult::IncompleteChain);
//
// Check if we need to enable host name verification
//
if(!_engine->getCheckCertName() || _host.empty() || _engine->getVerifyPeer() == 0)
{
stream->Control->IgnorableServerCertificateErrors->Append(ChainValidationResult::InvalidName);
}
stream->Control->IgnorableServerCertificateErrors->Append(ChainValidationResult::RevocationFailure);
stream->Control->IgnorableServerCertificateErrors->Append(ChainValidationResult::RevocationInformationMissing);
stream->Control->IgnorableServerCertificateErrors->Append(ChainValidationResult::Untrusted);
stream->Control->IgnorableServerCertificateErrors->Append(ChainValidationResult::WrongUsage);
if(_engine->certificate())
{
stream->Control->ClientCertificate = _engine->certificate()->getCert();
}
try
{
Windows::Foundation::IAsyncAction^ action = stream->UpgradeToSslAsync(SocketProtectionLevel::Tls12, host);
getNativeInfo()->queueAction(IceInternal::SocketOperationWrite, action);
}
catch(Platform::Exception^ ex)
{
IceInternal::checkErrorCode(__FILE__, __LINE__, ex->HResult);
}
return true;
}
return _delegate->startWrite(buf);
}
void
UWP::TransceiverI::finishWrite(IceInternal::Buffer& buf)
{
if(_connected && !_upgraded)
{
IceInternal::AsyncInfo* asyncInfo = getNativeInfo()->getAsyncInfo(IceInternal::SocketOperationWrite);
if(asyncInfo->error != ERROR_SUCCESS)
{
if(CERT_E_CN_NO_MATCH == asyncInfo->error)
{
ostringstream ostr;
ostr << "IceSSL: certificate validation failure: "
<< (IceInternal::isIpAddress(_host) ? "IP address mismatch" : "Hostname mismatch");
string msg = ostr.str();
if(_engine->securityTraceLevel() >= 1)
{
_instance->logger()->trace(_instance->traceCategory(), msg);
}
throw SecurityException(__FILE__, __LINE__, msg);
}
else
{
IceInternal::checkErrorCode(__FILE__, __LINE__, asyncInfo->error);
}
}
return;
}
_delegate->finishWrite(buf);
}
void
UWP::TransceiverI::startRead(IceInternal::Buffer& buf)
{
_delegate->startRead(buf);
}
void
UWP::TransceiverI::finishRead(IceInternal::Buffer& buf)
{
_delegate->finishRead(buf);
}
string
UWP::TransceiverI::protocol() const
{
return _instance->protocol();
}
string
UWP::TransceiverI::toString() const
{
return _delegate->toString();
}
string
UWP::TransceiverI::toDetailedString() const
{
return toString();
}
Ice::ConnectionInfoPtr
UWP::TransceiverI::getInfo() const
{
ConnectionInfoPtr info = ICE_MAKE_SHARED(ConnectionInfo);
info->verified = _verified;
info->adapterName = _adapterName;
info->incoming = _incoming;
info->underlying = _delegate->getInfo();
info->certs = _certs;
return info;
}
void
UWP::TransceiverI::checkSendSize(const IceInternal::Buffer&)
{
}
void
UWP::TransceiverI::setBufferSize(int rcvSize, int sndSize)
{
_delegate->setBufferSize(rcvSize, sndSize);
}
UWP::TransceiverI::TransceiverI(const InstancePtr& instance,
const IceInternal::TransceiverPtr& delegate,
const string& hostOrAdapterName,
bool incoming) :
_instance(instance),
_engine(UWP::SSLEnginePtr::dynamicCast(instance->engine())),
_host(incoming ? "" : hostOrAdapterName),
_adapterName(incoming ? hostOrAdapterName : ""),
_incoming(incoming),
_delegate(delegate),
_connected(false),
_upgraded(false),
_verified(false)
{
}
UWP::TransceiverI::~TransceiverI()
{
}
|