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>
43 #include <tls_handshake.h>
44 #include <tls_record.h>
46 /* extern struct ccrng_state *ccDRBGGetRngState(); */
47 #include <CommonCrypto/CommonRandomSPI.h>
48 #define CCRNGSTATE ccDRBGGetRngState()
53 tls_stream_parser_t parser
;
60 #pragma mark SecureTransport support
63 static void hexdump(const char *s
, const uint8_t *bytes
, size_t len
) {
65 printf("socket %s(%p, %lu)\n", s
, bytes
, len
);
66 for (ix
= 0; ix
< len
; ++ix
) {
69 printf("%02X ", bytes
[ix
]);
74 #define hexdump(string, bytes, len)
77 static OSStatus
SocketWrite(SSLConnectionRef h
, const void *data
, size_t *length
)
79 ssl_test_handle
*handle
=(ssl_test_handle
*)h
;
82 uint8_t *ptr
= (uint8_t *)data
;
87 return tls_stream_parser_parse(handle
->parser
, buffer
);
90 static OSStatus
SocketRead(SSLConnectionRef h
, void *data
, size_t *length
)
95 static int process(tls_stream_parser_ctx_t ctx
, tls_buffer record
)
97 ssl_test_handle
*h
= (ssl_test_handle
*)ctx
;
102 decrypted
.length
= tls_record_decrypted_size(h
->record
, record
.length
);
103 decrypted
.data
= malloc(decrypted
.length
);
105 require_action(decrypted
.data
, errOut
, err
=ENOMEM
);
106 require_noerr((err
=tls_record_decrypt(h
->record
, record
, &decrypted
, &ct
)), errOut
);
107 err
=tls_handshake_process(h
->hdsk
, decrypted
, ct
);
115 tls_handshake_message_callback(tls_handshake_ctx_t ctx
, tls_handshake_message_t event
)
120 case tls_handshake_message_client_hello
:
132 tls_handshake_set_protocol_version(tls_handshake_ctx_t ctx
, tls_protocol_version protocolVersion
)
138 tls_handshake_write(tls_handshake_ctx_t ctx
, const tls_buffer data
, uint8_t content_type
)
144 tls_handshake_set_retransmit_timer(tls_handshake_ctx_t ctx
, int attempt
)
151 tls_handshake_callbacks_t tls_handshake_callbacks
= {
152 .message
= tls_handshake_message_callback
,
153 .set_protocol_version
= tls_handshake_set_protocol_version
,
154 .write
= tls_handshake_write
,
155 .set_retransmit_timer
= tls_handshake_set_retransmit_timer
,
160 ssl_test_handle_destroy(ssl_test_handle
*handle
)
163 if(handle
->parser
) tls_stream_parser_destroy(handle
->parser
);
164 if(handle
->record
) tls_record_destroy(handle
->record
);
165 if(handle
->hdsk
) tls_handshake_destroy(handle
->hdsk
);
166 if(handle
->st
) CFRelease(handle
->st
);
171 static uint16_t ciphers
[] = {
172 TLS_RSA_WITH_AES_128_CBC_SHA
,
173 //FIXME: re-enable this test when its fixed.
174 //TLS_RSA_WITH_RC4_128_SHA,
176 static int nciphers
= sizeof(ciphers
)/sizeof(ciphers
[0]);
179 static ssl_test_handle
*
180 ssl_test_handle_create(bool server
)
182 ssl_test_handle
*handle
= calloc(1, sizeof(ssl_test_handle
));
183 SSLContextRef ctx
= SSLCreateContext(kCFAllocatorDefault
, server
?kSSLServerSide
:kSSLClientSide
, kSSLStreamType
);
185 require(handle
, out
);
188 require_noerr(SSLSetIOFuncs(ctx
,
189 (SSLReadFunc
)SocketRead
, (SSLWriteFunc
)SocketWrite
), out
);
190 require_noerr(SSLSetConnection(ctx
, (SSLConnectionRef
)handle
), out
);
192 require_noerr(SSLSetSessionOption(ctx
,
193 kSSLSessionOptionBreakOnServerAuth
, true), out
);
195 /* Tell SecureTransport to not check certs itself: it will break out of the
196 handshake to let us take care of it instead. */
197 require_noerr(SSLSetEnableCertVerify(ctx
, false), out
);
200 handle
->parser
= tls_stream_parser_create(handle
, process
);
201 handle
->record
= tls_record_create(false, CCRNGSTATE
);
202 handle
->hdsk
= tls_handshake_create(false, true); // server.
203 tls_handshake_set_ciphersuites(handle
->hdsk
, ciphers
, nciphers
);
205 tls_handshake_set_callbacks(handle
->hdsk
, &tls_handshake_callbacks
, handle
);
210 if (handle
) free(handle
);
211 if (ctx
) CFRelease(ctx
);
215 static SSLProtocolVersion versions
[] = {
221 static int nversions
= sizeof(versions
)/sizeof(versions
[0]);
223 static char peername
[] = "peername";
231 for(j
=0; j
<nversions
; j
++)
233 ssl_test_handle
*client
;
234 const tls_buffer
*sni
;
236 client
= ssl_test_handle_create(false);
238 require(client
, out
);
240 require_noerr(SSLSetProtocolVersionMax(client
->st
, versions
[j
]), out
);
241 require_noerr(SSLSetPeerDomainName(client
->st
, peername
, sizeof(peername
)), out
);
243 ortn
= SSLHandshake(client
->st
);
245 ok(ortn
==-1234, "Unexpected Handshake exit code");
247 sni
= tls_handshake_get_sni_hostname(client
->hdsk
);
249 if(versions
[j
]==kSSLProtocol3
) {
250 ok(sni
==NULL
|| sni
->data
==NULL
,"Unexpected SNI");
252 ok(sni
!=NULL
&& sni
->data
!=NULL
&&
253 sni
->length
== sizeof(peername
) &&
254 (memcmp(sni
->data
, peername
, sizeof(peername
))==0),
255 "SNI does not match");
259 ssl_test_handle_destroy(client
);
264 int ssl_49_sni(int argc
, char *const *argv
)