2 * Copyright (c) 2006-2008,2010-2014 Apple Inc. All Rights Reserved.
5 #include "sslAppUtils.h"
10 #include <Security/SecBase.h>
12 #include <CoreFoundation/CoreFoundation.h>
13 #include <Security/Security.h>
14 #include <Security/SecIdentityPriv.h>
15 #include <AssertMacros.h>
17 #define CFReleaseSafe(CF) { CFTypeRef _cf = (CF); if (_cf) CFRelease(_cf); }
19 const char *sslGetCipherSuiteString(SSLCipherSuite cs
)
21 static char noSuite
[40];
24 /* TLS cipher suites, RFC 2246 */
25 case SSL_NULL_WITH_NULL_NULL
: return "TLS_NULL_WITH_NULL_NULL";
26 case SSL_RSA_WITH_NULL_MD5
: return "TLS_RSA_WITH_NULL_MD5";
27 case SSL_RSA_WITH_NULL_SHA
: return "TLS_RSA_WITH_NULL_SHA";
28 case SSL_RSA_EXPORT_WITH_RC4_40_MD5
: return "TLS_RSA_EXPORT_WITH_RC4_40_MD5";
29 case SSL_RSA_WITH_RC4_128_MD5
: return "TLS_RSA_WITH_RC4_128_MD5";
30 case SSL_RSA_WITH_RC4_128_SHA
: return "TLS_RSA_WITH_RC4_128_SHA";
31 case SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
: return "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5";
32 case SSL_RSA_WITH_IDEA_CBC_SHA
: return "TLS_RSA_WITH_IDEA_CBC_SHA";
33 case SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA";
34 case SSL_RSA_WITH_DES_CBC_SHA
: return "TLS_RSA_WITH_DES_CBC_SHA";
35 case SSL_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_RSA_WITH_3DES_EDE_CBC_SHA";
36 case SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA";
37 case SSL_DH_DSS_WITH_DES_CBC_SHA
: return "TLS_DH_DSS_WITH_DES_CBC_SHA";
38 case SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
: return "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA";
39 case SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA";
40 case SSL_DH_RSA_WITH_DES_CBC_SHA
: return "TLS_DH_RSA_WITH_DES_CBC_SHA";
41 case SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA";
42 case SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA";
43 case SSL_DHE_DSS_WITH_DES_CBC_SHA
: return "TLS_DHE_DSS_WITH_DES_CBC_SHA";
44 case SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
: return "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA";
45 case SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA";
46 case SSL_DHE_RSA_WITH_DES_CBC_SHA
: return "TLS_DHE_RSA_WITH_DES_CBC_SHA";
47 case SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA";
48 case SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
: return "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5";
49 case SSL_DH_anon_WITH_RC4_128_MD5
: return "TLS_DH_anon_WITH_RC4_128_MD5";
50 case SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA";
51 case SSL_DH_anon_WITH_DES_CBC_SHA
: return "TLS_DH_anon_WITH_DES_CBC_SHA";
52 case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
: return "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA";
54 /* SSLv3 Fortezza cipher suites, from NSS */
55 case SSL_FORTEZZA_DMS_WITH_NULL_SHA
: return "SSL_FORTEZZA_DMS_WITH_NULL_SHA";
56 case SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA
:return "SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA";
58 /* TLS addenda using AES-CBC, RFC 3268 */
59 case TLS_RSA_WITH_AES_128_CBC_SHA
: return "TLS_RSA_WITH_AES_128_CBC_SHA";
60 case TLS_DH_DSS_WITH_AES_128_CBC_SHA
: return "TLS_DH_DSS_WITH_AES_128_CBC_SHA";
61 case TLS_DH_RSA_WITH_AES_128_CBC_SHA
: return "TLS_DH_RSA_WITH_AES_128_CBC_SHA";
62 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA
: return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA";
63 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA
: return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
64 case TLS_DH_anon_WITH_AES_128_CBC_SHA
: return "TLS_DH_anon_WITH_AES_128_CBC_SHA";
65 case TLS_RSA_WITH_AES_256_CBC_SHA
: return "TLS_RSA_WITH_AES_256_CBC_SHA";
66 case TLS_DH_DSS_WITH_AES_256_CBC_SHA
: return "TLS_DH_DSS_WITH_AES_256_CBC_SHA";
67 case TLS_DH_RSA_WITH_AES_256_CBC_SHA
: return "TLS_DH_RSA_WITH_AES_256_CBC_SHA";
68 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA
: return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA";
69 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA
: return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
70 case TLS_DH_anon_WITH_AES_256_CBC_SHA
: return "TLS_DH_anon_WITH_AES_256_CBC_SHA";
72 /* ECDSA addenda, RFC 4492 */
73 case TLS_ECDH_ECDSA_WITH_NULL_SHA
: return "TLS_ECDH_ECDSA_WITH_NULL_SHA";
74 case TLS_ECDH_ECDSA_WITH_RC4_128_SHA
: return "TLS_ECDH_ECDSA_WITH_RC4_128_SHA";
75 case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA";
76 case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
: return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA";
77 case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
: return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA";
78 case TLS_ECDHE_ECDSA_WITH_NULL_SHA
: return "TLS_ECDHE_ECDSA_WITH_NULL_SHA";
79 case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
: return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA";
80 case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA";
81 case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
: return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA";
82 case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
: return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA";
83 case TLS_ECDH_RSA_WITH_NULL_SHA
: return "TLS_ECDH_RSA_WITH_NULL_SHA";
84 case TLS_ECDH_RSA_WITH_RC4_128_SHA
: return "TLS_ECDH_RSA_WITH_RC4_128_SHA";
85 case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA";
86 case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
: return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA";
87 case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
: return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA";
88 case TLS_ECDHE_RSA_WITH_NULL_SHA
: return "TLS_ECDHE_RSA_WITH_NULL_SHA";
89 case TLS_ECDHE_RSA_WITH_RC4_128_SHA
: return "TLS_ECDHE_RSA_WITH_RC4_128_SHA";
90 case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA";
91 case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
: return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA";
92 case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
: return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA";
93 case TLS_ECDH_anon_WITH_NULL_SHA
: return "TLS_ECDH_anon_WITH_NULL_SHA";
94 case TLS_ECDH_anon_WITH_RC4_128_SHA
: return "TLS_ECDH_anon_WITH_RC4_128_SHA";
95 case TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA";
96 case TLS_ECDH_anon_WITH_AES_128_CBC_SHA
: return "TLS_ECDH_anon_WITH_AES_128_CBC_SHA";
97 case TLS_ECDH_anon_WITH_AES_256_CBC_SHA
: return "TLS_ECDH_anon_WITH_AES_256_CBC_SHA";
99 /* TLS 1.2 addenda, RFC 5246 */
100 case TLS_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_RSA_WITH_AES_128_CBC_SHA256";
101 case TLS_RSA_WITH_AES_256_CBC_SHA256
: return "TLS_RSA_WITH_AES_256_CBC_SHA256";
102 case TLS_DH_DSS_WITH_AES_128_CBC_SHA256
: return "TLS_DH_DSS_WITH_AES_128_CBC_SHA256";
103 case TLS_DH_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_DH_RSA_WITH_AES_128_CBC_SHA256";
104 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
: return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256";
105 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256";
106 case TLS_DH_DSS_WITH_AES_256_CBC_SHA256
: return "TLS_DH_DSS_WITH_AES_256_CBC_SHA256";
107 case TLS_DH_RSA_WITH_AES_256_CBC_SHA256
: return "TLS_DH_RSA_WITH_AES_256_CBC_SHA256";
108 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
: return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256";
109 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
: return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256";
110 case TLS_DH_anon_WITH_AES_128_CBC_SHA256
: return "TLS_DH_anon_WITH_AES_128_CBC_SHA256";
111 case TLS_DH_anon_WITH_AES_256_CBC_SHA256
: return "TLS_DH_anon_WITH_AES_256_CBC_SHA256";
113 /* TLS addenda using AES-GCM, RFC 5288 */
114 case TLS_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_RSA_WITH_AES_128_GCM_SHA256";
115 case TLS_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
116 case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
117 case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384";
118 case TLS_DH_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_DH_RSA_WITH_AES_128_GCM_SHA256";
119 case TLS_DH_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_DH_RSA_WITH_AES_256_GCM_SHA384";
120 case TLS_DHE_DSS_WITH_AES_128_GCM_SHA256
: return "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256";
121 case TLS_DHE_DSS_WITH_AES_256_GCM_SHA384
: return "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384";
122 case TLS_DH_DSS_WITH_AES_128_GCM_SHA256
: return "TLS_DH_DSS_WITH_AES_128_GCM_SHA256";
123 case TLS_DH_DSS_WITH_AES_256_GCM_SHA384
: return "TLS_DH_DSS_WITH_AES_256_GCM_SHA384";
124 case TLS_DH_anon_WITH_AES_128_GCM_SHA256
: return "TLS_DH_anon_WITH_AES_128_GCM_SHA256";
125 case TLS_DH_anon_WITH_AES_256_GCM_SHA384
: return "TLS_DH_anon_WITH_AES_256_GCM_SHA384";
127 /* ECDSA addenda, RFC 5289 */
128 case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
: return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256";
129 case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
: return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384";
130 case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
: return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256";
131 case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
: return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384";
132 case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256";
133 case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
: return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384";
134 case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256";
135 case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
: return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384";
136 case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
: return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
137 case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
: return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384";
138 case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
: return "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256";
139 case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
: return "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384";
140 case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
141 case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
142 case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256";
143 case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384";
146 * Tags for SSL 2 cipher kinds which are not specified for SSL 3.
148 case SSL_RSA_WITH_RC2_CBC_MD5
: return "TLS_RSA_WITH_RC2_CBC_MD5";
149 case SSL_RSA_WITH_IDEA_CBC_MD5
: return "TLS_RSA_WITH_IDEA_CBC_MD5";
150 case SSL_RSA_WITH_DES_CBC_MD5
: return "TLS_RSA_WITH_DES_CBC_MD5";
151 case SSL_RSA_WITH_3DES_EDE_CBC_MD5
: return "TLS_RSA_WITH_3DES_EDE_CBC_MD5";
152 case SSL_NO_SUCH_CIPHERSUITE
: return "SSL_NO_SUCH_CIPHERSUITE";
155 snprintf(noSuite
, sizeof(noSuite
), "Unknown ciphersuite 0x%04x", (unsigned)cs
);
161 * Given a SSLProtocolVersion - typically from SSLGetProtocolVersion -
162 * return a string representation.
164 const char *sslGetProtocolVersionString(SSLProtocol prot
)
166 static char noProt
[20];
169 case kSSLProtocolUnknown
: return "kSSLProtocolUnknown";
170 case kSSLProtocol2
: return "kSSLProtocol2";
171 case kSSLProtocol3
: return "kSSLProtocol3";
172 case kSSLProtocol3Only
: return "kSSLProtocol3Only";
173 case kTLSProtocol1
: return "kTLSProtocol1";
174 case kTLSProtocol1Only
: return "kTLSProtocol1Only";
175 case kTLSProtocol11
: return "kTLSProtocol11";
176 case kTLSProtocol12
: return "kTLSProtocol12";
178 sprintf(noProt
, "Unknown (%d)", (unsigned)prot
);
184 * Return string representation of SecureTransport-related OSStatus.
186 const char *sslGetSSLErrString(OSStatus err
)
188 static char errSecSuccessStr
[20];
191 case errSecSuccess
: return "errSecSuccess";
192 case errSecAllocate
: return "errSecAllocate";
193 case errSecParam
: return "errSecParam";
194 case errSecUnimplemented
: return "errSecUnimplemented";
195 case errSecIO
: return "errSecIO";
196 case errSecBadReq
: return "errSecBadReq";
198 case errSSLProtocol
: return "errSSLProtocol";
199 case errSSLNegotiation
: return "errSSLNegotiation";
200 case errSSLFatalAlert
: return "errSSLFatalAlert";
201 case errSSLWouldBlock
: return "errSSLWouldBlock";
202 case errSSLSessionNotFound
: return "errSSLSessionNotFound";
203 case errSSLClosedGraceful
: return "errSSLClosedGraceful";
204 case errSSLClosedAbort
: return "errSSLClosedAbort";
205 case errSSLXCertChainInvalid
: return "errSSLXCertChainInvalid";
206 case errSSLBadCert
: return "errSSLBadCert";
207 case errSSLCrypto
: return "errSSLCrypto";
208 case errSSLInternal
: return "errSSLInternal";
209 case errSSLModuleAttach
: return "errSSLModuleAttach";
210 case errSSLUnknownRootCert
: return "errSSLUnknownRootCert";
211 case errSSLNoRootCert
: return "errSSLNoRootCert";
212 case errSSLCertExpired
: return "errSSLCertExpired";
213 case errSSLCertNotYetValid
: return "errSSLCertNotYetValid";
214 case errSSLClosedNoNotify
: return "errSSLClosedNoNotify";
215 case errSSLBufferOverflow
: return "errSSLBufferOverflow";
216 case errSSLBadCipherSuite
: return "errSSLBadCipherSuite";
217 /* TLS/Panther addenda */
218 case errSSLPeerUnexpectedMsg
: return "errSSLPeerUnexpectedMsg";
219 case errSSLPeerBadRecordMac
: return "errSSLPeerBadRecordMac";
220 case errSSLPeerDecryptionFail
: return "errSSLPeerDecryptionFail";
221 case errSSLPeerRecordOverflow
: return "errSSLPeerRecordOverflow";
222 case errSSLPeerDecompressFail
: return "errSSLPeerDecompressFail";
223 case errSSLPeerHandshakeFail
: return "errSSLPeerHandshakeFail";
224 case errSSLPeerBadCert
: return "errSSLPeerBadCert";
225 case errSSLPeerUnsupportedCert
: return "errSSLPeerUnsupportedCert";
226 case errSSLPeerCertRevoked
: return "errSSLPeerCertRevoked";
227 case errSSLPeerCertExpired
: return "errSSLPeerCertExpired";
228 case errSSLPeerCertUnknown
: return "errSSLPeerCertUnknown";
229 case errSSLIllegalParam
: return "errSSLIllegalParam";
230 case errSSLPeerUnknownCA
: return "errSSLPeerUnknownCA";
231 case errSSLPeerAccessDenied
: return "errSSLPeerAccessDenied";
232 case errSSLPeerDecodeError
: return "errSSLPeerDecodeError";
233 case errSSLPeerDecryptError
: return "errSSLPeerDecryptError";
234 case errSSLPeerExportRestriction
: return "errSSLPeerExportRestriction";
235 case errSSLPeerProtocolVersion
: return "errSSLPeerProtocolVersion";
236 case errSSLPeerInsufficientSecurity
:return "errSSLPeerInsufficientSecurity";
237 case errSSLPeerInternalError
: return "errSSLPeerInternalError";
238 case errSSLPeerUserCancelled
: return "errSSLPeerUserCancelled";
239 case errSSLPeerNoRenegotiation
: return "errSSLPeerNoRenegotiation";
240 case errSSLHostNameMismatch
: return "errSSLHostNameMismatch";
241 case errSSLConnectionRefused
: return "errSSLConnectionRefused";
242 case errSSLDecryptionFail
: return "errSSLDecryptionFail";
243 case errSSLBadRecordMac
: return "errSSLBadRecordMac";
244 case errSSLRecordOverflow
: return "errSSLRecordOverflow";
245 case errSSLBadConfiguration
: return "errSSLBadConfiguration";
247 /* some from the Sec layer */
248 case errSecNotAvailable
: return "errSecNotAvailable";
249 case errSecDuplicateItem
: return "errSecDuplicateItem";
250 case errSecItemNotFound
: return "errSecItemNotFound";
251 #if !TARGET_OS_IPHONE
252 case errSecReadOnly
: return "errSecReadOnly";
253 case errSecAuthFailed
: return "errSecAuthFailed";
254 case errSecNoSuchKeychain
: return "errSecNoSuchKeychain";
255 case errSecInvalidKeychain
: return "errSecInvalidKeychain";
256 case errSecNoSuchAttr
: return "errSecNoSuchAttr";
257 case errSecInvalidItemRef
: return "errSecInvalidItemRef";
258 case errSecInvalidSearchRef
: return "errSecInvalidSearchRef";
259 case errSecNoSuchClass
: return "errSecNoSuchClass";
260 case errSecNoDefaultKeychain
: return "errSecNoDefaultKeychain";
261 case errSecWrongSecVersion
: return "errSecWrongSecVersion";
262 case errSecInvalidTrustSettings
: return "errSecInvalidTrustSettings";
263 case errSecNoTrustSettings
: return "errSecNoTrustSettings";
267 if (err
< (CSSM_BASE_ERROR
+
268 (CSSM_ERRORCODE_MODULE_EXTENT
* 8)))
270 /* assume CSSM error */
271 return cssmErrToStr(err
);
276 sprintf(errSecSuccessStr
, "Unknown (%d)", (unsigned)err
);
277 return errSecSuccessStr
;
286 printf("*** %s: %s\n", op
, sslGetSSLErrString(err
));
289 const char *sslGetClientCertStateString(SSLClientCertificateState state
)
291 static char noState
[20];
294 case kSSLClientCertNone
: return "ClientCertNone";
295 case kSSLClientCertRequested
: return "CertRequested";
296 case kSSLClientCertSent
: return "ClientCertSent";
297 case kSSLClientCertRejected
: return "ClientCertRejected";
299 sprintf(noState
, "Unknown (%d)", (unsigned)state
);
305 const char *sslGetClientAuthTypeString(SSLClientAuthenticationType authType
)
307 static char noType
[20];
310 case SSLClientAuthNone
: return "None";
311 case SSLClientAuth_RSASign
: return "RSASign";
312 case SSLClientAuth_DSSSign
: return "DSSSign";
313 case SSLClientAuth_RSAFixedDH
: return "RSAFixedDH";
314 case SSLClientAuth_DSS_FixedDH
: return "DSS_FixedDH";
315 case SSLClientAuth_ECDSASign
: return "ECDSASign";
316 case SSLClientAuth_RSAFixedECDH
: return "RSAFixedECDH";
317 case SSLClientAuth_ECDSAFixedECDH
: return "ECDSAFixedECDH";
319 sprintf(noType
, "Unknown (%d)", (unsigned)authType
);
325 * Convert a keychain name (which may be NULL) into the CFArrayRef required
326 * by SSLSetCertificate. This is a bare-bones example of this operation,
327 * since it requires and assumes that there is exactly one SecIdentity
328 * in the keychain - i.e., there is exactly one matching cert/private key
329 * pair. A real world server would probably search a keychain for a SecIdentity
330 * matching some specific criteria.
332 CFArrayRef
getSslCerts(
333 const char *kcName
, // may be NULL, i.e., use default
335 bool completeCertChain
,
336 const char *anchorFile
, // optional trusted anchor
337 SecKeychainRef
*pKcRef
) // RETURNED
340 SecKeychainRef kcRef
= nil
;
345 /* pick a keychain */
347 ortn
= SecKeychainOpen(kcName
, &kcRef
);
349 printf("SecKeychainOpen returned %d.\n", (int)ortn
);
350 printf("Cannot open keychain at %s. Aborting.\n", kcName
);
355 /* use default keychain */
356 ortn
= SecKeychainCopyDefault(&kcRef
);
358 printf("SecKeychainCopyDefault returned %d; aborting.\n", (int)ortn
);
363 return sslKcRefToCertArray(kcRef
, encryptOnly
, completeCertChain
, anchorFile
);
365 SecCertificateRef cert
= NULL
;
366 SecIdentityRef identity
= NULL
;
367 CFMutableArrayRef certificates
= NULL
, result
= NULL
;
368 CFMutableDictionaryRef certQuery
= NULL
, keyQuery
= NULL
, keyResult
= NULL
;
369 SecTrustRef trust
= NULL
;
370 SecKeyRef key
= NULL
;
371 CFTypeRef pkdigest
= NULL
;
373 // Find the first private key in the keychain and return both its
374 // attributes and a ref to it.
375 require(keyQuery
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
), errOut
);
376 CFDictionaryAddValue(keyQuery
, kSecClass
, kSecClassKey
);
377 CFDictionaryAddValue(keyQuery
, kSecAttrKeyClass
, kSecAttrKeyClassPrivate
);
378 CFDictionaryAddValue(keyQuery
, kSecReturnRef
, kCFBooleanTrue
);
379 CFDictionaryAddValue(keyQuery
, kSecReturnAttributes
, kCFBooleanTrue
);
380 require_noerr(SecItemCopyMatching(keyQuery
, (CFTypeRef
*)&keyResult
),
382 require(key
= (SecKeyRef
)CFDictionaryGetValue(keyResult
, kSecValueRef
),
384 require(pkdigest
= CFDictionaryGetValue(keyResult
, kSecAttrApplicationLabel
),
387 // Find the first certificate that has the same public key hash as the
388 // returned private key and return it as a ref.
389 require(certQuery
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
), errOut
);
390 CFDictionaryAddValue(certQuery
, kSecClass
, kSecClassCertificate
);
391 CFDictionaryAddValue(certQuery
, kSecAttrPublicKeyHash
, pkdigest
);
392 CFDictionaryAddValue(certQuery
, kSecReturnRef
, kCFBooleanTrue
);
393 require_noerr(SecItemCopyMatching(certQuery
, (CFTypeRef
*)&cert
), errOut
);
395 // Create an identity from the key and certificate.
396 require(identity
= SecIdentityCreate(NULL
, cert
, key
), errOut
);
398 // Build a (partial) certificate chain from cert
399 require(certificates
= CFArrayCreateMutable(NULL
, 0,
400 &kCFTypeArrayCallBacks
), errOut
);
401 CFArrayAppendValue(certificates
, cert
);
402 require_noerr(SecTrustCreateWithCertificates(certificates
, NULL
, &trust
),
404 SecTrustResultType tresult
;
405 require_noerr(SecTrustEvaluate(trust
, &tresult
), errOut
);
407 CFIndex certCount
, ix
;
408 // We need at least 1 certificate
409 require(certCount
= SecTrustGetCertificateCount(trust
), errOut
);
411 // Build a result where element 0 is the identity and the other elements
412 // are the certs in the chain starting at the first intermediate up to the
413 // anchor, if we found one, or as far as we were able to build the chain
415 require(result
= CFArrayCreateMutable(NULL
, certCount
, &kCFTypeArrayCallBacks
),
418 // We are commited to returning a result now, so do not use require below
419 // this line without setting result to NULL again.
420 CFArrayAppendValue(result
, identity
);
421 for (ix
= 1; ix
< certCount
; ++ix
) {
422 CFArrayAppendValue(result
, SecTrustGetCertificateAtIndex(trust
, ix
));
426 CFReleaseSafe(trust
);
427 CFReleaseSafe(certificates
);
428 CFReleaseSafe(identity
);
430 CFReleaseSafe(certQuery
);
431 CFReleaseSafe(keyResult
);
432 CFReleaseSafe(keyQuery
);
436 #else /* !TARGET_OS_IOS */
437 SecIdentityRef identity
= NULL
;
438 CFMutableDictionaryRef query
= NULL
;
439 CFArrayRef items
= NULL
;
440 require(query
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
), errOut
);
441 CFDictionaryAddValue(query
, kSecClass
, kSecClassIdentity
);
442 CFDictionaryAddValue(query
, kSecReturnRef
, kCFBooleanTrue
);
443 require_noerr(SecItemCopyMatching(query
, (CFTypeRef
*)&identity
), errOut
);
445 items
= CFArrayCreate(kCFAllocatorDefault
,
446 (const void **)&identity
, 1, &kCFTypeArrayCallBacks
);
449 CFReleaseSafe(identity
);
450 CFReleaseSafe(query
);
460 * Determine if specified SecCertificateRef is a self-signed cert.
461 * We do this by comparing the subject and issuerr names; no cryptographic
462 * verification is performed.
464 * Returns true if the cert appears to be a root.
466 static bool isCertRefRoot(
467 SecCertificateRef certRef
)
471 /* just search for the two attrs we want */
472 UInt32 tags
[2] = {kSecSubjectItemAttr
, kSecIssuerItemAttr
};
473 SecKeychainAttributeInfo attrInfo
;
476 attrInfo
.format
= NULL
;
477 SecKeychainAttributeList
*attrList
= NULL
;
478 SecKeychainAttribute
*attr1
= NULL
;
479 SecKeychainAttribute
*attr2
= NULL
;
481 OSStatus ortn
= SecKeychainItemCopyAttributesAndData(
482 (SecKeychainItemRef
)certRef
,
486 NULL
, // length - don't need the data
489 cssmPerror("SecKeychainItemCopyAttributesAndData", ortn
);
490 /* may want to be a bit more robust here, but this should
494 /* subsequent errors to errOut: */
496 if((attrList
== NULL
) || (attrList
->count
!= 2)) {
497 printf("***Unexpected result fetching label attr\n");
501 /* rootness is just byte-for-byte compare of the two names */
502 attr1
= &attrList
->attr
[0];
503 attr2
= &attrList
->attr
[1];
504 if(attr1
->length
== attr2
->length
) {
505 if(memcmp(attr1
->data
, attr2
->data
, attr1
->length
) == 0) {
510 SecKeychainItemFreeAttributesAndData(attrList
, NULL
);
518 * Given a SecIdentityRef, do our best to construct a complete, ordered, and
519 * verified cert chain, returning the result in a CFArrayRef. The result is
520 * suitable for use when calling SSLSetCertificate().
522 OSStatus
sslCompleteCertChain(
523 SecIdentityRef identity
,
524 SecCertificateRef trustedAnchor
, // optional additional trusted anchor
525 bool includeRoot
, // include the root in outArray
526 CFArrayRef
*outArray
) // created and RETURNED
528 CFMutableArrayRef certArray
;
529 SecTrustRef secTrust
= NULL
;
530 SecPolicyRef policy
= NULL
;
531 SecPolicySearchRef policySearch
= NULL
;
532 SecTrustResultType secTrustResult
;
533 CSSM_TP_APPLE_EVIDENCE_INFO
*dummyEv
; // not used
534 CFArrayRef certChain
= NULL
; // constructed chain
537 certArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
538 CFArrayAppendValue(certArray
, identity
);
541 * Case 1: identity is a root; we're done. Note that this case
542 * overrides the includeRoot argument.
544 SecCertificateRef certRef
;
545 OSStatus ortn
= SecIdentityCopyCertificate(identity
, &certRef
);
547 /* should never happen */
548 cssmPerror("SecIdentityCopyCertificate", ortn
);
551 bool isRoot
= isCertRefRoot(certRef
);
553 *outArray
= certArray
;
555 return errSecSuccess
;
559 * Now use SecTrust to get a complete cert chain, using all of the
560 * user's keychains to look for intermediate certs.
561 * NOTE this does NOT handle root certs which are not in the system
562 * root cert DB. (The above case, where the identity is a root cert, does.)
564 CFMutableArrayRef subjCerts
= CFArrayCreateMutable(NULL
, 1, &kCFTypeArrayCallBacks
);
565 CFArraySetValueAtIndex(subjCerts
, 0, certRef
);
567 /* the array owns the subject cert ref now */
570 /* Get a SecPolicyRef for generic X509 cert chain verification */
571 ortn
= SecPolicySearchCreate(CSSM_CERT_X_509v3
,
572 &CSSMOID_APPLE_X509_BASIC
,
576 cssmPerror("SecPolicySearchCreate", ortn
);
579 ortn
= SecPolicySearchCopyNext(policySearch
, &policy
);
581 cssmPerror("SecPolicySearchCopyNext", ortn
);
585 /* build a SecTrustRef for specified policy and certs */
586 ortn
= SecTrustCreateWithCertificates(subjCerts
,
589 cssmPerror("SecTrustCreateWithCertificates", ortn
);
595 * Tell SecTrust to trust this one in addition to the current
596 * trusted system-wide anchors.
598 CFMutableArrayRef newAnchors
;
599 CFArrayRef currAnchors
;
601 ortn
= SecTrustCopyAnchorCertificates(&currAnchors
);
603 /* should never happen */
604 cssmPerror("SecTrustCopyAnchorCertificates", ortn
);
607 newAnchors
= CFArrayCreateMutableCopy(NULL
,
608 CFArrayGetCount(currAnchors
) + 1,
610 CFRelease(currAnchors
);
611 CFArrayAppendValue(newAnchors
, trustedAnchor
);
612 ortn
= SecTrustSetAnchorCertificates(secTrust
, newAnchors
);
613 CFRelease(newAnchors
);
615 cssmPerror("SecTrustSetAnchorCertificates", ortn
);
620 ortn
= SecTrustEvaluate(secTrust
, &secTrustResult
);
622 cssmPerror("SecTrustEvaluate", ortn
);
625 switch(secTrustResult
) {
626 case kSecTrustResultUnspecified
:
627 /* cert chain valid, no special UserTrust assignments */
628 case kSecTrustResultProceed
:
629 /* cert chain valid AND user explicitly trusts this */
633 * Cert chain construction failed.
634 * Just go with the single subject cert we were given.
636 printf("***Warning: could not construct completed cert chain\n");
637 ortn
= errSecSuccess
;
641 /* get resulting constructed cert chain */
642 ortn
= SecTrustGetResult(secTrust
, &secTrustResult
, &certChain
, &dummyEv
);
644 cssmPerror("SecTrustEvaluate", ortn
);
649 * Copy certs from constructed chain to our result array, skipping
650 * the leaf (which is already there, as a SecIdentityRef) and possibly
653 numResCerts
= CFArrayGetCount(certChain
);
654 if(numResCerts
< 2) {
656 * Can't happen: if subject was a root, we'd already have returned.
657 * If chain doesn't verify to a root, we'd have bailed after
658 * SecTrustEvaluate().
660 printf("***sslCompleteCertChain screwup: numResCerts %d\n",
662 ortn
= errSecSuccess
;
666 /* skip the last (root) cert) */
669 for(CFIndex dex
=1; dex
<numResCerts
; dex
++) {
670 certRef
= (SecCertificateRef
)CFArrayGetValueAtIndex(certChain
, dex
);
671 CFArrayAppendValue(certArray
, certRef
);
679 CFRelease(subjCerts
);
685 CFRelease(policySearch
);
687 *outArray
= certArray
;
693 * Given an open keychain, find a SecIdentityRef and munge it into
694 * a CFArrayRef required by SSLSetCertificate().
696 CFArrayRef
sslKcRefToCertArray(
697 SecKeychainRef kcRef
,
699 bool completeCertChain
,
700 const char *trustedAnchorFile
)
702 /* quick check to make sure the keychain exists */
703 SecKeychainStatus kcStat
;
704 OSStatus ortn
= SecKeychainGetStatus(kcRef
, &kcStat
);
706 printSslErrStr("SecKeychainGetStatus", ortn
);
707 printf("Can not open keychain. Aborting.\n");
712 * Search for "any" identity matching specified key use;
713 * in this app, we expect there to be exactly one.
715 SecIdentitySearchRef srchRef
= nil
;
716 ortn
= SecIdentitySearchCreate(kcRef
,
717 encryptOnly
? CSSM_KEYUSE_DECRYPT
: CSSM_KEYUSE_SIGN
,
720 printf("SecIdentitySearchCreate returned %d.\n", (int)ortn
);
721 printf("Cannot find signing key in keychain. Aborting.\n");
724 SecIdentityRef identity
= nil
;
725 ortn
= SecIdentitySearchCopyNext(srchRef
, &identity
);
727 printf("SecIdentitySearchCopyNext returned %d.\n", (int)ortn
);
728 printf("Cannot find signing key in keychain. Aborting.\n");
731 if(CFGetTypeID(identity
) != SecIdentityGetTypeID()) {
732 printf("SecIdentitySearchCopyNext CFTypeID failure!\n");
739 if(completeCertChain
) {
741 * Place it and the other certs needed to verify it -
742 * up to but not including the root - in a CFArray.
744 SecCertificateRef anchorCert
= NULL
;
745 if(trustedAnchorFile
) {
746 ortn
= sslReadAnchor(trustedAnchorFile
, &anchorCert
);
748 printf("***Error reading anchor file\n");
752 ortn
= sslCompleteCertChain(identity
, anchorCert
, false, &ca
);
754 CFRelease(anchorCert
);
759 /* simple case, just this one identity */
760 CFArrayRef ca
= CFArrayCreate(NULL
,
761 (const void **)&identity
,
765 printf("CFArrayCreate error\n");
772 OSStatus
addTrustedSecCert(
774 SecCertificateRef secCert
,
778 CFMutableArrayRef array
;
780 if(secCert
== NULL
) {
781 printf("***addTrustedSecCert screwup\n");
784 array
= CFArrayCreateMutable(kCFAllocatorDefault
,
785 (CFIndex
)1, &kCFTypeArrayCallBacks
);
787 return errSecAllocate
;
789 CFArrayAppendValue(array
, secCert
);
790 ortn
= SSLSetTrustedRoots(ctx
, array
, replaceAnchors
? true : false);
792 printSslErrStr("SSLSetTrustedRoots", ortn
);
798 OSStatus
sslReadAnchor(
799 const char *anchorFile
,
800 SecCertificateRef
*certRef
)
802 SecCertificateRef secCert
;
803 unsigned char *certData
;
807 if(readFile(anchorFile
, &certData
, &certLen
)) {
810 dataRef
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
,
811 (const UInt8
*)certData
, (CFIndex
)certLen
, kCFAllocatorNull
);
812 secCert
= SecCertificateCreateWithData(kCFAllocatorDefault
, dataRef
);
813 CFReleaseSafe(dataRef
);
816 printf("***SecCertificateCreateWithData returned NULL\n");
822 return errSecSuccess
;
825 OSStatus
sslAddTrustedRoot(
827 const char *anchorFile
,
833 OSStatus
addIdentityAsTrustedRoot(
835 CFArrayRef identArray
)
837 return errSecSuccess
;
841 * Lists of SSLCipherSuites used in sslSetCipherRestrictions. Note that the
842 * SecureTransport library does not implement all of these; we only specify
843 * the ones it claims to support.
845 const SSLCipherSuite suites40
[] = {
846 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
847 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
848 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
849 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
850 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
851 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
852 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
853 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
854 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
855 SSL_NO_SUCH_CIPHERSUITE
857 const SSLCipherSuite suitesDES
[] = {
858 SSL_RSA_WITH_DES_CBC_SHA
,
859 SSL_DH_DSS_WITH_DES_CBC_SHA
,
860 SSL_DH_RSA_WITH_DES_CBC_SHA
,
861 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
862 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
863 SSL_DH_anon_WITH_DES_CBC_SHA
,
864 SSL_RSA_WITH_DES_CBC_MD5
,
865 SSL_NO_SUCH_CIPHERSUITE
867 const SSLCipherSuite suitesDES40
[] = {
868 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
869 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
870 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
871 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
872 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
873 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
874 SSL_NO_SUCH_CIPHERSUITE
876 const SSLCipherSuite suites3DES
[] = {
877 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
878 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
879 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
880 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
881 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
882 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
883 SSL_RSA_WITH_3DES_EDE_CBC_MD5
,
884 SSL_NO_SUCH_CIPHERSUITE
886 const SSLCipherSuite suitesRC4
[] = {
887 SSL_RSA_WITH_RC4_128_MD5
,
888 SSL_RSA_WITH_RC4_128_SHA
,
889 SSL_DH_anon_WITH_RC4_128_MD5
,
890 SSL_NO_SUCH_CIPHERSUITE
892 const SSLCipherSuite suitesRC4_40
[] = {
893 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
894 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
895 SSL_NO_SUCH_CIPHERSUITE
897 const SSLCipherSuite suitesRC2
[] = {
898 SSL_RSA_WITH_RC2_CBC_MD5
,
899 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
900 SSL_NO_SUCH_CIPHERSUITE
902 const SSLCipherSuite suitesAES128
[] = {
903 TLS_RSA_WITH_AES_128_CBC_SHA
,
904 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
905 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
906 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
907 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
908 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
909 SSL_NO_SUCH_CIPHERSUITE
911 const SSLCipherSuite suitesAES256
[] = {
912 TLS_RSA_WITH_AES_256_CBC_SHA
,
913 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
914 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
915 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
916 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
917 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
918 SSL_NO_SUCH_CIPHERSUITE
920 const SSLCipherSuite suitesDH
[] = {
921 SSL_DH_DSS_WITH_DES_CBC_SHA
,
922 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
923 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
924 SSL_DH_RSA_WITH_DES_CBC_SHA
,
925 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
926 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
927 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
928 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
929 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
930 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
931 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
932 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
933 SSL_DH_anon_WITH_RC4_128_MD5
,
934 SSL_DH_anon_WITH_DES_CBC_SHA
,
935 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
936 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
937 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
938 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
939 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
940 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
941 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
942 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
943 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
944 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
945 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
946 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
947 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
948 SSL_NO_SUCH_CIPHERSUITE
950 const SSLCipherSuite suitesDHAnon
[] = {
951 SSL_DH_anon_WITH_RC4_128_MD5
,
952 SSL_DH_anon_WITH_DES_CBC_SHA
,
953 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
954 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
955 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
956 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
957 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
958 SSL_NO_SUCH_CIPHERSUITE
960 const SSLCipherSuite suitesDH_RSA
[] = {
961 SSL_DH_RSA_WITH_DES_CBC_SHA
,
962 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
963 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
964 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
965 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
966 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
967 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
968 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
969 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
970 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
971 SSL_NO_SUCH_CIPHERSUITE
973 const SSLCipherSuite suitesDH_DSS
[] = {
974 SSL_DH_DSS_WITH_DES_CBC_SHA
,
975 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
976 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
977 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
978 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
979 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
980 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
981 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
982 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
983 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
984 SSL_NO_SUCH_CIPHERSUITE
986 const SSLCipherSuite suites_SHA1
[] = {
987 SSL_RSA_WITH_RC4_128_SHA
,
988 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
989 SSL_RSA_WITH_IDEA_CBC_SHA
,
990 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
991 SSL_RSA_WITH_DES_CBC_SHA
,
992 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
993 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
994 SSL_DH_DSS_WITH_DES_CBC_SHA
,
995 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
996 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
997 SSL_DH_RSA_WITH_DES_CBC_SHA
,
998 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
999 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
1000 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
1001 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
1002 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
1003 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
1004 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
1005 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
1006 SSL_DH_anon_WITH_DES_CBC_SHA
,
1007 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
1008 SSL_FORTEZZA_DMS_WITH_NULL_SHA
,
1009 SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA
,
1010 TLS_RSA_WITH_AES_128_CBC_SHA
,
1011 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
1012 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
1013 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
1014 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
1015 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
1016 TLS_RSA_WITH_AES_256_CBC_SHA
,
1017 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
1018 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
1019 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
1020 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
1021 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
1022 SSL_NO_SUCH_CIPHERSUITE
1024 const SSLCipherSuite suites_MD5
[] = {
1025 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
1026 SSL_RSA_WITH_RC4_128_MD5
,
1027 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
1028 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
1029 SSL_DH_anon_WITH_RC4_128_MD5
,
1030 SSL_NO_SUCH_CIPHERSUITE
1032 const SSLCipherSuite suites_NULL
[] = {
1033 SSL_RSA_WITH_NULL_MD5
,
1034 SSL_NO_SUCH_CIPHERSUITE
1037 const SSLCipherSuite suites_ECDHE
[] = {
1038 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
,
1039 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
,
1040 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
,
1041 TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
,
1042 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
,
1043 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
,
1044 TLS_ECDHE_RSA_WITH_RC4_128_SHA
,
1045 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
,
1046 SSL_NO_SUCH_CIPHERSUITE
1049 const SSLCipherSuite suites_ECDH
[] = {
1050 TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
,
1051 TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
,
1052 TLS_ECDH_ECDSA_WITH_RC4_128_SHA
,
1053 TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
,
1054 TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
,
1055 TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
,
1056 TLS_ECDH_RSA_WITH_RC4_128_SHA
,
1057 TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
,
1058 SSL_NO_SUCH_CIPHERSUITE
1062 * Given an SSLContextRef and an array of SSLCipherSuites, terminated by
1063 * SSL_NO_SUCH_CIPHERSUITE, select those SSLCipherSuites which the library
1064 * supports and do a SSLSetEnabledCiphers() specifying those.
1066 OSStatus
sslSetEnabledCiphers(
1068 const SSLCipherSuite
*ciphers
)
1070 size_t numSupported
;
1072 SSLCipherSuite
*supported
= NULL
;
1073 SSLCipherSuite
*enabled
= NULL
;
1074 unsigned enabledDex
= 0; // index into enabled
1075 unsigned supportedDex
= 0; // index into supported
1076 unsigned inDex
= 0; // index into ciphers
1078 /* first get all the supported ciphers */
1079 ortn
= SSLGetNumberSupportedCiphers(ctx
, &numSupported
);
1081 printSslErrStr("SSLGetNumberSupportedCiphers", ortn
);
1084 supported
= (SSLCipherSuite
*)malloc(numSupported
* sizeof(SSLCipherSuite
));
1085 ortn
= SSLGetSupportedCiphers(ctx
, supported
, &numSupported
);
1087 printSslErrStr("SSLGetSupportedCiphers", ortn
);
1092 * Malloc an array we'll use for SSLGetEnabledCiphers - this will be
1093 * bigger than the number of suites we actually specify
1095 enabled
= (SSLCipherSuite
*)malloc(numSupported
* sizeof(SSLCipherSuite
));
1098 * For each valid suite in ciphers, see if it's in the list of
1099 * supported ciphers. If it is, add it to the list of ciphers to be
1102 for(inDex
=0; ciphers
[inDex
] != SSL_NO_SUCH_CIPHERSUITE
; inDex
++) {
1103 for(supportedDex
=0; supportedDex
<numSupported
; supportedDex
++) {
1104 if(ciphers
[inDex
] == supported
[supportedDex
]) {
1105 enabled
[enabledDex
++] = ciphers
[inDex
];
1111 /* send it on down. */
1112 ortn
= SSLSetEnabledCiphers(ctx
, enabled
, enabledDex
);
1114 printSslErrStr("SSLSetEnabledCiphers", ortn
);
1122 * Specify a restricted set of cipherspecs.
1124 OSStatus
sslSetCipherRestrictions(
1126 char cipherRestrict
)
1130 if(cipherRestrict
== '\0') {
1131 return errSecSuccess
; // actually should not have been called
1133 switch(cipherRestrict
) {
1135 ortn
= sslSetEnabledCiphers(ctx
, suites40
);
1138 ortn
= sslSetEnabledCiphers(ctx
, suitesDES
);
1141 ortn
= sslSetEnabledCiphers(ctx
, suitesDES40
);
1144 ortn
= sslSetEnabledCiphers(ctx
, suites3DES
);
1147 ortn
= sslSetEnabledCiphers(ctx
, suitesRC4
);
1150 ortn
= sslSetEnabledCiphers(ctx
, suitesRC4_40
);
1153 ortn
= sslSetEnabledCiphers(ctx
, suitesRC2
);
1156 ortn
= sslSetEnabledCiphers(ctx
, suitesAES128
);
1159 ortn
= sslSetEnabledCiphers(ctx
, suitesAES256
);
1162 ortn
= sslSetEnabledCiphers(ctx
, suitesDH
);
1165 ortn
= sslSetEnabledCiphers(ctx
, suitesDHAnon
);
1168 ortn
= sslSetEnabledCiphers(ctx
, suitesDH_RSA
);
1171 ortn
= sslSetEnabledCiphers(ctx
, suitesDH_DSS
);
1174 ortn
= sslSetEnabledCiphers(ctx
, suites_NULL
);
1177 ortn
= sslSetEnabledCiphers(ctx
, suites_ECDHE
);
1180 ortn
= sslSetEnabledCiphers(ctx
, suites_ECDH
);
1183 printf("***bad cipherSpec***\n");
1190 int sslVerifyClientCertState(
1191 const char *whichSide
, // "client" or "server"
1192 SSLClientCertificateState expectState
,
1193 SSLClientCertificateState gotState
)
1195 if(expectState
== SSL_CLIENT_CERT_IGNORE
) {
1196 /* app says "don't bother checking" */
1199 if(expectState
== gotState
) {
1202 printf("***%s: Expected clientCertState %s; got %s\n", whichSide
,
1203 sslGetClientCertStateString(expectState
),
1204 sslGetClientCertStateString(gotState
));
1209 char *whichSide
, // "client" or "server"
1213 if(expectRtn
== gotRtn
) {
1216 printf("***%s: Expected return %s; got %s\n", whichSide
,
1217 sslGetSSLErrString(expectRtn
),
1218 sslGetSSLErrString(gotRtn
));
1222 int sslVerifyProtVers(
1223 char *whichSide
, // "client" or "server"
1224 SSLProtocol expectProt
,
1225 SSLProtocol gotProt
)
1227 if(expectProt
== SSL_PROTOCOL_IGNORE
) {
1228 /* app says "don't bopther checking" */
1231 if(expectProt
== gotProt
) {
1234 printf("***%s: Expected return %s; got %s\n", whichSide
,
1235 sslGetProtocolVersionString(expectProt
),
1236 sslGetProtocolVersionString(gotProt
));
1240 int sslVerifyCipher(
1241 char *whichSide
, // "client" or "server"
1242 SSLCipherSuite expectCipher
,
1243 SSLCipherSuite gotCipher
)
1245 if(expectCipher
== SSL_CIPHER_IGNORE
) {
1246 /* app says "don't bopther checking" */
1249 if(expectCipher
== gotCipher
) {
1252 printf("***%s: Expected return %s; got %s\n", whichSide
,
1253 sslGetCipherSuiteString(expectCipher
),
1254 sslGetCipherSuiteString(gotCipher
));
1259 OSStatus
sslSetProtocols(
1261 const char *acceptedProts
,
1262 SSLProtocol tryVersion
) // only used if acceptedProts NULL
1267 ortn
= SSLSetProtocolVersionEnabled(ctx
, kSSLProtocolAll
, false);
1269 printSslErrStr("SSLSetProtocolVersionEnabled(all off)", ortn
);
1272 for(const char *cp
= acceptedProts
; *cp
; cp
++) {
1276 prot
= kSSLProtocol2
;
1279 prot
= kSSLProtocol3
;
1282 prot
= kTLSProtocol1
;
1285 printf("***BRRZAP! Bad acceptedProts string %s. Aborting.\n", acceptedProts
);
1288 ortn
= SSLSetProtocolVersionEnabled(ctx
, prot
, true);
1290 printSslErrStr("SSLSetProtocolVersionEnabled", ortn
);
1296 ortn
= SSLSetProtocolVersion(ctx
, tryVersion
);
1298 printSslErrStr("SSLSetProtocolVersion", ortn
);
1302 return errSecSuccess
;
1306 const char *whichSide
, // "client" or "server"
1307 SslAppTestParams
*params
)
1309 printf("%s status:\n", whichSide
);
1310 if(params
->acceptedProts
) {
1311 printf(" Allowed SSL versions : %s\n", params
->acceptedProts
);
1314 printf(" Attempted SSL version : %s\n",
1315 sslGetProtocolVersionString(params
->tryVersion
));
1317 printf(" Result : %s\n", sslGetSSLErrString(params
->ortn
));
1318 printf(" Negotiated SSL version : %s\n",
1319 sslGetProtocolVersionString(params
->negVersion
));
1320 printf(" Negotiated CipherSuite : %s\n",
1321 sslGetCipherSuiteString(params
->negCipher
));
1322 if(params
->certState
!= kSSLClientCertNone
) {
1323 printf(" Client Cert State : %s\n",
1324 sslGetClientCertStateString(params
->certState
));
1329 /* print a '.' every few seconds to keep UI alive while connecting */
1330 static CFAbsoluteTime lastTime
= (CFAbsoluteTime
)0.0;
1331 #define TIME_INTERVAL 3.0
1335 CFAbsoluteTime thisTime
= CFAbsoluteTimeGetCurrent();
1340 if(lastTime
== 0.0) {
1341 /* avoid printing first time thru */
1342 lastTime
= thisTime
;
1345 if((thisTime
- lastTime
) >= TIME_INTERVAL
) {
1346 printf("."); fflush(stdout
);
1347 lastTime
= thisTime
;
1352 /* main server pthread body */
1353 static void *sslServerThread(void *arg
)
1355 SslAppTestParams
*testParams
= (SslAppTestParams
*)arg
;
1358 status
= sslAppServe(testParams
);
1359 pthread_exit((void*)status
);
1361 return (void *)status
;
1365 * Run one session, with the server in a separate thread.
1366 * On entry, serverParams->port is the port we attempt to run on;
1367 * the server thread may overwrite that with a different port if it's
1368 * unable to open the port we specify. Whatever is left in
1369 * serverParams->port is what's used for the client side.
1371 #define CLIENT_WAIT_SECONDS 1
1373 SslAppTestParams
*serverParams
,
1374 SslAppTestParams
*clientParams
,
1375 const char *testDesc
)
1377 pthread_t serverPthread
;
1381 if(testDesc
&& !clientParams
->quiet
) {
1382 printf("===== %s =====\n", testDesc
);
1385 if(pthread_mutex_init(&serverParams
->pthreadMutex
, NULL
)) {
1386 printf("***Error initializing mutex; aborting.\n");
1389 if(pthread_cond_init(&serverParams
->pthreadCond
, NULL
)) {
1390 printf("***Error initializing pthreadCond; aborting.\n");
1393 serverParams
->serverReady
= false; // server sets true
1395 int result
= pthread_create(&serverPthread
, NULL
,
1396 sslServerThread
, serverParams
);
1398 printf("***Error starting up server thread; aborting.\n");
1402 /* wait for server to set up a socket we can connect to */
1403 if(pthread_mutex_lock(&serverParams
->pthreadMutex
)) {
1404 printf("***Error acquiring server lock; aborting.\n");
1407 while(!serverParams
->serverReady
) {
1408 if(pthread_cond_wait(&serverParams
->pthreadCond
, &serverParams
->pthreadMutex
)) {
1409 printf("***Error waiting server thread; aborting.\n");
1413 pthread_mutex_unlock(&serverParams
->pthreadMutex
);
1414 pthread_cond_destroy(&serverParams
->pthreadCond
);
1415 pthread_mutex_destroy(&serverParams
->pthreadMutex
);
1417 clientParams
->port
= serverParams
->port
;
1418 clientRtn
= sslAppClient(clientParams
);
1419 /* server doesn't shut down its socket until it sees this */
1420 serverParams
->clientDone
= 1;
1421 result
= pthread_join(serverPthread
, &serverRtn
);
1423 printf("***pthread_join returned %d, aborting\n", result
);
1427 if(serverParams
->verbose
) {
1428 sslShowResult("server", serverParams
);
1430 if(clientParams
->verbose
) {
1431 sslShowResult("client", clientParams
);
1434 /* verify results */
1436 ourRtn
+= sslVerifyRtn("server", serverParams
->expectRtn
, serverParams
->ortn
);
1437 ourRtn
+= sslVerifyRtn("client", clientParams
->expectRtn
, clientParams
->ortn
);
1438 ourRtn
+= sslVerifyProtVers("server", serverParams
->expectVersion
,
1439 serverParams
->negVersion
);
1440 ourRtn
+= sslVerifyProtVers("client", clientParams
->expectVersion
,
1441 clientParams
->negVersion
);
1442 ourRtn
+= sslVerifyClientCertState("server", serverParams
->expectCertState
,
1443 serverParams
->certState
);
1444 ourRtn
+= sslVerifyClientCertState("client", clientParams
->expectCertState
,
1445 clientParams
->certState
);
1446 if(serverParams
->ortn
== errSecSuccess
) {
1447 ourRtn
+= sslVerifyCipher("server", serverParams
->expectCipher
,
1448 serverParams
->negCipher
);
1450 if(clientParams
->ortn
== errSecSuccess
) {
1451 ourRtn
+= sslVerifyCipher("client", clientParams
->expectCipher
,
1452 clientParams
->negCipher
);
1458 * Add all of the roots in a given KC to SSL ctx's trusted anchors.
1460 OSStatus
sslAddTrustedRoots(
1462 SecKeychainRef keychain
,
1463 bool *foundOne
) // RETURNED, true if we found
1464 // at least one root cert
1467 SecCertificateRef secCert
;
1468 SecKeychainSearchRef srch
;
1471 ortn
= SecKeychainSearchCreateFromAttributes(keychain
,
1472 kSecCertificateItemClass
,
1476 printSslErrStr("SecKeychainSearchCreateFromAttributes", ortn
);
1481 * Only use root certs. Not an error if we don't find any.
1484 ortn
= SecKeychainSearchCopyNext(srch
,
1485 (SecKeychainItemRef
*)&secCert
);
1490 /* see if it's a root */
1491 if(!isCertRoot(secCert
)) {
1495 /* Tell Secure Transport to trust this one. */
1496 ortn
= addTrustedSecCert(ctx
, secCert
, false);
1499 printSslErrStr("addTrustedSecCert", ortn
);
1504 } while(ortn
== errSecSuccess
);
1506 return errSecSuccess
;
1510 * Wrapper for sslIdentPicker, with optional trusted anchor specified as a filename.
1512 OSStatus
sslIdentityPicker(
1513 SecKeychainRef kcRef
, // NULL means use default list
1514 const char *trustedAnchor
, // optional additional trusted anchor
1515 bool includeRoot
, // true --> root is appended to outArray
1516 // false --> root not included
1517 CFArrayRef
*outArray
) // created and RETURNED
1519 SecCertificateRef trustedCert
= NULL
;
1523 ortn
= sslReadAnchor(trustedAnchor
, &trustedCert
);
1525 printf("***Error reading %s. sslIdentityPicker proceeding with no anchor.\n",
1530 ortn
= sslIdentPicker(kcRef
, trustedCert
, includeRoot
, outArray
);
1532 CFRelease(trustedCert
);
1538 * Given a keychain name, convert it into a full path using the "SSL regression
1539 * test suite algorithm". The Sec layer by default locates root root's keychains
1540 * in different places depending on whether we're actually logged in as root
1541 * or running via e.g. cron, so we force the location of root keychains to
1542 * a hard-coded path. User keychain names we leave alone.
1544 void sslKeychainPath(
1546 char *kcPath
) // allocd by caller, MAXPATHLEN
1548 if(kcName
[0] == '\0') {
1551 else if(geteuid() == 0) {
1553 sprintf(kcPath
, "/Library/Keychains/%s", kcName
);
1556 /* user, leave alone */
1557 strcpy(kcPath
, kcName
);
1561 /* Verify presence of required file. Returns nonzero if not found. */
1562 int sslCheckFile(const char *path
)
1566 if(stat(path
, &sb
)) {
1567 printf("***Can't find file %s.\n", path
);
1568 printf(" Try running in the build directory, perhaps after running the\n"
1569 " makeLocalCert script.\n");
1577 /* Stringify a SSL_ECDSA_NamedCurve */
1578 extern const char *sslCurveString(
1579 SSL_ECDSA_NamedCurve namedCurve
)
1581 static char unk
[100];
1583 switch(namedCurve
) {
1584 case SSL_Curve_None
: return "Curve_None";
1585 case SSL_Curve_secp256r1
: return "secp256r1";
1586 case SSL_Curve_secp384r1
: return "secp384r1";
1587 case SSL_Curve_secp521r1
: return "secp521r1";
1589 sprintf(unk
, "Unknown <%d>", (int)namedCurve
);