- Main Page
- Related Pages
- Data Structures
- Files

# ssl_srv.c

Go to the documentation of this file.

    00001 /*
    00002  *  SSLv3/TLSv1 server-side functions
    00003  *
    00004  *  Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
    00005  *  All rights reserved.
    00006  *
    00007  *  Joined copyright on original XySSL code with: Christophe Devine
    00008  *
    00009  *  This program is free software; you can redistribute it and/or modify
    00010  *  it under the terms of the GNU General Public License as published by
    00011  *  the Free Software Foundation; either version 2 of the License, or
    00012  *  (at your option) any later version.
    00013  *
    00014  *  This program is distributed in the hope that it will be useful,
    00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00017  *  GNU General Public License for more details.
    00018  *
    00019  *  You should have received a copy of the GNU General Public License along
    00020  *  with this program; if not, write to the Free Software Foundation, Inc.,
    00021  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    00022  */
    00023
    00024 #include "polarssl/config.h"
    00025
    00026 #if defined(POLARSSL_SSL_SRV_C)
    00027
    00028 #include "polarssl/debug.h"
    00029 #include "polarssl/ssl.h"
    00030
    00031 #include <string.h>
    00032 #include <stdlib.h>
    00033 #include <stdio.h>
    00034 #include <time.h>
    00035
    00036 static int ssl_parse_client_hello( ssl_context *ssl )
    00037 {
    00038     int ret, i, j, n;
    00039     int ciph_len, sess_len;
    00040     int chal_len, comp_len;
    00041     unsigned char *buf, *p;
    00042
    00043     SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
    00044
    00045     if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
    00046     {
    00047         SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
    00048         return( ret );
    00049     }
    00050
    00051     buf = ssl->in_hdr;
    00052
    00053     if( ( buf[0] & 0x80 ) != 0 )
    00054     {
    00055         SSL_DEBUG_BUF( 4, "record header", buf, 5 );
    00056
    00057         SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
    00058                        buf[2] ) );
    00059         SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
    00060                        ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
    00061         SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
    00062                        buf[3], buf[4] ) );
    00063
    00064         /*
    00065          * SSLv2 Client Hello
    00066          *
    00067          * Record layer:
    00068          *     0  .   1   message length
    00069          *
    00070          * SSL layer:
    00071          *     2  .   2   message type
    00072          *     3  .   4   protocol version
    00073          */
    00074         if( buf[2] != SSL_HS_CLIENT_HELLO ||
    00075             buf[3] != SSL_MAJOR_VERSION_3 )
    00076         {
    00077             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00078             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00079         }
    00080
    00081         n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
    00082
    00083         if( n < 17 || n > 512 )
    00084         {
    00085             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00086             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00087         }
    00088
    00089         ssl->max_major_ver = buf[3];
    00090         ssl->max_minor_ver = buf[4];
    00091
    00092         ssl->major_ver = SSL_MAJOR_VERSION_3;
    00093         ssl->minor_ver = ( buf[4] <= SSL_MINOR_VERSION_1 )
    00094                          ? buf[4]  : SSL_MINOR_VERSION_1;
    00095
    00096         if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
    00097         {
    00098             SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
    00099             return( ret );
    00100         }
    00101
    00102          md5_update( &ssl->fin_md5 , buf + 2, n );
    00103         sha1_update( &ssl->fin_sha1, buf + 2, n );
    00104
    00105         buf = ssl->in_msg;
    00106         n = ssl->in_left - 5;
    00107
    00108         /*
    00109          *    0  .   1   cipherlist length
    00110          *    2  .   3   session id length
    00111          *    4  .   5   challenge length
    00112          *    6  .  ..   cipherlist
    00113          *   ..  .  ..   session id
    00114          *   ..  .  ..   challenge
    00115          */
    00116         SSL_DEBUG_BUF( 4, "record contents", buf, n );
    00117
    00118         ciph_len = ( buf[0] << 8 ) | buf[1];
    00119         sess_len = ( buf[2] << 8 ) | buf[3];
    00120         chal_len = ( buf[4] << 8 ) | buf[5];
    00121
    00122         SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
    00123                        ciph_len, sess_len, chal_len ) );
    00124
    00125         /*
    00126          * Make sure each parameter length is valid
    00127          */
    00128         if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
    00129         {
    00130             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00131             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00132         }
    00133
    00134         if( sess_len < 0 || sess_len > 32 )
    00135         {
    00136             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00137             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00138         }
    00139
    00140         if( chal_len < 8 || chal_len > 32 )
    00141         {
    00142             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00143             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00144         }
    00145
    00146         if( n != 6 + ciph_len + sess_len + chal_len )
    00147         {
    00148             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00149             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00150         }
    00151
    00152         SSL_DEBUG_BUF( 3, "client hello, cipherlist",
    00153                        buf + 6,  ciph_len );
    00154         SSL_DEBUG_BUF( 3, "client hello, session id",
    00155                        buf + 6 + ciph_len,  sess_len );
    00156         SSL_DEBUG_BUF( 3, "client hello, challenge",
    00157                        buf + 6 + ciph_len + sess_len,  chal_len );
    00158
    00159         p = buf + 6 + ciph_len;
    00160         ssl->session->length = sess_len;
    00161         memset( ssl->session->id, 0, sizeof( ssl->session->id ) );
    00162         memcpy( ssl->session->id, p, ssl->session->length );
    00163
    00164         p += sess_len;
    00165         memset( ssl->randbytes, 0, 64 );
    00166         memcpy( ssl->randbytes + 32 - chal_len, p, chal_len );
    00167
    00168         for( i = 0; ssl->ciphers[i] != 0; i++ )
    00169         {
    00170             for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
    00171             {
    00172                 if( p[0] == 0 &&
    00173                     p[1] == 0 &&
    00174                     p[2] == ssl->ciphers[i] )
    00175                     goto have_cipher;
    00176             }
    00177         }
    00178     }
    00179     else
    00180     {
    00181         SSL_DEBUG_BUF( 4, "record header", buf, 5 );
    00182
    00183         SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
    00184                        buf[0] ) );
    00185         SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
    00186                        ( buf[3] << 8 ) | buf[4] ) );
    00187         SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
    00188                        buf[1], buf[2] ) );
    00189
    00190         /*
    00191          * SSLv3 Client Hello
    00192          *
    00193          * Record layer:
    00194          *     0  .   0   message type
    00195          *     1  .   2   protocol version
    00196          *     3  .   4   message length
    00197          */
    00198         if( buf[0] != SSL_MSG_HANDSHAKE ||
    00199             buf[1] != SSL_MAJOR_VERSION_3 )
    00200         {
    00201             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00202             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00203         }
    00204
    00205         n = ( buf[3] << 8 ) | buf[4];
    00206
    00207         if( n < 45 || n > 512 )
    00208         {
    00209             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00210             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00211         }
    00212
    00213         if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
    00214         {
    00215             SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
    00216             return( ret );
    00217         }
    00218
    00219         buf = ssl->in_msg;
    00220         n = ssl->in_left - 5;
    00221
    00222          md5_update( &ssl->fin_md5 , buf, n );
    00223         sha1_update( &ssl->fin_sha1, buf, n );
    00224
    00225         /*
    00226          * SSL layer:
    00227          *     0  .   0   handshake type
    00228          *     1  .   3   handshake length
    00229          *     4  .   5   protocol version
    00230          *     6  .   9   UNIX time()
    00231          *    10  .  37   random bytes
    00232          *    38  .  38   session id length
    00233          *    39  . 38+x  session id
    00234          *   39+x . 40+x  cipherlist length
    00235          *   41+x .  ..   cipherlist
    00236          *    ..  .  ..   compression alg.
    00237          *    ..  .  ..   extensions
    00238          */
    00239         SSL_DEBUG_BUF( 4, "record contents", buf, n );
    00240
    00241         SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
    00242                        buf[0] ) );
    00243         SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
    00244                        ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
    00245         SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
    00246                        buf[4], buf[5] ) );
    00247
    00248         /*
    00249          * Check the handshake type and protocol version
    00250          */
    00251         if( buf[0] != SSL_HS_CLIENT_HELLO ||
    00252             buf[4] != SSL_MAJOR_VERSION_3 )
    00253         {
    00254             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00255             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00256         }
    00257
    00258         ssl->major_ver = SSL_MAJOR_VERSION_3;
    00259         ssl->minor_ver = ( buf[5] <= SSL_MINOR_VERSION_1 )
    00260                          ? buf[5]  : SSL_MINOR_VERSION_1;
    00261
    00262         ssl->max_major_ver = buf[4];
    00263         ssl->max_minor_ver = buf[5];
    00264
    00265         memcpy( ssl->randbytes, buf + 6, 32 );
    00266
    00267         /*
    00268          * Check the handshake message length
    00269          */
    00270         if( buf[1] != 0 || n != 4 + ( ( buf[2] << 8 ) | buf[3] ) )
    00271         {
    00272             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00273             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00274         }
    00275
    00276         /*
    00277          * Check the session length
    00278          */
    00279         sess_len = buf[38];
    00280
    00281         if( sess_len < 0 || sess_len > 32 )
    00282         {
    00283             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00284             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00285         }
    00286
    00287         ssl->session->length = sess_len;
    00288         memset( ssl->session->id, 0, sizeof( ssl->session->id ) );
    00289         memcpy( ssl->session->id, buf + 39 , ssl->session->length );
    00290
    00291         /*
    00292          * Check the cipherlist length
    00293          */
    00294         ciph_len = ( buf[39 + sess_len] << 8 )
    00295                  | ( buf[40 + sess_len]      );
    00296
    00297         if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
    00298         {
    00299             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00300             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00301         }
    00302
    00303         /*
    00304          * Check the compression algorithms length
    00305          */
    00306         comp_len = buf[41 + sess_len + ciph_len];
    00307
    00308         if( comp_len < 1 || comp_len > 16 )
    00309         {
    00310             SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
    00311             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    00312         }
    00313
    00314         SSL_DEBUG_BUF( 3, "client hello, random bytes",
    00315                        buf +  6,  32 );
    00316         SSL_DEBUG_BUF( 3, "client hello, session id",
    00317                        buf + 38,  sess_len );
    00318         SSL_DEBUG_BUF( 3, "client hello, cipherlist",
    00319                        buf + 41 + sess_len,  ciph_len );
    00320         SSL_DEBUG_BUF( 3, "client hello, compression",
    00321                        buf + 42 + sess_len + ciph_len, comp_len );
    00322
    00323         /*
    00324          * Search for a matching cipher
    00325          */
    00326         for( i = 0; ssl->ciphers[i] != 0; i++ )
    00327         {
    00328             for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
    00329                 j += 2, p += 2 )
    00330             {
    00331                 if( p[0] == 0 && p[1] == ssl->ciphers[i] )
    00332                     goto have_cipher;
    00333             }
    00334         }
    00335     }
    00336
    00337     SSL_DEBUG_MSG( 1, ( "got no ciphers in common" ) );
    00338
    00339     return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
    00340
    00341 have_cipher:
    00342
    00343     ssl->session->cipher = ssl->ciphers[i];
    00344     ssl->in_left = 0;
    00345     ssl->state++;
    00346
    00347     SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
    00348
    00349     return( 0 );
    00350 }
    00351
    00352 static int ssl_write_server_hello( ssl_context *ssl )
    00353 {
    00354     time_t t;
    00355     int ret, i, n;
    00356     unsigned char *buf, *p;
    00357
    00358     SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
    00359
    00360     /*
    00361      *     0  .   0   handshake type
    00362      *     1  .   3   handshake length
    00363      *     4  .   5   protocol version
    00364      *     6  .   9   UNIX time()
    00365      *    10  .  37   random bytes
    00366      */
    00367     buf = ssl->out_msg;
    00368     p = buf + 4;
    00369
    00370     *p++ = (unsigned char) ssl->major_ver;
    00371     *p++ = (unsigned char) ssl->minor_ver;
    00372
    00373     SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
    00374                    buf[4], buf[5] ) );
    00375
    00376     t = time( NULL );
    00377     *p++ = (unsigned char)( t >> 24 );
    00378     *p++ = (unsigned char)( t >> 16 );
    00379     *p++ = (unsigned char)( t >>  8 );
    00380     *p++ = (unsigned char)( t       );
    00381
    00382     SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
    00383
    00384     for( i = 28; i > 0; i-- )
    00385         *p++ = (unsigned char) ssl->f_rng( ssl->p_rng );
    00386
    00387     memcpy( ssl->randbytes + 32, buf + 6, 32 );
    00388
    00389     SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
    00390
    00391     /*
    00392      *    38  .  38   session id length
    00393      *    39  . 38+n  session id
    00394      *   39+n . 40+n  chosen cipher
    00395      *   41+n . 41+n  chosen compression alg.
    00396      */
    00397     ssl->session->length = n = 32;
    00398     *p++ = (unsigned char) ssl->session->length;
    00399
    00400     if( ssl->s_get == NULL ||
    00401         ssl->s_get( ssl ) != 0 )
    00402     {
    00403         /*
    00404          * Not found, create a new session id
    00405          */
    00406         ssl->resume = 0;
    00407         ssl->state++;
    00408
    00409         for( i = 0; i < n; i++ )
    00410             ssl->session->id[i] =
    00411                 (unsigned char) ssl->f_rng( ssl->p_rng );
    00412     }
    00413     else
    00414     {
    00415         /*
    00416          * Found a matching session, resume it
    00417          */
    00418         ssl->resume = 1;
    00419         ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
    00420         ssl_derive_keys( ssl );
    00421     }
    00422
    00423     memcpy( p, ssl->session->id, ssl->session->length );
    00424     p += ssl->session->length;
    00425
    00426     SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
    00427     SSL_DEBUG_BUF( 3,   "server hello, session id", buf + 39, n );
    00428     SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
    00429                    ssl->resume ? "a" : "no" ) );
    00430
    00431     *p++ = (unsigned char)( ssl->session->cipher >> 8 );
    00432     *p++ = (unsigned char)( ssl->session->cipher      );
    00433     *p++ = SSL_COMPRESS_NULL;
    00434
    00435     SSL_DEBUG_MSG( 3, ( "server hello, chosen cipher: %d",
    00436                    ssl->session->cipher ) );
    00437     SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", 0 ) );
    00438
    00439     ssl->out_msglen  = p - buf;
    00440     ssl->out_msgtype = SSL_MSG_HANDSHAKE;
    00441     ssl->out_msg[0]  = SSL_HS_SERVER_HELLO;
    00442
    00443     ret = ssl_write_record( ssl );
    00444
    00445     SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
    00446
    00447     return( ret );
    00448 }
    00449
    00450 static int ssl_write_certificate_request( ssl_context *ssl )
    00451 {
    00452     int ret, n;
    00453     unsigned char *buf, *p;
    00454     x509_cert *crt;
    00455
    00456     SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
    00457
    00458     ssl->state++;
    00459
    00460     if( ssl->authmode == SSL_VERIFY_NONE )
    00461     {
    00462         SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
    00463         return( 0 );
    00464     }
    00465
    00466     /*
    00467      *     0  .   0   handshake type
    00468      *     1  .   3   handshake length
    00469      *     4  .   4   cert type count
    00470      *     5  .. n-1  cert types
    00471      *     n  .. n+1  length of all DNs
    00472      *    n+2 .. n+3  length of DN 1
    00473      *    n+4 .. ...  Distinguished Name #1
    00474      *    ... .. ...  length of DN 2, etc.
    00475      */
    00476     buf = ssl->out_msg;
    00477     p = buf + 4;
    00478
    00479     /*
    00480      * At the moment, only RSA certificates are supported
    00481      */
    00482     *p++ = 1;
    00483     *p++ = 1;
    00484
    00485     p += 2;
    00486     crt = ssl->ca_chain;
    00487
    00488     while( crt != NULL && crt->next != NULL )
    00489     {
    00490         if( p - buf > 4096 )
    00491             break;
    00492
    00493         n = crt->subject_raw.len;
    00494         *p++ = (unsigned char)( n >> 8 );
    00495         *p++ = (unsigned char)( n      );
    00496         memcpy( p, crt->subject_raw.p, n );
    00497
    00498         SSL_DEBUG_BUF( 3, "requested DN", p, n );
    00499         p += n; crt = crt->next;
    00500     }
    00501
    00502     ssl->out_msglen  = n = p - buf;
    00503     ssl->out_msgtype = SSL_MSG_HANDSHAKE;
    00504     ssl->out_msg[0]  = SSL_HS_CERTIFICATE_REQUEST;
    00505     ssl->out_msg[6]  = (unsigned char)( ( n - 8 ) >> 8 );
    00506     ssl->out_msg[7]  = (unsigned char)( ( n - 8 )      );
    00507
    00508     ret = ssl_write_record( ssl );
    00509
    00510     SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
    00511
    00512     return( ret );
    00513 }
    00514
    00515 static int ssl_write_server_key_exchange( ssl_context *ssl )
    00516 {
    00517     int ret, n;
    00518     unsigned char hash[36];
    00519     md5_context md5;
    00520     sha1_context sha1;
    00521
    00522     SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
    00523
    00524     if( ssl->session->cipher != SSL_EDH_RSA_DES_168_SHA &&
    00525         ssl->session->cipher != SSL_EDH_RSA_AES_256_SHA &&
    00526     ssl->session->cipher != SSL_EDH_RSA_CAMELLIA_256_SHA)
    00527     {
    00528         SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
    00529         ssl->state++;
    00530         return( 0 );
    00531     }
    00532
    00533 #if !defined(POLARSSL_DHM_C)
    00534     SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) );
    00535     return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
    00536 #else
    00537     /*
    00538      * Ephemeral DH parameters:
    00539      *
    00540      * struct {
    00541      *     opaque dh_p<1..2^16-1>;
    00542      *     opaque dh_g<1..2^16-1>;
    00543      *     opaque dh_Ys<1..2^16-1>;
    00544      * } ServerDHParams;
    00545      */
    00546     if( ( ret = dhm_make_params( &ssl->dhm_ctx, 256, ssl->out_msg + 4,
    00547                                  &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
    00548     {
    00549         SSL_DEBUG_RET( 1, "dhm_make_params", ret );
    00550         return( ret );
    00551     }
    00552
    00553     SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->dhm_ctx.X  );
    00554     SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->dhm_ctx.P  );
    00555     SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->dhm_ctx.G  );
    00556     SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->dhm_ctx.GX );
    00557
    00558     /*
    00559      * digitally-signed struct {
    00560      *     opaque md5_hash[16];
    00561      *     opaque sha_hash[20];
    00562      * };
    00563      *
    00564      * md5_hash
    00565      *     MD5(ClientHello.random + ServerHello.random
    00566      *                            + ServerParams);
    00567      * sha_hash
    00568      *     SHA(ClientHello.random + ServerHello.random
    00569      *                            + ServerParams);
    00570      */
    00571     md5_starts( &md5 );
    00572     md5_update( &md5, ssl->randbytes,  64 );
    00573     md5_update( &md5, ssl->out_msg + 4, n );
    00574     md5_finish( &md5, hash );
    00575
    00576     sha1_starts( &sha1 );
    00577     sha1_update( &sha1, ssl->randbytes,  64 );
    00578     sha1_update( &sha1, ssl->out_msg + 4, n );
    00579     sha1_finish( &sha1, hash + 16 );
    00580
    00581     SSL_DEBUG_BUF( 3, "parameters hash", hash, 36 );
    00582
    00583     ssl->out_msg[4 + n] = (unsigned char)( ssl->rsa_key->len >> 8 );
    00584     ssl->out_msg[5 + n] = (unsigned char)( ssl->rsa_key->len      );
    00585
    00586     ret = rsa_pkcs1_sign( ssl->rsa_key, RSA_PRIVATE,
    00587                           SIG_RSA_RAW, 36, hash, ssl->out_msg + 6 + n );
    00588     if( ret != 0 )
    00589     {
    00590         SSL_DEBUG_RET( 1, "rsa_pkcs1_sign", ret );
    00591         return( ret );
    00592     }
    00593
    00594     SSL_DEBUG_BUF( 3, "my RSA sig", ssl->out_msg + 6 + n,
    00595                                     ssl->rsa_key->len );
    00596
    00597     ssl->out_msglen  = 6 + n + ssl->rsa_key->len;
    00598     ssl->out_msgtype = SSL_MSG_HANDSHAKE;
    00599     ssl->out_msg[0]  = SSL_HS_SERVER_KEY_EXCHANGE;
    00600
    00601     ssl->state++;
    00602
    00603     if( ( ret = ssl_write_record( ssl ) ) != 0 )
    00604     {
    00605         SSL_DEBUG_RET( 1, "ssl_write_record", ret );
    00606         return( ret );
    00607     }
    00608
    00609     SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
    00610
    00611     return( 0 );
    00612 #endif
    00613 }
    00614
    00615 static int ssl_write_server_hello_done( ssl_context *ssl )
    00616 {
    00617     int ret;
    00618
    00619     SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
    00620
    00621     ssl->out_msglen  = 4;
    00622     ssl->out_msgtype = SSL_MSG_HANDSHAKE;
    00623     ssl->out_msg[0]  = SSL_HS_SERVER_HELLO_DONE;
    00624
    00625     ssl->state++;
    00626
    00627     if( ( ret = ssl_write_record( ssl ) ) != 0 )
    00628     {
    00629         SSL_DEBUG_RET( 1, "ssl_write_record", ret );
    00630         return( ret );
    00631     }
    00632
    00633     SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
    00634
    00635     return( 0 );
    00636 }
    00637
    00638 static int ssl_parse_client_key_exchange( ssl_context *ssl )
    00639 {
    00640     int ret, i, n;
    00641
    00642     SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
    00643
    00644     if( ( ret = ssl_read_record( ssl ) ) != 0 )
    00645     {
    00646         SSL_DEBUG_RET( 1, "ssl_read_record", ret );
    00647         return( ret );
    00648     }
    00649
    00650     if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
    00651     {
    00652         SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
    00653         return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
    00654     }
    00655
    00656     if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
    00657     {
    00658         SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
    00659         return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
    00660     }
    00661
    00662     if( ssl->session->cipher == SSL_EDH_RSA_DES_168_SHA ||
    00663         ssl->session->cipher == SSL_EDH_RSA_AES_256_SHA ||
    00664     ssl->session->cipher == SSL_EDH_RSA_CAMELLIA_256_SHA)
    00665     {
    00666 #if !defined(POLARSSL_DHM_C)
    00667         SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) );
    00668         return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
    00669 #else
    00670         /*
    00671          * Receive G^Y mod P, premaster = (G^Y)^X mod P
    00672          */
    00673         n = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
    00674
    00675         if( n < 1 || n > ssl->dhm_ctx.len ||
    00676             n + 6 != ssl->in_hslen )
    00677         {
    00678             SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
    00679             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
    00680         }
    00681
    00682         if( ( ret = dhm_read_public( &ssl->dhm_ctx,
    00683                                       ssl->in_msg + 6, n ) ) != 0 )
    00684         {
    00685             SSL_DEBUG_RET( 1, "dhm_read_public", ret );
    00686             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE | ret );
    00687         }
    00688
    00689         SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->dhm_ctx.GY );
    00690
    00691         ssl->pmslen = ssl->dhm_ctx.len;
    00692
    00693         if( ( ret = dhm_calc_secret( &ssl->dhm_ctx,
    00694                      ssl->premaster, &ssl->pmslen ) ) != 0 )
    00695         {
    00696             SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
    00697             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE | ret );
    00698         }
    00699
    00700         SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->dhm_ctx.K  );
    00701 #endif
    00702     }
    00703     else
    00704     {
    00705         /*
    00706          * Decrypt the premaster using own private RSA key
    00707          */
    00708         i = 4;
    00709         n = ssl->rsa_key->len;
    00710         ssl->pmslen = 48;
    00711
    00712         if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
    00713         {
    00714             i += 2;
    00715             if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
    00716                 ssl->in_msg[5] != ( ( n      ) & 0xFF ) )
    00717             {
    00718                 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
    00719                 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
    00720             }
    00721         }
    00722
    00723         if( ssl->in_hslen != i + n )
    00724         {
    00725             SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
    00726             return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
    00727         }
    00728
    00729         ret = rsa_pkcs1_decrypt( ssl->rsa_key, RSA_PRIVATE, &ssl->pmslen,
    00730                                  ssl->in_msg + i, ssl->premaster,
    00731                  sizeof(ssl->premaster) );
    00732
    00733         if( ret != 0 || ssl->pmslen != 48 ||
    00734             ssl->premaster[0] != ssl->max_major_ver ||
    00735             ssl->premaster[1] != ssl->max_minor_ver )
    00736         {
    00737             SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
    00738
    00739             /*
    00740              * Protection against Bleichenbacher's attack:
    00741              * invalid PKCS#1 v1.5 padding must not cause
    00742              * the connection to end immediately; instead,
    00743              * send a bad_record_mac later in the handshake.
    00744              */
    00745             ssl->pmslen = 48;
    00746
    00747             for( i = 0; i < ssl->pmslen; i++ )
    00748                 ssl->premaster[i] = (unsigned char) ssl->f_rng( ssl->p_rng );
    00749         }
    00750     }
    00751
    00752     ssl_derive_keys( ssl );
    00753
    00754     if( ssl->s_set != NULL )
    00755         ssl->s_set( ssl );
    00756
    00757     ssl->state++;
    00758
    00759     SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
    00760
    00761     return( 0 );
    00762 }
    00763
    00764 static int ssl_parse_certificate_verify( ssl_context *ssl )
    00765 {
    00766     int n1, n2, ret;
    00767     unsigned char hash[36];
    00768
    00769     SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
    00770
    00771     if( ssl->peer_cert == NULL )
    00772     {
    00773         SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
    00774         ssl->state++;
    00775         return( 0 );
    00776     }
    00777
    00778     ssl_calc_verify( ssl, hash );
    00779
    00780     if( ( ret = ssl_read_record( ssl ) ) != 0 )
    00781     {
    00782         SSL_DEBUG_RET( 1, "ssl_read_record", ret );
    00783         return( ret );
    00784     }
    00785
    00786     ssl->state++;
    00787
    00788     if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
    00789     {
    00790         SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
    00791         return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
    00792     }
    00793
    00794     if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
    00795     {
    00796         SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
    00797         return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
    00798     }
    00799
    00800     n1 = ssl->peer_cert->rsa.len;
    00801     n2 = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
    00802
    00803     if( n1 + 6 != ssl->in_hslen || n1 != n2 )
    00804     {
    00805         SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
    00806         return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
    00807     }
    00808
    00809     ret = rsa_pkcs1_verify( &ssl->peer_cert->rsa, RSA_PUBLIC,
    00810                             SIG_RSA_RAW, 36, hash, ssl->in_msg + 6 );
    00811     if( ret != 0 )
    00812     {
    00813         SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret );
    00814         return( ret );
    00815     }
    00816
    00817     SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
    00818
    00819     return( 0 );
    00820 }
    00821
    00822 /*
    00823  * SSL handshake -- server side
    00824  */
    00825 int ssl_handshake_server( ssl_context *ssl )
    00826 {
    00827     int ret = 0;
    00828
    00829     SSL_DEBUG_MSG( 2, ( "=> handshake server" ) );
    00830
    00831     while( ssl->state != SSL_HANDSHAKE_OVER )
    00832     {
    00833         SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
    00834
    00835         if( ( ret = ssl_flush_output( ssl ) ) != 0 )
    00836             break;
    00837
    00838         switch( ssl->state )
    00839         {
    00840             case SSL_HELLO_REQUEST:
    00841                 ssl->state = SSL_CLIENT_HELLO;
    00842                 break;
    00843
    00844             /*
    00845              *  <==   ClientHello
    00846              */
    00847             case SSL_CLIENT_HELLO:
    00848                 ret = ssl_parse_client_hello( ssl );
    00849                 break;
    00850
    00851             /*
    00852              *  ==>   ServerHello
    00853              *        Certificate
    00854              *      ( ServerKeyExchange  )
    00855              *      ( CertificateRequest )
    00856              *        ServerHelloDone
    00857              */
    00858             case SSL_SERVER_HELLO:
    00859                 ret = ssl_write_server_hello( ssl );
    00860                 break;
    00861
    00862             case SSL_SERVER_CERTIFICATE:
    00863                 ret = ssl_write_certificate( ssl );
    00864                 break;
    00865
    00866             case SSL_SERVER_KEY_EXCHANGE:
    00867                 ret = ssl_write_server_key_exchange( ssl );
    00868                 break;
    00869
    00870             case SSL_CERTIFICATE_REQUEST:
    00871                 ret = ssl_write_certificate_request( ssl );
    00872                 break;
    00873
    00874             case SSL_SERVER_HELLO_DONE:
    00875                 ret = ssl_write_server_hello_done( ssl );
    00876                 break;
    00877
    00878             /*
    00879              *  <== ( Certificate/Alert  )
    00880              *        ClientKeyExchange
    00881              *      ( CertificateVerify  )
    00882              *        ChangeCipherSpec
    00883              *        Finished
    00884              */
    00885             case SSL_CLIENT_CERTIFICATE:
    00886                 ret = ssl_parse_certificate( ssl );
    00887                 break;
    00888
    00889             case SSL_CLIENT_KEY_EXCHANGE:
    00890                 ret = ssl_parse_client_key_exchange( ssl );
    00891                 break;
    00892
    00893             case SSL_CERTIFICATE_VERIFY:
    00894                 ret = ssl_parse_certificate_verify( ssl );
    00895                 break;
    00896
    00897             case SSL_CLIENT_CHANGE_CIPHER_SPEC:
    00898                 ret = ssl_parse_change_cipher_spec( ssl );
    00899                 break;
    00900
    00901             case SSL_CLIENT_FINISHED:
    00902                 ret = ssl_parse_finished( ssl );
    00903                 break;
    00904
    00905             /*
    00906              *  ==>   ChangeCipherSpec
    00907              *        Finished
    00908              */
    00909             case SSL_SERVER_CHANGE_CIPHER_SPEC:
    00910                 ret = ssl_write_change_cipher_spec( ssl );
    00911                 break;
    00912
    00913             case SSL_SERVER_FINISHED:
    00914                 ret = ssl_write_finished( ssl );
    00915                 break;
    00916
    00917             case SSL_FLUSH_BUFFERS:
    00918                 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
    00919                 ssl->state = SSL_HANDSHAKE_OVER;
    00920                 break;
    00921
    00922             default:
    00923                 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
    00924                 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
    00925         }
    00926
    00927         if( ret != 0 )
    00928             break;
    00929     }
    00930
    00931     SSL_DEBUG_MSG( 2, ( "<= handshake server" ) );
    00932
    00933     return( ret );
    00934 }
    00935
    00936 #endif