]>
git.saurik.com Git - apple/security.git/blob - tlsnke/tlsnketest/main.c
5 // Created by Fabrice Gautier on 12/7/11.
6 // Copyright (c) 2011 Apple, Inc. All rights reserved.
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <net/kext_net.h>
24 #include <AssertMacros.h>
25 #include "tlssocket.h"
29 static void print_data(const char *s
, size_t l
, const unsigned char *p
)
31 printf("%s, %zu:",s
, l
);
32 for(int i
=0; i
<l
; i
++)
33 printf(" %02x", p
[i
]);
37 static void *server_thread_func(void *arg
)
40 struct sockaddr_in server_addr
;
43 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
44 perror("server socket");
48 // Dont use TLSSocket_Attach for the server:
49 // TLSSocket_Attach can only open one TLS socket at a time.
51 struct so_nke so_tlsnke
;
53 memset(&so_tlsnke
, 0, sizeof(so_tlsnke
));
54 so_tlsnke
.nke_handle
= TLS_HANDLE_IP4
;
55 err
=setsockopt(sock
, SOL_SOCKET
, SO_NKE
, &so_tlsnke
, sizeof(so_tlsnke
));
57 perror("attach (server)");
62 server_addr
.sin_family
= AF_INET
;
63 server_addr
.sin_port
= htons(23232);
64 server_addr
.sin_addr
.s_addr
= INADDR_ANY
;
65 bzero(&(server_addr
.sin_zero
),8);
67 if (bind(sock
, (struct sockaddr
*)&server_addr
, sizeof(struct sockaddr
))
69 perror("Unable to bind");
73 printf("\nBound - Server Waiting for client on port 23232\n");
80 rc
=TLSSocket_Funcs
.read((intptr_t)sock
, &rec
);
82 print_data("recvd", rec
.contents
.length
, rec
.contents
.data
);
83 rec
.contents
.data
[rec
.contents
.length
-1]=0;
84 printf("recvd: %ld, %s\n", rec
.contents
.length
, rec
.contents
.data
);
85 free(rec
.contents
.data
);
87 printf("read failed: %d\n", rc
);
95 static int create_client_socket(const char *hostname
)
101 printf("Create client socket\n");
102 sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
104 perror("client socket");
110 err
=TLSSocket_Attach(sock
);
112 perror("TLSSocket_Attach (server)");
118 struct hostent
*host
;
119 struct sockaddr_in server_addr
;
121 //host = gethostbyname("kruk.apple.com");
122 //host = gethostbyname("localhost");
123 host
= gethostbyname(hostname
);
128 server_addr
.sin_family
= AF_INET
;
129 server_addr
.sin_port
= htons(23232);
130 server_addr
.sin_addr
= *((struct in_addr
*)host
->h_addr
);
131 bzero(&(server_addr
.sin_zero
),8);
133 err
= connect(sock
, (struct sockaddr
*)&server_addr
,
134 sizeof(struct sockaddr
));
145 static int kext_test(const char *hostname
, int bypass
)
148 char send_data
[1024];
150 pthread_t server_thread
;
152 if(strcmp(hostname
, "localhost")==0) {
153 pthread_create(&server_thread
, NULL
, server_thread_func
, NULL
);
154 // Just wait for the server to be setup
159 sock
= create_client_socket(hostname
);
162 /* Have to open this after we attached the filter to the client socket */
163 tlsfd
=open("/dev/tlsnke", O_RDWR
);
165 perror("open tlsnke");
174 n
=sprintf(send_data
, "Message #%d\n", i
);
180 printf("Client(1) sending %d bytes (\"%s\")\n", n
, send_data
);
183 err
= write(tlsfd
, send_data
, n
);
185 perror("write to tlsnke");
191 rec
.contentType
= SSL_RecordTypeAppData
;
192 rec
.protocolVersion
= DTLS_Version_1_0
;
193 rec
.contents
.data
= (uint8_t *)send_data
;
194 rec
.contents
.length
= n
;
196 err
= TLSSocket_Funcs
.write((intptr_t)sock
, rec
);
198 perror("write to socket");
202 /* serviceWriteQueue every 2 writes, this will trigger rdar://11348395 */
205 err
= TLSSocket_Funcs
.serviceWriteQueue((intptr_t)sock
);
207 perror("service write queue");
224 int dtls_client(const char *hostname
, int bypass
);
227 int usage(const char *argv0
)
229 printf("Usage: %s <test> <hostname> <bypass>\n", argv0
);
230 printf(" <test>: type of test: 's'imple, 'h'andshake or 'e'cho] (see below)\n");
231 printf(" <hostname>: hostname of server\n");
232 printf(" <bypass>: use /dev/tlsnke bypass test\n");
234 printf("\n 'S'imple test:\n"
235 "\tVery basic test with no handshake. DTLS packets are sent through the socket filter, non encrypted.\n"
236 "\tIf hostname is 'localhost', a local simple server will be created that will also use the tls filter,\n"
237 "\tsuch that the input path is tested.\n"
238 "\tOtherwise, a server on the other side is not required only the output path is tested. If there is no server replying\n"
239 "\tonly the ouput path will be tested. If a server is replying, input packet will be processed but are never read to userspace\n"
240 "\tif bypass=1, also send the same packet through the /dev/tlsnke interface, as if they were coming from utun\n");
242 printf("\n 'H'andshake:\n");
243 printf("\tTest SSL Handshake with various ciphers, between a local client going through the tlsnke\n"
244 "\tfilter, and a local server using only the userland SecureTransport.\n"
245 "\thostname and bypass are ignored.\n");
247 printf("\n 'E'cho:\n");
248 printf("\tTest to connect to an udp echo server indicated by hostname, on port 23232.\n"
249 "\tSet bypass=1 to use the /dev/tlsnke bsd device to send/recv the app data (emulate utun behaviour)\n");
251 printf("\n\tbypass=1 require the tlsnke kext to be compiled with TLS_TEST=1 (not the default in the build)\n");
256 int main (int argc
, const char * argv
[])
259 printf("argv0=%s argc=%d\n", argv
[0], argc
);
261 return usage(argv
[0]);
263 switch (argv
[1][0]) {
266 if(argc
<3) return usage(argv
[0]);
267 return kext_test(argv
[2], atoi(argv
[3])?1:0);
273 if(argc
<3) return usage(argv
[0]);
274 return dtls_client(argv
[2], atoi(argv
[3])?1:0);
276 return usage(argv
[0]);