~ubuntu-branches/ubuntu/oneiric/openssl/oneiric

« back to all changes in this revision

Viewing changes to ssl/s3_srvr.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-05-01 23:51:53 UTC
  • mfrom: (11.1.20 sid)
  • Revision ID: james.westby@ubuntu.com-20110501235153-bjcxitndquaezb68
Tags: 1.0.0d-2ubuntu1
* Resynchronise with Debian (LP: #675566).  Remaining changes:
  - debian/libssl1.0.0.postinst:
    + Display a system restart required notification bubble on libssl1.0.0
      upgrade.
    + Use a different priority for libssl1.0.0/restart-services depending
      on whether a desktop, or server dist-upgrade is being performed.
  - debian/{libssl1.0.0-udeb.dirs, control, rules}: Create
    libssl1.0.0-udeb, for the benefit of wget-udeb (no wget-udeb package
    in Debian).
  - debian/{libcrypto1.0.0-udeb.dirs, libssl1.0.0.dirs, libssl1.0.0.files,
    rules}: Move runtime libraries to /lib, for the benefit of
    wpasupplicant.
  - debian/patches/aesni.patch: Backport Intel AES-NI support, now from
    http://rt.openssl.org/Ticket/Display.html?id=2065 rather than the
    0.9.8 variant.
  - debian/patches/Bsymbolic-functions.patch: Link using
    -Bsymbolic-functions.
  - debian/patches/perlpath-quilt.patch: Don't change perl #! paths under
    .pc.
  - debian/rules:
    + Don't run 'make test' when cross-building.
    + Use host compiler when cross-building.  Patch from Neil Williams.
    + Don't build for processors no longer supported: i486, i586 (on
      i386), v8 (on sparc).
    + Fix Makefile to properly clean up libs/ dirs in clean target.
    + Replace duplicate files in the doc directory with symlinks.
* Update architectures affected by Bsymbolic-functions.patch.
* Drop debian/patches/no-sslv2.patch; Debian now adds the 'no-ssl2'
  configure option, which compiles out SSLv2 support entirely, so this is
  no longer needed.
* Drop openssl-doc in favour of the libssl-doc package introduced by
  Debian.  Add Conflicts/Replaces until the next LTS release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ssl/s3_srvr.c */
 
1
/* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
2
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
3
 * All rights reserved.
4
4
 *
56
56
 * [including the GNU Public Licence.]
57
57
 */
58
58
/* ====================================================================
59
 
 * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
 
59
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60
60
 *
61
61
 * Redistribution and use in source and binary forms, with or without
62
62
 * modification, are permitted provided that the following conditions
121
121
 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122
122
 *
123
123
 */
 
124
/* ====================================================================
 
125
 * Copyright 2005 Nokia. All rights reserved.
 
126
 *
 
127
 * The portions of the attached software ("Contribution") is developed by
 
128
 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
 
129
 * license.
 
130
 *
 
131
 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
 
132
 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
 
133
 * support (see RFC 4279) to OpenSSL.
 
134
 *
 
135
 * No patent licenses or other rights except those expressly stated in
 
136
 * the OpenSSL open source license shall be deemed granted or received
 
137
 * expressly, by implication, estoppel, or otherwise.
 
138
 *
 
139
 * No assurances are provided by Nokia that the Contribution does not
 
140
 * infringe the patent or other intellectual property rights of any third
 
141
 * party or that the license provides you with all the necessary rights
 
142
 * to make use of the Contribution.
 
143
 *
 
144
 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
 
145
 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
 
146
 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
 
147
 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
 
148
 * OTHERWISE.
 
149
 */
124
150
 
125
151
#define REUSE_CIPHER_BUG
126
152
#define NETSCAPE_HANG_BUG
143
169
#endif
144
170
#include <openssl/md5.h>
145
171
 
146
 
static SSL_METHOD *ssl3_get_server_method(int ver);
147
 
#ifndef OPENSSL_NO_ECDH
148
 
static int nid2curve_id(int nid);
149
 
#endif
 
172
static const SSL_METHOD *ssl3_get_server_method(int ver);
150
173
 
151
 
static SSL_METHOD *ssl3_get_server_method(int ver)
 
174
static const SSL_METHOD *ssl3_get_server_method(int ver)
152
175
        {
153
176
        if (ver == SSL3_VERSION)
154
177
                return(SSLv3_server_method());
164
187
int ssl3_accept(SSL *s)
165
188
        {
166
189
        BUF_MEM *buf;
167
 
        unsigned long l,Time=(unsigned long)time(NULL);
 
190
        unsigned long alg_k,Time=(unsigned long)time(NULL);
168
191
        void (*cb)(const SSL *ssl,int type,int val)=NULL;
169
192
        int ret= -1;
170
193
        int new_state,state,skip=0;
292
315
                        s->shutdown=0;
293
316
                        ret=ssl3_get_client_hello(s);
294
317
                        if (ret <= 0) goto end;
 
318
                        
295
319
                        s->new_session = 2;
296
320
                        s->state=SSL3_ST_SW_SRVR_HELLO_A;
297
321
                        s->init_num=0;
320
344
 
321
345
                case SSL3_ST_SW_CERT_A:
322
346
                case SSL3_ST_SW_CERT_B:
323
 
                        /* Check if it is anon DH or anon ECDH or KRB5 */
324
 
                        if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)
325
 
                                && !(s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
 
347
                        /* Check if it is anon DH or anon ECDH, */
 
348
                        /* normal PSK or KRB5 */
 
349
                        if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
 
350
                                && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
 
351
                                && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
326
352
                                {
327
353
                                ret=ssl3_send_server_certificate(s);
328
354
                                if (ret <= 0) goto end;
349
375
 
350
376
                case SSL3_ST_SW_KEY_EXCH_A:
351
377
                case SSL3_ST_SW_KEY_EXCH_B:
352
 
                        l=s->s3->tmp.new_cipher->algorithms;
 
378
                        alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
353
379
 
354
380
                        /* clear this, it may get reset by
355
381
                         * send_server_key_exchange */
356
382
                        if ((s->options & SSL_OP_EPHEMERAL_RSA)
357
383
#ifndef OPENSSL_NO_KRB5
358
 
                                && !(l & SSL_KRB5)
 
384
                                && !(alg_k & SSL_kKRB5)
359
385
#endif /* OPENSSL_NO_KRB5 */
360
386
                                )
361
387
                                /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
370
396
                        /* only send if a DH key exchange, fortezza or
371
397
                         * RSA but we have a sign only certificate
372
398
                         *
 
399
                         * PSK: may send PSK identity hints
 
400
                         *
373
401
                         * For ECC ciphersuites, we send a serverKeyExchange
374
402
                         * message only if the cipher suite is either
375
403
                         * ECDH-anon or ECDHE. In other cases, the
376
 
                         * server certificate contains the server's 
 
404
                         * server certificate contains the server's
377
405
                         * public key for key exchange.
378
406
                         */
379
407
                        if (s->s3->tmp.use_rsa_tmp
380
 
                            || (l & SSL_kECDHE)
381
 
                            || (l & (SSL_DH|SSL_kFZA))
382
 
                            || ((l & SSL_kRSA)
 
408
                        /* PSK: send ServerKeyExchange if PSK identity
 
409
                         * hint if provided */
 
410
#ifndef OPENSSL_NO_PSK
 
411
                            || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
 
412
#endif
 
413
                            || (alg_k & (SSL_kDHr|SSL_kDHd|SSL_kEDH))
 
414
                            || (alg_k & SSL_kEECDH)
 
415
                            || ((alg_k & SSL_kRSA)
383
416
                                && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
384
417
                                    || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
385
418
                                        && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
409
442
                                /* never request cert in anonymous ciphersuites
410
443
                                 * (see section "Certificate request" in SSL 3 drafts
411
444
                                 * and in RFC 2246): */
412
 
                                ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
 
445
                                ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
413
446
                                 /* ... except when the application insists on verification
414
447
                                  * (against the specs, but s3_clnt.c accepts this for SSL 3) */
415
448
                                 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
416
 
                                 /* never request cert in Kerberos ciphersuites */
417
 
                                (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
 
449
                                 /* never request cert in Kerberos ciphersuites */
 
450
                                (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
 
451
                                /* With normal PSK Certificates and
 
452
                                 * Certificate Requests are omitted */
 
453
                                || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
418
454
                                {
419
455
                                /* no cert request */
420
456
                                skip=1;
490
526
                case SSL3_ST_SR_KEY_EXCH_A:
491
527
                case SSL3_ST_SR_KEY_EXCH_B:
492
528
                        ret=ssl3_get_client_key_exchange(s);
493
 
                        if (ret <= 0) 
 
529
                        if (ret <= 0)
494
530
                                goto end;
495
531
                        if (ret == 2)
496
532
                                {
498
534
                                 * the client sends its ECDH pub key in
499
535
                                 * a certificate, the CertificateVerify
500
536
                                 * message is not sent.
 
537
                                 * Also for GOST ciphersuites when
 
538
                                 * the client uses its key from the certificate
 
539
                                 * for key exchange.
501
540
                                 */
502
541
                                s->state=SSL3_ST_SR_FINISHED_A;
503
542
                                s->init_num = 0;
504
543
                                }
505
 
                        else   
 
544
                        else
506
545
                                {
 
546
                                int offset=0;
 
547
                                int dgst_num;
 
548
 
507
549
                                s->state=SSL3_ST_SR_CERT_VRFY_A;
508
550
                                s->init_num=0;
509
551
 
510
552
                                /* We need to get hashes here so if there is
511
553
                                 * a client cert, it can be verified
512
 
                                 */ 
513
 
                                s->method->ssl3_enc->cert_verify_mac(s,
514
 
                                    &(s->s3->finish_dgst1),
515
 
                                    &(s->s3->tmp.cert_verify_md[0]));
516
 
                                s->method->ssl3_enc->cert_verify_mac(s,
517
 
                                    &(s->s3->finish_dgst2),
518
 
                                    &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
 
554
                                 * FIXME - digest processing for CertificateVerify
 
555
                                 * should be generalized. But it is next step
 
556
                                 */
 
557
                                if (s->s3->handshake_buffer)
 
558
                                        if (!ssl3_digest_cached_records(s))
 
559
                                                return -1;
 
560
                                for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++)    
 
561
                                        if (s->s3->handshake_dgst[dgst_num]) 
 
562
                                                {
 
563
                                                int dgst_size;
 
564
 
 
565
                                                s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset]));
 
566
                                                dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
 
567
                                                if (dgst_size < 0)
 
568
                                                        {
 
569
                                                        ret = -1;
 
570
                                                        goto end;
 
571
                                                        }
 
572
                                                offset+=dgst_size;
 
573
                                                }               
519
574
                                }
520
575
                        break;
521
576
 
535
590
                        ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
536
591
                                SSL3_ST_SR_FINISHED_B);
537
592
                        if (ret <= 0) goto end;
538
 
                        if (s->hit)
539
 
                                s->state=SSL_ST_OK;
540
593
#ifndef OPENSSL_NO_TLSEXT
541
 
                        else if (s->tlsext_ticket_expected)
 
594
                        if (s->tlsext_ticket_expected)
542
595
                                s->state=SSL3_ST_SW_SESSION_TICKET_A;
 
596
                        else if (s->hit)
 
597
                                s->state=SSL_ST_OK;
 
598
#else
 
599
                        if (s->hit)
 
600
                                s->state=SSL_ST_OK;
543
601
#endif
544
602
                        else
545
603
                                s->state=SSL3_ST_SW_CHANGE_A;
769
827
            (s->version != DTLS1_VERSION && s->client_version < s->version))
770
828
                {
771
829
                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
772
 
                if ((s->client_version>>8) == SSL3_VERSION_MAJOR) 
 
830
                if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
773
831
                        {
774
832
                        /* similar to ssl3_get_record, send alert using remote version number */
775
833
                        s->version = s->client_version;
995
1053
                        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
996
1054
                        goto err;
997
1055
                }
 
1056
 
 
1057
        /* Check if we want to use external pre-shared secret for this
 
1058
         * handshake for not reused session only. We need to generate
 
1059
         * server_random before calling tls_session_secret_cb in order to allow
 
1060
         * SessionTicket processing to use it in key derivation. */
 
1061
        {
 
1062
                unsigned long Time;
 
1063
                unsigned char *pos;
 
1064
                Time=(unsigned long)time(NULL);                 /* Time */
 
1065
                pos=s->s3->server_random;
 
1066
                l2n(Time,pos);
 
1067
                if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
 
1068
                        {
 
1069
                        al=SSL_AD_INTERNAL_ERROR;
 
1070
                        goto f_err;
 
1071
                        }
 
1072
        }
 
1073
 
 
1074
        if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
 
1075
                {
 
1076
                SSL_CIPHER *pref_cipher=NULL;
 
1077
 
 
1078
                s->session->master_key_length=sizeof(s->session->master_key);
 
1079
                if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
 
1080
                        ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
 
1081
                        {
 
1082
                        s->hit=1;
 
1083
                        s->session->ciphers=ciphers;
 
1084
                        s->session->verify_result=X509_V_OK;
 
1085
 
 
1086
                        ciphers=NULL;
 
1087
 
 
1088
                        /* check if some cipher was preferred by call back */
 
1089
                        pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
 
1090
                        if (pref_cipher == NULL)
 
1091
                                {
 
1092
                                al=SSL_AD_HANDSHAKE_FAILURE;
 
1093
                                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
 
1094
                                goto f_err;
 
1095
                                }
 
1096
 
 
1097
                        s->session->cipher=pref_cipher;
 
1098
 
 
1099
                        if (s->cipher_list)
 
1100
                                sk_SSL_CIPHER_free(s->cipher_list);
 
1101
 
 
1102
                        if (s->cipher_list_by_id)
 
1103
                                sk_SSL_CIPHER_free(s->cipher_list_by_id);
 
1104
 
 
1105
                        s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
 
1106
                        s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
 
1107
                        }
 
1108
                }
998
1109
#endif
 
1110
 
999
1111
        /* Worst case, we will use the NULL compression, but if we have other
1000
1112
         * options, we will now look for them.  We have i-1 compression
1001
1113
         * algorithms from the client, starting at q. */
1002
1114
        s->s3->tmp.new_compression=NULL;
1003
1115
#ifndef OPENSSL_NO_COMP
1004
 
        if (s->ctx->comp_methods != NULL)
 
1116
        /* This only happens if we have a cache hit */
 
1117
        if (s->session->compress_meth != 0)
 
1118
                {
 
1119
                int m, comp_id = s->session->compress_meth;
 
1120
                /* Perform sanity checks on resumed compression algorithm */
 
1121
                /* Can't disable compression */
 
1122
                if (s->options & SSL_OP_NO_COMPRESSION)
 
1123
                        {
 
1124
                        al=SSL_AD_INTERNAL_ERROR;
 
1125
                        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
 
1126
                        goto f_err;
 
1127
                        }
 
1128
                /* Look for resumed compression method */
 
1129
                for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++)
 
1130
                        {
 
1131
                        comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
 
1132
                        if (comp_id == comp->id)
 
1133
                                {
 
1134
                                s->s3->tmp.new_compression=comp;
 
1135
                                break;
 
1136
                                }
 
1137
                        }
 
1138
                if (s->s3->tmp.new_compression == NULL)
 
1139
                        {
 
1140
                        al=SSL_AD_INTERNAL_ERROR;
 
1141
                        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INVALID_COMPRESSION_ALGORITHM);
 
1142
                        goto f_err;
 
1143
                        }
 
1144
                /* Look for resumed method in compression list */
 
1145
                for (m = 0; m < i; m++)
 
1146
                        {
 
1147
                        if (q[m] == comp_id)
 
1148
                                break;
 
1149
                        }
 
1150
                if (m >= i)
 
1151
                        {
 
1152
                        al=SSL_AD_ILLEGAL_PARAMETER;
 
1153
                        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
 
1154
                        goto f_err;
 
1155
                        }
 
1156
                }
 
1157
        else if (s->hit)
 
1158
                comp = NULL;
 
1159
        else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods)
1005
1160
                { /* See if we have a match */
1006
1161
                int m,nn,o,v,done=0;
1007
1162
 
1025
1180
                else
1026
1181
                        comp=NULL;
1027
1182
                }
1028
 
#endif
1029
 
 
1030
 
        /* TLS does not mind if there is extra stuff */
1031
 
#if 0   /* SSL 3.0 does not mind either, so we should disable this test
1032
 
         * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b,
1033
 
         * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */
1034
 
        if (s->version == SSL3_VERSION)
 
1183
#else
 
1184
        /* If compression is disabled we'd better not try to resume a session
 
1185
         * using compression.
 
1186
         */
 
1187
        if (s->session->compress_meth != 0)
1035
1188
                {
1036
 
                if (p < (d+n))
1037
 
                        {
1038
 
                        /* wrong number of bytes,
1039
 
                         * there could be more to follow */
1040
 
                        al=SSL_AD_DECODE_ERROR;
1041
 
                        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
1042
 
                        goto f_err;
1043
 
                        }
 
1189
                al=SSL_AD_INTERNAL_ERROR;
 
1190
                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
 
1191
                goto f_err;
1044
1192
                }
1045
1193
#endif
1046
1194
 
1089
1237
                        for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1090
1238
                                {
1091
1239
                                c=sk_SSL_CIPHER_value(sk,i);
1092
 
                                if (c->algorithms & SSL_eNULL)
 
1240
                                if (c->algorithm_enc & SSL_eNULL)
1093
1241
                                        nc=c;
1094
1242
                                if (SSL_C_IS_EXPORT(c))
1095
1243
                                        ec=c;
1105
1253
#endif
1106
1254
                s->s3->tmp.new_cipher=s->session->cipher;
1107
1255
                }
 
1256
 
 
1257
        if (!ssl3_digest_cached_records(s))
 
1258
                goto f_err;
1108
1259
        
1109
1260
        /* we now have the following setup. 
1110
1261
         * client_random
1133
1284
        unsigned char *buf;
1134
1285
        unsigned char *p,*d;
1135
1286
        int i,sl;
1136
 
        unsigned long l,Time;
 
1287
        unsigned long l;
 
1288
#ifdef OPENSSL_NO_TLSEXT
 
1289
        unsigned long Time;
 
1290
#endif
1137
1291
 
1138
1292
        if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
1139
1293
                {
1140
1294
                buf=(unsigned char *)s->init_buf->data;
 
1295
#ifdef OPENSSL_NO_TLSEXT
1141
1296
                p=s->s3->server_random;
 
1297
                /* Generate server_random if it was not needed previously */
1142
1298
                Time=(unsigned long)time(NULL);                 /* Time */
1143
1299
                l2n(Time,p);
1144
1300
                if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
1145
1301
                        return -1;
 
1302
#endif
1146
1303
                /* Do the message type and length last */
1147
1304
                d=p= &(buf[4]);
1148
1305
 
1196
1353
                        *(p++)=s->s3->tmp.new_compression->id;
1197
1354
#endif
1198
1355
#ifndef OPENSSL_NO_TLSEXT
 
1356
                if (ssl_prepare_serverhello_tlsext(s) <= 0)
 
1357
                        {
 
1358
                        SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
 
1359
                        return -1;
 
1360
                        }
1199
1361
                if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
1200
1362
                        {
1201
1363
                        SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
1275
1437
        EVP_MD_CTX_init(&md_ctx);
1276
1438
        if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1277
1439
                {
1278
 
                type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
 
1440
                type=s->s3->tmp.new_cipher->algorithm_mkey;
1279
1441
                cert=s->cert;
1280
1442
 
1281
1443
                buf=s->init_buf;
1370
1532
                else 
1371
1533
#endif
1372
1534
#ifndef OPENSSL_NO_ECDH
1373
 
                        if (type & SSL_kECDHE)
 
1535
                        if (type & SSL_kEECDH)
1374
1536
                        {
1375
1537
                        const EC_GROUP *group;
1376
1538
 
1440
1602
                         * supported named curves, curve_id is non-zero.
1441
1603
                         */
1442
1604
                        if ((curve_id = 
1443
 
                            nid2curve_id(EC_GROUP_get_curve_name(group)))
 
1605
                            tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1444
1606
                            == 0)
1445
1607
                                {
1446
1608
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1497
1659
                        }
1498
1660
                else 
1499
1661
#endif /* !OPENSSL_NO_ECDH */
 
1662
#ifndef OPENSSL_NO_PSK
 
1663
                        if (type & SSL_kPSK)
 
1664
                                {
 
1665
                                /* reserve size for record length and PSK identity hint*/
 
1666
                                n+=2+strlen(s->ctx->psk_identity_hint);
 
1667
                                }
 
1668
                        else
 
1669
#endif /* !OPENSSL_NO_PSK */
1500
1670
                        {
1501
1671
                        al=SSL_AD_HANDSHAKE_FAILURE;
1502
1672
                        SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1508
1678
                        n+=2+nr[i];
1509
1679
                        }
1510
1680
 
1511
 
                if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
 
1681
                if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
 
1682
                        && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
1512
1683
                        {
1513
1684
                        if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1514
1685
                                == NULL)
1540
1711
                        }
1541
1712
 
1542
1713
#ifndef OPENSSL_NO_ECDH
1543
 
                if (type & SSL_kECDHE) 
 
1714
                if (type & SSL_kEECDH) 
1544
1715
                        {
1545
1716
                        /* XXX: For now, we only support named (not generic) curves.
1546
1717
                         * In this situation, the serverKeyExchange message has:
1564
1735
                        }
1565
1736
#endif
1566
1737
 
 
1738
#ifndef OPENSSL_NO_PSK
 
1739
                if (type & SSL_kPSK)
 
1740
                        {
 
1741
                        /* copy PSK identity hint */
 
1742
                        s2n(strlen(s->ctx->psk_identity_hint), p); 
 
1743
                        strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
 
1744
                        p+=strlen(s->ctx->psk_identity_hint);
 
1745
                        }
 
1746
#endif
 
1747
 
1567
1748
                /* not anonymous */
1568
1749
                if (pkey != NULL)
1569
1750
                        {
1576
1757
                                j=0;
1577
1758
                                for (num=2; num > 0; num--)
1578
1759
                                        {
1579
 
                                        EVP_MD_CTX_set_flags(&md_ctx,
1580
 
                                                EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1581
1760
                                        EVP_DigestInit_ex(&md_ctx,(num == 2)
1582
1761
                                                ?s->ctx->md5:s->ctx->sha1, NULL);
1583
1762
                                        EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1761
1940
        {
1762
1941
        int i,al,ok;
1763
1942
        long n;
1764
 
        unsigned long l;
 
1943
        unsigned long alg_k;
1765
1944
        unsigned char *p;
1766
1945
#ifndef OPENSSL_NO_RSA
1767
1946
        RSA *rsa=NULL;
1772
1951
        DH *dh_srvr;
1773
1952
#endif
1774
1953
#ifndef OPENSSL_NO_KRB5
1775
 
        KSSL_ERR kssl_err;
 
1954
        KSSL_ERR kssl_err;
1776
1955
#endif /* OPENSSL_NO_KRB5 */
1777
1956
 
1778
1957
#ifndef OPENSSL_NO_ECDH
1792
1971
        if (!ok) return((int)n);
1793
1972
        p=(unsigned char *)s->init_msg;
1794
1973
 
1795
 
        l=s->s3->tmp.new_cipher->algorithms;
 
1974
        alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1796
1975
 
1797
1976
#ifndef OPENSSL_NO_RSA
1798
 
        if (l & SSL_kRSA)
 
1977
        if (alg_k & SSL_kRSA)
1799
1978
                {
1800
1979
                /* FIX THIS UP EAY EAY EAY EAY */
1801
1980
                if (s->s3->tmp.use_rsa_tmp)
1826
2005
                        rsa=pkey->pkey.rsa;
1827
2006
                        }
1828
2007
 
1829
 
                /* TLS and [incidentally] DTLS, including pre-0.9.8f */
1830
 
                if (s->version > SSL3_VERSION &&
1831
 
                    s->client_version != DTLS1_BAD_VER)
 
2008
                /* TLS and [incidentally] DTLS{0xFEFF} */
 
2009
                if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER)
1832
2010
                        {
1833
2011
                        n2s(p,i);
1834
2012
                        if (n != i+2)
1902
2080
        else
1903
2081
#endif
1904
2082
#ifndef OPENSSL_NO_DH
1905
 
                if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
 
2083
                if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1906
2084
                {
1907
2085
                n2s(p,i);
1908
2086
                if (n != i+2)
1965
2143
        else
1966
2144
#endif
1967
2145
#ifndef OPENSSL_NO_KRB5
1968
 
        if (l & SSL_kKRB5)
1969
 
                {
1970
 
                krb5_error_code         krb5rc;
 
2146
        if (alg_k & SSL_kKRB5)
 
2147
                {
 
2148
                krb5_error_code         krb5rc;
1971
2149
                krb5_data               enc_ticket;
1972
2150
                krb5_data               authenticator;
1973
2151
                krb5_data               enc_pms;
1974
 
                KSSL_CTX                *kssl_ctx = s->kssl_ctx;
 
2152
                KSSL_CTX                *kssl_ctx = s->kssl_ctx;
1975
2153
                EVP_CIPHER_CTX          ciph_ctx;
1976
 
                EVP_CIPHER              *enc = NULL;
 
2154
                const EVP_CIPHER        *enc = NULL;
1977
2155
                unsigned char           iv[EVP_MAX_IV_LENGTH];
1978
2156
                unsigned char           pms[SSL_MAX_MASTER_KEY_LENGTH
1979
 
                                               + EVP_MAX_BLOCK_LENGTH];
1980
 
                int                     padl, outl;
 
2157
                                               + EVP_MAX_BLOCK_LENGTH];
 
2158
                int                  padl, outl;
1981
2159
                krb5_timestamp          authtime = 0;
1982
2160
                krb5_ticket_times       ttimes;
1983
2161
 
1984
2162
                EVP_CIPHER_CTX_init(&ciph_ctx);
1985
2163
 
1986
 
                if (!kssl_ctx)  kssl_ctx = kssl_ctx_new();
 
2164
                if (!kssl_ctx)  kssl_ctx = kssl_ctx_new();
1987
2165
 
1988
2166
                n2s(p,i);
1989
2167
                enc_ticket.length = i;
1990
2168
 
1991
 
                if (n < (int)enc_ticket.length + 6)
 
2169
                if (n < (long)(enc_ticket.length + 6))
1992
2170
                        {
1993
2171
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1994
2172
                                SSL_R_DATA_LENGTH_TOO_LONG);
2001
2179
                n2s(p,i);
2002
2180
                authenticator.length = i;
2003
2181
 
2004
 
                if (n < (int)(enc_ticket.length + authenticator.length) + 6)
 
2182
                if (n < (long)(enc_ticket.length + authenticator.length + 6))
2005
2183
                        {
2006
2184
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2007
2185
                                SSL_R_DATA_LENGTH_TOO_LONG);
2034
2212
                        goto err;
2035
2213
                        }
2036
2214
 
2037
 
                if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
 
2215
                if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2038
2216
                                        &kssl_err)) != 0)
2039
 
                        {
 
2217
                        {
2040
2218
#ifdef KSSL_DEBUG
2041
 
                        printf("kssl_sget_tkt rtn %d [%d]\n",
2042
 
                                krb5rc, kssl_err.reason);
2043
 
                        if (kssl_err.text)
2044
 
                                printf("kssl_err text= %s\n", kssl_err.text);
 
2219
                        printf("kssl_sget_tkt rtn %d [%d]\n",
 
2220
                                krb5rc, kssl_err.reason);
 
2221
                        if (kssl_err.text)
 
2222
                                printf("kssl_err text= %s\n", kssl_err.text);
2045
2223
#endif  /* KSSL_DEBUG */
2046
 
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2047
 
                                kssl_err.reason);
2048
 
                        goto err;
2049
 
                        }
 
2224
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2225
                                kssl_err.reason);
 
2226
                        goto err;
 
2227
                        }
2050
2228
 
2051
2229
                /*  Note: no authenticator is not considered an error,
2052
2230
                **  but will return authtime == 0.
2055
2233
                                        &authtime, &kssl_err)) != 0)
2056
2234
                        {
2057
2235
#ifdef KSSL_DEBUG
2058
 
                        printf("kssl_check_authent rtn %d [%d]\n",
2059
 
                                krb5rc, kssl_err.reason);
2060
 
                        if (kssl_err.text)
2061
 
                                printf("kssl_err text= %s\n", kssl_err.text);
 
2236
                        printf("kssl_check_authent rtn %d [%d]\n",
 
2237
                                krb5rc, kssl_err.reason);
 
2238
                        if (kssl_err.text)
 
2239
                                printf("kssl_err text= %s\n", kssl_err.text);
2062
2240
#endif  /* KSSL_DEBUG */
2063
 
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2064
 
                                kssl_err.reason);
2065
 
                        goto err;
 
2241
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2242
                                kssl_err.reason);
 
2243
                        goto err;
2066
2244
                        }
2067
2245
 
2068
2246
                if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
2069
2247
                        {
2070
2248
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2071
 
                        goto err;
 
2249
                        goto err;
2072
2250
                        }
2073
2251
 
2074
2252
#ifdef KSSL_DEBUG
2075
 
                kssl_ctx_show(kssl_ctx);
 
2253
                kssl_ctx_show(kssl_ctx);
2076
2254
#endif  /* KSSL_DEBUG */
2077
2255
 
2078
2256
                enc = kssl_map_enc(kssl_ctx->enctype);
2079
 
                if (enc == NULL)
2080
 
                    goto err;
 
2257
                if (enc == NULL)
 
2258
                    goto err;
2081
2259
 
2082
2260
                memset(iv, 0, sizeof iv);       /* per RFC 1510 */
2083
2261
 
2124
2302
                     * (Perhaps we should have a separate BUG value for the Kerberos cipher)
2125
2303
                     */
2126
2304
                    if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG))
2127
 
                        {
 
2305
                        {
2128
2306
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2129
2307
                               SSL_AD_DECODE_ERROR);
2130
2308
                        goto err;
2133
2311
 
2134
2312
                EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2135
2313
 
2136
 
                s->session->master_key_length=
2137
 
                        s->method->ssl3_enc->generate_master_secret(s,
2138
 
                                s->session->master_key, pms, outl);
2139
 
 
2140
 
                if (kssl_ctx->client_princ)
2141
 
                        {
2142
 
                        size_t len = strlen(kssl_ctx->client_princ);
2143
 
                        if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH ) 
2144
 
                                {
2145
 
                                s->session->krb5_client_princ_len = len;
2146
 
                                memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
2147
 
                                }
2148
 
                        }
2149
 
 
2150
 
 
2151
 
                /*  Was doing kssl_ctx_free() here,
 
2314
                s->session->master_key_length=
 
2315
                        s->method->ssl3_enc->generate_master_secret(s,
 
2316
                                s->session->master_key, pms, outl);
 
2317
 
 
2318
                if (kssl_ctx->client_princ)
 
2319
                        {
 
2320
                        size_t len = strlen(kssl_ctx->client_princ);
 
2321
                        if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH ) 
 
2322
                                {
 
2323
                                s->session->krb5_client_princ_len = len;
 
2324
                                memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
 
2325
                                }
 
2326
                        }
 
2327
 
 
2328
 
 
2329
                /*  Was doing kssl_ctx_free() here,
2152
2330
                **  but it caused problems for apache.
2153
 
                **  kssl_ctx = kssl_ctx_free(kssl_ctx);
2154
 
                **  if (s->kssl_ctx)  s->kssl_ctx = NULL;
2155
 
                */
2156
 
                }
 
2331
                **  kssl_ctx = kssl_ctx_free(kssl_ctx);
 
2332
                **  if (s->kssl_ctx)  s->kssl_ctx = NULL;
 
2333
                */
 
2334
                }
2157
2335
        else
2158
2336
#endif  /* OPENSSL_NO_KRB5 */
2159
2337
 
2160
2338
#ifndef OPENSSL_NO_ECDH
2161
 
                if ((l & SSL_kECDH) || (l & SSL_kECDHE))
 
2339
                if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2162
2340
                {
2163
2341
                int ret = 1;
2164
2342
                int field_size = 0;
2166
2344
                const EC_GROUP *group;
2167
2345
                const BIGNUM *priv_key;
2168
2346
 
2169
 
                /* initialize structures for server's ECDH key pair */
 
2347
                /* initialize structures for server's ECDH key pair */
2170
2348
                if ((srvr_ecdh = EC_KEY_new()) == NULL) 
2171
2349
                        {
2172
 
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2350
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2173
2351
                            ERR_R_MALLOC_FAILURE);
2174
 
                        goto err;
 
2352
                        goto err;
2175
2353
                        }
2176
2354
 
2177
2355
                /* Let's get server private key and group information */
2178
 
                if (l & SSL_kECDH) 
 
2356
                if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2179
2357
                        { 
2180
 
                        /* use the certificate */
 
2358
                        /* use the certificate */
2181
2359
                        tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2182
2360
                        }
2183
2361
                else
2207
2385
                        goto err;
2208
2386
                        }
2209
2387
 
2210
 
                if (n == 0L) 
2211
 
                        {
 
2388
                if (n == 0L) 
 
2389
                        {
2212
2390
                        /* Client Publickey was in Client Certificate */
2213
2391
 
2214
 
                         if (l & SSL_kECDHE) 
 
2392
                         if (alg_k & SSL_kEECDH)
2215
2393
                                 {
2216
2394
                                 al=SSL_AD_HANDSHAKE_FAILURE;
2217
2395
                                 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
2218
2396
                                 goto f_err;
2219
2397
                                 }
2220
 
                        if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
 
2398
                        if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
2221
2399
                            == NULL) || 
2222
2400
                            (clnt_pub_pkey->type != EVP_PKEY_EC))
2223
 
                                {
 
2401
                                {
2224
2402
                                /* XXX: For now, we do not support client
2225
2403
                                 * authentication using ECDH certificates
2226
2404
                                 * so this branch (n == 0L) of the code is
2232
2410
                                 * the two ECDH shares are for the same
2233
2411
                                 * group.
2234
2412
                                 */
2235
 
                                al=SSL_AD_HANDSHAKE_FAILURE;
2236
 
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2413
                                al=SSL_AD_HANDSHAKE_FAILURE;
 
2414
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2237
2415
                                    SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2238
 
                                goto f_err;
2239
 
                                }
 
2416
                                goto f_err;
 
2417
                                }
2240
2418
 
2241
2419
                        if (EC_POINT_copy(clnt_ecpoint,
2242
2420
                            EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
2245
2423
                                        ERR_R_EC_LIB);
2246
2424
                                goto err;
2247
2425
                                }
2248
 
                        ret = 2; /* Skip certificate verify processing */
2249
 
                        }
2250
 
                else
2251
 
                        {
 
2426
                        ret = 2; /* Skip certificate verify processing */
 
2427
                        }
 
2428
                else
 
2429
                        {
2252
2430
                        /* Get client's public key from encoded point
2253
2431
                         * in the ClientKeyExchange message.
2254
2432
                         */
2259
2437
                                goto err;
2260
2438
                                }
2261
2439
 
2262
 
                        /* Get encoded point length */
2263
 
                        i = *p; 
 
2440
                        /* Get encoded point length */
 
2441
                        i = *p; 
2264
2442
                        p += 1;
2265
 
                        if (EC_POINT_oct2point(group, 
 
2443
                        if (EC_POINT_oct2point(group, 
2266
2444
                            clnt_ecpoint, p, i, bn_ctx) == 0)
2267
2445
                                {
2268
2446
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2269
2447
                                    ERR_R_EC_LIB);
2270
2448
                                goto err;
2271
2449
                                }
2272
 
                        /* p is pointing to somewhere in the buffer
2273
 
                         * currently, so set it to the start 
2274
 
                         */ 
2275
 
                        p=(unsigned char *)s->init_buf->data;
2276
 
                        }
 
2450
                        /* p is pointing to somewhere in the buffer
 
2451
                         * currently, so set it to the start 
 
2452
                         */ 
 
2453
                        p=(unsigned char *)s->init_buf->data;
 
2454
                        }
2277
2455
 
2278
2456
                /* Compute the shared pre-master secret */
2279
2457
                field_size = EC_GROUP_get_degree(group);
2284
2462
                        goto err;
2285
2463
                        }
2286
2464
                i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
2287
 
                if (i <= 0)
2288
 
                        {
2289
 
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2465
                if (i <= 0)
 
2466
                        {
 
2467
                        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2290
2468
                            ERR_R_ECDH_LIB);
2291
 
                        goto err;
2292
 
                        }
 
2469
                        goto err;
 
2470
                        }
2293
2471
 
2294
2472
                EVP_PKEY_free(clnt_pub_pkey);
2295
2473
                EC_POINT_free(clnt_ecpoint);
2296
 
                if (srvr_ecdh != NULL) 
2297
 
                        EC_KEY_free(srvr_ecdh);
 
2474
                EC_KEY_free(srvr_ecdh);
2298
2475
                BN_CTX_free(bn_ctx);
 
2476
                EC_KEY_free(s->s3->tmp.ecdh);
 
2477
                s->s3->tmp.ecdh = NULL; 
2299
2478
 
2300
2479
                /* Compute the master secret */
2301
 
                s->session->master_key_length = s->method->ssl3_enc-> \
 
2480
                s->session->master_key_length = s->method->ssl3_enc-> \
2302
2481
                    generate_master_secret(s, s->session->master_key, p, i);
2303
2482
                
2304
 
                OPENSSL_cleanse(p, i);
2305
 
                return (ret);
 
2483
                OPENSSL_cleanse(p, i);
 
2484
                return (ret);
2306
2485
                }
2307
2486
        else
2308
2487
#endif
 
2488
#ifndef OPENSSL_NO_PSK
 
2489
                if (alg_k & SSL_kPSK)
 
2490
                        {
 
2491
                        unsigned char *t = NULL;
 
2492
                        unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
 
2493
                        unsigned int pre_ms_len = 0, psk_len = 0;
 
2494
                        int psk_err = 1;
 
2495
                        char tmp_id[PSK_MAX_IDENTITY_LEN+1];
 
2496
 
 
2497
                        al=SSL_AD_HANDSHAKE_FAILURE;
 
2498
 
 
2499
                        n2s(p,i);
 
2500
                        if (n != i+2)
 
2501
                                {
 
2502
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2503
                                        SSL_R_LENGTH_MISMATCH);
 
2504
                                goto psk_err;
 
2505
                                }
 
2506
                        if (i > PSK_MAX_IDENTITY_LEN)
 
2507
                                {
 
2508
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2509
                                        SSL_R_DATA_LENGTH_TOO_LONG);
 
2510
                                goto psk_err;
 
2511
                                }
 
2512
                        if (s->psk_server_callback == NULL)
 
2513
                                {
 
2514
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2515
                                       SSL_R_PSK_NO_SERVER_CB);
 
2516
                                goto psk_err;
 
2517
                                }
 
2518
 
 
2519
                        /* Create guaranteed NULL-terminated identity
 
2520
                         * string for the callback */
 
2521
                        memcpy(tmp_id, p, i);
 
2522
                        memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
 
2523
                        psk_len = s->psk_server_callback(s, tmp_id,
 
2524
                                psk_or_pre_ms, sizeof(psk_or_pre_ms));
 
2525
                        OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
 
2526
 
 
2527
                        if (psk_len > PSK_MAX_PSK_LEN)
 
2528
                                {
 
2529
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2530
                                        ERR_R_INTERNAL_ERROR);
 
2531
                                goto psk_err;
 
2532
                                }
 
2533
                        else if (psk_len == 0)
 
2534
                                {
 
2535
                                /* PSK related to the given identity not found */
 
2536
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2537
                                       SSL_R_PSK_IDENTITY_NOT_FOUND);
 
2538
                                al=SSL_AD_UNKNOWN_PSK_IDENTITY;
 
2539
                                goto psk_err;
 
2540
                                }
 
2541
 
 
2542
                        /* create PSK pre_master_secret */
 
2543
                        pre_ms_len=2+psk_len+2+psk_len;
 
2544
                        t = psk_or_pre_ms;
 
2545
                        memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
 
2546
                        s2n(psk_len, t);
 
2547
                        memset(t, 0, psk_len);
 
2548
                        t+=psk_len;
 
2549
                        s2n(psk_len, t);
 
2550
 
 
2551
                        if (s->session->psk_identity != NULL)
 
2552
                                OPENSSL_free(s->session->psk_identity);
 
2553
                        s->session->psk_identity = BUF_strdup((char *)p);
 
2554
                        if (s->session->psk_identity == NULL)
 
2555
                                {
 
2556
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2557
                                        ERR_R_MALLOC_FAILURE);
 
2558
                                goto psk_err;
 
2559
                                }
 
2560
 
 
2561
                        if (s->session->psk_identity_hint != NULL)
 
2562
                                OPENSSL_free(s->session->psk_identity_hint);
 
2563
                        s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
 
2564
                        if (s->ctx->psk_identity_hint != NULL &&
 
2565
                                s->session->psk_identity_hint == NULL)
 
2566
                                {
 
2567
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
 
2568
                                        ERR_R_MALLOC_FAILURE);
 
2569
                                goto psk_err;
 
2570
                                }
 
2571
 
 
2572
                        s->session->master_key_length=
 
2573
                                s->method->ssl3_enc->generate_master_secret(s,
 
2574
                                        s->session->master_key, psk_or_pre_ms, pre_ms_len);
 
2575
                        psk_err = 0;
 
2576
                psk_err:
 
2577
                        OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
 
2578
                        if (psk_err != 0)
 
2579
                                goto f_err;
 
2580
                        }
 
2581
                else
 
2582
#endif
 
2583
                if (alg_k & SSL_kGOST) 
 
2584
                        {
 
2585
                        int ret = 0;
 
2586
                        EVP_PKEY_CTX *pkey_ctx;
 
2587
                        EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
 
2588
                        unsigned char premaster_secret[32], *start;
 
2589
                        size_t outlen=32, inlen;
 
2590
                        unsigned long alg_a;
 
2591
 
 
2592
                        /* Get our certificate private key*/
 
2593
                        alg_a = s->s3->tmp.new_cipher->algorithm_auth;
 
2594
                        if (alg_a & SSL_aGOST94)
 
2595
                                pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
 
2596
                        else if (alg_a & SSL_aGOST01)
 
2597
                                pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
 
2598
 
 
2599
                        pkey_ctx = EVP_PKEY_CTX_new(pk,NULL);
 
2600
                        EVP_PKEY_decrypt_init(pkey_ctx);
 
2601
                        /* If client certificate is present and is of the same type, maybe
 
2602
                         * use it for key exchange.  Don't mind errors from
 
2603
                         * EVP_PKEY_derive_set_peer, because it is completely valid to use
 
2604
                         * a client certificate for authorization only. */
 
2605
                        client_pub_pkey = X509_get_pubkey(s->session->peer);
 
2606
                        if (client_pub_pkey)
 
2607
                                {
 
2608
                                if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
 
2609
                                        ERR_clear_error();
 
2610
                                }
 
2611
                        /* Decrypt session key */
 
2612
                        if ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED))) 
 
2613
                                {
 
2614
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
 
2615
                                goto gerr;
 
2616
                                }
 
2617
                        if (p[1] == 0x81)
 
2618
                                {
 
2619
                                start = p+3;
 
2620
                                inlen = p[2];
 
2621
                                }
 
2622
                        else if (p[1] < 0x80)
 
2623
                                {
 
2624
                                start = p+2;
 
2625
                                inlen = p[1];
 
2626
                                }
 
2627
                        else
 
2628
                                {
 
2629
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
 
2630
                                goto gerr;
 
2631
                                }
 
2632
                        if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,start,inlen) <=0) 
 
2633
 
 
2634
                                {
 
2635
                                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
 
2636
                                goto gerr;
 
2637
                                }
 
2638
                        /* Generate master secret */
 
2639
                        s->session->master_key_length=
 
2640
                                s->method->ssl3_enc->generate_master_secret(s,
 
2641
                                        s->session->master_key,premaster_secret,32);
 
2642
                        /* Check if pubkey from client certificate was used */
 
2643
                        if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
 
2644
                                ret = 2;
 
2645
                        else
 
2646
                                ret = 1;
 
2647
                gerr:
 
2648
                        EVP_PKEY_free(client_pub_pkey);
 
2649
                        EVP_PKEY_CTX_free(pkey_ctx);
 
2650
                        if (ret)
 
2651
                                return ret;
 
2652
                        else
 
2653
                                goto err;
 
2654
                        }
 
2655
                else
2309
2656
                {
2310
2657
                al=SSL_AD_HANDSHAKE_FAILURE;
2311
2658
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2395
2742
 
2396
2743
        /* we now have a signature that we need to verify */
2397
2744
        p=(unsigned char *)s->init_msg;
2398
 
        n2s(p,i);
2399
 
        n-=2;
2400
 
        if (i > n)
 
2745
        /* Check for broken implementations of GOST ciphersuites */
 
2746
        /* If key is GOST and n is exactly 64, it is bare
 
2747
         * signature without length field */
 
2748
        if (n==64 && (pkey->type==NID_id_GostR3410_94 ||
 
2749
                pkey->type == NID_id_GostR3410_2001) )
2401
2750
                {
2402
 
                SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
2403
 
                al=SSL_AD_DECODE_ERROR;
2404
 
                goto f_err;
2405
 
                }
2406
 
 
 
2751
                i=64;
 
2752
                } 
 
2753
        else 
 
2754
                {       
 
2755
                n2s(p,i);
 
2756
                n-=2;
 
2757
                if (i > n)
 
2758
                        {
 
2759
                        SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
 
2760
                        al=SSL_AD_DECODE_ERROR;
 
2761
                        goto f_err;
 
2762
                        }
 
2763
        }
2407
2764
        j=EVP_PKEY_size(pkey);
2408
2765
        if ((i > j) || (n > j) || (n <= 0))
2409
2766
                {
2466
2823
                }
2467
2824
        else
2468
2825
#endif
 
2826
        if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
 
2827
                {   unsigned char signature[64];
 
2828
                        int idx;
 
2829
                        EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL);
 
2830
                        EVP_PKEY_verify_init(pctx);
 
2831
                        if (i!=64) {
 
2832
                                fprintf(stderr,"GOST signature length is %d",i);
 
2833
                        }       
 
2834
                        for (idx=0;idx<64;idx++) {
 
2835
                                signature[63-idx]=p[idx];
 
2836
                        }       
 
2837
                        j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_verify_md,32);
 
2838
                        EVP_PKEY_CTX_free(pctx);
 
2839
                        if (j<=0) 
 
2840
                                {
 
2841
                                al=SSL_AD_DECRYPT_ERROR;
 
2842
                                SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
 
2843
                                        SSL_R_BAD_ECDSA_SIGNATURE);
 
2844
                                goto f_err;
 
2845
                                }       
 
2846
                }
 
2847
        else    
2469
2848
                {
2470
2849
                SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
2471
2850
                al=SSL_AD_UNSUPPORTED_CERTIFICATE;
2648
3027
        if (s->state == SSL3_ST_SW_CERT_A)
2649
3028
                {
2650
3029
                x=ssl_get_server_send_cert(s);
2651
 
                if (x == NULL &&
2652
 
                        /* VRS: allow null cert if auth == KRB5 */
2653
 
                        (s->s3->tmp.new_cipher->algorithms
2654
 
                                & (SSL_MKEY_MASK|SSL_AUTH_MASK))
2655
 
                        != (SSL_aKRB5|SSL_kKRB5))
 
3030
                if (x == NULL)
2656
3031
                        {
2657
 
                        SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2658
 
                        return(0);
 
3032
                        /* VRS: allow null cert if auth == KRB5 */
 
3033
                        if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
 
3034
                            (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5))
 
3035
                                {
 
3036
                                SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
 
3037
                                return(0);
 
3038
                                }
2659
3039
                        }
2660
3040
 
2661
3041
                l=ssl3_output_cert_chain(s,x);
2667
3047
        /* SSL3_ST_SW_CERT_B */
2668
3048
        return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2669
3049
        }
2670
 
 
2671
 
 
2672
 
#ifndef OPENSSL_NO_ECDH
2673
 
/* This is the complement of curve_id2nid in s3_clnt.c. */
2674
 
static int nid2curve_id(int nid)
2675
 
{
2676
 
        /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001)
2677
 
         * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */
2678
 
        switch (nid) {
2679
 
        case NID_sect163k1: /* sect163k1 (1) */
2680
 
                return 1;
2681
 
        case NID_sect163r1: /* sect163r1 (2) */
2682
 
                return 2;
2683
 
        case NID_sect163r2: /* sect163r2 (3) */
2684
 
                return 3;
2685
 
        case NID_sect193r1: /* sect193r1 (4) */ 
2686
 
                return 4;
2687
 
        case NID_sect193r2: /* sect193r2 (5) */ 
2688
 
                return 5;
2689
 
        case NID_sect233k1: /* sect233k1 (6) */
2690
 
                return 6;
2691
 
        case NID_sect233r1: /* sect233r1 (7) */ 
2692
 
                return 7;
2693
 
        case NID_sect239k1: /* sect239k1 (8) */ 
2694
 
                return 8;
2695
 
        case NID_sect283k1: /* sect283k1 (9) */
2696
 
                return 9;
2697
 
        case NID_sect283r1: /* sect283r1 (10) */ 
2698
 
                return 10;
2699
 
        case NID_sect409k1: /* sect409k1 (11) */ 
2700
 
                return 11;
2701
 
        case NID_sect409r1: /* sect409r1 (12) */
2702
 
                return 12;
2703
 
        case NID_sect571k1: /* sect571k1 (13) */ 
2704
 
                return 13;
2705
 
        case NID_sect571r1: /* sect571r1 (14) */ 
2706
 
                return 14;
2707
 
        case NID_secp160k1: /* secp160k1 (15) */
2708
 
                return 15;
2709
 
        case NID_secp160r1: /* secp160r1 (16) */ 
2710
 
                return 16;
2711
 
        case NID_secp160r2: /* secp160r2 (17) */ 
2712
 
                return 17;
2713
 
        case NID_secp192k1: /* secp192k1 (18) */
2714
 
                return 18;
2715
 
        case NID_X9_62_prime192v1: /* secp192r1 (19) */ 
2716
 
                return 19;
2717
 
        case NID_secp224k1: /* secp224k1 (20) */ 
2718
 
                return 20;
2719
 
        case NID_secp224r1: /* secp224r1 (21) */
2720
 
                return 21;
2721
 
        case NID_secp256k1: /* secp256k1 (22) */ 
2722
 
                return 22;
2723
 
        case NID_X9_62_prime256v1: /* secp256r1 (23) */ 
2724
 
                return 23;
2725
 
        case NID_secp384r1: /* secp384r1 (24) */
2726
 
                return 24;
2727
 
        case NID_secp521r1:  /* secp521r1 (25) */       
2728
 
                return 25;
2729
 
        default:
2730
 
                return 0;
2731
 
        }
2732
 
}
2733
 
#endif
2734
3050
#ifndef OPENSSL_NO_TLSEXT
2735
3051
int ssl3_send_newsession_ticket(SSL *s)
2736
3052
        {