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>
40 #include <Security/SecBase.h>
41 #include <Security/SecureTransportPriv.h>
43 #include "SecProtocolInternal.h"
45 #include <TargetConditionals.h>
48 /* SecureTransport needs it's own copy of KnownCipherSuites for now, there is a copy in coreTLS,
49 that is exported, but it actually should only included the "default" not the supported */
52 #define ENABLE_AES_GCM 1
54 #define ENABLE_CHACHA20_POLY1305 1
55 #define ENABLE_AES_CCM 0
58 static const uint16_t STKnownCipherSuites
[] = {
60 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
,
61 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
,
63 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
,
64 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
,
65 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
,
66 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
,
67 TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
,
69 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
,
70 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
,
72 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
,
73 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
,
74 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
,
75 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
,
76 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
,
79 TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
,
80 TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
,
82 TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
,
83 TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
,
84 TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
,
85 TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
,
86 TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
,
88 TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
,
89 TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
,
91 TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
,
92 TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
,
93 TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
,
94 TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
,
95 TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
,
99 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
,
100 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
,
101 #endif // ENABLE_AES_GCM
102 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
,
103 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
,
104 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
105 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
106 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
109 TLS_RSA_WITH_AES_256_GCM_SHA384
,
110 TLS_RSA_WITH_AES_128_GCM_SHA256
,
112 TLS_RSA_WITH_AES_256_CBC_SHA256
,
113 TLS_RSA_WITH_AES_128_CBC_SHA256
,
114 TLS_RSA_WITH_AES_256_CBC_SHA
,
115 TLS_RSA_WITH_AES_128_CBC_SHA
,
116 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
118 /* TLS 1.3 ciphersuites */
120 TLS_AES_128_GCM_SHA256
,
121 TLS_AES_256_GCM_SHA384
,
123 #if ENABLE_CHACHA20_POLY1305
124 TLS_CHACHA20_POLY1305_SHA256
,
127 TLS_AES_128_CCM_SHA256
,
128 TLS_AES_128_CCM_8_SHA256
,
131 /* Unsafe ciphersuites */
134 TLS_DH_anon_WITH_AES_256_GCM_SHA384
,
135 TLS_DH_anon_WITH_AES_128_GCM_SHA256
,
137 TLS_DH_anon_WITH_AES_128_CBC_SHA256
,
138 TLS_DH_anon_WITH_AES_256_CBC_SHA256
,
139 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
140 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
141 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
143 TLS_ECDHE_ECDSA_WITH_NULL_SHA
,
144 TLS_ECDHE_RSA_WITH_NULL_SHA
,
146 TLS_ECDH_ECDSA_WITH_NULL_SHA
,
147 TLS_ECDH_RSA_WITH_NULL_SHA
,
151 TLS_PSK_WITH_AES_256_CBC_SHA384
,
152 TLS_PSK_WITH_AES_128_CBC_SHA256
,
153 TLS_PSK_WITH_AES_256_CBC_SHA
,
154 TLS_PSK_WITH_AES_128_CBC_SHA
,
155 TLS_PSK_WITH_3DES_EDE_CBC_SHA
,
156 TLS_PSK_WITH_NULL_SHA384
,
157 TLS_PSK_WITH_NULL_SHA256
,
158 TLS_PSK_WITH_NULL_SHA
,
161 TLS_RSA_WITH_NULL_SHA256
,
162 SSL_RSA_WITH_NULL_SHA
,
163 SSL_RSA_WITH_NULL_MD5
166 static const unsigned STCipherSuiteCount
= sizeof(STKnownCipherSuites
)/sizeof(STKnownCipherSuites
[0]);
168 static tls_ciphersuite_group_t
169 _SSLCiphersuteGroupToTLSCiphersuiteGroup(SSLCiphersuiteGroup group
)
172 case kSSLCiphersuiteGroupDefault
:
173 return tls_ciphersuite_group_default
;
174 case kSSLCiphersuiteGroupCompatibility
:
175 return tls_ciphersuite_group_compatibility
;
176 case kSSLCiphersuiteGroupLegacy
:
177 return tls_ciphersuite_group_legacy
;
178 case kSSLCiphersuiteGroupATS
:
179 return tls_ciphersuite_group_ats
;
180 case kSSLCiphersuiteGroupATSCompatibility
:
181 return tls_ciphersuite_group_ats_compatibility
;
183 return tls_ciphersuite_group_default
;
186 const SSLCipherSuite
*
187 SSLCiphersuiteGroupToCiphersuiteList(SSLCiphersuiteGroup group
, size_t *listSize
)
189 tls_ciphersuite_group_t tls_group
= _SSLCiphersuteGroupToTLSCiphersuiteGroup(group
);
190 const tls_ciphersuite_t
*list
= sec_protocol_helper_ciphersuite_group_to_ciphersuite_list(tls_group
, listSize
);
191 return (const SSLCipherSuite
*)list
;
195 SSLCiphersuiteGroupContainsCiphersuite(SSLCiphersuiteGroup group
, SSLCipherSuite suite
)
197 tls_ciphersuite_group_t tls_group
= _SSLCiphersuteGroupToTLSCiphersuiteGroup(group
);
198 return sec_protocol_helper_ciphersuite_group_contains_ciphersuite(tls_group
, (tls_ciphersuite_t
)suite
);
201 static struct ssl_protocol_version_map_entry
{
202 SSLProtocol protocol
;
204 } ssl_protocol_version_map
[] = {
205 { .protocol
= kTLSProtocol13
, .codepoint
= tls_protocol_version_TLSv13
},
206 { .protocol
= kTLSProtocol12
, .codepoint
= tls_protocol_version_TLSv12
},
207 { .protocol
= kTLSProtocol11
, .codepoint
= tls_protocol_version_TLSv11
},
208 { .protocol
= kTLSProtocol1
, .codepoint
= tls_protocol_version_TLSv10
},
209 { .protocol
= kDTLSProtocol12
, .codepoint
= tls_protocol_version_DTLSv12
},
210 { .protocol
= kDTLSProtocol1
, .codepoint
= tls_protocol_version_DTLSv10
},
211 { .protocol
= kSSLProtocol3
, .codepoint
= 0x0300 },
212 { .protocol
= kSSLProtocol2
, .codepoint
= 0x0000 },
214 static size_t ssl_protocol_version_map_len
= sizeof(ssl_protocol_version_map
) / sizeof(ssl_protocol_version_map
[0]);
217 SSLProtocolGetVersionCodepoint(SSLProtocol protocol_version
)
219 for (size_t i
= 0; i
< ssl_protocol_version_map_len
; i
++) {
220 if (ssl_protocol_version_map
[i
].protocol
== protocol_version
) {
221 return ssl_protocol_version_map
[i
].codepoint
;
228 SSLProtocolFromVersionCodepoint(uint16_t protocol_version
)
230 for (size_t i
= 0; i
< ssl_protocol_version_map_len
; i
++) {
231 if (ssl_protocol_version_map
[i
].codepoint
== protocol_version
) {
232 return ssl_protocol_version_map
[i
].protocol
;
235 return kSSLProtocolUnknown
;
239 SSLCiphersuiteMinimumTLSVersion(SSLCipherSuite ciphersuite
)
241 tls_protocol_version_t version
= sec_protocol_helper_ciphersuite_minimum_TLS_version((tls_ciphersuite_t
)ciphersuite
);
242 return SSLProtocolFromVersionCodepoint((uint16_t)version
);
246 SSLCiphersuiteMaximumTLSVersion(SSLCipherSuite ciphersuite
)
248 tls_protocol_version_t version
= sec_protocol_helper_ciphersuite_maximum_TLS_version((tls_ciphersuite_t
)ciphersuite
);
249 return SSLProtocolFromVersionCodepoint((uint16_t)version
);
253 SSLCiphersuiteGetName(SSLCipherSuite ciphersuite
)
255 return sec_protocol_helper_get_ciphersuite_name((tls_ciphersuite_t
)ciphersuite
);
259 * Convert an array of uint16_t
260 * to an array of SSLCipherSuites.
263 cipherSuitesToCipherSuites(
264 size_t numCipherSuites
,
265 const uint16_t *cipherSuites
,
266 SSLCipherSuite
*ciphers
, /* RETURNED */
267 size_t *numCiphers
) /* IN/OUT */
270 if(*numCiphers
< numCipherSuites
) {
271 return errSSLBufferOverflow
;
274 /* NOTE: this is required to go from uint16_t to SSLCipherSuite
275 which is either 32 or 16 bits, depending on the platform */
276 for(i
=0;i
<numCipherSuites
; i
++) {
277 ciphers
[i
]=cipherSuites
[i
];
279 *numCiphers
= numCipherSuites
;
280 return errSecSuccess
;
284 *** Publically exported functions declared in SecureTransport.h
288 * Determine number and values of all of the SSLCipherSuites we support.
289 * Caller allocates output buffer for SSLGetSupportedCiphers() and passes in
290 * its size in *numCiphers. If supplied buffer is too small, errSSLBufferOverflow
294 SSLGetNumberSupportedCiphers (SSLContextRef ctx
,
297 if((ctx
== NULL
) || (numCiphers
== NULL
)) {
300 *numCiphers
= STCipherSuiteCount
;
301 return errSecSuccess
;
305 SSLGetSupportedCiphers (SSLContextRef ctx
,
306 SSLCipherSuite
*ciphers
, /* RETURNED */
307 size_t *numCiphers
) /* IN/OUT */
309 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== NULL
)) {
312 return cipherSuitesToCipherSuites(STCipherSuiteCount
,
319 * Specify a (typically) restricted set of SSLCipherSuites to be enabled by
320 * the current SSLContext. Can only be called when no session is active. Default
321 * set of enabled SSLCipherSuites is NOT the same as the complete set of supported
322 * SSLCipherSuites as obtained by SSLGetSupportedCiphers().
325 SSLSetEnabledCiphers (SSLContextRef ctx
,
326 const SSLCipherSuite
*ciphers
,
331 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== 0)) {
335 if(sslIsSessionActive(ctx
)) {
336 /* can't do this with an active session */
341 for(int i
=0; i
<numCiphers
; i
++) {
342 for (int j
=0; j
< STCipherSuiteCount
; j
++) {
343 if (STKnownCipherSuites
[j
] == ciphers
[i
]) {
350 // If the caller specified no ciphersuites that we actually support, return an error code.
351 if (matchCount
== 0) {
355 cs
= (uint16_t *)sslMalloc(matchCount
* sizeof(uint16_t));
357 return errSecAllocate
;
361 for(int i
=0; i
<numCiphers
; i
++) {
362 for (int j
=0; j
< STCipherSuiteCount
; j
++) {
363 if (STKnownCipherSuites
[j
] == ciphers
[i
]) {
364 cs
[matchCount
++] = ciphers
[i
];
371 OSStatus result
= tls_handshake_set_ciphersuites(ctx
->hdsk
, cs
, (unsigned) matchCount
);
379 * Determine number and values of all of the SSLCipherSuites currently enabled.
380 * Caller allocates output buffer for SSLGetEnabledCiphers() and passes in
381 * its size in *numCiphers. If supplied buffer is too small, errSSLBufferOverflow
385 SSLGetNumberEnabledCiphers (SSLContextRef ctx
,
388 if((ctx
== NULL
) || (numCiphers
== NULL
)) {
393 const uint16_t *ciphersuites
;
396 err
= tls_handshake_get_ciphersuites(ctx
->hdsk
, &ciphersuites
, &n
);
402 return errSecSuccess
;
407 SSLGetEnabledCiphers (SSLContextRef ctx
,
408 SSLCipherSuite
*ciphers
, /* RETURNED */
409 size_t *numCiphers
) /* IN/OUT */
411 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== NULL
)) {
416 const uint16_t *ciphersuites
;
419 err
= tls_handshake_get_ciphersuites(ctx
->hdsk
, &ciphersuites
, &n
);
424 return cipherSuitesToCipherSuites(n
,
432 SSLSetSessionTicketsEnabled (SSLContextRef context
,
435 if (context
== NULL
) {
439 return tls_handshake_set_session_ticket_enabled(context
->hdsk
, enabled
);