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 (clientauth)
44 SSL Client Auth tests:
46 Test both the client and server side.
48 Server side test goals:
49 Verify Server behavior in the following cases:
51 - when using kTryAuthenticate vs kAlwaysAuthenticate
52 - with or without breakOnClientAuth.
53 - AnonDH and PSK ciphersuites.
55 - Client sends back no cert vs a cert.
56 - Client sends back an unsupported cert.
57 - Client sends back a malformed cert.
58 - Client cert is trusted vs untrusted.
59 - Client does not have private key (ie: Certificate Verify message should fail).
61 - handshake pass or fail
62 - SSLGetClientCertificateState returns expected results
64 Client side test goals:
66 - with or without breakOnCertRequest.
69 - No client cert requested, vs client cert requested vs client cert required.
71 - handshake pass or fail
72 - SSLGetClientCertificateState returns expected results
77 static OSStatus SocketWrite(SSLConnectionRef conn, const void *data, size_t *length)
80 uint8_t *ptr = (uint8_t *)data;
85 ret = write((int)conn, ptr, len);
86 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
96 *length = *length - len;
100 static OSStatus SocketRead(SSLConnectionRef conn, void *data, size_t *length)
102 size_t len = *length;
103 uint8_t *ptr = (uint8_t *)data;
108 ret = read((int)conn, ptr, len);
109 } while ((ret < 0) && (errno == EINPROGRESS || errno == EAGAIN || errno == EINTR));
114 printf("read error(%d): ret=%zd, errno=%d\n", (int)conn, ret, errno);
119 *length = *length - len;
120 return errSecSuccess;
128 int auth; //expected client auth behavior of the server (0=no request, 1=optional , 2=required)
131 static ssl_client_handle *
132 ssl_client_handle_create(bool break_on_req, int comm, CFArrayRef certs, CFArrayRef trustedCA, int auth)
134 ssl_client_handle *handle = calloc(1, sizeof(ssl_client_handle));
135 SSLContextRef ctx = SSLCreateContext(kCFAllocatorDefault, kSSLClientSide, kSSLStreamType);
137 require(handle, out);
140 require_noerr(SSLSetIOFuncs(ctx,
141 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
142 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)comm), out);
143 static const char *peer_domain_name = "localhost";
144 require_noerr(SSLSetPeerDomainName(ctx, peer_domain_name,
145 strlen(peer_domain_name)), out);
147 CFArrayRef trustedRoots = NULL;
148 require_noerr(SSLCopyTrustedRoots(ctx, &trustedRoots), out);
149 #if (TARGET_OS_MAC && !TARGET_OS_IPHONE)
150 require(trustedRoots, out);
152 CFReleaseNull(trustedRoots);
153 require_noerr(SSLSetTrustedRoots(ctx, trustedCA, true), out);
154 require_noerr(SSLCopyTrustedRoots(ctx, &trustedRoots), out);
155 CFReleaseNull(trustedRoots);
157 /* Setting client certificate in advance */
158 if (!break_on_req && certs) {
159 require_noerr(SSLSetCertificate(ctx, certs), out);
163 require_noerr(SSLSetSessionOption(ctx,
164 kSSLSessionOptionBreakOnCertRequested, true), out);
167 handle->break_on_req = break_on_req;
169 handle->certs = certs;
184 ssl_client_handle_destroy(ssl_client_handle *handle)
187 SSLClose(handle->st);
188 CFRelease(handle->st);
193 static void *securetransport_ssl_client_thread(void *arg)
196 ssl_client_handle * ssl = (ssl_client_handle *)arg;
197 SSLContextRef ctx = ssl->st;
198 bool got_client_cert_req = false;
199 SSLSessionState ssl_state;
200 SSLSignatureAndHashAlgorithm *sigAlgs;
201 unsigned int numSigAlgs, numTypes;
202 SSLClientAuthenticationType *authTypes;
204 pthread_setname_np("client thread");
206 require_noerr(ortn = SSLGetSessionState(ctx, &ssl_state), out);
207 require_action(ssl_state == kSSLIdle, out, ortn = -1);
210 ortn = SSLHandshake(ctx);
211 require_noerr(SSLGetSessionState(ctx, &ssl_state), out);
213 if (ortn == errSSLClientCertRequested) {
214 require_noerr(SSLGetNumberOfSignatureAlgorithms(ctx, &numSigAlgs), out);
215 sigAlgs = malloc(numSigAlgs * sizeof(SSLSignatureAndHashAlgorithm));
216 require_noerr(SSLGetSignatureAlgorithms(ctx, sigAlgs, &numSigAlgs), out);
217 require_noerr(SSLGetNumberOfClientAuthTypes(ctx, &numTypes), out);
218 authTypes = malloc(numTypes * sizeof(SSLClientAuthenticationType));
219 require_noerr(SSLGetClientAuthTypes(ctx, authTypes, &numTypes), out);
220 require_string(ssl->auth, out, "cert req not expected");
221 require_string(ssl_state == kSSLHandshake, out, "wrong client handshake state after errSSLClientCertRequested");
222 require_string(!got_client_cert_req, out, "second client cert req");
223 got_client_cert_req = true;
225 SSLClientCertificateState clientState;
226 SSLGetClientCertificateState(ctx, &clientState);
227 require_string(clientState == kSSLClientCertRequested, out, "Wrong client cert state after cert request");
229 require_string(ssl->break_on_req, out, "errSSLClientCertRequested in run not testing that");
231 require_noerr(SSLSetCertificate(ctx, ssl->certs), out);
234 } else if (ortn == errSSLWouldBlock) {
235 require_string(ssl_state == kSSLHandshake, out, "Wrong client handshake state after errSSLWouldBlock");
237 } while (ortn == errSSLWouldBlock || ortn == errSSLClientCertRequested);
239 require_string((got_client_cert_req || !ssl->auth || !ssl->break_on_req), out, "didn't get client cert req as expected");
245 pthread_exit((void *)(intptr_t)ortn);
253 SSLAuthenticate client_auth;
257 static ssl_server_handle *
258 ssl_server_handle_create(SSLAuthenticate client_auth, int comm, CFArrayRef certs, CFArrayRef trustedCA)
260 ssl_server_handle *handle = calloc(1, sizeof(ssl_server_handle));
261 SSLContextRef ctx = SSLCreateContext(kCFAllocatorDefault, kSSLServerSide, kSSLStreamType);
263 require(handle, out);
266 require_noerr(SSLSetIOFuncs(ctx,
267 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
268 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)comm), out);
270 require_noerr(SSLSetCertificate(ctx, certs), out);
272 require_noerr(SSLSetTrustedRoots(ctx, trustedCA, true), out);
274 SSLAuthenticate auth;
275 require_noerr(SSLSetClientSideAuthenticate(ctx, client_auth), out);
276 require_noerr(SSLGetClientSideAuthenticate(ctx, &auth), out);
277 require(auth == client_auth, out);
279 handle->client_auth = client_auth;
281 handle->certs = certs;
296 ssl_server_handle_destroy(ssl_server_handle *handle)
299 SSLClose(handle->st);
300 CFRelease(handle->st);
305 static void *securetransport_ssl_server_thread(void *arg)
308 ssl_server_handle * ssl = (ssl_server_handle *)arg;
309 SSLContextRef ctx = ssl->st;
310 SSLSessionState ssl_state;
312 pthread_setname_np("server thread");
314 require_noerr(ortn = SSLGetSessionState(ctx, &ssl_state), out);
315 require_action(ssl_state == kSSLIdle, out, ortn = -1);
318 ortn = SSLHandshake(ctx);
319 require_noerr(SSLGetSessionState(ctx, &ssl_state), out);
321 if (ortn == errSSLWouldBlock) {
322 require_action(ssl_state == kSSLHandshake, out, ortn = -1);
324 } while (ortn == errSSLWouldBlock);
326 require_noerr_quiet(ortn, out);
328 require_action(ssl_state == kSSLConnected, out, ortn = -1);
333 pthread_exit((void *)(intptr_t)ortn);
339 -(void)testClientAuth
341 pthread_t client_thread, server_thread;
342 CFArrayRef server_certs = server_chain();
343 CFArrayRef trusted_ca = trusted_roots();
345 XCTAssert(server_certs, "got server certs");
346 XCTAssert(trusted_ca, "got trusted roots");
350 for (i = 0; i < 3; i++) {/* client side cert: 0 = no cert, 1 = trusted cert, 2 = untrusted cert. */
351 for (j = 0; j < 2; j++) { /* break on cert request */
352 for (k = 0; k < 3; k++) { /* server behvior: 0 = no cert request, 1 = optional cert, 2 = required cert */
355 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) exit(errno);
356 fcntl(sp[0], F_SETNOSIGPIPE, 1);
357 fcntl(sp[1], F_SETNOSIGPIPE, 1);
359 bool break_on_req = (j != 0);
360 SSLAuthenticate auth = (k == 0) ? kNeverAuthenticate
361 : (k == 1) ? kTryAuthenticate
362 : kAlwaysAuthenticate;
364 CFArrayRef client_certs = (i == 0) ? NULL
365 : (i == 1) ? trusted_client_chain()
366 : untrusted_client_chain();
368 ssl_client_handle *client;
369 client = ssl_client_handle_create(break_on_req, sp[0], client_certs, trusted_ca, auth);
371 ssl_server_handle *server;
372 server = ssl_server_handle_create(auth, sp[1], server_certs, trusted_ca);
374 pthread_create(&client_thread, NULL, securetransport_ssl_client_thread, client);
375 pthread_create(&server_thread, NULL, securetransport_ssl_server_thread, server);
377 intptr_t server_err, client_err;
378 int expected_server_err = 0, expected_client_err = 0;
379 SSLClientCertificateState client_cauth_state, server_cauth_state;
380 SSLClientCertificateState expected_client_cauth_state = 0, expected_server_cauth_state = 0;
383 if(((k == 2) && (i != 1)) // Server requires good cert, but client not sending good cert,
384 || ((k == 1) && (i == 2)) // Or server request optionally, and client sending bad cert.
386 expected_client_err = errSSLPeerCertUnknown;
387 expected_server_err = errSSLXCertChainInvalid;
393 expected_client_cauth_state = kSSLClientCertRequested;
394 expected_server_cauth_state = kSSLClientCertRequested;
396 expected_client_cauth_state = kSSLClientCertSent;
397 expected_server_cauth_state = kSSLClientCertSent;
401 pthread_join(client_thread, (void*)&client_err);
402 pthread_join(server_thread, (void*)&server_err);
405 XCTAssertEqual(errSecSuccess, SSLGetClientCertificateState(client->st, &client_cauth_state), "SSLGetClientCertificateState (client %d:%d:%d)", i, j, k);
406 XCTAssertEqual(errSecSuccess, SSLGetClientCertificateState(client->st, &server_cauth_state), "SSLGetClientCertificateState (server %d:%d:%d)", i, j, k);
408 XCTAssertEqual(client_err, expected_client_err, "unexpected error %d!=%d (client %d:%d:%d)", (int)client_err, expected_client_err, i, j, k);
409 XCTAssertEqual(server_err, expected_server_err, "unexpected error %d!=%d (server %d:%d:%d)", (int)server_err, expected_server_err, i, j, k);
411 XCTAssertEqual(client_cauth_state, expected_client_cauth_state, "unexpected client auth state %d!=%d (client %d:%d:%d)", client_cauth_state, expected_client_cauth_state, i, j, k);
412 XCTAssertEqual(server_cauth_state, expected_server_cauth_state, "unexpected client auth state %d!=%d (server %d:%d:%d)", server_cauth_state, expected_server_cauth_state, i, j, k);
415 ssl_server_handle_destroy(server);
416 ssl_client_handle_destroy(client);
418 CFReleaseSafe(client_certs);
423 CFReleaseSafe(server_certs);
424 CFReleaseSafe(trusted_ca);
429 #pragma clang diagnostic pop