Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL server demonstration program |
| 3 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
Paul Bakker | 27db1f5 | 2009-01-25 15:27:00 +0000 | [diff] [blame^] | 6 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 24 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 25 | #endif |
| 26 | |
| 27 | #ifdef WIN32 |
| 28 | #include <windows.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 35 | #include "polarssl/havege.h" |
| 36 | #include "polarssl/certs.h" |
| 37 | #include "polarssl/x509.h" |
| 38 | #include "polarssl/ssl.h" |
| 39 | #include "polarssl/net.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 40 | |
| 41 | #define HTTP_RESPONSE \ |
| 42 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
| 43 | "<h2><p><center>Successful connection using: %s\r\n" |
| 44 | |
| 45 | /* |
| 46 | * Computing a "safe" DH-1024 prime can take a very |
| 47 | * long time, so a precomputed value is provided below. |
| 48 | * You may run dh_genprime to generate a new value. |
| 49 | */ |
| 50 | char *my_dhm_P = |
| 51 | "E4004C1F94182000103D883A448B3F80" \ |
| 52 | "2CE4B44A83301270002C20D0321CFD00" \ |
| 53 | "11CCEF784C26A400F43DFB901BCA7538" \ |
| 54 | "F2C6B176001CF5A0FD16D2C48B1D0C1C" \ |
| 55 | "F6AC8E1DA6BCC3B4E1F96B0564965300" \ |
| 56 | "FFA1D0B601EB2800F489AA512C4B248C" \ |
| 57 | "01F76949A60BB7F00A40B1EAB64BDD48" \ |
| 58 | "E8A700D60B7F1200FA8E77B0A979DABF"; |
| 59 | |
| 60 | char *my_dhm_G = "4"; |
| 61 | |
| 62 | /* |
| 63 | * Sorted by order of preference |
| 64 | */ |
| 65 | int my_ciphers[] = |
| 66 | { |
| 67 | SSL_EDH_RSA_AES_256_SHA, |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 68 | SSL_EDH_RSA_CAMELLIA_256_SHA, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | SSL_EDH_RSA_DES_168_SHA, |
| 70 | SSL_RSA_AES_256_SHA, |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 71 | SSL_RSA_CAMELLIA_256_SHA, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | SSL_RSA_AES_128_SHA, |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 73 | SSL_RSA_CAMELLIA_128_SHA, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | SSL_RSA_DES_168_SHA, |
| 75 | SSL_RSA_RC4_128_SHA, |
| 76 | SSL_RSA_RC4_128_MD5, |
| 77 | 0 |
| 78 | }; |
| 79 | |
| 80 | #define DEBUG_LEVEL 0 |
| 81 | |
| 82 | void my_debug( void *ctx, int level, char *str ) |
| 83 | { |
| 84 | if( level < DEBUG_LEVEL ) |
| 85 | { |
| 86 | fprintf( (FILE *) ctx, "%s", str ); |
| 87 | fflush( (FILE *) ctx ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * These session callbacks use a simple chained list |
| 93 | * to store and retrieve the session information. |
| 94 | */ |
| 95 | ssl_session *s_list_1st = NULL; |
| 96 | ssl_session *cur, *prv; |
| 97 | |
| 98 | static int my_get_session( ssl_context *ssl ) |
| 99 | { |
| 100 | time_t t = time( NULL ); |
| 101 | |
| 102 | if( ssl->resume == 0 ) |
| 103 | return( 1 ); |
| 104 | |
| 105 | cur = s_list_1st; |
| 106 | prv = NULL; |
| 107 | |
| 108 | while( cur != NULL ) |
| 109 | { |
| 110 | prv = cur; |
| 111 | cur = cur->next; |
| 112 | |
| 113 | if( ssl->timeout != 0 && t - prv->start > ssl->timeout ) |
| 114 | continue; |
| 115 | |
| 116 | if( ssl->session->cipher != prv->cipher || |
| 117 | ssl->session->length != prv->length ) |
| 118 | continue; |
| 119 | |
| 120 | if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 ) |
| 121 | continue; |
| 122 | |
| 123 | memcpy( ssl->session->master, prv->master, 48 ); |
| 124 | return( 0 ); |
| 125 | } |
| 126 | |
| 127 | return( 1 ); |
| 128 | } |
| 129 | |
| 130 | static int my_set_session( ssl_context *ssl ) |
| 131 | { |
| 132 | time_t t = time( NULL ); |
| 133 | |
| 134 | cur = s_list_1st; |
| 135 | prv = NULL; |
| 136 | |
| 137 | while( cur != NULL ) |
| 138 | { |
| 139 | if( ssl->timeout != 0 && t - cur->start > ssl->timeout ) |
| 140 | break; /* expired, reuse this slot */ |
| 141 | |
| 142 | if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 ) |
| 143 | break; /* client reconnected */ |
| 144 | |
| 145 | prv = cur; |
| 146 | cur = cur->next; |
| 147 | } |
| 148 | |
| 149 | if( cur == NULL ) |
| 150 | { |
| 151 | cur = (ssl_session *) malloc( sizeof( ssl_session ) ); |
| 152 | if( cur == NULL ) |
| 153 | return( 1 ); |
| 154 | |
| 155 | if( prv == NULL ) |
| 156 | s_list_1st = cur; |
| 157 | else prv->next = cur; |
| 158 | } |
| 159 | |
| 160 | memcpy( cur, ssl->session, sizeof( ssl_session ) ); |
| 161 | |
| 162 | return( 0 ); |
| 163 | } |
| 164 | |
| 165 | int main( void ) |
| 166 | { |
| 167 | int ret, len; |
| 168 | int listen_fd; |
| 169 | int client_fd; |
| 170 | unsigned char buf[1024]; |
| 171 | |
| 172 | havege_state hs; |
| 173 | ssl_context ssl; |
| 174 | ssl_session ssn; |
| 175 | x509_cert srvcert; |
| 176 | rsa_context rsa; |
| 177 | |
| 178 | /* |
| 179 | * 1. Load the certificates and private RSA key |
| 180 | */ |
| 181 | printf( "\n . Loading the server cert. and key..." ); |
| 182 | fflush( stdout ); |
| 183 | |
| 184 | memset( &srvcert, 0, sizeof( x509_cert ) ); |
| 185 | |
| 186 | /* |
| 187 | * This demonstration program uses embedded test certificates. |
| 188 | * Instead, you may want to use x509parse_crtfile() to read the |
| 189 | * server and CA certificates, as well as x509parse_keyfile(). |
| 190 | */ |
| 191 | ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt, |
| 192 | strlen( test_srv_crt ) ); |
| 193 | if( ret != 0 ) |
| 194 | { |
| 195 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 196 | goto exit; |
| 197 | } |
| 198 | |
| 199 | ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt, |
| 200 | strlen( test_ca_crt ) ); |
| 201 | if( ret != 0 ) |
| 202 | { |
| 203 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 204 | goto exit; |
| 205 | } |
| 206 | |
| 207 | ret = x509parse_key( &rsa, (unsigned char *) test_srv_key, |
| 208 | strlen( test_srv_key ), NULL, 0 ); |
| 209 | if( ret != 0 ) |
| 210 | { |
| 211 | printf( " failed\n ! x509parse_key returned %d\n\n", ret ); |
| 212 | goto exit; |
| 213 | } |
| 214 | |
| 215 | printf( " ok\n" ); |
| 216 | |
| 217 | /* |
| 218 | * 2. Setup the listening TCP socket |
| 219 | */ |
| 220 | printf( " . Bind on https://localhost:4433/ ..." ); |
| 221 | fflush( stdout ); |
| 222 | |
| 223 | if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 ) |
| 224 | { |
| 225 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 226 | goto exit; |
| 227 | } |
| 228 | |
| 229 | printf( " ok\n" ); |
| 230 | |
| 231 | /* |
| 232 | * 3. Wait until a client connects |
| 233 | */ |
| 234 | #ifdef WIN32 |
| 235 | ShellExecute( NULL, "open", "https://localhost:4433/", |
| 236 | NULL, NULL, SW_SHOWNORMAL ); |
| 237 | #endif |
| 238 | |
| 239 | client_fd = -1; |
| 240 | memset( &ssl, 0, sizeof( ssl ) ); |
| 241 | |
| 242 | accept: |
| 243 | |
| 244 | net_close( client_fd ); |
| 245 | ssl_free( &ssl ); |
| 246 | |
| 247 | printf( " . Waiting for a remote connection ..." ); |
| 248 | fflush( stdout ); |
| 249 | |
| 250 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 251 | { |
| 252 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 253 | goto exit; |
| 254 | } |
| 255 | |
| 256 | printf( " ok\n" ); |
| 257 | |
| 258 | /* |
| 259 | * 4. Setup stuff |
| 260 | */ |
| 261 | printf( " . Setting up the RNG and SSL data...." ); |
| 262 | fflush( stdout ); |
| 263 | |
| 264 | havege_init( &hs ); |
| 265 | |
| 266 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 267 | { |
| 268 | printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
| 269 | goto accept; |
| 270 | } |
| 271 | |
| 272 | printf( " ok\n" ); |
| 273 | |
| 274 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
| 275 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
| 276 | |
| 277 | ssl_set_rng( &ssl, havege_rand, &hs ); |
| 278 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 279 | ssl_set_bio( &ssl, net_recv, &client_fd, |
| 280 | net_send, &client_fd ); |
| 281 | ssl_set_scb( &ssl, my_get_session, |
| 282 | my_set_session ); |
| 283 | |
| 284 | ssl_set_ciphers( &ssl, my_ciphers ); |
| 285 | ssl_set_session( &ssl, 1, 0, &ssn ); |
| 286 | |
| 287 | memset( &ssn, 0, sizeof( ssl_session ) ); |
| 288 | |
| 289 | ssl_set_ca_chain( &ssl, srvcert.next, NULL ); |
| 290 | ssl_set_own_cert( &ssl, &srvcert, &rsa ); |
| 291 | ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G ); |
| 292 | |
| 293 | /* |
| 294 | * 5. Handshake |
| 295 | */ |
| 296 | printf( " . Performing the SSL/TLS handshake..." ); |
| 297 | fflush( stdout ); |
| 298 | |
| 299 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 300 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 301 | if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 302 | { |
| 303 | printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
| 304 | goto accept; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | printf( " ok\n" ); |
| 309 | |
| 310 | /* |
| 311 | * 6. Read the HTTP Request |
| 312 | */ |
| 313 | printf( " < Read from client:" ); |
| 314 | fflush( stdout ); |
| 315 | |
| 316 | do |
| 317 | { |
| 318 | len = sizeof( buf ) - 1; |
| 319 | memset( buf, 0, sizeof( buf ) ); |
| 320 | ret = ssl_read( &ssl, buf, len ); |
| 321 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 322 | if( ret == POLARSSL_ERR_NET_TRY_AGAIN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 323 | continue; |
| 324 | |
| 325 | if( ret <= 0 ) |
| 326 | { |
| 327 | switch( ret ) |
| 328 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 329 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | printf( " connection was closed gracefully\n" ); |
| 331 | break; |
| 332 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 333 | case POLARSSL_ERR_NET_CONN_RESET: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 334 | printf( " connection was reset by peer\n" ); |
| 335 | break; |
| 336 | |
| 337 | default: |
| 338 | printf( " ssl_read returned %d\n", ret ); |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | len = ret; |
| 346 | printf( " %d bytes read\n\n%s", len, (char *) buf ); |
| 347 | } |
| 348 | while( 0 ); |
| 349 | |
| 350 | /* |
| 351 | * 7. Write the 200 Response |
| 352 | */ |
| 353 | printf( " > Write to client:" ); |
| 354 | fflush( stdout ); |
| 355 | |
| 356 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
| 357 | ssl_get_cipher( &ssl ) ); |
| 358 | |
| 359 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) |
| 360 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 361 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 362 | { |
| 363 | printf( " failed\n ! peer closed the connection\n\n" ); |
| 364 | goto accept; |
| 365 | } |
| 366 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 367 | if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 368 | { |
| 369 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 370 | goto exit; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | len = ret; |
| 375 | printf( " %d bytes written\n\n%s\n", len, (char *) buf ); |
| 376 | |
| 377 | ssl_close_notify( &ssl ); |
| 378 | goto accept; |
| 379 | |
| 380 | exit: |
| 381 | |
| 382 | net_close( client_fd ); |
| 383 | x509_free( &srvcert ); |
| 384 | rsa_free( &rsa ); |
| 385 | ssl_free( &ssl ); |
| 386 | |
| 387 | cur = s_list_1st; |
| 388 | while( cur != NULL ) |
| 389 | { |
| 390 | prv = cur; |
| 391 | cur = cur->next; |
| 392 | memset( prv, 0, sizeof( ssl_session ) ); |
| 393 | free( prv ); |
| 394 | } |
| 395 | |
| 396 | memset( &ssl, 0, sizeof( ssl_context ) ); |
| 397 | |
| 398 | #ifdef WIN32 |
| 399 | printf( " Press Enter to exit this program.\n" ); |
| 400 | fflush( stdout ); getchar(); |
| 401 | #endif |
| 402 | |
| 403 | return( ret ); |
| 404 | } |