]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_ssl/regressions/SecureTransportTests/STLegacyTests+tls12.m
Security-58286.240.4.tar.gz
[apple/security.git] / OSX / libsecurity_ssl / regressions / SecureTransportTests / STLegacyTests+tls12.m
1 /*
2 * Copyright (c) 2011-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 #include <Security/SecPolicy.h>
41 #include <Security/SecTrust.h>
42 #include <Security/SecIdentity.h>
43 #include <Security/SecIdentityPriv.h>
44 #include <Security/SecCertificatePriv.h>
45 #include <Security/SecKeyPriv.h>
46 #include <Security/SecItem.h>
47 #include <Security/SecRandom.h>
48
49 #include <string.h>
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <errno.h>
53 #include <stdlib.h>
54 #include <mach/mach_time.h>
55
56 #include "STLegacyTests.h"
57
58 @implementation STLegacyTests (tls12)
59
60 struct s_server {
61 char *host;
62 int port;
63 SSLProtocol maxprot;
64 };
65
66 typedef struct {
67 struct s_server *server;
68 uint32_t session_id;
69 bool is_session_resume;
70 SSLContextRef st;
71 bool client_side_auth;
72 bool dh_anonymous;
73 int comm;
74 CFArrayRef certs;
75 } ssl_test_handle;
76
77
78
79 #if 0
80 static void hexdump(const uint8_t *bytes, size_t len) {
81 size_t ix;
82 printf("socket write(%p, %lu)\n", bytes, len);
83 for (ix = 0; ix < len; ++ix) {
84 if (!(ix % 16))
85 printf("\n");
86 printf("%02X ", bytes[ix]);
87 }
88 printf("\n");
89 }
90 #else
91 #define hexdump(bytes, len)
92 #endif
93
94 static int SocketConnect(const char *hostName, int port)
95 {
96 struct sockaddr_in addr;
97 struct in_addr host;
98 int sock;
99 int err;
100 struct hostent *ent;
101
102 if (hostName[0] >= '0' && hostName[0] <= '9') {
103 host.s_addr = inet_addr(hostName);
104 } else {
105 ent = gethostbyname(hostName);
106 if(ent == NULL) {
107 printf("\n***gethostbyname(%s) returned: %s\n", hostName, hstrerror(h_errno));
108 return -2;
109 }
110 memcpy(&host, ent->h_addr, sizeof(struct in_addr));
111 }
112
113 sock = socket(AF_INET, SOCK_STREAM, 0);
114 addr.sin_addr = host;
115 addr.sin_port = htons((u_short)port);
116
117 addr.sin_family = AF_INET;
118 err = connect(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
119
120 if(err!=0)
121 {
122 perror("connect failed");
123 return -1;
124 }
125
126 return sock;
127 }
128
129
130 static OSStatus SocketWrite(SSLConnectionRef conn, const void *data, size_t *length)
131 {
132 size_t len = *length;
133 uint8_t *ptr = (uint8_t *)data;
134
135 do {
136 ssize_t ret;
137 do {
138 hexdump(ptr, len);
139 ret = write((int)conn, ptr, len);
140 if (ret < 0)
141 perror("send");
142 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
143 if (ret > 0) {
144 len -= ret;
145 ptr += ret;
146 }
147 else
148 return -36;
149 } while (len > 0);
150
151 *length = *length - len;
152 return errSecSuccess;
153 }
154
155 static OSStatus SocketRead(SSLConnectionRef conn, void *data, size_t *length)
156 {
157 size_t len = *length;
158 uint8_t *ptr = (uint8_t *)data;
159
160 do {
161 ssize_t ret;
162 do {
163 ret = read((int)conn, ptr, len);
164 if (ret < 0)
165 perror("send");
166 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
167 if (ret > 0) {
168 len -= ret;
169 ptr += ret;
170 }
171 else
172 return -36;
173 } while (len > 0);
174
175 *length = *length - len;
176 return errSecSuccess;
177 }
178
179 static SSLContextRef make_ssl_ref(int sock, SSLProtocol maxprot, const char *peerName)
180 {
181 SSLContextRef ctx = NULL;
182
183 require_noerr(SSLNewContext(false, &ctx), out);
184 require_noerr(SSLSetIOFuncs(ctx,
185 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
186 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)sock), out);
187
188 require_noerr(SSLSetSessionOption(ctx,
189 kSSLSessionOptionBreakOnServerAuth, true), out);
190
191 require_noerr(SSLSetProtocolVersionMax(ctx, maxprot), out);
192
193 require_noerr(SSLSetPeerDomainName(ctx, peerName, strlen(peerName)), out);
194 /* Tell SecureTransport to not check certs itself: it will break out of the
195 handshake to let us take care of it instead. */
196 require_noerr(SSLSetEnableCertVerify(ctx, false), out);
197
198 return ctx;
199 out:
200 if (ctx)
201 SSLDisposeContext(ctx);
202 return NULL;
203 }
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 //uint64_t start = mach_absolute_time();
213 do {
214 ortn = SSLHandshake(ctx);
215
216 if (ortn == errSSLServerAuthCompleted)
217 {
218 require_string(!got_server_auth, out, "second server auth");
219 require_string(!got_client_cert_req, out, "got client cert req before server auth");
220 got_server_auth = true;
221 require_string(!trust, out, "Got errSSLServerAuthCompleted twice?");
222 /* verify peer cert chain */
223 require_noerr(SSLCopyPeerTrust(ctx, &trust), out);
224 SecTrustResultType trust_result = 0;
225 /* this won't verify without setting up a trusted anchor */
226 require_noerr(SecTrustEvaluate(trust, &trust_result), out);
227 require((trust_result == kSecTrustResultUnspecified), out);
228
229 }
230 } while (ortn == errSSLWouldBlock
231 || ortn == errSSLServerAuthCompleted);
232 require_noerr_action_quiet(ortn, out,
233 fprintf(stderr, "Fell out of SSLHandshake with error: %d\n", (int)ortn));
234
235 require_string(got_server_auth, out, "never got server auth");
236
237 //uint64_t elapsed = mach_absolute_time() - start;
238 //fprintf(stderr, "setr elapsed: %lld\n", elapsed);
239
240 /*
241 SSLProtocol proto = kSSLProtocolUnknown;
242 require_noerr_quiet(SSLGetNegotiatedProtocolVersion(ctx, &proto), out); */
243
244 SSLCipherSuite cipherSuite;
245 require_noerr_quiet(ortn = SSLGetNegotiatedCipher(ctx, &cipherSuite), out);
246 //fprintf(stderr, "st negotiated %02x\n", cipherSuite);
247
248
249 out:
250 SSLClose(ctx);
251 SSLDisposeContext(ctx);
252 if (trust) CFRelease(trust);
253
254 return ortn;
255 }
256
257
258
259 #define CONNECT_TRIES 3
260
261 -(ssl_test_handle*)ssl_test_handle_create:(struct s_server *)server
262 {
263 int comm = -1;
264
265 for(int try = 0; comm<0 && try<CONNECT_TRIES; try++) {
266 comm=SocketConnect(server->host, server->port);
267 }
268
269 if(comm<0) {
270 XCTFail("connect failed with err=%d - %s:%d", comm, server->host, server->port);
271 return NULL;
272 }
273
274 ssl_test_handle *handle = calloc(1, sizeof(ssl_test_handle));
275 if (handle) {
276 handle->comm = comm;
277 handle->st = make_ssl_ref(comm, server->maxprot, server->host);
278 handle->server = server;
279 }
280 return handle;
281 }
282
283 static void
284 ssl_test_handle_destroy(ssl_test_handle *handle)
285 {
286 close(handle->comm);
287 free(handle);
288 }
289
290
291 struct s_server servers[] = {
292 #if !TARGET_OS_IPHONE //This test run on AppleWifi on iPhone, so we can't connect to internal servers.
293 {"nod.apple.com", 636, kTLSProtocol12 }, // This one has only the server eku, not the client one.
294 #endif
295 {"gsa.apple.com", 443, kTLSProtocol12 }, // This one has only the server eku, not the client one.
296 /* Good tls 1.2 servers */
297 {"sslanalyzer.comodoca.com", 443, kTLSProtocol12 }, // This one has a stapled OCSP response with SCTs.
298 {"encrypted.google.com", 443, kTLSProtocol12 },
299 {"www.amazon.com",443, kTLSProtocol12 },
300 //{"www.mikestoolbox.org",443, kTLSProtocol12 },
301 /* servers with issues */
302 // This server went offline as of May 2016 -- {"vpp.visa.co.uk", 443, kTLSProtocol12 }, // Doesnt like SSL 3.0 in initial record layer version
303 {"imap.softbank.jp",993, kTLSProtocol12 }, // softbank imap server, there are multiple servers behind this, one of them is not able to handle downgrading to TLS 1.2 properly (126.240.66.17).
304 {"mobile.charter.net",993, kTLSProtocol12 }, // Support 1.2 but fail to negotiate properly
305 {"mybill.vodafone.com.au", 443, kTLSProtocol1 }, /* 2056 bit server key */
306 };
307
308 #define NSERVERS (int)(sizeof(servers)/sizeof(servers[0]))
309 #define NLOOPS 1
310
311 -(void)testTls12
312 {
313 int p;
314 OSStatus r;
315
316 for(p=0; p<NSERVERS;p++) {
317 for(int loops=0; loops<NLOOPS; loops++) {
318 ssl_test_handle *client;
319
320 SKIP: {
321 if(!(client = [self ssl_test_handle_create:&servers[p]]))
322 continue;
323 r=securetransport(client);
324 XCTAssert(r==errSecSuccess, "handshake failed with err=%ld - %s:%d (try %d)", (long)r, servers[p].host, servers[p].port, loops);
325
326 ssl_test_handle_destroy(client);
327 }
328 }
329 }
330 }
331
332 @end
333