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

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

Issue 9764001: Add DTLS support to NSS, contributed by Eric Rescorla. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Made a second pass, reviewed dtls1con.c only for coding style Created 8 years, 9 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 * ***** BEGIN LICENSE BLOCK ***** 5 * ***** BEGIN LICENSE BLOCK *****
6 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 6 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * 7 *
8 * The contents of this file are subject to the Mozilla Public License Version 8 * The contents of this file are subject to the Mozilla Public License Version
9 * 1.1 (the "License"); you may not use this file except in compliance with 9 * 1.1 (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at 10 * the License. You may obtain a copy of the License at
(...skipping 24 matching lines...) Expand all
35 * under the terms of either the GPL or the LGPL, and not to allow others to 35 * under the terms of either the GPL or the LGPL, and not to allow others to
36 * use your version of this file under the terms of the MPL, indicate your 36 * use your version of this file under the terms of the MPL, indicate your
37 * decision by deleting the provisions above and replace them with the notice 37 * decision by deleting the provisions above and replace them with the notice
38 * and other provisions required by the GPL or the LGPL. If you do not delete 38 * and other provisions required by the GPL or the LGPL. If you do not delete
39 * the provisions above, a recipient may use your version of this file under 39 * the provisions above, a recipient may use your version of this file under
40 * the terms of any one of the MPL, the GPL or the LGPL. 40 * the terms of any one of the MPL, the GPL or the LGPL.
41 * 41 *
42 * ***** END LICENSE BLOCK ***** */ 42 * ***** END LICENSE BLOCK ***** */
43 /* $Id: ssl3con.c,v 1.173 2012/03/18 00:31:19 wtc%google.com Exp $ */ 43 /* $Id: ssl3con.c,v 1.173 2012/03/18 00:31:19 wtc%google.com Exp $ */
44 44
45 /* TODO(ekr): Implement HelloVerifyRequest on server side. OK for now. */
Ryan Sleevi 2012/03/22 22:26:37 XXX instead (if upstreaming is intended)
46
45 #include "cert.h" 47 #include "cert.h"
46 #include "ssl.h" 48 #include "ssl.h"
47 #include "cryptohi.h" /* for DSAU_ stuff */ 49 #include "cryptohi.h" /* for DSAU_ stuff */
48 #include "keyhi.h" 50 #include "keyhi.h"
49 #include "secder.h" 51 #include "secder.h"
50 #include "secitem.h" 52 #include "secitem.h"
51 53
52 #include "sslimpl.h" 54 #include "sslimpl.h"
53 #include "sslproto.h" 55 #include "sslproto.h"
54 #include "sslerr.h" 56 #include "sslerr.h"
(...skipping 30 matching lines...) Expand all
85 static SECStatus ssl3_SendCertificateRequest(sslSocket *ss); 87 static SECStatus ssl3_SendCertificateRequest(sslSocket *ss);
86 static SECStatus ssl3_SendNextProto( sslSocket *ss); 88 static SECStatus ssl3_SendNextProto( sslSocket *ss);
87 static SECStatus ssl3_SendFinished( sslSocket *ss, PRInt32 flags); 89 static SECStatus ssl3_SendFinished( sslSocket *ss, PRInt32 flags);
88 static SECStatus ssl3_SendServerHello( sslSocket *ss); 90 static SECStatus ssl3_SendServerHello( sslSocket *ss);
89 static SECStatus ssl3_SendServerHelloDone( sslSocket *ss); 91 static SECStatus ssl3_SendServerHelloDone( sslSocket *ss);
90 static SECStatus ssl3_SendServerKeyExchange( sslSocket *ss); 92 static SECStatus ssl3_SendServerKeyExchange( sslSocket *ss);
91 static SECStatus ssl3_NewHandshakeHashes( sslSocket *ss); 93 static SECStatus ssl3_NewHandshakeHashes( sslSocket *ss);
92 static SECStatus ssl3_UpdateHandshakeHashes( sslSocket *ss, 94 static SECStatus ssl3_UpdateHandshakeHashes( sslSocket *ss,
93 const unsigned char *b, 95 const unsigned char *b,
94 unsigned int l); 96 unsigned int l);
97 static SECStatus ssl3_FlushHandshakeMessages(sslSocket *ss, PRInt32 flags);
95 98
96 static SECStatus Null_Cipher(void *ctx, unsigned char *output, int *outputLen, 99 static SECStatus Null_Cipher(void *ctx, unsigned char *output, int *outputLen,
97 int maxOutputLen, const unsigned char *input, 100 int maxOutputLen, const unsigned char *input,
98 int inputLen); 101 int inputLen);
99 102
100 #define MAX_SEND_BUF_LENGTH 32000 /* watch for 16-bit integer overflow */ 103 #define MAX_SEND_BUF_LENGTH 32000 /* watch for 16-bit integer overflow */
101 #define MIN_SEND_BUF_LENGTH 4000 104 #define MIN_SEND_BUF_LENGTH 4000
102 105
103 #define MAX_CIPHER_SUITES 20 106 #define MAX_CIPHER_SUITES 20
104 107
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 217 }
215 218
216 static const /*SSL3ClientCertificateType */ uint8 certificate_types [] = { 219 static const /*SSL3ClientCertificateType */ uint8 certificate_types [] = {
217 ct_RSA_sign, 220 ct_RSA_sign,
218 ct_DSS_sign, 221 ct_DSS_sign,
219 #ifdef NSS_ENABLE_ECC 222 #ifdef NSS_ENABLE_ECC
220 ct_ECDSA_sign, 223 ct_ECDSA_sign,
221 #endif /* NSS_ENABLE_ECC */ 224 #endif /* NSS_ENABLE_ECC */
222 }; 225 };
223 226
224 #ifdef NSS_ENABLE_ZLIB
225 /*
226 * The DEFLATE algorithm can result in an expansion of 0.1% + 12 bytes. For a
227 * maximum TLS record payload of 2**14 bytes, that's 29 bytes.
228 */
229 #define SSL3_COMPRESSION_MAX_EXPANSION 29
230 #else /* !NSS_ENABLE_ZLIB */
231 #define SSL3_COMPRESSION_MAX_EXPANSION 0
232 #endif
233
234 /*
235 * make sure there is room in the write buffer for padding and
236 * other compression and cryptographic expansions.
237 */
238 #define SSL3_BUFFER_FUDGE 100 + SSL3_COMPRESSION_MAX_EXPANSION
239
240 #define EXPORT_RSA_KEY_LENGTH 64 /* bytes */ 227 #define EXPORT_RSA_KEY_LENGTH 64 /* bytes */
241 228
242 229
243 /* This global item is used only in servers. It is is initialized by 230 /* This global item is used only in servers. It is is initialized by
244 ** SSL_ConfigSecureServer(), and is used in ssl3_SendCertificateRequest(). 231 ** SSL_ConfigSecureServer(), and is used in ssl3_SendCertificateRequest().
245 */ 232 */
246 CERTDistNames *ssl3_server_ca_list = NULL; 233 CERTDistNames *ssl3_server_ca_list = NULL;
247 static SSL3Statistics ssl3stats; 234 static SSL3Statistics ssl3stats;
248 235
249 /* indexed by SSL3BulkCipher */ 236 /* indexed by SSL3BulkCipher */
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 static char * 497 static char *
511 ssl3_DecodeHandshakeType(int msgType) 498 ssl3_DecodeHandshakeType(int msgType)
512 { 499 {
513 char * rv; 500 char * rv;
514 static char line[40]; 501 static char line[40];
515 502
516 switch(msgType) { 503 switch(msgType) {
517 case hello_request: rv = "hello_request (0)"; break; 504 case hello_request: rv = "hello_request (0)"; break;
518 case client_hello: rv = "client_hello (1)"; break; 505 case client_hello: rv = "client_hello (1)"; break;
519 case server_hello: rv = "server_hello (2)"; break; 506 case server_hello: rv = "server_hello (2)"; break;
507 case hello_verify_request: rv = "hello_verify_request (3)"; break;
520 case certificate: rv = "certificate (11)"; break; 508 case certificate: rv = "certificate (11)"; break;
521 case server_key_exchange: rv = "server_key_exchange (12)"; break; 509 case server_key_exchange: rv = "server_key_exchange (12)"; break;
522 case certificate_request: rv = "certificate_request (13)"; break; 510 case certificate_request: rv = "certificate_request (13)"; break;
523 case server_hello_done: rv = "server_hello_done (14)"; break; 511 case server_hello_done: rv = "server_hello_done (14)"; break;
524 case certificate_verify: rv = "certificate_verify (15)"; break; 512 case certificate_verify: rv = "certificate_verify (15)"; break;
525 case client_key_exchange: rv = "client_key_exchange (16)"; break; 513 case client_key_exchange: rv = "client_key_exchange (16)"; break;
526 case finished: rv = "finished (20)"; break; 514 case finished: rv = "finished (20)"; break;
527 default: 515 default:
528 sprintf(line, "*UNKNOWN* handshake type! (%d)", msgType); 516 sprintf(line, "*UNKNOWN* handshake type! (%d)", msgType);
529 rv = line; 517 rv = line;
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 if (suite->enabled) { 637 if (suite->enabled) {
650 ++numEnabled; 638 ++numEnabled;
651 /* We need the cipher defs to see if we have a token that can handle 639 /* We need the cipher defs to see if we have a token that can handle
652 * this cipher. It isn't part of the static definition. 640 * this cipher. It isn't part of the static definition.
653 */ 641 */
654 cipher_def = ssl_LookupCipherSuiteDef(suite->cipher_suite); 642 cipher_def = ssl_LookupCipherSuiteDef(suite->cipher_suite);
655 if (!cipher_def) { 643 if (!cipher_def) {
656 suite->isPresent = PR_FALSE; 644 suite->isPresent = PR_FALSE;
657 continue; 645 continue;
658 } 646 }
659 » cipher_alg=bulk_cipher_defs[cipher_def->bulk_cipher_alg ].calg; 647 » cipher_alg = bulk_cipher_defs[cipher_def->bulk_cipher_alg].calg;
660 PORT_Assert( alg2Mech[cipher_alg].calg == cipher_alg); 648 PORT_Assert( alg2Mech[cipher_alg].calg == cipher_alg);
661 cipher_mech = alg2Mech[cipher_alg].cmech; 649 cipher_mech = alg2Mech[cipher_alg].cmech;
662 exchKeyType = 650 exchKeyType =
663 kea_defs[cipher_def->key_exchange_alg].exchKeyType; 651 kea_defs[cipher_def->key_exchange_alg].exchKeyType;
664 #ifndef NSS_ENABLE_ECC 652 #ifndef NSS_ENABLE_ECC
665 svrAuth = ss->serverCerts + exchKeyType; 653 svrAuth = ss->serverCerts + exchKeyType;
666 #else 654 #else
667 /* XXX SSLKEAType isn't really a good choice for 655 /* XXX SSLKEAType isn't really a good choice for
668 * indexing certificates. It doesn't work for 656 * indexing certificates. It doesn't work for
669 * (EC)DHE-* ciphers. Here we use a hack to ensure 657 * (EC)DHE-* ciphers. Here we use a hack to ensure
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after
1141 PK11_DestroyContext(mat->write_mac_context, PR_TRUE); 1129 PK11_DestroyContext(mat->write_mac_context, PR_TRUE);
1142 mat->write_mac_context = NULL; 1130 mat->write_mac_context = NULL;
1143 } 1131 }
1144 } 1132 }
1145 1133
1146 /* Called from ssl3_SendChangeCipherSpecs() and 1134 /* Called from ssl3_SendChangeCipherSpecs() and
1147 ** ssl3_HandleChangeCipherSpecs() 1135 ** ssl3_HandleChangeCipherSpecs()
1148 ** ssl3_DestroySSL3Info 1136 ** ssl3_DestroySSL3Info
1149 ** Caller must hold SpecWriteLock. 1137 ** Caller must hold SpecWriteLock.
1150 */ 1138 */
1151 static void 1139 void
1152 ssl3_DestroyCipherSpec(ssl3CipherSpec *spec, PRBool freeSrvName) 1140 ssl3_DestroyCipherSpec(ssl3CipherSpec *spec, PRBool freeSrvName)
1153 { 1141 {
1154 PRBool freeit = (PRBool)(!spec->bypassCiphers); 1142 PRBool freeit = (PRBool)(!spec->bypassCiphers);
1155 /* PORT_Assert( ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); Don't have ss! * / 1143 /* PORT_Assert( ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); Don't have ss! * /
1156 if (spec->destroy) { 1144 if (spec->destroy) {
1157 spec->destroy(spec->encodeContext, freeit); 1145 spec->destroy(spec->encodeContext, freeit);
1158 spec->destroy(spec->decodeContext, freeit); 1146 spec->destroy(spec->decodeContext, freeit);
1159 spec->encodeContext = NULL; /* paranoia */ 1147 spec->encodeContext = NULL; /* paranoia */
1160 spec->decodeContext = NULL; 1148 spec->decodeContext = NULL;
1161 } 1149 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1221 1209
1222 SSL_TRC(3, ("%d: SSL3[%d]: Set XXX Pending Cipher Suite to 0x%04x", 1210 SSL_TRC(3, ("%d: SSL3[%d]: Set XXX Pending Cipher Suite to 0x%04x",
1223 SSL_GETPID(), ss->fd, suite)); 1211 SSL_GETPID(), ss->fd, suite));
1224 1212
1225 suite_def = ssl_LookupCipherSuiteDef(suite); 1213 suite_def = ssl_LookupCipherSuiteDef(suite);
1226 if (suite_def == NULL) { 1214 if (suite_def == NULL) {
1227 ssl_ReleaseSpecWriteLock(ss); 1215 ssl_ReleaseSpecWriteLock(ss);
1228 return SECFailure; /* error code set by ssl_LookupCipherSuiteDef */ 1216 return SECFailure; /* error code set by ssl_LookupCipherSuiteDef */
1229 } 1217 }
1230 1218
1219 if (IS_DTLS(ss)) {
1220 /* Double-check that we did not pick an RC4 suite */
1221 PORT_Assert((suite_def->bulk_cipher_alg != cipher_rc4) &&
1222 (suite_def->bulk_cipher_alg != cipher_rc4_40) &&
1223 (suite_def->bulk_cipher_alg != cipher_rc4_56));
Ryan Sleevi 2012/03/22 22:26:37 My understanding is that PORT_Assert is intended t
ekr 2012/03/23 12:46:41 There is a separate place where this is allegedly
wtc 2012/03/23 13:48:49 I think the use of PORT_Assert is appropriate here
Ryan Sleevi 2012/03/26 19:11:55 Good point. SGTM.
1224 }
1231 1225
1232 cipher = suite_def->bulk_cipher_alg; 1226 cipher = suite_def->bulk_cipher_alg;
1233 kea = suite_def->key_exchange_alg; 1227 kea = suite_def->key_exchange_alg;
1234 mac = suite_def->mac_alg; 1228 mac = suite_def->mac_alg;
1235 if (isTLS) 1229 if (isTLS)
1236 mac += 2; 1230 mac += 2;
1237 1231
1238 ss->ssl3.hs.suite_def = suite_def; 1232 ss->ssl3.hs.suite_def = suite_def;
1239 ss->ssl3.hs.kea_def = &kea_defs[kea]; 1233 ss->ssl3.hs.kea_def = &kea_defs[kea];
1240 PORT_Assert(ss->ssl3.hs.kea_def->kea == kea); 1234 PORT_Assert(ss->ssl3.hs.kea_def->kea == kea);
1241 1235
1242 pwSpec->cipher_def = &bulk_cipher_defs[cipher]; 1236 pwSpec->cipher_def = &bulk_cipher_defs[cipher];
1243 PORT_Assert(pwSpec->cipher_def->cipher == cipher); 1237 PORT_Assert(pwSpec->cipher_def->cipher == cipher);
1244 1238
1245 pwSpec->mac_def = &mac_defs[mac]; 1239 pwSpec->mac_def = &mac_defs[mac];
1246 PORT_Assert(pwSpec->mac_def->mac == mac); 1240 PORT_Assert(pwSpec->mac_def->mac == mac);
1247 1241
1248 ss->sec.keyBits = pwSpec->cipher_def->key_size * BPB; 1242 ss->sec.keyBits = pwSpec->cipher_def->key_size * BPB;
1249 ss->sec.secretKeyBits = pwSpec->cipher_def->secret_key_size * BPB; 1243 ss->sec.secretKeyBits = pwSpec->cipher_def->secret_key_size * BPB;
1250 ss->sec.cipherType = cipher; 1244 ss->sec.cipherType = cipher;
1251 1245
1252 pwSpec->encodeContext = NULL; 1246 pwSpec->encodeContext = NULL;
1253 pwSpec->decodeContext = NULL; 1247 pwSpec->decodeContext = NULL;
1254 1248
1255 pwSpec->mac_size = pwSpec->mac_def->mac_size; 1249 pwSpec->mac_size = pwSpec->mac_def->mac_size;
1256 1250
1257 pwSpec->compression_method = ss->ssl3.hs.compression; 1251 pwSpec->compression_method = ss->ssl3.hs.compression;
1258 pwSpec->compressContext = NULL; 1252 pwSpec->compressContext = NULL;
1259 pwSpec->decompressContext = NULL; 1253 pwSpec->decompressContext = NULL;
1254
1255 /* Note: pwSpec == prSpec here so we're really doing the read side.
1256 * The epoch is set up in InitPendingCipherSpec */
1257 dtls_InitRecvdRecords(&pwSpec->recvdRecords);
1260 1258
1261 ssl_ReleaseSpecWriteLock(ss); /*******************************/ 1259 ssl_ReleaseSpecWriteLock(ss); /*******************************/
1262 return SECSuccess; 1260 return SECSuccess;
1263 } 1261 }
1264 1262
1265 #ifdef NSS_ENABLE_ZLIB 1263 #ifdef NSS_ENABLE_ZLIB
1266 #define SSL3_DEFLATE_CONTEXT_SIZE sizeof(z_stream) 1264 #define SSL3_DEFLATE_CONTEXT_SIZE sizeof(z_stream)
1267 1265
1268 static SECStatus 1266 static SECStatus
1269 ssl3_MapZlibError(int zlib_error) 1267 ssl3_MapZlibError(int zlib_error)
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
1747 * Sets error code, but caller probably should override to disambiguate. 1745 * Sets error code, but caller probably should override to disambiguate.
1748 * NULL pms means re-use old master_secret. 1746 * NULL pms means re-use old master_secret.
1749 * 1747 *
1750 * This code is common to the bypass and PKCS11 execution paths. 1748 * This code is common to the bypass and PKCS11 execution paths.
1751 * For the bypass case, pms is NULL. 1749 * For the bypass case, pms is NULL.
1752 */ 1750 */
1753 SECStatus 1751 SECStatus
1754 ssl3_InitPendingCipherSpec(sslSocket *ss, PK11SymKey *pms) 1752 ssl3_InitPendingCipherSpec(sslSocket *ss, PK11SymKey *pms)
1755 { 1753 {
1756 ssl3CipherSpec * pwSpec; 1754 ssl3CipherSpec * pwSpec;
1755 ssl3CipherSpec * cwSpec;
1757 SECStatus rv; 1756 SECStatus rv;
1758 1757
1759 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); 1758 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
1760 1759
1761 ssl_GetSpecWriteLock(ss); /**************************************/ 1760 ssl_GetSpecWriteLock(ss); /**************************************/
1762 1761
1763 PORT_Assert(ss->ssl3.prSpec == ss->ssl3.pwSpec); 1762 PORT_Assert(ss->ssl3.prSpec == ss->ssl3.pwSpec);
1764 1763
1765 pwSpec = ss->ssl3.pwSpec; 1764 pwSpec = ss->ssl3.pwSpec;
1765 cwSpec = ss->ssl3.cwSpec;
1766 1766
1767 if (pms || (!pwSpec->msItem.len && !pwSpec->master_secret)) { 1767 if (pms || (!pwSpec->msItem.len && !pwSpec->master_secret)) {
1768 rv = ssl3_DeriveMasterSecret(ss, pms); 1768 rv = ssl3_DeriveMasterSecret(ss, pms);
1769 if (rv != SECSuccess) { 1769 if (rv != SECSuccess) {
1770 goto done; /* err code set by ssl3_DeriveMasterSecret */ 1770 goto done; /* err code set by ssl3_DeriveMasterSecret */
1771 } 1771 }
1772 } 1772 }
1773 if (ss->opt.bypassPKCS11 && pwSpec->msItem.len && pwSpec->msItem.data) { 1773 if (ss->opt.bypassPKCS11 && pwSpec->msItem.len && pwSpec->msItem.data) {
1774 /* Double Bypass succeeded in extracting the master_secret */ 1774 /* Double Bypass succeeded in extracting the master_secret */
1775 const ssl3KEADef * kea_def = ss->ssl3.hs.kea_def; 1775 const ssl3KEADef * kea_def = ss->ssl3.hs.kea_def;
(...skipping 12 matching lines...) Expand all
1788 rv = ssl3_DeriveConnectionKeysPKCS11(ss); 1788 rv = ssl3_DeriveConnectionKeysPKCS11(ss);
1789 if (rv == SECSuccess) { 1789 if (rv == SECSuccess) {
1790 rv = ssl3_InitPendingContextsPKCS11(ss); 1790 rv = ssl3_InitPendingContextsPKCS11(ss);
1791 } 1791 }
1792 } else { 1792 } else {
1793 PORT_Assert(pwSpec->master_secret); 1793 PORT_Assert(pwSpec->master_secret);
1794 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 1794 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1795 rv = SECFailure; 1795 rv = SECFailure;
1796 } 1796 }
1797 1797
1798 /* Generic behaviors -- common to all crypto methods */
1799 if (!IS_DTLS(ss)) {
1800 pwSpec->read_seq_num.high = pwSpec->write_seq_num.high = 0;
1801 } else {
1802 if (cwSpec->epoch == 65535) {
Ryan Sleevi 2012/03/22 22:26:37 nit: PR_UINT16_MAX ?
1803 /* The problem here is that we have rehandshaked too many
1804 * times (you are not allowed to wrap the epoch). The
1805 * spec says you should be discarding the connection
1806 * and start over, so not much we can do here. */
1807 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1808 rv = SECFailure;
1809 }
1810 /* We only need to modify pwSpec since there is only one
1811 * "pending" spec object being pointed to by pwSpec and
1812 * cwSpec at different times.
1813 *
1814 * The sequence number has the high 16 bits as the epoch.
1815 */
1816 pwSpec->epoch = cwSpec->epoch + 1;
1817 pwSpec->read_seq_num.high = pwSpec->write_seq_num.high =
1818 pwSpec->epoch << 16;
1819 }
1820 pwSpec->read_seq_num.low = pwSpec->write_seq_num.low = 0;
wtc 2012/03/22 01:11:42 rsleevi: this block of code is originally in - ssl
1821
1798 done: 1822 done:
1799 ssl_ReleaseSpecWriteLock(ss); /******************************/ 1823 ssl_ReleaseSpecWriteLock(ss); /******************************/
1800 if (rv != SECSuccess) 1824 if (rv != SECSuccess)
1801 ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE); 1825 ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE);
1802 return rv; 1826 return rv;
1803 } 1827 }
1804 1828
1805 /* 1829 /*
1806 * 60 bytes is 3 times the maximum length MAC size that is supported. 1830 * 60 bytes is 3 times the maximum length MAC size that is supported.
1807 */ 1831 */
(...skipping 19 matching lines...) Expand all
1827 }; 1851 };
1828 1852
1829 /* Called from: ssl3_SendRecord() 1853 /* Called from: ssl3_SendRecord()
1830 ** ssl3_HandleRecord() 1854 ** ssl3_HandleRecord()
1831 ** Caller must already hold the SpecReadLock. (wish we could assert that!) 1855 ** Caller must already hold the SpecReadLock. (wish we could assert that!)
1832 */ 1856 */
1833 static SECStatus 1857 static SECStatus
1834 ssl3_ComputeRecordMAC( 1858 ssl3_ComputeRecordMAC(
1835 ssl3CipherSpec * spec, 1859 ssl3CipherSpec * spec,
1836 PRBool useServerMacKey, 1860 PRBool useServerMacKey,
1861 PRBool isDTLS,
1837 SSL3ContentType type, 1862 SSL3ContentType type,
1838 SSL3ProtocolVersion version, 1863 SSL3ProtocolVersion version,
1839 SSL3SequenceNumber seq_num, 1864 SSL3SequenceNumber seq_num,
1840 const SSL3Opaque * input, 1865 const SSL3Opaque * input,
1841 int inputLength, 1866 int inputLength,
1842 unsigned char * outbuf, 1867 unsigned char * outbuf,
1843 unsigned int * outLength) 1868 unsigned int * outLength)
1844 { 1869 {
1845 const ssl3MACDef * mac_def; 1870 const ssl3MACDef * mac_def;
1846 SECStatus rv; 1871 SECStatus rv;
(...skipping 17 matching lines...) Expand all
1864 ** NOT based on the version value in the record itself. 1889 ** NOT based on the version value in the record itself.
1865 ** But, we use the record'v version value in the computation. 1890 ** But, we use the record'v version value in the computation.
1866 */ 1891 */
1867 if (spec->version <= SSL_LIBRARY_VERSION_3_0) { 1892 if (spec->version <= SSL_LIBRARY_VERSION_3_0) {
1868 temp[9] = MSB(inputLength); 1893 temp[9] = MSB(inputLength);
1869 temp[10] = LSB(inputLength); 1894 temp[10] = LSB(inputLength);
1870 tempLen = 11; 1895 tempLen = 11;
1871 isTLS = PR_FALSE; 1896 isTLS = PR_FALSE;
1872 } else { 1897 } else {
1873 /* New TLS hash includes version. */ 1898 /* New TLS hash includes version. */
1874 » temp[9] = MSB(version); 1899 » if (isDTLS) {
1875 » temp[10] = LSB(version); 1900 » SSL3ProtocolVersion dtls_version;
1901
1902 » dtls_version = dtls_TLSVersionToDTLSVersion(version);
1903 » temp[9] = MSB(dtls_version);
1904 » temp[10] = LSB(dtls_version);
1905 } else {
1906 » temp[9] = MSB(version);
1907 » temp[10] = LSB(version);
1908 }
1876 temp[11] = MSB(inputLength); 1909 temp[11] = MSB(inputLength);
1877 temp[12] = LSB(inputLength); 1910 temp[12] = LSB(inputLength);
1878 tempLen = 13; 1911 tempLen = 13;
1879 isTLS = PR_TRUE; 1912 isTLS = PR_TRUE;
1880 } 1913 }
1881 1914
1882 PRINT_BUF(95, (NULL, "frag hash1: temp", temp, tempLen)); 1915 PRINT_BUF(95, (NULL, "frag hash1: temp", temp, tempLen));
1883 PRINT_BUF(95, (NULL, "frag hash1: input", input, inputLength)); 1916 PRINT_BUF(95, (NULL, "frag hash1: input", input, inputLength));
1884 1917
1885 mac_def = spec->mac_def; 1918 mac_def = spec->mac_def;
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
2015 (PK11_NeedLogin(slot) && !PK11_IsLoggedIn(slot, NULL))) { 2048 (PK11_NeedLogin(slot) && !PK11_IsLoggedIn(slot, NULL))) {
2016 isPresent = PR_FALSE; 2049 isPresent = PR_FALSE;
2017 } 2050 }
2018 if (slot) { 2051 if (slot) {
2019 PK11_FreeSlot(slot); 2052 PK11_FreeSlot(slot);
2020 } 2053 }
2021 return isPresent; 2054 return isPresent;
2022 } 2055 }
2023 2056
2024 /* Caller must hold the spec read lock. */ 2057 /* Caller must hold the spec read lock. */
2025 static SECStatus 2058 SECStatus
2026 ssl3_CompressMACEncryptRecord(ssl3CipherSpec * cwSpec, 2059 ssl3_CompressMACEncryptRecord(ssl3CipherSpec * cwSpec,
2027 PRBool isServer, 2060 PRBool isServer,
2061 PRBool isDTLS,
2028 SSL3ContentType type, 2062 SSL3ContentType type,
2029 const SSL3Opaque * pIn, 2063 const SSL3Opaque * pIn,
2030 PRUint32 contentLen, 2064 PRUint32 contentLen,
2031 sslBuffer * wrBuf) 2065 sslBuffer * wrBuf)
2032 { 2066 {
2033 const ssl3BulkCipherDef * cipher_def; 2067 const ssl3BulkCipherDef * cipher_def;
2034 SECStatus rv; 2068 SECStatus rv;
2035 PRUint32 macLen = 0; 2069 PRUint32 macLen = 0;
2036 PRUint32 fragLen; 2070 PRUint32 fragLen;
2037 PRUint32 p1Len, p2Len, oddLen = 0; 2071 PRUint32 p1Len, p2Len, oddLen = 0;
2072 PRUint16 headerLen;
2038 int ivLen = 0; 2073 int ivLen = 0;
2039 int cipherBytes = 0; 2074 int cipherBytes = 0;
2040 2075
2041 cipher_def = cwSpec->cipher_def; 2076 cipher_def = cwSpec->cipher_def;
2077 headerLen = isDTLS ? DTLS_RECORD_HEADER_LENGTH : SSL3_RECORD_HEADER_LENGTH;
2042 2078
2043 if (cipher_def->type == type_block && 2079 if (cipher_def->type == type_block &&
2044 cwSpec->version >= SSL_LIBRARY_VERSION_TLS_1_1) { 2080 cwSpec->version >= SSL_LIBRARY_VERSION_TLS_1_1) {
2045 /* Prepend the per-record explicit IV using technique 2b from 2081 /* Prepend the per-record explicit IV using technique 2b from
2046 * RFC 4346 section 6.2.3.2: The IV is a cryptographically 2082 * RFC 4346 section 6.2.3.2: The IV is a cryptographically
2047 * strong random number XORed with the CBC residue from the previous 2083 * strong random number XORed with the CBC residue from the previous
2048 * record. 2084 * record.
2049 */ 2085 */
2050 ivLen = cipher_def->iv_size; 2086 ivLen = cipher_def->iv_size;
2051 » if (ivLen > wrBuf->space - SSL3_RECORD_HEADER_LENGTH) { 2087 » if (ivLen > wrBuf->space - headerLen) {
2052 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 2088 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
2053 return SECFailure; 2089 return SECFailure;
2054 } 2090 }
2055 » rv = PK11_GenerateRandom(wrBuf->buf + SSL3_RECORD_HEADER_LENGTH, ivLen); 2091 » rv = PK11_GenerateRandom(wrBuf->buf + headerLen, ivLen);
2056 if (rv != SECSuccess) { 2092 if (rv != SECSuccess) {
2057 ssl_MapLowLevelError(SSL_ERROR_GENERATE_RANDOM_FAILURE); 2093 ssl_MapLowLevelError(SSL_ERROR_GENERATE_RANDOM_FAILURE);
2058 return rv; 2094 return rv;
2059 } 2095 }
2060 rv = cwSpec->encode( cwSpec->encodeContext, 2096 rv = cwSpec->encode( cwSpec->encodeContext,
2061 » wrBuf->buf + SSL3_RECORD_HEADER_LENGTH, 2097 » wrBuf->buf + headerLen,
2062 &cipherBytes, /* output and actual outLen */ 2098 &cipherBytes, /* output and actual outLen */
2063 ivLen, /* max outlen */ 2099 ivLen, /* max outlen */
2064 » wrBuf->buf + SSL3_RECORD_HEADER_LENGTH, 2100 » wrBuf->buf + headerLen,
2065 ivLen); /* input and inputLen*/ 2101 ivLen); /* input and inputLen*/
2066 if (rv != SECSuccess || cipherBytes != ivLen) { 2102 if (rv != SECSuccess || cipherBytes != ivLen) {
2067 PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE); 2103 PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE);
2068 return SECFailure; 2104 return SECFailure;
2069 } 2105 }
2070 } 2106 }
2071 2107
2072 if (cwSpec->compressor) { 2108 if (cwSpec->compressor) {
2073 int outlen; 2109 int outlen;
2074 rv = cwSpec->compressor( 2110 rv = cwSpec->compressor(
2075 cwSpec->compressContext, 2111 cwSpec->compressContext,
2076 » wrBuf->buf + SSL3_RECORD_HEADER_LENGTH + ivLen, &outlen, 2112 » wrBuf->buf + headerLen + ivLen, &outlen,
2077 » wrBuf->space - SSL3_RECORD_HEADER_LENGTH - ivLen, pIn, contentLen); 2113 » wrBuf->space - headerLen - ivLen, pIn, contentLen);
2078 if (rv != SECSuccess) 2114 if (rv != SECSuccess)
2079 return rv; 2115 return rv;
2080 » pIn = wrBuf->buf + SSL3_RECORD_HEADER_LENGTH + ivLen; 2116 » pIn = wrBuf->buf + headerLen + ivLen;
2081 contentLen = outlen; 2117 contentLen = outlen;
2082 } 2118 }
2083 2119
2084 /* 2120 /*
2085 * Add the MAC 2121 * Add the MAC
2086 */ 2122 */
2087 rv = ssl3_ComputeRecordMAC( cwSpec, isServer, 2123 rv = ssl3_ComputeRecordMAC( cwSpec, isServer, isDTLS,
2088 type, cwSpec->version, cwSpec->write_seq_num, pIn, contentLen, 2124 type, cwSpec->version, cwSpec->write_seq_num, pIn, contentLen,
2089 » wrBuf->buf + SSL3_RECORD_HEADER_LENGTH + ivLen + contentLen, &macLen); 2125 » wrBuf->buf + headerLen + ivLen + contentLen, &macLen);
2090 if (rv != SECSuccess) { 2126 if (rv != SECSuccess) {
2091 ssl_MapLowLevelError(SSL_ERROR_MAC_COMPUTATION_FAILURE); 2127 ssl_MapLowLevelError(SSL_ERROR_MAC_COMPUTATION_FAILURE);
2092 return SECFailure; 2128 return SECFailure;
2093 } 2129 }
2094 p1Len = contentLen; 2130 p1Len = contentLen;
2095 p2Len = macLen; 2131 p2Len = macLen;
2096 fragLen = contentLen + macLen; /* needs to be encrypted */ 2132 fragLen = contentLen + macLen; /* needs to be encrypted */
2097 PORT_Assert(fragLen <= MAX_FRAGMENT_LENGTH + 1024); 2133 PORT_Assert(fragLen <= MAX_FRAGMENT_LENGTH + 1024);
2098 2134
2099 /* 2135 /*
2100 * Pad the text (if we're doing a block cipher) 2136 * Pad the text (if we're doing a block cipher)
2101 * then Encrypt it 2137 * then Encrypt it
2102 */ 2138 */
2103 if (cipher_def->type == type_block) { 2139 if (cipher_def->type == type_block) {
2104 unsigned char * pBuf; 2140 unsigned char * pBuf;
2105 int padding_length; 2141 int padding_length;
2106 int i; 2142 int i;
2107 2143
2108 oddLen = contentLen % cipher_def->block_size; 2144 oddLen = contentLen % cipher_def->block_size;
2109 /* Assume blockSize is a power of two */ 2145 /* Assume blockSize is a power of two */
2110 padding_length = cipher_def->block_size - 1 - 2146 padding_length = cipher_def->block_size - 1 -
2111 ((fragLen) & (cipher_def->block_size - 1)); 2147 ((fragLen) & (cipher_def->block_size - 1));
2112 fragLen += padding_length + 1; 2148 fragLen += padding_length + 1;
2113 PORT_Assert((fragLen % cipher_def->block_size) == 0); 2149 PORT_Assert((fragLen % cipher_def->block_size) == 0);
2114 2150
2115 /* Pad according to TLS rules (also acceptable to SSL3). */ 2151 /* Pad according to TLS rules (also acceptable to SSL3). */
2116 » pBuf = &wrBuf->buf[SSL3_RECORD_HEADER_LENGTH + ivLen + fragLen - 1]; 2152 » pBuf = &wrBuf->buf[headerLen + ivLen + fragLen - 1];
2117 for (i = padding_length + 1; i > 0; --i) { 2153 for (i = padding_length + 1; i > 0; --i) {
2118 *pBuf-- = padding_length; 2154 *pBuf-- = padding_length;
2119 } 2155 }
2120 /* now, if contentLen is not a multiple of block size, fix it */ 2156 /* now, if contentLen is not a multiple of block size, fix it */
2121 p2Len = fragLen - p1Len; 2157 p2Len = fragLen - p1Len;
2122 } 2158 }
2123 if (p1Len < 256) { 2159 if (p1Len < 256) {
2124 oddLen = p1Len; 2160 oddLen = p1Len;
2125 p1Len = 0; 2161 p1Len = 0;
2126 } else { 2162 } else {
2127 p1Len -= oddLen; 2163 p1Len -= oddLen;
2128 } 2164 }
2129 if (oddLen) { 2165 if (oddLen) {
2130 p2Len += oddLen; 2166 p2Len += oddLen;
2131 PORT_Assert( (cipher_def->block_size < 2) || \ 2167 PORT_Assert( (cipher_def->block_size < 2) || \
2132 (p2Len % cipher_def->block_size) == 0); 2168 (p2Len % cipher_def->block_size) == 0);
2133 » memmove(wrBuf->buf + SSL3_RECORD_HEADER_LENGTH + ivLen + p1Len, 2169 » memmove(wrBuf->buf + headerLen + ivLen + p1Len, pIn + p1Len, oddLen);
2134 » pIn + p1Len, oddLen);
2135 } 2170 }
2136 if (p1Len > 0) { 2171 if (p1Len > 0) {
2137 int cipherBytesPart1 = -1; 2172 int cipherBytesPart1 = -1;
2138 rv = cwSpec->encode( cwSpec->encodeContext, 2173 rv = cwSpec->encode( cwSpec->encodeContext,
2139 » wrBuf->buf + SSL3_RECORD_HEADER_LENGTH + ivLen, /* output */ 2174 » wrBuf->buf + headerLen + ivLen, /* output */
2140 &cipherBytesPart1, /* actual outlen */ 2175 &cipherBytesPart1, /* actual outlen */
2141 p1Len, /* max outlen */ 2176 p1Len, /* max outlen */
2142 pIn, p1Len); /* input, and inputlen */ 2177 pIn, p1Len); /* input, and inputlen */
2143 PORT_Assert(rv == SECSuccess && cipherBytesPart1 == (int) p1Len); 2178 PORT_Assert(rv == SECSuccess && cipherBytesPart1 == (int) p1Len);
2144 if (rv != SECSuccess || cipherBytesPart1 != (int) p1Len) { 2179 if (rv != SECSuccess || cipherBytesPart1 != (int) p1Len) {
2145 PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE); 2180 PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE);
2146 return SECFailure; 2181 return SECFailure;
2147 } 2182 }
2148 cipherBytes += cipherBytesPart1; 2183 cipherBytes += cipherBytesPart1;
2149 } 2184 }
2150 if (p2Len > 0) { 2185 if (p2Len > 0) {
2151 int cipherBytesPart2 = -1; 2186 int cipherBytesPart2 = -1;
2152 rv = cwSpec->encode( cwSpec->encodeContext, 2187 rv = cwSpec->encode( cwSpec->encodeContext,
2153 » wrBuf->buf + SSL3_RECORD_HEADER_LENGTH + ivLen + p1Len, 2188 » wrBuf->buf + headerLen + ivLen + p1Len,
2154 &cipherBytesPart2, /* output and actual outLen */ 2189 &cipherBytesPart2, /* output and actual outLen */
2155 p2Len, /* max outlen */ 2190 p2Len, /* max outlen */
2156 » wrBuf->buf + SSL3_RECORD_HEADER_LENGTH + ivLen + p1Len, 2191 » wrBuf->buf + headerLen + ivLen + p1Len,
2157 p2Len); /* input and inputLen*/ 2192 p2Len); /* input and inputLen*/
2158 PORT_Assert(rv == SECSuccess && cipherBytesPart2 == (int) p2Len); 2193 PORT_Assert(rv == SECSuccess && cipherBytesPart2 == (int) p2Len);
2159 if (rv != SECSuccess || cipherBytesPart2 != (int) p2Len) { 2194 if (rv != SECSuccess || cipherBytesPart2 != (int) p2Len) {
2160 PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE); 2195 PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE);
2161 return SECFailure; 2196 return SECFailure;
2162 } 2197 }
2163 cipherBytes += cipherBytesPart2; 2198 cipherBytes += cipherBytesPart2;
2164 } 2199 }
2165 PORT_Assert(cipherBytes <= MAX_FRAGMENT_LENGTH + 1024); 2200 PORT_Assert(cipherBytes <= MAX_FRAGMENT_LENGTH + 1024);
2166 2201
2202 wrBuf->len = cipherBytes + headerLen;
2203 wrBuf->buf[0] = type;
2204 if (isDTLS) {
2205 SSL3ProtocolVersion version;
2206
2207 version = dtls_TLSVersionToDTLSVersion(cwSpec->version);
2208 wrBuf->buf[1] = MSB(version);
2209 wrBuf->buf[2] = LSB(version);
2210 wrBuf->buf[3] = (unsigned char)(cwSpec->write_seq_num.high >> 24);
2211 wrBuf->buf[4] = (unsigned char)(cwSpec->write_seq_num.high >> 16);
2212 wrBuf->buf[5] = (unsigned char)(cwSpec->write_seq_num.high >> 8);
2213 wrBuf->buf[6] = (unsigned char)(cwSpec->write_seq_num.high >> 0);
2214 wrBuf->buf[7] = (unsigned char)(cwSpec->write_seq_num.low >> 24);
2215 wrBuf->buf[8] = (unsigned char)(cwSpec->write_seq_num.low >> 16);
2216 wrBuf->buf[9] = (unsigned char)(cwSpec->write_seq_num.low >> 8);
2217 wrBuf->buf[10] = (unsigned char)(cwSpec->write_seq_num.low >> 0);
2218 wrBuf->buf[11] = MSB(cipherBytes);
2219 wrBuf->buf[12] = LSB(cipherBytes);
2220 } else {
2221 wrBuf->buf[1] = MSB(cwSpec->version);
2222 wrBuf->buf[2] = LSB(cwSpec->version);
2223 wrBuf->buf[3] = MSB(cipherBytes);
2224 wrBuf->buf[4] = LSB(cipherBytes);
2225 }
2226
2167 ssl3_BumpSequenceNumber(&cwSpec->write_seq_num); 2227 ssl3_BumpSequenceNumber(&cwSpec->write_seq_num);
2168 2228
2169 wrBuf->len = cipherBytes + SSL3_RECORD_HEADER_LENGTH;
2170 wrBuf->buf[0] = type;
2171 wrBuf->buf[1] = MSB(cwSpec->version);
2172 wrBuf->buf[2] = LSB(cwSpec->version);
2173 wrBuf->buf[3] = MSB(cipherBytes);
2174 wrBuf->buf[4] = LSB(cipherBytes);
2175
2176 return SECSuccess; 2229 return SECSuccess;
2177 } 2230 }
2178 2231
2179 /* Process the plain text before sending it. 2232 /* Process the plain text before sending it.
2180 * Returns the number of bytes of plaintext that were successfully sent 2233 * Returns the number of bytes of plaintext that were successfully sent
2181 * plus the number of bytes of plaintext that were copied into the 2234 * plus the number of bytes of plaintext that were copied into the
2182 * output (write) buffer. 2235 * output (write) buffer.
2183 * Returns SECFailure on a hard IO error, memory error, or crypto error. 2236 * Returns SECFailure on a hard IO error, memory error, or crypto error.
2184 * Does NOT return SECWouldBlock. 2237 * Does NOT return SECWouldBlock.
2185 * 2238 *
2186 * Notes on the use of the private ssl flags: 2239 * Notes on the use of the private ssl flags:
2187 * (no private SSL flags) 2240 * (no private SSL flags)
2188 * Attempt to make and send SSL records for all plaintext 2241 * Attempt to make and send SSL records for all plaintext
2189 * If non-blocking and a send gets WOULD_BLOCK, 2242 * If non-blocking and a send gets WOULD_BLOCK,
2190 * or if the pending (ciphertext) buffer is not empty, 2243 * or if the pending (ciphertext) buffer is not empty,
2191 * then buffer remaining bytes of ciphertext into pending buf, 2244 * then buffer remaining bytes of ciphertext into pending buf,
2192 * and continue to do that for all succssive records until all 2245 * and continue to do that for all succssive records until all
2193 * bytes are used. 2246 * bytes are used.
2194 * ssl_SEND_FLAG_FORCE_INTO_BUFFER 2247 * ssl_SEND_FLAG_FORCE_INTO_BUFFER
2195 * As above, except this suppresses all write attempts, and forces 2248 * As above, except this suppresses all write attempts, and forces
2196 * all ciphertext into the pending ciphertext buffer. 2249 * all ciphertext into the pending ciphertext buffer.
2250 * ssl_SEND_FLAG_USE_EPOCH (for DTLS)
2251 * Forces the use of the provided epoch
2197 * 2252 *
2198 */ 2253 */
2199 static PRInt32 2254 PRInt32
2200 ssl3_SendRecord( sslSocket * ss, 2255 ssl3_SendRecord( sslSocket * ss,
2256 DTLSEpoch epoch, /* DTLS only */
2201 SSL3ContentType type, 2257 SSL3ContentType type,
2202 const SSL3Opaque * pIn, /* input buffer */ 2258 const SSL3Opaque * pIn, /* input buffer */
2203 PRInt32 nIn, /* bytes of input */ 2259 PRInt32 nIn, /* bytes of input */
2204 PRInt32 flags) 2260 PRInt32 flags)
2205 { 2261 {
2206 sslBuffer * wrBuf = &ss->sec.writeBuf; 2262 sslBuffer * wrBuf = &ss->sec.writeBuf;
2207 SECStatus rv; 2263 SECStatus rv;
2208 PRInt32 totalSent = 0; 2264 PRInt32 totalSent = 0;
2209 2265
2210 SSL_TRC(3, ("%d: SSL3[%d] SendRecord type: %s nIn=%d", 2266 SSL_TRC(3, ("%d: SSL3[%d] SendRecord type: %s nIn=%d",
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
2262 SSL_DBG(("%d: SSL3[%d]: SendRecord, tried to get %d bytes", 2318 SSL_DBG(("%d: SSL3[%d]: SendRecord, tried to get %d bytes",
2263 SSL_GETPID(), ss->fd, spaceNeeded)); 2319 SSL_GETPID(), ss->fd, spaceNeeded));
2264 goto spec_locked_loser; /* sslBuffer_Grow set error code. */ 2320 goto spec_locked_loser; /* sslBuffer_Grow set error code. */
2265 } 2321 }
2266 } 2322 }
2267 2323
2268 if (numRecords == 2) { 2324 if (numRecords == 2) {
2269 sslBuffer secondRecord; 2325 sslBuffer secondRecord;
2270 2326
2271 rv = ssl3_CompressMACEncryptRecord(ss->ssl3.cwSpec, 2327 rv = ssl3_CompressMACEncryptRecord(ss->ssl3.cwSpec,
2272 » ss->sec.isServer, type, pIn, 1, 2328 » ss->sec.isServer, IS_DTLS(ss),
2273 » wrBuf); 2329 » » » » » type, pIn, 1, wrBuf);
Ryan Sleevi 2012/03/22 22:26:37 nit: tabs & spaces inconsistently mixed.
2274 if (rv != SECSuccess) 2330 if (rv != SECSuccess)
2275 goto spec_locked_loser; 2331 goto spec_locked_loser;
2276 2332
2277 PRINT_BUF(50, (ss, "send (encrypted) record data [1/2]:", 2333 PRINT_BUF(50, (ss, "send (encrypted) record data [1/2]:",
2278 wrBuf->buf, wrBuf->len)); 2334 wrBuf->buf, wrBuf->len));
2279 2335
2280 secondRecord.buf = wrBuf->buf + wrBuf->len; 2336 secondRecord.buf = wrBuf->buf + wrBuf->len;
2281 secondRecord.len = 0; 2337 secondRecord.len = 0;
2282 secondRecord.space = wrBuf->space - wrBuf->len; 2338 secondRecord.space = wrBuf->space - wrBuf->len;
2283 2339
2284 rv = ssl3_CompressMACEncryptRecord(ss->ssl3.cwSpec, 2340 rv = ssl3_CompressMACEncryptRecord(ss->ssl3.cwSpec,
2285 » ss->sec.isServer, type, pIn + 1, 2341 » ss->sec.isServer, IS_DTLS(ss),
2286 » contentLen - 1, &secondRecord); 2342 » » » » » type, pIn + 1, contentLen - 1,
2343 » &secondRecord);
2287 if (rv == SECSuccess) { 2344 if (rv == SECSuccess) {
2288 PRINT_BUF(50, (ss, "send (encrypted) record data [2/2]:", 2345 PRINT_BUF(50, (ss, "send (encrypted) record data [2/2]:",
2289 secondRecord.buf, secondRecord.len)); 2346 secondRecord.buf, secondRecord.len));
2290 wrBuf->len += secondRecord.len; 2347 wrBuf->len += secondRecord.len;
2291 } 2348 }
2292 } else { 2349 } else {
2293 » rv = ssl3_CompressMACEncryptRecord(ss->ssl3.cwSpec, 2350 » if (!IS_DTLS(ss)) {
2294 » ss->sec.isServer, type, pIn, 2351 » » rv = ssl3_CompressMACEncryptRecord(ss->ssl3.cwSpec,
2295 » contentLen, wrBuf); 2352 » » » » » » ss->sec.isServer,
2353 » » » » » » IS_DTLS(ss),
2354 » » » » » » type, pIn,
2355 » » » » » » contentLen, wrBuf);
2356 » } else {
2357 » » rv = dtls_CompressMACEncryptRecord(ss, epoch,
2358 » » » » » » !!(flags & ssl_SEND_FLAG_USE_ EPOCH),
2359 » » » » » » type, pIn,
2360 » » » » » » contentLen, wrBuf);
2361 » }
2362
2296 if (rv == SECSuccess) { 2363 if (rv == SECSuccess) {
2297 PRINT_BUF(50, (ss, "send (encrypted) record data:", 2364 PRINT_BUF(50, (ss, "send (encrypted) record data:",
2298 wrBuf->buf, wrBuf->len)); 2365 wrBuf->buf, wrBuf->len));
2299 } 2366 }
2300 } 2367 }
2301 2368
2302 spec_locked_loser: 2369 spec_locked_loser:
2303 ssl_ReleaseSpecReadLock(ss); /************************************/ 2370 ssl_ReleaseSpecReadLock(ss); /************************************/
2304 2371
2305 if (rv != SECSuccess) 2372 if (rv != SECSuccess)
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2343 if (sent < 0) { 2410 if (sent < 0) {
2344 if (PR_GetError() != PR_WOULD_BLOCK_ERROR) { 2411 if (PR_GetError() != PR_WOULD_BLOCK_ERROR) {
2345 ssl_MapLowLevelError(SSL_ERROR_SOCKET_WRITE_FAILURE); 2412 ssl_MapLowLevelError(SSL_ERROR_SOCKET_WRITE_FAILURE);
2346 return SECFailure; 2413 return SECFailure;
2347 } 2414 }
2348 /* we got PR_WOULD_BLOCK_ERROR, which means none was sent. */ 2415 /* we got PR_WOULD_BLOCK_ERROR, which means none was sent. */
2349 sent = 0; 2416 sent = 0;
2350 } 2417 }
2351 wrBuf->len -= sent; 2418 wrBuf->len -= sent;
2352 if (wrBuf->len) { 2419 if (wrBuf->len) {
2420 if (IS_DTLS(ss)) {
2421 /* DTLS just says no in this case. No buffering */
2422 PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
2423 return SECFailure;
2424 }
2353 /* now take all the remaining unsent new ciphertext and 2425 /* now take all the remaining unsent new ciphertext and
2354 * append it to the buffer of previously unsent ciphertext. 2426 * append it to the buffer of previously unsent ciphertext.
2355 */ 2427 */
2356 rv = ssl_SaveWriteData(ss, wrBuf->buf + sent, wrBuf->len); 2428 rv = ssl_SaveWriteData(ss, wrBuf->buf + sent, wrBuf->len);
2357 if (rv != SECSuccess) { 2429 if (rv != SECSuccess) {
2358 /* presumably a memory error, SEC_ERROR_NO_MEMORY */ 2430 /* presumably a memory error, SEC_ERROR_NO_MEMORY */
2359 return SECFailure; 2431 return SECFailure;
2360 } 2432 }
2361 } 2433 }
2362 } 2434 }
2363 totalSent += contentLen; 2435 totalSent += contentLen;
2364 } 2436 }
2365 return totalSent; 2437 return totalSent;
2366 } 2438 }
2367 2439
2368 #define SSL3_PENDING_HIGH_WATER 1024 2440 #define SSL3_PENDING_HIGH_WATER 1024
2369 2441
2370 /* Attempt to send the content of "in" in an SSL application_data record. 2442 /* Attempt to send the content of "in" in an SSL application_data record.
2371 * Returns "len" or SECFailure, never SECWouldBlock, nor SECSuccess. 2443 * Returns "len" or SECFailure, never SECWouldBlock, nor SECSuccess.
2372 */ 2444 */
2373 int 2445 int
2374 ssl3_SendApplicationData(sslSocket *ss, const unsigned char *in, 2446 ssl3_SendApplicationData(sslSocket *ss, const unsigned char *in,
2375 PRInt32 len, PRInt32 flags) 2447 PRInt32 len, PRInt32 flags)
2376 { 2448 {
2377 PRInt32 totalSent = 0; 2449 PRInt32 totalSent = 0;
2378 PRInt32 discarded = 0; 2450 PRInt32 discarded = 0;
2379 2451
2380 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) ); 2452 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) );
2453 /* These flags for internal use only */
2454 PORT_Assert(!(flags & (ssl_SEND_FLAG_USE_EPOCH |
2455 ssl_SEND_FLAG_NO_RETRANSMIT)));
2381 if (len < 0 || !in) { 2456 if (len < 0 || !in) {
2382 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); 2457 PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
2383 return SECFailure; 2458 return SECFailure;
2384 } 2459 }
2385 2460
2386 if (ss->pendingBuf.len > SSL3_PENDING_HIGH_WATER && 2461 if (ss->pendingBuf.len > SSL3_PENDING_HIGH_WATER &&
2387 !ssl_SocketIsBlocking(ss)) { 2462 !ssl_SocketIsBlocking(ss)) {
2388 PORT_Assert(!ssl_SocketIsBlocking(ss)); 2463 PORT_Assert(!ssl_SocketIsBlocking(ss));
2389 PORT_SetError(PR_WOULD_BLOCK_ERROR); 2464 PORT_SetError(PR_WOULD_BLOCK_ERROR);
2390 return SECFailure; 2465 return SECFailure;
(...skipping 17 matching lines...) Expand all
2408 * The thread yield is intended to give the reader thread a 2483 * The thread yield is intended to give the reader thread a
2409 * chance to get some cycles while the writer thread is in 2484 * chance to get some cycles while the writer thread is in
2410 * the middle of a large application data write. (See 2485 * the middle of a large application data write. (See
2411 * Bugzilla bug 127740, comment #1.) 2486 * Bugzilla bug 127740, comment #1.)
2412 */ 2487 */
2413 ssl_ReleaseXmitBufLock(ss); 2488 ssl_ReleaseXmitBufLock(ss);
2414 PR_Sleep(PR_INTERVAL_NO_WAIT); /* PR_Yield(); */ 2489 PR_Sleep(PR_INTERVAL_NO_WAIT); /* PR_Yield(); */
2415 ssl_GetXmitBufLock(ss); 2490 ssl_GetXmitBufLock(ss);
2416 } 2491 }
2417 toSend = PR_MIN(len - totalSent, MAX_FRAGMENT_LENGTH); 2492 toSend = PR_MIN(len - totalSent, MAX_FRAGMENT_LENGTH);
2418 » sent = ssl3_SendRecord(ss, content_application_data, 2493 » /*
2494 » * Note that the 0 epoch is OK because flags will never require
2495 » * its use, as guaranteed by the PORT_Assert above.
2496 » */
2497 » sent = ssl3_SendRecord(ss, 0, content_application_data,
2419 in + totalSent, toSend, flags); 2498 in + totalSent, toSend, flags);
2420 if (sent < 0) { 2499 if (sent < 0) {
2421 if (totalSent > 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR) { 2500 if (totalSent > 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR) {
2422 PORT_Assert(ss->lastWriteBlocked); 2501 PORT_Assert(ss->lastWriteBlocked);
2423 break; 2502 break;
2424 } 2503 }
2425 return SECFailure; /* error code set by ssl3_SendRecord */ 2504 return SECFailure; /* error code set by ssl3_SendRecord */
2426 } 2505 }
2427 totalSent += sent; 2506 totalSent += sent;
2428 if (ss->pendingBuf.len) { 2507 if (ss->pendingBuf.len) {
(...skipping 14 matching lines...) Expand all
2443 if (totalSent <= 0) { 2522 if (totalSent <= 0) {
2444 PORT_SetError(PR_WOULD_BLOCK_ERROR); 2523 PORT_SetError(PR_WOULD_BLOCK_ERROR);
2445 totalSent = SECFailure; 2524 totalSent = SECFailure;
2446 } 2525 }
2447 return totalSent; 2526 return totalSent;
2448 } 2527 }
2449 ss->appDataBuffered = 0; 2528 ss->appDataBuffered = 0;
2450 return totalSent + discarded; 2529 return totalSent + discarded;
2451 } 2530 }
2452 2531
2453 /* Attempt to send the content of sendBuf buffer in an SSL handshake record. 2532 /* Attempt to send buffered handshake messages.
2454 * This function returns SECSuccess or SECFailure, never SECWouldBlock. 2533 * This function returns SECSuccess or SECFailure, never SECWouldBlock.
2455 * Always set sendBuf.len to 0, even when returning SECFailure. 2534 * Always set sendBuf.len to 0, even when returning SECFailure.
2456 * 2535 *
2536 * Depending on whether we are doing DTLS or not, this either calls
2537 *
2538 * - ssl3_FlushHandshakeMessages if non-DTLS
2539 * - dtls_FlushHandshakeMessages if DTLS
2540 *
2457 * Called from SSL3_SendAlert(), ssl3_SendChangeCipherSpecs(), 2541 * Called from SSL3_SendAlert(), ssl3_SendChangeCipherSpecs(),
2458 * ssl3_AppendHandshake(), ssl3_SendClientHello(), 2542 * ssl3_AppendHandshake(), ssl3_SendClientHello(),
2459 * ssl3_SendHelloRequest(), ssl3_SendServerHelloDone(), 2543 * ssl3_SendHelloRequest(), ssl3_SendServerHelloDone(),
2460 * ssl3_SendFinished(), 2544 * ssl3_SendFinished(),
2461 */ 2545 */
2462 static SECStatus 2546 static SECStatus
2463 ssl3_FlushHandshake(sslSocket *ss, PRInt32 flags) 2547 ssl3_FlushHandshake(sslSocket *ss, PRInt32 flags)
2464 { 2548 {
2549 if (IS_DTLS(ss)) {
2550 return dtls_FlushHandshakeMessages(ss, flags);
2551 } else {
2552 return ssl3_FlushHandshakeMessages(ss, flags);
2553 }
2554 }
2555
2556 /* Attempt to send the content of sendBuf buffer in an SSL handshake record.
2557 * This function returns SECSuccess or SECFailure, never SECWouldBlock.
2558 * Always set sendBuf.len to 0, even when returning SECFailure.
2559 *
2560 * Called from ssl3_FlushHandshake
2561 */
2562 static SECStatus
2563 ssl3_FlushHandshakeMessages(sslSocket *ss, PRInt32 flags)
2564 {
2465 PRInt32 rv = SECSuccess; 2565 PRInt32 rv = SECSuccess;
2466 2566
2467 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); 2567 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
2468 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) ); 2568 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) );
2469 2569
2470 if (!ss->sec.ci.sendBuf.buf || !ss->sec.ci.sendBuf.len) 2570 if (!ss->sec.ci.sendBuf.buf || !ss->sec.ci.sendBuf.len)
2471 return rv; 2571 return rv;
2472 2572
2473 /* only this flag is allowed */ 2573 /* only this flag is allowed */
2474 PORT_Assert(!(flags & ~ssl_SEND_FLAG_FORCE_INTO_BUFFER)); 2574 PORT_Assert(!(flags & ~ssl_SEND_FLAG_FORCE_INTO_BUFFER));
2475 if ((flags & ~ssl_SEND_FLAG_FORCE_INTO_BUFFER) != 0) { 2575 if ((flags & ~ssl_SEND_FLAG_FORCE_INTO_BUFFER) != 0) {
2476 PORT_SetError(SEC_ERROR_INVALID_ARGS); 2576 PORT_SetError(SEC_ERROR_INVALID_ARGS);
2477 rv = SECFailure; 2577 rv = SECFailure;
2478 } else { 2578 } else {
2479 » rv = ssl3_SendRecord(ss, content_handshake, ss->sec.ci.sendBuf.buf, 2579 » rv = ssl3_SendRecord(ss, 0, content_handshake, ss->sec.ci.sendBuf.buf,
2480 ss->sec.ci.sendBuf.len, flags); 2580 ss->sec.ci.sendBuf.len, flags);
2481 } 2581 }
2482 if (rv < 0) { 2582 if (rv < 0) {
2483 int err = PORT_GetError(); 2583 int err = PORT_GetError();
2484 PORT_Assert(err != PR_WOULD_BLOCK_ERROR); 2584 PORT_Assert(err != PR_WOULD_BLOCK_ERROR);
2485 if (err == PR_WOULD_BLOCK_ERROR) { 2585 if (err == PR_WOULD_BLOCK_ERROR) {
2486 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 2586 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
2487 } 2587 }
2488 } else if (rv < ss->sec.ci.sendBuf.len) { 2588 } else if (rv < ss->sec.ci.sendBuf.len) {
2489 /* short write should never happen */ 2589 /* short write should never happen */
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
2586 ssl_GetSSL3HandshakeLock(ss); 2686 ssl_GetSSL3HandshakeLock(ss);
2587 if (level == alert_fatal) { 2687 if (level == alert_fatal) {
2588 if (ss->sec.ci.sid) { 2688 if (ss->sec.ci.sid) {
2589 ss->sec.uncache(ss->sec.ci.sid); 2689 ss->sec.uncache(ss->sec.ci.sid);
2590 } 2690 }
2591 } 2691 }
2592 ssl_GetXmitBufLock(ss); 2692 ssl_GetXmitBufLock(ss);
2593 rv = ssl3_FlushHandshake(ss, ssl_SEND_FLAG_FORCE_INTO_BUFFER); 2693 rv = ssl3_FlushHandshake(ss, ssl_SEND_FLAG_FORCE_INTO_BUFFER);
2594 if (rv == SECSuccess) { 2694 if (rv == SECSuccess) {
2595 PRInt32 sent; 2695 PRInt32 sent;
2596 » sent = ssl3_SendRecord(ss, content_alert, bytes, 2, 2696 » sent = ssl3_SendRecord(ss, 0, content_alert, bytes, 2,
2597 desc == no_certificate 2697 desc == no_certificate
2598 ? ssl_SEND_FLAG_FORCE_INTO_BUFFER : 0); 2698 ? ssl_SEND_FLAG_FORCE_INTO_BUFFER : 0);
2599 rv = (sent >= 0) ? SECSuccess : (SECStatus)sent; 2699 rv = (sent >= 0) ? SECSuccess : (SECStatus)sent;
2600 } 2700 }
2601 ssl_ReleaseXmitBufLock(ss); 2701 ssl_ReleaseXmitBufLock(ss);
2602 ssl_ReleaseSSL3HandshakeLock(ss); 2702 ssl_ReleaseSSL3HandshakeLock(ss);
2603 return rv; /* error set by ssl3_FlushHandshake or ssl3_SendRecord */ 2703 return rv; /* error set by ssl3_FlushHandshake or ssl3_SendRecord */
2604 } 2704 }
2605 2705
2606 /* 2706 /*
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2660 SSL_DBG(("%d: SSL3[%d]: peer certificate is no good: error=%d", 2760 SSL_DBG(("%d: SSL3[%d]: peer certificate is no good: error=%d",
2661 SSL_GETPID(), ss->fd, errCode)); 2761 SSL_GETPID(), ss->fd, errCode));
2662 2762
2663 (void) SSL3_SendAlert(ss, alert_fatal, desc); 2763 (void) SSL3_SendAlert(ss, alert_fatal, desc);
2664 } 2764 }
2665 2765
2666 2766
2667 /* 2767 /*
2668 * Send handshake_Failure alert. Set generic error number. 2768 * Send handshake_Failure alert. Set generic error number.
2669 */ 2769 */
2670 static SECStatus 2770 SECStatus
2671 ssl3_DecodeError(sslSocket *ss) 2771 ssl3_DecodeError(sslSocket *ss)
2672 { 2772 {
2673 (void)SSL3_SendAlert(ss, alert_fatal, 2773 (void)SSL3_SendAlert(ss, alert_fatal,
2674 ss->version > SSL_LIBRARY_VERSION_3_0 ? decode_error 2774 ss->version > SSL_LIBRARY_VERSION_3_0 ? decode_error
2675 : illegal_parameter); 2775 : illegal_parameter);
2676 PORT_SetError( ss->sec.isServer ? SSL_ERROR_BAD_CLIENT 2776 PORT_SetError( ss->sec.isServer ? SSL_ERROR_BAD_CLIENT
2677 : SSL_ERROR_BAD_SERVER ); 2777 : SSL_ERROR_BAD_SERVER );
2678 return SECFailure; 2778 return SECFailure;
2679 } 2779 }
2680 2780
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
2748 error = SSL_ERROR_CERTIFICATE_UNOBTAINABLE_ALERT; break; 2848 error = SSL_ERROR_CERTIFICATE_UNOBTAINABLE_ALERT; break;
2749 case unrecognized_name: 2849 case unrecognized_name:
2750 error = SSL_ERROR_UNRECOGNIZED_NAME_ALERT; break; 2850 error = SSL_ERROR_UNRECOGNIZED_NAME_ALERT; break;
2751 case bad_certificate_status_response: 2851 case bad_certificate_status_response:
2752 error = SSL_ERROR_BAD_CERT_STATUS_RESPONSE_ALERT; break; 2852 error = SSL_ERROR_BAD_CERT_STATUS_RESPONSE_ALERT; break;
2753 case bad_certificate_hash_value: 2853 case bad_certificate_hash_value:
2754 error = SSL_ERROR_BAD_CERT_HASH_VALUE_ALERT; break; 2854 error = SSL_ERROR_BAD_CERT_HASH_VALUE_ALERT; break;
2755 default: error = SSL_ERROR_RX_UNKNOWN_ALERT; break; 2855 default: error = SSL_ERROR_RX_UNKNOWN_ALERT; break;
2756 } 2856 }
2757 if (level == alert_fatal) { 2857 if (level == alert_fatal) {
2758 » ss->sec.uncache(ss->sec.ci.sid); 2858 » if (!ss->opt.noCache)
2859 » ss->sec.uncache(ss->sec.ci.sid);
wtc 2012/03/22 01:11:42 rsleevi: this is a fix for the crash described in
2759 if ((ss->ssl3.hs.ws == wait_server_hello) && 2860 if ((ss->ssl3.hs.ws == wait_server_hello) &&
2760 (desc == handshake_failure)) { 2861 (desc == handshake_failure)) {
2761 /* XXX This is a hack. We're assuming that any handshake failure 2862 /* XXX This is a hack. We're assuming that any handshake failure
2762 * XXX on the client hello is a failure to match ciphers. 2863 * XXX on the client hello is a failure to match ciphers.
2763 */ 2864 */
2764 error = SSL_ERROR_NO_CYPHER_OVERLAP; 2865 error = SSL_ERROR_NO_CYPHER_OVERLAP;
2765 } 2866 }
2766 PORT_SetError(error); 2867 PORT_SetError(error);
2767 return SECFailure; 2868 return SECFailure;
2768 } 2869 }
(...skipping 30 matching lines...) Expand all
2799 SSL_TRC(3, ("%d: SSL3[%d]: send change_cipher_spec record", 2900 SSL_TRC(3, ("%d: SSL3[%d]: send change_cipher_spec record",
2800 SSL_GETPID(), ss->fd)); 2901 SSL_GETPID(), ss->fd));
2801 2902
2802 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) ); 2903 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) );
2803 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); 2904 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
2804 2905
2805 rv = ssl3_FlushHandshake(ss, ssl_SEND_FLAG_FORCE_INTO_BUFFER); 2906 rv = ssl3_FlushHandshake(ss, ssl_SEND_FLAG_FORCE_INTO_BUFFER);
2806 if (rv != SECSuccess) { 2907 if (rv != SECSuccess) {
2807 return rv; /* error code set by ssl3_FlushHandshake */ 2908 return rv; /* error code set by ssl3_FlushHandshake */
2808 } 2909 }
2809 sent = ssl3_SendRecord(ss, content_change_cipher_spec, &change, 1, 2910 if (!IS_DTLS(ss)) {
2810 ssl_SEND_FLAG_FORCE_INTO_BUFFER); 2911 » sent = ssl3_SendRecord(ss, 0, content_change_cipher_spec, &change, 1,
2811 if (sent < 0) { 2912 » » » ssl_SEND_FLAG_FORCE_INTO_BUFFER);
2812 » return (SECStatus)sent;»/* error code set by ssl3_SendRecord */ 2913 » if (sent < 0) {
2914 » return (SECStatus)sent;» /* error code set by ssl3_SendRecord */
2915 » }
2916 } else {
2917 » rv = dtls_QueueMessage(ss, content_change_cipher_spec, &change, 1);
2918 » if (rv != SECSuccess) {
2919 » return rv;
2920 » }
2813 } 2921 }
2814 2922
2815 /* swap the pending and current write specs. */ 2923 /* swap the pending and current write specs. */
2816 ssl_GetSpecWriteLock(ss); /**************************************/ 2924 ssl_GetSpecWriteLock(ss); /**************************************/
2817 pwSpec = ss->ssl3.pwSpec; 2925 pwSpec = ss->ssl3.pwSpec;
2818 pwSpec->write_seq_num.high = 0;
2819 pwSpec->write_seq_num.low = 0;
2820 2926
2821 ss->ssl3.pwSpec = ss->ssl3.cwSpec; 2927 ss->ssl3.pwSpec = ss->ssl3.cwSpec;
2822 ss->ssl3.cwSpec = pwSpec; 2928 ss->ssl3.cwSpec = pwSpec;
2823 2929
2824 SSL_TRC(3, ("%d: SSL3[%d] Set Current Write Cipher Suite to Pending", 2930 SSL_TRC(3, ("%d: SSL3[%d] Set Current Write Cipher Suite to Pending",
2825 SSL_GETPID(), ss->fd )); 2931 SSL_GETPID(), ss->fd ));
2826 2932
2827 /* We need to free up the contexts, keys and certs ! */ 2933 /* We need to free up the contexts, keys and certs ! */
2828 /* If we are really through with the old cipher spec 2934 /* If we are really through with the old cipher spec
2829 * (Both the read and write sides have changed) destroy it. 2935 * (Both the read and write sides have changed) destroy it.
2830 */ 2936 */
2831 if (ss->ssl3.prSpec == ss->ssl3.pwSpec) { 2937 if (ss->ssl3.prSpec == ss->ssl3.pwSpec) {
2832 » ssl3_DestroyCipherSpec(ss->ssl3.pwSpec, PR_FALSE/*freeSrvName*/); 2938 » if (!IS_DTLS(ss)) {
2939 » ssl3_DestroyCipherSpec(ss->ssl3.pwSpec, PR_FALSE/*freeSrvName*/);
2940 » } else {
2941 » /* With DTLS, we need to set a holddown timer in case the final
2942 » * message got lost */
2943 » ss->ssl3.hs.rtTimeoutMs = DTLS_FINISHED_TIMER_MS;
2944 » dtls_StartTimer(ss, dtls_FinishedTimerCb);
2945 » }
2833 } 2946 }
2834 ssl_ReleaseSpecWriteLock(ss); /**************************************/ 2947 ssl_ReleaseSpecWriteLock(ss); /**************************************/
2835 2948
2836 return SECSuccess; 2949 return SECSuccess;
2837 } 2950 }
2838 2951
2839 /* Called from ssl3_HandleRecord. 2952 /* Called from ssl3_HandleRecord.
2840 ** Caller must hold both RecvBuf and Handshake locks. 2953 ** Caller must hold both RecvBuf and Handshake locks.
2841 * 2954 *
2842 * Acquires and releases spec write lock, to protect switching the current 2955 * Acquires and releases spec write lock, to protect switching the current
(...skipping 28 matching lines...) Expand all
2871 /* illegal_parameter is correct here for both SSL3 and TLS. */ 2984 /* illegal_parameter is correct here for both SSL3 and TLS. */
2872 (void)ssl3_IllegalParameter(ss); 2985 (void)ssl3_IllegalParameter(ss);
2873 PORT_SetError(SSL_ERROR_RX_MALFORMED_CHANGE_CIPHER); 2986 PORT_SetError(SSL_ERROR_RX_MALFORMED_CHANGE_CIPHER);
2874 return SECFailure; 2987 return SECFailure;
2875 } 2988 }
2876 buf->len = 0; 2989 buf->len = 0;
2877 2990
2878 /* Swap the pending and current read specs. */ 2991 /* Swap the pending and current read specs. */
2879 ssl_GetSpecWriteLock(ss); /*************************************/ 2992 ssl_GetSpecWriteLock(ss); /*************************************/
2880 prSpec = ss->ssl3.prSpec; 2993 prSpec = ss->ssl3.prSpec;
2881 prSpec->read_seq_num.high = prSpec->read_seq_num.low = 0;
2882 2994
2883 ss->ssl3.prSpec = ss->ssl3.crSpec; 2995 ss->ssl3.prSpec = ss->ssl3.crSpec;
2884 ss->ssl3.crSpec = prSpec; 2996 ss->ssl3.crSpec = prSpec;
2885 2997
2886 if (ss->sec.isServer && 2998 if (ss->sec.isServer &&
2887 ss->opt.requestCertificate && 2999 ss->opt.requestCertificate &&
2888 ssl3_ExtensionNegotiated(ss, ssl_encrypted_client_certs)) { 3000 ssl3_ExtensionNegotiated(ss, ssl_encrypted_client_certs)) {
2889 ss->ssl3.hs.ws = wait_client_cert; 3001 ss->ssl3.hs.ws = wait_client_cert;
2890 } else { 3002 } else {
2891 ss->ssl3.hs.ws = wait_finished; 3003 ss->ssl3.hs.ws = wait_finished;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
2974 keyData->data, keyData->len); 3086 keyData->data, keyData->len);
2975 } 3087 }
2976 } 3088 }
2977 } 3089 }
2978 #endif 3090 #endif
2979 pwSpec->master_secret = PK11_DeriveWithFlags(pms, master_derive, 3091 pwSpec->master_secret = PK11_DeriveWithFlags(pms, master_derive,
2980 &params, key_derive, CKA_DERIVE, 0, keyFlags); 3092 &params, key_derive, CKA_DERIVE, 0, keyFlags);
2981 if (!isDH && pwSpec->master_secret && ss->opt.detectRollBack) { 3093 if (!isDH && pwSpec->master_secret && ss->opt.detectRollBack) {
2982 SSL3ProtocolVersion client_version; 3094 SSL3ProtocolVersion client_version;
2983 client_version = pms_version.major << 8 | pms_version.minor; 3095 client_version = pms_version.major << 8 | pms_version.minor;
3096
3097 if (IS_DTLS(ss)) {
3098 client_version = dtls_DTLSVersionToTLSVersion(client_version);
3099 }
3100
2984 if (client_version != ss->clientHelloVersion) { 3101 if (client_version != ss->clientHelloVersion) {
2985 /* Destroy it. Version roll-back detected. */ 3102 /* Destroy it. Version roll-back detected. */
2986 PK11_FreeSymKey(pwSpec->master_secret); 3103 PK11_FreeSymKey(pwSpec->master_secret);
2987 pwSpec->master_secret = NULL; 3104 pwSpec->master_secret = NULL;
2988 } 3105 }
2989 } 3106 }
2990 if (pwSpec->master_secret == NULL) { 3107 if (pwSpec->master_secret == NULL) {
2991 /* Generate a faux master secret in the same slot as the old one. */ 3108 /* Generate a faux master secret in the same slot as the old one. */
2992 PK11SlotInfo * slot = PK11_GetSlotFromKey((PK11SymKey *)pms); 3109 PK11SlotInfo * slot = PK11_GetSlotFromKey((PK11SymKey *)pms);
2993 PK11SymKey * fpms = ssl3_GenerateRSAPMS(ss, pwSpec, slot); 3110 PK11SymKey * fpms = ssl3_GenerateRSAPMS(ss, pwSpec, slot);
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
3398 SSL_TRC(60, ("data:")); 3515 SSL_TRC(60, ("data:"));
3399 rv = ssl3_AppendHandshake(ss, src, bytes); 3516 rv = ssl3_AppendHandshake(ss, src, bytes);
3400 return rv; /* error code set by AppendHandshake, if applicable. */ 3517 return rv; /* error code set by AppendHandshake, if applicable. */
3401 } 3518 }
3402 3519
3403 SECStatus 3520 SECStatus
3404 ssl3_AppendHandshakeHeader(sslSocket *ss, SSL3HandshakeType t, PRUint32 length) 3521 ssl3_AppendHandshakeHeader(sslSocket *ss, SSL3HandshakeType t, PRUint32 length)
3405 { 3522 {
3406 SECStatus rv; 3523 SECStatus rv;
3407 3524
3525 /* If we already have a message in place, we need to enqueue it.
3526 * This empties the buffer
3527 */
3528 if (IS_DTLS(ss)) {
3529 rv = dtls_StageHandshakeMessage(ss);
3530 if (rv != SECSuccess) {
3531 return rv;
3532 }
3533 }
3534
3408 SSL_TRC(30,("%d: SSL3[%d]: append handshake header: type %s", 3535 SSL_TRC(30,("%d: SSL3[%d]: append handshake header: type %s",
3409 SSL_GETPID(), ss->fd, ssl3_DecodeHandshakeType(t))); 3536 SSL_GETPID(), ss->fd, ssl3_DecodeHandshakeType(t)));
3410 PRINT_BUF(60, (ss, "MD5 handshake hash:", 3537 PRINT_BUF(60, (ss, "MD5 handshake hash:",
3411 (unsigned char*)ss->ssl3.hs.md5_cx, MD5_LENGTH)); 3538 (unsigned char*)ss->ssl3.hs.md5_cx, MD5_LENGTH));
3412 PRINT_BUF(95, (ss, "SHA handshake hash:", 3539 PRINT_BUF(95, (ss, "SHA handshake hash:",
3413 (unsigned char*)ss->ssl3.hs.sha_cx, SHA1_LENGTH)); 3540 (unsigned char*)ss->ssl3.hs.sha_cx, SHA1_LENGTH));
3414 3541
3415 rv = ssl3_AppendHandshakeNumber(ss, t, 1); 3542 rv = ssl3_AppendHandshakeNumber(ss, t, 1);
3416 if (rv != SECSuccess) { 3543 if (rv != SECSuccess) {
3417 return rv; /* error code set by AppendHandshake, if applicable. */ 3544 return rv; /* error code set by AppendHandshake, if applicable. */
3418 } 3545 }
3419 rv = ssl3_AppendHandshakeNumber(ss, length, 3); 3546 rv = ssl3_AppendHandshakeNumber(ss, length, 3);
3547 if (rv != SECSuccess) {
3548 return rv; /* error code set by AppendHandshake, if applicable. */
3549 }
3550
3551 if (IS_DTLS(ss)) {
3552 /* Note that we make an unfragmented message here. We fragment in the
3553 * transmission code, if necessary */
3554 rv = ssl3_AppendHandshakeNumber(ss, ss->ssl3.hs.sendMessageSeq, 2);
3555 if (rv != SECSuccess) {
3556 return rv; /* error code set by AppendHandshake, if applicable. */
3557 }
3558 ss->ssl3.hs.sendMessageSeq++;
Ryan Sleevi 2012/03/22 22:26:37 I admit, I haven't fully analyzed this code, but w
wtc 2012/03/23 00:21:18 This should be reset at the beginning of each hand
3559
3560 /* 0 is the fragment offset, because it's not fragmented yet */
3561 rv = ssl3_AppendHandshakeNumber(ss, 0, 3);
3562 if (rv != SECSuccess) {
3563 return rv; /* error code set by AppendHandshake, if applicable. */
3564 }
3565
3566 /* Fragment length -- set to the packet length because not fragmented */
3567 rv = ssl3_AppendHandshakeNumber(ss, length, 3);
3568 if (rv != SECSuccess) {
3569 return rv; /* error code set by AppendHandshake, if applicable. */
3570 }
3571 }
3572
3420 return rv; /* error code set by AppendHandshake, if applicable. */ 3573 return rv; /* error code set by AppendHandshake, if applicable. */
3421 } 3574 }
3422 3575
3423 /************************************************************************** 3576 /**************************************************************************
3424 * Consume Handshake functions. 3577 * Consume Handshake functions.
3425 * 3578 *
3426 * All data used in these functions is protected by two locks, 3579 * All data used in these functions is protected by two locks,
3427 * the RecvBufLock and the SSL3HandshakeLock 3580 * the RecvBufLock and the SSL3HandshakeLock
3428 **************************************************************************/ 3581 **************************************************************************/
3429 3582
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
3816 } 3969 }
3817 3970
3818 /************************************************************************** 3971 /**************************************************************************
3819 * end of Handshake Hash functions. 3972 * end of Handshake Hash functions.
3820 * Begin Send and Handle functions for handshakes. 3973 * Begin Send and Handle functions for handshakes.
3821 **************************************************************************/ 3974 **************************************************************************/
3822 3975
3823 /* Called from ssl3_HandleHelloRequest(), 3976 /* Called from ssl3_HandleHelloRequest(),
3824 * ssl3_RedoHandshake() 3977 * ssl3_RedoHandshake()
3825 * ssl2_BeginClientHandshake (when resuming ssl3 session) 3978 * ssl2_BeginClientHandshake (when resuming ssl3 session)
3979 * dtls_HandleHelloVerifyRequest(with resending=PR_TRUE)
3826 */ 3980 */
3827 SECStatus 3981 SECStatus
3828 ssl3_SendClientHello(sslSocket *ss) 3982 ssl3_SendClientHello(sslSocket *ss, PRBool resending)
3829 { 3983 {
3830 sslSessionID * sid; 3984 sslSessionID * sid;
3831 ssl3CipherSpec * cwSpec; 3985 ssl3CipherSpec * cwSpec;
3832 SECStatus rv; 3986 SECStatus rv;
3833 int i; 3987 int i;
3834 int length; 3988 int length;
3835 int num_suites; 3989 int num_suites;
3836 int actual_count = 0; 3990 int actual_count = 0;
3837 PRBool isTLS = PR_FALSE; 3991 PRBool isTLS = PR_FALSE;
3838 PRInt32 total_exten_len = 0; 3992 PRInt32 total_exten_len = 0;
3839 unsigned numCompressionMethods; 3993 unsigned numCompressionMethods;
3840 3994
3841 SSL_TRC(3, ("%d: SSL3[%d]: send client_hello handshake", SSL_GETPID(), 3995 SSL_TRC(3, ("%d: SSL3[%d]: send client_hello handshake", SSL_GETPID(),
3842 ss->fd)); 3996 ss->fd));
3843 3997
3844 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) ); 3998 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
3845 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) ); 3999 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss) );
3846 4000
3847 rv = ssl3_InitState(ss); 4001 rv = ssl3_InitState(ss);
3848 if (rv != SECSuccess) { 4002 if (rv != SECSuccess) {
3849 return rv; /* ssl3_InitState has set the error code. */ 4003 return rv; /* ssl3_InitState has set the error code. */
3850 } 4004 }
3851 ss->ssl3.hs.sendingSCSV = PR_FALSE; /* Must be reset every handshake */ 4005 ss->ssl3.hs.sendingSCSV = PR_FALSE; /* Must be reset every handshake */
4006 PORT_Assert(IS_DTLS(ss) || !resending);
3852 4007
3853 /* We might be starting a session renegotiation in which case we should 4008 /* We might be starting a session renegotiation in which case we should
3854 * clear previous state. 4009 * clear previous state.
3855 */ 4010 */
3856 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData)); 4011 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData));
3857 4012
3858 SSL_TRC(30,("%d: SSL3[%d]: reset handshake hashes", 4013 SSL_TRC(30,("%d: SSL3[%d]: reset handshake hashes",
3859 SSL_GETPID(), ss->fd )); 4014 SSL_GETPID(), ss->fd ));
3860 rv = ssl3_RestartHandshakeHashes(ss); 4015 rv = ssl3_RestartHandshakeHashes(ss);
3861 if (rv != SECSuccess) { 4016 if (rv != SECSuccess) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
4001 total_exten_len += 2; 4156 total_exten_len += 2;
4002 } 4157 }
4003 4158
4004 #if defined(NSS_ENABLE_ECC) && !defined(NSS_ECC_MORE_THAN_SUITE_B) 4159 #if defined(NSS_ENABLE_ECC) && !defined(NSS_ECC_MORE_THAN_SUITE_B)
4005 if (!total_exten_len || !isTLS) { 4160 if (!total_exten_len || !isTLS) {
4006 /* not sending the elliptic_curves and ec_point_formats extensions */ 4161 /* not sending the elliptic_curves and ec_point_formats extensions */
4007 ssl3_DisableECCSuites(ss, NULL); /* disable all ECC suites */ 4162 ssl3_DisableECCSuites(ss, NULL); /* disable all ECC suites */
4008 } 4163 }
4009 #endif 4164 #endif
4010 4165
4166 if (IS_DTLS(ss)) {
4167 ssl3_DisableNonDTLSSuites(ss);
4168 }
4169
4011 /* how many suites are permitted by policy and user preference? */ 4170 /* how many suites are permitted by policy and user preference? */
4012 num_suites = count_cipher_suites(ss, ss->ssl3.policy, PR_TRUE); 4171 num_suites = count_cipher_suites(ss, ss->ssl3.policy, PR_TRUE);
4013 if (!num_suites) 4172 if (!num_suites)
4014 return SECFailure; /* count_cipher_suites has set error code. */ 4173 return SECFailure; /* count_cipher_suites has set error code. */
4015 if (ss->ssl3.hs.sendingSCSV) { 4174 if (ss->ssl3.hs.sendingSCSV) {
4016 ++num_suites; /* make room for SCSV */ 4175 ++num_suites; /* make room for SCSV */
4017 } 4176 }
4018 4177
4019 /* count compression methods */ 4178 /* count compression methods */
4020 numCompressionMethods = 0; 4179 numCompressionMethods = 0;
4021 for (i = 0; i < compressionMethodsCount; i++) { 4180 for (i = 0; i < compressionMethodsCount; i++) {
4022 if (compressionEnabled(ss, compressions[i])) 4181 if (compressionEnabled(ss, compressions[i]))
4023 numCompressionMethods++; 4182 numCompressionMethods++;
4024 } 4183 }
4025 4184
4026 length = sizeof(SSL3ProtocolVersion) + SSL3_RANDOM_LENGTH + 4185 length = sizeof(SSL3ProtocolVersion) + SSL3_RANDOM_LENGTH +
4027 1 + ((sid == NULL) ? 0 : sid->u.ssl3.sessionIDLength) + 4186 1 + ((sid == NULL) ? 0 : sid->u.ssl3.sessionIDLength) +
4028 2 + num_suites*sizeof(ssl3CipherSuite) + 4187 2 + num_suites*sizeof(ssl3CipherSuite) +
4029 1 + numCompressionMethods + total_exten_len; 4188 1 + numCompressionMethods + total_exten_len;
4189 if (IS_DTLS(ss)) {
4190 length += 1 + ss->ssl3.hs.cookieLen;
4191 }
4030 4192
4031 rv = ssl3_AppendHandshakeHeader(ss, client_hello, length); 4193 rv = ssl3_AppendHandshakeHeader(ss, client_hello, length);
4032 if (rv != SECSuccess) { 4194 if (rv != SECSuccess) {
4033 return rv; /* err set by ssl3_AppendHandshake* */ 4195 return rv; /* err set by ssl3_AppendHandshake* */
4034 } 4196 }
4035 4197
4036 ss->clientHelloVersion = ss->version; 4198 ss->clientHelloVersion = ss->version;
4037 rv = ssl3_AppendHandshakeNumber(ss, ss->clientHelloVersion, 2); 4199 if (IS_DTLS(ss)) {
4200 » PRUint16 version;
4201
4202 » version = dtls_TLSVersionToDTLSVersion(ss->clientHelloVersion);
4203 » rv = ssl3_AppendHandshakeNumber(ss, version, 2);
4204 } else {
4205 » rv = ssl3_AppendHandshakeNumber(ss, ss->clientHelloVersion, 2);
4206 }
4038 if (rv != SECSuccess) { 4207 if (rv != SECSuccess) {
4039 return rv; /* err set by ssl3_AppendHandshake* */ 4208 return rv; /* err set by ssl3_AppendHandshake* */
4040 } 4209 }
4041 rv = ssl3_GetNewRandom(&ss->ssl3.hs.client_random); 4210
4042 if (rv != SECSuccess) { 4211 if (!resending) { /* Don't re-generate if we are in DTLS re-sending mode */
4043 » return rv;» /* err set by GetNewRandom. */ 4212 » rv = ssl3_GetNewRandom(&ss->ssl3.hs.client_random);
4213 » if (rv != SECSuccess) {
4214 » return rv;» /* err set by GetNewRandom. */
4215 » }
4044 } 4216 }
4045 rv = ssl3_AppendHandshake(ss, &ss->ssl3.hs.client_random, 4217 rv = ssl3_AppendHandshake(ss, &ss->ssl3.hs.client_random,
4046 SSL3_RANDOM_LENGTH); 4218 SSL3_RANDOM_LENGTH);
4047 if (rv != SECSuccess) { 4219 if (rv != SECSuccess) {
4048 return rv; /* err set by ssl3_AppendHandshake* */ 4220 return rv; /* err set by ssl3_AppendHandshake* */
4049 } 4221 }
4050 4222
4051 if (sid) 4223 if (sid)
4052 rv = ssl3_AppendHandshakeVariable( 4224 rv = ssl3_AppendHandshakeVariable(
4053 ss, sid->u.ssl3.sessionID, sid->u.ssl3.sessionIDLength, 1); 4225 ss, sid->u.ssl3.sessionID, sid->u.ssl3.sessionIDLength, 1);
4054 else 4226 else
4055 rv = ssl3_AppendHandshakeVariable(ss, NULL, 0, 1); 4227 rv = ssl3_AppendHandshakeVariable(ss, NULL, 0, 1);
4056 if (rv != SECSuccess) { 4228 if (rv != SECSuccess) {
4057 return rv; /* err set by ssl3_AppendHandshake* */ 4229 return rv; /* err set by ssl3_AppendHandshake* */
4058 } 4230 }
4059 4231
4232 if (IS_DTLS(ss)) {
4233 rv = ssl3_AppendHandshakeVariable(
4234 ss, ss->ssl3.hs.cookie, ss->ssl3.hs.cookieLen, 1);
4235 if (rv != SECSuccess) {
4236 return rv; /* err set by ssl3_AppendHandshake* */
4237 }
4238 }
4239
4060 rv = ssl3_AppendHandshakeNumber(ss, num_suites*sizeof(ssl3CipherSuite), 2); 4240 rv = ssl3_AppendHandshakeNumber(ss, num_suites*sizeof(ssl3CipherSuite), 2);
4061 if (rv != SECSuccess) { 4241 if (rv != SECSuccess) {
4062 return rv; /* err set by ssl3_AppendHandshake* */ 4242 return rv; /* err set by ssl3_AppendHandshake* */
4063 } 4243 }
4064 4244
4065 if (ss->ssl3.hs.sendingSCSV) { 4245 if (ss->ssl3.hs.sendingSCSV) {
4066 /* Add the actual SCSV */ 4246 /* Add the actual SCSV */
4067 rv = ssl3_AppendHandshakeNumber(ss, TLS_EMPTY_RENEGOTIATION_INFO_SCSV, 4247 rv = ssl3_AppendHandshakeNumber(ss, TLS_EMPTY_RENEGOTIATION_INFO_SCSV,
4068 sizeof(ssl3CipherSuite)); 4248 sizeof(ssl3CipherSuite));
4069 if (rv != SECSuccess) { 4249 if (rv != SECSuccess) {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
4173 PORT_SetError(SSL_ERROR_RENEGOTIATION_NOT_ALLOWED); 4353 PORT_SetError(SSL_ERROR_RENEGOTIATION_NOT_ALLOWED);
4174 return SECFailure; 4354 return SECFailure;
4175 } 4355 }
4176 4356
4177 if (sid) { 4357 if (sid) {
4178 ss->sec.uncache(sid); 4358 ss->sec.uncache(sid);
4179 ssl_FreeSID(sid); 4359 ssl_FreeSID(sid);
4180 ss->sec.ci.sid = NULL; 4360 ss->sec.ci.sid = NULL;
4181 } 4361 }
4182 4362
4363 if (IS_DTLS(ss)) {
4364 dtls_RehandshakeCleanup(ss);
4365 }
4366
4183 ssl_GetXmitBufLock(ss); 4367 ssl_GetXmitBufLock(ss);
4184 rv = ssl3_SendClientHello(ss); 4368 rv = ssl3_SendClientHello(ss, PR_FALSE);
4185 ssl_ReleaseXmitBufLock(ss); 4369 ssl_ReleaseXmitBufLock(ss);
4186 4370
4187 return rv; 4371 return rv;
4188 } 4372 }
4189 4373
4190 #define UNKNOWN_WRAP_MECHANISM 0x7fffffff 4374 #define UNKNOWN_WRAP_MECHANISM 0x7fffffff
4191 4375
4192 static const CK_MECHANISM_TYPE wrapMechanismList[SSL_NUM_WRAP_MECHS] = { 4376 static const CK_MECHANISM_TYPE wrapMechanismList[SSL_NUM_WRAP_MECHS] = {
4193 CKM_DES3_ECB, 4377 CKM_DES3_ECB,
4194 CKM_CAST5_ECB, 4378 CKM_CAST5_ECB,
(...skipping 834 matching lines...) Expand 10 before | Expand all | Expand 10 after
5029 ss->ssl3.platformClientKey = (PlatformKey)NULL; 5213 ss->ssl3.platformClientKey = (PlatformKey)NULL;
5030 } 5214 }
5031 #endif /* NSS_PLATFORM_CLIENT_AUTH */ 5215 #endif /* NSS_PLATFORM_CLIENT_AUTH */
5032 5216
5033 temp = ssl3_ConsumeHandshakeNumber(ss, 2, &b, &length); 5217 temp = ssl3_ConsumeHandshakeNumber(ss, 2, &b, &length);
5034 if (temp < 0) { 5218 if (temp < 0) {
5035 goto loser; /* alert has been sent */ 5219 goto loser; /* alert has been sent */
5036 } 5220 }
5037 version = (SSL3ProtocolVersion)temp; 5221 version = (SSL3ProtocolVersion)temp;
5038 5222
5223 if (IS_DTLS(ss)) {
5224 /* RFC 4347 required that you verify that the server versions
5225 * match (S 4.2.1) in the HelloVerifyRequest and the ServerHello
5226 *
5227 * RFC 6347 suggests (SHOULD) that servers always use 1.0
5228 * in HelloVerifyRequest and allows the versions not to match,
5229 * esp. when 1.2 is being negotiated.
5230 *
5231 * Therefore we do not check for matching here.
5232 */
5233 version = dtls_DTLSVersionToTLSVersion(version);
5234 if (version == 0) { /* Insane version number */
5235 goto alert_loser;
5236 }
5237 }
5238
5039 rv = ssl3_NegotiateVersion(ss, version, PR_FALSE); 5239 rv = ssl3_NegotiateVersion(ss, version, PR_FALSE);
5040 if (rv != SECSuccess) { 5240 if (rv != SECSuccess) {
5041 desc = (version > SSL_LIBRARY_VERSION_3_0) ? protocol_version 5241 desc = (version > SSL_LIBRARY_VERSION_3_0) ? protocol_version
5042 : handshake_failure; 5242 : handshake_failure;
5043 errCode = SSL_ERROR_NO_CYPHER_OVERLAP; 5243 errCode = SSL_ERROR_NO_CYPHER_OVERLAP;
5044 goto alert_loser; 5244 goto alert_loser;
5045 } 5245 }
5046 isTLS = (ss->version > SSL_LIBRARY_VERSION_3_0); 5246 isTLS = (ss->version > SSL_LIBRARY_VERSION_3_0);
5047 5247
5048 rv = ssl3_ConsumeHandshake( 5248 rv = ssl3_ConsumeHandshake(
(...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after
6257 sslSessionID * sid = NULL; 6457 sslSessionID * sid = NULL;
6258 PRInt32 tmp; 6458 PRInt32 tmp;
6259 unsigned int i; 6459 unsigned int i;
6260 int j; 6460 int j;
6261 SECStatus rv; 6461 SECStatus rv;
6262 int errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; 6462 int errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO;
6263 SSL3AlertDescription desc = illegal_parameter; 6463 SSL3AlertDescription desc = illegal_parameter;
6264 SSL3AlertLevel level = alert_fatal; 6464 SSL3AlertLevel level = alert_fatal;
6265 SSL3ProtocolVersion version; 6465 SSL3ProtocolVersion version;
6266 SECItem sidBytes = {siBuffer, NULL, 0}; 6466 SECItem sidBytes = {siBuffer, NULL, 0};
6467 SECItem cookieBytes = {siBuffer, NULL, 0};
6267 SECItem suites = {siBuffer, NULL, 0}; 6468 SECItem suites = {siBuffer, NULL, 0};
6268 SECItem comps = {siBuffer, NULL, 0}; 6469 SECItem comps = {siBuffer, NULL, 0};
6269 PRBool haveSpecWriteLock = PR_FALSE; 6470 PRBool haveSpecWriteLock = PR_FALSE;
6270 PRBool haveXmitBufLock = PR_FALSE; 6471 PRBool haveXmitBufLock = PR_FALSE;
6271 6472
6272 SSL_TRC(3, ("%d: SSL3[%d]: handle client_hello handshake", 6473 SSL_TRC(3, ("%d: SSL3[%d]: handle client_hello handshake",
6273 SSL_GETPID(), ss->fd)); 6474 SSL_GETPID(), ss->fd));
6274 6475
6275 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) ); 6476 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
6276 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); 6477 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
6277 6478
6278 /* Get peer name of client */ 6479 /* Get peer name of client */
6279 rv = ssl_GetPeerInfo(ss); 6480 rv = ssl_GetPeerInfo(ss);
6280 if (rv != SECSuccess) { 6481 if (rv != SECSuccess) {
6281 return rv; /* error code is set. */ 6482 return rv; /* error code is set. */
6282 } 6483 }
6283 6484
6485 /* Clearing the handshake pointers so that ssl_Do1stHandshake won't
6486 * call ssl2_HandleMessage.
6487 *
6488 * The issue here is that TLS ordinarily starts out in
6489 * ssl2_HandleV3HandshakeRecord() because of the backward-compatibility
6490 * code paths. That function zeroes these next pointers. But with DTLS,
6491 * we don't even try to do the v2 ClientHello so we skip that function
6492 * and need to reset these values here.
6493 */
6494 ss->nextHandshake = NULL;
6495 ss->securityHandshake = NULL;
wtc 2012/03/22 01:11:42 rsleevi: I don't fully understand this change. I
Ryan Sleevi 2012/03/22 22:26:37 style nit: use 0 rather than NULL, for consistency
wtc 2012/03/23 00:21:18 The complete lock rank is documented in ssl/notes.
6496
6284 /* We might be starting session renegotiation in which case we should 6497 /* We might be starting session renegotiation in which case we should
6285 * clear previous state. 6498 * clear previous state.
6286 */ 6499 */
6287 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData)); 6500 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData));
6288 ss->statelessResume = PR_FALSE; 6501 ss->statelessResume = PR_FALSE;
6289 6502
6290 rv = ssl3_InitState(ss); 6503 rv = ssl3_InitState(ss);
6291 if (rv != SECSuccess) { 6504 if (rv != SECSuccess) {
6292 return rv; /* ssl3_InitState has set the error code. */ 6505 return rv; /* ssl3_InitState has set the error code. */
6293 } 6506 }
6294 6507
6295 if ((ss->ssl3.hs.ws != wait_client_hello) && 6508 if ((ss->ssl3.hs.ws != wait_client_hello) &&
6296 (ss->ssl3.hs.ws != idle_handshake)) { 6509 (ss->ssl3.hs.ws != idle_handshake)) {
6297 desc = unexpected_message; 6510 desc = unexpected_message;
6298 errCode = SSL_ERROR_RX_UNEXPECTED_CLIENT_HELLO; 6511 errCode = SSL_ERROR_RX_UNEXPECTED_CLIENT_HELLO;
6299 goto alert_loser; 6512 goto alert_loser;
6300 } 6513 }
6301 if (ss->ssl3.hs.ws == idle_handshake && 6514 if (ss->ssl3.hs.ws == idle_handshake &&
6302 ss->opt.enableRenegotiation == SSL_RENEGOTIATE_NEVER) { 6515 ss->opt.enableRenegotiation == SSL_RENEGOTIATE_NEVER) {
6303 desc = no_renegotiation; 6516 desc = no_renegotiation;
6304 level = alert_warning; 6517 level = alert_warning;
6305 errCode = SSL_ERROR_RENEGOTIATION_NOT_ALLOWED; 6518 errCode = SSL_ERROR_RENEGOTIATION_NOT_ALLOWED;
6306 goto alert_loser; 6519 goto alert_loser;
6307 } 6520 }
6308 6521
6522 if (IS_DTLS(ss)) {
6523 dtls_RehandshakeCleanup(ss);
6524 }
6525
6309 tmp = ssl3_ConsumeHandshakeNumber(ss, 2, &b, &length); 6526 tmp = ssl3_ConsumeHandshakeNumber(ss, 2, &b, &length);
6310 if (tmp < 0) 6527 if (tmp < 0)
6311 goto loser; /* malformed, alert already sent */ 6528 goto loser; /* malformed, alert already sent */
6312 ss->clientHelloVersion = version = (SSL3ProtocolVersion)tmp; 6529
6530 /* Translate the version */
6531 if (IS_DTLS(ss)) {
6532 » ss->clientHelloVersion = version =
6533 » dtls_DTLSVersionToTLSVersion((SSL3ProtocolVersion)tmp);
6534 } else {
6535 » ss->clientHelloVersion = version = (SSL3ProtocolVersion)tmp;
6536 }
6537
6313 rv = ssl3_NegotiateVersion(ss, version, PR_TRUE); 6538 rv = ssl3_NegotiateVersion(ss, version, PR_TRUE);
6314 if (rv != SECSuccess) { 6539 if (rv != SECSuccess) {
6315 desc = (version > SSL_LIBRARY_VERSION_3_0) ? protocol_version 6540 desc = (version > SSL_LIBRARY_VERSION_3_0) ? protocol_version
6316 : handshake_failure; 6541 : handshake_failure;
6317 errCode = SSL_ERROR_NO_CYPHER_OVERLAP; 6542 errCode = SSL_ERROR_NO_CYPHER_OVERLAP;
6318 goto alert_loser; 6543 goto alert_loser;
6319 } 6544 }
6320 6545
6321 /* grab the client random data. */ 6546 /* grab the client random data. */
6322 rv = ssl3_ConsumeHandshake( 6547 rv = ssl3_ConsumeHandshake(
6323 ss, &ss->ssl3.hs.client_random, SSL3_RANDOM_LENGTH, &b, &length); 6548 ss, &ss->ssl3.hs.client_random, SSL3_RANDOM_LENGTH, &b, &length);
6324 if (rv != SECSuccess) { 6549 if (rv != SECSuccess) {
6325 goto loser; /* malformed */ 6550 goto loser; /* malformed */
6326 } 6551 }
6327 6552
6328 /* grab the client's SID, if present. */ 6553 /* grab the client's SID, if present. */
6329 rv = ssl3_ConsumeHandshakeVariable(ss, &sidBytes, 1, &b, &length); 6554 rv = ssl3_ConsumeHandshakeVariable(ss, &sidBytes, 1, &b, &length);
6330 if (rv != SECSuccess) { 6555 if (rv != SECSuccess) {
6331 goto loser; /* malformed */ 6556 goto loser; /* malformed */
6332 } 6557 }
6333 6558
6559 /* grab the client's cookie, if present. */
6560 if (IS_DTLS(ss)) {
6561 rv = ssl3_ConsumeHandshakeVariable(ss, &cookieBytes, 1, &b, &length);
6562 if (rv != SECSuccess) {
6563 goto loser; /* malformed */
6564 }
6565 }
6566
6334 /* grab the list of cipher suites. */ 6567 /* grab the list of cipher suites. */
6335 rv = ssl3_ConsumeHandshakeVariable(ss, &suites, 2, &b, &length); 6568 rv = ssl3_ConsumeHandshakeVariable(ss, &suites, 2, &b, &length);
6336 if (rv != SECSuccess) { 6569 if (rv != SECSuccess) {
6337 goto loser; /* malformed */ 6570 goto loser; /* malformed */
6338 } 6571 }
6339 6572
6340 /* grab the list of compression methods. */ 6573 /* grab the list of compression methods. */
6341 rv = ssl3_ConsumeHandshakeVariable(ss, &comps, 1, &b, &length); 6574 rv = ssl3_ConsumeHandshakeVariable(ss, &comps, 1, &b, &length);
6342 if (rv != SECSuccess) { 6575 if (rv != SECSuccess) {
6343 goto loser; /* malformed */ 6576 goto loser; /* malformed */
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
6472 ssl_FreeSID(sid); 6705 ssl_FreeSID(sid);
6473 sid = NULL; 6706 sid = NULL;
6474 } 6707 }
6475 } 6708 }
6476 6709
6477 #ifdef NSS_ENABLE_ECC 6710 #ifdef NSS_ENABLE_ECC
6478 /* Disable any ECC cipher suites for which we have no cert. */ 6711 /* Disable any ECC cipher suites for which we have no cert. */
6479 ssl3_FilterECCipherSuitesByServerCerts(ss); 6712 ssl3_FilterECCipherSuitesByServerCerts(ss);
6480 #endif 6713 #endif
6481 6714
6715 if (IS_DTLS(ss)) {
6716 ssl3_DisableNonDTLSSuites(ss);
6717 }
6718
6482 #ifdef PARANOID 6719 #ifdef PARANOID
6483 /* Look for a matching cipher suite. */ 6720 /* Look for a matching cipher suite. */
6484 j = ssl3_config_match_init(ss); 6721 j = ssl3_config_match_init(ss);
6485 if (j <= 0) { /* no ciphers are working/supported by PK11 */ 6722 if (j <= 0) { /* no ciphers are working/supported by PK11 */
6486 errCode = PORT_GetError(); /* error code is already set. */ 6723 errCode = PORT_GetError(); /* error code is already set. */
6487 goto alert_loser; 6724 goto alert_loser;
6488 } 6725 }
6489 #endif 6726 #endif
6490 6727
6491 /* If we already have a session for this client, be sure to pick the 6728 /* If we already have a session for this client, be sure to pick the
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after
7159 ** ssl3_SendServerHelloSequence <- ssl3_HandleV2ClientHello (new session) 7396 ** ssl3_SendServerHelloSequence <- ssl3_HandleV2ClientHello (new session)
7160 */ 7397 */
7161 static SECStatus 7398 static SECStatus
7162 ssl3_SendServerHello(sslSocket *ss) 7399 ssl3_SendServerHello(sslSocket *ss)
7163 { 7400 {
7164 sslSessionID *sid; 7401 sslSessionID *sid;
7165 SECStatus rv; 7402 SECStatus rv;
7166 PRUint32 maxBytes = 65535; 7403 PRUint32 maxBytes = 65535;
7167 PRUint32 length; 7404 PRUint32 length;
7168 PRInt32 extensions_len = 0; 7405 PRInt32 extensions_len = 0;
7406 SSL3ProtocolVersion version;
7169 7407
7170 SSL_TRC(3, ("%d: SSL3[%d]: send server_hello handshake", SSL_GETPID(), 7408 SSL_TRC(3, ("%d: SSL3[%d]: send server_hello handshake", SSL_GETPID(),
7171 ss->fd)); 7409 ss->fd));
7172 7410
7173 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); 7411 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss));
7174 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); 7412 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
7175 PORT_Assert( MSB(ss->version) == MSB(SSL_LIBRARY_VERSION_3_0));
7176 7413
7177 if (MSB(ss->version) != MSB(SSL_LIBRARY_VERSION_3_0)) { 7414 if (!IS_DTLS(ss)) {
7178 » PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP); 7415 » PORT_Assert(MSB(ss->version) == MSB(SSL_LIBRARY_VERSION_3_0));
7179 » return SECFailure; 7416
7417 » if (MSB(ss->version) != MSB(SSL_LIBRARY_VERSION_3_0)) {
7418 » PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP);
7419 » return SECFailure;
7420 » }
7421 } else {
7422 » PORT_Assert(MSB(ss->version) == MSB(SSL_LIBRARY_VERSION_DTLS_1_0));
7423
7424 » if (MSB(ss->version) != MSB(SSL_LIBRARY_VERSION_DTLS_1_0)) {
7425 » PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP);
7426 » return SECFailure;
7427 » }
7180 } 7428 }
7181 7429
7182 sid = ss->sec.ci.sid; 7430 sid = ss->sec.ci.sid;
7183 7431
7184 extensions_len = ssl3_CallHelloExtensionSenders(ss, PR_FALSE, maxBytes, 7432 extensions_len = ssl3_CallHelloExtensionSenders(ss, PR_FALSE, maxBytes,
7185 &ss->xtnData.serverSenders[0]); 7433 &ss->xtnData.serverSenders[0]);
7186 if (extensions_len > 0) 7434 if (extensions_len > 0)
7187 extensions_len += 2; /* Add sizeof total extension length */ 7435 extensions_len += 2; /* Add sizeof total extension length */
7188 7436
7189 length = sizeof(SSL3ProtocolVersion) + SSL3_RANDOM_LENGTH + 1 + 7437 length = sizeof(SSL3ProtocolVersion) + SSL3_RANDOM_LENGTH + 1 +
7190 ((sid == NULL) ? 0: sid->u.ssl3.sessionIDLength) + 7438 ((sid == NULL) ? 0: sid->u.ssl3.sessionIDLength) +
7191 sizeof(ssl3CipherSuite) + 1 + extensions_len; 7439 sizeof(ssl3CipherSuite) + 1 + extensions_len;
7192 rv = ssl3_AppendHandshakeHeader(ss, server_hello, length); 7440 rv = ssl3_AppendHandshakeHeader(ss, server_hello, length);
7193 if (rv != SECSuccess) { 7441 if (rv != SECSuccess) {
7194 return rv; /* err set by AppendHandshake. */ 7442 return rv; /* err set by AppendHandshake. */
7195 } 7443 }
7196 7444
7197 rv = ssl3_AppendHandshakeNumber(ss, ss->version, 2); 7445 if (IS_DTLS(ss)) {
7446 » version = dtls_TLSVersionToDTLSVersion(ss->version);
7447 } else {
7448 » version = ss->version;
7449 }
7450
7451 rv = ssl3_AppendHandshakeNumber(ss, version, 2);
7198 if (rv != SECSuccess) { 7452 if (rv != SECSuccess) {
7199 return rv; /* err set by AppendHandshake. */ 7453 return rv; /* err set by AppendHandshake. */
7200 } 7454 }
7201 rv = ssl3_GetNewRandom(&ss->ssl3.hs.server_random); 7455 rv = ssl3_GetNewRandom(&ss->ssl3.hs.server_random);
7202 if (rv != SECSuccess) { 7456 if (rv != SECSuccess) {
7203 ssl_MapLowLevelError(SSL_ERROR_GENERATE_RANDOM_FAILURE); 7457 ssl_MapLowLevelError(SSL_ERROR_GENERATE_RANDOM_FAILURE);
7204 return rv; 7458 return rv;
7205 } 7459 }
7206 rv = ssl3_AppendHandshake( 7460 rv = ssl3_AppendHandshake(
7207 ss, &ss->ssl3.hs.server_random, SSL3_RANDOM_LENGTH); 7461 ss, &ss->ssl3.hs.server_random, SSL3_RANDOM_LENGTH);
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
7372 ca_list = ss->ssl3.ca_list; 7626 ca_list = ss->ssl3.ca_list;
7373 if (!ca_list) { 7627 if (!ca_list) {
7374 ca_list = ssl3_server_ca_list; 7628 ca_list = ssl3_server_ca_list;
7375 } 7629 }
7376 7630
7377 if (ca_list != NULL) { 7631 if (ca_list != NULL) {
7378 names = ca_list->names; 7632 names = ca_list->names;
7379 nnames = ca_list->nnames; 7633 nnames = ca_list->nnames;
7380 } 7634 }
7381 7635
7382 if (!nnames) { 7636 /* There used to be a test here to require a CA, but there
7383 » PORT_SetError(SSL_ERROR_NO_TRUSTED_SSL_CLIENT_CA); 7637 * are cases where you want to have no CAs offered. */
7384 » return SECFailure;
7385 }
wtc 2012/03/22 01:11:42 Removing this check allows an SSL server to reques
Ryan Sleevi 2012/03/22 22:26:37 I'm not sure I agree with this explanation, but I'
wtc 2012/03/23 00:21:18 DTLS server mode works. Only HelloVerifyRequest i
ekr 2012/03/23 12:46:41 Correct, and this is optional. Let me give you a
7386
7387 for (i = 0, name = names; i < nnames; i++, name++) { 7638 for (i = 0, name = names; i < nnames; i++, name++) {
7388 calen += 2 + name->len; 7639 calen += 2 + name->len;
7389 } 7640 }
7390 7641
7391 certTypes = certificate_types; 7642 certTypes = certificate_types;
7392 certTypesLength = sizeof certificate_types; 7643 certTypesLength = sizeof certificate_types;
7393 7644
7394 length = 1 + certTypesLength + 2 + calen; 7645 length = 1 + certTypesLength + 2 + calen;
7395 7646
7396 rv = ssl3_AppendHandshakeHeader(ss, certificate_request, length); 7647 rv = ssl3_AppendHandshakeHeader(ss, certificate_request, length);
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
7544 /* can't find a slot with all three, find a slot with the minimum */ 7795 /* can't find a slot with all three, find a slot with the minimum */
7545 slot = PK11_GetBestSlotMultiple(mechanism_array, 2, pwArg); 7796 slot = PK11_GetBestSlotMultiple(mechanism_array, 2, pwArg);
7546 if (slot == NULL) { 7797 if (slot == NULL) {
7547 PORT_SetError(SSL_ERROR_TOKEN_SLOT_NOT_FOUND); 7798 PORT_SetError(SSL_ERROR_TOKEN_SLOT_NOT_FOUND);
7548 return pms; /* which is NULL */ 7799 return pms; /* which is NULL */
7549 } 7800 }
7550 } 7801 }
7551 } 7802 }
7552 7803
7553 /* Generate the pre-master secret ... */ 7804 /* Generate the pre-master secret ... */
7554 version.major = MSB(ss->clientHelloVersion); 7805 if (IS_DTLS(ss)) {
7555 version.minor = LSB(ss->clientHelloVersion); 7806 » SSL3ProtocolVersion temp;
7807
7808 » temp = dtls_TLSVersionToDTLSVersion(ss->clientHelloVersion);
7809 » version.major = MSB(temp);
7810 » version.minor = LSB(temp);
7811 } else {
7812 » version.major = MSB(ss->clientHelloVersion);
7813 » version.minor = LSB(ss->clientHelloVersion);
7814 }
7556 7815
7557 param.data = (unsigned char *)&version; 7816 param.data = (unsigned char *)&version;
7558 param.len = sizeof version; 7817 param.len = sizeof version;
7559 7818
7560 pms = PK11_KeyGen(slot, CKM_SSL3_PRE_MASTER_KEY_GEN, &param, 0, pwArg); 7819 pms = PK11_KeyGen(slot, CKM_SSL3_PRE_MASTER_KEY_GEN, &param, 0, pwArg);
7561 if (!serverKeySlot) 7820 if (!serverKeySlot)
7562 PK11_FreeSlot(slot); 7821 PK11_FreeSlot(slot);
7563 if (pms == NULL) { 7822 if (pms == NULL) {
7564 ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); 7823 ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE);
7565 } 7824 }
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
7628 */ 7887 */
7629 7888
7630 rv = PK11_PrivDecryptPKCS1(serverKey, rsaPmsBuf, &outLen, 7889 rv = PK11_PrivDecryptPKCS1(serverKey, rsaPmsBuf, &outLen,
7631 sizeof rsaPmsBuf, enc_pms.data, enc_pms.len); 7890 sizeof rsaPmsBuf, enc_pms.data, enc_pms.len);
7632 if (rv != SECSuccess) { 7891 if (rv != SECSuccess) {
7633 /* triple bypass failed. Let's try for a double bypass. */ 7892 /* triple bypass failed. Let's try for a double bypass. */
7634 goto double_bypass; 7893 goto double_bypass;
7635 } else if (ss->opt.detectRollBack) { 7894 } else if (ss->opt.detectRollBack) {
7636 SSL3ProtocolVersion client_version = 7895 SSL3ProtocolVersion client_version =
7637 (rsaPmsBuf[0] << 8) | rsaPmsBuf[1]; 7896 (rsaPmsBuf[0] << 8) | rsaPmsBuf[1];
7897
7898 if (IS_DTLS(ss)) {
7899 client_version = dtls_DTLSVersionToTLSVersion(client_version);
7900 }
Ryan Sleevi 2012/03/22 22:26:37 nit: Inconsistent tabbing within this (compared to
wtc 2012/03/23 00:21:18 The new code uses tabs. I think Rietveld doesn't
7901
7638 if (client_version != ss->clientHelloVersion) { 7902 if (client_version != ss->clientHelloVersion) {
7639 /* Version roll-back detected. ensure failure. */ 7903 /* Version roll-back detected. ensure failure. */
7640 rv = PK11_GenerateRandom(rsaPmsBuf, sizeof rsaPmsBuf); 7904 rv = PK11_GenerateRandom(rsaPmsBuf, sizeof rsaPmsBuf);
7641 } 7905 }
7642 } 7906 }
7643 /* have PMS, build MS without PKCS11 */ 7907 /* have PMS, build MS without PKCS11 */
7644 rv = ssl3_MasterKeyDeriveBypass(pwSpec, cr, sr, &pmsItem, isTLS, 7908 rv = ssl3_MasterKeyDeriveBypass(pwSpec, cr, sr, &pmsItem, isTLS,
7645 PR_TRUE); 7909 PR_TRUE);
7646 if (rv != SECSuccess) { 7910 if (rv != SECSuccess) {
7647 pwSpec->msItem.data = pwSpec->raw_master_secret; 7911 pwSpec->msItem.data = pwSpec->raw_master_secret;
(...skipping 1196 matching lines...) Expand 10 before | Expand all | Expand 10 after
8844 flags = ssl_SEND_FLAG_FORCE_INTO_BUFFER; 9108 flags = ssl_SEND_FLAG_FORCE_INTO_BUFFER;
8845 } 9109 }
8846 9110
8847 if (!isServer && !ss->firstHsDone) { 9111 if (!isServer && !ss->firstHsDone) {
8848 rv = ssl3_SendNextProto(ss); 9112 rv = ssl3_SendNextProto(ss);
8849 if (rv != SECSuccess) { 9113 if (rv != SECSuccess) {
8850 goto xmit_loser; /* err code was set. */ 9114 goto xmit_loser; /* err code was set. */
8851 } 9115 }
8852 } 9116 }
8853 9117
9118 if (IS_DTLS(ss)) {
9119 flags |= ssl_SEND_FLAG_NO_RETRANSMIT;
9120 }
9121
8854 rv = ssl3_SendFinished(ss, flags); 9122 rv = ssl3_SendFinished(ss, flags);
8855 if (rv != SECSuccess) { 9123 if (rv != SECSuccess) {
8856 goto xmit_loser; /* err is set. */ 9124 goto xmit_loser; /* err is set. */
8857 } 9125 }
8858 } 9126 }
8859 9127
8860 xmit_loser: 9128 xmit_loser:
8861 ssl_ReleaseXmitBufLock(ss); /*************************************/ 9129 ssl_ReleaseXmitBufLock(ss); /*************************************/
8862 if (rv != SECSuccess) { 9130 if (rv != SECSuccess) {
8863 return rv; 9131 return rv;
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
8973 ss->ssl3.hs.pending_cert_msg.len); 9241 ss->ssl3.hs.pending_cert_msg.len);
8974 SECITEM_FreeItem(&ss->ssl3.hs.pending_cert_msg, PR_FALSE); 9242 SECITEM_FreeItem(&ss->ssl3.hs.pending_cert_msg, PR_FALSE);
8975 } 9243 }
8976 return rv; 9244 return rv;
8977 } 9245 }
8978 9246
8979 /* Called from ssl3_HandleHandshake() when it has gathered a complete ssl3 9247 /* Called from ssl3_HandleHandshake() when it has gathered a complete ssl3
8980 * hanshake message. 9248 * hanshake message.
8981 * Caller must hold Handshake and RecvBuf locks. 9249 * Caller must hold Handshake and RecvBuf locks.
8982 */ 9250 */
8983 static SECStatus 9251 SECStatus
8984 ssl3_HandleHandshakeMessage(sslSocket *ss, SSL3Opaque *b, PRUint32 length) 9252 ssl3_HandleHandshakeMessage(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
8985 { 9253 {
8986 SECStatus rv = SECSuccess; 9254 SECStatus rv = SECSuccess;
8987 SSL3HandshakeType type = ss->ssl3.hs.msg_type; 9255 SSL3HandshakeType type = ss->ssl3.hs.msg_type;
8988 SSL3Hashes hashes; /* computed hashes are put here. */ 9256 SSL3Hashes hashes; /* computed hashes are put here. */
8989 PRUint8 hdr[4]; 9257 PRUint8 hdr[4];
9258 PRUint8 dtlsData[8];
8990 9259
8991 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) ); 9260 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
8992 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) ); 9261 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
8993 /* 9262 /*
8994 * We have to compute the hashes before we update them with the 9263 * We have to compute the hashes before we update them with the
8995 * current message. 9264 * current message.
8996 */ 9265 */
8997 ssl_GetSpecReadLock(ss); /************************************/ 9266 ssl_GetSpecReadLock(ss); /************************************/
8998 if((type == finished) || (type == certificate_verify)) { 9267 if((type == finished) || (type == certificate_verify)) {
8999 SSL3Sender sender = (SSL3Sender)0; 9268 SSL3Sender sender = (SSL3Sender)0;
(...skipping 26 matching lines...) Expand all
9026 /* Start new handshake hashes when we start a new handshake */ 9295 /* Start new handshake hashes when we start a new handshake */
9027 if (ss->ssl3.hs.msg_type == client_hello) { 9296 if (ss->ssl3.hs.msg_type == client_hello) {
9028 SSL_TRC(30,("%d: SSL3[%d]: reset handshake hashes", 9297 SSL_TRC(30,("%d: SSL3[%d]: reset handshake hashes",
9029 SSL_GETPID(), ss->fd )); 9298 SSL_GETPID(), ss->fd ));
9030 rv = ssl3_RestartHandshakeHashes(ss); 9299 rv = ssl3_RestartHandshakeHashes(ss);
9031 if (rv != SECSuccess) { 9300 if (rv != SECSuccess) {
9032 return rv; 9301 return rv;
9033 } 9302 }
9034 } 9303 }
9035 /* We should not include hello_request messages in the handshake hashes */ 9304 /* We should not include hello_request messages in the handshake hashes */
9036 if (ss->ssl3.hs.msg_type != hello_request) { 9305 if ((ss->ssl3.hs.msg_type != hello_request) &&
9306 » (ss->ssl3.hs.msg_type != hello_verify_request)) {
Ryan Sleevi 2012/03/22 22:26:37 Should there also be a check for && (!IS_DTLS(ss
wtc 2012/03/23 00:21:18 I guess you're asking about excluding the initial
9037 rv = ssl3_UpdateHandshakeHashes(ss, (unsigned char*) hdr, 4); 9307 rv = ssl3_UpdateHandshakeHashes(ss, (unsigned char*) hdr, 4);
9038 if (rv != SECSuccess) return rv; /* err code already set. */ 9308 if (rv != SECSuccess) return rv; /* err code already set. */
9309
9310 /* Extra data to simulate a complete DTLS handshake fragment */
9311 if (IS_DTLS(ss)) {
9312 /* Sequence number */
9313 dtlsData[0] = MSB(ss->ssl3.hs.recvMessageSeq);
9314 dtlsData[1] = LSB(ss->ssl3.hs.recvMessageSeq);
9315
9316 /* Fragment offset */
9317 dtlsData[2] = 0;
9318 dtlsData[3] = 0;
9319 dtlsData[4] = 0;
9320
9321 /* Fragment length */
9322 dtlsData[5] = (PRUint8)(length >> 16);
9323 dtlsData[6] = (PRUint8)(length >> 8);
9324 dtlsData[7] = (PRUint8)(length );
9325
9326 rv = ssl3_UpdateHandshakeHashes(ss, (unsigned char*) dtlsData,
Ryan Sleevi 2012/03/22 22:26:37 nit: delete the space in "(unsigned char*) dtlsDat
wtc 2012/03/23 00:21:18 I suspect that space was copied from line 9307 abo
9327 sizeof(dtlsData));
9328 if (rv != SECSuccess) return rv; /* err code already set. */
9329 }
9330
9331 /* The message body */
9039 rv = ssl3_UpdateHandshakeHashes(ss, b, length); 9332 rv = ssl3_UpdateHandshakeHashes(ss, b, length);
9040 if (rv != SECSuccess) return rv; /* err code already set. */ 9333 if (rv != SECSuccess) return rv; /* err code already set. */
9041 } 9334 }
9042 9335
9043 PORT_SetError(0); /* each message starts with no error. */ 9336 PORT_SetError(0); /* each message starts with no error. */
9044 switch (ss->ssl3.hs.msg_type) { 9337 switch (ss->ssl3.hs.msg_type) {
9045 case hello_request: 9338 case hello_request:
9046 if (length != 0) { 9339 if (length != 0) {
9047 (void)ssl3_DecodeError(ss); 9340 (void)ssl3_DecodeError(ss);
9048 PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_REQUEST); 9341 PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_REQUEST);
(...skipping 15 matching lines...) Expand all
9064 rv = ssl3_HandleClientHello(ss, b, length); 9357 rv = ssl3_HandleClientHello(ss, b, length);
9065 break; 9358 break;
9066 case server_hello: 9359 case server_hello:
9067 if (ss->sec.isServer) { 9360 if (ss->sec.isServer) {
9068 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 9361 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9069 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_SERVER_HELLO); 9362 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_SERVER_HELLO);
9070 return SECFailure; 9363 return SECFailure;
9071 } 9364 }
9072 rv = ssl3_HandleServerHello(ss, b, length); 9365 rv = ssl3_HandleServerHello(ss, b, length);
9073 break; 9366 break;
9367 case hello_verify_request:
9368 if (!IS_DTLS(ss) || ss->sec.isServer) {
9369 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9370 PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_VERIFY_REQUEST);
9371 return SECFailure;
9372 }
9373 rv = dtls_HandleHelloVerifyRequest(ss, b, length);
9374 break;
9074 case certificate: 9375 case certificate:
9075 if (ss->ssl3.hs.may_get_cert_status) { 9376 if (ss->ssl3.hs.may_get_cert_status) {
9076 /* If we might get a CertificateStatus then we want to postpone the 9377 /* If we might get a CertificateStatus then we want to postpone the
9077 * processing of the Certificate message until after we have 9378 * processing of the Certificate message until after we have
9078 * processed the CertificateStatus */ 9379 * processed the CertificateStatus */
9079 if (ss->ssl3.hs.pending_cert_msg.data || 9380 if (ss->ssl3.hs.pending_cert_msg.data ||
9080 ss->ssl3.hs.ws != wait_server_cert) { 9381 ss->ssl3.hs.ws != wait_server_cert) {
9081 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 9382 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9082 (void)ssl_MapLowLevelError(SSL_ERROR_RX_UNEXPECTED_CERTIFICATE); 9383 (void)ssl_MapLowLevelError(SSL_ERROR_RX_UNEXPECTED_CERTIFICATE);
9083 return SECFailure; 9384 return SECFailure;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
9162 rv = ssl3_HandleNewSessionTicket(ss, b, length); 9463 rv = ssl3_HandleNewSessionTicket(ss, b, length);
9163 break; 9464 break;
9164 case finished: 9465 case finished:
9165 rv = ssl3_HandleFinished(ss, b, length, &hashes); 9466 rv = ssl3_HandleFinished(ss, b, length, &hashes);
9166 break; 9467 break;
9167 default: 9468 default:
9168 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); 9469 (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message);
9169 PORT_SetError(SSL_ERROR_RX_UNKNOWN_HANDSHAKE); 9470 PORT_SetError(SSL_ERROR_RX_UNKNOWN_HANDSHAKE);
9170 rv = SECFailure; 9471 rv = SECFailure;
9171 } 9472 }
9473
9474 if (IS_DTLS(ss) && (rv == SECSuccess)) {
9475 /* Increment the expected sequence number */
9476 ss->ssl3.hs.recvMessageSeq++;
9477 }
9478
9172 return rv; 9479 return rv;
9173 } 9480 }
9174 9481
9175 /* Called only from ssl3_HandleRecord, for each (deciphered) ssl3 record. 9482 /* Called only from ssl3_HandleRecord, for each (deciphered) ssl3 record.
9176 * origBuf is the decrypted ssl record content. 9483 * origBuf is the decrypted ssl record content.
9177 * Caller must hold the handshake and RecvBuf locks. 9484 * Caller must hold the handshake and RecvBuf locks.
9178 */ 9485 */
9179 static SECStatus 9486 static SECStatus
9180 ssl3_HandleHandshake(sslSocket *ss, sslBuffer *origBuf) 9487 ssl3_HandleHandshake(sslSocket *ss, sslBuffer *origBuf)
9181 { 9488 {
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
9324 ssl3CipherSpec * crSpec; 9631 ssl3CipherSpec * crSpec;
9325 SECStatus rv; 9632 SECStatus rv;
9326 unsigned int hashBytes = MAX_MAC_LENGTH + 1; 9633 unsigned int hashBytes = MAX_MAC_LENGTH + 1;
9327 unsigned int padding_length; 9634 unsigned int padding_length;
9328 PRBool isTLS; 9635 PRBool isTLS;
9329 PRBool padIsBad = PR_FALSE; 9636 PRBool padIsBad = PR_FALSE;
9330 SSL3ContentType rType; 9637 SSL3ContentType rType;
9331 SSL3Opaque hash[MAX_MAC_LENGTH]; 9638 SSL3Opaque hash[MAX_MAC_LENGTH];
9332 sslBuffer *plaintext; 9639 sslBuffer *plaintext;
9333 sslBuffer temp_buf; 9640 sslBuffer temp_buf;
9641 PRUint64 dtls_seq_num;
9334 unsigned int ivLen = 0; 9642 unsigned int ivLen = 0;
9335 9643
9336 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) ); 9644 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
9337 9645
9338 if (!ss->ssl3.initialized) { 9646 if (!ss->ssl3.initialized) {
9339 ssl_GetSSL3HandshakeLock(ss); 9647 ssl_GetSSL3HandshakeLock(ss);
9340 rv = ssl3_InitState(ss); 9648 rv = ssl3_InitState(ss);
9341 ssl_ReleaseSSL3HandshakeLock(ss); 9649 ssl_ReleaseSSL3HandshakeLock(ss);
9342 if (rv != SECSuccess) { 9650 if (rv != SECSuccess) {
9343 return rv; /* ssl3_InitState has set the error code. */ 9651 return rv; /* ssl3_InitState has set the error code. */
(...skipping 15 matching lines...) Expand all
9359 SSL_GETPID(), ss->fd)); 9667 SSL_GETPID(), ss->fd));
9360 rType = content_handshake; 9668 rType = content_handshake;
9361 goto process_it; 9669 goto process_it;
9362 } 9670 }
9363 9671
9364 ssl_GetSpecReadLock(ss); /******************************************/ 9672 ssl_GetSpecReadLock(ss); /******************************************/
9365 9673
9366 crSpec = ss->ssl3.crSpec; 9674 crSpec = ss->ssl3.crSpec;
9367 cipher_def = crSpec->cipher_def; 9675 cipher_def = crSpec->cipher_def;
9368 9676
9677 /*
9678 * DTLS relevance checks:
9679 * Note that this code currently ignores all out-of-epoch packets,
9680 * which means we lose some in the case of rehandshake +
9681 * loss/reordering. Since DTLS is explicitly unreliable, this
9682 * seems like a good tradeoff for implementation effort and is
9683 * consistent with the guidance of RFC 6347 S 4.1 and S 4.2.4.1
9684 */
9685 if (IS_DTLS(ss)) {
9686 DTLSEpoch epoch = (cText->seq_num.high >> 16) & 0xffff;
9687
9688 if (crSpec->epoch != epoch) {
9689 ssl_ReleaseSpecReadLock(ss);
9690 SSL_DBG(("%d: SSL3[%d]: HandleRecord, received packet "
9691 "from irrelevant epoch %d", SSL_GETPID(), ss->fd, epoch));
9692 return SECSuccess;
9693 }
9694
9695 dtls_seq_num = (((PRUint64)(cText->seq_num.high & 0xffff)) << 32) |
9696 ((PRUint64)cText->seq_num.low);
9697
9698 if (dtls_RecordGetRecvd(&crSpec->recvdRecords, dtls_seq_num)) {
Ryan Sleevi 2012/03/22 22:26:37 nit: While totally valid, I wonder if it makes mor
ekr 2012/03/23 12:46:41 That would be fine with me as well. I'm not sure m
9699 ssl_ReleaseSpecReadLock(ss);
9700 SSL_DBG(("%d: SSL3[%d]: HandleRecord, rejecting "
9701 "potentially replayed packet", SSL_GETPID(), ss->fd));
9702 return SECSuccess;
9703 }
9704 }
9705
9369 if (cipher_def->type == type_block && 9706 if (cipher_def->type == type_block &&
9370 crSpec->version >= SSL_LIBRARY_VERSION_TLS_1_1) { 9707 crSpec->version >= SSL_LIBRARY_VERSION_TLS_1_1) {
9371 /* Consume the per-record explicit IV. RFC 4346 Section 6.2.3.2 states 9708 /* Consume the per-record explicit IV. RFC 4346 Section 6.2.3.2 states
9372 * "The receiver decrypts the entire GenericBlockCipher structure and 9709 * "The receiver decrypts the entire GenericBlockCipher structure and
9373 * then discards the first cipher block corresponding to the IV 9710 * then discards the first cipher block corresponding to the IV
9374 * component." Instead, we decrypt the first cipher block and then 9711 * component." Instead, we decrypt the first cipher block and then
9375 * discard it before decrypting the rest. 9712 * discard it before decrypting the rest.
9376 */ 9713 */
9377 SSL3Opaque iv[MAX_IV_LENGTH]; 9714 SSL3Opaque iv[MAX_IV_LENGTH];
9378 int decoded; 9715 int decoded;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
9480 9817
9481 /* Remove the MAC. */ 9818 /* Remove the MAC. */
9482 if (plaintext->len >= crSpec->mac_size) 9819 if (plaintext->len >= crSpec->mac_size)
9483 plaintext->len -= crSpec->mac_size; 9820 plaintext->len -= crSpec->mac_size;
9484 else 9821 else
9485 padIsBad = PR_TRUE; /* really macIsBad */ 9822 padIsBad = PR_TRUE; /* really macIsBad */
9486 9823
9487 /* compute the MAC */ 9824 /* compute the MAC */
9488 rType = cText->type; 9825 rType = cText->type;
9489 rv = ssl3_ComputeRecordMAC( crSpec, (PRBool)(!ss->sec.isServer), 9826 rv = ssl3_ComputeRecordMAC( crSpec, (PRBool)(!ss->sec.isServer),
9490 » rType, cText->version, crSpec->read_seq_num, 9827 IS_DTLS(ss), rType, cText->version,
9828 IS_DTLS(ss) ? cText->seq_num : crSpec->read_seq_num,
9491 plaintext->buf, plaintext->len, hash, &hashBytes); 9829 plaintext->buf, plaintext->len, hash, &hashBytes);
9492 if (rv != SECSuccess) { 9830 if (rv != SECSuccess) {
9493 padIsBad = PR_TRUE; /* really macIsBad */ 9831 padIsBad = PR_TRUE; /* really macIsBad */
9494 } 9832 }
9495 9833
9496 /* Check the MAC */ 9834 /* Check the MAC */
9497 if (hashBytes != (unsigned)crSpec->mac_size || padIsBad || 9835 if (hashBytes != (unsigned)crSpec->mac_size || padIsBad ||
9498 NSS_SecureMemcmp(plaintext->buf + plaintext->len, hash, 9836 NSS_SecureMemcmp(plaintext->buf + plaintext->len, hash,
9499 crSpec->mac_size) != 0) { 9837 crSpec->mac_size) != 0) {
9500 /* must not hold spec lock when calling SSL3_SendAlert. */ 9838 /* must not hold spec lock when calling SSL3_SendAlert. */
9501 ssl_ReleaseSpecReadLock(ss); 9839 ssl_ReleaseSpecReadLock(ss);
9502 SSL3_SendAlert(ss, alert_fatal, bad_record_mac);
9503 /* always log mac error, in case attacker can read server logs. */
9504 PORT_SetError(SSL_ERROR_BAD_MAC_READ);
9505 9840
9506 SSL_DBG(("%d: SSL3[%d]: mac check failed", SSL_GETPID(), ss->fd)); 9841 SSL_DBG(("%d: SSL3[%d]: mac check failed", SSL_GETPID(), ss->fd));
9507 9842
9508 » return SECFailure; 9843 » if (!IS_DTLS(ss)) {
9844 » SSL3_SendAlert(ss, alert_fatal, bad_record_mac);
9845 » /* always log mac error, in case attacker can read server logs. */
9846 » PORT_SetError(SSL_ERROR_BAD_MAC_READ);
9847 » return SECFailure;
9848 » } else {
9849 » /* Silently drop the packet */
9850 plaintext->len = 0; /* Needed to ensure data not left around */
9851 » return SECSuccess;
9852 » }
9509 } 9853 }
9510 9854
9511 9855 if (!IS_DTLS(ss)) {
9512 9856 » ssl3_BumpSequenceNumber(&crSpec->read_seq_num);
9513 ssl3_BumpSequenceNumber(&crSpec->read_seq_num); 9857 } else {
9858 » dtls_RecordSetRecvd(&crSpec->recvdRecords, dtls_seq_num);
9859 }
9514 9860
9515 ssl_ReleaseSpecReadLock(ss); /*****************************************/ 9861 ssl_ReleaseSpecReadLock(ss); /*****************************************/
9516 9862
9517 /* 9863 /*
9518 * The decrypted data is now in plaintext. 9864 * The decrypted data is now in plaintext.
9519 */ 9865 */
9520 9866
9521 /* possibly decompress the record. If we aren't using compression then 9867 /* possibly decompress the record. If we aren't using compression then
9522 * plaintext == databuf and so the uncompressed data is already in 9868 * plaintext == databuf and so the uncompressed data is already in
9523 * databuf. */ 9869 * databuf. */
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
9608 ** they return SECFailure or SECWouldBlock. 9954 ** they return SECFailure or SECWouldBlock.
9609 */ 9955 */
9610 switch (rType) { 9956 switch (rType) {
9611 case content_change_cipher_spec: 9957 case content_change_cipher_spec:
9612 rv = ssl3_HandleChangeCipherSpecs(ss, databuf); 9958 rv = ssl3_HandleChangeCipherSpecs(ss, databuf);
9613 break; 9959 break;
9614 case content_alert: 9960 case content_alert:
9615 rv = ssl3_HandleAlert(ss, databuf); 9961 rv = ssl3_HandleAlert(ss, databuf);
9616 break; 9962 break;
9617 case content_handshake: 9963 case content_handshake:
9618 » rv = ssl3_HandleHandshake(ss, databuf); 9964 » if (!IS_DTLS(ss)) {
9965 » rv = ssl3_HandleHandshake(ss, databuf);
9966 » } else {
9967 » rv = dtls_HandleHandshake(ss, databuf);
9968 » }
9619 break; 9969 break;
9620 /* 9970 /*
9621 case content_application_data is handled before this switch 9971 case content_application_data is handled before this switch
9622 */ 9972 */
9623 default: 9973 default:
9624 SSL_DBG(("%d: SSL3[%d]: bogus content type=%d", 9974 SSL_DBG(("%d: SSL3[%d]: bogus content type=%d",
9625 SSL_GETPID(), ss->fd, cText->type)); 9975 SSL_GETPID(), ss->fd, cText->type));
9626 /* XXX Send an alert ??? */ 9976 /* XXX Send an alert ??? */
9627 PORT_SetError(SSL_ERROR_RX_UNKNOWN_RECORD_TYPE); 9977 PORT_SetError(SSL_ERROR_RX_UNKNOWN_RECORD_TYPE);
9628 rv = SECFailure; 9978 rv = SECFailure;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
9668 spec->server.write_key = NULL; 10018 spec->server.write_key = NULL;
9669 spec->server.write_mac_key = NULL; 10019 spec->server.write_mac_key = NULL;
9670 spec->server.write_mac_context = NULL; 10020 spec->server.write_mac_context = NULL;
9671 10021
9672 spec->write_seq_num.high = 0; 10022 spec->write_seq_num.high = 0;
9673 spec->write_seq_num.low = 0; 10023 spec->write_seq_num.low = 0;
9674 10024
9675 spec->read_seq_num.high = 0; 10025 spec->read_seq_num.high = 0;
9676 spec->read_seq_num.low = 0; 10026 spec->read_seq_num.low = 0;
9677 10027
10028 spec->epoch = 0;
10029 dtls_InitRecvdRecords(&spec->recvdRecords);
10030
9678 spec->version = ss->vrange.max; 10031 spec->version = ss->vrange.max;
9679 } 10032 }
9680 10033
9681 /* Called from: ssl3_SendRecord 10034 /* Called from: ssl3_SendRecord
9682 ** ssl3_StartHandshakeHash() <- ssl2_BeginClientHandshake() 10035 ** ssl3_StartHandshakeHash() <- ssl2_BeginClientHandshake()
9683 ** ssl3_SendClientHello() 10036 ** ssl3_SendClientHello()
9684 ** ssl3_HandleServerHello() 10037 ** ssl3_HandleServerHello()
9685 ** ssl3_HandleClientHello() 10038 ** ssl3_HandleClientHello()
9686 ** ssl3_HandleV2ClientHello() 10039 ** ssl3_HandleV2ClientHello()
9687 ** ssl3_HandleRecord() 10040 ** ssl3_HandleRecord()
(...skipping 21 matching lines...) Expand all
9709 ssl3_InitCipherSpec(ss, ss->ssl3.prSpec); 10062 ssl3_InitCipherSpec(ss, ss->ssl3.prSpec);
9710 10063
9711 ss->ssl3.hs.ws = (ss->sec.isServer) ? wait_client_hello : wait_server_hello; 10064 ss->ssl3.hs.ws = (ss->sec.isServer) ? wait_client_hello : wait_server_hello;
9712 #ifdef NSS_ENABLE_ECC 10065 #ifdef NSS_ENABLE_ECC
9713 ss->ssl3.hs.negotiatedECCurves = SSL3_SUPPORTED_CURVES_MASK; 10066 ss->ssl3.hs.negotiatedECCurves = SSL3_SUPPORTED_CURVES_MASK;
9714 #endif 10067 #endif
9715 ssl_ReleaseSpecWriteLock(ss); 10068 ssl_ReleaseSpecWriteLock(ss);
9716 10069
9717 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData)); 10070 PORT_Memset(&ss->xtnData, 0, sizeof(TLSExtensionData));
9718 10071
10072 if (IS_DTLS(ss)) {
10073 ss->ssl3.hs.sendMessageSeq = 0;
10074 ss->ssl3.hs.recvMessageSeq = 0;
10075 ss->ssl3.hs.rtTimeoutMs = INITIAL_DTLS_TIMEOUT_MS;
10076 ss->ssl3.hs.rtRetries = 0;
10077
10078 /* Have to allocate this because ssl_FreeSocket relocates
10079 * this structure in DEBUG mode */
10080 if (!(ss->ssl3.hs.lastMessageFlight = PORT_New(PRCList)))
10081 return SECFailure;
10082 ss->ssl3.hs.recvdHighWater = -1;
10083 PR_INIT_CLIST(ss->ssl3.hs.lastMessageFlight);
10084 dtls_SetMTU(ss, 0); /* Set the MTU to the highest plateau */
10085 }
10086
9719 rv = ssl3_NewHandshakeHashes(ss); 10087 rv = ssl3_NewHandshakeHashes(ss);
9720 if (rv == SECSuccess) { 10088 if (rv == SECSuccess) {
9721 ss->ssl3.initialized = PR_TRUE; 10089 ss->ssl3.initialized = PR_TRUE;
9722 } 10090 }
9723 10091
9724 return rv; 10092 return rv;
9725 } 10093 }
9726 10094
9727 /* Returns a reference counted object that contains a key pair. 10095 /* Returns a reference counted object that contains a key pair.
9728 * Or NULL on failure. Initial ref count is 1. 10096 * Or NULL on failure. Initial ref count is 1.
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
9961 10329
9962 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) ); 10330 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
9963 10331
9964 if (!ss->firstHsDone || 10332 if (!ss->firstHsDone ||
9965 ((ss->version >= SSL_LIBRARY_VERSION_3_0) && 10333 ((ss->version >= SSL_LIBRARY_VERSION_3_0) &&
9966 ss->ssl3.initialized && 10334 ss->ssl3.initialized &&
9967 (ss->ssl3.hs.ws != idle_handshake))) { 10335 (ss->ssl3.hs.ws != idle_handshake))) {
9968 PORT_SetError(SSL_ERROR_HANDSHAKE_NOT_COMPLETED); 10336 PORT_SetError(SSL_ERROR_HANDSHAKE_NOT_COMPLETED);
9969 return SECFailure; 10337 return SECFailure;
9970 } 10338 }
10339
10340 if (IS_DTLS(ss)) {
10341 dtls_RehandshakeCleanup(ss);
10342 }
10343
9971 if (ss->opt.enableRenegotiation == SSL_RENEGOTIATE_NEVER) { 10344 if (ss->opt.enableRenegotiation == SSL_RENEGOTIATE_NEVER) {
9972 PORT_SetError(SSL_ERROR_RENEGOTIATION_NOT_ALLOWED); 10345 PORT_SetError(SSL_ERROR_RENEGOTIATION_NOT_ALLOWED);
9973 return SECFailure; 10346 return SECFailure;
9974 } 10347 }
9975 if (sid && flushCache) { 10348 if (sid && flushCache) {
9976 ss->sec.uncache(sid); /* remove it from whichever cache it's in. */ 10349 ss->sec.uncache(sid); /* remove it from whichever cache it's in. */
9977 ssl_FreeSID(sid); /* dec ref count and free if zero. */ 10350 ssl_FreeSID(sid); /* dec ref count and free if zero. */
9978 ss->sec.ci.sid = NULL; 10351 ss->sec.ci.sid = NULL;
9979 } 10352 }
9980 10353
9981 ssl_GetXmitBufLock(ss); /**************************************/ 10354 ssl_GetXmitBufLock(ss); /**************************************/
9982 10355
9983 /* start off a new handshake. */ 10356 /* start off a new handshake. */
9984 rv = (ss->sec.isServer) ? ssl3_SendHelloRequest(ss) 10357 rv = (ss->sec.isServer) ? ssl3_SendHelloRequest(ss)
9985 : ssl3_SendClientHello(ss); 10358 : ssl3_SendClientHello(ss, PR_FALSE);
9986 10359
9987 ssl_ReleaseXmitBufLock(ss); /**************************************/ 10360 ssl_ReleaseXmitBufLock(ss); /**************************************/
9988 return rv; 10361 return rv;
9989 } 10362 }
9990 10363
9991 /* Called from ssl_DestroySocketContents() in sslsock.c */ 10364 /* Called from ssl_DestroySocketContents() in sslsock.c */
9992 void 10365 void
9993 ssl3_DestroySSL3Info(sslSocket *ss) 10366 ssl3_DestroySSL3Info(sslSocket *ss)
9994 { 10367 {
9995 10368
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
10035 SECITEM_FreeItem(&ss->ssl3.hs.cert_status, PR_FALSE); 10408 SECITEM_FreeItem(&ss->ssl3.hs.cert_status, PR_FALSE);
10036 } 10409 }
10037 10410
10038 /* free the SSL3Buffer (msg_body) */ 10411 /* free the SSL3Buffer (msg_body) */
10039 PORT_Free(ss->ssl3.hs.msg_body.buf); 10412 PORT_Free(ss->ssl3.hs.msg_body.buf);
10040 10413
10041 /* free up the CipherSpecs */ 10414 /* free up the CipherSpecs */
10042 ssl3_DestroyCipherSpec(&ss->ssl3.specs[0], PR_TRUE/*freeSrvName*/); 10415 ssl3_DestroyCipherSpec(&ss->ssl3.specs[0], PR_TRUE/*freeSrvName*/);
10043 ssl3_DestroyCipherSpec(&ss->ssl3.specs[1], PR_TRUE/*freeSrvName*/); 10416 ssl3_DestroyCipherSpec(&ss->ssl3.specs[1], PR_TRUE/*freeSrvName*/);
10044 10417
10418 /* Destroy the DTLS data */
10419 if (IS_DTLS(ss)) {
10420 if (ss->ssl3.hs.lastMessageFlight) {
10421 dtls_FreeHandshakeMessages(ss->ssl3.hs.lastMessageFlight);
10422 PORT_Free(ss->ssl3.hs.lastMessageFlight);
10423 }
10424 if (ss->ssl3.hs.recvdFragments.buf) {
10425 PORT_Free(ss->ssl3.hs.recvdFragments.buf);
10426 }
10427 }
10428
10045 ss->ssl3.initialized = PR_FALSE; 10429 ss->ssl3.initialized = PR_FALSE;
10046 10430
10047 SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE); 10431 SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE);
10048 } 10432 }
10049 10433
10050 /* End of ssl3con.c */ 10434 /* End of ssl3con.c */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698