Subversion Repositories Kolibri OS

Rev

Rev 9082 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  *  SSL client demonstration program
  3.  *
  4.  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5.  *  SPDX-License-Identifier: GPL-2.0
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License along
  18.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  19.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  *  This file is part of mbed TLS (https://tls.mbed.org)
  22.  */
  23. /*#if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif*/
  28.  
  29. #include <sys/ksys.h>
  30.  
  31. //#if defined(MBEDTLS_PLATFORM_C)
  32. //#include "mbedtls/platform.h"
  33. //#else
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #define mbedtls_time            time
  37. #define mbedtls_time_t          time_t
  38. #define mbedtls_fprintf         fprintf
  39. #define mbedtls_printf          printf
  40. #define mbedtls_exit            exit
  41. #define MBEDTLS_EXIT_SUCCESS    0
  42. #define MBEDTLS_EXIT_FAILURE    -1
  43. //#endif /* MBEDTLS_PLATFORM_C */
  44.  
  45. //#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||  \
  46.     !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
  47.     !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) ||         \
  48.     !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
  49.     !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
  50. /*int main( void )
  51. {
  52.     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  53.            "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
  54.            "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  55.            "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
  56.            "not defined.\n");
  57.     return( 0 );
  58. }
  59. //#else*/
  60.  
  61. #include "mbedtls/net_sockets.h"
  62. #include "mbedtls/debug.h"
  63. #include "mbedtls/ssl.h"
  64. #include "mbedtls/entropy.h"
  65. #include "mbedtls/ctr_drbg.h"
  66. #include "mbedtls/error.h"
  67. #include "mbedtls/certs.h"
  68.  
  69. #include <string.h>
  70.  
  71. #define SERVER_NAME_SIZE    255
  72. #define SERVER_PORT_SIZE    16
  73. #define REQUEST_URL_SIZE    STDIO_MAX_MEM-SERVER_NAME_SIZE-32
  74.  
  75. static char server_port[SERVER_PORT_SIZE];
  76. static char server_name[SERVER_NAME_SIZE];
  77. static char request_url[REQUEST_URL_SIZE];
  78. static char get_request[STDIO_MAX_MEM];
  79.  
  80. #define DEBUG_LEVEL 1
  81.  
  82. extern int *_mbedtls_test_cas_pem_len;
  83. extern char* _mbedtls_test_cas_pem;
  84.  
  85. #define mbedtls_test_cas_pem_len *_mbedtls_test_cas_pem_len
  86. #define mbedtls_test_cas_pem _mbedtls_test_cas_pem
  87.  
  88. //gmtime(time_t t){puts("gmtime stub");};
  89.  
  90. //int load_network_obj(){return networklib_init();}
  91.  
  92. static void my_debug( void *ctx, int level,
  93.                       const char *file, int line,
  94.                       const char *str )
  95. {
  96.     ((void) level);
  97.  
  98.     //mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
  99.     //fflush(  (FILE *) ctx  );
  100.     printf("%s:%04d: %s", file, line, str );
  101. }
  102.  
  103. char* safe_gets(char *str, size_t n){
  104.     char* ret = fgets(str, n, stdin);
  105.     if(ret){
  106.          size_t str_len = strlen(str);
  107.          if(str[str_len-1]=='\n'){
  108.             str[str_len-1]= '\0';
  109.          }
  110.          return str;
  111.     }
  112.     return NULL;
  113. }
  114.  
  115. extern int mbedtls_load();
  116. extern int mbedtls_init();
  117.  
  118. int main( void )
  119. {
  120.     if(mbedtls_load()){
  121.         printf("mbedtls.obj not load!\n");
  122.         return -1;
  123.     }
  124.     if(mbedtls_init()){
  125.         puts("mbedtls.obj not init!");
  126.         return -1;
  127.     }
  128.    
  129.     puts("Enter server name : ");
  130.     safe_gets(server_name, SERVER_NAME_SIZE);
  131.  
  132.     puts("Enter request_url : ");
  133.     safe_gets(request_url, REQUEST_URL_SIZE);
  134.    
  135.     puts("Enter server port : ");
  136.     safe_gets(server_port, SERVER_PORT_SIZE);
  137.    
  138.     sprintf(get_request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", request_url, server_name);
  139.     //puts(get_request);
  140.     int ret = 1, len;
  141.     int exit_code = MBEDTLS_EXIT_FAILURE;
  142.     mbedtls_net_context server_fd;
  143.     uint32_t flags;
  144.     unsigned char buf[STDIO_MAX_MEM];
  145.     const char *pers = "ssl_client1";
  146.     mbedtls_entropy_context entropy;
  147.     mbedtls_ctr_drbg_context ctr_drbg;
  148.     mbedtls_ssl_context ssl;
  149.     mbedtls_ssl_config conf;
  150.     mbedtls_x509_crt cacert;
  151.  
  152. #if defined(MBEDTLS_DEBUG_C)
  153.    // mbedtls_debug_set_threshold( DEBUG_LEVEL );
  154. #endif
  155.  
  156.     /*
  157.      * 0. Initialize the RNG and the session data
  158.      */
  159.     mbedtls_net_init( &server_fd );
  160.     mbedtls_ssl_init( &ssl );
  161.     mbedtls_ssl_config_init( &conf );
  162.     mbedtls_x509_crt_init( &cacert );
  163.     mbedtls_ctr_drbg_init( &ctr_drbg );
  164.     mbedtls_printf( "\n  . Seeding the random number generator..." );
  165.     //fflush( stdout );
  166.     mbedtls_entropy_init( &entropy );
  167.     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  168.                                (const unsigned char *) pers,
  169.                                strlen( pers ) ) ) != 0 )
  170.     {
  171.         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  172.         goto exit;
  173.     }
  174.    
  175.     mbedtls_printf( " ok\n" );
  176.      
  177.     /*
  178.      * 0. Initialize certificates
  179.      */;
  180.     mbedtls_printf( "  . Loading the CA root certificate ..." );
  181.     //fflush( stdout );
  182.    
  183.     ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
  184.                           mbedtls_test_cas_pem_len );
  185.     if( ret < 0 )
  186.     {
  187.         mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
  188.         goto exit;
  189.     }
  190.  
  191.     mbedtls_printf( " ok (%d skipped)\n", ret );
  192.    
  193.     /*
  194.      * 1. Start the connection
  195.      */
  196.     mbedtls_printf( "  . Connecting to tcp/%s/%s...", server_name, server_port );
  197.     //fflush( stdout );
  198.  
  199.     if( ( ret = mbedtls_net_connect( &server_fd, server_name,
  200.                                          server_port, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  201.     {
  202.         mbedtls_printf( " failed\n  ! mbedtls_net_connect returned %d\n\n", ret );
  203.         goto exit;
  204.     }
  205.  
  206.     mbedtls_printf( " ok\n" );
  207.  
  208.     /*
  209.      * 2. Setup stuff
  210.      */
  211.     mbedtls_printf( "  . Setting up the SSL/TLS structure..." );
  212.     //fflush( stdout );
  213.  
  214.     if( ( ret = mbedtls_ssl_config_defaults( &conf,
  215.                     MBEDTLS_SSL_IS_CLIENT,
  216.                     MBEDTLS_SSL_TRANSPORT_STREAM,
  217.                     MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  218.     {
  219.         mbedtls_printf( " failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  220.         goto exit;
  221.     }
  222.  
  223.     mbedtls_printf( " ok\n" );
  224.  
  225.     /* OPTIONAL is not optimal for security,
  226.      * but makes interop easier in this simplified example */
  227.     mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
  228.     mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  229.     mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  230.     mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  231.  
  232.     if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  233.     {
  234.         mbedtls_printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret );
  235.         goto exit;
  236.     }
  237.  
  238.     if( ( ret = mbedtls_ssl_set_hostname( &ssl, server_name ) ) != 0 )
  239.     {
  240.         mbedtls_printf( " failed\n  ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  241.         goto exit;
  242.     }
  243.  
  244.     mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
  245.     /*
  246.      * 4. Handshake
  247.      */
  248.     mbedtls_printf( "  . Performing the SSL/TLS handshake..." );
  249.     //fflush( stdout );
  250.  
  251.     while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  252.     {
  253.         if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  254.         {
  255.             mbedtls_printf( " failed\n  ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
  256.             goto exit;
  257.         }
  258.     }
  259.  
  260.     mbedtls_printf( " ok\n" );
  261.  
  262.     /*
  263.      * 5. Verify the server certificate
  264.      */
  265.     mbedtls_printf( "  . Verifying peer X.509 certificate..." );
  266.  
  267.     /* In real life, we probably want to bail out when ret != 0 */
  268.     if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  269.     {
  270.         char vrfy_buf[512];
  271.  
  272.         mbedtls_printf( " failed\n" );
  273.  
  274.         mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), "  ! ", flags );
  275.  
  276.         mbedtls_printf( "%s\n", vrfy_buf );
  277.     }
  278.     else
  279.         mbedtls_printf( " ok\n" );
  280.  
  281.     /*
  282.      * 3. Write the GET request
  283.      */
  284.     mbedtls_printf( "  > Write to server:" );
  285.     //fflush( stdout );
  286.  
  287.     len = sprintf( (char *) buf, get_request );
  288.  
  289.     while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
  290.     {
  291.         if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  292.         {
  293.             mbedtls_printf( " failed\n  ! mbedtls_ssl_write returned %d\n\n", ret );
  294.             goto exit;
  295.         }
  296.     }
  297.  
  298.     len = ret;
  299.     mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
  300.  
  301.     /*
  302.      * 7. Read the HTTP response
  303.      */
  304.     mbedtls_printf( "  < Read from server:" );
  305.     //fflush( stdout );
  306.  
  307.     do
  308.     {
  309.         len = sizeof( buf ) - 1;
  310.         memset( buf, 0, sizeof( buf ) );
  311.         ret = mbedtls_ssl_read( &ssl, buf, len );
  312.  
  313.         if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  314.             continue;
  315.  
  316.         if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
  317.             break;
  318.  
  319.         if( ret < 0 )
  320.         {
  321.             mbedtls_printf( "failed\n  ! mbedtls_ssl_read returned %d\n\n", ret );
  322.             break;
  323.         }
  324.  
  325.         if( ret == 0 )
  326.         {
  327.             mbedtls_printf( "\n\nEOF\n\n" );
  328.             break;
  329.         }
  330.  
  331.         len = ret;
  332.         mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  333.     }
  334.     while( 1 );
  335.  
  336.     mbedtls_ssl_close_notify( &ssl );
  337.  
  338.     exit_code = MBEDTLS_EXIT_SUCCESS;
  339.  
  340. exit:
  341. //#ifdef MBEDTLS_ERROR_C
  342.     if( exit_code != MBEDTLS_EXIT_SUCCESS )
  343.     {
  344.         static char error_buf[100];
  345.         mbedtls_strerror( ret, error_buf, 100 );
  346.         mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
  347.     }
  348. //#endif
  349.  
  350.     mbedtls_net_free( &server_fd );
  351.  
  352.     mbedtls_x509_crt_free( &cacert );
  353.     mbedtls_ssl_free( &ssl );
  354.     mbedtls_ssl_config_free( &conf );
  355.     mbedtls_ctr_drbg_free( &ctr_drbg );
  356.     mbedtls_entropy_free( &entropy );
  357.  
  358.  
  359.     return( exit_code );
  360. }
  361. /*#endif MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  362.           MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  363.           MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
  364.           MBEDTLS_X509_CRT_PARSE_C */
  365.