8 #include <CoreFoundation/CoreFoundation.h>
10 #include <AssertMacros.h>
11 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
12 #include <Security/SecureTransport.h>
13 #include <Security/SecPolicy.h>
14 #include <Security/SecTrust.h>
15 #include <Security/SecIdentity.h>
16 #include <Security/SecIdentityPriv.h>
17 #include <Security/SecCertificatePriv.h>
18 #include <Security/SecKeyPriv.h>
19 #include <Security/SecItem.h>
20 #include <Security/SecRandom.h>
22 #include <utilities/array_size.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <mach/mach_time.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
42 #include <Security/SecRSAKey.h>
45 #include "ssl_regressions.h"
46 #include "ssl-utils.h"
56 // MARK: SecureTransport support
59 static void hexdump(const uint8_t *bytes
, size_t len
) {
61 printf("socket write(%p, %lu)\n", bytes
, len
);
62 for (ix
= 0; ix
< len
; ++ix
) {
65 printf("%02X ", bytes
[ix
]);
70 #define hexdump(bytes, len)
74 /* Listen to on port */
75 static int SocketListen(int port
)
77 struct sockaddr_in sa
;
81 if ((sock
=socket(AF_INET
, SOCK_STREAM
, 0))==-1) {
86 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&val
, sizeof(val
));
88 memset((char *) &sa
, 0, sizeof(sa
));
89 sa
.sin_family
= AF_INET
;
90 sa
.sin_port
= htons(port
);
91 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
93 if(bind (sock
, (struct sockaddr
*)&sa
, sizeof(sa
))==-1)
99 if(listen(sock
, 5)==-1)
109 static OSStatus
SocketWrite(SSLConnectionRef conn
, const void *data
, size_t *length
)
111 size_t len
= *length
;
112 uint8_t *ptr
= (uint8_t *)data
;
118 ret
= write((int)conn
, ptr
, len
);
119 } while ((ret
< 0) && (errno
== EAGAIN
|| errno
== EINTR
));
128 *length
= *length
- len
;
129 return errSecSuccess
;
132 static OSStatus
SocketRead(SSLConnectionRef conn
, void *data
, size_t *length
)
134 size_t len
= *length
;
135 uint8_t *ptr
= (uint8_t *)data
;
140 ret
= read((int)conn
, ptr
, len
);
141 } while ((ret
< 0) && (errno
== EINPROGRESS
|| errno
== EAGAIN
|| errno
== EINTR
));
146 printf("read error(%d): ret=%zd, errno=%d\n", (int)conn
, ret
, errno
);
151 *length
= *length
- len
;
152 return errSecSuccess
;
156 static SSLContextRef
make_ssl_ref(bool server
, int sock
, CFArrayRef certs
)
158 SSLContextRef ctx
= SSLCreateContext(kCFAllocatorDefault
, server
?kSSLServerSide
:kSSLClientSide
, kSSLStreamType
);
161 require_noerr(SSLSetIOFuncs(ctx
, (SSLReadFunc
)SocketRead
, (SSLWriteFunc
)SocketWrite
), out
);
162 require_noerr(SSLSetConnection(ctx
, (SSLConnectionRef
)(intptr_t)sock
), out
);
163 require_noerr(SSLSetCertificate(ctx
, certs
), out
);
173 static ssl_test_handle
*
174 ssl_test_handle_create(int comm
, CFArrayRef certs
)
176 ssl_test_handle
*handle
= calloc(1, sizeof(ssl_test_handle
));
179 handle
->certs
= certs
;
180 handle
->st
= make_ssl_ref(true, comm
, certs
);
185 static void *securetransport_ssl_thread(void *arg
)
188 int sock
= *((int*)arg
);
190 int socket
= accept(sock
, NULL
, NULL
);
192 CFArrayRef server_certs
= server_chain();
193 ssl_test_handle
* ssl
= ssl_test_handle_create(socket
, server_certs
);
194 SSLContextRef ctx
= ssl
->st
;
196 pthread_setname_np("server thread");
198 //uint64_t start = mach_absolute_time();
200 ortn
= SSLHandshake(ctx
);
201 } while (ortn
== errSSLWouldBlock
);
203 require_noerr_action_quiet(ortn
, out
,
204 fprintf(stderr
, "Fell out of SSLHandshake with error: %d\n", (int)ortn
));
206 //uint64_t elapsed = mach_absolute_time() - start;
207 //fprintf(stderr, "setr elapsed: %lld\n", elapsed);
210 SSLProtocol proto = kSSLProtocolUnknown;
211 require_noerr_quiet(SSLGetNegotiatedProtocolVersion(ctx, &proto), out); */
213 SSLCipherSuite cipherSuite
;
214 require_noerr_quiet(ortn
= SSLGetNegotiatedCipher(ctx
, &cipherSuite
), out
);
215 //fprintf(stderr, "st negotiated %s\n", sslcipher_itoa(cipherSuite));
219 CFRelease(server_certs
);
227 pthread_exit((void *)(intptr_t)ortn
);
236 pthread_t server_thread
;
239 socket
= SocketListen(4443);
241 ok(socket
>=0, "SocketListen failed");
245 //fprintf(stderr, "session_id: %d\n", session_id);
247 pthread_create(&server_thread
, NULL
, securetransport_ssl_thread
, (void*)&socket
);
249 system("/usr/bin/openssl s_client -msg -debug -connect localhost:4443");
252 pthread_join(server_thread
, (void*)&server_err
);
254 ok(!server_err
, "Server thread failed err=%d", server_err
);
257 int ssl_50_server(int argc
, char *const *argv
)
260 plan_tests(1 + 1 /*cert*/);