ssl_access (http->https)
Dependencies: CyaSSL EthernetInterface mbed-rtos mbed
Fork of ssl_access by
main.cpp@6:498ff8506b32, 2015-07-22 (annotated)
- Committer:
- thursday1024
- Date:
- Wed Jul 22 08:19:33 2015 +0000
- Revision:
- 6:498ff8506b32
- Parent:
- 5:962734db89e5
ssl access
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thursday1024 | 6:498ff8506b32 | 1 | // ssl_access main.cpp |
wolfSSL | 0:0c584b87ea42 | 2 | #include "mbed.h" |
wolfSSL | 0:0c584b87ea42 | 3 | #include "EthernetInterface.h" |
wolfSSL | 0:0c584b87ea42 | 4 | |
wolfSSL | 1:ac91b4f8d818 | 5 | /*** SSL #include <cyassl/ssl.h> ***/ |
wolfSSL | 0:0c584b87ea42 | 6 | |
wolfSSL | 2:29a1370416cb | 7 | #define PORT 80 /*** SSL 443 ***/ |
wolfSSL | 0:0c584b87ea42 | 8 | |
wolfSSL | 2:29a1370416cb | 9 | #define err_sys(m) puts(m) |
wolfSSL | 0:0c584b87ea42 | 10 | |
wolfSSL | 0:0c584b87ea42 | 11 | TCPSocketConnection socket; |
wolfSSL | 0:0c584b87ea42 | 12 | |
wolfSSL | 1:ac91b4f8d818 | 13 | /*** SSL |
wolfSSL | 0:0c584b87ea42 | 14 | static int SocketReceive(CYASSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 0:0c584b87ea42 | 15 | { |
wolfSSL | 0:0c584b87ea42 | 16 | int n ; |
wolfSSL | 0:0c584b87ea42 | 17 | int i ; |
wolfSSL | 0:0c584b87ea42 | 18 | #define RECV_RETRY 3 |
wolfSSL | 0:0c584b87ea42 | 19 | for(i=0; i<RECV_RETRY; i++) { |
wolfSSL | 0:0c584b87ea42 | 20 | n = socket.receive(buf, sz) ; |
wolfSSL | 0:0c584b87ea42 | 21 | if(n >= 0)return n ; |
wolfSSL | 0:0c584b87ea42 | 22 | } |
wolfSSL | 0:0c584b87ea42 | 23 | printf("SocketReceive:%d/%d\n", n, sz) ; |
wolfSSL | 0:0c584b87ea42 | 24 | return n ; |
wolfSSL | 0:0c584b87ea42 | 25 | } |
wolfSSL | 0:0c584b87ea42 | 26 | |
wolfSSL | 0:0c584b87ea42 | 27 | static int SocketSend(CYASSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 0:0c584b87ea42 | 28 | { |
wolfSSL | 0:0c584b87ea42 | 29 | int n ; |
wolfSSL | 0:0c584b87ea42 | 30 | |
wolfSSL | 0:0c584b87ea42 | 31 | n = socket.send(buf, sz); |
wolfSSL | 0:0c584b87ea42 | 32 | if(n > 0) { |
wolfSSL | 0:0c584b87ea42 | 33 | return n ; |
wolfSSL | 0:0c584b87ea42 | 34 | } else printf("SocketSend:%d/%d\n", n, sz); |
wolfSSL | 0:0c584b87ea42 | 35 | return n ; |
wolfSSL | 0:0c584b87ea42 | 36 | } |
wolfSSL | 2:29a1370416cb | 37 | ***/ |
wolfSSL | 0:0c584b87ea42 | 38 | |
wolfSSL | 1:ac91b4f8d818 | 39 | EthernetInterface eth; |
wolfSSL | 1:ac91b4f8d818 | 40 | |
wolfSSL | 5:962734db89e5 | 41 | void net_main(void const *av) |
wolfSSL | 0:0c584b87ea42 | 42 | { |
wolfSSL | 1:ac91b4f8d818 | 43 | char server_ip[20] ; |
wolfSSL | 2:29a1370416cb | 44 | |
wolfSSL | 2:29a1370416cb | 45 | eth.init(); //Use DHCP |
wolfSSL | 2:29a1370416cb | 46 | printf("===== Simple TCP Client ========\n") ; |
wolfSSL | 2:29a1370416cb | 47 | /*** SSL |
wolfSSL | 2:29a1370416cb | 48 | printf("===== Simple SSL Client ========\n") ; |
wolfSSL | 2:29a1370416cb | 49 | ***/ |
wolfSSL | 2:29a1370416cb | 50 | |
wolfSSL | 2:29a1370416cb | 51 | while(1) { |
wolfSSL | 2:29a1370416cb | 52 | if(eth.connect()== 0)break ; |
wolfSSL | 2:29a1370416cb | 53 | wait(0.1); |
wolfSSL | 2:29a1370416cb | 54 | } |
wolfSSL | 2:29a1370416cb | 55 | printf("Client IP: %s\n", eth.getIPAddress()); |
wolfSSL | 2:29a1370416cb | 56 | |
wolfSSL | 1:ac91b4f8d818 | 57 | /*** SSL |
wolfSSL | 0:0c584b87ea42 | 58 | CYASSL_CTX* ctx = 0; |
wolfSSL | 0:0c584b87ea42 | 59 | CYASSL* ssl = 0; |
wolfSSL | 0:0c584b87ea42 | 60 | |
wolfSSL | 0:0c584b87ea42 | 61 | CYASSL_METHOD* method = CyaTLSv1_2_client_method(); |
wolfSSL | 2:29a1370416cb | 62 | ***/ |
wolfSSL | 0:0c584b87ea42 | 63 | |
wolfSSL | 0:0c584b87ea42 | 64 | /* Initialize CyaSSL Context */ |
wolfSSL | 1:ac91b4f8d818 | 65 | /*** SSL |
wolfSSL | 0:0c584b87ea42 | 66 | ctx = CyaSSL_CTX_new(method); |
wolfSSL | 0:0c584b87ea42 | 67 | if (ctx == NULL) |
wolfSSL | 0:0c584b87ea42 | 68 | err_sys("unable to get ctx"); |
wolfSSL | 0:0c584b87ea42 | 69 | CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0); |
wolfSSL | 0:0c584b87ea42 | 70 | CyaSSL_SetIORecv(ctx, SocketReceive) ; |
wolfSSL | 0:0c584b87ea42 | 71 | CyaSSL_SetIOSend(ctx, SocketSend) ; |
wolfSSL | 1:ac91b4f8d818 | 72 | end SSL ***/ |
wolfSSL | 0:0c584b87ea42 | 73 | |
wolfSSL | 0:0c584b87ea42 | 74 | socket.set_blocking(false, 300) ; |
wolfSSL | 1:ac91b4f8d818 | 75 | printf("Server IP: ") ; |
wolfSSL | 2:29a1370416cb | 76 | for(int i=0; i<sizeof(server_ip); i++) { |
wolfSSL | 2:29a1370416cb | 77 | if((server_ip[i] = getchar()) == '\r') { |
wolfSSL | 2:29a1370416cb | 78 | server_ip[i] = '\0' ; |
wolfSSL | 2:29a1370416cb | 79 | putchar('\n') ; |
wolfSSL | 2:29a1370416cb | 80 | break ; |
wolfSSL | 2:29a1370416cb | 81 | } else putchar(server_ip[i]) ; |
wolfSSL | 2:29a1370416cb | 82 | } |
wolfSSL | 2:29a1370416cb | 83 | |
wolfSSL | 1:ac91b4f8d818 | 84 | while (socket.connect(server_ip, PORT) < 0) { |
wolfSSL | 1:ac91b4f8d818 | 85 | printf("Unable to connect to (%s) on port (%d)\n", server_ip, PORT); |
wolfSSL | 0:0c584b87ea42 | 86 | wait(1); |
wolfSSL | 0:0c584b87ea42 | 87 | } |
wolfSSL | 0:0c584b87ea42 | 88 | printf("TCP Connected\n") ; |
wolfSSL | 0:0c584b87ea42 | 89 | |
wolfSSL | 1:ac91b4f8d818 | 90 | /*** SSL |
wolfSSL | 0:0c584b87ea42 | 91 | ssl = CyaSSL_new(ctx); |
wolfSSL | 0:0c584b87ea42 | 92 | if (ssl == NULL) |
wolfSSL | 0:0c584b87ea42 | 93 | err_sys("unable to get SSL object"); |
wolfSSL | 0:0c584b87ea42 | 94 | if (CyaSSL_connect(ssl) != SSL_SUCCESS) { |
wolfSSL | 0:0c584b87ea42 | 95 | int err = CyaSSL_get_error(ssl, 0); |
wolfSSL | 0:0c584b87ea42 | 96 | printf("err = %d, %s\n", err, |
wolfSSL | 1:ac91b4f8d818 | 97 | CyaSSL_ERR_error_string(err, "\n")); |
wolfSSL | 0:0c584b87ea42 | 98 | err_sys("SSL Connection Error"); |
wolfSSL | 0:0c584b87ea42 | 99 | } |
wolfSSL | 0:0c584b87ea42 | 100 | printf("SSL Connected\n") ; |
wolfSSL | 1:ac91b4f8d818 | 101 | ***/ |
wolfSSL | 0:0c584b87ea42 | 102 | |
wolfSSL | 5:962734db89e5 | 103 | char msg[] = "GET /congrats.html HTTP/1.0\r\nConnection: Close\r\n\r\n" ; |
wolfSSL | 0:0c584b87ea42 | 104 | // const char msg[] = "Hello World\r\n" ; |
wolfSSL | 0:0c584b87ea42 | 105 | |
wolfSSL | 2:29a1370416cb | 106 | if ( |
wolfSSL | 2:29a1370416cb | 107 | /*** SSL |
wolfSSL | 2:29a1370416cb | 108 | CyaSSL_write(ssl, msg, sizeof(msg)-1) != (sizeof(msg)-1)) ***/ |
wolfSSL | 1:ac91b4f8d818 | 109 | socket.send(msg, sizeof(msg)-1) != (sizeof(msg)-1)) |
wolfSSL | 0:0c584b87ea42 | 110 | err_sys("CyaSSL_write failed"); |
wolfSSL | 0:0c584b87ea42 | 111 | |
wolfSSL | 0:0c584b87ea42 | 112 | char buf[1024]; |
wolfSSL | 0:0c584b87ea42 | 113 | int n ; |
wolfSSL | 1:ac91b4f8d818 | 114 | puts("Server Response:\n") ; |
wolfSSL | 1:ac91b4f8d818 | 115 | do { |
wolfSSL | 2:29a1370416cb | 116 | n = /*** SSL CyaSSL_read(ssl, buf, sizeof(buf)-1); ***/ |
wolfSSL | 1:ac91b4f8d818 | 117 | socket.receive(buf, sizeof(buf)-1); |
wolfSSL | 1:ac91b4f8d818 | 118 | if (n >= 0) { |
wolfSSL | 1:ac91b4f8d818 | 119 | buf[n] = 0; |
wolfSSL | 1:ac91b4f8d818 | 120 | printf("%s", buf); |
wolfSSL | 5:962734db89e5 | 121 | } else break ; |
wolfSSL | 1:ac91b4f8d818 | 122 | } while(n > 0) ; |
wolfSSL | 1:ac91b4f8d818 | 123 | puts("=== === === ===") ; |
wolfSSL | 1:ac91b4f8d818 | 124 | /*** SSL CyaSSL_free(ssl) ; ***/ |
wolfSSL | 1:ac91b4f8d818 | 125 | socket.close(); |
wolfSSL | 1:ac91b4f8d818 | 126 | /*** SSL CyaSSL_CTX_free(ctx) ; ***/ |
wolfSSL | 1:ac91b4f8d818 | 127 | eth.disconnect(); |
wolfSSL | 1:ac91b4f8d818 | 128 | } |
wolfSSL | 5:962734db89e5 | 129 | |
wolfSSL | 5:962734db89e5 | 130 | main() |
wolfSSL | 5:962734db89e5 | 131 | { |
wolfSSL | 5:962734db89e5 | 132 | |
wolfSSL | 5:962734db89e5 | 133 | #define STACK_SIZE 20000 |
wolfSSL | 5:962734db89e5 | 134 | Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE); |
wolfSSL | 5:962734db89e5 | 135 | |
wolfSSL | 5:962734db89e5 | 136 | while (true) { |
wolfSSL | 5:962734db89e5 | 137 | Thread::wait(1000); |
wolfSSL | 5:962734db89e5 | 138 | } |
wolfSSL | 5:962734db89e5 | 139 | } |