]> git.saurik.com Git - apple/security.git/blob - dtlsEcho/dtlsEchoClient.c
Security-59754.60.13.tar.gz
[apple/security.git] / dtlsEcho / dtlsEchoClient.c
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 <Security/Security.h>
26 #include <Security/SecBase.h>
27
28 #include "../sslViewer/sslAppUtils.h"
29
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <unistd.h> /* close() */
38 #include <string.h> /* memset() */
39 #include <fcntl.h>
40 #include <time.h>
41
42 #ifdef NO_SERVER
43 #include "keychain/securityd/spi.h"
44 #endif
45
46 static
47 unsigned char ServerRSA_Key_der[] = {
48 0x30, 0x82, 0x02, 0x5b, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xab,
49 0x05, 0xba, 0xdc, 0x2d, 0xb3, 0x03, 0xf1, 0x6c, 0x60, 0x7f, 0x5e, 0x80,
50 0x85, 0x54, 0x24, 0xc7, 0x87, 0x6e, 0xe5, 0xf1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3c, 0xc8, 0x17, 0x97, 0xb7, 0x05, 0xd9,
96 0x18, 0xb6, 0x43, 0x16, 0xf8, 0xf9, 0x5e, 0xed, 0x7e, 0x6f, 0xc4, 0xa6,
97 0x6a, 0x3a, 0xc7, 0xc0, 0x21, 0x6b, 0x39, 0xac, 0xf3, 0xf6, 0x40, 0xd5,
98 0x25, 0xf7, 0x44, 0x1d, 0xd4, 0xb2, 0x37
99 };
100
101 static
102 unsigned char ServerRSA_Cert_CA_RSA_der[] = {
103 0x30, 0x82, 0x02, 0x69, 0x30, 0x82, 0x01, 0x51, 0xa0, 0x03, 0x02, 0x01,
104 0x02, 0x02, 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
105 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x25, 0x31, 0x23, 0x30,
106 0x21, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x1a, 0x53, 0x65, 0x63, 0x75,
107 0x72, 0x69, 0x74, 0x79, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x41, 0x20,
108 0x43, 0x65, 0x72, 0x74, 0x20, 0x28, 0x52, 0x53, 0x41, 0x29, 0x30, 0x20,
109 0x17, 0x0d, 0x31, 0x35, 0x30, 0x33, 0x32, 0x33, 0x30, 0x37, 0x31, 0x30,
110 0x32, 0x36, 0x5a, 0x18, 0x0f, 0x32, 0x30, 0x35, 0x35, 0x30, 0x33, 0x31,
111 0x33, 0x30, 0x37, 0x31, 0x30, 0x32, 0x36, 0x5a, 0x30, 0x3e, 0x31, 0x28,
112 0x30, 0x26, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x1f, 0x53, 0x65, 0x63,
113 0x75, 0x72, 0x69, 0x74, 0x79, 0x54, 0x65, 0x73, 0x74, 0x73, 0x20, 0x53,
114 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x43, 0x65, 0x72, 0x74, 0x20, 0x28,
115 0x52, 0x53, 0x41, 0x29, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04,
116 0x03, 0x13, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
117 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
118 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81,
119 0x89, 0x02, 0x81, 0x81, 0x00, 0xab, 0x05, 0xba, 0xdc, 0x2d, 0xb3, 0x03,
120 0xf1, 0x6c, 0x60, 0x7f, 0x5e, 0x80, 0x85, 0x54, 0x24, 0xc7, 0x87, 0x6e,
121 0xe5, 0xf1, 0xae, 0x76, 0x59, 0xe3, 0x4b, 0x9c, 0xff, 0xa2, 0x41, 0xfc,
122 0x40, 0xf8, 0xa8, 0x33, 0x12, 0xa9, 0x1b, 0x1a, 0xc5, 0xe8, 0xef, 0xb1,
123 0xe3, 0x75, 0x0b, 0xd2, 0x28, 0x49, 0x48, 0x64, 0x9c, 0x3d, 0x89, 0xb6,
124 0xf8, 0xa5, 0x93, 0xd0, 0x29, 0x8c, 0x9e, 0x7a, 0xf6, 0x00, 0x20, 0x08,
125 0x52, 0x08, 0xdc, 0x1d, 0x17, 0x8b, 0x44, 0x4e, 0x32, 0x13, 0xdc, 0xb1,
126 0x50, 0xa5, 0xf0, 0x94, 0x25, 0x50, 0xfa, 0x1e, 0xe3, 0xae, 0x66, 0x19,
127 0x13, 0x3e, 0x20, 0x86, 0x05, 0x9c, 0xda, 0xd9, 0xff, 0x8f, 0x72, 0x6e,
128 0xf5, 0xc1, 0xfd, 0x86, 0xae, 0x26, 0xcc, 0x4b, 0xc8, 0x9f, 0xa9, 0xd6,
129 0x3a, 0x1f, 0xb6, 0x8a, 0x8f, 0x04, 0x2d, 0xbb, 0xa4, 0x47, 0xb3, 0xfb,
130 0xf9, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x0d, 0x30, 0x0b, 0x30, 0x09,
131 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0d, 0x06,
132 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
133 0x03, 0x82, 0x01, 0x01, 0x00, 0xa7, 0x35, 0x63, 0xc8, 0x0a, 0xf7, 0xae,
134 0x0f, 0xe8, 0x10, 0x50, 0xb0, 0x79, 0x04, 0xa5, 0xf3, 0x48, 0x5d, 0x0a,
135 0x7a, 0x6b, 0xd0, 0xef, 0x17, 0x43, 0xe5, 0x21, 0xd4, 0xbb, 0xef, 0xac,
136 0x04, 0x21, 0x50, 0x7d, 0xd8, 0xe8, 0xf1, 0x3f, 0xd1, 0xb7, 0xa5, 0x93,
137 0xb4, 0xa8, 0xec, 0x23, 0xbe, 0xb6, 0xd9, 0xcc, 0xbe, 0x3c, 0x81, 0x34,
138 0x25, 0x24, 0x81, 0x1e, 0xd9, 0x8c, 0xd6, 0x20, 0x14, 0x36, 0x83, 0x29,
139 0x7f, 0x56, 0xcd, 0xc3, 0x90, 0xd0, 0x2d, 0x54, 0x8b, 0x05, 0xcb, 0xab,
140 0xb1, 0xf2, 0x44, 0xfc, 0xba, 0x73, 0xbf, 0x97, 0xc2, 0x2b, 0x5a, 0x6a,
141 0x49, 0x27, 0x29, 0x7c, 0xb7, 0xb1, 0x4a, 0x1f, 0x28, 0x41, 0x05, 0x63,
142 0x58, 0x8e, 0xd5, 0x7e, 0x46, 0x74, 0x11, 0x01, 0x72, 0x93, 0x1f, 0xea,
143 0xf7, 0x37, 0x4a, 0xfa, 0x84, 0x53, 0xb6, 0x3c, 0x0e, 0xde, 0xe5, 0x1c,
144 0x12, 0x86, 0x0a, 0xf6, 0x8b, 0xac, 0xc8, 0xb5, 0x9a, 0x9b, 0xd2, 0x28,
145 0x15, 0x18, 0x83, 0x0a, 0xfc, 0x47, 0x1a, 0xcf, 0xed, 0xa1, 0x95, 0x4e,
146 0xcc, 0x3c, 0x2a, 0x9a, 0xdf, 0x09, 0xec, 0x28, 0x20, 0xfd, 0xc5, 0x42,
147 0xf1, 0xd8, 0x2f, 0x21, 0x88, 0xec, 0xe2, 0x24, 0xb2, 0xe2, 0x45, 0x5a,
148 0xce, 0xb4, 0x78, 0xb3, 0x30, 0x38, 0x0e, 0x1c, 0x6a, 0xa3, 0x04, 0x0e,
149 0xac, 0xa4, 0x97, 0xe3, 0xc1, 0x46, 0x0a, 0x9d, 0x65, 0x9a, 0xe9, 0x02,
150 0x12, 0xf0, 0x88, 0x58, 0xc6, 0xde, 0xe5, 0x23, 0x42, 0x3c, 0x58, 0x52,
151 0x27, 0x1a, 0xe2, 0xf5, 0x4a, 0x21, 0x47, 0xb2, 0x13, 0x0c, 0xb2, 0xd0,
152 0xcc, 0xb3, 0xfd, 0x66, 0x2a, 0xa6, 0x38, 0x5b, 0xe6, 0x2e, 0x90, 0x9e,
153 0x62, 0x3f, 0x7e, 0x60, 0xee, 0xd4, 0x02, 0x58, 0x7d, 0x5c, 0xf8, 0x39,
154 0x27, 0xa9, 0xdb, 0x3e, 0x24, 0x3c, 0xc0, 0xde, 0xc8
155 };
156
157
158 #define SERVER "127.0.0.1"
159 //#define SERVER "17.201.58.114"
160 #define PORT 23232
161 #define BUFLEN 128
162 #define COUNT 10
163
164 #if 0
165 static void dumppacket(const unsigned char *data, unsigned long len)
166 {
167 unsigned long i;
168 for(i=0;i<len;i++)
169 {
170 if((i&0xf)==0) printf("%04lx :",i);
171 printf(" %02x", data[i]);
172 if((i&0xf)==0xf) printf("\n");
173 }
174 printf("\n");
175 }
176 #endif
177
178 /* 2K should be enough for everybody */
179 #define MTU 2048
180 static unsigned char readBuffer[MTU];
181 static unsigned int readOff=0;
182 static size_t readLeft=0;
183
184 static
185 OSStatus SocketRead(
186 SSLConnectionRef connection,
187 void *data,
188 size_t *dataLength)
189 {
190 int fd = (int)connection;
191 ssize_t len;
192 uint8_t *d=readBuffer;
193
194 if(readLeft==0)
195 {
196 len = read(fd, readBuffer, MTU);
197
198 if(len>0) {
199 readOff=0;
200 readLeft=(size_t) len;
201 printf("SocketRead: %ld bytes... epoch: %02x seq=%02x%02x\n",
202 len, d[4], d[9], d[10]);
203
204 } else {
205 int theErr = errno;
206 switch(theErr) {
207 case EAGAIN:
208 //printf("SocketRead: EAGAIN\n");
209 *dataLength=0;
210 /* nonblocking, no data */
211 return errSSLWouldBlock;
212 default:
213 perror("SocketRead");
214 return errSecIO;
215 }
216 }
217 }
218
219 if(readLeft<*dataLength) {
220 *dataLength=readLeft;
221 }
222
223 memcpy(data, readBuffer+readOff, *dataLength);
224 readLeft-=*dataLength;
225 readOff+=*dataLength;
226
227 return errSecSuccess;
228
229 }
230
231 static
232 OSStatus SocketWrite(
233 SSLConnectionRef connection,
234 const void *data,
235 size_t *dataLength) /* IN/OUT */
236 {
237 int fd = (int)connection;
238 ssize_t len;
239 OSStatus err = errSecSuccess;
240 const uint8_t *d=data;
241
242 #if 0
243 if((rand()&3)==1) {
244
245 /* drop 1/8th packets */
246 printf("SocketWrite: Drop %ld bytes... epoch: %02x seq=%02x%02x\n",
247 *dataLength, d[4], d[9], d[10]);
248 return errSecSuccess;
249
250 }
251 #endif
252
253 len = send(fd, data, *dataLength, 0);
254
255 if(len>0) {
256 *dataLength=(size_t)len;
257 printf("SocketWrite: Sent %ld bytes... epoch: %02x seq=%02x%02x\n",
258 len, d[4], d[9], d[10]);
259 return err;
260 }
261
262 int theErr = errno;
263 switch(theErr) {
264 case EAGAIN:
265 /* nonblocking, no data */
266 err = errSSLWouldBlock;
267 break;
268 default:
269 perror("SocketWrite");
270 err = errSecIO;
271 break;
272 }
273
274 return err;
275
276 }
277
278
279 int main(int argc, char **argv)
280 {
281 int fd;
282 struct sockaddr_in sa;
283
284 if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
285 perror("socket");
286 exit(-1);
287 }
288
289 #ifdef NO_SERVER
290 # if DEBUG
291 securityd_init();
292 # endif
293 #endif
294
295 memset((char *) &sa, 0, sizeof(sa));
296 sa.sin_family = AF_INET;
297 sa.sin_port = htons(PORT);
298 if (inet_aton(SERVER, &sa.sin_addr)==0) {
299 fprintf(stderr, "inet_aton() failed\n");
300 exit(1);
301 }
302
303 time_t seed=time(NULL);
304 // time_t seed=1298952499;
305 srand((unsigned)seed);
306 printf("Random drop initialized with seed = %lu\n", seed);
307
308 if(connect(fd, (struct sockaddr *)&sa, sizeof(sa))==-1)
309 {
310 perror("connect");
311 return errno;
312 }
313
314 /* Change to non blocking io */
315 fcntl(fd, F_SETFL, O_NONBLOCK);
316
317 SSLConnectionRef c=(SSLConnectionRef)(intptr_t)fd;
318
319
320 OSStatus ortn;
321 SSLContextRef ctx = NULL;
322
323 #pragma clang diagnostic push
324 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
325
326 SSLClientCertificateState certState;
327 SSLCipherSuite negCipher;
328 SSLProtocol negVersion;
329
330 /*
331 * Set up a SecureTransport session.
332 */
333 ortn = SSLNewDatagramContext(false, &ctx);
334 if(ortn) {
335 printSslErrStr("SSLNewDatagramContext", ortn);
336 return ortn;
337 }
338 ortn = SSLSetIOFuncs(ctx, SocketRead, SocketWrite);
339 if(ortn) {
340 printSslErrStr("SSLSetIOFuncs", ortn);
341 return ortn;
342 }
343
344 ortn = SSLSetConnection(ctx, c);
345 if(ortn) {
346 printSslErrStr("SSLSetConnection", ortn);
347 return ortn;
348 }
349
350 ortn = SSLSetMaxDatagramRecordSize(ctx, 400);
351 if(ortn) {
352 printSslErrStr("SSLSetMaxDatagramRecordSize", ortn);
353 return ortn;
354 }
355
356 /* Lets not verify the cert, which is a random test cert */
357 ortn = SSLSetEnableCertVerify(ctx, false);
358 if(ortn) {
359 printSslErrStr("SSLSetEnableCertVerify", ortn);
360 return ortn;
361 }
362
363 ortn = SSLSetCertificate(ctx, chain_from_der(false, ServerRSA_Key_der, sizeof(ServerRSA_Key_der),
364 ServerRSA_Cert_CA_RSA_der, sizeof(ServerRSA_Cert_CA_RSA_der)));
365 if(ortn) {
366 printSslErrStr("SSLSetCertificate", ortn);
367 return ortn;
368 }
369
370 do {
371 ortn = SSLHandshake(ctx);
372 if(ortn == errSSLWouldBlock) {
373 /* keep UI responsive */
374 sslOutputDot();
375 }
376 } while (ortn == errSSLWouldBlock);
377
378
379 SSLGetClientCertificateState(ctx, &certState);
380 SSLGetNegotiatedCipher(ctx, &negCipher);
381 SSLGetNegotiatedProtocolVersion(ctx, &negVersion);
382
383 int count;
384 size_t len, readLen, writeLen;
385 char buffer[BUFLEN];
386
387 count = 0;
388 while(count<COUNT) {
389 int timeout = 10000;
390
391 snprintf(buffer, BUFLEN, "Message %d", count);
392 len = strlen(buffer);
393
394 ortn=SSLWrite(ctx, buffer, len, &writeLen);
395 if(ortn) {
396 printSslErrStr("SSLWrite", ortn);
397 break;
398 }
399 printf("Wrote %lu bytes\n", writeLen);
400
401 count++;
402
403 do {
404 ortn=SSLRead(ctx, buffer, BUFLEN, &readLen);
405 } while((ortn==errSSLWouldBlock) && (timeout--));
406 if(ortn==errSSLWouldBlock) {
407 printf("Echo timeout...\n");
408 continue;
409 }
410 if(ortn) {
411 printSslErrStr("SSLRead", ortn);
412 break;
413 }
414 buffer[readLen]=0;
415 printf("Received %lu bytes: %s\n", readLen, buffer);
416
417 }
418
419 SSLClose(ctx);
420
421 SSLDisposeContext(ctx);
422
423 #pragma clang diagnostic pop
424
425 return ortn;
426 }