ssl_access (http->https)

Dependencies:   CyaSSL EthernetInterface mbed-rtos mbed

Fork of ssl_access by shinichi satoh

Committer:
wolfSSL
Date:
Sun May 25 06:47:15 2014 +0000
Revision:
0:0c584b87ea42
Child:
1:ac91b4f8d818
Publish version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:0c584b87ea42 1 #include "mbed.h"
wolfSSL 0:0c584b87ea42 2 #include "EthernetInterface.h"
wolfSSL 0:0c584b87ea42 3
wolfSSL 0:0c584b87ea42 4 #include <cyassl/ssl.h>
wolfSSL 0:0c584b87ea42 5
wolfSSL 0:0c584b87ea42 6 const char* ECHO_SERVER_ADDRESS = "192.168.137.1";
wolfSSL 0:0c584b87ea42 7 const int ECHO_SERVER_PORT = 443;
wolfSSL 0:0c584b87ea42 8
wolfSSL 0:0c584b87ea42 9 #define err_sys(m) { puts(m) ; return -1 ; }
wolfSSL 0:0c584b87ea42 10
wolfSSL 0:0c584b87ea42 11 TCPSocketConnection socket;
wolfSSL 0:0c584b87ea42 12
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 0:0c584b87ea42 36
wolfSSL 0:0c584b87ea42 37 int main()
wolfSSL 0:0c584b87ea42 38 {
wolfSSL 0:0c584b87ea42 39 CYASSL_CTX* ctx = 0;
wolfSSL 0:0c584b87ea42 40 CYASSL* ssl = 0;
wolfSSL 0:0c584b87ea42 41
wolfSSL 0:0c584b87ea42 42 CYASSL_METHOD* method = CyaTLSv1_2_client_method();
wolfSSL 0:0c584b87ea42 43 EthernetInterface eth;
wolfSSL 0:0c584b87ea42 44
wolfSSL 0:0c584b87ea42 45 printf("===== ECHO CLIENT ========\n") ;
wolfSSL 0:0c584b87ea42 46 /* CyaSSL_Debugging_ON() ; */
wolfSSL 0:0c584b87ea42 47
wolfSSL 0:0c584b87ea42 48 eth.init(); //Use DHCP
wolfSSL 0:0c584b87ea42 49 eth.connect();
wolfSSL 0:0c584b87ea42 50 printf("Client IP: %s\n", eth.getIPAddress());
wolfSSL 0:0c584b87ea42 51
wolfSSL 0:0c584b87ea42 52 /* Initialize CyaSSL Context */
wolfSSL 0:0c584b87ea42 53 ctx = CyaSSL_CTX_new(method);
wolfSSL 0:0c584b87ea42 54 if (ctx == NULL)
wolfSSL 0:0c584b87ea42 55 err_sys("unable to get ctx");
wolfSSL 0:0c584b87ea42 56 CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
wolfSSL 0:0c584b87ea42 57 CyaSSL_SetIORecv(ctx, SocketReceive) ;
wolfSSL 0:0c584b87ea42 58 CyaSSL_SetIOSend(ctx, SocketSend) ;
wolfSSL 0:0c584b87ea42 59
wolfSSL 0:0c584b87ea42 60 socket.set_blocking(false, 300) ;
wolfSSL 0:0c584b87ea42 61 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
wolfSSL 0:0c584b87ea42 62 printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
wolfSSL 0:0c584b87ea42 63 wait(1);
wolfSSL 0:0c584b87ea42 64 }
wolfSSL 0:0c584b87ea42 65 printf("TCP Connected\n") ;
wolfSSL 0:0c584b87ea42 66
wolfSSL 0:0c584b87ea42 67 ssl = CyaSSL_new(ctx);
wolfSSL 0:0c584b87ea42 68 if (ssl == NULL)
wolfSSL 0:0c584b87ea42 69 err_sys("unable to get SSL object");
wolfSSL 0:0c584b87ea42 70 if (CyaSSL_connect(ssl) != SSL_SUCCESS) {
wolfSSL 0:0c584b87ea42 71 int err = CyaSSL_get_error(ssl, 0);
wolfSSL 0:0c584b87ea42 72 printf("err = %d, %s\n", err,
wolfSSL 0:0c584b87ea42 73 CyaSSL_ERR_error_string(err, "\n" /*buffer*/));
wolfSSL 0:0c584b87ea42 74 err_sys("SSL Connection Error");
wolfSSL 0:0c584b87ea42 75 }
wolfSSL 0:0c584b87ea42 76 printf("SSL Connected\n") ;
wolfSSL 0:0c584b87ea42 77
wolfSSL 0:0c584b87ea42 78 const char msg[] = "GET / HTTP/1.0\r\n\r\n" ;
wolfSSL 0:0c584b87ea42 79 // const char msg[] = "Hello World\r\n" ;
wolfSSL 0:0c584b87ea42 80
wolfSSL 0:0c584b87ea42 81 if (CyaSSL_write(ssl, msg, sizeof(msg)-1) != (sizeof(msg)-1))
wolfSSL 0:0c584b87ea42 82 err_sys("CyaSSL_write failed");
wolfSSL 0:0c584b87ea42 83
wolfSSL 0:0c584b87ea42 84 char buf[1024];
wolfSSL 0:0c584b87ea42 85 int n ;
wolfSSL 0:0c584b87ea42 86
wolfSSL 0:0c584b87ea42 87 n = CyaSSL_read(ssl, buf, sizeof(buf)-1);
wolfSSL 0:0c584b87ea42 88 if (n > 0) {
wolfSSL 0:0c584b87ea42 89 puts("Server Response") ;
wolfSSL 0:0c584b87ea42 90 buf[n] = 0;
wolfSSL 0:0c584b87ea42 91 puts(buf);
wolfSSL 0:0c584b87ea42 92 puts("=== === === ===") ;
wolfSSL 0:0c584b87ea42 93 } else
wolfSSL 0:0c584b87ea42 94 err_sys("CyaSSL_read failed");
wolfSSL 0:0c584b87ea42 95
wolfSSL 0:0c584b87ea42 96 CyaSSL_free(ssl) ;
wolfSSL 0:0c584b87ea42 97 socket.close();
wolfSSL 0:0c584b87ea42 98
wolfSSL 0:0c584b87ea42 99 CyaSSL_CTX_free(ctx) ;
wolfSSL 0:0c584b87ea42 100 eth.disconnect();
wolfSSL 0:0c584b87ea42 101 while(true) {}
wolfSSL 0:0c584b87ea42 102 }