Example TLS client with wolfSSL, with cert
Dependencies: EthernetInterface NTPClient SDFileSystem mbed-rtos mbed wolfSSL
client-tls.cpp@2:53d82dd5e556, 2015-07-20 (annotated)
- Committer:
- wolfSSL
- Date:
- Mon Jul 20 08:39:55 2015 +0000
- Revision:
- 2:53d82dd5e556
- Parent:
- 1:29638701a63a
- Child:
- 3:25d42ccf2f12
client-tls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:ea20b58bf112 | 1 | /* client-tcp.c |
wolfSSL | 0:ea20b58bf112 | 2 | * |
wolfSSL | 0:ea20b58bf112 | 3 | * Copyright (C) 2006-2015 wolfSSL Inc. |
wolfSSL | 0:ea20b58bf112 | 4 | * |
wolfSSL | 0:ea20b58bf112 | 5 | * This file is part of wolfSSL. (formerly known as CyaSSL) |
wolfSSL | 0:ea20b58bf112 | 6 | * |
wolfSSL | 0:ea20b58bf112 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:ea20b58bf112 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:ea20b58bf112 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:ea20b58bf112 | 10 | * (at your option) any later version. |
wolfSSL | 0:ea20b58bf112 | 11 | * |
wolfSSL | 0:ea20b58bf112 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:ea20b58bf112 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:ea20b58bf112 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:ea20b58bf112 | 15 | * GNU General Public License for more details. |
wolfSSL | 0:ea20b58bf112 | 16 | * |
wolfSSL | 0:ea20b58bf112 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:ea20b58bf112 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:ea20b58bf112 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
wolfSSL | 0:ea20b58bf112 | 20 | */ |
wolfSSL | 0:ea20b58bf112 | 21 | |
wolfSSL | 0:ea20b58bf112 | 22 | #include "mbed.h" |
wolfSSL | 0:ea20b58bf112 | 23 | #include "EthernetInterface.h" |
wolfSSL | 2:53d82dd5e556 | 24 | #include "SDFileSystem.h" |
wolfSSL | 0:ea20b58bf112 | 25 | #include <stdio.h> |
wolfSSL | 0:ea20b58bf112 | 26 | #include <stdlib.h> |
wolfSSL | 0:ea20b58bf112 | 27 | #include <string.h> |
wolfSSL | 2:53d82dd5e556 | 28 | #include <wolfssl/ssl.h> /* wolfSSL security library */ |
wolfSSL | 2:53d82dd5e556 | 29 | #include <user_settings.h> |
wolfSSL | 0:ea20b58bf112 | 30 | |
wolfSSL | 0:ea20b58bf112 | 31 | #define MAXDATASIZE (1024*4) |
wolfSSL | 0:ea20b58bf112 | 32 | |
wolfSSL | 2:53d82dd5e556 | 33 | #ifndef WOLFSSL_NO_VERIFYSERVER |
wolfSSL | 2:53d82dd5e556 | 34 | SDFileSystem sdCard(PTE3, PTE1, PTE2, PTE4, "sd"); |
wolfSSL | 2:53d82dd5e556 | 35 | const char* cert = "/sd/cert-file.crt"; |
wolfSSL | 2:53d82dd5e556 | 36 | #endif |
wolfSSL | 2:53d82dd5e556 | 37 | |
wolfSSL | 2:53d82dd5e556 | 38 | static int SocketReceive(WOLFSSL* ssl, char *buf, int sz, void *sock) |
wolfSSL | 2:53d82dd5e556 | 39 | { |
wolfSSL | 2:53d82dd5e556 | 40 | return ((TCPSocketConnection *)sock)->receive(buf, sz) ; |
wolfSSL | 2:53d82dd5e556 | 41 | } |
wolfSSL | 2:53d82dd5e556 | 42 | |
wolfSSL | 2:53d82dd5e556 | 43 | static int SocketSend(WOLFSSL* ssl, char *buf, int sz, void *sock) |
wolfSSL | 2:53d82dd5e556 | 44 | { |
wolfSSL | 2:53d82dd5e556 | 45 | return ((TCPSocketConnection *)sock)->send(buf, sz); |
wolfSSL | 2:53d82dd5e556 | 46 | } |
wolfSSL | 2:53d82dd5e556 | 47 | |
wolfSSL | 0:ea20b58bf112 | 48 | static int getline(char *prompt, char *buff, int size) |
wolfSSL | 0:ea20b58bf112 | 49 | { |
wolfSSL | 0:ea20b58bf112 | 50 | int sz ; |
wolfSSL | 0:ea20b58bf112 | 51 | |
wolfSSL | 0:ea20b58bf112 | 52 | printf("%s", prompt) ; |
wolfSSL | 0:ea20b58bf112 | 53 | for(sz = 0 ; (sz < size) && ((*buff = getchar()) != '\r'); sz++, buff++) { |
wolfSSL | 0:ea20b58bf112 | 54 | putchar(*buff) ; |
wolfSSL | 0:ea20b58bf112 | 55 | if(*buff == '\\') { |
wolfSSL | 0:ea20b58bf112 | 56 | if(++sz >= size)break ; |
wolfSSL | 0:ea20b58bf112 | 57 | *buff = getchar() ; |
wolfSSL | 0:ea20b58bf112 | 58 | putchar(*buff) ; |
wolfSSL | 0:ea20b58bf112 | 59 | switch(*buff) { |
wolfSSL | 0:ea20b58bf112 | 60 | case 'n' : |
wolfSSL | 0:ea20b58bf112 | 61 | *buff = '\n' ; |
wolfSSL | 0:ea20b58bf112 | 62 | break ; |
wolfSSL | 0:ea20b58bf112 | 63 | case 'r' : |
wolfSSL | 0:ea20b58bf112 | 64 | *buff = '\r' ; |
wolfSSL | 0:ea20b58bf112 | 65 | break ; |
wolfSSL | 0:ea20b58bf112 | 66 | case 't' : |
wolfSSL | 0:ea20b58bf112 | 67 | *buff = '\t' ; |
wolfSSL | 0:ea20b58bf112 | 68 | break ; |
wolfSSL | 0:ea20b58bf112 | 69 | case '\\': |
wolfSSL | 0:ea20b58bf112 | 70 | *buff = '\\' ; |
wolfSSL | 0:ea20b58bf112 | 71 | break ; |
wolfSSL | 0:ea20b58bf112 | 72 | default: |
wolfSSL | 0:ea20b58bf112 | 73 | buff[1] = buff[0] ; |
wolfSSL | 0:ea20b58bf112 | 74 | buff[0] = '\\' ; |
wolfSSL | 0:ea20b58bf112 | 75 | buff++ ; |
wolfSSL | 0:ea20b58bf112 | 76 | } |
wolfSSL | 0:ea20b58bf112 | 77 | } else if(*buff == '\b') { |
wolfSSL | 0:ea20b58bf112 | 78 | if(sz >= 2) { |
wolfSSL | 0:ea20b58bf112 | 79 | buff-=2 ; |
wolfSSL | 0:ea20b58bf112 | 80 | sz-=2; |
wolfSSL | 0:ea20b58bf112 | 81 | } |
wolfSSL | 0:ea20b58bf112 | 82 | } |
wolfSSL | 0:ea20b58bf112 | 83 | } ; |
wolfSSL | 0:ea20b58bf112 | 84 | putchar('\n') ; |
wolfSSL | 0:ea20b58bf112 | 85 | *buff = '\0' ; |
wolfSSL | 0:ea20b58bf112 | 86 | return sz ; |
wolfSSL | 0:ea20b58bf112 | 87 | } |
wolfSSL | 0:ea20b58bf112 | 88 | |
wolfSSL | 0:ea20b58bf112 | 89 | /* |
wolfSSL | 0:ea20b58bf112 | 90 | * clients initial contact with server. Socket to connect to: sock |
wolfSSL | 0:ea20b58bf112 | 91 | */ |
wolfSSL | 2:53d82dd5e556 | 92 | int ClientGreet(TCPSocketConnection *socket, WOLFSSL *ssl) |
wolfSSL | 0:ea20b58bf112 | 93 | { |
wolfSSL | 0:ea20b58bf112 | 94 | /* data to send to the server, data recieved from the server */ |
wolfSSL | 0:ea20b58bf112 | 95 | char sendBuff[MAXDATASIZE], rcvBuff[MAXDATASIZE] = {0}; |
wolfSSL | 0:ea20b58bf112 | 96 | int ret ; |
wolfSSL | 0:ea20b58bf112 | 97 | |
wolfSSL | 0:ea20b58bf112 | 98 | ret = getline("Message for server: ", sendBuff, MAXDATASIZE); |
wolfSSL | 0:ea20b58bf112 | 99 | printf("Send[%d]:\n%s\n", ret, sendBuff) ; |
wolfSSL | 2:53d82dd5e556 | 100 | if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) < 0) { |
wolfSSL | 2:53d82dd5e556 | 101 | /* the message is not able to send, or error trying */ |
wolfSSL | 2:53d82dd5e556 | 102 | ret = wolfSSL_get_error(ssl, 0); |
wolfSSL | 2:53d82dd5e556 | 103 | printf("Write error: Error: %i\n", ret); |
wolfSSL | 0:ea20b58bf112 | 104 | return EXIT_FAILURE; |
wolfSSL | 0:ea20b58bf112 | 105 | } |
wolfSSL | 0:ea20b58bf112 | 106 | printf("Recieved:\n"); |
wolfSSL | 0:ea20b58bf112 | 107 | while(1) { |
wolfSSL | 2:53d82dd5e556 | 108 | if ((ret = wolfSSL_read(ssl, rcvBuff, sizeof(rcvBuff))) < 0) { |
wolfSSL | 0:ea20b58bf112 | 109 | if(ret == 0)break ; |
wolfSSL | 2:53d82dd5e556 | 110 | /* the server failed to send data, or error trying */ |
wolfSSL | 2:53d82dd5e556 | 111 | ret = wolfSSL_get_error(ssl, 0); |
wolfSSL | 0:ea20b58bf112 | 112 | printf("Read error. Error: %i\n", ret); |
wolfSSL | 0:ea20b58bf112 | 113 | return EXIT_FAILURE; |
wolfSSL | 0:ea20b58bf112 | 114 | } |
wolfSSL | 0:ea20b58bf112 | 115 | rcvBuff[ret] = '\0' ; |
wolfSSL | 0:ea20b58bf112 | 116 | printf("%s", rcvBuff); |
wolfSSL | 0:ea20b58bf112 | 117 | if((rcvBuff[ret-3] == '\n')&& |
wolfSSL | 0:ea20b58bf112 | 118 | (rcvBuff[ret-2] == '\n')&& |
wolfSSL | 0:ea20b58bf112 | 119 | (rcvBuff[ret-1] == '\n'))break ; |
wolfSSL | 0:ea20b58bf112 | 120 | } |
wolfSSL | 0:ea20b58bf112 | 121 | return ret; |
wolfSSL | 0:ea20b58bf112 | 122 | } |
wolfSSL | 0:ea20b58bf112 | 123 | |
wolfSSL | 2:53d82dd5e556 | 124 | |
wolfSSL | 2:53d82dd5e556 | 125 | /* |
wolfSSL | 2:53d82dd5e556 | 126 | * applies TLS 1.2 security layer to data being sent. |
wolfSSL | 2:53d82dd5e556 | 127 | */ |
wolfSSL | 2:53d82dd5e556 | 128 | int Security(TCPSocketConnection *socket) |
wolfSSL | 2:53d82dd5e556 | 129 | { |
wolfSSL | 2:53d82dd5e556 | 130 | WOLFSSL_CTX* ctx; |
wolfSSL | 2:53d82dd5e556 | 131 | WOLFSSL* ssl; /* create WOLFSSL object */ |
wolfSSL | 2:53d82dd5e556 | 132 | int ret = 0; |
wolfSSL | 2:53d82dd5e556 | 133 | |
wolfSSL | 2:53d82dd5e556 | 134 | /* create and initiLize WOLFSSL_CTX structure */ |
wolfSSL | 2:53d82dd5e556 | 135 | if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { |
wolfSSL | 2:53d82dd5e556 | 136 | printf("SSL_CTX_new error.\n"); |
wolfSSL | 2:53d82dd5e556 | 137 | return EXIT_FAILURE; |
wolfSSL | 2:53d82dd5e556 | 138 | } |
wolfSSL | 2:53d82dd5e556 | 139 | |
wolfSSL | 2:53d82dd5e556 | 140 | wolfSSL_SetIORecv(ctx, SocketReceive) ; |
wolfSSL | 2:53d82dd5e556 | 141 | wolfSSL_SetIOSend(ctx, SocketSend) ; |
wolfSSL | 2:53d82dd5e556 | 142 | |
wolfSSL | 2:53d82dd5e556 | 143 | wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0); |
wolfSSL | 2:53d82dd5e556 | 144 | |
wolfSSL | 2:53d82dd5e556 | 145 | if ((ssl = wolfSSL_new(ctx)) == NULL) { |
wolfSSL | 2:53d82dd5e556 | 146 | printf("wolfSSL_new error.\n"); |
wolfSSL | 2:53d82dd5e556 | 147 | return EXIT_FAILURE; |
wolfSSL | 2:53d82dd5e556 | 148 | } |
wolfSSL | 2:53d82dd5e556 | 149 | |
wolfSSL | 2:53d82dd5e556 | 150 | wolfSSL_SetIOReadCtx(ssl, (void *)socket) ; |
wolfSSL | 2:53d82dd5e556 | 151 | wolfSSL_SetIOWriteCtx(ssl, (void *)socket) ; |
wolfSSL | 2:53d82dd5e556 | 152 | |
wolfSSL | 2:53d82dd5e556 | 153 | ret = wolfSSL_connect(ssl); |
wolfSSL | 2:53d82dd5e556 | 154 | if (ret == SSL_SUCCESS) { |
wolfSSL | 2:53d82dd5e556 | 155 | printf("TLS Connected\n") ; |
wolfSSL | 2:53d82dd5e556 | 156 | ret = ClientGreet(socket, ssl); |
wolfSSL | 2:53d82dd5e556 | 157 | } else { |
wolfSSL | 2:53d82dd5e556 | 158 | ret = wolfSSL_get_error(ssl, 0); |
wolfSSL | 2:53d82dd5e556 | 159 | printf("TLS Connect error. Error: %i\n", ret); |
wolfSSL | 2:53d82dd5e556 | 160 | } |
wolfSSL | 2:53d82dd5e556 | 161 | /* frees all data before client termination */ |
wolfSSL | 2:53d82dd5e556 | 162 | wolfSSL_free(ssl); |
wolfSSL | 2:53d82dd5e556 | 163 | wolfSSL_CTX_free(ctx); |
wolfSSL | 2:53d82dd5e556 | 164 | wolfSSL_Cleanup(); |
wolfSSL | 2:53d82dd5e556 | 165 | |
wolfSSL | 2:53d82dd5e556 | 166 | return ret; |
wolfSSL | 2:53d82dd5e556 | 167 | } |
wolfSSL | 2:53d82dd5e556 | 168 | |
wolfSSL | 0:ea20b58bf112 | 169 | /* |
wolfSSL | 0:ea20b58bf112 | 170 | * command line argumentCount and argumentValues |
wolfSSL | 0:ea20b58bf112 | 171 | */ |
wolfSSL | 1:29638701a63a | 172 | void net_main(const void *av) |
wolfSSL | 0:ea20b58bf112 | 173 | { |
wolfSSL | 1:29638701a63a | 174 | char server_addr[40] ; |
wolfSSL | 1:29638701a63a | 175 | char server_port[10] ; |
wolfSSL | 1:29638701a63a | 176 | |
wolfSSL | 0:ea20b58bf112 | 177 | EthernetInterface eth; |
wolfSSL | 0:ea20b58bf112 | 178 | TCPSocketConnection socket; |
wolfSSL | 0:ea20b58bf112 | 179 | |
wolfSSL | 2:53d82dd5e556 | 180 | wolfSSL_Init(); /* initialize wolfSSL */ |
wolfSSL | 0:ea20b58bf112 | 181 | eth.init(); //Use DHCP |
wolfSSL | 0:ea20b58bf112 | 182 | eth.connect(); |
wolfSSL | 0:ea20b58bf112 | 183 | printf("Client Addr: %s\n", eth.getIPAddress()); |
wolfSSL | 0:ea20b58bf112 | 184 | |
wolfSSL | 0:ea20b58bf112 | 185 | getline("Server Addr: ", server_addr, sizeof(server_addr)) ; |
wolfSSL | 0:ea20b58bf112 | 186 | getline("Server Port: ", server_port, sizeof(server_port)) ; |
wolfSSL | 0:ea20b58bf112 | 187 | |
wolfSSL | 0:ea20b58bf112 | 188 | while (socket.connect(server_addr, atoi(server_port)) < 0) { |
wolfSSL | 0:ea20b58bf112 | 189 | printf("Unable to connect to (%s) on port (%d)\n", server_addr, server_port); |
wolfSSL | 0:ea20b58bf112 | 190 | wait(1.0); |
wolfSSL | 0:ea20b58bf112 | 191 | } |
wolfSSL | 0:ea20b58bf112 | 192 | printf("TCP Connected\n") ; |
wolfSSL | 0:ea20b58bf112 | 193 | |
wolfSSL | 2:53d82dd5e556 | 194 | Security(&socket); |
wolfSSL | 1:29638701a63a | 195 | return ; |
wolfSSL | 1:29638701a63a | 196 | } |
wolfSSL | 1:29638701a63a | 197 | |
wolfSSL | 1:29638701a63a | 198 | int main(void) |
wolfSSL | 1:29638701a63a | 199 | { |
wolfSSL | 1:29638701a63a | 200 | #define STACK_SIZE 24000 |
wolfSSL | 1:29638701a63a | 201 | Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE); |
wolfSSL | 1:29638701a63a | 202 | while(1)wait(1.0) ; |
wolfSSL | 0:ea20b58bf112 | 203 | } |