-/*
- * Given a valid ctx->validCipherSpecs array, calculate how many of those
- * cipherSpecs are *not* SSLv2 only, storing result in
- * ctx->numValidNonSSLv2Specs. ClientHello routines need this to set
- * up outgoing cipherSpecs arrays correctly.
- *
- * Also determines if any ECDSA/ECDH ciphers are enabled; we need to know
- * that when creating a hello message.
- */
-static void sslAnalyzeCipherSpecs(SSLContext *ctx)
-{
- unsigned dex;
- const SSLCipherSuite *cipherSuite;
-
-#if ENABLE_SSLV2
- ctx->numValidNonSSLv2Suites = 0;
-#endif
- cipherSuite = &ctx->validCipherSuites[0];
- ctx->ecdsaEnable = false;
- for(dex=0; dex<ctx->numValidCipherSuites; dex++, cipherSuite++) {
-#if ENABLE_SSLV2
- if(!CIPHER_SPEC_IS_SSLv2(*cipherSuite)) {
- ctx->numValidNonSSLv2Suites++;
- }
-#endif
- switch(sslCipherSuiteGetKeyExchangeMethod(*cipherSuite)) {
- case SSL_ECDH_ECDSA:
- case SSL_ECDHE_ECDSA:
- case SSL_ECDH_RSA:
- case SSL_ECDHE_RSA:
- case SSL_ECDH_anon:
- ctx->ecdsaEnable = true;
- break;
- default:
- break;
- }
- }
-}
-
-/*
- * Build ctx->validCipherSpecs as a copy of KnownCipherSpecs, assuming that
- * validCipherSpecs is currently not valid (i.e., SSLSetEnabledCiphers() has
- * not been called).
- */
-OSStatus sslBuildCipherSuiteArray(SSLContext *ctx)
-{
- size_t size;
- unsigned dex;
-
- assert(ctx != NULL);
- assert(ctx->validCipherSuites == NULL);
-
- ctx->numValidCipherSuites = CipherSuiteCount;
- size = CipherSuiteCount * sizeof(SSLCipherSpec);
- ctx->validCipherSuites = (SSLCipherSuite *)sslMalloc(size);
- if(ctx->validCipherSuites == NULL) {
- ctx->numValidCipherSuites = 0;
- return memFullErr;
- }
-
- /*
- * Trim out inappropriate ciphers:
- * -- trim anonymous ciphers if !ctx->anonCipherEnable (default)
- * -- trim ECDSA ciphers for server side if appropriate
- * -- trim ECDSA ciphers if TLSv1 disable or SSLv2 enabled (since
- * we MUST do the Client Hello extensions to make these ciphers
- * work reliably)
- * -- trim 40 and 56-bit ciphers if !ctx->weakCipherEnable (default)
- * -- trim ciphers incompatible with our private key in server mode
- * -- trim RC4 ciphers if DTLSv1 enable
- */
- SSLCipherSuite *dst = ctx->validCipherSuites;
- const SSLCipherSuite *src = KnownCipherSuites;
-
- bool trimECDSA = false;
- if((ctx->protocolSide == kSSLServerSide) && !SSL_ECDSA_SERVER) {
- trimECDSA = true;
- }
- if(ctx->minProtocolVersion == SSL_Version_2_0
- || ctx->maxProtocolVersion == SSL_Version_3_0) {
- /* We trim ECDSA cipher suites if SSL2 is enabled or
- The maximum allowed protocol is SSL3. Note that this
- won't trim ECDSA cipherspecs for DTLS which should be
- the right thing to do here. */
- trimECDSA = true;
- }
-
- bool trimRC4 = ctx->isDTLS;
-
- bool trimDHE = (ctx->protocolSide == kSSLServerSide) &&
- !ctx->dhParamsEncoded.length;
-
- for(dex=0; dex<CipherSuiteCount; dex++) {
- KeyExchangeMethod kem = sslCipherSuiteGetKeyExchangeMethod(*src);
- const SSLSymmetricCipher *cipher = sslCipherSuiteGetSymmetricCipher(*src);
- SSLProtocolVersion minVersion = sslCipherSuiteGetMinSupportedTLSVersion(*src);
-
- /* Trim according to supported versions */
- if(((ctx->isDTLS) && (minVersion>TLS_Version_1_1)) || /* DTLS is like TLS.1.1 */
- (minVersion > ctx->maxProtocolVersion))
- {
- ctx->numValidCipherSuites--;
- src++;
- continue;
- }
-
- /* First skip ECDSA ciphers as appropriate */
- switch(kem) {
- case SSL_ECDH_ECDSA:
- case SSL_ECDHE_ECDSA:
- case SSL_ECDH_RSA:
- case SSL_ECDHE_RSA:
- case SSL_ECDH_anon:
- if(trimECDSA) {
- /* Skip this one */
- ctx->numValidCipherSuites--;
- src++;
- continue;
- }
- else {
- break;
- }
- default:
- break;
- }
-
- if(!ctx->anonCipherEnable) {
- /* trim out the anonymous (and null-cipher) ciphers */
- if(cipher == &SSLCipherNull) {
- /* skip this one */
- ctx->numValidCipherSuites--;
- src++;
- continue;
- }
- switch(kem) {
- case SSL_DH_anon:
- case SSL_DH_anon_EXPORT:
- case SSL_ECDH_anon:
- /* skip this one */
- ctx->numValidCipherSuites--;
- src++;
- continue;
- default:
- break;
- }
- }
-
- if (false
- /* trim out 40 and 56 bit ciphers (considered unsafe to use) */
-#if ENABLE_RC4
- || (cipher == &SSLCipherRC4_40)
-#endif
-#if ENABLE_RC2
- || (cipher == &SSLCipherRC2_40)
-#endif
-#if ENABLE_DES
- || (cipher == &SSLCipherDES_CBC)
- || (cipher == &SSLCipherDES40_CBC)
-#endif
- ) {
- /* skip this one */
- ctx->numValidCipherSuites--;
- src++;
- continue;
- }
-
- if(ctx->protocolSide == kSSLServerSide && ctx->signingPrivKeyRef != NULL) {
- /* in server mode, trim out ciphers incompatible with our private key */
- SSLCipherSpec testCipherSpec = {
- .cipherSpec = *src,
- .keyExchangeMethod = kem,
- .cipher = cipher
- };
- if(sslVerifySelectedCipher(ctx, &testCipherSpec) != noErr) {
- /* skip this one */
- ctx->numValidCipherSuites--;
- src++;
- continue;
- }
- }
-
- if (trimDHE) {
- switch(kem) {
- case SSL_DHE_DSS:
- case SSL_DHE_DSS_EXPORT:
- case SSL_DHE_RSA:
- case SSL_DHE_RSA_EXPORT:
- /* skip this one */
- ctx->numValidCipherSuites--;
- src++;
- continue;
- default:
- break;
- }
- }
-
- if (trimRC4 && cipher && (cipher->keyAlg == kCCAlgorithmRC4)) {
- ctx->numValidCipherSuites--;
- src++;
- continue;
- }
-
- /* This one is good to go */
- *dst++ = *src++;
- }
- sslAnalyzeCipherSpecs(ctx);
- return noErr;
-}
-
-/*
- * Convert an array of SSLCipherSuites (which is always KnownCipherSpecs)
- * to an array of SSLCipherSuites.
- */
-static OSStatus
-cipherSuitesToCipherSuites(
- size_t numCipherSuites,
- const SSLCipherSuite *cipherSuites,
- SSLCipherSuite *ciphers, /* RETURNED */
- size_t *numCiphers) /* IN/OUT */
-{
- if(*numCiphers < numCipherSuites) {
- return errSSLBufferOverflow;
- }
- memcpy(ciphers, cipherSuites, numCipherSuites * sizeof(SSLCipherSuite));
- *numCiphers = numCipherSuites;
- return noErr;
-}
-
-/***
- *** Publicly exported functions declared in SecureTransport.h
- ***/
-
-/*
- * Determine number and values of all of the SSLCipherSuites we support.
- * Caller allocates output buffer for SSLGetSupportedCiphers() and passes in
- * its size in *numCiphers. If supplied buffer is too small, errSSLBufferOverflow
- * will be returned.
- */
-OSStatus
-SSLGetNumberSupportedCiphers (SSLContextRef ctx,
- size_t *numCiphers)
-{
- if((ctx == NULL) || (numCiphers == NULL)) {
- return paramErr;
- }
- *numCiphers = CipherSuiteCount;
- return noErr;
-}
-
-OSStatus
-SSLGetSupportedCiphers (SSLContextRef ctx,
- SSLCipherSuite *ciphers, /* RETURNED */
- size_t *numCiphers) /* IN/OUT */
-{
- if((ctx == NULL) || (ciphers == NULL) || (numCiphers == NULL)) {
- return paramErr;
- }
- return cipherSuitesToCipherSuites(CipherSuiteCount,
- KnownCipherSuites,
- ciphers,
- numCiphers);
-}
-
-/*
- * Specify a (typically) restricted set of SSLCipherSuites to be enabled by
- * the current SSLContext. Can only be called when no session is active. Default
- * set of enabled SSLCipherSuites is the same as the complete set of supported
- * SSLCipherSuites as obtained by SSLGetSupportedCiphers().
- */
-OSStatus
-SSLSetEnabledCiphers (SSLContextRef ctx,
- const SSLCipherSuite *ciphers,
- size_t numCiphers)
-{
- size_t size;
- unsigned callerDex;
- unsigned validDex;
- unsigned tableDex;
-
- if((ctx == NULL) || (ciphers == NULL) || (numCiphers == 0)) {
- return paramErr;
- }
- if(sslIsSessionActive(ctx)) {
- /* can't do this with an active session */
- return badReqErr;
- }
- ctx->numValidCipherSuites = 0;
- size = numCiphers * sizeof(SSLCipherSuite);
- ctx->validCipherSuites = (SSLCipherSuite *)sslMalloc(size);
- if(ctx->validCipherSuites == NULL) {
- return memFullErr;
- }
-
- /*
- * Run thru caller's specs, finding a matching SSLCipherSpec for each one.
- * If caller specifies one we don't know about, skip it.
- */
- for(callerDex=0, validDex=0; callerDex<numCiphers; callerDex++) {
- /* find matching CipherSpec in our known table */
- int foundOne = 0;
- for(tableDex=0; tableDex<CipherSuiteCount; tableDex++) {
- if(ciphers[callerDex] == KnownCipherSuites[tableDex]) {
- ctx->validCipherSuites[validDex++] = KnownCipherSuites[tableDex];
- ctx->numValidCipherSuites++;
- foundOne = 1;
- break;
- }
- }
- if(!foundOne) {
- /* caller specified one we don't implement */
- sslErrorLog("SSLSetEnabledCiphers: invalid cipher suite %04hX",
- ciphers[callerDex]);
- #if 0
- sslFree(ctx->validCipherSuites);
- ctx->validCipherSuites = NULL;
- ctx->numValidCipherSuites = 0;
- return errSSLBadCipherSuite;
- #endif
- }
- }
-
- /* success */
- sslAnalyzeCipherSpecs(ctx);
- return noErr;
-}
-
-/*
- * Determine number and values of all of the SSLCipherSuites currently enabled.
- * Caller allocates output buffer for SSLGetEnabledCiphers() and passes in
- * its size in *numCiphers. If supplied buffer is too small, errSSLBufferOverflow
- * will be returned.
- */
-OSStatus
-SSLGetNumberEnabledCiphers (SSLContextRef ctx,
- size_t *numCiphers)
-{
- if((ctx == NULL) || (numCiphers == NULL)) {
- return paramErr;
- }
- if(ctx->validCipherSuites == NULL) {
- /* hasn't been set; build default array temporarily */
- OSStatus status = sslBuildCipherSuiteArray(ctx);
- if(!status) {
- *numCiphers = ctx->numValidCipherSuites;
- /* put things back as we found them */
- sslFree(ctx->validCipherSuites);
- ctx->validCipherSuites = NULL;
- ctx->numValidCipherSuites = 0;
- } else {
- /* unable to build default array; use known cipher count */
- *numCiphers = CipherSuiteCount;
- }
- }
- else {
- /* caller set via SSLSetEnabledCiphers */
- *numCiphers = ctx->numValidCipherSuites;
- }
- return noErr;
-}
-
-OSStatus
-SSLGetEnabledCiphers (SSLContextRef ctx,
- SSLCipherSuite *ciphers, /* RETURNED */
- size_t *numCiphers) /* IN/OUT */
-{
- if((ctx == NULL) || (ciphers == NULL) || (numCiphers == NULL)) {
- return paramErr;
- }
- if(ctx->validCipherSuites == NULL) {
- /* hasn't been set; build default array temporarily */
- OSStatus status = sslBuildCipherSuiteArray(ctx);
- if(!status) {
- status = cipherSuitesToCipherSuites(ctx->numValidCipherSuites,
- ctx->validCipherSuites,
- ciphers,
- numCiphers);
- /* put things back as we found them */
- sslFree(ctx->validCipherSuites);
- ctx->validCipherSuites = NULL;
- ctx->numValidCipherSuites = 0;
- } else {
- /* unable to build default array; use known cipher suite array */
- status = cipherSuitesToCipherSuites(CipherSuiteCount,
- KnownCipherSuites,
- ciphers,
- numCiphers);
- }
- return status;
- }
- else {
- /* use the ones specified in SSLSetEnabledCiphers() */
- return cipherSuitesToCipherSuites(ctx->numValidCipherSuites,
- ctx->validCipherSuites,
- ciphers,
- numCiphers);
- }