Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(711)

Side by Side Diff: net/third_party/nss/ssl/ssl3con.c

Issue 14522022: Update NSS libSSL to NSS_3_15_BETA2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Adjust secitemarray.patch, remove handlecertloser.patch Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* 2 /*
3 * SSL3 Protocol 3 * SSL3 Protocol
4 * 4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public 5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 /* $Id: ssl3con.c,v 1.192 2012/09/28 05:10:25 wtc%google.com Exp $ */ 8 /* $Id$ */
9 9
10 /* TODO(ekr): Implement HelloVerifyRequest on server side. OK for now. */ 10 /* TODO(ekr): Implement HelloVerifyRequest on server side. OK for now. */
11 11
12 #include "cert.h" 12 #include "cert.h"
13 #include "ssl.h" 13 #include "ssl.h"
14 #include "cryptohi.h" /* for DSAU_ stuff */ 14 #include "cryptohi.h" /* for DSAU_ stuff */
15 #include "keyhi.h" 15 #include "keyhi.h"
16 #include "secder.h" 16 #include "secder.h"
17 #include "secitem.h" 17 #include "secitem.h"
18 18
(...skipping 24 matching lines...) Expand all
43 43
44 static void ssl3_CleanupPeerCerts(sslSocket *ss); 44 static void ssl3_CleanupPeerCerts(sslSocket *ss);
45 static void ssl3_CopyPeerCertsFromSID(sslSocket *ss, sslSessionID *sid); 45 static void ssl3_CopyPeerCertsFromSID(sslSocket *ss, sslSessionID *sid);
46 static PK11SymKey *ssl3_GenerateRSAPMS(sslSocket *ss, ssl3CipherSpec *spec, 46 static PK11SymKey *ssl3_GenerateRSAPMS(sslSocket *ss, ssl3CipherSpec *spec,
47 PK11SlotInfo * serverKeySlot); 47 PK11SlotInfo * serverKeySlot);
48 static SECStatus ssl3_DeriveMasterSecret(sslSocket *ss, PK11SymKey *pms); 48 static SECStatus ssl3_DeriveMasterSecret(sslSocket *ss, PK11SymKey *pms);
49 static SECStatus ssl3_DeriveConnectionKeysPKCS11(sslSocket *ss); 49 static SECStatus ssl3_DeriveConnectionKeysPKCS11(sslSocket *ss);
50 static SECStatus ssl3_HandshakeFailure( sslSocket *ss); 50 static SECStatus ssl3_HandshakeFailure( sslSocket *ss);
51 static SECStatus ssl3_InitState( sslSocket *ss); 51 static SECStatus ssl3_InitState( sslSocket *ss);
52 static SECStatus ssl3_SendCertificate( sslSocket *ss); 52 static SECStatus ssl3_SendCertificate( sslSocket *ss);
53 static SECStatus ssl3_SendCertificateStatus( sslSocket *ss);
53 static SECStatus ssl3_SendEmptyCertificate( sslSocket *ss); 54 static SECStatus ssl3_SendEmptyCertificate( sslSocket *ss);
54 static SECStatus ssl3_SendCertificateRequest(sslSocket *ss); 55 static SECStatus ssl3_SendCertificateRequest(sslSocket *ss);
55 static SECStatus ssl3_SendNextProto( sslSocket *ss); 56 static SECStatus ssl3_SendNextProto( sslSocket *ss);
56 static SECStatus ssl3_SendEncryptedExtensions(sslSocket *ss); 57 static SECStatus ssl3_SendEncryptedExtensions(sslSocket *ss);
57 static SECStatus ssl3_SendFinished( sslSocket *ss, PRInt32 flags); 58 static SECStatus ssl3_SendFinished( sslSocket *ss, PRInt32 flags);
58 static SECStatus ssl3_SendServerHello( sslSocket *ss); 59 static SECStatus ssl3_SendServerHello( sslSocket *ss);
59 static SECStatus ssl3_SendServerHelloDone( sslSocket *ss); 60 static SECStatus ssl3_SendServerHelloDone( sslSocket *ss);
60 static SECStatus ssl3_SendServerKeyExchange( sslSocket *ss); 61 static SECStatus ssl3_SendServerKeyExchange( sslSocket *ss);
61 static SECStatus ssl3_NewHandshakeHashes( sslSocket *ss); 62 static SECStatus ssl3_NewHandshakeHashes( sslSocket *ss);
62 static SECStatus ssl3_UpdateHandshakeHashes( sslSocket *ss, 63 static SECStatus ssl3_UpdateHandshakeHashes( sslSocket *ss,
(...skipping 4114 matching lines...) Expand 10 before | Expand all | Expand 10 after
4177 if (rv != SECSuccess) { 4178 if (rv != SECSuccess) {
4178 return rv; /* ssl3_InitState has set the error code. */ 4179 return rv; /* ssl3_InitState has set the error code. */
4179 } 4180 }
4180 ss->ssl3.hs.sendingSCSV = PR_FALSE; /* Must be reset every handshake */ 4181 ss->ssl3.hs.sendingSCSV = PR_FALSE; /* Must be reset every handshake */
4181 PORT_Assert(IS_DTLS(ss) || !resending); 4182 PORT_Assert(IS_DTLS(ss) || !resending);
4182 4183
4183 /* We might be starting a session renegotiation in which case we should 4184 /* We might be starting a session renegotiation in which case we should
4184 * clear previous state. 4185 * clear previous state.
4185 */ 4186 */
4186 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData)); 4187 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData));
4187 ss->ssl3.hs.may_get_cert_status = PR_FALSE;
4188 if (ss->ssl3.hs.cert_status.data) {
4189 SECITEM_FreeItem(&ss->ssl3.hs.cert_status, PR_FALSE);
4190 }
wtc 2013/04/29 18:09:59 This is rsleevi's CL: https://chromiumcodereview.a
4191 4188
4192 SSL_TRC(30,("%d: SSL3[%d]: reset handshake hashes", 4189 SSL_TRC(30,("%d: SSL3[%d]: reset handshake hashes",
4193 SSL_GETPID(), ss->fd )); 4190 SSL_GETPID(), ss->fd ));
4194 rv = ssl3_RestartHandshakeHashes(ss); 4191 rv = ssl3_RestartHandshakeHashes(ss);
4195 if (rv != SECSuccess) { 4192 if (rv != SECSuccess) {
4196 return rv; 4193 return rv;
4197 } 4194 }
4198 4195
4199 /* 4196 /*
4200 * During a renegotiation, ss->clientHelloVersion will be used again to 4197 * During a renegotiation, ss->clientHelloVersion will be used again to
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
4391 if (extLen < 0) { 4388 if (extLen < 0) {
4392 return SECFailure; 4389 return SECFailure;
4393 } 4390 }
4394 maxBytes -= extLen; 4391 maxBytes -= extLen;
4395 total_exten_len += extLen; 4392 total_exten_len += extLen;
4396 4393
4397 if (total_exten_len > 0) 4394 if (total_exten_len > 0)
4398 total_exten_len += 2; 4395 total_exten_len += 2;
4399 } 4396 }
4400 4397
4401 #if defined(NSS_ENABLE_ECC) && !defined(NSS_ECC_MORE_THAN_SUITE_B) 4398 #if defined(NSS_ENABLE_ECC)
4402 if (!total_exten_len || !isTLS) { 4399 if (!total_exten_len || !isTLS) {
4403 /* not sending the elliptic_curves and ec_point_formats extensions */ 4400 /* not sending the elliptic_curves and ec_point_formats extensions */
4404 ssl3_DisableECCSuites(ss, NULL); /* disable all ECC suites */ 4401 ssl3_DisableECCSuites(ss, NULL); /* disable all ECC suites */
4405 } 4402 }
4406 #endif 4403 #endif
4407 4404
4408 if (IS_DTLS(ss)) { 4405 if (IS_DTLS(ss)) {
4409 ssl3_DisableNonDTLSSuites(ss); 4406 ssl3_DisableNonDTLSSuites(ss);
4410 } 4407 }
4411 4408
(...skipping 947 matching lines...) Expand 10 before | Expand all | Expand 10 after
5359 rv = ssl3_ComputeHandshakeHashes(ss, ss->ssl3.pwSpec, &hashes, 0); 5356 rv = ssl3_ComputeHandshakeHashes(ss, ss->ssl3.pwSpec, &hashes, 0);
5360 ssl_ReleaseSpecReadLock(ss); 5357 ssl_ReleaseSpecReadLock(ss);
5361 if (rv != SECSuccess) { 5358 if (rv != SECSuccess) {
5362 goto done; /* err code was set by ssl3_ComputeHandshakeHashes */ 5359 goto done; /* err code was set by ssl3_ComputeHandshakeHashes */
5363 } 5360 }
5364 5361
5365 isTLS = (PRBool)(ss->ssl3.pwSpec->version > SSL_LIBRARY_VERSION_3_0); 5362 isTLS = (PRBool)(ss->ssl3.pwSpec->version > SSL_LIBRARY_VERSION_3_0);
5366 if (ss->ssl3.platformClientKey) { 5363 if (ss->ssl3.platformClientKey) {
5367 #ifdef NSS_PLATFORM_CLIENT_AUTH 5364 #ifdef NSS_PLATFORM_CLIENT_AUTH
5368 rv = ssl3_PlatformSignHashes( 5365 rv = ssl3_PlatformSignHashes(
5369 » &hashes, ss->ssl3.platformClientKey, &buf, isTLS, 5366 » &hashes, ss->ssl3.platformClientKey, &buf, isTLS,
5370 » CERT_GetCertKeyType(&ss->ssl3.clientCertificate->subjectPublicKeyInf o)); 5367 » CERT_GetCertKeyType(
5368 » » &ss->ssl3.clientCertificate->subjectPublicKeyInfo));
5371 ssl_FreePlatformKey(ss->ssl3.platformClientKey); 5369 ssl_FreePlatformKey(ss->ssl3.platformClientKey);
5372 ss->ssl3.platformClientKey = (PlatformKey)NULL; 5370 ss->ssl3.platformClientKey = (PlatformKey)NULL;
5373 #endif /* NSS_PLATFORM_CLIENT_AUTH */ 5371 #endif /* NSS_PLATFORM_CLIENT_AUTH */
5374 } else { 5372 } else {
5375 rv = ssl3_SignHashes(&hashes, ss->ssl3.clientPrivateKey, &buf, isTLS); 5373 rv = ssl3_SignHashes(&hashes, ss->ssl3.clientPrivateKey, &buf, isTLS);
5376 if (rv == SECSuccess) { 5374 if (rv == SECSuccess) {
5377 PK11SlotInfo * slot; 5375 PK11SlotInfo * slot;
5378 sslSessionID * sid = ss->sec.ci.sid; 5376 sslSessionID * sid = ss->sec.ci.sid;
5379 5377
5380 /* Remember the info about the slot that did the signing. 5378 /* Remember the info about the slot that did the signing.
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
5734 ss->ssl3.hs.ws = wait_change_cipher; 5732 ss->ssl3.hs.ws = wait_change_cipher;
5735 5733
5736 ss->ssl3.hs.isResuming = PR_TRUE; 5734 ss->ssl3.hs.isResuming = PR_TRUE;
5737 5735
5738 /* copy the peer cert from the SID */ 5736 /* copy the peer cert from the SID */
5739 if (sid->peerCert != NULL) { 5737 if (sid->peerCert != NULL) {
5740 ss->sec.peerCert = CERT_DupCertificate(sid->peerCert); 5738 ss->sec.peerCert = CERT_DupCertificate(sid->peerCert);
5741 ssl3_CopyPeerCertsFromSID(ss, sid); 5739 ssl3_CopyPeerCertsFromSID(ss, sid);
5742 } 5740 }
5743 5741
5744
5745 /* NULL value for PMS signifies re-use of the old MS */ 5742 /* NULL value for PMS signifies re-use of the old MS */
5746 rv = ssl3_InitPendingCipherSpec(ss, NULL); 5743 rv = ssl3_InitPendingCipherSpec(ss, NULL);
5747 if (rv != SECSuccess) { 5744 if (rv != SECSuccess) {
5748 goto alert_loser; /* err code was set */ 5745 goto alert_loser; /* err code was set */
5749 } 5746 }
5750 goto winner; 5747 goto winner;
5751 } while (0); 5748 } while (0);
5752 5749
5753 if (sid_match) 5750 if (sid_match)
5754 SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_cache_not_ok ); 5751 SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_cache_not_ok );
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
6179 /* XXX Should pass cert_types in this call!! */ 6176 /* XXX Should pass cert_types in this call!! */
6180 rv = (SECStatus)(*ss->getPlatformClientAuthData)( 6177 rv = (SECStatus)(*ss->getPlatformClientAuthData)(
6181 ss->getPlatformClientAuthDataArg, 6178 ss->getPlatformClientAuthDataArg,
6182 ss->fd, &ca_list, 6179 ss->fd, &ca_list,
6183 &platform_cert_list, 6180 &platform_cert_list,
6184 (void**)&ss->ssl3.platformClientKey, 6181 (void**)&ss->ssl3.platformClientKey,
6185 &ss->ssl3.clientCertificate, 6182 &ss->ssl3.clientCertificate,
6186 &ss->ssl3.clientPrivateKey); 6183 &ss->ssl3.clientPrivateKey);
6187 } else 6184 } else
6188 #endif 6185 #endif
6189 if (ss->getClientAuthData == NULL) { 6186 if (ss->getClientAuthData != NULL) {
6190 » rv = SECFailure; /* force it to send a no_certificate alert */
6191 } else {
6192 /* XXX Should pass cert_types in this call!! */ 6187 /* XXX Should pass cert_types in this call!! */
6193 rv = (SECStatus)(*ss->getClientAuthData)(ss->getClientAuthDataArg, 6188 rv = (SECStatus)(*ss->getClientAuthData)(ss->getClientAuthDataArg,
6194 ss->fd, &ca_list, 6189 ss->fd, &ca_list,
6195 &ss->ssl3.clientCertificate, 6190 &ss->ssl3.clientCertificate,
6196 &ss->ssl3.clientPrivateKey); 6191 &ss->ssl3.clientPrivateKey);
6192 } else {
6193 rv = SECFailure; /* force it to send a no_certificate alert */
6197 } 6194 }
6198 6195
6199 switch (rv) { 6196 switch (rv) {
6200 case SECWouldBlock: /* getClientAuthData has put up a dialog box. */ 6197 case SECWouldBlock: /* getClientAuthData has put up a dialog box. */
6201 ssl3_SetAlwaysBlock(ss); 6198 ssl3_SetAlwaysBlock(ss);
6202 break; /* not an error */ 6199 break; /* not an error */
6203 6200
6204 case SECSuccess: 6201 case SECSuccess:
6205 #ifdef NSS_PLATFORM_CLIENT_AUTH 6202 #ifdef NSS_PLATFORM_CLIENT_AUTH
6206 if (!platform_cert_list || CERT_LIST_EMPTY(platform_cert_list) || 6203 if (!platform_cert_list || CERT_LIST_EMPTY(platform_cert_list) ||
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
6698 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) ); 6695 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) );
6699 6696
6700 rv = ssl3_SendServerHello(ss); 6697 rv = ssl3_SendServerHello(ss);
6701 if (rv != SECSuccess) { 6698 if (rv != SECSuccess) {
6702 return rv; /* err code is set. */ 6699 return rv; /* err code is set. */
6703 } 6700 }
6704 rv = ssl3_SendCertificate(ss); 6701 rv = ssl3_SendCertificate(ss);
6705 if (rv != SECSuccess) { 6702 if (rv != SECSuccess) {
6706 return rv; /* error code is set. */ 6703 return rv; /* error code is set. */
6707 } 6704 }
6705 rv = ssl3_SendCertificateStatus(ss);
6706 if (rv != SECSuccess) {
6707 return rv; /* error code is set. */
6708 }
6708 /* We have to do this after the call to ssl3_SendServerHello, 6709 /* We have to do this after the call to ssl3_SendServerHello,
6709 * because kea_def is set up by ssl3_SendServerHello(). 6710 * because kea_def is set up by ssl3_SendServerHello().
6710 */ 6711 */
6711 kea_def = ss->ssl3.hs.kea_def; 6712 kea_def = ss->ssl3.hs.kea_def;
6712 ss->ssl3.hs.usedStepDownKey = PR_FALSE; 6713 ss->ssl3.hs.usedStepDownKey = PR_FALSE;
6713 6714
6714 if (kea_def->is_limited && kea_def->exchKeyType == kt_rsa) { 6715 if (kea_def->is_limited && kea_def->exchKeyType == kt_rsa) {
6715 /* see if we can legally use the key in the cert. */ 6716 /* see if we can legally use the key in the cert. */
6716 int keyLen; /* bytes */ 6717 int keyLen; /* bytes */
6717 6718
(...skipping 1923 matching lines...) Expand 10 before | Expand all | Expand 10 after
8641 #endif 8642 #endif
8642 if (rv != SECSuccess) { 8643 if (rv != SECSuccess) {
8643 return rv; /* err set by AppendHandshake. */ 8644 return rv; /* err set by AppendHandshake. */
8644 } 8645 }
8645 } 8646 }
8646 } 8647 }
8647 8648
8648 return SECSuccess; 8649 return SECSuccess;
8649 } 8650 }
8650 8651
8652 /*
8653 * Used by server only.
8654 * single-stapling, send only a single cert status
8655 */
8656 static SECStatus
8657 ssl3_SendCertificateStatus(sslSocket *ss)
8658 {
8659 SECStatus rv;
8660 CERTCertificateList *certChain;
8661 int len = 0;
8662 int i;
8663 SSL3KEAType certIndex;
8664
8665 SSL_TRC(3, ("%d: SSL3[%d]: send certificate status handshake",
8666 SSL_GETPID(), ss->fd));
8667
8668 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss));
8669 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
8670
8671 if (!ssl3_ExtensionNegotiated(ss, ssl_cert_status_xtn))
8672 return SECSuccess;
8673
8674 if (!ss->certStatusArray)
8675 return SECSuccess;
8676
8677 /* Use the array's first item only (single stapling) */
8678 len = 1 + ss->certStatusArray->items[0].len + 3;
8679
8680 rv = ssl3_AppendHandshakeHeader(ss, certificate_status, len);
8681 if (rv != SECSuccess) {
8682 return rv; /* err set by AppendHandshake. */
8683 }
8684 rv = ssl3_AppendHandshakeNumber(ss, 1 /*ocsp*/, 1);
8685 if (rv != SECSuccess)
8686 return rv; /* err set by AppendHandshake. */
8687
8688 rv = ssl3_AppendHandshakeVariable(ss,
8689 ss->certStatusArray->items[0].data,
8690 ss->certStatusArray->items[0].len,
8691 3);
8692 if (rv != SECSuccess)
8693 return rv; /* err set by AppendHandshake. */
8694
8695 return SECSuccess;
8696 }
8697
8651 /* This is used to delete the CA certificates in the peer certificate chain 8698 /* This is used to delete the CA certificates in the peer certificate chain
8652 * from the cert database after they've been validated. 8699 * from the cert database after they've been validated.
8653 */ 8700 */
8654 static void 8701 static void
8655 ssl3_CleanupPeerCerts(sslSocket *ss) 8702 ssl3_CleanupPeerCerts(sslSocket *ss)
8656 { 8703 {
8657 PRArenaPool * arena = ss->ssl3.peerCertArena; 8704 PRArenaPool * arena = ss->ssl3.peerCertArena;
8658 ssl3CertNode *certs = (ssl3CertNode *)ss->ssl3.peerCertChain; 8705 ssl3CertNode *certs = (ssl3CertNode *)ss->ssl3.peerCertChain;
8659 8706
8660 for (; certs; certs = certs->next) { 8707 for (; certs; certs = certs->next) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
8706 /* Called from ssl3_HandleHandshakeMessage() when it has deciphered a complete 8753 /* Called from ssl3_HandleHandshakeMessage() when it has deciphered a complete
8707 * ssl3 CertificateStatus message. 8754 * ssl3 CertificateStatus message.
8708 * Caller must hold Handshake and RecvBuf locks. 8755 * Caller must hold Handshake and RecvBuf locks.
8709 * This is always called before ssl3_HandleCertificate, even if the Certificate 8756 * This is always called before ssl3_HandleCertificate, even if the Certificate
8710 * message is sent first. 8757 * message is sent first.
8711 */ 8758 */
8712 static SECStatus 8759 static SECStatus
8713 ssl3_HandleCertificateStatus(sslSocket *ss, SSL3Opaque *b, PRUint32 length) 8760 ssl3_HandleCertificateStatus(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
8714 { 8761 {
8715 PRInt32 status, len; 8762 PRInt32 status, len;
8716 int errCode; 8763 PORT_Assert(ss->ssl3.hs.ws == wait_certificate_status);
8717 SSL3AlertDescription desc;
8718
8719 if (!ss->ssl3.hs.may_get_cert_status ||
8720 » ss->ssl3.hs.ws != wait_server_cert ||
8721 » !ss->ssl3.hs.pending_cert_msg.data ||
8722 » ss->ssl3.hs.cert_status.data) {
8723 » errCode = SSL_ERROR_RX_UNEXPECTED_CERT_STATUS;
8724 » desc = unexpected_message;
8725 » goto alert_loser;
8726 }
8727 8764
8728 /* Consume the CertificateStatusType enum */ 8765 /* Consume the CertificateStatusType enum */
8729 status = ssl3_ConsumeHandshakeNumber(ss, 1, &b, &length); 8766 status = ssl3_ConsumeHandshakeNumber(ss, 1, &b, &length);
8730 if (status != 1 /* ocsp */) { 8767 if (status != 1 /* ocsp */) {
8731 » goto format_loser; 8768 goto format_loser;
8732 } 8769 }
8733 8770
8734 len = ssl3_ConsumeHandshakeNumber(ss, 3, &b, &length); 8771 len = ssl3_ConsumeHandshakeNumber(ss, 3, &b, &length);
8735 if (len != length) { 8772 if (len != length) {
8736 » goto format_loser; 8773 goto format_loser;
8737 } 8774 }
8738 8775
8739 if (SECITEM_AllocItem(NULL, &ss->ssl3.hs.cert_status, length) == NULL) { 8776 #define MAX_CERTSTATUS_LEN 0x1ffff /* 128k - 1 */
8777 if (length > MAX_CERTSTATUS_LEN)
8778 goto format_loser;
8779 #undef MAX_CERTSTATUS_LEN
8780
8781 /* Array size 1, because we currently implement single-stapling only*/
8782 SECITEM_AllocArray(NULL, &ss->sec.ci.sid->peerCertStatus, 1);
8783 if (!ss->sec.ci.sid->peerCertStatus.items)
8784 return SECFailure;
8785
8786 ss->sec.ci.sid->peerCertStatus.items[0].data = PORT_Alloc(length);
8787
8788 if (!ss->sec.ci.sid->peerCertStatus.items[0].data) {
8789 SECITEM_FreeArray(&ss->sec.ci.sid->peerCertStatus, PR_FALSE);
8740 return SECFailure; 8790 return SECFailure;
8741 } 8791 }
8742 ss->ssl3.hs.cert_status.type = siBuffer;
8743 PORT_Memcpy(ss->ssl3.hs.cert_status.data, b, length);
8744 8792
8793 PORT_Memcpy(ss->sec.ci.sid->peerCertStatus.items[0].data, b, length);
8794 ss->sec.ci.sid->peerCertStatus.items[0].len = length;
8795 ss->sec.ci.sid->peerCertStatus.items[0].type = siBuffer;
8745 return SECSuccess; 8796 return SECSuccess;
8746 8797
8747 format_loser: 8798 format_loser:
8748 errCode = SSL_ERROR_BAD_CERT_STATUS_RESPONSE_ALERT; 8799 return ssl3_DecodeError(ss);
8749 desc = bad_certificate_status_response; 8800 }
8750 8801
8751 alert_loser: 8802 static SECStatus ssl3_AuthCertificate(sslSocket *ss);
8752 (void)SSL3_SendAlert(ss, alert_fatal, desc);
8753 (void)ssl_MapLowLevelError(errCode);
8754 return SECFailure;
8755 }
8756 8803
8757 /* Called from ssl3_HandleHandshakeMessage() when it has deciphered a complete 8804 /* Called from ssl3_HandleHandshakeMessage() when it has deciphered a complete
8758 * ssl3 Certificate message. 8805 * ssl3 Certificate message.
8759 * Caller must hold Handshake and RecvBuf locks. 8806 * Caller must hold Handshake and RecvBuf locks.
8760 */ 8807 */
8761 static SECStatus 8808 static SECStatus
8762 ssl3_HandleCertificate(sslSocket *ss, SSL3Opaque *b, PRUint32 length) 8809 ssl3_HandleCertificate(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
8763 { 8810 {
8764 ssl3CertNode * c; 8811 ssl3CertNode * c;
8765 ssl3CertNode * lastCert = NULL; 8812 ssl3CertNode * lastCert = NULL;
8766 PRInt32 remaining = 0; 8813 PRInt32 remaining = 0;
8767 PRInt32 size; 8814 PRInt32 size;
8768 SECStatus rv; 8815 SECStatus rv;
8769 PRBool isServer = (PRBool)(!!ss->sec.isServer); 8816 PRBool isServer = (PRBool)(!!ss->sec.isServer);
8770 PRBool trusted = PR_FALSE;
8771 PRBool isTLS; 8817 PRBool isTLS;
8772 SSL3AlertDescription desc; 8818 SSL3AlertDescription desc;
8773 int errCode = SSL_ERROR_RX_MALFORMED_CERTIFICATE; 8819 int errCode = SSL_ERROR_RX_MALFORMED_CERTIFICATE;
8774 SECItem certItem; 8820 SECItem certItem;
8775 8821
8776 SSL_TRC(3, ("%d: SSL3[%d]: handle certificate handshake", 8822 SSL_TRC(3, ("%d: SSL3[%d]: handle certificate handshake",
8777 SSL_GETPID(), ss->fd)); 8823 SSL_GETPID(), ss->fd));
8778 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) ); 8824 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
8779 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) ); 8825 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
8780 8826
(...skipping 22 matching lines...) Expand all
8803 */ 8849 */
8804 if (length) { 8850 if (length) {
8805 remaining = ssl3_ConsumeHandshakeNumber(ss, 3, &b, &length); 8851 remaining = ssl3_ConsumeHandshakeNumber(ss, 3, &b, &length);
8806 if (remaining < 0) 8852 if (remaining < 0)
8807 goto loser; /* fatal alert already sent by ConsumeHandshake. */ 8853 goto loser; /* fatal alert already sent by ConsumeHandshake. */
8808 if ((PRUint32)remaining > length) 8854 if ((PRUint32)remaining > length)
8809 goto decode_loser; 8855 goto decode_loser;
8810 } 8856 }
8811 8857
8812 if (!remaining) { 8858 if (!remaining) {
8813 » if (!(isTLS && isServer)) 8859 » if (!(isTLS && isServer)) {
8860 » desc = bad_certificate;
8814 goto alert_loser; 8861 goto alert_loser;
8862 }
8815 /* This is TLS's version of a no_certificate alert. */ 8863 /* This is TLS's version of a no_certificate alert. */
8816 /* I'm a server. I've requested a client cert. He hasn't got one. */ 8864 /* I'm a server. I've requested a client cert. He hasn't got one. */
8817 rv = ssl3_HandleNoCertificate(ss); 8865 rv = ssl3_HandleNoCertificate(ss);
8818 if (rv != SECSuccess) { 8866 if (rv != SECSuccess) {
8819 errCode = PORT_GetError(); 8867 errCode = PORT_GetError();
8820 goto loser; 8868 goto loser;
8821 } 8869 }
8822 » goto server_no_cert; 8870 ss->ssl3.hs.ws = wait_client_key;
8871 return SECSuccess;
8823 } 8872 }
8824 8873
8825 ss->ssl3.peerCertArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); 8874 ss->ssl3.peerCertArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
8826 if (ss->ssl3.peerCertArena == NULL) { 8875 if (ss->ssl3.peerCertArena == NULL) {
8827 goto loser; /* don't send alerts on memory errors */ 8876 goto loser; /* don't send alerts on memory errors */
8828 } 8877 }
8829 8878
8830 /* First get the peer cert. */ 8879 /* First get the peer cert. */
8831 remaining -= 3; 8880 remaining -= 3;
8832 if (remaining < 0) 8881 if (remaining < 0)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
8877 if (c == NULL) { 8926 if (c == NULL) {
8878 goto loser; /* don't send alerts on memory errors */ 8927 goto loser; /* don't send alerts on memory errors */
8879 } 8928 }
8880 8929
8881 c->cert = CERT_NewTempCertificate(ss->dbHandle, &certItem, NULL, 8930 c->cert = CERT_NewTempCertificate(ss->dbHandle, &certItem, NULL,
8882 PR_FALSE, PR_TRUE); 8931 PR_FALSE, PR_TRUE);
8883 if (c->cert == NULL) { 8932 if (c->cert == NULL) {
8884 goto ambiguous_err; 8933 goto ambiguous_err;
8885 } 8934 }
8886 8935
8887 if (c->cert->trust)
8888 trusted = PR_TRUE;
8889
8890 c->next = NULL; 8936 c->next = NULL;
8891 if (lastCert) { 8937 if (lastCert) {
8892 lastCert->next = c; 8938 lastCert->next = c;
8893 } else { 8939 } else {
8894 ss->ssl3.peerCertChain = c; 8940 ss->ssl3.peerCertChain = c;
8895 } 8941 }
8896 lastCert = c; 8942 lastCert = c;
8897 } 8943 }
8898 8944
8899 if (remaining != 0) 8945 if (remaining != 0)
8900 goto decode_loser; 8946 goto decode_loser;
8901 8947
8902 SECKEY_UpdateCertPQG(ss->sec.peerCert); 8948 SECKEY_UpdateCertPQG(ss->sec.peerCert);
8903 8949
8950 if (!isServer && ssl3_ExtensionNegotiated(ss, ssl_cert_status_xtn)) {
8951 ss->ssl3.hs.ws = wait_certificate_status;
8952 rv = SECSuccess;
8953 } else {
8954 rv = ssl3_AuthCertificate(ss); /* sets ss->ssl3.hs.ws */
8955 }
8956
8957 return rv;
8958
8959 ambiguous_err:
8960 errCode = PORT_GetError();
8961 switch (errCode) {
8962 case PR_OUT_OF_MEMORY_ERROR:
8963 case SEC_ERROR_BAD_DATABASE:
8964 case SEC_ERROR_NO_MEMORY:
8965 if (isTLS) {
8966 desc = internal_error;
8967 goto alert_loser;
8968 }
8969 goto loser;
8970 }
8971 ssl3_SendAlertForCertError(ss, errCode);
8972 goto loser;
8973
8974 decode_loser:
8975 desc = isTLS ? decode_error : bad_certificate;
8976
8977 alert_loser:
8978 (void)SSL3_SendAlert(ss, alert_fatal, desc);
8979
8980 loser:
8981 (void)ssl_MapLowLevelError(errCode);
8982 return SECFailure;
8983 }
8984
8985 static SECStatus
8986 ssl3_AuthCertificate(sslSocket *ss)
8987 {
8988 SECStatus rv;
8989 PRBool isServer = (PRBool)(!!ss->sec.isServer);
8990 int errCode;
8991
8904 ss->ssl3.hs.authCertificatePending = PR_FALSE; 8992 ss->ssl3.hs.authCertificatePending = PR_FALSE;
8905 8993
8906 /* 8994 /*
8907 * Ask caller-supplied callback function to validate cert chain. 8995 * Ask caller-supplied callback function to validate cert chain.
8908 */ 8996 */
8909 rv = (SECStatus)(*ss->authCertificate)(ss->authCertificateArg, ss->fd, 8997 rv = (SECStatus)(*ss->authCertificate)(ss->authCertificateArg, ss->fd,
8910 PR_TRUE, isServer); 8998 PR_TRUE, isServer);
8911 if (rv) { 8999 if (rv) {
8912 errCode = PORT_GetError(); 9000 errCode = PORT_GetError();
8913 if (rv != SECWouldBlock) { 9001 if (rv != SECWouldBlock) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
8991 if (ss->ssl3.hs.kea_def->is_limited || 9079 if (ss->ssl3.hs.kea_def->is_limited ||
8992 /* XXX OR server cert is signing only. */ 9080 /* XXX OR server cert is signing only. */
8993 #ifdef NSS_ENABLE_ECC 9081 #ifdef NSS_ENABLE_ECC
8994 ss->ssl3.hs.kea_def->kea == kea_ecdhe_ecdsa || 9082 ss->ssl3.hs.kea_def->kea == kea_ecdhe_ecdsa ||
8995 ss->ssl3.hs.kea_def->kea == kea_ecdhe_rsa || 9083 ss->ssl3.hs.kea_def->kea == kea_ecdhe_rsa ||
8996 #endif /* NSS_ENABLE_ECC */ 9084 #endif /* NSS_ENABLE_ECC */
8997 ss->ssl3.hs.kea_def->exchKeyType == kt_dh) { 9085 ss->ssl3.hs.kea_def->exchKeyType == kt_dh) {
8998 ss->ssl3.hs.ws = wait_server_key; /* allow server_key_exchange */ 9086 ss->ssl3.hs.ws = wait_server_key; /* allow server_key_exchange */
8999 } 9087 }
9000 } else { 9088 } else {
9001 server_no_cert:
9002 ss->ssl3.hs.ws = wait_client_key; 9089 ss->ssl3.hs.ws = wait_client_key;
9003 } 9090 }
9004 9091
9005 PORT_Assert(rv == SECSuccess); 9092 PORT_Assert(rv == SECSuccess);
9006 if (rv != SECSuccess) { 9093 if (rv != SECSuccess) {
9007 errCode = SEC_ERROR_LIBRARY_FAILURE; 9094 errCode = SEC_ERROR_LIBRARY_FAILURE;
9008 rv = SECFailure; 9095 rv = SECFailure;
9009 goto loser; 9096 goto loser;
9010 } 9097 }
9011 9098
9012 return rv; 9099 return rv;
9013 9100
9014 ambiguous_err:
9015 errCode = PORT_GetError();
9016 switch (errCode) {
9017 case PR_OUT_OF_MEMORY_ERROR:
9018 case SEC_ERROR_BAD_DATABASE:
9019 case SEC_ERROR_NO_MEMORY:
9020 if (isTLS) {
9021 desc = internal_error;
9022 goto alert_loser;
9023 }
9024 goto loser;
9025 }
9026 ssl3_SendAlertForCertError(ss, errCode);
9027 goto loser;
9028
9029 decode_loser:
9030 desc = isTLS ? decode_error : bad_certificate;
9031
9032 alert_loser:
9033 (void)SSL3_SendAlert(ss, alert_fatal, desc);
9034
9035 loser: 9101 loser:
9036 ssl3_CleanupPeerCerts(ss);
9037
9038 if (ss->sec.peerCert != NULL) {
9039 CERT_DestroyCertificate(ss->sec.peerCert);
9040 ss->sec.peerCert = NULL;
9041 }
wtc 2013/04/29 18:09:59 I noticed that the new code is no longer deleting
wtc 2013/05/01 23:58:38 Looking at this code more, I think it seems reason
9042 (void)ssl_MapLowLevelError(errCode); 9102 (void)ssl_MapLowLevelError(errCode);
9043 return SECFailure; 9103 return SECFailure;
9044 } 9104 }
9045 9105
9046 static SECStatus ssl3_FinishHandshake(sslSocket *ss); 9106 static SECStatus ssl3_FinishHandshake(sslSocket *ss);
9047 9107
9048 static SECStatus 9108 static SECStatus
9049 ssl3_AlwaysFail(sslSocket * ss) 9109 ssl3_AlwaysFail(sslSocket * ss)
9050 { 9110 {
9051 PORT_SetError(PR_INVALID_STATE_ERROR); 9111 PORT_SetError(PR_INVALID_STATE_ERROR);
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after
9794 ss->ssl3.hs.ws = idle_handshake; 9854 ss->ssl3.hs.ws = idle_handshake;
9795 9855
9796 /* Do the handshake callback for sslv3 here, if we cannot false start. */ 9856 /* Do the handshake callback for sslv3 here, if we cannot false start. */
9797 if (ss->handshakeCallback != NULL && !ssl3_CanFalseStart(ss)) { 9857 if (ss->handshakeCallback != NULL && !ssl3_CanFalseStart(ss)) {
9798 (ss->handshakeCallback)(ss->fd, ss->handshakeCallbackData); 9858 (ss->handshakeCallback)(ss->fd, ss->handshakeCallbackData);
9799 } 9859 }
9800 9860
9801 return SECSuccess; 9861 return SECSuccess;
9802 } 9862 }
9803 9863
9804 /* This function handles any pending Certificate messages. Certificate messages
9805 * can be pending if we expect a possible CertificateStatus message to follow.
9806 *
9807 * This function must be called immediately after handling the
9808 * CertificateStatus message, and before handling any ServerKeyExchange or
9809 * CertificateRequest messages.
9810 */
9811 static SECStatus
9812 ssl3_MaybeHandlePendingCertificateMessage(sslSocket *ss)
9813 {
9814 SECStatus rv = SECSuccess;
9815
9816 if (ss->ssl3.hs.pending_cert_msg.data) {
9817 rv = ssl3_HandleCertificate(ss, ss->ssl3.hs.pending_cert_msg.data,
9818 ss->ssl3.hs.pending_cert_msg.len);
9819 SECITEM_FreeItem(&ss->ssl3.hs.pending_cert_msg, PR_FALSE);
9820 }
9821 return rv;
9822 }
9823
9824 /* Called from ssl3_HandleHandshake() when it has gathered a complete ssl3 9864 /* Called from ssl3_HandleHandshake() when it has gathered a complete ssl3
9825 * hanshake message. 9865 * hanshake message.
9826 * Caller must hold Handshake and RecvBuf locks. 9866 * Caller must hold Handshake and RecvBuf locks.
9827 */ 9867 */
9828 SECStatus 9868 SECStatus
9829 ssl3_HandleHandshakeMessage(sslSocket *ss, SSL3Opaque *b, PRUint32 length) 9869 ssl3_HandleHandshakeMessage(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
9830 { 9870 {
9831 SECStatus rv = SECSuccess; 9871 SECStatus rv = SECSuccess;
9832 SSL3HandshakeType type = ss->ssl3.hs.msg_type; 9872 SSL3HandshakeType type = ss->ssl3.hs.msg_type;
9833 SSL3Hashes hashes; /* computed hashes are put here. */ 9873 SSL3Hashes hashes; /* computed hashes are put here. */
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
9903 sizeof(dtlsData)); 9943 sizeof(dtlsData));
9904 if (rv != SECSuccess) return rv; /* err code already set. */ 9944 if (rv != SECSuccess) return rv; /* err code already set. */
9905 } 9945 }
9906 9946
9907 /* The message body */ 9947 /* The message body */
9908 rv = ssl3_UpdateHandshakeHashes(ss, b, length); 9948 rv = ssl3_UpdateHandshakeHashes(ss, b, length);
9909 if (rv != SECSuccess) return rv; /* err code already set. */ 9949 if (rv != SECSuccess) return rv; /* err code already set. */
9910 } 9950 }
9911 9951
9912 PORT_SetError(0); /* each message starts with no error. */ 9952 PORT_SetError(0); /* each message starts with no error. */
9913 switch (ss->ssl3.hs.msg_type) { 9953
9954 /* The CertificateStatus message is optional. We process the message if we
9955 * get one when it is allowed, but otherwise we just carry on.
9956 */
9957 if (ss->ssl3.hs.ws == wait_certificate_status) {
9958 /* We must process any CertificateStatus message before we call
9959 * ssl3_AuthCertificate, as ssl3_AuthCertificate needs any stapled OCSP
9960 * response we get.
9961 */
9962 if (ss->ssl3.hs.msg_type == certificate_status) {
9963 rv = ssl3_HandleCertificateStatus(ss, b, length);
9964 if (rv != SECSuccess)
9965 return rv;
9966 }
9967
9968 /* Regardless of whether we got a CertificateStatus message, we must
9969 * authenticate the cert before we handle any more handshake messages.
9970 */
9971 rv = ssl3_AuthCertificate(ss); /* sets ss->ssl3.hs.ws */
9972 } else switch (ss->ssl3.hs.msg_type) {
9914 case hello_request: 9973 case hello_request:
9915 if (length != 0) { 9974 if (length != 0) {
9916 (void)ssl3_DecodeError(ss); 9975 (void)ssl3_DecodeError(ss);
9917 PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_REQUEST); 9976 PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_REQUEST);
9918 return SECFailure; 9977 return SECFailure;
9919 } 9978 }
9920 if (ss->sec.isServer) { 9979 if (ss->sec.isServer) {
9921 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 9980 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9922 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_REQUEST); 9981 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_REQUEST);
9923 return SECFailure; 9982 return SECFailure;
(...skipping 18 matching lines...) Expand all
9942 break; 10001 break;
9943 case hello_verify_request: 10002 case hello_verify_request:
9944 if (!IS_DTLS(ss) || ss->sec.isServer) { 10003 if (!IS_DTLS(ss) || ss->sec.isServer) {
9945 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 10004 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9946 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_VERIFY_REQUEST); 10005 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_VERIFY_REQUEST);
9947 return SECFailure; 10006 return SECFailure;
9948 } 10007 }
9949 rv = dtls_HandleHelloVerifyRequest(ss, b, length); 10008 rv = dtls_HandleHelloVerifyRequest(ss, b, length);
9950 break; 10009 break;
9951 case certificate: 10010 case certificate:
9952 if (ss->ssl3.hs.may_get_cert_status) {
9953 /* If we might get a CertificateStatus then we want to postpone the
9954 * processing of the Certificate message until after we have
9955 * processed the CertificateStatus */
9956 if (ss->ssl3.hs.pending_cert_msg.data ||
9957 ss->ssl3.hs.ws != wait_server_cert) {
9958 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9959 (void)ssl_MapLowLevelError(SSL_ERROR_RX_UNEXPECTED_CERTIFICATE);
9960 return SECFailure;
9961 }
9962 if (SECITEM_AllocItem(NULL, &ss->ssl3.hs.pending_cert_msg,
9963 length) == NULL) {
9964 return SECFailure;
9965 }
9966 ss->ssl3.hs.pending_cert_msg.type = siBuffer;
9967 PORT_Memcpy(ss->ssl3.hs.pending_cert_msg.data, b, length);
9968 break;
9969 }
9970 rv = ssl3_HandleCertificate(ss, b, length); 10011 rv = ssl3_HandleCertificate(ss, b, length);
9971 break; 10012 break;
9972 case certificate_status: 10013 case certificate_status:
9973 » rv = ssl3_HandleCertificateStatus(ss, b, length); 10014 /* The good case is handled above */
9974 » if (rv != SECSuccess) 10015 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERT_STATUS);
9975 » break; 10016 rv = SECFailure;
9976 » PORT_Assert(ss->ssl3.hs.pending_cert_msg.data); 10017 break;
9977 » rv = ssl3_MaybeHandlePendingCertificateMessage(ss);
9978 » break;
9979 case server_key_exchange: 10018 case server_key_exchange:
9980 if (ss->sec.isServer) { 10019 if (ss->sec.isServer) {
9981 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 10020 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9982 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_SERVER_KEY_EXCH); 10021 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_SERVER_KEY_EXCH);
9983 return SECFailure; 10022 return SECFailure;
9984 } 10023 }
9985 rv = ssl3_MaybeHandlePendingCertificateMessage(ss);
9986 if (rv != SECSuccess)
9987 break;
9988 rv = ssl3_HandleServerKeyExchange(ss, b, length); 10024 rv = ssl3_HandleServerKeyExchange(ss, b, length);
9989 break; 10025 break;
9990 case certificate_request: 10026 case certificate_request:
9991 if (ss->sec.isServer) { 10027 if (ss->sec.isServer) {
9992 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 10028 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9993 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERT_REQUEST); 10029 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERT_REQUEST);
9994 return SECFailure; 10030 return SECFailure;
9995 } 10031 }
9996 rv = ssl3_MaybeHandlePendingCertificateMessage(ss);
9997 if (rv != SECSuccess)
9998 break;
9999 rv = ssl3_HandleCertificateRequest(ss, b, length); 10032 rv = ssl3_HandleCertificateRequest(ss, b, length);
10000 break; 10033 break;
10001 case server_hello_done: 10034 case server_hello_done:
10002 if (length != 0) { 10035 if (length != 0) {
10003 (void)ssl3_DecodeError(ss); 10036 (void)ssl3_DecodeError(ss);
10004 PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_DONE); 10037 PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_DONE);
10005 return SECFailure; 10038 return SECFailure;
10006 } 10039 }
10007 if (ss->sec.isServer) { 10040 if (ss->sec.isServer) {
10008 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 10041 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
10009 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_DONE); 10042 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_DONE);
10010 return SECFailure; 10043 return SECFailure;
10011 } 10044 }
10012 rv = ssl3_MaybeHandlePendingCertificateMessage(ss);
10013 if (rv != SECSuccess)
10014 break;
10015 rv = ssl3_HandleServerHelloDone(ss); 10045 rv = ssl3_HandleServerHelloDone(ss);
10016 break; 10046 break;
10017 case certificate_verify: 10047 case certificate_verify:
10018 if (!ss->sec.isServer) { 10048 if (!ss->sec.isServer) {
10019 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 10049 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
10020 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERT_VERIFY); 10050 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERT_VERIFY);
10021 return SECFailure; 10051 return SECFailure;
10022 } 10052 }
10023 rv = ssl3_HandleCertificateVerify(ss, b, length, &hashes); 10053 rv = ssl3_HandleCertificateVerify(ss, b, length, &hashes);
10024 break; 10054 break;
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
10179 return SECSuccess; 10209 return SECSuccess;
10180 } 10210 }
10181 10211
10182 /* These macros return the given value with the MSB copied to all the other 10212 /* These macros return the given value with the MSB copied to all the other
10183 * bits. They use the fact that arithmetic shift shifts-in the sign bit. 10213 * bits. They use the fact that arithmetic shift shifts-in the sign bit.
10184 * However, this is not ensured by the C standard so you may need to replace 10214 * However, this is not ensured by the C standard so you may need to replace
10185 * them with something else for odd compilers. */ 10215 * them with something else for odd compilers. */
10186 #define DUPLICATE_MSB_TO_ALL(x) ( (unsigned)( (int)(x) >> (sizeof(int)*8-1) ) ) 10216 #define DUPLICATE_MSB_TO_ALL(x) ( (unsigned)( (int)(x) >> (sizeof(int)*8-1) ) )
10187 #define DUPLICATE_MSB_TO_ALL_8(x) ((unsigned char)(DUPLICATE_MSB_TO_ALL(x))) 10217 #define DUPLICATE_MSB_TO_ALL_8(x) ((unsigned char)(DUPLICATE_MSB_TO_ALL(x)))
10188 10218
10189 /* SECStatusToMask returns, in constant time, a mask value of all ones if rv == 10219 /* SECStatusToMask returns, in constant time, a mask value of all ones if
10190 * SECSuccess. Otherwise it returns zero. */ 10220 * rv == SECSuccess. Otherwise it returns zero. */
10191 static unsigned SECStatusToMask(SECStatus rv) 10221 static unsigned int
10222 SECStatusToMask(SECStatus rv)
10192 { 10223 {
10193 unsigned int good; 10224 unsigned int good;
10194 /* rv ^ SECSuccess is zero iff rv == SECSuccess. Subtracting one results in 10225 /* rv ^ SECSuccess is zero iff rv == SECSuccess. Subtracting one results
10195 * the MSB being set to one iff it was zero before. */ 10226 * in the MSB being set to one iff it was zero before. */
10196 good = rv ^ SECSuccess; 10227 good = rv ^ SECSuccess;
10197 good--; 10228 good--;
10198 return DUPLICATE_MSB_TO_ALL(good); 10229 return DUPLICATE_MSB_TO_ALL(good);
10199 } 10230 }
10200 10231
10201 /* ssl_ConstantTimeGE returns 0xff if a>=b and 0x00 otherwise. */ 10232 /* ssl_ConstantTimeGE returns 0xff if a>=b and 0x00 otherwise. */
10202 static unsigned char ssl_ConstantTimeGE(unsigned a, unsigned b) 10233 static unsigned char
10234 ssl_ConstantTimeGE(unsigned int a, unsigned int b)
10203 { 10235 {
10204 a -= b; 10236 a -= b;
10205 return DUPLICATE_MSB_TO_ALL(~a); 10237 return DUPLICATE_MSB_TO_ALL(~a);
10206 } 10238 }
10207 10239
10208 /* ssl_ConstantTimeEQ8 returns 0xff if a==b and 0x00 otherwise. */ 10240 /* ssl_ConstantTimeEQ8 returns 0xff if a==b and 0x00 otherwise. */
10209 static unsigned char ssl_ConstantTimeEQ8(unsigned char a, unsigned char b) 10241 static unsigned char
10242 ssl_ConstantTimeEQ8(unsigned char a, unsigned char b)
10210 { 10243 {
10211 unsigned c = a ^ b; 10244 unsigned int c = a ^ b;
10212 c--; 10245 c--;
10213 return DUPLICATE_MSB_TO_ALL_8(c); 10246 return DUPLICATE_MSB_TO_ALL_8(c);
10214 } 10247 }
10215 10248
10216 static SECStatus ssl_RemoveSSLv3CBCPadding(sslBuffer *plaintext, 10249 static SECStatus
10217 » » » » » unsigned blockSize, 10250 ssl_RemoveSSLv3CBCPadding(sslBuffer *plaintext,
10218 » » » » » unsigned macSize) { 10251 » » » unsigned int blockSize,
10252 » » » unsigned int macSize)
10253 {
10219 unsigned int paddingLength, good, t; 10254 unsigned int paddingLength, good, t;
10220 const unsigned int overhead = 1 /* padding length byte */ + macSize; 10255 const unsigned int overhead = 1 /* padding length byte */ + macSize;
10221 10256
10222 /* These lengths are all public so we can test them in non-constant 10257 /* These lengths are all public so we can test them in non-constant
10223 * time. */ 10258 * time. */
10224 if (overhead > plaintext->len) { 10259 if (overhead > plaintext->len) {
10225 return SECFailure; 10260 return SECFailure;
10226 } 10261 }
10227 10262
10228 paddingLength = plaintext->buf[plaintext->len-1]; 10263 paddingLength = plaintext->buf[plaintext->len-1];
10229 /* SSLv3 padding bytes are random and cannot be checked. */ 10264 /* SSLv3 padding bytes are random and cannot be checked. */
10230 t = plaintext->len; 10265 t = plaintext->len;
10231 t -= paddingLength+overhead; 10266 t -= paddingLength+overhead;
10232 /* If len >= padding_length+overhead then the MSB of t is zero. */ 10267 /* If len >= padding_length+overhead then the MSB of t is zero. */
10233 good = DUPLICATE_MSB_TO_ALL(~t); 10268 good = DUPLICATE_MSB_TO_ALL(~t);
10234 /* SSLv3 requires that the padding is minimal. */ 10269 /* SSLv3 requires that the padding is minimal. */
10235 t = blockSize - (paddingLength+1); 10270 t = blockSize - (paddingLength+1);
10236 good &= DUPLICATE_MSB_TO_ALL(~t); 10271 good &= DUPLICATE_MSB_TO_ALL(~t);
10237 plaintext->len -= good & (paddingLength+1); 10272 plaintext->len -= good & (paddingLength+1);
10238 return (good & SECSuccess) | (~good & SECFailure); 10273 return (good & SECSuccess) | (~good & SECFailure);
10239 } 10274 }
10240 10275
10241 10276 static SECStatus
10242 static SECStatus ssl_RemoveTLSCBCPadding(sslBuffer *plaintext, 10277 ssl_RemoveTLSCBCPadding(sslBuffer *plaintext, unsigned int macSize)
10243 » » » » » unsigned macSize) { 10278 {
10244 unsigned int paddingLength, good, t, toCheck, i; 10279 unsigned int paddingLength, good, t, toCheck, i;
10245 const unsigned int overhead = 1 /* padding length byte */ + macSize; 10280 const unsigned int overhead = 1 /* padding length byte */ + macSize;
10246 10281
10247 /* These lengths are all public so we can test them in non-constant 10282 /* These lengths are all public so we can test them in non-constant
10248 * time. */ 10283 * time. */
10249 if (overhead > plaintext->len) { 10284 if (overhead > plaintext->len) {
10250 return SECFailure; 10285 return SECFailure;
10251 } 10286 }
10252 10287
10253 paddingLength = plaintext->buf[plaintext->len-1]; 10288 paddingLength = plaintext->buf[plaintext->len-1];
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
10293 10328
10294 plaintext->len -= good & (paddingLength+1); 10329 plaintext->len -= good & (paddingLength+1);
10295 return (good & SECSuccess) | (~good & SECFailure); 10330 return (good & SECSuccess) | (~good & SECFailure);
10296 } 10331 }
10297 10332
10298 /* On entry: 10333 /* On entry:
10299 * originalLength >= macSize 10334 * originalLength >= macSize
10300 * macSize <= MAX_MAC_LENGTH 10335 * macSize <= MAX_MAC_LENGTH
10301 * plaintext->len >= macSize 10336 * plaintext->len >= macSize
10302 */ 10337 */
10303 static void ssl_CBCExtractMAC(sslBuffer *plaintext, 10338 static void
10304 » » » unsigned int originalLength, 10339 ssl_CBCExtractMAC(sslBuffer *plaintext,
10305 » » » SSL3Opaque* out, 10340 » » unsigned int originalLength,
10306 » » » unsigned int macSize) { 10341 » » SSL3Opaque* out,
10342 » » unsigned int macSize)
10343 {
10307 unsigned char rotatedMac[MAX_MAC_LENGTH]; 10344 unsigned char rotatedMac[MAX_MAC_LENGTH];
10308 /* macEnd is the index of |plaintext->buf| just after the end of the MAC. */ 10345 /* macEnd is the index of |plaintext->buf| just after the end of the
10346 * MAC. */
10309 unsigned macEnd = plaintext->len; 10347 unsigned macEnd = plaintext->len;
10310 unsigned macStart = macEnd - macSize; 10348 unsigned macStart = macEnd - macSize;
10311 /* scanStart contains the number of bytes that we can ignore because 10349 /* scanStart contains the number of bytes that we can ignore because
10312 * the MAC's position can only vary by 255 bytes. */ 10350 * the MAC's position can only vary by 255 bytes. */
10313 unsigned scanStart = 0; 10351 unsigned scanStart = 0;
10314 unsigned i, j, divSpoiler; 10352 unsigned i, j, divSpoiler;
10315 unsigned char rotateOffset; 10353 unsigned char rotateOffset;
10316 10354
10317 if (originalLength > macSize + 255 + 1) 10355 if (originalLength > macSize + 255 + 1)
10318 scanStart = originalLength - (macSize + 255 + 1); 10356 scanStart = originalLength - (macSize + 255 + 1);
(...skipping 13 matching lines...) Expand all
10332 for (i = scanStart; i < originalLength;) { 10370 for (i = scanStart; i < originalLength;) {
10333 for (j = 0; j < macSize && i < originalLength; i++, j++) { 10371 for (j = 0; j < macSize && i < originalLength; i++, j++) {
10334 unsigned char macStarted = ssl_ConstantTimeGE(i, macStart); 10372 unsigned char macStarted = ssl_ConstantTimeGE(i, macStart);
10335 unsigned char macEnded = ssl_ConstantTimeGE(i, macEnd); 10373 unsigned char macEnded = ssl_ConstantTimeGE(i, macEnd);
10336 unsigned char b = 0; 10374 unsigned char b = 0;
10337 b = plaintext->buf[i]; 10375 b = plaintext->buf[i];
10338 rotatedMac[j] |= b & macStarted & ~macEnded; 10376 rotatedMac[j] |= b & macStarted & ~macEnded;
10339 } 10377 }
10340 } 10378 }
10341 10379
10342 /* Now rotate the MAC. If we knew that the MAC fit into a CPU cache line we 10380 /* Now rotate the MAC. If we knew that the MAC fit into a CPU cache line
10343 * could line-align |rotatedMac| and rotate in place. */ 10381 * we could line-align |rotatedMac| and rotate in place. */
10344 memset(out, 0, macSize); 10382 memset(out, 0, macSize);
10345 for (i = 0; i < macSize; i++) { 10383 for (i = 0; i < macSize; i++) {
10346 » unsigned char offset = (divSpoiler + macSize - rotateOffset + i) % macSi ze; 10384 » unsigned char offset =
10385 » (divSpoiler + macSize - rotateOffset + i) % macSize;
10347 for (j = 0; j < macSize; j++) { 10386 for (j = 0; j < macSize; j++) {
10348 out[j] |= rotatedMac[i] & ssl_ConstantTimeEQ8(j, offset); 10387 out[j] |= rotatedMac[i] & ssl_ConstantTimeEQ8(j, offset);
10349 } 10388 }
10350 } 10389 }
10351 } 10390 }
10352 10391
10353 /* if cText is non-null, then decipher, check MAC, and decompress the 10392 /* if cText is non-null, then decipher, check MAC, and decompress the
10354 * SSL record from cText->buf (typically gs->inbuf) 10393 * SSL record from cText->buf (typically gs->inbuf)
10355 * into databuf (typically gs->buf), and any previous contents of databuf 10394 * into databuf (typically gs->buf), and any previous contents of databuf
10356 * is lost. Then handle databuf according to its SSL record type, 10395 * is lost. Then handle databuf according to its SSL record type,
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after
10836 10875
10837 ssl_GetSpecWriteLock(ss); 10876 ssl_GetSpecWriteLock(ss);
10838 ss->ssl3.crSpec = ss->ssl3.cwSpec = &ss->ssl3.specs[0]; 10877 ss->ssl3.crSpec = ss->ssl3.cwSpec = &ss->ssl3.specs[0];
10839 ss->ssl3.prSpec = ss->ssl3.pwSpec = &ss->ssl3.specs[1]; 10878 ss->ssl3.prSpec = ss->ssl3.pwSpec = &ss->ssl3.specs[1];
10840 ss->ssl3.hs.sendingSCSV = PR_FALSE; 10879 ss->ssl3.hs.sendingSCSV = PR_FALSE;
10841 ssl3_InitCipherSpec(ss, ss->ssl3.crSpec); 10880 ssl3_InitCipherSpec(ss, ss->ssl3.crSpec);
10842 ssl3_InitCipherSpec(ss, ss->ssl3.prSpec); 10881 ssl3_InitCipherSpec(ss, ss->ssl3.prSpec);
10843 10882
10844 ss->ssl3.hs.ws = (ss->sec.isServer) ? wait_client_hello : wait_server_hello; 10883 ss->ssl3.hs.ws = (ss->sec.isServer) ? wait_client_hello : wait_server_hello;
10845 #ifdef NSS_ENABLE_ECC 10884 #ifdef NSS_ENABLE_ECC
10846 ss->ssl3.hs.negotiatedECCurves = SSL3_SUPPORTED_CURVES_MASK; 10885 ss->ssl3.hs.negotiatedECCurves = ssl3_GetSupportedECCCurveMask(ss);
10847 #endif 10886 #endif
10848 ssl_ReleaseSpecWriteLock(ss); 10887 ssl_ReleaseSpecWriteLock(ss);
10849 10888
10850 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData)); 10889 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData));
10851 10890
10852 if (IS_DTLS(ss)) { 10891 if (IS_DTLS(ss)) {
10853 ss->ssl3.hs.sendMessageSeq = 0; 10892 ss->ssl3.hs.sendMessageSeq = 0;
10854 ss->ssl3.hs.recvMessageSeq = 0; 10893 ss->ssl3.hs.recvMessageSeq = 0;
10855 ss->ssl3.hs.rtTimeoutMs = INITIAL_DTLS_TIMEOUT_MS; 10894 ss->ssl3.hs.rtTimeoutMs = INITIAL_DTLS_TIMEOUT_MS;
10856 ss->ssl3.hs.rtRetries = 0; 10895 ss->ssl3.hs.rtRetries = 0;
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
11239 } 11278 }
11240 if (ss->ssl3.hs.sha) { 11279 if (ss->ssl3.hs.sha) {
11241 PK11_DestroyContext(ss->ssl3.hs.sha,PR_TRUE); 11280 PK11_DestroyContext(ss->ssl3.hs.sha,PR_TRUE);
11242 } 11281 }
11243 if (ss->ssl3.hs.messages.buf) { 11282 if (ss->ssl3.hs.messages.buf) {
11244 PORT_Free(ss->ssl3.hs.messages.buf); 11283 PORT_Free(ss->ssl3.hs.messages.buf);
11245 ss->ssl3.hs.messages.buf = NULL; 11284 ss->ssl3.hs.messages.buf = NULL;
11246 ss->ssl3.hs.messages.len = 0; 11285 ss->ssl3.hs.messages.len = 0;
11247 ss->ssl3.hs.messages.space = 0; 11286 ss->ssl3.hs.messages.space = 0;
11248 } 11287 }
11249 if (ss->ssl3.hs.pending_cert_msg.data) {
11250 SECITEM_FreeItem(&ss->ssl3.hs.pending_cert_msg, PR_FALSE);
11251 }
11252 if (ss->ssl3.hs.cert_status.data) {
11253 SECITEM_FreeItem(&ss->ssl3.hs.cert_status, PR_FALSE);
11254 }
11255 11288
11256 /* free the SSL3Buffer (msg_body) */ 11289 /* free the SSL3Buffer (msg_body) */
11257 PORT_Free(ss->ssl3.hs.msg_body.buf); 11290 PORT_Free(ss->ssl3.hs.msg_body.buf);
11258 11291
11259 /* free up the CipherSpecs */ 11292 /* free up the CipherSpecs */
11260 ssl3_DestroyCipherSpec(&ss->ssl3.specs[0], PR_TRUE/*freeSrvName*/); 11293 ssl3_DestroyCipherSpec(&ss->ssl3.specs[0], PR_TRUE/*freeSrvName*/);
11261 ssl3_DestroyCipherSpec(&ss->ssl3.specs[1], PR_TRUE/*freeSrvName*/); 11294 ssl3_DestroyCipherSpec(&ss->ssl3.specs[1], PR_TRUE/*freeSrvName*/);
11262 11295
11263 /* Destroy the DTLS data */ 11296 /* Destroy the DTLS data */
11264 if (IS_DTLS(ss)) { 11297 if (IS_DTLS(ss)) {
11265 dtls_FreeHandshakeMessages(&ss->ssl3.hs.lastMessageFlight); 11298 dtls_FreeHandshakeMessages(&ss->ssl3.hs.lastMessageFlight);
11266 if (ss->ssl3.hs.recvdFragments.buf) { 11299 if (ss->ssl3.hs.recvdFragments.buf) {
11267 PORT_Free(ss->ssl3.hs.recvdFragments.buf); 11300 PORT_Free(ss->ssl3.hs.recvdFragments.buf);
11268 } 11301 }
11269 } 11302 }
11270 11303
11271 ss->ssl3.initialized = PR_FALSE; 11304 ss->ssl3.initialized = PR_FALSE;
11272 11305
11273 SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE); 11306 SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE);
11274 } 11307 }
11275 11308
11276 /* End of ssl3con.c */ 11309 /* End of ssl3con.c */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698