~ubuntu-branches/ubuntu/quantal/nss/quantal-updates

« back to all changes in this revision

Viewing changes to nss/lib/ssl/ssl3con.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2014-01-22 15:16:14 UTC
  • mfrom: (1.1.21)
  • Revision ID: package-import@ubuntu.com-20140122151614-yrna9tfai5hbd1ku
Tags: 3.15.4-0ubuntu0.12.10.1
* SECURITY UPDATE: MITM attack via TLS False Start
  - CVE-2013-1740
* Adjusted packaging for new upstream release 3.15.4:
  - debian/patches/*: refreshed.
  - debian/libnss3.symbols: added new symbols.

Show diffs side-by-side

added added

removed removed

Lines of Context:
82
82
 * precedence (desirability).  It only includes cipher suites we implement.
83
83
 * This table is modified by SSL3_SetPolicy(). The ordering of cipher suites
84
84
 * in this table must match the ordering in SSL_ImplementedCiphers (sslenum.c)
 
85
 *
 
86
 * Important: See bug 946147 before enabling, reordering, or adding any cipher
 
87
 * suites to this list.
85
88
 */
86
89
static ssl3CipherSuiteCfg cipherSuites[ssl_V3_SUITES_IMPLEMENTED] = {
87
90
   /*      cipher_suite                     policy       enabled   isPresent */
 
91
 
88
92
#ifdef NSS_ENABLE_ECC
89
93
 { TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_ALLOWED, PR_FALSE, PR_FALSE},
90
94
 { TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
91
 
#endif /* NSS_ENABLE_ECC */
92
 
 { TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,     SSL_ALLOWED, PR_TRUE,  PR_FALSE},
93
 
 { TLS_RSA_WITH_AES_128_GCM_SHA256,         SSL_ALLOWED, PR_TRUE,  PR_FALSE},
94
 
 
95
 
#ifdef NSS_ENABLE_ECC
 
95
   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA is out of order to work around
 
96
    * bug 946147.
 
97
    */
96
98
 { TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,    SSL_ALLOWED, PR_FALSE, PR_FALSE},
97
 
 { TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
98
 
#endif /* NSS_ENABLE_ECC */
99
 
 { TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
100
 
 { TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
101
 
 { TLS_DHE_RSA_WITH_AES_256_CBC_SHA,        SSL_ALLOWED, PR_TRUE,  PR_FALSE},
102
 
 { TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,     SSL_ALLOWED, PR_TRUE,  PR_FALSE},
103
 
 { TLS_DHE_DSS_WITH_AES_256_CBC_SHA,        SSL_ALLOWED, PR_TRUE,  PR_FALSE},
104
 
#ifdef NSS_ENABLE_ECC
105
 
 { TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,       SSL_ALLOWED, PR_FALSE, PR_FALSE},
106
 
 { TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,     SSL_ALLOWED, PR_FALSE, PR_FALSE},
107
 
#endif /* NSS_ENABLE_ECC */
108
 
 { TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,       SSL_ALLOWED, PR_FALSE, PR_FALSE},
109
 
 { TLS_RSA_WITH_AES_256_CBC_SHA,            SSL_ALLOWED, PR_TRUE,  PR_FALSE},
110
 
 { TLS_RSA_WITH_AES_256_CBC_SHA256,         SSL_ALLOWED, PR_TRUE,  PR_FALSE},
111
 
 
112
 
#ifdef NSS_ENABLE_ECC
113
99
 { TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,    SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
100
 { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
114
101
 { TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
102
 { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
103
 { TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
104
 { TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
105
 { TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,     SSL_ALLOWED, PR_FALSE, PR_FALSE},
115
106
 { TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,        SSL_ALLOWED, PR_FALSE, PR_FALSE},
116
 
 { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
117
 
 { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
118
107
 { TLS_ECDHE_RSA_WITH_RC4_128_SHA,          SSL_ALLOWED, PR_FALSE, PR_FALSE},
119
108
#endif /* NSS_ENABLE_ECC */
 
109
 
 
110
 { TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,     SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
111
 { TLS_DHE_RSA_WITH_AES_128_CBC_SHA,        SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
112
 { TLS_DHE_DSS_WITH_AES_128_CBC_SHA,        SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
113
 { TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,     SSL_ALLOWED, PR_TRUE,  PR_FALSE},
120
114
 { TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
121
115
 { TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
122
 
 { TLS_DHE_RSA_WITH_AES_128_CBC_SHA,        SSL_ALLOWED, PR_TRUE,  PR_FALSE},
123
 
 { TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,     SSL_ALLOWED, PR_TRUE,  PR_FALSE},
124
 
 { TLS_DHE_DSS_WITH_AES_128_CBC_SHA,        SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
116
 { TLS_DHE_RSA_WITH_AES_256_CBC_SHA,        SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
117
 { TLS_DHE_DSS_WITH_AES_256_CBC_SHA,        SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
118
 { TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,     SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
119
 { TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
120
 { TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
121
 { SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA,       SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
122
 { SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA,       SSL_ALLOWED, PR_TRUE,  PR_FALSE},
125
123
 { TLS_DHE_DSS_WITH_RC4_128_SHA,            SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
124
 
126
125
#ifdef NSS_ENABLE_ECC
 
126
 { TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,     SSL_ALLOWED, PR_FALSE, PR_FALSE},
127
127
 { TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,       SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
128
 { TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,     SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
129
 { TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,       SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
130
 { TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,    SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
131
 { TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
132
 { TLS_ECDH_ECDSA_WITH_RC4_128_SHA,         SSL_ALLOWED, PR_FALSE, PR_FALSE},
128
133
 { TLS_ECDH_RSA_WITH_RC4_128_SHA,           SSL_ALLOWED, PR_FALSE, PR_FALSE},
129
 
 { TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,     SSL_ALLOWED, PR_FALSE, PR_FALSE},
130
 
 { TLS_ECDH_ECDSA_WITH_RC4_128_SHA,         SSL_ALLOWED, PR_FALSE, PR_FALSE},
131
134
#endif /* NSS_ENABLE_ECC */
132
 
 { TLS_RSA_WITH_SEED_CBC_SHA,               SSL_ALLOWED, PR_FALSE, PR_FALSE},
133
 
 { TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,       SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
135
 
 
136
 /* RSA */
 
137
 { TLS_RSA_WITH_AES_128_GCM_SHA256,         SSL_ALLOWED, PR_TRUE,  PR_FALSE},
134
138
 { TLS_RSA_WITH_AES_128_CBC_SHA,            SSL_ALLOWED, PR_TRUE,  PR_FALSE},
135
139
 { TLS_RSA_WITH_AES_128_CBC_SHA256,         SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
140
 { TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,       SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
141
 { TLS_RSA_WITH_AES_256_CBC_SHA,            SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
142
 { TLS_RSA_WITH_AES_256_CBC_SHA256,         SSL_ALLOWED, PR_TRUE,  PR_FALSE},
 
143
 { TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,       SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
144
 { TLS_RSA_WITH_SEED_CBC_SHA,               SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
145
 { SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
146
 { SSL_RSA_WITH_3DES_EDE_CBC_SHA,           SSL_ALLOWED, PR_TRUE,  PR_FALSE},
136
147
 { SSL_RSA_WITH_RC4_128_SHA,                SSL_ALLOWED, PR_TRUE,  PR_FALSE},
137
148
 { SSL_RSA_WITH_RC4_128_MD5,                SSL_ALLOWED, PR_TRUE,  PR_FALSE},
138
149
 
139
 
#ifdef NSS_ENABLE_ECC
140
 
 { TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,   SSL_ALLOWED, PR_FALSE, PR_FALSE},
141
 
 { TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,     SSL_ALLOWED, PR_FALSE, PR_FALSE},
142
 
#endif /* NSS_ENABLE_ECC */
143
 
 { SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA,       SSL_ALLOWED, PR_TRUE,  PR_FALSE},
144
 
 { SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA,       SSL_ALLOWED, PR_TRUE,  PR_FALSE},
145
 
#ifdef NSS_ENABLE_ECC
146
 
 { TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
147
 
 { TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,    SSL_ALLOWED, PR_FALSE, PR_FALSE},
148
 
#endif /* NSS_ENABLE_ECC */
149
 
 { SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
150
 
 { SSL_RSA_WITH_3DES_EDE_CBC_SHA,           SSL_ALLOWED, PR_TRUE,  PR_FALSE},
151
 
 
152
 
 
 
150
 /* 56-bit DES "domestic" cipher suites */
153
151
 { SSL_DHE_RSA_WITH_DES_CBC_SHA,            SSL_ALLOWED, PR_FALSE, PR_FALSE},
154
152
 { SSL_DHE_DSS_WITH_DES_CBC_SHA,            SSL_ALLOWED, PR_FALSE, PR_FALSE},
155
153
 { SSL_RSA_FIPS_WITH_DES_CBC_SHA,           SSL_ALLOWED, PR_FALSE, PR_FALSE},
156
154
 { SSL_RSA_WITH_DES_CBC_SHA,                SSL_ALLOWED, PR_FALSE, PR_FALSE},
 
155
 
 
156
 /* export ciphersuites with 1024-bit public key exchange keys */
157
157
 { TLS_RSA_EXPORT1024_WITH_RC4_56_SHA,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
158
158
 { TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA,     SSL_ALLOWED, PR_FALSE, PR_FALSE},
159
159
 
 
160
 /* export ciphersuites with 512-bit public key exchange keys */
160
161
 { SSL_RSA_EXPORT_WITH_RC4_40_MD5,          SSL_ALLOWED, PR_FALSE, PR_FALSE},
161
162
 { SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5,      SSL_ALLOWED, PR_FALSE, PR_FALSE},
162
163
 
 
164
 /* ciphersuites with no encryption */
163
165
#ifdef NSS_ENABLE_ECC
164
166
 { TLS_ECDHE_ECDSA_WITH_NULL_SHA,           SSL_ALLOWED, PR_FALSE, PR_FALSE},
165
167
 { TLS_ECDHE_RSA_WITH_NULL_SHA,             SSL_ALLOWED, PR_FALSE, PR_FALSE},
171
173
 { SSL_RSA_WITH_NULL_MD5,                   SSL_ALLOWED, PR_FALSE, PR_FALSE},
172
174
};
173
175
 
 
176
/* Verify that SSL_ImplementedCiphers and cipherSuites are in consistent order.
 
177
 */
 
178
#ifdef DEBUG
 
179
void ssl3_CheckCipherSuiteOrderConsistency()
 
180
{
 
181
    unsigned int i;
 
182
 
 
183
    /* Note that SSL_ImplementedCiphers has more elements than cipherSuites
 
184
     * because it SSL_ImplementedCiphers includes SSL 2.0 cipher suites.
 
185
     */
 
186
    PORT_Assert(SSL_NumImplementedCiphers >= PR_ARRAY_SIZE(cipherSuites));
 
187
 
 
188
    for (i = 0; i < PR_ARRAY_SIZE(cipherSuites); ++i) {
 
189
        PORT_Assert(SSL_ImplementedCiphers[i] == cipherSuites[i].cipher_suite);
 
190
    }
 
191
}
 
192
#endif
 
193
 
174
194
/* This list of SSL3 compression methods is sorted in descending order of
175
195
 * precedence (desirability).  It only includes compression methods we
176
196
 * implement.
865
885
static SECStatus
866
886
ssl3_GetNewRandom(SSL3Random *random)
867
887
{
868
 
    PRUint32 gmt = ssl_Time();
869
888
    SECStatus rv;
870
889
 
871
 
    random->rand[0] = (unsigned char)(gmt >> 24);
872
 
    random->rand[1] = (unsigned char)(gmt >> 16);
873
 
    random->rand[2] = (unsigned char)(gmt >>  8);
874
 
    random->rand[3] = (unsigned char)(gmt);
875
 
 
876
890
    /* first 4 bytes are reserverd for time */
877
 
    rv = PK11_GenerateRandom(&random->rand[4], SSL3_RANDOM_LENGTH - 4);
 
891
    rv = PK11_GenerateRandom(random->rand, SSL3_RANDOM_LENGTH);
878
892
    if (rv != SECSuccess) {
879
893
        ssl_MapLowLevelError(SSL_ERROR_GENERATE_RANDOM_FAILURE);
880
894
    }
1012
1026
        }
1013
1027
        /* Allow DER encoded DSA signatures in SSL 3.0 */
1014
1028
        if (isTLS || buf->len != SECKEY_SignatureLen(key)) {
1015
 
            signature = DSAU_DecodeDerSig(buf);
 
1029
            signature = DSAU_DecodeDerSigToLen(buf, SECKEY_SignatureLen(key));
1016
1030
            if (!signature) {
1017
1031
                PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE);
1018
1032
                return SECFailure;
1597
1611
 
1598
1612
    calg = cipher_def->calg;
1599
1613
 
1600
 
    if (calg == calg_aes_gcm) {
 
1614
    if (calg == ssl_calg_aes_gcm) {
1601
1615
        pwSpec->encode = NULL;
1602
1616
        pwSpec->decode = NULL;
1603
1617
        pwSpec->destroy = NULL;
1713
1727
    case ssl_calg_rc2:
1714
1728
    case ssl_calg_idea:
1715
1729
    case ssl_calg_fortezza:
 
1730
    case ssl_calg_aes_gcm:
1716
1731
        break;
1717
1732
    }
1718
1733
 
2712
2727
 *    Forces the use of the provided epoch
2713
2728
 * ssl_SEND_FLAG_CAP_RECORD_VERSION
2714
2729
 *    Caps the record layer version number of TLS ClientHello to { 3, 1 }
2715
 
 *    (TLS 1.0). Some TLS 1.0 servers (which seem to use F5 BIG-IP) ignore 
 
2730
 *    (TLS 1.0). Some TLS 1.0 servers (which seem to use F5 BIG-IP) ignore
2716
2731
 *    ClientHello.client_version and use the record layer version number
2717
2732
 *    (TLSPlaintext.version) instead when negotiating protocol versions. In
2718
2733
 *    addition, if the record layer version number of ClientHello is { 3, 2 }
2719
 
 *    (TLS 1.1) or higher, these servers reset the TCP connections. Set this
 
2734
 *    (TLS 1.1) or higher, these servers reset the TCP connections. Lastly,
 
2735
 *    some F5 BIG-IP servers hang if a record containing a ClientHello has a
 
2736
 *    version greater than { 3, 1 } and a length greater than 255. Set this
2720
2737
 *    flag to work around such servers.
2721
2738
 */
2722
2739
PRInt32
2735
2752
    SSL_TRC(3, ("%d: SSL3[%d] SendRecord type: %s nIn=%d",
2736
2753
                SSL_GETPID(), ss->fd, ssl3_DecodeContentType(type),
2737
2754
                nIn));
2738
 
    PRINT_BUF(3, (ss, "Send record (plain text)", pIn, nIn));
 
2755
    PRINT_BUF(50, (ss, "Send record (plain text)", pIn, nIn));
2739
2756
 
2740
2757
    PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) );
2741
2758
 
3853
3870
                ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE);
3854
3871
                return SECFailure;
3855
3872
            }
 
3873
 
 
3874
            /* Create a backup SHA-1 hash for a potential client auth
 
3875
             * signature.
 
3876
             *
 
3877
             * In TLS 1.2, ssl3_ComputeHandshakeHashes always uses the
 
3878
             * handshake hash function (SHA-256). If the server or the client
 
3879
             * does not support SHA-256 as a signature hash, we can either
 
3880
             * maintain a backup SHA-1 handshake hash or buffer all handshake
 
3881
             * messages.
 
3882
             */
 
3883
            if (!ss->sec.isServer) {
 
3884
                ss->ssl3.hs.backupHash = PK11_CreateDigestContext(SEC_OID_SHA1);
 
3885
                if (ss->ssl3.hs.backupHash == NULL) {
 
3886
                    ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE);
 
3887
                    return SECFailure;
 
3888
                }
 
3889
 
 
3890
                if (PK11_DigestBegin(ss->ssl3.hs.backupHash) != SECSuccess) {
 
3891
                    ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE);
 
3892
                    return SECFailure;
 
3893
                }
 
3894
            }
3856
3895
        } else {
3857
3896
            /* Both ss->ssl3.hs.md5 and ss->ssl3.hs.sha should be NULL or
3858
3897
             * created successfully. */
3963
4002
            ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE);
3964
4003
            return rv;
3965
4004
        }
 
4005
        if (ss->ssl3.hs.backupHash) {
 
4006
            rv = PK11_DigestOp(ss->ssl3.hs.backupHash, b, l);
 
4007
            if (rv != SECSuccess) {
 
4008
                ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE);
 
4009
                return rv;
 
4010
            }
 
4011
        }
3966
4012
    } else {
3967
4013
        rv = PK11_DigestOp(ss->ssl3.hs.md5, b, l);
3968
4014
        if (rv != SECSuccess) {
4711
4757
    return rv;
4712
4758
}
4713
4759
 
 
4760
static SECStatus
 
4761
ssl3_ComputeBackupHandshakeHashes(sslSocket * ss,
 
4762
                                  SSL3Hashes * hashes) /* output goes here. */
 
4763
{
 
4764
    SECStatus rv = SECSuccess;
 
4765
 
 
4766
    PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
 
4767
    PORT_Assert( !ss->sec.isServer );
 
4768
    PORT_Assert( ss->ssl3.hs.hashType == handshake_hash_single );
 
4769
 
 
4770
    rv = PK11_DigestFinal(ss->ssl3.hs.backupHash, hashes->u.raw, &hashes->len,
 
4771
                          sizeof(hashes->u.raw));
 
4772
    if (rv != SECSuccess) {
 
4773
        ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE);
 
4774
        rv = SECFailure;
 
4775
        goto loser;
 
4776
    }
 
4777
    hashes->hashAlg = SEC_OID_SHA1;
 
4778
 
 
4779
loser:
 
4780
    PK11_DestroyContext(ss->ssl3.hs.backupHash, PR_TRUE);
 
4781
    ss->ssl3.hs.backupHash = NULL;
 
4782
    return rv;
 
4783
}
 
4784
 
4714
4785
/*
4715
4786
 * SSL 2 based implementations pass in the initial outbound buffer
4716
4787
 * so that the handshake hash can contain the included information.
4768
4839
    int              num_suites;
4769
4840
    int              actual_count = 0;
4770
4841
    PRBool           isTLS = PR_FALSE;
4771
 
    PRBool           requestingResume = PR_FALSE;
4772
4842
    PRInt32          total_exten_len = 0;
4773
4843
    unsigned         numCompressionMethods;
4774
4844
    PRInt32          flags;
4786
4856
    ss->ssl3.hs.sendingSCSV = PR_FALSE; /* Must be reset every handshake */
4787
4857
    PORT_Assert(IS_DTLS(ss) || !resending);
4788
4858
 
 
4859
    SECITEM_FreeItem(&ss->ssl3.hs.newSessionTicket.ticket, PR_FALSE);
 
4860
    ss->ssl3.hs.receivedNewSessionTicket = PR_FALSE;
 
4861
 
4789
4862
    /* We might be starting a session renegotiation in which case we should
4790
4863
     * clear previous state.
4791
4864
     */
4907
4980
    }
4908
4981
 
4909
4982
    if (sid) {
4910
 
        requestingResume = PR_TRUE;
4911
4983
        SSL_AtomicIncrementLong(& ssl3stats.sch_sid_cache_hits );
4912
4984
 
4913
 
        /* Are we attempting a stateless session resume? */
4914
 
        if (sid->version > SSL_LIBRARY_VERSION_3_0 &&
4915
 
            sid->u.ssl3.sessionTicket.ticket.data)
4916
 
            SSL_AtomicIncrementLong(& ssl3stats.sch_sid_stateless_resumes );
4917
 
 
4918
4985
        PRINT_BUF(4, (ss, "client, found session-id:", sid->u.ssl3.sessionID,
4919
4986
                      sid->u.ssl3.sessionIDLength));
4920
4987
 
4974
5041
        return SECFailure;      /* ssl3_config_match_init has set error code. */
4975
5042
 
4976
5043
    /* HACK for SCSV in SSL 3.0.  On initial handshake, prepend SCSV,
4977
 
     * only if we're willing to complete an SSL 3.0 handshake.
 
5044
     * only if TLS is disabled.
4978
5045
     */
4979
 
    if (!ss->firstHsDone && ss->vrange.min == SSL_LIBRARY_VERSION_3_0) {
 
5046
    if (!ss->firstHsDone && !isTLS) {
4980
5047
        /* Must set this before calling Hello Extension Senders, 
4981
5048
         * to suppress sending of empty RI extension.
4982
5049
         */
4983
5050
        ss->ssl3.hs.sendingSCSV = PR_TRUE;
4984
5051
    }
4985
5052
 
 
5053
    /* When we attempt session resumption (only), we must lock the sid to
 
5054
     * prevent races with other resumption connections that receive a
 
5055
     * NewSessionTicket that will cause the ticket in the sid to be replaced.
 
5056
     * Once we've copied the session ticket into our ClientHello message, it
 
5057
     * is OK for the ticket to change, so we just need to make sure we hold
 
5058
     * the lock across the calls to ssl3_CallHelloExtensionSenders.
 
5059
     */
 
5060
    if (sid->u.ssl3.lock) {
 
5061
        PR_RWLock_Rlock(sid->u.ssl3.lock);
 
5062
    }
 
5063
 
4986
5064
    if (isTLS || (ss->firstHsDone && ss->peerRequestedProtection)) {
4987
5065
        PRUint32 maxBytes = 65535; /* 2^16 - 1 */
4988
5066
        PRInt32  extLen;
4989
5067
 
4990
5068
        extLen = ssl3_CallHelloExtensionSenders(ss, PR_FALSE, maxBytes, NULL);
4991
5069
        if (extLen < 0) {
 
5070
            if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
4992
5071
            return SECFailure;
4993
5072
        }
4994
5073
        maxBytes        -= extLen;
5011
5090
 
5012
5091
    /* how many suites are permitted by policy and user preference? */
5013
5092
    num_suites = count_cipher_suites(ss, ss->ssl3.policy, PR_TRUE);
5014
 
    if (!num_suites)
 
5093
    if (!num_suites) {
 
5094
        if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5015
5095
        return SECFailure;      /* count_cipher_suites has set error code. */
 
5096
    }
5016
5097
    if (ss->ssl3.hs.sendingSCSV) {
5017
5098
        ++num_suites;   /* make room for SCSV */
5018
5099
    }
5034
5115
 
5035
5116
    rv = ssl3_AppendHandshakeHeader(ss, client_hello, length);
5036
5117
    if (rv != SECSuccess) {
 
5118
        if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5037
5119
        return rv;      /* err set by ssl3_AppendHandshake* */
5038
5120
    }
5039
5121
 
5052
5134
        rv = ssl3_AppendHandshakeNumber(ss, ss->clientHelloVersion, 2);
5053
5135
    }
5054
5136
    if (rv != SECSuccess) {
 
5137
        if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5055
5138
        return rv;      /* err set by ssl3_AppendHandshake* */
5056
5139
    }
5057
5140
 
5058
5141
    if (!resending) { /* Don't re-generate if we are in DTLS re-sending mode */
5059
5142
        rv = ssl3_GetNewRandom(&ss->ssl3.hs.client_random);
5060
5143
        if (rv != SECSuccess) {
 
5144
            if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5061
5145
            return rv;  /* err set by GetNewRandom. */
5062
5146
        }
5063
5147
    }
5064
5148
    rv = ssl3_AppendHandshake(ss, &ss->ssl3.hs.client_random,
5065
5149
                              SSL3_RANDOM_LENGTH);
5066
5150
    if (rv != SECSuccess) {
 
5151
        if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5067
5152
        return rv;      /* err set by ssl3_AppendHandshake* */
5068
5153
    }
5069
5154
 
5073
5158
    else
5074
5159
        rv = ssl3_AppendHandshakeVariable(ss, NULL, 0, 1);
5075
5160
    if (rv != SECSuccess) {
 
5161
        if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5076
5162
        return rv;      /* err set by ssl3_AppendHandshake* */
5077
5163
    }
5078
5164
 
5080
5166
        rv = ssl3_AppendHandshakeVariable(
5081
5167
            ss, ss->ssl3.hs.cookie, ss->ssl3.hs.cookieLen, 1);
5082
5168
        if (rv != SECSuccess) {
 
5169
            if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5083
5170
            return rv;  /* err set by ssl3_AppendHandshake* */
5084
5171
        }
5085
5172
    }
5086
5173
 
5087
5174
    rv = ssl3_AppendHandshakeNumber(ss, num_suites*sizeof(ssl3CipherSuite), 2);
5088
5175
    if (rv != SECSuccess) {
 
5176
        if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5089
5177
        return rv;      /* err set by ssl3_AppendHandshake* */
5090
5178
    }
5091
5179
 
5094
5182
        rv = ssl3_AppendHandshakeNumber(ss, TLS_EMPTY_RENEGOTIATION_INFO_SCSV,
5095
5183
                                        sizeof(ssl3CipherSuite));
5096
5184
        if (rv != SECSuccess) {
 
5185
            if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5097
5186
            return rv;  /* err set by ssl3_AppendHandshake* */
5098
5187
        }
5099
5188
        actual_count++;
5103
5192
        if (config_match(suite, ss->ssl3.policy, PR_TRUE, &ss->vrange)) {
5104
5193
            actual_count++;
5105
5194
            if (actual_count > num_suites) {
 
5195
                if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5106
5196
                /* set error card removal/insertion error */
5107
5197
                PORT_SetError(SSL_ERROR_TOKEN_INSERTION_REMOVAL);
5108
5198
                return SECFailure;
5110
5200
            rv = ssl3_AppendHandshakeNumber(ss, suite->cipher_suite,
5111
5201
                                            sizeof(ssl3CipherSuite));
5112
5202
            if (rv != SECSuccess) {
 
5203
                if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5113
5204
                return rv;      /* err set by ssl3_AppendHandshake* */
5114
5205
            }
5115
5206
        }
5120
5211
     * the server.. */
5121
5212
    if (actual_count != num_suites) {
5122
5213
        /* Card removal/insertion error */
 
5214
        if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5123
5215
        PORT_SetError(SSL_ERROR_TOKEN_INSERTION_REMOVAL);
5124
5216
        return SECFailure;
5125
5217
    }
5126
5218
 
5127
5219
    rv = ssl3_AppendHandshakeNumber(ss, numCompressionMethods, 1);
5128
5220
    if (rv != SECSuccess) {
 
5221
        if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5129
5222
        return rv;      /* err set by ssl3_AppendHandshake* */
5130
5223
    }
5131
5224
    for (i = 0; i < compressionMethodsCount; i++) {
5133
5226
            continue;
5134
5227
        rv = ssl3_AppendHandshakeNumber(ss, compressions[i], 1);
5135
5228
        if (rv != SECSuccess) {
 
5229
            if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5136
5230
            return rv;  /* err set by ssl3_AppendHandshake* */
5137
5231
        }
5138
5232
    }
5143
5237
 
5144
5238
        rv = ssl3_AppendHandshakeNumber(ss, maxBytes, 2);
5145
5239
        if (rv != SECSuccess) {
 
5240
            if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5146
5241
            return rv;  /* err set by AppendHandshake. */
5147
5242
        }
5148
5243
 
5149
5244
        extLen = ssl3_CallHelloExtensionSenders(ss, PR_TRUE, maxBytes, NULL);
5150
5245
        if (extLen < 0) {
 
5246
            if (sid->u.ssl3.lock) { PR_RWLock_Unlock(sid->u.ssl3.lock); }
5151
5247
            return SECFailure;
5152
5248
        }
5153
5249
        maxBytes -= extLen;
5154
5250
        PORT_Assert(!maxBytes);
5155
5251
    } 
 
5252
 
 
5253
    if (sid->u.ssl3.lock) {
 
5254
        PR_RWLock_Unlock(sid->u.ssl3.lock);
 
5255
    }
 
5256
 
 
5257
    if (ss->xtnData.sentSessionTicketInClientHello) {
 
5258
        SSL_AtomicIncrementLong(&ssl3stats.sch_sid_stateless_resumes);
 
5259
    }
 
5260
 
5156
5261
    if (ss->ssl3.hs.sendingSCSV) {
5157
5262
        /* Since we sent the SCSV, pretend we sent empty RI extension. */
5158
5263
        TLSExtensionData *xtnData = &ss->xtnData;
5161
5266
    }
5162
5267
 
5163
5268
    flags = 0;
5164
 
    if (!ss->firstHsDone && !requestingResume && !IS_DTLS(ss)) {
 
5269
    if (!ss->firstHsDone && !IS_DTLS(ss)) {
5165
5270
        flags |= ssl_SEND_FLAG_CAP_RECORD_VERSION;
5166
5271
    }
5167
5272
    rv = ssl3_FlushHandshake(ss, flags);
5960
6065
                SSL_GETPID(), ss->fd));
5961
6066
 
5962
6067
    ssl_GetSpecReadLock(ss);
5963
 
    rv = ssl3_ComputeHandshakeHashes(ss, ss->ssl3.pwSpec, &hashes, 0);
 
6068
    if (ss->ssl3.hs.hashType == handshake_hash_single &&
 
6069
        ss->ssl3.hs.backupHash) {
 
6070
        rv = ssl3_ComputeBackupHandshakeHashes(ss, &hashes);
 
6071
        PORT_Assert(!ss->ssl3.hs.backupHash);
 
6072
    } else {
 
6073
        rv = ssl3_ComputeHandshakeHashes(ss, ss->ssl3.pwSpec, &hashes, 0);
 
6074
    }
5964
6075
    ssl_ReleaseSpecReadLock(ss);
5965
6076
    if (rv != SECSuccess) {
5966
6077
        goto done;      /* err code was set by ssl3_ComputeHandshakeHashes */
6003
6114
        if (rv != SECSuccess) {
6004
6115
            goto done;
6005
6116
        }
6006
 
        /* We always sign using the handshake hash function. It's possible that
6007
 
         * a server could support SHA-256 as the handshake hash but not as a
6008
 
         * signature hash. In that case we wouldn't be able to do client
6009
 
         * certificates with it. The alternative is to buffer all handshake
6010
 
         * messages. */
6011
6117
        sigAndHash.hashAlg = hashes.hashAlg;
6012
6118
 
6013
6119
        rv = ssl3_AppendSignatureAndHashAlgorithm(ss, &sigAndHash);
6332
6438
        SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_cache_hits );
6333
6439
 
6334
6440
        /* If we sent a session ticket, then this is a stateless resume. */
6335
 
        if (sid->version > SSL_LIBRARY_VERSION_3_0 &&
6336
 
            sid->u.ssl3.sessionTicket.ticket.data != NULL)
 
6441
        if (ss->xtnData.sentSessionTicketInClientHello)
6337
6442
            SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_stateless_resumes );
6338
6443
 
6339
6444
        if (ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn))
6674
6779
}
6675
6780
 
6676
6781
 
 
6782
/*
 
6783
 * Returns the TLS signature algorithm for the client authentication key and
 
6784
 * whether it is an RSA or DSA key that may be able to sign only SHA-1 hashes.
 
6785
 */
 
6786
static SECStatus
 
6787
ssl3_ExtractClientKeyInfo(sslSocket *ss,
 
6788
                          TLSSignatureAlgorithm *sigAlg,
 
6789
                          PRBool *preferSha1)
 
6790
{
 
6791
    SECStatus rv = SECSuccess;
 
6792
    SECKEYPublicKey *pubk;
 
6793
 
 
6794
    pubk = CERT_ExtractPublicKey(ss->ssl3.clientCertificate);
 
6795
    if (pubk == NULL) {
 
6796
        rv = SECFailure;
 
6797
        goto done;
 
6798
    }
 
6799
 
 
6800
    rv = ssl3_TLSSignatureAlgorithmForKeyType(pubk->keyType, sigAlg);
 
6801
    if (rv != SECSuccess) {
 
6802
        goto done;
 
6803
    }
 
6804
 
 
6805
    /* If the key is a 1024-bit RSA or DSA key, assume conservatively that
 
6806
     * it may be unable to sign SHA-256 hashes. This is the case for older
 
6807
     * Estonian ID cards that have 1024-bit RSA keys. In FIPS 186-2 and
 
6808
     * older, DSA key size is at most 1024 bits and the hash function must
 
6809
     * be SHA-1.
 
6810
     */
 
6811
    if (pubk->keyType == rsaKey || pubk->keyType == dsaKey) {
 
6812
        *preferSha1 = SECKEY_PublicKeyStrength(pubk) <= 128;
 
6813
    } else {
 
6814
        *preferSha1 = PR_FALSE;
 
6815
    }
 
6816
 
 
6817
done:
 
6818
    if (pubk)
 
6819
        SECKEY_DestroyPublicKey(pubk);
 
6820
    return rv;
 
6821
}
 
6822
 
 
6823
/* Destroys the backup handshake hash context if we don't need it. Note that
 
6824
 * this function selects the hash algorithm for client authentication
 
6825
 * signatures; ssl3_SendCertificateVerify uses the presence of the backup hash
 
6826
 * to determine whether to use SHA-1 or SHA-256. */
 
6827
static void
 
6828
ssl3_DestroyBackupHandshakeHashIfNotNeeded(sslSocket *ss,
 
6829
                                           const SECItem *algorithms)
 
6830
{
 
6831
    SECStatus rv;
 
6832
    TLSSignatureAlgorithm sigAlg;
 
6833
    PRBool preferSha1;
 
6834
    PRBool supportsSha1 = PR_FALSE;
 
6835
    PRBool supportsSha256 = PR_FALSE;
 
6836
    PRBool needBackupHash = PR_FALSE;
 
6837
    unsigned int i;
 
6838
 
 
6839
#ifndef NO_PKCS11_BYPASS
 
6840
    /* Backup handshake hash is not supported in PKCS #11 bypass mode. */
 
6841
    if (ss->opt.bypassPKCS11) {
 
6842
        PORT_Assert(!ss->ssl3.hs.backupHash);
 
6843
        return;
 
6844
    }
 
6845
#endif
 
6846
    PORT_Assert(ss->ssl3.hs.backupHash);
 
6847
 
 
6848
    /* Determine the key's signature algorithm and whether it prefers SHA-1. */
 
6849
    rv = ssl3_ExtractClientKeyInfo(ss, &sigAlg, &preferSha1);
 
6850
    if (rv != SECSuccess) {
 
6851
        goto done;
 
6852
    }
 
6853
 
 
6854
    /* Determine the server's hash support for that signature algorithm. */
 
6855
    for (i = 0; i < algorithms->len; i += 2) {
 
6856
        if (algorithms->data[i+1] == sigAlg) {
 
6857
            if (algorithms->data[i] == tls_hash_sha1) {
 
6858
                supportsSha1 = PR_TRUE;
 
6859
            } else if (algorithms->data[i] == tls_hash_sha256) {
 
6860
                supportsSha256 = PR_TRUE;
 
6861
            }
 
6862
        }
 
6863
    }
 
6864
 
 
6865
    /* If either the server does not support SHA-256 or the client key prefers
 
6866
     * SHA-1, leave the backup hash. */
 
6867
    if (supportsSha1 && (preferSha1 || !supportsSha256)) {
 
6868
        needBackupHash = PR_TRUE;
 
6869
    }
 
6870
 
 
6871
done:
 
6872
    if (!needBackupHash) {
 
6873
        PK11_DestroyContext(ss->ssl3.hs.backupHash, PR_TRUE);
 
6874
        ss->ssl3.hs.backupHash = NULL;
 
6875
    }
 
6876
}
 
6877
 
6677
6878
typedef struct dnameNode {
6678
6879
    struct dnameNode *next;
6679
6880
    SECItem           name;
6831
7032
                                        ss->ssl3.clientCertificate,
6832
7033
                                        certUsageSSLClient, PR_FALSE);
6833
7034
        if (ss->ssl3.clientCertChain == NULL) {
6834
 
            if (ss->ssl3.clientCertificate != NULL) {
6835
 
                CERT_DestroyCertificate(ss->ssl3.clientCertificate);
6836
 
                ss->ssl3.clientCertificate = NULL;
6837
 
            }
6838
 
            if (ss->ssl3.clientPrivateKey != NULL) {
6839
 
                SECKEY_DestroyPrivateKey(ss->ssl3.clientPrivateKey);
6840
 
                ss->ssl3.clientPrivateKey = NULL;
6841
 
            }
 
7035
            CERT_DestroyCertificate(ss->ssl3.clientCertificate);
 
7036
            ss->ssl3.clientCertificate = NULL;
 
7037
            SECKEY_DestroyPrivateKey(ss->ssl3.clientPrivateKey);
 
7038
            ss->ssl3.clientPrivateKey = NULL;
6842
7039
            goto send_no_certificate;
6843
7040
        }
 
7041
        if (ss->ssl3.hs.hashType == handshake_hash_single) {
 
7042
            ssl3_DestroyBackupHandshakeHashIfNotNeeded(ss, &algorithms);
 
7043
        }
6844
7044
        break;  /* not an error */
6845
7045
 
6846
7046
    case SECFailure:
6874
7074
    return rv;
6875
7075
}
6876
7076
 
 
7077
static SECStatus
 
7078
ssl3_CheckFalseStart(sslSocket *ss)
 
7079
{
 
7080
    PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
 
7081
    PORT_Assert( !ss->ssl3.hs.authCertificatePending );
 
7082
    PORT_Assert( !ss->ssl3.hs.canFalseStart );
 
7083
 
 
7084
    if (!ss->canFalseStartCallback) {
 
7085
        SSL_TRC(3, ("%d: SSL[%d]: no false start callback so no false start",
 
7086
                    SSL_GETPID(), ss->fd));
 
7087
    } else {
 
7088
        PRBool maybeFalseStart;
 
7089
        SECStatus rv;
 
7090
 
 
7091
        /* An attacker can control the selected ciphersuite so we only wish to
 
7092
         * do False Start in the case that the selected ciphersuite is
 
7093
         * sufficiently strong that the attack can gain no advantage.
 
7094
         * Therefore we always require an 80-bit cipher. */
 
7095
        ssl_GetSpecReadLock(ss);
 
7096
        maybeFalseStart = ss->ssl3.cwSpec->cipher_def->secret_key_size >= 10;
 
7097
        ssl_ReleaseSpecReadLock(ss);
 
7098
 
 
7099
        if (!maybeFalseStart) {
 
7100
            SSL_TRC(3, ("%d: SSL[%d]: no false start due to weak cipher",
 
7101
                        SSL_GETPID(), ss->fd));
 
7102
        } else {
 
7103
            rv = (ss->canFalseStartCallback)(ss->fd,
 
7104
                                             ss->canFalseStartCallbackData,
 
7105
                                             &ss->ssl3.hs.canFalseStart);
 
7106
            if (rv == SECSuccess) {
 
7107
                SSL_TRC(3, ("%d: SSL[%d]: false start callback returned %s",
 
7108
                            SSL_GETPID(), ss->fd,
 
7109
                            ss->ssl3.hs.canFalseStart ? "TRUE" : "FALSE"));
 
7110
            } else {
 
7111
                SSL_TRC(3, ("%d: SSL[%d]: false start callback failed (%s)",
 
7112
                            SSL_GETPID(), ss->fd,
 
7113
                            PR_ErrorToName(PR_GetError())));
 
7114
            }
 
7115
            return rv;
 
7116
        }
 
7117
    }
 
7118
 
 
7119
    ss->ssl3.hs.canFalseStart = PR_FALSE;
 
7120
    return SECSuccess;
 
7121
}
 
7122
 
6877
7123
PRBool
6878
 
ssl3_CanFalseStart(sslSocket *ss) {
6879
 
    PRBool rv;
 
7124
ssl3_WaitingForStartOfServerSecondRound(sslSocket *ss)
 
7125
{
 
7126
    PRBool result;
6880
7127
 
6881
7128
    PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
6882
7129
 
6883
 
    /* XXX: does not take into account whether we are waiting for
6884
 
     * SSL_AuthCertificateComplete or SSL_RestartHandshakeAfterCertReq. If/when
6885
 
     * that is done, this function could return different results each time it
6886
 
     * would be called.
6887
 
     */
6888
 
 
6889
 
    ssl_GetSpecReadLock(ss);
6890
 
    rv = ss->opt.enableFalseStart &&
6891
 
         !ss->sec.isServer &&
6892
 
         !ss->ssl3.hs.isResuming &&
6893
 
         ss->ssl3.cwSpec &&
6894
 
 
6895
 
         /* An attacker can control the selected ciphersuite so we only wish to
6896
 
          * do False Start in the case that the selected ciphersuite is
6897
 
          * sufficiently strong that the attack can gain no advantage.
6898
 
          * Therefore we require an 80-bit cipher and a forward-secret key
6899
 
          * exchange. */
6900
 
         ss->ssl3.cwSpec->cipher_def->secret_key_size >= 10 &&
6901
 
        (ss->ssl3.hs.kea_def->kea == kea_dhe_dss ||
6902
 
         ss->ssl3.hs.kea_def->kea == kea_dhe_rsa ||
6903
 
         ss->ssl3.hs.kea_def->kea == kea_ecdhe_ecdsa ||
6904
 
         ss->ssl3.hs.kea_def->kea == kea_ecdhe_rsa);
6905
 
    ssl_ReleaseSpecReadLock(ss);
6906
 
    return rv;
 
7130
    switch (ss->ssl3.hs.ws) {
 
7131
    case wait_new_session_ticket:
 
7132
        result = PR_TRUE;
 
7133
        break;
 
7134
    case wait_change_cipher:
 
7135
        result = !ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn);
 
7136
        break;
 
7137
    default:
 
7138
        result = PR_FALSE;
 
7139
        break;
 
7140
    }
 
7141
 
 
7142
    return result;
6907
7143
}
6908
7144
 
6909
7145
static SECStatus ssl3_SendClientSecondRound(sslSocket *ss);
6954
7190
                     ss->ssl3.clientCertChain  != NULL &&
6955
7191
                     ss->ssl3.clientPrivateKey != NULL;
6956
7192
 
 
7193
    if (!sendClientCert &&
 
7194
        ss->ssl3.hs.hashType == handshake_hash_single &&
 
7195
        ss->ssl3.hs.backupHash) {
 
7196
        /* Don't need the backup handshake hash. */
 
7197
        PK11_DestroyContext(ss->ssl3.hs.backupHash, PR_TRUE);
 
7198
        ss->ssl3.hs.backupHash = NULL;
 
7199
    }
 
7200
 
6957
7201
    /* We must wait for the server's certificate to be authenticated before
6958
7202
     * sending the client certificate in order to disclosing the client
6959
7203
     * certificate to an attacker that does not have a valid cert for the
6985
7229
    }
6986
7230
    if (ss->ssl3.hs.authCertificatePending &&
6987
7231
        (sendClientCert || ss->ssl3.sendEmptyCert || ss->firstHsDone)) {
 
7232
        SSL_TRC(3, ("%d: SSL3[%p]: deferring ssl3_SendClientSecondRound because"
 
7233
                    " certificate authentication is still pending.",
 
7234
                    SSL_GETPID(), ss->fd));
6988
7235
        ss->ssl3.hs.restartTarget = ssl3_SendClientSecondRound;
6989
7236
        return SECWouldBlock;
6990
7237
    }
7022
7269
        goto loser;     /* err code was set. */
7023
7270
    }
7024
7271
 
7025
 
    /* XXX: If the server's certificate hasn't been authenticated by this
7026
 
     * point, then we may be leaking this NPN message to an attacker.
 
7272
    /* This must be done after we've set ss->ssl3.cwSpec in
 
7273
     * ssl3_SendChangeCipherSpecs because SSL_GetChannelInfo uses information
 
7274
     * from cwSpec. This must be done before we call ssl3_CheckFalseStart
 
7275
     * because the false start callback (if any) may need the information from
 
7276
     * the functions that depend on this being set.
7027
7277
     */
 
7278
    ss->enoughFirstHsDone = PR_TRUE;
 
7279
 
7028
7280
    if (!ss->firstHsDone) {
 
7281
        /* XXX: If the server's certificate hasn't been authenticated by this
 
7282
         * point, then we may be leaking this NPN message to an attacker.
 
7283
         */
7029
7284
        rv = ssl3_SendNextProto(ss);
7030
7285
        if (rv != SECSuccess) {
7031
7286
            goto loser; /* err code was set. */
7032
7287
        }
 
7288
 
 
7289
        if (ss->opt.enableFalseStart) {
 
7290
            if (!ss->ssl3.hs.authCertificatePending) {
 
7291
                /* When we fix bug 589047, we will need to know whether we are
 
7292
                 * false starting before we try to flush the client second
 
7293
                 * round to the network. With that in mind, we purposefully
 
7294
                 * call ssl3_CheckFalseStart before calling ssl3_SendFinished,
 
7295
                 * which includes a call to ssl3_FlushHandshake, so that
 
7296
                 * no application develops a reliance on such flushing being
 
7297
                 * done before its false start callback is called.
 
7298
                 */
 
7299
                ssl_ReleaseXmitBufLock(ss);
 
7300
                rv = ssl3_CheckFalseStart(ss);
 
7301
                ssl_GetXmitBufLock(ss);
 
7302
                if (rv != SECSuccess) {
 
7303
                    goto loser;
 
7304
                }
 
7305
            } else {
 
7306
                /* The certificate authentication and the server's Finished
 
7307
                 * message are racing each other. If the certificate
 
7308
                 * authentication wins, then we will try to false start in
 
7309
                 * ssl3_AuthCertificateComplete.
 
7310
                 */
 
7311
                SSL_TRC(3, ("%d: SSL3[%p]: deferring false start check because"
 
7312
                            " certificate authentication is still pending.",
 
7313
                            SSL_GETPID(), ss->fd));
 
7314
            }
 
7315
        }
7033
7316
    }
7034
7317
 
7035
7318
    rv = ssl3_SendFinished(ss, 0);
7044
7327
    else
7045
7328
        ss->ssl3.hs.ws = wait_change_cipher;
7046
7329
 
7047
 
    /* Do the handshake callback for sslv3 here, if we can false start. */
7048
 
    if (ss->handshakeCallback != NULL && ssl3_CanFalseStart(ss)) {
7049
 
        (ss->handshakeCallback)(ss->fd, ss->handshakeCallbackData);
7050
 
    }
 
7330
    PORT_Assert(ssl3_WaitingForStartOfServerSecondRound(ss));
7051
7331
 
7052
7332
    return SECSuccess;
7053
7333
 
9074
9354
SECStatus
9075
9355
ssl3_HandleNewSessionTicket(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
9076
9356
{
9077
 
    SECStatus         rv;
9078
 
    NewSessionTicket  session_ticket;
 
9357
    SECStatus rv;
 
9358
    SECItem ticketData;
9079
9359
 
9080
9360
    SSL_TRC(3, ("%d: SSL3[%d]: handle session_ticket handshake",
9081
9361
                SSL_GETPID(), ss->fd));
9083
9363
    PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
9084
9364
    PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
9085
9365
 
 
9366
    PORT_Assert(!ss->ssl3.hs.newSessionTicket.ticket.data);
 
9367
    PORT_Assert(!ss->ssl3.hs.receivedNewSessionTicket);
 
9368
 
9086
9369
    if (ss->ssl3.hs.ws != wait_new_session_ticket) {
9087
9370
        SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9088
9371
        PORT_SetError(SSL_ERROR_RX_UNEXPECTED_NEW_SESSION_TICKET);
9089
9372
        return SECFailure;
9090
9373
    }
9091
9374
 
9092
 
    session_ticket.received_timestamp = ssl_Time();
 
9375
    /* RFC5077 Section 3.3: "The client MUST NOT treat the ticket as valid
 
9376
     * until it has verified the server's Finished message." See the comment in
 
9377
     * ssl3_FinishHandshake for more details.
 
9378
     */
 
9379
    ss->ssl3.hs.newSessionTicket.received_timestamp = ssl_Time();
9093
9380
    if (length < 4) {
9094
9381
        (void)SSL3_SendAlert(ss, alert_fatal, decode_error);
9095
9382
        PORT_SetError(SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET);
9096
9383
        return SECFailure;
9097
9384
    }
9098
 
    session_ticket.ticket_lifetime_hint =
 
9385
    ss->ssl3.hs.newSessionTicket.ticket_lifetime_hint =
9099
9386
        (PRUint32)ssl3_ConsumeHandshakeNumber(ss, 4, &b, &length);
9100
9387
 
9101
 
    rv = ssl3_ConsumeHandshakeVariable(ss, &session_ticket.ticket, 2,
9102
 
        &b, &length);
 
9388
    rv = ssl3_ConsumeHandshakeVariable(ss, &ticketData, 2, &b, &length);
9103
9389
    if (length != 0 || rv != SECSuccess) {
9104
9390
        (void)SSL3_SendAlert(ss, alert_fatal, decode_error);
9105
9391
        PORT_SetError(SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET);
9106
9392
        return SECFailure;  /* malformed */
9107
9393
    }
9108
 
 
9109
 
    rv = ssl3_SetSIDSessionTicket(ss->sec.ci.sid, &session_ticket);
 
9394
    rv = SECITEM_CopyItem(NULL, &ss->ssl3.hs.newSessionTicket.ticket,
 
9395
                          &ticketData);
9110
9396
    if (rv != SECSuccess) {
9111
 
        (void)SSL3_SendAlert(ss, alert_fatal, handshake_failure);
9112
 
        PORT_SetError(SSL_ERROR_INTERNAL_ERROR_ALERT);
9113
 
        return SECFailure;
 
9397
        return rv;
9114
9398
    }
 
9399
    ss->ssl3.hs.receivedNewSessionTicket = PR_TRUE;
 
9400
 
9115
9401
    ss->ssl3.hs.ws = wait_change_cipher;
9116
9402
    return SECSuccess;
9117
9403
}
9622
9908
 
9623
9909
            ss->ssl3.hs.authCertificatePending = PR_TRUE;
9624
9910
            rv = SECSuccess;
9625
 
 
9626
 
            /* XXX: Async cert validation and False Start don't work together
9627
 
             * safely yet; if we leave False Start enabled, we may end up false
9628
 
             * starting (sending application data) before we
9629
 
             * SSL_AuthCertificateComplete has been called.
9630
 
             */
9631
 
            ss->opt.enableFalseStart = PR_FALSE;
9632
9911
        }
9633
9912
 
9634
9913
        if (rv != SECSuccess) {
9752
10031
    } else if (ss->ssl3.hs.restartTarget != NULL) {
9753
10032
        sslRestartTarget target = ss->ssl3.hs.restartTarget;
9754
10033
        ss->ssl3.hs.restartTarget = NULL;
 
10034
 
 
10035
        if (target == ssl3_FinishHandshake) {
 
10036
            SSL_TRC(3,("%d: SSL3[%p]: certificate authentication lost the race"
 
10037
                       " with peer's finished message", SSL_GETPID(), ss->fd));
 
10038
        }
 
10039
 
9755
10040
        rv = target(ss);
9756
10041
        /* Even if we blocked here, we have accomplished enough to claim
9757
10042
         * success. Any remaining work will be taken care of by subsequent
9761
10046
            rv = SECSuccess;
9762
10047
        }
9763
10048
    } else {
9764
 
        rv = SECSuccess;
 
10049
        SSL_TRC(3, ("%d: SSL3[%p]: certificate authentication won the race with"
 
10050
                    " peer's finished message", SSL_GETPID(), ss->fd));
 
10051
 
 
10052
        PORT_Assert(!ss->ssl3.hs.isResuming);
 
10053
        PORT_Assert(ss->ssl3.hs.ws != idle_handshake);
 
10054
 
 
10055
        if (ss->opt.enableFalseStart &&
 
10056
            !ss->firstHsDone &&
 
10057
            !ss->ssl3.hs.isResuming &&
 
10058
            ssl3_WaitingForStartOfServerSecondRound(ss)) {
 
10059
            /* ssl3_SendClientSecondRound deferred the false start check because
 
10060
             * certificate authentication was pending, so we do it now if we still
 
10061
             * haven't received any of the server's second round yet.
 
10062
             */
 
10063
            rv = ssl3_CheckFalseStart(ss);
 
10064
        } else {
 
10065
            rv = SECSuccess;
 
10066
        }
9765
10067
    }
9766
10068
 
9767
10069
done:
9850
10152
    return rv;
9851
10153
}
9852
10154
 
9853
 
/* called from ssl3_HandleServerHelloDone
 
10155
/* called from ssl3_SendClientSecondRound
 
10156
 *             ssl3_HandleFinished
9854
10157
 */
9855
10158
static SECStatus
9856
10159
ssl3_SendNextProto(sslSocket *ss)
9943
10246
    return;
9944
10247
}
9945
10248
 
9946
 
/* called from ssl3_HandleServerHelloDone
 
10249
/* called from ssl3_SendClientSecondRound
9947
10250
 *             ssl3_HandleClientHello
9948
10251
 *             ssl3_HandleFinished
9949
10252
 */
10179
10482
         */
10180
10483
        if (isServer && !ss->ssl3.hs.isResuming &&
10181
10484
            ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn)) {
 
10485
            /* RFC 5077 Section 3.3: "In the case of a full handshake, the
 
10486
             * server MUST verify the client's Finished message before sending
 
10487
             * the ticket." Presumably, this also means that the client's
 
10488
             * certificate, if any, must be verified beforehand too.
 
10489
             */
10182
10490
            rv = ssl3_SendNewSessionTicket(ss);
10183
10491
            if (rv != SECSuccess) {
10184
10492
                goto xmit_loser;
10221
10529
        return rv;
10222
10530
    }
10223
10531
 
10224
 
    ss->gs.writeOffset = 0;
10225
 
    ss->gs.readOffset  = 0;
10226
 
 
10227
10532
    if (ss->ssl3.hs.kea_def->kea == kea_ecdhe_rsa) {
10228
10533
        effectiveExchKeyType = kt_rsa;
10229
10534
    } else {
10288
10593
    return rv;
10289
10594
}
10290
10595
 
 
10596
/* The return type is SECStatus instead of void because this function needs
 
10597
 * to have type sslRestartTarget.
 
10598
 */
10291
10599
SECStatus
10292
10600
ssl3_FinishHandshake(sslSocket * ss)
10293
10601
{
10297
10605
 
10298
10606
    /* The first handshake is now completed. */
10299
10607
    ss->handshake           = NULL;
10300
 
    ss->firstHsDone         = PR_TRUE;
 
10608
 
 
10609
    /* RFC 5077 Section 3.3: "The client MUST NOT treat the ticket as valid
 
10610
     * until it has verified the server's Finished message." When the server
 
10611
     * sends a NewSessionTicket in a resumption handshake, we must wait until
 
10612
     * the handshake is finished (we have verified the server's Finished
 
10613
     * AND the server's certificate) before we update the ticket in the sid.
 
10614
     *
 
10615
     * This must be done before we call (*ss->sec.cache)(ss->sec.ci.sid)
 
10616
     * because CacheSID requires the session ticket to already be set, and also
 
10617
     * because of the lazy lock creation scheme used by CacheSID and
 
10618
     * ssl3_SetSIDSessionTicket.
 
10619
     */
 
10620
    if (ss->ssl3.hs.receivedNewSessionTicket) {
 
10621
        PORT_Assert(!ss->sec.isServer);
 
10622
        ssl3_SetSIDSessionTicket(ss->sec.ci.sid, &ss->ssl3.hs.newSessionTicket);
 
10623
        /* The sid took over the ticket data */
 
10624
        PORT_Assert(!ss->ssl3.hs.newSessionTicket.ticket.data);
 
10625
        ss->ssl3.hs.receivedNewSessionTicket = PR_FALSE;
 
10626
    }
10301
10627
 
10302
10628
    if (ss->ssl3.hs.cacheSID) {
 
10629
        PORT_Assert(ss->sec.ci.sid->cached == never_cached);
10303
10630
        (*ss->sec.cache)(ss->sec.ci.sid);
10304
10631
        ss->ssl3.hs.cacheSID = PR_FALSE;
10305
10632
    }
10306
10633
 
 
10634
    ss->ssl3.hs.canFalseStart = PR_FALSE; /* False Start phase is complete */
10307
10635
    ss->ssl3.hs.ws = idle_handshake;
10308
10636
 
10309
 
    /* Do the handshake callback for sslv3 here, if we cannot false start. */
10310
 
    if (ss->handshakeCallback != NULL && !ssl3_CanFalseStart(ss)) {
10311
 
        (ss->handshakeCallback)(ss->fd, ss->handshakeCallbackData);
10312
 
    }
 
10637
    ssl_FinishHandshake(ss);
10313
10638
 
10314
10639
    return SECSuccess;
10315
10640
}
11274
11599
 
11275
11600
    ssl_ReleaseSSL3HandshakeLock(ss);
11276
11601
    return rv;
11277
 
 
11278
11602
}
11279
11603
 
11280
11604
/*
11373
11697
    ss->ssl3.hs.messages.buf = NULL;
11374
11698
    ss->ssl3.hs.messages.space = 0;
11375
11699
 
 
11700
    ss->ssl3.hs.receivedNewSessionTicket = PR_FALSE;
 
11701
    PORT_Memset(&ss->ssl3.hs.newSessionTicket, 0,
 
11702
                sizeof(ss->ssl3.hs.newSessionTicket));
 
11703
 
11376
11704
    ss->ssl3.initialized = PR_TRUE;
11377
11705
    return SECSuccess;
11378
11706
}
11696
12024
    /* free the SSL3Buffer (msg_body) */
11697
12025
    PORT_Free(ss->ssl3.hs.msg_body.buf);
11698
12026
 
 
12027
    SECITEM_FreeItem(&ss->ssl3.hs.newSessionTicket.ticket, PR_FALSE);
 
12028
 
11699
12029
    /* free up the CipherSpecs */
11700
12030
    ssl3_DestroyCipherSpec(&ss->ssl3.specs[0], PR_TRUE/*freeSrvName*/);
11701
12031
    ssl3_DestroyCipherSpec(&ss->ssl3.specs[1], PR_TRUE/*freeSrvName*/);