-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
pal_ssl.c
713 lines (591 loc) · 22.2 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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "pal_ssl.h"
#include <dlfcn.h>
#include "coretls_structs.h"
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_SslSetConnection(SSLContextRef sslContext, SSLConnectionRef sslConnection)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return SSLSetConnection(sslContext, sslConnection);
#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)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
case PAL_SslProtocol_Tls13:
return kTLSProtocol13;
case PAL_SslProtocol_Tls12:
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 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 SslSetSessionOption(sslContext, kSSLSessionOptionBreakOnServerAuth, setBreak, pOSStatus);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslSetBreakOnCertRequested(SSLContextRef sslContext, int32_t setBreak, int32_t* pOSStatus)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return SslSetSessionOption(sslContext, kSSLSessionOptionBreakOnCertRequested, 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 SslSetSessionOption(sslContext, kSSLSessionOptionBreakOnClientAuth, setBreak, pOSStatus);
#pragma clang diagnostic pop
}
int32_t AppleCryptoNative_SslSetBreakOnClientHello(SSLContextRef sslContext, int32_t setBreak, int32_t* pOSStatus)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return SslSetSessionOption(sslContext, kSSLSessionOptionBreakOnClientHello, 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;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// The underlying call handles NULL inputs, so just pass it through
*pOSStatus = SSLSetALPNProtocols(sslContext, protocols);
#pragma clang diagnostic pop
return *pOSStatus == noErr;
}
int32_t AppleCryptoNative_SSLSetALPNProtocol(SSLContextRef sslContext, void* protocol, int length, int32_t* pOSStatus)
{
if (sslContext == NULL || protocol == NULL || length <= 0 || pOSStatus == NULL)
return -1;
CFStringRef value = CFStringCreateWithBytes(NULL, protocol, length, kCFStringEncodingASCII, 0);
if (!value)
{
*pOSStatus = errSecMemoryError;
return -2;
}
CFArrayRef protocolList = CFArrayCreate(kCFAllocatorDefault, (const void **)&value, 1, &kCFTypeArrayCallBacks);
if (!protocolList)
{
CFRelease(value);
*pOSStatus = errSecMemoryError;
return -2;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
*pOSStatus = SSLSetALPNProtocols(sslContext, protocolList);
#pragma clang diagnostic pop
if (*pOSStatus == 0)
{
struct SSLContext* ctx = (struct SSLContext*)sslContext;
tls_handshake_t tls = ctx->hdsk;
// This is extra consistency check to verify that the ALPN data appeared where we expect them
// before dereferencing sslContext
if (tls != NULL && tls->alpnOwnData.length == (size_t)length + 1)
{
tls->alpn_announced = 1;
tls->alpn_received = 1 ;
}
else
{
*pOSStatus = errSecNotAvailable;
}
}
CFRelease(value);
CFRelease(protocolList);
return 1;
}
int32_t AppleCryptoNative_SslGetAlpnSelected(SSLContextRef sslContext, CFDataRef* protocol)
{
if (sslContext == NULL || protocol == NULL)
return -1;
*protocol = NULL;
CFArrayRef protocols = NULL;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus osStatus = SSLCopyALPNProtocols(sslContext, &protocols);
#pragma clang diagnostic pop
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;
case errSSLClientCertRequested:
return PAL_TlsHandshakeState_ClientCertRequested;
case errSSLClientHelloReceived:
return PAL_TlsHandshakeState_ClientHelloReceived;
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, int32_t* pOSStatus)
{
if (pOSStatus != NULL)
*pOSStatus = noErr;
if (sslContext == NULL || notBefore == NULL || pOSStatus == 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);
*pOSStatus = osStatus;
return -5;
}
CFMutableArrayRef anchors = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
if (anchors == NULL)
{
CFRelease(certs);
CFRelease(existingTrust);
return -6;
}
CFIndex certificateCount = SecTrustGetCertificateCount(existingTrust);
for (CFIndex i = 0; i < certificateCount; i++)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
SecCertificateRef item = SecTrustGetCertificateAtIndex(existingTrust, i);
#pragma clang diagnostic pop
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);
if (trustResult == kSecTrustResultRecoverableTrustFailure && osStatus == noErr && certificateCount > 1)
{
// If we get recoverable failure, let's try it again with full anchor list.
// We already stored just the first certificate into anchors; now we store the rest.
for (CFIndex i = 1; i < certificateCount; i++)
{
CFArrayAppendValue(anchors, SecTrustGetCertificateAtIndex(existingTrust, i));
}
osStatus = SecTrustSetAnchorCertificates(trust, anchors);
if (osStatus == noErr)
{
memset(&trustResult, 0, sizeof(SecTrustResultType));
osStatus = SecTrustEvaluate(trust, &trustResult);
}
}
#pragma clang diagnostic pop
if (osStatus == noErr && trustResult != kSecTrustResultUnspecified && trustResult != kSecTrustResultProceed)
{
// If evaluation succeeded but result is not trusted try to get details.
CFDictionaryRef detailsAndStuff = SecTrustCopyResult(trust);
if (detailsAndStuff != NULL)
{
CFArrayRef details = CFDictionaryGetValue(detailsAndStuff, CFSTR("TrustResultDetails"));
if (details != NULL && CFArrayGetCount(details) > 0)
{
CFArrayRef statusCodes = CFDictionaryGetValue(CFArrayGetValueAtIndex(details,0), CFSTR("StatusCodes"));
if (statusCodes != NULL)
{
OSStatus status = 0;
// look for first failure to keep it simple. Normally, there will be exactly one.
for (int i = 0; i < CFArrayGetCount(statusCodes); i++)
{
CFNumberGetValue(CFArrayGetValueAtIndex(statusCodes, i), kCFNumberSInt32Type, &status);
if (status != noErr)
{
*pOSStatus = status;
break;
}
}
}
}
CFRelease(detailsAndStuff);
}
}
if (osStatus != noErr)
{
ret = -7;
*pOSStatus = osStatus;
}
else if (trustResult == kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed)
{
ret = 1;
}
else if (trustResult == kSecTrustResultDeny || trustResult == kSecTrustResultRecoverableTrustFailure)
{
ret = 0;
}
else
{
ret = -8;
}
}
else
{
*pOSStatus = osStatus;
}
if (trust != NULL)
CFRelease(trust);
if (certs != NULL)
CFRelease(certs);
if (anchors != NULL)
CFRelease(anchors);
if (existingTrust != NULL)
CFRelease(existingTrust);
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)
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 !defined(TARGET_ARM64) && !defined(TARGET_IOS) && !defined(TARGET_TVOS)
if (sizeof(SSLCipherSuite) == sizeof(uint32_t))
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// macOS & MacCatalyst x64
return SSLSetEnabledCiphers(sslContext, (const SSLCipherSuite *)cipherSuites, (size_t)numCipherSuites);
#pragma clang diagnostic pop
}
else
#endif
{
// MacCatalyst arm64, 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;
}
}
PALEXPORT int32_t AppleCryptoNative_SslSetCertificateAuthorities(SSLContextRef sslContext, CFArrayRef certificates, int32_t replaceExisting)
{
#if defined(TARGET_OSX)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// The underlying call handles NULL inputs, so just pass it through
return SSLSetCertificateAuthorities(sslContext, certificates, replaceExisting > 0 ? 1 : 0);
#pragma clang diagnostic pop
#else
// ignore unused parameters
(void)sslContext;
(void)certificates;
(void)replaceExisting;
return 0;
#endif
}