12 #include <CoreFoundation/CoreFoundation.h>
14 #include <AssertMacros.h>
15 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
16 #include <Security/SecureTransport.h>
17 #include <Security/SecPolicy.h>
18 #include <Security/SecTrust.h>
19 #include <Security/SecIdentity.h>
20 #include <Security/SecIdentityPriv.h>
21 #include <Security/SecCertificatePriv.h>
22 #include <Security/SecKeyPriv.h>
23 #include <Security/SecItem.h>
24 #include <Security/SecRandom.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <mach/mach_time.h>
34 #include <Security/SecRSAKey.h>
37 #include "ssl_regressions.h"
38 #include "ssl-utils.h"
40 #include <tls_stream_parser.h>
41 #include <tls_handshake.h>
42 #include <tls_record.h>
44 #include <sys/queue.h>
47 #define test_printf(x...)
49 /* extern struct ccrng_state *ccDRBGGetRngState(); */
50 #include <CommonCrypto/CommonRandomSPI.h>
51 #define CCRNGSTATE ccDRBGGetRngState()
54 STAILQ_ENTRY(RecQueueItem
) next
; /* link to next queued entry or NULL */
56 size_t offset
; /* byte reads from this one */
61 tls_stream_parser_t parser
;
64 STAILQ_HEAD(, RecQueueItem
) rec_queue
; // coretls server queue packet in this queue
70 int tls_buffer_alloc(tls_buffer
*buf
, size_t length
)
72 buf
->data
= malloc(length
);
73 if(!buf
->data
) return -ENOMEM
;
79 int tls_buffer_free(tls_buffer
*buf
)
88 #pragma mark SecureTransport support
91 static void hexdump(const char *s
, const uint8_t *bytes
, size_t len
) {
93 printf("socket %s(%p, %lu)\n", s
, bytes
, len
);
94 for (ix
= 0; ix
< len
; ++ix
) {
97 printf("%02X ", bytes
[ix
]);
102 #define hexdump(string, bytes, len)
105 static OSStatus
SocketWrite(SSLConnectionRef h
, const void *data
, size_t *length
)
107 ssl_test_handle
*handle
=(ssl_test_handle
*)h
;
109 size_t len
= *length
;
110 uint8_t *ptr
= (uint8_t *)data
;
115 return tls_stream_parser_parse(handle
->parser
, buffer
);
118 static OSStatus
SocketRead(SSLConnectionRef h
, void *data
, size_t *length
)
120 ssl_test_handle
*handle
=(ssl_test_handle
*)h
;
122 test_printf("%s: %p requesting len=%zd\n", __FUNCTION__
, h
, *length
);
124 struct RecQueueItem
*item
= STAILQ_FIRST(&handle
->rec_queue
);
127 test_printf("%s: %p no data available\n", __FUNCTION__
, h
);
128 return errSSLWouldBlock
;
131 size_t avail
= item
->record
.length
- item
->offset
;
133 test_printf("%s: %p %zd bytes available in %p\n", __FUNCTION__
, h
, avail
, item
);
135 if(avail
> *length
) {
136 memcpy(data
, item
->record
.data
+item
->offset
, *length
);
137 item
->offset
+= *length
;
139 memcpy(data
, item
->record
.data
+item
->offset
, avail
);
141 STAILQ_REMOVE_HEAD(&handle
->rec_queue
, next
);
142 tls_buffer_free(&item
->record
);
146 test_printf("%s: %p %zd bytes read\n", __FUNCTION__
, h
, *length
);
152 static int process(tls_stream_parser_ctx_t ctx
, tls_buffer record
)
154 ssl_test_handle
*h
= (ssl_test_handle
*)ctx
;
155 tls_buffer decrypted
;
159 test_printf("%s: %p processing %zd bytes\n", __FUNCTION__
, ctx
, record
.length
);
162 decrypted
.length
= tls_record_decrypted_size(h
->record
, record
.length
);
163 decrypted
.data
= malloc(decrypted
.length
);
165 require_action(decrypted
.data
, errOut
, err
=-ENOMEM
);
166 require_noerr((err
=tls_record_decrypt(h
->record
, record
, &decrypted
, &ct
)), errOut
);
168 test_printf("%s: %p decrypted %zd bytes, ct=%d\n", __FUNCTION__
, ctx
, decrypted
.length
, ct
);
170 err
=tls_handshake_process(h
->hdsk
, decrypted
, ct
);
172 test_printf("%s: %p processed, err=%d\n", __FUNCTION__
, ctx
, err
);
175 free(decrypted
.data
);
180 tls_handshake_write_callback(tls_handshake_ctx_t ctx
, const tls_buffer data
, uint8_t content_type
)
183 ssl_test_handle
*handle
= (ssl_test_handle
*)ctx
;
185 test_printf("%s: %p writing data ct=%d, len=%zd\n", __FUNCTION__
, ctx
, content_type
, data
.length
);
187 struct RecQueueItem
*item
= malloc(sizeof(struct RecQueueItem
));
188 require_action(item
, errOut
, err
=-ENOMEM
);
190 err
=tls_buffer_alloc(&item
->record
, tls_record_encrypted_size(handle
->record
, content_type
, data
.length
));
191 require_noerr(err
, errOut
);
193 err
=tls_record_encrypt(handle
->record
, data
, content_type
, &item
->record
);
194 require_noerr(err
, errOut
);
198 test_printf("%s: %p queing %zd encrypted bytes, item=%p\n", __FUNCTION__
, ctx
, item
->record
.length
, item
);
200 STAILQ_INSERT_TAIL(&handle
->rec_queue
, item
, next
);
206 tls_buffer_free(&item
->record
);
214 tls_handshake_message_callback(tls_handshake_ctx_t ctx
, tls_handshake_message_t event
)
216 ssl_test_handle __unused
*handle
= (ssl_test_handle
*)ctx
;
218 test_printf("%s: %p event = %d\n", __FUNCTION__
, handle
, event
);
227 static uint8_t appdata
[] = "appdata";
229 tls_buffer appdata_buffer
= {
231 .length
= sizeof(appdata
),
236 tls_handshake_ready_callback(tls_handshake_ctx_t ctx
, bool write
, bool ready
)
238 ssl_test_handle
*handle
= (ssl_test_handle
*)ctx
;
240 test_printf("%s: %p %s ready=%d\n", __FUNCTION__
, handle
, write
?"write":"read", ready
);
244 if(handle
->ready_count
== 0) {
245 tls_handshake_request_renegotiation(handle
->hdsk
);
247 tls_handshake_write_callback(ctx
, appdata_buffer
, tls_record_type_AppData
);
249 handle
->ready_count
++;;
255 tls_handshake_set_retransmit_timer_callback(tls_handshake_ctx_t ctx
, int attempt
)
257 ssl_test_handle __unused
*handle
= (ssl_test_handle
*)ctx
;
259 test_printf("%s: %p attempt = %d\n", __FUNCTION__
, handle
, attempt
);
265 int mySSLRecordInitPendingCiphersFunc(tls_handshake_ctx_t ref
,
266 uint16_t selectedCipher
,
270 ssl_test_handle
*handle
= (ssl_test_handle
*)ref
;
272 test_printf("%s: %p, cipher=%04x, server=%d\n", __FUNCTION__
, ref
, selectedCipher
, server
);
273 return tls_record_init_pending_ciphers(handle
->record
, selectedCipher
, server
, key
);
277 int mySSLRecordAdvanceWriteCipherFunc(tls_handshake_ctx_t ref
)
279 ssl_test_handle
*handle
= (ssl_test_handle
*)ref
;
280 test_printf("%s: %p\n", __FUNCTION__
, ref
);
281 return tls_record_advance_write_cipher(handle
->record
);
285 int mySSLRecordRollbackWriteCipherFunc(tls_handshake_ctx_t ref
)
287 ssl_test_handle
*handle
= (ssl_test_handle
*)ref
;
288 test_printf("%s: %p\n", __FUNCTION__
, ref
);
289 return tls_record_rollback_write_cipher(handle
->record
);
293 int mySSLRecordAdvanceReadCipherFunc(tls_handshake_ctx_t ref
)
295 ssl_test_handle
*handle
= (ssl_test_handle
*)ref
;
296 test_printf("%s: %p\n", __FUNCTION__
, ref
);
297 return tls_record_advance_read_cipher(handle
->record
);
301 int mySSLRecordSetProtocolVersionFunc(tls_handshake_ctx_t ref
,
302 tls_protocol_version protocolVersion
)
304 ssl_test_handle
*handle
= (ssl_test_handle
*)ref
;
305 test_printf("%s: %p, version=%04x\n", __FUNCTION__
, ref
, protocolVersion
);
306 return tls_record_set_protocol_version(handle
->record
, protocolVersion
);
311 tls_handshake_save_session_data_callback(tls_handshake_ctx_t ctx
, tls_buffer sessionKey
, tls_buffer sessionData
)
313 ssl_test_handle __unused
*handle
= (ssl_test_handle
*)ctx
;
315 test_printf("%s: %p\n", __FUNCTION__
, handle
);
321 tls_handshake_load_session_data_callback(tls_handshake_ctx_t ctx
, tls_buffer sessionKey
, tls_buffer
*sessionData
)
323 ssl_test_handle __unused
*handle
= (ssl_test_handle
*)ctx
;
325 test_printf("%s: %p\n", __FUNCTION__
, handle
);
331 tls_handshake_delete_session_data_callback(tls_handshake_ctx_t ctx
, tls_buffer sessionKey
)
333 ssl_test_handle __unused
*handle
= (ssl_test_handle
*)ctx
;
335 test_printf("%s: %p\n", __FUNCTION__
, handle
);
341 tls_handshake_delete_all_sessions_callback(tls_handshake_ctx_t ctx
)
343 ssl_test_handle __unused
*handle
= (ssl_test_handle
*)ctx
;
345 test_printf("%s: %p\n", __FUNCTION__
, handle
);
351 tls_handshake_callbacks_t tls_handshake_callbacks
= {
352 .write
= tls_handshake_write_callback
,
353 .message
= tls_handshake_message_callback
,
354 .ready
= tls_handshake_ready_callback
,
355 .set_retransmit_timer
= tls_handshake_set_retransmit_timer_callback
,
356 .init_pending_cipher
= mySSLRecordInitPendingCiphersFunc
,
357 .advance_write_cipher
= mySSLRecordAdvanceWriteCipherFunc
,
358 .rollback_write_cipher
= mySSLRecordRollbackWriteCipherFunc
,
359 .advance_read_cipher
= mySSLRecordAdvanceReadCipherFunc
,
360 .set_protocol_version
= mySSLRecordSetProtocolVersionFunc
,
361 .load_session_data
= tls_handshake_load_session_data_callback
,
362 .save_session_data
= tls_handshake_save_session_data_callback
,
363 .delete_session_data
= tls_handshake_delete_session_data_callback
,
364 .delete_all_sessions
= tls_handshake_delete_all_sessions_callback
,
369 ssl_test_handle_destroy(ssl_test_handle
*handle
)
372 if(handle
->parser
) tls_stream_parser_destroy(handle
->parser
);
373 if(handle
->record
) tls_record_destroy(handle
->record
);
374 if(handle
->hdsk
) tls_handshake_destroy(handle
->hdsk
);
375 if(handle
->st
) CFRelease(handle
->st
);
380 static uint16_t ciphers
[] = {
381 TLS_PSK_WITH_AES_128_CBC_SHA
,
383 static int nciphers
= sizeof(ciphers
)/sizeof(ciphers
[0]);
385 static SSLCipherSuite ciphersuites
[] = {
386 TLS_PSK_WITH_AES_128_CBC_SHA
,
388 static int nciphersuites
= sizeof(ciphersuites
)/sizeof(ciphersuites
[0]);
392 static uint8_t shared_secret
[] = "secret";
394 tls_buffer psk_secret
= {
395 .data
= shared_secret
,
396 .length
= sizeof(shared_secret
),
399 static ssl_test_handle
*
400 ssl_test_handle_create(bool server
)
402 ssl_test_handle
*handle
= calloc(1, sizeof(ssl_test_handle
));
403 SSLContextRef ctx
= SSLCreateContext(kCFAllocatorDefault
, server
?kSSLServerSide
:kSSLClientSide
, kSSLStreamType
);
405 require(handle
, out
);
408 require_noerr(SSLSetIOFuncs(ctx
, (SSLReadFunc
)SocketRead
, (SSLWriteFunc
)SocketWrite
), out
);
409 require_noerr(SSLSetConnection(ctx
, (SSLConnectionRef
)handle
), out
);
410 require_noerr(SSLSetSessionOption(ctx
, kSSLSessionOptionBreakOnServerAuth
, true), out
);
411 require_noerr(SSLSetEnabledCiphers(ctx
, ciphersuites
, nciphersuites
), out
);
412 require_noerr(SSLSetPSKSharedSecret(ctx
, shared_secret
, sizeof(shared_secret
)), out
);
415 handle
->parser
= tls_stream_parser_create(handle
, process
);
416 handle
->record
= tls_record_create(false, CCRNGSTATE
);
417 handle
->hdsk
= tls_handshake_create(false, true); // server.
419 require_noerr(tls_handshake_set_ciphersuites(handle
->hdsk
, ciphers
, nciphers
), out
);
420 require_noerr(tls_handshake_set_callbacks(handle
->hdsk
, &tls_handshake_callbacks
, handle
), out
);
421 require_noerr(tls_handshake_set_psk_secret(handle
->hdsk
, &psk_secret
), out
);
422 require_noerr(tls_handshake_set_renegotiation(handle
->hdsk
, true), out
);
424 // Initialize the record queue
425 STAILQ_INIT(&handle
->rec_queue
);
430 if (handle
) free(handle
);
431 if (ctx
) CFRelease(ctx
);
440 ssl_test_handle
*client
;
441 SSLSessionState state
;
443 client
= ssl_test_handle_create(false);
445 require_action(client
, out
, ortn
= -1);
447 ortn
= SSLGetSessionState(client
->st
, &state
);
448 require_noerr(ortn
, out
);
449 is(state
, kSSLIdle
, "State should be Idle");
452 ortn
= SSLHandshake(client
->st
);
453 test_printf("SSLHandshake returned err=%d\n", (int)ortn
);
455 require_noerr(SSLGetSessionState(client
->st
, &state
), out
);
457 if (ortn
== errSSLPeerAuthCompleted
|| ortn
== errSSLWouldBlock
)
459 require_action(state
==kSSLHandshake
, out
, ortn
= -1);
462 } while(ortn
==errSSLWouldBlock
||
463 ortn
==errSSLPeerAuthCompleted
);
466 is(ortn
, 0, "Unexpected SSLHandshake exit code");
467 is(state
, kSSLConnected
, "State should be Connected");
470 size_t available
= 0;
472 test_printf("Initial handshake done\n");
475 ortn
= SSLRead(client
->st
, buffer
, sizeof(buffer
), &available
);
476 test_printf("SSLRead returned err=%d, avail=%zd\n", (int)ortn
, available
);
477 require_noerr(SSLGetSessionState(client
->st
, &state
), out
);
479 if (ortn
== errSSLPeerAuthCompleted
)
481 require_action(state
==kSSLHandshake
, out
, ortn
= -1);
484 } while(available
==0);
486 is(ortn
, 0, "Unexpected SSLRead exit code");
487 is(state
, kSSLConnected
, "State should be Connected");
491 is(ortn
, 0, "Final result is non zero");
492 ssl_test_handle_destroy(client
);
496 int ssl_51_state(int argc
, char *const *argv
)