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"
37 #include <tls_handshake.h>
41 #include <Security/SecBase.h>
43 #include <TargetConditionals.h>
46 /* SecureTransport needs it's own copy of KnownCipherSuites for now, there is a copy in coreTLS,
47 that is exported, but it actually should only included the "default" not the supported */
50 #define ENABLE_AES_GCM 0
53 static const uint16_t STKnownCipherSuites
[] = {
55 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
,
56 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
,
58 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
,
59 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
,
60 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
,
61 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
,
62 TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
,
64 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
,
65 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
,
67 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
,
68 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
,
69 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
,
70 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
,
71 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
,
74 TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
,
75 TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
,
77 TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
,
78 TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
,
79 TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
,
80 TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
,
81 TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
,
83 TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
,
84 TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
,
86 TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
,
87 TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
,
88 TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
,
89 TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
,
90 TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
,
94 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
,
95 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
,
96 #endif // ENABLE_AES_GCM
97 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
,
98 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
,
99 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
100 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
101 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
104 TLS_RSA_WITH_AES_256_GCM_SHA384
,
105 TLS_RSA_WITH_AES_128_GCM_SHA256
,
107 TLS_RSA_WITH_AES_256_CBC_SHA256
,
108 TLS_RSA_WITH_AES_128_CBC_SHA256
,
109 TLS_RSA_WITH_AES_256_CBC_SHA
,
110 TLS_RSA_WITH_AES_128_CBC_SHA
,
111 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
114 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
,
115 TLS_ECDHE_RSA_WITH_RC4_128_SHA
,
116 TLS_ECDH_ECDSA_WITH_RC4_128_SHA
,
117 TLS_ECDH_RSA_WITH_RC4_128_SHA
,
118 SSL_RSA_WITH_RC4_128_SHA
,
119 SSL_RSA_WITH_RC4_128_MD5
,
123 /* Unsafe ciphersuites */
126 TLS_DH_anon_WITH_AES_256_GCM_SHA384
,
127 TLS_DH_anon_WITH_AES_128_GCM_SHA256
,
129 TLS_DH_anon_WITH_AES_128_CBC_SHA256
,
130 TLS_DH_anon_WITH_AES_256_CBC_SHA256
,
131 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
132 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
133 SSL_DH_anon_WITH_RC4_128_MD5
,
134 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
136 TLS_ECDHE_ECDSA_WITH_NULL_SHA
,
137 TLS_ECDHE_RSA_WITH_NULL_SHA
,
139 TLS_ECDH_ECDSA_WITH_NULL_SHA
,
140 TLS_ECDH_RSA_WITH_NULL_SHA
,
144 TLS_PSK_WITH_AES_256_CBC_SHA384
,
145 TLS_PSK_WITH_AES_128_CBC_SHA256
,
146 TLS_PSK_WITH_AES_256_CBC_SHA
,
147 TLS_PSK_WITH_AES_128_CBC_SHA
,
148 TLS_PSK_WITH_RC4_128_SHA
,
149 TLS_PSK_WITH_3DES_EDE_CBC_SHA
,
150 TLS_PSK_WITH_NULL_SHA384
,
151 TLS_PSK_WITH_NULL_SHA256
,
152 TLS_PSK_WITH_NULL_SHA
,
155 TLS_RSA_WITH_NULL_SHA256
,
156 SSL_RSA_WITH_NULL_SHA
,
157 SSL_RSA_WITH_NULL_MD5
161 static const unsigned STCipherSuiteCount
= sizeof(STKnownCipherSuites
)/sizeof(STKnownCipherSuites
[0]);
165 * Build ctx->validCipherSpecs as a copy of KnownCipherSpecs, assuming that
166 * validCipherSpecs is currently not valid (i.e., SSLSetEnabledCiphers() has
169 OSStatus
sslBuildCipherSuiteArray(SSLContext
*ctx
)
175 assert(ctx
->validCipherSuites
== NULL
);
177 ctx
->numValidCipherSuites
= STCipherSuiteCount
;
178 size
= STCipherSuiteCount
* sizeof(uint16_t);
179 ctx
->validCipherSuites
= (uint16_t *)sslMalloc(size
);
180 if(ctx
->validCipherSuites
== NULL
) {
181 ctx
->numValidCipherSuites
= 0;
182 return errSecAllocate
;
186 * Trim out inappropriate ciphers:
187 * -- trim anonymous ciphers if !ctx->anonCipherEnable
188 * -- trim ECDSA ciphers for server side if appropriate
189 * -- trim ECDSA ciphers if TLSv1 disable or SSLv2 enabled (since
190 * we MUST do the Client Hello extensions to make these ciphers
192 * -- trim Stream ciphers if DTLSv1 enable
193 * -- trim CBC ciphers when doing SSLv3 fallback
195 uint16_t *dst
= ctx
->validCipherSuites
;
196 const uint16_t *src
= STKnownCipherSuites
;
198 bool trimECDSA
= false;
199 if((ctx
->protocolSide
==kSSLServerSide
) && !SSL_ECDSA_SERVER
) {
202 if(ctx
->minProtocolVersion
== SSL_Version_2_0
203 || ctx
->maxProtocolVersion
== SSL_Version_3_0
) {
204 /* We trim ECDSA cipher suites if SSL2 is enabled or
205 The maximum allowed protocol is SSL3. Note that this
206 won't trim ECDSA cipherspecs for DTLS which should be
207 the right thing to do here. */
211 /* trim Stream Ciphers for DTLS */
212 bool trimRC4
= ctx
->isDTLS
;
214 /* trim CBC cipher when doing SSLv3 only fallback */
215 bool trimCBC
= (ctx
->protocolSide
==kSSLClientSide
)
216 && (ctx
->maxProtocolVersion
== SSL_Version_3_0
)
217 && ctx
->fallbackEnabled
;
219 bool trimDHE
= (ctx
->protocolSide
==kSSLServerSide
) &&
220 !ctx
->dhParamsEncoded
.length
;
222 for(dex
=0; dex
<STCipherSuiteCount
; dex
++) {
223 KeyExchangeMethod kem
= sslCipherSuiteGetKeyExchangeMethod(*src
);
224 uint8_t keySize
= sslCipherSuiteGetSymmetricCipherKeySize(*src
);
225 HMAC_Algs mac
= sslCipherSuiteGetMacAlgorithm(*src
);
226 SSL_CipherAlgorithm cipher
= sslCipherSuiteGetSymmetricCipherAlgorithm(*src
);
227 /* First skip ECDSA ciphers as appropriate */
230 case SSL_ECDHE_ECDSA
:
236 ctx
->numValidCipherSuites
--;
246 if(!ctx
->anonCipherEnable
) {
247 /* trim out the anonymous (and null-auth-cipher) ciphers */
250 ctx
->numValidCipherSuites
--;
256 case SSL_DH_anon_EXPORT
:
259 ctx
->numValidCipherSuites
--;
266 if(ctx
->falseStartEnabled
) {
268 case SSL_ECDHE_ECDSA
:
272 /* Ok for false start */
276 ctx
->numValidCipherSuites
--;
281 case SSL_CipherAlgorithmAES_128_CBC
:
282 case SSL_CipherAlgorithmAES_128_GCM
:
283 case SSL_CipherAlgorithmAES_256_CBC
:
284 case SSL_CipherAlgorithmAES_256_GCM
:
285 case SSL_CipherAlgorithmRC4_128
:
286 /* Ok for false start */
290 ctx
->numValidCipherSuites
--;
296 /* This will skip the simple DES cipher suites, but not the NULL cipher ones */
300 ctx
->numValidCipherSuites
--;
305 /* Trim PSK ciphersuites, they need to be enabled explicitely */
307 ctx
->numValidCipherSuites
--;
315 case SSL_DHE_DSS_EXPORT
:
317 case SSL_DHE_RSA_EXPORT
:
319 ctx
->numValidCipherSuites
--;
327 if (trimRC4
&& (cipher
==SSL_CipherAlgorithmRC4_128
)) {
328 ctx
->numValidCipherSuites
--;
335 case SSL_CipherAlgorithmAES_128_CBC
:
336 case SSL_CipherAlgorithmAES_256_CBC
:
337 case SSL_CipherAlgorithm3DES_CBC
:
338 ctx
->numValidCipherSuites
--;
346 if(cipher
==SSL_CipherAlgorithmNull
) {
347 ctx
->numValidCipherSuites
--;
352 /* This one is good to go */
356 // sslAnalyzeCipherSpecs(ctx);
358 tls_handshake_set_ciphersuites(ctx
->hdsk
, ctx
->validCipherSuites
, ctx
->numValidCipherSuites
);
360 return errSecSuccess
;
365 * Convert an array of SSLCipherSuites (which is always KnownCipherSpecs)
366 * to an array of SSLCipherSuites.
369 cipherSuitesToCipherSuites(
370 size_t numCipherSuites
,
371 const uint16_t *cipherSuites
,
372 SSLCipherSuite
*ciphers
, /* RETURNED */
373 size_t *numCiphers
) /* IN/OUT */
376 if(*numCiphers
< numCipherSuites
) {
377 return errSSLBufferOverflow
;
380 /* NOTE: this is required to go from uint16_t to SSLCipherSuite
381 which is either 32 or 16 bits, depending on the platform */
382 for(i
=0;i
<numCipherSuites
; i
++) {
383 ciphers
[i
]=cipherSuites
[i
];
385 *numCiphers
= numCipherSuites
;
386 return errSecSuccess
;
390 *** Publically exported functions declared in SecureTransport.h
394 * Determine number and values of all of the SSLCipherSuites we support.
395 * Caller allocates output buffer for SSLGetSupportedCiphers() and passes in
396 * its size in *numCiphers. If supplied buffer is too small, errSSLBufferOverflow
400 SSLGetNumberSupportedCiphers (SSLContextRef ctx
,
403 if((ctx
== NULL
) || (numCiphers
== NULL
)) {
406 *numCiphers
= STCipherSuiteCount
;
407 return errSecSuccess
;
411 SSLGetSupportedCiphers (SSLContextRef ctx
,
412 SSLCipherSuite
*ciphers
, /* RETURNED */
413 size_t *numCiphers
) /* IN/OUT */
415 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== NULL
)) {
418 return cipherSuitesToCipherSuites(STCipherSuiteCount
,
425 * Specify a (typically) restricted set of SSLCipherSuites to be enabled by
426 * the current SSLContext. Can only be called when no session is active. Default
427 * set of enabled SSLCipherSuites is NOT the same as the complete set of supported
428 * SSLCipherSuites as obtained by SSLGetSupportedCiphers().
431 SSLSetEnabledCiphers (SSLContextRef ctx
,
432 const SSLCipherSuite
*ciphers
,
436 unsigned foundCiphers
=0;
440 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== 0)) {
443 if(sslIsSessionActive(ctx
)) {
444 /* can't do this with an active session */
447 size
= numCiphers
* sizeof(uint16_t);
448 ctx
->validCipherSuites
= (uint16_t *)sslMalloc(size
);
449 if(ctx
->validCipherSuites
== NULL
) {
450 ctx
->numValidCipherSuites
= 0;
451 return errSecAllocate
;
455 * Run thru caller's specs, keep only the supported ones.
457 for(callerDex
=0; callerDex
<numCiphers
; callerDex
++) {
458 /* find matching CipherSpec in our known table */
459 for(tableDex
=0; tableDex
<STCipherSuiteCount
; tableDex
++) {
460 if(ciphers
[callerDex
] == STKnownCipherSuites
[tableDex
]) {
461 ctx
->validCipherSuites
[foundCiphers
] = STKnownCipherSuites
[tableDex
];
468 if(foundCiphers
==0) {
469 /* caller specified only unsupported ciphersuites */
470 sslFree(ctx
->validCipherSuites
);
471 ctx
->validCipherSuites
= NULL
;
472 return errSSLBadCipherSuite
;
476 ctx
->numValidCipherSuites
= foundCiphers
;
478 tls_handshake_set_ciphersuites(ctx
->hdsk
, ctx
->validCipherSuites
, ctx
->numValidCipherSuites
);
480 return errSecSuccess
;
484 * Determine number and values of all of the SSLCipherSuites currently enabled.
485 * Caller allocates output buffer for SSLGetEnabledCiphers() and passes in
486 * its size in *numCiphers. If supplied buffer is too small, errSSLBufferOverflow
490 SSLGetNumberEnabledCiphers (SSLContextRef ctx
,
493 if((ctx
== NULL
) || (numCiphers
== NULL
)) {
496 if(ctx
->validCipherSuites
== NULL
) {
497 /* hasn't been set; use default */
498 *numCiphers
= STCipherSuiteCount
;
501 /* caller set via SSLSetEnabledCiphers */
502 *numCiphers
= ctx
->numValidCipherSuites
;
504 return errSecSuccess
;
508 SSLGetEnabledCiphers (SSLContextRef ctx
,
509 SSLCipherSuite
*ciphers
, /* RETURNED */
510 size_t *numCiphers
) /* IN/OUT */
512 if((ctx
== NULL
) || (ciphers
== NULL
) || (numCiphers
== NULL
)) {
515 if(ctx
->validCipherSuites
== NULL
) {
516 /* hasn't been set; use default */
517 return cipherSuitesToCipherSuites(STCipherSuiteCount
,
523 /* use the ones specified in SSLSetEnabledCiphers() */
524 return cipherSuitesToCipherSuites(ctx
->numValidCipherSuites
,
525 ctx
->validCipherSuites
,