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>
28 #include <utilities/SecCFRelease.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include <mach/mach_time.h>
37 #include <Security/SecRSAKey.h>
40 #include "ssl_regressions.h"
41 #include "ssl-utils.h"
43 #include <tls_stream_parser.h>
52 tls_stream_parser_t parser
;
58 #pragma mark SecureTransport support
61 static void hexdump(const char *s
, const uint8_t *bytes
, size_t len
) {
63 printf("socket %s(%p, %lu)\n", s
, bytes
, len
);
64 for (ix
= 0; ix
< len
; ++ix
) {
67 printf("%02X ", bytes
[ix
]);
72 #define hexdump(string, bytes, len)
75 static OSStatus
SocketWrite(SSLConnectionRef h
, const void *data
, size_t *length
)
77 ssl_test_handle
*handle
=(ssl_test_handle
*)h
;
78 int conn
= handle
->comm
;
80 uint8_t *ptr
= (uint8_t *)data
;
82 if(handle
->is_server
) {
83 //printf("SocketWrite: server write len=%zd\n", len);
88 tls_stream_parser_parse(handle
->parser
, buffer
);
94 hexdump("write", ptr
, len
);
95 ret
= write((int)conn
, ptr
, len
);
96 } while ((ret
< 0) && (errno
== EAGAIN
|| errno
== EINTR
));
105 *length
= *length
- len
;
106 return errSecSuccess
;
109 static OSStatus
SocketRead(SSLConnectionRef h
, void *data
, size_t *length
)
111 const ssl_test_handle
*handle
=h
;
112 int conn
= handle
->comm
;
113 size_t len
= *length
;
114 uint8_t *ptr
= (uint8_t *)data
;
120 ret
= read((int)conn
, ptr
, len
);
121 } while ((ret
< 0) && (errno
== EAGAIN
|| errno
== EINTR
));
131 printf("Something went wrong here... len=%d\n", (int)len
);
133 *length
= *length
- len
;
134 return errSecSuccess
;
137 static int process(tls_stream_parser_ctx_t ctx
, tls_buffer record
)
139 ssl_test_handle
*handle
= (ssl_test_handle
*)ctx
;
141 // printf("processing record len=%zd, type=%d\n", record.length, record.data[0]);
142 if(record
.data
[0]==tls_record_type_AppData
) {
143 handle
->write_counter
++;
144 // printf("record count = %d\n", handle->write_counter);
151 static void *securetransport_ssl_thread(void *arg
)
154 ssl_test_handle
* ssl
= (ssl_test_handle
*)arg
;
155 SSLContextRef ctx
= ssl
->st
;
156 bool got_server_auth
= false;
158 //uint64_t start = mach_absolute_time();
160 ortn
= SSLHandshake(ctx
);
162 if (ortn
== errSSLServerAuthCompleted
)
164 require_string(!got_server_auth
, out
, "second server auth");
165 got_server_auth
= true;
167 } while (ortn
== errSSLWouldBlock
168 || ortn
== errSSLServerAuthCompleted
);
170 require_noerr_action_quiet(ortn
, out
,
171 fprintf(stderr
, "Fell out of SSLHandshake with error: %d\n", (int)ortn
));
173 unsigned char ibuf
[90000], obuf
[45000];
175 if (ssl
->is_server
) {
177 require_action(errSecSuccess
==SecRandomCopyBytes(kSecRandomDefault
, ssl
->write_size
, obuf
), out
, ortn
= -1);
178 require_noerr(ortn
= SSLWrite(ctx
, obuf
, ssl
->write_size
, &len
), out
);
179 require_action(len
== ssl
->write_size
, out
, ortn
= -1);
180 require_noerr(ortn
= SSLWrite(ctx
, obuf
, ssl
->write_size
, &len
), out
);
181 require_action(len
== ssl
->write_size
, out
, ortn
= -1);
183 size_t len
= ssl
->write_size
*2;
185 unsigned char *p
= ibuf
;
187 require_noerr(ortn
= SSLRead(ctx
, p
, len
, &olen
), out
);
197 pthread_exit((void *)(intptr_t)ortn
);
202 ssl_test_handle_destroy(ssl_test_handle
*handle
)
205 if(handle
->parser
) tls_stream_parser_destroy(handle
->parser
);
210 static ssl_test_handle
*
211 ssl_test_handle_create(bool server
, int comm
, CFArrayRef certs
)
213 ssl_test_handle
*handle
= calloc(1, sizeof(ssl_test_handle
));
214 SSLContextRef ctx
= SSLCreateContext(kCFAllocatorDefault
, server
?kSSLServerSide
:kSSLClientSide
, kSSLStreamType
);
216 require(handle
, out
);
219 require_noerr(SSLSetIOFuncs(ctx
,
220 (SSLReadFunc
)SocketRead
, (SSLWriteFunc
)SocketWrite
), out
);
221 require_noerr(SSLSetConnection(ctx
, (SSLConnectionRef
)handle
), out
);
224 require_noerr(SSLSetCertificate(ctx
, certs
), out
);
226 require_noerr(SSLSetSessionOption(ctx
,
227 kSSLSessionOptionBreakOnServerAuth
, true), out
);
229 /* Tell SecureTransport to not check certs itself: it will break out of the
230 handshake to let us take care of it instead. */
231 require_noerr(SSLSetEnableCertVerify(ctx
, false), out
);
233 handle
->is_server
= server
;
235 handle
->certs
= certs
;
237 handle
->write_counter
= 0;
238 handle
->parser
= tls_stream_parser_create(handle
, process
);
243 if (handle
) free(handle
);
244 if (ctx
) CFRelease(ctx
);
248 static SSLCipherSuite ciphers
[] = {
249 TLS_RSA_WITH_AES_128_CBC_SHA
,
250 //FIXME: re-enable this test when its fixed.
251 //TLS_RSA_WITH_RC4_128_SHA,
253 static int nciphers
= sizeof(ciphers
)/sizeof(ciphers
[0]);
255 static SSLProtocol versions
[] = {
261 static int nversions
= sizeof(versions
)/sizeof(versions
[0]);
263 // { write size, expected count when nosplit, expected count when split }
264 static size_t wsizes
[][3] = {
280 static int nwsizes
= sizeof(wsizes
)/sizeof(wsizes
[0]);
285 pthread_t client_thread
, server_thread
;
286 CFArrayRef server_certs
= server_chain();
287 ok(server_certs
, "got server certs");
291 for(i
=0; i
<nciphers
; i
++)
292 for(j
=0; j
<nversions
; j
++)
293 for(k
=0; k
<nwsizes
; k
++)
297 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sp
)) exit(errno
);
298 fcntl(sp
[0], F_SETNOSIGPIPE
, 1);
299 fcntl(sp
[1], F_SETNOSIGPIPE
, 1);
301 ssl_test_handle
*server
, *client
;
303 server
= ssl_test_handle_create(true /*server*/, sp
[0], server_certs
);
304 client
= ssl_test_handle_create(false/*client*/, sp
[1], NULL
);
306 server
->write_size
= wsizes
[k
][0];
307 client
->write_size
= wsizes
[k
][0];
309 require(client
, out
);
310 require(server
, out
);
312 require_noerr(SSLSetProtocolVersionMin(server
->st
, kSSLProtocol3
), out
); // We need this server to do SSL3.
313 require_noerr(SSLSetProtocolVersionMax(client
->st
, versions
[j
]), out
);
314 require_noerr(SSLSetEnabledCiphers(client
->st
, &ciphers
[i
], 1), out
);
316 // s=0: default (should be enabled)
317 // s=1: explicit enable
318 // s=2: expliciti disable
319 require_noerr(SSLSetSessionOption(server
->st
, kSSLSessionOptionSendOneByteRecord
, (s
==1)?true:false), out
);
321 // printf("**** Test Case: i=%d, j=%d, k=%d (%zd), s=%d ****\n", i, j, k, wsizes[k][0], s);
323 pthread_create(&client_thread
, NULL
, securetransport_ssl_thread
, client
);
324 pthread_create(&server_thread
, NULL
, securetransport_ssl_thread
, server
);
326 intptr_t server_err
, client_err
;
327 pthread_join(client_thread
, (void*)&client_err
);
328 pthread_join(server_thread
, (void*)&server_err
);
330 ok(!server_err
, "Server error = %ld", server_err
);
331 ok(!client_err
, "Client error = %ld", client_err
);
333 /* one byte split is expected only for AES when using TLS 1.0 or lower, and when not disabled */
334 bool expected_split
= (i
==0) && (s
!=2) && (versions
[j
]<=kTLSProtocol1
);
335 int expected_count
= (int)(expected_split
? wsizes
[k
][2]: wsizes
[k
][1]);
337 is(server
->write_counter
, expected_count
, "wrong number of data records");
339 // fprintf(stderr, "Server write counter = %d, expected %d\n", server->write_counter, expected_count);
342 ssl_test_handle_destroy(client
);
343 ssl_test_handle_destroy(server
);
346 CFReleaseNull(server_certs
);
349 int ssl_48_split(int argc
, char *const *argv
)
352 plan_tests(1 + nciphers
*nversions
*nwsizes
*3 * 3);