7 #include <CoreFoundation/CoreFoundation.h>
9 #include <AssertMacros.h>
10 #include <Security/SecureTransportPriv.h> /* SSLSetOption */
11 #include <Security/SecureTransport.h>
12 #include <Security/SecPolicy.h>
13 #include <Security/SecTrust.h>
14 #include <Security/SecIdentity.h>
15 #include <Security/SecIdentityPriv.h>
16 #include <Security/SecCertificatePriv.h>
17 #include <Security/SecKeyPriv.h>
18 #include <Security/SecItem.h>
19 #include <Security/SecRandom.h>
21 #include <utilities/SecCFRelease.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
27 #include <mach/mach_time.h>
30 #include <Security/SecRSAKey.h>
33 #include "ssl-utils.h"
34 #import "STLegacyTests.h"
36 #define serverSelectedProtocol "baz"
37 #define serverAdvertisedProtocols "\x03""baz"
39 #pragma clang diagnostic push
40 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
42 @implementation STLegacyTests (sni)
53 #pragma mark SecureTransport support
55 #if SECTRANS_VERBOSE_DEBUG
56 static void hexdump(const uint8_t *bytes, size_t len) {
58 printf("socket write(%p, %lu)\n", bytes, len);
59 for (ix = 0; ix < len; ++ix) {
62 printf("%02X ", bytes[ix]);
67 #define hexdump(bytes, len)
71 static OSStatus SocketWrite(SSLConnectionRef h, const void *data, size_t *length)
74 uint8_t *ptr = (uint8_t *)data;
80 ret = write((int)h, ptr, len);
81 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
90 *length = *length - len;
94 static OSStatus SocketRead(SSLConnectionRef h, void *data, size_t *length)
97 uint8_t *ptr = (uint8_t *)data;
102 ret = read((int)h, ptr, len);
103 } while ((ret < 0) && (errno == EAGAIN || errno == EINTR));
108 printf("read error(%d): ret=%zd, errno=%d\n", (int)h, ret, errno);
113 *length = *length - len;
114 return errSecSuccess;
117 static char peername[] = "localhost";
119 static void *securetransport_server_thread(void *arg)
122 ssl_test_handle * ssl = (ssl_test_handle *)arg;
123 SSLContextRef ctx = ssl->handle;
124 CFArrayRef server_certs = server_chain();
127 ortn = SSLHandshake(ctx);
128 } while (ortn == errSSLWouldBlock);
130 if (ortn == errSSLClientHelloReceived) {
133 SSLCopyRequestedPeerNameLength(ctx, &length);
135 sni = malloc(length);
136 SSLCopyRequestedPeerName(ctx, sni, &length);
139 SSLProtocol version = 0;
140 require_noerr(SSLGetProtocolVersionMax(ctx, &version), out);
141 if (version == kSSLProtocol3) {
142 require_string(sni == NULL, out, "Unexpected SNI");
144 require_string(sni != NULL &&
145 length == sizeof(peername) &&
146 (memcmp(sni, peername, sizeof(peername))==0), out,
147 "SNI does not match");
149 require_noerr(SSLSetCertificate(ctx, server_certs), out);
153 alpnData.length = strlen(serverSelectedProtocol);
154 alpnData.data = malloc(alpnData.length);
155 memcpy(alpnData.data, serverSelectedProtocol, alpnData.length);
156 require_noerr_string(SSLSetALPNData(ctx, alpnData.data, alpnData.length), out, "Error setting alpn data");
160 npnData.length = strlen(serverAdvertisedProtocols);
161 npnData.data = malloc(npnData.length);
162 memcpy(npnData.data, serverAdvertisedProtocols, npnData.length);
163 require_noerr_string(SSLSetNPNData(ctx, npnData.data, npnData.length), out, "Error setting npn data");
172 CFReleaseSafe(server_certs);
174 pthread_exit((void *)(intptr_t)ortn);
178 static void *securetransport_client_thread(void *arg)
181 ssl_test_handle * ssl = (ssl_test_handle *)arg;
182 SSLContextRef ctx = ssl->handle;
185 ortn = SSLHandshake(ctx);
186 } while (ortn == errSSLWouldBlock || ortn != errSSLClosedGraceful);
189 uint8_t *alpnData = NULL;
190 alpnData = (uint8_t*)SSLGetALPNData(ctx, &length);
191 if (alpnData != NULL) {
192 require_noerr(memcmp(alpnData, serverSelectedProtocol, strlen(serverSelectedProtocol)), out);
196 uint8_t *npnData = NULL;
197 npnData = (uint8_t*)SSLGetNPNData(ctx, &length);
198 if (npnData != NULL) {
199 require_noerr_string(memcmp(npnData, serverAdvertisedProtocols, strlen(serverAdvertisedProtocols)),
200 out, "npn Data received does not match");
207 pthread_exit((void *)(intptr_t)ortn);
211 static SSLCipherSuite ciphers[] = {
212 TLS_RSA_WITH_AES_128_CBC_SHA,
215 static ssl_test_handle *
216 ssl_test_handle_create(uint32_t session_id, bool server, int comm)
218 ssl_test_handle *handle = calloc(1, sizeof(ssl_test_handle));
219 SSLContextRef ctx = SSLCreateContext(kCFAllocatorDefault, server?kSSLServerSide:kSSLClientSide, kSSLStreamType);
221 require(handle, out);
224 require_noerr(SSLSetIOFuncs(ctx,
225 (SSLReadFunc)SocketRead, (SSLWriteFunc)SocketWrite), out);
226 require_noerr(SSLSetConnection(ctx, (SSLConnectionRef)(intptr_t)comm), out);
229 require_noerr(SSLSetSessionOption(ctx,
230 kSSLSessionOptionBreakOnClientHello, true), out);
232 require_noerr(SSLSetSessionOption(ctx,
233 kSSLSessionOptionBreakOnServerAuth, true), out);
235 handle->handle = ctx;
236 handle->is_server = server;
237 handle->session_id = session_id;
243 if (handle) free(handle);
244 if (ctx) CFRelease(ctx);
248 static SSLProtocol versions[] = {
254 static int nversions = sizeof(versions)/sizeof(versions[0]);
259 pthread_t client_thread, server_thread;
261 for(j = 0; j < nversions; j++)
264 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) exit(errno);
266 ssl_test_handle *server, *client;
268 uint32_t session_id = (j+1) << 16 | 1 << 8;
269 server = ssl_test_handle_create(session_id, true /*server*/, sp[0]);
270 client = ssl_test_handle_create(session_id, false/*client*/, sp[1]);
272 XCTAssertEqual(errSecSuccess, SSLSetPeerID(server->handle, &session_id, sizeof(session_id)));
273 XCTAssertEqual(errSecSuccess, SSLSetPeerID(client->handle, &session_id, sizeof(session_id)));
274 const void *inputPeerId = NULL;
275 size_t inputPeerIdLen;
276 XCTAssertEqual(errSecSuccess, SSLGetPeerID(client->handle, &inputPeerId, &inputPeerIdLen));
278 /* set fixed cipher on client and server */
279 XCTAssertEqual(errSecSuccess, SSLSetEnabledCiphers(client->handle, &ciphers[0], 1));
280 XCTAssertEqual(errSecSuccess, SSLSetEnabledCiphers(server->handle, &ciphers[0], 1));
282 XCTAssertEqual(errSecSuccess, SSLSetProtocolVersionMax(client->handle, versions[j]));
283 XCTAssertEqual(errSecSuccess, SSLSetPeerDomainName(client->handle, peername, sizeof(peername)));
285 XCTAssertEqual(errSecSuccess, SSLSetProtocolVersionMax(server->handle, versions[j]));
287 pthread_create(&client_thread, NULL, securetransport_client_thread, client);
288 pthread_create(&server_thread, NULL, securetransport_server_thread, server);
290 intptr_t server_err, client_err;
291 pthread_join(client_thread, (void*)&client_err);
292 pthread_join(server_thread, (void*)&server_err);
302 #pragma clang diagnostic pop