8 #include <CoreFoundation/CoreFoundation.h>
10 #include <AssertMacros.h>
11 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
12 #include <Security/SecureTransport.h>
13 #include <Security/SecPolicy.h>
14 #include <Security/SecTrust.h>
15 #include <Security/SecIdentity.h>
16 #include <Security/SecIdentityPriv.h>
17 #include <Security/SecCertificatePriv.h>
18 #include <Security/SecKeyPriv.h>
19 #include <Security/SecItem.h>
20 #include <Security/SecRandom.h>
22 #include <utilities/array_size.h>
23 #include <utilities/SecCFRelease.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include <mach/mach_time.h>
32 #include <Security/SecRSAKey.h>
35 #include "ssl-utils.h"
36 #import "STLegacyTests.h"
38 #pragma clang diagnostic push
39 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
41 @implementation STLegacyTests (sessioncache)
44 SSL Session Cache tests:
46 Test both the client and server side.
49 - Make sure that resumption fails after session cache TTL.
52 - handshake pass or fail
57 static OSStatus SocketWrite(SSLConnectionRef conn, const void *data, size_t *length)
60 uint8_t *ptr = (uint8_t *)data;
65 ret = write((int)conn, ptr, len);
66 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
75 *length = *length - len;
79 static OSStatus SocketRead(SSLConnectionRef conn, void *data, size_t *length)
82 uint8_t *ptr = (uint8_t *)data;
87 ret = read((int)conn, ptr, len);
88 } while ((ret < 0) && (errno == EINPROGRESS || errno == EAGAIN || errno == EINTR));
93 printf("read error(%d): ret=%zd, errno=%d\n", (int)conn, ret, errno);
98 *length = *length - len;
108 static ssl_client_handle *
109 ssl_client_handle_create(int comm, bool anyRoot, CFArrayRef trustedCA, bool trustedCAOnly, CFArrayRef trustedLeafs, uint32_t cache_ttl, uintptr_t peerID)
111 ssl_client_handle *handle = calloc(1, sizeof(ssl_client_handle));
112 SSLContextRef ctx = SSLCreateContext(kCFAllocatorDefault, kSSLClientSide, kSSLStreamType);
114 require(handle, out);
117 require_noerr(SSLSetIOFuncs(ctx,
118 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
119 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)comm), out);
120 static const char *peer_domain_name = "localhost";
121 require_noerr(SSLSetPeerDomainName(ctx, peer_domain_name,
122 strlen(peer_domain_name)), out);
124 require_noerr(SSLSetAllowsAnyRoot(ctx, anyRoot), out);
125 require_noerr(SSLSetTrustedRoots(ctx, trustedCA, trustedCAOnly), out);
126 #if !TARGET_OS_IPHONE
127 require_noerr(SSLSetTrustedLeafCertificates(ctx, trustedLeafs), out);
128 CFArrayRef recvTrustedLeafs = NULL;
129 require_noerr(SSLCopyTrustedLeafCertificates(ctx, &recvTrustedLeafs), out);
132 require_noerr(SSLSetSessionCacheTimeout(ctx, cache_ttl), out);
134 require_noerr(SSLSetPeerID(ctx, &peerID, sizeof(peerID)), out);
151 ssl_client_handle_destroy(ssl_client_handle *handle)
154 SSLClose(handle->st);
155 CFRelease(handle->st);
160 static void *securetransport_ssl_client_thread(void *arg)
163 ssl_client_handle * ssl = (ssl_client_handle *)arg;
164 SSLContextRef ctx = ssl->st;
165 SSLSessionState ssl_state;
167 pthread_setname_np("client thread");
169 require_noerr(ortn = SSLGetSessionState(ctx, &ssl_state), out);
170 require_action(ssl_state == kSSLIdle, out, ortn = -1);
173 ortn = SSLHandshake(ctx);
174 require_noerr(SSLGetSessionState(ctx, &ssl_state), out);
176 if (ortn == errSSLWouldBlock) {
177 require_string(ssl_state == kSSLHandshake, out, "Wrong client handshake state after errSSLWouldBlock");
179 } while (ortn == errSSLWouldBlock);
184 pthread_exit((void *)(intptr_t)ortn);
196 static ssl_server_handle *
197 ssl_server_handle_create(int comm, CFArrayRef certs, uint32_t cache_ttl)
199 ssl_server_handle *handle = calloc(1, sizeof(ssl_server_handle));
200 SSLContextRef ctx = SSLCreateContext(kCFAllocatorDefault, kSSLServerSide, kSSLStreamType);
201 SSLCipherSuite cipher = TLS_RSA_WITH_AES_256_CBC_SHA256;
202 uintptr_t peerID = 0xdeadbeef;
204 require(handle, out);
207 require_noerr(SSLSetIOFuncs(ctx,
208 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
209 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)comm), out);
211 require_noerr(SSLSetCertificate(ctx, certs), out);
213 require_noerr(SSLSetEnabledCiphers(ctx, &cipher, 1), out);
215 require_noerr(SSLSetSessionCacheTimeout(ctx, cache_ttl), out);
217 require_noerr(SSLSetPeerID(ctx, &peerID, sizeof(peerID)), out);
220 handle->certs = certs;
235 ssl_server_handle_destroy(ssl_server_handle *handle)
238 SSLClose(handle->st);
239 CFRelease(handle->st);
244 static void *securetransport_ssl_server_thread(void *arg)
247 ssl_server_handle * ssl = (ssl_server_handle *)arg;
248 SSLContextRef ctx = ssl->st;
249 SSLSessionState ssl_state;
251 pthread_setname_np("server thread");
253 require_noerr(ortn = SSLGetSessionState(ctx, &ssl_state), out);
254 require_action(ssl_state == kSSLIdle, out, ortn = -1);
257 ortn = SSLHandshake(ctx);
258 require_noerr(SSLGetSessionState(ctx, &ssl_state), out);
260 if (ortn == errSSLWouldBlock) {
261 require_action(ssl_state == kSSLHandshake, out, ortn = -1);
263 } while (ortn == errSSLWouldBlock);
265 require_noerr_quiet(ortn, out);
267 require_action(ssl_state == kSSLConnected, out, ortn = -1);
272 pthread_exit((void *)(intptr_t)ortn);
277 -(void)cache_ttl_test
279 pthread_t client_thread, server_thread;
280 CFArrayRef server_certs = server_chain();
281 CFArrayRef trusted_ca = trusted_roots();
283 XCTAssert(server_certs, "ttl: got server certs");
284 XCTAssert(trusted_ca, "ttl: got trusted roots");
288 for (i = 0; i < 2; i++) { // client cache TTL
289 for (j = 0; j < 2; j++) { // Server cache TTL
290 for (k = 0; k < 2; k++) {
291 ssl_client_handle *client = NULL;
292 ssl_server_handle *server = NULL;
295 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) exit(errno);
296 fcntl(sp[0], F_SETNOSIGPIPE, 1);
297 fcntl(sp[1], F_SETNOSIGPIPE, 1);
299 client = ssl_client_handle_create(sp[0], false, trusted_ca, true, NULL, i, (i<<8)|(j+1));
300 XCTAssert(client != NULL, "ttl: could not create client handle (%d:%d:%d)", i, j, k);
301 require(client, errOut);
303 server = ssl_server_handle_create(sp[1], server_certs, j);
304 XCTAssert(server != NULL, "ttl: could not create server handle (%d:%d:%d)", i, j, k);
305 require(server, errOut);
306 pthread_create(&client_thread, NULL, securetransport_ssl_client_thread, client);
307 pthread_create(&server_thread, NULL, securetransport_ssl_server_thread, server);
309 intptr_t server_err, client_err;
311 pthread_join(client_thread, (void*)&client_err);
312 pthread_join(server_thread, (void*)&server_err);
315 unsigned char sessionID[32];
316 size_t sessionIDLength = sizeof(sessionID);
318 XCTAssertEqual(client_err, 0, "ttl: unexpected error %ld (client %d:%d:%d)", client_err, i, j, k);
319 XCTAssertEqual(server_err, 0, "ttl: unexpected error %ld (server %d:%d:%d)", server_err, i, j, k);
320 XCTAssertEqual(errSecSuccess, SSLGetResumableSessionInfo(client->st, &resumed, sessionID, &sessionIDLength), "SSLGetResumableSessionInfo");
322 XCTAssertEqual((bool)resumed, (bool)(k && (!i) && (!j)), "ttl: Unexpected resumption state=%d (%d:%d:%d)", resumed, i, j, k);
325 ssl_server_handle_destroy(server);
326 ssl_client_handle_destroy(client);
328 /* Sleep two seconds so that Session cache TTL can expire */
334 CFReleaseSafe(server_certs);
335 CFReleaseSafe(trusted_ca);
338 -(void) cache_trust_test
340 pthread_t client_thread, server_thread;
341 CFArrayRef server_certs = server_chain();
342 CFArrayRef trusted_ca = trusted_roots();
343 CFMutableArrayRef trusted_ca2 = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, trusted_ca);
344 CFArrayAppendArray(trusted_ca2, trusted_ca, CFRangeMake(0, CFArrayGetCount(trusted_ca)));
346 XCTAssert(server_certs, "trust: got server certs");
347 XCTAssert(trusted_ca, "trust: got trusted roots");
348 XCTAssert(trusted_ca2, "trust: got trusted roots extra");
350 int any, ca, caonly, leaf, k;
352 // Test cache and trust options:
355 for (any=0; any<2; any++) // any root ?
356 for (ca=0; ca<2; ca++) // trustedCA ?
357 for (caonly=0; caonly<2; caonly++) // leaf>
362 for (leaf=0; leaf<2; leaf++)
365 // attempt initial connection, then resumed connection, but all with same peer id (0xdeadbeef)
366 for (k=0; k<2; k++) {
367 ssl_client_handle *client = NULL;
368 ssl_server_handle *server = NULL;
371 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) exit(errno);
372 fcntl(sp[0], F_SETNOSIGPIPE, 1);
373 fcntl(sp[1], F_SETNOSIGPIPE, 1);
375 client = ssl_client_handle_create(sp[0], any, ca?trusted_ca:trusted_ca2, caonly, leaf?NULL:trusted_ca, 300, 0xdeadbeef);
376 XCTAssert(client!=NULL, "trust: could not create client handle (%d:%d:%d:%d:%d)", any, ca, caonly, leaf, k);
377 require(client, errOut);
379 server = ssl_server_handle_create(sp[1], server_certs, 300);
380 XCTAssert(server != NULL, "trust: could not create server handle (%d:%d:%d:%d:%d)", any, ca, caonly, leaf, k);
381 require(server, errOut);
383 pthread_create(&client_thread, NULL, securetransport_ssl_client_thread, client);
384 pthread_create(&server_thread, NULL, securetransport_ssl_server_thread, server);
386 intptr_t server_err, client_err;
388 pthread_join(client_thread, (void*)&client_err);
389 pthread_join(server_thread, (void*)&server_err);
392 unsigned char sessionID[32];
393 size_t sessionIDLength = sizeof(sessionID);
395 XCTAssertEqual(client_err, 0, "trust: unexpected error %ld (client %d:%d:%d:%d:%d)", client_err, any, ca, caonly, leaf, k);
396 XCTAssertEqual(server_err, 0, "trust: unexpected error %ld (server %d:%d:%d:%d:%d)", server_err, any, ca, caonly, leaf, k);
397 XCTAssertEqual(errSecSuccess, SSLGetResumableSessionInfo(client->st, &resumed, sessionID, &sessionIDLength), "SSLGetResumableSessionInfo");
399 XCTAssertEqual((bool)resumed, (bool)(k), "trust: Unexpected resumption state=%d (%d:%d:%d:%d:%d)", resumed, any, ca, caonly, leaf, k);
402 ssl_server_handle_destroy(server);
403 ssl_client_handle_destroy(client);
408 CFReleaseSafe(server_certs);
409 CFReleaseSafe(trusted_ca);
412 -(void) testSessionCache
415 [self cache_ttl_test];
417 [self cache_trust_test];
422 #pragma clang diagnostic pop