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