2 * Copyright (c) 1999-2001,2005-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * cipherSpecs.c - SSLCipherSpec declarations
28 #include "sslBuildFlags.h"
29 #include "sslContext.h"
30 #include "sslCipherSpecs.h"
32 #include "sslMemory.h"
36 #include <tls_handshake.h>
39 #include <Security/SecBase.h>
40 #include <Security/SecureTransportPriv.h>
42 #include "SecProtocolInternal.h"
44 #include <TargetConditionals.h>
47 /* SecureTransport needs it's own copy of KnownCipherSuites for now, there is a copy in coreTLS,
48 that is exported, but it actually should only included the "default" not the supported */
51 #define ENABLE_AES_GCM 1
53 #define ENABLE_CHACHA20_POLY1305 1
54 #define ENABLE_AES_CCM 0
57 static const uint16_t STKnownCipherSuites
[] = {
59 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
,
60 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
,
62 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
,
63 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
,
64 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
,
65 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
,
66 TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
,
68 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
,
69 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
,
71 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
,
72 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
,
73 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
,
74 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
,
75 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
,
78 TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
,
79 TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
,
81 TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
,
82 TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
,
83 TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
,
84 TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
,
85 TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
,
87 TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
,
88 TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
,
90 TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
,
91 TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
,
92 TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
,
93 TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
,
94 TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
,
98 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
,
99 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
,
100 #endif // ENABLE_AES_GCM
101 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
,
102 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
,
103 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
104 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
105 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
108 TLS_RSA_WITH_AES_256_GCM_SHA384
,
109 TLS_RSA_WITH_AES_128_GCM_SHA256
,
111 TLS_RSA_WITH_AES_256_CBC_SHA256
,
112 TLS_RSA_WITH_AES_128_CBC_SHA256
,
113 TLS_RSA_WITH_AES_256_CBC_SHA
,
114 TLS_RSA_WITH_AES_128_CBC_SHA
,
115 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
117 /* TLS 1.3 ciphersuites */
119 TLS_AES_128_GCM_SHA256
,
120 TLS_AES_256_GCM_SHA384
,
122 #if ENABLE_CHACHA20_POLY1305
123 TLS_CHACHA20_POLY1305_SHA256
,
126 TLS_AES_128_CCM_SHA256
,
127 TLS_AES_128_CCM_8_SHA256
,
130 /* Unsafe ciphersuites */
133 TLS_DH_anon_WITH_AES_256_GCM_SHA384
,
134 TLS_DH_anon_WITH_AES_128_GCM_SHA256
,
136 TLS_DH_anon_WITH_AES_128_CBC_SHA256
,
137 TLS_DH_anon_WITH_AES_256_CBC_SHA256
,
138 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
139 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
140 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
142 TLS_ECDHE_ECDSA_WITH_NULL_SHA
,
143 TLS_ECDHE_RSA_WITH_NULL_SHA
,
145 TLS_ECDH_ECDSA_WITH_NULL_SHA
,
146 TLS_ECDH_RSA_WITH_NULL_SHA
,
150 TLS_PSK_WITH_AES_256_CBC_SHA384
,
151 TLS_PSK_WITH_AES_128_CBC_SHA256
,
152 TLS_PSK_WITH_AES_256_CBC_SHA
,
153 TLS_PSK_WITH_AES_128_CBC_SHA
,
154 TLS_PSK_WITH_3DES_EDE_CBC_SHA
,
155 TLS_PSK_WITH_NULL_SHA384
,
156 TLS_PSK_WITH_NULL_SHA256
,
157 TLS_PSK_WITH_NULL_SHA
,
160 TLS_RSA_WITH_NULL_SHA256
,
161 SSL_RSA_WITH_NULL_SHA
,
162 SSL_RSA_WITH_NULL_MD5
165 static const unsigned STCipherSuiteCount
= sizeof(STKnownCipherSuites
)/sizeof(STKnownCipherSuites
[0]);
167 static tls_ciphersuite_group_t
168 _SSLCiphersuteGroupToTLSCiphersuiteGroup(SSLCiphersuiteGroup group
)
171 case kSSLCiphersuiteGroupDefault
:
172 return tls_ciphersuite_group_default
;
173 case kSSLCiphersuiteGroupCompatibility
:
174 return tls_ciphersuite_group_compatibility
;
175 case kSSLCiphersuiteGroupLegacy
:
176 return tls_ciphersuite_group_legacy
;
177 case kSSLCiphersuiteGroupATS
:
178 return tls_ciphersuite_group_ats
;
179 case kSSLCiphersuiteGroupATSCompatibility
:
180 return tls_ciphersuite_group_ats_compatibility
;
182 return tls_ciphersuite_group_default
;
185 const SSLCipherSuite
*
186 SSLCiphersuiteGroupToCiphersuiteList(SSLCiphersuiteGroup group
, size_t *listSize
)
188 tls_ciphersuite_group_t tls_group
= _SSLCiphersuteGroupToTLSCiphersuiteGroup(group
);
189 const tls_ciphersuite_t
*list
= sec_protocol_helper_ciphersuite_group_to_ciphersuite_list(tls_group
, listSize
);
190 return (const SSLCipherSuite
*)list
;
194 SSLCiphersuiteGroupContainsCiphersuite(SSLCiphersuiteGroup group
, SSLCipherSuite suite
)
196 tls_ciphersuite_group_t tls_group
= _SSLCiphersuteGroupToTLSCiphersuiteGroup(group
);
197 return sec_protocol_helper_ciphersuite_group_contains_ciphersuite(tls_group
, (tls_ciphersuite_t
)suite
);
200 static struct ssl_protocol_version_map_entry
{
201 SSLProtocol protocol
;
203 } ssl_protocol_version_map
[] = {
204 { .protocol
= kTLSProtocol13
, .codepoint
= tls_protocol_version_TLSv13
},
205 { .protocol
= kTLSProtocol12
, .codepoint
= tls_protocol_version_TLSv12
},
206 { .protocol
= kTLSProtocol11
, .codepoint
= tls_protocol_version_TLSv11
},
207 { .protocol
= kTLSProtocol1
, .codepoint
= tls_protocol_version_TLSv10
},
208 { .protocol
= kDTLSProtocol12
, .codepoint
= tls_protocol_version_DTLSv12
},
209 { .protocol
= kDTLSProtocol1
, .codepoint
= tls_protocol_version_DTLSv10
},
210 { .protocol
= kSSLProtocol3
, .codepoint
= 0x0300 },
211 { .protocol
= kSSLProtocol2
, .codepoint
= 0x0000 },
213 static size_t ssl_protocol_version_map_len
= sizeof(ssl_protocol_version_map
) / sizeof(ssl_protocol_version_map
[0]);
216 SSLProtocolGetVersionCodepoint(SSLProtocol protocol_version
)
218 for (size_t i
= 0; i
< ssl_protocol_version_map_len
; i
++) {
219 if (ssl_protocol_version_map
[i
].protocol
== protocol_version
) {
220 return ssl_protocol_version_map
[i
].codepoint
;
227 SSLProtocolFromVersionCodepoint(uint16_t protocol_version
)
229 for (size_t i
= 0; i
< ssl_protocol_version_map_len
; i
++) {
230 if (ssl_protocol_version_map
[i
].codepoint
== protocol_version
) {
231 return ssl_protocol_version_map
[i
].protocol
;
234 return kSSLProtocolUnknown
;
238 SSLCiphersuiteMinimumTLSVersion(SSLCipherSuite ciphersuite
)
240 tls_protocol_version_t version
= sec_protocol_helper_ciphersuite_minimum_TLS_version((tls_ciphersuite_t
)ciphersuite
);
241 return SSLProtocolFromVersionCodepoint((uint16_t)version
);
245 SSLCiphersuiteMaximumTLSVersion(SSLCipherSuite ciphersuite
)
247 tls_protocol_version_t version
= sec_protocol_helper_ciphersuite_maximum_TLS_version((tls_ciphersuite_t
)ciphersuite
);
248 return SSLProtocolFromVersionCodepoint((uint16_t)version
);
252 SSLCiphersuiteGetName(SSLCipherSuite ciphersuite
)
254 return sec_protocol_helper_get_ciphersuite_name((tls_ciphersuite_t
)ciphersuite
);
258 * Convert an array of uint16_t
259 * to an array of SSLCipherSuites.
262 cipherSuitesToCipherSuites(
263 size_t numCipherSuites
,
264 const uint16_t *cipherSuites
,
265 SSLCipherSuite
*ciphers
, /* RETURNED */
266 size_t *numCiphers
) /* IN/OUT */
269 if(*numCiphers
< numCipherSuites
) {
270 return errSSLBufferOverflow
;
273 /* NOTE: this is required to go from uint16_t to SSLCipherSuite
274 which is either 32 or 16 bits, depending on the platform */
275 for(i
=0;i
<numCipherSuites
; i
++) {
276 ciphers
[i
]=cipherSuites
[i
];
278 *numCiphers
= numCipherSuites
;
279 return errSecSuccess
;
283 *** Publically exported functions declared in SecureTransport.h
287 * Determine number and values of all of the SSLCipherSuites we support.
288 * Caller allocates output buffer for SSLGetSupportedCiphers() and passes in
289 * its size in *numCiphers. If supplied buffer is too small, errSSLBufferOverflow
293 SSLGetNumberSupportedCiphers (SSLContextRef ctx
,
296 if((ctx
== NULL
) || (numCiphers
== NULL
)) {
299 *numCiphers
= STCipherSuiteCount
;
300 return errSecSuccess
;
304 SSLGetSupportedCiphers (SSLContextRef ctx
,
305 SSLCipherSuite
*ciphers
, /* RETURNED */
306 size_t *numCiphers
) /* IN/OUT */
308 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== NULL
)) {
311 return cipherSuitesToCipherSuites(STCipherSuiteCount
,
318 * Specify a (typically) restricted set of SSLCipherSuites to be enabled by
319 * the current SSLContext. Can only be called when no session is active. Default
320 * set of enabled SSLCipherSuites is NOT the same as the complete set of supported
321 * SSLCipherSuites as obtained by SSLGetSupportedCiphers().
324 SSLSetEnabledCiphers (SSLContextRef ctx
,
325 const SSLCipherSuite
*ciphers
,
330 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== 0)) {
334 if(sslIsSessionActive(ctx
)) {
335 /* can't do this with an active session */
340 for(int i
=0; i
<numCiphers
; i
++) {
341 for (int j
=0; j
< STCipherSuiteCount
; j
++) {
342 if (STKnownCipherSuites
[j
] == ciphers
[i
]) {
349 // If the caller specified no ciphersuites that we actually support, return an error code.
350 if (matchCount
== 0) {
354 cs
= (uint16_t *)sslMalloc(matchCount
* sizeof(uint16_t));
356 return errSecAllocate
;
360 for(int i
=0; i
<numCiphers
; i
++) {
361 for (int j
=0; j
< STCipherSuiteCount
; j
++) {
362 if (STKnownCipherSuites
[j
] == ciphers
[i
]) {
363 cs
[matchCount
++] = ciphers
[i
];
370 OSStatus result
= tls_handshake_set_ciphersuites(ctx
->hdsk
, cs
, (unsigned) matchCount
);
378 * Determine number and values of all of the SSLCipherSuites currently enabled.
379 * Caller allocates output buffer for SSLGetEnabledCiphers() and passes in
380 * its size in *numCiphers. If supplied buffer is too small, errSSLBufferOverflow
384 SSLGetNumberEnabledCiphers (SSLContextRef ctx
,
387 if((ctx
== NULL
) || (numCiphers
== NULL
)) {
392 const uint16_t *ciphersuites
;
395 err
= tls_handshake_get_ciphersuites(ctx
->hdsk
, &ciphersuites
, &n
);
401 return errSecSuccess
;
406 SSLGetEnabledCiphers (SSLContextRef ctx
,
407 SSLCipherSuite
*ciphers
, /* RETURNED */
408 size_t *numCiphers
) /* IN/OUT */
410 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== NULL
)) {
415 const uint16_t *ciphersuites
;
418 err
= tls_handshake_get_ciphersuites(ctx
->hdsk
, &ciphersuites
, &n
);
423 return cipherSuitesToCipherSuites(n
,
431 SSLSetSessionTicketsEnabled (SSLContextRef context
,
434 if (context
== NULL
) {
438 return tls_handshake_set_session_ticket_enabled(context
->hdsk
, enabled
);