2 * Copyright (c) 2006-2008,2010-2013 Apple Inc. All Rights Reserved.
5 #include "sslAppUtils.h"
6 //#include "sslThreading.h"
7 //#include "identPicker.h"
8 //#include <utilLib/common.h>
11 #include <sys/param.h>
12 #include <Security/SecBase.h>
14 #include <CoreFoundation/CoreFoundation.h>
15 #include <Security/Security.h>
16 #include <Security/SecIdentityPriv.h>
17 #include <AssertMacros.h>
20 #include "utilities/SecCFRelease.h"
22 /* Set true when PR-3074739 is merged to TOT */
23 #define NEW_SSL_ERRS_3074739 1
26 const char *sslGetCipherSuiteString(SSLCipherSuite cs
)
28 static char noSuite
[40];
31 /* TLS cipher suites, RFC 2246 */
32 case SSL_NULL_WITH_NULL_NULL
: return "TLS_NULL_WITH_NULL_NULL";
33 case SSL_RSA_WITH_NULL_MD5
: return "TLS_RSA_WITH_NULL_MD5";
34 case SSL_RSA_WITH_NULL_SHA
: return "TLS_RSA_WITH_NULL_SHA";
35 case SSL_RSA_EXPORT_WITH_RC4_40_MD5
: return "TLS_RSA_EXPORT_WITH_RC4_40_MD5";
36 case SSL_RSA_WITH_RC4_128_MD5
: return "TLS_RSA_WITH_RC4_128_MD5";
37 case SSL_RSA_WITH_RC4_128_SHA
: return "TLS_RSA_WITH_RC4_128_SHA";
38 case SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
: return "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5";
39 case SSL_RSA_WITH_IDEA_CBC_SHA
: return "TLS_RSA_WITH_IDEA_CBC_SHA";
40 case SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA";
41 case SSL_RSA_WITH_DES_CBC_SHA
: return "TLS_RSA_WITH_DES_CBC_SHA";
42 case SSL_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_RSA_WITH_3DES_EDE_CBC_SHA";
43 case SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA";
44 case SSL_DH_DSS_WITH_DES_CBC_SHA
: return "TLS_DH_DSS_WITH_DES_CBC_SHA";
45 case SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
: return "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA";
46 case SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA";
47 case SSL_DH_RSA_WITH_DES_CBC_SHA
: return "TLS_DH_RSA_WITH_DES_CBC_SHA";
48 case SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA";
49 case SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA";
50 case SSL_DHE_DSS_WITH_DES_CBC_SHA
: return "TLS_DHE_DSS_WITH_DES_CBC_SHA";
51 case SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
: return "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA";
52 case SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA";
53 case SSL_DHE_RSA_WITH_DES_CBC_SHA
: return "TLS_DHE_RSA_WITH_DES_CBC_SHA";
54 case SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA";
55 case SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
: return "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5";
56 case SSL_DH_anon_WITH_RC4_128_MD5
: return "TLS_DH_anon_WITH_RC4_128_MD5";
57 case SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
: return "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA";
58 case SSL_DH_anon_WITH_DES_CBC_SHA
: return "TLS_DH_anon_WITH_DES_CBC_SHA";
59 case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
: return "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA";
61 /* SSLv3 Fortezza cipher suites, from NSS */
62 case SSL_FORTEZZA_DMS_WITH_NULL_SHA
: return "SSL_FORTEZZA_DMS_WITH_NULL_SHA";
63 case SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA
:return "SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA";
65 /* TLS addenda using AES-CBC, RFC 3268 */
66 case TLS_RSA_WITH_AES_128_CBC_SHA
: return "TLS_RSA_WITH_AES_128_CBC_SHA";
67 case TLS_DH_DSS_WITH_AES_128_CBC_SHA
: return "TLS_DH_DSS_WITH_AES_128_CBC_SHA";
68 case TLS_DH_RSA_WITH_AES_128_CBC_SHA
: return "TLS_DH_RSA_WITH_AES_128_CBC_SHA";
69 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA
: return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA";
70 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA
: return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
71 case TLS_DH_anon_WITH_AES_128_CBC_SHA
: return "TLS_DH_anon_WITH_AES_128_CBC_SHA";
72 case TLS_RSA_WITH_AES_256_CBC_SHA
: return "TLS_RSA_WITH_AES_256_CBC_SHA";
73 case TLS_DH_DSS_WITH_AES_256_CBC_SHA
: return "TLS_DH_DSS_WITH_AES_256_CBC_SHA";
74 case TLS_DH_RSA_WITH_AES_256_CBC_SHA
: return "TLS_DH_RSA_WITH_AES_256_CBC_SHA";
75 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA
: return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA";
76 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA
: return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
77 case TLS_DH_anon_WITH_AES_256_CBC_SHA
: return "TLS_DH_anon_WITH_AES_256_CBC_SHA";
79 /* ECDSA addenda, RFC 4492 */
80 case TLS_ECDH_ECDSA_WITH_NULL_SHA
: return "TLS_ECDH_ECDSA_WITH_NULL_SHA";
81 case TLS_ECDH_ECDSA_WITH_RC4_128_SHA
: return "TLS_ECDH_ECDSA_WITH_RC4_128_SHA";
82 case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA";
83 case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
: return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA";
84 case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
: return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA";
85 case TLS_ECDHE_ECDSA_WITH_NULL_SHA
: return "TLS_ECDHE_ECDSA_WITH_NULL_SHA";
86 case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
: return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA";
87 case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA";
88 case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
: return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA";
89 case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
: return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA";
90 case TLS_ECDH_RSA_WITH_NULL_SHA
: return "TLS_ECDH_RSA_WITH_NULL_SHA";
91 case TLS_ECDH_RSA_WITH_RC4_128_SHA
: return "TLS_ECDH_RSA_WITH_RC4_128_SHA";
92 case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA";
93 case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
: return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA";
94 case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
: return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA";
95 case TLS_ECDHE_RSA_WITH_NULL_SHA
: return "TLS_ECDHE_RSA_WITH_NULL_SHA";
96 case TLS_ECDHE_RSA_WITH_RC4_128_SHA
: return "TLS_ECDHE_RSA_WITH_RC4_128_SHA";
97 case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA";
98 case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
: return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA";
99 case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
: return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA";
100 case TLS_ECDH_anon_WITH_NULL_SHA
: return "TLS_ECDH_anon_WITH_NULL_SHA";
101 case TLS_ECDH_anon_WITH_RC4_128_SHA
: return "TLS_ECDH_anon_WITH_RC4_128_SHA";
102 case TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA
: return "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA";
103 case TLS_ECDH_anon_WITH_AES_128_CBC_SHA
: return "TLS_ECDH_anon_WITH_AES_128_CBC_SHA";
104 case TLS_ECDH_anon_WITH_AES_256_CBC_SHA
: return "TLS_ECDH_anon_WITH_AES_256_CBC_SHA";
106 /* TLS 1.2 addenda, RFC 5246 */
107 case TLS_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_RSA_WITH_AES_128_CBC_SHA256";
108 case TLS_RSA_WITH_AES_256_CBC_SHA256
: return "TLS_RSA_WITH_AES_256_CBC_SHA256";
109 case TLS_DH_DSS_WITH_AES_128_CBC_SHA256
: return "TLS_DH_DSS_WITH_AES_128_CBC_SHA256";
110 case TLS_DH_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_DH_RSA_WITH_AES_128_CBC_SHA256";
111 case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
: return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256";
112 case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256";
113 case TLS_DH_DSS_WITH_AES_256_CBC_SHA256
: return "TLS_DH_DSS_WITH_AES_256_CBC_SHA256";
114 case TLS_DH_RSA_WITH_AES_256_CBC_SHA256
: return "TLS_DH_RSA_WITH_AES_256_CBC_SHA256";
115 case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
: return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256";
116 case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
: return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256";
117 case TLS_DH_anon_WITH_AES_128_CBC_SHA256
: return "TLS_DH_anon_WITH_AES_128_CBC_SHA256";
118 case TLS_DH_anon_WITH_AES_256_CBC_SHA256
: return "TLS_DH_anon_WITH_AES_256_CBC_SHA256";
120 /* TLS addenda using AES-GCM, RFC 5288 */
121 case TLS_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_RSA_WITH_AES_128_GCM_SHA256";
122 case TLS_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
123 case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
124 case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384";
125 case TLS_DH_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_DH_RSA_WITH_AES_128_GCM_SHA256";
126 case TLS_DH_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_DH_RSA_WITH_AES_256_GCM_SHA384";
127 case TLS_DHE_DSS_WITH_AES_128_GCM_SHA256
: return "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256";
128 case TLS_DHE_DSS_WITH_AES_256_GCM_SHA384
: return "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384";
129 case TLS_DH_DSS_WITH_AES_128_GCM_SHA256
: return "TLS_DH_DSS_WITH_AES_128_GCM_SHA256";
130 case TLS_DH_DSS_WITH_AES_256_GCM_SHA384
: return "TLS_DH_DSS_WITH_AES_256_GCM_SHA384";
131 case TLS_DH_anon_WITH_AES_128_GCM_SHA256
: return "TLS_DH_anon_WITH_AES_128_GCM_SHA256";
132 case TLS_DH_anon_WITH_AES_256_GCM_SHA384
: return "TLS_DH_anon_WITH_AES_256_GCM_SHA384";
134 /* ECDSA addenda, RFC 5289 */
135 case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
: return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256";
136 case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
: return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384";
137 case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
: return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256";
138 case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
: return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384";
139 case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256";
140 case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
: return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384";
141 case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
: return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256";
142 case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
: return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384";
143 case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
: return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
144 case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
: return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384";
145 case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
: return "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256";
146 case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
: return "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384";
147 case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
148 case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
149 case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
: return "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256";
150 case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
: return "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384";
153 * Tags for SSL 2 cipher kinds which are not specified for SSL 3.
155 case SSL_RSA_WITH_RC2_CBC_MD5
: return "TLS_RSA_WITH_RC2_CBC_MD5";
156 case SSL_RSA_WITH_IDEA_CBC_MD5
: return "TLS_RSA_WITH_IDEA_CBC_MD5";
157 case SSL_RSA_WITH_DES_CBC_MD5
: return "TLS_RSA_WITH_DES_CBC_MD5";
158 case SSL_RSA_WITH_3DES_EDE_CBC_MD5
: return "TLS_RSA_WITH_3DES_EDE_CBC_MD5";
159 case SSL_NO_SUCH_CIPHERSUITE
: return "SSL_NO_SUCH_CIPHERSUITE";
162 snprintf(noSuite
, sizeof(noSuite
), "Unknown ciphersuite 0x%04x", (unsigned)cs
);
168 * Given a SSLProtocolVersion - typically from SSLGetProtocolVersion -
169 * return a string representation.
171 const char *sslGetProtocolVersionString(SSLProtocol prot
)
173 static char noProt
[20];
176 case kSSLProtocolUnknown
:
177 return "kSSLProtocolUnknown";
179 return "kSSLProtocol2";
181 return "kSSLProtocol3";
182 case kSSLProtocol3Only
:
183 return "kSSLProtocol3Only";
185 return "kTLSProtocol1";
186 case kTLSProtocol1Only
:
187 return "kTLSProtocol1Only";
189 return "kTLSProtocol11";
191 return "kTLSProtocol12";
193 sprintf(noProt
, "Unknown (%d)", (unsigned)prot
);
199 * Return string representation of SecureTransport-related OSStatus.
201 const char *sslGetSSLErrString(OSStatus err
)
203 static char errSecSuccessStr
[20];
207 return "errSecSuccess";
209 return "errSecAllocate";
211 return "errSecParam";
212 case errSecUnimplemented
:
213 return "errSecUnimplemented";
217 return "errSecBadReq";
219 return "errSSLProtocol";
220 case errSSLNegotiation
:
221 return "errSSLNegotiation";
222 case errSSLFatalAlert
:
223 return "errSSLFatalAlert";
224 case errSSLWouldBlock
:
225 return "errSSLWouldBlock";
226 case errSSLSessionNotFound
:
227 return "errSSLSessionNotFound";
228 case errSSLClosedGraceful
:
229 return "errSSLClosedGraceful";
230 case errSSLClosedAbort
:
231 return "errSSLClosedAbort";
232 case errSSLXCertChainInvalid
:
233 return "errSSLXCertChainInvalid";
235 return "errSSLBadCert";
237 return "errSSLCrypto";
239 return "errSSLInternal";
240 case errSSLModuleAttach
:
241 return "errSSLModuleAttach";
242 case errSSLUnknownRootCert
:
243 return "errSSLUnknownRootCert";
244 case errSSLNoRootCert
:
245 return "errSSLNoRootCert";
246 case errSSLCertExpired
:
247 return "errSSLCertExpired";
248 case errSSLCertNotYetValid
:
249 return "errSSLCertNotYetValid";
250 case errSSLClosedNoNotify
:
251 return "errSSLClosedNoNotify";
252 case errSSLBufferOverflow
:
253 return "errSSLBufferOverflow";
254 case errSSLBadCipherSuite
:
255 return "errSSLBadCipherSuite";
256 /* TLS/Panther addenda */
257 case errSSLPeerUnexpectedMsg
:
258 return "errSSLPeerUnexpectedMsg";
259 case errSSLPeerBadRecordMac
:
260 return "errSSLPeerBadRecordMac";
261 case errSSLPeerDecryptionFail
:
262 return "errSSLPeerDecryptionFail";
263 case errSSLPeerRecordOverflow
:
264 return "errSSLPeerRecordOverflow";
265 case errSSLPeerDecompressFail
:
266 return "errSSLPeerDecompressFail";
267 case errSSLPeerHandshakeFail
:
268 return "errSSLPeerHandshakeFail";
269 case errSSLPeerBadCert
:
270 return "errSSLPeerBadCert";
271 case errSSLPeerUnsupportedCert
:
272 return "errSSLPeerUnsupportedCert";
273 case errSSLPeerCertRevoked
:
274 return "errSSLPeerCertRevoked";
275 case errSSLPeerCertExpired
:
276 return "errSSLPeerCertExpired";
277 case errSSLPeerCertUnknown
:
278 return "errSSLPeerCertUnknown";
279 case errSSLIllegalParam
:
280 return "errSSLIllegalParam";
281 case errSSLPeerUnknownCA
:
282 return "errSSLPeerUnknownCA";
283 case errSSLPeerAccessDenied
:
284 return "errSSLPeerAccessDenied";
285 case errSSLPeerDecodeError
:
286 return "errSSLPeerDecodeError";
287 case errSSLPeerDecryptError
:
288 return "errSSLPeerDecryptError";
289 case errSSLPeerExportRestriction
:
290 return "errSSLPeerExportRestriction";
291 case errSSLPeerProtocolVersion
:
292 return "errSSLPeerProtocolVersion";
293 case errSSLPeerInsufficientSecurity
:
294 return "errSSLPeerInsufficientSecurity";
295 case errSSLPeerInternalError
:
296 return "errSSLPeerInternalError";
297 case errSSLPeerUserCancelled
:
298 return "errSSLPeerUserCancelled";
299 case errSSLPeerNoRenegotiation
:
300 return "errSSLPeerNoRenegotiation";
301 case errSSLHostNameMismatch
:
302 return "errSSLHostNameMismatch";
303 case errSSLConnectionRefused
:
304 return "errSSLConnectionRefused";
305 case errSSLDecryptionFail
:
306 return "errSSLDecryptionFail";
307 case errSSLBadRecordMac
:
308 return "errSSLBadRecordMac";
309 case errSSLRecordOverflow
:
310 return "errSSLRecordOverflow";
311 case errSSLBadConfiguration
:
312 return "errSSLBadConfiguration";
314 /* some from the Sec layer */
315 case errSecNotAvailable
: return "errSecNotAvailable";
316 case errSecDuplicateItem
: return "errSecDuplicateItem";
317 case errSecItemNotFound
: return "errSecItemNotFound";
319 case errSessionInvalidId
: return "errSessionInvalidId";
320 case errSessionInvalidAttributes
: return "errSessionInvalidAttributes";
321 case errSessionAuthorizationDenied
: return "errSessionAuthorizationDenied";
322 case errSessionInternal
: return "errSessionInternal";
323 case errSessionInvalidFlags
: return "errSessionInvalidFlags";
328 if (err
< (CSSM_BASE_ERROR
+
329 (CSSM_ERRORCODE_MODULE_EXTENT
* 8)))
331 /* assume CSSM error */
332 return cssmErrToStr(err
);
337 sprintf(errSecSuccessStr
, "Unknown (%d)", (unsigned)err
);
338 return errSecSuccessStr
;
347 printf("*** %s: %s\n", op
, sslGetSSLErrString(err
));
350 const char *sslGetClientCertStateString(SSLClientCertificateState state
)
352 static char noState
[20];
355 case kSSLClientCertNone
:
356 return "ClientCertNone";
357 case kSSLClientCertRequested
:
358 return "CertRequested";
359 case kSSLClientCertSent
:
360 return "ClientCertSent";
361 case kSSLClientCertRejected
:
362 return "ClientCertRejected";
364 sprintf(noState
, "Unknown (%d)", (unsigned)state
);
371 * Convert a keychain name (which may be NULL) into the CFArrayRef required
372 * by SSLSetCertificate. This is a bare-bones example of this operation,
373 * since it requires and assumes that there is exactly one SecIdentity
374 * in the keychain - i.e., there is exactly one matching cert/private key
375 * pair. A real world server would probably search a keychain for a SecIdentity
376 * matching some specific criteria.
378 CFArrayRef
getSslCerts(
379 const char *kcName
, // may be NULL, i.e., use default
381 bool completeCertChain
,
382 const char *anchorFile
, // optional trusted anchor
383 SecKeychainRef
*pKcRef
) // RETURNED
386 SecKeychainRef kcRef
= nil
;
391 /* pick a keychain */
393 ortn
= SecKeychainOpen(kcName
, &kcRef
);
395 printf("SecKeychainOpen returned %d.\n", (int)ortn
);
396 printf("Cannot open keychain at %s. Aborting.\n", kcName
);
401 /* use default keychain */
402 ortn
= SecKeychainCopyDefault(&kcRef
);
404 printf("SecKeychainCopyDefault returned %d; aborting.\n", (int)ortn
);
409 return sslKcRefToCertArray(kcRef
, encryptOnly
, completeCertChain
, anchorFile
);
411 SecCertificateRef cert
= NULL
;
412 SecIdentityRef identity
= NULL
;
413 CFMutableArrayRef certificates
= NULL
, result
= NULL
;
414 CFMutableDictionaryRef certQuery
= NULL
, keyQuery
= NULL
, keyResult
= NULL
;
415 SecTrustRef trust
= NULL
;
416 SecKeyRef key
= NULL
;
417 CFTypeRef pkdigest
= NULL
;
419 // Find the first private key in the keychain and return both it's
420 // attributes and a ref to it.
421 require(keyQuery
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
), errOut
);
422 CFDictionaryAddValue(keyQuery
, kSecClass
, kSecClassKey
);
423 CFDictionaryAddValue(keyQuery
, kSecAttrKeyClass
, kSecAttrKeyClassPrivate
);
424 CFDictionaryAddValue(keyQuery
, kSecReturnRef
, kCFBooleanTrue
);
425 CFDictionaryAddValue(keyQuery
, kSecReturnAttributes
, kCFBooleanTrue
);
426 require_noerr(SecItemCopyMatching(keyQuery
, (CFTypeRef
*)&keyResult
),
428 require(key
= (SecKeyRef
)CFDictionaryGetValue(keyResult
, kSecValueRef
),
430 require(pkdigest
= CFDictionaryGetValue(keyResult
, kSecAttrApplicationLabel
),
433 // Find the first certificate that has the same public key hash as the
434 // returned private key and return it as a ref.
435 require(certQuery
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
), errOut
);
436 CFDictionaryAddValue(certQuery
, kSecClass
, kSecClassCertificate
);
437 CFDictionaryAddValue(certQuery
, kSecAttrPublicKeyHash
, pkdigest
);
438 CFDictionaryAddValue(certQuery
, kSecReturnRef
, kCFBooleanTrue
);
439 require_noerr(SecItemCopyMatching(certQuery
, (CFTypeRef
*)&cert
), errOut
);
441 // Create an identity from the key and certificate.
442 require(identity
= SecIdentityCreate(NULL
, cert
, key
), errOut
);
444 // Build a (partial) certificate chain from cert
445 require(certificates
= CFArrayCreateMutable(NULL
, 0,
446 &kCFTypeArrayCallBacks
), errOut
);
447 CFArrayAppendValue(certificates
, cert
);
448 require_noerr(SecTrustCreateWithCertificates(certificates
, NULL
, &trust
),
450 SecTrustResultType tresult
;
451 require_noerr(SecTrustEvaluate(trust
, &tresult
), errOut
);
453 CFIndex certCount
, ix
;
454 // We need at least 1 certificate
455 require(certCount
= SecTrustGetCertificateCount(trust
), errOut
);
457 // Build a result where element 0 is the identity and the other elements
458 // are the certs in the chain starting at the first intermediate up to the
459 // anchor, if we found one, or as far as we were able to build the chain
461 require(result
= CFArrayCreateMutable(NULL
, certCount
, &kCFTypeArrayCallBacks
),
464 // We are commited to returning a result now, so do not use require below
465 // this line without setting result to NULL again.
466 CFArrayAppendValue(result
, identity
);
467 for (ix
= 1; ix
< certCount
; ++ix
) {
468 CFArrayAppendValue(result
, SecTrustGetCertificateAtIndex(trust
, ix
));
472 CFReleaseSafe(trust
);
473 CFReleaseSafe(certificates
);
474 CFReleaseSafe(identity
);
476 CFReleaseSafe(certQuery
);
477 CFReleaseSafe(keyResult
);
478 CFReleaseSafe(keyQuery
);
486 * Determine if specified SecCertificateRef is a self-signed cert.
487 * We do this by comparing the subject and issuerr names; no cryptographic
488 * verification is performed.
490 * Returns true if the cert appears to be a root.
492 static bool isCertRefRoot(
493 SecCertificateRef certRef
)
497 /* just search for the two attrs we want */
498 UInt32 tags
[2] = {kSecSubjectItemAttr
, kSecIssuerItemAttr
};
499 SecKeychainAttributeInfo attrInfo
;
502 attrInfo
.format
= NULL
;
503 SecKeychainAttributeList
*attrList
= NULL
;
504 SecKeychainAttribute
*attr1
= NULL
;
505 SecKeychainAttribute
*attr2
= NULL
;
507 OSStatus ortn
= SecKeychainItemCopyAttributesAndData(
508 (SecKeychainItemRef
)certRef
,
512 NULL
, // length - don't need the data
515 cssmPerror("SecKeychainItemCopyAttributesAndData", ortn
);
516 /* may want to be a bit more robust here, but this should
520 /* subsequent errors to errOut: */
522 if((attrList
== NULL
) || (attrList
->count
!= 2)) {
523 printf("***Unexpected result fetching label attr\n");
527 /* rootness is just byte-for-byte compare of the two names */
528 attr1
= &attrList
->attr
[0];
529 attr2
= &attrList
->attr
[1];
530 if(attr1
->length
== attr2
->length
) {
531 if(memcmp(attr1
->data
, attr2
->data
, attr1
->length
) == 0) {
536 SecKeychainItemFreeAttributesAndData(attrList
, NULL
);
544 * Given a SecIdentityRef, do our best to construct a complete, ordered, and
545 * verified cert chain, returning the result in a CFArrayRef. The result is
546 * suitable for use when calling SSLSetCertificate().
548 OSStatus
sslCompleteCertChain(
549 SecIdentityRef identity
,
550 SecCertificateRef trustedAnchor
, // optional additional trusted anchor
551 bool includeRoot
, // include the root in outArray
552 CFArrayRef
*outArray
) // created and RETURNED
554 CFMutableArrayRef certArray
;
555 SecTrustRef secTrust
= NULL
;
556 SecPolicyRef policy
= NULL
;
557 SecPolicySearchRef policySearch
= NULL
;
558 SecTrustResultType secTrustResult
;
559 CSSM_TP_APPLE_EVIDENCE_INFO
*dummyEv
; // not used
560 CFArrayRef certChain
= NULL
; // constructed chain
563 certArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
564 CFArrayAppendValue(certArray
, identity
);
567 * Case 1: identity is a root; we're done. Note that this case
568 * overrides the includeRoot argument.
570 SecCertificateRef certRef
;
571 OSStatus ortn
= SecIdentityCopyCertificate(identity
, &certRef
);
573 /* should never happen */
574 cssmPerror("SecIdentityCopyCertificate", ortn
);
577 bool isRoot
= isCertRefRoot(certRef
);
579 *outArray
= certArray
;
581 return errSecSuccess
;
585 * Now use SecTrust to get a complete cert chain, using all of the
586 * user's keychains to look for intermediate certs.
587 * NOTE this does NOT handle root certs which are not in the system
588 * root cert DB. (The above case, where the identity is a root cert, does.)
590 CFMutableArrayRef subjCerts
= CFArrayCreateMutable(NULL
, 1, &kCFTypeArrayCallBacks
);
591 CFArraySetValueAtIndex(subjCerts
, 0, certRef
);
593 /* the array owns the subject cert ref now */
596 /* Get a SecPolicyRef for generic X509 cert chain verification */
597 ortn
= SecPolicySearchCreate(CSSM_CERT_X_509v3
,
598 &CSSMOID_APPLE_X509_BASIC
,
602 cssmPerror("SecPolicySearchCreate", ortn
);
605 ortn
= SecPolicySearchCopyNext(policySearch
, &policy
);
607 cssmPerror("SecPolicySearchCopyNext", ortn
);
611 /* build a SecTrustRef for specified policy and certs */
612 ortn
= SecTrustCreateWithCertificates(subjCerts
,
615 cssmPerror("SecTrustCreateWithCertificates", ortn
);
621 * Tell SecTrust to trust this one in addition to the current
622 * trusted system-wide anchors.
624 CFMutableArrayRef newAnchors
;
625 CFArrayRef currAnchors
;
627 ortn
= SecTrustCopyAnchorCertificates(&currAnchors
);
629 /* should never happen */
630 cssmPerror("SecTrustCopyAnchorCertificates", ortn
);
633 newAnchors
= CFArrayCreateMutableCopy(NULL
,
634 CFArrayGetCount(currAnchors
) + 1,
636 CFRelease(currAnchors
);
637 CFArrayAppendValue(newAnchors
, trustedAnchor
);
638 ortn
= SecTrustSetAnchorCertificates(secTrust
, newAnchors
);
639 CFRelease(newAnchors
);
641 cssmPerror("SecTrustSetAnchorCertificates", ortn
);
646 ortn
= SecTrustEvaluate(secTrust
, &secTrustResult
);
648 cssmPerror("SecTrustEvaluate", ortn
);
651 switch(secTrustResult
) {
652 case kSecTrustResultUnspecified
:
653 /* cert chain valid, no special UserTrust assignments */
654 case kSecTrustResultProceed
:
655 /* cert chain valid AND user explicitly trusts this */
659 * Cert chain construction failed.
660 * Just go with the single subject cert we were given.
662 printf("***Warning: could not construct completed cert chain\n");
663 ortn
= errSecSuccess
;
667 /* get resulting constructed cert chain */
668 ortn
= SecTrustGetResult(secTrust
, &secTrustResult
, &certChain
, &dummyEv
);
670 cssmPerror("SecTrustEvaluate", ortn
);
675 * Copy certs from constructed chain to our result array, skipping
676 * the leaf (which is already there, as a SecIdentityRef) and possibly
679 numResCerts
= CFArrayGetCount(certChain
);
680 if(numResCerts
< 2) {
682 * Can't happen: if subject was a root, we'd already have returned.
683 * If chain doesn't verify to a root, we'd have bailed after
684 * SecTrustEvaluate().
686 printf("***sslCompleteCertChain screwup: numResCerts %d\n",
688 ortn
= errSecSuccess
;
692 /* skip the last (root) cert) */
695 for(CFIndex dex
=1; dex
<numResCerts
; dex
++) {
696 certRef
= (SecCertificateRef
)CFArrayGetValueAtIndex(certChain
, dex
);
697 CFArrayAppendValue(certArray
, certRef
);
705 CFRelease(subjCerts
);
711 CFRelease(policySearch
);
713 *outArray
= certArray
;
719 * Given an open keychain, find a SecIdentityRef and munge it into
720 * a CFArrayRef required by SSLSetCertificate().
722 CFArrayRef
sslKcRefToCertArray(
723 SecKeychainRef kcRef
,
725 bool completeCertChain
,
726 const char *trustedAnchorFile
)
728 /* quick check to make sure the keychain exists */
729 SecKeychainStatus kcStat
;
730 OSStatus ortn
= SecKeychainGetStatus(kcRef
, &kcStat
);
732 printSslErrStr("SecKeychainGetStatus", ortn
);
733 printf("Can not open keychain. Aborting.\n");
738 * Search for "any" identity matching specified key use;
739 * in this app, we expect there to be exactly one.
741 SecIdentitySearchRef srchRef
= nil
;
742 ortn
= SecIdentitySearchCreate(kcRef
,
743 encryptOnly
? CSSM_KEYUSE_DECRYPT
: CSSM_KEYUSE_SIGN
,
746 printf("SecIdentitySearchCreate returned %d.\n", (int)ortn
);
747 printf("Cannot find signing key in keychain. Aborting.\n");
750 SecIdentityRef identity
= nil
;
751 ortn
= SecIdentitySearchCopyNext(srchRef
, &identity
);
753 printf("SecIdentitySearchCopyNext returned %d.\n", (int)ortn
);
754 printf("Cannot find signing key in keychain. Aborting.\n");
757 if(CFGetTypeID(identity
) != SecIdentityGetTypeID()) {
758 printf("SecIdentitySearchCopyNext CFTypeID failure!\n");
765 if(completeCertChain
) {
767 * Place it and the other certs needed to verify it -
768 * up to but not including the root - in a CFArray.
770 SecCertificateRef anchorCert
= NULL
;
771 if(trustedAnchorFile
) {
772 ortn
= sslReadAnchor(trustedAnchorFile
, &anchorCert
);
774 printf("***Error reading anchor file\n");
778 ortn
= sslCompleteCertChain(identity
, anchorCert
, false, &ca
);
780 CFRelease(anchorCert
);
785 /* simple case, just this one identity */
786 CFArrayRef ca
= CFArrayCreate(NULL
,
787 (const void **)&identity
,
791 printf("CFArrayCreate error\n");
798 OSStatus
addTrustedSecCert(
800 SecCertificateRef secCert
,
804 CFMutableArrayRef array
;
806 if(secCert
== NULL
) {
807 printf("***addTrustedSecCert screwup\n");
810 array
= CFArrayCreateMutable(kCFAllocatorDefault
,
811 (CFIndex
)1, &kCFTypeArrayCallBacks
);
813 return errSecAllocate
;
815 CFArrayAppendValue(array
, secCert
);
816 ortn
= SSLSetTrustedRoots(ctx
, array
, replaceAnchors
? true : false);
818 printSslErrStr("SSLSetTrustedRoots", ortn
);
824 OSStatus
sslAddTrustedRoot(
826 const char *anchorFile
,
832 OSStatus
addIdentityAsTrustedRoot(
834 CFArrayRef identArray
)
836 return errSecSuccess
;
840 * Lists of SSLCipherSuites used in sslSetCipherRestrictions. Note that the
841 * SecureTransport library does not implement all of these; we only specify
842 * the ones it claims to support.
844 const SSLCipherSuite suites40
[] = {
845 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
846 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
847 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
848 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
849 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
850 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
851 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
852 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
853 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
854 SSL_NO_SUCH_CIPHERSUITE
856 const SSLCipherSuite suitesDES
[] = {
857 SSL_RSA_WITH_DES_CBC_SHA
,
858 SSL_DH_DSS_WITH_DES_CBC_SHA
,
859 SSL_DH_RSA_WITH_DES_CBC_SHA
,
860 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
861 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
862 SSL_DH_anon_WITH_DES_CBC_SHA
,
863 SSL_RSA_WITH_DES_CBC_MD5
,
864 SSL_NO_SUCH_CIPHERSUITE
866 const SSLCipherSuite suitesDES40
[] = {
867 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
868 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
869 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
870 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
871 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
872 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
873 SSL_NO_SUCH_CIPHERSUITE
875 const SSLCipherSuite suites3DES
[] = {
876 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
877 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
878 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
879 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
880 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
881 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
882 SSL_RSA_WITH_3DES_EDE_CBC_MD5
,
883 SSL_NO_SUCH_CIPHERSUITE
885 const SSLCipherSuite suitesRC4
[] = {
886 SSL_RSA_WITH_RC4_128_MD5
,
887 SSL_RSA_WITH_RC4_128_SHA
,
888 SSL_DH_anon_WITH_RC4_128_MD5
,
889 SSL_NO_SUCH_CIPHERSUITE
891 const SSLCipherSuite suitesRC4_40
[] = {
892 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
893 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
894 SSL_NO_SUCH_CIPHERSUITE
896 const SSLCipherSuite suitesRC2
[] = {
897 SSL_RSA_WITH_RC2_CBC_MD5
,
898 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
899 SSL_NO_SUCH_CIPHERSUITE
901 const SSLCipherSuite suitesAES128
[] = {
902 TLS_RSA_WITH_AES_128_CBC_SHA
,
903 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
904 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
905 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
906 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
907 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
908 SSL_NO_SUCH_CIPHERSUITE
910 const SSLCipherSuite suitesAES256
[] = {
911 TLS_RSA_WITH_AES_256_CBC_SHA
,
912 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
913 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
914 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
915 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
916 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
917 SSL_NO_SUCH_CIPHERSUITE
919 const SSLCipherSuite suitesDH
[] = {
920 SSL_DH_DSS_WITH_DES_CBC_SHA
,
921 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
922 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
923 SSL_DH_RSA_WITH_DES_CBC_SHA
,
924 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
925 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
926 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
927 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
928 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
929 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
930 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
931 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
932 SSL_DH_anon_WITH_RC4_128_MD5
,
933 SSL_DH_anon_WITH_DES_CBC_SHA
,
934 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
935 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
936 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
937 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
938 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
939 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
940 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
941 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
942 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
943 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
944 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
945 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
946 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
947 SSL_NO_SUCH_CIPHERSUITE
949 const SSLCipherSuite suitesDHAnon
[] = {
950 SSL_DH_anon_WITH_RC4_128_MD5
,
951 SSL_DH_anon_WITH_DES_CBC_SHA
,
952 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
953 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
954 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
955 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
956 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
957 SSL_NO_SUCH_CIPHERSUITE
959 const SSLCipherSuite suitesDH_RSA
[] = {
960 SSL_DH_RSA_WITH_DES_CBC_SHA
,
961 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
962 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
963 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
964 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
965 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
966 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
967 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
968 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
969 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
970 SSL_NO_SUCH_CIPHERSUITE
972 const SSLCipherSuite suitesDH_DSS
[] = {
973 SSL_DH_DSS_WITH_DES_CBC_SHA
,
974 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
975 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
976 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
977 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
978 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
979 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
980 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
981 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
982 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
983 SSL_NO_SUCH_CIPHERSUITE
985 const SSLCipherSuite suites_SHA1
[] = {
986 SSL_RSA_WITH_RC4_128_SHA
,
987 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
988 SSL_RSA_WITH_IDEA_CBC_SHA
,
989 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
990 SSL_RSA_WITH_DES_CBC_SHA
,
991 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
992 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
993 SSL_DH_DSS_WITH_DES_CBC_SHA
,
994 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
995 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
996 SSL_DH_RSA_WITH_DES_CBC_SHA
,
997 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
998 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
999 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
1000 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
1001 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
1002 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
1003 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
1004 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
1005 SSL_DH_anon_WITH_DES_CBC_SHA
,
1006 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
1007 SSL_FORTEZZA_DMS_WITH_NULL_SHA
,
1008 SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA
,
1009 TLS_RSA_WITH_AES_128_CBC_SHA
,
1010 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
1011 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
1012 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
1013 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
1014 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
1015 TLS_RSA_WITH_AES_256_CBC_SHA
,
1016 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
1017 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
1018 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
1019 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
1020 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
1021 SSL_NO_SUCH_CIPHERSUITE
1023 const SSLCipherSuite suites_MD5
[] = {
1024 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
1025 SSL_RSA_WITH_RC4_128_MD5
,
1026 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
1027 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
1028 SSL_DH_anon_WITH_RC4_128_MD5
,
1029 SSL_NO_SUCH_CIPHERSUITE
1034 * Given an SSLContextRef and an array of SSLCipherSuites, terminated by
1035 * SSL_NO_SUCH_CIPHERSUITE, select those SSLCipherSuites which the library
1036 * supports and do a SSLSetEnabledCiphers() specifying those.
1038 OSStatus
sslSetEnabledCiphers(
1040 const SSLCipherSuite
*ciphers
)
1042 size_t numSupported
;
1044 SSLCipherSuite
*supported
= NULL
;
1045 SSLCipherSuite
*enabled
= NULL
;
1046 unsigned enabledDex
= 0; // index into enabled
1047 unsigned supportedDex
= 0; // index into supported
1048 unsigned inDex
= 0; // index into ciphers
1050 /* first get all the supported ciphers */
1051 ortn
= SSLGetNumberSupportedCiphers(ctx
, &numSupported
);
1053 printSslErrStr("SSLGetNumberSupportedCiphers", ortn
);
1056 supported
= (SSLCipherSuite
*)malloc(numSupported
* sizeof(SSLCipherSuite
));
1057 ortn
= SSLGetSupportedCiphers(ctx
, supported
, &numSupported
);
1059 printSslErrStr("SSLGetSupportedCiphers", ortn
);
1064 * Malloc an array we'll use for SSLGetEnabledCiphers - this will be
1065 * bigger than the number of suites we actually specify
1067 enabled
= (SSLCipherSuite
*)malloc(numSupported
* sizeof(SSLCipherSuite
));
1070 * For each valid suite in ciphers, see if it's in the list of
1071 * supported ciphers. If it is, add it to the list of ciphers to be
1074 for(inDex
=0; ciphers
[inDex
] != SSL_NO_SUCH_CIPHERSUITE
; inDex
++) {
1075 for(supportedDex
=0; supportedDex
<numSupported
; supportedDex
++) {
1076 if(ciphers
[inDex
] == supported
[supportedDex
]) {
1077 enabled
[enabledDex
++] = ciphers
[inDex
];
1083 /* send it on down. */
1084 ortn
= SSLSetEnabledCiphers(ctx
, enabled
, enabledDex
);
1086 printSslErrStr("SSLSetEnabledCiphers", ortn
);
1094 * Specify a restricted set of cipherspecs.
1096 OSStatus
sslSetCipherRestrictions(
1098 char cipherRestrict
)
1102 if(cipherRestrict
== '\0') {
1103 return errSecSuccess
; // actually should not have been called
1105 switch(cipherRestrict
) {
1107 ortn
= sslSetEnabledCiphers(ctx
, suites40
);
1110 ortn
= sslSetEnabledCiphers(ctx
, suitesDES
);
1113 ortn
= sslSetEnabledCiphers(ctx
, suitesDES40
);
1116 ortn
= sslSetEnabledCiphers(ctx
, suites3DES
);
1119 ortn
= sslSetEnabledCiphers(ctx
, suitesRC4
);
1122 ortn
= sslSetEnabledCiphers(ctx
, suitesRC4_40
);
1125 ortn
= sslSetEnabledCiphers(ctx
, suitesRC2
);
1128 ortn
= sslSetEnabledCiphers(ctx
, suitesAES128
);
1131 ortn
= sslSetEnabledCiphers(ctx
, suitesAES256
);
1134 ortn
= sslSetEnabledCiphers(ctx
, suitesDH
);
1137 ortn
= sslSetEnabledCiphers(ctx
, suitesDHAnon
);
1140 ortn
= sslSetEnabledCiphers(ctx
, suitesDH_RSA
);
1143 ortn
= sslSetEnabledCiphers(ctx
, suitesDH_DSS
);
1146 printf("***bad cipherSpec***\n");
1153 int sslVerifyClientCertState(
1154 char *whichSide
, // "client" or "server"
1155 SSLClientCertificateState expectState
,
1156 SSLClientCertificateState gotState
)
1158 if(expectState
== SSL_CLIENT_CERT_IGNORE
) {
1159 /* app says "don't bopther checking" */
1162 if(expectState
== gotState
) {
1165 printf("***%s: Expected clientCertState %s; got %s\n", whichSide
,
1166 sslGetClientCertStateString(expectState
),
1167 sslGetClientCertStateString(gotState
));
1172 char *whichSide
, // "client" or "server"
1176 if(expectRtn
== gotRtn
) {
1179 printf("***%s: Expected return %s; got %s\n", whichSide
,
1180 sslGetSSLErrString(expectRtn
),
1181 sslGetSSLErrString(gotRtn
));
1185 int sslVerifyProtVers(
1186 char *whichSide
, // "client" or "server"
1187 SSLProtocol expectProt
,
1188 SSLProtocol gotProt
)
1190 if(expectProt
== SSL_PROTOCOL_IGNORE
) {
1191 /* app says "don't bopther checking" */
1194 if(expectProt
== gotProt
) {
1197 printf("***%s: Expected return %s; got %s\n", whichSide
,
1198 sslGetProtocolVersionString(expectProt
),
1199 sslGetProtocolVersionString(gotProt
));
1203 int sslVerifyCipher(
1204 char *whichSide
, // "client" or "server"
1205 SSLCipherSuite expectCipher
,
1206 SSLCipherSuite gotCipher
)
1208 if(expectCipher
== SSL_CIPHER_IGNORE
) {
1209 /* app says "don't bopther checking" */
1212 if(expectCipher
== gotCipher
) {
1215 printf("***%s: Expected return %s; got %s\n", whichSide
,
1216 sslGetCipherSuiteString(expectCipher
),
1217 sslGetCipherSuiteString(gotCipher
));
1222 OSStatus
sslSetProtocols(
1224 const char *acceptedProts
,
1225 SSLProtocol tryVersion
) // only used if acceptedProts NULL
1231 printf("***SSLSetProtocolVersionEnabled not supported in this config.\n");
1234 ortn
= SSLSetProtocolVersionEnabled(ctx
, kSSLProtocolAll
, false);
1236 printSslErrStr("SSLSetProtocolVersionEnabled(all off)", ortn
);
1239 for(const char *cp
= acceptedProts
; *cp
; cp
++) {
1243 prot
= kSSLProtocol2
;
1246 prot
= kSSLProtocol3
;
1249 prot
= kTLSProtocol1
;
1252 printf("***BRRZAP! Bad acceptedProts string %s. Aborting.\n", acceptedProts
);
1255 ortn
= SSLSetProtocolVersionEnabled(ctx
, prot
, true);
1257 printSslErrStr("SSLSetProtocolVersionEnabled", ortn
);
1263 ortn
= SSLSetProtocolVersion(ctx
, tryVersion
);
1265 printSslErrStr("SSLSetProtocolVersion", ortn
);
1269 return errSecSuccess
;
1273 char *whichSide
, // "client" or "server"
1274 SslAppTestParams
*params
)
1276 printf("%s status:\n", whichSide
);
1277 if(params
->acceptedProts
) {
1278 printf(" Allowed SSL versions : %s\n", params
->acceptedProts
);
1281 printf(" Attempted SSL version : %s\n",
1282 sslGetProtocolVersionString(params
->tryVersion
));
1284 printf(" Result : %s\n", sslGetSSLErrString(params
->ortn
));
1285 printf(" Negotiated SSL version : %s\n",
1286 sslGetProtocolVersionString(params
->negVersion
));
1287 printf(" Negotiated CipherSuite : %s\n",
1288 sslGetCipherSuiteString(params
->negCipher
));
1289 if(params
->certState
!= kSSLClientCertNone
) {
1290 printf(" Client Cert State : %s\n",
1291 sslGetClientCertStateString(params
->certState
));
1296 /* print a '.' every few seconds to keep UI alive while connecting */
1297 static time_t lastTime
= (time_t)0;
1298 #define TIME_INTERVAL 3
1302 time_t thisTime
= time(0);
1304 if((thisTime
- lastTime
) >= TIME_INTERVAL
) {
1305 printf("."); fflush(stdout
);
1306 lastTime
= thisTime
;
1311 /* main server pthread body */
1312 static void *sslServerThread(void *arg
)
1314 SslAppTestParams
*testParams
= (SslAppTestParams
*)arg
;
1317 status
= sslAppServe(testParams
);
1318 pthread_exit((void*)status
);
1320 return (void *)status
;
1324 * Run one session, with the server in a separate thread.
1325 * On entry, serverParams->port is the port we attempt to run on;
1326 * the server thread may overwrite that with a different port if it's
1327 * unable to open the port we specify. Whatever is left in
1328 * serverParams->port is what's used for the client side.
1330 #define CLIENT_WAIT_SECONDS 1
1332 SslAppTestParams
*serverParams
,
1333 SslAppTestParams
*clientParams
,
1334 const char *testDesc
)
1336 pthread_t serverPthread
;
1340 if(testDesc
&& !clientParams
->quiet
) {
1341 printf("===== %s =====\n", testDesc
);
1344 if(pthread_mutex_init(&serverParams
->pthreadMutex
, NULL
)) {
1345 printf("***Error initializing mutex; aborting.\n");
1348 if(pthread_cond_init(&serverParams
->pthreadCond
, NULL
)) {
1349 printf("***Error initializing pthreadCond; aborting.\n");
1352 serverParams
->serverReady
= false; // server sets true
1354 int result
= pthread_create(&serverPthread
, NULL
,
1355 sslServerThread
, serverParams
);
1357 printf("***Error starting up server thread; aborting.\n");
1361 /* wait for server to set up a socket we can connect to */
1362 if(pthread_mutex_lock(&serverParams
->pthreadMutex
)) {
1363 printf("***Error acquiring server lock; aborting.\n");
1366 while(!serverParams
->serverReady
) {
1367 if(pthread_cond_wait(&serverParams
->pthreadCond
, &serverParams
->pthreadMutex
)) {
1368 printf("***Error waiting server thread; aborting.\n");
1372 pthread_mutex_unlock(&serverParams
->pthreadMutex
);
1373 pthread_cond_destroy(&serverParams
->pthreadCond
);
1374 pthread_mutex_destroy(&serverParams
->pthreadMutex
);
1376 clientParams
->port
= serverParams
->port
;
1377 clientRtn
= sslAppClient(clientParams
);
1378 /* server doesn't shut down its socket until it sees this */
1379 serverParams
->clientDone
= 1;
1380 result
= pthread_join(serverPthread
, &serverRtn
);
1382 printf("***pthread_join returned %d, aborting\n", result
);
1386 if(serverParams
->verbose
) {
1387 sslShowResult("server", serverParams
);
1389 if(clientParams
->verbose
) {
1390 sslShowResult("client", clientParams
);
1393 /* verify results */
1395 ourRtn
+= sslVerifyRtn("server", serverParams
->expectRtn
, serverParams
->ortn
);
1396 ourRtn
+= sslVerifyRtn("client", clientParams
->expectRtn
, clientParams
->ortn
);
1397 ourRtn
+= sslVerifyProtVers("server", serverParams
->expectVersion
,
1398 serverParams
->negVersion
);
1399 ourRtn
+= sslVerifyProtVers("client", clientParams
->expectVersion
,
1400 clientParams
->negVersion
);
1401 ourRtn
+= sslVerifyClientCertState("server", serverParams
->expectCertState
,
1402 serverParams
->certState
);
1403 ourRtn
+= sslVerifyClientCertState("client", clientParams
->expectCertState
,
1404 clientParams
->certState
);
1405 if(serverParams
->ortn
== errSecSuccess
) {
1406 ourRtn
+= sslVerifyCipher("server", serverParams
->expectCipher
,
1407 serverParams
->negCipher
);
1409 if(clientParams
->ortn
== errSecSuccess
) {
1410 ourRtn
+= sslVerifyCipher("client", clientParams
->expectCipher
,
1411 clientParams
->negCipher
);
1416 static bool isCertRoot(
1417 SecCertificateRef cert
)
1419 /* FIXME - per Radar 3247491, the Sec-level functions we'd like to use for this
1420 * haven't been written yet...
1421 CSSM_X509_NAME subject;
1422 CSSM_X509_NAME issuer;
1429 * Add all of the roots in a given KC to SSL ctx's trusted anchors.
1431 OSStatus
sslAddTrustedRoots(
1433 SecKeychainRef keychain
,
1434 bool *foundOne
) // RETURNED, true if we found
1435 // at least one root cert
1438 SecCertificateRef secCert
;
1439 SecKeychainSearchRef srch
;
1442 ortn
= SecKeychainSearchCreateFromAttributes(keychain
,
1443 kSecCertificateItemClass
,
1447 printSslErrStr("SecKeychainSearchCreateFromAttributes", ortn
);
1452 * Only use root certs. Not an error if we don't find any.
1455 ortn
= SecKeychainSearchCopyNext(srch
,
1456 (SecKeychainItemRef
*)&secCert
);
1461 /* see if it's a root */
1462 if(!isCertRoot(secCert
)) {
1466 /* Tell Secure Transport to trust this one. */
1467 ortn
= addTrustedSecCert(ctx
, secCert
, false);
1470 printSslErrStr("addTrustedSecCert", ortn
);
1475 } while(ortn
== errSecSuccess
);
1477 return errSecSuccess
;
1481 * Wrapper for sslIdentPicker, with optional trusted anchor specified as a filename.
1483 OSStatus
sslIdentityPicker(
1484 SecKeychainRef kcRef
, // NULL means use default list
1485 const char *trustedAnchor
, // optional additional trusted anchor
1486 bool includeRoot
, // true --> root is appended to outArray
1487 // false --> root not included
1488 CFArrayRef
*outArray
) // created and RETURNED
1490 SecCertificateRef trustedCert
= NULL
;
1494 ortn
= sslReadAnchor(trustedAnchor
, &trustedCert
);
1496 printf("***Error reading %s. sslIdentityPicker proceeding with no anchor.\n",
1501 ortn
= sslIdentPicker(kcRef
, trustedCert
, includeRoot
, outArray
);
1503 CFRelease(trustedCert
);
1509 * Given a keychain name, convert it into a full path using the "SSL regression
1510 * test suite algorithm". The Sec layer by default locates root root's keychains
1511 * in different places depending on whether we're actually logged in as root
1512 * or running via e.g. cron, so we force the location of root keychains to
1513 * a hard-coded path. User keychain names we leave alone.
1515 void sslKeychainPath(
1517 char *kcPath
) // allocd by caller, MAXPATHLEN
1519 if(kcName
[0] == '\0') {
1522 else if(geteuid() == 0) {
1524 sprintf(kcPath
, "/Library/Keychains/%s", kcName
);
1527 /* user, leave alone */
1528 strcpy(kcPath
, kcName
);
1532 /* Verify presence of required file. Returns nonzero if not found. */
1533 int sslCheckFile(const char *path
)
1537 if(stat(path
, &sb
)) {
1538 printf("***Can't find file %s.\n", path
);
1539 printf(" Try running in the build directory, perhaps after running the\n"
1540 " makeLocalCert script.\n");
1548 /* Stringify a SSL_ECDSA_NamedCurve */
1549 extern const char *sslCurveString(
1550 SSL_ECDSA_NamedCurve namedCurve
)
1552 static char unk
[100];
1554 switch(namedCurve
) {
1555 case SSL_Curve_None
: return "Curve_None";
1556 case SSL_Curve_secp256r1
: return "secp256r1";
1557 case SSL_Curve_secp384r1
: return "secp384r1";
1558 case SSL_Curve_secp521r1
: return "secp521r1";
1560 sprintf(unk
, "Unknown <%d>", (int)namedCurve
);