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