14 #include <CoreFoundation/CoreFoundation.h>
16 #include <AssertMacros.h>
17 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
18 #include <Security/SecureTransport.h>
19 #include <Security/SecPolicy.h>
20 #include <Security/SecTrust.h>
21 #include <Security/SecIdentity.h>
22 #include <Security/SecIdentityPriv.h>
23 #include <Security/SecCertificatePriv.h>
24 #include <Security/SecKeyPriv.h>
25 #include <Security/SecItem.h>
26 #include <Security/SecRandom.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <mach/mach_time.h>
36 #include <Security/SecRSAKey.h>
39 #include "ssl_regressions.h"
40 #include "ssl-utils.h"
42 #include <tls_stream_parser.h>
51 tls_stream_parser_t parser
;
57 #pragma mark SecureTransport support
60 static void hexdump(const char *s
, const uint8_t *bytes
, size_t len
) {
62 printf("socket %s(%p, %lu)\n", s
, bytes
, len
);
63 for (ix
= 0; ix
< len
; ++ix
) {
66 printf("%02X ", bytes
[ix
]);
71 #define hexdump(string, bytes, len)
74 static OSStatus
SocketWrite(SSLConnectionRef h
, const void *data
, size_t *length
)
76 ssl_test_handle
*handle
=(ssl_test_handle
*)h
;
77 int conn
= handle
->comm
;
79 uint8_t *ptr
= (uint8_t *)data
;
81 if(handle
->is_server
) {
82 //printf("SocketWrite: server write len=%zd\n", len);
87 tls_stream_parser_parse(handle
->parser
, buffer
);
93 hexdump("write", ptr
, len
);
94 ret
= write((int)conn
, ptr
, len
);
95 } while ((ret
< 0) && (errno
== EAGAIN
|| errno
== EINTR
));
104 *length
= *length
- len
;
105 return errSecSuccess
;
108 static OSStatus
SocketRead(SSLConnectionRef h
, void *data
, size_t *length
)
110 const ssl_test_handle
*handle
=h
;
111 int conn
= handle
->comm
;
112 size_t len
= *length
;
113 uint8_t *ptr
= (uint8_t *)data
;
119 ret
= read((int)conn
, ptr
, len
);
120 } while ((ret
< 0) && (errno
== EAGAIN
|| errno
== EINTR
));
130 printf("Something went wrong here... len=%d\n", (int)len
);
132 *length
= *length
- len
;
133 return errSecSuccess
;
136 static int process(tls_stream_parser_ctx_t ctx
, tls_buffer record
)
138 ssl_test_handle
*handle
= (ssl_test_handle
*)ctx
;
140 // printf("processing record len=%zd, type=%d\n", record.length, record.data[0]);
141 if(record
.data
[0]==tls_record_type_AppData
) {
142 handle
->write_counter
++;
143 // printf("record count = %d\n", handle->write_counter);
150 static void *securetransport_ssl_thread(void *arg
)
153 ssl_test_handle
* ssl
= (ssl_test_handle
*)arg
;
154 SSLContextRef ctx
= ssl
->st
;
155 bool got_server_auth
= false;
157 //uint64_t start = mach_absolute_time();
159 ortn
= SSLHandshake(ctx
);
161 if (ortn
== errSSLServerAuthCompleted
)
163 require_string(!got_server_auth
, out
, "second server auth");
164 got_server_auth
= true;
166 } while (ortn
== errSSLWouldBlock
167 || ortn
== errSSLServerAuthCompleted
);
169 require_noerr_action_quiet(ortn
, out
,
170 fprintf(stderr
, "Fell out of SSLHandshake with error: %d\n", (int)ortn
));
172 unsigned char ibuf
[90000], obuf
[45000];
174 if (ssl
->is_server
) {
176 require_action(errSecSuccess
==SecRandomCopyBytes(kSecRandomDefault
, ssl
->write_size
, obuf
), out
, ortn
= -1);
177 require_noerr(ortn
= SSLWrite(ctx
, obuf
, ssl
->write_size
, &len
), out
);
178 require_action(len
== ssl
->write_size
, out
, ortn
= -1);
179 require_noerr(ortn
= SSLWrite(ctx
, obuf
, ssl
->write_size
, &len
), out
);
180 require_action(len
== ssl
->write_size
, out
, ortn
= -1);
182 size_t len
= ssl
->write_size
*2;
184 unsigned char *p
= ibuf
;
186 require_noerr(ortn
= SSLRead(ctx
, p
, len
, &olen
), out
);
196 pthread_exit((void *)(intptr_t)ortn
);
201 ssl_test_handle_destroy(ssl_test_handle
*handle
)
204 if(handle
->parser
) tls_stream_parser_destroy(handle
->parser
);
209 static ssl_test_handle
*
210 ssl_test_handle_create(bool server
, int comm
, CFArrayRef certs
)
212 ssl_test_handle
*handle
= calloc(1, sizeof(ssl_test_handle
));
213 SSLContextRef ctx
= SSLCreateContext(kCFAllocatorDefault
, server
?kSSLServerSide
:kSSLClientSide
, kSSLStreamType
);
215 require(handle
, out
);
218 require_noerr(SSLSetIOFuncs(ctx
,
219 (SSLReadFunc
)SocketRead
, (SSLWriteFunc
)SocketWrite
), out
);
220 require_noerr(SSLSetConnection(ctx
, (SSLConnectionRef
)handle
), out
);
223 require_noerr(SSLSetCertificate(ctx
, certs
), out
);
225 require_noerr(SSLSetSessionOption(ctx
,
226 kSSLSessionOptionBreakOnServerAuth
, true), out
);
228 /* Tell SecureTransport to not check certs itself: it will break out of the
229 handshake to let us take care of it instead. */
230 require_noerr(SSLSetEnableCertVerify(ctx
, false), out
);
232 handle
->is_server
= server
;
234 handle
->certs
= certs
;
236 handle
->write_counter
= 0;
237 handle
->parser
= tls_stream_parser_create(handle
, process
);
242 if (handle
) free(handle
);
243 if (ctx
) CFRelease(ctx
);
247 static SSLCipherSuite ciphers
[] = {
248 TLS_RSA_WITH_AES_128_CBC_SHA
,
249 //FIXME: re-enable this test when its fixed.
250 //TLS_RSA_WITH_RC4_128_SHA,
252 static int nciphers
= sizeof(ciphers
)/sizeof(ciphers
[0]);
254 static SSLProtocol versions
[] = {
260 static int nversions
= sizeof(versions
)/sizeof(versions
[0]);
262 // { write size, expected count when nosplit, expected count when split }
263 static size_t wsizes
[][3] = {
279 static int nwsizes
= sizeof(wsizes
)/sizeof(wsizes
[0]);
284 pthread_t client_thread
, server_thread
;
285 CFArrayRef server_certs
= server_chain();
286 ok(server_certs
, "got server certs");
290 for(i
=0; i
<nciphers
; i
++)
291 for(j
=0; j
<nversions
; j
++)
292 for(k
=0; k
<nwsizes
; k
++)
296 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sp
)) exit(errno
);
297 fcntl(sp
[0], F_SETNOSIGPIPE
, 1);
298 fcntl(sp
[1], F_SETNOSIGPIPE
, 1);
300 ssl_test_handle
*server
, *client
;
302 server
= ssl_test_handle_create(true /*server*/, sp
[0], server_certs
);
303 client
= ssl_test_handle_create(false/*client*/, sp
[1], NULL
);
305 server
->write_size
= wsizes
[k
][0];
306 client
->write_size
= wsizes
[k
][0];
308 require(client
, out
);
309 require(server
, out
);
311 require_noerr(SSLSetProtocolVersionMin(server
->st
, kSSLProtocol3
), out
); // We need this server to do SSL3.
312 require_noerr(SSLSetProtocolVersionMax(client
->st
, versions
[j
]), out
);
313 require_noerr(SSLSetEnabledCiphers(client
->st
, &ciphers
[i
], 1), out
);
315 // s=0: default (should be enabled)
316 // s=1: explicit enable
317 // s=2: expliciti disable
318 require_noerr(SSLSetSessionOption(server
->st
, kSSLSessionOptionSendOneByteRecord
, (s
==1)?true:false), out
);
320 // printf("**** Test Case: i=%d, j=%d, k=%d (%zd), s=%d ****\n", i, j, k, wsizes[k][0], s);
322 pthread_create(&client_thread
, NULL
, securetransport_ssl_thread
, client
);
323 pthread_create(&server_thread
, NULL
, securetransport_ssl_thread
, server
);
325 int server_err
, client_err
;
326 pthread_join(client_thread
, (void*)&client_err
);
327 pthread_join(server_thread
, (void*)&server_err
);
329 ok(!server_err
, "Server error = %d", server_err
);
330 ok(!client_err
, "Client error = %d", client_err
);
332 /* one byte split is expected only for AES when using TLS 1.0 or lower, and when not disabled */
333 bool expected_split
= (i
==0) && (s
!=2) && (versions
[j
]<=kTLSProtocol1
);
334 int expected_count
= (int)(expected_split
? wsizes
[k
][2]: wsizes
[k
][1]);
336 is(server
->write_counter
, expected_count
, "wrong number of data records");
338 // fprintf(stderr, "Server write counter = %d, expected %d\n", server->write_counter, expected_count);
341 ssl_test_handle_destroy(client
);
342 ssl_test_handle_destroy(server
);
345 CFReleaseNull(server_certs
);
348 int ssl_48_split(int argc
, char *const *argv
)
351 plan_tests(1 + nciphers
*nversions
*nwsizes
*3 * 3);