7 #include <CoreFoundation/CoreFoundation.h>
9 #include <AssertMacros.h>
10 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
11 #include <Security/SecureTransport.h>
12 #include <Security/SecPolicy.h>
13 #include <Security/SecTrust.h>
14 #include <Security/SecIdentity.h>
15 #include <Security/SecIdentityPriv.h>
16 #include <Security/SecCertificatePriv.h>
17 #include <Security/SecKeyPriv.h>
18 #include <Security/SecItem.h>
19 #include <Security/SecRandom.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <mach/mach_time.h>
29 #include <Security/SecRSAKey.h>
32 #include "ssl-utils.h"
33 #import "STLegacyTests.h"
35 @implementation STLegacyTests (sni)
46 #pragma mark SecureTransport support
49 static void hexdump(const uint8_t *bytes, size_t len) {
51 printf("socket write(%p, %lu)\n", bytes, len);
52 for (ix = 0; ix < len; ++ix) {
55 printf("%02X ", bytes[ix]);
60 #define hexdump(bytes, len)
64 static OSStatus SocketWrite(SSLConnectionRef h, const void *data, size_t *length)
67 uint8_t *ptr = (uint8_t *)data;
73 ret = write((int)h, ptr, len);
74 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
83 *length = *length - len;
87 static OSStatus SocketRead(SSLConnectionRef h, void *data, size_t *length)
90 uint8_t *ptr = (uint8_t *)data;
95 ret = read((int)h, ptr, len);
96 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
101 printf("read error(%d): ret=%zd, errno=%d\n", (int)h, ret, errno);
106 *length = *length - len;
107 return errSecSuccess;
110 static char peername[] = "localhost";
112 static void *securetransport_server_thread(void *arg)
115 ssl_test_handle * ssl = (ssl_test_handle *)arg;
116 SSLContextRef ctx = ssl->handle;
117 CFArrayRef server_certs = server_chain();
120 ortn = SSLHandshake(ctx);
121 } while (ortn == errSSLWouldBlock);
123 ok(ortn==errSSLClientHelloReceived, "Unexpected Handshake exit code");
125 if (ortn == errSSLClientHelloReceived) {
128 SSLCopyRequestedPeerNameLength(ctx, &length);
130 sni = malloc(length);
131 SSLCopyRequestedPeerName(ctx, sni, &length);
134 SSLProtocol version = 0;
135 require_noerr(SSLGetProtocolVersionMax(ctx, &version), out);
136 if (version == kSSLProtocol3) {
137 ok(sni==NULL, "Unexpected SNI");
140 length == sizeof(peername) &&
141 (memcmp(sni, peername, sizeof(peername))==0),
142 "SNI does not match");
144 require_noerr(SSLSetCertificate(ctx, server_certs), out);
150 SSLDisposeContext(ctx);
152 CFReleaseSafe(server_certs);
154 pthread_exit((void *)(intptr_t)ortn);
158 static void *securetransport_client_thread(void *arg)
161 ssl_test_handle * ssl = (ssl_test_handle *)arg;
162 SSLContextRef ctx = ssl->handle;
165 ortn = SSLHandshake(ctx);
166 } while (ortn == errSSLWouldBlock || ortn != errSSLClosedGraceful);
169 SSLDisposeContext(ctx);
172 pthread_exit((void *)(intptr_t)ortn);
176 static SSLCipherSuite ciphers[] = {
177 TLS_RSA_WITH_AES_128_CBC_SHA,
180 static ssl_test_handle *
181 ssl_test_handle_create(uint32_t session_id, bool server, int comm)
183 ssl_test_handle *handle = calloc(1, sizeof(ssl_test_handle));
184 SSLContextRef ctx = SSLCreateContext(kCFAllocatorDefault, server?kSSLServerSide:kSSLClientSide, kSSLStreamType);
186 require(handle, out);
189 require_noerr(SSLSetIOFuncs(ctx,
190 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
191 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)comm), out);
194 require_noerr(SSLSetSessionOption(ctx,
195 kSSLSessionOptionBreakOnClientHello, true), out);
197 require_noerr(SSLSetSessionOption(ctx,
198 kSSLSessionOptionBreakOnServerAuth, true), out);
200 /* Tell SecureTransport to not check certs itself: it will break out of the
201 handshake to let us take care of it instead. */
202 require_noerr(SSLSetEnableCertVerify(ctx, false), out);
204 handle->handle = ctx;
205 handle->is_server = server;
206 handle->session_id = session_id;
212 if (handle) free(handle);
213 if (ctx) CFRelease(ctx);
217 static SSLProtocol versions[] = {
223 static int nversions = sizeof(versions)/sizeof(versions[0]);
228 pthread_t client_thread, server_thread;
230 for(j=0; j<nversions; j++)
233 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) exit(errno);
235 ssl_test_handle *server, *client;
237 uint32_t session_id = (j+1) << 16 | 1 << 8;
238 server = ssl_test_handle_create(session_id, true /*server*/, sp[0]);
239 client = ssl_test_handle_create(session_id, false/*client*/, sp[1]);
241 require_noerr(SSLSetPeerID(server->handle, &session_id, sizeof(session_id)), out);
242 require_noerr(SSLSetPeerID(client->handle, &session_id, sizeof(session_id)), out);
244 /* set fixed cipher on client and server */
245 require_noerr(SSLSetEnabledCiphers(client->handle, &ciphers[0], 1), out);
246 require_noerr(SSLSetEnabledCiphers(server->handle, &ciphers[0], 1), out);
248 require_noerr(SSLSetProtocolVersionMax(client->handle, versions[j]), out);
249 require_noerr(SSLSetPeerDomainName(client->handle, peername, sizeof(peername)), out);
251 require_noerr(SSLSetProtocolVersionMax(server->handle, versions[j]), out);
253 pthread_create(&client_thread, NULL, securetransport_client_thread, client);
254 pthread_create(&server_thread, NULL, securetransport_server_thread, server);
256 intptr_t server_err, client_err;
257 pthread_join(client_thread, (void*)&client_err);
258 pthread_join(server_thread, (void*)&server_err);