X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/c38e3ce98599a410a47dc10253faa4d5830f13b2..427c49bcad63d042b29ada2ac27e3dfc4845c779:/tlsnke/tlsnketest/dtls_client.c diff --git a/tlsnke/tlsnketest/dtls_client.c b/tlsnke/tlsnketest/dtls_client.c new file mode 100644 index 00000000..1dc76b03 --- /dev/null +++ b/tlsnke/tlsnketest/dtls_client.c @@ -0,0 +1,261 @@ +// +// dtls_client.c +// tlsnke +// +// Created by Fabrice Gautier on 2/7/12. +// Copyright (c) 2012 Apple, Inc. All rights reserved. +// + +/* + * dtlsEchoClient.c + * Security + * + * Created by Fabrice Gautier on 1/31/11. + * Copyright 2011 Apple, Inc. All rights reserved. + * + */ + +#include + +#include "ssl-utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include /* close() */ +#include /* memset() */ +#include +#include + +#include "tlssocket.h" + +#define SERVER "10.0.2.1" +#define PORT 23232 +#define BUFLEN 128 +#define COUNT 10 + +#if 0 +static void dumppacket(const unsigned char *data, unsigned long len) +{ + unsigned long i; + for(i=0;i= TIME_INTERVAL) { + printf("."); fflush(stdout); + lastTime = thisTime; + } +} + +static void printSslErrStr( + const char *op, + OSStatus err) +{ + printf("*** %s: %ld\n", op, (long)err); +} + +/* 2K should be enough for everybody */ +#define MTU 2048 + + +int dtls_client(const char *hostname, int bypass); + +int dtls_client(const char *hostname, int bypass) +{ + int fd; + int tlsfd; + struct sockaddr_in sa; + + printf("Running dtls_client test with hostname=%s, bypass=%d\n", hostname, bypass); + + if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1) { + perror("socket"); + exit(-1); + } + + memset((char *) &sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_port = htons(PORT); + if (inet_aton(hostname, &sa.sin_addr)==0) { + fprintf(stderr, "inet_aton() failed\n"); + exit(1); + } + + if(connect(fd, (struct sockaddr *)&sa, sizeof(sa))==-1) + { + perror("connect"); + return errno; + } + + /* Change to non blocking io */ + fcntl(fd, F_SETFL, O_NONBLOCK); + + SSLRecordContextRef c=(intptr_t)fd; + + + OSStatus ortn; + SSLContextRef ctx = NULL; + + SSLClientCertificateState certState; + SSLCipherSuite negCipher; + SSLProtocol negVersion; + + /* + * Set up a SecureTransport session. + */ + + ctx = SSLCreateContextWithRecordFuncs(kCFAllocatorDefault, kSSLClientSide, kSSLDatagramType, &TLSSocket_Funcs); + if(!ctx) { + printSslErrStr("SSLCreateContextWithRecordFuncs", -1); + return -1; + } + + printf("Attaching filter\n"); + ortn = TLSSocket_Attach(fd); + if(ortn) { + printSslErrStr("TLSSocket_Attach", ortn); + return ortn; + } + + if(bypass) { + tlsfd = open("/dev/tlsnke", O_RDWR); + if(tlsfd<0) { + perror("opening tlsnke dev"); + exit(-1); + } + } + + ortn = SSLSetRecordContext(ctx, c); + if(ortn) { + printSslErrStr("SSLSetRecordContext", ortn); + return ortn; + } + + ortn = SSLSetMaxDatagramRecordSize(ctx, 600); + if(ortn) { + printSslErrStr("SSLSetMaxDatagramRecordSize", ortn); + return ortn; + } + + /* Lets not verify the cert, which is a random test cert */ + ortn = SSLSetEnableCertVerify(ctx, false); + if(ortn) { + printSslErrStr("SSLSetEnableCertVerify", ortn); + return ortn; + } + + ortn = SSLSetCertificate(ctx, server_chain()); + if(ortn) { + printSslErrStr("SSLSetCertificate", ortn); + return ortn; + } + + printf("Handshake...\n"); + + do { + ortn = SSLHandshake(ctx); + if(ortn == errSSLWouldBlock) { + /* keep UI responsive */ + sslOutputDot(); + } + } while (ortn == errSSLWouldBlock); + + + SSLGetClientCertificateState(ctx, &certState); + SSLGetNegotiatedCipher(ctx, &negCipher); + SSLGetNegotiatedProtocolVersion(ctx, &negVersion); + + int count; + size_t len; + ssize_t sreadLen, swriteLen; + size_t readLen, writeLen; + + char buffer[BUFLEN]; + + count = 0; + while(count