1 /*
2 * SSL client demonstration program
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22 #include "mbedtls/config.h"
23 #include "mbedtls/platform.h"
24 #include "mbedtls/net_sockets.h"
25 #include "mbedtls/debug.h"
26 #include "mbedtls/ssl.h"
27 #include "mbedtls/entropy.h"
28 #include "mbedtls/ctr_drbg.h"
29 #include "mbedtls/error.h"
30 #include "mbedtls/certs.h"
31
32 #if AOS_COMP_CLI
33 #include "aos/cli.h"
34 #endif
35
36 #include <string.h>
37
38 static const char globalsign_g2_ca_cert[] =
39 "-----BEGIN CERTIFICATE-----\r\n"
40 "MIIEaTCCA1GgAwIBAgILBAAAAAABRE7wQkcwDQYJKoZIhvcNAQELBQAwVzELMAkG\r\n"
41 "A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\r\n"
42 "b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw0xNDAyMjAxMDAw\r\n"
43 "MDBaFw0yNDAyMjAxMDAwMDBaMGYxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\r\n"
44 "YWxTaWduIG52LXNhMTwwOgYDVQQDEzNHbG9iYWxTaWduIE9yZ2FuaXphdGlvbiBW\r\n"
45 "YWxpZGF0aW9uIENBIC0gU0hBMjU2IC0gRzIwggEiMA0GCSqGSIb3DQEBAQUAA4IB\r\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hcIOKrwbFINihY9Bsrk8y1658GEV1BSl3\r\n"
61 "30JAZGSGvip2CTFvHST0mdCF/vIhCPnG9vHQWe3WVjwIKANnuvD58ZAWR65n5ryA\r\n"
62 "SOlCdjSXVWkkDoPWoC209fN5ikkodBpBocLTJIg1MGCUF7ThBCIxPTsvFwayuJ2G\r\n"
63 "K1pp74P1S8SqtCr4fKGxhZSM9AyHDPSsQPhZSZg=\r\n"
64 "-----END CERTIFICATE-----";
65
66 static const size_t globalsign_g2_ca_cert_len = sizeof(globalsign_g2_ca_cert);
67
68 #define SERVER_PORT "443"
69 #define SERVER_NAME "www.baidu.com"
70 #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
71
72 #define DEBUG_LEVEL 1
73
tls_example_debug(void * ctx,int level,const char * file,int line,const char * str)74 static void tls_example_debug( void *ctx, int level,
75 const char *file, int line,
76 const char *str )
77 {
78 ((void) level);
79
80 printf("%s\n", str);
81 }
82
83 /* Notice: It's designed only for this example, please design your own
84 * random algorithm for security concern.
85 */
tls_example_random(void * handle,unsigned char * output,size_t output_len)86 static int tls_example_random(void *handle, unsigned char *output, size_t output_len)
87 {
88 uint32_t idx = 0, bytes = 0, rand_num = 0;
89 struct timeval time;
90
91 memset(&time, 0, sizeof(struct timeval));
92 gettimeofday(&time, NULL);
93
94 srand((unsigned int)(time.tv_sec * 1000 + time.tv_usec / 1000) + rand());
95
96 for (idx = 0; idx < output_len;) {
97 if (output_len - idx < 4) {
98 bytes = output_len - idx;
99 } else {
100 bytes = 4;
101 }
102 rand_num = rand();
103 while (bytes-- > 0) {
104 output[idx++] = (uint8_t)(rand_num >> bytes * 8);
105 }
106 }
107 return 0;
108 }
109
tls_example(int argc,char ** argv)110 static int tls_example(int argc, char **argv)
111 {
112 int ret = 1, len;
113 int exit_code = MBEDTLS_EXIT_FAILURE;
114 mbedtls_net_context server_fd;
115 uint32_t flags;
116 unsigned char buf[1024];
117
118 mbedtls_ssl_context ssl;
119 mbedtls_ssl_config conf;
120 mbedtls_x509_crt cacert;
121
122 #if defined(MBEDTLS_DEBUG_C)
123 mbedtls_debug_set_threshold( DEBUG_LEVEL );
124 #endif
125
126 /*
127 * 0. Initialize the session data
128 */
129 mbedtls_net_init( &server_fd );
130 mbedtls_ssl_init( &ssl );
131 mbedtls_ssl_config_init( &conf );
132 mbedtls_x509_crt_init( &cacert );
133
134 /*
135 * 0. Initialize certificates
136 */
137 mbedtls_printf( " . Loading the CA root certificate ..." );
138 fflush( stdout );
139
140 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) globalsign_g2_ca_cert,
141 globalsign_g2_ca_cert_len );
142 if( ret < 0 )
143 {
144 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
145 goto exit;
146 }
147
148 mbedtls_printf( " ok (%d skipped)\n", ret );
149
150 /*
151 * 1. Start the connection
152 */
153 mbedtls_printf( " . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT );
154 fflush( stdout );
155
156 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
157 SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
158 {
159 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
160 goto exit;
161 }
162
163 mbedtls_printf( " ok\n" );
164
165 /*
166 * 2. Setup stuff
167 */
168 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
169 fflush( stdout );
170
171 if( ( ret = mbedtls_ssl_config_defaults( &conf,
172 MBEDTLS_SSL_IS_CLIENT,
173 MBEDTLS_SSL_TRANSPORT_STREAM,
174 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
175 {
176 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
177 goto exit;
178 }
179
180 mbedtls_printf( " ok\n" );
181
182 /* OPTIONAL is not optimal for security,
183 * but makes interop easier in this simplified example */
184 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
185 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
186 mbedtls_ssl_conf_rng( &conf, tls_example_random, NULL );
187 mbedtls_ssl_conf_dbg( &conf, tls_example_debug, stdout );
188
189 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
190 {
191 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
192 goto exit;
193 }
194
195 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
196 {
197 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
198 goto exit;
199 }
200
201 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
202
203 /*
204 * 4. Handshake
205 */
206 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
207 fflush( stdout );
208
209 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
210 {
211 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
212 {
213 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
214 goto exit;
215 }
216 }
217
218 mbedtls_printf( " ok\n" );
219
220 /*
221 * 5. Verify the server certificate
222 */
223 mbedtls_printf( " . Verifying peer X.509 certificate..." );
224
225 /* In real life, we probably want to bail out when ret != 0 */
226 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
227 {
228 char vrfy_buf[512];
229
230 mbedtls_printf( " failed\n" );
231
232 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
233
234 mbedtls_printf( "%s\n", vrfy_buf );
235 }
236 else
237 mbedtls_printf( " ok\n" );
238
239 /*
240 * 3. Write the GET request
241 */
242 mbedtls_printf( " > Write to server:" );
243 fflush( stdout );
244
245 len = sprintf( (char *) buf, GET_REQUEST );
246
247 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
248 {
249 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
250 {
251 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
252 goto exit;
253 }
254 }
255
256 len = ret;
257 mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
258
259 /*
260 * 7. Read the HTTP response
261 */
262 mbedtls_printf( " < Read from server:" );
263 fflush( stdout );
264
265 do
266 {
267 len = sizeof( buf ) - 1;
268 memset( buf, 0, sizeof( buf ) );
269 ret = mbedtls_ssl_read( &ssl, buf, len );
270
271 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
272 continue;
273
274 if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
275 break;
276
277 if( ret < 0 )
278 {
279 mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
280 break;
281 }
282
283 if( ret == 0 )
284 {
285 mbedtls_printf( "\n\nEOF\n\n" );
286 break;
287 }
288
289 len = ret;
290 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
291 }
292 while( 1 );
293
294 mbedtls_ssl_close_notify( &ssl );
295
296 exit_code = MBEDTLS_EXIT_SUCCESS;
297 printf("tls_example test success!\n");
298
299 exit:
300
301 #ifdef MBEDTLS_ERROR_C
302 if( exit_code != MBEDTLS_EXIT_SUCCESS )
303 {
304 char error_buf[100];
305 mbedtls_strerror( ret, error_buf, 100 );
306 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
307 }
308 #endif
309
310 mbedtls_net_free( &server_fd );
311
312 mbedtls_x509_crt_free( &cacert );
313 mbedtls_ssl_free( &ssl );
314 mbedtls_ssl_config_free( &conf );
315
316 return( exit_code );
317 }
318
319 #if AOS_COMP_CLI
320 /* reg args: fun, cmd, description*/
321 ALIOS_CLI_CMD_REGISTER(tls_example, tls_example, tls client example)
322 #endif
323