5 // Created by Fabrice Gautier on 6/7/11.
6 // Copyright 2011 Apple, Inc. All rights reserved.
14 #include <sys/types.h>
15 #include <netinet/in.h>
16 #include <sys/socket.h>
18 #include <arpa/inet.h>
19 #include <CoreFoundation/CoreFoundation.h>
21 #include <AssertMacros.h>
22 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
23 #include <Security/SecureTransport.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
30 #include <mach/mach_time.h>
33 #include "ssl_regressions.h"
38 bool is_session_resume
;
41 bool client_side_auth
;
50 static void hexdump(const uint8_t *bytes
, size_t len
) {
52 printf("socket write(%p, %lu)\n", bytes
, len
);
53 for (ix
= 0; ix
< len
; ++ix
) {
56 printf("%02X ", bytes
[ix
]);
61 #define hexdump(bytes, len)
64 static int SocketConnect(const char *hostName
, int port
)
66 struct sockaddr_in addr
;
72 if (hostName
[0] >= '0' && hostName
[0] <= '9')
74 host
.s_addr
= inet_addr(hostName
);
78 #define GETHOST_RETRIES 5
79 /* seeing a lot of soft failures here that I really don't want to track down */
80 for(dex
=0; dex
<GETHOST_RETRIES
; dex
++) {
82 printf("\n...retrying gethostbyname(%s)", hostName
);
84 ent
= gethostbyname(hostName
);
90 printf("\n***gethostbyname(%s) returned: %s\n", hostName
, hstrerror(h_errno
));
93 memcpy(&host
, ent
->h_addr
, sizeof(struct in_addr
));
97 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
99 addr
.sin_port
= htons((u_short
)port
);
101 addr
.sin_family
= AF_INET
;
102 err
= connect(sock
, (struct sockaddr
*) &addr
, sizeof(struct sockaddr_in
));
106 perror("connect failed");
110 /* make non blocking */
111 fcntl(sock
, F_SETFL
, O_NONBLOCK
);
118 static OSStatus
SocketWrite(SSLConnectionRef conn
, const void *data
, size_t *length
)
120 size_t len
= *length
;
121 uint8_t *ptr
= (uint8_t *)data
;
127 ret
= write((int)conn
, ptr
, len
);
130 } while ((ret
< 0) && (errno
== EAGAIN
|| errno
== EINTR
));
139 *length
= *length
- len
;
140 return errSecSuccess
;
145 SSLConnectionRef connection
,
149 int fd
= (int)connection
;
152 len
= read(fd
, data
, *dataLength
);
158 //printf("SocketRead: EAGAIN\n");
160 /* nonblocking, no data */
161 return errSSLWouldBlock
;
163 perror("SocketRead");
168 if(len
<(ssize_t
)*dataLength
) {
170 return errSSLWouldBlock
;
173 return errSecSuccess
;
176 static SSLContextRef
make_ssl_ref(int sock
, SSLProtocol maxprot
, Boolean false_start
)
178 SSLContextRef ctx
= NULL
;
180 require_noerr(SSLNewContext(false, &ctx
), out
);
181 require_noerr(SSLSetIOFuncs(ctx
,
182 (SSLReadFunc
)SocketRead
, (SSLWriteFunc
)SocketWrite
), out
);
183 require_noerr(SSLSetConnection(ctx
, (SSLConnectionRef
)(intptr_t)sock
), out
);
185 require_noerr(SSLSetSessionOption(ctx
,
186 kSSLSessionOptionBreakOnServerAuth
, true), out
);
188 require_noerr(SSLSetSessionOption(ctx
,
189 kSSLSessionOptionFalseStart
, false_start
), out
);
191 require_noerr(SSLSetProtocolVersionMax(ctx
, maxprot
), out
);
196 SSLDisposeContext(ctx
);
200 const char request
[]="GET / HTTP/1.1\n\n";
203 static OSStatus
securetransport(ssl_test_handle
* ssl
)
206 SSLContextRef ctx
= ssl
->st
;
207 SecTrustRef trust
= NULL
;
208 bool got_server_auth
= false, got_client_cert_req
= false;
210 ortn
= SSLHandshake(ctx
);
211 //fprintf(stderr, "Fell out of SSLHandshake with error: %ld\n", (long)ortn);
213 size_t sent
, received
;
214 const char *r
=request
;
215 size_t l
=sizeof(request
);
219 ortn
= SSLWrite(ctx
, r
, l
, &sent
);
221 if(ortn
== errSSLWouldBlock
) {
226 if (ortn
== errSSLServerAuthCompleted
)
228 require_string(!got_server_auth
, out
, "second server auth");
229 require_string(!got_client_cert_req
, out
, "got client cert req before server auth");
230 got_server_auth
= true;
231 require_string(!trust
, out
, "Got errSSLServerAuthCompleted twice?");
232 /* verify peer cert chain */
233 require_noerr(SSLCopyPeerTrust(ctx
, &trust
), out
);
234 SecTrustResultType trust_result
= 0;
235 /* this won't verify without setting up a trusted anchor */
236 require_noerr(SecTrustEvaluate(trust
, &trust_result
), out
);
239 } while(ortn
== errSSLWouldBlock
|| ortn
== errSSLServerAuthCompleted
);
241 //fprintf(stderr, "\nHTTP Request Sent\n");
243 require_noerr_action_quiet(ortn
, out
, printf("SSLWrite failed with err %ld\n", (long)ortn
));
245 require_string(got_server_auth
, out
, "never got server auth");
248 ortn
= SSLRead(ctx
, reply
, sizeof(reply
)-1, &received
);
249 //fprintf(stderr, "r"); usleep(1000);
250 } while(ortn
== errSSLWouldBlock
);
252 //fprintf(stderr, "\n");
254 require_noerr_action_quiet(ortn
, out
, printf("SSLRead failed with err %ld\n", (long)ortn
));
258 //fprintf(stderr, "HTTP reply:\n");
259 //fprintf(stderr, "%s\n",reply);
263 SSLDisposeContext(ctx
);
264 if (trust
) CFRelease(trust
);
271 static ssl_test_handle
*
272 ssl_test_handle_create(int comm
, SSLProtocol maxprot
, Boolean false_start
)
274 ssl_test_handle
*handle
= calloc(1, sizeof(ssl_test_handle
));
277 handle
->st
= make_ssl_ref(comm
, maxprot
, false_start
);
288 /* Good tls 1.2 servers */
289 {"encrypted.google.com", 443, kTLSProtocol12
},
290 {"www.amazon.com",443, kTLSProtocol12
},
291 {"www.mikestoolbox.org",443, kTLSProtocol12
},
294 #define NSERVERS (int)(sizeof(servers)/sizeof(servers[0]))
303 for(p
=0; p
<NSERVERS
;p
++) {
304 for(int loops
=0; loops
<NLOOPS
; loops
++) {
305 for(fs
=0;fs
<2; fs
++) {
307 ssl_test_handle
*client
;
312 s
=SocketConnect(servers
[p
].host
, servers
[p
].port
);
314 fail("connect failed with err=%d - %s:%d (try %d)", s
, servers
[p
].host
, servers
[p
].port
, loops
);
318 client
= ssl_test_handle_create(s
, servers
[p
].maxprot
, fs
);
320 r
=securetransport(client
);
321 ok(!r
, "handshake failed with err=%ld - %s:%d (try %d), false start=%d", (long)r
, servers
[p
].host
, servers
[p
].port
, loops
, fs
);
327 int ssl_47_falsestart(int argc
, char *const *argv
)
329 plan_tests(NSERVERS
*NLOOPS
*2);