This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
pal_ssl.c
568 lines (468 loc) · 17.3 KB
/
pal_ssl.c
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include "pal_ssl.h"
#include <dlfcn.h>
// TLS 1.3 is only defined with 10.13 headers, but we build on 10.12
#define kTLSProtocol13_ForwardDef 10
// 10.13.4 introduced public API but linking would fail on all prior versions.
// For that reason we use function pointers instead of direct call.
// This can be revisited after we drop support for 10.12.
static OSStatus (*SSLSetALPNProtocolsPtr)(SSLContextRef context, CFArrayRef protocols) = NULL;
static OSStatus (*SSLCopyALPNProtocolsPtr)(SSLContextRef context, CFArrayRef* protocols) = NULL;
// end of ALPN.
SSLContextRef AppleCryptoNative_SslCreateContext(int32_t isServer)
{
if (isServer != 0 && isServer != 1)
return NULL;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return SSLCreateContext(NULL, isServer ? kSSLServerSide : kSSLClientSide, kSSLStreamType);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslSetAcceptClientCert(SSLContextRef sslContext)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// NULL and other illegal values are handled by the underlying API
return SSLSetClientSideAuthenticate(sslContext, kTryAuthenticate);
#pragma clang diagnostic pop
}
static SSLProtocol PalSslProtocolToSslProtocol(PAL_SslProtocol palProtocolId)
{
switch (palProtocolId)
{
case PAL_SslProtocol_Tls13:
return kTLSProtocol13_ForwardDef;
case PAL_SslProtocol_Tls12:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return kTLSProtocol12;
case PAL_SslProtocol_Tls11:
return kTLSProtocol11;
case PAL_SslProtocol_Tls10:
return kTLSProtocol1;
case PAL_SslProtocol_Ssl3:
return kSSLProtocol3;
case PAL_SslProtocol_Ssl2:
return kSSLProtocol2;
case PAL_SslProtocol_None:
default:
return kSSLProtocolUnknown;
#pragma clang diagnostic pop
}
}
int32_t AppleCryptoNative_SslSetMinProtocolVersion(SSLContextRef sslContext, PAL_SslProtocol sslProtocol)
{
SSLProtocol protocol = PalSslProtocolToSslProtocol(sslProtocol);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (protocol == kSSLProtocolUnknown)
return errSecParam;
// NULL and other illegal values are handled by the underlying API
return SSLSetProtocolVersionMin(sslContext, protocol);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslSetMaxProtocolVersion(SSLContextRef sslContext, PAL_SslProtocol sslProtocol)
{
SSLProtocol protocol = PalSslProtocolToSslProtocol(sslProtocol);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (protocol == kSSLProtocolUnknown)
return errSecParam;
// NULL and other illegal values are handled by the underlying API
return SSLSetProtocolVersionMax(sslContext, protocol);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslCopyCertChain(SSLContextRef sslContext, SecTrustRef* pChainOut, int32_t* pOSStatus)
{
if (pChainOut != NULL)
*pChainOut = NULL;
if (pOSStatus != NULL)
*pOSStatus = noErr;
if (sslContext == NULL || pChainOut == NULL || pOSStatus == NULL)
return -1;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
*pOSStatus = SSLCopyPeerTrust(sslContext, pChainOut);
#pragma clang diagnostic pop
return *pOSStatus == noErr;
}
int32_t
AppleCryptoNative_SslCopyCADistinguishedNames(SSLContextRef sslContext, CFArrayRef* pArrayOut, int32_t* pOSStatus)
{
if (pArrayOut != NULL)
*pArrayOut = NULL;
if (pOSStatus != NULL)
*pOSStatus = noErr;
if (sslContext == NULL || pArrayOut == NULL || pOSStatus == NULL)
return -1;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
*pOSStatus = SSLCopyDistinguishedNames(sslContext, pArrayOut);
#pragma clang diagnostic pop
return *pOSStatus == noErr;
}
static int32_t AppleCryptoNative_SslSetSessionOption(SSLContextRef sslContext,
SSLSessionOption option,
int32_t value,
int32_t* pOSStatus)
{
if (sslContext == NULL)
return -1;
if (value != 0 && value != 1)
return -2;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
*pOSStatus = SSLSetSessionOption(sslContext, option, !!value);
#pragma clang diagnostic pop
return *pOSStatus == noErr;
}
int32_t AppleCryptoNative_SslSetBreakOnServerAuth(SSLContextRef sslContext, int32_t setBreak, int32_t* pOSStatus)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return AppleCryptoNative_SslSetSessionOption(sslContext, kSSLSessionOptionBreakOnServerAuth, setBreak, pOSStatus);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslSetBreakOnClientAuth(SSLContextRef sslContext, int32_t setBreak, int32_t* pOSStatus)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return AppleCryptoNative_SslSetSessionOption(sslContext, kSSLSessionOptionBreakOnClientAuth, setBreak, pOSStatus);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslSetCertificate(SSLContextRef sslContext, CFArrayRef certRefs)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// The underlying call handles NULL inputs, so just pass it through
return SSLSetCertificate(sslContext, certRefs);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslSetTargetName(SSLContextRef sslContext,
const char* pszTargetName,
int32_t cbTargetName,
int32_t* pOSStatus)
{
if (pOSStatus != NULL)
*pOSStatus = noErr;
if (sslContext == NULL || pszTargetName == NULL || pOSStatus == NULL)
return -1;
if (cbTargetName < 0)
return -2;
size_t currentLength;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
*pOSStatus = SSLGetPeerDomainNameLength(sslContext, ¤tLength);
// We'll end up walking down the path that sets the hostname more than once during
// the handshake dance. But once the handshake starts Secure Transport isn't willing to
// listen to this. So, if we've already set it, don't set it again.
if (*pOSStatus == noErr && currentLength == 0)
{
*pOSStatus = SSLSetPeerDomainName(sslContext, pszTargetName, (size_t)cbTargetName);
}
#pragma clang diagnostic pop
return *pOSStatus == noErr;
}
int32_t AppleCryptoNative_SSLSetALPNProtocols(SSLContextRef sslContext,
CFArrayRef protocols,
int32_t* pOSStatus)
{
if (sslContext == NULL || protocols == NULL || pOSStatus == NULL)
return -1;
if (!SSLSetALPNProtocolsPtr)
{
// not available.
*pOSStatus = 0;
return 1;
}
// The underlying call handles NULL inputs, so just pass it through
*pOSStatus = (*SSLSetALPNProtocolsPtr)(sslContext, protocols);
return *pOSStatus == noErr;
}
int32_t AppleCryptoNative_SslGetAlpnSelected(SSLContextRef sslContext, CFDataRef* protocol)
{
if (sslContext == NULL || protocol == NULL)
return -1;
*protocol = NULL;
if (!SSLCopyALPNProtocolsPtr)
{
// not available.
return 0;
}
CFArrayRef protocols = NULL;
OSStatus osStatus = (*SSLCopyALPNProtocolsPtr)(sslContext, &protocols);
if (osStatus == noErr && protocols != NULL && CFArrayGetCount(protocols) > 0)
{
*protocol =
CFStringCreateExternalRepresentation(NULL, CFArrayGetValueAtIndex(protocols, 0), kCFStringEncodingASCII, 0);
}
if (protocols)
CFRelease(protocols);
return *protocol != NULL;
}
int32_t AppleCryptoNative_SslSetIoCallbacks(SSLContextRef sslContext, SSLReadFunc readFunc, SSLWriteFunc writeFunc)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return SSLSetIOFuncs(sslContext, readFunc, writeFunc);
#pragma clang diagnostic pop
}
PAL_TlsHandshakeState AppleCryptoNative_SslHandshake(SSLContextRef sslContext)
{
if (sslContext == NULL)
return PAL_TlsHandshakeState_Unknown;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus osStatus = SSLHandshake(sslContext);
#pragma clang diagnostic pop
switch (osStatus)
{
case noErr:
return PAL_TlsHandshakeState_Complete;
case errSSLWouldBlock:
return PAL_TlsHandshakeState_WouldBlock;
case errSSLServerAuthCompleted:
return PAL_TlsHandshakeState_ServerAuthCompleted;
default:
return osStatus;
}
}
static PAL_TlsIo OSStatusToPAL_TlsIo(OSStatus status)
{
switch (status)
{
case noErr:
return PAL_TlsIo_Success;
case errSSLWouldBlock:
return PAL_TlsIo_WouldBlock;
case errSSLClosedGraceful:
return PAL_TlsIo_ClosedGracefully;
default:
return status;
}
}
PAL_TlsIo
AppleCryptoNative_SslWrite(SSLContextRef sslContext, const uint8_t* buf, uint32_t bufLen, uint32_t* bytesWritten)
{
if (bytesWritten == NULL)
return PAL_TlsIo_Unknown;
size_t expected = (size_t)bufLen;
size_t totalWritten;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus status = SSLWrite(sslContext, buf, expected, &totalWritten);
#pragma clang diagnostic pop
if (status != noErr)
{
*bytesWritten = (uint32_t)totalWritten;
return OSStatusToPAL_TlsIo(status);
}
return PAL_TlsIo_Success;
}
PAL_TlsIo AppleCryptoNative_SslRead(SSLContextRef sslContext, uint8_t* buf, uint32_t bufLen, uint32_t* written)
{
if (written == NULL)
return PAL_TlsIo_Unknown;
size_t writtenSize = 0;
size_t bufSize = (size_t)bufLen;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus status = SSLRead(sslContext, buf, bufSize, &writtenSize);
#pragma clang diagnostic pop
if (writtenSize > UINT_MAX)
{
// This shouldn't happen, because we passed a uint32_t as the initial buffer size.
// But, just in case it does, report back that we're no longer in a known state.
return PAL_TlsIo_Unknown;
}
*written = (uint32_t)writtenSize;
if (writtenSize == 0 && status == errSSLWouldBlock)
{
SSLSessionState state;
memset(&state, 0, sizeof(SSLSessionState));
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus localStatus = SSLGetSessionState(sslContext, &state);
if (localStatus == noErr && state == kSSLHandshake)
{
return PAL_TlsIo_Renegotiate;
}
#pragma clang diagnostic pop
}
return OSStatusToPAL_TlsIo(status);
}
int32_t AppleCryptoNative_SslIsHostnameMatch(SSLContextRef sslContext, CFStringRef cfHostname, CFDateRef notBefore)
{
if (sslContext == NULL || notBefore == NULL)
return -1;
if (cfHostname == NULL)
return -2;
SecPolicyRef sslPolicy = SecPolicyCreateSSL(true, cfHostname);
if (sslPolicy == NULL)
return -3;
CFMutableArrayRef certs = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
if (certs == NULL)
return -4;
SecTrustRef existingTrust = NULL;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus osStatus = SSLCopyPeerTrust(sslContext, &existingTrust);
#pragma clang diagnostic pop
if (osStatus != noErr)
{
CFRelease(certs);
return -5;
}
CFMutableArrayRef anchors = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
if (anchors == NULL)
{
CFRelease(certs);
return -6;
}
CFIndex count = SecTrustGetCertificateCount(existingTrust);
for (CFIndex i = 0; i < count; i++)
{
SecCertificateRef item = SecTrustGetCertificateAtIndex(existingTrust, i);
CFArrayAppendValue(certs, item);
// Copy the EE cert into the anchors set, this will make the chain part
// always return true.
if (i == 0)
{
CFArrayAppendValue(anchors, item);
}
}
SecTrustRef trust = NULL;
osStatus = SecTrustCreateWithCertificates(certs, sslPolicy, &trust);
int32_t ret = INT_MIN;
if (osStatus == noErr)
{
osStatus = SecTrustSetAnchorCertificates(trust, anchors);
}
if (osStatus == noErr)
{
osStatus = SecTrustSetVerifyDate(trust, notBefore);
}
if (osStatus == noErr)
{
SecTrustResultType trustResult;
memset(&trustResult, 0, sizeof(SecTrustResultType));
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
osStatus = SecTrustEvaluate(trust, &trustResult);
#pragma clang diagnostic pop
if (osStatus != noErr)
{
ret = -7;
}
else if (trustResult == kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed)
{
ret = 1;
}
else if (trustResult == kSecTrustResultDeny || trustResult == kSecTrustResultRecoverableTrustFailure)
{
ret = 0;
}
else
{
ret = -8;
}
}
if (trust != NULL)
CFRelease(trust);
if (certs != NULL)
CFRelease(certs);
if (anchors != NULL)
CFRelease(anchors);
CFRelease(sslPolicy);
return ret;
}
int32_t AppleCryptoNative_SslShutdown(SSLContextRef sslContext)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return SSLClose(sslContext);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslGetProtocolVersion(SSLContextRef sslContext, PAL_SslProtocol* pProtocol)
{
if (pProtocol != NULL)
*pProtocol = 0;
if (sslContext == NULL || pProtocol == NULL)
return errSecParam;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
SSLProtocol protocol = kSSLProtocolUnknown;
OSStatus osStatus = SSLGetNegotiatedProtocolVersion(sslContext, &protocol);
if (osStatus == noErr)
{
PAL_SslProtocol matchedProtocol = PAL_SslProtocol_None;
if (protocol == kTLSProtocol13_ForwardDef)
matchedProtocol = PAL_SslProtocol_Tls13;
else if (protocol == kTLSProtocol12)
matchedProtocol = PAL_SslProtocol_Tls12;
else if (protocol == kTLSProtocol11)
matchedProtocol = PAL_SslProtocol_Tls11;
else if (protocol == kTLSProtocol1)
matchedProtocol = PAL_SslProtocol_Tls10;
else if (protocol == kSSLProtocol3)
matchedProtocol = PAL_SslProtocol_Ssl3;
else if (protocol == kSSLProtocol2)
matchedProtocol = PAL_SslProtocol_Ssl2;
*pProtocol = matchedProtocol;
}
#pragma clang diagnostic pop
return osStatus;
}
int32_t AppleCryptoNative_SslGetCipherSuite(SSLContextRef sslContext, uint16_t* pCipherSuiteOut)
{
if (pCipherSuiteOut == NULL)
{
return errSecParam;
}
SSLCipherSuite cipherSuite;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus status = SSLGetNegotiatedCipher(sslContext, &cipherSuite);
#pragma clang diagnostic pop
*pCipherSuiteOut = (uint16_t)cipherSuite;
return status;
}
int32_t AppleCryptoNative_SslSetEnabledCipherSuites(SSLContextRef sslContext, const uint32_t* cipherSuites, int32_t numCipherSuites)
{
// Max numCipherSuites is 2^16 (all possible cipher suites)
assert(numCipherSuites < (1 << 16));
if (sizeof(SSLCipherSuite) == sizeof(uint32_t))
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// macOS
return SSLSetEnabledCiphers(sslContext, cipherSuites, (size_t)numCipherSuites);
#pragma clang diagnostic pop
}
else
{
// iOS, tvOS, watchOS
SSLCipherSuite* cipherSuites16 = (SSLCipherSuite*)calloc((size_t)numCipherSuites, sizeof(SSLCipherSuite));
if (cipherSuites16 == NULL)
{
return errSSLInternal;
}
for (int i = 0; i < numCipherSuites; i++)
{
cipherSuites16[i] = (SSLCipherSuite)cipherSuites[i];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus status = SSLSetEnabledCiphers(sslContext, cipherSuites16, (size_t)numCipherSuites);
#pragma clang diagnostic pop
free(cipherSuites16);
return status;
}
}
__attribute__((constructor)) static void InitializeAppleCryptoSslShim()
{
SSLSetALPNProtocolsPtr = (OSStatus(*)(SSLContextRef, CFArrayRef))dlsym(RTLD_DEFAULT, "SSLSetALPNProtocols");
SSLCopyALPNProtocolsPtr = (OSStatus(*)(SSLContextRef, CFArrayRef*))dlsym(RTLD_DEFAULT, "SSLCopyALPNProtocols");
}