]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_ssl/regressions/SecureTransportTests/STLegacyTests+falsestart.m
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / libsecurity_ssl / regressions / SecureTransportTests / STLegacyTests+falsestart.m
1 /*
2 * Copyright (c) 2011,2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include <stdbool.h>
26 #include <pthread.h>
27 #include <fcntl.h>
28 #include <sys/mman.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 #include <netdb.h>
34 #include <arpa/inet.h>
35 #include <CoreFoundation/CoreFoundation.h>
36
37 #include <AssertMacros.h>
38 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
39 #include <Security/SecureTransport.h>
40
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <mach/mach_time.h>
47
48 #import "STLegacyTests.h"
49
50 #pragma clang diagnostic push
51 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
52
53 @implementation STLegacyTests (falsestart)
54
55 typedef struct {
56 uint32_t session_id;
57 bool is_session_resume;
58 SSLContextRef st;
59 bool is_server;
60 bool client_side_auth;
61 bool dh_anonymous;
62 int comm;
63 CFArrayRef certs;
64 } ssl_test_handle;
65
66
67 #if SECTRANS_VERBOSE_DEBUG
68 static void hexdump(const uint8_t *bytes, size_t len) {
69 size_t ix;
70 printf("socket write(%p, %lu)\n", bytes, len);
71 for (ix = 0; ix < len; ++ix) {
72 if (!(ix % 16))
73 printf("\n");
74 printf("%02X ", bytes[ix]);
75 }
76 printf("\n");
77 }
78 #else
79 #define hexdump(bytes, len)
80 #endif
81
82 static int SocketConnect(const char *hostName, int port)
83 {
84 struct sockaddr_in addr;
85 struct in_addr host;
86 int sock;
87 int err;
88 struct hostent *ent;
89
90 if (hostName[0] >= '0' && hostName[0] <= '9') {
91 host.s_addr = inet_addr(hostName);
92 } else {
93 ent = gethostbyname(hostName);
94 if(ent == NULL) {
95 printf("\n***gethostbyname(%s) returned: %s\n", hostName, hstrerror(h_errno));
96 return -2;
97 }
98 memcpy(&host, ent->h_addr, sizeof(struct in_addr));
99 }
100
101 sock = socket(AF_INET, SOCK_STREAM, 0);
102 addr.sin_addr = host;
103 addr.sin_port = htons((u_short)port);
104
105 addr.sin_family = AF_INET;
106 err = connect(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
107
108 if(err != 0)
109 {
110 perror("connect failed");
111 return -1;
112 }
113
114 /* make non blocking */
115 fcntl(sock, F_SETFL, O_NONBLOCK);
116
117 return sock;
118 }
119
120 static OSStatus SocketWrite(SSLConnectionRef conn, const void *data, size_t *length)
121 {
122 size_t len = *length;
123 uint8_t *ptr = (uint8_t *)data;
124
125 do {
126 ssize_t ret;
127 do {
128 hexdump(ptr, len);
129 ret = write((int)conn, ptr, len);
130 if (ret < 0)
131 perror("send");
132 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
133 if (ret > 0) {
134 len -= ret;
135 ptr += ret;
136 } else {
137 return -36;
138 }
139 } while (len > 0);
140
141 *length = *length - len;
142 return errSecSuccess;
143 }
144
145 static
146 OSStatus SocketRead(
147 SSLConnectionRef connection,
148 void *data,
149 size_t *dataLength)
150 {
151 int fd = (int)connection;
152 ssize_t len;
153
154 len = read(fd, data, *dataLength);
155
156 if (len < 0) {
157 int theErr = errno;
158 switch(theErr) {
159 case EAGAIN:
160 *dataLength=0;
161 /* nonblocking, no data */
162 return errSSLWouldBlock;
163 default:
164 perror("SocketRead");
165 return -36;
166 }
167 }
168
169 if (len < (ssize_t)*dataLength) {
170 *dataLength = len;
171 return errSSLWouldBlock;
172 }
173
174 return errSecSuccess;
175 }
176
177 static SSLContextRef make_ssl_ref(int sock, SSLProtocol maxprot, Boolean false_start)
178 {
179 SSLContextRef ctx = NULL;
180
181 require_noerr(SSLNewContext(false, &ctx), out);
182 require_noerr(SSLSetIOFuncs(ctx,
183 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
184 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)sock), out);
185
186 require_noerr(SSLSetSessionOption(ctx,
187 kSSLSessionOptionBreakOnServerAuth, true), out);
188
189 require_noerr(SSLSetSessionOption(ctx,
190 kSSLSessionOptionFalseStart, false_start), out);
191
192 require_noerr(SSLSetProtocolVersionMax(ctx, maxprot), out);
193
194 return ctx;
195 out:
196 if (ctx) {
197 SSLDisposeContext(ctx);
198 }
199 return NULL;
200 }
201
202 const char request[]="GET / HTTP/1.1\n\n";
203 char reply[2048];
204
205 static OSStatus securetransport(ssl_test_handle * ssl)
206 {
207 OSStatus ortn;
208 SSLContextRef ctx = ssl->st;
209 SecTrustRef trust = NULL;
210 bool got_server_auth = false, got_client_cert_req = false;
211
212 ortn = SSLHandshake(ctx);
213
214 require_action_quiet(ortn == errSSLWouldBlock, out, printf("SSLHandshake failed with err %ld\n", (long)ortn));
215
216 size_t sent, received;
217 const char *r = request;
218 size_t l = sizeof(request);
219
220 do {
221
222 ortn = SSLWrite(ctx, r, l, &sent);
223
224 if (ortn == errSSLWouldBlock) {
225 r += sent;
226 l -= sent;
227 }
228
229 if (ortn == errSSLServerAuthCompleted) {
230 require_string(!got_server_auth, out, "second server auth");
231 require_string(!got_client_cert_req, out, "got client cert req before server auth");
232 got_server_auth = true;
233 require_string(!trust, out, "Got errSSLServerAuthCompleted twice?");
234 /* verify peer cert chain */
235 require_noerr(SSLCopyPeerTrust(ctx, &trust), out);
236 SecTrustResultType trust_result = 0;
237 /* this won't verify without setting up a trusted anchor */
238 require_noerr(SecTrustEvaluate(trust, &trust_result), out);
239 }
240 } while (ortn == errSSLWouldBlock || ortn == errSSLServerAuthCompleted);
241
242 require_noerr_action_quiet(ortn, out, printf("SSLWrite failed with err %ld\n", (long)ortn));
243
244 require_string(got_server_auth, out, "never got server auth");
245
246 do {
247 ortn = SSLRead(ctx, reply, sizeof(reply)-1, &received);
248 } while (ortn == errSSLWouldBlock);
249
250 require_noerr_action_quiet(ortn, out, printf("SSLRead failed with err %ld\n", (long)ortn));
251
252 reply[received] = 0;
253
254 out:
255 SSLClose(ctx);
256 SSLDisposeContext(ctx);
257 if (trust) CFRelease(trust);
258
259 return ortn;
260 }
261
262 static ssl_test_handle *
263 ssl_test_handle_create(int comm, SSLProtocol maxprot, Boolean false_start)
264 {
265 ssl_test_handle *handle = calloc(1, sizeof(ssl_test_handle));
266 if (handle) {
267 handle->comm = comm;
268 handle->st = make_ssl_ref(comm, maxprot, false_start);
269 }
270 return handle;
271 }
272
273 static
274 struct s_server {
275 char *host;
276 int port;
277 SSLProtocol maxprot;
278 } servers[] = {
279 /* Good tls 1.2 servers */
280 {"encrypted.google.com", 443, kTLSProtocol12 },
281 {"www.amazon.com",443, kTLSProtocol12 },
282 };
283
284 #define NSERVERS (int)(sizeof(servers)/sizeof(servers[0]))
285 #define NLOOPS 1
286 #define CONNECT_TRIES 3
287
288 -(void)testFalseStart
289 {
290 int p;
291 int fs;
292
293 for (p = 0; p < NSERVERS; p++) {
294 for (int loops = 0; loops < NLOOPS; loops++) {
295 for (fs = 0;fs < 2; fs++) {
296
297 ssl_test_handle *client;
298 OSStatus r;
299 int s = -1;
300
301 for (int try = 0; s < 0 && try < CONNECT_TRIES; try++) {
302 s = SocketConnect(servers[p].host, servers[p].port);
303 }
304 if (s < 0) {
305 break;
306 }
307 client = ssl_test_handle_create(s, servers[p].maxprot, fs);
308
309 r = securetransport(client);
310 XCTAssert(!r, "handshake failed with err=%ld - %s:%d (try %d), false start=%d", (long)r, servers[p].host, servers[p].port, loops, fs);
311
312 close(s);
313 free(client);
314 }
315 }
316 }
317 }
318
319 @end
320
321 #pragma clang diagnostic pop