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"
52 #pragma mark SecureTransport support
55 static void hexdump(const uint8_t *bytes
, size_t len
) {
57 printf("socket write(%p, %lu)\n", bytes
, len
);
58 for (ix
= 0; ix
< len
; ++ix
) {
61 printf("%02X ", bytes
[ix
]);
66 #define hexdump(bytes, len)
70 static OSStatus
SocketWrite(SSLConnectionRef h
, const void *data
, size_t *length
)
73 uint8_t *ptr
= (uint8_t *)data
;
79 ret
= write((int)h
, ptr
, len
);
80 } while ((ret
< 0) && (errno
== EAGAIN
|| errno
== EINTR
));
89 *length
= *length
- len
;
93 static OSStatus
SocketRead(SSLConnectionRef h
, void *data
, size_t *length
)
96 uint8_t *ptr
= (uint8_t *)data
;
101 ret
= read((int)h
, ptr
, len
);
102 } while ((ret
< 0) && (errno
== EAGAIN
|| errno
== EINTR
));
107 printf("read error(%d): ret=%zd, errno=%d\n", (int)h
, ret
, errno
);
112 *length
= *length
- len
;
113 return errSecSuccess
;
116 static char peername
[] = "localhost";
118 static void *securetransport_server_thread(void *arg
)
121 ssl_test_handle
* ssl
= (ssl_test_handle
*)arg
;
122 SSLContextRef ctx
= ssl
->handle
;
123 CFArrayRef server_certs
= server_chain();
126 ortn
= SSLHandshake(ctx
);
127 } while (ortn
== errSSLWouldBlock
);
129 ok(ortn
==errSSLClientHelloReceived
, "Unexpected Handshake exit code");
131 if (ortn
== errSSLClientHelloReceived
) {
134 SSLCopyRequestedPeerNameLength(ctx
, &length
);
136 sni
= malloc(length
);
137 SSLCopyRequestedPeerName(ctx
, sni
, &length
);
140 SSLProtocol version
= 0;
141 require_noerr(SSLGetProtocolVersionMax(ctx
, &version
), out
);
142 if (version
== kSSLProtocol3
) {
143 ok(sni
==NULL
, "Unexpected SNI");
146 length
== sizeof(peername
) &&
147 (memcmp(sni
, peername
, sizeof(peername
))==0),
148 "SNI does not match");
150 require_noerr(SSLSetCertificate(ctx
, server_certs
), out
);
156 SSLDisposeContext(ctx
);
158 CFReleaseSafe(server_certs
);
160 pthread_exit((void *)(intptr_t)ortn
);
164 static void *securetransport_client_thread(void *arg
)
167 ssl_test_handle
* ssl
= (ssl_test_handle
*)arg
;
168 SSLContextRef ctx
= ssl
->handle
;
171 ortn
= SSLHandshake(ctx
);
172 } while (ortn
== errSSLWouldBlock
|| ortn
!= errSSLClosedGraceful
);
175 SSLDisposeContext(ctx
);
178 pthread_exit((void *)(intptr_t)ortn
);
182 static SSLCipherSuite ciphers
[] = {
183 TLS_RSA_WITH_AES_128_CBC_SHA
,
184 //FIXME: re-enable this test when its fixed.
185 //TLS_RSA_WITH_RC4_128_SHA,
188 static ssl_test_handle
*
189 ssl_test_handle_create(uint32_t session_id
, bool server
, int comm
)
191 ssl_test_handle
*handle
= calloc(1, sizeof(ssl_test_handle
));
192 SSLContextRef ctx
= SSLCreateContext(kCFAllocatorDefault
, server
?kSSLServerSide
:kSSLClientSide
, kSSLStreamType
);
194 require(handle
, out
);
197 require_noerr(SSLSetIOFuncs(ctx
,
198 (SSLReadFunc
)SocketRead
, (SSLWriteFunc
)SocketWrite
), out
);
199 require_noerr(SSLSetConnection(ctx
, (SSLConnectionRef
)(intptr_t)comm
), out
);
202 require_noerr(SSLSetSessionOption(ctx
,
203 kSSLSessionOptionBreakOnClientHello
, true), out
);
205 require_noerr(SSLSetSessionOption(ctx
,
206 kSSLSessionOptionBreakOnServerAuth
, true), out
);
208 /* Tell SecureTransport to not check certs itself: it will break out of the
209 handshake to let us take care of it instead. */
210 require_noerr(SSLSetEnableCertVerify(ctx
, false), out
);
212 handle
->handle
= ctx
;
213 handle
->is_server
= server
;
214 handle
->session_id
= session_id
;
220 if (handle
) free(handle
);
221 if (ctx
) CFRelease(ctx
);
225 static SSLProtocol versions
[] = {
231 static int nversions
= sizeof(versions
)/sizeof(versions
[0]);
237 pthread_t client_thread
, server_thread
;
239 for(j
=0; j
<nversions
; j
++)
242 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sp
)) exit(errno
);
244 ssl_test_handle
*server
, *client
;
246 uint32_t session_id
= (j
+1) << 16 | 1 << 8;
247 server
= ssl_test_handle_create(session_id
, true /*server*/, sp
[0]);
248 client
= ssl_test_handle_create(session_id
, false/*client*/, sp
[1]);
250 require_noerr(SSLSetPeerID(server
->handle
, &session_id
, sizeof(session_id
)), out
);
251 require_noerr(SSLSetPeerID(client
->handle
, &session_id
, sizeof(session_id
)), out
);
253 /* set fixed cipher on client and server */
254 require_noerr(SSLSetEnabledCiphers(client
->handle
, &ciphers
[0], 1), out
);
255 require_noerr(SSLSetEnabledCiphers(server
->handle
, &ciphers
[0], 1), out
);
257 require_noerr(SSLSetProtocolVersionMax(client
->handle
, versions
[j
]), out
);
258 require_noerr(SSLSetPeerDomainName(client
->handle
, peername
, sizeof(peername
)), out
);
260 require_noerr(SSLSetProtocolVersionMax(server
->handle
, versions
[j
]), out
);
262 pthread_create(&client_thread
, NULL
, securetransport_client_thread
, client
);
263 pthread_create(&server_thread
, NULL
, securetransport_server_thread
, server
);
265 intptr_t server_err
, client_err
;
266 pthread_join(client_thread
, (void*)&client_err
);
267 pthread_join(server_thread
, (void*)&server_err
);
276 int ssl_49_sni(int argc
, char *const *argv
)