2 * Copyright (c) 2011,2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
34 #include <arpa/inet.h>
35 #include <CoreFoundation/CoreFoundation.h>
37 #include <AssertMacros.h>
38 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
39 #include <Security/SecureTransport.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
46 #include <mach/mach_time.h>
48 #import "STLegacyTests.h"
50 #pragma clang diagnostic push
51 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
53 @implementation STLegacyTests (falsestart)
57 bool is_session_resume;
60 bool client_side_auth;
67 #if SECTRANS_VERBOSE_DEBUG
68 static void hexdump(const uint8_t *bytes, size_t len) {
70 printf("socket write(%p, %lu)\n", bytes, len);
71 for (ix = 0; ix < len; ++ix) {
74 printf("%02X ", bytes[ix]);
79 #define hexdump(bytes, len)
82 static int SocketConnect(const char *hostName, int port)
84 struct sockaddr_in addr;
89 char **h_addr_list = NULL;
91 if (hostName[0] >= '0' && hostName[0] <= '9') {
92 host.s_addr = inet_addr(hostName);
94 ent = gethostbyname(hostName);
96 printf("\n***gethostbyname(%s) returned: %s\n", hostName, hstrerror(h_errno));
99 h_addr_list = malloc(sizeof(char *));
100 memcpy(h_addr_list, ent->h_addr_list, sizeof(char *));
101 memcpy(&host, h_addr_list[0], sizeof(struct in_addr));
104 sock = socket(AF_INET, SOCK_STREAM, 0);
105 addr.sin_addr.s_addr = host.s_addr;
107 addr.sin_port = htons((u_short)port);
109 addr.sin_family = AF_INET;
110 err = connect(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
114 perror("connect failed");
118 /* make non blocking */
119 fcntl(sock, F_SETFL, O_NONBLOCK);
123 static OSStatus SocketWrite(SSLConnectionRef conn, const void *data, size_t *length)
125 size_t len = *length;
126 uint8_t *ptr = (uint8_t *)data;
132 ret = write((int)conn, ptr, len);
135 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
144 *length = *length - len;
145 return errSecSuccess;
150 SSLConnectionRef connection,
154 int fd = (int)connection;
157 len = read(fd, data, *dataLength);
164 /* nonblocking, no data */
165 return errSSLWouldBlock;
167 perror("SocketRead");
172 if (len < (ssize_t)*dataLength) {
174 return errSSLWouldBlock;
177 return errSecSuccess;
180 static SSLContextRef make_ssl_ref(int sock, SSLProtocol maxprot, Boolean false_start)
182 SSLContextRef ctx = NULL;
184 require_noerr(SSLNewContext(false, &ctx), out);
185 require_noerr(SSLSetIOFuncs(ctx,
186 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
187 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)sock), out);
189 require_noerr(SSLSetSessionOption(ctx,
190 kSSLSessionOptionBreakOnServerAuth, true), out);
192 require_noerr(SSLSetSessionOption(ctx,
193 kSSLSessionOptionFalseStart, false_start), out);
195 require_noerr(SSLSetProtocolVersionMax(ctx, maxprot), out);
200 SSLDisposeContext(ctx);
205 const char request[]="GET / HTTP/1.1\n\n";
208 static OSStatus securetransport(ssl_test_handle * ssl)
211 SSLContextRef ctx = ssl->st;
212 SecTrustRef trust = NULL;
213 bool got_server_auth = false, got_client_cert_req = false;
215 ortn = SSLHandshake(ctx);
217 require_action_quiet(ortn == errSSLWouldBlock, out, printf("SSLHandshake failed with err %ld\n", (long)ortn));
219 size_t sent, received;
220 const char *r = request;
221 size_t l = sizeof(request);
225 ortn = SSLWrite(ctx, r, l, &sent);
227 if (ortn == errSSLWouldBlock) {
232 if (ortn == errSSLServerAuthCompleted) {
233 require_string(!got_server_auth, out, "second server auth");
234 require_string(!got_client_cert_req, out, "got client cert req before server auth");
235 got_server_auth = true;
236 require_string(!trust, out, "Got errSSLServerAuthCompleted twice?");
237 /* verify peer cert chain */
238 require_noerr(SSLCopyPeerTrust(ctx, &trust), out);
239 SecTrustResultType trust_result = 0;
240 /* this won't verify without setting up a trusted anchor */
241 require_noerr(SecTrustEvaluate(trust, &trust_result), out);
243 } while (ortn == errSSLWouldBlock || ortn == errSSLServerAuthCompleted);
245 require_noerr_action_quiet(ortn, out, printf("SSLWrite failed with err %ld\n", (long)ortn));
247 require_string(got_server_auth, out, "never got server auth");
250 ortn = SSLRead(ctx, reply, sizeof(reply)-1, &received);
251 } while (ortn == errSSLWouldBlock);
253 require_noerr_action_quiet(ortn, out, printf("SSLRead failed with err %ld\n", (long)ortn));
259 SSLDisposeContext(ctx);
260 if (trust) CFRelease(trust);
265 static ssl_test_handle *
266 ssl_test_handle_create(int comm, SSLProtocol maxprot, Boolean false_start)
268 ssl_test_handle *handle = calloc(1, sizeof(ssl_test_handle));
271 handle->st = make_ssl_ref(comm, maxprot, false_start);
282 /* Good tls 1.2 servers */
283 {"encrypted.google.com", 443, kTLSProtocol12 },
284 {"www.amazon.com",443, kTLSProtocol12 },
287 #define NSERVERS (int)(sizeof(servers)/sizeof(servers[0]))
289 #define CONNECT_TRIES 3
291 -(void)testFalseStart
296 for (p = 0; p < NSERVERS; p++) {
297 for (int loops = 0; loops < NLOOPS; loops++) {
298 for (fs = 0;fs < 2; fs++) {
300 ssl_test_handle *client;
304 for (int try = 0; s < 0 && try < CONNECT_TRIES; try++) {
305 s = SocketConnect(servers[p].host, servers[p].port);
310 client = ssl_test_handle_create(s, servers[p].maxprot, fs);
312 r = securetransport(client);
313 XCTAssert(!r, "handshake failed with err=%ld - %s:%d (try %d), false start=%d", (long)r, servers[p].host, servers[p].port, loops, fs);
324 #pragma clang diagnostic pop