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 "errSSLPeerUserCanceled";
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 #pragma clang diagnostic push
799 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
801 OSStatus
addTrustedSecCert(
803 SecCertificateRef secCert
,
807 CFMutableArrayRef array
;
809 if(secCert
== NULL
) {
810 printf("***addTrustedSecCert screwup\n");
813 array
= CFArrayCreateMutable(kCFAllocatorDefault
,
814 (CFIndex
)1, &kCFTypeArrayCallBacks
);
816 return errSecAllocate
;
818 CFArrayAppendValue(array
, secCert
);
819 ortn
= SSLSetTrustedRoots(ctx
, array
, replaceAnchors
? true : false);
821 printSslErrStr("SSLSetTrustedRoots", ortn
);
827 #pragma clang diagnostic pop
829 OSStatus
sslAddTrustedRoot(
831 const char *anchorFile
,
837 OSStatus
addIdentityAsTrustedRoot(
839 CFArrayRef identArray
)
841 return errSecSuccess
;
845 * Lists of SSLCipherSuites used in sslSetCipherRestrictions. Note that the
846 * SecureTransport library does not implement all of these; we only specify
847 * the ones it claims to support.
849 const SSLCipherSuite suites40
[] = {
850 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
851 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
852 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
853 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
854 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
855 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
856 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
857 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
858 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
859 SSL_NO_SUCH_CIPHERSUITE
861 const SSLCipherSuite suitesDES
[] = {
862 SSL_RSA_WITH_DES_CBC_SHA
,
863 SSL_DH_DSS_WITH_DES_CBC_SHA
,
864 SSL_DH_RSA_WITH_DES_CBC_SHA
,
865 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
866 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
867 SSL_DH_anon_WITH_DES_CBC_SHA
,
868 SSL_RSA_WITH_DES_CBC_MD5
,
869 SSL_NO_SUCH_CIPHERSUITE
871 const SSLCipherSuite suitesDES40
[] = {
872 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
873 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
874 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
875 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
876 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
877 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
878 SSL_NO_SUCH_CIPHERSUITE
880 const SSLCipherSuite suites3DES
[] = {
881 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
882 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
883 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
884 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
885 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
886 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
887 SSL_RSA_WITH_3DES_EDE_CBC_MD5
,
888 SSL_NO_SUCH_CIPHERSUITE
890 const SSLCipherSuite suitesRC4
[] = {
891 SSL_RSA_WITH_RC4_128_MD5
,
892 SSL_RSA_WITH_RC4_128_SHA
,
893 SSL_DH_anon_WITH_RC4_128_MD5
,
894 SSL_NO_SUCH_CIPHERSUITE
896 const SSLCipherSuite suitesRC4_40
[] = {
897 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
898 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
899 SSL_NO_SUCH_CIPHERSUITE
901 const SSLCipherSuite suitesRC2
[] = {
902 SSL_RSA_WITH_RC2_CBC_MD5
,
903 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
904 SSL_NO_SUCH_CIPHERSUITE
906 const SSLCipherSuite suitesAES128
[] = {
907 TLS_RSA_WITH_AES_128_CBC_SHA
,
908 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
909 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
910 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
911 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
912 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
913 SSL_NO_SUCH_CIPHERSUITE
915 const SSLCipherSuite suitesAES256
[] = {
916 TLS_RSA_WITH_AES_256_CBC_SHA
,
917 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
918 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
919 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
920 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
921 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
922 SSL_NO_SUCH_CIPHERSUITE
924 const SSLCipherSuite suitesDH
[] = {
925 SSL_DH_DSS_WITH_DES_CBC_SHA
,
926 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
927 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
928 SSL_DH_RSA_WITH_DES_CBC_SHA
,
929 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
930 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
931 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
932 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
933 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
934 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
935 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
936 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
937 SSL_DH_anon_WITH_RC4_128_MD5
,
938 SSL_DH_anon_WITH_DES_CBC_SHA
,
939 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
940 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
941 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
942 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
943 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
944 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
945 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
946 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
947 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
948 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
949 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
950 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
951 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
952 SSL_NO_SUCH_CIPHERSUITE
954 const SSLCipherSuite suitesDHAnon
[] = {
955 SSL_DH_anon_WITH_RC4_128_MD5
,
956 SSL_DH_anon_WITH_DES_CBC_SHA
,
957 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
958 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
959 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
960 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
961 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
962 SSL_NO_SUCH_CIPHERSUITE
964 const SSLCipherSuite suitesDH_RSA
[] = {
965 SSL_DH_RSA_WITH_DES_CBC_SHA
,
966 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
967 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
968 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
969 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
970 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
971 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
972 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
973 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
974 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
975 SSL_NO_SUCH_CIPHERSUITE
977 const SSLCipherSuite suitesDH_DSS
[] = {
978 SSL_DH_DSS_WITH_DES_CBC_SHA
,
979 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
980 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
981 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
982 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
983 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
984 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
985 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
986 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
987 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
988 SSL_NO_SUCH_CIPHERSUITE
990 const SSLCipherSuite suites_SHA1
[] = {
991 SSL_RSA_WITH_RC4_128_SHA
,
992 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
993 SSL_RSA_WITH_IDEA_CBC_SHA
,
994 SSL_RSA_EXPORT_WITH_DES40_CBC_SHA
,
995 SSL_RSA_WITH_DES_CBC_SHA
,
996 SSL_RSA_WITH_3DES_EDE_CBC_SHA
,
997 SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA
,
998 SSL_DH_DSS_WITH_DES_CBC_SHA
,
999 SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA
,
1000 SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA
,
1001 SSL_DH_RSA_WITH_DES_CBC_SHA
,
1002 SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA
,
1003 SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA
,
1004 SSL_DHE_DSS_WITH_DES_CBC_SHA
,
1005 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
,
1006 SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA
,
1007 SSL_DHE_RSA_WITH_DES_CBC_SHA
,
1008 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
1009 SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA
,
1010 SSL_DH_anon_WITH_DES_CBC_SHA
,
1011 SSL_DH_anon_WITH_3DES_EDE_CBC_SHA
,
1012 SSL_FORTEZZA_DMS_WITH_NULL_SHA
,
1013 SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA
,
1014 TLS_RSA_WITH_AES_128_CBC_SHA
,
1015 TLS_DH_DSS_WITH_AES_128_CBC_SHA
,
1016 TLS_DH_RSA_WITH_AES_128_CBC_SHA
,
1017 TLS_DHE_DSS_WITH_AES_128_CBC_SHA
,
1018 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
,
1019 TLS_DH_anon_WITH_AES_128_CBC_SHA
,
1020 TLS_RSA_WITH_AES_256_CBC_SHA
,
1021 TLS_DH_DSS_WITH_AES_256_CBC_SHA
,
1022 TLS_DH_RSA_WITH_AES_256_CBC_SHA
,
1023 TLS_DHE_DSS_WITH_AES_256_CBC_SHA
,
1024 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
,
1025 TLS_DH_anon_WITH_AES_256_CBC_SHA
,
1026 SSL_NO_SUCH_CIPHERSUITE
1028 const SSLCipherSuite suites_MD5
[] = {
1029 SSL_RSA_EXPORT_WITH_RC4_40_MD5
,
1030 SSL_RSA_WITH_RC4_128_MD5
,
1031 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
,
1032 SSL_DH_anon_EXPORT_WITH_RC4_40_MD5
,
1033 SSL_DH_anon_WITH_RC4_128_MD5
,
1034 SSL_NO_SUCH_CIPHERSUITE
1039 * Given an SSLContextRef and an array of SSLCipherSuites, terminated by
1040 * SSL_NO_SUCH_CIPHERSUITE, select those SSLCipherSuites which the library
1041 * supports and do a SSLSetEnabledCiphers() specifying those.
1043 OSStatus
sslSetEnabledCiphers(
1045 const SSLCipherSuite
*ciphers
)
1047 size_t numSupported
;
1049 SSLCipherSuite
*supported
= NULL
;
1050 SSLCipherSuite
*enabled
= NULL
;
1051 unsigned enabledDex
= 0; // index into enabled
1052 unsigned supportedDex
= 0; // index into supported
1053 unsigned inDex
= 0; // index into ciphers
1055 /* first get all the supported ciphers */
1056 ortn
= SSLGetNumberSupportedCiphers(ctx
, &numSupported
);
1058 printSslErrStr("SSLGetNumberSupportedCiphers", ortn
);
1061 supported
= (SSLCipherSuite
*)malloc(numSupported
* sizeof(SSLCipherSuite
));
1062 ortn
= SSLGetSupportedCiphers(ctx
, supported
, &numSupported
);
1064 printSslErrStr("SSLGetSupportedCiphers", ortn
);
1069 * Malloc an array we'll use for SSLGetEnabledCiphers - this will be
1070 * bigger than the number of suites we actually specify
1072 enabled
= (SSLCipherSuite
*)malloc(numSupported
* sizeof(SSLCipherSuite
));
1075 * For each valid suite in ciphers, see if it's in the list of
1076 * supported ciphers. If it is, add it to the list of ciphers to be
1079 for(inDex
=0; ciphers
[inDex
] != SSL_NO_SUCH_CIPHERSUITE
; inDex
++) {
1080 for(supportedDex
=0; supportedDex
<numSupported
; supportedDex
++) {
1081 if(ciphers
[inDex
] == supported
[supportedDex
]) {
1082 enabled
[enabledDex
++] = ciphers
[inDex
];
1088 /* send it on down. */
1089 ortn
= SSLSetEnabledCiphers(ctx
, enabled
, enabledDex
);
1091 printSslErrStr("SSLSetEnabledCiphers", ortn
);
1099 * Specify a restricted set of cipherspecs.
1101 OSStatus
sslSetCipherRestrictions(
1103 char cipherRestrict
)
1107 if(cipherRestrict
== '\0') {
1108 return errSecSuccess
; // actually should not have been called
1110 switch(cipherRestrict
) {
1112 ortn
= sslSetEnabledCiphers(ctx
, suites40
);
1115 ortn
= sslSetEnabledCiphers(ctx
, suitesDES
);
1118 ortn
= sslSetEnabledCiphers(ctx
, suitesDES40
);
1121 ortn
= sslSetEnabledCiphers(ctx
, suites3DES
);
1124 ortn
= sslSetEnabledCiphers(ctx
, suitesRC4
);
1127 ortn
= sslSetEnabledCiphers(ctx
, suitesRC4_40
);
1130 ortn
= sslSetEnabledCiphers(ctx
, suitesRC2
);
1133 ortn
= sslSetEnabledCiphers(ctx
, suitesAES128
);
1136 ortn
= sslSetEnabledCiphers(ctx
, suitesAES256
);
1139 ortn
= sslSetEnabledCiphers(ctx
, suitesDH
);
1142 ortn
= sslSetEnabledCiphers(ctx
, suitesDHAnon
);
1145 ortn
= sslSetEnabledCiphers(ctx
, suitesDH_RSA
);
1148 ortn
= sslSetEnabledCiphers(ctx
, suitesDH_DSS
);
1151 printf("***bad cipherSpec***\n");
1158 int sslVerifyClientCertState(
1159 char *whichSide
, // "client" or "server"
1160 SSLClientCertificateState expectState
,
1161 SSLClientCertificateState gotState
)
1163 if(expectState
== SSL_CLIENT_CERT_IGNORE
) {
1164 /* app says "don't bopther checking" */
1167 if(expectState
== gotState
) {
1170 printf("***%s: Expected clientCertState %s; got %s\n", whichSide
,
1171 sslGetClientCertStateString(expectState
),
1172 sslGetClientCertStateString(gotState
));
1177 char *whichSide
, // "client" or "server"
1181 if(expectRtn
== gotRtn
) {
1184 printf("***%s: Expected return %s; got %s\n", whichSide
,
1185 sslGetSSLErrString(expectRtn
),
1186 sslGetSSLErrString(gotRtn
));
1190 int sslVerifyProtVers(
1191 char *whichSide
, // "client" or "server"
1192 SSLProtocol expectProt
,
1193 SSLProtocol gotProt
)
1195 if(expectProt
== SSL_PROTOCOL_IGNORE
) {
1196 /* app says "don't bopther checking" */
1199 if(expectProt
== gotProt
) {
1202 printf("***%s: Expected return %s; got %s\n", whichSide
,
1203 sslGetProtocolVersionString(expectProt
),
1204 sslGetProtocolVersionString(gotProt
));
1208 int sslVerifyCipher(
1209 char *whichSide
, // "client" or "server"
1210 SSLCipherSuite expectCipher
,
1211 SSLCipherSuite gotCipher
)
1213 if(expectCipher
== SSL_CIPHER_IGNORE
) {
1214 /* app says "don't bopther checking" */
1217 if(expectCipher
== gotCipher
) {
1220 printf("***%s: Expected return %s; got %s\n", whichSide
,
1221 sslGetCipherSuiteString(expectCipher
),
1222 sslGetCipherSuiteString(gotCipher
));
1227 OSStatus
sslSetProtocols(
1229 const char *acceptedProts
,
1230 SSLProtocol tryVersion
) // only used if acceptedProts NULL
1236 printf("***SSLSetProtocolVersionEnabled not supported in this config.\n");
1239 ortn
= SSLSetProtocolVersionEnabled(ctx
, kSSLProtocolAll
, false);
1241 printSslErrStr("SSLSetProtocolVersionEnabled(all off)", ortn
);
1244 for(const char *cp
= acceptedProts
; *cp
; cp
++) {
1248 prot
= kSSLProtocol2
;
1251 prot
= kSSLProtocol3
;
1254 prot
= kTLSProtocol1
;
1257 printf("***BRRZAP! Bad acceptedProts string %s. Aborting.\n", acceptedProts
);
1260 ortn
= SSLSetProtocolVersionEnabled(ctx
, prot
, true);
1262 printSslErrStr("SSLSetProtocolVersionEnabled", ortn
);
1268 ortn
= SSLSetProtocolVersion(ctx
, tryVersion
);
1270 printSslErrStr("SSLSetProtocolVersion", ortn
);
1274 return errSecSuccess
;
1278 char *whichSide
, // "client" or "server"
1279 SslAppTestParams
*params
)
1281 printf("%s status:\n", whichSide
);
1282 if(params
->acceptedProts
) {
1283 printf(" Allowed SSL versions : %s\n", params
->acceptedProts
);
1286 printf(" Attempted SSL version : %s\n",
1287 sslGetProtocolVersionString(params
->tryVersion
));
1289 printf(" Result : %s\n", sslGetSSLErrString(params
->ortn
));
1290 printf(" Negotiated SSL version : %s\n",
1291 sslGetProtocolVersionString(params
->negVersion
));
1292 printf(" Negotiated CipherSuite : %s\n",
1293 sslGetCipherSuiteString(params
->negCipher
));
1294 if(params
->certState
!= kSSLClientCertNone
) {
1295 printf(" Client Cert State : %s\n",
1296 sslGetClientCertStateString(params
->certState
));
1301 /* print a '.' every few seconds to keep UI alive while connecting */
1302 static time_t lastTime
= (time_t)0;
1303 #define TIME_INTERVAL 3
1307 time_t thisTime
= time(0);
1309 if((thisTime
- lastTime
) >= TIME_INTERVAL
) {
1310 printf("."); fflush(stdout
);
1311 lastTime
= thisTime
;
1316 /* main server pthread body */
1317 static void *sslServerThread(void *arg
)
1319 SslAppTestParams
*testParams
= (SslAppTestParams
*)arg
;
1322 status
= sslAppServe(testParams
);
1323 pthread_exit((void*)status
);
1325 return (void *)status
;
1329 * Run one session, with the server in a separate thread.
1330 * On entry, serverParams->port is the port we attempt to run on;
1331 * the server thread may overwrite that with a different port if it's
1332 * unable to open the port we specify. Whatever is left in
1333 * serverParams->port is what's used for the client side.
1335 #define CLIENT_WAIT_SECONDS 1
1337 SslAppTestParams
*serverParams
,
1338 SslAppTestParams
*clientParams
,
1339 const char *testDesc
)
1341 pthread_t serverPthread
;
1345 if(testDesc
&& !clientParams
->quiet
) {
1346 printf("===== %s =====\n", testDesc
);
1349 if(pthread_mutex_init(&serverParams
->pthreadMutex
, NULL
)) {
1350 printf("***Error initializing mutex; aborting.\n");
1353 if(pthread_cond_init(&serverParams
->pthreadCond
, NULL
)) {
1354 printf("***Error initializing pthreadCond; aborting.\n");
1357 serverParams
->serverReady
= false; // server sets true
1359 int result
= pthread_create(&serverPthread
, NULL
,
1360 sslServerThread
, serverParams
);
1362 printf("***Error starting up server thread; aborting.\n");
1366 /* wait for server to set up a socket we can connect to */
1367 if(pthread_mutex_lock(&serverParams
->pthreadMutex
)) {
1368 printf("***Error acquiring server lock; aborting.\n");
1371 while(!serverParams
->serverReady
) {
1372 if(pthread_cond_wait(&serverParams
->pthreadCond
, &serverParams
->pthreadMutex
)) {
1373 printf("***Error waiting server thread; aborting.\n");
1377 pthread_mutex_unlock(&serverParams
->pthreadMutex
);
1378 pthread_cond_destroy(&serverParams
->pthreadCond
);
1379 pthread_mutex_destroy(&serverParams
->pthreadMutex
);
1381 clientParams
->port
= serverParams
->port
;
1382 clientRtn
= sslAppClient(clientParams
);
1383 /* server doesn't shut down its socket until it sees this */
1384 serverParams
->clientDone
= 1;
1385 result
= pthread_join(serverPthread
, &serverRtn
);
1387 printf("***pthread_join returned %d, aborting\n", result
);
1391 if(serverParams
->verbose
) {
1392 sslShowResult("server", serverParams
);
1394 if(clientParams
->verbose
) {
1395 sslShowResult("client", clientParams
);
1398 /* verify results */
1400 ourRtn
+= sslVerifyRtn("server", serverParams
->expectRtn
, serverParams
->ortn
);
1401 ourRtn
+= sslVerifyRtn("client", clientParams
->expectRtn
, clientParams
->ortn
);
1402 ourRtn
+= sslVerifyProtVers("server", serverParams
->expectVersion
,
1403 serverParams
->negVersion
);
1404 ourRtn
+= sslVerifyProtVers("client", clientParams
->expectVersion
,
1405 clientParams
->negVersion
);
1406 ourRtn
+= sslVerifyClientCertState("server", serverParams
->expectCertState
,
1407 serverParams
->certState
);
1408 ourRtn
+= sslVerifyClientCertState("client", clientParams
->expectCertState
,
1409 clientParams
->certState
);
1410 if(serverParams
->ortn
== errSecSuccess
) {
1411 ourRtn
+= sslVerifyCipher("server", serverParams
->expectCipher
,
1412 serverParams
->negCipher
);
1414 if(clientParams
->ortn
== errSecSuccess
) {
1415 ourRtn
+= sslVerifyCipher("client", clientParams
->expectCipher
,
1416 clientParams
->negCipher
);
1421 static bool isCertRoot(
1422 SecCertificateRef cert
)
1424 /* FIXME - per Radar 3247491, the Sec-level functions we'd like to use for this
1425 * haven't been written yet...
1426 CSSM_X509_NAME subject;
1427 CSSM_X509_NAME issuer;
1434 * Add all of the roots in a given KC to SSL ctx's trusted anchors.
1436 OSStatus
sslAddTrustedRoots(
1438 SecKeychainRef keychain
,
1439 bool *foundOne
) // RETURNED, true if we found
1440 // at least one root cert
1443 SecCertificateRef secCert
;
1444 SecKeychainSearchRef srch
;
1447 ortn
= SecKeychainSearchCreateFromAttributes(keychain
,
1448 kSecCertificateItemClass
,
1452 printSslErrStr("SecKeychainSearchCreateFromAttributes", ortn
);
1457 * Only use root certs. Not an error if we don't find any.
1460 ortn
= SecKeychainSearchCopyNext(srch
,
1461 (SecKeychainItemRef
*)&secCert
);
1466 /* see if it's a root */
1467 if(!isCertRoot(secCert
)) {
1471 /* Tell Secure Transport to trust this one. */
1472 ortn
= addTrustedSecCert(ctx
, secCert
, false);
1475 printSslErrStr("addTrustedSecCert", ortn
);
1480 } while(ortn
== errSecSuccess
);
1482 return errSecSuccess
;
1486 * Wrapper for sslIdentPicker, with optional trusted anchor specified as a filename.
1488 OSStatus
sslIdentityPicker(
1489 SecKeychainRef kcRef
, // NULL means use default list
1490 const char *trustedAnchor
, // optional additional trusted anchor
1491 bool includeRoot
, // true --> root is appended to outArray
1492 // false --> root not included
1493 CFArrayRef
*outArray
) // created and RETURNED
1495 SecCertificateRef trustedCert
= NULL
;
1499 ortn
= sslReadAnchor(trustedAnchor
, &trustedCert
);
1501 printf("***Error reading %s. sslIdentityPicker proceeding with no anchor.\n",
1506 ortn
= sslIdentPicker(kcRef
, trustedCert
, includeRoot
, outArray
);
1508 CFRelease(trustedCert
);
1514 * Given a keychain name, convert it into a full path using the "SSL regression
1515 * test suite algorithm". The Sec layer by default locates root root's keychains
1516 * in different places depending on whether we're actually logged in as root
1517 * or running via e.g. cron, so we force the location of root keychains to
1518 * a hard-coded path. User keychain names we leave alone.
1520 void sslKeychainPath(
1522 char *kcPath
) // allocd by caller, MAXPATHLEN
1524 if(kcName
[0] == '\0') {
1527 else if(geteuid() == 0) {
1529 sprintf(kcPath
, "/Library/Keychains/%s", kcName
);
1532 /* user, leave alone */
1533 strcpy(kcPath
, kcName
);
1537 /* Verify presence of required file. Returns nonzero if not found. */
1538 int sslCheckFile(const char *path
)
1542 if(stat(path
, &sb
)) {
1543 printf("***Can't find file %s.\n", path
);
1544 printf(" Try running in the build directory, perhaps after running the\n"
1545 " makeLocalCert script.\n");
1553 /* Stringify a SSL_ECDSA_NamedCurve */
1554 extern const char *sslCurveString(
1555 SSL_ECDSA_NamedCurve namedCurve
)
1557 static char unk
[100];
1559 switch(namedCurve
) {
1560 case SSL_Curve_None
: return "Curve_None";
1561 case SSL_Curve_secp256r1
: return "secp256r1";
1562 case SSL_Curve_secp384r1
: return "secp384r1";
1563 case SSL_Curve_secp521r1
: return "secp521r1";
1565 sprintf(unk
, "Unknown <%d>", (int)namedCurve
);
1573 #include <Security/SecCertificatePriv.h>
1574 #include <Security/SecKey.h>
1576 SecKeyRef
create_private_key_from_der(bool ecdsa
, const unsigned char *pkey_der
, size_t pkey_der_len
)
1579 CFErrorRef error
= NULL
;
1580 CFDataRef keyData
= CFDataCreate(kCFAllocatorDefault
, pkey_der
, pkey_der_len
);
1581 CFMutableDictionaryRef parameters
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, NULL
, NULL
);
1582 CFDictionarySetValue(parameters
, kSecAttrKeyType
, ecdsa
?kSecAttrKeyTypeECSECPrimeRandom
:kSecAttrKeyTypeRSA
);
1583 CFDictionarySetValue(parameters
, kSecAttrKeyClass
, kSecAttrKeyClassPrivate
);
1584 privKey
= SecKeyCreateWithData(keyData
, parameters
, &error
);
1585 CFReleaseNull(keyData
);
1586 CFReleaseNull(parameters
);
1587 CFReleaseNull(error
);
1591 CFArrayRef
chain_from_der(bool ecdsa
, const unsigned char *pkey_der
, size_t pkey_der_len
, const unsigned char *cert_der
, size_t cert_der_len
)
1593 SecKeyRef pkey
= NULL
;
1594 SecCertificateRef cert
= NULL
;
1595 SecIdentityRef ident
= NULL
;
1596 CFArrayRef items
= NULL
;
1598 require(pkey
= create_private_key_from_der(ecdsa
, pkey_der
, pkey_der_len
), errOut
);
1599 require(cert
= SecCertificateCreateWithBytes(kCFAllocatorDefault
, cert_der
, cert_der_len
), errOut
);
1600 require(ident
= SecIdentityCreate(kCFAllocatorDefault
, cert
, pkey
), errOut
);
1601 require(items
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&ident
, 1, &kCFTypeArrayCallBacks
), errOut
);
1604 CFReleaseSafe(pkey
);
1605 CFReleaseSafe(cert
);
1606 CFReleaseSafe(ident
);