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"
34 #include <tls_stream_parser.h>
35 #import "STLegacyTests.h"
37 @implementation STLegacyTests (split)
45 tls_stream_parser_t parser;
51 #pragma mark SecureTransport support
54 static void hexdump(const char *s, const uint8_t *bytes, size_t len) {
56 printf("socket %s(%p, %lu)\n", s, bytes, len);
57 for (ix = 0; ix < len; ++ix) {
60 printf("%02X ", bytes[ix]);
65 #define hexdump(string, bytes, len)
68 static OSStatus SocketWrite(SSLConnectionRef h, const void *data, size_t *length)
70 ssl_test_handle *handle =(ssl_test_handle *)h;
71 int conn = handle->comm;
73 uint8_t *ptr = (uint8_t *)data;
75 if(handle->is_server) {
76 //printf("SocketWrite: server write len=%zd\n", len);
81 tls_stream_parser_parse(handle->parser, buffer);
87 hexdump("write", ptr, len);
88 ret = write((int)conn, ptr, len);
89 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
98 *length = *length - len;
102 static OSStatus SocketRead(SSLConnectionRef h, void *data, size_t *length)
104 const ssl_test_handle *handle=h;
105 int conn = handle->comm;
106 size_t len = *length;
107 uint8_t *ptr = (uint8_t *)data;
113 ret = read((int)conn, ptr, len);
114 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
124 printf("Something went wrong here... len=%d\n", (int)len);
126 *length = *length - len;
127 return errSecSuccess;
130 static int process(tls_stream_parser_ctx_t ctx, tls_buffer record)
132 ssl_test_handle *handle = (ssl_test_handle *)ctx;
134 // printf("processing record len=%zd, type=%d\n", record.length, record.data[0]);
135 if(record.data[0]==tls_record_type_AppData) {
136 handle->write_counter++;
137 // printf("record count = %d\n", handle->write_counter);
144 static void *securetransport_ssl_thread(void *arg)
147 ssl_test_handle * ssl = (ssl_test_handle *)arg;
148 SSLContextRef ctx = ssl->st;
149 bool got_server_auth = false;
151 //uint64_t start = mach_absolute_time();
153 ortn = SSLHandshake(ctx);
155 if (ortn == errSSLServerAuthCompleted)
157 require_string(!got_server_auth, out, "second server auth");
158 got_server_auth = true;
160 } while (ortn == errSSLWouldBlock
161 || ortn == errSSLServerAuthCompleted);
163 require_noerr_action_quiet(ortn, out,
164 fprintf(stderr, "Fell out of SSLHandshake with error: %d\n", (int)ortn));
166 unsigned char ibuf[90000], obuf[45000];
168 if (ssl->is_server) {
170 require_action(errSecSuccess==SecRandomCopyBytes(kSecRandomDefault, ssl->write_size, obuf), out, ortn = -1);
171 require_noerr(ortn = SSLWrite(ctx, obuf, ssl->write_size, &len), out);
172 require_action(len == ssl->write_size, out, ortn = -1);
173 require_noerr(ortn = SSLWrite(ctx, obuf, ssl->write_size, &len), out);
174 require_action(len == ssl->write_size, out, ortn = -1);
176 size_t len = ssl->write_size*2;
178 unsigned char *p = ibuf;
180 require_noerr(ortn = SSLRead(ctx, p, len, &olen), out);
190 pthread_exit((void *)(intptr_t)ortn);
195 ssl_test_handle_destroy(ssl_test_handle *handle)
198 if(handle->parser) tls_stream_parser_destroy(handle->parser);
203 static ssl_test_handle *
204 ssl_test_handle_create(bool server, int comm, CFArrayRef certs)
206 ssl_test_handle *handle = calloc(1, sizeof(ssl_test_handle));
207 SSLContextRef ctx = SSLCreateContext(kCFAllocatorDefault, server?kSSLServerSide:kSSLClientSide, kSSLStreamType);
209 require(handle, out);
212 require_noerr(SSLSetIOFuncs(ctx,
213 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
214 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)handle), out);
217 require_noerr(SSLSetCertificate(ctx, certs), out);
219 require_noerr(SSLSetSessionOption(ctx,
220 kSSLSessionOptionBreakOnServerAuth, true), out);
222 /* Tell SecureTransport to not check certs itself: it will break out of the
223 handshake to let us take care of it instead. */
224 require_noerr(SSLSetEnableCertVerify(ctx, false), out);
226 handle->is_server = server;
228 handle->certs = certs;
230 handle->write_counter = 0;
231 handle->parser = tls_stream_parser_create(handle, process);
236 if (handle) free(handle);
237 if (ctx) CFRelease(ctx);
241 static SSLCipherSuite ciphers[] = {
242 TLS_RSA_WITH_AES_128_CBC_SHA,
244 static int nciphers = sizeof(ciphers)/sizeof(ciphers[0]);
246 static SSLProtocol versions[] = {
252 static int nversions = sizeof(versions)/sizeof(versions[0]);
254 // { write size, expected count when nosplit, expected count when split }
255 static size_t wsizes[][3] = {
271 static int nwsizes = sizeof(wsizes)/sizeof(wsizes[0]);
275 pthread_t client_thread, server_thread;
276 CFArrayRef server_certs = server_chain();
277 XCTAssert(server_certs, "got server certs");
281 for(i=0; i<nciphers; i++)
282 for(j=0; j<nversions; j++)
283 for(k=0; k<nwsizes; k++)
287 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) exit(errno);
288 fcntl(sp[0], F_SETNOSIGPIPE, 1);
289 fcntl(sp[1], F_SETNOSIGPIPE, 1);
291 ssl_test_handle *server, *client;
293 server = ssl_test_handle_create(true /*server*/, sp[0], server_certs);
294 client = ssl_test_handle_create(false/*client*/, sp[1], NULL);
296 server->write_size = wsizes[k][0];
297 client->write_size = wsizes[k][0];
299 require(client, out);
300 require(server, out);
302 require_noerr(SSLSetProtocolVersionMin(server->st, kSSLProtocol3), out); // We need this server to do SSL3.
303 require_noerr(SSLSetProtocolVersionMax(client->st, versions[j]), out);
304 require_noerr(SSLSetEnabledCiphers(client->st, &ciphers[i], 1), out);
306 // s=0: default (should be enabled)
307 // s=1: explicit enable
308 // s=2: expliciti disable
309 require_noerr(SSLSetSessionOption(server->st, kSSLSessionOptionSendOneByteRecord, (s==1)?true:false), out);
311 // printf("**** Test Case: i=%d, j=%d, k=%d (%zd), s=%d ****\n", i, j, k, wsizes[k][0], s);
313 pthread_create(&client_thread, NULL, securetransport_ssl_thread, client);
314 pthread_create(&server_thread, NULL, securetransport_ssl_thread, server);
316 intptr_t server_err, client_err;
317 pthread_join(client_thread, (void*)&client_err);
318 pthread_join(server_thread, (void*)&server_err);
320 XCTAssert(!server_err, "Server error = %ld", server_err);
321 XCTAssert(!client_err, "Client error = %ld", client_err);
323 /* one byte split is expected only for AES when using TLS 1.0 or lower, and when not disabled */
324 bool expected_split = (i==0) && (s!=2) && (versions[j]<=kTLSProtocol1);
325 int expected_count = (int)(expected_split ? wsizes[k][2]: wsizes[k][1]);
327 XCTAssertEqual(server->write_counter, expected_count, "wrong number of data records");
329 // fprintf(stderr, "Server write counter = %d, expected %d\n", server->write_counter, expected_count);
332 ssl_test_handle_destroy(client);
333 ssl_test_handle_destroy(server);
336 CFReleaseNull(server_certs);