/src/nss/lib/ssl/ssl3con.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * SSL3 Protocol |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
8 | | |
9 | | /* TODO(ekr): Implement HelloVerifyRequest on server side. OK for now. */ |
10 | | |
11 | | #include "cert.h" |
12 | | #include "ssl.h" |
13 | | #include "cryptohi.h" /* for DSAU_ stuff */ |
14 | | #include "keyhi.h" |
15 | | #include "secder.h" |
16 | | #include "secitem.h" |
17 | | #include "sechash.h" |
18 | | |
19 | | #include "sslimpl.h" |
20 | | #include "sslproto.h" |
21 | | #include "sslerr.h" |
22 | | #include "ssl3ext.h" |
23 | | #include "ssl3exthandle.h" |
24 | | #include "tls13ech.h" |
25 | | #include "tls13exthandle.h" |
26 | | #include "tls13psk.h" |
27 | | #include "tls13subcerts.h" |
28 | | #include "prtime.h" |
29 | | #include "prinrval.h" |
30 | | #include "prerror.h" |
31 | | #include "pratom.h" |
32 | | #include "prthread.h" |
33 | | #include "nss.h" |
34 | | #include "nssoptions.h" |
35 | | |
36 | | #include "pk11func.h" |
37 | | #include "secmod.h" |
38 | | #include "blapi.h" |
39 | | |
40 | | #include <limits.h> |
41 | | #include <stdio.h> |
42 | | |
43 | | static PK11SymKey *ssl3_GenerateRSAPMS(sslSocket *ss, ssl3CipherSpec *spec, |
44 | | PK11SlotInfo *serverKeySlot); |
45 | | static SECStatus ssl3_ComputeMasterSecret(sslSocket *ss, PK11SymKey *pms, |
46 | | PK11SymKey **msp); |
47 | | static SECStatus ssl3_DeriveConnectionKeys(sslSocket *ss, |
48 | | PK11SymKey *masterSecret); |
49 | | static SECStatus ssl3_HandshakeFailure(sslSocket *ss); |
50 | | static SECStatus ssl3_SendCertificate(sslSocket *ss); |
51 | | static SECStatus ssl3_SendCertificateRequest(sslSocket *ss); |
52 | | static SECStatus ssl3_SendNextProto(sslSocket *ss); |
53 | | static SECStatus ssl3_SendFinished(sslSocket *ss, PRInt32 flags); |
54 | | static SECStatus ssl3_SendServerHelloDone(sslSocket *ss); |
55 | | static SECStatus ssl3_SendServerKeyExchange(sslSocket *ss); |
56 | | static SECStatus ssl3_HandleClientHelloPart2(sslSocket *ss, |
57 | | SECItem *suites, |
58 | | sslSessionID *sid, |
59 | | const PRUint8 *msg, |
60 | | unsigned int len); |
61 | | static SECStatus ssl3_HandleServerHelloPart2(sslSocket *ss, |
62 | | const SECItem *sidBytes, |
63 | | int *retErrCode); |
64 | | static SECStatus ssl3_HandlePostHelloHandshakeMessage(sslSocket *ss, |
65 | | PRUint8 *b, |
66 | | PRUint32 length); |
67 | | static SECStatus ssl3_FlushHandshakeMessages(sslSocket *ss, PRInt32 flags); |
68 | | static CK_MECHANISM_TYPE ssl3_GetHashMechanismByHashType(SSLHashType hashType); |
69 | | static CK_MECHANISM_TYPE ssl3_GetMgfMechanismByHashType(SSLHashType hash); |
70 | | PRBool ssl_IsRsaPssSignatureScheme(SSLSignatureScheme scheme); |
71 | | PRBool ssl_IsRsaeSignatureScheme(SSLSignatureScheme scheme); |
72 | | PRBool ssl_IsRsaPkcs1SignatureScheme(SSLSignatureScheme scheme); |
73 | | PRBool ssl_IsDsaSignatureScheme(SSLSignatureScheme scheme); |
74 | | static SECStatus ssl3_UpdateDefaultHandshakeHashes(sslSocket *ss, |
75 | | const unsigned char *b, |
76 | | unsigned int l); |
77 | | const PRUint32 kSSLSigSchemePolicy = |
78 | | NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_ANY_SIGNATURE; |
79 | | |
80 | | const PRUint8 ssl_hello_retry_random[] = { |
81 | | 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, |
82 | | 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, |
83 | | 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, |
84 | | 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C |
85 | | }; |
86 | | PR_STATIC_ASSERT(PR_ARRAY_SIZE(ssl_hello_retry_random) == SSL3_RANDOM_LENGTH); |
87 | | |
88 | | /* This list of SSL3 cipher suites is sorted in descending order of |
89 | | * precedence (desirability). It only includes cipher suites we implement. |
90 | | * This table is modified by SSL3_SetPolicy(). The ordering of cipher suites |
91 | | * in this table must match the ordering in SSL_ImplementedCiphers (sslenum.c) |
92 | | * |
93 | | * Important: See bug 946147 before enabling, reordering, or adding any cipher |
94 | | * suites to this list. |
95 | | */ |
96 | | /* clang-format off */ |
97 | | static ssl3CipherSuiteCfg cipherSuites[ssl_V3_SUITES_IMPLEMENTED] = { |
98 | | /* cipher_suite policy enabled isPresent */ |
99 | | /* Special TLS 1.3 suites. */ |
100 | | { TLS_AES_128_GCM_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE }, |
101 | | { TLS_CHACHA20_POLY1305_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE }, |
102 | | { TLS_AES_256_GCM_SHA384, SSL_ALLOWED, PR_TRUE, PR_FALSE }, |
103 | | |
104 | | { TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
105 | | { TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
106 | | { TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
107 | | { TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
108 | | { TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
109 | | { TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
110 | | /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA is out of order to work around |
111 | | * bug 946147. |
112 | | */ |
113 | | { TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
114 | | { TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
115 | | { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
116 | | { TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
117 | | { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
118 | | { TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
119 | | { TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
120 | | { TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
121 | | { TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
122 | | { TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
123 | | { TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
124 | | { TLS_ECDHE_RSA_WITH_RC4_128_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
125 | | |
126 | | { TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
127 | | { TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,SSL_ALLOWED,PR_TRUE, PR_FALSE}, |
128 | | { TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
129 | | { TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
130 | | { TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
131 | | { TLS_DHE_RSA_WITH_AES_128_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
132 | | { TLS_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
133 | | { TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
134 | | { TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
135 | | { TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
136 | | { TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
137 | | { TLS_DHE_RSA_WITH_AES_256_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
138 | | { TLS_DHE_DSS_WITH_AES_256_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
139 | | { TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
140 | | { TLS_DHE_DSS_WITH_AES_256_CBC_SHA256, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
141 | | { TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
142 | | { TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
143 | | { TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
144 | | { TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
145 | | { TLS_DHE_DSS_WITH_RC4_128_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
146 | | |
147 | | { TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
148 | | { TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
149 | | { TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
150 | | { TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
151 | | { TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
152 | | { TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
153 | | { TLS_ECDH_ECDSA_WITH_RC4_128_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
154 | | { TLS_ECDH_RSA_WITH_RC4_128_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
155 | | |
156 | | /* RSA */ |
157 | | { TLS_RSA_WITH_AES_128_GCM_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
158 | | { TLS_RSA_WITH_AES_256_GCM_SHA384, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
159 | | { TLS_RSA_WITH_AES_128_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
160 | | { TLS_RSA_WITH_AES_128_CBC_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
161 | | { TLS_RSA_WITH_CAMELLIA_128_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
162 | | { TLS_RSA_WITH_AES_256_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
163 | | { TLS_RSA_WITH_AES_256_CBC_SHA256, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
164 | | { TLS_RSA_WITH_CAMELLIA_256_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
165 | | { TLS_RSA_WITH_SEED_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
166 | | { TLS_RSA_WITH_3DES_EDE_CBC_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
167 | | { TLS_RSA_WITH_RC4_128_SHA, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
168 | | { TLS_RSA_WITH_RC4_128_MD5, SSL_ALLOWED, PR_TRUE, PR_FALSE}, |
169 | | |
170 | | /* 56-bit DES "domestic" cipher suites */ |
171 | | { TLS_DHE_RSA_WITH_DES_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
172 | | { TLS_DHE_DSS_WITH_DES_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
173 | | { TLS_RSA_WITH_DES_CBC_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
174 | | |
175 | | /* ciphersuites with no encryption */ |
176 | | { TLS_ECDHE_ECDSA_WITH_NULL_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
177 | | { TLS_ECDHE_RSA_WITH_NULL_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
178 | | { TLS_ECDH_RSA_WITH_NULL_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
179 | | { TLS_ECDH_ECDSA_WITH_NULL_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
180 | | { TLS_RSA_WITH_NULL_SHA, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
181 | | { TLS_RSA_WITH_NULL_SHA256, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
182 | | { TLS_RSA_WITH_NULL_MD5, SSL_ALLOWED, PR_FALSE, PR_FALSE}, |
183 | | }; |
184 | | /* clang-format on */ |
185 | | |
186 | | /* This is the default supported set of signature schemes. The order of the |
187 | | * hashes here is all that is important, since that will (sometimes) determine |
188 | | * which hash we use. The key pair (i.e., cert) is the primary thing that |
189 | | * determines what we use and this doesn't affect how we select key pairs. The |
190 | | * order of signature types is based on the same rules for ordering we use for |
191 | | * cipher suites just for consistency. |
192 | | */ |
193 | | static const SSLSignatureScheme defaultSignatureSchemes[] = { |
194 | | ssl_sig_ecdsa_secp256r1_sha256, |
195 | | ssl_sig_ecdsa_secp384r1_sha384, |
196 | | ssl_sig_ecdsa_secp521r1_sha512, |
197 | | ssl_sig_ecdsa_sha1, |
198 | | ssl_sig_rsa_pss_rsae_sha256, |
199 | | ssl_sig_rsa_pss_rsae_sha384, |
200 | | ssl_sig_rsa_pss_rsae_sha512, |
201 | | ssl_sig_rsa_pkcs1_sha256, |
202 | | ssl_sig_rsa_pkcs1_sha384, |
203 | | ssl_sig_rsa_pkcs1_sha512, |
204 | | ssl_sig_rsa_pkcs1_sha1, |
205 | | ssl_sig_dsa_sha256, |
206 | | ssl_sig_dsa_sha384, |
207 | | ssl_sig_dsa_sha512, |
208 | | ssl_sig_dsa_sha1 |
209 | | }; |
210 | | PR_STATIC_ASSERT(PR_ARRAY_SIZE(defaultSignatureSchemes) <= |
211 | | MAX_SIGNATURE_SCHEMES); |
212 | | |
213 | | /* Verify that SSL_ImplementedCiphers and cipherSuites are in consistent order. |
214 | | */ |
215 | | #ifdef DEBUG |
216 | | void |
217 | | ssl3_CheckCipherSuiteOrderConsistency() |
218 | 1 | { |
219 | 1 | unsigned int i; |
220 | | |
221 | 1 | PORT_Assert(SSL_NumImplementedCiphers == PR_ARRAY_SIZE(cipherSuites)); |
222 | | |
223 | 72 | for (i = 0; i < PR_ARRAY_SIZE(cipherSuites); ++i) { |
224 | 71 | PORT_Assert(SSL_ImplementedCiphers[i] == cipherSuites[i].cipher_suite); |
225 | 71 | } |
226 | 1 | } |
227 | | #endif |
228 | | |
229 | | static const /*SSL3ClientCertificateType */ PRUint8 certificate_types[] = { |
230 | | ct_RSA_sign, |
231 | | ct_ECDSA_sign, |
232 | | ct_DSS_sign, |
233 | | }; |
234 | | |
235 | | static SSL3Statistics ssl3stats; |
236 | | |
237 | | static const ssl3KEADef kea_defs[] = { |
238 | | /* indexed by SSL3KeyExchangeAlgorithm */ |
239 | | /* kea exchKeyType signKeyType authKeyType ephemeral oid */ |
240 | | { kea_null, ssl_kea_null, nullKey, ssl_auth_null, PR_FALSE, 0 }, |
241 | | { kea_rsa, ssl_kea_rsa, nullKey, ssl_auth_rsa_decrypt, PR_FALSE, SEC_OID_TLS_RSA }, |
242 | | { kea_dh_dss, ssl_kea_dh, dsaKey, ssl_auth_dsa, PR_FALSE, SEC_OID_TLS_DH_DSS }, |
243 | | { kea_dh_rsa, ssl_kea_dh, rsaKey, ssl_auth_rsa_sign, PR_FALSE, SEC_OID_TLS_DH_RSA }, |
244 | | { kea_dhe_dss, ssl_kea_dh, dsaKey, ssl_auth_dsa, PR_TRUE, SEC_OID_TLS_DHE_DSS }, |
245 | | { kea_dhe_rsa, ssl_kea_dh, rsaKey, ssl_auth_rsa_sign, PR_TRUE, SEC_OID_TLS_DHE_RSA }, |
246 | | { kea_dh_anon, ssl_kea_dh, nullKey, ssl_auth_null, PR_TRUE, SEC_OID_TLS_DH_ANON }, |
247 | | { kea_ecdh_ecdsa, ssl_kea_ecdh, nullKey, ssl_auth_ecdh_ecdsa, PR_FALSE, SEC_OID_TLS_ECDH_ECDSA }, |
248 | | { kea_ecdhe_ecdsa, ssl_kea_ecdh, ecKey, ssl_auth_ecdsa, PR_TRUE, SEC_OID_TLS_ECDHE_ECDSA }, |
249 | | { kea_ecdh_rsa, ssl_kea_ecdh, nullKey, ssl_auth_ecdh_rsa, PR_FALSE, SEC_OID_TLS_ECDH_RSA }, |
250 | | { kea_ecdhe_rsa, ssl_kea_ecdh, rsaKey, ssl_auth_rsa_sign, PR_TRUE, SEC_OID_TLS_ECDHE_RSA }, |
251 | | { kea_ecdh_anon, ssl_kea_ecdh, nullKey, ssl_auth_null, PR_TRUE, SEC_OID_TLS_ECDH_ANON }, |
252 | | { kea_ecdhe_psk, ssl_kea_ecdh_psk, nullKey, ssl_auth_psk, PR_TRUE, SEC_OID_TLS_ECDHE_PSK }, |
253 | | { kea_dhe_psk, ssl_kea_dh_psk, nullKey, ssl_auth_psk, PR_TRUE, SEC_OID_TLS_DHE_PSK }, |
254 | | { kea_tls13_any, ssl_kea_tls13_any, nullKey, ssl_auth_tls13_any, PR_TRUE, SEC_OID_TLS13_KEA_ANY }, |
255 | | }; |
256 | | |
257 | | /* must use ssl_LookupCipherSuiteDef to access */ |
258 | | static const ssl3CipherSuiteDef cipher_suite_defs[] = { |
259 | | /* cipher_suite bulk_cipher_alg mac_alg key_exchange_alg prf_hash */ |
260 | | /* Note that the prf_hash_alg is the hash function used by the PRF, see sslimpl.h. */ |
261 | | |
262 | | { TLS_NULL_WITH_NULL_NULL, cipher_null, ssl_mac_null, kea_null, ssl_hash_none }, |
263 | | { TLS_RSA_WITH_NULL_MD5, cipher_null, ssl_mac_md5, kea_rsa, ssl_hash_none }, |
264 | | { TLS_RSA_WITH_NULL_SHA, cipher_null, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
265 | | { TLS_RSA_WITH_NULL_SHA256, cipher_null, ssl_hmac_sha256, kea_rsa, ssl_hash_sha256 }, |
266 | | { TLS_RSA_WITH_RC4_128_MD5, cipher_rc4, ssl_mac_md5, kea_rsa, ssl_hash_none }, |
267 | | { TLS_RSA_WITH_RC4_128_SHA, cipher_rc4, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
268 | | { TLS_RSA_WITH_DES_CBC_SHA, cipher_des, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
269 | | { TLS_RSA_WITH_3DES_EDE_CBC_SHA, cipher_3des, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
270 | | { TLS_DHE_DSS_WITH_DES_CBC_SHA, cipher_des, ssl_mac_sha, kea_dhe_dss, ssl_hash_none }, |
271 | | { TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA, |
272 | | cipher_3des, ssl_mac_sha, kea_dhe_dss, ssl_hash_none }, |
273 | | { TLS_DHE_DSS_WITH_RC4_128_SHA, cipher_rc4, ssl_mac_sha, kea_dhe_dss, ssl_hash_none }, |
274 | | { TLS_DHE_RSA_WITH_DES_CBC_SHA, cipher_des, ssl_mac_sha, kea_dhe_rsa, ssl_hash_none }, |
275 | | { TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, |
276 | | cipher_3des, ssl_mac_sha, kea_dhe_rsa, ssl_hash_none }, |
277 | | |
278 | | /* New TLS cipher suites */ |
279 | | { TLS_RSA_WITH_AES_128_CBC_SHA, cipher_aes_128, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
280 | | { TLS_RSA_WITH_AES_128_CBC_SHA256, cipher_aes_128, ssl_hmac_sha256, kea_rsa, ssl_hash_sha256 }, |
281 | | { TLS_DHE_DSS_WITH_AES_128_CBC_SHA, cipher_aes_128, ssl_mac_sha, kea_dhe_dss, ssl_hash_none }, |
282 | | { TLS_DHE_RSA_WITH_AES_128_CBC_SHA, cipher_aes_128, ssl_mac_sha, kea_dhe_rsa, ssl_hash_none }, |
283 | | { TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, cipher_aes_128, ssl_hmac_sha256, kea_dhe_rsa, ssl_hash_sha256 }, |
284 | | { TLS_RSA_WITH_AES_256_CBC_SHA, cipher_aes_256, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
285 | | { TLS_RSA_WITH_AES_256_CBC_SHA256, cipher_aes_256, ssl_hmac_sha256, kea_rsa, ssl_hash_sha256 }, |
286 | | { TLS_DHE_DSS_WITH_AES_256_CBC_SHA, cipher_aes_256, ssl_mac_sha, kea_dhe_dss, ssl_hash_none }, |
287 | | { TLS_DHE_RSA_WITH_AES_256_CBC_SHA, cipher_aes_256, ssl_mac_sha, kea_dhe_rsa, ssl_hash_none }, |
288 | | { TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, cipher_aes_256, ssl_hmac_sha256, kea_dhe_rsa, ssl_hash_sha256 }, |
289 | | { TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, cipher_aes_256_gcm, ssl_mac_aead, kea_dhe_rsa, ssl_hash_sha384 }, |
290 | | |
291 | | { TLS_RSA_WITH_SEED_CBC_SHA, cipher_seed, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
292 | | |
293 | | { TLS_RSA_WITH_CAMELLIA_128_CBC_SHA, cipher_camellia_128, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
294 | | { TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA, |
295 | | cipher_camellia_128, ssl_mac_sha, kea_dhe_dss, ssl_hash_none }, |
296 | | { TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA, |
297 | | cipher_camellia_128, ssl_mac_sha, kea_dhe_rsa, ssl_hash_none }, |
298 | | { TLS_RSA_WITH_CAMELLIA_256_CBC_SHA, cipher_camellia_256, ssl_mac_sha, kea_rsa, ssl_hash_none }, |
299 | | { TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA, |
300 | | cipher_camellia_256, ssl_mac_sha, kea_dhe_dss, ssl_hash_none }, |
301 | | { TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA, |
302 | | cipher_camellia_256, ssl_mac_sha, kea_dhe_rsa, ssl_hash_none }, |
303 | | |
304 | | { TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, cipher_aes_128_gcm, ssl_mac_aead, kea_dhe_rsa, ssl_hash_sha256 }, |
305 | | { TLS_RSA_WITH_AES_128_GCM_SHA256, cipher_aes_128_gcm, ssl_mac_aead, kea_rsa, ssl_hash_sha256 }, |
306 | | |
307 | | { TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, cipher_aes_128_gcm, ssl_mac_aead, kea_ecdhe_rsa, ssl_hash_sha256 }, |
308 | | { TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, cipher_aes_128_gcm, ssl_mac_aead, kea_ecdhe_ecdsa, ssl_hash_sha256 }, |
309 | | { TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, cipher_aes_256_gcm, ssl_mac_aead, kea_ecdhe_ecdsa, ssl_hash_sha384 }, |
310 | | { TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, cipher_aes_256_gcm, ssl_mac_aead, kea_ecdhe_rsa, ssl_hash_sha384 }, |
311 | | { TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, cipher_aes_256, ssl_hmac_sha384, kea_ecdhe_ecdsa, ssl_hash_sha384 }, |
312 | | { TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, cipher_aes_256, ssl_hmac_sha384, kea_ecdhe_rsa, ssl_hash_sha384 }, |
313 | | { TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, cipher_aes_128_gcm, ssl_mac_aead, kea_dhe_dss, ssl_hash_sha256 }, |
314 | | { TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, cipher_aes_128, ssl_hmac_sha256, kea_dhe_dss, ssl_hash_sha256 }, |
315 | | { TLS_DHE_DSS_WITH_AES_256_CBC_SHA256, cipher_aes_256, ssl_hmac_sha256, kea_dhe_dss, ssl_hash_sha256 }, |
316 | | { TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, cipher_aes_256_gcm, ssl_mac_aead, kea_dhe_dss, ssl_hash_sha384 }, |
317 | | { TLS_RSA_WITH_AES_256_GCM_SHA384, cipher_aes_256_gcm, ssl_mac_aead, kea_rsa, ssl_hash_sha384 }, |
318 | | |
319 | | { TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256, cipher_chacha20, ssl_mac_aead, kea_dhe_rsa, ssl_hash_sha256 }, |
320 | | |
321 | | { TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, cipher_chacha20, ssl_mac_aead, kea_ecdhe_rsa, ssl_hash_sha256 }, |
322 | | { TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, cipher_chacha20, ssl_mac_aead, kea_ecdhe_ecdsa, ssl_hash_sha256 }, |
323 | | |
324 | | { TLS_ECDH_ECDSA_WITH_NULL_SHA, cipher_null, ssl_mac_sha, kea_ecdh_ecdsa, ssl_hash_none }, |
325 | | { TLS_ECDH_ECDSA_WITH_RC4_128_SHA, cipher_rc4, ssl_mac_sha, kea_ecdh_ecdsa, ssl_hash_none }, |
326 | | { TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, cipher_3des, ssl_mac_sha, kea_ecdh_ecdsa, ssl_hash_none }, |
327 | | { TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, cipher_aes_128, ssl_mac_sha, kea_ecdh_ecdsa, ssl_hash_none }, |
328 | | { TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, cipher_aes_256, ssl_mac_sha, kea_ecdh_ecdsa, ssl_hash_none }, |
329 | | |
330 | | { TLS_ECDHE_ECDSA_WITH_NULL_SHA, cipher_null, ssl_mac_sha, kea_ecdhe_ecdsa, ssl_hash_none }, |
331 | | { TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, cipher_rc4, ssl_mac_sha, kea_ecdhe_ecdsa, ssl_hash_none }, |
332 | | { TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, cipher_3des, ssl_mac_sha, kea_ecdhe_ecdsa, ssl_hash_none }, |
333 | | { TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, cipher_aes_128, ssl_mac_sha, kea_ecdhe_ecdsa, ssl_hash_none }, |
334 | | { TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, cipher_aes_128, ssl_hmac_sha256, kea_ecdhe_ecdsa, ssl_hash_sha256 }, |
335 | | { TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, cipher_aes_256, ssl_mac_sha, kea_ecdhe_ecdsa, ssl_hash_none }, |
336 | | |
337 | | { TLS_ECDH_RSA_WITH_NULL_SHA, cipher_null, ssl_mac_sha, kea_ecdh_rsa, ssl_hash_none }, |
338 | | { TLS_ECDH_RSA_WITH_RC4_128_SHA, cipher_rc4, ssl_mac_sha, kea_ecdh_rsa, ssl_hash_none }, |
339 | | { TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, cipher_3des, ssl_mac_sha, kea_ecdh_rsa, ssl_hash_none }, |
340 | | { TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, cipher_aes_128, ssl_mac_sha, kea_ecdh_rsa, ssl_hash_none }, |
341 | | { TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, cipher_aes_256, ssl_mac_sha, kea_ecdh_rsa, ssl_hash_none }, |
342 | | |
343 | | { TLS_ECDHE_RSA_WITH_NULL_SHA, cipher_null, ssl_mac_sha, kea_ecdhe_rsa, ssl_hash_none }, |
344 | | { TLS_ECDHE_RSA_WITH_RC4_128_SHA, cipher_rc4, ssl_mac_sha, kea_ecdhe_rsa, ssl_hash_none }, |
345 | | { TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, cipher_3des, ssl_mac_sha, kea_ecdhe_rsa, ssl_hash_none }, |
346 | | { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, cipher_aes_128, ssl_mac_sha, kea_ecdhe_rsa, ssl_hash_none }, |
347 | | { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, cipher_aes_128, ssl_hmac_sha256, kea_ecdhe_rsa, ssl_hash_sha256 }, |
348 | | { TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, cipher_aes_256, ssl_mac_sha, kea_ecdhe_rsa, ssl_hash_none }, |
349 | | |
350 | | { TLS_AES_128_GCM_SHA256, cipher_aes_128_gcm, ssl_mac_aead, kea_tls13_any, ssl_hash_sha256 }, |
351 | | { TLS_CHACHA20_POLY1305_SHA256, cipher_chacha20, ssl_mac_aead, kea_tls13_any, ssl_hash_sha256 }, |
352 | | { TLS_AES_256_GCM_SHA384, cipher_aes_256_gcm, ssl_mac_aead, kea_tls13_any, ssl_hash_sha384 }, |
353 | | }; |
354 | | |
355 | | static const CK_MECHANISM_TYPE auth_alg_defs[] = { |
356 | | CKM_INVALID_MECHANISM, /* ssl_auth_null */ |
357 | | CKM_RSA_PKCS, /* ssl_auth_rsa_decrypt */ |
358 | | CKM_DSA, /* ? _SHA1 */ /* ssl_auth_dsa */ |
359 | | CKM_INVALID_MECHANISM, /* ssl_auth_kea (unused) */ |
360 | | CKM_ECDSA, /* ssl_auth_ecdsa */ |
361 | | CKM_ECDH1_DERIVE, /* ssl_auth_ecdh_rsa */ |
362 | | CKM_ECDH1_DERIVE, /* ssl_auth_ecdh_ecdsa */ |
363 | | CKM_RSA_PKCS, /* ssl_auth_rsa_sign */ |
364 | | CKM_RSA_PKCS_PSS, /* ssl_auth_rsa_pss */ |
365 | | CKM_NSS_HKDF_SHA256, /* ssl_auth_psk (just check for HKDF) */ |
366 | | CKM_INVALID_MECHANISM /* ssl_auth_tls13_any */ |
367 | | }; |
368 | | PR_STATIC_ASSERT(PR_ARRAY_SIZE(auth_alg_defs) == ssl_auth_size); |
369 | | |
370 | | static const CK_MECHANISM_TYPE kea_alg_defs[] = { |
371 | | CKM_INVALID_MECHANISM, /* ssl_kea_null */ |
372 | | CKM_RSA_PKCS, /* ssl_kea_rsa */ |
373 | | CKM_DH_PKCS_DERIVE, /* ssl_kea_dh */ |
374 | | CKM_INVALID_MECHANISM, /* ssl_kea_fortezza (unused) */ |
375 | | CKM_ECDH1_DERIVE, /* ssl_kea_ecdh */ |
376 | | CKM_ECDH1_DERIVE, /* ssl_kea_ecdh_psk */ |
377 | | CKM_DH_PKCS_DERIVE, /* ssl_kea_dh_psk */ |
378 | | CKM_INVALID_MECHANISM, /* ssl_kea_tls13_any */ |
379 | | CKM_INVALID_MECHANISM, /* ssl_kea_ecdh_hybrid */ |
380 | | CKM_INVALID_MECHANISM, /* ssl_kea_ecdh_hybrid_psk */ |
381 | | }; |
382 | | PR_STATIC_ASSERT(PR_ARRAY_SIZE(kea_alg_defs) == ssl_kea_size); |
383 | | |
384 | | typedef struct SSLCipher2MechStr { |
385 | | SSLCipherAlgorithm calg; |
386 | | CK_MECHANISM_TYPE cmech; |
387 | | } SSLCipher2Mech; |
388 | | |
389 | | /* indexed by type SSLCipherAlgorithm */ |
390 | | static const SSLCipher2Mech alg2Mech[] = { |
391 | | /* calg, cmech */ |
392 | | { ssl_calg_null, CKM_INVALID_MECHANISM }, |
393 | | { ssl_calg_rc4, CKM_RC4 }, |
394 | | { ssl_calg_rc2, CKM_RC2_CBC }, |
395 | | { ssl_calg_des, CKM_DES_CBC }, |
396 | | { ssl_calg_3des, CKM_DES3_CBC }, |
397 | | { ssl_calg_idea, CKM_IDEA_CBC }, |
398 | | { ssl_calg_fortezza, CKM_SKIPJACK_CBC64 }, |
399 | | { ssl_calg_aes, CKM_AES_CBC }, |
400 | | { ssl_calg_camellia, CKM_CAMELLIA_CBC }, |
401 | | { ssl_calg_seed, CKM_SEED_CBC }, |
402 | | { ssl_calg_aes_gcm, CKM_AES_GCM }, |
403 | | { ssl_calg_chacha20, CKM_CHACHA20_POLY1305 }, |
404 | | }; |
405 | | |
406 | | const PRUint8 tls12_downgrade_random[] = { 0x44, 0x4F, 0x57, 0x4E, |
407 | | 0x47, 0x52, 0x44, 0x01 }; |
408 | | const PRUint8 tls1_downgrade_random[] = { 0x44, 0x4F, 0x57, 0x4E, |
409 | | 0x47, 0x52, 0x44, 0x00 }; |
410 | | PR_STATIC_ASSERT(sizeof(tls12_downgrade_random) == |
411 | | sizeof(tls1_downgrade_random)); |
412 | | |
413 | | /* The ECCWrappedKeyInfo structure defines how various pieces of |
414 | | * information are laid out within wrappedSymmetricWrappingkey |
415 | | * for ECDH key exchange. Since wrappedSymmetricWrappingkey is |
416 | | * a 512-byte buffer (see sslimpl.h), the variable length field |
417 | | * in ECCWrappedKeyInfo can be at most (512 - 8) = 504 bytes. |
418 | | * |
419 | | * XXX For now, NSS only supports named elliptic curves of size 571 bits |
420 | | * or smaller. The public value will fit within 145 bytes and EC params |
421 | | * will fit within 12 bytes. We'll need to revisit this when NSS |
422 | | * supports arbitrary curves. |
423 | | */ |
424 | 0 | #define MAX_EC_WRAPPED_KEY_BUFLEN 504 |
425 | | |
426 | | typedef struct ECCWrappedKeyInfoStr { |
427 | | PRUint16 size; /* EC public key size in bits */ |
428 | | PRUint16 encodedParamLen; /* length (in bytes) of DER encoded EC params */ |
429 | | PRUint16 pubValueLen; /* length (in bytes) of EC public value */ |
430 | | PRUint16 wrappedKeyLen; /* length (in bytes) of the wrapped key */ |
431 | | PRUint8 var[MAX_EC_WRAPPED_KEY_BUFLEN]; /* this buffer contains the */ |
432 | | /* EC public-key params, the EC public value and the wrapped key */ |
433 | | } ECCWrappedKeyInfo; |
434 | | |
435 | | CK_MECHANISM_TYPE |
436 | | ssl3_Alg2Mech(SSLCipherAlgorithm calg) |
437 | 5.88M | { |
438 | 5.88M | PORT_Assert(alg2Mech[calg].calg == calg); |
439 | 5.88M | return alg2Mech[calg].cmech; |
440 | 5.88M | } |
441 | | |
442 | | #if defined(TRACE) |
443 | | |
444 | | static char * |
445 | | ssl3_DecodeHandshakeType(int msgType) |
446 | 0 | { |
447 | 0 | char *rv; |
448 | 0 | static char line[40]; |
449 | |
|
450 | 0 | switch (msgType) { |
451 | 0 | case ssl_hs_hello_request: |
452 | 0 | rv = "hello_request (0)"; |
453 | 0 | break; |
454 | 0 | case ssl_hs_client_hello: |
455 | 0 | rv = "client_hello (1)"; |
456 | 0 | break; |
457 | 0 | case ssl_hs_server_hello: |
458 | 0 | rv = "server_hello (2)"; |
459 | 0 | break; |
460 | 0 | case ssl_hs_hello_verify_request: |
461 | 0 | rv = "hello_verify_request (3)"; |
462 | 0 | break; |
463 | 0 | case ssl_hs_new_session_ticket: |
464 | 0 | rv = "new_session_ticket (4)"; |
465 | 0 | break; |
466 | 0 | case ssl_hs_end_of_early_data: |
467 | 0 | rv = "end_of_early_data (5)"; |
468 | 0 | break; |
469 | 0 | case ssl_hs_hello_retry_request: |
470 | 0 | rv = "hello_retry_request (6)"; |
471 | 0 | break; |
472 | 0 | case ssl_hs_encrypted_extensions: |
473 | 0 | rv = "encrypted_extensions (8)"; |
474 | 0 | break; |
475 | 0 | case ssl_hs_certificate: |
476 | 0 | rv = "certificate (11)"; |
477 | 0 | break; |
478 | 0 | case ssl_hs_server_key_exchange: |
479 | 0 | rv = "server_key_exchange (12)"; |
480 | 0 | break; |
481 | 0 | case ssl_hs_certificate_request: |
482 | 0 | rv = "certificate_request (13)"; |
483 | 0 | break; |
484 | 0 | case ssl_hs_server_hello_done: |
485 | 0 | rv = "server_hello_done (14)"; |
486 | 0 | break; |
487 | 0 | case ssl_hs_certificate_verify: |
488 | 0 | rv = "certificate_verify (15)"; |
489 | 0 | break; |
490 | 0 | case ssl_hs_client_key_exchange: |
491 | 0 | rv = "client_key_exchange (16)"; |
492 | 0 | break; |
493 | 0 | case ssl_hs_finished: |
494 | 0 | rv = "finished (20)"; |
495 | 0 | break; |
496 | 0 | case ssl_hs_certificate_status: |
497 | 0 | rv = "certificate_status (22)"; |
498 | 0 | break; |
499 | 0 | case ssl_hs_key_update: |
500 | 0 | rv = "key_update (24)"; |
501 | 0 | break; |
502 | 0 | case ssl_hs_compressed_certificate: |
503 | 0 | rv = "compressed certificate (25)"; |
504 | 0 | break; |
505 | 0 | default: |
506 | 0 | snprintf(line, sizeof(line), "*UNKNOWN* handshake type! (%d)", msgType); |
507 | 0 | rv = line; |
508 | 0 | } |
509 | 0 | return rv; |
510 | 0 | } |
511 | | |
512 | | static char * |
513 | | ssl3_DecodeContentType(int msgType) |
514 | 0 | { |
515 | 0 | char *rv; |
516 | 0 | static char line[40]; |
517 | |
|
518 | 0 | switch (msgType) { |
519 | 0 | case ssl_ct_change_cipher_spec: |
520 | 0 | rv = "change_cipher_spec (20)"; |
521 | 0 | break; |
522 | 0 | case ssl_ct_alert: |
523 | 0 | rv = "alert (21)"; |
524 | 0 | break; |
525 | 0 | case ssl_ct_handshake: |
526 | 0 | rv = "handshake (22)"; |
527 | 0 | break; |
528 | 0 | case ssl_ct_application_data: |
529 | 0 | rv = "application_data (23)"; |
530 | 0 | break; |
531 | 0 | case ssl_ct_ack: |
532 | 0 | rv = "ack (26)"; |
533 | 0 | break; |
534 | 0 | default: |
535 | 0 | snprintf(line, sizeof(line), "*UNKNOWN* record type! (%d)", msgType); |
536 | 0 | rv = line; |
537 | 0 | } |
538 | 0 | return rv; |
539 | 0 | } |
540 | | |
541 | | #endif |
542 | | |
543 | | SSL3Statistics * |
544 | | SSL_GetStatistics(void) |
545 | 32.2k | { |
546 | 32.2k | return &ssl3stats; |
547 | 32.2k | } |
548 | | |
549 | | typedef struct tooLongStr { |
550 | | #if defined(IS_LITTLE_ENDIAN) |
551 | | PRInt32 low; |
552 | | PRInt32 high; |
553 | | #else |
554 | | PRInt32 high; |
555 | | PRInt32 low; |
556 | | #endif |
557 | | } tooLong; |
558 | | |
559 | | void |
560 | | SSL_AtomicIncrementLong(long *x) |
561 | 80.2k | { |
562 | 80.2k | if ((sizeof *x) == sizeof(PRInt32)) { |
563 | 0 | PR_ATOMIC_INCREMENT((PRInt32 *)x); |
564 | 80.2k | } else { |
565 | 80.2k | tooLong *tl = (tooLong *)x; |
566 | 80.2k | if (PR_ATOMIC_INCREMENT(&tl->low) == 0) |
567 | 0 | PR_ATOMIC_INCREMENT(&tl->high); |
568 | 80.2k | } |
569 | 80.2k | } |
570 | | |
571 | | PRBool |
572 | | ssl3_CipherSuiteAllowedForVersionRange(ssl3CipherSuite cipherSuite, |
573 | | const SSLVersionRange *vrange) |
574 | 3.24M | { |
575 | 3.24M | switch (cipherSuite) { |
576 | 45.1k | case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256: |
577 | 90.2k | case TLS_RSA_WITH_AES_256_CBC_SHA256: |
578 | 135k | case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256: |
579 | 180k | case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384: |
580 | 226k | case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256: |
581 | 271k | case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384: |
582 | 316k | case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256: |
583 | 361k | case TLS_RSA_WITH_AES_128_CBC_SHA256: |
584 | 407k | case TLS_RSA_WITH_AES_128_GCM_SHA256: |
585 | 453k | case TLS_RSA_WITH_AES_256_GCM_SHA384: |
586 | 498k | case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256: |
587 | 544k | case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256: |
588 | 589k | case TLS_RSA_WITH_NULL_SHA256: |
589 | 634k | case TLS_DHE_DSS_WITH_AES_128_GCM_SHA256: |
590 | 679k | case TLS_DHE_DSS_WITH_AES_256_GCM_SHA384: |
591 | 725k | case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: |
592 | 770k | case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: |
593 | 816k | case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: |
594 | 861k | case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: |
595 | 906k | case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256: |
596 | 952k | case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384: |
597 | 997k | case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256: |
598 | 1.04M | case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256: |
599 | 1.08M | case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256: |
600 | 1.08M | return vrange->max >= SSL_LIBRARY_VERSION_TLS_1_2 && |
601 | 1.08M | vrange->min < SSL_LIBRARY_VERSION_TLS_1_3; |
602 | | |
603 | | /* RFC 4492: ECC cipher suites need TLS extensions to negotiate curves and |
604 | | * point formats.*/ |
605 | 45.6k | case TLS_ECDH_ECDSA_WITH_NULL_SHA: |
606 | 90.8k | case TLS_ECDH_ECDSA_WITH_RC4_128_SHA: |
607 | 137k | case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA: |
608 | 182k | case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA: |
609 | 227k | case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA: |
610 | 272k | case TLS_ECDHE_ECDSA_WITH_NULL_SHA: |
611 | 317k | case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA: |
612 | 362k | case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA: |
613 | 407k | case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA: |
614 | 453k | case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: |
615 | 498k | case TLS_ECDH_RSA_WITH_NULL_SHA: |
616 | 544k | case TLS_ECDH_RSA_WITH_RC4_128_SHA: |
617 | 589k | case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA: |
618 | 634k | case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA: |
619 | 700k | case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA: |
620 | 747k | case TLS_ECDHE_RSA_WITH_NULL_SHA: |
621 | 792k | case TLS_ECDHE_RSA_WITH_RC4_128_SHA: |
622 | 838k | case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA: |
623 | 885k | case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: |
624 | 931k | case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: |
625 | 931k | return vrange->max >= SSL_LIBRARY_VERSION_TLS_1_0 && |
626 | 931k | vrange->min < SSL_LIBRARY_VERSION_TLS_1_3; |
627 | | |
628 | 45.5k | case TLS_AES_128_GCM_SHA256: |
629 | 90.9k | case TLS_AES_256_GCM_SHA384: |
630 | 136k | case TLS_CHACHA20_POLY1305_SHA256: |
631 | 136k | return vrange->max >= SSL_LIBRARY_VERSION_TLS_1_3; |
632 | | |
633 | 1.08M | default: |
634 | 1.08M | return vrange->min < SSL_LIBRARY_VERSION_TLS_1_3; |
635 | 3.24M | } |
636 | 3.24M | } |
637 | | |
638 | | /* return pointer to ssl3CipherSuiteDef for suite, or NULL */ |
639 | | /* XXX This does a linear search. A binary search would be better. */ |
640 | | const ssl3CipherSuiteDef * |
641 | | ssl_LookupCipherSuiteDef(ssl3CipherSuite suite) |
642 | 9.10M | { |
643 | 9.10M | int cipher_suite_def_len = |
644 | 9.10M | sizeof(cipher_suite_defs) / sizeof(cipher_suite_defs[0]); |
645 | 9.10M | int i; |
646 | | |
647 | 338M | for (i = 0; i < cipher_suite_def_len; i++) { |
648 | 338M | if (cipher_suite_defs[i].cipher_suite == suite) |
649 | 9.10M | return &cipher_suite_defs[i]; |
650 | 338M | } |
651 | 0 | PORT_Assert(PR_FALSE); /* We should never get here. */ |
652 | 0 | PORT_SetError(SSL_ERROR_UNKNOWN_CIPHER_SUITE); |
653 | 0 | return NULL; |
654 | 9.10M | } |
655 | | |
656 | | /* Find the cipher configuration struct associate with suite */ |
657 | | /* XXX This does a linear search. A binary search would be better. */ |
658 | | static ssl3CipherSuiteCfg * |
659 | | ssl_LookupCipherSuiteCfgMutable(ssl3CipherSuite suite, |
660 | | ssl3CipherSuiteCfg *suites) |
661 | 777k | { |
662 | 777k | int i; |
663 | | |
664 | 27.0M | for (i = 0; i < ssl_V3_SUITES_IMPLEMENTED; i++) { |
665 | 27.0M | if (suites[i].cipher_suite == suite) |
666 | 777k | return &suites[i]; |
667 | 27.0M | } |
668 | | /* return NULL and let the caller handle it. */ |
669 | 0 | PORT_SetError(SSL_ERROR_UNKNOWN_CIPHER_SUITE); |
670 | 0 | return NULL; |
671 | 777k | } |
672 | | |
673 | | const ssl3CipherSuiteCfg * |
674 | | ssl_LookupCipherSuiteCfg(ssl3CipherSuite suite, const ssl3CipherSuiteCfg *suites) |
675 | 56.0k | { |
676 | 56.0k | return ssl_LookupCipherSuiteCfgMutable(suite, |
677 | 56.0k | CONST_CAST(ssl3CipherSuiteCfg, suites)); |
678 | 56.0k | } |
679 | | |
680 | | static PRBool |
681 | | ssl_NamedGroupTypeEnabled(const sslSocket *ss, SSLKEAType keaType) |
682 | 1.86M | { |
683 | 1.86M | unsigned int i; |
684 | 4.73M | for (i = 0; i < SSL_NAMED_GROUP_COUNT; ++i) { |
685 | 4.73M | if (ss->namedGroupPreferences[i] && |
686 | 4.73M | ss->namedGroupPreferences[i]->keaType == keaType) { |
687 | 1.86M | return PR_TRUE; |
688 | 1.86M | } |
689 | 4.73M | } |
690 | 0 | return PR_FALSE; |
691 | 1.86M | } |
692 | | |
693 | | static PRBool |
694 | | ssl_KEAEnabled(const sslSocket *ss, SSLKEAType keaType) |
695 | 3.24M | { |
696 | 3.24M | switch (keaType) { |
697 | 727k | case ssl_kea_rsa: |
698 | 727k | return PR_TRUE; |
699 | | |
700 | 995k | case ssl_kea_dh: |
701 | 995k | case ssl_kea_dh_psk: { |
702 | 995k | if (ss->sec.isServer && !ss->opt.enableServerDhe) { |
703 | 0 | return PR_FALSE; |
704 | 0 | } |
705 | | |
706 | 995k | if (ss->sec.isServer) { |
707 | | /* If the server requires named FFDHE groups, then the client |
708 | | * must have included an FFDHE group. peerSupportsFfdheGroups |
709 | | * is set to true in ssl_HandleSupportedGroupsXtn(). */ |
710 | 0 | if (ss->opt.requireDHENamedGroups && |
711 | 0 | !ss->xtnData.peerSupportsFfdheGroups) { |
712 | 0 | return PR_FALSE; |
713 | 0 | } |
714 | | |
715 | | /* We can use the weak DH group if all of these are true: |
716 | | * 1. We don't require named groups. |
717 | | * 2. The peer doesn't support named groups. |
718 | | * 3. This isn't TLS 1.3. |
719 | | * 4. The weak group is enabled. */ |
720 | 0 | if (!ss->opt.requireDHENamedGroups && |
721 | 0 | !ss->xtnData.peerSupportsFfdheGroups && |
722 | 0 | ss->version < SSL_LIBRARY_VERSION_TLS_1_3 && |
723 | 0 | ss->ssl3.dheWeakGroupEnabled) { |
724 | 0 | return PR_TRUE; |
725 | 0 | } |
726 | 995k | } else { |
727 | 995k | if (ss->vrange.min < SSL_LIBRARY_VERSION_TLS_1_3 && |
728 | 995k | !ss->opt.requireDHENamedGroups) { |
729 | | /* The client enables DHE cipher suites even if no DHE groups |
730 | | * are enabled. Only if this isn't TLS 1.3 and named groups |
731 | | * are not required. */ |
732 | 516k | return PR_TRUE; |
733 | 516k | } |
734 | 995k | } |
735 | 478k | return ssl_NamedGroupTypeEnabled(ss, ssl_kea_dh); |
736 | 995k | } |
737 | | |
738 | 1.38M | case ssl_kea_ecdh: |
739 | 1.38M | case ssl_kea_ecdh_psk: |
740 | 1.38M | return ssl_NamedGroupTypeEnabled(ss, ssl_kea_ecdh); |
741 | | |
742 | 0 | case ssl_kea_ecdh_hybrid: |
743 | 0 | case ssl_kea_ecdh_hybrid_psk: |
744 | 0 | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
745 | 0 | return PR_FALSE; |
746 | 0 | } |
747 | 0 | return ssl_NamedGroupTypeEnabled(ss, ssl_kea_ecdh_hybrid); |
748 | | |
749 | 136k | case ssl_kea_tls13_any: |
750 | 136k | return PR_TRUE; |
751 | | |
752 | 0 | case ssl_kea_fortezza: |
753 | 0 | default: |
754 | 0 | PORT_Assert(0); |
755 | 3.24M | } |
756 | 0 | return PR_FALSE; |
757 | 3.24M | } |
758 | | |
759 | | static PRBool |
760 | | ssl_HasCert(const sslSocket *ss, PRUint16 maxVersion, SSLAuthType authType) |
761 | 0 | { |
762 | 0 | PRCList *cursor; |
763 | 0 | if (authType == ssl_auth_null || authType == ssl_auth_psk || authType == ssl_auth_tls13_any) { |
764 | 0 | return PR_TRUE; |
765 | 0 | } |
766 | 0 | for (cursor = PR_NEXT_LINK(&ss->serverCerts); |
767 | 0 | cursor != &ss->serverCerts; |
768 | 0 | cursor = PR_NEXT_LINK(cursor)) { |
769 | 0 | sslServerCert *cert = (sslServerCert *)cursor; |
770 | 0 | if (!cert->serverKeyPair || |
771 | 0 | !cert->serverKeyPair->privKey || |
772 | 0 | !cert->serverCertChain || |
773 | 0 | !SSL_CERT_IS(cert, authType)) { |
774 | 0 | continue; |
775 | 0 | } |
776 | | /* When called from ssl3_config_match_init(), all the EC curves will be |
777 | | * enabled, so this will essentially do nothing (unless we implement |
778 | | * curve configuration). However, once we have seen the |
779 | | * supported_groups extension and this is called from config_match(), |
780 | | * this will filter out certificates with an unsupported curve. |
781 | | * |
782 | | * If we might negotiate TLS 1.3, skip this test as group configuration |
783 | | * doesn't affect choices in TLS 1.3. |
784 | | */ |
785 | 0 | if (maxVersion < SSL_LIBRARY_VERSION_TLS_1_3 && |
786 | 0 | (authType == ssl_auth_ecdsa || |
787 | 0 | authType == ssl_auth_ecdh_ecdsa || |
788 | 0 | authType == ssl_auth_ecdh_rsa) && |
789 | 0 | !ssl_NamedGroupEnabled(ss, cert->namedCurve)) { |
790 | 0 | continue; |
791 | 0 | } |
792 | 0 | return PR_TRUE; |
793 | 0 | } |
794 | 0 | if (authType == ssl_auth_rsa_sign) { |
795 | 0 | return ssl_HasCert(ss, maxVersion, ssl_auth_rsa_pss); |
796 | 0 | } |
797 | 0 | return PR_FALSE; |
798 | 0 | } |
799 | | |
800 | | /* return true if the scheme is allowed by policy, This prevents |
801 | | * failures later when our actual signatures are rejected by |
802 | | * policy by either ssl code, or lower level NSS code */ |
803 | | static PRBool |
804 | | ssl_SchemePolicyOK(SSLSignatureScheme scheme, PRUint32 require) |
805 | 688k | { |
806 | | /* Hash policy. */ |
807 | 688k | PRUint32 policy; |
808 | 688k | SECOidTag hashOID = ssl3_HashTypeToOID(ssl_SignatureSchemeToHashType(scheme)); |
809 | 688k | SECOidTag sigOID; |
810 | | |
811 | | /* policy bits needed to enable a SignatureScheme */ |
812 | 688k | SECStatus rv = NSS_GetAlgorithmPolicy(hashOID, &policy); |
813 | 688k | if (rv == SECSuccess && |
814 | 688k | (policy & require) != require) { |
815 | 0 | return PR_FALSE; |
816 | 0 | } |
817 | | |
818 | | /* ssl_SignatureSchemeToAuthType reports rsa for rsa_pss_rsae, but we |
819 | | * actually implement pss signatures when we sign, so just use RSA_PSS |
820 | | * for all RSA PSS Siganture schemes */ |
821 | 688k | if (ssl_IsRsaPssSignatureScheme(scheme)) { |
822 | 111k | sigOID = SEC_OID_PKCS1_RSA_PSS_SIGNATURE; |
823 | 576k | } else { |
824 | 576k | sigOID = ssl3_AuthTypeToOID(ssl_SignatureSchemeToAuthType(scheme)); |
825 | 576k | } |
826 | | /* Signature Policy. */ |
827 | 688k | rv = NSS_GetAlgorithmPolicy(sigOID, &policy); |
828 | 688k | if (rv == SECSuccess && |
829 | 688k | (policy & require) != require) { |
830 | 0 | return PR_FALSE; |
831 | 0 | } |
832 | 688k | return PR_TRUE; |
833 | 688k | } |
834 | | |
835 | | /* Check that a signature scheme is accepted. |
836 | | * Both by policy and by having a token that supports it. */ |
837 | | static PRBool |
838 | | ssl_SignatureSchemeAccepted(PRUint16 minVersion, |
839 | | SSLSignatureScheme scheme, |
840 | | PRBool forCert) |
841 | 806k | { |
842 | | /* Disable RSA-PSS schemes if there are no tokens to verify them. */ |
843 | 806k | if (ssl_IsRsaPssSignatureScheme(scheme)) { |
844 | 109k | if (!PK11_TokenExists(auth_alg_defs[ssl_auth_rsa_pss])) { |
845 | 0 | return PR_FALSE; |
846 | 0 | } |
847 | 697k | } else if (!forCert && ssl_IsRsaPkcs1SignatureScheme(scheme)) { |
848 | | /* Disable PKCS#1 signatures if we are limited to TLS 1.3. |
849 | | * We still need to advertise PKCS#1 signatures in CH and CR |
850 | | * for certificate signatures. |
851 | | */ |
852 | 64.3k | if (minVersion >= SSL_LIBRARY_VERSION_TLS_1_3) { |
853 | 64.3k | return PR_FALSE; |
854 | 64.3k | } |
855 | 632k | } else if (ssl_IsDsaSignatureScheme(scheme)) { |
856 | | /* DSA: not in TLS 1.3, and check policy. */ |
857 | 209k | if (minVersion >= SSL_LIBRARY_VERSION_TLS_1_3) { |
858 | 65.6k | return PR_FALSE; |
859 | 65.6k | } |
860 | 209k | } |
861 | | |
862 | 676k | return ssl_SchemePolicyOK(scheme, kSSLSigSchemePolicy); |
863 | 806k | } |
864 | | |
865 | | static SECStatus |
866 | | ssl_CheckSignatureSchemes(sslSocket *ss) |
867 | 81.3k | { |
868 | 81.3k | if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_2) { |
869 | 10.4k | return SECSuccess; |
870 | 10.4k | } |
871 | | |
872 | | /* If this is a server using TLS 1.3, we just need to have one signature |
873 | | * scheme for which we have a usable certificate. |
874 | | * |
875 | | * Note: Certificates for earlier TLS versions are checked along with the |
876 | | * cipher suite in ssl3_config_match_init. */ |
877 | 70.9k | if (ss->sec.isServer && ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3) { |
878 | 0 | PRBool foundCert = PR_FALSE; |
879 | 0 | for (unsigned int i = 0; i < ss->ssl3.signatureSchemeCount; ++i) { |
880 | 0 | SSLAuthType authType = |
881 | 0 | ssl_SignatureSchemeToAuthType(ss->ssl3.signatureSchemes[i]); |
882 | 0 | if (ssl_HasCert(ss, ss->vrange.max, authType)) { |
883 | 0 | foundCert = PR_TRUE; |
884 | 0 | break; |
885 | 0 | } |
886 | 0 | } |
887 | 0 | if (!foundCert) { |
888 | 0 | PORT_SetError(SSL_ERROR_NO_SUPPORTED_SIGNATURE_ALGORITHM); |
889 | 0 | return SECFailure; |
890 | 0 | } |
891 | 0 | } |
892 | | |
893 | | /* Ensure that there is a signature scheme that can be accepted.*/ |
894 | 70.9k | for (unsigned int i = 0; i < ss->ssl3.signatureSchemeCount; ++i) { |
895 | 70.9k | if (ssl_SignatureSchemeAccepted(ss->vrange.min, |
896 | 70.9k | ss->ssl3.signatureSchemes[i], |
897 | 70.9k | PR_FALSE /* forCert */)) { |
898 | 70.9k | return SECSuccess; |
899 | 70.9k | } |
900 | 70.9k | } |
901 | 0 | PORT_SetError(SSL_ERROR_NO_SUPPORTED_SIGNATURE_ALGORITHM); |
902 | 0 | return SECFailure; |
903 | 70.9k | } |
904 | | |
905 | | /* For a server, check that a signature scheme that can be used with the |
906 | | * provided authType is both enabled and usable. */ |
907 | | static PRBool |
908 | | ssl_HasSignatureScheme(const sslSocket *ss, SSLAuthType authType) |
909 | 0 | { |
910 | 0 | PORT_Assert(ss->sec.isServer); |
911 | 0 | PORT_Assert(ss->ssl3.hs.preliminaryInfo & ssl_preinfo_version); |
912 | 0 | PORT_Assert(authType != ssl_auth_null); |
913 | 0 | PORT_Assert(authType != ssl_auth_tls13_any); |
914 | 0 | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_2 || |
915 | 0 | authType == ssl_auth_rsa_decrypt || |
916 | 0 | authType == ssl_auth_ecdh_rsa || |
917 | 0 | authType == ssl_auth_ecdh_ecdsa) { |
918 | 0 | return PR_TRUE; |
919 | 0 | } |
920 | 0 | for (unsigned int i = 0; i < ss->ssl3.signatureSchemeCount; ++i) { |
921 | 0 | SSLSignatureScheme scheme = ss->ssl3.signatureSchemes[i]; |
922 | 0 | SSLAuthType schemeAuthType = ssl_SignatureSchemeToAuthType(scheme); |
923 | 0 | PRBool acceptable = authType == schemeAuthType || |
924 | 0 | (schemeAuthType == ssl_auth_rsa_pss && |
925 | 0 | authType == ssl_auth_rsa_sign); |
926 | 0 | if (acceptable && ssl_SignatureSchemeAccepted(ss->version, scheme, PR_FALSE /* forCert */)) { |
927 | 0 | return PR_TRUE; |
928 | 0 | } |
929 | 0 | } |
930 | 0 | return PR_FALSE; |
931 | 0 | } |
932 | | |
933 | | /* Initialize the suite->isPresent value for config_match |
934 | | * Returns count of enabled ciphers supported by extant tokens, |
935 | | * regardless of policy or user preference. |
936 | | * If this returns zero, the user cannot do SSL v3. |
937 | | */ |
938 | | unsigned int |
939 | | ssl3_config_match_init(sslSocket *ss) |
940 | 81.3k | { |
941 | 81.3k | ssl3CipherSuiteCfg *suite; |
942 | 81.3k | const ssl3CipherSuiteDef *cipher_def; |
943 | 81.3k | SSLCipherAlgorithm cipher_alg; |
944 | 81.3k | CK_MECHANISM_TYPE cipher_mech; |
945 | 81.3k | SSLAuthType authType; |
946 | 81.3k | SSLKEAType keaType; |
947 | 81.3k | unsigned int i; |
948 | 81.3k | unsigned int numPresent = 0; |
949 | 81.3k | unsigned int numEnabled = 0; |
950 | | |
951 | 81.3k | PORT_Assert(ss); |
952 | 81.3k | if (!ss) { |
953 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
954 | 0 | return 0; |
955 | 0 | } |
956 | 81.3k | if (SSL_ALL_VERSIONS_DISABLED(&ss->vrange)) { |
957 | 0 | return 0; |
958 | 0 | } |
959 | 81.3k | if (ss->sec.isServer && ss->psk && |
960 | 81.3k | PR_CLIST_IS_EMPTY(&ss->serverCerts) && |
961 | 81.3k | (ss->opt.requestCertificate || ss->opt.requireCertificate)) { |
962 | | /* PSK and certificate auth cannot be combined. */ |
963 | 0 | PORT_SetError(SSL_ERROR_NO_CERTIFICATE); |
964 | 0 | return 0; |
965 | 0 | } |
966 | 81.3k | if (ssl_CheckSignatureSchemes(ss) != SECSuccess) { |
967 | 0 | return 0; /* Code already set. */ |
968 | 0 | } |
969 | | |
970 | 81.3k | ssl_FilterSupportedGroups(ss); |
971 | 5.86M | for (i = 0; i < ssl_V3_SUITES_IMPLEMENTED; i++) { |
972 | 5.77M | suite = &ss->cipherSuites[i]; |
973 | 5.77M | if (suite->enabled) { |
974 | 5.77M | ++numEnabled; |
975 | | /* We need the cipher defs to see if we have a token that can handle |
976 | | * this cipher. It isn't part of the static definition. |
977 | | */ |
978 | 5.77M | cipher_def = ssl_LookupCipherSuiteDef(suite->cipher_suite); |
979 | 5.77M | if (!cipher_def) { |
980 | 0 | suite->isPresent = PR_FALSE; |
981 | 0 | continue; |
982 | 0 | } |
983 | 5.77M | cipher_alg = ssl_GetBulkCipherDef(cipher_def)->calg; |
984 | 5.77M | cipher_mech = ssl3_Alg2Mech(cipher_alg); |
985 | | |
986 | | /* Mark the suites that are backed by real tokens, certs and keys */ |
987 | 5.77M | suite->isPresent = PR_TRUE; |
988 | | |
989 | 5.77M | authType = kea_defs[cipher_def->key_exchange_alg].authKeyType; |
990 | 5.77M | if (authType != ssl_auth_null && authType != ssl_auth_tls13_any) { |
991 | 5.53M | if (ss->sec.isServer && |
992 | 5.53M | !(ssl_HasCert(ss, ss->vrange.max, authType) && |
993 | 0 | ssl_HasSignatureScheme(ss, authType))) { |
994 | 0 | suite->isPresent = PR_FALSE; |
995 | 5.53M | } else if (!PK11_TokenExists(auth_alg_defs[authType])) { |
996 | 0 | suite->isPresent = PR_FALSE; |
997 | 0 | } |
998 | 5.53M | } |
999 | | |
1000 | 5.77M | keaType = kea_defs[cipher_def->key_exchange_alg].exchKeyType; |
1001 | 5.77M | if (keaType != ssl_kea_null && |
1002 | 5.77M | keaType != ssl_kea_tls13_any && |
1003 | 5.77M | !PK11_TokenExists(kea_alg_defs[keaType])) { |
1004 | 0 | suite->isPresent = PR_FALSE; |
1005 | 0 | } |
1006 | | |
1007 | 5.77M | if (cipher_alg != ssl_calg_null && |
1008 | 5.77M | !PK11_TokenExists(cipher_mech)) { |
1009 | 0 | suite->isPresent = PR_FALSE; |
1010 | 0 | } |
1011 | | |
1012 | 5.77M | if (suite->isPresent) { |
1013 | 5.77M | ++numPresent; |
1014 | 5.77M | } |
1015 | 5.77M | } |
1016 | 5.77M | } |
1017 | 81.3k | PORT_AssertArg(numPresent > 0 || numEnabled == 0); |
1018 | 81.3k | if (numPresent == 0) { |
1019 | 0 | PORT_SetError(SSL_ERROR_NO_CIPHERS_SUPPORTED); |
1020 | 0 | } |
1021 | 81.3k | return numPresent; |
1022 | 81.3k | } |
1023 | | |
1024 | | /* Return PR_TRUE if suite is usable. This if the suite is permitted by policy, |
1025 | | * enabled, has a certificate (as needed), has a viable key agreement method, is |
1026 | | * usable with the negotiated TLS version, and is otherwise usable. */ |
1027 | | PRBool |
1028 | | ssl3_config_match(const ssl3CipherSuiteCfg *suite, PRUint8 policy, |
1029 | | const SSLVersionRange *vrange, const sslSocket *ss) |
1030 | 3.24M | { |
1031 | 3.24M | const ssl3CipherSuiteDef *cipher_def; |
1032 | 3.24M | const ssl3KEADef *kea_def; |
1033 | | |
1034 | 3.24M | if (!suite) { |
1035 | 0 | PORT_Assert(suite); |
1036 | 0 | return PR_FALSE; |
1037 | 0 | } |
1038 | | |
1039 | 3.24M | PORT_Assert(policy != SSL_NOT_ALLOWED); |
1040 | 3.24M | if (policy == SSL_NOT_ALLOWED) |
1041 | 0 | return PR_FALSE; |
1042 | | |
1043 | 3.24M | if (!suite->enabled || !suite->isPresent) |
1044 | 0 | return PR_FALSE; |
1045 | | |
1046 | 3.24M | if ((suite->policy == SSL_NOT_ALLOWED) || |
1047 | 3.24M | (suite->policy > policy)) |
1048 | 0 | return PR_FALSE; |
1049 | | |
1050 | 3.24M | PORT_Assert(ss != NULL); |
1051 | 3.24M | cipher_def = ssl_LookupCipherSuiteDef(suite->cipher_suite); |
1052 | 3.24M | PORT_Assert(cipher_def != NULL); |
1053 | 3.24M | kea_def = &kea_defs[cipher_def->key_exchange_alg]; |
1054 | 3.24M | PORT_Assert(kea_def != NULL); |
1055 | 3.24M | if (!ssl_KEAEnabled(ss, kea_def->exchKeyType)) { |
1056 | 0 | return PR_FALSE; |
1057 | 0 | } |
1058 | | |
1059 | 3.24M | if (ss->sec.isServer && !ssl_HasCert(ss, vrange->max, kea_def->authKeyType)) { |
1060 | 0 | return PR_FALSE; |
1061 | 0 | } |
1062 | | |
1063 | | /* If a PSK is selected, disable suites that use a different hash than |
1064 | | * the PSK. We advertise non-PSK-compatible suites in the CH, as we could |
1065 | | * fallback to certificate auth. The client handler will check hash |
1066 | | * compatibility before committing to use the PSK. */ |
1067 | 3.24M | if (ss->xtnData.selectedPsk) { |
1068 | 0 | if (ss->xtnData.selectedPsk->hash != cipher_def->prf_hash) { |
1069 | 0 | return PR_FALSE; |
1070 | 0 | } |
1071 | 0 | } |
1072 | | |
1073 | 3.24M | return ssl3_CipherSuiteAllowedForVersionRange(suite->cipher_suite, vrange); |
1074 | 3.24M | } |
1075 | | |
1076 | | /* For TLS 1.3, when resuming, check for a ciphersuite that is both compatible |
1077 | | * with the identified ciphersuite and enabled. */ |
1078 | | static PRBool |
1079 | | tls13_ResumptionCompatible(sslSocket *ss, ssl3CipherSuite suite) |
1080 | 0 | { |
1081 | 0 | SSLVersionRange vrange = { SSL_LIBRARY_VERSION_TLS_1_3, |
1082 | 0 | SSL_LIBRARY_VERSION_TLS_1_3 }; |
1083 | 0 | SSLHashType hash = tls13_GetHashForCipherSuite(suite); |
1084 | 0 | for (unsigned int i = 0; i < PR_ARRAY_SIZE(cipher_suite_defs); i++) { |
1085 | 0 | if (cipher_suite_defs[i].prf_hash == hash) { |
1086 | 0 | const ssl3CipherSuiteCfg *suiteCfg = |
1087 | 0 | ssl_LookupCipherSuiteCfg(cipher_suite_defs[i].cipher_suite, |
1088 | 0 | ss->cipherSuites); |
1089 | 0 | if (suite && ssl3_config_match(suiteCfg, ss->ssl3.policy, &vrange, ss)) { |
1090 | 0 | return PR_TRUE; |
1091 | 0 | } |
1092 | 0 | } |
1093 | 0 | } |
1094 | 0 | return PR_FALSE; |
1095 | 0 | } |
1096 | | |
1097 | | /* |
1098 | | * Null compression, mac and encryption functions |
1099 | | */ |
1100 | | SECStatus |
1101 | | Null_Cipher(void *ctx, unsigned char *output, unsigned int *outputLen, unsigned int maxOutputLen, |
1102 | | const unsigned char *input, unsigned int inputLen) |
1103 | 363k | { |
1104 | 363k | if (inputLen > maxOutputLen) { |
1105 | 0 | *outputLen = 0; /* Match PK11_CipherOp in setting outputLen */ |
1106 | 0 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
1107 | 0 | return SECFailure; |
1108 | 0 | } |
1109 | 363k | *outputLen = inputLen; |
1110 | 363k | if (inputLen > 0 && input != output) { |
1111 | 354k | PORT_Memcpy(output, input, inputLen); |
1112 | 354k | } |
1113 | 363k | return SECSuccess; |
1114 | 363k | } |
1115 | | |
1116 | | /* Wrapper around PK11_CipherOp to avoid undefined behavior due to incompatible |
1117 | | * function pointer type cast |
1118 | | */ |
1119 | | static SECStatus |
1120 | | SSLCipher_PK11_CipherOp(void *ctx, unsigned char *output, unsigned int *outputLen, unsigned int maxOutputLen, |
1121 | | const unsigned char *input, unsigned int inputLen) |
1122 | 0 | { |
1123 | 0 | PK11Context *pctx = ctx; |
1124 | 0 | PORT_Assert(maxOutputLen <= INT_MAX); |
1125 | 0 | int signedOutputLen = maxOutputLen; |
1126 | 0 | SECStatus rv = PK11_CipherOp(pctx, output, &signedOutputLen, maxOutputLen, input, inputLen); |
1127 | 0 | PORT_Assert(signedOutputLen >= 0); |
1128 | 0 | *outputLen = signedOutputLen; |
1129 | 0 | return rv; |
1130 | 0 | } |
1131 | | |
1132 | | /* |
1133 | | * SSL3 Utility functions |
1134 | | */ |
1135 | | |
1136 | | static void |
1137 | | ssl_SetSpecVersions(sslSocket *ss, ssl3CipherSpec *spec) |
1138 | 86.2k | { |
1139 | 86.2k | spec->version = ss->version; |
1140 | 86.2k | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
1141 | 667 | tls13_SetSpecRecordVersion(ss, spec); |
1142 | 85.5k | } else if (IS_DTLS(ss)) { |
1143 | 0 | spec->recordVersion = dtls_TLSVersionToDTLSVersion(ss->version); |
1144 | 85.5k | } else { |
1145 | 85.5k | spec->recordVersion = ss->version; |
1146 | 85.5k | } |
1147 | 86.2k | } |
1148 | | |
1149 | | /* allowLargerPeerVersion controls whether the function will select the |
1150 | | * highest enabled SSL version or fail when peerVersion is greater than the |
1151 | | * highest enabled version. |
1152 | | * |
1153 | | * If allowLargerPeerVersion is true, peerVersion is the peer's highest |
1154 | | * enabled version rather than the peer's selected version. |
1155 | | */ |
1156 | | SECStatus |
1157 | | ssl3_NegotiateVersion(sslSocket *ss, SSL3ProtocolVersion peerVersion, |
1158 | | PRBool allowLargerPeerVersion) |
1159 | 0 | { |
1160 | 0 | SSL3ProtocolVersion negotiated; |
1161 | | |
1162 | | /* Prevent negotiating to a lower version in response to a TLS 1.3 HRR. */ |
1163 | 0 | if (ss->ssl3.hs.helloRetry) { |
1164 | 0 | PORT_SetError(SSL_ERROR_UNSUPPORTED_VERSION); |
1165 | 0 | return SECFailure; |
1166 | 0 | } |
1167 | | |
1168 | 0 | if (SSL_ALL_VERSIONS_DISABLED(&ss->vrange)) { |
1169 | 0 | PORT_SetError(SSL_ERROR_SSL_DISABLED); |
1170 | 0 | return SECFailure; |
1171 | 0 | } |
1172 | | |
1173 | 0 | if (peerVersion < ss->vrange.min || |
1174 | 0 | (peerVersion > ss->vrange.max && !allowLargerPeerVersion)) { |
1175 | 0 | PORT_SetError(SSL_ERROR_UNSUPPORTED_VERSION); |
1176 | 0 | return SECFailure; |
1177 | 0 | } |
1178 | | |
1179 | 0 | negotiated = PR_MIN(peerVersion, ss->vrange.max); |
1180 | 0 | PORT_Assert(ssl3_VersionIsSupported(ss->protocolVariant, negotiated)); |
1181 | 0 | if (ss->firstHsDone && ss->version != negotiated) { |
1182 | 0 | PORT_SetError(SSL_ERROR_UNSUPPORTED_VERSION); |
1183 | 0 | return SECFailure; |
1184 | 0 | } |
1185 | | |
1186 | 0 | ss->version = negotiated; |
1187 | 0 | return SECSuccess; |
1188 | 0 | } |
1189 | | |
1190 | | /* Used by the client when the server produces a version number. |
1191 | | * This reads, validates, and normalizes the value. */ |
1192 | | SECStatus |
1193 | | ssl_ClientReadVersion(sslSocket *ss, PRUint8 **b, unsigned int *len, |
1194 | | SSL3ProtocolVersion *version) |
1195 | 40.2k | { |
1196 | 40.2k | SSL3ProtocolVersion v; |
1197 | 40.2k | PRUint32 temp; |
1198 | 40.2k | SECStatus rv; |
1199 | | |
1200 | 40.2k | rv = ssl3_ConsumeHandshakeNumber(ss, &temp, 2, b, len); |
1201 | 40.2k | if (rv != SECSuccess) { |
1202 | 7 | return SECFailure; /* alert has been sent */ |
1203 | 7 | } |
1204 | 40.2k | v = (SSL3ProtocolVersion)temp; |
1205 | | |
1206 | 40.2k | if (IS_DTLS(ss)) { |
1207 | 0 | v = dtls_DTLSVersionToTLSVersion(v); |
1208 | | /* Check for failure. */ |
1209 | 0 | if (!v || v > SSL_LIBRARY_VERSION_MAX_SUPPORTED) { |
1210 | 0 | SSL3_SendAlert(ss, alert_fatal, illegal_parameter); |
1211 | 0 | return SECFailure; |
1212 | 0 | } |
1213 | 0 | } |
1214 | | |
1215 | | /* You can't negotiate TLS 1.3 this way. */ |
1216 | 40.2k | if (v >= SSL_LIBRARY_VERSION_TLS_1_3) { |
1217 | 12 | SSL3_SendAlert(ss, alert_fatal, illegal_parameter); |
1218 | 12 | return SECFailure; |
1219 | 12 | } |
1220 | 40.2k | *version = v; |
1221 | 40.2k | return SECSuccess; |
1222 | 40.2k | } |
1223 | | |
1224 | | SECStatus |
1225 | | ssl3_GetNewRandom(SSL3Random random) |
1226 | 44.6k | { |
1227 | 44.6k | SECStatus rv; |
1228 | | |
1229 | 44.6k | rv = PK11_GenerateRandom(random, SSL3_RANDOM_LENGTH); |
1230 | 44.6k | if (rv != SECSuccess) { |
1231 | 0 | ssl_MapLowLevelError(SSL_ERROR_GENERATE_RANDOM_FAILURE); |
1232 | 0 | } |
1233 | 44.6k | return rv; |
1234 | 44.6k | } |
1235 | | |
1236 | | SECStatus |
1237 | | ssl3_SignHashesWithPrivKey(SSL3Hashes *hash, SECKEYPrivateKey *key, |
1238 | | SSLSignatureScheme scheme, PRBool isTls, SECItem *buf) |
1239 | 0 | { |
1240 | 0 | SECStatus rv = SECFailure; |
1241 | 0 | PRBool doDerEncode = PR_FALSE; |
1242 | 0 | PRBool useRsaPss = ssl_IsRsaPssSignatureScheme(scheme); |
1243 | 0 | SECItem hashItem; |
1244 | |
|
1245 | 0 | buf->data = NULL; |
1246 | |
|
1247 | 0 | switch (SECKEY_GetPrivateKeyType(key)) { |
1248 | 0 | case rsaKey: |
1249 | 0 | hashItem.data = hash->u.raw; |
1250 | 0 | hashItem.len = hash->len; |
1251 | 0 | break; |
1252 | 0 | case dsaKey: |
1253 | 0 | doDerEncode = isTls; |
1254 | | /* ssl_hash_none is used to specify the MD5/SHA1 concatenated hash. |
1255 | | * In that case, we use just the SHA1 part. */ |
1256 | 0 | if (hash->hashAlg == ssl_hash_none) { |
1257 | 0 | hashItem.data = hash->u.s.sha; |
1258 | 0 | hashItem.len = sizeof(hash->u.s.sha); |
1259 | 0 | } else { |
1260 | 0 | hashItem.data = hash->u.raw; |
1261 | 0 | hashItem.len = hash->len; |
1262 | 0 | } |
1263 | 0 | break; |
1264 | 0 | case ecKey: |
1265 | 0 | doDerEncode = PR_TRUE; |
1266 | | /* ssl_hash_none is used to specify the MD5/SHA1 concatenated hash. |
1267 | | * In that case, we use just the SHA1 part. */ |
1268 | 0 | if (hash->hashAlg == ssl_hash_none) { |
1269 | 0 | hashItem.data = hash->u.s.sha; |
1270 | 0 | hashItem.len = sizeof(hash->u.s.sha); |
1271 | 0 | } else { |
1272 | 0 | hashItem.data = hash->u.raw; |
1273 | 0 | hashItem.len = hash->len; |
1274 | 0 | } |
1275 | 0 | break; |
1276 | 0 | default: |
1277 | 0 | PORT_SetError(SEC_ERROR_INVALID_KEY); |
1278 | 0 | goto done; |
1279 | 0 | } |
1280 | 0 | PRINT_BUF(60, (NULL, "hash(es) to be signed", hashItem.data, hashItem.len)); |
1281 | |
|
1282 | 0 | if (useRsaPss || hash->hashAlg == ssl_hash_none) { |
1283 | 0 | CK_MECHANISM_TYPE mech = PK11_MapSignKeyType(key->keyType); |
1284 | 0 | int signatureLen = PK11_SignatureLen(key); |
1285 | 0 | PRInt32 optval; |
1286 | |
|
1287 | 0 | SECItem *params = NULL; |
1288 | 0 | CK_RSA_PKCS_PSS_PARAMS pssParams; |
1289 | 0 | SECItem pssParamsItem = { siBuffer, |
1290 | 0 | (unsigned char *)&pssParams, |
1291 | 0 | sizeof(pssParams) }; |
1292 | |
|
1293 | 0 | if (signatureLen <= 0) { |
1294 | 0 | PORT_SetError(SEC_ERROR_INVALID_KEY); |
1295 | 0 | goto done; |
1296 | 0 | } |
1297 | | /* since we are calling PK11_SignWithMechanism directly, we need to check the |
1298 | | * key policy ourselves (which is already checked in SGN_Digest */ |
1299 | 0 | rv = NSS_OptionGet(NSS_KEY_SIZE_POLICY_FLAGS, &optval); |
1300 | 0 | if ((rv == SECSuccess) && |
1301 | 0 | ((optval & NSS_KEY_SIZE_POLICY_SIGN_FLAG) == NSS_KEY_SIZE_POLICY_SIGN_FLAG)) { |
1302 | 0 | rv = SECKEY_EnforceKeySize(key->keyType, SECKEY_PrivateKeyStrengthInBits(key), |
1303 | 0 | SEC_ERROR_SIGNATURE_ALGORITHM_DISABLED); |
1304 | 0 | if (rv != SECSuccess) { |
1305 | 0 | goto done; /* error code already set */ |
1306 | 0 | } |
1307 | 0 | } |
1308 | | |
1309 | 0 | buf->len = (unsigned)signatureLen; |
1310 | 0 | buf->data = (unsigned char *)PORT_Alloc(signatureLen); |
1311 | 0 | if (!buf->data) |
1312 | 0 | goto done; /* error code was set. */ |
1313 | | |
1314 | 0 | if (useRsaPss) { |
1315 | 0 | pssParams.hashAlg = ssl3_GetHashMechanismByHashType(hash->hashAlg); |
1316 | 0 | pssParams.mgf = ssl3_GetMgfMechanismByHashType(hash->hashAlg); |
1317 | 0 | pssParams.sLen = hashItem.len; |
1318 | 0 | params = &pssParamsItem; |
1319 | 0 | mech = CKM_RSA_PKCS_PSS; |
1320 | 0 | } |
1321 | |
|
1322 | 0 | rv = PK11_SignWithMechanism(key, mech, params, buf, &hashItem); |
1323 | 0 | } else { |
1324 | 0 | SECOidTag hashOID = ssl3_HashTypeToOID(hash->hashAlg); |
1325 | 0 | rv = SGN_Digest(key, hashOID, buf, &hashItem); |
1326 | 0 | } |
1327 | 0 | if (rv != SECSuccess) { |
1328 | 0 | ssl_MapLowLevelError(SSL_ERROR_SIGN_HASHES_FAILURE); |
1329 | 0 | } else if (doDerEncode) { |
1330 | 0 | SECItem derSig = { siBuffer, NULL, 0 }; |
1331 | | |
1332 | | /* This also works for an ECDSA signature */ |
1333 | 0 | rv = DSAU_EncodeDerSigWithLen(&derSig, buf, buf->len); |
1334 | 0 | if (rv == SECSuccess) { |
1335 | 0 | PORT_Free(buf->data); /* discard unencoded signature. */ |
1336 | 0 | *buf = derSig; /* give caller encoded signature. */ |
1337 | 0 | } else if (derSig.data) { |
1338 | 0 | PORT_Free(derSig.data); |
1339 | 0 | } |
1340 | 0 | } |
1341 | |
|
1342 | 0 | PRINT_BUF(60, (NULL, "signed hashes", (unsigned char *)buf->data, buf->len)); |
1343 | 0 | done: |
1344 | 0 | if (rv != SECSuccess && buf->data) { |
1345 | 0 | PORT_Free(buf->data); |
1346 | 0 | buf->data = NULL; |
1347 | 0 | } |
1348 | 0 | return rv; |
1349 | 0 | } |
1350 | | |
1351 | | /* Called by ssl3_SendServerKeyExchange and ssl3_SendCertificateVerify */ |
1352 | | SECStatus |
1353 | | ssl3_SignHashes(sslSocket *ss, SSL3Hashes *hash, SECKEYPrivateKey *key, |
1354 | | SECItem *buf) |
1355 | 0 | { |
1356 | 0 | SECStatus rv = SECFailure; |
1357 | 0 | PRBool isTLS = (PRBool)(ss->version > SSL_LIBRARY_VERSION_3_0); |
1358 | 0 | SSLSignatureScheme scheme = ss->ssl3.hs.signatureScheme; |
1359 | |
|
1360 | 0 | rv = ssl3_SignHashesWithPrivKey(hash, key, scheme, isTLS, buf); |
1361 | 0 | if (rv != SECSuccess) { |
1362 | 0 | return SECFailure; |
1363 | 0 | } |
1364 | | |
1365 | 0 | if (ss->sec.isServer) { |
1366 | 0 | ss->sec.signatureScheme = scheme; |
1367 | 0 | ss->sec.authType = ssl_SignatureSchemeToAuthType(scheme); |
1368 | 0 | } |
1369 | |
|
1370 | 0 | return SECSuccess; |
1371 | 0 | } |
1372 | | |
1373 | | /* Called from ssl3_VerifySignedHashes and tls13_HandleCertificateVerify. */ |
1374 | | SECStatus |
1375 | | ssl_VerifySignedHashesWithPubKey(sslSocket *ss, SECKEYPublicKey *key, |
1376 | | SSLSignatureScheme scheme, |
1377 | | SSL3Hashes *hash, SECItem *buf) |
1378 | 8.60k | { |
1379 | 8.60k | SECItem *signature = NULL; |
1380 | 8.60k | SECStatus rv = SECFailure; |
1381 | 8.60k | SECItem hashItem; |
1382 | 8.60k | SECOidTag encAlg; |
1383 | 8.60k | SECOidTag hashAlg; |
1384 | 8.60k | void *pwArg = ss->pkcs11PinArg; |
1385 | 8.60k | PRBool isRsaPssScheme = ssl_IsRsaPssSignatureScheme(scheme); |
1386 | | |
1387 | 8.60k | PRINT_BUF(60, (NULL, "check signed hashes", buf->data, buf->len)); |
1388 | | |
1389 | 8.60k | hashAlg = ssl3_HashTypeToOID(hash->hashAlg); |
1390 | 8.60k | switch (SECKEY_GetPublicKeyType(key)) { |
1391 | 7.35k | case rsaKey: |
1392 | 7.35k | encAlg = SEC_OID_PKCS1_RSA_ENCRYPTION; |
1393 | 7.35k | hashItem.data = hash->u.raw; |
1394 | 7.35k | hashItem.len = hash->len; |
1395 | 7.35k | if (scheme == ssl_sig_none) { |
1396 | 3.13k | scheme = ssl_sig_rsa_pkcs1_sha1md5; |
1397 | 3.13k | } |
1398 | 7.35k | break; |
1399 | 900 | case dsaKey: |
1400 | 900 | encAlg = SEC_OID_ANSIX9_DSA_SIGNATURE; |
1401 | | /* ssl_hash_none is used to specify the MD5/SHA1 concatenated hash. |
1402 | | * In that case, we use just the SHA1 part. */ |
1403 | 900 | if (hash->hashAlg == ssl_hash_none) { |
1404 | 1 | hashItem.data = hash->u.s.sha; |
1405 | 1 | hashItem.len = sizeof(hash->u.s.sha); |
1406 | 899 | } else { |
1407 | 899 | hashItem.data = hash->u.raw; |
1408 | 899 | hashItem.len = hash->len; |
1409 | 899 | } |
1410 | | /* Allow DER encoded DSA signatures in SSL 3.0 */ |
1411 | 900 | if (ss->ssl3.prSpec->version > SSL_LIBRARY_VERSION_3_0 || |
1412 | 900 | buf->len != SECKEY_SignatureLen(key)) { |
1413 | 900 | signature = DSAU_DecodeDerSigToLen(buf, SECKEY_SignatureLen(key)); |
1414 | 900 | if (!signature) { |
1415 | 4 | PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE); |
1416 | 4 | goto loser; |
1417 | 4 | } |
1418 | 896 | buf = signature; |
1419 | 896 | } |
1420 | 896 | if (scheme == ssl_sig_none) { |
1421 | 0 | scheme = ssl_sig_dsa_sha1; |
1422 | 0 | } |
1423 | 896 | break; |
1424 | | |
1425 | 342 | case ecKey: |
1426 | 342 | encAlg = SEC_OID_ANSIX962_EC_PUBLIC_KEY; |
1427 | | /* ssl_hash_none is used to specify the MD5/SHA1 concatenated hash. |
1428 | | * In that case, we use just the SHA1 part. |
1429 | | * ECDSA signatures always encode the integers r and s using ASN.1 |
1430 | | * (unlike DSA where ASN.1 encoding is used with TLS but not with |
1431 | | * SSL3). So we can use VFY_VerifyDigestDirect for ECDSA. |
1432 | | */ |
1433 | 342 | if (hash->hashAlg == ssl_hash_none) { |
1434 | 182 | hashAlg = SEC_OID_SHA1; |
1435 | 182 | hashItem.data = hash->u.s.sha; |
1436 | 182 | hashItem.len = sizeof(hash->u.s.sha); |
1437 | 182 | } else { |
1438 | 160 | hashItem.data = hash->u.raw; |
1439 | 160 | hashItem.len = hash->len; |
1440 | 160 | } |
1441 | 342 | if (scheme == ssl_sig_none) { |
1442 | 182 | scheme = ssl_sig_ecdsa_sha1; |
1443 | 182 | } |
1444 | 342 | break; |
1445 | | |
1446 | 0 | default: |
1447 | 0 | PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); |
1448 | 0 | goto loser; |
1449 | 8.60k | } |
1450 | | |
1451 | 8.59k | PRINT_BUF(60, (NULL, "hash(es) to be verified", |
1452 | 8.59k | hashItem.data, hashItem.len)); |
1453 | | |
1454 | 8.59k | if (isRsaPssScheme || |
1455 | 8.59k | hashAlg == SEC_OID_UNKNOWN || |
1456 | 8.59k | SECKEY_GetPublicKeyType(key) == dsaKey) { |
1457 | | /* VFY_VerifyDigestDirect requires DSA signatures to be DER-encoded. |
1458 | | * DSA signatures are DER-encoded in TLS but not in SSL3 and the code |
1459 | | * above always removes the DER encoding of DSA signatures when |
1460 | | * present. Thus DSA signatures are always verified with PK11_Verify. |
1461 | | */ |
1462 | 5.01k | CK_MECHANISM_TYPE mech = PK11_MapSignKeyType(key->keyType); |
1463 | | |
1464 | 5.01k | SECItem *params = NULL; |
1465 | 5.01k | CK_RSA_PKCS_PSS_PARAMS pssParams; |
1466 | 5.01k | SECItem pssParamsItem = { siBuffer, |
1467 | 5.01k | (unsigned char *)&pssParams, |
1468 | 5.01k | sizeof(pssParams) }; |
1469 | | |
1470 | 5.01k | if (isRsaPssScheme) { |
1471 | 987 | pssParams.hashAlg = ssl3_GetHashMechanismByHashType(hash->hashAlg); |
1472 | 987 | pssParams.mgf = ssl3_GetMgfMechanismByHashType(hash->hashAlg); |
1473 | 987 | pssParams.sLen = hashItem.len; |
1474 | 987 | params = &pssParamsItem; |
1475 | 987 | mech = CKM_RSA_PKCS_PSS; |
1476 | 987 | } |
1477 | | |
1478 | 5.01k | rv = PK11_VerifyWithMechanism(key, mech, params, buf, &hashItem, pwArg); |
1479 | 5.01k | } else { |
1480 | 3.58k | rv = VFY_VerifyDigestDirect(&hashItem, key, buf, encAlg, hashAlg, |
1481 | 3.58k | pwArg); |
1482 | 3.58k | } |
1483 | 8.59k | if (signature) { |
1484 | 896 | SECITEM_FreeItem(signature, PR_TRUE); |
1485 | 896 | } |
1486 | 8.59k | if (rv != SECSuccess) { |
1487 | 8.59k | ssl_MapLowLevelError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE); |
1488 | 8.59k | } |
1489 | 8.59k | if (!ss->sec.isServer) { |
1490 | 8.59k | ss->sec.signatureScheme = scheme; |
1491 | 8.59k | ss->sec.authType = ssl_SignatureSchemeToAuthType(scheme); |
1492 | 8.59k | } |
1493 | | |
1494 | 8.60k | loser: |
1495 | 8.60k | #ifdef UNSAFE_FUZZER_MODE |
1496 | 8.60k | rv = SECSuccess; |
1497 | 8.60k | PORT_SetError(0); |
1498 | 8.60k | #endif |
1499 | 8.60k | return rv; |
1500 | 8.59k | } |
1501 | | |
1502 | | /* Called from ssl3_HandleServerKeyExchange, ssl3_HandleCertificateVerify */ |
1503 | | SECStatus |
1504 | | ssl3_VerifySignedHashes(sslSocket *ss, SSLSignatureScheme scheme, SSL3Hashes *hash, |
1505 | | SECItem *buf) |
1506 | 8.60k | { |
1507 | 8.60k | SECKEYPublicKey *pubKey = |
1508 | 8.60k | SECKEY_ExtractPublicKey(&ss->sec.peerCert->subjectPublicKeyInfo); |
1509 | 8.60k | if (pubKey == NULL) { |
1510 | 0 | ssl_MapLowLevelError(SSL_ERROR_EXTRACT_PUBLIC_KEY_FAILURE); |
1511 | 0 | return SECFailure; |
1512 | 0 | } |
1513 | 8.60k | SECStatus rv = ssl_VerifySignedHashesWithPubKey(ss, pubKey, scheme, |
1514 | 8.60k | hash, buf); |
1515 | 8.60k | SECKEY_DestroyPublicKey(pubKey); |
1516 | 8.60k | return rv; |
1517 | 8.60k | } |
1518 | | |
1519 | | /* Caller must set hiLevel error code. */ |
1520 | | /* Called from ssl3_ComputeDHKeyHash |
1521 | | * which are called from ssl3_HandleServerKeyExchange. |
1522 | | * |
1523 | | * hashAlg: ssl_hash_none indicates the pre-1.2, MD5/SHA1 combination hash. |
1524 | | */ |
1525 | | SECStatus |
1526 | | ssl3_ComputeCommonKeyHash(SSLHashType hashAlg, |
1527 | | PRUint8 *hashBuf, unsigned int bufLen, |
1528 | | SSL3Hashes *hashes) |
1529 | 8.60k | { |
1530 | 8.60k | SECStatus rv; |
1531 | 8.60k | SECOidTag hashOID; |
1532 | 8.60k | PRUint32 policy; |
1533 | | |
1534 | 8.60k | if (hashAlg == ssl_hash_none) { |
1535 | 3.31k | if ((NSS_GetAlgorithmPolicy(SEC_OID_SHA1, &policy) == SECSuccess) && |
1536 | 3.31k | !(policy & NSS_USE_ALG_IN_SSL_KX)) { |
1537 | 0 | ssl_MapLowLevelError(SSL_ERROR_UNSUPPORTED_HASH_ALGORITHM); |
1538 | 0 | return SECFailure; |
1539 | 0 | } |
1540 | 3.31k | rv = PK11_HashBuf(SEC_OID_MD5, hashes->u.s.md5, hashBuf, bufLen); |
1541 | 3.31k | if (rv != SECSuccess) { |
1542 | 0 | ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); |
1543 | 0 | return rv; |
1544 | 0 | } |
1545 | 3.31k | rv = PK11_HashBuf(SEC_OID_SHA1, hashes->u.s.sha, hashBuf, bufLen); |
1546 | 3.31k | if (rv != SECSuccess) { |
1547 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
1548 | 0 | return rv; |
1549 | 0 | } |
1550 | 3.31k | hashes->len = MD5_LENGTH + SHA1_LENGTH; |
1551 | 5.28k | } else { |
1552 | 5.28k | hashOID = ssl3_HashTypeToOID(hashAlg); |
1553 | 5.28k | if ((NSS_GetAlgorithmPolicy(hashOID, &policy) == SECSuccess) && |
1554 | 5.28k | !(policy & NSS_USE_ALG_IN_SSL_KX)) { |
1555 | 0 | ssl_MapLowLevelError(SSL_ERROR_UNSUPPORTED_HASH_ALGORITHM); |
1556 | 0 | return SECFailure; |
1557 | 0 | } |
1558 | 5.28k | hashes->len = HASH_ResultLenByOidTag(hashOID); |
1559 | 5.28k | if (hashes->len == 0 || hashes->len > sizeof(hashes->u.raw)) { |
1560 | 0 | ssl_MapLowLevelError(SSL_ERROR_UNSUPPORTED_HASH_ALGORITHM); |
1561 | 0 | return SECFailure; |
1562 | 0 | } |
1563 | 5.28k | rv = PK11_HashBuf(hashOID, hashes->u.raw, hashBuf, bufLen); |
1564 | 5.28k | if (rv != SECSuccess) { |
1565 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
1566 | 0 | return rv; |
1567 | 0 | } |
1568 | 5.28k | } |
1569 | 8.60k | hashes->hashAlg = hashAlg; |
1570 | 8.60k | return SECSuccess; |
1571 | 8.60k | } |
1572 | | |
1573 | | /* Caller must set hiLevel error code. */ |
1574 | | /* Called from ssl3_HandleServerKeyExchange. */ |
1575 | | static SECStatus |
1576 | | ssl3_ComputeDHKeyHash(sslSocket *ss, SSLHashType hashAlg, SSL3Hashes *hashes, |
1577 | | SECItem dh_p, SECItem dh_g, SECItem dh_Ys, PRBool padY) |
1578 | 2.08k | { |
1579 | 2.08k | sslBuffer buf = SSL_BUFFER_EMPTY; |
1580 | 2.08k | SECStatus rv; |
1581 | 2.08k | unsigned int yLen; |
1582 | 2.08k | unsigned int i; |
1583 | | |
1584 | 2.08k | PORT_Assert(dh_p.data); |
1585 | 2.08k | PORT_Assert(dh_g.data); |
1586 | 2.08k | PORT_Assert(dh_Ys.data); |
1587 | | |
1588 | 2.08k | rv = sslBuffer_Append(&buf, ss->ssl3.hs.client_random, SSL3_RANDOM_LENGTH); |
1589 | 2.08k | if (rv != SECSuccess) { |
1590 | 0 | goto loser; |
1591 | 0 | } |
1592 | 2.08k | rv = sslBuffer_Append(&buf, ss->ssl3.hs.server_random, SSL3_RANDOM_LENGTH); |
1593 | 2.08k | if (rv != SECSuccess) { |
1594 | 0 | goto loser; |
1595 | 0 | } |
1596 | | /* p */ |
1597 | 2.08k | rv = sslBuffer_AppendVariable(&buf, dh_p.data, dh_p.len, 2); |
1598 | 2.08k | if (rv != SECSuccess) { |
1599 | 0 | goto loser; |
1600 | 0 | } |
1601 | | /* g */ |
1602 | 2.08k | rv = sslBuffer_AppendVariable(&buf, dh_g.data, dh_g.len, 2); |
1603 | 2.08k | if (rv != SECSuccess) { |
1604 | 0 | goto loser; |
1605 | 0 | } |
1606 | | /* y - complicated by padding */ |
1607 | 2.08k | yLen = padY ? dh_p.len : dh_Ys.len; |
1608 | 2.08k | rv = sslBuffer_AppendNumber(&buf, yLen, 2); |
1609 | 2.08k | if (rv != SECSuccess) { |
1610 | 0 | goto loser; |
1611 | 0 | } |
1612 | | /* If we're padding Y, dh_Ys can't be longer than dh_p. */ |
1613 | 2.08k | PORT_Assert(!padY || dh_p.len >= dh_Ys.len); |
1614 | 2.08k | for (i = dh_Ys.len; i < yLen; ++i) { |
1615 | 0 | rv = sslBuffer_AppendNumber(&buf, 0, 1); |
1616 | 0 | if (rv != SECSuccess) { |
1617 | 0 | goto loser; |
1618 | 0 | } |
1619 | 0 | } |
1620 | 2.08k | rv = sslBuffer_Append(&buf, dh_Ys.data, dh_Ys.len); |
1621 | 2.08k | if (rv != SECSuccess) { |
1622 | 0 | goto loser; |
1623 | 0 | } |
1624 | | |
1625 | 2.08k | rv = ssl3_ComputeCommonKeyHash(hashAlg, SSL_BUFFER_BASE(&buf), |
1626 | 2.08k | SSL_BUFFER_LEN(&buf), hashes); |
1627 | 2.08k | if (rv != SECSuccess) { |
1628 | 0 | goto loser; |
1629 | 0 | } |
1630 | | |
1631 | 2.08k | PRINT_BUF(95, (NULL, "DHkey hash: ", SSL_BUFFER_BASE(&buf), |
1632 | 2.08k | SSL_BUFFER_LEN(&buf))); |
1633 | 2.08k | if (hashAlg == ssl_hash_none) { |
1634 | 1.13k | PRINT_BUF(95, (NULL, "DHkey hash: MD5 result", |
1635 | 1.13k | hashes->u.s.md5, MD5_LENGTH)); |
1636 | 1.13k | PRINT_BUF(95, (NULL, "DHkey hash: SHA1 result", |
1637 | 1.13k | hashes->u.s.sha, SHA1_LENGTH)); |
1638 | 1.13k | } else { |
1639 | 953 | PRINT_BUF(95, (NULL, "DHkey hash: result", |
1640 | 953 | hashes->u.raw, hashes->len)); |
1641 | 953 | } |
1642 | | |
1643 | 2.08k | sslBuffer_Clear(&buf); |
1644 | 2.08k | return SECSuccess; |
1645 | | |
1646 | 0 | loser: |
1647 | 0 | sslBuffer_Clear(&buf); |
1648 | 0 | return SECFailure; |
1649 | 2.08k | } |
1650 | | |
1651 | | static SECStatus |
1652 | | ssl3_SetupPendingCipherSpec(sslSocket *ss, SSLSecretDirection direction, |
1653 | | const ssl3CipherSuiteDef *suiteDef, |
1654 | | ssl3CipherSpec **specp) |
1655 | 77.4k | { |
1656 | 77.4k | ssl3CipherSpec *spec; |
1657 | 77.4k | const ssl3CipherSpec *prev; |
1658 | | |
1659 | 77.4k | prev = (direction == ssl_secret_write) ? ss->ssl3.cwSpec : ss->ssl3.crSpec; |
1660 | 77.4k | if (prev->epoch == PR_UINT16_MAX) { |
1661 | 0 | PORT_SetError(SSL_ERROR_RENEGOTIATION_NOT_ALLOWED); |
1662 | 0 | return SECFailure; |
1663 | 0 | } |
1664 | | |
1665 | 77.4k | spec = ssl_CreateCipherSpec(ss, direction); |
1666 | 77.4k | if (!spec) { |
1667 | 0 | return SECFailure; |
1668 | 0 | } |
1669 | | |
1670 | 77.4k | spec->cipherDef = ssl_GetBulkCipherDef(suiteDef); |
1671 | 77.4k | spec->macDef = ssl_GetMacDef(ss, suiteDef); |
1672 | | |
1673 | 77.4k | spec->epoch = prev->epoch + 1; |
1674 | 77.4k | spec->nextSeqNum = 0; |
1675 | 77.4k | if (IS_DTLS(ss) && direction == ssl_secret_read) { |
1676 | 0 | dtls_InitRecvdRecords(&spec->recvdRecords); |
1677 | 0 | } |
1678 | 77.4k | ssl_SetSpecVersions(ss, spec); |
1679 | | |
1680 | 77.4k | ssl_SaveCipherSpec(ss, spec); |
1681 | 77.4k | *specp = spec; |
1682 | 77.4k | return SECSuccess; |
1683 | 77.4k | } |
1684 | | |
1685 | | /* Fill in the pending cipher spec with info from the selected ciphersuite. |
1686 | | ** This is as much initialization as we can do without having key material. |
1687 | | ** Called from ssl3_HandleServerHello(), ssl3_SendServerHello() |
1688 | | ** Caller must hold the ssl3 handshake lock. |
1689 | | ** Acquires & releases SpecWriteLock. |
1690 | | */ |
1691 | | SECStatus |
1692 | | ssl3_SetupBothPendingCipherSpecs(sslSocket *ss) |
1693 | 38.7k | { |
1694 | 38.7k | ssl3CipherSuite suite = ss->ssl3.hs.cipher_suite; |
1695 | 38.7k | SSL3KeyExchangeAlgorithm kea; |
1696 | 38.7k | const ssl3CipherSuiteDef *suiteDef; |
1697 | 38.7k | SECStatus rv; |
1698 | | |
1699 | 38.7k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
1700 | 38.7k | PORT_Assert(ss->version < SSL_LIBRARY_VERSION_TLS_1_3); |
1701 | | |
1702 | 38.7k | ssl_GetSpecWriteLock(ss); /*******************************/ |
1703 | | |
1704 | | /* This hack provides maximal interoperability with SSL 3 servers. */ |
1705 | 38.7k | if (ss->ssl3.cwSpec->macDef->mac == ssl_mac_null) { |
1706 | | /* SSL records are not being MACed. */ |
1707 | 7.99k | ss->ssl3.cwSpec->version = ss->version; |
1708 | 7.99k | } |
1709 | | |
1710 | 38.7k | SSL_TRC(3, ("%d: SSL3[%d]: Set XXX Pending Cipher Suite to 0x%04x", |
1711 | 38.7k | SSL_GETPID(), ss->fd, suite)); |
1712 | | |
1713 | 38.7k | suiteDef = ssl_LookupCipherSuiteDef(suite); |
1714 | 38.7k | if (suiteDef == NULL) { |
1715 | 0 | goto loser; |
1716 | 0 | } |
1717 | | |
1718 | 38.7k | if (IS_DTLS(ss)) { |
1719 | | /* Double-check that we did not pick an RC4 suite */ |
1720 | 0 | PORT_Assert(suiteDef->bulk_cipher_alg != cipher_rc4); |
1721 | 0 | } |
1722 | | |
1723 | 38.7k | ss->ssl3.hs.suite_def = suiteDef; |
1724 | | |
1725 | 38.7k | kea = suiteDef->key_exchange_alg; |
1726 | 38.7k | ss->ssl3.hs.kea_def = &kea_defs[kea]; |
1727 | 38.7k | PORT_Assert(ss->ssl3.hs.kea_def->kea == kea); |
1728 | | |
1729 | 38.7k | rv = ssl3_SetupPendingCipherSpec(ss, ssl_secret_read, suiteDef, |
1730 | 38.7k | &ss->ssl3.prSpec); |
1731 | 38.7k | if (rv != SECSuccess) { |
1732 | 0 | goto loser; |
1733 | 0 | } |
1734 | 38.7k | rv = ssl3_SetupPendingCipherSpec(ss, ssl_secret_write, suiteDef, |
1735 | 38.7k | &ss->ssl3.pwSpec); |
1736 | 38.7k | if (rv != SECSuccess) { |
1737 | 0 | goto loser; |
1738 | 0 | } |
1739 | | |
1740 | 38.7k | if (ssl3_ExtensionNegotiated(ss, ssl_record_size_limit_xtn)) { |
1741 | 1.30k | ss->ssl3.prSpec->recordSizeLimit = PR_MIN(MAX_FRAGMENT_LENGTH, |
1742 | 1.30k | ss->opt.recordSizeLimit); |
1743 | 1.30k | ss->ssl3.pwSpec->recordSizeLimit = PR_MIN(MAX_FRAGMENT_LENGTH, |
1744 | 1.30k | ss->xtnData.recordSizeLimit); |
1745 | 1.30k | } |
1746 | | |
1747 | 38.7k | ssl_ReleaseSpecWriteLock(ss); /*******************************/ |
1748 | 38.7k | return SECSuccess; |
1749 | | |
1750 | 0 | loser: |
1751 | 0 | ssl_ReleaseSpecWriteLock(ss); |
1752 | 0 | return SECFailure; |
1753 | 38.7k | } |
1754 | | |
1755 | | /* ssl3_BuildRecordPseudoHeader writes the SSL/TLS pseudo-header (the data which |
1756 | | * is included in the MAC or AEAD additional data) to |buf|. See |
1757 | | * https://tools.ietf.org/html/rfc5246#section-6.2.3.3 for the definition of the |
1758 | | * AEAD additional data. |
1759 | | * |
1760 | | * TLS pseudo-header includes the record's version field, SSL's doesn't. Which |
1761 | | * pseudo-header definition to use should be decided based on the version of |
1762 | | * the protocol that was negotiated when the cipher spec became current, NOT |
1763 | | * based on the version value in the record itself, and the decision is passed |
1764 | | * to this function as the |includesVersion| argument. But, the |version| |
1765 | | * argument should be the record's version value. |
1766 | | */ |
1767 | | static SECStatus |
1768 | | ssl3_BuildRecordPseudoHeader(DTLSEpoch epoch, |
1769 | | sslSequenceNumber seqNum, |
1770 | | SSLContentType ct, |
1771 | | PRBool includesVersion, |
1772 | | SSL3ProtocolVersion version, |
1773 | | PRBool isDTLS, |
1774 | | int length, |
1775 | | sslBuffer *buf, SSL3ProtocolVersion v) |
1776 | 0 | { |
1777 | 0 | SECStatus rv; |
1778 | 0 | if (isDTLS && v < SSL_LIBRARY_VERSION_TLS_1_3) { |
1779 | 0 | rv = sslBuffer_AppendNumber(buf, epoch, 2); |
1780 | 0 | if (rv != SECSuccess) { |
1781 | 0 | return SECFailure; |
1782 | 0 | } |
1783 | 0 | rv = sslBuffer_AppendNumber(buf, seqNum, 6); |
1784 | 0 | } else { |
1785 | 0 | rv = sslBuffer_AppendNumber(buf, seqNum, 8); |
1786 | 0 | } |
1787 | 0 | if (rv != SECSuccess) { |
1788 | 0 | return SECFailure; |
1789 | 0 | } |
1790 | 0 | rv = sslBuffer_AppendNumber(buf, ct, 1); |
1791 | 0 | if (rv != SECSuccess) { |
1792 | 0 | return SECFailure; |
1793 | 0 | } |
1794 | | |
1795 | | /* SSL3 MAC doesn't include the record's version field. */ |
1796 | 0 | if (includesVersion) { |
1797 | | /* TLS MAC and AEAD additional data include version. */ |
1798 | 0 | rv = sslBuffer_AppendNumber(buf, version, 2); |
1799 | 0 | if (rv != SECSuccess) { |
1800 | 0 | return SECFailure; |
1801 | 0 | } |
1802 | 0 | } |
1803 | 0 | rv = sslBuffer_AppendNumber(buf, length, 2); |
1804 | 0 | if (rv != SECSuccess) { |
1805 | 0 | return SECFailure; |
1806 | 0 | } |
1807 | | |
1808 | 0 | return SECSuccess; |
1809 | 0 | } |
1810 | | |
1811 | | /* Initialize encryption and MAC contexts for pending spec. |
1812 | | * Master Secret already is derived. |
1813 | | * Caller holds Spec write lock. |
1814 | | */ |
1815 | | static SECStatus |
1816 | | ssl3_InitPendingContexts(sslSocket *ss, ssl3CipherSpec *spec) |
1817 | 66.5k | { |
1818 | 66.5k | CK_MECHANISM_TYPE encMechanism; |
1819 | 66.5k | CK_ATTRIBUTE_TYPE encMode; |
1820 | 66.5k | SECItem macParam; |
1821 | 66.5k | CK_ULONG macLength; |
1822 | 66.5k | SECItem iv; |
1823 | 66.5k | SSLCipherAlgorithm calg; |
1824 | | |
1825 | 66.5k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
1826 | 66.5k | PORT_Assert(ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); |
1827 | | |
1828 | 66.5k | calg = spec->cipherDef->calg; |
1829 | 66.5k | PORT_Assert(alg2Mech[calg].calg == calg); |
1830 | | |
1831 | 66.5k | if (spec->cipherDef->type != type_aead) { |
1832 | 63.6k | macLength = spec->macDef->mac_size; |
1833 | | |
1834 | | /* |
1835 | | ** Now setup the MAC contexts, |
1836 | | ** crypto contexts are setup below. |
1837 | | */ |
1838 | 63.6k | macParam.data = (unsigned char *)&macLength; |
1839 | 63.6k | macParam.len = sizeof(macLength); |
1840 | 63.6k | macParam.type = siBuffer; |
1841 | | |
1842 | 63.6k | spec->keyMaterial.macContext = PK11_CreateContextBySymKey( |
1843 | 63.6k | spec->macDef->mmech, CKA_SIGN, spec->keyMaterial.macKey, &macParam); |
1844 | 63.6k | if (!spec->keyMaterial.macContext) { |
1845 | 0 | ssl_MapLowLevelError(SSL_ERROR_SYM_KEY_CONTEXT_FAILURE); |
1846 | 0 | return SECFailure; |
1847 | 0 | } |
1848 | 63.6k | } |
1849 | | |
1850 | | /* |
1851 | | ** Now setup the crypto contexts. |
1852 | | */ |
1853 | 66.5k | if (calg == ssl_calg_null) { |
1854 | 6.49k | spec->cipher = Null_Cipher; |
1855 | 6.49k | return SECSuccess; |
1856 | 6.49k | } |
1857 | | |
1858 | 60.0k | encMechanism = ssl3_Alg2Mech(calg); |
1859 | 60.0k | encMode = (spec->direction == ssl_secret_write) ? CKA_ENCRYPT : CKA_DECRYPT; |
1860 | 60.0k | if (spec->cipherDef->type == type_aead) { |
1861 | 2.82k | encMode |= CKA_NSS_MESSAGE; |
1862 | 2.82k | iv.data = NULL; |
1863 | 2.82k | iv.len = 0; |
1864 | 57.1k | } else { |
1865 | 57.1k | spec->cipher = SSLCipher_PK11_CipherOp; |
1866 | 57.1k | iv.data = spec->keyMaterial.iv; |
1867 | 57.1k | iv.len = spec->cipherDef->iv_size; |
1868 | 57.1k | } |
1869 | | |
1870 | | /* |
1871 | | * build the context |
1872 | | */ |
1873 | 60.0k | spec->cipherContext = PK11_CreateContextBySymKey(encMechanism, encMode, |
1874 | 60.0k | spec->keyMaterial.key, |
1875 | 60.0k | &iv); |
1876 | 60.0k | if (!spec->cipherContext) { |
1877 | 0 | ssl_MapLowLevelError(SSL_ERROR_SYM_KEY_CONTEXT_FAILURE); |
1878 | 0 | return SECFailure; |
1879 | 0 | } |
1880 | | |
1881 | 60.0k | return SECSuccess; |
1882 | 60.0k | } |
1883 | | |
1884 | | /* Complete the initialization of all keys, ciphers, MACs and their contexts |
1885 | | * for the pending Cipher Spec. |
1886 | | * Called from: ssl3_SendClientKeyExchange (for Full handshake) |
1887 | | * ssl3_HandleRSAClientKeyExchange (for Full handshake) |
1888 | | * ssl3_HandleServerHello (for session restart) |
1889 | | * ssl3_HandleClientHello (for session restart) |
1890 | | * Sets error code, but caller probably should override to disambiguate. |
1891 | | * |
1892 | | * If |secret| is a master secret from a previous connection is reused, |derive| |
1893 | | * is PR_FALSE. If the secret is a pre-master secret, then |derive| is PR_TRUE |
1894 | | * and the master secret is derived from |secret|. |
1895 | | */ |
1896 | | SECStatus |
1897 | | ssl3_InitPendingCipherSpecs(sslSocket *ss, PK11SymKey *secret, PRBool derive) |
1898 | 33.2k | { |
1899 | 33.2k | PK11SymKey *masterSecret; |
1900 | 33.2k | ssl3CipherSpec *pwSpec; |
1901 | 33.2k | ssl3CipherSpec *prSpec; |
1902 | 33.2k | SECStatus rv; |
1903 | | |
1904 | 33.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
1905 | 33.2k | PORT_Assert(secret); |
1906 | | |
1907 | 33.2k | ssl_GetSpecWriteLock(ss); /**************************************/ |
1908 | | |
1909 | 33.2k | PORT_Assert(ss->ssl3.pwSpec); |
1910 | 33.2k | PORT_Assert(ss->ssl3.cwSpec->epoch == ss->ssl3.crSpec->epoch); |
1911 | 33.2k | prSpec = ss->ssl3.prSpec; |
1912 | 33.2k | pwSpec = ss->ssl3.pwSpec; |
1913 | | |
1914 | 33.2k | if (ss->ssl3.cwSpec->epoch == PR_UINT16_MAX) { |
1915 | | /* The problem here is that we have rehandshaked too many |
1916 | | * times (you are not allowed to wrap the epoch). The |
1917 | | * spec says you should be discarding the connection |
1918 | | * and start over, so not much we can do here. */ |
1919 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1920 | 0 | goto loser; |
1921 | 0 | } |
1922 | | |
1923 | 33.2k | if (derive) { |
1924 | 33.2k | rv = ssl3_ComputeMasterSecret(ss, secret, &masterSecret); |
1925 | 33.2k | if (rv != SECSuccess) { |
1926 | 0 | goto loser; |
1927 | 0 | } |
1928 | 33.2k | } else { |
1929 | 0 | masterSecret = secret; |
1930 | 0 | } |
1931 | | |
1932 | 33.2k | PORT_Assert(masterSecret); |
1933 | 33.2k | rv = ssl3_DeriveConnectionKeys(ss, masterSecret); |
1934 | 33.2k | if (rv != SECSuccess) { |
1935 | 0 | if (derive) { |
1936 | | /* masterSecret was created here. */ |
1937 | 0 | PK11_FreeSymKey(masterSecret); |
1938 | 0 | } |
1939 | 0 | goto loser; |
1940 | 0 | } |
1941 | | |
1942 | | /* Both cipher specs maintain a reference to the master secret, since each |
1943 | | * is managed and freed independently. */ |
1944 | 33.2k | prSpec->masterSecret = masterSecret; |
1945 | 33.2k | pwSpec->masterSecret = PK11_ReferenceSymKey(masterSecret); |
1946 | 33.2k | rv = ssl3_InitPendingContexts(ss, ss->ssl3.prSpec); |
1947 | 33.2k | if (rv != SECSuccess) { |
1948 | 0 | goto loser; |
1949 | 0 | } |
1950 | | |
1951 | 33.2k | rv = ssl3_InitPendingContexts(ss, ss->ssl3.pwSpec); |
1952 | 33.2k | if (rv != SECSuccess) { |
1953 | 0 | goto loser; |
1954 | 0 | } |
1955 | | |
1956 | 33.2k | ssl_ReleaseSpecWriteLock(ss); /******************************/ |
1957 | 33.2k | return SECSuccess; |
1958 | | |
1959 | 0 | loser: |
1960 | 0 | ssl_ReleaseSpecWriteLock(ss); /******************************/ |
1961 | 0 | ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE); |
1962 | 0 | return SECFailure; |
1963 | 33.2k | } |
1964 | | |
1965 | | /* |
1966 | | * 60 bytes is 3 times the maximum length MAC size that is supported. |
1967 | | */ |
1968 | | static const unsigned char mac_pad_1[60] = { |
1969 | | 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, |
1970 | | 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, |
1971 | | 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, |
1972 | | 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, |
1973 | | 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, |
1974 | | 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, |
1975 | | 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, |
1976 | | 0x36, 0x36, 0x36, 0x36 |
1977 | | }; |
1978 | | static const unsigned char mac_pad_2[60] = { |
1979 | | 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, |
1980 | | 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, |
1981 | | 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, |
1982 | | 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, |
1983 | | 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, |
1984 | | 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, |
1985 | | 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, |
1986 | | 0x5c, 0x5c, 0x5c, 0x5c |
1987 | | }; |
1988 | | |
1989 | | /* Called from: ssl3_SendRecord() |
1990 | | ** Caller must already hold the SpecReadLock. (wish we could assert that!) |
1991 | | */ |
1992 | | static SECStatus |
1993 | | ssl3_ComputeRecordMAC( |
1994 | | ssl3CipherSpec *spec, |
1995 | | const unsigned char *header, |
1996 | | unsigned int headerLen, |
1997 | | const PRUint8 *input, |
1998 | | int inputLen, |
1999 | | unsigned char *outbuf, |
2000 | | unsigned int *outLen) |
2001 | 0 | { |
2002 | 0 | PK11Context *context; |
2003 | 0 | int macSize = spec->macDef->mac_size; |
2004 | 0 | SECStatus rv; |
2005 | |
|
2006 | 0 | PRINT_BUF(95, (NULL, "frag hash1: header", header, headerLen)); |
2007 | 0 | PRINT_BUF(95, (NULL, "frag hash1: input", input, inputLen)); |
2008 | |
|
2009 | 0 | if (spec->macDef->mac == ssl_mac_null) { |
2010 | 0 | *outLen = 0; |
2011 | 0 | return SECSuccess; |
2012 | 0 | } |
2013 | | |
2014 | 0 | context = spec->keyMaterial.macContext; |
2015 | 0 | rv = PK11_DigestBegin(context); |
2016 | 0 | rv |= PK11_DigestOp(context, header, headerLen); |
2017 | 0 | rv |= PK11_DigestOp(context, input, inputLen); |
2018 | 0 | rv |= PK11_DigestFinal(context, outbuf, outLen, macSize); |
2019 | 0 | PORT_Assert(rv != SECSuccess || *outLen == (unsigned)macSize); |
2020 | |
|
2021 | 0 | PRINT_BUF(95, (NULL, "frag hash2: result", outbuf, *outLen)); |
2022 | |
|
2023 | 0 | if (rv != SECSuccess) { |
2024 | 0 | rv = SECFailure; |
2025 | 0 | ssl_MapLowLevelError(SSL_ERROR_MAC_COMPUTATION_FAILURE); |
2026 | 0 | } |
2027 | 0 | return rv; |
2028 | 0 | } |
2029 | | |
2030 | | /* Called from: ssl3_HandleRecord() |
2031 | | * Caller must already hold the SpecReadLock. (wish we could assert that!) |
2032 | | * |
2033 | | * On entry: |
2034 | | * originalLen >= inputLen >= MAC size |
2035 | | */ |
2036 | | static SECStatus |
2037 | | ssl3_ComputeRecordMACConstantTime( |
2038 | | ssl3CipherSpec *spec, |
2039 | | const unsigned char *header, |
2040 | | unsigned int headerLen, |
2041 | | const PRUint8 *input, |
2042 | | int inputLen, |
2043 | | int originalLen, |
2044 | | unsigned char *outbuf, |
2045 | | unsigned int *outLen) |
2046 | 0 | { |
2047 | 0 | CK_MECHANISM_TYPE macType; |
2048 | 0 | CK_NSS_MAC_CONSTANT_TIME_PARAMS params; |
2049 | 0 | SECItem param, inputItem, outputItem; |
2050 | 0 | int macSize = spec->macDef->mac_size; |
2051 | 0 | SECStatus rv; |
2052 | 0 |
|
2053 | 0 | PORT_Assert(inputLen >= spec->macDef->mac_size); |
2054 | 0 | PORT_Assert(originalLen >= inputLen); |
2055 | 0 |
|
2056 | 0 | if (spec->macDef->mac == ssl_mac_null) { |
2057 | 0 | *outLen = 0; |
2058 | 0 | return SECSuccess; |
2059 | 0 | } |
2060 | 0 |
|
2061 | 0 | macType = CKM_NSS_HMAC_CONSTANT_TIME; |
2062 | 0 | if (spec->version == SSL_LIBRARY_VERSION_3_0) { |
2063 | 0 | macType = CKM_NSS_SSL3_MAC_CONSTANT_TIME; |
2064 | 0 | } |
2065 | 0 |
|
2066 | 0 | params.macAlg = spec->macDef->mmech; |
2067 | 0 | params.ulBodyTotalLen = originalLen; |
2068 | 0 | params.pHeader = (unsigned char *)header; /* const cast */ |
2069 | 0 | params.ulHeaderLen = headerLen; |
2070 | 0 |
|
2071 | 0 | param.data = (unsigned char *)¶ms; |
2072 | 0 | param.len = sizeof(params); |
2073 | 0 | param.type = 0; |
2074 | 0 |
|
2075 | 0 | inputItem.data = (unsigned char *)input; |
2076 | 0 | inputItem.len = inputLen; |
2077 | 0 | inputItem.type = 0; |
2078 | 0 |
|
2079 | 0 | outputItem.data = outbuf; |
2080 | 0 | outputItem.len = *outLen; |
2081 | 0 | outputItem.type = 0; |
2082 | 0 |
|
2083 | 0 | rv = PK11_SignWithSymKey(spec->keyMaterial.macKey, macType, ¶m, |
2084 | 0 | &outputItem, &inputItem); |
2085 | 0 | if (rv != SECSuccess) { |
2086 | 0 | if (PORT_GetError() == SEC_ERROR_INVALID_ALGORITHM) { |
2087 | 0 | /* ssl3_ComputeRecordMAC() expects the MAC to have been removed |
2088 | 0 | * from the input length already. */ |
2089 | 0 | return ssl3_ComputeRecordMAC(spec, header, headerLen, |
2090 | 0 | input, inputLen - macSize, |
2091 | 0 | outbuf, outLen); |
2092 | 0 | } |
2093 | 0 |
|
2094 | 0 | *outLen = 0; |
2095 | 0 | rv = SECFailure; |
2096 | 0 | ssl_MapLowLevelError(SSL_ERROR_MAC_COMPUTATION_FAILURE); |
2097 | 0 | return rv; |
2098 | 0 | } |
2099 | 0 |
|
2100 | 0 | PORT_Assert(outputItem.len == (unsigned)macSize); |
2101 | 0 | *outLen = outputItem.len; |
2102 | 0 |
|
2103 | 0 | return rv; |
2104 | 0 | } |
2105 | | |
2106 | | static PRBool |
2107 | | ssl3_ClientAuthTokenPresent(sslSessionID *sid) |
2108 | 561k | { |
2109 | 561k | PK11SlotInfo *slot = NULL; |
2110 | 561k | PRBool isPresent = PR_TRUE; |
2111 | | |
2112 | | /* we only care if we are doing client auth */ |
2113 | 561k | if (!sid || !sid->u.ssl3.clAuthValid) { |
2114 | 561k | return PR_TRUE; |
2115 | 561k | } |
2116 | | |
2117 | | /* get the slot */ |
2118 | 0 | slot = SECMOD_LookupSlot(sid->u.ssl3.clAuthModuleID, |
2119 | 0 | sid->u.ssl3.clAuthSlotID); |
2120 | 0 | if (slot == NULL || |
2121 | 0 | !PK11_IsPresent(slot) || |
2122 | 0 | sid->u.ssl3.clAuthSeries != PK11_GetSlotSeries(slot) || |
2123 | 0 | sid->u.ssl3.clAuthSlotID != PK11_GetSlotID(slot) || |
2124 | 0 | sid->u.ssl3.clAuthModuleID != PK11_GetModuleID(slot) || |
2125 | 0 | (PK11_NeedLogin(slot) && !PK11_IsLoggedIn(slot, NULL))) { |
2126 | 0 | isPresent = PR_FALSE; |
2127 | 0 | } |
2128 | 0 | if (slot) { |
2129 | 0 | PK11_FreeSlot(slot); |
2130 | 0 | } |
2131 | 0 | return isPresent; |
2132 | 561k | } |
2133 | | |
2134 | | /* Caller must hold the spec read lock. */ |
2135 | | SECStatus |
2136 | | ssl3_MACEncryptRecord(ssl3CipherSpec *cwSpec, |
2137 | | PRBool isServer, |
2138 | | PRBool isDTLS, |
2139 | | SSLContentType ct, |
2140 | | const PRUint8 *pIn, |
2141 | | PRUint32 contentLen, |
2142 | | sslBuffer *wrBuf) |
2143 | 0 | { |
2144 | 0 | SECStatus rv; |
2145 | 0 | PRUint32 macLen = 0; |
2146 | 0 | PRUint32 fragLen; |
2147 | 0 | PRUint32 p1Len, p2Len, oddLen = 0; |
2148 | 0 | unsigned int ivLen = 0; |
2149 | 0 | unsigned char pseudoHeaderBuf[13]; |
2150 | 0 | sslBuffer pseudoHeader = SSL_BUFFER(pseudoHeaderBuf); |
2151 | 0 | unsigned int len; |
2152 | |
|
2153 | 0 | if (cwSpec->cipherDef->type == type_block && |
2154 | 0 | cwSpec->version >= SSL_LIBRARY_VERSION_TLS_1_1) { |
2155 | | /* Prepend the per-record explicit IV using technique 2b from |
2156 | | * RFC 4346 section 6.2.3.2: The IV is a cryptographically |
2157 | | * strong random number XORed with the CBC residue from the previous |
2158 | | * record. |
2159 | | */ |
2160 | 0 | ivLen = cwSpec->cipherDef->iv_size; |
2161 | 0 | if (ivLen > SSL_BUFFER_SPACE(wrBuf)) { |
2162 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
2163 | 0 | return SECFailure; |
2164 | 0 | } |
2165 | 0 | rv = PK11_GenerateRandom(SSL_BUFFER_NEXT(wrBuf), ivLen); |
2166 | 0 | if (rv != SECSuccess) { |
2167 | 0 | ssl_MapLowLevelError(SSL_ERROR_GENERATE_RANDOM_FAILURE); |
2168 | 0 | return rv; |
2169 | 0 | } |
2170 | 0 | rv = cwSpec->cipher(cwSpec->cipherContext, |
2171 | 0 | SSL_BUFFER_NEXT(wrBuf), /* output */ |
2172 | 0 | &len, /* outlen */ |
2173 | 0 | ivLen, /* max outlen */ |
2174 | 0 | SSL_BUFFER_NEXT(wrBuf), /* input */ |
2175 | 0 | ivLen); /* input len */ |
2176 | 0 | if (rv != SECSuccess || len != ivLen) { |
2177 | 0 | PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE); |
2178 | 0 | return SECFailure; |
2179 | 0 | } |
2180 | | |
2181 | 0 | rv = sslBuffer_Skip(wrBuf, len, NULL); |
2182 | 0 | PORT_Assert(rv == SECSuccess); /* Can't fail. */ |
2183 | 0 | } |
2184 | 0 | rv = ssl3_BuildRecordPseudoHeader( |
2185 | 0 | cwSpec->epoch, cwSpec->nextSeqNum, ct, |
2186 | 0 | cwSpec->version >= SSL_LIBRARY_VERSION_TLS_1_0, cwSpec->recordVersion, |
2187 | 0 | isDTLS, contentLen, &pseudoHeader, cwSpec->version); |
2188 | 0 | PORT_Assert(rv == SECSuccess); |
2189 | 0 | if (cwSpec->cipherDef->type == type_aead) { |
2190 | 0 | const unsigned int nonceLen = cwSpec->cipherDef->explicit_nonce_size; |
2191 | 0 | const unsigned int tagLen = cwSpec->cipherDef->tag_size; |
2192 | 0 | unsigned int ivOffset = 0; |
2193 | 0 | CK_GENERATOR_FUNCTION gen; |
2194 | | /* ivOut includes the iv and the nonce and is the internal iv/nonce |
2195 | | * for the AEAD function. On Encrypt, this is an in/out parameter */ |
2196 | 0 | unsigned char ivOut[MAX_IV_LENGTH]; |
2197 | 0 | ivLen = cwSpec->cipherDef->iv_size; |
2198 | |
|
2199 | 0 | PORT_Assert((ivLen + nonceLen) <= MAX_IV_LENGTH); |
2200 | 0 | PORT_Assert((ivLen + nonceLen) >= sizeof(sslSequenceNumber)); |
2201 | |
|
2202 | 0 | if (nonceLen + contentLen + tagLen > SSL_BUFFER_SPACE(wrBuf)) { |
2203 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
2204 | 0 | return SECFailure; |
2205 | 0 | } |
2206 | | |
2207 | 0 | if (nonceLen == 0) { |
2208 | 0 | ivOffset = ivLen - sizeof(sslSequenceNumber); |
2209 | 0 | gen = CKG_GENERATE_COUNTER_XOR; |
2210 | 0 | } else { |
2211 | 0 | ivOffset = ivLen; |
2212 | 0 | gen = CKG_GENERATE_COUNTER; |
2213 | 0 | } |
2214 | 0 | ivOffset = tls13_SetupAeadIv(isDTLS, cwSpec->version, ivOut, cwSpec->keyMaterial.iv, |
2215 | 0 | ivOffset, ivLen, cwSpec->epoch); |
2216 | 0 | rv = tls13_AEAD(cwSpec->cipherContext, |
2217 | 0 | PR_FALSE, |
2218 | 0 | gen, ivOffset * BPB, /* iv generator params */ |
2219 | 0 | ivOut, /* iv in */ |
2220 | 0 | ivOut, /* iv out */ |
2221 | 0 | ivLen + nonceLen, /* full iv length */ |
2222 | 0 | NULL, 0, /* nonce is generated*/ |
2223 | 0 | SSL_BUFFER_BASE(&pseudoHeader), /* aad */ |
2224 | 0 | SSL_BUFFER_LEN(&pseudoHeader), /* aadlen */ |
2225 | 0 | SSL_BUFFER_NEXT(wrBuf) + nonceLen, /* output */ |
2226 | 0 | &len, /* out len */ |
2227 | 0 | SSL_BUFFER_SPACE(wrBuf) - nonceLen, /* max out */ |
2228 | 0 | tagLen, |
2229 | 0 | pIn, contentLen); /* input */ |
2230 | 0 | if (rv != SECSuccess) { |
2231 | 0 | PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE); |
2232 | 0 | return SECFailure; |
2233 | 0 | } |
2234 | 0 | len += nonceLen; /* include the nonce at the beginning */ |
2235 | | /* copy out the generated iv if we are using explict nonces */ |
2236 | 0 | if (nonceLen) { |
2237 | 0 | PORT_Memcpy(SSL_BUFFER_NEXT(wrBuf), ivOut + ivLen, nonceLen); |
2238 | 0 | } |
2239 | |
|
2240 | 0 | rv = sslBuffer_Skip(wrBuf, len, NULL); |
2241 | 0 | PORT_Assert(rv == SECSuccess); /* Can't fail. */ |
2242 | 0 | } else { |
2243 | 0 | int blockSize = cwSpec->cipherDef->block_size; |
2244 | | |
2245 | | /* |
2246 | | * Add the MAC |
2247 | | */ |
2248 | 0 | rv = ssl3_ComputeRecordMAC(cwSpec, SSL_BUFFER_BASE(&pseudoHeader), |
2249 | 0 | SSL_BUFFER_LEN(&pseudoHeader), |
2250 | 0 | pIn, contentLen, |
2251 | 0 | SSL_BUFFER_NEXT(wrBuf) + contentLen, &macLen); |
2252 | 0 | if (rv != SECSuccess) { |
2253 | 0 | ssl_MapLowLevelError(SSL_ERROR_MAC_COMPUTATION_FAILURE); |
2254 | 0 | return SECFailure; |
2255 | 0 | } |
2256 | 0 | p1Len = contentLen; |
2257 | 0 | p2Len = macLen; |
2258 | 0 | fragLen = contentLen + macLen; /* needs to be encrypted */ |
2259 | 0 | PORT_Assert(fragLen <= MAX_FRAGMENT_LENGTH + 1024); |
2260 | | |
2261 | | /* |
2262 | | * Pad the text (if we're doing a block cipher) |
2263 | | * then Encrypt it |
2264 | | */ |
2265 | 0 | if (cwSpec->cipherDef->type == type_block) { |
2266 | 0 | unsigned char *pBuf; |
2267 | 0 | int padding_length; |
2268 | 0 | int i; |
2269 | |
|
2270 | 0 | oddLen = contentLen % blockSize; |
2271 | | /* Assume blockSize is a power of two */ |
2272 | 0 | padding_length = blockSize - 1 - ((fragLen) & (blockSize - 1)); |
2273 | 0 | fragLen += padding_length + 1; |
2274 | 0 | PORT_Assert((fragLen % blockSize) == 0); |
2275 | | |
2276 | | /* Pad according to TLS rules (also acceptable to SSL3). */ |
2277 | 0 | pBuf = SSL_BUFFER_NEXT(wrBuf) + fragLen - 1; |
2278 | 0 | for (i = padding_length + 1; i > 0; --i) { |
2279 | 0 | *pBuf-- = padding_length; |
2280 | 0 | } |
2281 | | /* now, if contentLen is not a multiple of block size, fix it */ |
2282 | 0 | p2Len = fragLen - p1Len; |
2283 | 0 | } |
2284 | 0 | if (p1Len < 256) { |
2285 | 0 | oddLen = p1Len; |
2286 | 0 | p1Len = 0; |
2287 | 0 | } else { |
2288 | 0 | p1Len -= oddLen; |
2289 | 0 | } |
2290 | 0 | if (oddLen) { |
2291 | 0 | p2Len += oddLen; |
2292 | 0 | PORT_Assert((blockSize < 2) || |
2293 | 0 | (p2Len % blockSize) == 0); |
2294 | 0 | memmove(SSL_BUFFER_NEXT(wrBuf) + p1Len, pIn + p1Len, oddLen); |
2295 | 0 | } |
2296 | 0 | if (p1Len > 0) { |
2297 | 0 | unsigned int cipherBytesPart1 = 0; |
2298 | 0 | rv = cwSpec->cipher(cwSpec->cipherContext, |
2299 | 0 | SSL_BUFFER_NEXT(wrBuf), /* output */ |
2300 | 0 | &cipherBytesPart1, /* actual outlen */ |
2301 | 0 | p1Len, /* max outlen */ |
2302 | 0 | pIn, |
2303 | 0 | p1Len); /* input, and inputlen */ |
2304 | 0 | PORT_Assert(rv == SECSuccess && cipherBytesPart1 == p1Len); |
2305 | 0 | if (rv != SECSuccess || cipherBytesPart1 != p1Len) { |
2306 | 0 | PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE); |
2307 | 0 | return SECFailure; |
2308 | 0 | } |
2309 | 0 | rv = sslBuffer_Skip(wrBuf, p1Len, NULL); |
2310 | 0 | PORT_Assert(rv == SECSuccess); |
2311 | 0 | } |
2312 | 0 | if (p2Len > 0) { |
2313 | 0 | unsigned int cipherBytesPart2 = 0; |
2314 | 0 | rv = cwSpec->cipher(cwSpec->cipherContext, |
2315 | 0 | SSL_BUFFER_NEXT(wrBuf), |
2316 | 0 | &cipherBytesPart2, /* output and actual outLen */ |
2317 | 0 | p2Len, /* max outlen */ |
2318 | 0 | SSL_BUFFER_NEXT(wrBuf), |
2319 | 0 | p2Len); /* input and inputLen*/ |
2320 | 0 | PORT_Assert(rv == SECSuccess && cipherBytesPart2 == p2Len); |
2321 | 0 | if (rv != SECSuccess || cipherBytesPart2 != p2Len) { |
2322 | 0 | PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE); |
2323 | 0 | return SECFailure; |
2324 | 0 | } |
2325 | 0 | rv = sslBuffer_Skip(wrBuf, p2Len, NULL); |
2326 | 0 | PORT_Assert(rv == SECSuccess); |
2327 | 0 | } |
2328 | 0 | } |
2329 | | |
2330 | 0 | return SECSuccess; |
2331 | 0 | } |
2332 | | |
2333 | | /* Note: though this can report failure, it shouldn't. */ |
2334 | | SECStatus |
2335 | | ssl_InsertRecordHeader(const sslSocket *ss, ssl3CipherSpec *cwSpec, |
2336 | | SSLContentType contentType, sslBuffer *wrBuf, |
2337 | | PRBool *needsLength) |
2338 | 156k | { |
2339 | 156k | SECStatus rv; |
2340 | | |
2341 | | #ifndef UNSAFE_FUZZER_MODE |
2342 | | if (cwSpec->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
2343 | | cwSpec->epoch > TrafficKeyClearText) { |
2344 | | if (IS_DTLS(ss)) { |
2345 | | return dtls13_InsertCipherTextHeader(ss, cwSpec, wrBuf, |
2346 | | needsLength); |
2347 | | } |
2348 | | contentType = ssl_ct_application_data; |
2349 | | } |
2350 | | #endif |
2351 | 156k | rv = sslBuffer_AppendNumber(wrBuf, contentType, 1); |
2352 | 156k | if (rv != SECSuccess) { |
2353 | 0 | return SECFailure; |
2354 | 0 | } |
2355 | | |
2356 | 156k | rv = sslBuffer_AppendNumber(wrBuf, cwSpec->recordVersion, 2); |
2357 | 156k | if (rv != SECSuccess) { |
2358 | 0 | return SECFailure; |
2359 | 0 | } |
2360 | 156k | if (IS_DTLS(ss)) { |
2361 | 0 | rv = sslBuffer_AppendNumber(wrBuf, cwSpec->epoch, 2); |
2362 | 0 | if (rv != SECSuccess) { |
2363 | 0 | return SECFailure; |
2364 | 0 | } |
2365 | 0 | rv = sslBuffer_AppendNumber(wrBuf, cwSpec->nextSeqNum, 6); |
2366 | 0 | if (rv != SECSuccess) { |
2367 | 0 | return SECFailure; |
2368 | 0 | } |
2369 | 0 | } |
2370 | 156k | *needsLength = PR_TRUE; |
2371 | 156k | return SECSuccess; |
2372 | 156k | } |
2373 | | |
2374 | | SECStatus |
2375 | | ssl_ProtectRecord(sslSocket *ss, ssl3CipherSpec *cwSpec, SSLContentType ct, |
2376 | | const PRUint8 *pIn, PRUint32 contentLen, sslBuffer *wrBuf) |
2377 | 156k | { |
2378 | 156k | PRBool needsLength; |
2379 | 156k | unsigned int lenOffset; |
2380 | 156k | SECStatus rv; |
2381 | | |
2382 | 156k | PORT_Assert(cwSpec->direction == ssl_secret_write); |
2383 | 156k | PORT_Assert(SSL_BUFFER_LEN(wrBuf) == 0); |
2384 | 156k | PORT_Assert(cwSpec->cipherDef->max_records <= RECORD_SEQ_MAX); |
2385 | | |
2386 | 156k | if (cwSpec->nextSeqNum >= cwSpec->cipherDef->max_records) { |
2387 | 0 | SSL_TRC(3, ("%d: SSL[-]: write sequence number at limit 0x%0llx", |
2388 | 0 | SSL_GETPID(), cwSpec->nextSeqNum)); |
2389 | 0 | PORT_SetError(SSL_ERROR_TOO_MANY_RECORDS); |
2390 | 0 | return SECFailure; |
2391 | 0 | } |
2392 | | |
2393 | 156k | rv = ssl_InsertRecordHeader(ss, cwSpec, ct, wrBuf, &needsLength); |
2394 | 156k | if (rv != SECSuccess) { |
2395 | 0 | return SECFailure; |
2396 | 0 | } |
2397 | 156k | if (needsLength) { |
2398 | 156k | rv = sslBuffer_Skip(wrBuf, 2, &lenOffset); |
2399 | 156k | if (rv != SECSuccess) { |
2400 | 0 | return SECFailure; |
2401 | 0 | } |
2402 | 156k | } |
2403 | | |
2404 | 156k | #ifdef UNSAFE_FUZZER_MODE |
2405 | 156k | { |
2406 | 156k | unsigned int len; |
2407 | 156k | rv = Null_Cipher(NULL, SSL_BUFFER_NEXT(wrBuf), &len, |
2408 | 156k | SSL_BUFFER_SPACE(wrBuf), pIn, contentLen); |
2409 | 156k | if (rv != SECSuccess) { |
2410 | 0 | return SECFailure; /* error was set */ |
2411 | 0 | } |
2412 | 156k | rv = sslBuffer_Skip(wrBuf, len, NULL); |
2413 | 156k | PORT_Assert(rv == SECSuccess); /* Can't fail. */ |
2414 | 156k | } |
2415 | | #else |
2416 | | if (cwSpec->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
2417 | | PRUint8 *cipherText = SSL_BUFFER_NEXT(wrBuf); |
2418 | | unsigned int bufLen = SSL_BUFFER_LEN(wrBuf); |
2419 | | rv = tls13_ProtectRecord(ss, cwSpec, ct, pIn, contentLen, wrBuf); |
2420 | | if (rv != SECSuccess) { |
2421 | | return SECFailure; |
2422 | | } |
2423 | | if (IS_DTLS(ss)) { |
2424 | | bufLen = SSL_BUFFER_LEN(wrBuf) - bufLen; |
2425 | | rv = dtls13_MaskSequenceNumber(ss, cwSpec, |
2426 | | SSL_BUFFER_BASE(wrBuf), |
2427 | | cipherText, bufLen); |
2428 | | } |
2429 | | } else { |
2430 | | rv = ssl3_MACEncryptRecord(cwSpec, ss->sec.isServer, IS_DTLS(ss), ct, |
2431 | | pIn, contentLen, wrBuf); |
2432 | | } |
2433 | | #endif |
2434 | 156k | if (rv != SECSuccess) { |
2435 | 0 | return SECFailure; /* error was set */ |
2436 | 0 | } |
2437 | | |
2438 | 156k | if (needsLength) { |
2439 | | /* Insert the length. */ |
2440 | 156k | rv = sslBuffer_InsertLength(wrBuf, lenOffset, 2); |
2441 | 156k | if (rv != SECSuccess) { |
2442 | 0 | PORT_Assert(0); /* Can't fail. */ |
2443 | 0 | return SECFailure; |
2444 | 0 | } |
2445 | 156k | } |
2446 | | |
2447 | 156k | ++cwSpec->nextSeqNum; |
2448 | 156k | return SECSuccess; |
2449 | 156k | } |
2450 | | |
2451 | | SECStatus |
2452 | | ssl_ProtectNextRecord(sslSocket *ss, ssl3CipherSpec *spec, SSLContentType ct, |
2453 | | const PRUint8 *pIn, unsigned int nIn, |
2454 | | unsigned int *written) |
2455 | 156k | { |
2456 | 156k | sslBuffer *wrBuf = &ss->sec.writeBuf; |
2457 | 156k | unsigned int contentLen; |
2458 | 156k | unsigned int spaceNeeded; |
2459 | 156k | SECStatus rv; |
2460 | | |
2461 | 156k | contentLen = PR_MIN(nIn, spec->recordSizeLimit); |
2462 | 156k | spaceNeeded = contentLen + SSL3_BUFFER_FUDGE; |
2463 | 156k | if (spec->version >= SSL_LIBRARY_VERSION_TLS_1_1 && |
2464 | 156k | spec->cipherDef->type == type_block) { |
2465 | 25.7k | spaceNeeded += spec->cipherDef->iv_size; |
2466 | 25.7k | } |
2467 | 156k | if (spaceNeeded > SSL_BUFFER_SPACE(wrBuf)) { |
2468 | 0 | rv = sslBuffer_Grow(wrBuf, spaceNeeded); |
2469 | 0 | if (rv != SECSuccess) { |
2470 | 0 | SSL_DBG(("%d: SSL3[%d]: failed to expand write buffer to %d", |
2471 | 0 | SSL_GETPID(), ss->fd, spaceNeeded)); |
2472 | 0 | return SECFailure; |
2473 | 0 | } |
2474 | 0 | } |
2475 | | |
2476 | 156k | rv = ssl_ProtectRecord(ss, spec, ct, pIn, contentLen, wrBuf); |
2477 | 156k | if (rv != SECSuccess) { |
2478 | 0 | return SECFailure; |
2479 | 0 | } |
2480 | 156k | PRINT_BUF(50, (ss, "send (encrypted) record data:", |
2481 | 156k | SSL_BUFFER_BASE(wrBuf), SSL_BUFFER_LEN(wrBuf))); |
2482 | 156k | *written = contentLen; |
2483 | 156k | return SECSuccess; |
2484 | 156k | } |
2485 | | |
2486 | | /* Process the plain text before sending it. |
2487 | | * Returns the number of bytes of plaintext that were successfully sent |
2488 | | * plus the number of bytes of plaintext that were copied into the |
2489 | | * output (write) buffer. |
2490 | | * Returns -1 on an error. PR_WOULD_BLOCK_ERROR is set if the error is blocking |
2491 | | * and not terminal. |
2492 | | * |
2493 | | * Notes on the use of the private ssl flags: |
2494 | | * (no private SSL flags) |
2495 | | * Attempt to make and send SSL records for all plaintext |
2496 | | * If non-blocking and a send gets WOULD_BLOCK, |
2497 | | * or if the pending (ciphertext) buffer is not empty, |
2498 | | * then buffer remaining bytes of ciphertext into pending buf, |
2499 | | * and continue to do that for all succssive records until all |
2500 | | * bytes are used. |
2501 | | * ssl_SEND_FLAG_FORCE_INTO_BUFFER |
2502 | | * As above, except this suppresses all write attempts, and forces |
2503 | | * all ciphertext into the pending ciphertext buffer. |
2504 | | * ssl_SEND_FLAG_USE_EPOCH (for DTLS) |
2505 | | * Forces the use of the provided epoch |
2506 | | */ |
2507 | | PRInt32 |
2508 | | ssl3_SendRecord(sslSocket *ss, |
2509 | | ssl3CipherSpec *cwSpec, /* non-NULL for DTLS retransmits */ |
2510 | | SSLContentType ct, |
2511 | | const PRUint8 *pIn, /* input buffer */ |
2512 | | PRInt32 nIn, /* bytes of input */ |
2513 | | PRInt32 flags) |
2514 | 153k | { |
2515 | 153k | sslBuffer *wrBuf = &ss->sec.writeBuf; |
2516 | 153k | ssl3CipherSpec *spec; |
2517 | 153k | SECStatus rv; |
2518 | 153k | PRInt32 totalSent = 0; |
2519 | | |
2520 | 153k | SSL_TRC(3, ("%d: SSL3[%d] SendRecord type: %s nIn=%d", |
2521 | 153k | SSL_GETPID(), ss->fd, ssl3_DecodeContentType(ct), |
2522 | 153k | nIn)); |
2523 | 153k | PRINT_BUF(50, (ss, "Send record (plain text)", pIn, nIn)); |
2524 | | |
2525 | 153k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
2526 | 153k | PORT_Assert(SSL_BUFFER_LEN(wrBuf) == 0); |
2527 | | |
2528 | 153k | if (ss->ssl3.fatalAlertSent) { |
2529 | 1.10k | SSL_TRC(3, ("%d: SSL3[%d] Suppress write, fatal alert already sent", |
2530 | 1.10k | SSL_GETPID(), ss->fd)); |
2531 | 1.10k | if (ct != ssl_ct_alert) { |
2532 | | /* If we are sending an alert, then we already have an |
2533 | | * error, so don't overwrite. */ |
2534 | 0 | PORT_SetError(SSL_ERROR_HANDSHAKE_FAILED); |
2535 | 0 | } |
2536 | 1.10k | return -1; |
2537 | 1.10k | } |
2538 | | |
2539 | | /* check for Token Presence */ |
2540 | 151k | if (!ssl3_ClientAuthTokenPresent(ss->sec.ci.sid)) { |
2541 | 0 | PORT_SetError(SSL_ERROR_TOKEN_INSERTION_REMOVAL); |
2542 | 0 | return -1; |
2543 | 0 | } |
2544 | | |
2545 | 151k | if (ss->recordWriteCallback) { |
2546 | 0 | PRUint16 epoch; |
2547 | 0 | ssl_GetSpecReadLock(ss); |
2548 | 0 | epoch = ss->ssl3.cwSpec->epoch; |
2549 | 0 | ssl_ReleaseSpecReadLock(ss); |
2550 | 0 | rv = ss->recordWriteCallback(ss->fd, epoch, ct, pIn, nIn, |
2551 | 0 | ss->recordWriteCallbackArg); |
2552 | 0 | if (rv != SECSuccess) { |
2553 | 0 | return -1; |
2554 | 0 | } |
2555 | 0 | return nIn; |
2556 | 0 | } |
2557 | | |
2558 | 151k | if (cwSpec) { |
2559 | | /* cwSpec can only be set for retransmissions of the DTLS handshake. */ |
2560 | 0 | PORT_Assert(IS_DTLS(ss) && |
2561 | 0 | (ct == ssl_ct_handshake || |
2562 | 0 | ct == ssl_ct_change_cipher_spec)); |
2563 | 0 | spec = cwSpec; |
2564 | 151k | } else { |
2565 | 151k | spec = ss->ssl3.cwSpec; |
2566 | 151k | } |
2567 | | |
2568 | 308k | while (nIn > 0) { |
2569 | 156k | unsigned int written = 0; |
2570 | 156k | PRInt32 sent; |
2571 | | |
2572 | 156k | ssl_GetSpecReadLock(ss); |
2573 | 156k | rv = ssl_ProtectNextRecord(ss, spec, ct, pIn, nIn, &written); |
2574 | 156k | ssl_ReleaseSpecReadLock(ss); |
2575 | 156k | if (rv != SECSuccess) { |
2576 | 0 | goto loser; |
2577 | 0 | } |
2578 | | |
2579 | 156k | PORT_Assert(written > 0); |
2580 | | /* DTLS should not fragment non-application data here. */ |
2581 | 156k | if (IS_DTLS(ss) && ct != ssl_ct_application_data) { |
2582 | 0 | PORT_Assert(written == nIn); |
2583 | 0 | } |
2584 | | |
2585 | 156k | pIn += written; |
2586 | 156k | nIn -= written; |
2587 | 156k | PORT_Assert(nIn >= 0); |
2588 | | |
2589 | | /* If there's still some previously saved ciphertext, |
2590 | | * or the caller doesn't want us to send the data yet, |
2591 | | * then add all our new ciphertext to the amount previously saved. |
2592 | | */ |
2593 | 156k | if ((ss->pendingBuf.len > 0) || |
2594 | 156k | (flags & ssl_SEND_FLAG_FORCE_INTO_BUFFER)) { |
2595 | | |
2596 | 99.7k | rv = ssl_SaveWriteData(ss, SSL_BUFFER_BASE(wrBuf), |
2597 | 99.7k | SSL_BUFFER_LEN(wrBuf)); |
2598 | 99.7k | if (rv != SECSuccess) { |
2599 | | /* presumably a memory error, SEC_ERROR_NO_MEMORY */ |
2600 | 0 | goto loser; |
2601 | 0 | } |
2602 | | |
2603 | 99.7k | if (!(flags & ssl_SEND_FLAG_FORCE_INTO_BUFFER)) { |
2604 | 33.2k | ss->handshakeBegun = 1; |
2605 | 33.2k | sent = ssl_SendSavedWriteData(ss); |
2606 | 33.2k | if (sent < 0 && PR_GetError() != PR_WOULD_BLOCK_ERROR) { |
2607 | 0 | ssl_MapLowLevelError(SSL_ERROR_SOCKET_WRITE_FAILURE); |
2608 | 0 | goto loser; |
2609 | 0 | } |
2610 | 33.2k | if (ss->pendingBuf.len) { |
2611 | 0 | flags |= ssl_SEND_FLAG_FORCE_INTO_BUFFER; |
2612 | 0 | } |
2613 | 33.2k | } |
2614 | 99.7k | } else { |
2615 | 56.3k | PORT_Assert(SSL_BUFFER_LEN(wrBuf) > 0); |
2616 | 56.3k | ss->handshakeBegun = 1; |
2617 | 56.3k | sent = ssl_DefSend(ss, SSL_BUFFER_BASE(wrBuf), |
2618 | 56.3k | SSL_BUFFER_LEN(wrBuf), |
2619 | 56.3k | flags & ~ssl_SEND_FLAG_MASK); |
2620 | 56.3k | if (sent < 0) { |
2621 | 0 | if (PORT_GetError() != PR_WOULD_BLOCK_ERROR) { |
2622 | 0 | ssl_MapLowLevelError(SSL_ERROR_SOCKET_WRITE_FAILURE); |
2623 | 0 | goto loser; |
2624 | 0 | } |
2625 | | /* we got PR_WOULD_BLOCK_ERROR, which means none was sent. */ |
2626 | 0 | sent = 0; |
2627 | 0 | } |
2628 | 56.3k | if (SSL_BUFFER_LEN(wrBuf) > (unsigned int)sent) { |
2629 | 0 | if (IS_DTLS(ss)) { |
2630 | | /* DTLS just says no in this case. No buffering */ |
2631 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
2632 | 0 | goto loser; |
2633 | 0 | } |
2634 | | /* now take all the remaining unsent new ciphertext and |
2635 | | * append it to the buffer of previously unsent ciphertext. |
2636 | | */ |
2637 | 0 | rv = ssl_SaveWriteData(ss, SSL_BUFFER_BASE(wrBuf) + sent, |
2638 | 0 | SSL_BUFFER_LEN(wrBuf) - sent); |
2639 | 0 | if (rv != SECSuccess) { |
2640 | | /* presumably a memory error, SEC_ERROR_NO_MEMORY */ |
2641 | 0 | goto loser; |
2642 | 0 | } |
2643 | 0 | } |
2644 | 56.3k | } |
2645 | 156k | wrBuf->len = 0; |
2646 | 156k | totalSent += written; |
2647 | 156k | } |
2648 | 151k | return totalSent; |
2649 | | |
2650 | 0 | loser: |
2651 | | /* Don't leave bits of buffer lying around. */ |
2652 | 0 | wrBuf->len = 0; |
2653 | 0 | return -1; |
2654 | 151k | } |
2655 | | |
2656 | 8.94k | #define SSL3_PENDING_HIGH_WATER 1024 |
2657 | | |
2658 | | /* Attempt to send the content of "in" in an SSL application_data record. |
2659 | | * Returns "len" or -1 on failure. |
2660 | | */ |
2661 | | int |
2662 | | ssl3_SendApplicationData(sslSocket *ss, const unsigned char *in, |
2663 | | PRInt32 len, PRInt32 flags) |
2664 | 4.47k | { |
2665 | 4.47k | PRInt32 totalSent = 0; |
2666 | 4.47k | PRInt32 discarded = 0; |
2667 | 4.47k | PRBool splitNeeded = PR_FALSE; |
2668 | | |
2669 | 4.47k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
2670 | | /* These flags for internal use only */ |
2671 | 4.47k | PORT_Assert(!(flags & ssl_SEND_FLAG_NO_RETRANSMIT)); |
2672 | 4.47k | if (len < 0 || !in) { |
2673 | 0 | PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
2674 | 0 | return -1; |
2675 | 0 | } |
2676 | | |
2677 | 4.47k | if (ss->pendingBuf.len > SSL3_PENDING_HIGH_WATER && |
2678 | 4.47k | !ssl_SocketIsBlocking(ss)) { |
2679 | 0 | PORT_Assert(!ssl_SocketIsBlocking(ss)); |
2680 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
2681 | 0 | return -1; |
2682 | 0 | } |
2683 | | |
2684 | 4.47k | if (ss->appDataBuffered && len) { |
2685 | 0 | PORT_Assert(in[0] == (unsigned char)(ss->appDataBuffered)); |
2686 | 0 | if (in[0] != (unsigned char)(ss->appDataBuffered)) { |
2687 | 0 | PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
2688 | 0 | return -1; |
2689 | 0 | } |
2690 | 0 | in++; |
2691 | 0 | len--; |
2692 | 0 | discarded = 1; |
2693 | 0 | } |
2694 | | |
2695 | | /* We will split the first byte of the record into its own record, as |
2696 | | * explained in the documentation for SSL_CBC_RANDOM_IV in ssl.h. |
2697 | | */ |
2698 | 4.47k | if (len > 1 && ss->opt.cbcRandomIV && |
2699 | 4.47k | ss->version < SSL_LIBRARY_VERSION_TLS_1_1 && |
2700 | 4.47k | ss->ssl3.cwSpec->cipherDef->type == type_block /* CBC */) { |
2701 | 1.36k | splitNeeded = PR_TRUE; |
2702 | 1.36k | } |
2703 | | |
2704 | 10.3k | while (len > totalSent) { |
2705 | 5.83k | PRInt32 sent, toSend; |
2706 | | |
2707 | 5.83k | if (totalSent > 0) { |
2708 | | /* |
2709 | | * The thread yield is intended to give the reader thread a |
2710 | | * chance to get some cycles while the writer thread is in |
2711 | | * the middle of a large application data write. (See |
2712 | | * Bugzilla bug 127740, comment #1.) |
2713 | | */ |
2714 | 1.36k | ssl_ReleaseXmitBufLock(ss); |
2715 | 1.36k | PR_Sleep(PR_INTERVAL_NO_WAIT); /* PR_Yield(); */ |
2716 | 1.36k | ssl_GetXmitBufLock(ss); |
2717 | 1.36k | } |
2718 | | |
2719 | 5.83k | if (splitNeeded) { |
2720 | 1.36k | toSend = 1; |
2721 | 1.36k | splitNeeded = PR_FALSE; |
2722 | 4.47k | } else { |
2723 | 4.47k | toSend = PR_MIN(len - totalSent, MAX_FRAGMENT_LENGTH); |
2724 | 4.47k | } |
2725 | | |
2726 | | /* |
2727 | | * Note that the 0 epoch is OK because flags will never require |
2728 | | * its use, as guaranteed by the PORT_Assert above. |
2729 | | */ |
2730 | 5.83k | sent = ssl3_SendRecord(ss, NULL, ssl_ct_application_data, |
2731 | 5.83k | in + totalSent, toSend, flags); |
2732 | 5.83k | if (sent < 0) { |
2733 | 0 | if (totalSent > 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR) { |
2734 | 0 | PORT_Assert(ss->lastWriteBlocked); |
2735 | 0 | break; |
2736 | 0 | } |
2737 | 0 | return -1; /* error code set by ssl3_SendRecord */ |
2738 | 0 | } |
2739 | 5.83k | totalSent += sent; |
2740 | 5.83k | if (ss->pendingBuf.len) { |
2741 | | /* must be a non-blocking socket */ |
2742 | 0 | PORT_Assert(!ssl_SocketIsBlocking(ss)); |
2743 | 0 | PORT_Assert(ss->lastWriteBlocked); |
2744 | 0 | break; |
2745 | 0 | } |
2746 | 5.83k | } |
2747 | 4.47k | if (ss->pendingBuf.len) { |
2748 | | /* Must be non-blocking. */ |
2749 | 0 | PORT_Assert(!ssl_SocketIsBlocking(ss)); |
2750 | 0 | if (totalSent > 0) { |
2751 | 0 | ss->appDataBuffered = 0x100 | in[totalSent - 1]; |
2752 | 0 | } |
2753 | |
|
2754 | 0 | totalSent = totalSent + discarded - 1; |
2755 | 0 | if (totalSent <= 0) { |
2756 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
2757 | 0 | totalSent = SECFailure; |
2758 | 0 | } |
2759 | 0 | return totalSent; |
2760 | 0 | } |
2761 | 4.47k | ss->appDataBuffered = 0; |
2762 | 4.47k | return totalSent + discarded; |
2763 | 4.47k | } |
2764 | | |
2765 | | /* Attempt to send buffered handshake messages. |
2766 | | * Always set sendBuf.len to 0, even when returning SECFailure. |
2767 | | * |
2768 | | * Depending on whether we are doing DTLS or not, this either calls |
2769 | | * |
2770 | | * - ssl3_FlushHandshakeMessages if non-DTLS |
2771 | | * - dtls_FlushHandshakeMessages if DTLS |
2772 | | * |
2773 | | * Called from SSL3_SendAlert(), ssl3_SendChangeCipherSpecs(), |
2774 | | * ssl3_AppendHandshake(), ssl3_SendClientHello(), |
2775 | | * ssl3_SendHelloRequest(), ssl3_SendServerHelloDone(), |
2776 | | * ssl3_SendFinished(), |
2777 | | */ |
2778 | | SECStatus |
2779 | | ssl3_FlushHandshake(sslSocket *ss, PRInt32 flags) |
2780 | 116k | { |
2781 | 116k | if (IS_DTLS(ss)) { |
2782 | 0 | return dtls_FlushHandshakeMessages(ss, flags); |
2783 | 0 | } |
2784 | 116k | return ssl3_FlushHandshakeMessages(ss, flags); |
2785 | 116k | } |
2786 | | |
2787 | | /* Attempt to send the content of sendBuf buffer in an SSL handshake record. |
2788 | | * Always set sendBuf.len to 0, even when returning SECFailure. |
2789 | | * |
2790 | | * Called from ssl3_FlushHandshake |
2791 | | */ |
2792 | | static SECStatus |
2793 | | ssl3_FlushHandshakeMessages(sslSocket *ss, PRInt32 flags) |
2794 | 116k | { |
2795 | 116k | static const PRInt32 allowedFlags = ssl_SEND_FLAG_FORCE_INTO_BUFFER; |
2796 | 116k | PRInt32 count = -1; |
2797 | 116k | SECStatus rv; |
2798 | | |
2799 | 116k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
2800 | 116k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
2801 | | |
2802 | 116k | if (!ss->sec.ci.sendBuf.buf || !ss->sec.ci.sendBuf.len) |
2803 | 7.95k | return SECSuccess; |
2804 | | |
2805 | | /* only these flags are allowed */ |
2806 | 108k | PORT_Assert(!(flags & ~allowedFlags)); |
2807 | 108k | if ((flags & ~allowedFlags) != 0) { |
2808 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
2809 | 0 | return SECFailure; |
2810 | 0 | } |
2811 | 108k | count = ssl3_SendRecord(ss, NULL, ssl_ct_handshake, |
2812 | 108k | ss->sec.ci.sendBuf.buf, |
2813 | 108k | ss->sec.ci.sendBuf.len, flags); |
2814 | 108k | if (count < 0) { |
2815 | 0 | int err = PORT_GetError(); |
2816 | 0 | PORT_Assert(err != PR_WOULD_BLOCK_ERROR); |
2817 | 0 | if (err == PR_WOULD_BLOCK_ERROR) { |
2818 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
2819 | 0 | } |
2820 | 0 | rv = SECFailure; |
2821 | 108k | } else if ((unsigned int)count < ss->sec.ci.sendBuf.len) { |
2822 | | /* short write should never happen */ |
2823 | 0 | PORT_Assert((unsigned int)count >= ss->sec.ci.sendBuf.len); |
2824 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
2825 | 0 | rv = SECFailure; |
2826 | 108k | } else { |
2827 | 108k | rv = SECSuccess; |
2828 | 108k | } |
2829 | | |
2830 | | /* Whether we succeeded or failed, toss the old handshake data. */ |
2831 | 108k | ss->sec.ci.sendBuf.len = 0; |
2832 | 108k | return rv; |
2833 | 108k | } |
2834 | | |
2835 | | /* |
2836 | | * Called from ssl3_HandleAlert and from ssl3_HandleCertificate when |
2837 | | * the remote client sends a negative response to our certificate request. |
2838 | | * Returns SECFailure if the application has required client auth. |
2839 | | * SECSuccess otherwise. |
2840 | | */ |
2841 | | SECStatus |
2842 | | ssl3_HandleNoCertificate(sslSocket *ss) |
2843 | 0 | { |
2844 | 0 | ssl3_CleanupPeerCerts(ss); |
2845 | | |
2846 | | /* If the server has required client-auth blindly but doesn't |
2847 | | * actually look at the certificate it won't know that no |
2848 | | * certificate was presented so we shutdown the socket to ensure |
2849 | | * an error. We only do this if we haven't already completed the |
2850 | | * first handshake because if we're redoing the handshake we |
2851 | | * know the server is paying attention to the certificate. |
2852 | | */ |
2853 | 0 | if ((ss->opt.requireCertificate == SSL_REQUIRE_ALWAYS) || |
2854 | 0 | (!ss->firstHsDone && |
2855 | 0 | (ss->opt.requireCertificate == SSL_REQUIRE_FIRST_HANDSHAKE))) { |
2856 | 0 | PRFileDesc *lower; |
2857 | |
|
2858 | 0 | ssl_UncacheSessionID(ss); |
2859 | |
|
2860 | 0 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
2861 | 0 | SSL3_SendAlert(ss, alert_fatal, certificate_required); |
2862 | 0 | } else { |
2863 | 0 | SSL3_SendAlert(ss, alert_fatal, bad_certificate); |
2864 | 0 | } |
2865 | |
|
2866 | 0 | lower = ss->fd->lower; |
2867 | | #ifdef _WIN32 |
2868 | | lower->methods->shutdown(lower, PR_SHUTDOWN_SEND); |
2869 | | #else |
2870 | 0 | lower->methods->shutdown(lower, PR_SHUTDOWN_BOTH); |
2871 | 0 | #endif |
2872 | 0 | PORT_SetError(SSL_ERROR_NO_CERTIFICATE); |
2873 | 0 | return SECFailure; |
2874 | 0 | } |
2875 | 0 | return SECSuccess; |
2876 | 0 | } |
2877 | | |
2878 | | /************************************************************************ |
2879 | | * Alerts |
2880 | | */ |
2881 | | |
2882 | | /* |
2883 | | ** Acquires both handshake and XmitBuf locks. |
2884 | | ** Called from: ssl3_IllegalParameter <- |
2885 | | ** ssl3_HandshakeFailure <- |
2886 | | ** ssl3_HandleAlert <- ssl3_HandleRecord. |
2887 | | ** ssl3_HandleChangeCipherSpecs <- ssl3_HandleRecord |
2888 | | ** ssl3_ConsumeHandshakeVariable <- |
2889 | | ** ssl3_HandleHelloRequest <- |
2890 | | ** ssl3_HandleServerHello <- |
2891 | | ** ssl3_HandleServerKeyExchange <- |
2892 | | ** ssl3_HandleCertificateRequest <- |
2893 | | ** ssl3_HandleServerHelloDone <- |
2894 | | ** ssl3_HandleClientHello <- |
2895 | | ** ssl3_HandleV2ClientHello <- |
2896 | | ** ssl3_HandleCertificateVerify <- |
2897 | | ** ssl3_HandleClientKeyExchange <- |
2898 | | ** ssl3_HandleCertificate <- |
2899 | | ** ssl3_HandleFinished <- |
2900 | | ** ssl3_HandleHandshakeMessage <- |
2901 | | ** ssl3_HandlePostHelloHandshakeMessage <- |
2902 | | ** ssl3_HandleRecord <- |
2903 | | ** |
2904 | | */ |
2905 | | SECStatus |
2906 | | SSL3_SendAlert(sslSocket *ss, SSL3AlertLevel level, SSL3AlertDescription desc) |
2907 | 5.60k | { |
2908 | 5.60k | PRUint8 bytes[2]; |
2909 | 5.60k | SECStatus rv; |
2910 | 5.60k | PRBool needHsLock = !ssl_HaveSSL3HandshakeLock(ss); |
2911 | | |
2912 | | /* Check that if I need the HS lock I also need the Xmit lock */ |
2913 | 5.60k | PORT_Assert(!needHsLock || !ssl_HaveXmitBufLock(ss)); |
2914 | | |
2915 | 5.60k | SSL_TRC(3, ("%d: SSL3[%d]: send alert record, level=%d desc=%d", |
2916 | 5.60k | SSL_GETPID(), ss->fd, level, desc)); |
2917 | | |
2918 | 5.60k | bytes[0] = level; |
2919 | 5.60k | bytes[1] = desc; |
2920 | | |
2921 | 5.60k | if (needHsLock) { |
2922 | 3.88k | ssl_GetSSL3HandshakeLock(ss); |
2923 | 3.88k | } |
2924 | 5.60k | if (level == alert_fatal) { |
2925 | 3.72k | if (ss->sec.ci.sid) { |
2926 | 3.72k | ssl_UncacheSessionID(ss); |
2927 | 3.72k | } |
2928 | 3.72k | } |
2929 | | |
2930 | 5.60k | rv = tls13_SetAlertCipherSpec(ss); |
2931 | 5.60k | if (rv != SECSuccess) { |
2932 | 0 | if (needHsLock) { |
2933 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
2934 | 0 | } |
2935 | 0 | return rv; |
2936 | 0 | } |
2937 | | |
2938 | 5.60k | ssl_GetXmitBufLock(ss); |
2939 | 5.60k | rv = ssl3_FlushHandshake(ss, ssl_SEND_FLAG_FORCE_INTO_BUFFER); |
2940 | 5.60k | if (rv == SECSuccess) { |
2941 | 5.60k | PRInt32 sent; |
2942 | 5.60k | sent = ssl3_SendRecord(ss, NULL, ssl_ct_alert, bytes, 2, |
2943 | 5.60k | (desc == no_certificate) ? ssl_SEND_FLAG_FORCE_INTO_BUFFER : 0); |
2944 | 5.60k | rv = (sent >= 0) ? SECSuccess : (SECStatus)sent; |
2945 | 5.60k | } |
2946 | 5.60k | if (level == alert_fatal) { |
2947 | 3.72k | ss->ssl3.fatalAlertSent = PR_TRUE; |
2948 | 3.72k | } |
2949 | 5.60k | ssl_ReleaseXmitBufLock(ss); |
2950 | 5.60k | if (needHsLock) { |
2951 | 3.88k | ssl_ReleaseSSL3HandshakeLock(ss); |
2952 | 3.88k | } |
2953 | 5.60k | if (rv == SECSuccess && ss->alertSentCallback) { |
2954 | 0 | SSLAlert alert = { level, desc }; |
2955 | 0 | ss->alertSentCallback(ss->fd, ss->alertSentCallbackArg, &alert); |
2956 | 0 | } |
2957 | 5.60k | return rv; /* error set by ssl3_FlushHandshake or ssl3_SendRecord */ |
2958 | 5.60k | } |
2959 | | |
2960 | | /* |
2961 | | * Send illegal_parameter alert. Set generic error number. |
2962 | | */ |
2963 | | static SECStatus |
2964 | | ssl3_IllegalParameter(sslSocket *ss) |
2965 | 7 | { |
2966 | 7 | (void)SSL3_SendAlert(ss, alert_fatal, illegal_parameter); |
2967 | 7 | PORT_SetError(ss->sec.isServer ? SSL_ERROR_BAD_CLIENT |
2968 | 7 | : SSL_ERROR_BAD_SERVER); |
2969 | 7 | return SECFailure; |
2970 | 7 | } |
2971 | | |
2972 | | /* |
2973 | | * Send handshake_Failure alert. Set generic error number. |
2974 | | */ |
2975 | | static SECStatus |
2976 | | ssl3_HandshakeFailure(sslSocket *ss) |
2977 | 0 | { |
2978 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, handshake_failure); |
2979 | 0 | PORT_SetError(ss->sec.isServer ? SSL_ERROR_BAD_CLIENT |
2980 | 0 | : SSL_ERROR_BAD_SERVER); |
2981 | 0 | return SECFailure; |
2982 | 0 | } |
2983 | | |
2984 | | void |
2985 | | ssl3_SendAlertForCertError(sslSocket *ss, PRErrorCode errCode) |
2986 | 371 | { |
2987 | 371 | SSL3AlertDescription desc = bad_certificate; |
2988 | 371 | PRBool isTLS = ss->version >= SSL_LIBRARY_VERSION_3_1_TLS; |
2989 | | |
2990 | 371 | switch (errCode) { |
2991 | 87 | case SEC_ERROR_LIBRARY_FAILURE: |
2992 | 87 | desc = unsupported_certificate; |
2993 | 87 | break; |
2994 | 0 | case SEC_ERROR_EXPIRED_CERTIFICATE: |
2995 | 0 | desc = certificate_expired; |
2996 | 0 | break; |
2997 | 0 | case SEC_ERROR_REVOKED_CERTIFICATE: |
2998 | 0 | desc = certificate_revoked; |
2999 | 0 | break; |
3000 | 0 | case SEC_ERROR_INADEQUATE_KEY_USAGE: |
3001 | 0 | case SEC_ERROR_INADEQUATE_CERT_TYPE: |
3002 | 0 | desc = certificate_unknown; |
3003 | 0 | break; |
3004 | 0 | case SEC_ERROR_UNTRUSTED_CERT: |
3005 | 0 | desc = isTLS ? access_denied : certificate_unknown; |
3006 | 0 | break; |
3007 | 0 | case SEC_ERROR_UNKNOWN_ISSUER: |
3008 | 0 | case SEC_ERROR_UNTRUSTED_ISSUER: |
3009 | 0 | desc = isTLS ? unknown_ca : certificate_unknown; |
3010 | 0 | break; |
3011 | 0 | case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE: |
3012 | 0 | desc = isTLS ? unknown_ca : certificate_expired; |
3013 | 0 | break; |
3014 | | |
3015 | 0 | case SEC_ERROR_CERT_NOT_IN_NAME_SPACE: |
3016 | 0 | case SEC_ERROR_PATH_LEN_CONSTRAINT_INVALID: |
3017 | 0 | case SEC_ERROR_CA_CERT_INVALID: |
3018 | 0 | case SEC_ERROR_BAD_SIGNATURE: |
3019 | 284 | default: |
3020 | 284 | desc = bad_certificate; |
3021 | 284 | break; |
3022 | 371 | } |
3023 | 371 | SSL_DBG(("%d: SSL3[%d]: peer certificate is no good: error=%d", |
3024 | 371 | SSL_GETPID(), ss->fd, errCode)); |
3025 | | |
3026 | 371 | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
3027 | 371 | } |
3028 | | |
3029 | | /* |
3030 | | * Send decode_error alert. Set generic error number. |
3031 | | */ |
3032 | | SECStatus |
3033 | | ssl3_DecodeError(sslSocket *ss) |
3034 | 1.01k | { |
3035 | 1.01k | (void)SSL3_SendAlert(ss, alert_fatal, |
3036 | 1.01k | ss->version > SSL_LIBRARY_VERSION_3_0 ? decode_error |
3037 | 1.01k | : illegal_parameter); |
3038 | 1.01k | PORT_SetError(ss->sec.isServer ? SSL_ERROR_BAD_CLIENT |
3039 | 1.01k | : SSL_ERROR_BAD_SERVER); |
3040 | 1.01k | return SECFailure; |
3041 | 1.01k | } |
3042 | | |
3043 | | /* Called from ssl3_HandleRecord. |
3044 | | ** Caller must hold both RecvBuf and Handshake locks. |
3045 | | */ |
3046 | | static SECStatus |
3047 | | ssl3_HandleAlert(sslSocket *ss, sslBuffer *buf) |
3048 | 11.1k | { |
3049 | 11.1k | SSL3AlertLevel level; |
3050 | 11.1k | SSL3AlertDescription desc; |
3051 | 11.1k | int error; |
3052 | | |
3053 | 11.1k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
3054 | 11.1k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3055 | | |
3056 | 11.1k | SSL_TRC(3, ("%d: SSL3[%d]: handle alert record", SSL_GETPID(), ss->fd)); |
3057 | | |
3058 | 11.1k | if (buf->len != 2) { |
3059 | 123 | (void)ssl3_DecodeError(ss); |
3060 | 123 | PORT_SetError(SSL_ERROR_RX_MALFORMED_ALERT); |
3061 | 123 | return SECFailure; |
3062 | 123 | } |
3063 | 11.0k | level = (SSL3AlertLevel)buf->buf[0]; |
3064 | 11.0k | desc = (SSL3AlertDescription)buf->buf[1]; |
3065 | 11.0k | buf->len = 0; |
3066 | 11.0k | SSL_TRC(5, ("%d: SSL3[%d] received alert, level = %d, description = %d", |
3067 | 11.0k | SSL_GETPID(), ss->fd, level, desc)); |
3068 | | |
3069 | 11.0k | if (ss->alertReceivedCallback) { |
3070 | 0 | SSLAlert alert = { level, desc }; |
3071 | 0 | ss->alertReceivedCallback(ss->fd, ss->alertReceivedCallbackArg, &alert); |
3072 | 0 | } |
3073 | | |
3074 | 11.0k | switch (desc) { |
3075 | 5 | case close_notify: |
3076 | 5 | ss->recvdCloseNotify = 1; |
3077 | 5 | error = SSL_ERROR_CLOSE_NOTIFY_ALERT; |
3078 | 5 | break; |
3079 | 250 | case unexpected_message: |
3080 | 250 | error = SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT; |
3081 | 250 | break; |
3082 | 248 | case bad_record_mac: |
3083 | 248 | error = SSL_ERROR_BAD_MAC_ALERT; |
3084 | 248 | break; |
3085 | 234 | case decryption_failed_RESERVED: |
3086 | 234 | error = SSL_ERROR_DECRYPTION_FAILED_ALERT; |
3087 | 234 | break; |
3088 | 210 | case record_overflow: |
3089 | 210 | error = SSL_ERROR_RECORD_OVERFLOW_ALERT; |
3090 | 210 | break; |
3091 | 257 | case decompression_failure: |
3092 | 257 | error = SSL_ERROR_DECOMPRESSION_FAILURE_ALERT; |
3093 | 257 | break; |
3094 | 227 | case handshake_failure: |
3095 | 227 | error = SSL_ERROR_HANDSHAKE_FAILURE_ALERT; |
3096 | 227 | break; |
3097 | 393 | case no_certificate: |
3098 | 393 | error = SSL_ERROR_NO_CERTIFICATE; |
3099 | 393 | break; |
3100 | 853 | case certificate_required: |
3101 | 853 | error = SSL_ERROR_RX_CERTIFICATE_REQUIRED_ALERT; |
3102 | 853 | break; |
3103 | 472 | case bad_certificate: |
3104 | 472 | error = SSL_ERROR_BAD_CERT_ALERT; |
3105 | 472 | break; |
3106 | 219 | case unsupported_certificate: |
3107 | 219 | error = SSL_ERROR_UNSUPPORTED_CERT_ALERT; |
3108 | 219 | break; |
3109 | 250 | case certificate_revoked: |
3110 | 250 | error = SSL_ERROR_REVOKED_CERT_ALERT; |
3111 | 250 | break; |
3112 | 215 | case certificate_expired: |
3113 | 215 | error = SSL_ERROR_EXPIRED_CERT_ALERT; |
3114 | 215 | break; |
3115 | 365 | case certificate_unknown: |
3116 | 365 | error = SSL_ERROR_CERTIFICATE_UNKNOWN_ALERT; |
3117 | 365 | break; |
3118 | 240 | case illegal_parameter: |
3119 | 240 | error = SSL_ERROR_ILLEGAL_PARAMETER_ALERT; |
3120 | 240 | break; |
3121 | 207 | case inappropriate_fallback: |
3122 | 207 | error = SSL_ERROR_INAPPROPRIATE_FALLBACK_ALERT; |
3123 | 207 | break; |
3124 | | |
3125 | | /* All alerts below are TLS only. */ |
3126 | 271 | case unknown_ca: |
3127 | 271 | error = SSL_ERROR_UNKNOWN_CA_ALERT; |
3128 | 271 | break; |
3129 | 254 | case access_denied: |
3130 | 254 | error = SSL_ERROR_ACCESS_DENIED_ALERT; |
3131 | 254 | break; |
3132 | 225 | case decode_error: |
3133 | 225 | error = SSL_ERROR_DECODE_ERROR_ALERT; |
3134 | 225 | break; |
3135 | 418 | case decrypt_error: |
3136 | 418 | error = SSL_ERROR_DECRYPT_ERROR_ALERT; |
3137 | 418 | break; |
3138 | 259 | case export_restriction: |
3139 | 259 | error = SSL_ERROR_EXPORT_RESTRICTION_ALERT; |
3140 | 259 | break; |
3141 | 247 | case protocol_version: |
3142 | 247 | error = SSL_ERROR_PROTOCOL_VERSION_ALERT; |
3143 | 247 | break; |
3144 | 234 | case insufficient_security: |
3145 | 234 | error = SSL_ERROR_INSUFFICIENT_SECURITY_ALERT; |
3146 | 234 | break; |
3147 | 303 | case internal_error: |
3148 | 303 | error = SSL_ERROR_INTERNAL_ERROR_ALERT; |
3149 | 303 | break; |
3150 | 476 | case user_canceled: |
3151 | 476 | error = SSL_ERROR_USER_CANCELED_ALERT; |
3152 | 476 | break; |
3153 | 218 | case no_renegotiation: |
3154 | 218 | error = SSL_ERROR_NO_RENEGOTIATION_ALERT; |
3155 | 218 | break; |
3156 | | |
3157 | | /* Alerts for TLS client hello extensions */ |
3158 | 493 | case missing_extension: |
3159 | 493 | error = SSL_ERROR_MISSING_EXTENSION_ALERT; |
3160 | 493 | break; |
3161 | 492 | case unsupported_extension: |
3162 | 492 | error = SSL_ERROR_UNSUPPORTED_EXTENSION_ALERT; |
3163 | 492 | break; |
3164 | 497 | case certificate_unobtainable: |
3165 | 497 | error = SSL_ERROR_CERTIFICATE_UNOBTAINABLE_ALERT; |
3166 | 497 | break; |
3167 | 274 | case unrecognized_name: |
3168 | 274 | error = SSL_ERROR_UNRECOGNIZED_NAME_ALERT; |
3169 | 274 | break; |
3170 | 441 | case bad_certificate_status_response: |
3171 | 441 | error = SSL_ERROR_BAD_CERT_STATUS_RESPONSE_ALERT; |
3172 | 441 | break; |
3173 | 388 | case bad_certificate_hash_value: |
3174 | 388 | error = SSL_ERROR_BAD_CERT_HASH_VALUE_ALERT; |
3175 | 388 | break; |
3176 | 305 | case no_application_protocol: |
3177 | 305 | error = SSL_ERROR_NEXT_PROTOCOL_NO_PROTOCOL; |
3178 | 305 | break; |
3179 | 217 | case ech_required: |
3180 | 217 | error = SSL_ERROR_ECH_REQUIRED_ALERT; |
3181 | 217 | break; |
3182 | 345 | default: |
3183 | 345 | error = SSL_ERROR_RX_UNKNOWN_ALERT; |
3184 | 345 | break; |
3185 | 11.0k | } |
3186 | 11.0k | if ((ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) && |
3187 | 11.0k | (ss->ssl3.hs.ws != wait_server_hello)) { |
3188 | | /* TLS 1.3 requires all but "end of data" alerts to be |
3189 | | * treated as fatal. */ |
3190 | 239 | switch (desc) { |
3191 | 3 | case close_notify: |
3192 | 237 | case user_canceled: |
3193 | 237 | break; |
3194 | 2 | default: |
3195 | 2 | level = alert_fatal; |
3196 | 239 | } |
3197 | 239 | } |
3198 | 11.0k | if (level == alert_fatal) { |
3199 | 23 | ssl_UncacheSessionID(ss); |
3200 | 23 | if ((ss->ssl3.hs.ws == wait_server_hello) && |
3201 | 23 | (desc == handshake_failure)) { |
3202 | | /* XXX This is a hack. We're assuming that any handshake failure |
3203 | | * XXX on the client hello is a failure to match ciphers. |
3204 | | */ |
3205 | 1 | error = SSL_ERROR_NO_CYPHER_OVERLAP; |
3206 | 1 | } |
3207 | 23 | PORT_SetError(error); |
3208 | 23 | return SECFailure; |
3209 | 23 | } |
3210 | 10.9k | if ((desc == no_certificate) && (ss->ssl3.hs.ws == wait_client_cert)) { |
3211 | | /* I'm a server. I've requested a client cert. He hasn't got one. */ |
3212 | 0 | SECStatus rv; |
3213 | |
|
3214 | 0 | PORT_Assert(ss->sec.isServer); |
3215 | 0 | ss->ssl3.hs.ws = wait_client_key; |
3216 | 0 | rv = ssl3_HandleNoCertificate(ss); |
3217 | 0 | return rv; |
3218 | 0 | } |
3219 | 10.9k | return SECSuccess; |
3220 | 10.9k | } |
3221 | | |
3222 | | /* |
3223 | | * Change Cipher Specs |
3224 | | * Called from ssl3_HandleServerHelloDone, |
3225 | | * ssl3_HandleClientHello, |
3226 | | * and ssl3_HandleFinished |
3227 | | * |
3228 | | * Acquires and releases spec write lock, to protect switching the current |
3229 | | * and pending write spec pointers. |
3230 | | */ |
3231 | | |
3232 | | SECStatus |
3233 | | ssl3_SendChangeCipherSpecsInt(sslSocket *ss) |
3234 | 33.2k | { |
3235 | 33.2k | PRUint8 change = change_cipher_spec_choice; |
3236 | 33.2k | SECStatus rv; |
3237 | | |
3238 | 33.2k | SSL_TRC(3, ("%d: SSL3[%d]: send change_cipher_spec record", |
3239 | 33.2k | SSL_GETPID(), ss->fd)); |
3240 | | |
3241 | 33.2k | rv = ssl3_FlushHandshake(ss, ssl_SEND_FLAG_FORCE_INTO_BUFFER); |
3242 | 33.2k | if (rv != SECSuccess) { |
3243 | 0 | return SECFailure; /* error code set by ssl3_FlushHandshake */ |
3244 | 0 | } |
3245 | | |
3246 | 33.2k | if (!IS_DTLS(ss)) { |
3247 | 33.2k | PRInt32 sent; |
3248 | 33.2k | sent = ssl3_SendRecord(ss, NULL, ssl_ct_change_cipher_spec, |
3249 | 33.2k | &change, 1, ssl_SEND_FLAG_FORCE_INTO_BUFFER); |
3250 | 33.2k | if (sent < 0) { |
3251 | 0 | return SECFailure; /* error code set by ssl3_SendRecord */ |
3252 | 0 | } |
3253 | 33.2k | } else { |
3254 | 0 | rv = dtls_QueueMessage(ss, ssl_ct_change_cipher_spec, &change, 1); |
3255 | 0 | if (rv != SECSuccess) { |
3256 | 0 | return SECFailure; |
3257 | 0 | } |
3258 | 0 | } |
3259 | 33.2k | return SECSuccess; |
3260 | 33.2k | } |
3261 | | |
3262 | | static SECStatus |
3263 | | ssl3_SendChangeCipherSpecs(sslSocket *ss) |
3264 | 33.2k | { |
3265 | 33.2k | SECStatus rv; |
3266 | | |
3267 | 33.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
3268 | 33.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3269 | | |
3270 | 33.2k | rv = ssl3_SendChangeCipherSpecsInt(ss); |
3271 | 33.2k | if (rv != SECSuccess) { |
3272 | 0 | return rv; /* Error code set. */ |
3273 | 0 | } |
3274 | | |
3275 | | /* swap the pending and current write specs. */ |
3276 | 33.2k | ssl_GetSpecWriteLock(ss); /**************************************/ |
3277 | | |
3278 | 33.2k | ssl_CipherSpecRelease(ss->ssl3.cwSpec); |
3279 | 33.2k | ss->ssl3.cwSpec = ss->ssl3.pwSpec; |
3280 | 33.2k | ss->ssl3.pwSpec = NULL; |
3281 | | |
3282 | 33.2k | SSL_TRC(3, ("%d: SSL3[%d] Set Current Write Cipher Suite to Pending", |
3283 | 33.2k | SSL_GETPID(), ss->fd)); |
3284 | | |
3285 | | /* With DTLS, we need to set a holddown timer in case the final |
3286 | | * message got lost */ |
3287 | 33.2k | if (IS_DTLS(ss) && ss->ssl3.crSpec->epoch == ss->ssl3.cwSpec->epoch) { |
3288 | 0 | rv = dtls_StartHolddownTimer(ss); |
3289 | 0 | } |
3290 | 33.2k | ssl_ReleaseSpecWriteLock(ss); /**************************************/ |
3291 | | |
3292 | 33.2k | return rv; |
3293 | 33.2k | } |
3294 | | |
3295 | | /* Called from ssl3_HandleRecord. |
3296 | | ** Caller must hold both RecvBuf and Handshake locks. |
3297 | | * |
3298 | | * Acquires and releases spec write lock, to protect switching the current |
3299 | | * and pending write spec pointers. |
3300 | | */ |
3301 | | static SECStatus |
3302 | | ssl3_HandleChangeCipherSpecs(sslSocket *ss, sslBuffer *buf) |
3303 | 31.9k | { |
3304 | 31.9k | SSL3WaitState ws = ss->ssl3.hs.ws; |
3305 | 31.9k | SSL3ChangeCipherSpecChoice change; |
3306 | | |
3307 | 31.9k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
3308 | 31.9k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3309 | | |
3310 | 31.9k | SSL_TRC(3, ("%d: SSL3[%d]: handle change_cipher_spec record", |
3311 | 31.9k | SSL_GETPID(), ss->fd)); |
3312 | | |
3313 | | /* For DTLS: Ignore this if we aren't expecting it. Don't kill a connection |
3314 | | * as a result of receiving trash. |
3315 | | * For TLS: Maybe ignore, but only after checking format. */ |
3316 | 31.9k | if (ws != wait_change_cipher && IS_DTLS(ss)) { |
3317 | | /* Ignore this because it's out of order. */ |
3318 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: discard out of order " |
3319 | 0 | "DTLS change_cipher_spec", |
3320 | 0 | SSL_GETPID(), ss->fd)); |
3321 | 0 | buf->len = 0; |
3322 | 0 | return SECSuccess; |
3323 | 0 | } |
3324 | | |
3325 | | /* Handshake messages should not span ChangeCipherSpec. */ |
3326 | 31.9k | if (ss->ssl3.hs.header_bytes) { |
3327 | 34 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
3328 | 34 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CHANGE_CIPHER); |
3329 | 34 | return SECFailure; |
3330 | 34 | } |
3331 | 31.9k | if (buf->len != 1) { |
3332 | 126 | (void)ssl3_DecodeError(ss); |
3333 | 126 | PORT_SetError(SSL_ERROR_RX_MALFORMED_CHANGE_CIPHER); |
3334 | 126 | return SECFailure; |
3335 | 126 | } |
3336 | 31.7k | change = (SSL3ChangeCipherSpecChoice)buf->buf[0]; |
3337 | 31.7k | if (change != change_cipher_spec_choice) { |
3338 | | /* illegal_parameter is correct here for both SSL3 and TLS. */ |
3339 | 7 | (void)ssl3_IllegalParameter(ss); |
3340 | 7 | PORT_SetError(SSL_ERROR_RX_MALFORMED_CHANGE_CIPHER); |
3341 | 7 | return SECFailure; |
3342 | 7 | } |
3343 | | |
3344 | 31.7k | buf->len = 0; |
3345 | 31.7k | if (ws != wait_change_cipher) { |
3346 | | /* Ignore a CCS for TLS 1.3. This only happens if the server sends a |
3347 | | * HelloRetryRequest. In other cases, the CCS will fail decryption and |
3348 | | * will be discarded by ssl3_HandleRecord(). */ |
3349 | 423 | if (ws == wait_server_hello && |
3350 | 423 | ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
3351 | 423 | ss->ssl3.hs.helloRetry) { |
3352 | 392 | PORT_Assert(!ss->sec.isServer); |
3353 | 392 | return SECSuccess; |
3354 | 392 | } |
3355 | | /* Note: For a server, we can't test ss->ssl3.hs.helloRetry or |
3356 | | * ss->version because the server might be stateless (and so it won't |
3357 | | * have set either value yet). Set a flag so that at least we will |
3358 | | * guarantee that the server will treat any ClientHello properly. */ |
3359 | 31 | if (ws == wait_client_hello && |
3360 | 31 | ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3 && |
3361 | 31 | !ss->ssl3.hs.receivedCcs) { |
3362 | 0 | PORT_Assert(ss->sec.isServer); |
3363 | 0 | ss->ssl3.hs.receivedCcs = PR_TRUE; |
3364 | 0 | return SECSuccess; |
3365 | 0 | } |
3366 | 31 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
3367 | 31 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CHANGE_CIPHER); |
3368 | 31 | return SECFailure; |
3369 | 31 | } |
3370 | | |
3371 | 31.3k | SSL_TRC(3, ("%d: SSL3[%d] Set Current Read Cipher Suite to Pending", |
3372 | 31.3k | SSL_GETPID(), ss->fd)); |
3373 | 31.3k | ssl_GetSpecWriteLock(ss); /*************************************/ |
3374 | 31.3k | PORT_Assert(ss->ssl3.prSpec); |
3375 | 31.3k | ssl_CipherSpecRelease(ss->ssl3.crSpec); |
3376 | 31.3k | ss->ssl3.crSpec = ss->ssl3.prSpec; |
3377 | 31.3k | ss->ssl3.prSpec = NULL; |
3378 | 31.3k | ssl_ReleaseSpecWriteLock(ss); /*************************************/ |
3379 | | |
3380 | 31.3k | ss->ssl3.hs.ws = wait_finished; |
3381 | 31.3k | return SECSuccess; |
3382 | 31.7k | } |
3383 | | |
3384 | | static CK_MECHANISM_TYPE |
3385 | | ssl3_GetMgfMechanismByHashType(SSLHashType hash) |
3386 | 987 | { |
3387 | 987 | switch (hash) { |
3388 | 789 | case ssl_hash_sha256: |
3389 | 789 | return CKG_MGF1_SHA256; |
3390 | 16 | case ssl_hash_sha384: |
3391 | 16 | return CKG_MGF1_SHA384; |
3392 | 182 | case ssl_hash_sha512: |
3393 | 182 | return CKG_MGF1_SHA512; |
3394 | 0 | default: |
3395 | 0 | PORT_Assert(0); |
3396 | 987 | } |
3397 | 0 | return CKG_MGF1_SHA256; |
3398 | 987 | } |
3399 | | |
3400 | | /* Function valid for >= TLS 1.2, only. */ |
3401 | | static CK_MECHANISM_TYPE |
3402 | | ssl3_GetHashMechanismByHashType(SSLHashType hashType) |
3403 | 37.1k | { |
3404 | 37.1k | switch (hashType) { |
3405 | 182 | case ssl_hash_sha512: |
3406 | 182 | return CKM_SHA512; |
3407 | 4.58k | case ssl_hash_sha384: |
3408 | 4.58k | return CKM_SHA384; |
3409 | 4.13k | case ssl_hash_sha256: |
3410 | 32.3k | case ssl_hash_none: |
3411 | | /* ssl_hash_none is for pre-1.2 suites, which use SHA-256. */ |
3412 | 32.3k | return CKM_SHA256; |
3413 | 0 | case ssl_hash_sha1: |
3414 | 0 | return CKM_SHA_1; |
3415 | 0 | default: |
3416 | 0 | PORT_Assert(0); |
3417 | 37.1k | } |
3418 | 0 | return CKM_SHA256; |
3419 | 37.1k | } |
3420 | | |
3421 | | /* Function valid for >= TLS 1.2, only. */ |
3422 | | static CK_MECHANISM_TYPE |
3423 | | ssl3_GetPrfHashMechanism(sslSocket *ss) |
3424 | 36.1k | { |
3425 | 36.1k | return ssl3_GetHashMechanismByHashType(ss->ssl3.hs.suite_def->prf_hash); |
3426 | 36.1k | } |
3427 | | |
3428 | | static SSLHashType |
3429 | | ssl3_GetSuitePrfHash(sslSocket *ss) |
3430 | 17.5k | { |
3431 | | /* ssl_hash_none is for pre-1.2 suites, which use SHA-256. */ |
3432 | 17.5k | if (ss->ssl3.hs.suite_def->prf_hash == ssl_hash_none) { |
3433 | 13.9k | return ssl_hash_sha256; |
3434 | 13.9k | } |
3435 | 3.65k | return ss->ssl3.hs.suite_def->prf_hash; |
3436 | 17.5k | } |
3437 | | |
3438 | | /* This method completes the derivation of the MS from the PMS. |
3439 | | ** |
3440 | | ** 1. Derive the MS, if possible, else return an error. |
3441 | | ** |
3442 | | ** 2. Check the version if |pms_version| is non-zero and if wrong, |
3443 | | ** return an error. |
3444 | | ** |
3445 | | ** 3. If |msp| is nonzero, return MS in |*msp|. |
3446 | | |
3447 | | ** Called from: |
3448 | | ** ssl3_ComputeMasterSecretInt |
3449 | | ** tls_ComputeExtendedMasterSecretInt |
3450 | | */ |
3451 | | static SECStatus |
3452 | | ssl3_ComputeMasterSecretFinish(sslSocket *ss, |
3453 | | CK_MECHANISM_TYPE master_derive, |
3454 | | CK_MECHANISM_TYPE key_derive, |
3455 | | CK_VERSION *pms_version, |
3456 | | SECItem *params, CK_FLAGS keyFlags, |
3457 | | PK11SymKey *pms, PK11SymKey **msp) |
3458 | 33.2k | { |
3459 | 33.2k | PK11SymKey *ms = NULL; |
3460 | | |
3461 | 33.2k | ms = PK11_DeriveWithFlags(pms, master_derive, |
3462 | 33.2k | params, key_derive, |
3463 | 33.2k | CKA_DERIVE, 0, keyFlags); |
3464 | 33.2k | if (!ms) { |
3465 | 0 | ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE); |
3466 | 0 | return SECFailure; |
3467 | 0 | } |
3468 | | |
3469 | 33.2k | if (pms_version && ss->opt.detectRollBack) { |
3470 | 5.35k | SSL3ProtocolVersion client_version; |
3471 | 5.35k | client_version = pms_version->major << 8 | pms_version->minor; |
3472 | | |
3473 | 5.35k | if (IS_DTLS(ss)) { |
3474 | 0 | client_version = dtls_DTLSVersionToTLSVersion(client_version); |
3475 | 0 | } |
3476 | | |
3477 | 5.35k | if (client_version != ss->clientHelloVersion) { |
3478 | | /* Destroy MS. Version roll-back detected. */ |
3479 | 0 | PK11_FreeSymKey(ms); |
3480 | 0 | ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE); |
3481 | 0 | return SECFailure; |
3482 | 0 | } |
3483 | 5.35k | } |
3484 | | |
3485 | 33.2k | if (msp) { |
3486 | 33.2k | *msp = ms; |
3487 | 33.2k | } else { |
3488 | 0 | PK11_FreeSymKey(ms); |
3489 | 0 | } |
3490 | | |
3491 | 33.2k | return SECSuccess; |
3492 | 33.2k | } |
3493 | | |
3494 | | /* Compute the ordinary (pre draft-ietf-tls-session-hash) master |
3495 | | ** secret and return it in |*msp|. |
3496 | | ** |
3497 | | ** Called from: ssl3_ComputeMasterSecret |
3498 | | */ |
3499 | | static SECStatus |
3500 | | ssl3_ComputeMasterSecretInt(sslSocket *ss, PK11SymKey *pms, |
3501 | | PK11SymKey **msp) |
3502 | 32.9k | { |
3503 | 32.9k | PRBool isTLS = (PRBool)(ss->version > SSL_LIBRARY_VERSION_3_0); |
3504 | 32.9k | PRBool isTLS12 = (PRBool)(ss->version >= SSL_LIBRARY_VERSION_TLS_1_2); |
3505 | | /* |
3506 | | * Whenever isDH is true, we need to use CKM_TLS_MASTER_KEY_DERIVE_DH |
3507 | | * which, unlike CKM_TLS_MASTER_KEY_DERIVE, converts arbitrary size |
3508 | | * data into a 48-byte value, and does not expect to return the version. |
3509 | | */ |
3510 | 32.9k | PRBool isDH = (PRBool)((ss->ssl3.hs.kea_def->exchKeyType == ssl_kea_dh) || |
3511 | 32.9k | (ss->ssl3.hs.kea_def->exchKeyType == ssl_kea_ecdh) || |
3512 | 32.9k | (ss->ssl3.hs.kea_def->exchKeyType == ssl_kea_ecdh_hybrid)); |
3513 | 32.9k | CK_MECHANISM_TYPE master_derive; |
3514 | 32.9k | CK_MECHANISM_TYPE key_derive; |
3515 | 32.9k | SECItem params; |
3516 | 32.9k | CK_FLAGS keyFlags; |
3517 | 32.9k | CK_VERSION pms_version; |
3518 | 32.9k | CK_VERSION *pms_version_ptr = NULL; |
3519 | | /* master_params may be used as a CK_SSL3_MASTER_KEY_DERIVE_PARAMS */ |
3520 | 32.9k | CK_TLS12_MASTER_KEY_DERIVE_PARAMS master_params; |
3521 | 32.9k | unsigned int master_params_len; |
3522 | | |
3523 | | /* if we are using TLS and we aren't using the extended master secret, |
3524 | | * and SEC_OID_TLS_REQUIRE_EMS policy is true, fail. The caller will |
3525 | | * send an alert (eventually). In the RSA Server case, the alert |
3526 | | * won't happen until Finish time because the upper level code |
3527 | | * can't tell a difference between this failure and an RSA decrypt |
3528 | | * failure, so it will proceed with a faux key */ |
3529 | 32.9k | if (isTLS) { |
3530 | 32.9k | PRUint32 policy; |
3531 | 32.9k | SECStatus rv; |
3532 | | |
3533 | | /* first fetch the policy for this algorithm */ |
3534 | 32.9k | rv = NSS_GetAlgorithmPolicy(SEC_OID_TLS_REQUIRE_EMS, &policy); |
3535 | | /* we only look at the policy if we can fetch it. */ |
3536 | 32.9k | if ((rv == SECSuccess) && (policy & NSS_USE_ALG_IN_SSL_KX)) { |
3537 | | /* just set the error, we don't want to map any errors |
3538 | | * set by NSS_GetAlgorithmPolicy here */ |
3539 | 0 | PORT_SetError(SSL_ERROR_MISSING_EXTENDED_MASTER_SECRET); |
3540 | 0 | return SECFailure; |
3541 | 0 | } |
3542 | 32.9k | } |
3543 | | |
3544 | 32.9k | if (isTLS12) { |
3545 | 8.78k | if (isDH) |
3546 | 4.01k | master_derive = CKM_TLS12_MASTER_KEY_DERIVE_DH; |
3547 | 4.77k | else |
3548 | 4.77k | master_derive = CKM_TLS12_MASTER_KEY_DERIVE; |
3549 | 8.78k | key_derive = CKM_TLS12_KEY_AND_MAC_DERIVE; |
3550 | 8.78k | keyFlags = CKF_SIGN | CKF_VERIFY; |
3551 | 24.1k | } else if (isTLS) { |
3552 | 24.1k | if (isDH) |
3553 | 23.5k | master_derive = CKM_TLS_MASTER_KEY_DERIVE_DH; |
3554 | 567 | else |
3555 | 567 | master_derive = CKM_TLS_MASTER_KEY_DERIVE; |
3556 | 24.1k | key_derive = CKM_TLS_KEY_AND_MAC_DERIVE; |
3557 | 24.1k | keyFlags = CKF_SIGN | CKF_VERIFY; |
3558 | 24.1k | } else { |
3559 | 0 | if (isDH) |
3560 | 0 | master_derive = CKM_SSL3_MASTER_KEY_DERIVE_DH; |
3561 | 0 | else |
3562 | 0 | master_derive = CKM_SSL3_MASTER_KEY_DERIVE; |
3563 | 0 | key_derive = CKM_SSL3_KEY_AND_MAC_DERIVE; |
3564 | 0 | keyFlags = 0; |
3565 | 0 | } |
3566 | | |
3567 | 32.9k | if (!isDH) { |
3568 | 5.34k | pms_version_ptr = &pms_version; |
3569 | 5.34k | } |
3570 | | |
3571 | 32.9k | master_params.pVersion = pms_version_ptr; |
3572 | 32.9k | master_params.RandomInfo.pClientRandom = ss->ssl3.hs.client_random; |
3573 | 32.9k | master_params.RandomInfo.ulClientRandomLen = SSL3_RANDOM_LENGTH; |
3574 | 32.9k | master_params.RandomInfo.pServerRandom = ss->ssl3.hs.server_random; |
3575 | 32.9k | master_params.RandomInfo.ulServerRandomLen = SSL3_RANDOM_LENGTH; |
3576 | 32.9k | if (isTLS12) { |
3577 | 8.78k | master_params.prfHashMechanism = ssl3_GetPrfHashMechanism(ss); |
3578 | 8.78k | master_params_len = sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS); |
3579 | 24.1k | } else { |
3580 | | /* prfHashMechanism is not relevant with this PRF */ |
3581 | 24.1k | master_params_len = sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS); |
3582 | 24.1k | } |
3583 | | |
3584 | 32.9k | params.data = (unsigned char *)&master_params; |
3585 | 32.9k | params.len = master_params_len; |
3586 | | |
3587 | 32.9k | return ssl3_ComputeMasterSecretFinish(ss, master_derive, key_derive, |
3588 | 32.9k | pms_version_ptr, ¶ms, |
3589 | 32.9k | keyFlags, pms, msp); |
3590 | 32.9k | } |
3591 | | |
3592 | | /* Compute the draft-ietf-tls-session-hash master |
3593 | | ** secret and return it in |*msp|. |
3594 | | ** |
3595 | | ** Called from: ssl3_ComputeMasterSecret |
3596 | | */ |
3597 | | static SECStatus |
3598 | | tls_ComputeExtendedMasterSecretInt(sslSocket *ss, PK11SymKey *pms, |
3599 | | PK11SymKey **msp) |
3600 | 346 | { |
3601 | 346 | ssl3CipherSpec *pwSpec = ss->ssl3.pwSpec; |
3602 | 346 | CK_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE_PARAMS extended_master_params; |
3603 | 346 | SSL3Hashes hashes; |
3604 | | |
3605 | | /* |
3606 | | * Determine whether to use the DH/ECDH or RSA derivation modes. |
3607 | | */ |
3608 | | /* |
3609 | | * TODO(ekr@rtfm.com): Verify that the slot can handle this key expansion |
3610 | | * mode. Bug 1198298 */ |
3611 | 346 | PRBool isDH = (PRBool)((ss->ssl3.hs.kea_def->exchKeyType == ssl_kea_dh) || |
3612 | 346 | (ss->ssl3.hs.kea_def->exchKeyType == ssl_kea_ecdh) || |
3613 | 346 | (ss->ssl3.hs.kea_def->exchKeyType == ssl_kea_ecdh_hybrid)); |
3614 | 346 | CK_MECHANISM_TYPE master_derive; |
3615 | 346 | CK_MECHANISM_TYPE key_derive; |
3616 | 346 | SECItem params; |
3617 | 346 | const CK_FLAGS keyFlags = CKF_SIGN | CKF_VERIFY; |
3618 | 346 | CK_VERSION pms_version; |
3619 | 346 | CK_VERSION *pms_version_ptr = NULL; |
3620 | 346 | SECStatus rv; |
3621 | | |
3622 | 346 | rv = ssl3_ComputeHandshakeHashes(ss, pwSpec, &hashes, 0); |
3623 | 346 | if (rv != SECSuccess) { |
3624 | 0 | PORT_Assert(0); /* Should never fail */ |
3625 | 0 | ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE); |
3626 | 0 | return SECFailure; |
3627 | 0 | } |
3628 | | |
3629 | 346 | if (isDH) { |
3630 | 336 | master_derive = CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE_DH; |
3631 | 336 | } else { |
3632 | 10 | master_derive = CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE; |
3633 | 10 | pms_version_ptr = &pms_version; |
3634 | 10 | } |
3635 | | |
3636 | 346 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_2) { |
3637 | | /* TLS 1.2+ */ |
3638 | 340 | extended_master_params.prfHashMechanism = ssl3_GetPrfHashMechanism(ss); |
3639 | 340 | key_derive = CKM_TLS12_KEY_AND_MAC_DERIVE; |
3640 | 340 | } else { |
3641 | | /* TLS < 1.2 */ |
3642 | 6 | extended_master_params.prfHashMechanism = CKM_TLS_PRF; |
3643 | 6 | key_derive = CKM_TLS_KEY_AND_MAC_DERIVE; |
3644 | 6 | } |
3645 | | |
3646 | 346 | extended_master_params.pVersion = pms_version_ptr; |
3647 | 346 | extended_master_params.pSessionHash = hashes.u.raw; |
3648 | 346 | extended_master_params.ulSessionHashLen = hashes.len; |
3649 | | |
3650 | 346 | params.data = (unsigned char *)&extended_master_params; |
3651 | 346 | params.len = sizeof extended_master_params; |
3652 | | |
3653 | 346 | return ssl3_ComputeMasterSecretFinish(ss, master_derive, key_derive, |
3654 | 346 | pms_version_ptr, ¶ms, |
3655 | 346 | keyFlags, pms, msp); |
3656 | 346 | } |
3657 | | |
3658 | | /* Wrapper method to compute the master secret and return it in |*msp|. |
3659 | | ** |
3660 | | ** Called from ssl3_ComputeMasterSecret |
3661 | | */ |
3662 | | static SECStatus |
3663 | | ssl3_ComputeMasterSecret(sslSocket *ss, PK11SymKey *pms, |
3664 | | PK11SymKey **msp) |
3665 | 33.2k | { |
3666 | 33.2k | PORT_Assert(pms != NULL); |
3667 | 33.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3668 | | |
3669 | 33.2k | if (ssl3_ExtensionNegotiated(ss, ssl_extended_master_secret_xtn)) { |
3670 | 346 | return tls_ComputeExtendedMasterSecretInt(ss, pms, msp); |
3671 | 32.9k | } else { |
3672 | 32.9k | return ssl3_ComputeMasterSecretInt(ss, pms, msp); |
3673 | 32.9k | } |
3674 | 33.2k | } |
3675 | | |
3676 | | /* |
3677 | | * Derive encryption and MAC Keys (and IVs) from master secret |
3678 | | * Sets a useful error code when returning SECFailure. |
3679 | | * |
3680 | | * Called only from ssl3_InitPendingCipherSpec(), |
3681 | | * which in turn is called from |
3682 | | * ssl3_SendRSAClientKeyExchange (for Full handshake) |
3683 | | * ssl3_SendDHClientKeyExchange (for Full handshake) |
3684 | | * ssl3_HandleClientKeyExchange (for Full handshake) |
3685 | | * ssl3_HandleServerHello (for session restart) |
3686 | | * ssl3_HandleClientHello (for session restart) |
3687 | | * Caller MUST hold the specWriteLock, and SSL3HandshakeLock. |
3688 | | * ssl3_InitPendingCipherSpec does that. |
3689 | | * |
3690 | | */ |
3691 | | static SECStatus |
3692 | | ssl3_DeriveConnectionKeys(sslSocket *ss, PK11SymKey *masterSecret) |
3693 | 33.2k | { |
3694 | 33.2k | ssl3CipherSpec *pwSpec = ss->ssl3.pwSpec; |
3695 | 33.2k | ssl3CipherSpec *prSpec = ss->ssl3.prSpec; |
3696 | 33.2k | ssl3CipherSpec *clientSpec; |
3697 | 33.2k | ssl3CipherSpec *serverSpec; |
3698 | 33.2k | PRBool isTLS = (PRBool)(ss->version > SSL_LIBRARY_VERSION_3_0); |
3699 | 33.2k | PRBool isTLS12 = |
3700 | 33.2k | (PRBool)(isTLS && ss->version >= SSL_LIBRARY_VERSION_TLS_1_2); |
3701 | 33.2k | const ssl3BulkCipherDef *cipher_def = pwSpec->cipherDef; |
3702 | 33.2k | PK11SlotInfo *slot = NULL; |
3703 | 33.2k | PK11SymKey *derivedKeyHandle = NULL; |
3704 | 33.2k | void *pwArg = ss->pkcs11PinArg; |
3705 | 33.2k | int keySize; |
3706 | 33.2k | CK_TLS12_KEY_MAT_PARAMS key_material_params; /* may be used as a |
3707 | | * CK_SSL3_KEY_MAT_PARAMS */ |
3708 | 33.2k | unsigned int key_material_params_len; |
3709 | 33.2k | CK_SSL3_KEY_MAT_OUT returnedKeys; |
3710 | 33.2k | CK_MECHANISM_TYPE key_derive; |
3711 | 33.2k | CK_MECHANISM_TYPE bulk_mechanism; |
3712 | 33.2k | SSLCipherAlgorithm calg; |
3713 | 33.2k | SECItem params; |
3714 | 33.2k | PRBool skipKeysAndIVs = (PRBool)(cipher_def->calg == ssl_calg_null); |
3715 | | |
3716 | 33.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3717 | 33.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); |
3718 | 33.2k | PORT_Assert(masterSecret); |
3719 | | |
3720 | | /* These functions operate in terms of who is writing specs. */ |
3721 | 33.2k | if (ss->sec.isServer) { |
3722 | 0 | clientSpec = prSpec; |
3723 | 0 | serverSpec = pwSpec; |
3724 | 33.2k | } else { |
3725 | 33.2k | clientSpec = pwSpec; |
3726 | 33.2k | serverSpec = prSpec; |
3727 | 33.2k | } |
3728 | | |
3729 | | /* |
3730 | | * generate the key material |
3731 | | */ |
3732 | 33.2k | if (cipher_def->type == type_block && |
3733 | 33.2k | ss->version >= SSL_LIBRARY_VERSION_TLS_1_1) { |
3734 | | /* Block ciphers in >= TLS 1.1 use a per-record, explicit IV. */ |
3735 | 6.72k | key_material_params.ulIVSizeInBits = 0; |
3736 | 6.72k | PORT_Memset(clientSpec->keyMaterial.iv, 0, cipher_def->iv_size); |
3737 | 6.72k | PORT_Memset(serverSpec->keyMaterial.iv, 0, cipher_def->iv_size); |
3738 | 6.72k | } |
3739 | | |
3740 | 33.2k | key_material_params.bIsExport = PR_FALSE; |
3741 | 33.2k | key_material_params.RandomInfo.pClientRandom = ss->ssl3.hs.client_random; |
3742 | 33.2k | key_material_params.RandomInfo.ulClientRandomLen = SSL3_RANDOM_LENGTH; |
3743 | 33.2k | key_material_params.RandomInfo.pServerRandom = ss->ssl3.hs.server_random; |
3744 | 33.2k | key_material_params.RandomInfo.ulServerRandomLen = SSL3_RANDOM_LENGTH; |
3745 | 33.2k | key_material_params.pReturnedKeyMaterial = &returnedKeys; |
3746 | | |
3747 | 33.2k | if (skipKeysAndIVs) { |
3748 | 3.24k | keySize = 0; |
3749 | 3.24k | returnedKeys.pIVClient = NULL; |
3750 | 3.24k | returnedKeys.pIVServer = NULL; |
3751 | 3.24k | key_material_params.ulKeySizeInBits = 0; |
3752 | 3.24k | key_material_params.ulIVSizeInBits = 0; |
3753 | 30.0k | } else { |
3754 | 30.0k | keySize = cipher_def->key_size; |
3755 | 30.0k | returnedKeys.pIVClient = clientSpec->keyMaterial.iv; |
3756 | 30.0k | returnedKeys.pIVServer = serverSpec->keyMaterial.iv; |
3757 | 30.0k | key_material_params.ulKeySizeInBits = cipher_def->secret_key_size * BPB; |
3758 | 30.0k | key_material_params.ulIVSizeInBits = cipher_def->iv_size * BPB; |
3759 | 30.0k | } |
3760 | 33.2k | key_material_params.ulMacSizeInBits = pwSpec->macDef->mac_size * BPB; |
3761 | | |
3762 | 33.2k | calg = cipher_def->calg; |
3763 | 33.2k | bulk_mechanism = ssl3_Alg2Mech(calg); |
3764 | | |
3765 | 33.2k | if (isTLS12) { |
3766 | 9.12k | key_derive = CKM_TLS12_KEY_AND_MAC_DERIVE; |
3767 | 9.12k | key_material_params.prfHashMechanism = ssl3_GetPrfHashMechanism(ss); |
3768 | 9.12k | key_material_params_len = sizeof(CK_TLS12_KEY_MAT_PARAMS); |
3769 | 24.1k | } else if (isTLS) { |
3770 | 24.1k | key_derive = CKM_TLS_KEY_AND_MAC_DERIVE; |
3771 | 24.1k | key_material_params_len = sizeof(CK_SSL3_KEY_MAT_PARAMS); |
3772 | 24.1k | } else { |
3773 | 0 | key_derive = CKM_SSL3_KEY_AND_MAC_DERIVE; |
3774 | 0 | key_material_params_len = sizeof(CK_SSL3_KEY_MAT_PARAMS); |
3775 | 0 | } |
3776 | | |
3777 | 33.2k | params.data = (unsigned char *)&key_material_params; |
3778 | 33.2k | params.len = key_material_params_len; |
3779 | | |
3780 | | /* CKM_SSL3_KEY_AND_MAC_DERIVE is defined to set ENCRYPT, DECRYPT, and |
3781 | | * DERIVE by DEFAULT */ |
3782 | 33.2k | derivedKeyHandle = PK11_Derive(masterSecret, key_derive, ¶ms, |
3783 | 33.2k | bulk_mechanism, CKA_ENCRYPT, keySize); |
3784 | 33.2k | if (!derivedKeyHandle) { |
3785 | 0 | ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE); |
3786 | 0 | return SECFailure; |
3787 | 0 | } |
3788 | | /* we really should use the actual mac'ing mechanism here, but we |
3789 | | * don't because these types are used to map keytype anyway and both |
3790 | | * mac's map to the same keytype. |
3791 | | */ |
3792 | 33.2k | slot = PK11_GetSlotFromKey(derivedKeyHandle); |
3793 | | |
3794 | 33.2k | PK11_FreeSlot(slot); /* slot is held until the key is freed */ |
3795 | 33.2k | clientSpec->keyMaterial.macKey = |
3796 | 33.2k | PK11_SymKeyFromHandle(slot, derivedKeyHandle, PK11_OriginDerive, |
3797 | 33.2k | CKM_SSL3_SHA1_MAC, returnedKeys.hClientMacSecret, |
3798 | 33.2k | PR_TRUE, pwArg); |
3799 | 33.2k | if (clientSpec->keyMaterial.macKey == NULL) { |
3800 | 0 | goto loser; /* loser sets err */ |
3801 | 0 | } |
3802 | 33.2k | serverSpec->keyMaterial.macKey = |
3803 | 33.2k | PK11_SymKeyFromHandle(slot, derivedKeyHandle, PK11_OriginDerive, |
3804 | 33.2k | CKM_SSL3_SHA1_MAC, returnedKeys.hServerMacSecret, |
3805 | 33.2k | PR_TRUE, pwArg); |
3806 | 33.2k | if (serverSpec->keyMaterial.macKey == NULL) { |
3807 | 0 | goto loser; /* loser sets err */ |
3808 | 0 | } |
3809 | 33.2k | if (!skipKeysAndIVs) { |
3810 | 30.0k | clientSpec->keyMaterial.key = |
3811 | 30.0k | PK11_SymKeyFromHandle(slot, derivedKeyHandle, PK11_OriginDerive, |
3812 | 30.0k | bulk_mechanism, returnedKeys.hClientKey, |
3813 | 30.0k | PR_TRUE, pwArg); |
3814 | 30.0k | if (clientSpec->keyMaterial.key == NULL) { |
3815 | 0 | goto loser; /* loser sets err */ |
3816 | 0 | } |
3817 | 30.0k | serverSpec->keyMaterial.key = |
3818 | 30.0k | PK11_SymKeyFromHandle(slot, derivedKeyHandle, PK11_OriginDerive, |
3819 | 30.0k | bulk_mechanism, returnedKeys.hServerKey, |
3820 | 30.0k | PR_TRUE, pwArg); |
3821 | 30.0k | if (serverSpec->keyMaterial.key == NULL) { |
3822 | 0 | goto loser; /* loser sets err */ |
3823 | 0 | } |
3824 | 30.0k | } |
3825 | 33.2k | PK11_FreeSymKey(derivedKeyHandle); |
3826 | 33.2k | return SECSuccess; |
3827 | | |
3828 | 0 | loser: |
3829 | 0 | PK11_FreeSymKey(derivedKeyHandle); |
3830 | 0 | ssl_MapLowLevelError(SSL_ERROR_SESSION_KEY_GEN_FAILURE); |
3831 | 0 | return SECFailure; |
3832 | 33.2k | } |
3833 | | |
3834 | | void |
3835 | | ssl3_CoalesceEchHandshakeHashes(sslSocket *ss) |
3836 | 346 | { |
3837 | | /* |sha| contains the CHOuter transcript, which is the singular |
3838 | | * transcript if not doing ECH. If the server responded with 1.2, |
3839 | | * contexts are not yet initialized. */ |
3840 | 346 | if (ss->ssl3.hs.echAccepted) { |
3841 | 0 | if (ss->ssl3.hs.sha) { |
3842 | 0 | PORT_Assert(ss->ssl3.hs.shaEchInner); |
3843 | 0 | PK11_DestroyContext(ss->ssl3.hs.sha, PR_TRUE); |
3844 | 0 | ss->ssl3.hs.sha = ss->ssl3.hs.shaEchInner; |
3845 | 0 | ss->ssl3.hs.shaEchInner = NULL; |
3846 | 0 | } |
3847 | 346 | } else { |
3848 | 346 | if (ss->ssl3.hs.shaEchInner) { |
3849 | 190 | PK11_DestroyContext(ss->ssl3.hs.shaEchInner, PR_TRUE); |
3850 | 190 | ss->ssl3.hs.shaEchInner = NULL; |
3851 | 190 | } |
3852 | 346 | } |
3853 | 346 | } |
3854 | | |
3855 | | /* ssl3_InitHandshakeHashes creates handshake hash contexts and hashes in |
3856 | | * buffered messages in ss->ssl3.hs.messages. Called from |
3857 | | * ssl3_NegotiateCipherSuite(), tls13_HandleClientHelloPart2(), |
3858 | | * and ssl3_HandleServerHello. */ |
3859 | | SECStatus |
3860 | | ssl3_InitHandshakeHashes(sslSocket *ss) |
3861 | 39.4k | { |
3862 | 39.4k | SSL_TRC(30, ("%d: SSL3[%d]: start handshake hashes", SSL_GETPID(), ss->fd)); |
3863 | | |
3864 | 39.4k | PORT_Assert(ss->ssl3.hs.hashType == handshake_hash_unknown); |
3865 | 39.4k | if (ss->version == SSL_LIBRARY_VERSION_TLS_1_2) { |
3866 | 12.5k | ss->ssl3.hs.hashType = handshake_hash_record; |
3867 | 26.9k | } else { |
3868 | 26.9k | PORT_Assert(!ss->ssl3.hs.md5 && !ss->ssl3.hs.sha); |
3869 | | /* |
3870 | | * note: We should probably lookup an SSL3 slot for these |
3871 | | * handshake hashes in hopes that we wind up with the same slots |
3872 | | * that the master secret will wind up in ... |
3873 | | */ |
3874 | 26.9k | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
3875 | | /* determine the hash from the prf */ |
3876 | 659 | const SECOidData *hash_oid = |
3877 | 659 | SECOID_FindOIDByMechanism(ssl3_GetPrfHashMechanism(ss)); |
3878 | | |
3879 | | /* Get the PKCS #11 mechanism for the Hash from the cipher suite (prf_hash) |
3880 | | * Convert that to the OidTag. We can then use that OidTag to create our |
3881 | | * PK11Context */ |
3882 | 659 | PORT_Assert(hash_oid != NULL); |
3883 | 659 | if (hash_oid == NULL) { |
3884 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
3885 | 0 | return SECFailure; |
3886 | 0 | } |
3887 | | |
3888 | 659 | ss->ssl3.hs.sha = PK11_CreateDigestContext(hash_oid->offset); |
3889 | 659 | if (ss->ssl3.hs.sha == NULL) { |
3890 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
3891 | 0 | return SECFailure; |
3892 | 0 | } |
3893 | 659 | ss->ssl3.hs.hashType = handshake_hash_single; |
3894 | 659 | if (PK11_DigestBegin(ss->ssl3.hs.sha) != SECSuccess) { |
3895 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
3896 | 0 | return SECFailure; |
3897 | 0 | } |
3898 | | |
3899 | | /* Transcript hash used on ECH client. */ |
3900 | 659 | if (!ss->sec.isServer && ss->ssl3.hs.echHpkeCtx) { |
3901 | 277 | ss->ssl3.hs.shaEchInner = PK11_CreateDigestContext(hash_oid->offset); |
3902 | 277 | if (ss->ssl3.hs.shaEchInner == NULL) { |
3903 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
3904 | 0 | return SECFailure; |
3905 | 0 | } |
3906 | 277 | if (PK11_DigestBegin(ss->ssl3.hs.shaEchInner) != SECSuccess) { |
3907 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
3908 | 0 | return SECFailure; |
3909 | 0 | } |
3910 | 277 | } |
3911 | 26.2k | } else { |
3912 | | /* Both ss->ssl3.hs.md5 and ss->ssl3.hs.sha should be NULL or |
3913 | | * created successfully. */ |
3914 | 26.2k | ss->ssl3.hs.md5 = PK11_CreateDigestContext(SEC_OID_MD5); |
3915 | 26.2k | if (ss->ssl3.hs.md5 == NULL) { |
3916 | 0 | ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); |
3917 | 0 | return SECFailure; |
3918 | 0 | } |
3919 | 26.2k | ss->ssl3.hs.sha = PK11_CreateDigestContext(SEC_OID_SHA1); |
3920 | 26.2k | if (ss->ssl3.hs.sha == NULL) { |
3921 | 0 | PK11_DestroyContext(ss->ssl3.hs.md5, PR_TRUE); |
3922 | 0 | ss->ssl3.hs.md5 = NULL; |
3923 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
3924 | 0 | return SECFailure; |
3925 | 0 | } |
3926 | 26.2k | ss->ssl3.hs.hashType = handshake_hash_combo; |
3927 | | |
3928 | 26.2k | if (PK11_DigestBegin(ss->ssl3.hs.md5) != SECSuccess) { |
3929 | 0 | ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); |
3930 | 0 | return SECFailure; |
3931 | 0 | } |
3932 | 26.2k | if (PK11_DigestBegin(ss->ssl3.hs.sha) != SECSuccess) { |
3933 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
3934 | 0 | return SECFailure; |
3935 | 0 | } |
3936 | 26.2k | } |
3937 | 26.9k | } |
3938 | | |
3939 | 39.4k | if (ss->ssl3.hs.hashType != handshake_hash_record && |
3940 | 39.4k | ss->ssl3.hs.messages.len > 0) { |
3941 | | /* When doing ECH, ssl3_UpdateHandshakeHashes will store outer messages |
3942 | | * into the both the outer and inner transcripts. |
3943 | | * ssl3_UpdateDefaultHandshakeHashes uses the default context which is |
3944 | | * the outer when doing client ECH. For ECH shared-mode or backend |
3945 | | * servers only the hs.messages buffer is used. */ |
3946 | 26.9k | if (ssl3_UpdateDefaultHandshakeHashes(ss, ss->ssl3.hs.messages.buf, |
3947 | 26.9k | ss->ssl3.hs.messages.len) != SECSuccess) { |
3948 | 0 | return SECFailure; |
3949 | 0 | } |
3950 | | /* When doing ECH, deriving the accept_confirmation value requires all |
3951 | | * messages up to and including the ServerHello |
3952 | | * (see draft-ietf-tls-esni-14, Section 7.2). |
3953 | | * |
3954 | | * Don't free the transcript buffer until confirmation calculation. */ |
3955 | 26.9k | if (!ss->ssl3.hs.echHpkeCtx && !ss->opt.enableTls13BackendEch) { |
3956 | 25.6k | sslBuffer_Clear(&ss->ssl3.hs.messages); |
3957 | 25.6k | } |
3958 | 26.9k | } |
3959 | 39.4k | if (ss->ssl3.hs.shaEchInner && |
3960 | 39.4k | ss->ssl3.hs.echInnerMessages.len > 0) { |
3961 | 277 | if (PK11_DigestOp(ss->ssl3.hs.shaEchInner, ss->ssl3.hs.echInnerMessages.buf, |
3962 | 277 | ss->ssl3.hs.echInnerMessages.len) != SECSuccess) { |
3963 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
3964 | 0 | return SECFailure; |
3965 | 0 | } |
3966 | 277 | if (!ss->ssl3.hs.echHpkeCtx) { |
3967 | 0 | sslBuffer_Clear(&ss->ssl3.hs.echInnerMessages); |
3968 | 0 | } |
3969 | 277 | } |
3970 | | |
3971 | 39.4k | return SECSuccess; |
3972 | 39.4k | } |
3973 | | |
3974 | | void |
3975 | | ssl3_RestartHandshakeHashes(sslSocket *ss) |
3976 | 41.5k | { |
3977 | 41.5k | SSL_TRC(30, ("%d: SSL3[%d]: reset handshake hashes", |
3978 | 41.5k | SSL_GETPID(), ss->fd)); |
3979 | 41.5k | ss->ssl3.hs.hashType = handshake_hash_unknown; |
3980 | 41.5k | ss->ssl3.hs.messages.len = 0; |
3981 | 41.5k | ss->ssl3.hs.echInnerMessages.len = 0; |
3982 | 41.5k | if (ss->ssl3.hs.md5) { |
3983 | 23.0k | PK11_DestroyContext(ss->ssl3.hs.md5, PR_TRUE); |
3984 | 23.0k | ss->ssl3.hs.md5 = NULL; |
3985 | 23.0k | } |
3986 | 41.5k | if (ss->ssl3.hs.sha) { |
3987 | 23.0k | PK11_DestroyContext(ss->ssl3.hs.sha, PR_TRUE); |
3988 | 23.0k | ss->ssl3.hs.sha = NULL; |
3989 | 23.0k | } |
3990 | 41.5k | if (ss->ssl3.hs.shaEchInner) { |
3991 | 0 | PK11_DestroyContext(ss->ssl3.hs.shaEchInner, PR_TRUE); |
3992 | 0 | ss->ssl3.hs.shaEchInner = NULL; |
3993 | 0 | } |
3994 | 41.5k | if (ss->ssl3.hs.shaPostHandshake) { |
3995 | 0 | PK11_DestroyContext(ss->ssl3.hs.shaPostHandshake, PR_TRUE); |
3996 | 0 | ss->ssl3.hs.shaPostHandshake = NULL; |
3997 | 0 | } |
3998 | 41.5k | } |
3999 | | |
4000 | | /* Add the provided bytes to the handshake hash context. When doing |
4001 | | * TLS 1.3 ECH, |target| may be provided to specify only the inner/outer |
4002 | | * transcript, else the input is added to both contexts. This happens |
4003 | | * only on the client. On the server, only the default context is used. */ |
4004 | | SECStatus |
4005 | | ssl3_UpdateHandshakeHashesInt(sslSocket *ss, const unsigned char *b, |
4006 | | unsigned int l, sslBuffer *target) |
4007 | 610k | { |
4008 | | |
4009 | 610k | SECStatus rv = SECSuccess; |
4010 | 610k | PRBool explicit = (target != NULL); |
4011 | 610k | PRBool appendToEchInner = !ss->sec.isServer && |
4012 | 610k | ss->ssl3.hs.echHpkeCtx && |
4013 | 610k | !explicit; |
4014 | 610k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4015 | 610k | PORT_Assert(target != &ss->ssl3.hs.echInnerMessages || |
4016 | 610k | !ss->sec.isServer); |
4017 | | |
4018 | 610k | if (target == NULL) { |
4019 | | /* Default context. */ |
4020 | 575k | target = &ss->ssl3.hs.messages; |
4021 | 575k | } |
4022 | | /* With TLS 1.3, and versions TLS.1.1 and older, we keep the hash(es) |
4023 | | * always up to date. However, we must initially buffer the handshake |
4024 | | * messages, until we know what to do. |
4025 | | * If ss->ssl3.hs.hashType != handshake_hash_unknown, |
4026 | | * it means we know what to do. We calculate (hash our input), |
4027 | | * and we stop appending to the buffer. |
4028 | | * |
4029 | | * With TLS 1.2, we always append all handshake messages, |
4030 | | * and never update the hash, because the hash function we must use for |
4031 | | * certificate_verify might be different from the hash function we use |
4032 | | * when signing other handshake hashes. */ |
4033 | 610k | if (ss->ssl3.hs.hashType == handshake_hash_unknown || |
4034 | 610k | ss->ssl3.hs.hashType == handshake_hash_record) { |
4035 | 206k | rv = sslBuffer_Append(target, b, l); |
4036 | 206k | if (rv != SECSuccess) { |
4037 | 0 | return SECFailure; |
4038 | 0 | } |
4039 | 206k | if (appendToEchInner) { |
4040 | 12.0k | return sslBuffer_Append(&ss->ssl3.hs.echInnerMessages, b, l); |
4041 | 12.0k | } |
4042 | 194k | return SECSuccess; |
4043 | 206k | } |
4044 | | |
4045 | 404k | PRINT_BUF(90, (ss, "handshake hash input:", b, l)); |
4046 | | |
4047 | 404k | if (ss->ssl3.hs.hashType == handshake_hash_single) { |
4048 | 3.68k | PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
4049 | 3.68k | if (target == &ss->ssl3.hs.messages) { |
4050 | 3.68k | rv = PK11_DigestOp(ss->ssl3.hs.sha, b, l); |
4051 | 3.68k | if (rv != SECSuccess) { |
4052 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
4053 | 0 | return rv; |
4054 | 0 | } |
4055 | 3.68k | } |
4056 | 3.68k | if (ss->ssl3.hs.shaEchInner && |
4057 | 3.68k | (target == &ss->ssl3.hs.echInnerMessages || !explicit)) { |
4058 | 496 | rv = PK11_DigestOp(ss->ssl3.hs.shaEchInner, b, l); |
4059 | 496 | if (rv != SECSuccess) { |
4060 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
4061 | 0 | return rv; |
4062 | 0 | } |
4063 | 496 | } |
4064 | 400k | } else if (ss->ssl3.hs.hashType == handshake_hash_combo) { |
4065 | 400k | rv = PK11_DigestOp(ss->ssl3.hs.md5, b, l); |
4066 | 400k | if (rv != SECSuccess) { |
4067 | 0 | ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); |
4068 | 0 | return rv; |
4069 | 0 | } |
4070 | 400k | rv = PK11_DigestOp(ss->ssl3.hs.sha, b, l); |
4071 | 400k | if (rv != SECSuccess) { |
4072 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
4073 | 0 | return rv; |
4074 | 0 | } |
4075 | 400k | } |
4076 | 404k | return rv; |
4077 | 404k | } |
4078 | | |
4079 | | static SECStatus |
4080 | | ssl3_UpdateDefaultHandshakeHashes(sslSocket *ss, const unsigned char *b, |
4081 | | unsigned int l) |
4082 | 31.1k | { |
4083 | 31.1k | return ssl3_UpdateHandshakeHashesInt(ss, b, l, |
4084 | 31.1k | &ss->ssl3.hs.messages); |
4085 | 31.1k | } |
4086 | | |
4087 | | static SECStatus |
4088 | | ssl3_UpdateInnerHandshakeHashes(sslSocket *ss, const unsigned char *b, |
4089 | | unsigned int l) |
4090 | 312 | { |
4091 | 312 | return ssl3_UpdateHandshakeHashesInt(ss, b, l, |
4092 | 312 | &ss->ssl3.hs.echInnerMessages); |
4093 | 312 | } |
4094 | | |
4095 | | /* |
4096 | | * Handshake messages |
4097 | | */ |
4098 | | /* Called from ssl3_InitHandshakeHashes() |
4099 | | ** ssl3_AppendHandshake() |
4100 | | ** ssl3_HandleV2ClientHello() |
4101 | | ** ssl3_HandleHandshakeMessage() |
4102 | | ** Caller must hold the ssl3Handshake lock. |
4103 | | */ |
4104 | | SECStatus |
4105 | | ssl3_UpdateHandshakeHashes(sslSocket *ss, const unsigned char *b, unsigned int l) |
4106 | 575k | { |
4107 | 575k | return ssl3_UpdateHandshakeHashesInt(ss, b, l, NULL); |
4108 | 575k | } |
4109 | | |
4110 | | SECStatus |
4111 | | ssl3_UpdatePostHandshakeHashes(sslSocket *ss, const unsigned char *b, unsigned int l) |
4112 | 0 | { |
4113 | 0 | SECStatus rv = SECSuccess; |
4114 | |
|
4115 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4116 | |
|
4117 | 0 | PRINT_BUF(90, (ss, "post handshake hash input:", b, l)); |
4118 | |
|
4119 | 0 | PORT_Assert(ss->ssl3.hs.hashType == handshake_hash_single); |
4120 | 0 | PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
4121 | 0 | rv = PK11_DigestOp(ss->ssl3.hs.shaPostHandshake, b, l); |
4122 | 0 | if (rv != SECSuccess) { |
4123 | 0 | PORT_SetError(SSL_ERROR_DIGEST_FAILURE); |
4124 | 0 | } |
4125 | 0 | return rv; |
4126 | 0 | } |
4127 | | |
4128 | | /* The next two functions serve to append the handshake header. |
4129 | | The first one additionally writes to seqNumberBuffer |
4130 | | the sequence number of the message we are generating. |
4131 | | This function is used when generating the keyUpdate message in dtls13_enqueueKeyUpdateMessage. |
4132 | | */ |
4133 | | SECStatus |
4134 | | ssl3_AppendHandshakeHeaderAndStashSeqNum(sslSocket *ss, SSLHandshakeType t, PRUint32 length, PRUint64 *sendMessageSeqOut) |
4135 | 66.8k | { |
4136 | 66.8k | PORT_Assert(t != ssl_hs_client_hello); |
4137 | 66.8k | SECStatus rv; |
4138 | | |
4139 | | /* If we already have a message in place, we need to enqueue it. |
4140 | | * This empties the buffer. This is a convenient place to call |
4141 | | * dtls_StageHandshakeMessage to mark the message boundary. |
4142 | | */ |
4143 | 66.8k | if (IS_DTLS(ss)) { |
4144 | 0 | rv = dtls_StageHandshakeMessage(ss); |
4145 | 0 | if (rv != SECSuccess) { |
4146 | 0 | return rv; |
4147 | 0 | } |
4148 | 0 | } |
4149 | | |
4150 | 66.8k | SSL_TRC(30, ("%d: SSL3[%d]: append handshake header: type %s", |
4151 | 66.8k | SSL_GETPID(), ss->fd, ssl3_DecodeHandshakeType(t))); |
4152 | | |
4153 | 66.8k | rv = ssl3_AppendHandshakeNumber(ss, t, 1); |
4154 | 66.8k | if (rv != SECSuccess) { |
4155 | 0 | return rv; /* error code set by AppendHandshake, if applicable. */ |
4156 | 0 | } |
4157 | 66.8k | rv = ssl3_AppendHandshakeNumber(ss, length, 3); |
4158 | 66.8k | if (rv != SECSuccess) { |
4159 | 0 | return rv; /* error code set by AppendHandshake, if applicable. */ |
4160 | 0 | } |
4161 | | |
4162 | 66.8k | if (IS_DTLS(ss)) { |
4163 | | /* RFC 9147. 5.2. DTLS Handshake Message Format. |
4164 | | * In DTLS 1.3, the message transcript is computed over the original TLS |
4165 | | * 1.3-style Handshake messages without the message_seq, |
4166 | | * fragment_offset, and fragment_length values. Note that this is a |
4167 | | * change from DTLS 1.2 where those values were included in the transcript. */ |
4168 | 0 | PRBool suppressHash = ss->version == SSL_LIBRARY_VERSION_TLS_1_3 ? PR_TRUE : PR_FALSE; |
4169 | | |
4170 | | /* Note that we make an unfragmented message here. We fragment in the |
4171 | | * transmission code, if necessary */ |
4172 | 0 | rv = ssl3_AppendHandshakeNumberSuppressHash(ss, ss->ssl3.hs.sendMessageSeq, 2, suppressHash); |
4173 | 0 | if (rv != SECSuccess) { |
4174 | 0 | return rv; /* error code set by AppendHandshake, if applicable. */ |
4175 | 0 | } |
4176 | | /* In case if we provide a buffer for the sequence message, |
4177 | | we write down sendMessageSeq to the buffer. */ |
4178 | 0 | if (sendMessageSeqOut != NULL) { |
4179 | 0 | *sendMessageSeqOut = ss->ssl3.hs.sendMessageSeq; |
4180 | 0 | } |
4181 | 0 | ss->ssl3.hs.sendMessageSeq++; |
4182 | | |
4183 | | /* 0 is the fragment offset, because it's not fragmented yet */ |
4184 | 0 | rv = ssl3_AppendHandshakeNumberSuppressHash(ss, 0, 3, suppressHash); |
4185 | 0 | if (rv != SECSuccess) { |
4186 | 0 | return rv; /* error code set by AppendHandshake, if applicable. */ |
4187 | 0 | } |
4188 | | |
4189 | | /* Fragment length -- set to the packet length because not fragmented */ |
4190 | 0 | rv = ssl3_AppendHandshakeNumberSuppressHash(ss, length, 3, suppressHash); |
4191 | 0 | if (rv != SECSuccess) { |
4192 | 0 | return rv; /* error code set by AppendHandshake, if applicable. */ |
4193 | 0 | } |
4194 | 0 | } |
4195 | | |
4196 | 66.8k | return rv; /* error code set by AppendHandshake, if applicable. */ |
4197 | 66.8k | } |
4198 | | |
4199 | | /* The function calls the ssl3_AppendHandshakeHeaderAndStashSeqNum implemented above. |
4200 | | As in the majority of the cases we do not need the last parameter, |
4201 | | we separate out this function. */ |
4202 | | SECStatus |
4203 | | ssl3_AppendHandshakeHeader(sslSocket *ss, SSLHandshakeType t, PRUint32 length) |
4204 | 66.8k | { |
4205 | 66.8k | return ssl3_AppendHandshakeHeaderAndStashSeqNum(ss, t, length, NULL); |
4206 | 66.8k | } |
4207 | | |
4208 | | /************************************************************************** |
4209 | | * Consume Handshake functions. |
4210 | | * |
4211 | | * All data used in these functions is protected by two locks, |
4212 | | * the RecvBufLock and the SSL3HandshakeLock |
4213 | | **************************************************************************/ |
4214 | | |
4215 | | /* Read up the next "bytes" number of bytes from the (decrypted) input |
4216 | | * stream "b" (which is *length bytes long). Copy them into buffer "v". |
4217 | | * Reduces *length by bytes. Advances *b by bytes. |
4218 | | * |
4219 | | * If this function returns SECFailure, it has already sent an alert, |
4220 | | * and has set a generic error code. The caller should probably |
4221 | | * override the generic error code by setting another. |
4222 | | */ |
4223 | | SECStatus |
4224 | | ssl3_ConsumeHandshake(sslSocket *ss, void *v, PRUint32 bytes, PRUint8 **b, |
4225 | | PRUint32 *length) |
4226 | 46.8k | { |
4227 | 46.8k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
4228 | 46.8k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4229 | | |
4230 | 46.8k | if ((PRUint32)bytes > *length) { |
4231 | 23 | return ssl3_DecodeError(ss); |
4232 | 23 | } |
4233 | 46.7k | PORT_Memcpy(v, *b, bytes); |
4234 | 46.7k | PRINT_BUF(60, (ss, "consume bytes:", *b, bytes)); |
4235 | 46.7k | *b += bytes; |
4236 | 46.7k | *length -= bytes; |
4237 | 46.7k | return SECSuccess; |
4238 | 46.8k | } |
4239 | | |
4240 | | /* Read up the next "bytes" number of bytes from the (decrypted) input |
4241 | | * stream "b" (which is *length bytes long), and interpret them as an |
4242 | | * integer in network byte order. Sets *num to the received value. |
4243 | | * Reduces *length by bytes. Advances *b by bytes. |
4244 | | * |
4245 | | * On error, an alert has been sent, and a generic error code has been set. |
4246 | | */ |
4247 | | SECStatus |
4248 | | ssl3_ConsumeHandshakeNumber64(sslSocket *ss, PRUint64 *num, PRUint32 bytes, |
4249 | | PRUint8 **b, PRUint32 *length) |
4250 | 337k | { |
4251 | 337k | PRUint8 *buf = *b; |
4252 | 337k | PRUint32 i; |
4253 | | |
4254 | 337k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
4255 | 337k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4256 | | |
4257 | 337k | *num = 0; |
4258 | 337k | if (bytes > sizeof(*num)) { |
4259 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
4260 | 0 | return SECFailure; |
4261 | 0 | } |
4262 | | |
4263 | 337k | if (bytes > *length) { |
4264 | 189 | return ssl3_DecodeError(ss); |
4265 | 189 | } |
4266 | 337k | PRINT_BUF(60, (ss, "consume bytes:", *b, bytes)); |
4267 | | |
4268 | 1.00M | for (i = 0; i < bytes; i++) { |
4269 | 664k | *num = (*num << 8) + buf[i]; |
4270 | 664k | } |
4271 | 337k | *b += bytes; |
4272 | 337k | *length -= bytes; |
4273 | 337k | return SECSuccess; |
4274 | 337k | } |
4275 | | |
4276 | | SECStatus |
4277 | | ssl3_ConsumeHandshakeNumber(sslSocket *ss, PRUint32 *num, PRUint32 bytes, |
4278 | | PRUint8 **b, PRUint32 *length) |
4279 | 337k | { |
4280 | 337k | PRUint64 num64; |
4281 | 337k | SECStatus rv; |
4282 | | |
4283 | 337k | PORT_Assert(bytes <= sizeof(*num)); |
4284 | 337k | if (bytes > sizeof(*num)) { |
4285 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
4286 | 0 | return SECFailure; |
4287 | 0 | } |
4288 | 337k | rv = ssl3_ConsumeHandshakeNumber64(ss, &num64, bytes, b, length); |
4289 | 337k | if (rv != SECSuccess) { |
4290 | 189 | return SECFailure; |
4291 | 189 | } |
4292 | 337k | *num = num64 & 0xffffffff; |
4293 | 337k | return SECSuccess; |
4294 | 337k | } |
4295 | | |
4296 | | /* Read in two values from the incoming decrypted byte stream "b", which is |
4297 | | * *length bytes long. The first value is a number whose size is "bytes" |
4298 | | * bytes long. The second value is a byte-string whose size is the value |
4299 | | * of the first number received. The latter byte-string, and its length, |
4300 | | * is returned in the SECItem i. |
4301 | | * |
4302 | | * Returns SECFailure (-1) on failure. |
4303 | | * On error, an alert has been sent, and a generic error code has been set. |
4304 | | * |
4305 | | * RADICAL CHANGE for NSS 3.11. All callers of this function make copies |
4306 | | * of the data returned in the SECItem *i, so making a copy of it here |
4307 | | * is simply wasteful. So, This function now just sets SECItem *i to |
4308 | | * point to the values in the buffer **b. |
4309 | | */ |
4310 | | SECStatus |
4311 | | ssl3_ConsumeHandshakeVariable(sslSocket *ss, SECItem *i, PRUint32 bytes, |
4312 | | PRUint8 **b, PRUint32 *length) |
4313 | 79.7k | { |
4314 | 79.7k | PRUint32 count; |
4315 | 79.7k | SECStatus rv; |
4316 | | |
4317 | 79.7k | PORT_Assert(bytes <= 3); |
4318 | 79.7k | i->len = 0; |
4319 | 79.7k | i->data = NULL; |
4320 | 79.7k | i->type = siBuffer; |
4321 | 79.7k | rv = ssl3_ConsumeHandshakeNumber(ss, &count, bytes, b, length); |
4322 | 79.7k | if (rv != SECSuccess) { |
4323 | 93 | return SECFailure; |
4324 | 93 | } |
4325 | 79.6k | if (count > 0) { |
4326 | 41.6k | if (count > *length) { |
4327 | 191 | return ssl3_DecodeError(ss); |
4328 | 191 | } |
4329 | 41.4k | i->data = *b; |
4330 | 41.4k | i->len = count; |
4331 | 41.4k | *b += count; |
4332 | 41.4k | *length -= count; |
4333 | 41.4k | } |
4334 | 79.4k | return SECSuccess; |
4335 | 79.6k | } |
4336 | | |
4337 | | /* ssl3_TLSHashAlgorithmToOID converts a TLS hash identifier into an OID value. |
4338 | | * If the hash is not recognised, SEC_OID_UNKNOWN is returned. |
4339 | | * |
4340 | | * See https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ |
4341 | | SECOidTag |
4342 | | ssl3_HashTypeToOID(SSLHashType hashType) |
4343 | 743k | { |
4344 | 743k | switch (hashType) { |
4345 | 129k | case ssl_hash_sha1: |
4346 | 129k | return SEC_OID_SHA1; |
4347 | 262k | case ssl_hash_sha256: |
4348 | 262k | return SEC_OID_SHA256; |
4349 | 185k | case ssl_hash_sha384: |
4350 | 185k | return SEC_OID_SHA384; |
4351 | 162k | case ssl_hash_sha512: |
4352 | 162k | return SEC_OID_SHA512; |
4353 | 3.31k | default: |
4354 | 3.31k | break; |
4355 | 743k | } |
4356 | 3.31k | return SEC_OID_UNKNOWN; |
4357 | 743k | } |
4358 | | |
4359 | | SECOidTag |
4360 | | ssl3_AuthTypeToOID(SSLAuthType authType) |
4361 | 581k | { |
4362 | 581k | switch (authType) { |
4363 | 155k | case ssl_auth_rsa_sign: |
4364 | 155k | return SEC_OID_PKCS1_RSA_ENCRYPTION; |
4365 | 6 | case ssl_auth_rsa_pss: |
4366 | 6 | return SEC_OID_PKCS1_RSA_PSS_SIGNATURE; |
4367 | 279k | case ssl_auth_ecdsa: |
4368 | 279k | return SEC_OID_ANSIX962_EC_PUBLIC_KEY; |
4369 | 146k | case ssl_auth_dsa: |
4370 | 146k | return SEC_OID_ANSIX9_DSA_SIGNATURE; |
4371 | 0 | default: |
4372 | 0 | break; |
4373 | 581k | } |
4374 | | /* shouldn't ever get there */ |
4375 | 0 | PORT_Assert(0); |
4376 | 0 | return SEC_OID_UNKNOWN; |
4377 | 581k | } |
4378 | | |
4379 | | SSLHashType |
4380 | | ssl_SignatureSchemeToHashType(SSLSignatureScheme scheme) |
4381 | 693k | { |
4382 | 693k | switch (scheme) { |
4383 | 39.4k | case ssl_sig_rsa_pkcs1_sha1: |
4384 | 76.3k | case ssl_sig_dsa_sha1: |
4385 | 128k | case ssl_sig_ecdsa_sha1: |
4386 | 128k | return ssl_hash_sha1; |
4387 | 42.1k | case ssl_sig_rsa_pkcs1_sha256: |
4388 | 165k | case ssl_sig_ecdsa_secp256r1_sha256: |
4389 | 204k | case ssl_sig_rsa_pss_rsae_sha256: |
4390 | 204k | case ssl_sig_rsa_pss_pss_sha256: |
4391 | 240k | case ssl_sig_dsa_sha256: |
4392 | 240k | return ssl_hash_sha256; |
4393 | 36.2k | case ssl_sig_rsa_pkcs1_sha384: |
4394 | 88.3k | case ssl_sig_ecdsa_secp384r1_sha384: |
4395 | 124k | case ssl_sig_rsa_pss_rsae_sha384: |
4396 | 124k | case ssl_sig_rsa_pss_pss_sha384: |
4397 | 161k | case ssl_sig_dsa_sha384: |
4398 | 161k | return ssl_hash_sha384; |
4399 | 36.9k | case ssl_sig_rsa_pkcs1_sha512: |
4400 | 89.2k | case ssl_sig_ecdsa_secp521r1_sha512: |
4401 | 126k | case ssl_sig_rsa_pss_rsae_sha512: |
4402 | 126k | case ssl_sig_rsa_pss_pss_sha512: |
4403 | 162k | case ssl_sig_dsa_sha512: |
4404 | 162k | return ssl_hash_sha512; |
4405 | 0 | case ssl_sig_rsa_pkcs1_sha1md5: |
4406 | 0 | return ssl_hash_none; /* Special for TLS 1.0/1.1. */ |
4407 | 0 | case ssl_sig_none: |
4408 | 0 | case ssl_sig_ed25519: |
4409 | 0 | case ssl_sig_ed448: |
4410 | 0 | break; |
4411 | 693k | } |
4412 | 0 | PORT_Assert(0); |
4413 | 0 | return ssl_hash_none; |
4414 | 693k | } |
4415 | | |
4416 | | static PRBool |
4417 | | ssl_SignatureSchemeMatchesSpkiOid(SSLSignatureScheme scheme, SECOidTag spkiOid) |
4418 | 5.30k | { |
4419 | 5.30k | SECOidTag authOid = ssl3_AuthTypeToOID(ssl_SignatureSchemeToAuthType(scheme)); |
4420 | | |
4421 | 5.30k | if (spkiOid == authOid) { |
4422 | 5.29k | return PR_TRUE; |
4423 | 5.29k | } |
4424 | 11 | if ((authOid == SEC_OID_PKCS1_RSA_ENCRYPTION) && |
4425 | 11 | (spkiOid == SEC_OID_X500_RSA_ENCRYPTION)) { |
4426 | 0 | return PR_TRUE; |
4427 | 0 | } |
4428 | 11 | return PR_FALSE; |
4429 | 11 | } |
4430 | | |
4431 | | /* Validate that the signature scheme works for the given key type. */ |
4432 | | PRBool |
4433 | | ssl_SignatureSchemeValid(SSLSignatureScheme scheme, SECOidTag spkiOid, |
4434 | | PRBool isTls13) |
4435 | 27.5k | { |
4436 | 27.5k | if (!ssl_IsSupportedSignatureScheme(scheme)) { |
4437 | 21.3k | return PR_FALSE; |
4438 | 21.3k | } |
4439 | | /* if we are purposefully passed SEC_OID_UNKNOWN, it means |
4440 | | * we not checking the scheme against a potential key, so skip |
4441 | | * the call */ |
4442 | 6.28k | if ((spkiOid != SEC_OID_UNKNOWN) && |
4443 | 6.28k | !ssl_SignatureSchemeMatchesSpkiOid(scheme, spkiOid)) { |
4444 | 11 | return PR_FALSE; |
4445 | 11 | } |
4446 | 6.27k | if (isTls13) { |
4447 | 0 | if (ssl_SignatureSchemeToHashType(scheme) == ssl_hash_sha1) { |
4448 | 0 | return PR_FALSE; |
4449 | 0 | } |
4450 | 0 | if (ssl_IsRsaPkcs1SignatureScheme(scheme)) { |
4451 | 0 | return PR_FALSE; |
4452 | 0 | } |
4453 | 0 | if (ssl_IsDsaSignatureScheme(scheme)) { |
4454 | 0 | return PR_FALSE; |
4455 | 0 | } |
4456 | | /* With TLS 1.3, EC keys should have been selected based on calling |
4457 | | * ssl_SignatureSchemeFromSpki(), reject them otherwise. */ |
4458 | 0 | return spkiOid != SEC_OID_ANSIX962_EC_PUBLIC_KEY; |
4459 | 0 | } |
4460 | 6.27k | return PR_TRUE; |
4461 | 6.27k | } |
4462 | | |
4463 | | static SECStatus |
4464 | | ssl_SignatureSchemeFromPssSpki(const CERTSubjectPublicKeyInfo *spki, |
4465 | | SSLSignatureScheme *scheme) |
4466 | 7 | { |
4467 | 7 | SECKEYRSAPSSParams pssParam = { 0 }; |
4468 | 7 | PORTCheapArenaPool arena; |
4469 | 7 | SECStatus rv; |
4470 | | |
4471 | | /* The key doesn't have parameters, boo. */ |
4472 | 7 | if (!spki->algorithm.parameters.len) { |
4473 | 1 | *scheme = ssl_sig_none; |
4474 | 1 | return SECSuccess; |
4475 | 1 | } |
4476 | | |
4477 | 6 | PORT_InitCheapArena(&arena, DER_DEFAULT_CHUNKSIZE); |
4478 | 6 | rv = SEC_QuickDERDecodeItem(&arena.arena, &pssParam, |
4479 | 6 | SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate), |
4480 | 6 | &spki->algorithm.parameters); |
4481 | 6 | if (rv != SECSuccess) { |
4482 | 2 | goto loser; |
4483 | 2 | } |
4484 | | /* Not having hashAlg means SHA-1 and we don't accept that. */ |
4485 | 4 | if (!pssParam.hashAlg) { |
4486 | 1 | goto loser; |
4487 | 1 | } |
4488 | 3 | switch (SECOID_GetAlgorithmTag(pssParam.hashAlg)) { |
4489 | 1 | case SEC_OID_SHA256: |
4490 | 1 | *scheme = ssl_sig_rsa_pss_pss_sha256; |
4491 | 1 | break; |
4492 | 0 | case SEC_OID_SHA384: |
4493 | 0 | *scheme = ssl_sig_rsa_pss_pss_sha384; |
4494 | 0 | break; |
4495 | 1 | case SEC_OID_SHA512: |
4496 | 1 | *scheme = ssl_sig_rsa_pss_pss_sha512; |
4497 | 1 | break; |
4498 | 1 | default: |
4499 | 1 | goto loser; |
4500 | 3 | } |
4501 | | |
4502 | 2 | PORT_DestroyCheapArena(&arena); |
4503 | 2 | return SECSuccess; |
4504 | | |
4505 | 4 | loser: |
4506 | 4 | PORT_DestroyCheapArena(&arena); |
4507 | 4 | PORT_SetError(SSL_ERROR_BAD_CERTIFICATE); |
4508 | 4 | return SECFailure; |
4509 | 3 | } |
4510 | | |
4511 | | static SECStatus |
4512 | | ssl_SignatureSchemeFromEcSpki(const CERTSubjectPublicKeyInfo *spki, |
4513 | | SSLSignatureScheme *scheme) |
4514 | 0 | { |
4515 | 0 | const sslNamedGroupDef *group; |
4516 | 0 | SECKEYPublicKey *key; |
4517 | |
|
4518 | 0 | key = SECKEY_ExtractPublicKey(spki); |
4519 | 0 | if (!key) { |
4520 | 0 | PORT_SetError(SSL_ERROR_BAD_CERTIFICATE); |
4521 | 0 | return SECFailure; |
4522 | 0 | } |
4523 | 0 | group = ssl_ECPubKey2NamedGroup(key); |
4524 | 0 | SECKEY_DestroyPublicKey(key); |
4525 | 0 | if (!group) { |
4526 | 0 | PORT_SetError(SSL_ERROR_BAD_CERTIFICATE); |
4527 | 0 | return SECFailure; |
4528 | 0 | } |
4529 | 0 | switch (group->name) { |
4530 | 0 | case ssl_grp_ec_secp256r1: |
4531 | 0 | *scheme = ssl_sig_ecdsa_secp256r1_sha256; |
4532 | 0 | return SECSuccess; |
4533 | 0 | case ssl_grp_ec_secp384r1: |
4534 | 0 | *scheme = ssl_sig_ecdsa_secp384r1_sha384; |
4535 | 0 | return SECSuccess; |
4536 | 0 | case ssl_grp_ec_secp521r1: |
4537 | 0 | *scheme = ssl_sig_ecdsa_secp521r1_sha512; |
4538 | 0 | return SECSuccess; |
4539 | 0 | default: |
4540 | 0 | break; |
4541 | 0 | } |
4542 | 0 | PORT_SetError(SSL_ERROR_BAD_CERTIFICATE); |
4543 | 0 | return SECFailure; |
4544 | 0 | } |
4545 | | |
4546 | | /* Newer signature schemes are designed so that a single SPKI can be used with |
4547 | | * that scheme. This determines that scheme from the SPKI. If the SPKI doesn't |
4548 | | * have a single scheme, |*scheme| is set to ssl_sig_none. */ |
4549 | | SECStatus |
4550 | | ssl_SignatureSchemeFromSpki(const CERTSubjectPublicKeyInfo *spki, |
4551 | | PRBool isTls13, SSLSignatureScheme *scheme) |
4552 | 5.31k | { |
4553 | 5.31k | SECOidTag spkiOid = SECOID_GetAlgorithmTag(&spki->algorithm); |
4554 | | |
4555 | 5.31k | if (spkiOid == SEC_OID_PKCS1_RSA_PSS_SIGNATURE) { |
4556 | 7 | return ssl_SignatureSchemeFromPssSpki(spki, scheme); |
4557 | 7 | } |
4558 | | |
4559 | | /* Only do this lookup for TLS 1.3, where the scheme can be determined from |
4560 | | * the SPKI alone because the ECDSA key size determines the hash. Earlier |
4561 | | * TLS versions allow the same EC key to be used with different hashes. */ |
4562 | 5.30k | if (isTls13 && spkiOid == SEC_OID_ANSIX962_EC_PUBLIC_KEY) { |
4563 | 0 | return ssl_SignatureSchemeFromEcSpki(spki, scheme); |
4564 | 0 | } |
4565 | | |
4566 | 5.30k | *scheme = ssl_sig_none; |
4567 | 5.30k | return SECSuccess; |
4568 | 5.30k | } |
4569 | | |
4570 | | /* Check that a signature scheme is enabled by configuration. */ |
4571 | | PRBool |
4572 | | ssl_SignatureSchemeEnabled(const sslSocket *ss, SSLSignatureScheme scheme) |
4573 | 5.29k | { |
4574 | 5.29k | unsigned int i; |
4575 | 47.3k | for (i = 0; i < ss->ssl3.signatureSchemeCount; ++i) { |
4576 | 47.3k | if (scheme == ss->ssl3.signatureSchemes[i]) { |
4577 | 5.29k | return PR_TRUE; |
4578 | 5.29k | } |
4579 | 47.3k | } |
4580 | 2 | return PR_FALSE; |
4581 | 5.29k | } |
4582 | | |
4583 | | static PRBool |
4584 | | ssl_SignatureKeyMatchesSpkiOid(const ssl3KEADef *keaDef, SECOidTag spkiOid) |
4585 | 5.30k | { |
4586 | 5.30k | switch (spkiOid) { |
4587 | 0 | case SEC_OID_X500_RSA_ENCRYPTION: |
4588 | 4.23k | case SEC_OID_PKCS1_RSA_ENCRYPTION: |
4589 | 4.23k | case SEC_OID_PKCS1_RSA_PSS_SIGNATURE: |
4590 | 4.23k | return keaDef->signKeyType == rsaKey; |
4591 | 900 | case SEC_OID_ANSIX9_DSA_SIGNATURE: |
4592 | 900 | return keaDef->signKeyType == dsaKey; |
4593 | 173 | case SEC_OID_ANSIX962_EC_PUBLIC_KEY: |
4594 | 173 | return keaDef->signKeyType == ecKey; |
4595 | 1 | default: |
4596 | 1 | break; |
4597 | 5.30k | } |
4598 | 1 | return PR_FALSE; |
4599 | 5.30k | } |
4600 | | |
4601 | | /* ssl3_CheckSignatureSchemeConsistency checks that the signature algorithm |
4602 | | * identifier in |scheme| is consistent with the public key in |spki|. It also |
4603 | | * checks the hash algorithm against the configured signature algorithms. If |
4604 | | * all the tests pass, SECSuccess is returned. Otherwise, PORT_SetError is |
4605 | | * called and SECFailure is returned. */ |
4606 | | SECStatus |
4607 | | ssl_CheckSignatureSchemeConsistency(sslSocket *ss, SSLSignatureScheme scheme, |
4608 | | CERTSubjectPublicKeyInfo *spki) |
4609 | 5.31k | { |
4610 | 5.31k | SSLSignatureScheme spkiScheme; |
4611 | 5.31k | PRBool isTLS13 = ss->version == SSL_LIBRARY_VERSION_TLS_1_3; |
4612 | 5.31k | SECOidTag spkiOid; |
4613 | 5.31k | SECStatus rv; |
4614 | | |
4615 | 5.31k | rv = ssl_SignatureSchemeFromSpki(spki, isTLS13, &spkiScheme); |
4616 | 5.31k | if (rv != SECSuccess) { |
4617 | 4 | return SECFailure; |
4618 | 4 | } |
4619 | 5.31k | if (spkiScheme != ssl_sig_none) { |
4620 | | /* The SPKI in the certificate can only be used for a single scheme. */ |
4621 | 2 | if (spkiScheme != scheme || |
4622 | 2 | !ssl_SignatureSchemeEnabled(ss, scheme)) { |
4623 | 2 | PORT_SetError(SSL_ERROR_INCORRECT_SIGNATURE_ALGORITHM); |
4624 | 2 | return SECFailure; |
4625 | 2 | } |
4626 | 0 | return SECSuccess; |
4627 | 2 | } |
4628 | | |
4629 | 5.30k | spkiOid = SECOID_GetAlgorithmTag(&spki->algorithm); |
4630 | | |
4631 | | /* If we're a client, check that the signature algorithm matches the signing |
4632 | | * key type of the cipher suite. */ |
4633 | 5.30k | if (!isTLS13 && !ss->sec.isServer) { |
4634 | 5.30k | if (!ssl_SignatureKeyMatchesSpkiOid(ss->ssl3.hs.kea_def, spkiOid)) { |
4635 | 3 | PORT_SetError(SSL_ERROR_INCORRECT_SIGNATURE_ALGORITHM); |
4636 | 3 | return SECFailure; |
4637 | 3 | } |
4638 | 5.30k | } |
4639 | | |
4640 | | /* Verify that the signature scheme matches the signing key. */ |
4641 | 5.30k | if ((spkiOid == SEC_OID_UNKNOWN) || |
4642 | 5.30k | !ssl_SignatureSchemeValid(scheme, spkiOid, isTLS13)) { |
4643 | 11 | PORT_SetError(SSL_ERROR_INCORRECT_SIGNATURE_ALGORITHM); |
4644 | 11 | return SECFailure; |
4645 | 11 | } |
4646 | | |
4647 | 5.29k | if (!ssl_SignatureSchemeEnabled(ss, scheme)) { |
4648 | 1 | PORT_SetError(SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM); |
4649 | 1 | return SECFailure; |
4650 | 1 | } |
4651 | | |
4652 | 5.29k | return SECSuccess; |
4653 | 5.29k | } |
4654 | | |
4655 | | PRBool |
4656 | | ssl_IsSupportedSignatureScheme(SSLSignatureScheme scheme) |
4657 | 32.9k | { |
4658 | 32.9k | switch (scheme) { |
4659 | 2.17k | case ssl_sig_rsa_pkcs1_sha1: |
4660 | 6.11k | case ssl_sig_rsa_pkcs1_sha256: |
4661 | 6.17k | case ssl_sig_rsa_pkcs1_sha384: |
4662 | 6.69k | case ssl_sig_rsa_pkcs1_sha512: |
4663 | 8.30k | case ssl_sig_rsa_pss_rsae_sha256: |
4664 | 8.35k | case ssl_sig_rsa_pss_rsae_sha384: |
4665 | 8.73k | case ssl_sig_rsa_pss_rsae_sha512: |
4666 | 8.75k | case ssl_sig_rsa_pss_pss_sha256: |
4667 | 8.76k | case ssl_sig_rsa_pss_pss_sha384: |
4668 | 8.81k | case ssl_sig_rsa_pss_pss_sha512: |
4669 | 8.98k | case ssl_sig_ecdsa_secp256r1_sha256: |
4670 | 9.06k | case ssl_sig_ecdsa_secp384r1_sha384: |
4671 | 9.36k | case ssl_sig_ecdsa_secp521r1_sha512: |
4672 | 10.1k | case ssl_sig_dsa_sha1: |
4673 | 10.6k | case ssl_sig_dsa_sha256: |
4674 | 11.4k | case ssl_sig_dsa_sha384: |
4675 | 11.4k | case ssl_sig_dsa_sha512: |
4676 | 11.6k | case ssl_sig_ecdsa_sha1: |
4677 | 11.6k | return ssl_SchemePolicyOK(scheme, kSSLSigSchemePolicy); |
4678 | 0 | break; |
4679 | | |
4680 | 0 | case ssl_sig_rsa_pkcs1_sha1md5: |
4681 | 1.62k | case ssl_sig_none: |
4682 | 1.63k | case ssl_sig_ed25519: |
4683 | 1.90k | case ssl_sig_ed448: |
4684 | 1.90k | return PR_FALSE; |
4685 | 32.9k | } |
4686 | 19.4k | return PR_FALSE; |
4687 | 32.9k | } |
4688 | | |
4689 | | PRBool |
4690 | | ssl_IsRsaPssSignatureScheme(SSLSignatureScheme scheme) |
4691 | 1.50M | { |
4692 | 1.50M | switch (scheme) { |
4693 | 75.4k | case ssl_sig_rsa_pss_rsae_sha256: |
4694 | 148k | case ssl_sig_rsa_pss_rsae_sha384: |
4695 | 222k | case ssl_sig_rsa_pss_rsae_sha512: |
4696 | 222k | case ssl_sig_rsa_pss_pss_sha256: |
4697 | 222k | case ssl_sig_rsa_pss_pss_sha384: |
4698 | 222k | case ssl_sig_rsa_pss_pss_sha512: |
4699 | 222k | return PR_TRUE; |
4700 | | |
4701 | 1.28M | default: |
4702 | 1.28M | return PR_FALSE; |
4703 | 1.50M | } |
4704 | 0 | return PR_FALSE; |
4705 | 1.50M | } |
4706 | | |
4707 | | PRBool |
4708 | | ssl_IsRsaeSignatureScheme(SSLSignatureScheme scheme) |
4709 | 231k | { |
4710 | 231k | switch (scheme) { |
4711 | 15.4k | case ssl_sig_rsa_pss_rsae_sha256: |
4712 | 30.8k | case ssl_sig_rsa_pss_rsae_sha384: |
4713 | 46.2k | case ssl_sig_rsa_pss_rsae_sha512: |
4714 | 46.2k | return PR_TRUE; |
4715 | | |
4716 | 185k | default: |
4717 | 185k | return PR_FALSE; |
4718 | 231k | } |
4719 | 0 | return PR_FALSE; |
4720 | 231k | } |
4721 | | |
4722 | | PRBool |
4723 | | ssl_IsRsaPkcs1SignatureScheme(SSLSignatureScheme scheme) |
4724 | 263k | { |
4725 | 263k | switch (scheme) { |
4726 | 16.0k | case ssl_sig_rsa_pkcs1_sha256: |
4727 | 32.1k | case ssl_sig_rsa_pkcs1_sha384: |
4728 | 48.2k | case ssl_sig_rsa_pkcs1_sha512: |
4729 | 64.3k | case ssl_sig_rsa_pkcs1_sha1: |
4730 | 64.3k | return PR_TRUE; |
4731 | | |
4732 | 199k | default: |
4733 | 199k | return PR_FALSE; |
4734 | 263k | } |
4735 | 0 | return PR_FALSE; |
4736 | 263k | } |
4737 | | |
4738 | | PRBool |
4739 | | ssl_IsDsaSignatureScheme(SSLSignatureScheme scheme) |
4740 | 632k | { |
4741 | 632k | switch (scheme) { |
4742 | 52.2k | case ssl_sig_dsa_sha256: |
4743 | 104k | case ssl_sig_dsa_sha384: |
4744 | 156k | case ssl_sig_dsa_sha512: |
4745 | 209k | case ssl_sig_dsa_sha1: |
4746 | 209k | return PR_TRUE; |
4747 | | |
4748 | 423k | default: |
4749 | 423k | return PR_FALSE; |
4750 | 632k | } |
4751 | 0 | return PR_FALSE; |
4752 | 632k | } |
4753 | | |
4754 | | SSLAuthType |
4755 | | ssl_SignatureSchemeToAuthType(SSLSignatureScheme scheme) |
4756 | 590k | { |
4757 | 590k | switch (scheme) { |
4758 | 40.4k | case ssl_sig_rsa_pkcs1_sha1: |
4759 | 43.5k | case ssl_sig_rsa_pkcs1_sha1md5: |
4760 | 87.6k | case ssl_sig_rsa_pkcs1_sha256: |
4761 | 123k | case ssl_sig_rsa_pkcs1_sha384: |
4762 | 161k | case ssl_sig_rsa_pkcs1_sha512: |
4763 | | /* We report based on the key type for PSS signatures. */ |
4764 | 162k | case ssl_sig_rsa_pss_rsae_sha256: |
4765 | 162k | case ssl_sig_rsa_pss_rsae_sha384: |
4766 | 163k | case ssl_sig_rsa_pss_rsae_sha512: |
4767 | 163k | return ssl_auth_rsa_sign; |
4768 | 2 | case ssl_sig_rsa_pss_pss_sha256: |
4769 | 4 | case ssl_sig_rsa_pss_pss_sha384: |
4770 | 6 | case ssl_sig_rsa_pss_pss_sha512: |
4771 | 6 | return ssl_auth_rsa_pss; |
4772 | 123k | case ssl_sig_ecdsa_secp256r1_sha256: |
4773 | 175k | case ssl_sig_ecdsa_secp384r1_sha384: |
4774 | 227k | case ssl_sig_ecdsa_secp521r1_sha512: |
4775 | 280k | case ssl_sig_ecdsa_sha1: |
4776 | 280k | return ssl_auth_ecdsa; |
4777 | 37.2k | case ssl_sig_dsa_sha1: |
4778 | 74.2k | case ssl_sig_dsa_sha256: |
4779 | 111k | case ssl_sig_dsa_sha384: |
4780 | 147k | case ssl_sig_dsa_sha512: |
4781 | 147k | return ssl_auth_dsa; |
4782 | | |
4783 | 0 | default: |
4784 | 0 | PORT_Assert(0); |
4785 | 590k | } |
4786 | 0 | return ssl_auth_null; |
4787 | 590k | } |
4788 | | |
4789 | | /* ssl_ConsumeSignatureScheme reads a SSLSignatureScheme (formerly |
4790 | | * SignatureAndHashAlgorithm) structure from |b| and puts the resulting value |
4791 | | * into |out|. |b| and |length| are updated accordingly. |
4792 | | * |
4793 | | * See https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ |
4794 | | SECStatus |
4795 | | ssl_ConsumeSignatureScheme(sslSocket *ss, PRUint8 **b, |
4796 | | PRUint32 *length, SSLSignatureScheme *out) |
4797 | 5.32k | { |
4798 | 5.32k | PRUint32 tmp; |
4799 | 5.32k | SECStatus rv; |
4800 | | |
4801 | 5.32k | rv = ssl3_ConsumeHandshakeNumber(ss, &tmp, 2, b, length); |
4802 | 5.32k | if (rv != SECSuccess) { |
4803 | 2 | return SECFailure; /* Alert sent, Error code set already. */ |
4804 | 2 | } |
4805 | 5.32k | if (!ssl_IsSupportedSignatureScheme((SSLSignatureScheme)tmp)) { |
4806 | 6 | SSL3_SendAlert(ss, alert_fatal, illegal_parameter); |
4807 | 6 | PORT_SetError(SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM); |
4808 | 6 | return SECFailure; |
4809 | 6 | } |
4810 | 5.31k | *out = (SSLSignatureScheme)tmp; |
4811 | 5.31k | return SECSuccess; |
4812 | 5.32k | } |
4813 | | |
4814 | | /************************************************************************** |
4815 | | * end of Consume Handshake functions. |
4816 | | **************************************************************************/ |
4817 | | |
4818 | | static SECStatus |
4819 | | ssl3_ComputeHandshakeHash(unsigned char *buf, unsigned int len, |
4820 | | SSLHashType hashAlg, SSL3Hashes *hashes) |
4821 | 17.5k | { |
4822 | 17.5k | SECStatus rv = SECFailure; |
4823 | 17.5k | PK11Context *hashContext = PK11_CreateDigestContext( |
4824 | 17.5k | ssl3_HashTypeToOID(hashAlg)); |
4825 | | |
4826 | 17.5k | if (!hashContext) { |
4827 | 0 | return rv; |
4828 | 0 | } |
4829 | 17.5k | rv = PK11_DigestBegin(hashContext); |
4830 | 17.5k | if (rv == SECSuccess) { |
4831 | 17.5k | rv = PK11_DigestOp(hashContext, buf, len); |
4832 | 17.5k | } |
4833 | 17.5k | if (rv == SECSuccess) { |
4834 | 17.5k | rv = PK11_DigestFinal(hashContext, hashes->u.raw, &hashes->len, |
4835 | 17.5k | sizeof(hashes->u.raw)); |
4836 | 17.5k | } |
4837 | 17.5k | if (rv == SECSuccess) { |
4838 | 17.5k | hashes->hashAlg = hashAlg; |
4839 | 17.5k | } |
4840 | 17.5k | PK11_DestroyContext(hashContext, PR_TRUE); |
4841 | 17.5k | return rv; |
4842 | 17.5k | } |
4843 | | |
4844 | | /* Extract the hashes of handshake messages to this point. |
4845 | | * Called from ssl3_SendCertificateVerify |
4846 | | * ssl3_SendFinished |
4847 | | * ssl3_HandleHandshakeMessage |
4848 | | * |
4849 | | * Caller must hold the SSL3HandshakeLock. |
4850 | | * Caller must hold a read or write lock on the Spec R/W lock. |
4851 | | * (There is presently no way to assert on a Read lock.) |
4852 | | */ |
4853 | | SECStatus |
4854 | | ssl3_ComputeHandshakeHashes(sslSocket *ss, |
4855 | | ssl3CipherSpec *spec, /* uses ->master_secret */ |
4856 | | SSL3Hashes *hashes, /* output goes here. */ |
4857 | | PRUint32 sender) |
4858 | 64.8k | { |
4859 | 64.8k | SECStatus rv = SECSuccess; |
4860 | 64.8k | PRBool isTLS = (PRBool)(spec->version > SSL_LIBRARY_VERSION_3_0); |
4861 | 64.8k | unsigned int outLength; |
4862 | 64.8k | PRUint8 md5_inner[MAX_MAC_LENGTH]; |
4863 | 64.8k | PRUint8 sha_inner[MAX_MAC_LENGTH]; |
4864 | | |
4865 | 64.8k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4866 | 64.8k | if (ss->ssl3.hs.hashType == handshake_hash_unknown) { |
4867 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
4868 | 0 | return SECFailure; |
4869 | 0 | } |
4870 | | |
4871 | 64.8k | hashes->hashAlg = ssl_hash_none; |
4872 | | |
4873 | 64.8k | if (ss->ssl3.hs.hashType == handshake_hash_single) { |
4874 | 0 | PK11Context *h; |
4875 | 0 | unsigned int stateLen; |
4876 | 0 | unsigned char stackBuf[1024]; |
4877 | 0 | unsigned char *stateBuf = NULL; |
4878 | |
|
4879 | 0 | h = ss->ssl3.hs.sha; |
4880 | 0 | stateBuf = PK11_SaveContextAlloc(h, stackBuf, |
4881 | 0 | sizeof(stackBuf), &stateLen); |
4882 | 0 | if (stateBuf == NULL) { |
4883 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
4884 | 0 | rv = SECFailure; |
4885 | 0 | goto tls12_loser; |
4886 | 0 | } |
4887 | 0 | rv |= PK11_DigestFinal(h, hashes->u.raw, &hashes->len, |
4888 | 0 | sizeof(hashes->u.raw)); |
4889 | 0 | if (rv != SECSuccess) { |
4890 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
4891 | 0 | rv = SECFailure; |
4892 | 0 | goto tls12_loser; |
4893 | 0 | } |
4894 | | |
4895 | 0 | hashes->hashAlg = ssl3_GetSuitePrfHash(ss); |
4896 | |
|
4897 | 0 | tls12_loser: |
4898 | 0 | if (stateBuf) { |
4899 | 0 | if (PK11_RestoreContext(h, stateBuf, stateLen) != SECSuccess) { |
4900 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
4901 | 0 | rv = SECFailure; |
4902 | 0 | } |
4903 | 0 | if (stateBuf != stackBuf) { |
4904 | 0 | PORT_ZFree(stateBuf, stateLen); |
4905 | 0 | } |
4906 | 0 | } |
4907 | 64.8k | } else if (ss->ssl3.hs.hashType == handshake_hash_record) { |
4908 | 17.5k | rv = ssl3_ComputeHandshakeHash(ss->ssl3.hs.messages.buf, |
4909 | 17.5k | ss->ssl3.hs.messages.len, |
4910 | 17.5k | ssl3_GetSuitePrfHash(ss), |
4911 | 17.5k | hashes); |
4912 | 47.2k | } else { |
4913 | 47.2k | PK11Context *md5; |
4914 | 47.2k | PK11Context *sha = NULL; |
4915 | 47.2k | unsigned char *md5StateBuf = NULL; |
4916 | 47.2k | unsigned char *shaStateBuf = NULL; |
4917 | 47.2k | unsigned int md5StateLen, shaStateLen; |
4918 | 47.2k | unsigned char md5StackBuf[256]; |
4919 | 47.2k | unsigned char shaStackBuf[512]; |
4920 | 47.2k | const int md5Pad = ssl_GetMacDefByAlg(ssl_mac_md5)->pad_size; |
4921 | 47.2k | const int shaPad = ssl_GetMacDefByAlg(ssl_mac_sha)->pad_size; |
4922 | | |
4923 | 47.2k | md5StateBuf = PK11_SaveContextAlloc(ss->ssl3.hs.md5, md5StackBuf, |
4924 | 47.2k | sizeof md5StackBuf, &md5StateLen); |
4925 | 47.2k | if (md5StateBuf == NULL) { |
4926 | 0 | ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); |
4927 | 0 | rv = SECFailure; |
4928 | 0 | goto loser; |
4929 | 0 | } |
4930 | 47.2k | md5 = ss->ssl3.hs.md5; |
4931 | | |
4932 | 47.2k | shaStateBuf = PK11_SaveContextAlloc(ss->ssl3.hs.sha, shaStackBuf, |
4933 | 47.2k | sizeof shaStackBuf, &shaStateLen); |
4934 | 47.2k | if (shaStateBuf == NULL) { |
4935 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
4936 | 0 | rv = SECFailure; |
4937 | 0 | goto loser; |
4938 | 0 | } |
4939 | 47.2k | sha = ss->ssl3.hs.sha; |
4940 | | |
4941 | 47.2k | if (!isTLS) { |
4942 | | /* compute hashes for SSL3. */ |
4943 | 0 | unsigned char s[4]; |
4944 | |
|
4945 | 0 | if (!spec->masterSecret) { |
4946 | 0 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HANDSHAKE); |
4947 | 0 | rv = SECFailure; |
4948 | 0 | goto loser; |
4949 | 0 | } |
4950 | | |
4951 | 0 | s[0] = (unsigned char)(sender >> 24); |
4952 | 0 | s[1] = (unsigned char)(sender >> 16); |
4953 | 0 | s[2] = (unsigned char)(sender >> 8); |
4954 | 0 | s[3] = (unsigned char)sender; |
4955 | |
|
4956 | 0 | if (sender != 0) { |
4957 | 0 | rv |= PK11_DigestOp(md5, s, 4); |
4958 | 0 | PRINT_BUF(95, (NULL, "MD5 inner: sender", s, 4)); |
4959 | 0 | } |
4960 | |
|
4961 | 0 | PRINT_BUF(95, (NULL, "MD5 inner: MAC Pad 1", mac_pad_1, md5Pad)); |
4962 | |
|
4963 | 0 | rv |= PK11_DigestKey(md5, spec->masterSecret); |
4964 | 0 | rv |= PK11_DigestOp(md5, mac_pad_1, md5Pad); |
4965 | 0 | rv |= PK11_DigestFinal(md5, md5_inner, &outLength, MD5_LENGTH); |
4966 | 0 | PORT_Assert(rv != SECSuccess || outLength == MD5_LENGTH); |
4967 | 0 | if (rv != SECSuccess) { |
4968 | 0 | ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); |
4969 | 0 | rv = SECFailure; |
4970 | 0 | goto loser; |
4971 | 0 | } |
4972 | | |
4973 | 0 | PRINT_BUF(95, (NULL, "MD5 inner: result", md5_inner, outLength)); |
4974 | |
|
4975 | 0 | if (sender != 0) { |
4976 | 0 | rv |= PK11_DigestOp(sha, s, 4); |
4977 | 0 | PRINT_BUF(95, (NULL, "SHA inner: sender", s, 4)); |
4978 | 0 | } |
4979 | |
|
4980 | 0 | PRINT_BUF(95, (NULL, "SHA inner: MAC Pad 1", mac_pad_1, shaPad)); |
4981 | |
|
4982 | 0 | rv |= PK11_DigestKey(sha, spec->masterSecret); |
4983 | 0 | rv |= PK11_DigestOp(sha, mac_pad_1, shaPad); |
4984 | 0 | rv |= PK11_DigestFinal(sha, sha_inner, &outLength, SHA1_LENGTH); |
4985 | 0 | PORT_Assert(rv != SECSuccess || outLength == SHA1_LENGTH); |
4986 | 0 | if (rv != SECSuccess) { |
4987 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
4988 | 0 | rv = SECFailure; |
4989 | 0 | goto loser; |
4990 | 0 | } |
4991 | | |
4992 | 0 | PRINT_BUF(95, (NULL, "SHA inner: result", sha_inner, outLength)); |
4993 | |
|
4994 | 0 | PRINT_BUF(95, (NULL, "MD5 outer: MAC Pad 2", mac_pad_2, md5Pad)); |
4995 | 0 | PRINT_BUF(95, (NULL, "MD5 outer: MD5 inner", md5_inner, MD5_LENGTH)); |
4996 | |
|
4997 | 0 | rv |= PK11_DigestBegin(md5); |
4998 | 0 | rv |= PK11_DigestKey(md5, spec->masterSecret); |
4999 | 0 | rv |= PK11_DigestOp(md5, mac_pad_2, md5Pad); |
5000 | 0 | rv |= PK11_DigestOp(md5, md5_inner, MD5_LENGTH); |
5001 | 0 | } |
5002 | 47.2k | rv |= PK11_DigestFinal(md5, hashes->u.s.md5, &outLength, MD5_LENGTH); |
5003 | 47.2k | PORT_Assert(rv != SECSuccess || outLength == MD5_LENGTH); |
5004 | 47.2k | if (rv != SECSuccess) { |
5005 | 0 | ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); |
5006 | 0 | rv = SECFailure; |
5007 | 0 | goto loser; |
5008 | 0 | } |
5009 | | |
5010 | 47.2k | PRINT_BUF(60, (NULL, "MD5 outer: result", hashes->u.s.md5, MD5_LENGTH)); |
5011 | | |
5012 | 47.2k | if (!isTLS) { |
5013 | 0 | PRINT_BUF(95, (NULL, "SHA outer: MAC Pad 2", mac_pad_2, shaPad)); |
5014 | 0 | PRINT_BUF(95, (NULL, "SHA outer: SHA inner", sha_inner, SHA1_LENGTH)); |
5015 | |
|
5016 | 0 | rv |= PK11_DigestBegin(sha); |
5017 | 0 | rv |= PK11_DigestKey(sha, spec->masterSecret); |
5018 | 0 | rv |= PK11_DigestOp(sha, mac_pad_2, shaPad); |
5019 | 0 | rv |= PK11_DigestOp(sha, sha_inner, SHA1_LENGTH); |
5020 | 0 | } |
5021 | 47.2k | rv |= PK11_DigestFinal(sha, hashes->u.s.sha, &outLength, SHA1_LENGTH); |
5022 | 47.2k | PORT_Assert(rv != SECSuccess || outLength == SHA1_LENGTH); |
5023 | 47.2k | if (rv != SECSuccess) { |
5024 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
5025 | 0 | rv = SECFailure; |
5026 | 0 | goto loser; |
5027 | 0 | } |
5028 | | |
5029 | 47.2k | PRINT_BUF(60, (NULL, "SHA outer: result", hashes->u.s.sha, SHA1_LENGTH)); |
5030 | | |
5031 | 47.2k | hashes->len = MD5_LENGTH + SHA1_LENGTH; |
5032 | | |
5033 | 47.2k | loser: |
5034 | 47.2k | if (md5StateBuf) { |
5035 | 47.2k | if (PK11_RestoreContext(ss->ssl3.hs.md5, md5StateBuf, md5StateLen) != |
5036 | 47.2k | SECSuccess) { |
5037 | 0 | ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE); |
5038 | 0 | rv = SECFailure; |
5039 | 0 | } |
5040 | 47.2k | if (md5StateBuf != md5StackBuf) { |
5041 | 0 | PORT_ZFree(md5StateBuf, md5StateLen); |
5042 | 0 | } |
5043 | 47.2k | } |
5044 | 47.2k | if (shaStateBuf) { |
5045 | 47.2k | if (PK11_RestoreContext(ss->ssl3.hs.sha, shaStateBuf, shaStateLen) != |
5046 | 47.2k | SECSuccess) { |
5047 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
5048 | 0 | rv = SECFailure; |
5049 | 0 | } |
5050 | 47.2k | if (shaStateBuf != shaStackBuf) { |
5051 | 0 | PORT_ZFree(shaStateBuf, shaStateLen); |
5052 | 0 | } |
5053 | 47.2k | } |
5054 | 47.2k | } |
5055 | 64.8k | return rv; |
5056 | 64.8k | } |
5057 | | |
5058 | | /************************************************************************** |
5059 | | * end of Handshake Hash functions. |
5060 | | * Begin Send and Handle functions for handshakes. |
5061 | | **************************************************************************/ |
5062 | | |
5063 | | #ifdef TRACE |
5064 | | #define CHTYPE(t) \ |
5065 | 0 | case client_hello_##t: \ |
5066 | 0 | return #t; |
5067 | | |
5068 | | static const char * |
5069 | | ssl_ClientHelloTypeName(sslClientHelloType type) |
5070 | 0 | { |
5071 | 0 | switch (type) { |
5072 | 0 | CHTYPE(initial); |
5073 | 0 | CHTYPE(retry); |
5074 | 0 | CHTYPE(retransmit); /* DTLS only */ |
5075 | 0 | CHTYPE(renegotiation); /* TLS <= 1.2 only */ |
5076 | 0 | } |
5077 | 0 | PORT_Assert(0); |
5078 | 0 | return NULL; |
5079 | 0 | } |
5080 | | #undef CHTYPE |
5081 | | #endif |
5082 | | |
5083 | | PR_STATIC_ASSERT(SSL3_SESSIONID_BYTES == SSL3_RANDOM_LENGTH); |
5084 | | static void |
5085 | | ssl_MakeFakeSid(sslSocket *ss, PRUint8 *buf) |
5086 | 7.10k | { |
5087 | 7.10k | PRUint8 x = 0x5a; |
5088 | 7.10k | int i; |
5089 | 234k | for (i = 0; i < SSL3_SESSIONID_BYTES; ++i) { |
5090 | 227k | x += ss->ssl3.hs.client_random[i]; |
5091 | 227k | buf[i] = x; |
5092 | 227k | } |
5093 | 7.10k | } |
5094 | | |
5095 | | /* Set the version fields of the cipher spec for a ClientHello. */ |
5096 | | static void |
5097 | | ssl_SetClientHelloSpecVersion(sslSocket *ss, ssl3CipherSpec *spec) |
5098 | 10.1k | { |
5099 | 10.1k | ssl_GetSpecWriteLock(ss); |
5100 | 10.1k | PORT_Assert(spec->cipherDef->cipher == cipher_null); |
5101 | | /* This is - a best guess - but it doesn't matter here. */ |
5102 | 10.1k | spec->version = ss->vrange.max; |
5103 | 10.1k | if (IS_DTLS(ss)) { |
5104 | 0 | spec->recordVersion = SSL_LIBRARY_VERSION_DTLS_1_0_WIRE; |
5105 | 10.1k | } else { |
5106 | | /* For new connections, cap the record layer version number of TLS |
5107 | | * ClientHello to { 3, 1 } (TLS 1.0). Some TLS 1.0 servers (which seem |
5108 | | * to use F5 BIG-IP) ignore ClientHello.client_version and use the |
5109 | | * record layer version number (TLSPlaintext.version) instead when |
5110 | | * negotiating protocol versions. In addition, if the record layer |
5111 | | * version number of ClientHello is { 3, 2 } (TLS 1.1) or higher, these |
5112 | | * servers reset the TCP connections. Lastly, some F5 BIG-IP servers |
5113 | | * hang if a record containing a ClientHello has a version greater than |
5114 | | * { 3, 1 } and a length greater than 255. Set this flag to work around |
5115 | | * such servers. |
5116 | | * |
5117 | | * The final version is set when a version is negotiated. |
5118 | | */ |
5119 | 10.1k | spec->recordVersion = PR_MIN(SSL_LIBRARY_VERSION_TLS_1_0, |
5120 | 10.1k | ss->vrange.max); |
5121 | 10.1k | } |
5122 | 10.1k | ssl_ReleaseSpecWriteLock(ss); |
5123 | 10.1k | } |
5124 | | |
5125 | | SECStatus |
5126 | | ssl3_InsertChHeaderSize(const sslSocket *ss, sslBuffer *preamble, const sslBuffer *extensions) |
5127 | 86.6k | { |
5128 | 86.6k | SECStatus rv; |
5129 | 86.6k | unsigned int msgLen = preamble->len; |
5130 | 86.6k | msgLen += extensions->len ? (2 + extensions->len) : 0; |
5131 | 86.6k | unsigned int headerLen = IS_DTLS(ss) ? 12 : 4; |
5132 | | |
5133 | | /* Record the message length. */ |
5134 | 86.6k | rv = sslBuffer_InsertNumber(preamble, 1, msgLen - headerLen, 3); |
5135 | 86.6k | if (rv != SECSuccess) { |
5136 | 0 | return SECFailure; /* code set */ |
5137 | 0 | } |
5138 | 86.6k | if (IS_DTLS(ss)) { |
5139 | | /* Record the (unfragmented) fragment length. */ |
5140 | 0 | unsigned int offset = 1 /* ch */ + 3 /* len */ + |
5141 | 0 | 2 /* seq */ + 3 /* fragment offset */; |
5142 | 0 | rv = sslBuffer_InsertNumber(preamble, offset, msgLen - headerLen, 3); |
5143 | 0 | if (rv != SECSuccess) { |
5144 | 0 | return SECFailure; /* code set */ |
5145 | 0 | } |
5146 | 0 | } |
5147 | | |
5148 | 86.6k | return SECSuccess; |
5149 | 86.6k | } |
5150 | | |
5151 | | static SECStatus |
5152 | | ssl3_AppendCipherSuites(sslSocket *ss, PRBool fallbackSCSV, sslBuffer *buf) |
5153 | 45.1k | { |
5154 | 45.1k | SECStatus rv; |
5155 | 45.1k | unsigned int offset; |
5156 | 45.1k | unsigned int i; |
5157 | 45.1k | unsigned int saveLen; |
5158 | | |
5159 | 45.1k | rv = sslBuffer_Skip(buf, 2, &offset); |
5160 | 45.1k | if (rv != SECSuccess) { |
5161 | 0 | return SECFailure; |
5162 | 0 | } |
5163 | | |
5164 | 45.1k | if (ss->ssl3.hs.sendingSCSV) { |
5165 | | /* Add the actual SCSV */ |
5166 | 0 | rv = sslBuffer_AppendNumber(buf, TLS_EMPTY_RENEGOTIATION_INFO_SCSV, |
5167 | 0 | sizeof(ssl3CipherSuite)); |
5168 | 0 | if (rv != SECSuccess) { |
5169 | 0 | return SECFailure; |
5170 | 0 | } |
5171 | 0 | } |
5172 | 45.1k | if (fallbackSCSV) { |
5173 | 20.3k | rv = sslBuffer_AppendNumber(buf, TLS_FALLBACK_SCSV, |
5174 | 20.3k | sizeof(ssl3CipherSuite)); |
5175 | 20.3k | if (rv != SECSuccess) { |
5176 | 0 | return SECFailure; |
5177 | 0 | } |
5178 | 20.3k | } |
5179 | | |
5180 | 45.1k | saveLen = SSL_BUFFER_LEN(buf); |
5181 | | /* CipherSuites are appended to Hello message here */ |
5182 | 3.24M | for (i = 0; i < ssl_V3_SUITES_IMPLEMENTED; i++) { |
5183 | 3.20M | ssl3CipherSuiteCfg *suite = &ss->cipherSuites[i]; |
5184 | 3.20M | if (ssl3_config_match(suite, ss->ssl3.policy, &ss->vrange, ss)) { |
5185 | 3.01M | rv = sslBuffer_AppendNumber(buf, suite->cipher_suite, |
5186 | 3.01M | sizeof(ssl3CipherSuite)); |
5187 | 3.01M | if (rv != SECSuccess) { |
5188 | 0 | return SECFailure; |
5189 | 0 | } |
5190 | 3.01M | } |
5191 | 3.20M | } |
5192 | | |
5193 | | /* GREASE CipherSuites: |
5194 | | * A client MAY select one or more GREASE cipher suite values and advertise |
5195 | | * them in the "cipher_suites" field [RFC8701, Section 3.1]. */ |
5196 | 45.1k | if (ss->opt.enableGrease && ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3) { |
5197 | 18.7k | rv = sslBuffer_AppendNumber(buf, ss->ssl3.hs.grease->idx[grease_cipher], |
5198 | 18.7k | sizeof(ssl3CipherSuite)); |
5199 | 18.7k | if (rv != SECSuccess) { |
5200 | 0 | return SECFailure; |
5201 | 0 | } |
5202 | 18.7k | } |
5203 | | |
5204 | 45.1k | if (SSL_ALL_VERSIONS_DISABLED(&ss->vrange) || |
5205 | 45.1k | (SSL_BUFFER_LEN(buf) - saveLen) == 0) { |
5206 | 0 | PORT_SetError(SSL_ERROR_SSL_DISABLED); |
5207 | 0 | return SECFailure; |
5208 | 0 | } |
5209 | | |
5210 | 45.1k | return sslBuffer_InsertLength(buf, offset, 2); |
5211 | 45.1k | } |
5212 | | |
5213 | | SECStatus |
5214 | | ssl3_CreateClientHelloPreamble(sslSocket *ss, const sslSessionID *sid, |
5215 | | PRBool realSid, PRUint16 version, PRBool isEchInner, |
5216 | | const sslBuffer *extensions, sslBuffer *preamble) |
5217 | 45.1k | { |
5218 | 45.1k | SECStatus rv; |
5219 | 45.1k | sslBuffer constructed = SSL_BUFFER_EMPTY; |
5220 | 45.1k | const PRUint8 *client_random = isEchInner ? ss->ssl3.hs.client_inner_random : ss->ssl3.hs.client_random; |
5221 | 45.1k | PORT_Assert(sid); |
5222 | 45.1k | PRBool fallbackSCSV = ss->opt.enableFallbackSCSV && !isEchInner && |
5223 | 45.1k | (!realSid || version < sid->version); |
5224 | | |
5225 | 45.1k | rv = sslBuffer_AppendNumber(&constructed, ssl_hs_client_hello, 1); |
5226 | 45.1k | if (rv != SECSuccess) { |
5227 | 0 | goto loser; |
5228 | 0 | } |
5229 | | |
5230 | 45.1k | rv = sslBuffer_Skip(&constructed, 3, NULL); |
5231 | 45.1k | if (rv != SECSuccess) { |
5232 | 0 | goto loser; |
5233 | 0 | } |
5234 | | |
5235 | 45.1k | if (IS_DTLS(ss)) { |
5236 | | /* Note that we make an unfragmented message here. We fragment in the |
5237 | | * transmission code, if necessary */ |
5238 | 0 | rv = sslBuffer_AppendNumber(&constructed, ss->ssl3.hs.sendMessageSeq, 2); |
5239 | 0 | if (rv != SECSuccess) { |
5240 | 0 | goto loser; |
5241 | 0 | } |
5242 | 0 | ss->ssl3.hs.sendMessageSeq++; |
5243 | | |
5244 | | /* 0 is the fragment offset, because it's not fragmented yet */ |
5245 | 0 | rv = sslBuffer_AppendNumber(&constructed, 0, 3); |
5246 | 0 | if (rv != SECSuccess) { |
5247 | 0 | goto loser; |
5248 | 0 | } |
5249 | | |
5250 | | /* Fragment length -- set to the packet length because not fragmented */ |
5251 | 0 | rv = sslBuffer_Skip(&constructed, 3, NULL); |
5252 | 0 | if (rv != SECSuccess) { |
5253 | 0 | goto loser; |
5254 | 0 | } |
5255 | 0 | } |
5256 | | |
5257 | 45.1k | if (ss->firstHsDone) { |
5258 | | /* The client hello version must stay unchanged to work around |
5259 | | * the Windows SChannel bug described in ssl3_SendClientHello. */ |
5260 | 31.0k | PORT_Assert(version == ss->clientHelloVersion); |
5261 | 31.0k | } |
5262 | | |
5263 | 45.1k | ss->clientHelloVersion = PR_MIN(version, SSL_LIBRARY_VERSION_TLS_1_2); |
5264 | 45.1k | if (IS_DTLS(ss)) { |
5265 | 0 | PRUint16 dtlsVersion = dtls_TLSVersionToDTLSVersion(ss->clientHelloVersion); |
5266 | 0 | rv = sslBuffer_AppendNumber(&constructed, dtlsVersion, 2); |
5267 | 45.1k | } else { |
5268 | 45.1k | rv = sslBuffer_AppendNumber(&constructed, ss->clientHelloVersion, 2); |
5269 | 45.1k | } |
5270 | 45.1k | if (rv != SECSuccess) { |
5271 | 0 | goto loser; |
5272 | 0 | } |
5273 | | |
5274 | 45.1k | rv = sslBuffer_Append(&constructed, client_random, SSL3_RANDOM_LENGTH); |
5275 | 45.1k | if (rv != SECSuccess) { |
5276 | 0 | goto loser; |
5277 | 0 | } |
5278 | | |
5279 | 45.1k | if (sid->version < SSL_LIBRARY_VERSION_TLS_1_3 && !isEchInner) { |
5280 | 32.9k | rv = sslBuffer_AppendVariable(&constructed, sid->u.ssl3.sessionID, |
5281 | 32.9k | sid->u.ssl3.sessionIDLength, 1); |
5282 | 32.9k | } else if (ss->opt.enableTls13CompatMode && !IS_DTLS(ss)) { |
5283 | | /* We're faking session resumption, so rather than create new |
5284 | | * randomness, just mix up the client random a little. */ |
5285 | 5.19k | PRUint8 buf[SSL3_SESSIONID_BYTES]; |
5286 | 5.19k | ssl_MakeFakeSid(ss, buf); |
5287 | 5.19k | rv = sslBuffer_AppendVariable(&constructed, buf, SSL3_SESSIONID_BYTES, 1); |
5288 | 7.01k | } else { |
5289 | 7.01k | rv = sslBuffer_AppendNumber(&constructed, 0, 1); |
5290 | 7.01k | } |
5291 | 45.1k | if (rv != SECSuccess) { |
5292 | 0 | goto loser; |
5293 | 0 | } |
5294 | | |
5295 | 45.1k | if (IS_DTLS(ss)) { |
5296 | | /* This cookieLen applies to the cookie that appears in the DTLS |
5297 | | * ClientHello, which isn't used in DTLS 1.3. */ |
5298 | 0 | rv = sslBuffer_AppendVariable(&constructed, ss->ssl3.hs.cookie.data, |
5299 | 0 | ss->ssl3.hs.helloRetry ? 0 : ss->ssl3.hs.cookie.len, |
5300 | 0 | 1); |
5301 | 0 | if (rv != SECSuccess) { |
5302 | 0 | goto loser; |
5303 | 0 | } |
5304 | 0 | } |
5305 | | |
5306 | 45.1k | rv = ssl3_AppendCipherSuites(ss, fallbackSCSV, &constructed); |
5307 | 45.1k | if (rv != SECSuccess) { |
5308 | 0 | goto loser; |
5309 | 0 | } |
5310 | | |
5311 | | /* Compression methods: count is always 1, null compression. */ |
5312 | 45.1k | rv = sslBuffer_AppendNumber(&constructed, 1, 1); |
5313 | 45.1k | if (rv != SECSuccess) { |
5314 | 0 | goto loser; |
5315 | 0 | } |
5316 | 45.1k | rv = sslBuffer_AppendNumber(&constructed, ssl_compression_null, 1); |
5317 | 45.1k | if (rv != SECSuccess) { |
5318 | 0 | goto loser; |
5319 | 0 | } |
5320 | | |
5321 | 45.1k | rv = ssl3_InsertChHeaderSize(ss, &constructed, extensions); |
5322 | 45.1k | if (rv != SECSuccess) { |
5323 | 0 | goto loser; |
5324 | 0 | } |
5325 | | |
5326 | 45.1k | *preamble = constructed; |
5327 | 45.1k | return SECSuccess; |
5328 | 0 | loser: |
5329 | 0 | sslBuffer_Clear(&constructed); |
5330 | 0 | return SECFailure; |
5331 | 45.1k | } |
5332 | | |
5333 | | /* Called from ssl3_HandleHelloRequest(), |
5334 | | * ssl3_RedoHandshake() |
5335 | | * ssl_BeginClientHandshake (when resuming ssl3 session) |
5336 | | * dtls_HandleHelloVerifyRequest(with resending=PR_TRUE) |
5337 | | * |
5338 | | * The |type| argument indicates what is going on here: |
5339 | | * - client_hello_initial is set for the very first ClientHello |
5340 | | * - client_hello_retry indicates that this is a second attempt after receiving |
5341 | | * a HelloRetryRequest (in TLS 1.3) |
5342 | | * - client_hello_retransmit is used in DTLS when resending |
5343 | | * - client_hello_renegotiation is used to renegotiate (in TLS <1.3) |
5344 | | */ |
5345 | | SECStatus |
5346 | | ssl3_SendClientHello(sslSocket *ss, sslClientHelloType type) |
5347 | 41.5k | { |
5348 | 41.5k | sslSessionID *sid; |
5349 | 41.5k | SECStatus rv; |
5350 | 41.5k | PRBool isTLS = PR_FALSE; |
5351 | 41.5k | PRBool requestingResume = PR_FALSE; |
5352 | 41.5k | PRBool unlockNeeded = PR_FALSE; |
5353 | 41.5k | sslBuffer extensionBuf = SSL_BUFFER_EMPTY; |
5354 | 41.5k | PRUint16 version = ss->vrange.max; |
5355 | 41.5k | PRInt32 flags; |
5356 | 41.5k | sslBuffer chBuf = SSL_BUFFER_EMPTY; |
5357 | | |
5358 | 41.5k | SSL_TRC(3, ("%d: SSL3[%d]: send %s ClientHello handshake", SSL_GETPID(), |
5359 | 41.5k | ss->fd, ssl_ClientHelloTypeName(type))); |
5360 | | |
5361 | 41.5k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5362 | 41.5k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
5363 | | |
5364 | | /* shouldn't get here if SSL3 is disabled, but ... */ |
5365 | 41.5k | if (SSL_ALL_VERSIONS_DISABLED(&ss->vrange)) { |
5366 | 0 | PR_NOT_REACHED("No versions of SSL 3.0 or later are enabled"); |
5367 | 0 | PORT_SetError(SSL_ERROR_SSL_DISABLED); |
5368 | 0 | return SECFailure; |
5369 | 0 | } |
5370 | | |
5371 | | /* If we are responding to a HelloRetryRequest, don't reinitialize. We need |
5372 | | * to maintain the handshake hashes. */ |
5373 | 41.5k | if (!ss->ssl3.hs.helloRetry) { |
5374 | 41.1k | ssl3_RestartHandshakeHashes(ss); |
5375 | 41.1k | } |
5376 | 41.5k | PORT_Assert(!ss->ssl3.hs.helloRetry || type == client_hello_retry); |
5377 | | |
5378 | 41.5k | if (type == client_hello_initial) { |
5379 | 10.1k | ssl_SetClientHelloSpecVersion(ss, ss->ssl3.cwSpec); |
5380 | 10.1k | } |
5381 | | /* These must be reset every handshake. */ |
5382 | 41.5k | ssl3_ResetExtensionData(&ss->xtnData, ss); |
5383 | 41.5k | ss->ssl3.hs.sendingSCSV = PR_FALSE; |
5384 | 41.5k | ss->ssl3.hs.preliminaryInfo = 0; |
5385 | 41.5k | PORT_Assert(IS_DTLS(ss) || type != client_hello_retransmit); |
5386 | 41.5k | SECITEM_FreeItem(&ss->ssl3.hs.newSessionTicket.ticket, PR_FALSE); |
5387 | 41.5k | ss->ssl3.hs.receivedNewSessionTicket = PR_FALSE; |
5388 | | |
5389 | | /* How many suites does our PKCS11 support (regardless of policy)? */ |
5390 | 41.5k | if (ssl3_config_match_init(ss) == 0) { |
5391 | 0 | return SECFailure; /* ssl3_config_match_init has set error code. */ |
5392 | 0 | } |
5393 | | |
5394 | | /* |
5395 | | * During a renegotiation, ss->clientHelloVersion will be used again to |
5396 | | * work around a Windows SChannel bug. Ensure that it is still enabled. |
5397 | | */ |
5398 | 41.5k | if (ss->firstHsDone) { |
5399 | 31.0k | PORT_Assert(type != client_hello_initial); |
5400 | 31.0k | if (SSL_ALL_VERSIONS_DISABLED(&ss->vrange)) { |
5401 | 0 | PORT_SetError(SSL_ERROR_SSL_DISABLED); |
5402 | 0 | return SECFailure; |
5403 | 0 | } |
5404 | | |
5405 | 31.0k | if (ss->clientHelloVersion < ss->vrange.min || |
5406 | 31.0k | ss->clientHelloVersion > ss->vrange.max) { |
5407 | 0 | PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP); |
5408 | 0 | return SECFailure; |
5409 | 0 | } |
5410 | 31.0k | } |
5411 | | |
5412 | | /* Check if we have a ss->sec.ci.sid. |
5413 | | * Check that it's not expired. |
5414 | | * If we have an sid and it comes from an external cache, we use it. */ |
5415 | 41.5k | if (ss->sec.ci.sid && ss->sec.ci.sid->cached == in_external_cache) { |
5416 | 0 | PORT_Assert(!ss->sec.isServer); |
5417 | 0 | sid = ssl_ReferenceSID(ss->sec.ci.sid); |
5418 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: using external resumption token in ClientHello", |
5419 | 0 | SSL_GETPID(), ss->fd)); |
5420 | 41.5k | } else if (ss->sec.ci.sid && ss->statelessResume && type == client_hello_retry) { |
5421 | | /* If we are sending a second ClientHello, reuse the same SID |
5422 | | * as the original one. */ |
5423 | 0 | sid = ssl_ReferenceSID(ss->sec.ci.sid); |
5424 | 41.5k | } else if (!ss->opt.noCache) { |
5425 | | /* We ignore ss->sec.ci.sid here, and use ssl_Lookup because Lookup |
5426 | | * handles expired entries and other details. |
5427 | | * XXX If we've been called from ssl_BeginClientHandshake, then |
5428 | | * this lookup is duplicative and wasteful. |
5429 | | */ |
5430 | 21.4k | sid = ssl_LookupSID(ssl_Time(ss), &ss->sec.ci.peer, |
5431 | 21.4k | ss->sec.ci.port, ss->peerID, ss->url); |
5432 | 21.4k | } else { |
5433 | 20.1k | sid = NULL; |
5434 | 20.1k | } |
5435 | | |
5436 | | /* We can't resume based on a different token. If the sid exists, |
5437 | | * make sure the token that holds the master secret still exists ... |
5438 | | * If we previously did client-auth, make sure that the token that holds |
5439 | | * the private key still exists, is logged in, hasn't been removed, etc. |
5440 | | */ |
5441 | 41.5k | if (sid) { |
5442 | 0 | PRBool sidOK = PR_TRUE; |
5443 | |
|
5444 | 0 | if (sid->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
5445 | 0 | if (!tls13_ResumptionCompatible(ss, sid->u.ssl3.cipherSuite)) { |
5446 | 0 | sidOK = PR_FALSE; |
5447 | 0 | } |
5448 | 0 | } else { |
5449 | | /* Check that the cipher suite we need is enabled. */ |
5450 | 0 | const ssl3CipherSuiteCfg *suite = |
5451 | 0 | ssl_LookupCipherSuiteCfg(sid->u.ssl3.cipherSuite, |
5452 | 0 | ss->cipherSuites); |
5453 | 0 | SSLVersionRange vrange = { sid->version, sid->version }; |
5454 | 0 | if (!suite || !ssl3_config_match(suite, ss->ssl3.policy, &vrange, ss)) { |
5455 | 0 | sidOK = PR_FALSE; |
5456 | 0 | } |
5457 | | |
5458 | | /* Check that no (valid) ECHConfigs are setup in combination with a |
5459 | | * (resumable) TLS < 1.3 session id. */ |
5460 | 0 | if (!PR_CLIST_IS_EMPTY(&ss->echConfigs)) { |
5461 | | /* If there are ECH configs, the client must not resume but |
5462 | | * offer ECH. */ |
5463 | 0 | sidOK = PR_FALSE; |
5464 | 0 | } |
5465 | 0 | } |
5466 | | |
5467 | | /* Check that we can recover the master secret. */ |
5468 | 0 | if (sidOK) { |
5469 | 0 | PK11SlotInfo *slot = NULL; |
5470 | 0 | if (sid->u.ssl3.masterValid) { |
5471 | 0 | slot = SECMOD_LookupSlot(sid->u.ssl3.masterModuleID, |
5472 | 0 | sid->u.ssl3.masterSlotID); |
5473 | 0 | } |
5474 | 0 | if (slot == NULL) { |
5475 | 0 | sidOK = PR_FALSE; |
5476 | 0 | } else { |
5477 | 0 | PK11SymKey *wrapKey = NULL; |
5478 | 0 | if (!PK11_IsPresent(slot) || |
5479 | 0 | ((wrapKey = PK11_GetWrapKey(slot, |
5480 | 0 | sid->u.ssl3.masterWrapIndex, |
5481 | 0 | sid->u.ssl3.masterWrapMech, |
5482 | 0 | sid->u.ssl3.masterWrapSeries, |
5483 | 0 | ss->pkcs11PinArg)) == NULL)) { |
5484 | 0 | sidOK = PR_FALSE; |
5485 | 0 | } |
5486 | 0 | if (wrapKey) |
5487 | 0 | PK11_FreeSymKey(wrapKey); |
5488 | 0 | PK11_FreeSlot(slot); |
5489 | 0 | slot = NULL; |
5490 | 0 | } |
5491 | 0 | } |
5492 | | /* If we previously did client-auth, make sure that the token that |
5493 | | ** holds the private key still exists, is logged in, hasn't been |
5494 | | ** removed, etc. |
5495 | | */ |
5496 | 0 | if (sidOK && !ssl3_ClientAuthTokenPresent(sid)) { |
5497 | 0 | sidOK = PR_FALSE; |
5498 | 0 | } |
5499 | |
|
5500 | 0 | if (sidOK) { |
5501 | | /* Set version based on the sid. */ |
5502 | 0 | if (ss->firstHsDone) { |
5503 | | /* |
5504 | | * Windows SChannel compares the client_version inside the RSA |
5505 | | * EncryptedPreMasterSecret of a renegotiation with the |
5506 | | * client_version of the initial ClientHello rather than the |
5507 | | * ClientHello in the renegotiation. To work around this bug, we |
5508 | | * continue to use the client_version used in the initial |
5509 | | * ClientHello when renegotiating. |
5510 | | * |
5511 | | * The client_version of the initial ClientHello is still |
5512 | | * available in ss->clientHelloVersion. Ensure that |
5513 | | * sid->version is bounded within |
5514 | | * [ss->vrange.min, ss->clientHelloVersion], otherwise we |
5515 | | * can't use sid. |
5516 | | */ |
5517 | 0 | if (sid->version >= ss->vrange.min && |
5518 | 0 | sid->version <= ss->clientHelloVersion) { |
5519 | 0 | version = ss->clientHelloVersion; |
5520 | 0 | } else { |
5521 | 0 | sidOK = PR_FALSE; |
5522 | 0 | } |
5523 | 0 | } else { |
5524 | | /* |
5525 | | * Check sid->version is OK first. |
5526 | | * Previously, we would cap the version based on sid->version, |
5527 | | * but that prevents negotiation of a higher version if the |
5528 | | * previous session was reduced (e.g., with version fallback) |
5529 | | */ |
5530 | 0 | if (sid->version < ss->vrange.min || |
5531 | 0 | sid->version > ss->vrange.max) { |
5532 | 0 | sidOK = PR_FALSE; |
5533 | 0 | } |
5534 | 0 | } |
5535 | 0 | } |
5536 | |
|
5537 | 0 | if (!sidOK) { |
5538 | 0 | SSL_AtomicIncrementLong(&ssl3stats.sch_sid_cache_not_ok); |
5539 | 0 | ssl_UncacheSessionID(ss); |
5540 | 0 | ssl_FreeSID(sid); |
5541 | 0 | sid = NULL; |
5542 | 0 | } |
5543 | 0 | } |
5544 | | |
5545 | 41.5k | if (sid) { |
5546 | 0 | requestingResume = PR_TRUE; |
5547 | 0 | SSL_AtomicIncrementLong(&ssl3stats.sch_sid_cache_hits); |
5548 | |
|
5549 | 0 | PRINT_BUF(4, (ss, "client, found session-id:", sid->u.ssl3.sessionID, |
5550 | 0 | sid->u.ssl3.sessionIDLength)); |
5551 | |
|
5552 | 0 | ss->ssl3.policy = sid->u.ssl3.policy; |
5553 | 41.5k | } else { |
5554 | 41.5k | SSL_AtomicIncrementLong(&ssl3stats.sch_sid_cache_misses); |
5555 | | |
5556 | | /* |
5557 | | * Windows SChannel compares the client_version inside the RSA |
5558 | | * EncryptedPreMasterSecret of a renegotiation with the |
5559 | | * client_version of the initial ClientHello rather than the |
5560 | | * ClientHello in the renegotiation. To work around this bug, we |
5561 | | * continue to use the client_version used in the initial |
5562 | | * ClientHello when renegotiating. |
5563 | | */ |
5564 | 41.5k | if (ss->firstHsDone) { |
5565 | 31.0k | version = ss->clientHelloVersion; |
5566 | 31.0k | } |
5567 | | |
5568 | 41.5k | sid = ssl3_NewSessionID(ss, PR_FALSE); |
5569 | 41.5k | if (!sid) { |
5570 | 0 | return SECFailure; /* memory error is set */ |
5571 | 0 | } |
5572 | | /* ss->version isn't set yet, but the sid needs a sane value. */ |
5573 | 41.5k | sid->version = version; |
5574 | 41.5k | } |
5575 | | |
5576 | 41.5k | isTLS = (version > SSL_LIBRARY_VERSION_3_0); |
5577 | 41.5k | ssl_GetSpecWriteLock(ss); |
5578 | 41.5k | if (ss->ssl3.cwSpec->macDef->mac == ssl_mac_null) { |
5579 | | /* SSL records are not being MACed. */ |
5580 | 10.4k | ss->ssl3.cwSpec->version = version; |
5581 | 10.4k | } |
5582 | 41.5k | ssl_ReleaseSpecWriteLock(ss); |
5583 | | |
5584 | 41.5k | ssl_FreeSID(ss->sec.ci.sid); /* release the old sid */ |
5585 | 41.5k | ss->sec.ci.sid = sid; |
5586 | | |
5587 | | /* HACK for SCSV in SSL 3.0. On initial handshake, prepend SCSV, |
5588 | | * only if TLS is disabled. |
5589 | | */ |
5590 | 41.5k | if (!ss->firstHsDone && !isTLS) { |
5591 | | /* Must set this before calling Hello Extension Senders, |
5592 | | * to suppress sending of empty RI extension. |
5593 | | */ |
5594 | 0 | ss->ssl3.hs.sendingSCSV = PR_TRUE; |
5595 | 0 | } |
5596 | | |
5597 | | /* When we attempt session resumption (only), we must lock the sid to |
5598 | | * prevent races with other resumption connections that receive a |
5599 | | * NewSessionTicket that will cause the ticket in the sid to be replaced. |
5600 | | * Once we've copied the session ticket into our ClientHello message, it |
5601 | | * is OK for the ticket to change, so we just need to make sure we hold |
5602 | | * the lock across the calls to ssl_ConstructExtensions. |
5603 | | */ |
5604 | 41.5k | if (sid->u.ssl3.lock) { |
5605 | 0 | unlockNeeded = PR_TRUE; |
5606 | 0 | PR_RWLock_Rlock(sid->u.ssl3.lock); |
5607 | 0 | } |
5608 | | |
5609 | | /* Generate a new random if this is the first attempt or renegotiation. */ |
5610 | 41.5k | if (type == client_hello_initial || |
5611 | 41.5k | type == client_hello_renegotiation) { |
5612 | 41.1k | rv = ssl3_GetNewRandom(ss->ssl3.hs.client_random); |
5613 | 41.1k | if (rv != SECSuccess) { |
5614 | 0 | goto loser; /* err set by GetNewRandom. */ |
5615 | 0 | } |
5616 | 41.1k | } |
5617 | | |
5618 | 41.5k | if (ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3) { |
5619 | 31.6k | rv = tls13_SetupClientHello(ss, type); |
5620 | 31.6k | if (rv != SECSuccess) { |
5621 | 0 | goto loser; |
5622 | 0 | } |
5623 | 31.6k | } |
5624 | | |
5625 | | /* Setup TLS ClientHello Extension Permutation? */ |
5626 | 41.5k | if (type == client_hello_initial && |
5627 | 41.5k | ss->vrange.max > SSL_LIBRARY_VERSION_3_0 && |
5628 | 41.5k | ss->opt.enableChXtnPermutation) { |
5629 | 4.81k | rv = tls_ClientHelloExtensionPermutationSetup(ss); |
5630 | 4.81k | if (rv != SECSuccess) { |
5631 | 0 | goto loser; |
5632 | 0 | } |
5633 | 4.81k | } |
5634 | | |
5635 | 41.5k | if (isTLS || (ss->firstHsDone && ss->peerRequestedProtection)) { |
5636 | 41.5k | rv = ssl_ConstructExtensions(ss, &extensionBuf, ssl_hs_client_hello); |
5637 | 41.5k | if (rv != SECSuccess) { |
5638 | 0 | goto loser; |
5639 | 0 | } |
5640 | 41.5k | } |
5641 | | |
5642 | 41.5k | if (IS_DTLS(ss)) { |
5643 | 0 | ssl3_DisableNonDTLSSuites(ss); |
5644 | 0 | } |
5645 | | |
5646 | 41.5k | rv = ssl3_CreateClientHelloPreamble(ss, sid, requestingResume, version, |
5647 | 41.5k | PR_FALSE, &extensionBuf, &chBuf); |
5648 | 41.5k | if (rv != SECSuccess) { |
5649 | 0 | goto loser; /* err set by ssl3_CreateClientHelloPreamble. */ |
5650 | 0 | } |
5651 | | |
5652 | 41.5k | if (!ss->ssl3.hs.echHpkeCtx) { |
5653 | 37.8k | if (extensionBuf.len) { |
5654 | 37.8k | rv = tls13_MaybeGreaseEch(ss, &chBuf, &extensionBuf); |
5655 | 37.8k | if (rv != SECSuccess) { |
5656 | 0 | goto loser; /* err set by tls13_MaybeGreaseEch. */ |
5657 | 0 | } |
5658 | 37.8k | rv = ssl_InsertPaddingExtension(ss, chBuf.len, &extensionBuf); |
5659 | 37.8k | if (rv != SECSuccess) { |
5660 | 0 | goto loser; /* err set by ssl_InsertPaddingExtension. */ |
5661 | 0 | } |
5662 | | |
5663 | 37.8k | rv = ssl3_InsertChHeaderSize(ss, &chBuf, &extensionBuf); |
5664 | 37.8k | if (rv != SECSuccess) { |
5665 | 0 | goto loser; /* err set by ssl3_InsertChHeaderSize. */ |
5666 | 0 | } |
5667 | | |
5668 | | /* If we are sending a PSK binder, replace the dummy value. */ |
5669 | 37.8k | if (ssl3_ExtensionAdvertised(ss, ssl_tls13_pre_shared_key_xtn)) { |
5670 | 15.6k | rv = tls13_WriteExtensionsWithBinder(ss, &extensionBuf, &chBuf); |
5671 | 22.2k | } else { |
5672 | 22.2k | rv = sslBuffer_AppendNumber(&chBuf, extensionBuf.len, 2); |
5673 | 22.2k | if (rv != SECSuccess) { |
5674 | 0 | goto loser; |
5675 | 0 | } |
5676 | 22.2k | rv = sslBuffer_AppendBuffer(&chBuf, &extensionBuf); |
5677 | 22.2k | } |
5678 | 37.8k | if (rv != SECSuccess) { |
5679 | 0 | goto loser; /* err set by sslBuffer_Append*. */ |
5680 | 0 | } |
5681 | 37.8k | } |
5682 | | |
5683 | | /* If we already have a message in place, we need to enqueue it. |
5684 | | * This empties the buffer. This is a convenient place to call |
5685 | | * dtls_StageHandshakeMessage to mark the message boundary. */ |
5686 | 37.8k | if (IS_DTLS(ss)) { |
5687 | 0 | rv = dtls_StageHandshakeMessage(ss); |
5688 | 0 | if (rv != SECSuccess) { |
5689 | 0 | goto loser; |
5690 | 0 | } |
5691 | 0 | } |
5692 | | |
5693 | | /* As here the function takes the full message and hashes it in one go, |
5694 | | * For DTLS1.3, we skip hashing the unnecessary header fields. |
5695 | | * See ssl3_AppendHandshakeHeader. */ |
5696 | 37.8k | if (IS_DTLS(ss) && ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3) { |
5697 | 0 | rv = ssl3_AppendHandshakeSuppressHash(ss, chBuf.buf, chBuf.len); |
5698 | 0 | if (rv != SECSuccess) { |
5699 | 0 | goto loser; /* code set */ |
5700 | 0 | } |
5701 | 0 | if (!ss->firstHsDone) { |
5702 | 0 | PORT_Assert(type == client_hello_retransmit || |
5703 | 0 | ss->ssl3.hs.dtls13ClientMessageBuffer.len == 0); |
5704 | 0 | sslBuffer_Clear(&ss->ssl3.hs.dtls13ClientMessageBuffer); |
5705 | | /* Here instead of computing the hash, we copy the data to a buffer.*/ |
5706 | 0 | rv = sslBuffer_Append(&ss->ssl3.hs.dtls13ClientMessageBuffer, chBuf.buf, chBuf.len); |
5707 | 0 | } |
5708 | 37.8k | } else { |
5709 | 37.8k | rv = ssl3_AppendHandshake(ss, chBuf.buf, chBuf.len); |
5710 | 37.8k | } |
5711 | | |
5712 | 37.8k | } else { |
5713 | 3.61k | PORT_Assert(!IS_DTLS(ss)); |
5714 | 3.61k | rv = tls13_ConstructClientHelloWithEch(ss, sid, !requestingResume, &chBuf, &extensionBuf); |
5715 | 3.61k | if (rv != SECSuccess) { |
5716 | 0 | goto loser; /* code set */ |
5717 | 0 | } |
5718 | 3.61k | rv = ssl3_UpdateDefaultHandshakeHashes(ss, chBuf.buf, chBuf.len); |
5719 | 3.61k | if (rv != SECSuccess) { |
5720 | 0 | goto loser; /* code set */ |
5721 | 0 | } |
5722 | | |
5723 | 3.61k | if (IS_DTLS(ss)) { |
5724 | 0 | rv = dtls_StageHandshakeMessage(ss); |
5725 | 0 | if (rv != SECSuccess) { |
5726 | 0 | goto loser; |
5727 | 0 | } |
5728 | 0 | } |
5729 | | /* By default, all messagess are added to both the inner and |
5730 | | * outer transcripts. For CH (or CH2 if HRR), that's problematic. */ |
5731 | 3.61k | rv = ssl3_AppendHandshakeSuppressHash(ss, chBuf.buf, chBuf.len); |
5732 | 3.61k | } |
5733 | 41.5k | if (rv != SECSuccess) { |
5734 | 0 | goto loser; |
5735 | 0 | } |
5736 | | |
5737 | 41.5k | if (unlockNeeded) { |
5738 | | /* Note: goto loser can't be used past this point. */ |
5739 | 0 | PR_RWLock_Unlock(sid->u.ssl3.lock); |
5740 | 0 | } |
5741 | | |
5742 | 41.5k | if (ss->xtnData.sentSessionTicketInClientHello) { |
5743 | 0 | SSL_AtomicIncrementLong(&ssl3stats.sch_sid_stateless_resumes); |
5744 | 0 | } |
5745 | | |
5746 | 41.5k | if (ss->ssl3.hs.sendingSCSV) { |
5747 | | /* Since we sent the SCSV, pretend we sent empty RI extension. */ |
5748 | 0 | TLSExtensionData *xtnData = &ss->xtnData; |
5749 | 0 | xtnData->advertised[xtnData->numAdvertised++] = |
5750 | 0 | ssl_renegotiation_info_xtn; |
5751 | 0 | } |
5752 | | |
5753 | 41.5k | flags = 0; |
5754 | 41.5k | rv = ssl3_FlushHandshake(ss, flags); |
5755 | 41.5k | if (rv != SECSuccess) { |
5756 | 0 | return rv; /* error code set by ssl3_FlushHandshake */ |
5757 | 0 | } |
5758 | | |
5759 | 41.5k | if (version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
5760 | 8.59k | rv = tls13_MaybeDo0RTTHandshake(ss); |
5761 | 8.59k | if (rv != SECSuccess) { |
5762 | 0 | return SECFailure; /* error code set already. */ |
5763 | 0 | } |
5764 | 8.59k | } |
5765 | | |
5766 | 41.5k | ss->ssl3.hs.ws = wait_server_hello; |
5767 | 41.5k | sslBuffer_Clear(&chBuf); |
5768 | 41.5k | sslBuffer_Clear(&extensionBuf); |
5769 | 41.5k | return SECSuccess; |
5770 | | |
5771 | 0 | loser: |
5772 | 0 | if (unlockNeeded) { |
5773 | 0 | PR_RWLock_Unlock(sid->u.ssl3.lock); |
5774 | 0 | } |
5775 | 0 | sslBuffer_Clear(&chBuf); |
5776 | 0 | sslBuffer_Clear(&extensionBuf); |
5777 | 0 | return SECFailure; |
5778 | 41.5k | } |
5779 | | |
5780 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered a |
5781 | | * complete ssl3 Hello Request. |
5782 | | * Caller must hold Handshake and RecvBuf locks. |
5783 | | */ |
5784 | | static SECStatus |
5785 | | ssl3_HandleHelloRequest(sslSocket *ss) |
5786 | 33.7k | { |
5787 | 33.7k | sslSessionID *sid = ss->sec.ci.sid; |
5788 | 33.7k | SECStatus rv; |
5789 | | |
5790 | 33.7k | SSL_TRC(3, ("%d: SSL3[%d]: handle hello_request handshake", |
5791 | 33.7k | SSL_GETPID(), ss->fd)); |
5792 | | |
5793 | 33.7k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
5794 | 33.7k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5795 | 33.7k | PORT_Assert(ss->version < SSL_LIBRARY_VERSION_TLS_1_3); |
5796 | | |
5797 | 33.7k | if (ss->ssl3.hs.ws == wait_server_hello) |
5798 | 2.69k | return SECSuccess; |
5799 | 31.0k | if (ss->ssl3.hs.ws != idle_handshake || ss->sec.isServer) { |
5800 | 36 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
5801 | 36 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_REQUEST); |
5802 | 36 | return SECFailure; |
5803 | 36 | } |
5804 | 31.0k | if (ss->opt.enableRenegotiation == SSL_RENEGOTIATE_NEVER) { |
5805 | 0 | (void)SSL3_SendAlert(ss, alert_warning, no_renegotiation); |
5806 | 0 | PORT_SetError(SSL_ERROR_RENEGOTIATION_NOT_ALLOWED); |
5807 | 0 | return SECFailure; |
5808 | 0 | } |
5809 | | |
5810 | 31.0k | if (sid) { |
5811 | 31.0k | ssl_UncacheSessionID(ss); |
5812 | 31.0k | ssl_FreeSID(sid); |
5813 | 31.0k | ss->sec.ci.sid = NULL; |
5814 | 31.0k | } |
5815 | | |
5816 | 31.0k | if (IS_DTLS(ss)) { |
5817 | 0 | dtls_RehandshakeCleanup(ss); |
5818 | 0 | } |
5819 | | |
5820 | 31.0k | ssl_GetXmitBufLock(ss); |
5821 | 31.0k | rv = ssl3_SendClientHello(ss, client_hello_renegotiation); |
5822 | 31.0k | ssl_ReleaseXmitBufLock(ss); |
5823 | | |
5824 | 31.0k | return rv; |
5825 | 31.0k | } |
5826 | | |
5827 | | static const CK_MECHANISM_TYPE wrapMechanismList[SSL_NUM_WRAP_MECHS] = { |
5828 | | CKM_DES3_ECB, |
5829 | | CKM_CAST5_ECB, |
5830 | | CKM_DES_ECB, |
5831 | | CKM_KEY_WRAP_LYNKS, |
5832 | | CKM_IDEA_ECB, |
5833 | | CKM_CAST3_ECB, |
5834 | | CKM_CAST_ECB, |
5835 | | CKM_RC5_ECB, |
5836 | | CKM_RC2_ECB, |
5837 | | CKM_CDMF_ECB, |
5838 | | CKM_SKIPJACK_WRAP, |
5839 | | CKM_SKIPJACK_CBC64, |
5840 | | CKM_AES_ECB, |
5841 | | CKM_CAMELLIA_ECB, |
5842 | | CKM_SEED_ECB |
5843 | | }; |
5844 | | |
5845 | | static SECStatus |
5846 | | ssl_FindIndexByWrapMechanism(CK_MECHANISM_TYPE mech, unsigned int *wrapMechIndex) |
5847 | 0 | { |
5848 | 0 | unsigned int i; |
5849 | 0 | for (i = 0; i < SSL_NUM_WRAP_MECHS; ++i) { |
5850 | 0 | if (wrapMechanismList[i] == mech) { |
5851 | 0 | *wrapMechIndex = i; |
5852 | 0 | return SECSuccess; |
5853 | 0 | } |
5854 | 0 | } |
5855 | 0 | PORT_Assert(0); |
5856 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
5857 | 0 | return SECFailure; |
5858 | 0 | } |
5859 | | |
5860 | | /* Each process sharing the server session ID cache has its own array of SymKey |
5861 | | * pointers for the symmetric wrapping keys that are used to wrap the master |
5862 | | * secrets. There is one key for each authentication type. These Symkeys |
5863 | | * correspond to the wrapped SymKeys kept in the server session cache. |
5864 | | */ |
5865 | | const SSLAuthType ssl_wrap_key_auth_type[SSL_NUM_WRAP_KEYS] = { |
5866 | | ssl_auth_rsa_decrypt, |
5867 | | ssl_auth_rsa_sign, |
5868 | | ssl_auth_rsa_pss, |
5869 | | ssl_auth_ecdsa, |
5870 | | ssl_auth_ecdh_rsa, |
5871 | | ssl_auth_ecdh_ecdsa |
5872 | | }; |
5873 | | |
5874 | | static SECStatus |
5875 | | ssl_FindIndexByWrapKey(const sslServerCert *serverCert, unsigned int *wrapKeyIndex) |
5876 | 0 | { |
5877 | 0 | unsigned int i; |
5878 | 0 | for (i = 0; i < SSL_NUM_WRAP_KEYS; ++i) { |
5879 | 0 | if (SSL_CERT_IS(serverCert, ssl_wrap_key_auth_type[i])) { |
5880 | 0 | *wrapKeyIndex = i; |
5881 | 0 | return SECSuccess; |
5882 | 0 | } |
5883 | 0 | } |
5884 | | /* Can't assert here because we still get people using DSA certificates. */ |
5885 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
5886 | 0 | return SECFailure; |
5887 | 0 | } |
5888 | | |
5889 | | static PK11SymKey * |
5890 | | ssl_UnwrapSymWrappingKey( |
5891 | | SSLWrappedSymWrappingKey *pWswk, |
5892 | | SECKEYPrivateKey *svrPrivKey, |
5893 | | unsigned int wrapKeyIndex, |
5894 | | CK_MECHANISM_TYPE masterWrapMech, |
5895 | | void *pwArg) |
5896 | 0 | { |
5897 | 0 | PK11SymKey *unwrappedWrappingKey = NULL; |
5898 | 0 | SECItem wrappedKey; |
5899 | 0 | PK11SymKey *Ks; |
5900 | 0 | SECKEYPublicKey pubWrapKey; |
5901 | 0 | ECCWrappedKeyInfo *ecWrapped; |
5902 | | |
5903 | | /* found the wrapping key on disk. */ |
5904 | 0 | PORT_Assert(pWswk->symWrapMechanism == masterWrapMech); |
5905 | 0 | PORT_Assert(pWswk->wrapKeyIndex == wrapKeyIndex); |
5906 | 0 | if (pWswk->symWrapMechanism != masterWrapMech || |
5907 | 0 | pWswk->wrapKeyIndex != wrapKeyIndex) { |
5908 | 0 | goto loser; |
5909 | 0 | } |
5910 | 0 | wrappedKey.type = siBuffer; |
5911 | 0 | wrappedKey.data = pWswk->wrappedSymmetricWrappingkey; |
5912 | 0 | wrappedKey.len = pWswk->wrappedSymKeyLen; |
5913 | 0 | PORT_Assert(wrappedKey.len <= sizeof pWswk->wrappedSymmetricWrappingkey); |
5914 | |
|
5915 | 0 | switch (ssl_wrap_key_auth_type[wrapKeyIndex]) { |
5916 | | |
5917 | 0 | case ssl_auth_rsa_decrypt: |
5918 | 0 | case ssl_auth_rsa_sign: /* bad: see Bug 1248320 */ |
5919 | 0 | unwrappedWrappingKey = |
5920 | 0 | PK11_PubUnwrapSymKey(svrPrivKey, &wrappedKey, |
5921 | 0 | masterWrapMech, CKA_UNWRAP, 0); |
5922 | 0 | break; |
5923 | | |
5924 | 0 | case ssl_auth_ecdsa: |
5925 | 0 | case ssl_auth_ecdh_rsa: |
5926 | 0 | case ssl_auth_ecdh_ecdsa: |
5927 | | /* |
5928 | | * For ssl_auth_ecd*, we first create an EC public key based on |
5929 | | * data stored with the wrappedSymmetricWrappingkey. Next, |
5930 | | * we do an ECDH computation involving this public key and |
5931 | | * the SSL server's (long-term) EC private key. The resulting |
5932 | | * shared secret is treated the same way as Fortezza's Ks, i.e., |
5933 | | * it is used to recover the symmetric wrapping key. |
5934 | | * |
5935 | | * The data in wrappedSymmetricWrappingkey is laid out as defined |
5936 | | * in the ECCWrappedKeyInfo structure. |
5937 | | */ |
5938 | 0 | ecWrapped = (ECCWrappedKeyInfo *)pWswk->wrappedSymmetricWrappingkey; |
5939 | |
|
5940 | 0 | PORT_Assert(ecWrapped->encodedParamLen + ecWrapped->pubValueLen + |
5941 | 0 | ecWrapped->wrappedKeyLen <= |
5942 | 0 | MAX_EC_WRAPPED_KEY_BUFLEN); |
5943 | |
|
5944 | 0 | if (ecWrapped->encodedParamLen + ecWrapped->pubValueLen + |
5945 | 0 | ecWrapped->wrappedKeyLen > |
5946 | 0 | MAX_EC_WRAPPED_KEY_BUFLEN) { |
5947 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
5948 | 0 | goto loser; |
5949 | 0 | } |
5950 | | |
5951 | 0 | pubWrapKey.keyType = ecKey; |
5952 | 0 | pubWrapKey.u.ec.size = ecWrapped->size; |
5953 | 0 | pubWrapKey.u.ec.DEREncodedParams.len = ecWrapped->encodedParamLen; |
5954 | 0 | pubWrapKey.u.ec.DEREncodedParams.data = ecWrapped->var; |
5955 | 0 | pubWrapKey.u.ec.publicValue.len = ecWrapped->pubValueLen; |
5956 | 0 | pubWrapKey.u.ec.publicValue.data = ecWrapped->var + |
5957 | 0 | ecWrapped->encodedParamLen; |
5958 | |
|
5959 | 0 | wrappedKey.len = ecWrapped->wrappedKeyLen; |
5960 | 0 | wrappedKey.data = ecWrapped->var + ecWrapped->encodedParamLen + |
5961 | 0 | ecWrapped->pubValueLen; |
5962 | | |
5963 | | /* Derive Ks using ECDH */ |
5964 | 0 | Ks = PK11_PubDeriveWithKDF(svrPrivKey, &pubWrapKey, PR_FALSE, NULL, |
5965 | 0 | NULL, CKM_ECDH1_DERIVE, masterWrapMech, |
5966 | 0 | CKA_DERIVE, 0, CKD_NULL, NULL, NULL); |
5967 | 0 | if (Ks == NULL) { |
5968 | 0 | goto loser; |
5969 | 0 | } |
5970 | | |
5971 | | /* Use Ks to unwrap the wrapping key */ |
5972 | 0 | unwrappedWrappingKey = PK11_UnwrapSymKey(Ks, masterWrapMech, NULL, |
5973 | 0 | &wrappedKey, masterWrapMech, |
5974 | 0 | CKA_UNWRAP, 0); |
5975 | 0 | PK11_FreeSymKey(Ks); |
5976 | |
|
5977 | 0 | break; |
5978 | | |
5979 | 0 | default: |
5980 | 0 | PORT_Assert(0); |
5981 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
5982 | 0 | goto loser; |
5983 | 0 | } |
5984 | 0 | loser: |
5985 | 0 | return unwrappedWrappingKey; |
5986 | 0 | } |
5987 | | |
5988 | | typedef struct { |
5989 | | PK11SymKey *symWrapKey[SSL_NUM_WRAP_KEYS]; |
5990 | | } ssl3SymWrapKey; |
5991 | | |
5992 | | static PZLock *symWrapKeysLock = NULL; |
5993 | | static ssl3SymWrapKey symWrapKeys[SSL_NUM_WRAP_MECHS]; |
5994 | | |
5995 | | SECStatus |
5996 | | ssl_FreeSymWrapKeysLock(void) |
5997 | 1 | { |
5998 | 1 | if (symWrapKeysLock) { |
5999 | 1 | PZ_DestroyLock(symWrapKeysLock); |
6000 | 1 | symWrapKeysLock = NULL; |
6001 | 1 | return SECSuccess; |
6002 | 1 | } |
6003 | 0 | PORT_SetError(SEC_ERROR_NOT_INITIALIZED); |
6004 | 0 | return SECFailure; |
6005 | 1 | } |
6006 | | |
6007 | | SECStatus |
6008 | | SSL3_ShutdownServerCache(void) |
6009 | 0 | { |
6010 | 0 | int i, j; |
6011 | |
|
6012 | 0 | if (!symWrapKeysLock) |
6013 | 0 | return SECSuccess; /* lock was never initialized */ |
6014 | 0 | PZ_Lock(symWrapKeysLock); |
6015 | | /* get rid of all symWrapKeys */ |
6016 | 0 | for (i = 0; i < SSL_NUM_WRAP_MECHS; ++i) { |
6017 | 0 | for (j = 0; j < SSL_NUM_WRAP_KEYS; ++j) { |
6018 | 0 | PK11SymKey **pSymWrapKey; |
6019 | 0 | pSymWrapKey = &symWrapKeys[i].symWrapKey[j]; |
6020 | 0 | if (*pSymWrapKey) { |
6021 | 0 | PK11_FreeSymKey(*pSymWrapKey); |
6022 | 0 | *pSymWrapKey = NULL; |
6023 | 0 | } |
6024 | 0 | } |
6025 | 0 | } |
6026 | |
|
6027 | 0 | PZ_Unlock(symWrapKeysLock); |
6028 | 0 | ssl_FreeSessionCacheLocks(); |
6029 | 0 | return SECSuccess; |
6030 | 0 | } |
6031 | | |
6032 | | SECStatus |
6033 | | ssl_InitSymWrapKeysLock(void) |
6034 | 1 | { |
6035 | 1 | symWrapKeysLock = PZ_NewLock(nssILockOther); |
6036 | 1 | return symWrapKeysLock ? SECSuccess : SECFailure; |
6037 | 1 | } |
6038 | | |
6039 | | /* Try to get wrapping key for mechanism from in-memory array. |
6040 | | * If that fails, look for one on disk. |
6041 | | * If that fails, generate a new one, put the new one on disk, |
6042 | | * Put the new key in the in-memory array. |
6043 | | * |
6044 | | * Note that this function performs some fairly inadvisable functions with |
6045 | | * certificate private keys. ECDSA keys are used with ECDH; similarly, RSA |
6046 | | * signing keys are used to encrypt. Bug 1248320. |
6047 | | */ |
6048 | | PK11SymKey * |
6049 | | ssl3_GetWrappingKey(sslSocket *ss, |
6050 | | PK11SlotInfo *masterSecretSlot, |
6051 | | CK_MECHANISM_TYPE masterWrapMech, |
6052 | | void *pwArg) |
6053 | 0 | { |
6054 | 0 | SSLAuthType authType; |
6055 | 0 | SECKEYPrivateKey *svrPrivKey; |
6056 | 0 | SECKEYPublicKey *svrPubKey = NULL; |
6057 | 0 | PK11SymKey *unwrappedWrappingKey = NULL; |
6058 | 0 | PK11SymKey **pSymWrapKey; |
6059 | 0 | CK_MECHANISM_TYPE asymWrapMechanism = CKM_INVALID_MECHANISM; |
6060 | 0 | int length; |
6061 | 0 | unsigned int wrapMechIndex; |
6062 | 0 | unsigned int wrapKeyIndex; |
6063 | 0 | SECStatus rv; |
6064 | 0 | SECItem wrappedKey; |
6065 | 0 | SSLWrappedSymWrappingKey wswk; |
6066 | 0 | PK11SymKey *Ks = NULL; |
6067 | 0 | SECKEYPublicKey *pubWrapKey = NULL; |
6068 | 0 | SECKEYPrivateKey *privWrapKey = NULL; |
6069 | 0 | ECCWrappedKeyInfo *ecWrapped; |
6070 | 0 | const sslServerCert *serverCert = ss->sec.serverCert; |
6071 | |
|
6072 | 0 | PORT_Assert(serverCert); |
6073 | 0 | PORT_Assert(serverCert->serverKeyPair); |
6074 | 0 | PORT_Assert(serverCert->serverKeyPair->privKey); |
6075 | 0 | PORT_Assert(serverCert->serverKeyPair->pubKey); |
6076 | 0 | if (!serverCert || !serverCert->serverKeyPair || |
6077 | 0 | !serverCert->serverKeyPair->privKey || |
6078 | 0 | !serverCert->serverKeyPair->pubKey) { |
6079 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6080 | 0 | return NULL; /* hmm */ |
6081 | 0 | } |
6082 | | |
6083 | 0 | rv = ssl_FindIndexByWrapKey(serverCert, &wrapKeyIndex); |
6084 | 0 | if (rv != SECSuccess) |
6085 | 0 | return NULL; /* unusable wrapping key. */ |
6086 | | |
6087 | 0 | rv = ssl_FindIndexByWrapMechanism(masterWrapMech, &wrapMechIndex); |
6088 | 0 | if (rv != SECSuccess) |
6089 | 0 | return NULL; /* invalid masterWrapMech. */ |
6090 | | |
6091 | 0 | authType = ssl_wrap_key_auth_type[wrapKeyIndex]; |
6092 | 0 | svrPrivKey = serverCert->serverKeyPair->privKey; |
6093 | 0 | pSymWrapKey = &symWrapKeys[wrapMechIndex].symWrapKey[wrapKeyIndex]; |
6094 | |
|
6095 | 0 | ssl_InitSessionCacheLocks(PR_TRUE); |
6096 | |
|
6097 | 0 | PZ_Lock(symWrapKeysLock); |
6098 | |
|
6099 | 0 | unwrappedWrappingKey = *pSymWrapKey; |
6100 | 0 | if (unwrappedWrappingKey != NULL) { |
6101 | 0 | if (PK11_VerifyKeyOK(unwrappedWrappingKey)) { |
6102 | 0 | unwrappedWrappingKey = PK11_ReferenceSymKey(unwrappedWrappingKey); |
6103 | 0 | goto done; |
6104 | 0 | } |
6105 | | /* slot series has changed, so this key is no good any more. */ |
6106 | 0 | PK11_FreeSymKey(unwrappedWrappingKey); |
6107 | 0 | *pSymWrapKey = unwrappedWrappingKey = NULL; |
6108 | 0 | } |
6109 | | |
6110 | | /* Try to get wrapped SymWrapping key out of the (disk) cache. */ |
6111 | | /* Following call fills in wswk on success. */ |
6112 | 0 | rv = ssl_GetWrappingKey(wrapMechIndex, wrapKeyIndex, &wswk); |
6113 | 0 | if (rv == SECSuccess) { |
6114 | | /* found the wrapped sym wrapping key on disk. */ |
6115 | 0 | unwrappedWrappingKey = |
6116 | 0 | ssl_UnwrapSymWrappingKey(&wswk, svrPrivKey, wrapKeyIndex, |
6117 | 0 | masterWrapMech, pwArg); |
6118 | 0 | if (unwrappedWrappingKey) { |
6119 | 0 | goto install; |
6120 | 0 | } |
6121 | 0 | } |
6122 | | |
6123 | 0 | if (!masterSecretSlot) /* caller doesn't want to create a new one. */ |
6124 | 0 | goto loser; |
6125 | | |
6126 | 0 | length = PK11_GetBestKeyLength(masterSecretSlot, masterWrapMech); |
6127 | | /* Zero length means fixed key length algorithm, or error. |
6128 | | * It's ambiguous. |
6129 | | */ |
6130 | 0 | unwrappedWrappingKey = PK11_KeyGen(masterSecretSlot, masterWrapMech, NULL, |
6131 | 0 | length, pwArg); |
6132 | 0 | if (!unwrappedWrappingKey) { |
6133 | 0 | goto loser; |
6134 | 0 | } |
6135 | | |
6136 | | /* Prepare the buffer to receive the wrappedWrappingKey, |
6137 | | * the symmetric wrapping key wrapped using the server's pub key. |
6138 | | */ |
6139 | 0 | PORT_Memset(&wswk, 0, sizeof wswk); /* eliminate UMRs. */ |
6140 | |
|
6141 | 0 | svrPubKey = serverCert->serverKeyPair->pubKey; |
6142 | 0 | wrappedKey.type = siBuffer; |
6143 | 0 | wrappedKey.len = SECKEY_PublicKeyStrength(svrPubKey); |
6144 | 0 | wrappedKey.data = wswk.wrappedSymmetricWrappingkey; |
6145 | |
|
6146 | 0 | PORT_Assert(wrappedKey.len <= sizeof wswk.wrappedSymmetricWrappingkey); |
6147 | 0 | if (wrappedKey.len > sizeof wswk.wrappedSymmetricWrappingkey) |
6148 | 0 | goto loser; |
6149 | | |
6150 | | /* wrap symmetric wrapping key in server's public key. */ |
6151 | 0 | switch (authType) { |
6152 | 0 | case ssl_auth_rsa_decrypt: |
6153 | 0 | case ssl_auth_rsa_sign: /* bad: see Bug 1248320 */ |
6154 | 0 | case ssl_auth_rsa_pss: |
6155 | 0 | asymWrapMechanism = CKM_RSA_PKCS; |
6156 | 0 | rv = PK11_PubWrapSymKey(asymWrapMechanism, svrPubKey, |
6157 | 0 | unwrappedWrappingKey, &wrappedKey); |
6158 | 0 | break; |
6159 | | |
6160 | 0 | case ssl_auth_ecdsa: |
6161 | 0 | case ssl_auth_ecdh_rsa: |
6162 | 0 | case ssl_auth_ecdh_ecdsa: |
6163 | | /* |
6164 | | * We generate an ephemeral EC key pair. Perform an ECDH |
6165 | | * computation involving this ephemeral EC public key and |
6166 | | * the SSL server's (long-term) EC private key. The resulting |
6167 | | * shared secret is treated in the same way as Fortezza's Ks, |
6168 | | * i.e., it is used to wrap the wrapping key. To facilitate |
6169 | | * unwrapping in ssl_UnwrapWrappingKey, we also store all |
6170 | | * relevant info about the ephemeral EC public key in |
6171 | | * wswk.wrappedSymmetricWrappingkey and lay it out as |
6172 | | * described in the ECCWrappedKeyInfo structure. |
6173 | | */ |
6174 | 0 | PORT_Assert(SECKEY_GetPublicKeyType(svrPubKey) == ecKey); |
6175 | 0 | if (SECKEY_GetPublicKeyType(svrPubKey) != ecKey) { |
6176 | | /* something is wrong in sslsecur.c if this isn't an ecKey */ |
6177 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6178 | 0 | rv = SECFailure; |
6179 | 0 | goto ec_cleanup; |
6180 | 0 | } |
6181 | | |
6182 | 0 | privWrapKey = SECKEY_CreateECPrivateKey( |
6183 | 0 | &svrPubKey->u.ec.DEREncodedParams, &pubWrapKey, NULL); |
6184 | 0 | if ((privWrapKey == NULL) || (pubWrapKey == NULL)) { |
6185 | 0 | rv = SECFailure; |
6186 | 0 | goto ec_cleanup; |
6187 | 0 | } |
6188 | | |
6189 | | /* Set the key size in bits */ |
6190 | 0 | if (pubWrapKey->u.ec.size == 0) { |
6191 | 0 | pubWrapKey->u.ec.size = SECKEY_PublicKeyStrengthInBits(svrPubKey); |
6192 | 0 | } |
6193 | |
|
6194 | 0 | PORT_Assert(pubWrapKey->u.ec.DEREncodedParams.len + |
6195 | 0 | pubWrapKey->u.ec.publicValue.len < |
6196 | 0 | MAX_EC_WRAPPED_KEY_BUFLEN); |
6197 | 0 | if (pubWrapKey->u.ec.DEREncodedParams.len + |
6198 | 0 | pubWrapKey->u.ec.publicValue.len >= |
6199 | 0 | MAX_EC_WRAPPED_KEY_BUFLEN) { |
6200 | 0 | PORT_SetError(SEC_ERROR_INVALID_KEY); |
6201 | 0 | rv = SECFailure; |
6202 | 0 | goto ec_cleanup; |
6203 | 0 | } |
6204 | | |
6205 | | /* Derive Ks using ECDH */ |
6206 | 0 | Ks = PK11_PubDeriveWithKDF(svrPrivKey, pubWrapKey, PR_FALSE, NULL, |
6207 | 0 | NULL, CKM_ECDH1_DERIVE, masterWrapMech, |
6208 | 0 | CKA_DERIVE, 0, CKD_NULL, NULL, NULL); |
6209 | 0 | if (Ks == NULL) { |
6210 | 0 | rv = SECFailure; |
6211 | 0 | goto ec_cleanup; |
6212 | 0 | } |
6213 | | |
6214 | 0 | ecWrapped = (ECCWrappedKeyInfo *)(wswk.wrappedSymmetricWrappingkey); |
6215 | 0 | ecWrapped->size = pubWrapKey->u.ec.size; |
6216 | 0 | ecWrapped->encodedParamLen = pubWrapKey->u.ec.DEREncodedParams.len; |
6217 | 0 | PORT_Memcpy(ecWrapped->var, pubWrapKey->u.ec.DEREncodedParams.data, |
6218 | 0 | pubWrapKey->u.ec.DEREncodedParams.len); |
6219 | |
|
6220 | 0 | ecWrapped->pubValueLen = pubWrapKey->u.ec.publicValue.len; |
6221 | 0 | PORT_Memcpy(ecWrapped->var + ecWrapped->encodedParamLen, |
6222 | 0 | pubWrapKey->u.ec.publicValue.data, |
6223 | 0 | pubWrapKey->u.ec.publicValue.len); |
6224 | |
|
6225 | 0 | wrappedKey.len = MAX_EC_WRAPPED_KEY_BUFLEN - |
6226 | 0 | (ecWrapped->encodedParamLen + ecWrapped->pubValueLen); |
6227 | 0 | wrappedKey.data = ecWrapped->var + ecWrapped->encodedParamLen + |
6228 | 0 | ecWrapped->pubValueLen; |
6229 | | |
6230 | | /* wrap symmetricWrapping key with the local Ks */ |
6231 | 0 | rv = PK11_WrapSymKey(masterWrapMech, NULL, Ks, |
6232 | 0 | unwrappedWrappingKey, &wrappedKey); |
6233 | |
|
6234 | 0 | if (rv != SECSuccess) { |
6235 | 0 | goto ec_cleanup; |
6236 | 0 | } |
6237 | | |
6238 | | /* Write down the length of wrapped key in the buffer |
6239 | | * wswk.wrappedSymmetricWrappingkey at the appropriate offset |
6240 | | */ |
6241 | 0 | ecWrapped->wrappedKeyLen = wrappedKey.len; |
6242 | |
|
6243 | 0 | ec_cleanup: |
6244 | 0 | if (privWrapKey) |
6245 | 0 | SECKEY_DestroyPrivateKey(privWrapKey); |
6246 | 0 | if (pubWrapKey) |
6247 | 0 | SECKEY_DestroyPublicKey(pubWrapKey); |
6248 | 0 | if (Ks) |
6249 | 0 | PK11_FreeSymKey(Ks); |
6250 | 0 | asymWrapMechanism = masterWrapMech; |
6251 | 0 | break; |
6252 | | |
6253 | 0 | default: |
6254 | 0 | rv = SECFailure; |
6255 | 0 | break; |
6256 | 0 | } |
6257 | | |
6258 | 0 | if (rv != SECSuccess) { |
6259 | 0 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
6260 | 0 | goto loser; |
6261 | 0 | } |
6262 | | |
6263 | 0 | PORT_Assert(asymWrapMechanism != CKM_INVALID_MECHANISM); |
6264 | |
|
6265 | 0 | wswk.symWrapMechanism = masterWrapMech; |
6266 | 0 | wswk.asymWrapMechanism = asymWrapMechanism; |
6267 | 0 | wswk.wrapMechIndex = wrapMechIndex; |
6268 | 0 | wswk.wrapKeyIndex = wrapKeyIndex; |
6269 | 0 | wswk.wrappedSymKeyLen = wrappedKey.len; |
6270 | | |
6271 | | /* put it on disk. */ |
6272 | | /* If the wrapping key for this KEA type has already been set, |
6273 | | * then abandon the value we just computed and |
6274 | | * use the one we got from the disk. |
6275 | | */ |
6276 | 0 | rv = ssl_SetWrappingKey(&wswk); |
6277 | 0 | if (rv == SECSuccess) { |
6278 | | /* somebody beat us to it. The original contents of our wswk |
6279 | | * has been replaced with the content on disk. Now, discard |
6280 | | * the key we just created and unwrap this new one. |
6281 | | */ |
6282 | 0 | PK11_FreeSymKey(unwrappedWrappingKey); |
6283 | |
|
6284 | 0 | unwrappedWrappingKey = |
6285 | 0 | ssl_UnwrapSymWrappingKey(&wswk, svrPrivKey, wrapKeyIndex, |
6286 | 0 | masterWrapMech, pwArg); |
6287 | 0 | } |
6288 | |
|
6289 | 0 | install: |
6290 | 0 | if (unwrappedWrappingKey) { |
6291 | 0 | *pSymWrapKey = PK11_ReferenceSymKey(unwrappedWrappingKey); |
6292 | 0 | } |
6293 | |
|
6294 | 0 | loser: |
6295 | 0 | done: |
6296 | 0 | PZ_Unlock(symWrapKeysLock); |
6297 | 0 | return unwrappedWrappingKey; |
6298 | 0 | } |
6299 | | |
6300 | | #ifdef NSS_ALLOW_SSLKEYLOGFILE |
6301 | | /* hexEncode hex encodes |length| bytes from |in| and writes it as |length*2| |
6302 | | * bytes to |out|. */ |
6303 | | static void |
6304 | | hexEncode(char *out, const unsigned char *in, unsigned int length) |
6305 | 0 | { |
6306 | 0 | static const char hextable[] = "0123456789abcdef"; |
6307 | 0 | unsigned int i; |
6308 | |
|
6309 | 0 | for (i = 0; i < length; i++) { |
6310 | 0 | *(out++) = hextable[in[i] >> 4]; |
6311 | 0 | *(out++) = hextable[in[i] & 15]; |
6312 | 0 | } |
6313 | 0 | } |
6314 | | #endif |
6315 | | |
6316 | | /* Called from ssl3_SendClientKeyExchange(). */ |
6317 | | static SECStatus |
6318 | | ssl3_SendRSAClientKeyExchange(sslSocket *ss, SECKEYPublicKey *svrPubKey) |
6319 | 5.38k | { |
6320 | 5.38k | PK11SymKey *pms = NULL; |
6321 | 5.38k | SECStatus rv = SECFailure; |
6322 | 5.38k | SECItem enc_pms = { siBuffer, NULL, 0 }; |
6323 | 5.38k | PRBool isTLS; |
6324 | | |
6325 | 5.38k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
6326 | 5.38k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
6327 | | |
6328 | | /* Generate the pre-master secret ... */ |
6329 | 5.38k | ssl_GetSpecWriteLock(ss); |
6330 | 5.38k | isTLS = (PRBool)(ss->version > SSL_LIBRARY_VERSION_3_0); |
6331 | | |
6332 | 5.38k | pms = ssl3_GenerateRSAPMS(ss, ss->ssl3.pwSpec, NULL); |
6333 | 5.38k | ssl_ReleaseSpecWriteLock(ss); |
6334 | 5.38k | if (pms == NULL) { |
6335 | 0 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
6336 | 0 | goto loser; |
6337 | 0 | } |
6338 | | |
6339 | | /* Get the wrapped (encrypted) pre-master secret, enc_pms */ |
6340 | 5.38k | unsigned int svrPubKeyBits = SECKEY_PublicKeyStrengthInBits(svrPubKey); |
6341 | 5.38k | enc_pms.len = (svrPubKeyBits + 7) / 8; |
6342 | | /* Check that the RSA key isn't larger than 8k bit. */ |
6343 | 5.38k | if (svrPubKeyBits > SSL_MAX_RSA_KEY_BITS) { |
6344 | 1 | (void)SSL3_SendAlert(ss, alert_fatal, illegal_parameter); |
6345 | 1 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
6346 | 1 | goto loser; |
6347 | 1 | } |
6348 | 5.38k | enc_pms.data = (unsigned char *)PORT_Alloc(enc_pms.len); |
6349 | 5.38k | if (enc_pms.data == NULL) { |
6350 | 0 | goto loser; /* err set by PORT_Alloc */ |
6351 | 0 | } |
6352 | | |
6353 | | /* Wrap pre-master secret in server's public key. */ |
6354 | 5.38k | rv = PK11_PubWrapSymKey(CKM_RSA_PKCS, svrPubKey, pms, &enc_pms); |
6355 | 5.38k | if (rv != SECSuccess) { |
6356 | 35 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
6357 | 35 | goto loser; |
6358 | 35 | } |
6359 | | |
6360 | 5.35k | #ifdef TRACE |
6361 | 5.35k | if (ssl_trace >= 100) { |
6362 | 0 | SECStatus extractRV = PK11_ExtractKeyValue(pms); |
6363 | 0 | if (extractRV == SECSuccess) { |
6364 | 0 | SECItem *keyData = PK11_GetKeyData(pms); |
6365 | 0 | if (keyData && keyData->data && keyData->len) { |
6366 | 0 | ssl_PrintBuf(ss, "Pre-Master Secret", |
6367 | 0 | keyData->data, keyData->len); |
6368 | 0 | } |
6369 | 0 | } |
6370 | 0 | } |
6371 | 5.35k | #endif |
6372 | | |
6373 | 5.35k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_client_key_exchange, |
6374 | 5.35k | isTLS ? enc_pms.len + 2 |
6375 | 5.35k | : enc_pms.len); |
6376 | 5.35k | if (rv != SECSuccess) { |
6377 | 0 | goto loser; /* err set by ssl3_AppendHandshake* */ |
6378 | 0 | } |
6379 | 5.35k | if (isTLS) { |
6380 | 5.35k | rv = ssl3_AppendHandshakeVariable(ss, enc_pms.data, enc_pms.len, 2); |
6381 | 5.35k | } else { |
6382 | 0 | rv = ssl3_AppendHandshake(ss, enc_pms.data, enc_pms.len); |
6383 | 0 | } |
6384 | 5.35k | if (rv != SECSuccess) { |
6385 | 0 | goto loser; /* err set by ssl3_AppendHandshake* */ |
6386 | 0 | } |
6387 | | |
6388 | 5.35k | rv = ssl3_InitPendingCipherSpecs(ss, pms, PR_TRUE); |
6389 | 5.35k | PK11_FreeSymKey(pms); |
6390 | 5.35k | pms = NULL; |
6391 | | |
6392 | 5.35k | if (rv != SECSuccess) { |
6393 | 0 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
6394 | 0 | goto loser; |
6395 | 0 | } |
6396 | | |
6397 | 5.35k | rv = SECSuccess; |
6398 | | |
6399 | 5.38k | loser: |
6400 | 5.38k | if (enc_pms.data != NULL) { |
6401 | 5.38k | PORT_Free(enc_pms.data); |
6402 | 5.38k | } |
6403 | 5.38k | if (pms != NULL) { |
6404 | 36 | PK11_FreeSymKey(pms); |
6405 | 36 | } |
6406 | 5.38k | return rv; |
6407 | 5.35k | } |
6408 | | |
6409 | | /* DH shares need to be padded to the size of their prime. Some implementations |
6410 | | * require this. TLS 1.3 also requires this. */ |
6411 | | SECStatus |
6412 | | ssl_AppendPaddedDHKeyShare(sslBuffer *buf, const SECKEYPublicKey *pubKey, |
6413 | | PRBool appendLength) |
6414 | 1.31k | { |
6415 | 1.31k | SECStatus rv; |
6416 | 1.31k | unsigned int pad = pubKey->u.dh.prime.len - pubKey->u.dh.publicValue.len; |
6417 | | |
6418 | 1.31k | if (appendLength) { |
6419 | 1.18k | rv = sslBuffer_AppendNumber(buf, pubKey->u.dh.prime.len, 2); |
6420 | 1.18k | if (rv != SECSuccess) { |
6421 | 0 | return rv; |
6422 | 0 | } |
6423 | 1.18k | } |
6424 | 88.1k | while (pad) { |
6425 | 86.8k | rv = sslBuffer_AppendNumber(buf, 0, 1); |
6426 | 86.8k | if (rv != SECSuccess) { |
6427 | 0 | return rv; |
6428 | 0 | } |
6429 | 86.8k | --pad; |
6430 | 86.8k | } |
6431 | 1.31k | rv = sslBuffer_Append(buf, pubKey->u.dh.publicValue.data, |
6432 | 1.31k | pubKey->u.dh.publicValue.len); |
6433 | 1.31k | if (rv != SECSuccess) { |
6434 | 0 | return rv; |
6435 | 0 | } |
6436 | 1.31k | return SECSuccess; |
6437 | 1.31k | } |
6438 | | |
6439 | | /* Called from ssl3_SendClientKeyExchange(). */ |
6440 | | static SECStatus |
6441 | | ssl3_SendDHClientKeyExchange(sslSocket *ss, SECKEYPublicKey *svrPubKey) |
6442 | 1.18k | { |
6443 | 1.18k | PK11SymKey *pms = NULL; |
6444 | 1.18k | SECStatus rv; |
6445 | 1.18k | PRBool isTLS; |
6446 | 1.18k | CK_MECHANISM_TYPE target; |
6447 | | |
6448 | 1.18k | const ssl3DHParams *params; |
6449 | 1.18k | ssl3DHParams customParams; |
6450 | 1.18k | const sslNamedGroupDef *groupDef; |
6451 | 1.18k | static const sslNamedGroupDef customGroupDef = { |
6452 | 1.18k | ssl_grp_ffdhe_custom, 0, ssl_kea_dh, SEC_OID_TLS_DHE_CUSTOM, PR_FALSE |
6453 | 1.18k | }; |
6454 | 1.18k | sslEphemeralKeyPair *keyPair = NULL; |
6455 | 1.18k | SECKEYPublicKey *pubKey; |
6456 | 1.18k | PRUint8 dhData[SSL_MAX_DH_KEY_BITS / 8 + 2]; |
6457 | 1.18k | sslBuffer dhBuf = SSL_BUFFER(dhData); |
6458 | | |
6459 | 1.18k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
6460 | 1.18k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
6461 | | |
6462 | 1.18k | isTLS = (PRBool)(ss->version > SSL_LIBRARY_VERSION_3_0); |
6463 | | |
6464 | | /* Copy DH parameters from server key */ |
6465 | | |
6466 | 1.18k | if (SECKEY_GetPublicKeyType(svrPubKey) != dhKey) { |
6467 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
6468 | 0 | return SECFailure; |
6469 | 0 | } |
6470 | | |
6471 | | /* Work out the parameters. */ |
6472 | 1.18k | rv = ssl_ValidateDHENamedGroup(ss, &svrPubKey->u.dh.prime, |
6473 | 1.18k | &svrPubKey->u.dh.base, |
6474 | 1.18k | &groupDef, ¶ms); |
6475 | 1.18k | if (rv != SECSuccess) { |
6476 | | /* If we require named groups, we will have already validated the group |
6477 | | * in ssl_HandleDHServerKeyExchange() */ |
6478 | 1.18k | PORT_Assert(!ss->opt.requireDHENamedGroups && |
6479 | 1.18k | !ss->xtnData.peerSupportsFfdheGroups); |
6480 | | |
6481 | 1.18k | customParams.name = ssl_grp_ffdhe_custom; |
6482 | 1.18k | customParams.prime.data = svrPubKey->u.dh.prime.data; |
6483 | 1.18k | customParams.prime.len = svrPubKey->u.dh.prime.len; |
6484 | 1.18k | customParams.base.data = svrPubKey->u.dh.base.data; |
6485 | 1.18k | customParams.base.len = svrPubKey->u.dh.base.len; |
6486 | 1.18k | params = &customParams; |
6487 | 1.18k | groupDef = &customGroupDef; |
6488 | 1.18k | } |
6489 | 1.18k | ss->sec.keaGroup = groupDef; |
6490 | | |
6491 | 1.18k | rv = ssl_CreateDHEKeyPair(groupDef, params, &keyPair); |
6492 | 1.18k | if (rv != SECSuccess) { |
6493 | 0 | ssl_MapLowLevelError(SEC_ERROR_KEYGEN_FAIL); |
6494 | 0 | goto loser; |
6495 | 0 | } |
6496 | 1.18k | pubKey = keyPair->keys->pubKey; |
6497 | 1.18k | PRINT_BUF(50, (ss, "DH public value:", |
6498 | 1.18k | pubKey->u.dh.publicValue.data, |
6499 | 1.18k | pubKey->u.dh.publicValue.len)); |
6500 | | |
6501 | 1.18k | if (isTLS) |
6502 | 1.18k | target = CKM_TLS_MASTER_KEY_DERIVE_DH; |
6503 | 0 | else |
6504 | 0 | target = CKM_SSL3_MASTER_KEY_DERIVE_DH; |
6505 | | |
6506 | | /* Determine the PMS */ |
6507 | 1.18k | pms = PK11_PubDerive(keyPair->keys->privKey, svrPubKey, |
6508 | 1.18k | PR_FALSE, NULL, NULL, CKM_DH_PKCS_DERIVE, |
6509 | 1.18k | target, CKA_DERIVE, 0, NULL); |
6510 | | |
6511 | 1.18k | if (pms == NULL) { |
6512 | 3 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
6513 | 3 | goto loser; |
6514 | 3 | } |
6515 | | |
6516 | | /* Note: send the DH share padded to avoid triggering bugs. */ |
6517 | 1.18k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_client_key_exchange, |
6518 | 1.18k | params->prime.len + 2); |
6519 | 1.18k | if (rv != SECSuccess) { |
6520 | 0 | goto loser; /* err set by ssl3_AppendHandshake* */ |
6521 | 0 | } |
6522 | 1.18k | rv = ssl_AppendPaddedDHKeyShare(&dhBuf, pubKey, PR_TRUE); |
6523 | 1.18k | if (rv != SECSuccess) { |
6524 | 0 | goto loser; /* err set by ssl_AppendPaddedDHKeyShare */ |
6525 | 0 | } |
6526 | 1.18k | rv = ssl3_AppendBufferToHandshake(ss, &dhBuf); |
6527 | 1.18k | if (rv != SECSuccess) { |
6528 | 0 | goto loser; /* err set by ssl3_AppendBufferToHandshake */ |
6529 | 0 | } |
6530 | | |
6531 | 1.18k | rv = ssl3_InitPendingCipherSpecs(ss, pms, PR_TRUE); |
6532 | 1.18k | if (rv != SECSuccess) { |
6533 | 0 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
6534 | 0 | goto loser; |
6535 | 0 | } |
6536 | | |
6537 | 1.18k | sslBuffer_Clear(&dhBuf); |
6538 | 1.18k | PK11_FreeSymKey(pms); |
6539 | 1.18k | ssl_FreeEphemeralKeyPair(keyPair); |
6540 | 1.18k | return SECSuccess; |
6541 | | |
6542 | 3 | loser: |
6543 | 3 | if (pms) |
6544 | 0 | PK11_FreeSymKey(pms); |
6545 | 3 | if (keyPair) |
6546 | 3 | ssl_FreeEphemeralKeyPair(keyPair); |
6547 | 3 | sslBuffer_Clear(&dhBuf); |
6548 | 3 | return SECFailure; |
6549 | 1.18k | } |
6550 | | |
6551 | | /* Called from ssl3_HandleServerHelloDone(). */ |
6552 | | static SECStatus |
6553 | | ssl3_SendClientKeyExchange(sslSocket *ss) |
6554 | 33.4k | { |
6555 | 33.4k | SECKEYPublicKey *serverKey = NULL; |
6556 | 33.4k | SECStatus rv = SECFailure; |
6557 | | |
6558 | 33.4k | SSL_TRC(3, ("%d: SSL3[%d]: send client_key_exchange handshake", |
6559 | 33.4k | SSL_GETPID(), ss->fd)); |
6560 | | |
6561 | 33.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
6562 | 33.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
6563 | | |
6564 | 33.4k | if (ss->sec.peerKey == NULL) { |
6565 | 27.9k | serverKey = CERT_ExtractPublicKey(ss->sec.peerCert); |
6566 | 27.9k | if (serverKey == NULL) { |
6567 | 0 | ssl_MapLowLevelError(SSL_ERROR_EXTRACT_PUBLIC_KEY_FAILURE); |
6568 | 0 | return SECFailure; |
6569 | 0 | } |
6570 | 27.9k | } else { |
6571 | 5.43k | serverKey = ss->sec.peerKey; |
6572 | 5.43k | ss->sec.peerKey = NULL; /* we're done with it now */ |
6573 | 5.43k | } |
6574 | | |
6575 | 33.4k | ss->sec.keaType = ss->ssl3.hs.kea_def->exchKeyType; |
6576 | 33.4k | ss->sec.keaKeyBits = SECKEY_PublicKeyStrengthInBits(serverKey); |
6577 | | |
6578 | 33.4k | switch (ss->ssl3.hs.kea_def->exchKeyType) { |
6579 | 5.38k | case ssl_kea_rsa: |
6580 | 5.38k | rv = ssl3_SendRSAClientKeyExchange(ss, serverKey); |
6581 | 5.38k | break; |
6582 | | |
6583 | 1.18k | case ssl_kea_dh: |
6584 | 1.18k | rv = ssl3_SendDHClientKeyExchange(ss, serverKey); |
6585 | 1.18k | break; |
6586 | | |
6587 | 26.8k | case ssl_kea_ecdh: |
6588 | 26.8k | rv = ssl3_SendECDHClientKeyExchange(ss, serverKey); |
6589 | 26.8k | break; |
6590 | | |
6591 | 0 | default: |
6592 | 0 | PORT_Assert(0); |
6593 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6594 | 0 | break; |
6595 | 33.4k | } |
6596 | | |
6597 | 33.4k | SSL_TRC(3, ("%d: SSL3[%d]: DONE sending client_key_exchange", |
6598 | 33.4k | SSL_GETPID(), ss->fd)); |
6599 | | |
6600 | 33.4k | SECKEY_DestroyPublicKey(serverKey); |
6601 | 33.4k | return rv; /* err code already set. */ |
6602 | 33.4k | } |
6603 | | |
6604 | | /* Used by ssl_PickSignatureScheme(). */ |
6605 | | PRBool |
6606 | | ssl_CanUseSignatureScheme(SSLSignatureScheme scheme, |
6607 | | const SSLSignatureScheme *peerSchemes, |
6608 | | unsigned int peerSchemeCount, |
6609 | | PRBool requireSha1, |
6610 | | PRBool slotDoesPss) |
6611 | 0 | { |
6612 | 0 | SSLHashType hashType; |
6613 | 0 | unsigned int i; |
6614 | | |
6615 | | /* Skip RSA-PSS schemes when the certificate's private key slot does |
6616 | | * not support this signature mechanism. */ |
6617 | 0 | if (ssl_IsRsaPssSignatureScheme(scheme) && !slotDoesPss) { |
6618 | 0 | return PR_FALSE; |
6619 | 0 | } |
6620 | | |
6621 | 0 | hashType = ssl_SignatureSchemeToHashType(scheme); |
6622 | 0 | if (requireSha1 && (hashType != ssl_hash_sha1)) { |
6623 | 0 | return PR_FALSE; |
6624 | 0 | } |
6625 | | |
6626 | 0 | if (!ssl_SchemePolicyOK(scheme, kSSLSigSchemePolicy)) { |
6627 | 0 | return PR_FALSE; |
6628 | 0 | } |
6629 | | |
6630 | 0 | for (i = 0; i < peerSchemeCount; i++) { |
6631 | 0 | if (peerSchemes[i] == scheme) { |
6632 | 0 | return PR_TRUE; |
6633 | 0 | } |
6634 | 0 | } |
6635 | 0 | return PR_FALSE; |
6636 | 0 | } |
6637 | | |
6638 | | SECStatus |
6639 | | ssl_PrivateKeySupportsRsaPss(SECKEYPrivateKey *privKey, CERTCertificate *cert, |
6640 | | void *pwarg, PRBool *supportsRsaPss) |
6641 | 0 | { |
6642 | 0 | PK11SlotInfo *slot = NULL; |
6643 | 0 | if (privKey) { |
6644 | 0 | slot = PK11_GetSlotFromPrivateKey(privKey); |
6645 | 0 | } else { |
6646 | 0 | CK_OBJECT_HANDLE certID = PK11_FindObjectForCert(cert, pwarg, &slot); |
6647 | 0 | if (certID == CK_INVALID_HANDLE) { |
6648 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6649 | 0 | return SECFailure; |
6650 | 0 | } |
6651 | 0 | } |
6652 | 0 | if (!slot) { |
6653 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6654 | 0 | return SECFailure; |
6655 | 0 | } |
6656 | 0 | *supportsRsaPss = PK11_DoesMechanism(slot, auth_alg_defs[ssl_auth_rsa_pss]); |
6657 | 0 | PK11_FreeSlot(slot); |
6658 | 0 | return SECSuccess; |
6659 | 0 | } |
6660 | | |
6661 | | SECStatus |
6662 | | ssl_PickSignatureScheme(sslSocket *ss, |
6663 | | CERTCertificate *cert, |
6664 | | SECKEYPublicKey *pubKey, |
6665 | | SECKEYPrivateKey *privKey, |
6666 | | const SSLSignatureScheme *peerSchemes, |
6667 | | unsigned int peerSchemeCount, |
6668 | | PRBool requireSha1, |
6669 | | SSLSignatureScheme *schemePtr) |
6670 | 0 | { |
6671 | 0 | unsigned int i; |
6672 | 0 | PRBool doesRsaPss; |
6673 | 0 | PRBool isTLS13 = ss->version >= SSL_LIBRARY_VERSION_TLS_1_3; |
6674 | 0 | SECStatus rv; |
6675 | 0 | SSLSignatureScheme scheme; |
6676 | 0 | SECOidTag spkiOid; |
6677 | | |
6678 | | /* We can't require SHA-1 in TLS 1.3. */ |
6679 | 0 | PORT_Assert(!(requireSha1 && isTLS13)); |
6680 | 0 | if (!pubKey || !cert) { |
6681 | 0 | PORT_Assert(0); |
6682 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6683 | 0 | return SECFailure; |
6684 | 0 | } |
6685 | 0 | rv = ssl_PrivateKeySupportsRsaPss(privKey, cert, ss->pkcs11PinArg, |
6686 | 0 | &doesRsaPss); |
6687 | 0 | if (rv != SECSuccess) { |
6688 | 0 | return SECFailure; |
6689 | 0 | } |
6690 | | |
6691 | | /* If the certificate SPKI indicates a single scheme, don't search. */ |
6692 | 0 | rv = ssl_SignatureSchemeFromSpki(&cert->subjectPublicKeyInfo, |
6693 | 0 | isTLS13, &scheme); |
6694 | 0 | if (rv != SECSuccess) { |
6695 | 0 | return SECFailure; |
6696 | 0 | } |
6697 | 0 | if (scheme != ssl_sig_none) { |
6698 | 0 | if (!ssl_SignatureSchemeEnabled(ss, scheme) || |
6699 | 0 | !ssl_CanUseSignatureScheme(scheme, peerSchemes, peerSchemeCount, |
6700 | 0 | requireSha1, doesRsaPss)) { |
6701 | 0 | PORT_SetError(SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM); |
6702 | 0 | return SECFailure; |
6703 | 0 | } |
6704 | 0 | *schemePtr = scheme; |
6705 | 0 | return SECSuccess; |
6706 | 0 | } |
6707 | | |
6708 | 0 | spkiOid = SECOID_GetAlgorithmTag(&cert->subjectPublicKeyInfo.algorithm); |
6709 | 0 | if (spkiOid == SEC_OID_UNKNOWN) { |
6710 | 0 | return SECFailure; |
6711 | 0 | } |
6712 | | |
6713 | | /* Now we have to search based on the key type. Go through our preferred |
6714 | | * schemes in order and find the first that can be used. */ |
6715 | 0 | for (i = 0; i < ss->ssl3.signatureSchemeCount; ++i) { |
6716 | 0 | scheme = ss->ssl3.signatureSchemes[i]; |
6717 | |
|
6718 | 0 | if (ssl_SignatureSchemeValid(scheme, spkiOid, isTLS13) && |
6719 | 0 | ssl_CanUseSignatureScheme(scheme, peerSchemes, peerSchemeCount, |
6720 | 0 | requireSha1, doesRsaPss)) { |
6721 | 0 | *schemePtr = scheme; |
6722 | 0 | return SECSuccess; |
6723 | 0 | } |
6724 | 0 | } |
6725 | | |
6726 | 0 | PORT_SetError(SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM); |
6727 | 0 | return SECFailure; |
6728 | 0 | } |
6729 | | |
6730 | | static SECStatus |
6731 | | ssl_PickFallbackSignatureScheme(sslSocket *ss, SECKEYPublicKey *pubKey) |
6732 | 0 | { |
6733 | 0 | PRBool isTLS12 = ss->version >= SSL_LIBRARY_VERSION_TLS_1_2; |
6734 | |
|
6735 | 0 | switch (SECKEY_GetPublicKeyType(pubKey)) { |
6736 | 0 | case rsaKey: |
6737 | 0 | if (isTLS12) { |
6738 | 0 | ss->ssl3.hs.signatureScheme = ssl_sig_rsa_pkcs1_sha1; |
6739 | 0 | } else { |
6740 | 0 | ss->ssl3.hs.signatureScheme = ssl_sig_rsa_pkcs1_sha1md5; |
6741 | 0 | } |
6742 | 0 | break; |
6743 | 0 | case ecKey: |
6744 | 0 | ss->ssl3.hs.signatureScheme = ssl_sig_ecdsa_sha1; |
6745 | 0 | break; |
6746 | 0 | case dsaKey: |
6747 | 0 | ss->ssl3.hs.signatureScheme = ssl_sig_dsa_sha1; |
6748 | 0 | break; |
6749 | 0 | default: |
6750 | 0 | PORT_Assert(0); |
6751 | 0 | PORT_SetError(SEC_ERROR_INVALID_KEY); |
6752 | 0 | return SECFailure; |
6753 | 0 | } |
6754 | 0 | return SECSuccess; |
6755 | 0 | } |
6756 | | |
6757 | | /* ssl3_PickServerSignatureScheme selects a signature scheme for signing the |
6758 | | * handshake. Most of this is determined by the key pair we are using. |
6759 | | * Prior to TLS 1.2, the MD5/SHA1 combination is always used. With TLS 1.2, a |
6760 | | * client may advertise its support for signature and hash combinations. */ |
6761 | | static SECStatus |
6762 | | ssl3_PickServerSignatureScheme(sslSocket *ss) |
6763 | 0 | { |
6764 | 0 | const sslServerCert *cert = ss->sec.serverCert; |
6765 | 0 | PRBool isTLS12 = ss->version >= SSL_LIBRARY_VERSION_TLS_1_2; |
6766 | |
|
6767 | 0 | if (!isTLS12 || !ssl3_ExtensionNegotiated(ss, ssl_signature_algorithms_xtn)) { |
6768 | | /* If the client didn't provide any signature_algorithms extension then |
6769 | | * we can assume that they support SHA-1: RFC5246, Section 7.4.1.4.1. */ |
6770 | 0 | return ssl_PickFallbackSignatureScheme(ss, cert->serverKeyPair->pubKey); |
6771 | 0 | } |
6772 | | |
6773 | | /* Sets error code, if needed. */ |
6774 | 0 | return ssl_PickSignatureScheme(ss, cert->serverCert, |
6775 | 0 | cert->serverKeyPair->pubKey, |
6776 | 0 | cert->serverKeyPair->privKey, |
6777 | 0 | ss->xtnData.sigSchemes, |
6778 | 0 | ss->xtnData.numSigSchemes, |
6779 | 0 | PR_FALSE /* requireSha1 */, |
6780 | 0 | &ss->ssl3.hs.signatureScheme); |
6781 | 0 | } |
6782 | | |
6783 | | SECStatus |
6784 | | ssl_PickClientSignatureScheme(sslSocket *ss, CERTCertificate *clientCertificate, |
6785 | | SECKEYPrivateKey *privKey, |
6786 | | const SSLSignatureScheme *schemes, |
6787 | | unsigned int numSchemes, |
6788 | | SSLSignatureScheme *schemePtr) |
6789 | 0 | { |
6790 | 0 | SECStatus rv; |
6791 | 0 | PRBool isTLS13 = (PRBool)ss->version >= SSL_LIBRARY_VERSION_TLS_1_3; |
6792 | 0 | SECKEYPublicKey *pubKey = CERT_ExtractPublicKey(clientCertificate); |
6793 | |
|
6794 | 0 | PORT_Assert(pubKey); |
6795 | |
|
6796 | 0 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_2) { |
6797 | | /* We should have already checked that a signature scheme was |
6798 | | * listed in the request. */ |
6799 | 0 | PORT_Assert(schemes && numSchemes > 0); |
6800 | 0 | } |
6801 | |
|
6802 | 0 | if (!isTLS13 && |
6803 | 0 | (SECKEY_GetPublicKeyType(pubKey) == rsaKey || |
6804 | 0 | SECKEY_GetPublicKeyType(pubKey) == dsaKey) && |
6805 | 0 | SECKEY_PublicKeyStrengthInBits(pubKey) <= 1024) { |
6806 | | /* If the key is a 1024-bit RSA or DSA key, assume conservatively that |
6807 | | * it may be unable to sign SHA-256 hashes. This is the case for older |
6808 | | * Estonian ID cards that have 1024-bit RSA keys. In FIPS 186-2 and |
6809 | | * older, DSA key size is at most 1024 bits and the hash function must |
6810 | | * be SHA-1. |
6811 | | */ |
6812 | 0 | rv = ssl_PickSignatureScheme(ss, clientCertificate, |
6813 | 0 | pubKey, privKey, schemes, numSchemes, |
6814 | 0 | PR_TRUE /* requireSha1 */, schemePtr); |
6815 | 0 | if (rv == SECSuccess) { |
6816 | 0 | SECKEY_DestroyPublicKey(pubKey); |
6817 | 0 | return SECSuccess; |
6818 | 0 | } |
6819 | | /* If this fails, that's because the peer doesn't advertise SHA-1, |
6820 | | * so fall back to the full negotiation. */ |
6821 | 0 | } |
6822 | 0 | rv = ssl_PickSignatureScheme(ss, clientCertificate, |
6823 | 0 | pubKey, privKey, schemes, numSchemes, |
6824 | 0 | PR_FALSE /* requireSha1 */, schemePtr); |
6825 | 0 | SECKEY_DestroyPublicKey(pubKey); |
6826 | 0 | return rv; |
6827 | 0 | } |
6828 | | |
6829 | | /* Called from ssl3_HandleServerHelloDone(). */ |
6830 | | static SECStatus |
6831 | | ssl3_SendCertificateVerify(sslSocket *ss, SECKEYPrivateKey *privKey) |
6832 | 0 | { |
6833 | 0 | SECStatus rv = SECFailure; |
6834 | 0 | PRBool isTLS12; |
6835 | 0 | SECItem buf = { siBuffer, NULL, 0 }; |
6836 | 0 | SSL3Hashes hashes; |
6837 | 0 | unsigned int len; |
6838 | 0 | SSLHashType hashAlg; |
6839 | |
|
6840 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
6841 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
6842 | |
|
6843 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: send certificate_verify handshake", |
6844 | 0 | SSL_GETPID(), ss->fd)); |
6845 | |
|
6846 | 0 | ssl_GetSpecReadLock(ss); |
6847 | |
|
6848 | 0 | if (ss->ssl3.hs.hashType == handshake_hash_record) { |
6849 | 0 | hashAlg = ssl_SignatureSchemeToHashType(ss->ssl3.hs.signatureScheme); |
6850 | 0 | } else { |
6851 | | /* Use ssl_hash_none to represent the MD5+SHA1 combo. */ |
6852 | 0 | hashAlg = ssl_hash_none; |
6853 | 0 | } |
6854 | 0 | if (ss->ssl3.hs.hashType == handshake_hash_record && |
6855 | 0 | hashAlg != ssl3_GetSuitePrfHash(ss)) { |
6856 | 0 | rv = ssl3_ComputeHandshakeHash(ss->ssl3.hs.messages.buf, |
6857 | 0 | ss->ssl3.hs.messages.len, |
6858 | 0 | hashAlg, &hashes); |
6859 | 0 | if (rv != SECSuccess) { |
6860 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
6861 | 0 | } |
6862 | 0 | } else { |
6863 | 0 | rv = ssl3_ComputeHandshakeHashes(ss, ss->ssl3.pwSpec, &hashes, 0); |
6864 | 0 | } |
6865 | 0 | ssl_ReleaseSpecReadLock(ss); |
6866 | 0 | if (rv != SECSuccess) { |
6867 | 0 | goto done; /* err code was set by ssl3_ComputeHandshakeHash(es) */ |
6868 | 0 | } |
6869 | | |
6870 | 0 | isTLS12 = (PRBool)(ss->version == SSL_LIBRARY_VERSION_TLS_1_2); |
6871 | 0 | PORT_Assert(ss->version <= SSL_LIBRARY_VERSION_TLS_1_2); |
6872 | |
|
6873 | 0 | rv = ssl3_SignHashes(ss, &hashes, privKey, &buf); |
6874 | 0 | if (rv == SECSuccess && !ss->sec.isServer) { |
6875 | | /* Remember the info about the slot that did the signing. |
6876 | | ** Later, when doing an SSL restart handshake, verify this. |
6877 | | ** These calls are mere accessors, and can't fail. |
6878 | | */ |
6879 | 0 | PK11SlotInfo *slot; |
6880 | 0 | sslSessionID *sid = ss->sec.ci.sid; |
6881 | |
|
6882 | 0 | slot = PK11_GetSlotFromPrivateKey(privKey); |
6883 | 0 | sid->u.ssl3.clAuthSeries = PK11_GetSlotSeries(slot); |
6884 | 0 | sid->u.ssl3.clAuthSlotID = PK11_GetSlotID(slot); |
6885 | 0 | sid->u.ssl3.clAuthModuleID = PK11_GetModuleID(slot); |
6886 | 0 | sid->u.ssl3.clAuthValid = PR_TRUE; |
6887 | 0 | PK11_FreeSlot(slot); |
6888 | 0 | } |
6889 | 0 | if (rv != SECSuccess) { |
6890 | 0 | goto done; /* err code was set by ssl3_SignHashes */ |
6891 | 0 | } |
6892 | | |
6893 | 0 | len = buf.len + 2 + (isTLS12 ? 2 : 0); |
6894 | |
|
6895 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_certificate_verify, len); |
6896 | 0 | if (rv != SECSuccess) { |
6897 | 0 | goto done; /* error code set by AppendHandshake */ |
6898 | 0 | } |
6899 | 0 | if (isTLS12) { |
6900 | 0 | rv = ssl3_AppendHandshakeNumber(ss, ss->ssl3.hs.signatureScheme, 2); |
6901 | 0 | if (rv != SECSuccess) { |
6902 | 0 | goto done; /* err set by AppendHandshake. */ |
6903 | 0 | } |
6904 | 0 | } |
6905 | 0 | rv = ssl3_AppendHandshakeVariable(ss, buf.data, buf.len, 2); |
6906 | 0 | if (rv != SECSuccess) { |
6907 | 0 | goto done; /* error code set by AppendHandshake */ |
6908 | 0 | } |
6909 | | |
6910 | 0 | done: |
6911 | 0 | if (buf.data) |
6912 | 0 | PORT_Free(buf.data); |
6913 | 0 | return rv; |
6914 | 0 | } |
6915 | | |
6916 | | /* Once a cipher suite has been selected, make sure that the necessary secondary |
6917 | | * information is properly set. */ |
6918 | | SECStatus |
6919 | | ssl3_SetupCipherSuite(sslSocket *ss, PRBool initHashes) |
6920 | 39.8k | { |
6921 | 39.8k | ss->ssl3.hs.suite_def = ssl_LookupCipherSuiteDef(ss->ssl3.hs.cipher_suite); |
6922 | 39.8k | if (!ss->ssl3.hs.suite_def) { |
6923 | 0 | PORT_Assert(0); |
6924 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6925 | 0 | return SECFailure; |
6926 | 0 | } |
6927 | | |
6928 | 39.8k | ss->ssl3.hs.kea_def = &kea_defs[ss->ssl3.hs.suite_def->key_exchange_alg]; |
6929 | 39.8k | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_cipher_suite; |
6930 | | |
6931 | 39.8k | if (!initHashes) { |
6932 | 344 | return SECSuccess; |
6933 | 344 | } |
6934 | | /* Now we have a cipher suite, initialize the handshake hashes. */ |
6935 | 39.4k | return ssl3_InitHandshakeHashes(ss); |
6936 | 39.8k | } |
6937 | | |
6938 | | SECStatus |
6939 | | ssl_ClientSetCipherSuite(sslSocket *ss, SSL3ProtocolVersion version, |
6940 | | ssl3CipherSuite suite, PRBool initHashes) |
6941 | 39.8k | { |
6942 | 39.8k | unsigned int i; |
6943 | 39.8k | if (ssl3_config_match_init(ss) == 0) { |
6944 | 0 | PORT_Assert(PR_FALSE); |
6945 | 0 | return SECFailure; |
6946 | 0 | } |
6947 | 1.70M | for (i = 0; i < ssl_V3_SUITES_IMPLEMENTED; i++) { |
6948 | 1.70M | ssl3CipherSuiteCfg *suiteCfg = &ss->cipherSuites[i]; |
6949 | 1.70M | if (suite == suiteCfg->cipher_suite) { |
6950 | 39.8k | SSLVersionRange vrange = { version, version }; |
6951 | 39.8k | if (!ssl3_config_match(suiteCfg, ss->ssl3.policy, &vrange, ss)) { |
6952 | | /* config_match already checks whether the cipher suite is |
6953 | | * acceptable for the version, but the check is repeated here |
6954 | | * in order to give a more precise error code. */ |
6955 | 33 | if (!ssl3_CipherSuiteAllowedForVersionRange(suite, &vrange)) { |
6956 | 33 | PORT_SetError(SSL_ERROR_CIPHER_DISALLOWED_FOR_VERSION); |
6957 | 33 | } else { |
6958 | 0 | PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP); |
6959 | 0 | } |
6960 | 33 | return SECFailure; |
6961 | 33 | } |
6962 | 39.8k | break; |
6963 | 39.8k | } |
6964 | 1.70M | } |
6965 | 39.8k | if (i >= ssl_V3_SUITES_IMPLEMENTED) { |
6966 | 13 | PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP); |
6967 | 13 | return SECFailure; |
6968 | 13 | } |
6969 | | |
6970 | | /* Don't let the server change its mind. */ |
6971 | 39.8k | if (ss->ssl3.hs.helloRetry && suite != ss->ssl3.hs.cipher_suite) { |
6972 | 1 | (void)SSL3_SendAlert(ss, alert_fatal, illegal_parameter); |
6973 | 1 | PORT_SetError(SSL_ERROR_RX_MALFORMED_SERVER_HELLO); |
6974 | 1 | return SECFailure; |
6975 | 1 | } |
6976 | | |
6977 | 39.8k | ss->ssl3.hs.cipher_suite = (ssl3CipherSuite)suite; |
6978 | 39.8k | return ssl3_SetupCipherSuite(ss, initHashes); |
6979 | 39.8k | } |
6980 | | |
6981 | | /* Check that session ID we received from the server, if any, matches our |
6982 | | * expectations, depending on whether we're in compat mode and whether we |
6983 | | * negotiated TLS 1.3+ or TLS 1.2-. |
6984 | | */ |
6985 | | static PRBool |
6986 | | ssl_CheckServerSessionIdCorrectness(sslSocket *ss, SECItem *sidBytes) |
6987 | 39.8k | { |
6988 | 39.8k | sslSessionID *sid = ss->sec.ci.sid; |
6989 | 39.8k | PRBool sidMatch = PR_FALSE; |
6990 | 39.8k | PRBool sentFakeSid = PR_FALSE; |
6991 | 39.8k | PRBool sentRealSid = sid && sid->version < SSL_LIBRARY_VERSION_TLS_1_3; |
6992 | | |
6993 | | /* If attempting to resume a TLS 1.2 connection, the session ID won't be a |
6994 | | * fake. Check for the real value. */ |
6995 | 39.8k | if (sentRealSid) { |
6996 | 32.4k | sidMatch = (sidBytes->len == sid->u.ssl3.sessionIDLength) && |
6997 | 32.4k | (!sidBytes->len || PORT_Memcmp(sid->u.ssl3.sessionID, sidBytes->data, sidBytes->len) == 0); |
6998 | 32.4k | } else { |
6999 | | /* Otherwise, the session ID was a fake if TLS 1.3 compat mode is |
7000 | | * enabled. If so, check for the fake value. */ |
7001 | 7.42k | sentFakeSid = ss->opt.enableTls13CompatMode && !IS_DTLS(ss); |
7002 | 7.42k | if (sentFakeSid && sidBytes->len == SSL3_SESSIONID_BYTES) { |
7003 | 1.90k | PRUint8 buf[SSL3_SESSIONID_BYTES]; |
7004 | 1.90k | ssl_MakeFakeSid(ss, buf); |
7005 | 1.90k | sidMatch = PORT_Memcmp(buf, sidBytes->data, sidBytes->len) == 0; |
7006 | 1.90k | } |
7007 | 7.42k | } |
7008 | | |
7009 | | /* TLS 1.2: Session ID shouldn't match if we sent a fake. */ |
7010 | 39.8k | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
7011 | 38.8k | if (sentFakeSid) { |
7012 | 3.21k | return !sidMatch; |
7013 | 3.21k | } |
7014 | 35.6k | return PR_TRUE; |
7015 | 38.8k | } |
7016 | | |
7017 | | /* TLS 1.3: We sent a session ID. The server's should match. */ |
7018 | 1.01k | if (!IS_DTLS(ss) && (sentRealSid || sentFakeSid)) { |
7019 | 14 | return sidMatch; |
7020 | 14 | } |
7021 | | |
7022 | | /* TLS 1.3 (no SID)/DTLS 1.3: The server shouldn't send a session ID. */ |
7023 | 998 | return sidBytes->len == 0; |
7024 | 1.01k | } |
7025 | | |
7026 | | static SECStatus |
7027 | | ssl_CheckServerRandom(sslSocket *ss) |
7028 | 41.5k | { |
7029 | | /* Check the ServerHello.random per [RFC 8446 Section 4.1.3]. |
7030 | | * |
7031 | | * TLS 1.3 clients receiving a ServerHello indicating TLS 1.2 or below |
7032 | | * MUST check that the last 8 bytes are not equal to either of these |
7033 | | * values. TLS 1.2 clients SHOULD also check that the last 8 bytes are |
7034 | | * not equal to the second value if the ServerHello indicates TLS 1.1 or |
7035 | | * below. If a match is found, the client MUST abort the handshake with |
7036 | | * an "illegal_parameter" alert. |
7037 | | */ |
7038 | 41.5k | SSL3ProtocolVersion checkVersion = |
7039 | 41.5k | ss->ssl3.downgradeCheckVersion ? ss->ssl3.downgradeCheckVersion |
7040 | 41.5k | : ss->vrange.max; |
7041 | | |
7042 | 41.5k | if (checkVersion >= SSL_LIBRARY_VERSION_TLS_1_2 && |
7043 | 41.5k | checkVersion > ss->version) { |
7044 | | /* Both sections use the same sentinel region. */ |
7045 | 32.4k | PRUint8 *downgrade_sentinel = |
7046 | 32.4k | ss->ssl3.hs.server_random + |
7047 | 32.4k | SSL3_RANDOM_LENGTH - sizeof(tls12_downgrade_random); |
7048 | | |
7049 | 32.4k | if (!PORT_Memcmp(downgrade_sentinel, |
7050 | 32.4k | tls12_downgrade_random, |
7051 | 32.4k | sizeof(tls12_downgrade_random)) || |
7052 | 32.4k | !PORT_Memcmp(downgrade_sentinel, |
7053 | 32.4k | tls1_downgrade_random, |
7054 | 32.4k | sizeof(tls1_downgrade_random))) { |
7055 | 2 | return SECFailure; |
7056 | 2 | } |
7057 | 32.4k | } |
7058 | | |
7059 | 41.5k | return SECSuccess; |
7060 | 41.5k | } |
7061 | | |
7062 | | /* Called from ssl3_HandleHandshakeMessage() when it has deciphered a complete |
7063 | | * ssl3 ServerHello message. |
7064 | | * Caller must hold Handshake and RecvBuf locks. |
7065 | | */ |
7066 | | static SECStatus |
7067 | | ssl3_HandleServerHello(sslSocket *ss, PRUint8 *b, PRUint32 length) |
7068 | 40.3k | { |
7069 | 40.3k | PRUint32 cipher; |
7070 | 40.3k | int errCode = SSL_ERROR_RX_MALFORMED_SERVER_HELLO; |
7071 | 40.3k | PRUint32 compression; |
7072 | 40.3k | SECStatus rv; |
7073 | 40.3k | SECItem sidBytes = { siBuffer, NULL, 0 }; |
7074 | 40.3k | PRBool isHelloRetry; |
7075 | 40.3k | SSL3AlertDescription desc = illegal_parameter; |
7076 | 40.3k | const PRUint8 *savedMsg = b; |
7077 | 40.3k | const PRUint32 savedLength = length; |
7078 | | |
7079 | 40.3k | SSL_TRC(3, ("%d: SSL3[%d]: handle server_hello handshake", |
7080 | 40.3k | SSL_GETPID(), ss->fd)); |
7081 | 40.3k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
7082 | 40.3k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
7083 | | |
7084 | 40.3k | if (ss->ssl3.hs.ws != wait_server_hello) { |
7085 | 69 | errCode = SSL_ERROR_RX_UNEXPECTED_SERVER_HELLO; |
7086 | 69 | desc = unexpected_message; |
7087 | 69 | goto alert_loser; |
7088 | 69 | } |
7089 | | |
7090 | | /* clean up anything left from previous handshake. */ |
7091 | 40.2k | if (ss->ssl3.clientCertChain != NULL) { |
7092 | 0 | CERT_DestroyCertificateList(ss->ssl3.clientCertChain); |
7093 | 0 | ss->ssl3.clientCertChain = NULL; |
7094 | 0 | } |
7095 | 40.2k | if (ss->ssl3.clientCertificate != NULL) { |
7096 | 0 | CERT_DestroyCertificate(ss->ssl3.clientCertificate); |
7097 | 0 | ss->ssl3.clientCertificate = NULL; |
7098 | 0 | } |
7099 | 40.2k | if (ss->ssl3.clientPrivateKey != NULL) { |
7100 | 0 | SECKEY_DestroyPrivateKey(ss->ssl3.clientPrivateKey); |
7101 | 0 | ss->ssl3.clientPrivateKey = NULL; |
7102 | 0 | } |
7103 | | // TODO(djackson) - Bob removed this. Why? |
7104 | 40.2k | if (ss->ssl3.hs.clientAuthSignatureSchemes != NULL) { |
7105 | 0 | PR_Free(ss->ssl3.hs.clientAuthSignatureSchemes); |
7106 | 0 | ss->ssl3.hs.clientAuthSignatureSchemes = NULL; |
7107 | 0 | ss->ssl3.hs.clientAuthSignatureSchemesLen = 0; |
7108 | 0 | } |
7109 | | |
7110 | | /* Note that if the server selects TLS 1.3, this will set the version to TLS |
7111 | | * 1.2. We will amend that once all other fields have been read. */ |
7112 | 40.2k | rv = ssl_ClientReadVersion(ss, &b, &length, &ss->version); |
7113 | 40.2k | if (rv != SECSuccess) { |
7114 | 19 | goto loser; /* alert has been sent */ |
7115 | 19 | } |
7116 | | |
7117 | 40.2k | rv = ssl3_ConsumeHandshake( |
7118 | 40.2k | ss, ss->ssl3.hs.server_random, SSL3_RANDOM_LENGTH, &b, &length); |
7119 | 40.2k | if (rv != SECSuccess) { |
7120 | 21 | goto loser; /* alert has been sent */ |
7121 | 21 | } |
7122 | 40.2k | isHelloRetry = !PORT_Memcmp(ss->ssl3.hs.server_random, |
7123 | 40.2k | ssl_hello_retry_random, SSL3_RANDOM_LENGTH); |
7124 | | |
7125 | 40.2k | rv = ssl3_ConsumeHandshakeVariable(ss, &sidBytes, 1, &b, &length); |
7126 | 40.2k | if (rv != SECSuccess) { |
7127 | 129 | goto loser; /* alert has been sent */ |
7128 | 129 | } |
7129 | 40.0k | if (sidBytes.len > SSL3_SESSIONID_BYTES) { |
7130 | 28 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_0) |
7131 | 2 | desc = decode_error; |
7132 | 28 | goto alert_loser; /* malformed. */ |
7133 | 28 | } |
7134 | | |
7135 | | /* Read the cipher suite. */ |
7136 | 40.0k | rv = ssl3_ConsumeHandshakeNumber(ss, &cipher, 2, &b, &length); |
7137 | 40.0k | if (rv != SECSuccess) { |
7138 | 8 | goto loser; /* alert has been sent */ |
7139 | 8 | } |
7140 | | |
7141 | | /* Compression method. */ |
7142 | 40.0k | rv = ssl3_ConsumeHandshakeNumber(ss, &compression, 1, &b, &length); |
7143 | 40.0k | if (rv != SECSuccess) { |
7144 | 7 | goto loser; /* alert has been sent */ |
7145 | 7 | } |
7146 | 40.0k | if (compression != ssl_compression_null) { |
7147 | 13 | desc = illegal_parameter; |
7148 | 13 | errCode = SSL_ERROR_RX_MALFORMED_SERVER_HELLO; |
7149 | 13 | goto alert_loser; |
7150 | 13 | } |
7151 | | |
7152 | | /* Parse extensions. */ |
7153 | 40.0k | if (length != 0) { |
7154 | 9.19k | PRUint32 extensionLength; |
7155 | 9.19k | rv = ssl3_ConsumeHandshakeNumber(ss, &extensionLength, 2, &b, &length); |
7156 | 9.19k | if (rv != SECSuccess) { |
7157 | 2 | goto loser; /* alert already sent */ |
7158 | 2 | } |
7159 | 9.19k | if (extensionLength != length) { |
7160 | 35 | desc = decode_error; |
7161 | 35 | goto alert_loser; |
7162 | 35 | } |
7163 | 9.16k | rv = ssl3_ParseExtensions(ss, &b, &length); |
7164 | 9.16k | if (rv != SECSuccess) { |
7165 | 47 | goto alert_loser; /* malformed */ |
7166 | 47 | } |
7167 | 9.16k | } |
7168 | | |
7169 | | /* Read supported_versions if present. */ |
7170 | 39.9k | rv = tls13_ClientReadSupportedVersion(ss); |
7171 | 39.9k | if (rv != SECSuccess) { |
7172 | 19 | goto loser; |
7173 | 19 | } |
7174 | | |
7175 | | /* RFC 9147. 5.2. |
7176 | | * DTLS Handshake Message Format states the difference between the computation |
7177 | | * of the transcript if the version is DTLS1.2 or DTLS1.3. |
7178 | | * |
7179 | | * At this moment we are sure which version |
7180 | | * we are planning to use during the connection, so we can compute the hash. */ |
7181 | 39.9k | rv = ssl3_MaybeUpdateHashWithSavedRecord(ss); |
7182 | 39.9k | if (rv != SECSuccess) { |
7183 | 0 | goto loser; |
7184 | 0 | } |
7185 | | |
7186 | 39.9k | PORT_Assert(!SSL_ALL_VERSIONS_DISABLED(&ss->vrange)); |
7187 | | /* Check that the version is within the configured range. */ |
7188 | 39.9k | if (ss->vrange.min > ss->version || ss->vrange.max < ss->version) { |
7189 | 22 | desc = (ss->version > SSL_LIBRARY_VERSION_3_0) |
7190 | 22 | ? protocol_version |
7191 | 22 | : handshake_failure; |
7192 | 22 | errCode = SSL_ERROR_UNSUPPORTED_VERSION; |
7193 | 22 | goto alert_loser; |
7194 | 22 | } |
7195 | | |
7196 | 39.9k | if (isHelloRetry && ss->ssl3.hs.helloRetry) { |
7197 | 2 | SSL_TRC(3, ("%d: SSL3[%d]: received a second hello_retry_request", |
7198 | 2 | SSL_GETPID(), ss->fd)); |
7199 | 2 | desc = unexpected_message; |
7200 | 2 | errCode = SSL_ERROR_RX_UNEXPECTED_HELLO_RETRY_REQUEST; |
7201 | 2 | goto alert_loser; |
7202 | 2 | } |
7203 | | |
7204 | | /* There are three situations in which the server must pick |
7205 | | * TLS 1.3. |
7206 | | * |
7207 | | * 1. We received HRR |
7208 | | * 2. We sent early app data |
7209 | | * 3. ECH was accepted (checked in MaybeHandleEchSignal) |
7210 | | * |
7211 | | * If we offered ECH and the server negotiated a lower version, |
7212 | | * authenticate to the public name for secure disablement. |
7213 | | * |
7214 | | */ |
7215 | 39.9k | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
7216 | 38.8k | if (isHelloRetry || ss->ssl3.hs.helloRetry) { |
7217 | | /* SSL3_SendAlert() will uncache the SID. */ |
7218 | 5 | desc = illegal_parameter; |
7219 | 5 | errCode = SSL_ERROR_RX_MALFORMED_SERVER_HELLO; |
7220 | 5 | goto alert_loser; |
7221 | 5 | } |
7222 | 38.8k | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_sent) { |
7223 | | /* SSL3_SendAlert() will uncache the SID. */ |
7224 | 0 | desc = illegal_parameter; |
7225 | 0 | errCode = SSL_ERROR_DOWNGRADE_WITH_EARLY_DATA; |
7226 | 0 | goto alert_loser; |
7227 | 0 | } |
7228 | 38.8k | } |
7229 | | |
7230 | | /* Check that the server negotiated the same version as it did |
7231 | | * in the first handshake. This isn't really the best place for |
7232 | | * us to be getting this version number, but it's what we have. |
7233 | | * (1294697). */ |
7234 | 39.8k | if (ss->firstHsDone && (ss->version != ss->ssl3.crSpec->version)) { |
7235 | 1 | desc = protocol_version; |
7236 | 1 | errCode = SSL_ERROR_UNSUPPORTED_VERSION; |
7237 | 1 | goto alert_loser; |
7238 | 1 | } |
7239 | | |
7240 | 39.8k | if (ss->opt.enableHelloDowngradeCheck) { |
7241 | 39.8k | rv = ssl_CheckServerRandom(ss); |
7242 | 39.8k | if (rv != SECSuccess) { |
7243 | 2 | desc = illegal_parameter; |
7244 | 2 | errCode = SSL_ERROR_RX_MALFORMED_SERVER_HELLO; |
7245 | 2 | goto alert_loser; |
7246 | 2 | } |
7247 | 39.8k | } |
7248 | | |
7249 | | /* Finally, now all the version-related checks have passed. */ |
7250 | 39.8k | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_version; |
7251 | | /* Update the write cipher spec to match the version. But not after |
7252 | | * HelloRetryRequest, because cwSpec might be a 0-RTT cipher spec, |
7253 | | * in which case this is a no-op. */ |
7254 | 39.8k | if (!ss->firstHsDone && !isHelloRetry) { |
7255 | 8.79k | ssl_GetSpecWriteLock(ss); |
7256 | 8.79k | ssl_SetSpecVersions(ss, ss->ssl3.cwSpec); |
7257 | 8.79k | ssl_ReleaseSpecWriteLock(ss); |
7258 | 8.79k | } |
7259 | | |
7260 | | /* Check that the session ID is as expected. */ |
7261 | 39.8k | if (!ssl_CheckServerSessionIdCorrectness(ss, &sidBytes)) { |
7262 | 6 | desc = illegal_parameter; |
7263 | 6 | errCode = SSL_ERROR_RX_MALFORMED_SERVER_HELLO; |
7264 | 6 | goto alert_loser; |
7265 | 6 | } |
7266 | | |
7267 | | /* Only initialize hashes if this isn't a Hello Retry. */ |
7268 | 39.8k | rv = ssl_ClientSetCipherSuite(ss, ss->version, cipher, |
7269 | 39.8k | !isHelloRetry); |
7270 | 39.8k | if (rv != SECSuccess) { |
7271 | 47 | desc = illegal_parameter; |
7272 | 47 | errCode = PORT_GetError(); |
7273 | 47 | goto alert_loser; |
7274 | 47 | } |
7275 | | |
7276 | 39.8k | dtls_ReceivedFirstMessageInFlight(ss); |
7277 | | |
7278 | 39.8k | if (isHelloRetry) { |
7279 | 344 | rv = tls13_HandleHelloRetryRequest(ss, savedMsg, savedLength); |
7280 | 344 | if (rv != SECSuccess) { |
7281 | 22 | goto loser; |
7282 | 22 | } |
7283 | 322 | return SECSuccess; |
7284 | 344 | } |
7285 | | |
7286 | 39.4k | rv = ssl3_HandleParsedExtensions(ss, ssl_hs_server_hello); |
7287 | 39.4k | ssl3_DestroyRemoteExtensions(&ss->ssl3.hs.remoteExtensions); |
7288 | 39.4k | if (rv != SECSuccess) { |
7289 | 125 | goto alert_loser; |
7290 | 125 | } |
7291 | | |
7292 | 39.3k | rv = ssl_HashHandshakeMessage(ss, ssl_hs_server_hello, |
7293 | 39.3k | savedMsg, savedLength); |
7294 | 39.3k | if (rv != SECSuccess) { |
7295 | 0 | goto loser; |
7296 | 0 | } |
7297 | | |
7298 | 39.3k | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
7299 | 600 | rv = tls13_HandleServerHelloPart2(ss, savedMsg, savedLength); |
7300 | 600 | if (rv != SECSuccess) { |
7301 | 114 | errCode = PORT_GetError(); |
7302 | 114 | goto loser; |
7303 | 114 | } |
7304 | 38.7k | } else { |
7305 | 38.7k | rv = ssl3_HandleServerHelloPart2(ss, &sidBytes, &errCode); |
7306 | 38.7k | if (rv != SECSuccess) |
7307 | 44 | goto loser; |
7308 | 38.7k | } |
7309 | | |
7310 | 39.2k | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_ech; |
7311 | 39.2k | return SECSuccess; |
7312 | | |
7313 | 402 | alert_loser: |
7314 | 402 | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
7315 | | |
7316 | 787 | loser: |
7317 | | /* Clean up the temporary pointer to the handshake buffer. */ |
7318 | 787 | ss->xtnData.signedCertTimestamps.len = 0; |
7319 | 787 | ssl_MapLowLevelError(errCode); |
7320 | 787 | return SECFailure; |
7321 | 402 | } |
7322 | | |
7323 | | static SECStatus |
7324 | | ssl3_UnwrapMasterSecretClient(sslSocket *ss, sslSessionID *sid, PK11SymKey **ms) |
7325 | 0 | { |
7326 | 0 | PK11SlotInfo *slot; |
7327 | 0 | PK11SymKey *wrapKey; |
7328 | 0 | CK_FLAGS keyFlags = 0; |
7329 | 0 | SECItem wrappedMS = { |
7330 | 0 | siBuffer, |
7331 | 0 | sid->u.ssl3.keys.wrapped_master_secret, |
7332 | 0 | sid->u.ssl3.keys.wrapped_master_secret_len |
7333 | 0 | }; |
7334 | | |
7335 | | /* unwrap master secret */ |
7336 | 0 | slot = SECMOD_LookupSlot(sid->u.ssl3.masterModuleID, |
7337 | 0 | sid->u.ssl3.masterSlotID); |
7338 | 0 | if (slot == NULL) { |
7339 | 0 | return SECFailure; |
7340 | 0 | } |
7341 | 0 | if (!PK11_IsPresent(slot)) { |
7342 | 0 | PK11_FreeSlot(slot); |
7343 | 0 | return SECFailure; |
7344 | 0 | } |
7345 | 0 | wrapKey = PK11_GetWrapKey(slot, sid->u.ssl3.masterWrapIndex, |
7346 | 0 | sid->u.ssl3.masterWrapMech, |
7347 | 0 | sid->u.ssl3.masterWrapSeries, |
7348 | 0 | ss->pkcs11PinArg); |
7349 | 0 | PK11_FreeSlot(slot); |
7350 | 0 | if (wrapKey == NULL) { |
7351 | 0 | return SECFailure; |
7352 | 0 | } |
7353 | | |
7354 | 0 | if (ss->version > SSL_LIBRARY_VERSION_3_0) { /* isTLS */ |
7355 | 0 | keyFlags = CKF_SIGN | CKF_VERIFY; |
7356 | 0 | } |
7357 | |
|
7358 | 0 | *ms = PK11_UnwrapSymKeyWithFlags(wrapKey, sid->u.ssl3.masterWrapMech, |
7359 | 0 | NULL, &wrappedMS, CKM_SSL3_MASTER_KEY_DERIVE, |
7360 | 0 | CKA_DERIVE, SSL3_MASTER_SECRET_LENGTH, keyFlags); |
7361 | 0 | PK11_FreeSymKey(wrapKey); |
7362 | 0 | if (!*ms) { |
7363 | 0 | return SECFailure; |
7364 | 0 | } |
7365 | 0 | return SECSuccess; |
7366 | 0 | } |
7367 | | |
7368 | | static SECStatus |
7369 | | ssl3_HandleServerHelloPart2(sslSocket *ss, const SECItem *sidBytes, |
7370 | | int *retErrCode) |
7371 | 38.7k | { |
7372 | 38.7k | SSL3AlertDescription desc = handshake_failure; |
7373 | 38.7k | int errCode = SSL_ERROR_RX_MALFORMED_SERVER_HELLO; |
7374 | 38.7k | SECStatus rv; |
7375 | 38.7k | PRBool sid_match; |
7376 | 38.7k | sslSessionID *sid = ss->sec.ci.sid; |
7377 | | |
7378 | 38.7k | if ((ss->opt.requireSafeNegotiation || |
7379 | 38.7k | (ss->firstHsDone && (ss->peerRequestedProtection || |
7380 | 30.7k | ss->opt.enableRenegotiation == |
7381 | 30.7k | SSL_RENEGOTIATE_REQUIRES_XTN))) && |
7382 | 38.7k | !ssl3_ExtensionNegotiated(ss, ssl_renegotiation_info_xtn)) { |
7383 | 44 | desc = handshake_failure; |
7384 | 44 | errCode = ss->firstHsDone ? SSL_ERROR_RENEGOTIATION_NOT_ALLOWED |
7385 | 44 | : SSL_ERROR_UNSAFE_NEGOTIATION; |
7386 | 44 | goto alert_loser; |
7387 | 44 | } |
7388 | | |
7389 | | /* Any errors after this point are not "malformed" errors. */ |
7390 | 38.7k | desc = handshake_failure; |
7391 | | |
7392 | | /* we need to call ssl3_SetupPendingCipherSpec here so we can check the |
7393 | | * key exchange algorithm. */ |
7394 | 38.7k | rv = ssl3_SetupBothPendingCipherSpecs(ss); |
7395 | 38.7k | if (rv != SECSuccess) { |
7396 | 0 | goto alert_loser; /* error code is set. */ |
7397 | 0 | } |
7398 | | |
7399 | | /* We may or may not have sent a session id, we may get one back or |
7400 | | * not and if so it may match the one we sent. |
7401 | | * Attempt to restore the master secret to see if this is so... |
7402 | | * Don't consider failure to find a matching SID an error. |
7403 | | */ |
7404 | 38.7k | sid_match = (PRBool)(sidBytes->len > 0 && |
7405 | 38.7k | sidBytes->len == |
7406 | 6.96k | sid->u.ssl3.sessionIDLength && |
7407 | 38.7k | !PORT_Memcmp(sid->u.ssl3.sessionID, |
7408 | 0 | sidBytes->data, sidBytes->len)); |
7409 | | |
7410 | 38.7k | if (sid_match) { |
7411 | 0 | if (sid->version != ss->version || |
7412 | 0 | sid->u.ssl3.cipherSuite != ss->ssl3.hs.cipher_suite) { |
7413 | 0 | errCode = SSL_ERROR_RX_MALFORMED_SERVER_HELLO; |
7414 | 0 | goto alert_loser; |
7415 | 0 | } |
7416 | 0 | do { |
7417 | 0 | PK11SymKey *masterSecret; |
7418 | | |
7419 | | /* [draft-ietf-tls-session-hash-06; Section 5.3] |
7420 | | * |
7421 | | * o If the original session did not use the "extended_master_secret" |
7422 | | * extension but the new ServerHello contains the extension, the |
7423 | | * client MUST abort the handshake. |
7424 | | */ |
7425 | 0 | if (!sid->u.ssl3.keys.extendedMasterSecretUsed && |
7426 | 0 | ssl3_ExtensionNegotiated(ss, ssl_extended_master_secret_xtn)) { |
7427 | 0 | errCode = SSL_ERROR_UNEXPECTED_EXTENDED_MASTER_SECRET; |
7428 | 0 | goto alert_loser; |
7429 | 0 | } |
7430 | | |
7431 | | /* |
7432 | | * o If the original session used an extended master secret but the new |
7433 | | * ServerHello does not contain the "extended_master_secret" |
7434 | | * extension, the client SHOULD abort the handshake. |
7435 | | * |
7436 | | * TODO(ekr@rtfm.com): Add option to refuse to resume when EMS is not |
7437 | | * used at all (bug 1176526). |
7438 | | */ |
7439 | 0 | if (sid->u.ssl3.keys.extendedMasterSecretUsed && |
7440 | 0 | !ssl3_ExtensionNegotiated(ss, ssl_extended_master_secret_xtn)) { |
7441 | 0 | errCode = SSL_ERROR_MISSING_EXTENDED_MASTER_SECRET; |
7442 | 0 | goto alert_loser; |
7443 | 0 | } |
7444 | | |
7445 | 0 | ss->sec.authType = sid->authType; |
7446 | 0 | ss->sec.authKeyBits = sid->authKeyBits; |
7447 | 0 | ss->sec.keaType = sid->keaType; |
7448 | 0 | ss->sec.keaKeyBits = sid->keaKeyBits; |
7449 | 0 | ss->sec.originalKeaGroup = ssl_LookupNamedGroup(sid->keaGroup); |
7450 | 0 | ss->sec.signatureScheme = sid->sigScheme; |
7451 | |
|
7452 | 0 | rv = ssl3_UnwrapMasterSecretClient(ss, sid, &masterSecret); |
7453 | 0 | if (rv != SECSuccess) { |
7454 | 0 | break; /* not considered an error */ |
7455 | 0 | } |
7456 | | |
7457 | | /* Got a Match */ |
7458 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hsh_sid_cache_hits); |
7459 | | |
7460 | | /* If we sent a session ticket, then this is a stateless resume. */ |
7461 | 0 | if (ss->xtnData.sentSessionTicketInClientHello) |
7462 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hsh_sid_stateless_resumes); |
7463 | |
|
7464 | 0 | if (ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn)) |
7465 | 0 | ss->ssl3.hs.ws = wait_new_session_ticket; |
7466 | 0 | else |
7467 | 0 | ss->ssl3.hs.ws = wait_change_cipher; |
7468 | |
|
7469 | 0 | ss->ssl3.hs.isResuming = PR_TRUE; |
7470 | | |
7471 | | /* copy the peer cert from the SID */ |
7472 | 0 | if (sid->peerCert != NULL) { |
7473 | 0 | ss->sec.peerCert = CERT_DupCertificate(sid->peerCert); |
7474 | 0 | } |
7475 | | |
7476 | | /* We are re-using the old MS, so no need to derive again. */ |
7477 | 0 | rv = ssl3_InitPendingCipherSpecs(ss, masterSecret, PR_FALSE); |
7478 | 0 | if (rv != SECSuccess) { |
7479 | 0 | goto alert_loser; /* err code was set */ |
7480 | 0 | } |
7481 | 0 | return SECSuccess; |
7482 | 0 | } while (0); |
7483 | 0 | } |
7484 | | |
7485 | 38.7k | if (sid_match) |
7486 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hsh_sid_cache_not_ok); |
7487 | 38.7k | else |
7488 | 38.7k | SSL_AtomicIncrementLong(&ssl3stats.hsh_sid_cache_misses); |
7489 | | |
7490 | | /* We tried to resume a 1.3 session but the server negotiated 1.2. */ |
7491 | 38.7k | if (ss->statelessResume) { |
7492 | 0 | PORT_Assert(sid->version == SSL_LIBRARY_VERSION_TLS_1_3); |
7493 | 0 | PORT_Assert(ss->ssl3.hs.currentSecret); |
7494 | | |
7495 | | /* Reset resumption state, only used by 1.3 code. */ |
7496 | 0 | ss->statelessResume = PR_FALSE; |
7497 | | |
7498 | | /* Clear TLS 1.3 early data traffic key. */ |
7499 | 0 | PK11_FreeSymKey(ss->ssl3.hs.currentSecret); |
7500 | 0 | ss->ssl3.hs.currentSecret = NULL; |
7501 | 0 | } |
7502 | | |
7503 | | /* throw the old one away */ |
7504 | 38.7k | sid->u.ssl3.keys.resumable = PR_FALSE; |
7505 | 38.7k | ssl_UncacheSessionID(ss); |
7506 | 38.7k | ssl_FreeSID(sid); |
7507 | | |
7508 | | /* get a new sid */ |
7509 | 38.7k | ss->sec.ci.sid = sid = ssl3_NewSessionID(ss, PR_FALSE); |
7510 | 38.7k | if (sid == NULL) { |
7511 | 0 | goto alert_loser; /* memory error is set. */ |
7512 | 0 | } |
7513 | | |
7514 | 38.7k | sid->version = ss->version; |
7515 | 38.7k | sid->u.ssl3.sessionIDLength = sidBytes->len; |
7516 | 38.7k | if (sidBytes->len > 0) { |
7517 | 6.96k | PORT_Memcpy(sid->u.ssl3.sessionID, sidBytes->data, sidBytes->len); |
7518 | 6.96k | } |
7519 | | |
7520 | 38.7k | sid->u.ssl3.keys.extendedMasterSecretUsed = |
7521 | 38.7k | ssl3_ExtensionNegotiated(ss, ssl_extended_master_secret_xtn); |
7522 | | |
7523 | | /* Copy Signed Certificate Timestamps, if any. */ |
7524 | 38.7k | if (ss->xtnData.signedCertTimestamps.len) { |
7525 | 0 | rv = SECITEM_CopyItem(NULL, &sid->u.ssl3.signedCertTimestamps, |
7526 | 0 | &ss->xtnData.signedCertTimestamps); |
7527 | 0 | ss->xtnData.signedCertTimestamps.len = 0; |
7528 | 0 | if (rv != SECSuccess) |
7529 | 0 | goto loser; |
7530 | 0 | } |
7531 | | |
7532 | 38.7k | ss->ssl3.hs.isResuming = PR_FALSE; |
7533 | 38.7k | if (ss->ssl3.hs.kea_def->authKeyType != ssl_auth_null) { |
7534 | | /* All current cipher suites other than those with ssl_auth_null (i.e., |
7535 | | * (EC)DH_anon_* suites) require a certificate, so use that signal. */ |
7536 | 38.7k | ss->ssl3.hs.ws = wait_server_cert; |
7537 | 38.7k | } else { |
7538 | | /* All the remaining cipher suites must be (EC)DH_anon_* and so |
7539 | | * must be ephemeral. Note, if we ever add PSK this might |
7540 | | * change. */ |
7541 | 0 | PORT_Assert(ss->ssl3.hs.kea_def->ephemeral); |
7542 | 0 | ss->ssl3.hs.ws = wait_server_key; |
7543 | 0 | } |
7544 | 38.7k | return SECSuccess; |
7545 | | |
7546 | 44 | alert_loser: |
7547 | 44 | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
7548 | | |
7549 | 44 | loser: |
7550 | 44 | *retErrCode = errCode; |
7551 | 44 | return SECFailure; |
7552 | 44 | } |
7553 | | |
7554 | | static SECStatus |
7555 | | ssl_HandleDHServerKeyExchange(sslSocket *ss, PRUint8 *b, PRUint32 length) |
7556 | 2.22k | { |
7557 | 2.22k | SECStatus rv; |
7558 | 2.22k | int errCode = SSL_ERROR_RX_MALFORMED_SERVER_KEY_EXCH; |
7559 | 2.22k | SSL3AlertDescription desc = illegal_parameter; |
7560 | 2.22k | SSLHashType hashAlg; |
7561 | 2.22k | PRBool isTLS = ss->ssl3.prSpec->version > SSL_LIBRARY_VERSION_3_0; |
7562 | 2.22k | SSLSignatureScheme sigScheme; |
7563 | | |
7564 | 2.22k | SECItem dh_p = { siBuffer, NULL, 0 }; |
7565 | 2.22k | SECItem dh_g = { siBuffer, NULL, 0 }; |
7566 | 2.22k | SECItem dh_Ys = { siBuffer, NULL, 0 }; |
7567 | 2.22k | unsigned dh_p_bits; |
7568 | 2.22k | unsigned dh_g_bits; |
7569 | 2.22k | PRInt32 minDH = 0; |
7570 | 2.22k | PRInt32 optval; |
7571 | | |
7572 | 2.22k | SSL3Hashes hashes; |
7573 | 2.22k | SECItem signature = { siBuffer, NULL, 0 }; |
7574 | 2.22k | PLArenaPool *arena = NULL; |
7575 | 2.22k | SECKEYPublicKey *peerKey = NULL; |
7576 | | |
7577 | 2.22k | rv = ssl3_ConsumeHandshakeVariable(ss, &dh_p, 2, &b, &length); |
7578 | 2.22k | if (rv != SECSuccess) { |
7579 | 16 | goto loser; /* malformed. */ |
7580 | 16 | } |
7581 | 2.20k | rv = NSS_OptionGet(NSS_KEY_SIZE_POLICY_FLAGS, &optval); |
7582 | 2.20k | if ((rv == SECSuccess) && (optval & NSS_KEY_SIZE_POLICY_SSL_FLAG)) { |
7583 | 2.20k | (void)NSS_OptionGet(NSS_DH_MIN_KEY_SIZE, &minDH); |
7584 | 2.20k | } |
7585 | | |
7586 | 2.20k | if (minDH <= 0) { |
7587 | 0 | minDH = SSL_DH_MIN_P_BITS; |
7588 | 0 | } |
7589 | 2.20k | dh_p_bits = SECKEY_BigIntegerBitLength(&dh_p); |
7590 | 2.20k | if (dh_p_bits < (unsigned)minDH) { |
7591 | 9 | errCode = SSL_ERROR_WEAK_SERVER_EPHEMERAL_DH_KEY; |
7592 | 9 | goto alert_loser; |
7593 | 9 | } |
7594 | 2.19k | if (dh_p_bits > SSL_MAX_DH_KEY_BITS) { |
7595 | 14 | errCode = SSL_ERROR_DH_KEY_TOO_LONG; |
7596 | 14 | goto alert_loser; |
7597 | 14 | } |
7598 | 2.18k | rv = ssl3_ConsumeHandshakeVariable(ss, &dh_g, 2, &b, &length); |
7599 | 2.18k | if (rv != SECSuccess) { |
7600 | 21 | goto loser; /* malformed. */ |
7601 | 21 | } |
7602 | | /* Abort if dh_g is 0, 1, or obviously too big. */ |
7603 | 2.16k | dh_g_bits = SECKEY_BigIntegerBitLength(&dh_g); |
7604 | 2.16k | if (dh_g_bits > dh_p_bits || dh_g_bits <= 1) { |
7605 | 27 | goto alert_loser; |
7606 | 27 | } |
7607 | 2.13k | if (ss->opt.requireDHENamedGroups) { |
7608 | | /* If we're doing named groups, make sure it's good. */ |
7609 | 68 | rv = ssl_ValidateDHENamedGroup(ss, &dh_p, &dh_g, NULL, NULL); |
7610 | 68 | if (rv != SECSuccess) { |
7611 | 7 | errCode = SSL_ERROR_WEAK_SERVER_EPHEMERAL_DH_KEY; |
7612 | 7 | goto alert_loser; |
7613 | 7 | } |
7614 | 68 | } |
7615 | | |
7616 | 2.12k | rv = ssl3_ConsumeHandshakeVariable(ss, &dh_Ys, 2, &b, &length); |
7617 | 2.12k | if (rv != SECSuccess) { |
7618 | 4 | goto loser; /* malformed. */ |
7619 | 4 | } |
7620 | 2.12k | if (!ssl_IsValidDHEShare(&dh_p, &dh_Ys)) { |
7621 | 15 | errCode = SSL_ERROR_RX_MALFORMED_DHE_KEY_SHARE; |
7622 | 15 | goto alert_loser; |
7623 | 15 | } |
7624 | | |
7625 | 2.10k | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_2) { |
7626 | 959 | rv = ssl_ConsumeSignatureScheme(ss, &b, &length, &sigScheme); |
7627 | 959 | if (rv != SECSuccess) { |
7628 | 2 | goto loser; /* alert already sent */ |
7629 | 2 | } |
7630 | 957 | rv = ssl_CheckSignatureSchemeConsistency( |
7631 | 957 | ss, sigScheme, &ss->sec.peerCert->subjectPublicKeyInfo); |
7632 | 957 | if (rv != SECSuccess) { |
7633 | 4 | goto alert_loser; |
7634 | 4 | } |
7635 | 953 | hashAlg = ssl_SignatureSchemeToHashType(sigScheme); |
7636 | 1.14k | } else { |
7637 | | /* Use ssl_hash_none to represent the MD5+SHA1 combo. */ |
7638 | 1.14k | hashAlg = ssl_hash_none; |
7639 | 1.14k | sigScheme = ssl_sig_none; |
7640 | 1.14k | } |
7641 | 2.10k | rv = ssl3_ConsumeHandshakeVariable(ss, &signature, 2, &b, &length); |
7642 | 2.10k | if (rv != SECSuccess) { |
7643 | 7 | goto loser; /* malformed. */ |
7644 | 7 | } |
7645 | 2.09k | if (length != 0) { |
7646 | 5 | if (isTLS) { |
7647 | 5 | desc = decode_error; |
7648 | 5 | } |
7649 | 5 | goto alert_loser; /* malformed. */ |
7650 | 5 | } |
7651 | | |
7652 | 2.08k | PRINT_BUF(60, (NULL, "Server DH p", dh_p.data, dh_p.len)); |
7653 | 2.08k | PRINT_BUF(60, (NULL, "Server DH g", dh_g.data, dh_g.len)); |
7654 | 2.08k | PRINT_BUF(60, (NULL, "Server DH Ys", dh_Ys.data, dh_Ys.len)); |
7655 | | |
7656 | | /* failures after this point are not malformed handshakes. */ |
7657 | | /* TLS: send decrypt_error if signature failed. */ |
7658 | 2.08k | desc = isTLS ? decrypt_error : handshake_failure; |
7659 | | |
7660 | | /* |
7661 | | * Check to make sure the hash is signed by right guy. |
7662 | | */ |
7663 | 2.08k | rv = ssl3_ComputeDHKeyHash(ss, hashAlg, &hashes, |
7664 | 2.08k | dh_p, dh_g, dh_Ys, PR_FALSE /* padY */); |
7665 | 2.08k | if (rv != SECSuccess) { |
7666 | 0 | errCode = |
7667 | 0 | ssl_MapLowLevelError(SSL_ERROR_SERVER_KEY_EXCHANGE_FAILURE); |
7668 | 0 | goto alert_loser; |
7669 | 0 | } |
7670 | 2.08k | rv = ssl3_VerifySignedHashes(ss, sigScheme, &hashes, &signature); |
7671 | 2.08k | if (rv != SECSuccess) { |
7672 | 0 | errCode = |
7673 | 0 | ssl_MapLowLevelError(SSL_ERROR_SERVER_KEY_EXCHANGE_FAILURE); |
7674 | 0 | goto alert_loser; |
7675 | 0 | } |
7676 | | |
7677 | | /* |
7678 | | * we really need to build a new key here because we can no longer |
7679 | | * ignore calling SECKEY_DestroyPublicKey. Using the key may allocate |
7680 | | * pkcs11 slots and ID's. |
7681 | | */ |
7682 | 2.08k | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
7683 | 2.08k | if (arena == NULL) { |
7684 | 0 | errCode = SEC_ERROR_NO_MEMORY; |
7685 | 0 | goto loser; |
7686 | 0 | } |
7687 | | |
7688 | 2.08k | peerKey = PORT_ArenaZNew(arena, SECKEYPublicKey); |
7689 | 2.08k | if (peerKey == NULL) { |
7690 | 0 | errCode = SEC_ERROR_NO_MEMORY; |
7691 | 0 | goto loser; |
7692 | 0 | } |
7693 | | |
7694 | 2.08k | peerKey->arena = arena; |
7695 | 2.08k | peerKey->keyType = dhKey; |
7696 | 2.08k | peerKey->pkcs11Slot = NULL; |
7697 | 2.08k | peerKey->pkcs11ID = CK_INVALID_HANDLE; |
7698 | | |
7699 | 2.08k | if (SECITEM_CopyItem(arena, &peerKey->u.dh.prime, &dh_p) || |
7700 | 2.08k | SECITEM_CopyItem(arena, &peerKey->u.dh.base, &dh_g) || |
7701 | 2.08k | SECITEM_CopyItem(arena, &peerKey->u.dh.publicValue, &dh_Ys)) { |
7702 | 0 | errCode = SEC_ERROR_NO_MEMORY; |
7703 | 0 | goto loser; |
7704 | 0 | } |
7705 | 2.08k | ss->sec.peerKey = peerKey; |
7706 | 2.08k | return SECSuccess; |
7707 | | |
7708 | 81 | alert_loser: |
7709 | 81 | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
7710 | 131 | loser: |
7711 | 131 | if (arena) { |
7712 | 0 | PORT_FreeArena(arena, PR_FALSE); |
7713 | 0 | } |
7714 | 131 | PORT_SetError(ssl_MapLowLevelError(errCode)); |
7715 | 131 | return SECFailure; |
7716 | 81 | } |
7717 | | |
7718 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered a |
7719 | | * complete ssl3 ServerKeyExchange message. |
7720 | | * Caller must hold Handshake and RecvBuf locks. |
7721 | | */ |
7722 | | static SECStatus |
7723 | | ssl3_HandleServerKeyExchange(sslSocket *ss, PRUint8 *b, PRUint32 length) |
7724 | 8.78k | { |
7725 | 8.78k | SECStatus rv; |
7726 | | |
7727 | 8.78k | SSL_TRC(3, ("%d: SSL3[%d]: handle server_key_exchange handshake", |
7728 | 8.78k | SSL_GETPID(), ss->fd)); |
7729 | 8.78k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
7730 | 8.78k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
7731 | | |
7732 | 8.78k | if (ss->ssl3.hs.ws != wait_server_key) { |
7733 | 10 | SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
7734 | 10 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_SERVER_KEY_EXCH); |
7735 | 10 | return SECFailure; |
7736 | 10 | } |
7737 | | |
7738 | 8.77k | switch (ss->ssl3.hs.kea_def->exchKeyType) { |
7739 | 2.22k | case ssl_kea_dh: |
7740 | 2.22k | rv = ssl_HandleDHServerKeyExchange(ss, b, length); |
7741 | 2.22k | break; |
7742 | | |
7743 | 6.55k | case ssl_kea_ecdh: |
7744 | 6.55k | rv = ssl3_HandleECDHServerKeyExchange(ss, b, length); |
7745 | 6.55k | break; |
7746 | | |
7747 | 0 | default: |
7748 | 0 | SSL3_SendAlert(ss, alert_fatal, handshake_failure); |
7749 | 0 | PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); |
7750 | 0 | rv = SECFailure; |
7751 | 0 | break; |
7752 | 8.77k | } |
7753 | | |
7754 | 8.77k | if (rv == SECSuccess) { |
7755 | 8.60k | ss->ssl3.hs.ws = wait_cert_request; |
7756 | 8.60k | } |
7757 | | /* All Handle*ServerKeyExchange functions set the error code. */ |
7758 | 8.77k | return rv; |
7759 | 8.77k | } |
7760 | | |
7761 | | typedef struct dnameNode { |
7762 | | struct dnameNode *next; |
7763 | | SECItem name; |
7764 | | } dnameNode; |
7765 | | |
7766 | | /* |
7767 | | * Parse the ca_list structure in a CertificateRequest. |
7768 | | * |
7769 | | * Called from: |
7770 | | * ssl3_HandleCertificateRequest |
7771 | | * tls13_HandleCertificateRequest |
7772 | | */ |
7773 | | SECStatus |
7774 | | ssl3_ParseCertificateRequestCAs(sslSocket *ss, PRUint8 **b, PRUint32 *length, |
7775 | | CERTDistNames *ca_list) |
7776 | 173 | { |
7777 | 173 | PRUint32 remaining; |
7778 | 173 | int nnames = 0; |
7779 | 173 | dnameNode *node; |
7780 | 173 | SECStatus rv; |
7781 | 173 | int i; |
7782 | | |
7783 | 173 | rv = ssl3_ConsumeHandshakeNumber(ss, &remaining, 2, b, length); |
7784 | 173 | if (rv != SECSuccess) |
7785 | 37 | return SECFailure; /* malformed, alert has been sent */ |
7786 | | |
7787 | 136 | if (remaining > *length) |
7788 | 72 | goto alert_loser; |
7789 | | |
7790 | 64 | ca_list->head = node = PORT_ArenaZNew(ca_list->arena, dnameNode); |
7791 | 64 | if (node == NULL) |
7792 | 0 | goto no_mem; |
7793 | | |
7794 | 485 | while (remaining > 0) { |
7795 | 472 | PRUint32 len; |
7796 | | |
7797 | 472 | if (remaining < 2) |
7798 | 1 | goto alert_loser; /* malformed */ |
7799 | | |
7800 | 471 | rv = ssl3_ConsumeHandshakeNumber(ss, &len, 2, b, length); |
7801 | 471 | if (rv != SECSuccess) |
7802 | 0 | return SECFailure; /* malformed, alert has been sent */ |
7803 | 471 | if (len == 0 || remaining < len + 2) |
7804 | 40 | goto alert_loser; /* malformed */ |
7805 | | |
7806 | 431 | remaining -= 2; |
7807 | 431 | if (SECITEM_MakeItem(ca_list->arena, &node->name, *b, len) != SECSuccess) { |
7808 | 0 | goto no_mem; |
7809 | 0 | } |
7810 | 431 | node->name.len = len; |
7811 | 431 | *b += len; |
7812 | 431 | *length -= len; |
7813 | 431 | remaining -= len; |
7814 | 431 | nnames++; |
7815 | 431 | if (remaining <= 0) |
7816 | 10 | break; /* success */ |
7817 | | |
7818 | 421 | node->next = PORT_ArenaZNew(ca_list->arena, dnameNode); |
7819 | 421 | node = node->next; |
7820 | 421 | if (node == NULL) |
7821 | 0 | goto no_mem; |
7822 | 421 | } |
7823 | | |
7824 | 23 | ca_list->nnames = nnames; |
7825 | 23 | ca_list->names = PORT_ArenaNewArray(ca_list->arena, SECItem, nnames); |
7826 | 23 | if (nnames > 0 && ca_list->names == NULL) |
7827 | 0 | goto no_mem; |
7828 | | |
7829 | 23 | for (i = 0, node = (dnameNode *)ca_list->head; |
7830 | 328 | i < nnames; |
7831 | 305 | i++, node = node->next) { |
7832 | 305 | ca_list->names[i] = node->name; |
7833 | 305 | } |
7834 | | |
7835 | 23 | return SECSuccess; |
7836 | | |
7837 | 0 | no_mem: |
7838 | 0 | return SECFailure; |
7839 | | |
7840 | 113 | alert_loser: |
7841 | 113 | (void)SSL3_SendAlert(ss, alert_fatal, |
7842 | 113 | ss->version < SSL_LIBRARY_VERSION_TLS_1_0 ? illegal_parameter |
7843 | 113 | : decode_error); |
7844 | 113 | PORT_SetError(SSL_ERROR_RX_MALFORMED_CERT_REQUEST); |
7845 | 113 | return SECFailure; |
7846 | 23 | } |
7847 | | |
7848 | | SECStatus |
7849 | | ssl_ParseSignatureSchemes(const sslSocket *ss, PLArenaPool *arena, |
7850 | | SSLSignatureScheme **schemesOut, |
7851 | | unsigned int *numSchemesOut, |
7852 | | unsigned char **b, unsigned int *len) |
7853 | 158 | { |
7854 | 158 | SECStatus rv; |
7855 | 158 | SECItem buf; |
7856 | 158 | SSLSignatureScheme *schemes = NULL; |
7857 | 158 | unsigned int numSupported = 0; |
7858 | 158 | unsigned int numRemaining = 0; |
7859 | 158 | unsigned int max; |
7860 | | |
7861 | 158 | rv = ssl3_ExtConsumeHandshakeVariable(ss, &buf, 2, b, len); |
7862 | 158 | if (rv != SECSuccess) { |
7863 | 6 | return SECFailure; |
7864 | 6 | } |
7865 | | /* An odd-length value is invalid. */ |
7866 | 152 | if ((buf.len & 1) != 0) { |
7867 | 3 | ssl3_ExtSendAlert(ss, alert_fatal, decode_error); |
7868 | 3 | return SECFailure; |
7869 | 3 | } |
7870 | | |
7871 | | /* Let the caller decide whether to alert here. */ |
7872 | 149 | if (buf.len == 0) { |
7873 | 1 | goto done; |
7874 | 1 | } |
7875 | | |
7876 | | /* Limit the number of schemes we read. */ |
7877 | 148 | numRemaining = buf.len / 2; |
7878 | 148 | max = PR_MIN(numRemaining, MAX_SIGNATURE_SCHEMES); |
7879 | | |
7880 | 148 | if (arena) { |
7881 | 148 | schemes = PORT_ArenaZNewArray(arena, SSLSignatureScheme, max); |
7882 | 148 | } else { |
7883 | 0 | schemes = PORT_ZNewArray(SSLSignatureScheme, max); |
7884 | 0 | } |
7885 | 148 | if (!schemes) { |
7886 | 0 | ssl3_ExtSendAlert(ss, alert_fatal, internal_error); |
7887 | 0 | return SECFailure; |
7888 | 0 | } |
7889 | | |
7890 | 22.4k | for (; numRemaining && numSupported < MAX_SIGNATURE_SCHEMES; --numRemaining) { |
7891 | 22.2k | PRUint32 tmp; |
7892 | 22.2k | rv = ssl3_ExtConsumeHandshakeNumber(ss, &tmp, 2, &buf.data, &buf.len); |
7893 | 22.2k | if (rv != SECSuccess) { |
7894 | 0 | PORT_Assert(0); |
7895 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
7896 | 0 | return SECFailure; |
7897 | 0 | } |
7898 | 22.2k | if (ssl_SignatureSchemeValid((SSLSignatureScheme)tmp, SEC_OID_UNKNOWN, |
7899 | 22.2k | (PRBool)ss->version >= SSL_LIBRARY_VERSION_TLS_1_3)) { |
7900 | 981 | ; |
7901 | 981 | schemes[numSupported++] = (SSLSignatureScheme)tmp; |
7902 | 981 | } |
7903 | 22.2k | } |
7904 | | |
7905 | 148 | if (!numSupported) { |
7906 | 14 | if (!arena) { |
7907 | 0 | PORT_Free(schemes); |
7908 | 0 | } |
7909 | 14 | schemes = NULL; |
7910 | 14 | } |
7911 | | |
7912 | 149 | done: |
7913 | 149 | *schemesOut = schemes; |
7914 | 149 | *numSchemesOut = numSupported; |
7915 | 149 | return SECSuccess; |
7916 | 148 | } |
7917 | | |
7918 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered |
7919 | | * a complete ssl3 Certificate Request message. |
7920 | | * Caller must hold Handshake and RecvBuf locks. |
7921 | | */ |
7922 | | static SECStatus |
7923 | | ssl3_HandleCertificateRequest(sslSocket *ss, PRUint8 *b, PRUint32 length) |
7924 | 206 | { |
7925 | 206 | PLArenaPool *arena = NULL; |
7926 | 206 | PRBool isTLS = PR_FALSE; |
7927 | 206 | PRBool isTLS12 = PR_FALSE; |
7928 | 206 | int errCode = SSL_ERROR_RX_MALFORMED_CERT_REQUEST; |
7929 | 206 | SECStatus rv; |
7930 | 206 | SSL3AlertDescription desc = illegal_parameter; |
7931 | 206 | SECItem cert_types = { siBuffer, NULL, 0 }; |
7932 | 206 | SSLSignatureScheme *signatureSchemes = NULL; |
7933 | 206 | unsigned int signatureSchemeCount = 0; |
7934 | 206 | CERTDistNames ca_list; |
7935 | | |
7936 | 206 | SSL_TRC(3, ("%d: SSL3[%d]: handle certificate_request handshake", |
7937 | 206 | SSL_GETPID(), ss->fd)); |
7938 | 206 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
7939 | 206 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
7940 | | |
7941 | 206 | if (ss->ssl3.hs.ws != wait_cert_request) { |
7942 | 6 | desc = unexpected_message; |
7943 | 6 | errCode = SSL_ERROR_RX_UNEXPECTED_CERT_REQUEST; |
7944 | 6 | goto alert_loser; |
7945 | 6 | } |
7946 | | |
7947 | 200 | PORT_Assert(ss->ssl3.clientCertChain == NULL); |
7948 | 200 | PORT_Assert(ss->ssl3.clientCertificate == NULL); |
7949 | 200 | PORT_Assert(ss->ssl3.clientPrivateKey == NULL); |
7950 | | |
7951 | 200 | isTLS = (PRBool)(ss->ssl3.prSpec->version > SSL_LIBRARY_VERSION_3_0); |
7952 | 200 | isTLS12 = (PRBool)(ss->ssl3.prSpec->version >= SSL_LIBRARY_VERSION_TLS_1_2); |
7953 | 200 | rv = ssl3_ConsumeHandshakeVariable(ss, &cert_types, 1, &b, &length); |
7954 | 200 | if (rv != SECSuccess) |
7955 | 6 | goto loser; /* malformed, alert has been sent */ |
7956 | | |
7957 | 194 | arena = ca_list.arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
7958 | 194 | if (arena == NULL) |
7959 | 0 | goto no_mem; |
7960 | | |
7961 | 194 | if (isTLS12) { |
7962 | 155 | rv = ssl_ParseSignatureSchemes(ss, arena, |
7963 | 155 | &signatureSchemes, |
7964 | 155 | &signatureSchemeCount, |
7965 | 155 | &b, &length); |
7966 | 155 | if (rv != SECSuccess) { |
7967 | 6 | PORT_SetError(SSL_ERROR_RX_MALFORMED_CERT_REQUEST); |
7968 | 6 | goto loser; /* malformed, alert has been sent */ |
7969 | 6 | } |
7970 | 149 | if (signatureSchemeCount == 0) { |
7971 | 15 | errCode = SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM; |
7972 | 15 | desc = handshake_failure; |
7973 | 15 | goto alert_loser; |
7974 | 15 | } |
7975 | 149 | } |
7976 | | |
7977 | 173 | rv = ssl3_ParseCertificateRequestCAs(ss, &b, &length, &ca_list); |
7978 | 173 | if (rv != SECSuccess) |
7979 | 150 | goto done; /* alert sent in ssl3_ParseCertificateRequestCAs */ |
7980 | | |
7981 | 23 | if (length != 0) |
7982 | 12 | goto alert_loser; /* malformed */ |
7983 | | |
7984 | 11 | ss->ssl3.hs.ws = wait_hello_done; |
7985 | | |
7986 | 11 | rv = ssl3_BeginHandleCertificateRequest(ss, signatureSchemes, |
7987 | 11 | signatureSchemeCount, &ca_list); |
7988 | 11 | if (rv != SECSuccess) { |
7989 | 0 | PORT_Assert(0); |
7990 | 0 | errCode = SEC_ERROR_LIBRARY_FAILURE; |
7991 | 0 | desc = internal_error; |
7992 | 0 | goto alert_loser; |
7993 | 0 | } |
7994 | 11 | goto done; |
7995 | | |
7996 | 11 | no_mem: |
7997 | 0 | rv = SECFailure; |
7998 | 0 | PORT_SetError(SEC_ERROR_NO_MEMORY); |
7999 | 0 | goto done; |
8000 | | |
8001 | 33 | alert_loser: |
8002 | 33 | if (isTLS && desc == illegal_parameter) |
8003 | 12 | desc = decode_error; |
8004 | 33 | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
8005 | 45 | loser: |
8006 | 45 | PORT_SetError(errCode); |
8007 | 45 | rv = SECFailure; |
8008 | 206 | done: |
8009 | 206 | if (arena != NULL) |
8010 | 194 | PORT_FreeArena(arena, PR_FALSE); |
8011 | 206 | return rv; |
8012 | 45 | } |
8013 | | |
8014 | | static void |
8015 | | ssl3_ClientAuthCallbackOutcome(sslSocket *ss, SECStatus outcome) |
8016 | 11 | { |
8017 | 11 | SECStatus rv; |
8018 | 11 | switch (outcome) { |
8019 | 0 | case SECSuccess: |
8020 | | /* check what the callback function returned */ |
8021 | 0 | if ((!ss->ssl3.clientCertificate) || (!ss->ssl3.clientPrivateKey)) { |
8022 | | /* we are missing either the key or cert */ |
8023 | 0 | goto send_no_certificate; |
8024 | 0 | } |
8025 | | /* Setting ssl3.clientCertChain non-NULL will cause |
8026 | | * ssl3_HandleServerHelloDone to call SendCertificate. |
8027 | | */ |
8028 | 0 | ss->ssl3.clientCertChain = CERT_CertChainFromCert( |
8029 | 0 | ss->ssl3.clientCertificate, |
8030 | 0 | certUsageSSLClient, PR_FALSE); |
8031 | 0 | if (ss->ssl3.clientCertChain == NULL) { |
8032 | 0 | goto send_no_certificate; |
8033 | 0 | } |
8034 | 0 | if (ss->ssl3.hs.hashType == handshake_hash_record || |
8035 | 0 | ss->ssl3.hs.hashType == handshake_hash_single) { |
8036 | 0 | rv = ssl_PickClientSignatureScheme(ss, |
8037 | 0 | ss->ssl3.clientCertificate, |
8038 | 0 | ss->ssl3.clientPrivateKey, |
8039 | 0 | ss->ssl3.hs.clientAuthSignatureSchemes, |
8040 | 0 | ss->ssl3.hs.clientAuthSignatureSchemesLen, |
8041 | 0 | &ss->ssl3.hs.signatureScheme); |
8042 | 0 | if (rv != SECSuccess) { |
8043 | | /* This should only happen if our schemes changed or |
8044 | | * if an RSA-PSS cert was selected, but the token |
8045 | | * does not support PSS schemes. |
8046 | | */ |
8047 | 0 | goto send_no_certificate; |
8048 | 0 | } |
8049 | 0 | } |
8050 | 0 | break; |
8051 | | |
8052 | 11 | case SECFailure: |
8053 | 11 | default: |
8054 | 11 | send_no_certificate: |
8055 | 11 | CERT_DestroyCertificate(ss->ssl3.clientCertificate); |
8056 | 11 | SECKEY_DestroyPrivateKey(ss->ssl3.clientPrivateKey); |
8057 | 11 | ss->ssl3.clientCertificate = NULL; |
8058 | 11 | ss->ssl3.clientPrivateKey = NULL; |
8059 | 11 | if (ss->ssl3.clientCertChain) { |
8060 | 0 | CERT_DestroyCertificateList(ss->ssl3.clientCertChain); |
8061 | 0 | ss->ssl3.clientCertChain = NULL; |
8062 | 0 | } |
8063 | | |
8064 | 11 | if (ss->version > SSL_LIBRARY_VERSION_3_0) { |
8065 | 11 | ss->ssl3.sendEmptyCert = PR_TRUE; |
8066 | 11 | } else { |
8067 | 0 | (void)SSL3_SendAlert(ss, alert_warning, no_certificate); |
8068 | 0 | } |
8069 | 11 | break; |
8070 | 11 | } |
8071 | | |
8072 | | /* Release the cached parameters */ |
8073 | 11 | PORT_Free(ss->ssl3.hs.clientAuthSignatureSchemes); |
8074 | 11 | ss->ssl3.hs.clientAuthSignatureSchemes = NULL; |
8075 | 11 | ss->ssl3.hs.clientAuthSignatureSchemesLen = 0; |
8076 | 11 | } |
8077 | | |
8078 | | SECStatus |
8079 | | ssl3_BeginHandleCertificateRequest(sslSocket *ss, |
8080 | | const SSLSignatureScheme *signatureSchemes, |
8081 | | unsigned int signatureSchemeCount, |
8082 | | CERTDistNames *ca_list) |
8083 | 11 | { |
8084 | 11 | SECStatus rv; |
8085 | | |
8086 | 11 | PR_ASSERT(!ss->ssl3.hs.clientCertificatePending); |
8087 | | |
8088 | | /* Should not send a client cert when (non-GREASE) ECH is rejected. */ |
8089 | 11 | if (ss->ssl3.hs.echHpkeCtx && !ss->ssl3.hs.echAccepted) { |
8090 | 6 | PORT_Assert(ssl3_ExtensionAdvertised(ss, ssl_tls13_encrypted_client_hello_xtn)); |
8091 | 6 | rv = SECFailure; |
8092 | 6 | } else if (ss->getClientAuthData != NULL) { |
8093 | 0 | PORT_Assert(signatureSchemes || !signatureSchemeCount); |
8094 | 0 | PORT_Assert((ss->ssl3.hs.preliminaryInfo & ssl_preinfo_all) == |
8095 | 0 | ssl_preinfo_all); |
8096 | 0 | PORT_Assert(ss->ssl3.clientPrivateKey == NULL); |
8097 | 0 | PORT_Assert(ss->ssl3.clientCertificate == NULL); |
8098 | 0 | PORT_Assert(ss->ssl3.clientCertChain == NULL); |
8099 | | |
8100 | | /* Previously cached parameters should be empty */ |
8101 | 0 | PORT_Assert(ss->ssl3.hs.clientAuthSignatureSchemes == NULL); |
8102 | 0 | PORT_Assert(ss->ssl3.hs.clientAuthSignatureSchemesLen == 0); |
8103 | | /* |
8104 | | * Peer signatures are only available while in the context of |
8105 | | * of a getClientAuthData callback. It is required for proper |
8106 | | * functioning of SSL_CertIsUsable and SSL_FilterClientCertListBySocket |
8107 | | * Calling these functions outside the context of a getClientAuthData |
8108 | | * callback will result in no filtering.*/ |
8109 | |
|
8110 | 0 | ss->ssl3.hs.clientAuthSignatureSchemes = PORT_ZNewArray(SSLSignatureScheme, signatureSchemeCount); |
8111 | 0 | if (signatureSchemes) { |
8112 | 0 | PORT_Memcpy(ss->ssl3.hs.clientAuthSignatureSchemes, signatureSchemes, signatureSchemeCount * sizeof(SSLSignatureScheme)); |
8113 | 0 | } |
8114 | 0 | ss->ssl3.hs.clientAuthSignatureSchemesLen = signatureSchemeCount; |
8115 | |
|
8116 | 0 | rv = (SECStatus)(*ss->getClientAuthData)(ss->getClientAuthDataArg, |
8117 | 0 | ss->fd, ca_list, |
8118 | 0 | &ss->ssl3.clientCertificate, |
8119 | 0 | &ss->ssl3.clientPrivateKey); |
8120 | 5 | } else { |
8121 | 5 | rv = SECFailure; /* force it to send a no_certificate alert */ |
8122 | 5 | } |
8123 | | |
8124 | 11 | if (rv == SECWouldBlock) { |
8125 | | /* getClientAuthData needs more time (e.g. for user interaction) */ |
8126 | | |
8127 | | /* The out parameters should not have changed. */ |
8128 | 0 | PORT_Assert(ss->ssl3.clientCertificate == NULL); |
8129 | 0 | PORT_Assert(ss->ssl3.clientPrivateKey == NULL); |
8130 | | |
8131 | | /* Mark the handshake as blocked */ |
8132 | 0 | ss->ssl3.hs.clientCertificatePending = PR_TRUE; |
8133 | |
|
8134 | 0 | rv = SECSuccess; |
8135 | 11 | } else { |
8136 | | /* getClientAuthData returned SECSuccess or SECFailure immediately, handle accordingly */ |
8137 | 11 | ssl3_ClientAuthCallbackOutcome(ss, rv); |
8138 | 11 | rv = SECSuccess; |
8139 | 11 | } |
8140 | 11 | return rv; |
8141 | 11 | } |
8142 | | |
8143 | | /* Invoked by the application when client certificate selection is complete */ |
8144 | | SECStatus |
8145 | | ssl3_ClientCertCallbackComplete(sslSocket *ss, SECStatus outcome, SECKEYPrivateKey *clientPrivateKey, CERTCertificate *clientCertificate) |
8146 | 0 | { |
8147 | 0 | PORT_Assert(ss->ssl3.hs.clientCertificatePending); |
8148 | 0 | ss->ssl3.hs.clientCertificatePending = PR_FALSE; |
8149 | |
|
8150 | 0 | ss->ssl3.clientCertificate = clientCertificate; |
8151 | 0 | ss->ssl3.clientPrivateKey = clientPrivateKey; |
8152 | |
|
8153 | 0 | ssl3_ClientAuthCallbackOutcome(ss, outcome); |
8154 | | |
8155 | | /* Continue the handshake */ |
8156 | 0 | PORT_Assert(ss->ssl3.hs.restartTarget); |
8157 | 0 | if (!ss->ssl3.hs.restartTarget) { |
8158 | 0 | FATAL_ERROR(ss, PR_INVALID_STATE_ERROR, internal_error); |
8159 | 0 | return SECFailure; |
8160 | 0 | } |
8161 | 0 | sslRestartTarget target = ss->ssl3.hs.restartTarget; |
8162 | 0 | ss->ssl3.hs.restartTarget = NULL; |
8163 | 0 | return target(ss); |
8164 | 0 | } |
8165 | | |
8166 | | static SECStatus |
8167 | | ssl3_CheckFalseStart(sslSocket *ss) |
8168 | 1.61k | { |
8169 | 1.61k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
8170 | 1.61k | PORT_Assert(!ss->ssl3.hs.authCertificatePending); |
8171 | 1.61k | PORT_Assert(!ss->ssl3.hs.canFalseStart); |
8172 | | |
8173 | 1.61k | if (!ss->canFalseStartCallback) { |
8174 | 0 | SSL_TRC(3, ("%d: SSL[%d]: no false start callback so no false start", |
8175 | 0 | SSL_GETPID(), ss->fd)); |
8176 | 1.61k | } else { |
8177 | 1.61k | SECStatus rv; |
8178 | | |
8179 | 1.61k | rv = ssl_CheckServerRandom(ss); |
8180 | 1.61k | if (rv != SECSuccess) { |
8181 | 0 | SSL_TRC(3, ("%d: SSL[%d]: no false start due to possible downgrade", |
8182 | 0 | SSL_GETPID(), ss->fd)); |
8183 | 0 | goto no_false_start; |
8184 | 0 | } |
8185 | | |
8186 | | /* An attacker can control the selected ciphersuite so we only wish to |
8187 | | * do False Start in the case that the selected ciphersuite is |
8188 | | * sufficiently strong that the attack can gain no advantage. |
8189 | | * Therefore we always require an 80-bit cipher. */ |
8190 | 1.61k | ssl_GetSpecReadLock(ss); |
8191 | 1.61k | PRBool weakCipher = ss->ssl3.cwSpec->cipherDef->secret_key_size < 10; |
8192 | 1.61k | ssl_ReleaseSpecReadLock(ss); |
8193 | 1.61k | if (weakCipher) { |
8194 | 372 | SSL_TRC(3, ("%d: SSL[%d]: no false start due to weak cipher", |
8195 | 372 | SSL_GETPID(), ss->fd)); |
8196 | 372 | goto no_false_start; |
8197 | 372 | } |
8198 | | |
8199 | 1.24k | if (ssl3_ExtensionAdvertised(ss, ssl_tls13_encrypted_client_hello_xtn)) { |
8200 | 621 | SSL_TRC(3, ("%d: SSL[%d]: no false start due to lower version after ECH", |
8201 | 621 | SSL_GETPID(), ss->fd)); |
8202 | 621 | goto no_false_start; |
8203 | 621 | } |
8204 | | |
8205 | 623 | PORT_Assert((ss->ssl3.hs.preliminaryInfo & ssl_preinfo_all) == |
8206 | 623 | ssl_preinfo_all); |
8207 | 623 | rv = (ss->canFalseStartCallback)(ss->fd, |
8208 | 623 | ss->canFalseStartCallbackData, |
8209 | 623 | &ss->ssl3.hs.canFalseStart); |
8210 | 623 | if (rv == SECSuccess) { |
8211 | 623 | SSL_TRC(3, ("%d: SSL[%d]: false start callback returned %s", |
8212 | 623 | SSL_GETPID(), ss->fd, |
8213 | 623 | ss->ssl3.hs.canFalseStart ? "TRUE" |
8214 | 623 | : "FALSE")); |
8215 | 623 | } else { |
8216 | 0 | SSL_TRC(3, ("%d: SSL[%d]: false start callback failed (%s)", |
8217 | 0 | SSL_GETPID(), ss->fd, |
8218 | 0 | PR_ErrorToName(PR_GetError()))); |
8219 | 0 | } |
8220 | 623 | return rv; |
8221 | 1.24k | } |
8222 | | |
8223 | 993 | no_false_start: |
8224 | 993 | ss->ssl3.hs.canFalseStart = PR_FALSE; |
8225 | 993 | return SECSuccess; |
8226 | 1.61k | } |
8227 | | |
8228 | | PRBool |
8229 | | ssl3_WaitingForServerSecondRound(sslSocket *ss) |
8230 | 35.4k | { |
8231 | 35.4k | PRBool result; |
8232 | | |
8233 | 35.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
8234 | | |
8235 | 35.4k | switch (ss->ssl3.hs.ws) { |
8236 | 327 | case wait_new_session_ticket: |
8237 | 34.3k | case wait_change_cipher: |
8238 | 35.4k | case wait_finished: |
8239 | 35.4k | result = PR_TRUE; |
8240 | 35.4k | break; |
8241 | 0 | default: |
8242 | 0 | result = PR_FALSE; |
8243 | 0 | break; |
8244 | 35.4k | } |
8245 | | |
8246 | 35.4k | return result; |
8247 | 35.4k | } |
8248 | | |
8249 | | static SECStatus ssl3_SendClientSecondRound(sslSocket *ss); |
8250 | | |
8251 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered |
8252 | | * a complete ssl3 Server Hello Done message. |
8253 | | * Caller must hold Handshake and RecvBuf locks. |
8254 | | */ |
8255 | | static SECStatus |
8256 | | ssl3_HandleServerHelloDone(sslSocket *ss) |
8257 | 33.4k | { |
8258 | 33.4k | SECStatus rv; |
8259 | 33.4k | SSL3WaitState ws = ss->ssl3.hs.ws; |
8260 | | |
8261 | 33.4k | SSL_TRC(3, ("%d: SSL3[%d]: handle server_hello_done handshake", |
8262 | 33.4k | SSL_GETPID(), ss->fd)); |
8263 | 33.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
8264 | 33.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
8265 | | |
8266 | | /* Skipping CertificateRequest is always permitted. */ |
8267 | 33.4k | if (ws != wait_hello_done && |
8268 | 33.4k | ws != wait_cert_request) { |
8269 | 13 | SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
8270 | 13 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_DONE); |
8271 | 13 | return SECFailure; |
8272 | 13 | } |
8273 | | |
8274 | 33.4k | rv = ssl3_SendClientSecondRound(ss); |
8275 | | |
8276 | 33.4k | return rv; |
8277 | 33.4k | } |
8278 | | |
8279 | | /* Called from ssl3_HandleServerHelloDone and ssl3_AuthCertificateComplete. |
8280 | | * |
8281 | | * Caller must hold Handshake and RecvBuf locks. |
8282 | | */ |
8283 | | static SECStatus |
8284 | | ssl3_SendClientSecondRound(sslSocket *ss) |
8285 | 33.4k | { |
8286 | 33.4k | SECStatus rv; |
8287 | 33.4k | PRBool sendClientCert; |
8288 | | |
8289 | 33.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
8290 | 33.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
8291 | | |
8292 | 33.4k | sendClientCert = !ss->ssl3.sendEmptyCert && |
8293 | 33.4k | ss->ssl3.clientCertChain != NULL && |
8294 | 33.4k | ss->ssl3.clientPrivateKey != NULL; |
8295 | | |
8296 | | /* We must wait for the server's certificate to be authenticated before |
8297 | | * sending the client certificate in order to disclosing the client |
8298 | | * certificate to an attacker that does not have a valid cert for the |
8299 | | * domain we are connecting to. |
8300 | | * |
8301 | | * During the initial handshake on a connection, we never send/receive |
8302 | | * application data until we have authenticated the server's certificate; |
8303 | | * i.e. we have fully authenticated the handshake before using the cipher |
8304 | | * specs agreed upon for that handshake. During a renegotiation, we may |
8305 | | * continue sending and receiving application data during the handshake |
8306 | | * interleaved with the handshake records. If we were to send the client's |
8307 | | * second round for a renegotiation before the server's certificate was |
8308 | | * authenticated, then the application data sent/received after this point |
8309 | | * would be using cipher spec that hadn't been authenticated. By waiting |
8310 | | * until the server's certificate has been authenticated during |
8311 | | * renegotiations, we ensure that renegotiations have the same property |
8312 | | * as initial handshakes; i.e. we have fully authenticated the handshake |
8313 | | * before using the cipher specs agreed upon for that handshake for |
8314 | | * application data. |
8315 | | */ |
8316 | 33.4k | if (ss->ssl3.hs.restartTarget) { |
8317 | 0 | PR_NOT_REACHED("unexpected ss->ssl3.hs.restartTarget"); |
8318 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
8319 | 0 | return SECFailure; |
8320 | 0 | } |
8321 | | /* Check whether waiting for client certificate selection OR |
8322 | | waiting on server certificate verification AND |
8323 | | going to send client cert */ |
8324 | 33.4k | if ((ss->ssl3.hs.clientCertificatePending) || |
8325 | 33.4k | (ss->ssl3.hs.authCertificatePending && (sendClientCert || ss->ssl3.sendEmptyCert || ss->firstHsDone))) { |
8326 | 0 | SSL_TRC(3, ("%d: SSL3[%p]: deferring ssl3_SendClientSecondRound because" |
8327 | 0 | " certificate authentication is still pending.", |
8328 | 0 | SSL_GETPID(), ss->fd)); |
8329 | 0 | ss->ssl3.hs.restartTarget = ssl3_SendClientSecondRound; |
8330 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
8331 | 0 | return SECFailure; |
8332 | 0 | } |
8333 | | |
8334 | 33.4k | ssl_GetXmitBufLock(ss); /*******************************/ |
8335 | | |
8336 | 33.4k | if (ss->ssl3.sendEmptyCert) { |
8337 | 6 | ss->ssl3.sendEmptyCert = PR_FALSE; |
8338 | 6 | rv = ssl3_SendEmptyCertificate(ss); |
8339 | | /* Don't send verify */ |
8340 | 6 | if (rv != SECSuccess) { |
8341 | 0 | goto loser; /* error code is set. */ |
8342 | 0 | } |
8343 | 33.4k | } else if (sendClientCert) { |
8344 | 0 | rv = ssl3_SendCertificate(ss); |
8345 | 0 | if (rv != SECSuccess) { |
8346 | 0 | goto loser; /* error code is set. */ |
8347 | 0 | } |
8348 | 0 | } |
8349 | | |
8350 | 33.4k | rv = ssl3_SendClientKeyExchange(ss); |
8351 | 33.4k | if (rv != SECSuccess) { |
8352 | 165 | goto loser; /* err is set. */ |
8353 | 165 | } |
8354 | | |
8355 | 33.2k | if (sendClientCert) { |
8356 | 0 | rv = ssl3_SendCertificateVerify(ss, ss->ssl3.clientPrivateKey); |
8357 | 0 | SECKEY_DestroyPrivateKey(ss->ssl3.clientPrivateKey); |
8358 | 0 | ss->ssl3.clientPrivateKey = NULL; |
8359 | 0 | if (rv != SECSuccess) { |
8360 | 0 | goto loser; /* err is set. */ |
8361 | 0 | } |
8362 | 0 | } |
8363 | | |
8364 | 33.2k | rv = ssl3_SendChangeCipherSpecs(ss); |
8365 | 33.2k | if (rv != SECSuccess) { |
8366 | 0 | goto loser; /* err code was set. */ |
8367 | 0 | } |
8368 | | |
8369 | | /* This must be done after we've set ss->ssl3.cwSpec in |
8370 | | * ssl3_SendChangeCipherSpecs because SSL_GetChannelInfo uses information |
8371 | | * from cwSpec. This must be done before we call ssl3_CheckFalseStart |
8372 | | * because the false start callback (if any) may need the information from |
8373 | | * the functions that depend on this being set. |
8374 | | */ |
8375 | 33.2k | ss->enoughFirstHsDone = PR_TRUE; |
8376 | | |
8377 | 33.2k | if (!ss->firstHsDone) { |
8378 | 3.25k | if (ss->opt.enableFalseStart) { |
8379 | 1.61k | if (!ss->ssl3.hs.authCertificatePending) { |
8380 | | /* When we fix bug 589047, we will need to know whether we are |
8381 | | * false starting before we try to flush the client second |
8382 | | * round to the network. With that in mind, we purposefully |
8383 | | * call ssl3_CheckFalseStart before calling ssl3_SendFinished, |
8384 | | * which includes a call to ssl3_FlushHandshake, so that |
8385 | | * no application develops a reliance on such flushing being |
8386 | | * done before its false start callback is called. |
8387 | | */ |
8388 | 1.61k | ssl_ReleaseXmitBufLock(ss); |
8389 | 1.61k | rv = ssl3_CheckFalseStart(ss); |
8390 | 1.61k | ssl_GetXmitBufLock(ss); |
8391 | 1.61k | if (rv != SECSuccess) { |
8392 | 0 | goto loser; |
8393 | 0 | } |
8394 | 1.61k | } else { |
8395 | | /* The certificate authentication and the server's Finished |
8396 | | * message are racing each other. If the certificate |
8397 | | * authentication wins, then we will try to false start in |
8398 | | * ssl3_AuthCertificateComplete. |
8399 | | */ |
8400 | 0 | SSL_TRC(3, ("%d: SSL3[%p]: deferring false start check because" |
8401 | 0 | " certificate authentication is still pending.", |
8402 | 0 | SSL_GETPID(), ss->fd)); |
8403 | 0 | } |
8404 | 1.61k | } |
8405 | 3.25k | } |
8406 | | |
8407 | 33.2k | rv = ssl3_SendFinished(ss, 0); |
8408 | 33.2k | if (rv != SECSuccess) { |
8409 | 0 | goto loser; /* err code was set. */ |
8410 | 0 | } |
8411 | | |
8412 | 33.2k | ssl_ReleaseXmitBufLock(ss); /*******************************/ |
8413 | | |
8414 | 33.2k | if (ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn)) |
8415 | 46 | ss->ssl3.hs.ws = wait_new_session_ticket; |
8416 | 33.2k | else |
8417 | 33.2k | ss->ssl3.hs.ws = wait_change_cipher; |
8418 | | |
8419 | 33.2k | PORT_Assert(ssl3_WaitingForServerSecondRound(ss)); |
8420 | | |
8421 | 33.2k | return SECSuccess; |
8422 | | |
8423 | 165 | loser: |
8424 | 165 | ssl_ReleaseXmitBufLock(ss); |
8425 | 165 | return rv; |
8426 | 33.2k | } |
8427 | | |
8428 | | /* |
8429 | | * Routines used by servers |
8430 | | */ |
8431 | | static SECStatus |
8432 | | ssl3_SendHelloRequest(sslSocket *ss) |
8433 | 0 | { |
8434 | 0 | SECStatus rv; |
8435 | |
|
8436 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: send hello_request handshake", SSL_GETPID(), |
8437 | 0 | ss->fd)); |
8438 | |
|
8439 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
8440 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
8441 | |
|
8442 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_hello_request, 0); |
8443 | 0 | if (rv != SECSuccess) { |
8444 | 0 | return rv; /* err set by AppendHandshake */ |
8445 | 0 | } |
8446 | 0 | rv = ssl3_FlushHandshake(ss, 0); |
8447 | 0 | if (rv != SECSuccess) { |
8448 | 0 | return rv; /* error code set by ssl3_FlushHandshake */ |
8449 | 0 | } |
8450 | 0 | ss->ssl3.hs.ws = wait_client_hello; |
8451 | 0 | return SECSuccess; |
8452 | 0 | } |
8453 | | |
8454 | | /* |
8455 | | * Called from: |
8456 | | * ssl3_HandleClientHello() |
8457 | | */ |
8458 | | static SECComparison |
8459 | | ssl3_ServerNameCompare(const SECItem *name1, const SECItem *name2) |
8460 | 0 | { |
8461 | 0 | if (!name1 != !name2) { |
8462 | 0 | return SECLessThan; |
8463 | 0 | } |
8464 | 0 | if (!name1) { |
8465 | 0 | return SECEqual; |
8466 | 0 | } |
8467 | 0 | if (name1->type != name2->type) { |
8468 | 0 | return SECLessThan; |
8469 | 0 | } |
8470 | 0 | return SECITEM_CompareItem(name1, name2); |
8471 | 0 | } |
8472 | | |
8473 | | /* Sets memory error when returning NULL. |
8474 | | * Called from: |
8475 | | * ssl3_SendClientHello() |
8476 | | * ssl3_HandleServerHello() |
8477 | | * ssl3_HandleClientHello() |
8478 | | * ssl3_HandleV2ClientHello() |
8479 | | */ |
8480 | | sslSessionID * |
8481 | | ssl3_NewSessionID(sslSocket *ss, PRBool is_server) |
8482 | 91.0k | { |
8483 | 91.0k | sslSessionID *sid; |
8484 | | |
8485 | 91.0k | sid = PORT_ZNew(sslSessionID); |
8486 | 91.0k | if (sid == NULL) |
8487 | 0 | return sid; |
8488 | | |
8489 | 91.0k | if (is_server) { |
8490 | 0 | const SECItem *srvName; |
8491 | 0 | SECStatus rv = SECSuccess; |
8492 | |
|
8493 | 0 | ssl_GetSpecReadLock(ss); /********************************/ |
8494 | 0 | srvName = &ss->ssl3.hs.srvVirtName; |
8495 | 0 | if (srvName->len && srvName->data) { |
8496 | 0 | rv = SECITEM_CopyItem(NULL, &sid->u.ssl3.srvName, srvName); |
8497 | 0 | } |
8498 | 0 | ssl_ReleaseSpecReadLock(ss); /************************************/ |
8499 | 0 | if (rv != SECSuccess) { |
8500 | 0 | PORT_Free(sid); |
8501 | 0 | return NULL; |
8502 | 0 | } |
8503 | 0 | } |
8504 | 91.0k | sid->peerID = (ss->peerID == NULL) ? NULL : PORT_Strdup(ss->peerID); |
8505 | 91.0k | sid->urlSvrName = (ss->url == NULL) ? NULL : PORT_Strdup(ss->url); |
8506 | 91.0k | sid->addr = ss->sec.ci.peer; |
8507 | 91.0k | sid->port = ss->sec.ci.port; |
8508 | 91.0k | sid->references = 1; |
8509 | 91.0k | sid->cached = never_cached; |
8510 | 91.0k | sid->version = ss->version; |
8511 | 91.0k | sid->sigScheme = ssl_sig_none; |
8512 | | |
8513 | 91.0k | sid->u.ssl3.keys.resumable = PR_TRUE; |
8514 | 91.0k | sid->u.ssl3.policy = SSL_ALLOWED; |
8515 | 91.0k | sid->u.ssl3.keys.extendedMasterSecretUsed = PR_FALSE; |
8516 | | |
8517 | 91.0k | if (is_server) { |
8518 | 0 | SECStatus rv; |
8519 | 0 | int pid = SSL_GETPID(); |
8520 | |
|
8521 | 0 | sid->u.ssl3.sessionIDLength = SSL3_SESSIONID_BYTES; |
8522 | 0 | sid->u.ssl3.sessionID[0] = (pid >> 8) & 0xff; |
8523 | 0 | sid->u.ssl3.sessionID[1] = pid & 0xff; |
8524 | 0 | rv = PK11_GenerateRandom(sid->u.ssl3.sessionID + 2, |
8525 | 0 | SSL3_SESSIONID_BYTES - 2); |
8526 | 0 | if (rv != SECSuccess) { |
8527 | 0 | ssl_FreeSID(sid); |
8528 | 0 | ssl_MapLowLevelError(SSL_ERROR_GENERATE_RANDOM_FAILURE); |
8529 | 0 | return NULL; |
8530 | 0 | } |
8531 | 0 | } |
8532 | 91.0k | return sid; |
8533 | 91.0k | } |
8534 | | |
8535 | | /* Called from: ssl3_HandleClientHello, ssl3_HandleV2ClientHello */ |
8536 | | static SECStatus |
8537 | | ssl3_SendServerHelloSequence(sslSocket *ss) |
8538 | 0 | { |
8539 | 0 | const ssl3KEADef *kea_def; |
8540 | 0 | SECStatus rv; |
8541 | |
|
8542 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: begin send server_hello sequence", |
8543 | 0 | SSL_GETPID(), ss->fd)); |
8544 | |
|
8545 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
8546 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
8547 | |
|
8548 | 0 | rv = ssl3_SendServerHello(ss); |
8549 | 0 | if (rv != SECSuccess) { |
8550 | 0 | return rv; /* err code is set. */ |
8551 | 0 | } |
8552 | 0 | rv = ssl3_SendCertificate(ss); |
8553 | 0 | if (rv != SECSuccess) { |
8554 | 0 | return rv; /* error code is set. */ |
8555 | 0 | } |
8556 | 0 | rv = ssl3_SendCertificateStatus(ss); |
8557 | 0 | if (rv != SECSuccess) { |
8558 | 0 | return rv; /* error code is set. */ |
8559 | 0 | } |
8560 | | /* We have to do this after the call to ssl3_SendServerHello, |
8561 | | * because kea_def is set up by ssl3_SendServerHello(). |
8562 | | */ |
8563 | 0 | kea_def = ss->ssl3.hs.kea_def; |
8564 | |
|
8565 | 0 | if (kea_def->ephemeral) { |
8566 | 0 | rv = ssl3_SendServerKeyExchange(ss); |
8567 | 0 | if (rv != SECSuccess) { |
8568 | 0 | return rv; /* err code was set. */ |
8569 | 0 | } |
8570 | 0 | } |
8571 | | |
8572 | 0 | if (ss->opt.requestCertificate) { |
8573 | 0 | rv = ssl3_SendCertificateRequest(ss); |
8574 | 0 | if (rv != SECSuccess) { |
8575 | 0 | return rv; /* err code is set. */ |
8576 | 0 | } |
8577 | 0 | } |
8578 | 0 | rv = ssl3_SendServerHelloDone(ss); |
8579 | 0 | if (rv != SECSuccess) { |
8580 | 0 | return rv; /* err code is set. */ |
8581 | 0 | } |
8582 | | |
8583 | 0 | ss->ssl3.hs.ws = (ss->opt.requestCertificate) ? wait_client_cert |
8584 | 0 | : wait_client_key; |
8585 | 0 | return SECSuccess; |
8586 | 0 | } |
8587 | | |
8588 | | /* An empty TLS Renegotiation Info (RI) extension */ |
8589 | | static const PRUint8 emptyRIext[5] = { 0xff, 0x01, 0x00, 0x01, 0x00 }; |
8590 | | |
8591 | | static PRBool |
8592 | | ssl3_KEASupportsTickets(const ssl3KEADef *kea_def) |
8593 | 0 | { |
8594 | 0 | if (kea_def->signKeyType == dsaKey) { |
8595 | | /* TODO: Fix session tickets for DSS. The server code rejects the |
8596 | | * session ticket received from the client. Bug 1174677 */ |
8597 | 0 | return PR_FALSE; |
8598 | 0 | } |
8599 | 0 | return PR_TRUE; |
8600 | 0 | } |
8601 | | |
8602 | | static PRBool |
8603 | | ssl3_PeerSupportsCipherSuite(const SECItem *peerSuites, uint16_t suite) |
8604 | 0 | { |
8605 | 0 | for (unsigned int i = 0; i + 1 < peerSuites->len; i += 2) { |
8606 | 0 | PRUint16 suite_i = (peerSuites->data[i] << 8) | peerSuites->data[i + 1]; |
8607 | 0 | if (suite_i == suite) { |
8608 | 0 | return PR_TRUE; |
8609 | 0 | } |
8610 | 0 | } |
8611 | 0 | return PR_FALSE; |
8612 | 0 | } |
8613 | | |
8614 | | SECStatus |
8615 | | ssl3_NegotiateCipherSuiteInner(sslSocket *ss, const SECItem *suites, |
8616 | | PRUint16 version, PRUint16 *suitep) |
8617 | 0 | { |
8618 | 0 | unsigned int i; |
8619 | 0 | SSLVersionRange vrange = { version, version }; |
8620 | | |
8621 | | /* If we negotiated an External PSK and that PSK has a ciphersuite |
8622 | | * configured, we need to constrain our choice. If the client does |
8623 | | * not support it, negotiate a certificate auth suite and fall back. |
8624 | | */ |
8625 | 0 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
8626 | 0 | ss->xtnData.selectedPsk && |
8627 | 0 | ss->xtnData.selectedPsk->type == ssl_psk_external && |
8628 | 0 | ss->xtnData.selectedPsk->zeroRttSuite != TLS_NULL_WITH_NULL_NULL) { |
8629 | 0 | PRUint16 pskSuite = ss->xtnData.selectedPsk->zeroRttSuite; |
8630 | 0 | ssl3CipherSuiteCfg *pskSuiteCfg = ssl_LookupCipherSuiteCfgMutable(pskSuite, |
8631 | 0 | ss->cipherSuites); |
8632 | 0 | if (ssl3_config_match(pskSuiteCfg, ss->ssl3.policy, &vrange, ss) && |
8633 | 0 | ssl3_PeerSupportsCipherSuite(suites, pskSuite)) { |
8634 | 0 | *suitep = pskSuite; |
8635 | 0 | return SECSuccess; |
8636 | 0 | } |
8637 | 0 | } |
8638 | | |
8639 | 0 | for (i = 0; i < ssl_V3_SUITES_IMPLEMENTED; i++) { |
8640 | 0 | ssl3CipherSuiteCfg *suite = &ss->cipherSuites[i]; |
8641 | 0 | if (!ssl3_config_match(suite, ss->ssl3.policy, &vrange, ss)) { |
8642 | 0 | continue; |
8643 | 0 | } |
8644 | 0 | if (!ssl3_PeerSupportsCipherSuite(suites, suite->cipher_suite)) { |
8645 | 0 | continue; |
8646 | 0 | } |
8647 | 0 | *suitep = suite->cipher_suite; |
8648 | 0 | return SECSuccess; |
8649 | 0 | } |
8650 | 0 | PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP); |
8651 | 0 | return SECFailure; |
8652 | 0 | } |
8653 | | |
8654 | | /* Select a cipher suite. |
8655 | | ** |
8656 | | ** NOTE: This suite selection algorithm should be the same as the one in |
8657 | | ** ssl3_HandleV2ClientHello(). |
8658 | | ** |
8659 | | ** If TLS 1.0 is enabled, we could handle the case where the client |
8660 | | ** offered TLS 1.1 but offered only export cipher suites by choosing TLS |
8661 | | ** 1.0 and selecting one of those export cipher suites. However, a secure |
8662 | | ** TLS 1.1 client should not have export cipher suites enabled at all, |
8663 | | ** and a TLS 1.1 client should definitely not be offering *only* export |
8664 | | ** cipher suites. Therefore, we refuse to negotiate export cipher suites |
8665 | | ** with any client that indicates support for TLS 1.1 or higher when we |
8666 | | ** (the server) have TLS 1.1 support enabled. |
8667 | | */ |
8668 | | SECStatus |
8669 | | ssl3_NegotiateCipherSuite(sslSocket *ss, const SECItem *suites, |
8670 | | PRBool initHashes) |
8671 | 0 | { |
8672 | 0 | PRUint16 selected; |
8673 | 0 | SECStatus rv; |
8674 | | |
8675 | | /* Ensure that only valid cipher suites are enabled. */ |
8676 | 0 | if (ssl3_config_match_init(ss) == 0) { |
8677 | | /* No configured cipher is both supported by PK11 and allowed. |
8678 | | * This is a configuration error, so report handshake failure.*/ |
8679 | 0 | FATAL_ERROR(ss, PORT_GetError(), handshake_failure); |
8680 | 0 | return SECFailure; |
8681 | 0 | } |
8682 | | |
8683 | 0 | rv = ssl3_NegotiateCipherSuiteInner(ss, suites, ss->version, &selected); |
8684 | 0 | if (rv != SECSuccess) { |
8685 | 0 | return SECFailure; |
8686 | 0 | } |
8687 | | |
8688 | 0 | ss->ssl3.hs.cipher_suite = selected; |
8689 | 0 | return ssl3_SetupCipherSuite(ss, initHashes); |
8690 | 0 | } |
8691 | | |
8692 | | /* |
8693 | | * Call the SNI config hook. |
8694 | | * |
8695 | | * Called from: |
8696 | | * ssl3_HandleClientHello |
8697 | | * tls13_HandleClientHelloPart2 |
8698 | | */ |
8699 | | SECStatus |
8700 | | ssl3_ServerCallSNICallback(sslSocket *ss) |
8701 | 0 | { |
8702 | 0 | int errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; |
8703 | 0 | SSL3AlertDescription desc = illegal_parameter; |
8704 | 0 | int ret = 0; |
8705 | |
|
8706 | | #ifdef SSL_SNI_ALLOW_NAME_CHANGE_2HS |
8707 | | #error("No longer allowed to set SSL_SNI_ALLOW_NAME_CHANGE_2HS") |
8708 | | #endif |
8709 | 0 | if (!ssl3_ExtensionNegotiated(ss, ssl_server_name_xtn)) { |
8710 | 0 | if (ss->firstHsDone) { |
8711 | | /* Check that we don't have the name is current spec |
8712 | | * if this extension was not negotiated on the 2d hs. */ |
8713 | 0 | PRBool passed = PR_TRUE; |
8714 | 0 | ssl_GetSpecReadLock(ss); /*******************************/ |
8715 | 0 | if (ss->ssl3.hs.srvVirtName.data) { |
8716 | 0 | passed = PR_FALSE; |
8717 | 0 | } |
8718 | 0 | ssl_ReleaseSpecReadLock(ss); /***************************/ |
8719 | 0 | if (!passed) { |
8720 | 0 | errCode = SSL_ERROR_UNRECOGNIZED_NAME_ALERT; |
8721 | 0 | desc = handshake_failure; |
8722 | 0 | goto alert_loser; |
8723 | 0 | } |
8724 | 0 | } |
8725 | 0 | return SECSuccess; |
8726 | 0 | } |
8727 | | |
8728 | 0 | if (ss->sniSocketConfig) |
8729 | 0 | do { /* not a loop */ |
8730 | 0 | PORT_Assert((ss->ssl3.hs.preliminaryInfo & ssl_preinfo_all) == |
8731 | 0 | ssl_preinfo_all); |
8732 | |
|
8733 | 0 | ret = SSL_SNI_SEND_ALERT; |
8734 | | /* If extension is negotiated, the len of names should > 0. */ |
8735 | 0 | if (ss->xtnData.sniNameArrSize) { |
8736 | | /* Calling client callback to reconfigure the socket. */ |
8737 | 0 | ret = (SECStatus)(*ss->sniSocketConfig)(ss->fd, |
8738 | 0 | ss->xtnData.sniNameArr, |
8739 | 0 | ss->xtnData.sniNameArrSize, |
8740 | 0 | ss->sniSocketConfigArg); |
8741 | 0 | } |
8742 | 0 | if (ret <= SSL_SNI_SEND_ALERT) { |
8743 | | /* Application does not know the name or was not able to |
8744 | | * properly reconfigure the socket. */ |
8745 | 0 | errCode = SSL_ERROR_UNRECOGNIZED_NAME_ALERT; |
8746 | 0 | desc = unrecognized_name; |
8747 | 0 | break; |
8748 | 0 | } else if (ret == SSL_SNI_CURRENT_CONFIG_IS_USED) { |
8749 | 0 | SECStatus rv = SECSuccess; |
8750 | 0 | SECItem pwsNameBuf = { 0, NULL, 0 }; |
8751 | 0 | SECItem *pwsName = &pwsNameBuf; |
8752 | 0 | SECItem *cwsName; |
8753 | |
|
8754 | 0 | ssl_GetSpecWriteLock(ss); /*******************************/ |
8755 | 0 | cwsName = &ss->ssl3.hs.srvVirtName; |
8756 | | /* not allow name change on the 2d HS */ |
8757 | 0 | if (ss->firstHsDone) { |
8758 | 0 | if (ssl3_ServerNameCompare(pwsName, cwsName)) { |
8759 | 0 | ssl_ReleaseSpecWriteLock(ss); /******************/ |
8760 | 0 | errCode = SSL_ERROR_UNRECOGNIZED_NAME_ALERT; |
8761 | 0 | desc = handshake_failure; |
8762 | 0 | ret = SSL_SNI_SEND_ALERT; |
8763 | 0 | break; |
8764 | 0 | } |
8765 | 0 | } |
8766 | 0 | if (pwsName->data) { |
8767 | 0 | SECITEM_FreeItem(pwsName, PR_FALSE); |
8768 | 0 | } |
8769 | 0 | if (cwsName->data) { |
8770 | 0 | rv = SECITEM_CopyItem(NULL, pwsName, cwsName); |
8771 | 0 | } |
8772 | 0 | ssl_ReleaseSpecWriteLock(ss); /**************************/ |
8773 | 0 | if (rv != SECSuccess) { |
8774 | 0 | errCode = SSL_ERROR_INTERNAL_ERROR_ALERT; |
8775 | 0 | desc = internal_error; |
8776 | 0 | ret = SSL_SNI_SEND_ALERT; |
8777 | 0 | break; |
8778 | 0 | } |
8779 | 0 | } else if ((unsigned int)ret < ss->xtnData.sniNameArrSize) { |
8780 | | /* Application has configured new socket info. Lets check it |
8781 | | * and save the name. */ |
8782 | 0 | SECStatus rv; |
8783 | 0 | SECItem *name = &ss->xtnData.sniNameArr[ret]; |
8784 | 0 | SECItem *pwsName; |
8785 | | |
8786 | | /* get rid of the old name and save the newly picked. */ |
8787 | | /* This code is protected by ssl3HandshakeLock. */ |
8788 | 0 | ssl_GetSpecWriteLock(ss); /*******************************/ |
8789 | | /* not allow name change on the 2d HS */ |
8790 | 0 | if (ss->firstHsDone) { |
8791 | 0 | SECItem *cwsName = &ss->ssl3.hs.srvVirtName; |
8792 | 0 | if (ssl3_ServerNameCompare(name, cwsName)) { |
8793 | 0 | ssl_ReleaseSpecWriteLock(ss); /******************/ |
8794 | 0 | errCode = SSL_ERROR_UNRECOGNIZED_NAME_ALERT; |
8795 | 0 | desc = handshake_failure; |
8796 | 0 | ret = SSL_SNI_SEND_ALERT; |
8797 | 0 | break; |
8798 | 0 | } |
8799 | 0 | } |
8800 | 0 | pwsName = &ss->ssl3.hs.srvVirtName; |
8801 | 0 | if (pwsName->data) { |
8802 | 0 | SECITEM_FreeItem(pwsName, PR_FALSE); |
8803 | 0 | } |
8804 | 0 | rv = SECITEM_CopyItem(NULL, pwsName, name); |
8805 | 0 | ssl_ReleaseSpecWriteLock(ss); /***************************/ |
8806 | 0 | if (rv != SECSuccess) { |
8807 | 0 | errCode = SSL_ERROR_INTERNAL_ERROR_ALERT; |
8808 | 0 | desc = internal_error; |
8809 | 0 | ret = SSL_SNI_SEND_ALERT; |
8810 | 0 | break; |
8811 | 0 | } |
8812 | | /* Need to tell the client that application has picked |
8813 | | * the name from the offered list and reconfigured the socket. |
8814 | | */ |
8815 | 0 | ssl3_RegisterExtensionSender(ss, &ss->xtnData, ssl_server_name_xtn, |
8816 | 0 | ssl_SendEmptyExtension); |
8817 | 0 | } else { |
8818 | | /* Callback returned index outside of the boundary. */ |
8819 | 0 | PORT_Assert((unsigned int)ret < ss->xtnData.sniNameArrSize); |
8820 | 0 | errCode = SSL_ERROR_INTERNAL_ERROR_ALERT; |
8821 | 0 | desc = internal_error; |
8822 | 0 | ret = SSL_SNI_SEND_ALERT; |
8823 | 0 | break; |
8824 | 0 | } |
8825 | 0 | } while (0); |
8826 | 0 | ssl3_FreeSniNameArray(&ss->xtnData); |
8827 | 0 | if (ret <= SSL_SNI_SEND_ALERT) { |
8828 | | /* desc and errCode should be set. */ |
8829 | 0 | goto alert_loser; |
8830 | 0 | } |
8831 | | |
8832 | 0 | return SECSuccess; |
8833 | | |
8834 | 0 | alert_loser: |
8835 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
8836 | 0 | PORT_SetError(errCode); |
8837 | 0 | return SECFailure; |
8838 | 0 | } |
8839 | | |
8840 | | SECStatus |
8841 | | ssl3_SelectServerCert(sslSocket *ss) |
8842 | 0 | { |
8843 | 0 | const ssl3KEADef *kea_def = ss->ssl3.hs.kea_def; |
8844 | 0 | PRCList *cursor; |
8845 | 0 | SECStatus rv; |
8846 | | |
8847 | | /* If the client didn't include the supported groups extension, assume just |
8848 | | * P-256 support and disable all the other ECDHE groups. This also affects |
8849 | | * ECDHE group selection, but this function is called first. */ |
8850 | 0 | if (!ssl3_ExtensionNegotiated(ss, ssl_supported_groups_xtn)) { |
8851 | 0 | unsigned int i; |
8852 | 0 | for (i = 0; i < SSL_NAMED_GROUP_COUNT; ++i) { |
8853 | 0 | if (ss->namedGroupPreferences[i] && |
8854 | 0 | ss->namedGroupPreferences[i]->keaType == ssl_kea_ecdh && |
8855 | 0 | ss->namedGroupPreferences[i]->name != ssl_grp_ec_secp256r1) { |
8856 | 0 | ss->namedGroupPreferences[i] = NULL; |
8857 | 0 | } |
8858 | 0 | } |
8859 | 0 | } |
8860 | | |
8861 | | /* This picks the first certificate that has: |
8862 | | * a) the right authentication method, and |
8863 | | * b) the right named curve (EC only) |
8864 | | * |
8865 | | * We might want to do some sort of ranking here later. For now, it's all |
8866 | | * based on what order they are configured in. */ |
8867 | 0 | for (cursor = PR_NEXT_LINK(&ss->serverCerts); |
8868 | 0 | cursor != &ss->serverCerts; |
8869 | 0 | cursor = PR_NEXT_LINK(cursor)) { |
8870 | 0 | sslServerCert *cert = (sslServerCert *)cursor; |
8871 | 0 | if (kea_def->authKeyType == ssl_auth_rsa_sign) { |
8872 | | /* We consider PSS certificates here as well for TLS 1.2. */ |
8873 | 0 | if (!SSL_CERT_IS(cert, ssl_auth_rsa_sign) && |
8874 | 0 | (!SSL_CERT_IS(cert, ssl_auth_rsa_pss) || |
8875 | 0 | ss->version < SSL_LIBRARY_VERSION_TLS_1_2)) { |
8876 | 0 | continue; |
8877 | 0 | } |
8878 | 0 | } else { |
8879 | 0 | if (!SSL_CERT_IS(cert, kea_def->authKeyType)) { |
8880 | 0 | continue; |
8881 | 0 | } |
8882 | 0 | if (SSL_CERT_IS_EC(cert) && |
8883 | 0 | !ssl_NamedGroupEnabled(ss, cert->namedCurve)) { |
8884 | 0 | continue; |
8885 | 0 | } |
8886 | 0 | } |
8887 | | |
8888 | | /* Found one. */ |
8889 | 0 | ss->sec.serverCert = cert; |
8890 | 0 | ss->sec.authKeyBits = cert->serverKeyBits; |
8891 | | |
8892 | | /* Don't pick a signature scheme if we aren't going to use it. */ |
8893 | 0 | if (kea_def->signKeyType == nullKey) { |
8894 | 0 | ss->sec.authType = kea_def->authKeyType; |
8895 | 0 | return SECSuccess; |
8896 | 0 | } |
8897 | | |
8898 | 0 | rv = ssl3_PickServerSignatureScheme(ss); |
8899 | 0 | if (rv != SECSuccess) { |
8900 | 0 | return SECFailure; |
8901 | 0 | } |
8902 | 0 | ss->sec.authType = |
8903 | 0 | ssl_SignatureSchemeToAuthType(ss->ssl3.hs.signatureScheme); |
8904 | 0 | return SECSuccess; |
8905 | 0 | } |
8906 | | |
8907 | 0 | PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP); |
8908 | 0 | return SECFailure; |
8909 | 0 | } |
8910 | | |
8911 | | static SECStatus |
8912 | | ssl_GenerateServerRandom(sslSocket *ss) |
8913 | 0 | { |
8914 | 0 | SECStatus rv; |
8915 | 0 | PRUint8 *downgradeSentinel; |
8916 | |
|
8917 | 0 | rv = ssl3_GetNewRandom(ss->ssl3.hs.server_random); |
8918 | 0 | if (rv != SECSuccess) { |
8919 | 0 | return SECFailure; |
8920 | 0 | } |
8921 | | |
8922 | 0 | if (ss->version == ss->vrange.max) { |
8923 | 0 | return SECSuccess; |
8924 | 0 | } |
8925 | | |
8926 | | /* |
8927 | | * [RFC 8446 Section 4.1.3]. |
8928 | | * |
8929 | | * TLS 1.3 servers which negotiate TLS 1.2 or below in response to a |
8930 | | * ClientHello MUST set the last 8 bytes of their Random value specially in |
8931 | | * their ServerHello. |
8932 | | * |
8933 | | * If negotiating TLS 1.2, TLS 1.3 servers MUST set the last 8 bytes of |
8934 | | * their Random value to the bytes: |
8935 | | * |
8936 | | * 44 4F 57 4E 47 52 44 01 |
8937 | | * |
8938 | | * If negotiating TLS 1.1 or below, TLS 1.3 servers MUST, and TLS 1.2 |
8939 | | * servers SHOULD, set the last 8 bytes of their ServerHello.Random value to |
8940 | | * the bytes: |
8941 | | * |
8942 | | * 44 4F 57 4E 47 52 44 00 |
8943 | | */ |
8944 | 0 | downgradeSentinel = |
8945 | 0 | ss->ssl3.hs.server_random + |
8946 | 0 | SSL3_RANDOM_LENGTH - sizeof(tls12_downgrade_random); |
8947 | 0 | if (ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_2) { |
8948 | 0 | switch (ss->version) { |
8949 | 0 | case SSL_LIBRARY_VERSION_TLS_1_2: |
8950 | | /* vrange.max > 1.2, since we didn't early exit above. */ |
8951 | 0 | PORT_Memcpy(downgradeSentinel, |
8952 | 0 | tls12_downgrade_random, sizeof(tls12_downgrade_random)); |
8953 | 0 | break; |
8954 | 0 | case SSL_LIBRARY_VERSION_TLS_1_1: |
8955 | 0 | case SSL_LIBRARY_VERSION_TLS_1_0: |
8956 | 0 | PORT_Memcpy(downgradeSentinel, |
8957 | 0 | tls1_downgrade_random, sizeof(tls1_downgrade_random)); |
8958 | 0 | break; |
8959 | 0 | default: |
8960 | | /* Do not change random. */ |
8961 | 0 | break; |
8962 | 0 | } |
8963 | 0 | } |
8964 | | |
8965 | 0 | return SECSuccess; |
8966 | 0 | } |
8967 | | |
8968 | | SECStatus |
8969 | | ssl3_HandleClientHelloPreamble(sslSocket *ss, PRUint8 **b, PRUint32 *length, SECItem *sidBytes, |
8970 | | SECItem *cookieBytes, SECItem *suites, SECItem *comps) |
8971 | 0 | { |
8972 | 0 | SECStatus rv; |
8973 | 0 | PRUint32 tmp; |
8974 | 0 | rv = ssl3_ConsumeHandshakeNumber(ss, &tmp, 2, b, length); |
8975 | 0 | if (rv != SECSuccess) { |
8976 | 0 | return SECFailure; /* malformed, alert already sent */ |
8977 | 0 | } |
8978 | | |
8979 | | /* Translate the version. */ |
8980 | 0 | if (IS_DTLS(ss)) { |
8981 | 0 | ss->clientHelloVersion = dtls_DTLSVersionToTLSVersion((SSL3ProtocolVersion)tmp); |
8982 | 0 | } else { |
8983 | 0 | ss->clientHelloVersion = (SSL3ProtocolVersion)tmp; |
8984 | 0 | } |
8985 | | |
8986 | | /* Grab the client random data. */ |
8987 | 0 | rv = ssl3_ConsumeHandshake( |
8988 | 0 | ss, ss->ssl3.hs.client_random, SSL3_RANDOM_LENGTH, b, length); |
8989 | 0 | if (rv != SECSuccess) { |
8990 | 0 | return SECFailure; /* malformed */ |
8991 | 0 | } |
8992 | | |
8993 | | /* Grab the client's SID, if present. */ |
8994 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, sidBytes, 1, b, length); |
8995 | | /* Check that the SID has the format: opaque legacy_session_id<0..32>, as |
8996 | | * specified in RFC8446, Section 4.1.2. */ |
8997 | 0 | if (rv != SECSuccess || sidBytes->len > SSL3_SESSIONID_BYTES) { |
8998 | 0 | return SECFailure; /* malformed */ |
8999 | 0 | } |
9000 | | |
9001 | | /* Grab the client's cookie, if present. It is checked after version negotiation. */ |
9002 | 0 | if (IS_DTLS(ss)) { |
9003 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, cookieBytes, 1, b, length); |
9004 | 0 | if (rv != SECSuccess) { |
9005 | 0 | return SECFailure; /* malformed */ |
9006 | 0 | } |
9007 | 0 | } |
9008 | | |
9009 | | /* Grab the list of cipher suites. */ |
9010 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, suites, 2, b, length); |
9011 | 0 | if (rv != SECSuccess) { |
9012 | 0 | return SECFailure; /* malformed */ |
9013 | 0 | } |
9014 | | |
9015 | | /* Grab the list of compression methods. */ |
9016 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, comps, 1, b, length); |
9017 | 0 | if (rv != SECSuccess) { |
9018 | 0 | return SECFailure; /* malformed */ |
9019 | 0 | } |
9020 | 0 | return SECSuccess; |
9021 | 0 | } |
9022 | | |
9023 | | static SECStatus |
9024 | | ssl3_ValidatePreambleWithVersion(sslSocket *ss, const SECItem *sidBytes, const SECItem *comps, |
9025 | | const SECItem *cookieBytes) |
9026 | 0 | { |
9027 | 0 | SECStatus rv; |
9028 | 0 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
9029 | 0 | if (sidBytes->len > 0 && !IS_DTLS(ss)) { |
9030 | 0 | SECITEM_FreeItem(&ss->ssl3.hs.fakeSid, PR_FALSE); |
9031 | 0 | rv = SECITEM_CopyItem(NULL, &ss->ssl3.hs.fakeSid, sidBytes); |
9032 | 0 | if (rv != SECSuccess) { |
9033 | 0 | FATAL_ERROR(ss, PORT_GetError(), internal_error); |
9034 | 0 | return SECFailure; |
9035 | 0 | } |
9036 | 0 | } |
9037 | | |
9038 | | /* TLS 1.3 requires that compression include only null. */ |
9039 | 0 | if (comps->len != 1 || comps->data[0] != ssl_compression_null) { |
9040 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CLIENT_HELLO, illegal_parameter); |
9041 | 0 | return SECFailure; |
9042 | 0 | } |
9043 | | |
9044 | | /* receivedCcs is only valid if we sent an HRR. */ |
9045 | 0 | if (ss->ssl3.hs.receivedCcs && !ss->ssl3.hs.helloRetry) { |
9046 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_UNEXPECTED_CHANGE_CIPHER, unexpected_message); |
9047 | 0 | return SECFailure; |
9048 | 0 | } |
9049 | | |
9050 | | /* A DTLS 1.3-only client MUST set the legacy_cookie field to zero length. |
9051 | | * If a DTLS 1.3 ClientHello is received with any other value in this field, |
9052 | | * the server MUST abort the handshake with an "illegal_parameter" alert. */ |
9053 | 0 | if (IS_DTLS(ss) && cookieBytes->len != 0) { |
9054 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CLIENT_HELLO, illegal_parameter); |
9055 | 0 | return SECFailure; |
9056 | 0 | } |
9057 | 0 | } else { |
9058 | | /* ECH not possible here. */ |
9059 | 0 | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_ech; |
9060 | | |
9061 | | /* HRR and ECH are TLS1.3-only. We ignore the Cookie extension here. */ |
9062 | 0 | if (ss->ssl3.hs.helloRetry) { |
9063 | 0 | FATAL_ERROR(ss, SSL_ERROR_UNSUPPORTED_VERSION, protocol_version); |
9064 | 0 | return SECFailure; |
9065 | 0 | } |
9066 | | |
9067 | | /* receivedCcs is only valid if we sent an HRR. */ |
9068 | 0 | if (ss->ssl3.hs.receivedCcs) { |
9069 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_UNEXPECTED_CHANGE_CIPHER, unexpected_message); |
9070 | 0 | return SECFailure; |
9071 | 0 | } |
9072 | | |
9073 | | /* TLS versions prior to 1.3 must include null somewhere. */ |
9074 | 0 | if (comps->len < 1 || |
9075 | 0 | !memchr(comps->data, ssl_compression_null, comps->len)) { |
9076 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CLIENT_HELLO, illegal_parameter); |
9077 | 0 | return SECFailure; |
9078 | 0 | } |
9079 | | |
9080 | | /* We never send cookies in DTLS 1.2. */ |
9081 | 0 | if (IS_DTLS(ss) && cookieBytes->len != 0) { |
9082 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CLIENT_HELLO, illegal_parameter); |
9083 | 0 | return SECFailure; |
9084 | 0 | } |
9085 | 0 | } |
9086 | | |
9087 | 0 | return SECSuccess; |
9088 | 0 | } |
9089 | | |
9090 | | /* Called from ssl3_HandleHandshakeMessage() when it has deciphered a complete |
9091 | | * ssl3 Client Hello message. |
9092 | | * Caller must hold Handshake and RecvBuf locks. |
9093 | | */ |
9094 | | static SECStatus |
9095 | | ssl3_HandleClientHello(sslSocket *ss, PRUint8 *b, PRUint32 length) |
9096 | 0 | { |
9097 | 0 | sslSessionID *sid = NULL; |
9098 | 0 | unsigned int i; |
9099 | 0 | SECStatus rv; |
9100 | 0 | PRUint32 extensionLength; |
9101 | 0 | int errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; |
9102 | 0 | SSL3AlertDescription desc = illegal_parameter; |
9103 | 0 | SSL3AlertLevel level = alert_fatal; |
9104 | 0 | TLSExtension *versionExtension; |
9105 | 0 | SECItem sidBytes = { siBuffer, NULL, 0 }; |
9106 | 0 | SECItem cookieBytes = { siBuffer, NULL, 0 }; |
9107 | 0 | SECItem suites = { siBuffer, NULL, 0 }; |
9108 | 0 | SECItem comps = { siBuffer, NULL, 0 }; |
9109 | 0 | SECItem *echInner = NULL; |
9110 | 0 | PRBool isTLS13; |
9111 | 0 | const PRUint8 *savedMsg = b; |
9112 | 0 | const PRUint32 savedLen = length; |
9113 | |
|
9114 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: handle client_hello handshake", |
9115 | 0 | SSL_GETPID(), ss->fd)); |
9116 | |
|
9117 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
9118 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
9119 | 0 | ss->ssl3.hs.preliminaryInfo = 0; |
9120 | |
|
9121 | 0 | if (!ss->sec.isServer || |
9122 | 0 | (ss->ssl3.hs.ws != wait_client_hello && |
9123 | 0 | ss->ssl3.hs.ws != idle_handshake)) { |
9124 | 0 | desc = unexpected_message; |
9125 | 0 | errCode = SSL_ERROR_RX_UNEXPECTED_CLIENT_HELLO; |
9126 | 0 | goto alert_loser; |
9127 | 0 | } |
9128 | 0 | if (ss->ssl3.hs.ws == idle_handshake) { |
9129 | | /* Refuse re-handshake when we have already negotiated TLS 1.3. */ |
9130 | 0 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
9131 | 0 | desc = unexpected_message; |
9132 | 0 | errCode = SSL_ERROR_RENEGOTIATION_NOT_ALLOWED; |
9133 | 0 | goto alert_loser; |
9134 | 0 | } |
9135 | 0 | if (ss->opt.enableRenegotiation == SSL_RENEGOTIATE_NEVER) { |
9136 | 0 | desc = no_renegotiation; |
9137 | 0 | level = alert_warning; |
9138 | 0 | errCode = SSL_ERROR_RENEGOTIATION_NOT_ALLOWED; |
9139 | 0 | goto alert_loser; |
9140 | 0 | } |
9141 | 0 | } |
9142 | | |
9143 | | /* We should always be in a fresh state. */ |
9144 | 0 | SSL_ASSERT_HASHES_EMPTY(ss); |
9145 | | |
9146 | | /* Get peer name of client */ |
9147 | 0 | rv = ssl_GetPeerInfo(ss); |
9148 | 0 | if (rv != SECSuccess) { |
9149 | 0 | return rv; /* error code is set. */ |
9150 | 0 | } |
9151 | | |
9152 | | /* We might be starting session renegotiation in which case we should |
9153 | | * clear previous state. |
9154 | | */ |
9155 | 0 | ssl3_ResetExtensionData(&ss->xtnData, ss); |
9156 | 0 | ss->statelessResume = PR_FALSE; |
9157 | |
|
9158 | 0 | if (IS_DTLS(ss)) { |
9159 | 0 | dtls_RehandshakeCleanup(ss); |
9160 | 0 | } |
9161 | |
|
9162 | 0 | rv = ssl3_HandleClientHelloPreamble(ss, &b, &length, &sidBytes, |
9163 | 0 | &cookieBytes, &suites, &comps); |
9164 | 0 | if (rv != SECSuccess) { |
9165 | 0 | goto loser; /* malformed */ |
9166 | 0 | } |
9167 | | |
9168 | | /* Handle TLS hello extensions for SSL3 & TLS. We do not know if |
9169 | | * we are restarting a previous session until extensions have been |
9170 | | * parsed, since we might have received a SessionTicket extension. |
9171 | | * Note: we allow extensions even when negotiating SSL3 for the sake |
9172 | | * of interoperability (and backwards compatibility). |
9173 | | */ |
9174 | 0 | if (length) { |
9175 | | /* Get length of hello extensions */ |
9176 | 0 | rv = ssl3_ConsumeHandshakeNumber(ss, &extensionLength, 2, &b, &length); |
9177 | 0 | if (rv != SECSuccess) { |
9178 | 0 | goto loser; /* alert already sent */ |
9179 | 0 | } |
9180 | 0 | if (extensionLength != length) { |
9181 | 0 | errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; |
9182 | 0 | desc = decode_error; |
9183 | 0 | goto alert_loser; |
9184 | 0 | } |
9185 | | |
9186 | 0 | rv = ssl3_ParseExtensions(ss, &b, &length); |
9187 | 0 | if (rv != SECSuccess) { |
9188 | 0 | goto loser; /* malformed */ |
9189 | 0 | } |
9190 | 0 | } |
9191 | | |
9192 | 0 | versionExtension = ssl3_FindExtension(ss, ssl_tls13_supported_versions_xtn); |
9193 | 0 | if (versionExtension) { |
9194 | 0 | rv = tls13_NegotiateVersion(ss, versionExtension); |
9195 | 0 | if (rv != SECSuccess) { |
9196 | 0 | errCode = PORT_GetError(); |
9197 | 0 | desc = (errCode == SSL_ERROR_UNSUPPORTED_VERSION) ? protocol_version : illegal_parameter; |
9198 | 0 | goto alert_loser; |
9199 | 0 | } |
9200 | 0 | } else { |
9201 | | /* The PR_MIN here ensures that we never negotiate 1.3 if the |
9202 | | * peer didn't offer "supported_versions". */ |
9203 | 0 | rv = ssl3_NegotiateVersion(ss, |
9204 | 0 | PR_MIN(ss->clientHelloVersion, |
9205 | 0 | SSL_LIBRARY_VERSION_TLS_1_2), |
9206 | 0 | PR_TRUE); |
9207 | | /* Send protocol version alert if the ClientHello.legacy_version is not |
9208 | | * supported by the server. |
9209 | | * |
9210 | | * If the "supported_versions" extension is absent and the server only |
9211 | | * supports versions greater than ClientHello.legacy_version, the |
9212 | | * server MUST abort the handshake with a "protocol_version" alert |
9213 | | * [RFC8446, Appendix D.2]. */ |
9214 | 0 | if (rv != SECSuccess) { |
9215 | 0 | desc = protocol_version; |
9216 | 0 | errCode = SSL_ERROR_UNSUPPORTED_VERSION; |
9217 | 0 | goto alert_loser; |
9218 | 0 | } |
9219 | 0 | } |
9220 | 0 | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_version; |
9221 | | |
9222 | | /* Update the write spec to match the selected version. */ |
9223 | 0 | if (!ss->firstHsDone) { |
9224 | 0 | ssl_GetSpecWriteLock(ss); |
9225 | 0 | ssl_SetSpecVersions(ss, ss->ssl3.cwSpec); |
9226 | 0 | ssl_ReleaseSpecWriteLock(ss); |
9227 | 0 | } |
9228 | |
|
9229 | 0 | isTLS13 = ss->version >= SSL_LIBRARY_VERSION_TLS_1_3; |
9230 | 0 | if (isTLS13) { |
9231 | 0 | if (ss->firstHsDone) { |
9232 | 0 | desc = unexpected_message; |
9233 | 0 | errCode = SSL_ERROR_RENEGOTIATION_NOT_ALLOWED; |
9234 | 0 | goto alert_loser; |
9235 | 0 | } |
9236 | | |
9237 | | /* If there is a cookie, then this is a second ClientHello (TLS 1.3). */ |
9238 | 0 | if (ssl3_FindExtension(ss, ssl_tls13_cookie_xtn)) { |
9239 | 0 | ss->ssl3.hs.helloRetry = PR_TRUE; |
9240 | 0 | } |
9241 | |
|
9242 | 0 | rv = tls13_MaybeHandleEch(ss, savedMsg, savedLen, &sidBytes, |
9243 | 0 | &comps, &cookieBytes, &suites, &echInner); |
9244 | 0 | if (rv != SECSuccess) { |
9245 | 0 | errCode = PORT_GetError(); |
9246 | 0 | goto loser; /* code set, alert sent. */ |
9247 | 0 | } |
9248 | 0 | } |
9249 | | |
9250 | 0 | rv = ssl3_ValidatePreambleWithVersion(ss, &sidBytes, &comps, &cookieBytes); |
9251 | 0 | if (rv != SECSuccess) { |
9252 | 0 | errCode = PORT_GetError(); |
9253 | 0 | goto loser; /* code set, alert sent. */ |
9254 | 0 | } |
9255 | | |
9256 | | /* Now parse the rest of the extensions. */ |
9257 | 0 | rv = ssl3_HandleParsedExtensions(ss, ssl_hs_client_hello); |
9258 | 0 | ssl3_DestroyRemoteExtensions(&ss->ssl3.hs.remoteExtensions); |
9259 | 0 | if (rv != SECSuccess) { |
9260 | 0 | if (PORT_GetError() == SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM) { |
9261 | 0 | errCode = SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM; |
9262 | 0 | } |
9263 | 0 | goto loser; /* malformed */ |
9264 | 0 | } |
9265 | | |
9266 | | /* If the ClientHello version is less than our maximum version, check for a |
9267 | | * TLS_FALLBACK_SCSV and reject the connection if found. */ |
9268 | 0 | if (ss->vrange.max > ss->version) { |
9269 | 0 | for (i = 0; i + 1 < suites.len; i += 2) { |
9270 | 0 | PRUint16 suite_i = (suites.data[i] << 8) | suites.data[i + 1]; |
9271 | 0 | if (suite_i != TLS_FALLBACK_SCSV) |
9272 | 0 | continue; |
9273 | 0 | desc = inappropriate_fallback; |
9274 | 0 | errCode = SSL_ERROR_INAPPROPRIATE_FALLBACK_ALERT; |
9275 | 0 | goto alert_loser; |
9276 | 0 | } |
9277 | 0 | } |
9278 | | |
9279 | 0 | if (!ssl3_ExtensionNegotiated(ss, ssl_renegotiation_info_xtn)) { |
9280 | | /* If we didn't receive an RI extension, look for the SCSV, |
9281 | | * and if found, treat it just like an empty RI extension |
9282 | | * by processing a local copy of an empty RI extension. |
9283 | | */ |
9284 | 0 | for (i = 0; i + 1 < suites.len; i += 2) { |
9285 | 0 | PRUint16 suite_i = (suites.data[i] << 8) | suites.data[i + 1]; |
9286 | 0 | if (suite_i == TLS_EMPTY_RENEGOTIATION_INFO_SCSV) { |
9287 | 0 | PRUint8 *b2 = (PRUint8 *)emptyRIext; |
9288 | 0 | PRUint32 L2 = sizeof emptyRIext; |
9289 | 0 | (void)ssl3_HandleExtensions(ss, &b2, &L2, ssl_hs_client_hello); |
9290 | 0 | break; |
9291 | 0 | } |
9292 | 0 | } |
9293 | 0 | } |
9294 | | |
9295 | | /* The check for renegotiation in TLS 1.3 is earlier. */ |
9296 | 0 | if (!isTLS13) { |
9297 | 0 | if (ss->firstHsDone && |
9298 | 0 | (ss->opt.enableRenegotiation == SSL_RENEGOTIATE_REQUIRES_XTN || |
9299 | 0 | ss->opt.enableRenegotiation == SSL_RENEGOTIATE_TRANSITIONAL) && |
9300 | 0 | !ssl3_ExtensionNegotiated(ss, ssl_renegotiation_info_xtn)) { |
9301 | 0 | desc = no_renegotiation; |
9302 | 0 | level = alert_warning; |
9303 | 0 | errCode = SSL_ERROR_RENEGOTIATION_NOT_ALLOWED; |
9304 | 0 | goto alert_loser; |
9305 | 0 | } |
9306 | 0 | if ((ss->opt.requireSafeNegotiation || |
9307 | 0 | (ss->firstHsDone && ss->peerRequestedProtection)) && |
9308 | 0 | !ssl3_ExtensionNegotiated(ss, ssl_renegotiation_info_xtn)) { |
9309 | 0 | desc = handshake_failure; |
9310 | 0 | errCode = SSL_ERROR_UNSAFE_NEGOTIATION; |
9311 | 0 | goto alert_loser; |
9312 | 0 | } |
9313 | 0 | } |
9314 | | |
9315 | | /* We do stateful resumes only if we are in TLS < 1.3 and |
9316 | | * either of the following conditions are satisfied: |
9317 | | * (1) the client does not support the session ticket extension, or |
9318 | | * (2) the client support the session ticket extension, but sent an |
9319 | | * empty ticket. |
9320 | | */ |
9321 | 0 | if (!isTLS13 && |
9322 | 0 | (!ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn) || |
9323 | 0 | ss->xtnData.emptySessionTicket)) { |
9324 | 0 | if (sidBytes.len > 0 && !ss->opt.noCache) { |
9325 | 0 | SSL_TRC(7, ("%d: SSL3[%d]: server, lookup client session-id for 0x%08x%08x%08x%08x", |
9326 | 0 | SSL_GETPID(), ss->fd, ss->sec.ci.peer.pr_s6_addr32[0], |
9327 | 0 | ss->sec.ci.peer.pr_s6_addr32[1], |
9328 | 0 | ss->sec.ci.peer.pr_s6_addr32[2], |
9329 | 0 | ss->sec.ci.peer.pr_s6_addr32[3])); |
9330 | 0 | if (ssl_sid_lookup) { |
9331 | 0 | sid = (*ssl_sid_lookup)(ssl_Time(ss), &ss->sec.ci.peer, |
9332 | 0 | sidBytes.data, sidBytes.len, ss->dbHandle); |
9333 | 0 | } else { |
9334 | 0 | errCode = SSL_ERROR_SERVER_CACHE_NOT_CONFIGURED; |
9335 | 0 | goto loser; |
9336 | 0 | } |
9337 | 0 | } |
9338 | 0 | } else if (ss->statelessResume) { |
9339 | | /* Fill in the client's session ID if doing a stateless resume. |
9340 | | * (When doing stateless resumes, server echos client's SessionID.) |
9341 | | * This branch also handles TLS 1.3 resumption-PSK. |
9342 | | */ |
9343 | 0 | sid = ss->sec.ci.sid; |
9344 | 0 | PORT_Assert(sid != NULL); /* Should have already been filled in.*/ |
9345 | |
|
9346 | 0 | if (sidBytes.len > 0 && sidBytes.len <= SSL3_SESSIONID_BYTES) { |
9347 | 0 | sid->u.ssl3.sessionIDLength = sidBytes.len; |
9348 | 0 | PORT_Memcpy(sid->u.ssl3.sessionID, sidBytes.data, |
9349 | 0 | sidBytes.len); |
9350 | 0 | sid->u.ssl3.sessionIDLength = sidBytes.len; |
9351 | 0 | } else { |
9352 | 0 | sid->u.ssl3.sessionIDLength = 0; |
9353 | 0 | } |
9354 | 0 | ss->sec.ci.sid = NULL; |
9355 | 0 | } |
9356 | | |
9357 | | /* Free a potentially leftover session ID from a previous handshake. */ |
9358 | 0 | if (ss->sec.ci.sid) { |
9359 | 0 | ssl_FreeSID(ss->sec.ci.sid); |
9360 | 0 | ss->sec.ci.sid = NULL; |
9361 | 0 | } |
9362 | |
|
9363 | 0 | if (sid != NULL) { |
9364 | | /* We've found a session cache entry for this client. |
9365 | | * Now, if we're going to require a client-auth cert, |
9366 | | * and we don't already have this client's cert in the session cache, |
9367 | | * and this is the first handshake on this connection (not a redo), |
9368 | | * then drop this old cache entry and start a new session. |
9369 | | */ |
9370 | 0 | if ((sid->peerCert == NULL) && ss->opt.requestCertificate && |
9371 | 0 | ((ss->opt.requireCertificate == SSL_REQUIRE_ALWAYS) || |
9372 | 0 | (ss->opt.requireCertificate == SSL_REQUIRE_NO_ERROR) || |
9373 | 0 | ((ss->opt.requireCertificate == SSL_REQUIRE_FIRST_HANDSHAKE) && |
9374 | 0 | !ss->firstHsDone))) { |
9375 | |
|
9376 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hch_sid_cache_not_ok); |
9377 | 0 | ssl_FreeSID(sid); |
9378 | 0 | sid = NULL; |
9379 | 0 | ss->statelessResume = PR_FALSE; |
9380 | 0 | } |
9381 | 0 | } |
9382 | |
|
9383 | 0 | if (IS_DTLS(ss)) { |
9384 | 0 | ssl3_DisableNonDTLSSuites(ss); |
9385 | 0 | dtls_ReceivedFirstMessageInFlight(ss); |
9386 | 0 | } |
9387 | |
|
9388 | 0 | if (isTLS13) { |
9389 | 0 | rv = tls13_HandleClientHelloPart2(ss, &suites, sid, |
9390 | 0 | ss->ssl3.hs.echAccepted ? echInner->data : savedMsg, |
9391 | 0 | ss->ssl3.hs.echAccepted ? echInner->len : savedLen); |
9392 | 0 | SECITEM_FreeItem(echInner, PR_TRUE); |
9393 | 0 | echInner = NULL; |
9394 | 0 | } else { |
9395 | 0 | rv = ssl3_HandleClientHelloPart2(ss, &suites, sid, |
9396 | 0 | savedMsg, savedLen); |
9397 | 0 | } |
9398 | 0 | if (rv != SECSuccess) { |
9399 | 0 | errCode = PORT_GetError(); |
9400 | 0 | goto loser; |
9401 | 0 | } |
9402 | 0 | return SECSuccess; |
9403 | | |
9404 | 0 | alert_loser: |
9405 | 0 | (void)SSL3_SendAlert(ss, level, desc); |
9406 | | /* FALLTHRU */ |
9407 | 0 | loser: |
9408 | 0 | SECITEM_FreeItem(echInner, PR_TRUE); |
9409 | 0 | PORT_SetError(errCode); |
9410 | 0 | return SECFailure; |
9411 | 0 | } |
9412 | | |
9413 | | /* unwrap helper function to handle the case where the wrapKey doesn't wind |
9414 | | * up in the correct token for the master secret */ |
9415 | | PK11SymKey * |
9416 | | ssl_unwrapSymKey(PK11SymKey *wrapKey, |
9417 | | CK_MECHANISM_TYPE wrapType, SECItem *param, |
9418 | | SECItem *wrappedKey, |
9419 | | CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, |
9420 | | int keySize, CK_FLAGS keyFlags, void *pinArg) |
9421 | 0 | { |
9422 | 0 | PK11SymKey *unwrappedKey; |
9423 | | |
9424 | | /* unwrap the master secret. */ |
9425 | 0 | unwrappedKey = PK11_UnwrapSymKeyWithFlags(wrapKey, wrapType, param, |
9426 | 0 | wrappedKey, target, operation, keySize, |
9427 | 0 | keyFlags); |
9428 | 0 | if (!unwrappedKey) { |
9429 | 0 | PK11SlotInfo *targetSlot = PK11_GetBestSlot(target, pinArg); |
9430 | 0 | PK11SymKey *newWrapKey; |
9431 | | |
9432 | | /* it's possible that we failed to unwrap because the wrapKey is in |
9433 | | * a slot that can't handle target. Move the wrapKey to a slot that |
9434 | | * can handle this mechanism and retry the operation */ |
9435 | 0 | if (targetSlot == NULL) { |
9436 | 0 | return NULL; |
9437 | 0 | } |
9438 | 0 | newWrapKey = PK11_MoveSymKey(targetSlot, CKA_UNWRAP, 0, |
9439 | 0 | PR_FALSE, wrapKey); |
9440 | 0 | PK11_FreeSlot(targetSlot); |
9441 | 0 | if (newWrapKey == NULL) { |
9442 | 0 | return NULL; |
9443 | 0 | } |
9444 | 0 | unwrappedKey = PK11_UnwrapSymKeyWithFlags(newWrapKey, wrapType, param, |
9445 | 0 | wrappedKey, target, operation, keySize, |
9446 | 0 | keyFlags); |
9447 | 0 | PK11_FreeSymKey(newWrapKey); |
9448 | 0 | } |
9449 | 0 | return unwrappedKey; |
9450 | 0 | } |
9451 | | |
9452 | | static SECStatus |
9453 | | ssl3_UnwrapMasterSecretServer(sslSocket *ss, sslSessionID *sid, PK11SymKey **ms) |
9454 | 0 | { |
9455 | 0 | PK11SymKey *wrapKey; |
9456 | 0 | CK_FLAGS keyFlags = 0; |
9457 | 0 | SECItem wrappedMS = { |
9458 | 0 | siBuffer, |
9459 | 0 | sid->u.ssl3.keys.wrapped_master_secret, |
9460 | 0 | sid->u.ssl3.keys.wrapped_master_secret_len |
9461 | 0 | }; |
9462 | |
|
9463 | 0 | wrapKey = ssl3_GetWrappingKey(ss, NULL, sid->u.ssl3.masterWrapMech, |
9464 | 0 | ss->pkcs11PinArg); |
9465 | 0 | if (!wrapKey) { |
9466 | 0 | return SECFailure; |
9467 | 0 | } |
9468 | | |
9469 | 0 | if (ss->version > SSL_LIBRARY_VERSION_3_0) { /* isTLS */ |
9470 | 0 | keyFlags = CKF_SIGN | CKF_VERIFY; |
9471 | 0 | } |
9472 | |
|
9473 | 0 | *ms = ssl_unwrapSymKey(wrapKey, sid->u.ssl3.masterWrapMech, NULL, |
9474 | 0 | &wrappedMS, CKM_SSL3_MASTER_KEY_DERIVE, |
9475 | 0 | CKA_DERIVE, SSL3_MASTER_SECRET_LENGTH, |
9476 | 0 | keyFlags, ss->pkcs11PinArg); |
9477 | 0 | PK11_FreeSymKey(wrapKey); |
9478 | 0 | if (!*ms) { |
9479 | 0 | SSL_TRC(10, ("%d: SSL3[%d]: server wrapping key found, but couldn't unwrap MasterSecret. wrapMech=0x%0lx", |
9480 | 0 | SSL_GETPID(), ss->fd, sid->u.ssl3.masterWrapMech)); |
9481 | 0 | return SECFailure; |
9482 | 0 | } |
9483 | 0 | return SECSuccess; |
9484 | 0 | } |
9485 | | |
9486 | | static SECStatus |
9487 | | ssl3_HandleClientHelloPart2(sslSocket *ss, |
9488 | | SECItem *suites, |
9489 | | sslSessionID *sid, |
9490 | | const PRUint8 *msg, |
9491 | | unsigned int len) |
9492 | 0 | { |
9493 | 0 | PRBool haveXmitBufLock = PR_FALSE; |
9494 | 0 | int errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; |
9495 | 0 | SSL3AlertDescription desc = illegal_parameter; |
9496 | 0 | SECStatus rv; |
9497 | 0 | unsigned int i; |
9498 | 0 | unsigned int j; |
9499 | |
|
9500 | 0 | rv = ssl_HashHandshakeMessage(ss, ssl_hs_client_hello, msg, len); |
9501 | 0 | if (rv != SECSuccess) { |
9502 | 0 | errCode = SEC_ERROR_LIBRARY_FAILURE; |
9503 | 0 | desc = internal_error; |
9504 | 0 | goto alert_loser; |
9505 | 0 | } |
9506 | | |
9507 | | /* If we already have a session for this client, be sure to pick the same |
9508 | | ** cipher suite we picked before. This is not a loop, despite appearances. |
9509 | | */ |
9510 | 0 | if (sid) |
9511 | 0 | do { |
9512 | 0 | ssl3CipherSuiteCfg *suite; |
9513 | 0 | SSLVersionRange vrange = { ss->version, ss->version }; |
9514 | |
|
9515 | 0 | suite = ss->cipherSuites; |
9516 | | /* Find the entry for the cipher suite used in the cached session. */ |
9517 | 0 | for (j = ssl_V3_SUITES_IMPLEMENTED; j > 0; --j, ++suite) { |
9518 | 0 | if (suite->cipher_suite == sid->u.ssl3.cipherSuite) |
9519 | 0 | break; |
9520 | 0 | } |
9521 | |
|
9522 | 0 | if (j == 0) |
9523 | 0 | break; |
9524 | | |
9525 | | /* Double check that the cached cipher suite is still enabled, |
9526 | | * implemented, and allowed by policy. Might have been disabled. |
9527 | | */ |
9528 | 0 | if (ssl3_config_match_init(ss) == 0) { |
9529 | 0 | desc = handshake_failure; |
9530 | 0 | errCode = PORT_GetError(); |
9531 | 0 | goto alert_loser; |
9532 | 0 | } |
9533 | 0 | if (!ssl3_config_match(suite, ss->ssl3.policy, &vrange, ss)) |
9534 | 0 | break; |
9535 | | |
9536 | | /* Double check that the cached cipher suite is in the client's |
9537 | | * list. If it isn't, fall through and start a new session. */ |
9538 | 0 | for (i = 0; i + 1 < suites->len; i += 2) { |
9539 | 0 | PRUint16 suite_i = (suites->data[i] << 8) | suites->data[i + 1]; |
9540 | 0 | if (suite_i == suite->cipher_suite) { |
9541 | 0 | ss->ssl3.hs.cipher_suite = suite_i; |
9542 | 0 | rv = ssl3_SetupCipherSuite(ss, PR_TRUE); |
9543 | 0 | if (rv != SECSuccess) { |
9544 | 0 | desc = internal_error; |
9545 | 0 | errCode = PORT_GetError(); |
9546 | 0 | goto alert_loser; |
9547 | 0 | } |
9548 | | |
9549 | 0 | goto cipher_found; |
9550 | 0 | } |
9551 | 0 | } |
9552 | 0 | } while (0); |
9553 | | /* START A NEW SESSION */ |
9554 | | |
9555 | 0 | rv = ssl3_NegotiateCipherSuite(ss, suites, PR_TRUE); |
9556 | 0 | if (rv != SECSuccess) { |
9557 | 0 | desc = handshake_failure; |
9558 | 0 | errCode = PORT_GetError(); |
9559 | 0 | goto alert_loser; |
9560 | 0 | } |
9561 | | |
9562 | 0 | cipher_found: |
9563 | 0 | suites->data = NULL; |
9564 | | |
9565 | | /* If there are any failures while processing the old sid, |
9566 | | * we don't consider them to be errors. Instead, We just behave |
9567 | | * as if the client had sent us no sid to begin with, and make a new one. |
9568 | | * The exception here is attempts to resume extended_master_secret |
9569 | | * sessions without the extension, which causes an alert. |
9570 | | */ |
9571 | 0 | if (sid != NULL) |
9572 | 0 | do { |
9573 | 0 | PK11SymKey *masterSecret; |
9574 | |
|
9575 | 0 | if (sid->version != ss->version || |
9576 | 0 | sid->u.ssl3.cipherSuite != ss->ssl3.hs.cipher_suite) { |
9577 | 0 | break; /* not an error */ |
9578 | 0 | } |
9579 | | |
9580 | | /* server sids don't remember the server cert we previously sent, |
9581 | | ** but they do remember the slot we originally used, so we |
9582 | | ** can locate it again, provided that the current ssl socket |
9583 | | ** has had its server certs configured the same as the previous one. |
9584 | | */ |
9585 | 0 | ss->sec.serverCert = ssl_FindServerCert(ss, sid->authType, sid->namedCurve); |
9586 | 0 | if (!ss->sec.serverCert || !ss->sec.serverCert->serverCert) { |
9587 | | /* A compatible certificate must not have been configured. It |
9588 | | * might not be the same certificate, but we only find that out |
9589 | | * when the ticket fails to decrypt. */ |
9590 | 0 | break; |
9591 | 0 | } |
9592 | | |
9593 | | /* [draft-ietf-tls-session-hash-06; Section 5.3] |
9594 | | * o If the original session did not use the "extended_master_secret" |
9595 | | * extension but the new ClientHello contains the extension, then the |
9596 | | * server MUST NOT perform the abbreviated handshake. Instead, it |
9597 | | * SHOULD continue with a full handshake (as described in |
9598 | | * Section 5.2) to negotiate a new session. |
9599 | | * |
9600 | | * o If the original session used the "extended_master_secret" |
9601 | | * extension but the new ClientHello does not contain the extension, |
9602 | | * the server MUST abort the abbreviated handshake. |
9603 | | */ |
9604 | 0 | if (ssl3_ExtensionNegotiated(ss, ssl_extended_master_secret_xtn)) { |
9605 | 0 | if (!sid->u.ssl3.keys.extendedMasterSecretUsed) { |
9606 | 0 | break; /* not an error */ |
9607 | 0 | } |
9608 | 0 | } else { |
9609 | 0 | if (sid->u.ssl3.keys.extendedMasterSecretUsed) { |
9610 | | /* Note: we do not destroy the session */ |
9611 | 0 | desc = handshake_failure; |
9612 | 0 | errCode = SSL_ERROR_MISSING_EXTENDED_MASTER_SECRET; |
9613 | 0 | goto alert_loser; |
9614 | 0 | } |
9615 | 0 | } |
9616 | | |
9617 | 0 | if (ss->sec.ci.sid) { |
9618 | 0 | ssl_UncacheSessionID(ss); |
9619 | 0 | PORT_Assert(ss->sec.ci.sid != sid); /* should be impossible, but ... */ |
9620 | 0 | if (ss->sec.ci.sid != sid) { |
9621 | 0 | ssl_FreeSID(ss->sec.ci.sid); |
9622 | 0 | } |
9623 | 0 | ss->sec.ci.sid = NULL; |
9624 | 0 | } |
9625 | | |
9626 | | /* we need to resurrect the master secret.... */ |
9627 | 0 | rv = ssl3_UnwrapMasterSecretServer(ss, sid, &masterSecret); |
9628 | 0 | if (rv != SECSuccess) { |
9629 | 0 | break; /* not an error */ |
9630 | 0 | } |
9631 | | |
9632 | 0 | ss->sec.ci.sid = sid; |
9633 | 0 | if (sid->peerCert != NULL) { |
9634 | 0 | ss->sec.peerCert = CERT_DupCertificate(sid->peerCert); |
9635 | 0 | } |
9636 | | |
9637 | | /* |
9638 | | * Old SID passed all tests, so resume this old session. |
9639 | | */ |
9640 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hch_sid_cache_hits); |
9641 | 0 | if (ss->statelessResume) |
9642 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hch_sid_stateless_resumes); |
9643 | 0 | ss->ssl3.hs.isResuming = PR_TRUE; |
9644 | |
|
9645 | 0 | ss->sec.authType = sid->authType; |
9646 | 0 | ss->sec.authKeyBits = sid->authKeyBits; |
9647 | 0 | ss->sec.keaType = sid->keaType; |
9648 | 0 | ss->sec.keaKeyBits = sid->keaKeyBits; |
9649 | 0 | ss->sec.originalKeaGroup = ssl_LookupNamedGroup(sid->keaGroup); |
9650 | 0 | ss->sec.signatureScheme = sid->sigScheme; |
9651 | |
|
9652 | 0 | ss->sec.localCert = |
9653 | 0 | CERT_DupCertificate(ss->sec.serverCert->serverCert); |
9654 | | |
9655 | | /* Copy cached name in to pending spec */ |
9656 | 0 | if (sid != NULL && |
9657 | 0 | sid->version > SSL_LIBRARY_VERSION_3_0 && |
9658 | 0 | sid->u.ssl3.srvName.len && sid->u.ssl3.srvName.data) { |
9659 | | /* Set server name from sid */ |
9660 | 0 | SECItem *sidName = &sid->u.ssl3.srvName; |
9661 | 0 | SECItem *pwsName = &ss->ssl3.hs.srvVirtName; |
9662 | 0 | if (pwsName->data) { |
9663 | 0 | SECITEM_FreeItem(pwsName, PR_FALSE); |
9664 | 0 | } |
9665 | 0 | rv = SECITEM_CopyItem(NULL, pwsName, sidName); |
9666 | 0 | if (rv != SECSuccess) { |
9667 | 0 | errCode = PORT_GetError(); |
9668 | 0 | desc = internal_error; |
9669 | 0 | goto alert_loser; |
9670 | 0 | } |
9671 | 0 | } |
9672 | | |
9673 | | /* Clean up sni name array */ |
9674 | 0 | ssl3_FreeSniNameArray(&ss->xtnData); |
9675 | |
|
9676 | 0 | ssl_GetXmitBufLock(ss); |
9677 | 0 | haveXmitBufLock = PR_TRUE; |
9678 | |
|
9679 | 0 | rv = ssl3_SendServerHello(ss); |
9680 | 0 | if (rv != SECSuccess) { |
9681 | 0 | errCode = PORT_GetError(); |
9682 | 0 | goto loser; |
9683 | 0 | } |
9684 | | |
9685 | | /* We are re-using the old MS, so no need to derive again. */ |
9686 | 0 | rv = ssl3_InitPendingCipherSpecs(ss, masterSecret, PR_FALSE); |
9687 | 0 | if (rv != SECSuccess) { |
9688 | 0 | errCode = PORT_GetError(); |
9689 | 0 | goto loser; |
9690 | 0 | } |
9691 | | |
9692 | 0 | rv = ssl3_SendChangeCipherSpecs(ss); |
9693 | 0 | if (rv != SECSuccess) { |
9694 | 0 | errCode = PORT_GetError(); |
9695 | 0 | goto loser; |
9696 | 0 | } |
9697 | 0 | rv = ssl3_SendFinished(ss, 0); |
9698 | 0 | ss->ssl3.hs.ws = wait_change_cipher; |
9699 | 0 | if (rv != SECSuccess) { |
9700 | 0 | errCode = PORT_GetError(); |
9701 | 0 | goto loser; |
9702 | 0 | } |
9703 | | |
9704 | 0 | if (haveXmitBufLock) { |
9705 | 0 | ssl_ReleaseXmitBufLock(ss); |
9706 | 0 | } |
9707 | |
|
9708 | 0 | return SECSuccess; |
9709 | 0 | } while (0); |
9710 | | |
9711 | 0 | if (sid) { /* we had a sid, but it's no longer valid, free it */ |
9712 | 0 | ss->statelessResume = PR_FALSE; |
9713 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hch_sid_cache_not_ok); |
9714 | 0 | ssl_UncacheSessionID(ss); |
9715 | 0 | ssl_FreeSID(sid); |
9716 | 0 | sid = NULL; |
9717 | 0 | } |
9718 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hch_sid_cache_misses); |
9719 | | |
9720 | | /* We only send a session ticket extension if the client supports |
9721 | | * the extension and we are unable to resume. |
9722 | | * |
9723 | | * TODO: send a session ticket if performing a stateful |
9724 | | * resumption. (As per RFC4507, a server may issue a session |
9725 | | * ticket while doing a (stateless or stateful) session resume, |
9726 | | * but OpenSSL-0.9.8g does not accept session tickets while |
9727 | | * resuming.) |
9728 | | */ |
9729 | 0 | if (ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn) && |
9730 | 0 | ssl3_KEASupportsTickets(ss->ssl3.hs.kea_def)) { |
9731 | 0 | ssl3_RegisterExtensionSender(ss, &ss->xtnData, ssl_session_ticket_xtn, |
9732 | 0 | ssl_SendEmptyExtension); |
9733 | 0 | } |
9734 | |
|
9735 | 0 | rv = ssl3_ServerCallSNICallback(ss); |
9736 | 0 | if (rv != SECSuccess) { |
9737 | | /* The alert has already been sent. */ |
9738 | 0 | errCode = PORT_GetError(); |
9739 | 0 | goto loser; |
9740 | 0 | } |
9741 | | |
9742 | 0 | rv = ssl3_SelectServerCert(ss); |
9743 | 0 | if (rv != SECSuccess) { |
9744 | 0 | errCode = PORT_GetError(); |
9745 | 0 | desc = handshake_failure; |
9746 | 0 | goto alert_loser; |
9747 | 0 | } |
9748 | | |
9749 | 0 | sid = ssl3_NewSessionID(ss, PR_TRUE); |
9750 | 0 | if (sid == NULL) { |
9751 | 0 | errCode = PORT_GetError(); |
9752 | 0 | goto loser; /* memory error is set. */ |
9753 | 0 | } |
9754 | 0 | ss->sec.ci.sid = sid; |
9755 | |
|
9756 | 0 | sid->u.ssl3.keys.extendedMasterSecretUsed = |
9757 | 0 | ssl3_ExtensionNegotiated(ss, ssl_extended_master_secret_xtn); |
9758 | 0 | ss->ssl3.hs.isResuming = PR_FALSE; |
9759 | |
|
9760 | 0 | ssl_GetXmitBufLock(ss); |
9761 | 0 | rv = ssl3_SendServerHelloSequence(ss); |
9762 | 0 | ssl_ReleaseXmitBufLock(ss); |
9763 | 0 | if (rv != SECSuccess) { |
9764 | 0 | errCode = PORT_GetError(); |
9765 | 0 | desc = handshake_failure; |
9766 | 0 | goto alert_loser; |
9767 | 0 | } |
9768 | | |
9769 | 0 | if (haveXmitBufLock) { |
9770 | 0 | ssl_ReleaseXmitBufLock(ss); |
9771 | 0 | } |
9772 | |
|
9773 | 0 | return SECSuccess; |
9774 | | |
9775 | 0 | alert_loser: |
9776 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
9777 | | /* FALLTHRU */ |
9778 | 0 | loser: |
9779 | 0 | if (sid && sid != ss->sec.ci.sid) { |
9780 | 0 | ssl_UncacheSessionID(ss); |
9781 | 0 | ssl_FreeSID(sid); |
9782 | 0 | } |
9783 | |
|
9784 | 0 | if (haveXmitBufLock) { |
9785 | 0 | ssl_ReleaseXmitBufLock(ss); |
9786 | 0 | } |
9787 | |
|
9788 | 0 | PORT_SetError(errCode); |
9789 | 0 | return SECFailure; |
9790 | 0 | } |
9791 | | |
9792 | | /* |
9793 | | * ssl3_HandleV2ClientHello is used when a V2 formatted hello comes |
9794 | | * in asking to use the V3 handshake. |
9795 | | */ |
9796 | | SECStatus |
9797 | | ssl3_HandleV2ClientHello(sslSocket *ss, unsigned char *buffer, unsigned int length, |
9798 | | PRUint8 padding) |
9799 | 0 | { |
9800 | 0 | sslSessionID *sid = NULL; |
9801 | 0 | unsigned char *suites; |
9802 | 0 | unsigned char *random; |
9803 | 0 | SSL3ProtocolVersion version; |
9804 | 0 | SECStatus rv; |
9805 | 0 | unsigned int i; |
9806 | 0 | unsigned int j; |
9807 | 0 | unsigned int sid_length; |
9808 | 0 | unsigned int suite_length; |
9809 | 0 | unsigned int rand_length; |
9810 | 0 | int errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; |
9811 | 0 | SSL3AlertDescription desc = handshake_failure; |
9812 | 0 | unsigned int total = SSL_HL_CLIENT_HELLO_HBYTES; |
9813 | |
|
9814 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: handle v2 client_hello", SSL_GETPID(), ss->fd)); |
9815 | |
|
9816 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
9817 | |
|
9818 | 0 | ssl_GetSSL3HandshakeLock(ss); |
9819 | |
|
9820 | 0 | version = (buffer[1] << 8) | buffer[2]; |
9821 | 0 | if (version < SSL_LIBRARY_VERSION_3_0) { |
9822 | 0 | goto loser; |
9823 | 0 | } |
9824 | | |
9825 | 0 | ssl3_RestartHandshakeHashes(ss); |
9826 | |
|
9827 | 0 | if (ss->ssl3.hs.ws != wait_client_hello) { |
9828 | 0 | desc = unexpected_message; |
9829 | 0 | errCode = SSL_ERROR_RX_UNEXPECTED_CLIENT_HELLO; |
9830 | 0 | goto alert_loser; |
9831 | 0 | } |
9832 | | |
9833 | 0 | total += suite_length = (buffer[3] << 8) | buffer[4]; |
9834 | 0 | total += sid_length = (buffer[5] << 8) | buffer[6]; |
9835 | 0 | total += rand_length = (buffer[7] << 8) | buffer[8]; |
9836 | 0 | total += padding; |
9837 | 0 | ss->clientHelloVersion = version; |
9838 | |
|
9839 | 0 | if (version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
9840 | | /* [draft-ietf-tls-tls-11; C.3] forbids sending a TLS 1.3 |
9841 | | * ClientHello using the backwards-compatible format. */ |
9842 | 0 | desc = illegal_parameter; |
9843 | 0 | errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; |
9844 | 0 | goto alert_loser; |
9845 | 0 | } |
9846 | | |
9847 | 0 | rv = ssl3_NegotiateVersion(ss, version, PR_TRUE); |
9848 | 0 | if (rv != SECSuccess) { |
9849 | | /* send back which ever alert client will understand. */ |
9850 | 0 | desc = (version > SSL_LIBRARY_VERSION_3_0) ? protocol_version |
9851 | 0 | : handshake_failure; |
9852 | 0 | errCode = SSL_ERROR_UNSUPPORTED_VERSION; |
9853 | 0 | goto alert_loser; |
9854 | 0 | } |
9855 | | /* ECH not possible here. */ |
9856 | 0 | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_ech; |
9857 | 0 | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_version; |
9858 | 0 | if (!ss->firstHsDone) { |
9859 | 0 | ssl_GetSpecWriteLock(ss); |
9860 | 0 | ssl_SetSpecVersions(ss, ss->ssl3.cwSpec); |
9861 | 0 | ssl_ReleaseSpecWriteLock(ss); |
9862 | 0 | } |
9863 | | |
9864 | | /* if we get a non-zero SID, just ignore it. */ |
9865 | 0 | if (length != total) { |
9866 | 0 | SSL_DBG(("%d: SSL3[%d]: bad v2 client hello message, len=%d should=%d", |
9867 | 0 | SSL_GETPID(), ss->fd, length, total)); |
9868 | 0 | desc = illegal_parameter; |
9869 | 0 | errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; |
9870 | 0 | goto alert_loser; |
9871 | 0 | } |
9872 | | |
9873 | 0 | suites = buffer + SSL_HL_CLIENT_HELLO_HBYTES; |
9874 | 0 | random = suites + suite_length + sid_length; |
9875 | |
|
9876 | 0 | if (rand_length < SSL_MIN_CHALLENGE_BYTES || |
9877 | 0 | rand_length > SSL_MAX_CHALLENGE_BYTES) { |
9878 | 0 | desc = illegal_parameter; |
9879 | 0 | errCode = SSL_ERROR_RX_MALFORMED_CLIENT_HELLO; |
9880 | 0 | goto alert_loser; |
9881 | 0 | } |
9882 | | |
9883 | 0 | PORT_Assert(SSL_MAX_CHALLENGE_BYTES == SSL3_RANDOM_LENGTH); |
9884 | |
|
9885 | 0 | PORT_Memset(ss->ssl3.hs.client_random, 0, SSL3_RANDOM_LENGTH); |
9886 | 0 | PORT_Memcpy(&ss->ssl3.hs.client_random[SSL3_RANDOM_LENGTH - rand_length], |
9887 | 0 | random, rand_length); |
9888 | |
|
9889 | 0 | PRINT_BUF(60, (ss, "client random:", ss->ssl3.hs.client_random, |
9890 | 0 | SSL3_RANDOM_LENGTH)); |
9891 | |
|
9892 | 0 | if (ssl3_config_match_init(ss) == 0) { |
9893 | 0 | errCode = PORT_GetError(); /* error code is already set. */ |
9894 | 0 | goto alert_loser; |
9895 | 0 | } |
9896 | | |
9897 | | /* Select a cipher suite. |
9898 | | ** |
9899 | | ** NOTE: This suite selection algorithm should be the same as the one in |
9900 | | ** ssl3_HandleClientHello(). |
9901 | | */ |
9902 | 0 | for (j = 0; j < ssl_V3_SUITES_IMPLEMENTED; j++) { |
9903 | 0 | ssl3CipherSuiteCfg *suite = &ss->cipherSuites[j]; |
9904 | 0 | SSLVersionRange vrange = { ss->version, ss->version }; |
9905 | 0 | if (!ssl3_config_match(suite, ss->ssl3.policy, &vrange, ss)) { |
9906 | 0 | continue; |
9907 | 0 | } |
9908 | 0 | for (i = 0; i + 2 < suite_length; i += 3) { |
9909 | 0 | PRUint32 suite_i = (suites[i] << 16) | (suites[i + 1] << 8) | suites[i + 2]; |
9910 | 0 | if (suite_i == suite->cipher_suite) { |
9911 | 0 | ss->ssl3.hs.cipher_suite = suite_i; |
9912 | 0 | rv = ssl3_SetupCipherSuite(ss, PR_TRUE); |
9913 | 0 | if (rv != SECSuccess) { |
9914 | 0 | desc = internal_error; |
9915 | 0 | errCode = PORT_GetError(); |
9916 | 0 | goto alert_loser; |
9917 | 0 | } |
9918 | 0 | goto suite_found; |
9919 | 0 | } |
9920 | 0 | } |
9921 | 0 | } |
9922 | 0 | errCode = SSL_ERROR_NO_CYPHER_OVERLAP; |
9923 | 0 | goto alert_loser; |
9924 | | |
9925 | 0 | suite_found: |
9926 | | |
9927 | | /* If the ClientHello version is less than our maximum version, check for a |
9928 | | * TLS_FALLBACK_SCSV and reject the connection if found. */ |
9929 | 0 | if (ss->vrange.max > ss->clientHelloVersion) { |
9930 | 0 | for (i = 0; i + 2 < suite_length; i += 3) { |
9931 | 0 | PRUint16 suite_i = (suites[i] << 16) | (suites[i + 1] << 8) | suites[i + 2]; |
9932 | 0 | if (suite_i == TLS_FALLBACK_SCSV) { |
9933 | 0 | desc = inappropriate_fallback; |
9934 | 0 | errCode = SSL_ERROR_INAPPROPRIATE_FALLBACK_ALERT; |
9935 | 0 | goto alert_loser; |
9936 | 0 | } |
9937 | 0 | } |
9938 | 0 | } |
9939 | | |
9940 | | /* Look for the SCSV, and if found, treat it just like an empty RI |
9941 | | * extension by processing a local copy of an empty RI extension. |
9942 | | */ |
9943 | 0 | for (i = 0; i + 2 < suite_length; i += 3) { |
9944 | 0 | PRUint32 suite_i = (suites[i] << 16) | (suites[i + 1] << 8) | suites[i + 2]; |
9945 | 0 | if (suite_i == TLS_EMPTY_RENEGOTIATION_INFO_SCSV) { |
9946 | 0 | PRUint8 *b2 = (PRUint8 *)emptyRIext; |
9947 | 0 | PRUint32 L2 = sizeof emptyRIext; |
9948 | 0 | (void)ssl3_HandleExtensions(ss, &b2, &L2, ssl_hs_client_hello); |
9949 | 0 | break; |
9950 | 0 | } |
9951 | 0 | } |
9952 | |
|
9953 | 0 | if (ss->opt.requireSafeNegotiation && |
9954 | 0 | !ssl3_ExtensionNegotiated(ss, ssl_renegotiation_info_xtn)) { |
9955 | 0 | desc = handshake_failure; |
9956 | 0 | errCode = SSL_ERROR_UNSAFE_NEGOTIATION; |
9957 | 0 | goto alert_loser; |
9958 | 0 | } |
9959 | | |
9960 | 0 | rv = ssl3_SelectServerCert(ss); |
9961 | 0 | if (rv != SECSuccess) { |
9962 | 0 | errCode = PORT_GetError(); |
9963 | 0 | desc = handshake_failure; |
9964 | 0 | goto alert_loser; |
9965 | 0 | } |
9966 | | |
9967 | | /* we don't even search for a cache hit here. It's just a miss. */ |
9968 | 0 | SSL_AtomicIncrementLong(&ssl3stats.hch_sid_cache_misses); |
9969 | 0 | sid = ssl3_NewSessionID(ss, PR_TRUE); |
9970 | 0 | if (sid == NULL) { |
9971 | 0 | errCode = PORT_GetError(); |
9972 | 0 | goto loser; /* memory error is set. */ |
9973 | 0 | } |
9974 | 0 | ss->sec.ci.sid = sid; |
9975 | | /* do not worry about memory leak of sid since it now belongs to ci */ |
9976 | | |
9977 | | /* We have to update the handshake hashes before we can send stuff */ |
9978 | 0 | rv = ssl3_UpdateHandshakeHashes(ss, buffer, length); |
9979 | 0 | if (rv != SECSuccess) { |
9980 | 0 | errCode = PORT_GetError(); |
9981 | 0 | goto loser; |
9982 | 0 | } |
9983 | | |
9984 | 0 | ssl_GetXmitBufLock(ss); |
9985 | 0 | rv = ssl3_SendServerHelloSequence(ss); |
9986 | 0 | ssl_ReleaseXmitBufLock(ss); |
9987 | 0 | if (rv != SECSuccess) { |
9988 | 0 | errCode = PORT_GetError(); |
9989 | 0 | goto loser; |
9990 | 0 | } |
9991 | | |
9992 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
9993 | 0 | return SECSuccess; |
9994 | | |
9995 | 0 | alert_loser: |
9996 | 0 | SSL3_SendAlert(ss, alert_fatal, desc); |
9997 | 0 | loser: |
9998 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
9999 | 0 | PORT_SetError(errCode); |
10000 | 0 | return SECFailure; |
10001 | 0 | } |
10002 | | |
10003 | | SECStatus |
10004 | | ssl_ConstructServerHello(sslSocket *ss, PRBool helloRetry, |
10005 | | const sslBuffer *extensionBuf, sslBuffer *messageBuf) |
10006 | 0 | { |
10007 | 0 | SECStatus rv; |
10008 | 0 | SSL3ProtocolVersion version; |
10009 | 0 | sslSessionID *sid = ss->sec.ci.sid; |
10010 | 0 | const PRUint8 *random; |
10011 | |
|
10012 | 0 | version = PR_MIN(ss->version, SSL_LIBRARY_VERSION_TLS_1_2); |
10013 | 0 | if (IS_DTLS(ss)) { |
10014 | 0 | version = dtls_TLSVersionToDTLSVersion(version); |
10015 | 0 | } |
10016 | 0 | rv = sslBuffer_AppendNumber(messageBuf, version, 2); |
10017 | 0 | if (rv != SECSuccess) { |
10018 | 0 | return SECFailure; |
10019 | 0 | } |
10020 | | |
10021 | 0 | if (helloRetry) { |
10022 | 0 | random = ssl_hello_retry_random; |
10023 | 0 | } else { |
10024 | 0 | rv = ssl_GenerateServerRandom(ss); |
10025 | 0 | if (rv != SECSuccess) { |
10026 | 0 | return SECFailure; |
10027 | 0 | } |
10028 | 0 | random = ss->ssl3.hs.server_random; |
10029 | 0 | } |
10030 | 0 | rv = sslBuffer_Append(messageBuf, random, SSL3_RANDOM_LENGTH); |
10031 | 0 | if (rv != SECSuccess) { |
10032 | 0 | return SECFailure; |
10033 | 0 | } |
10034 | | |
10035 | 0 | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
10036 | 0 | if (sid) { |
10037 | 0 | rv = sslBuffer_AppendVariable(messageBuf, sid->u.ssl3.sessionID, |
10038 | 0 | sid->u.ssl3.sessionIDLength, 1); |
10039 | 0 | } else { |
10040 | 0 | rv = sslBuffer_AppendNumber(messageBuf, 0, 1); |
10041 | 0 | } |
10042 | 0 | } else { |
10043 | 0 | rv = sslBuffer_AppendVariable(messageBuf, ss->ssl3.hs.fakeSid.data, |
10044 | 0 | ss->ssl3.hs.fakeSid.len, 1); |
10045 | 0 | } |
10046 | 0 | if (rv != SECSuccess) { |
10047 | 0 | return SECFailure; |
10048 | 0 | } |
10049 | | |
10050 | 0 | rv = sslBuffer_AppendNumber(messageBuf, ss->ssl3.hs.cipher_suite, 2); |
10051 | 0 | if (rv != SECSuccess) { |
10052 | 0 | return SECFailure; |
10053 | 0 | } |
10054 | 0 | rv = sslBuffer_AppendNumber(messageBuf, ssl_compression_null, 1); |
10055 | 0 | if (rv != SECSuccess) { |
10056 | 0 | return SECFailure; |
10057 | 0 | } |
10058 | 0 | if (SSL_BUFFER_LEN(extensionBuf)) { |
10059 | | /* Directly copy the extensions */ |
10060 | 0 | rv = sslBuffer_AppendBufferVariable(messageBuf, extensionBuf, 2); |
10061 | 0 | if (rv != SECSuccess) { |
10062 | 0 | return SECFailure; |
10063 | 0 | } |
10064 | 0 | } |
10065 | | |
10066 | 0 | if (ss->xtnData.ech && ss->xtnData.ech->receivedInnerXtn) { |
10067 | | /* Signal ECH acceptance if we handled handled both CHOuter/CHInner (i.e. |
10068 | | * in shared mode), or if we received a CHInner in split/backend mode. */ |
10069 | 0 | if (ss->ssl3.hs.echAccepted || ss->opt.enableTls13BackendEch) { |
10070 | 0 | if (helloRetry) { |
10071 | 0 | return tls13_WriteServerEchHrrSignal(ss, SSL_BUFFER_BASE(messageBuf), |
10072 | 0 | SSL_BUFFER_LEN(messageBuf)); |
10073 | 0 | } else { |
10074 | 0 | return tls13_WriteServerEchSignal(ss, SSL_BUFFER_BASE(messageBuf), |
10075 | 0 | SSL_BUFFER_LEN(messageBuf)); |
10076 | 0 | } |
10077 | 0 | } |
10078 | 0 | } |
10079 | 0 | return SECSuccess; |
10080 | 0 | } |
10081 | | |
10082 | | /* The negotiated version number has been already placed in ss->version. |
10083 | | ** |
10084 | | ** Called from: ssl3_HandleClientHello (resuming session), |
10085 | | ** ssl3_SendServerHelloSequence <- ssl3_HandleClientHello (new session), |
10086 | | ** ssl3_SendServerHelloSequence <- ssl3_HandleV2ClientHello (new session) |
10087 | | */ |
10088 | | SECStatus |
10089 | | ssl3_SendServerHello(sslSocket *ss) |
10090 | 0 | { |
10091 | 0 | SECStatus rv; |
10092 | 0 | sslBuffer extensionBuf = SSL_BUFFER_EMPTY; |
10093 | 0 | sslBuffer messageBuf = SSL_BUFFER_EMPTY; |
10094 | |
|
10095 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: send server_hello handshake", SSL_GETPID(), |
10096 | 0 | ss->fd)); |
10097 | |
|
10098 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
10099 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10100 | |
|
10101 | 0 | PORT_Assert(MSB(ss->version) == MSB(SSL_LIBRARY_VERSION_3_0)); |
10102 | 0 | if (MSB(ss->version) != MSB(SSL_LIBRARY_VERSION_3_0)) { |
10103 | 0 | PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP); |
10104 | 0 | return SECFailure; |
10105 | 0 | } |
10106 | | |
10107 | 0 | rv = ssl_ConstructExtensions(ss, &extensionBuf, ssl_hs_server_hello); |
10108 | 0 | if (rv != SECSuccess) { |
10109 | 0 | goto loser; |
10110 | 0 | } |
10111 | | |
10112 | 0 | rv = ssl_ConstructServerHello(ss, PR_FALSE, &extensionBuf, &messageBuf); |
10113 | 0 | if (rv != SECSuccess) { |
10114 | 0 | goto loser; |
10115 | 0 | } |
10116 | | |
10117 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_server_hello, |
10118 | 0 | SSL_BUFFER_LEN(&messageBuf)); |
10119 | 0 | if (rv != SECSuccess) { |
10120 | 0 | goto loser; /* err set by AppendHandshake. */ |
10121 | 0 | } |
10122 | | |
10123 | 0 | rv = ssl3_AppendHandshake(ss, SSL_BUFFER_BASE(&messageBuf), |
10124 | 0 | SSL_BUFFER_LEN(&messageBuf)); |
10125 | 0 | if (rv != SECSuccess) { |
10126 | 0 | goto loser; /* err set by AppendHandshake. */ |
10127 | 0 | } |
10128 | | |
10129 | 0 | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
10130 | 0 | rv = ssl3_SetupBothPendingCipherSpecs(ss); |
10131 | 0 | if (rv != SECSuccess) { |
10132 | 0 | goto loser; /* err set */ |
10133 | 0 | } |
10134 | 0 | } |
10135 | | |
10136 | 0 | sslBuffer_Clear(&extensionBuf); |
10137 | 0 | sslBuffer_Clear(&messageBuf); |
10138 | 0 | return SECSuccess; |
10139 | | |
10140 | 0 | loser: |
10141 | 0 | sslBuffer_Clear(&extensionBuf); |
10142 | 0 | sslBuffer_Clear(&messageBuf); |
10143 | 0 | return SECFailure; |
10144 | 0 | } |
10145 | | |
10146 | | SECStatus |
10147 | | ssl_CreateDHEKeyPair(const sslNamedGroupDef *groupDef, |
10148 | | const ssl3DHParams *params, |
10149 | | sslEphemeralKeyPair **keyPair) |
10150 | 1.31k | { |
10151 | 1.31k | SECKEYDHParams dhParam; |
10152 | 1.31k | SECKEYPublicKey *pubKey = NULL; /* Ephemeral DH key */ |
10153 | 1.31k | SECKEYPrivateKey *privKey = NULL; /* Ephemeral DH key */ |
10154 | 1.31k | sslEphemeralKeyPair *pair; |
10155 | | |
10156 | 1.31k | dhParam.prime.data = params->prime.data; |
10157 | 1.31k | dhParam.prime.len = params->prime.len; |
10158 | 1.31k | dhParam.base.data = params->base.data; |
10159 | 1.31k | dhParam.base.len = params->base.len; |
10160 | | |
10161 | 1.31k | PRINT_BUF(60, (NULL, "Server DH p", dhParam.prime.data, |
10162 | 1.31k | dhParam.prime.len)); |
10163 | 1.31k | PRINT_BUF(60, (NULL, "Server DH g", dhParam.base.data, |
10164 | 1.31k | dhParam.base.len)); |
10165 | | |
10166 | | /* Generate ephemeral DH keypair */ |
10167 | 1.31k | privKey = SECKEY_CreateDHPrivateKey(&dhParam, &pubKey, NULL); |
10168 | 1.31k | if (!privKey || !pubKey) { |
10169 | 0 | ssl_MapLowLevelError(SEC_ERROR_KEYGEN_FAIL); |
10170 | 0 | return SECFailure; |
10171 | 0 | } |
10172 | | |
10173 | 1.31k | pair = ssl_NewEphemeralKeyPair(groupDef, privKey, pubKey); |
10174 | 1.31k | if (!pair) { |
10175 | 0 | SECKEY_DestroyPrivateKey(privKey); |
10176 | 0 | SECKEY_DestroyPublicKey(pubKey); |
10177 | |
|
10178 | 0 | return SECFailure; |
10179 | 0 | } |
10180 | | |
10181 | 1.31k | *keyPair = pair; |
10182 | 1.31k | return SECSuccess; |
10183 | 1.31k | } |
10184 | | |
10185 | | static SECStatus |
10186 | | ssl3_SendDHServerKeyExchange(sslSocket *ss) |
10187 | 0 | { |
10188 | 0 | const ssl3KEADef *kea_def = ss->ssl3.hs.kea_def; |
10189 | 0 | SECStatus rv = SECFailure; |
10190 | 0 | int length; |
10191 | 0 | SECItem signed_hash = { siBuffer, NULL, 0 }; |
10192 | 0 | SSL3Hashes hashes; |
10193 | 0 | SSLHashType hashAlg; |
10194 | |
|
10195 | 0 | const ssl3DHParams *params; |
10196 | 0 | sslEphemeralKeyPair *keyPair; |
10197 | 0 | SECKEYPublicKey *pubKey; |
10198 | 0 | SECKEYPrivateKey *certPrivateKey; |
10199 | 0 | const sslNamedGroupDef *groupDef; |
10200 | | /* Do this on the heap, this could be over 2k long. */ |
10201 | 0 | sslBuffer dhBuf = SSL_BUFFER_EMPTY; |
10202 | |
|
10203 | 0 | if (kea_def->kea != kea_dhe_dss && kea_def->kea != kea_dhe_rsa) { |
10204 | | /* TODO: Support DH_anon. It might be sufficient to drop the signature. |
10205 | | See bug 1170510. */ |
10206 | 0 | PORT_SetError(SSL_ERROR_SERVER_KEY_EXCHANGE_FAILURE); |
10207 | 0 | return SECFailure; |
10208 | 0 | } |
10209 | | |
10210 | 0 | rv = ssl_SelectDHEGroup(ss, &groupDef); |
10211 | 0 | if (rv == SECFailure) { |
10212 | 0 | PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP); |
10213 | 0 | return SECFailure; |
10214 | 0 | } |
10215 | 0 | ss->sec.keaGroup = groupDef; |
10216 | |
|
10217 | 0 | params = ssl_GetDHEParams(groupDef); |
10218 | 0 | rv = ssl_CreateDHEKeyPair(groupDef, params, &keyPair); |
10219 | 0 | if (rv == SECFailure) { |
10220 | 0 | ssl_MapLowLevelError(SEC_ERROR_KEYGEN_FAIL); |
10221 | 0 | return SECFailure; |
10222 | 0 | } |
10223 | 0 | PR_APPEND_LINK(&keyPair->link, &ss->ephemeralKeyPairs); |
10224 | |
|
10225 | 0 | if (ss->version == SSL_LIBRARY_VERSION_TLS_1_2) { |
10226 | 0 | hashAlg = ssl_SignatureSchemeToHashType(ss->ssl3.hs.signatureScheme); |
10227 | 0 | } else { |
10228 | | /* Use ssl_hash_none to represent the MD5+SHA1 combo. */ |
10229 | 0 | hashAlg = ssl_hash_none; |
10230 | 0 | } |
10231 | |
|
10232 | 0 | pubKey = keyPair->keys->pubKey; |
10233 | 0 | PRINT_BUF(50, (ss, "DH public value:", |
10234 | 0 | pubKey->u.dh.publicValue.data, |
10235 | 0 | pubKey->u.dh.publicValue.len)); |
10236 | 0 | rv = ssl3_ComputeDHKeyHash(ss, hashAlg, &hashes, |
10237 | 0 | pubKey->u.dh.prime, |
10238 | 0 | pubKey->u.dh.base, |
10239 | 0 | pubKey->u.dh.publicValue, |
10240 | 0 | PR_TRUE /* padY */); |
10241 | 0 | if (rv != SECSuccess) { |
10242 | 0 | ssl_MapLowLevelError(SSL_ERROR_SERVER_KEY_EXCHANGE_FAILURE); |
10243 | 0 | goto loser; |
10244 | 0 | } |
10245 | | |
10246 | 0 | certPrivateKey = ss->sec.serverCert->serverKeyPair->privKey; |
10247 | 0 | rv = ssl3_SignHashes(ss, &hashes, certPrivateKey, &signed_hash); |
10248 | 0 | if (rv != SECSuccess) { |
10249 | 0 | goto loser; /* ssl3_SignHashes has set err. */ |
10250 | 0 | } |
10251 | | |
10252 | 0 | length = 2 + pubKey->u.dh.prime.len + |
10253 | 0 | 2 + pubKey->u.dh.base.len + |
10254 | 0 | 2 + pubKey->u.dh.prime.len + |
10255 | 0 | 2 + signed_hash.len; |
10256 | |
|
10257 | 0 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_2) { |
10258 | 0 | length += 2; |
10259 | 0 | } |
10260 | |
|
10261 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_server_key_exchange, length); |
10262 | 0 | if (rv != SECSuccess) { |
10263 | 0 | goto loser; /* err set by AppendHandshake. */ |
10264 | 0 | } |
10265 | | |
10266 | 0 | rv = ssl3_AppendHandshakeVariable(ss, pubKey->u.dh.prime.data, |
10267 | 0 | pubKey->u.dh.prime.len, 2); |
10268 | 0 | if (rv != SECSuccess) { |
10269 | 0 | goto loser; /* err set by AppendHandshake. */ |
10270 | 0 | } |
10271 | | |
10272 | 0 | rv = ssl3_AppendHandshakeVariable(ss, pubKey->u.dh.base.data, |
10273 | 0 | pubKey->u.dh.base.len, 2); |
10274 | 0 | if (rv != SECSuccess) { |
10275 | 0 | goto loser; /* err set by AppendHandshake. */ |
10276 | 0 | } |
10277 | | |
10278 | 0 | rv = ssl_AppendPaddedDHKeyShare(&dhBuf, pubKey, PR_TRUE); |
10279 | 0 | if (rv != SECSuccess) { |
10280 | 0 | goto loser; /* err set by AppendPaddedDHKeyShare. */ |
10281 | 0 | } |
10282 | 0 | rv = ssl3_AppendBufferToHandshake(ss, &dhBuf); |
10283 | 0 | if (rv != SECSuccess) { |
10284 | 0 | goto loser; /* err set by AppendHandshake. */ |
10285 | 0 | } |
10286 | | |
10287 | 0 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_2) { |
10288 | 0 | rv = ssl3_AppendHandshakeNumber(ss, ss->ssl3.hs.signatureScheme, 2); |
10289 | 0 | if (rv != SECSuccess) { |
10290 | 0 | goto loser; /* err set by AppendHandshake. */ |
10291 | 0 | } |
10292 | 0 | } |
10293 | | |
10294 | 0 | rv = ssl3_AppendHandshakeVariable(ss, signed_hash.data, |
10295 | 0 | signed_hash.len, 2); |
10296 | 0 | if (rv != SECSuccess) { |
10297 | 0 | goto loser; /* err set by AppendHandshake. */ |
10298 | 0 | } |
10299 | | |
10300 | 0 | sslBuffer_Clear(&dhBuf); |
10301 | 0 | PORT_Free(signed_hash.data); |
10302 | 0 | return SECSuccess; |
10303 | | |
10304 | 0 | loser: |
10305 | 0 | if (signed_hash.data) |
10306 | 0 | PORT_Free(signed_hash.data); |
10307 | 0 | sslBuffer_Clear(&dhBuf); |
10308 | 0 | return SECFailure; |
10309 | 0 | } |
10310 | | |
10311 | | static SECStatus |
10312 | | ssl3_SendServerKeyExchange(sslSocket *ss) |
10313 | 0 | { |
10314 | 0 | const ssl3KEADef *kea_def = ss->ssl3.hs.kea_def; |
10315 | |
|
10316 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: send server_key_exchange handshake", |
10317 | 0 | SSL_GETPID(), ss->fd)); |
10318 | |
|
10319 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
10320 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10321 | |
|
10322 | 0 | switch (kea_def->exchKeyType) { |
10323 | 0 | case ssl_kea_dh: { |
10324 | 0 | return ssl3_SendDHServerKeyExchange(ss); |
10325 | 0 | } |
10326 | | |
10327 | 0 | case ssl_kea_ecdh: { |
10328 | 0 | return ssl3_SendECDHServerKeyExchange(ss); |
10329 | 0 | } |
10330 | | |
10331 | 0 | case ssl_kea_rsa: |
10332 | 0 | case ssl_kea_null: |
10333 | 0 | default: |
10334 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
10335 | 0 | break; |
10336 | 0 | } |
10337 | | |
10338 | 0 | return SECFailure; |
10339 | 0 | } |
10340 | | |
10341 | | SECStatus |
10342 | | ssl3_EncodeSigAlgs(const sslSocket *ss, PRUint16 minVersion, PRBool forCert, |
10343 | | PRBool grease, sslBuffer *buf) |
10344 | 36.2k | { |
10345 | 36.2k | SSLSignatureScheme filtered[MAX_SIGNATURE_SCHEMES] = { 0 }; |
10346 | 36.2k | unsigned int filteredCount = 0; |
10347 | | |
10348 | 36.2k | SECStatus rv = ssl3_FilterSigAlgs(ss, minVersion, PR_FALSE, forCert, |
10349 | 36.2k | PR_ARRAY_SIZE(filtered), |
10350 | 36.2k | filtered, &filteredCount); |
10351 | 36.2k | if (rv != SECSuccess) { |
10352 | 0 | return SECFailure; |
10353 | 0 | } |
10354 | 36.2k | return ssl3_EncodeFilteredSigAlgs(ss, filtered, filteredCount, grease, buf); |
10355 | 36.2k | } |
10356 | | |
10357 | | SECStatus |
10358 | | ssl3_EncodeFilteredSigAlgs(const sslSocket *ss, const SSLSignatureScheme *schemes, |
10359 | | PRUint32 numSchemes, PRBool grease, sslBuffer *buf) |
10360 | 51.6k | { |
10361 | 51.6k | if (!numSchemes) { |
10362 | 0 | PORT_SetError(SSL_ERROR_NO_SUPPORTED_SIGNATURE_ALGORITHM); |
10363 | 0 | return SECFailure; |
10364 | 0 | } |
10365 | | |
10366 | 51.6k | unsigned int lengthOffset; |
10367 | 51.6k | SECStatus rv; |
10368 | | |
10369 | 51.6k | rv = sslBuffer_Skip(buf, 2, &lengthOffset); |
10370 | 51.6k | if (rv != SECSuccess) { |
10371 | 0 | return SECFailure; |
10372 | 0 | } |
10373 | | |
10374 | 655k | for (unsigned int i = 0; i < numSchemes; ++i) { |
10375 | 603k | rv = sslBuffer_AppendNumber(buf, schemes[i], 2); |
10376 | 603k | if (rv != SECSuccess) { |
10377 | 0 | return SECFailure; |
10378 | 0 | } |
10379 | 603k | } |
10380 | | |
10381 | | /* GREASE SignatureAlgorithms: |
10382 | | * A client MAY select one or more GREASE signature algorithm values and |
10383 | | * advertise them in the "signature_algorithms" or |
10384 | | * "signature_algorithms_cert" extensions, if sent [RFC8701, Section 3.1]. |
10385 | | * |
10386 | | * When sending a CertificateRequest in TLS 1.3, a server MAY behave as |
10387 | | * follows: [...] A server MAY select one or more GREASE signature |
10388 | | * algorithm values and advertise them in the "signature_algorithms" or |
10389 | | * "signature_algorithms_cert" extensions, if present |
10390 | | * [RFC8701, Section 4.1]. */ |
10391 | 51.6k | if (grease && |
10392 | 51.6k | ((!ss->sec.isServer && ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3) || |
10393 | 19.2k | (ss->sec.isServer && ss->version >= SSL_LIBRARY_VERSION_TLS_1_3))) { |
10394 | 16.9k | PRUint16 value; |
10395 | 16.9k | if (ss->sec.isServer) { |
10396 | 0 | rv = tls13_RandomGreaseValue(&value); |
10397 | 0 | if (rv != SECSuccess) { |
10398 | 0 | return SECFailure; |
10399 | 0 | } |
10400 | 16.9k | } else { |
10401 | 16.9k | value = ss->ssl3.hs.grease->idx[grease_sigalg]; |
10402 | 16.9k | } |
10403 | 16.9k | rv = sslBuffer_AppendNumber(buf, value, 2); |
10404 | 16.9k | if (rv != SECSuccess) { |
10405 | 0 | return SECFailure; |
10406 | 0 | } |
10407 | 16.9k | } |
10408 | | |
10409 | 51.6k | return sslBuffer_InsertLength(buf, lengthOffset, 2); |
10410 | 51.6k | } |
10411 | | |
10412 | | /* |
10413 | | * In TLS 1.3 we are permitted to advertise support for PKCS#1 |
10414 | | * schemes. This doesn't affect the signatures in TLS itself, just |
10415 | | * those on certificates. Not advertising PKCS#1 signatures creates a |
10416 | | * serious compatibility risk as it excludes many certificate chains |
10417 | | * that include PKCS#1. Hence, forCert is used to enable advertising |
10418 | | * PKCS#1 support. Note that we include these in signature_algorithms |
10419 | | * because we don't yet support signature_algorithms_cert. TLS 1.3 |
10420 | | * requires that PKCS#1 schemes are placed last in the list if they |
10421 | | * are present. This sorting can be removed once we support |
10422 | | * signature_algorithms_cert. |
10423 | | */ |
10424 | | SECStatus |
10425 | | ssl3_FilterSigAlgs(const sslSocket *ss, PRUint16 minVersion, PRBool disableRsae, |
10426 | | PRBool forCert, |
10427 | | unsigned int maxSchemes, SSLSignatureScheme *filteredSchemes, |
10428 | | unsigned int *numFilteredSchemes) |
10429 | 51.6k | { |
10430 | 51.6k | PORT_Assert(filteredSchemes); |
10431 | 51.6k | PORT_Assert(numFilteredSchemes); |
10432 | 51.6k | PORT_Assert(maxSchemes >= ss->ssl3.signatureSchemeCount); |
10433 | 51.6k | if (maxSchemes < ss->ssl3.signatureSchemeCount) { |
10434 | 0 | return SECFailure; |
10435 | 0 | } |
10436 | | |
10437 | 51.6k | *numFilteredSchemes = 0; |
10438 | 51.6k | PRBool allowUnsortedPkcs1 = forCert && minVersion < SSL_LIBRARY_VERSION_TLS_1_3; |
10439 | 826k | for (unsigned int i = 0; i < ss->ssl3.signatureSchemeCount; ++i) { |
10440 | 774k | if (disableRsae && ssl_IsRsaeSignatureScheme(ss->ssl3.signatureSchemes[i])) { |
10441 | 46.2k | continue; |
10442 | 46.2k | } |
10443 | 728k | if (ssl_SignatureSchemeAccepted(minVersion, |
10444 | 728k | ss->ssl3.signatureSchemes[i], |
10445 | 728k | allowUnsortedPkcs1)) { |
10446 | 602k | filteredSchemes[(*numFilteredSchemes)++] = ss->ssl3.signatureSchemes[i]; |
10447 | 602k | } |
10448 | 728k | } |
10449 | 51.6k | if (forCert && !allowUnsortedPkcs1) { |
10450 | 5.28k | for (unsigned int i = 0; i < ss->ssl3.signatureSchemeCount; ++i) { |
10451 | 4.95k | if (disableRsae && ssl_IsRsaeSignatureScheme(ss->ssl3.signatureSchemes[i])) { |
10452 | 0 | continue; |
10453 | 0 | } |
10454 | 4.95k | if (!ssl_SignatureSchemeAccepted(minVersion, |
10455 | 4.95k | ss->ssl3.signatureSchemes[i], |
10456 | 4.95k | PR_FALSE) && |
10457 | 4.95k | ssl_SignatureSchemeAccepted(minVersion, |
10458 | 2.64k | ss->ssl3.signatureSchemes[i], |
10459 | 2.64k | PR_TRUE)) { |
10460 | 1.32k | filteredSchemes[(*numFilteredSchemes)++] = ss->ssl3.signatureSchemes[i]; |
10461 | 1.32k | } |
10462 | 4.95k | } |
10463 | 330 | } |
10464 | 51.6k | return SECSuccess; |
10465 | 51.6k | } |
10466 | | |
10467 | | static SECStatus |
10468 | | ssl3_SendCertificateRequest(sslSocket *ss) |
10469 | 0 | { |
10470 | 0 | PRBool isTLS12; |
10471 | 0 | const PRUint8 *certTypes; |
10472 | 0 | SECStatus rv; |
10473 | 0 | PRUint32 length; |
10474 | 0 | const SECItem *names; |
10475 | 0 | unsigned int calen; |
10476 | 0 | unsigned int nnames; |
10477 | 0 | const SECItem *name; |
10478 | 0 | unsigned int i; |
10479 | 0 | int certTypesLength; |
10480 | 0 | PRUint8 sigAlgs[2 + MAX_SIGNATURE_SCHEMES * 2]; |
10481 | 0 | sslBuffer sigAlgsBuf = SSL_BUFFER(sigAlgs); |
10482 | |
|
10483 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: send certificate_request handshake", |
10484 | 0 | SSL_GETPID(), ss->fd)); |
10485 | |
|
10486 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
10487 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10488 | |
|
10489 | 0 | isTLS12 = (PRBool)(ss->version >= SSL_LIBRARY_VERSION_TLS_1_2); |
10490 | |
|
10491 | 0 | rv = ssl_GetCertificateRequestCAs(ss, &calen, &names, &nnames); |
10492 | 0 | if (rv != SECSuccess) { |
10493 | 0 | return rv; |
10494 | 0 | } |
10495 | 0 | certTypes = certificate_types; |
10496 | 0 | certTypesLength = sizeof certificate_types; |
10497 | |
|
10498 | 0 | length = 1 + certTypesLength + 2 + calen; |
10499 | 0 | if (isTLS12) { |
10500 | 0 | rv = ssl3_EncodeSigAlgs(ss, ss->version, PR_TRUE /* forCert */, |
10501 | 0 | PR_FALSE /* GREASE */, &sigAlgsBuf); |
10502 | 0 | if (rv != SECSuccess) { |
10503 | 0 | return rv; |
10504 | 0 | } |
10505 | 0 | length += SSL_BUFFER_LEN(&sigAlgsBuf); |
10506 | 0 | } |
10507 | | |
10508 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_certificate_request, length); |
10509 | 0 | if (rv != SECSuccess) { |
10510 | 0 | return rv; /* err set by AppendHandshake. */ |
10511 | 0 | } |
10512 | 0 | rv = ssl3_AppendHandshakeVariable(ss, certTypes, certTypesLength, 1); |
10513 | 0 | if (rv != SECSuccess) { |
10514 | 0 | return rv; /* err set by AppendHandshake. */ |
10515 | 0 | } |
10516 | 0 | if (isTLS12) { |
10517 | 0 | rv = ssl3_AppendHandshake(ss, SSL_BUFFER_BASE(&sigAlgsBuf), |
10518 | 0 | SSL_BUFFER_LEN(&sigAlgsBuf)); |
10519 | 0 | if (rv != SECSuccess) { |
10520 | 0 | return rv; /* err set by AppendHandshake. */ |
10521 | 0 | } |
10522 | 0 | } |
10523 | 0 | rv = ssl3_AppendHandshakeNumber(ss, calen, 2); |
10524 | 0 | if (rv != SECSuccess) { |
10525 | 0 | return rv; /* err set by AppendHandshake. */ |
10526 | 0 | } |
10527 | 0 | for (i = 0, name = names; i < nnames; i++, name++) { |
10528 | 0 | rv = ssl3_AppendHandshakeVariable(ss, name->data, name->len, 2); |
10529 | 0 | if (rv != SECSuccess) { |
10530 | 0 | return rv; /* err set by AppendHandshake. */ |
10531 | 0 | } |
10532 | 0 | } |
10533 | | |
10534 | 0 | return SECSuccess; |
10535 | 0 | } |
10536 | | |
10537 | | static SECStatus |
10538 | | ssl3_SendServerHelloDone(sslSocket *ss) |
10539 | 0 | { |
10540 | 0 | SECStatus rv; |
10541 | |
|
10542 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: send server_hello_done handshake", |
10543 | 0 | SSL_GETPID(), ss->fd)); |
10544 | |
|
10545 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
10546 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10547 | |
|
10548 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_server_hello_done, 0); |
10549 | 0 | if (rv != SECSuccess) { |
10550 | 0 | return rv; /* err set by AppendHandshake. */ |
10551 | 0 | } |
10552 | 0 | rv = ssl3_FlushHandshake(ss, 0); |
10553 | 0 | if (rv != SECSuccess) { |
10554 | 0 | return rv; /* error code set by ssl3_FlushHandshake */ |
10555 | 0 | } |
10556 | 0 | return SECSuccess; |
10557 | 0 | } |
10558 | | |
10559 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered |
10560 | | * a complete ssl3 Certificate Verify message |
10561 | | * Caller must hold Handshake and RecvBuf locks. |
10562 | | */ |
10563 | | static SECStatus |
10564 | | ssl3_HandleCertificateVerify(sslSocket *ss, PRUint8 *b, PRUint32 length) |
10565 | 0 | { |
10566 | 0 | SECItem signed_hash = { siBuffer, NULL, 0 }; |
10567 | 0 | SECStatus rv; |
10568 | 0 | int errCode = SSL_ERROR_RX_MALFORMED_CERT_VERIFY; |
10569 | 0 | SSL3AlertDescription desc = handshake_failure; |
10570 | 0 | PRBool isTLS; |
10571 | 0 | SSLSignatureScheme sigScheme; |
10572 | 0 | SSL3Hashes hashes; |
10573 | 0 | const PRUint8 *savedMsg = b; |
10574 | 0 | const PRUint32 savedLen = length; |
10575 | |
|
10576 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: handle certificate_verify handshake", |
10577 | 0 | SSL_GETPID(), ss->fd)); |
10578 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
10579 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10580 | |
|
10581 | 0 | if (ss->ssl3.hs.ws != wait_cert_verify) { |
10582 | 0 | desc = unexpected_message; |
10583 | 0 | errCode = SSL_ERROR_RX_UNEXPECTED_CERT_VERIFY; |
10584 | 0 | goto alert_loser; |
10585 | 0 | } |
10586 | | |
10587 | | /* TLS 1.3 is handled by tls13_HandleCertificateVerify */ |
10588 | 0 | PORT_Assert(ss->ssl3.prSpec->version <= SSL_LIBRARY_VERSION_TLS_1_2); |
10589 | |
|
10590 | 0 | if (ss->ssl3.prSpec->version == SSL_LIBRARY_VERSION_TLS_1_2) { |
10591 | 0 | PORT_Assert(ss->ssl3.hs.hashType == handshake_hash_record); |
10592 | 0 | rv = ssl_ConsumeSignatureScheme(ss, &b, &length, &sigScheme); |
10593 | 0 | if (rv != SECSuccess) { |
10594 | 0 | if (PORT_GetError() == SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM) { |
10595 | 0 | errCode = SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM; |
10596 | 0 | } |
10597 | 0 | goto loser; /* alert already sent */ |
10598 | 0 | } |
10599 | 0 | rv = ssl_CheckSignatureSchemeConsistency( |
10600 | 0 | ss, sigScheme, &ss->sec.peerCert->subjectPublicKeyInfo); |
10601 | 0 | if (rv != SECSuccess) { |
10602 | 0 | errCode = PORT_GetError(); |
10603 | 0 | desc = illegal_parameter; |
10604 | 0 | goto alert_loser; |
10605 | 0 | } |
10606 | | |
10607 | 0 | rv = ssl3_ComputeHandshakeHash(ss->ssl3.hs.messages.buf, |
10608 | 0 | ss->ssl3.hs.messages.len, |
10609 | 0 | ssl_SignatureSchemeToHashType(sigScheme), |
10610 | 0 | &hashes); |
10611 | 0 | } else { |
10612 | 0 | PORT_Assert(ss->ssl3.hs.hashType != handshake_hash_record); |
10613 | 0 | sigScheme = ssl_sig_none; |
10614 | 0 | rv = ssl3_ComputeHandshakeHashes(ss, ss->ssl3.prSpec, &hashes, 0); |
10615 | 0 | } |
10616 | | |
10617 | 0 | if (rv != SECSuccess) { |
10618 | 0 | errCode = SSL_ERROR_DIGEST_FAILURE; |
10619 | 0 | desc = decrypt_error; |
10620 | 0 | goto alert_loser; |
10621 | 0 | } |
10622 | | |
10623 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, &signed_hash, 2, &b, &length); |
10624 | 0 | if (rv != SECSuccess) { |
10625 | 0 | goto loser; /* malformed. */ |
10626 | 0 | } |
10627 | | |
10628 | 0 | isTLS = (PRBool)(ss->ssl3.prSpec->version > SSL_LIBRARY_VERSION_3_0); |
10629 | | |
10630 | | /* XXX verify that the key & kea match */ |
10631 | 0 | rv = ssl3_VerifySignedHashes(ss, sigScheme, &hashes, &signed_hash); |
10632 | 0 | if (rv != SECSuccess) { |
10633 | 0 | errCode = PORT_GetError(); |
10634 | 0 | desc = isTLS ? decrypt_error : handshake_failure; |
10635 | 0 | goto alert_loser; |
10636 | 0 | } |
10637 | | |
10638 | 0 | signed_hash.data = NULL; |
10639 | |
|
10640 | 0 | if (length != 0) { |
10641 | 0 | desc = isTLS ? decode_error : illegal_parameter; |
10642 | 0 | goto alert_loser; /* malformed */ |
10643 | 0 | } |
10644 | | |
10645 | 0 | rv = ssl_HashHandshakeMessage(ss, ssl_hs_certificate_verify, |
10646 | 0 | savedMsg, savedLen); |
10647 | 0 | if (rv != SECSuccess) { |
10648 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
10649 | 0 | return rv; |
10650 | 0 | } |
10651 | | |
10652 | 0 | ss->ssl3.hs.ws = wait_change_cipher; |
10653 | 0 | return SECSuccess; |
10654 | | |
10655 | 0 | alert_loser: |
10656 | 0 | SSL3_SendAlert(ss, alert_fatal, desc); |
10657 | 0 | loser: |
10658 | 0 | PORT_SetError(errCode); |
10659 | 0 | return SECFailure; |
10660 | 0 | } |
10661 | | |
10662 | | /* find a slot that is able to generate a PMS and wrap it with RSA. |
10663 | | * Then generate and return the PMS. |
10664 | | * If the serverKeySlot parameter is non-null, this function will use |
10665 | | * that slot to do the job, otherwise it will find a slot. |
10666 | | * |
10667 | | * Called from ssl3_DeriveConnectionKeys() (above) |
10668 | | * ssl3_SendRSAClientKeyExchange() (above) |
10669 | | * ssl3_HandleRSAClientKeyExchange() (below) |
10670 | | * Caller must hold the SpecWriteLock, the SSL3HandshakeLock |
10671 | | */ |
10672 | | static PK11SymKey * |
10673 | | ssl3_GenerateRSAPMS(sslSocket *ss, ssl3CipherSpec *spec, |
10674 | | PK11SlotInfo *serverKeySlot) |
10675 | 5.38k | { |
10676 | 5.38k | PK11SymKey *pms = NULL; |
10677 | 5.38k | PK11SlotInfo *slot = serverKeySlot; |
10678 | 5.38k | void *pwArg = ss->pkcs11PinArg; |
10679 | 5.38k | SECItem param; |
10680 | 5.38k | CK_VERSION version; |
10681 | 5.38k | CK_MECHANISM_TYPE mechanism_array[3]; |
10682 | | |
10683 | 5.38k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10684 | | |
10685 | 5.38k | if (slot == NULL) { |
10686 | 5.38k | SSLCipherAlgorithm calg; |
10687 | | /* The specReadLock would suffice here, but we cannot assert on |
10688 | | ** read locks. Also, all the callers who call with a non-null |
10689 | | ** slot already hold the SpecWriteLock. |
10690 | | */ |
10691 | 5.38k | PORT_Assert(ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); |
10692 | 5.38k | PORT_Assert(ss->ssl3.prSpec->epoch == ss->ssl3.pwSpec->epoch); |
10693 | | |
10694 | 5.38k | calg = spec->cipherDef->calg; |
10695 | | |
10696 | | /* First get an appropriate slot. */ |
10697 | 5.38k | mechanism_array[0] = CKM_SSL3_PRE_MASTER_KEY_GEN; |
10698 | 5.38k | mechanism_array[1] = CKM_RSA_PKCS; |
10699 | 5.38k | mechanism_array[2] = ssl3_Alg2Mech(calg); |
10700 | | |
10701 | 5.38k | slot = PK11_GetBestSlotMultiple(mechanism_array, 3, pwArg); |
10702 | 5.38k | if (slot == NULL) { |
10703 | | /* can't find a slot with all three, find a slot with the minimum */ |
10704 | 965 | slot = PK11_GetBestSlotMultiple(mechanism_array, 2, pwArg); |
10705 | 965 | if (slot == NULL) { |
10706 | 0 | PORT_SetError(SSL_ERROR_TOKEN_SLOT_NOT_FOUND); |
10707 | 0 | return pms; /* which is NULL */ |
10708 | 0 | } |
10709 | 965 | } |
10710 | 5.38k | } |
10711 | | |
10712 | | /* Generate the pre-master secret ... */ |
10713 | 5.38k | if (IS_DTLS(ss)) { |
10714 | 0 | SSL3ProtocolVersion temp; |
10715 | |
|
10716 | 0 | temp = dtls_TLSVersionToDTLSVersion(ss->clientHelloVersion); |
10717 | 0 | version.major = MSB(temp); |
10718 | 0 | version.minor = LSB(temp); |
10719 | 5.38k | } else { |
10720 | 5.38k | version.major = MSB(ss->clientHelloVersion); |
10721 | 5.38k | version.minor = LSB(ss->clientHelloVersion); |
10722 | 5.38k | } |
10723 | | |
10724 | 5.38k | param.data = (unsigned char *)&version; |
10725 | 5.38k | param.len = sizeof version; |
10726 | | |
10727 | 5.38k | pms = PK11_KeyGen(slot, CKM_SSL3_PRE_MASTER_KEY_GEN, ¶m, 0, pwArg); |
10728 | 5.38k | if (!serverKeySlot) |
10729 | 5.38k | PK11_FreeSlot(slot); |
10730 | 5.38k | if (pms == NULL) { |
10731 | 0 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
10732 | 0 | } |
10733 | 5.38k | return pms; |
10734 | 5.38k | } |
10735 | | |
10736 | | static void |
10737 | | ssl3_CSwapPK11SymKey(PK11SymKey **x, PK11SymKey **y, PRBool c) |
10738 | 0 | { |
10739 | 0 | uintptr_t mask = (uintptr_t)c; |
10740 | 0 | unsigned int i; |
10741 | 0 | for (i = 1; i < sizeof(uintptr_t) * 8; i <<= 1) { |
10742 | 0 | mask |= mask << i; |
10743 | 0 | } |
10744 | 0 | uintptr_t x_ptr = (uintptr_t)*x; |
10745 | 0 | uintptr_t y_ptr = (uintptr_t)*y; |
10746 | 0 | uintptr_t tmp = (x_ptr ^ y_ptr) & mask; |
10747 | 0 | x_ptr = x_ptr ^ tmp; |
10748 | 0 | y_ptr = y_ptr ^ tmp; |
10749 | 0 | *x = (PK11SymKey *)x_ptr; |
10750 | 0 | *y = (PK11SymKey *)y_ptr; |
10751 | 0 | } |
10752 | | |
10753 | | /* Note: The Bleichenbacher attack on PKCS#1 necessitates that we NEVER |
10754 | | * return any indication of failure of the Client Key Exchange message, |
10755 | | * where that failure is caused by the content of the client's message. |
10756 | | * This function must not return SECFailure for any reason that is directly |
10757 | | * or indirectly caused by the content of the client's encrypted PMS. |
10758 | | * We must not send an alert and also not drop the connection. |
10759 | | * Instead, we generate a random PMS. This will cause a failure |
10760 | | * in the processing the finished message, which is exactly where |
10761 | | * the failure must occur. |
10762 | | * |
10763 | | * Called from ssl3_HandleClientKeyExchange |
10764 | | */ |
10765 | | static SECStatus |
10766 | | ssl3_HandleRSAClientKeyExchange(sslSocket *ss, |
10767 | | PRUint8 *b, |
10768 | | PRUint32 length, |
10769 | | sslKeyPair *serverKeyPair) |
10770 | 0 | { |
10771 | 0 | SECStatus rv; |
10772 | 0 | SECItem enc_pms; |
10773 | 0 | PK11SymKey *pms = NULL; |
10774 | 0 | PK11SymKey *fauxPms = NULL; |
10775 | 0 | PK11SlotInfo *slot = NULL; |
10776 | |
|
10777 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
10778 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10779 | 0 | PORT_Assert(ss->ssl3.prSpec->epoch == ss->ssl3.pwSpec->epoch); |
10780 | |
|
10781 | 0 | enc_pms.data = b; |
10782 | 0 | enc_pms.len = length; |
10783 | |
|
10784 | 0 | if (ss->ssl3.prSpec->version > SSL_LIBRARY_VERSION_3_0) { /* isTLS */ |
10785 | 0 | PRUint32 kLen; |
10786 | 0 | rv = ssl3_ConsumeHandshakeNumber(ss, &kLen, 2, &enc_pms.data, &enc_pms.len); |
10787 | 0 | if (rv != SECSuccess) { |
10788 | 0 | PORT_SetError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
10789 | 0 | return SECFailure; |
10790 | 0 | } |
10791 | 0 | if ((unsigned)kLen < enc_pms.len) { |
10792 | 0 | enc_pms.len = kLen; |
10793 | 0 | } |
10794 | 0 | } |
10795 | | |
10796 | | /* |
10797 | | * Get as close to algorithm 2 from RFC 5246; Section 7.4.7.1 |
10798 | | * as we can within the constraints of the PKCS#11 interface. |
10799 | | * |
10800 | | * 1. Unconditionally generate a bogus PMS (what RFC 5246 |
10801 | | * calls R). |
10802 | | * 2. Attempt the RSA decryption to recover the PMS (what |
10803 | | * RFC 5246 calls M). |
10804 | | * 3. Set PMS = (M == NULL) ? R : M |
10805 | | * 4. Use ssl3_ComputeMasterSecret(PMS) to attempt to derive |
10806 | | * the MS from PMS. This includes performing the version |
10807 | | * check and length check. |
10808 | | * 5. If either the initial RSA decryption failed or |
10809 | | * ssl3_ComputeMasterSecret(PMS) failed, then discard |
10810 | | * M and set PMS = R. Else, discard R and set PMS = M. |
10811 | | * |
10812 | | * We do two derivations here because we can't rely on having |
10813 | | * a function that only performs the PMS version and length |
10814 | | * check. The only redundant cost is that this runs the PRF, |
10815 | | * which isn't necessary here. |
10816 | | */ |
10817 | | |
10818 | | /* Generate the bogus PMS (R) */ |
10819 | 0 | slot = PK11_GetSlotFromPrivateKey(serverKeyPair->privKey); |
10820 | 0 | if (!slot) { |
10821 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
10822 | 0 | return SECFailure; |
10823 | 0 | } |
10824 | | |
10825 | 0 | if (!PK11_DoesMechanism(slot, CKM_SSL3_MASTER_KEY_DERIVE)) { |
10826 | 0 | PK11_FreeSlot(slot); |
10827 | 0 | slot = PK11_GetBestSlot(CKM_SSL3_MASTER_KEY_DERIVE, NULL); |
10828 | 0 | if (!slot) { |
10829 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
10830 | 0 | return SECFailure; |
10831 | 0 | } |
10832 | 0 | } |
10833 | | |
10834 | 0 | ssl_GetSpecWriteLock(ss); |
10835 | 0 | fauxPms = ssl3_GenerateRSAPMS(ss, ss->ssl3.prSpec, slot); |
10836 | 0 | ssl_ReleaseSpecWriteLock(ss); |
10837 | 0 | PK11_FreeSlot(slot); |
10838 | |
|
10839 | 0 | if (fauxPms == NULL) { |
10840 | 0 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
10841 | 0 | return SECFailure; |
10842 | 0 | } |
10843 | | |
10844 | | /* |
10845 | | * unwrap pms out of the incoming buffer |
10846 | | * Note: CKM_SSL3_MASTER_KEY_DERIVE is NOT the mechanism used to do |
10847 | | * the unwrap. Rather, it is the mechanism with which the |
10848 | | * unwrapped pms will be used. |
10849 | | */ |
10850 | 0 | pms = PK11_PubUnwrapSymKey(serverKeyPair->privKey, &enc_pms, |
10851 | 0 | CKM_SSL3_MASTER_KEY_DERIVE, CKA_DERIVE, 0); |
10852 | | /* Temporarily use the PMS if unwrapping the real PMS fails. */ |
10853 | 0 | ssl3_CSwapPK11SymKey(&pms, &fauxPms, pms == NULL); |
10854 | | |
10855 | | /* Attempt to derive the MS from the PMS. This is the only way to |
10856 | | * check the version field in the RSA PMS. If this fails, we |
10857 | | * then use the faux PMS in place of the PMS. Note that this |
10858 | | * operation should never fail if we are using the faux PMS |
10859 | | * since it is correctly formatted. */ |
10860 | 0 | rv = ssl3_ComputeMasterSecret(ss, pms, NULL); |
10861 | | |
10862 | | /* If we succeeded, then select the true PMS, else select the FPMS. */ |
10863 | 0 | ssl3_CSwapPK11SymKey(&pms, &fauxPms, (rv != SECSuccess) & (fauxPms != NULL)); |
10864 | | |
10865 | | /* This step will derive the MS from the PMS, among other things. */ |
10866 | 0 | rv = ssl3_InitPendingCipherSpecs(ss, pms, PR_TRUE); |
10867 | | |
10868 | | /* Clear both PMS. */ |
10869 | 0 | PK11_FreeSymKey(pms); |
10870 | 0 | PK11_FreeSymKey(fauxPms); |
10871 | |
|
10872 | 0 | if (rv != SECSuccess) { |
10873 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, handshake_failure); |
10874 | 0 | return SECFailure; /* error code set by ssl3_InitPendingCipherSpec */ |
10875 | 0 | } |
10876 | | |
10877 | 0 | return SECSuccess; |
10878 | 0 | } |
10879 | | |
10880 | | static SECStatus |
10881 | | ssl3_HandleDHClientKeyExchange(sslSocket *ss, |
10882 | | PRUint8 *b, |
10883 | | PRUint32 length, |
10884 | | sslKeyPair *serverKeyPair) |
10885 | 0 | { |
10886 | 0 | PK11SymKey *pms; |
10887 | 0 | SECStatus rv; |
10888 | 0 | SECKEYPublicKey clntPubKey; |
10889 | 0 | CK_MECHANISM_TYPE target; |
10890 | 0 | PRBool isTLS; |
10891 | |
|
10892 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
10893 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10894 | |
|
10895 | 0 | clntPubKey.keyType = dhKey; |
10896 | 0 | clntPubKey.u.dh.prime.len = serverKeyPair->pubKey->u.dh.prime.len; |
10897 | 0 | clntPubKey.u.dh.prime.data = serverKeyPair->pubKey->u.dh.prime.data; |
10898 | 0 | clntPubKey.u.dh.base.len = serverKeyPair->pubKey->u.dh.base.len; |
10899 | 0 | clntPubKey.u.dh.base.data = serverKeyPair->pubKey->u.dh.base.data; |
10900 | |
|
10901 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, &clntPubKey.u.dh.publicValue, |
10902 | 0 | 2, &b, &length); |
10903 | 0 | if (rv != SECSuccess) { |
10904 | 0 | return SECFailure; |
10905 | 0 | } |
10906 | | |
10907 | 0 | if (!ssl_IsValidDHEShare(&serverKeyPair->pubKey->u.dh.prime, |
10908 | 0 | &clntPubKey.u.dh.publicValue)) { |
10909 | 0 | PORT_SetError(SSL_ERROR_RX_MALFORMED_DHE_KEY_SHARE); |
10910 | 0 | return SECFailure; |
10911 | 0 | } |
10912 | | |
10913 | 0 | isTLS = (PRBool)(ss->ssl3.prSpec->version > SSL_LIBRARY_VERSION_3_0); |
10914 | |
|
10915 | 0 | if (isTLS) |
10916 | 0 | target = CKM_TLS_MASTER_KEY_DERIVE_DH; |
10917 | 0 | else |
10918 | 0 | target = CKM_SSL3_MASTER_KEY_DERIVE_DH; |
10919 | | |
10920 | | /* Determine the PMS */ |
10921 | 0 | pms = PK11_PubDerive(serverKeyPair->privKey, &clntPubKey, PR_FALSE, NULL, NULL, |
10922 | 0 | CKM_DH_PKCS_DERIVE, target, CKA_DERIVE, 0, NULL); |
10923 | 0 | if (pms == NULL) { |
10924 | 0 | ssl_FreeEphemeralKeyPairs(ss); |
10925 | 0 | ssl_MapLowLevelError(SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE); |
10926 | 0 | return SECFailure; |
10927 | 0 | } |
10928 | | |
10929 | 0 | rv = ssl3_InitPendingCipherSpecs(ss, pms, PR_TRUE); |
10930 | 0 | PK11_FreeSymKey(pms); |
10931 | 0 | ssl_FreeEphemeralKeyPairs(ss); |
10932 | 0 | return rv; |
10933 | 0 | } |
10934 | | |
10935 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered |
10936 | | * a complete ssl3 ClientKeyExchange message from the remote client |
10937 | | * Caller must hold Handshake and RecvBuf locks. |
10938 | | */ |
10939 | | static SECStatus |
10940 | | ssl3_HandleClientKeyExchange(sslSocket *ss, PRUint8 *b, PRUint32 length) |
10941 | 0 | { |
10942 | 0 | sslKeyPair *serverKeyPair = NULL; |
10943 | 0 | SECStatus rv; |
10944 | 0 | const ssl3KEADef *kea_def; |
10945 | |
|
10946 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: handle client_key_exchange handshake", |
10947 | 0 | SSL_GETPID(), ss->fd)); |
10948 | |
|
10949 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
10950 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
10951 | |
|
10952 | 0 | if (ss->ssl3.hs.ws != wait_client_key) { |
10953 | 0 | SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
10954 | 0 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CLIENT_KEY_EXCH); |
10955 | 0 | return SECFailure; |
10956 | 0 | } |
10957 | | |
10958 | 0 | kea_def = ss->ssl3.hs.kea_def; |
10959 | |
|
10960 | 0 | if (kea_def->ephemeral) { |
10961 | 0 | sslEphemeralKeyPair *keyPair; |
10962 | | /* There should be exactly one pair. */ |
10963 | 0 | PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ephemeralKeyPairs)); |
10964 | 0 | PORT_Assert(PR_PREV_LINK(&ss->ephemeralKeyPairs) == |
10965 | 0 | PR_NEXT_LINK(&ss->ephemeralKeyPairs)); |
10966 | 0 | keyPair = (sslEphemeralKeyPair *)PR_NEXT_LINK(&ss->ephemeralKeyPairs); |
10967 | 0 | serverKeyPair = keyPair->keys; |
10968 | 0 | ss->sec.keaKeyBits = |
10969 | 0 | SECKEY_PublicKeyStrengthInBits(serverKeyPair->pubKey); |
10970 | 0 | } else { |
10971 | 0 | serverKeyPair = ss->sec.serverCert->serverKeyPair; |
10972 | 0 | ss->sec.keaKeyBits = ss->sec.serverCert->serverKeyBits; |
10973 | 0 | } |
10974 | |
|
10975 | 0 | if (!serverKeyPair) { |
10976 | 0 | SSL3_SendAlert(ss, alert_fatal, handshake_failure); |
10977 | 0 | PORT_SetError(SSL_ERROR_NO_SERVER_KEY_FOR_ALG); |
10978 | 0 | return SECFailure; |
10979 | 0 | } |
10980 | 0 | PORT_Assert(serverKeyPair->pubKey); |
10981 | 0 | PORT_Assert(serverKeyPair->privKey); |
10982 | |
|
10983 | 0 | ss->sec.keaType = kea_def->exchKeyType; |
10984 | |
|
10985 | 0 | switch (kea_def->exchKeyType) { |
10986 | 0 | case ssl_kea_rsa: |
10987 | 0 | rv = ssl3_HandleRSAClientKeyExchange(ss, b, length, serverKeyPair); |
10988 | 0 | break; |
10989 | | |
10990 | 0 | case ssl_kea_dh: |
10991 | 0 | rv = ssl3_HandleDHClientKeyExchange(ss, b, length, serverKeyPair); |
10992 | 0 | break; |
10993 | | |
10994 | 0 | case ssl_kea_ecdh: |
10995 | 0 | rv = ssl3_HandleECDHClientKeyExchange(ss, b, length, serverKeyPair); |
10996 | 0 | break; |
10997 | | |
10998 | 0 | default: |
10999 | 0 | (void)ssl3_HandshakeFailure(ss); |
11000 | 0 | PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); |
11001 | 0 | return SECFailure; |
11002 | 0 | } |
11003 | 0 | ssl_FreeEphemeralKeyPairs(ss); |
11004 | 0 | if (rv == SECSuccess) { |
11005 | 0 | ss->ssl3.hs.ws = ss->sec.peerCert ? wait_cert_verify : wait_change_cipher; |
11006 | 0 | } else { |
11007 | | /* PORT_SetError has been called by all the Handle*ClientKeyExchange |
11008 | | * functions above. However, not all error paths result in an alert, so |
11009 | | * this ensures that the server knows about the error. Note that if an |
11010 | | * alert was already sent, SSL3_SendAlert() is a noop. */ |
11011 | 0 | PRErrorCode errCode = PORT_GetError(); |
11012 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, handshake_failure); |
11013 | 0 | PORT_SetError(errCode); |
11014 | 0 | } |
11015 | 0 | return rv; |
11016 | 0 | } |
11017 | | |
11018 | | /* This is TLS's equivalent of sending a no_certificate alert. */ |
11019 | | SECStatus |
11020 | | ssl3_SendEmptyCertificate(sslSocket *ss) |
11021 | 6 | { |
11022 | 6 | SECStatus rv; |
11023 | 6 | unsigned int len = 0; |
11024 | 6 | PRBool isTLS13 = PR_FALSE; |
11025 | 6 | const SECItem *context; |
11026 | | |
11027 | 6 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
11028 | 0 | PORT_Assert(ss->ssl3.hs.clientCertRequested); |
11029 | 0 | context = &ss->xtnData.certReqContext; |
11030 | 0 | len = context->len + 1; |
11031 | 0 | isTLS13 = PR_TRUE; |
11032 | 0 | } |
11033 | | |
11034 | 6 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_certificate, len + 3); |
11035 | 6 | if (rv != SECSuccess) { |
11036 | 0 | return rv; |
11037 | 0 | } |
11038 | | |
11039 | 6 | if (isTLS13) { |
11040 | 0 | rv = ssl3_AppendHandshakeVariable(ss, context->data, context->len, 1); |
11041 | 0 | if (rv != SECSuccess) { |
11042 | 0 | return rv; |
11043 | 0 | } |
11044 | 0 | } |
11045 | | |
11046 | 6 | return ssl3_AppendHandshakeNumber(ss, 0, 3); |
11047 | 6 | } |
11048 | | |
11049 | | /* |
11050 | | * NewSessionTicket |
11051 | | * Called from ssl3_HandleFinished |
11052 | | */ |
11053 | | static SECStatus |
11054 | | ssl3_SendNewSessionTicket(sslSocket *ss) |
11055 | 0 | { |
11056 | 0 | SECItem ticket = { 0, NULL, 0 }; |
11057 | 0 | SECStatus rv; |
11058 | 0 | NewSessionTicket nticket = { 0 }; |
11059 | |
|
11060 | 0 | rv = ssl3_EncodeSessionTicket(ss, &nticket, NULL, 0, |
11061 | 0 | ss->ssl3.pwSpec->masterSecret, &ticket); |
11062 | 0 | if (rv != SECSuccess) |
11063 | 0 | goto loser; |
11064 | | |
11065 | | /* Serialize the handshake message. Length = |
11066 | | * lifetime (4) + ticket length (2) + ticket. */ |
11067 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_new_session_ticket, |
11068 | 0 | 4 + 2 + ticket.len); |
11069 | 0 | if (rv != SECSuccess) |
11070 | 0 | goto loser; |
11071 | | |
11072 | | /* This is a fixed value. */ |
11073 | 0 | rv = ssl3_AppendHandshakeNumber(ss, ssl_ticket_lifetime, 4); |
11074 | 0 | if (rv != SECSuccess) |
11075 | 0 | goto loser; |
11076 | | |
11077 | | /* Encode the ticket. */ |
11078 | 0 | rv = ssl3_AppendHandshakeVariable(ss, ticket.data, ticket.len, 2); |
11079 | 0 | if (rv != SECSuccess) |
11080 | 0 | goto loser; |
11081 | | |
11082 | 0 | rv = SECSuccess; |
11083 | |
|
11084 | 0 | loser: |
11085 | 0 | if (ticket.data) { |
11086 | 0 | SECITEM_FreeItem(&ticket, PR_FALSE); |
11087 | 0 | } |
11088 | 0 | return rv; |
11089 | 0 | } |
11090 | | |
11091 | | static SECStatus |
11092 | | ssl3_HandleNewSessionTicket(sslSocket *ss, PRUint8 *b, PRUint32 length) |
11093 | 42 | { |
11094 | 42 | SECStatus rv; |
11095 | 42 | SECItem ticketData; |
11096 | 42 | PRUint32 temp; |
11097 | | |
11098 | 42 | SSL_TRC(3, ("%d: SSL3[%d]: handle session_ticket handshake", |
11099 | 42 | SSL_GETPID(), ss->fd)); |
11100 | | |
11101 | 42 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
11102 | 42 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
11103 | | |
11104 | 42 | PORT_Assert(!ss->ssl3.hs.newSessionTicket.ticket.data); |
11105 | 42 | PORT_Assert(!ss->ssl3.hs.receivedNewSessionTicket); |
11106 | | |
11107 | 42 | if (ss->ssl3.hs.ws != wait_new_session_ticket) { |
11108 | 6 | SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
11109 | 6 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_NEW_SESSION_TICKET); |
11110 | 6 | return SECFailure; |
11111 | 6 | } |
11112 | | |
11113 | | /* RFC5077 Section 3.3: "The client MUST NOT treat the ticket as valid |
11114 | | * until it has verified the server's Finished message." See the comment in |
11115 | | * ssl3_FinishHandshake for more details. |
11116 | | */ |
11117 | 36 | ss->ssl3.hs.newSessionTicket.received_timestamp = ssl_Time(ss); |
11118 | 36 | if (length < 4) { |
11119 | 2 | (void)SSL3_SendAlert(ss, alert_fatal, decode_error); |
11120 | 2 | PORT_SetError(SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET); |
11121 | 2 | return SECFailure; |
11122 | 2 | } |
11123 | | |
11124 | 34 | rv = ssl3_ConsumeHandshakeNumber(ss, &temp, 4, &b, &length); |
11125 | 34 | if (rv != SECSuccess) { |
11126 | 0 | PORT_SetError(SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET); |
11127 | 0 | return SECFailure; |
11128 | 0 | } |
11129 | 34 | ss->ssl3.hs.newSessionTicket.ticket_lifetime_hint = temp; |
11130 | | |
11131 | 34 | rv = ssl3_ConsumeHandshakeVariable(ss, &ticketData, 2, &b, &length); |
11132 | 34 | if (rv != SECSuccess || length != 0) { |
11133 | 31 | (void)SSL3_SendAlert(ss, alert_fatal, decode_error); |
11134 | 31 | PORT_SetError(SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET); |
11135 | 31 | return SECFailure; /* malformed */ |
11136 | 31 | } |
11137 | | /* If the server sent a zero-length ticket, ignore it and keep the |
11138 | | * existing ticket. */ |
11139 | 3 | if (ticketData.len != 0) { |
11140 | 1 | rv = SECITEM_CopyItem(NULL, &ss->ssl3.hs.newSessionTicket.ticket, |
11141 | 1 | &ticketData); |
11142 | 1 | if (rv != SECSuccess) { |
11143 | 0 | return rv; |
11144 | 0 | } |
11145 | 1 | ss->ssl3.hs.receivedNewSessionTicket = PR_TRUE; |
11146 | 1 | } |
11147 | | |
11148 | 3 | ss->ssl3.hs.ws = wait_change_cipher; |
11149 | 3 | return SECSuccess; |
11150 | 3 | } |
11151 | | |
11152 | | #ifdef NISCC_TEST |
11153 | | static PRInt32 connNum = 0; |
11154 | | |
11155 | | static SECStatus |
11156 | | get_fake_cert(SECItem *pCertItem, int *pIndex) |
11157 | | { |
11158 | | PRFileDesc *cf; |
11159 | | char *testdir; |
11160 | | char *startat; |
11161 | | char *stopat; |
11162 | | const char *extension; |
11163 | | int fileNum; |
11164 | | PRInt32 numBytes = 0; |
11165 | | PRStatus prStatus; |
11166 | | PRFileInfo info; |
11167 | | char cfn[100]; |
11168 | | |
11169 | | pCertItem->data = 0; |
11170 | | if ((testdir = PR_GetEnvSecure("NISCC_TEST")) == NULL) { |
11171 | | return SECSuccess; |
11172 | | } |
11173 | | *pIndex = (NULL != strstr(testdir, "root")); |
11174 | | extension = (strstr(testdir, "simple") ? "" : ".der"); |
11175 | | fileNum = PR_ATOMIC_INCREMENT(&connNum) - 1; |
11176 | | if ((startat = PR_GetEnvSecure("START_AT")) != NULL) { |
11177 | | fileNum += atoi(startat); |
11178 | | } |
11179 | | if ((stopat = PR_GetEnvSecure("STOP_AT")) != NULL && |
11180 | | fileNum >= atoi(stopat)) { |
11181 | | *pIndex = -1; |
11182 | | return SECSuccess; |
11183 | | } |
11184 | | snprintf(cfn, sizeof(cfn), "%s/%08d%s", testdir, fileNum, extension); |
11185 | | cf = PR_Open(cfn, PR_RDONLY, 0); |
11186 | | if (!cf) { |
11187 | | goto loser; |
11188 | | } |
11189 | | prStatus = PR_GetOpenFileInfo(cf, &info); |
11190 | | if (prStatus != PR_SUCCESS) { |
11191 | | PR_Close(cf); |
11192 | | goto loser; |
11193 | | } |
11194 | | pCertItem = SECITEM_AllocItem(NULL, pCertItem, info.size); |
11195 | | if (pCertItem) { |
11196 | | numBytes = PR_Read(cf, pCertItem->data, info.size); |
11197 | | } |
11198 | | PR_Close(cf); |
11199 | | if (numBytes != info.size) { |
11200 | | SECITEM_FreeItem(pCertItem, PR_FALSE); |
11201 | | PORT_SetError(SEC_ERROR_IO); |
11202 | | goto loser; |
11203 | | } |
11204 | | fprintf(stderr, "using %s\n", cfn); |
11205 | | return SECSuccess; |
11206 | | |
11207 | | loser: |
11208 | | fprintf(stderr, "failed to use %s\n", cfn); |
11209 | | *pIndex = -1; |
11210 | | return SECFailure; |
11211 | | } |
11212 | | #endif |
11213 | | |
11214 | | /* |
11215 | | * Used by both client and server. |
11216 | | * Called from HandleServerHelloDone and from SendServerHelloSequence. |
11217 | | */ |
11218 | | static SECStatus |
11219 | | ssl3_SendCertificate(sslSocket *ss) |
11220 | 0 | { |
11221 | 0 | SECStatus rv; |
11222 | 0 | CERTCertificateList *certChain; |
11223 | 0 | int certChainLen = 0; |
11224 | 0 | int i; |
11225 | | #ifdef NISCC_TEST |
11226 | | SECItem fakeCert; |
11227 | | int ndex = -1; |
11228 | | #endif |
11229 | 0 | PRBool isTLS13 = ss->version >= SSL_LIBRARY_VERSION_TLS_1_3; |
11230 | 0 | SECItem context = { siBuffer, NULL, 0 }; |
11231 | 0 | unsigned int contextLen = 0; |
11232 | |
|
11233 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: send certificate handshake", |
11234 | 0 | SSL_GETPID(), ss->fd)); |
11235 | |
|
11236 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
11237 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
11238 | 0 | PR_ASSERT(!ss->ssl3.hs.clientCertificatePending); |
11239 | |
|
11240 | 0 | if (ss->sec.localCert) |
11241 | 0 | CERT_DestroyCertificate(ss->sec.localCert); |
11242 | 0 | if (ss->sec.isServer) { |
11243 | | /* A server certificate is selected in ssl3_HandleClientHello. */ |
11244 | 0 | PORT_Assert(ss->sec.serverCert); |
11245 | |
|
11246 | 0 | certChain = ss->sec.serverCert->serverCertChain; |
11247 | 0 | ss->sec.localCert = CERT_DupCertificate(ss->sec.serverCert->serverCert); |
11248 | 0 | } else { |
11249 | 0 | certChain = ss->ssl3.clientCertChain; |
11250 | 0 | ss->sec.localCert = CERT_DupCertificate(ss->ssl3.clientCertificate); |
11251 | 0 | } |
11252 | |
|
11253 | | #ifdef NISCC_TEST |
11254 | | rv = get_fake_cert(&fakeCert, &ndex); |
11255 | | #endif |
11256 | |
|
11257 | 0 | if (isTLS13) { |
11258 | 0 | contextLen = 1; /* Size of the context length */ |
11259 | 0 | if (!ss->sec.isServer) { |
11260 | 0 | PORT_Assert(ss->ssl3.hs.clientCertRequested); |
11261 | 0 | context = ss->xtnData.certReqContext; |
11262 | 0 | contextLen += context.len; |
11263 | 0 | } |
11264 | 0 | } |
11265 | 0 | if (certChain) { |
11266 | 0 | for (i = 0; i < certChain->len; i++) { |
11267 | | #ifdef NISCC_TEST |
11268 | | if (fakeCert.len > 0 && i == ndex) { |
11269 | | certChainLen += fakeCert.len + 3; |
11270 | | } else { |
11271 | | certChainLen += certChain->certs[i].len + 3; |
11272 | | } |
11273 | | #else |
11274 | 0 | certChainLen += certChain->certs[i].len + 3; |
11275 | 0 | #endif |
11276 | 0 | } |
11277 | 0 | } |
11278 | |
|
11279 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_certificate, |
11280 | 0 | contextLen + certChainLen + 3); |
11281 | 0 | if (rv != SECSuccess) { |
11282 | 0 | return rv; /* err set by AppendHandshake. */ |
11283 | 0 | } |
11284 | | |
11285 | 0 | if (isTLS13) { |
11286 | 0 | rv = ssl3_AppendHandshakeVariable(ss, context.data, |
11287 | 0 | context.len, 1); |
11288 | 0 | if (rv != SECSuccess) { |
11289 | 0 | return rv; /* err set by AppendHandshake. */ |
11290 | 0 | } |
11291 | 0 | } |
11292 | | |
11293 | 0 | rv = ssl3_AppendHandshakeNumber(ss, certChainLen, 3); |
11294 | 0 | if (rv != SECSuccess) { |
11295 | 0 | return rv; /* err set by AppendHandshake. */ |
11296 | 0 | } |
11297 | 0 | if (certChain) { |
11298 | 0 | for (i = 0; i < certChain->len; i++) { |
11299 | | #ifdef NISCC_TEST |
11300 | | if (fakeCert.len > 0 && i == ndex) { |
11301 | | rv = ssl3_AppendHandshakeVariable(ss, fakeCert.data, |
11302 | | fakeCert.len, 3); |
11303 | | SECITEM_FreeItem(&fakeCert, PR_FALSE); |
11304 | | } else { |
11305 | | rv = ssl3_AppendHandshakeVariable(ss, certChain->certs[i].data, |
11306 | | certChain->certs[i].len, 3); |
11307 | | } |
11308 | | #else |
11309 | 0 | rv = ssl3_AppendHandshakeVariable(ss, certChain->certs[i].data, |
11310 | 0 | certChain->certs[i].len, 3); |
11311 | 0 | #endif |
11312 | 0 | if (rv != SECSuccess) { |
11313 | 0 | return rv; /* err set by AppendHandshake. */ |
11314 | 0 | } |
11315 | 0 | } |
11316 | 0 | } |
11317 | | |
11318 | 0 | return SECSuccess; |
11319 | 0 | } |
11320 | | |
11321 | | /* |
11322 | | * Used by server only. |
11323 | | * single-stapling, send only a single cert status |
11324 | | */ |
11325 | | SECStatus |
11326 | | ssl3_SendCertificateStatus(sslSocket *ss) |
11327 | 0 | { |
11328 | 0 | SECStatus rv; |
11329 | 0 | int len = 0; |
11330 | 0 | SECItemArray *statusToSend = NULL; |
11331 | 0 | const sslServerCert *serverCert; |
11332 | |
|
11333 | 0 | SSL_TRC(3, ("%d: SSL3[%d]: send certificate status handshake", |
11334 | 0 | SSL_GETPID(), ss->fd)); |
11335 | |
|
11336 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
11337 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
11338 | 0 | PORT_Assert(ss->sec.isServer); |
11339 | |
|
11340 | 0 | if (!ssl3_ExtensionNegotiated(ss, ssl_cert_status_xtn)) |
11341 | 0 | return SECSuccess; |
11342 | | |
11343 | | /* Use certStatus based on the cert being used. */ |
11344 | 0 | serverCert = ss->sec.serverCert; |
11345 | 0 | if (serverCert->certStatusArray && serverCert->certStatusArray->len) { |
11346 | 0 | statusToSend = serverCert->certStatusArray; |
11347 | 0 | } |
11348 | 0 | if (!statusToSend) |
11349 | 0 | return SECSuccess; |
11350 | | |
11351 | | /* Use the array's first item only (single stapling) */ |
11352 | 0 | len = 1 + statusToSend->items[0].len + 3; |
11353 | |
|
11354 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_certificate_status, len); |
11355 | 0 | if (rv != SECSuccess) { |
11356 | 0 | return rv; /* err set by AppendHandshake. */ |
11357 | 0 | } |
11358 | 0 | rv = ssl3_AppendHandshakeNumber(ss, 1 /*ocsp*/, 1); |
11359 | 0 | if (rv != SECSuccess) |
11360 | 0 | return rv; /* err set by AppendHandshake. */ |
11361 | | |
11362 | 0 | rv = ssl3_AppendHandshakeVariable(ss, |
11363 | 0 | statusToSend->items[0].data, |
11364 | 0 | statusToSend->items[0].len, |
11365 | 0 | 3); |
11366 | 0 | if (rv != SECSuccess) |
11367 | 0 | return rv; /* err set by AppendHandshake. */ |
11368 | | |
11369 | 0 | return SECSuccess; |
11370 | 0 | } |
11371 | | |
11372 | | /* This is used to delete the CA certificates in the peer certificate chain |
11373 | | * from the cert database after they've been validated. |
11374 | | */ |
11375 | | void |
11376 | | ssl3_CleanupPeerCerts(sslSocket *ss) |
11377 | 46.3k | { |
11378 | 46.3k | PLArenaPool *arena = ss->ssl3.peerCertArena; |
11379 | | |
11380 | 46.3k | if (arena) |
11381 | 38.3k | PORT_FreeArena(arena, PR_FALSE); |
11382 | 46.3k | ss->ssl3.peerCertArena = NULL; |
11383 | 46.3k | ss->ssl3.peerCertChain = NULL; |
11384 | | |
11385 | 46.3k | if (ss->sec.peerCert != NULL) { |
11386 | 30.5k | if (ss->sec.peerKey) { |
11387 | 0 | SECKEY_DestroyPublicKey(ss->sec.peerKey); |
11388 | 0 | ss->sec.peerKey = NULL; |
11389 | 0 | } |
11390 | 30.5k | CERT_DestroyCertificate(ss->sec.peerCert); |
11391 | 30.5k | ss->sec.peerCert = NULL; |
11392 | 30.5k | } |
11393 | 46.3k | } |
11394 | | |
11395 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered |
11396 | | * a complete ssl3 CertificateStatus message. |
11397 | | * Caller must hold Handshake and RecvBuf locks. |
11398 | | */ |
11399 | | static SECStatus |
11400 | | ssl3_HandleCertificateStatus(sslSocket *ss, PRUint8 *b, PRUint32 length) |
11401 | 51 | { |
11402 | 51 | SECStatus rv; |
11403 | | |
11404 | 51 | if (ss->ssl3.hs.ws != wait_certificate_status) { |
11405 | 3 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
11406 | 3 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERT_STATUS); |
11407 | 3 | return SECFailure; |
11408 | 3 | } |
11409 | | |
11410 | 48 | rv = ssl_ReadCertificateStatus(ss, b, length); |
11411 | 48 | if (rv != SECSuccess) { |
11412 | 43 | return SECFailure; /* code already set */ |
11413 | 43 | } |
11414 | | |
11415 | 5 | return ssl3_AuthCertificate(ss); |
11416 | 48 | } |
11417 | | |
11418 | | SECStatus |
11419 | | ssl_ReadCertificateStatus(sslSocket *ss, PRUint8 *b, PRUint32 length) |
11420 | 49 | { |
11421 | 49 | PRUint32 status, len; |
11422 | 49 | SECStatus rv; |
11423 | | |
11424 | 49 | PORT_Assert(!ss->sec.isServer); |
11425 | | |
11426 | | /* Consume the CertificateStatusType enum */ |
11427 | 49 | rv = ssl3_ConsumeHandshakeNumber(ss, &status, 1, &b, &length); |
11428 | 49 | if (rv != SECSuccess || status != 1 /* ocsp */) { |
11429 | 8 | return ssl3_DecodeError(ss); |
11430 | 8 | } |
11431 | | |
11432 | 41 | rv = ssl3_ConsumeHandshakeNumber(ss, &len, 3, &b, &length); |
11433 | 41 | if (rv != SECSuccess || len != length) { |
11434 | 36 | return ssl3_DecodeError(ss); |
11435 | 36 | } |
11436 | | |
11437 | 5 | #define MAX_CERTSTATUS_LEN 0x1ffff /* 128k - 1 */ |
11438 | 5 | if (length > MAX_CERTSTATUS_LEN) { |
11439 | 0 | ssl3_DecodeError(ss); /* sets error code */ |
11440 | 0 | return SECFailure; |
11441 | 0 | } |
11442 | 5 | #undef MAX_CERTSTATUS_LEN |
11443 | | |
11444 | | /* Array size 1, because we currently implement single-stapling only */ |
11445 | 5 | SECITEM_AllocArray(NULL, &ss->sec.ci.sid->peerCertStatus, 1); |
11446 | 5 | if (!ss->sec.ci.sid->peerCertStatus.items) |
11447 | 0 | return SECFailure; /* code already set */ |
11448 | | |
11449 | 5 | ss->sec.ci.sid->peerCertStatus.items[0].data = PORT_Alloc(length); |
11450 | | |
11451 | 5 | if (!ss->sec.ci.sid->peerCertStatus.items[0].data) { |
11452 | 0 | SECITEM_FreeArray(&ss->sec.ci.sid->peerCertStatus, PR_FALSE); |
11453 | 0 | return SECFailure; /* code already set */ |
11454 | 0 | } |
11455 | | |
11456 | 5 | PORT_Memcpy(ss->sec.ci.sid->peerCertStatus.items[0].data, b, length); |
11457 | 5 | ss->sec.ci.sid->peerCertStatus.items[0].len = length; |
11458 | 5 | ss->sec.ci.sid->peerCertStatus.items[0].type = siBuffer; |
11459 | 5 | return SECSuccess; |
11460 | 5 | } |
11461 | | |
11462 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered |
11463 | | * a complete ssl3 Certificate message. |
11464 | | * Caller must hold Handshake and RecvBuf locks. |
11465 | | */ |
11466 | | static SECStatus |
11467 | | ssl3_HandleCertificate(sslSocket *ss, PRUint8 *b, PRUint32 length) |
11468 | 38.4k | { |
11469 | 38.4k | SSL_TRC(3, ("%d: SSL3[%d]: handle certificate handshake", |
11470 | 38.4k | SSL_GETPID(), ss->fd)); |
11471 | 38.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
11472 | 38.4k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
11473 | | |
11474 | 38.4k | if ((ss->sec.isServer && ss->ssl3.hs.ws != wait_client_cert) || |
11475 | 38.4k | (!ss->sec.isServer && ss->ssl3.hs.ws != wait_server_cert)) { |
11476 | 31 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
11477 | 31 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERTIFICATE); |
11478 | 31 | return SECFailure; |
11479 | 31 | } |
11480 | | |
11481 | 38.4k | if (ss->sec.isServer) { |
11482 | 0 | dtls_ReceivedFirstMessageInFlight(ss); |
11483 | 0 | } |
11484 | | |
11485 | 38.4k | return ssl3_CompleteHandleCertificate(ss, b, length); |
11486 | 38.4k | } |
11487 | | |
11488 | | /* Called from ssl3_HandleCertificate |
11489 | | */ |
11490 | | SECStatus |
11491 | | ssl3_CompleteHandleCertificate(sslSocket *ss, PRUint8 *b, PRUint32 length) |
11492 | 38.4k | { |
11493 | 38.4k | ssl3CertNode *c; |
11494 | 38.4k | ssl3CertNode *lastCert = NULL; |
11495 | 38.4k | PRUint32 remaining = 0; |
11496 | 38.4k | PRUint32 size; |
11497 | 38.4k | SECStatus rv; |
11498 | 38.4k | PRBool isServer = ss->sec.isServer; |
11499 | 38.4k | PRBool isTLS; |
11500 | 38.4k | SSL3AlertDescription desc; |
11501 | 38.4k | int errCode = SSL_ERROR_RX_MALFORMED_CERTIFICATE; |
11502 | 38.4k | SECItem certItem; |
11503 | | |
11504 | 38.4k | ssl3_CleanupPeerCerts(ss); |
11505 | 38.4k | isTLS = (PRBool)(ss->ssl3.prSpec->version > SSL_LIBRARY_VERSION_3_0); |
11506 | | |
11507 | | /* It is reported that some TLS client sends a Certificate message |
11508 | | ** with a zero-length message body. We'll treat that case like a |
11509 | | ** normal no_certificates message to maximize interoperability. |
11510 | | */ |
11511 | 38.4k | if (length) { |
11512 | 38.4k | rv = ssl3_ConsumeHandshakeNumber(ss, &remaining, 3, &b, &length); |
11513 | 38.4k | if (rv != SECSuccess) |
11514 | 3 | goto loser; /* fatal alert already sent by ConsumeHandshake. */ |
11515 | 38.4k | if (remaining > length) |
11516 | 50 | goto decode_loser; |
11517 | 38.4k | } |
11518 | | |
11519 | 38.3k | if (!remaining) { |
11520 | 6 | if (!(isTLS && isServer)) { |
11521 | 6 | desc = bad_certificate; |
11522 | 6 | goto alert_loser; |
11523 | 6 | } |
11524 | | /* This is TLS's version of a no_certificate alert. */ |
11525 | | /* I'm a server. I've requested a client cert. He hasn't got one. */ |
11526 | 0 | rv = ssl3_HandleNoCertificate(ss); |
11527 | 0 | if (rv != SECSuccess) { |
11528 | 0 | errCode = PORT_GetError(); |
11529 | 0 | goto loser; |
11530 | 0 | } |
11531 | | |
11532 | 0 | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
11533 | 0 | ss->ssl3.hs.ws = wait_client_key; |
11534 | 0 | } else { |
11535 | 0 | TLS13_SET_HS_STATE(ss, wait_finished); |
11536 | 0 | } |
11537 | 0 | return SECSuccess; |
11538 | 0 | } |
11539 | | |
11540 | 38.3k | ss->ssl3.peerCertArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
11541 | 38.3k | if (ss->ssl3.peerCertArena == NULL) { |
11542 | 0 | goto loser; /* don't send alerts on memory errors */ |
11543 | 0 | } |
11544 | | |
11545 | | /* First get the peer cert. */ |
11546 | 38.3k | if (remaining < 3) |
11547 | 3 | goto decode_loser; |
11548 | | |
11549 | 38.3k | remaining -= 3; |
11550 | 38.3k | rv = ssl3_ConsumeHandshakeNumber(ss, &size, 3, &b, &length); |
11551 | 38.3k | if (rv != SECSuccess) |
11552 | 0 | goto loser; /* fatal alert already sent by ConsumeHandshake. */ |
11553 | 38.3k | if (size == 0 || remaining < size) |
11554 | 33 | goto decode_loser; |
11555 | | |
11556 | 38.3k | certItem.data = b; |
11557 | 38.3k | certItem.len = size; |
11558 | 38.3k | b += size; |
11559 | 38.3k | length -= size; |
11560 | 38.3k | remaining -= size; |
11561 | | |
11562 | 38.3k | ss->sec.peerCert = CERT_NewTempCertificate(ss->dbHandle, &certItem, NULL, |
11563 | 38.3k | PR_FALSE, PR_TRUE); |
11564 | 38.3k | if (ss->sec.peerCert == NULL) { |
11565 | | /* We should report an alert if the cert was bad, but not if the |
11566 | | * problem was just some local problem, like memory error. |
11567 | | */ |
11568 | 257 | goto ambiguous_err; |
11569 | 257 | } |
11570 | | |
11571 | | /* Now get all of the CA certs. */ |
11572 | 39.1k | while (remaining > 0) { |
11573 | 1.11k | if (remaining < 3) |
11574 | 3 | goto decode_loser; |
11575 | | |
11576 | 1.10k | remaining -= 3; |
11577 | 1.10k | rv = ssl3_ConsumeHandshakeNumber(ss, &size, 3, &b, &length); |
11578 | 1.10k | if (rv != SECSuccess) |
11579 | 0 | goto loser; /* fatal alert already sent by ConsumeHandshake. */ |
11580 | 1.10k | if (size == 0 || remaining < size) |
11581 | 74 | goto decode_loser; |
11582 | | |
11583 | 1.03k | certItem.data = b; |
11584 | 1.03k | certItem.len = size; |
11585 | 1.03k | b += size; |
11586 | 1.03k | length -= size; |
11587 | 1.03k | remaining -= size; |
11588 | | |
11589 | 1.03k | c = PORT_ArenaNew(ss->ssl3.peerCertArena, ssl3CertNode); |
11590 | 1.03k | if (c == NULL) { |
11591 | 0 | goto loser; /* don't send alerts on memory errors */ |
11592 | 0 | } |
11593 | | |
11594 | 1.03k | c->derCert = SECITEM_ArenaDupItem(ss->ssl3.peerCertArena, |
11595 | 1.03k | &certItem); |
11596 | 1.03k | if (c->derCert == NULL) { |
11597 | 0 | goto loser; |
11598 | 0 | } |
11599 | | |
11600 | 1.03k | c->next = NULL; |
11601 | 1.03k | if (lastCert) { |
11602 | 254 | lastCert->next = c; |
11603 | 780 | } else { |
11604 | 780 | ss->ssl3.peerCertChain = c; |
11605 | 780 | } |
11606 | 1.03k | lastCert = c; |
11607 | 1.03k | } |
11608 | | |
11609 | 38.0k | SECKEY_UpdateCertPQG(ss->sec.peerCert); |
11610 | | |
11611 | 38.0k | if (!isServer && |
11612 | 38.0k | ss->version < SSL_LIBRARY_VERSION_TLS_1_3 && |
11613 | 38.0k | ssl3_ExtensionNegotiated(ss, ssl_cert_status_xtn)) { |
11614 | 57 | ss->ssl3.hs.ws = wait_certificate_status; |
11615 | 57 | rv = SECSuccess; |
11616 | 37.9k | } else { |
11617 | 37.9k | rv = ssl3_AuthCertificate(ss); /* sets ss->ssl3.hs.ws */ |
11618 | 37.9k | } |
11619 | | |
11620 | 38.0k | return rv; |
11621 | | |
11622 | 257 | ambiguous_err: |
11623 | 257 | errCode = PORT_GetError(); |
11624 | 257 | switch (errCode) { |
11625 | 0 | case PR_OUT_OF_MEMORY_ERROR: |
11626 | 0 | case SEC_ERROR_BAD_DATABASE: |
11627 | 0 | case SEC_ERROR_NO_MEMORY: |
11628 | 0 | if (isTLS) { |
11629 | 0 | desc = internal_error; |
11630 | 0 | goto alert_loser; |
11631 | 0 | } |
11632 | 0 | goto loser; |
11633 | 257 | } |
11634 | 257 | ssl3_SendAlertForCertError(ss, errCode); |
11635 | 257 | goto loser; |
11636 | | |
11637 | 163 | decode_loser: |
11638 | 163 | desc = isTLS ? decode_error : bad_certificate; |
11639 | | |
11640 | 169 | alert_loser: |
11641 | 169 | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
11642 | | |
11643 | 429 | loser: |
11644 | 429 | (void)ssl_MapLowLevelError(errCode); |
11645 | 429 | return SECFailure; |
11646 | 169 | } |
11647 | | |
11648 | | SECStatus |
11649 | | ssl_SetAuthKeyBits(sslSocket *ss, const SECKEYPublicKey *pubKey) |
11650 | 37.5k | { |
11651 | 37.5k | SECStatus rv; |
11652 | 37.5k | PRUint32 minKey = 0; |
11653 | 37.5k | PRInt32 optval; |
11654 | 37.5k | PRBool usePolicyLength = PR_TRUE; |
11655 | | |
11656 | 37.5k | rv = NSS_OptionGet(NSS_KEY_SIZE_POLICY_FLAGS, &optval); |
11657 | 37.5k | if (rv == SECSuccess) { |
11658 | 37.5k | usePolicyLength = (PRBool)((optval & NSS_KEY_SIZE_POLICY_SSL_FLAG) == NSS_KEY_SIZE_POLICY_SSL_FLAG); |
11659 | 37.5k | } |
11660 | | |
11661 | 37.5k | ss->sec.authKeyBits = SECKEY_PublicKeyStrengthInBits(pubKey); |
11662 | 37.5k | switch (SECKEY_GetPublicKeyType(pubKey)) { |
11663 | 13.1k | case rsaKey: |
11664 | 13.1k | case rsaPssKey: |
11665 | 13.1k | case rsaOaepKey: |
11666 | 13.1k | rv = usePolicyLength ? NSS_OptionGet(NSS_RSA_MIN_KEY_SIZE, &optval) |
11667 | 13.1k | : SECFailure; |
11668 | 13.1k | if (rv == SECSuccess && optval > 0) { |
11669 | 13.1k | minKey = (PRUint32)optval; |
11670 | 13.1k | } else { |
11671 | 0 | minKey = SSL_RSA_MIN_MODULUS_BITS; |
11672 | 0 | } |
11673 | 13.1k | break; |
11674 | | |
11675 | 921 | case dsaKey: |
11676 | 921 | rv = usePolicyLength ? NSS_OptionGet(NSS_DSA_MIN_KEY_SIZE, &optval) |
11677 | 921 | : SECFailure; |
11678 | 921 | if (rv == SECSuccess && optval > 0) { |
11679 | 921 | minKey = (PRUint32)optval; |
11680 | 921 | } else { |
11681 | 0 | minKey = SSL_DSA_MIN_P_BITS; |
11682 | 0 | } |
11683 | 921 | break; |
11684 | | |
11685 | 2 | case dhKey: |
11686 | 2 | rv = usePolicyLength ? NSS_OptionGet(NSS_DH_MIN_KEY_SIZE, &optval) |
11687 | 2 | : SECFailure; |
11688 | 2 | if (rv == SECSuccess && optval > 0) { |
11689 | 2 | minKey = (PRUint32)optval; |
11690 | 2 | } else { |
11691 | 0 | minKey = SSL_DH_MIN_P_BITS; |
11692 | 0 | } |
11693 | 2 | break; |
11694 | | |
11695 | 23.5k | case ecKey: |
11696 | 23.5k | rv = usePolicyLength ? NSS_OptionGet(NSS_ECC_MIN_KEY_SIZE, &optval) |
11697 | 23.5k | : SECFailure; |
11698 | 23.5k | if (rv == SECSuccess && optval > 0) { |
11699 | 23.5k | minKey = (PRUint32)optval; |
11700 | 23.5k | } else { |
11701 | | /* Don't check EC strength here on the understanding that we |
11702 | | * only support curves we like. */ |
11703 | 0 | minKey = ss->sec.authKeyBits; |
11704 | 0 | } |
11705 | 23.5k | break; |
11706 | | |
11707 | 0 | default: |
11708 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
11709 | 0 | return SECFailure; |
11710 | 37.5k | } |
11711 | | |
11712 | | /* Too small: not good enough. Send a fatal alert. */ |
11713 | 37.5k | if (ss->sec.authKeyBits < minKey) { |
11714 | 172 | FATAL_ERROR(ss, SSL_ERROR_WEAK_SERVER_CERT_KEY, |
11715 | 172 | ss->version >= SSL_LIBRARY_VERSION_TLS_1_0 |
11716 | 172 | ? insufficient_security |
11717 | 172 | : illegal_parameter); |
11718 | 172 | return SECFailure; |
11719 | 172 | } |
11720 | | |
11721 | | /* PreliminaryChannelInfo.authKeyBits, scheme, and peerDelegCred are now valid. */ |
11722 | 37.4k | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_peer_auth; |
11723 | | |
11724 | 37.4k | return SECSuccess; |
11725 | 37.5k | } |
11726 | | |
11727 | | SECStatus |
11728 | | ssl3_HandleServerSpki(sslSocket *ss) |
11729 | 37.9k | { |
11730 | 37.9k | PORT_Assert(!ss->sec.isServer); |
11731 | 37.9k | SECKEYPublicKey *pubKey; |
11732 | | |
11733 | 37.9k | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
11734 | 37.9k | tls13_IsVerifyingWithDelegatedCredential(ss)) { |
11735 | 0 | sslDelegatedCredential *dc = ss->xtnData.peerDelegCred; |
11736 | 0 | pubKey = SECKEY_ExtractPublicKey(dc->spki); |
11737 | 0 | if (!pubKey) { |
11738 | 0 | PORT_SetError(SSL_ERROR_EXTRACT_PUBLIC_KEY_FAILURE); |
11739 | 0 | return SECFailure; |
11740 | 0 | } |
11741 | | |
11742 | | /* Because we have only a single authType (ssl_auth_tls13_any) |
11743 | | * for TLS 1.3 at this point, set the scheme so that the |
11744 | | * callback can interpret |authKeyBits| correctly. |
11745 | | */ |
11746 | 0 | ss->sec.signatureScheme = dc->expectedCertVerifyAlg; |
11747 | 37.9k | } else { |
11748 | 37.9k | pubKey = CERT_ExtractPublicKey(ss->sec.peerCert); |
11749 | 37.9k | if (!pubKey) { |
11750 | 396 | PORT_SetError(SSL_ERROR_EXTRACT_PUBLIC_KEY_FAILURE); |
11751 | 396 | return SECFailure; |
11752 | 396 | } |
11753 | 37.9k | } |
11754 | | |
11755 | 37.5k | SECStatus rv = ssl_SetAuthKeyBits(ss, pubKey); |
11756 | 37.5k | SECKEY_DestroyPublicKey(pubKey); |
11757 | 37.5k | if (rv != SECSuccess) { |
11758 | 172 | return rv; /* Alert sent and code set. */ |
11759 | 172 | } |
11760 | | |
11761 | 37.4k | return SECSuccess; |
11762 | 37.5k | } |
11763 | | |
11764 | | SECStatus |
11765 | | ssl3_AuthCertificate(sslSocket *ss) |
11766 | 37.9k | { |
11767 | 37.9k | SECStatus rv; |
11768 | 37.9k | PRBool isServer = ss->sec.isServer; |
11769 | 37.9k | int errCode; |
11770 | | |
11771 | 37.9k | ss->ssl3.hs.authCertificatePending = PR_FALSE; |
11772 | | |
11773 | 37.9k | PORT_Assert((ss->ssl3.hs.preliminaryInfo & ssl_preinfo_all) == |
11774 | 37.9k | ssl_preinfo_all); |
11775 | | |
11776 | 37.9k | if (!ss->sec.isServer) { |
11777 | | /* Set the |spki| used to verify the handshake. When verifying with a |
11778 | | * delegated credential (DC), this corresponds to the DC public key; |
11779 | | * otherwise it correspond to the public key of the peer's end-entity |
11780 | | * certificate. */ |
11781 | 37.9k | rv = ssl3_HandleServerSpki(ss); |
11782 | 37.9k | if (rv != SECSuccess) { |
11783 | | /* Alert sent and code set (if not SSL_ERROR_EXTRACT_PUBLIC_KEY_FAILURE). |
11784 | | * In either case, we're done here. */ |
11785 | 568 | errCode = PORT_GetError(); |
11786 | 568 | goto loser; |
11787 | 568 | } |
11788 | | |
11789 | 37.4k | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
11790 | 37.4k | ss->sec.authType = ss->ssl3.hs.kea_def->authKeyType; |
11791 | 37.4k | ss->sec.keaType = ss->ssl3.hs.kea_def->exchKeyType; |
11792 | 37.4k | } |
11793 | 37.4k | } |
11794 | | |
11795 | | /* |
11796 | | * Ask caller-supplied callback function to validate cert chain. |
11797 | | */ |
11798 | 37.4k | rv = (SECStatus)(*ss->authCertificate)(ss->authCertificateArg, ss->fd, |
11799 | 37.4k | PR_TRUE, isServer); |
11800 | 37.4k | if (rv != SECSuccess) { |
11801 | 113 | errCode = PORT_GetError(); |
11802 | 113 | if (errCode == 0) { |
11803 | 2 | errCode = SSL_ERROR_BAD_CERTIFICATE; |
11804 | 2 | } |
11805 | 113 | if (rv != SECWouldBlock) { |
11806 | 113 | if (ss->handleBadCert) { |
11807 | 0 | rv = (*ss->handleBadCert)(ss->badCertArg, ss->fd); |
11808 | 0 | } |
11809 | 113 | } |
11810 | | |
11811 | 113 | if (rv == SECWouldBlock) { |
11812 | 0 | if (ss->sec.isServer) { |
11813 | 0 | errCode = SSL_ERROR_FEATURE_NOT_SUPPORTED_FOR_SERVERS; |
11814 | 0 | goto loser; |
11815 | 0 | } |
11816 | | |
11817 | 0 | ss->ssl3.hs.authCertificatePending = PR_TRUE; |
11818 | 0 | rv = SECSuccess; |
11819 | 0 | } |
11820 | | |
11821 | 113 | if (rv != SECSuccess) { |
11822 | 113 | ssl3_SendAlertForCertError(ss, errCode); |
11823 | 113 | goto loser; |
11824 | 113 | } |
11825 | 113 | } |
11826 | | |
11827 | 37.2k | if (ss->sec.ci.sid->peerCert) { |
11828 | 0 | CERT_DestroyCertificate(ss->sec.ci.sid->peerCert); |
11829 | 0 | } |
11830 | 37.2k | ss->sec.ci.sid->peerCert = CERT_DupCertificate(ss->sec.peerCert); |
11831 | | |
11832 | 37.2k | if (!ss->sec.isServer) { |
11833 | 37.2k | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
11834 | 0 | TLS13_SET_HS_STATE(ss, wait_cert_verify); |
11835 | 37.2k | } else { |
11836 | | /* Ephemeral suites require ServerKeyExchange. */ |
11837 | 37.2k | if (ss->ssl3.hs.kea_def->ephemeral) { |
11838 | | /* require server_key_exchange */ |
11839 | 8.95k | ss->ssl3.hs.ws = wait_server_key; |
11840 | 28.3k | } else { |
11841 | | /* disallow server_key_exchange */ |
11842 | 28.3k | ss->ssl3.hs.ws = wait_cert_request; |
11843 | | /* This is static RSA key exchange so set the key exchange |
11844 | | * details to compensate for that. */ |
11845 | 28.3k | ss->sec.keaKeyBits = ss->sec.authKeyBits; |
11846 | 28.3k | ss->sec.signatureScheme = ssl_sig_none; |
11847 | 28.3k | ss->sec.keaGroup = NULL; |
11848 | 28.3k | } |
11849 | 37.2k | } |
11850 | 37.2k | } else { |
11851 | | /* Server */ |
11852 | 0 | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
11853 | 0 | ss->ssl3.hs.ws = wait_client_key; |
11854 | 0 | } else { |
11855 | 0 | TLS13_SET_HS_STATE(ss, wait_cert_verify); |
11856 | 0 | } |
11857 | 0 | } |
11858 | | |
11859 | 37.2k | PORT_Assert(rv == SECSuccess); |
11860 | 37.2k | if (rv != SECSuccess) { |
11861 | 0 | errCode = SEC_ERROR_LIBRARY_FAILURE; |
11862 | 0 | goto loser; |
11863 | 0 | } |
11864 | | |
11865 | 37.2k | return SECSuccess; |
11866 | | |
11867 | 681 | loser: |
11868 | 681 | (void)ssl_MapLowLevelError(errCode); |
11869 | 681 | return SECFailure; |
11870 | 37.2k | } |
11871 | | |
11872 | | static SECStatus ssl3_FinishHandshake(sslSocket *ss); |
11873 | | |
11874 | | static SECStatus |
11875 | | ssl3_AlwaysFail(sslSocket *ss) |
11876 | 0 | { |
11877 | | /* The caller should have cleared the callback. */ |
11878 | 0 | ss->ssl3.hs.restartTarget = ssl3_AlwaysFail; |
11879 | 0 | PORT_SetError(PR_INVALID_STATE_ERROR); |
11880 | 0 | return SECFailure; |
11881 | 0 | } |
11882 | | |
11883 | | /* Caller must hold 1stHandshakeLock. |
11884 | | */ |
11885 | | SECStatus |
11886 | | ssl3_AuthCertificateComplete(sslSocket *ss, PRErrorCode error) |
11887 | 0 | { |
11888 | 0 | SECStatus rv; |
11889 | |
|
11890 | 0 | PORT_Assert(ss->opt.noLocks || ssl_Have1stHandshakeLock(ss)); |
11891 | |
|
11892 | 0 | if (ss->sec.isServer) { |
11893 | 0 | PORT_SetError(SSL_ERROR_FEATURE_NOT_SUPPORTED_FOR_SERVERS); |
11894 | 0 | return SECFailure; |
11895 | 0 | } |
11896 | | |
11897 | 0 | ssl_GetRecvBufLock(ss); |
11898 | 0 | ssl_GetSSL3HandshakeLock(ss); |
11899 | |
|
11900 | 0 | if (!ss->ssl3.hs.authCertificatePending) { |
11901 | 0 | PORT_SetError(PR_INVALID_STATE_ERROR); |
11902 | 0 | rv = SECFailure; |
11903 | 0 | goto done; |
11904 | 0 | } |
11905 | | |
11906 | 0 | ss->ssl3.hs.authCertificatePending = PR_FALSE; |
11907 | |
|
11908 | 0 | if (error != 0) { |
11909 | 0 | ss->ssl3.hs.restartTarget = ssl3_AlwaysFail; |
11910 | 0 | ssl3_SendAlertForCertError(ss, error); |
11911 | 0 | rv = SECSuccess; |
11912 | 0 | } else if (ss->ssl3.hs.restartTarget != NULL) { |
11913 | 0 | sslRestartTarget target = ss->ssl3.hs.restartTarget; |
11914 | 0 | ss->ssl3.hs.restartTarget = NULL; |
11915 | |
|
11916 | 0 | if (target == ssl3_FinishHandshake) { |
11917 | 0 | SSL_TRC(3, ("%d: SSL3[%p]: certificate authentication lost the race" |
11918 | 0 | " with peer's finished message", |
11919 | 0 | SSL_GETPID(), ss->fd)); |
11920 | 0 | } |
11921 | |
|
11922 | 0 | rv = target(ss); |
11923 | 0 | } else { |
11924 | 0 | SSL_TRC(3, ("%d: SSL3[%p]: certificate authentication won the race with" |
11925 | 0 | " peer's finished message", |
11926 | 0 | SSL_GETPID(), ss->fd)); |
11927 | |
|
11928 | 0 | PORT_Assert(!ss->ssl3.hs.isResuming); |
11929 | 0 | PORT_Assert(ss->ssl3.hs.ws != idle_handshake); |
11930 | |
|
11931 | 0 | if (ss->opt.enableFalseStart && |
11932 | 0 | !ss->firstHsDone && |
11933 | 0 | !ss->ssl3.hs.isResuming && |
11934 | 0 | ssl3_WaitingForServerSecondRound(ss)) { |
11935 | | /* ssl3_SendClientSecondRound deferred the false start check because |
11936 | | * certificate authentication was pending, so we do it now if we still |
11937 | | * haven't received all of the server's second round yet. |
11938 | | */ |
11939 | 0 | rv = ssl3_CheckFalseStart(ss); |
11940 | 0 | } else { |
11941 | 0 | rv = SECSuccess; |
11942 | 0 | } |
11943 | 0 | } |
11944 | |
|
11945 | 0 | done: |
11946 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
11947 | 0 | ssl_ReleaseRecvBufLock(ss); |
11948 | |
|
11949 | 0 | return rv; |
11950 | 0 | } |
11951 | | |
11952 | | static SECStatus |
11953 | | ssl3_ComputeTLSFinished(sslSocket *ss, ssl3CipherSpec *spec, |
11954 | | PRBool isServer, |
11955 | | const SSL3Hashes *hashes, |
11956 | | TLSFinished *tlsFinished) |
11957 | 64.4k | { |
11958 | 64.4k | SECStatus rv; |
11959 | 64.4k | CK_TLS_MAC_PARAMS tls_mac_params; |
11960 | 64.4k | SECItem param = { siBuffer, NULL, 0 }; |
11961 | 64.4k | PK11Context *prf_context; |
11962 | 64.4k | unsigned int retLen; |
11963 | | |
11964 | 64.4k | PORT_Assert(spec->masterSecret); |
11965 | 64.4k | if (!spec->masterSecret) { |
11966 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
11967 | 0 | return SECFailure; |
11968 | 0 | } |
11969 | | |
11970 | 64.4k | if (spec->version < SSL_LIBRARY_VERSION_TLS_1_2) { |
11971 | 47.2k | tls_mac_params.prfHashMechanism = CKM_TLS_PRF; |
11972 | 47.2k | } else { |
11973 | 17.2k | tls_mac_params.prfHashMechanism = ssl3_GetPrfHashMechanism(ss); |
11974 | 17.2k | } |
11975 | 64.4k | tls_mac_params.ulMacLength = 12; |
11976 | 64.4k | tls_mac_params.ulServerOrClient = isServer ? 1 : 2; |
11977 | 64.4k | param.data = (unsigned char *)&tls_mac_params; |
11978 | 64.4k | param.len = sizeof(tls_mac_params); |
11979 | 64.4k | prf_context = PK11_CreateContextBySymKey(CKM_TLS_MAC, CKA_SIGN, |
11980 | 64.4k | spec->masterSecret, ¶m); |
11981 | 64.4k | if (!prf_context) |
11982 | 0 | return SECFailure; |
11983 | | |
11984 | 64.4k | rv = PK11_DigestBegin(prf_context); |
11985 | 64.4k | rv |= PK11_DigestOp(prf_context, hashes->u.raw, hashes->len); |
11986 | 64.4k | rv |= PK11_DigestFinal(prf_context, tlsFinished->verify_data, &retLen, |
11987 | 64.4k | sizeof tlsFinished->verify_data); |
11988 | 64.4k | PORT_Assert(rv != SECSuccess || retLen == sizeof tlsFinished->verify_data); |
11989 | | |
11990 | 64.4k | PK11_DestroyContext(prf_context, PR_TRUE); |
11991 | | |
11992 | 64.4k | return rv; |
11993 | 64.4k | } |
11994 | | |
11995 | | /* The calling function must acquire and release the appropriate |
11996 | | * lock (e.g., ssl_GetSpecReadLock / ssl_ReleaseSpecReadLock for |
11997 | | * ss->ssl3.crSpec). |
11998 | | */ |
11999 | | SECStatus |
12000 | | ssl3_TLSPRFWithMasterSecret(sslSocket *ss, ssl3CipherSpec *spec, |
12001 | | const char *label, unsigned int labelLen, |
12002 | | const unsigned char *val, unsigned int valLen, |
12003 | | unsigned char *out, unsigned int outLen) |
12004 | 0 | { |
12005 | 0 | SECItem param = { siBuffer, NULL, 0 }; |
12006 | 0 | CK_MECHANISM_TYPE mech = CKM_TLS_PRF_GENERAL; |
12007 | 0 | PK11Context *prf_context; |
12008 | 0 | unsigned int retLen; |
12009 | 0 | SECStatus rv; |
12010 | |
|
12011 | 0 | if (!spec->masterSecret) { |
12012 | 0 | PORT_Assert(spec->masterSecret); |
12013 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
12014 | 0 | return SECFailure; |
12015 | 0 | } |
12016 | | |
12017 | 0 | if (spec->version >= SSL_LIBRARY_VERSION_TLS_1_2) { |
12018 | | /* Bug 1312976 non-SHA256 exporters are broken. */ |
12019 | 0 | if (ssl3_GetPrfHashMechanism(ss) != CKM_SHA256) { |
12020 | 0 | PORT_Assert(0); |
12021 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
12022 | 0 | return SECFailure; |
12023 | 0 | } |
12024 | 0 | mech = CKM_NSS_TLS_PRF_GENERAL_SHA256; |
12025 | 0 | } |
12026 | 0 | prf_context = PK11_CreateContextBySymKey(mech, CKA_SIGN, |
12027 | 0 | spec->masterSecret, ¶m); |
12028 | 0 | if (!prf_context) |
12029 | 0 | return SECFailure; |
12030 | | |
12031 | 0 | rv = PK11_DigestBegin(prf_context); |
12032 | 0 | rv |= PK11_DigestOp(prf_context, (unsigned char *)label, labelLen); |
12033 | 0 | rv |= PK11_DigestOp(prf_context, val, valLen); |
12034 | 0 | rv |= PK11_DigestFinal(prf_context, out, &retLen, outLen); |
12035 | 0 | PORT_Assert(rv != SECSuccess || retLen == outLen); |
12036 | |
|
12037 | 0 | PK11_DestroyContext(prf_context, PR_TRUE); |
12038 | 0 | return rv; |
12039 | 0 | } |
12040 | | |
12041 | | /* called from ssl3_SendClientSecondRound |
12042 | | * ssl3_HandleFinished |
12043 | | */ |
12044 | | static SECStatus |
12045 | | ssl3_SendNextProto(sslSocket *ss) |
12046 | 0 | { |
12047 | 0 | SECStatus rv; |
12048 | 0 | int padding_len; |
12049 | 0 | static const unsigned char padding[32] = { 0 }; |
12050 | |
|
12051 | 0 | if (ss->xtnData.nextProto.len == 0 || |
12052 | 0 | ss->xtnData.nextProtoState == SSL_NEXT_PROTO_SELECTED) { |
12053 | 0 | return SECSuccess; |
12054 | 0 | } |
12055 | | |
12056 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
12057 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
12058 | |
|
12059 | 0 | padding_len = 32 - ((ss->xtnData.nextProto.len + 2) % 32); |
12060 | |
|
12061 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_next_proto, ss->xtnData.nextProto.len + 2 + padding_len); |
12062 | 0 | if (rv != SECSuccess) { |
12063 | 0 | return rv; /* error code set by AppendHandshakeHeader */ |
12064 | 0 | } |
12065 | 0 | rv = ssl3_AppendHandshakeVariable(ss, ss->xtnData.nextProto.data, |
12066 | 0 | ss->xtnData.nextProto.len, 1); |
12067 | 0 | if (rv != SECSuccess) { |
12068 | 0 | return rv; /* error code set by AppendHandshake */ |
12069 | 0 | } |
12070 | 0 | rv = ssl3_AppendHandshakeVariable(ss, padding, padding_len, 1); |
12071 | 0 | if (rv != SECSuccess) { |
12072 | 0 | return rv; /* error code set by AppendHandshake */ |
12073 | 0 | } |
12074 | 0 | return rv; |
12075 | 0 | } |
12076 | | |
12077 | | /* called from ssl3_SendFinished and tls13_DeriveSecret. |
12078 | | * |
12079 | | * This function is simply a debugging aid and therefore does not return a |
12080 | | * SECStatus. */ |
12081 | | void |
12082 | | ssl3_RecordKeyLog(sslSocket *ss, const char *label, PK11SymKey *secret) |
12083 | 34.7k | { |
12084 | 34.7k | #ifdef NSS_ALLOW_SSLKEYLOGFILE |
12085 | 34.7k | SECStatus rv; |
12086 | 34.7k | SECItem *keyData; |
12087 | | /* Longest label is "CLIENT_HANDSHAKE_TRAFFIC_SECRET", master secret is 48 |
12088 | | * bytes which happens to be the largest in TLS 1.3 as well (SHA384). |
12089 | | * Maximum line length: "CLIENT_HANDSHAKE_TRAFFIC_SECRET" (31) + " " (1) + |
12090 | | * client_random (32*2) + " " (1) + |
12091 | | * traffic_secret (48*2) + "\n" (1) = 194. */ |
12092 | 34.7k | char buf[200]; |
12093 | 34.7k | unsigned int offset, len; |
12094 | | |
12095 | 34.7k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
12096 | | |
12097 | 34.7k | if (!ssl_keylog_iob) |
12098 | 34.7k | return; |
12099 | | |
12100 | 0 | rv = PK11_ExtractKeyValue(secret); |
12101 | 0 | if (rv != SECSuccess) |
12102 | 0 | return; |
12103 | | |
12104 | | /* keyData does not need to be freed. */ |
12105 | 0 | keyData = PK11_GetKeyData(secret); |
12106 | 0 | if (!keyData || !keyData->data) |
12107 | 0 | return; |
12108 | | |
12109 | 0 | len = strlen(label) + 1 + /* label + space */ |
12110 | 0 | SSL3_RANDOM_LENGTH * 2 + 1 + /* client random (hex) + space */ |
12111 | 0 | keyData->len * 2 + 1; /* secret (hex) + newline */ |
12112 | 0 | PORT_Assert(len <= sizeof(buf)); |
12113 | 0 | if (len > sizeof(buf)) |
12114 | 0 | return; |
12115 | | |
12116 | | /* https://developer.mozilla.org/en/NSS_Key_Log_Format */ |
12117 | | |
12118 | | /* There could be multiple, concurrent writers to the |
12119 | | * keylog, so we have to do everything in a single call to |
12120 | | * fwrite. */ |
12121 | | |
12122 | 0 | strcpy(buf, label); |
12123 | 0 | offset = strlen(label); |
12124 | 0 | buf[offset++] += ' '; |
12125 | 0 | hexEncode(buf + offset, ss->ssl3.hs.client_random, SSL3_RANDOM_LENGTH); |
12126 | 0 | offset += SSL3_RANDOM_LENGTH * 2; |
12127 | 0 | buf[offset++] = ' '; |
12128 | 0 | hexEncode(buf + offset, keyData->data, keyData->len); |
12129 | 0 | offset += keyData->len * 2; |
12130 | 0 | buf[offset++] = '\n'; |
12131 | |
|
12132 | 0 | PORT_Assert(offset == len); |
12133 | |
|
12134 | 0 | PZ_Lock(ssl_keylog_lock); |
12135 | 0 | if (fwrite(buf, len, 1, ssl_keylog_iob) == 1) |
12136 | 0 | fflush(ssl_keylog_iob); |
12137 | 0 | PZ_Unlock(ssl_keylog_lock); |
12138 | 0 | #endif |
12139 | 0 | } |
12140 | | |
12141 | | /* called from ssl3_SendClientSecondRound |
12142 | | * ssl3_HandleClientHello |
12143 | | * ssl3_HandleFinished |
12144 | | */ |
12145 | | static SECStatus |
12146 | | ssl3_SendFinished(sslSocket *ss, PRInt32 flags) |
12147 | 33.2k | { |
12148 | 33.2k | ssl3CipherSpec *cwSpec; |
12149 | 33.2k | PRBool isTLS; |
12150 | 33.2k | PRBool isServer = ss->sec.isServer; |
12151 | 33.2k | SECStatus rv; |
12152 | 33.2k | SSL3Sender sender = isServer ? sender_server : sender_client; |
12153 | 33.2k | SSL3Hashes hashes; |
12154 | 33.2k | TLSFinished tlsFinished; |
12155 | | |
12156 | 33.2k | SSL_TRC(3, ("%d: SSL3[%d]: send finished handshake", SSL_GETPID(), ss->fd)); |
12157 | | |
12158 | 33.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
12159 | 33.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
12160 | 33.2k | PR_ASSERT(!ss->ssl3.hs.clientCertificatePending); |
12161 | | |
12162 | 33.2k | ssl_GetSpecReadLock(ss); |
12163 | 33.2k | cwSpec = ss->ssl3.cwSpec; |
12164 | 33.2k | isTLS = (PRBool)(cwSpec->version > SSL_LIBRARY_VERSION_3_0); |
12165 | 33.2k | rv = ssl3_ComputeHandshakeHashes(ss, cwSpec, &hashes, sender); |
12166 | 33.2k | if (isTLS && rv == SECSuccess) { |
12167 | 33.2k | rv = ssl3_ComputeTLSFinished(ss, cwSpec, isServer, &hashes, &tlsFinished); |
12168 | 33.2k | } |
12169 | 33.2k | ssl_ReleaseSpecReadLock(ss); |
12170 | 33.2k | if (rv != SECSuccess) { |
12171 | 0 | goto fail; /* err code was set by ssl3_ComputeHandshakeHashes */ |
12172 | 0 | } |
12173 | | |
12174 | 33.2k | if (isTLS) { |
12175 | 33.2k | if (isServer) |
12176 | 0 | ss->ssl3.hs.finishedMsgs.tFinished[1] = tlsFinished; |
12177 | 33.2k | else |
12178 | 33.2k | ss->ssl3.hs.finishedMsgs.tFinished[0] = tlsFinished; |
12179 | 33.2k | ss->ssl3.hs.finishedBytes = sizeof tlsFinished; |
12180 | 33.2k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_finished, sizeof tlsFinished); |
12181 | 33.2k | if (rv != SECSuccess) |
12182 | 0 | goto fail; /* err set by AppendHandshake. */ |
12183 | 33.2k | rv = ssl3_AppendHandshake(ss, &tlsFinished, sizeof tlsFinished); |
12184 | 33.2k | if (rv != SECSuccess) |
12185 | 0 | goto fail; /* err set by AppendHandshake. */ |
12186 | 33.2k | } else { |
12187 | 0 | if (isServer) |
12188 | 0 | ss->ssl3.hs.finishedMsgs.sFinished[1] = hashes.u.s; |
12189 | 0 | else |
12190 | 0 | ss->ssl3.hs.finishedMsgs.sFinished[0] = hashes.u.s; |
12191 | 0 | PORT_Assert(hashes.len == sizeof hashes.u.s); |
12192 | 0 | ss->ssl3.hs.finishedBytes = sizeof hashes.u.s; |
12193 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_finished, sizeof hashes.u.s); |
12194 | 0 | if (rv != SECSuccess) |
12195 | 0 | goto fail; /* err set by AppendHandshake. */ |
12196 | 0 | rv = ssl3_AppendHandshake(ss, &hashes.u.s, sizeof hashes.u.s); |
12197 | 0 | if (rv != SECSuccess) |
12198 | 0 | goto fail; /* err set by AppendHandshake. */ |
12199 | 0 | } |
12200 | 33.2k | rv = ssl3_FlushHandshake(ss, flags); |
12201 | 33.2k | if (rv != SECSuccess) { |
12202 | 0 | goto fail; /* error code set by ssl3_FlushHandshake */ |
12203 | 0 | } |
12204 | | |
12205 | 33.2k | ssl3_RecordKeyLog(ss, "CLIENT_RANDOM", ss->ssl3.cwSpec->masterSecret); |
12206 | | |
12207 | 33.2k | return SECSuccess; |
12208 | | |
12209 | 0 | fail: |
12210 | 0 | return rv; |
12211 | 33.2k | } |
12212 | | |
12213 | | /* wrap the master secret, and put it into the SID. |
12214 | | * Caller holds the Spec read lock. |
12215 | | */ |
12216 | | SECStatus |
12217 | | ssl3_CacheWrappedSecret(sslSocket *ss, sslSessionID *sid, |
12218 | | PK11SymKey *secret) |
12219 | 15.9k | { |
12220 | 15.9k | PK11SymKey *wrappingKey = NULL; |
12221 | 15.9k | PK11SlotInfo *symKeySlot; |
12222 | 15.9k | void *pwArg = ss->pkcs11PinArg; |
12223 | 15.9k | SECStatus rv = SECFailure; |
12224 | 15.9k | PRBool isServer = ss->sec.isServer; |
12225 | 15.9k | CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
12226 | | |
12227 | 15.9k | symKeySlot = PK11_GetSlotFromKey(secret); |
12228 | 15.9k | if (!isServer) { |
12229 | 15.9k | int wrapKeyIndex; |
12230 | 15.9k | int incarnation; |
12231 | | |
12232 | | /* these next few functions are mere accessors and don't fail. */ |
12233 | 15.9k | sid->u.ssl3.masterWrapIndex = wrapKeyIndex = |
12234 | 15.9k | PK11_GetCurrentWrapIndex(symKeySlot); |
12235 | 15.9k | PORT_Assert(wrapKeyIndex == 0); /* array has only one entry! */ |
12236 | | |
12237 | 15.9k | sid->u.ssl3.masterWrapSeries = incarnation = |
12238 | 15.9k | PK11_GetSlotSeries(symKeySlot); |
12239 | 15.9k | sid->u.ssl3.masterSlotID = PK11_GetSlotID(symKeySlot); |
12240 | 15.9k | sid->u.ssl3.masterModuleID = PK11_GetModuleID(symKeySlot); |
12241 | 15.9k | sid->u.ssl3.masterValid = PR_TRUE; |
12242 | | /* Get the default wrapping key, for wrapping the master secret before |
12243 | | * placing it in the SID cache entry. */ |
12244 | 15.9k | wrappingKey = PK11_GetWrapKey(symKeySlot, wrapKeyIndex, |
12245 | 15.9k | CKM_INVALID_MECHANISM, incarnation, |
12246 | 15.9k | pwArg); |
12247 | 15.9k | if (wrappingKey) { |
12248 | 15.9k | mechanism = PK11_GetMechanism(wrappingKey); /* can't fail. */ |
12249 | 15.9k | } else { |
12250 | 1 | int keyLength; |
12251 | | /* if the wrappingKey doesn't exist, attempt to create it. |
12252 | | * Note: we intentionally ignore errors here. If we cannot |
12253 | | * generate a wrapping key, it is not fatal to this SSL connection, |
12254 | | * but we will not be able to restart this session. |
12255 | | */ |
12256 | 1 | mechanism = PK11_GetBestWrapMechanism(symKeySlot); |
12257 | 1 | keyLength = PK11_GetBestKeyLength(symKeySlot, mechanism); |
12258 | | /* Zero length means fixed key length algorithm, or error. |
12259 | | * It's ambiguous. |
12260 | | */ |
12261 | 1 | wrappingKey = PK11_KeyGen(symKeySlot, mechanism, NULL, |
12262 | 1 | keyLength, pwArg); |
12263 | 1 | if (wrappingKey) { |
12264 | | /* The thread safety characteristics of PK11_[SG]etWrapKey is |
12265 | | * abominable. This protects against races in calling |
12266 | | * PK11_SetWrapKey by dropping and re-acquiring the canonical |
12267 | | * value once it is set. The mutex in PK11_[SG]etWrapKey will |
12268 | | * ensure that races produce the same value in the end. */ |
12269 | 1 | PK11_SetWrapKey(symKeySlot, wrapKeyIndex, wrappingKey); |
12270 | 1 | PK11_FreeSymKey(wrappingKey); |
12271 | 1 | wrappingKey = PK11_GetWrapKey(symKeySlot, wrapKeyIndex, |
12272 | 1 | CKM_INVALID_MECHANISM, incarnation, pwArg); |
12273 | 1 | if (!wrappingKey) { |
12274 | 0 | PK11_FreeSlot(symKeySlot); |
12275 | 0 | return SECFailure; |
12276 | 0 | } |
12277 | 1 | } |
12278 | 1 | } |
12279 | 15.9k | } else { |
12280 | | /* server socket using session cache. */ |
12281 | 0 | mechanism = PK11_GetBestWrapMechanism(symKeySlot); |
12282 | 0 | if (mechanism != CKM_INVALID_MECHANISM) { |
12283 | 0 | wrappingKey = |
12284 | 0 | ssl3_GetWrappingKey(ss, symKeySlot, mechanism, pwArg); |
12285 | 0 | if (wrappingKey) { |
12286 | 0 | mechanism = PK11_GetMechanism(wrappingKey); /* can't fail. */ |
12287 | 0 | } |
12288 | 0 | } |
12289 | 0 | } |
12290 | | |
12291 | 15.9k | sid->u.ssl3.masterWrapMech = mechanism; |
12292 | 15.9k | PK11_FreeSlot(symKeySlot); |
12293 | | |
12294 | 15.9k | if (wrappingKey) { |
12295 | 15.9k | SECItem wmsItem; |
12296 | | |
12297 | 15.9k | wmsItem.data = sid->u.ssl3.keys.wrapped_master_secret; |
12298 | 15.9k | wmsItem.len = sizeof sid->u.ssl3.keys.wrapped_master_secret; |
12299 | 15.9k | rv = PK11_WrapSymKey(mechanism, NULL, wrappingKey, |
12300 | 15.9k | secret, &wmsItem); |
12301 | | /* rv is examined below. */ |
12302 | 15.9k | sid->u.ssl3.keys.wrapped_master_secret_len = wmsItem.len; |
12303 | 15.9k | PK11_FreeSymKey(wrappingKey); |
12304 | 15.9k | } |
12305 | 15.9k | return rv; |
12306 | 15.9k | } |
12307 | | |
12308 | | /* Called from ssl3_HandlePostHelloHandshakeMessage() when it has deciphered |
12309 | | * a complete ssl3 Finished message from the peer. |
12310 | | * Caller must hold Handshake and RecvBuf locks. |
12311 | | */ |
12312 | | static SECStatus |
12313 | | ssl3_HandleFinished(sslSocket *ss, PRUint8 *b, PRUint32 length) |
12314 | 31.2k | { |
12315 | 31.2k | SECStatus rv = SECSuccess; |
12316 | 31.2k | PRBool isServer = ss->sec.isServer; |
12317 | 31.2k | PRBool isTLS; |
12318 | 31.2k | SSL3Hashes hashes; |
12319 | | |
12320 | 31.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
12321 | 31.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
12322 | | |
12323 | 31.2k | SSL_TRC(3, ("%d: SSL3[%d]: handle finished handshake", |
12324 | 31.2k | SSL_GETPID(), ss->fd)); |
12325 | | |
12326 | 31.2k | if (ss->ssl3.hs.ws != wait_finished) { |
12327 | 31 | SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12328 | 31 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_FINISHED); |
12329 | 31 | return SECFailure; |
12330 | 31 | } |
12331 | | |
12332 | 31.2k | if (!ss->sec.isServer || !ss->opt.requestCertificate) { |
12333 | 31.2k | dtls_ReceivedFirstMessageInFlight(ss); |
12334 | 31.2k | } |
12335 | | |
12336 | 31.2k | rv = ssl3_ComputeHandshakeHashes(ss, ss->ssl3.crSpec, &hashes, |
12337 | 31.2k | isServer ? sender_client : sender_server); |
12338 | 31.2k | if (rv != SECSuccess) { |
12339 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
12340 | 0 | return SECFailure; |
12341 | 0 | } |
12342 | | |
12343 | 31.2k | rv = ssl_HashHandshakeMessage(ss, ssl_hs_finished, b, length); |
12344 | 31.2k | if (rv != SECSuccess) { |
12345 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
12346 | 0 | return rv; |
12347 | 0 | } |
12348 | | |
12349 | 31.2k | isTLS = (PRBool)(ss->ssl3.crSpec->version > SSL_LIBRARY_VERSION_3_0); |
12350 | 31.2k | if (isTLS) { |
12351 | 31.2k | TLSFinished tlsFinished; |
12352 | | |
12353 | 31.2k | if (length != sizeof(tlsFinished)) { |
12354 | | #ifndef UNSAFE_FUZZER_MODE |
12355 | | (void)SSL3_SendAlert(ss, alert_fatal, decode_error); |
12356 | | PORT_SetError(SSL_ERROR_RX_MALFORMED_FINISHED); |
12357 | | return SECFailure; |
12358 | | #endif |
12359 | 31.2k | } |
12360 | 31.2k | rv = ssl3_ComputeTLSFinished(ss, ss->ssl3.crSpec, !isServer, |
12361 | 31.2k | &hashes, &tlsFinished); |
12362 | 31.2k | if (!isServer) |
12363 | 31.2k | ss->ssl3.hs.finishedMsgs.tFinished[1] = tlsFinished; |
12364 | 0 | else |
12365 | 0 | ss->ssl3.hs.finishedMsgs.tFinished[0] = tlsFinished; |
12366 | 31.2k | ss->ssl3.hs.finishedBytes = sizeof(tlsFinished); |
12367 | 31.2k | if (rv != SECSuccess || |
12368 | 31.2k | 0 != NSS_SecureMemcmp(&tlsFinished, b, |
12369 | 31.2k | PR_MIN(length, ss->ssl3.hs.finishedBytes))) { |
12370 | | #ifndef UNSAFE_FUZZER_MODE |
12371 | | (void)SSL3_SendAlert(ss, alert_fatal, decrypt_error); |
12372 | | PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE); |
12373 | | return SECFailure; |
12374 | | #endif |
12375 | 21 | } |
12376 | 31.2k | } else { |
12377 | 0 | if (length != sizeof(SSL3Finished)) { |
12378 | 0 | (void)ssl3_IllegalParameter(ss); |
12379 | 0 | PORT_SetError(SSL_ERROR_RX_MALFORMED_FINISHED); |
12380 | 0 | return SECFailure; |
12381 | 0 | } |
12382 | | |
12383 | 0 | if (!isServer) |
12384 | 0 | ss->ssl3.hs.finishedMsgs.sFinished[1] = hashes.u.s; |
12385 | 0 | else |
12386 | 0 | ss->ssl3.hs.finishedMsgs.sFinished[0] = hashes.u.s; |
12387 | 0 | PORT_Assert(hashes.len == sizeof hashes.u.s); |
12388 | 0 | ss->ssl3.hs.finishedBytes = sizeof hashes.u.s; |
12389 | 0 | if (0 != NSS_SecureMemcmp(&hashes.u.s, b, length)) { |
12390 | 0 | (void)ssl3_HandshakeFailure(ss); |
12391 | 0 | PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE); |
12392 | 0 | return SECFailure; |
12393 | 0 | } |
12394 | 0 | } |
12395 | | |
12396 | 31.2k | ssl_GetXmitBufLock(ss); /*************************************/ |
12397 | | |
12398 | 31.2k | if ((isServer && !ss->ssl3.hs.isResuming) || |
12399 | 31.2k | (!isServer && ss->ssl3.hs.isResuming)) { |
12400 | 0 | PRInt32 flags = 0; |
12401 | | |
12402 | | /* Send a NewSessionTicket message if the client sent us |
12403 | | * either an empty session ticket, or one that did not verify. |
12404 | | * (Note that if either of these conditions was met, then the |
12405 | | * server has sent a SessionTicket extension in the |
12406 | | * ServerHello message.) |
12407 | | */ |
12408 | 0 | if (isServer && !ss->ssl3.hs.isResuming && |
12409 | 0 | ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn) && |
12410 | 0 | ssl3_KEASupportsTickets(ss->ssl3.hs.kea_def)) { |
12411 | | /* RFC 5077 Section 3.3: "In the case of a full handshake, the |
12412 | | * server MUST verify the client's Finished message before sending |
12413 | | * the ticket." Presumably, this also means that the client's |
12414 | | * certificate, if any, must be verified beforehand too. |
12415 | | */ |
12416 | 0 | rv = ssl3_SendNewSessionTicket(ss); |
12417 | 0 | if (rv != SECSuccess) { |
12418 | 0 | goto xmit_loser; |
12419 | 0 | } |
12420 | 0 | } |
12421 | | |
12422 | 0 | rv = ssl3_SendChangeCipherSpecs(ss); |
12423 | 0 | if (rv != SECSuccess) { |
12424 | 0 | goto xmit_loser; /* err is set. */ |
12425 | 0 | } |
12426 | | /* If this thread is in SSL_SecureSend (trying to write some data) |
12427 | | ** then set the ssl_SEND_FLAG_FORCE_INTO_BUFFER flag, so that the |
12428 | | ** last two handshake messages (change cipher spec and finished) |
12429 | | ** will be sent in the same send/write call as the application data. |
12430 | | */ |
12431 | 0 | if (ss->writerThread == PR_GetCurrentThread()) { |
12432 | 0 | flags = ssl_SEND_FLAG_FORCE_INTO_BUFFER; |
12433 | 0 | } |
12434 | |
|
12435 | 0 | if (!isServer && !ss->firstHsDone) { |
12436 | 0 | rv = ssl3_SendNextProto(ss); |
12437 | 0 | if (rv != SECSuccess) { |
12438 | 0 | goto xmit_loser; /* err code was set. */ |
12439 | 0 | } |
12440 | 0 | } |
12441 | | |
12442 | 0 | if (IS_DTLS(ss)) { |
12443 | 0 | flags |= ssl_SEND_FLAG_NO_RETRANSMIT; |
12444 | 0 | } |
12445 | |
|
12446 | 0 | rv = ssl3_SendFinished(ss, flags); |
12447 | 0 | if (rv != SECSuccess) { |
12448 | 0 | goto xmit_loser; /* err is set. */ |
12449 | 0 | } |
12450 | 0 | } |
12451 | | |
12452 | 31.2k | xmit_loser: |
12453 | 31.2k | ssl_ReleaseXmitBufLock(ss); /*************************************/ |
12454 | 31.2k | if (rv != SECSuccess) { |
12455 | 0 | return rv; |
12456 | 0 | } |
12457 | | |
12458 | 31.2k | if (ss->ssl3.hs.authCertificatePending) { |
12459 | 0 | if (ss->ssl3.hs.restartTarget) { |
12460 | 0 | PR_NOT_REACHED("ssl3_HandleFinished: unexpected restartTarget"); |
12461 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
12462 | 0 | return SECFailure; |
12463 | 0 | } |
12464 | | |
12465 | 0 | ss->ssl3.hs.restartTarget = ssl3_FinishHandshake; |
12466 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
12467 | 0 | return SECFailure; |
12468 | 0 | } |
12469 | | |
12470 | 31.2k | rv = ssl3_FinishHandshake(ss); |
12471 | 31.2k | return rv; |
12472 | 31.2k | } |
12473 | | |
12474 | | SECStatus |
12475 | | ssl3_FillInCachedSID(sslSocket *ss, sslSessionID *sid, PK11SymKey *secret) |
12476 | 15.9k | { |
12477 | 15.9k | PORT_Assert(secret); |
12478 | | |
12479 | | /* fill in the sid */ |
12480 | 15.9k | sid->u.ssl3.cipherSuite = ss->ssl3.hs.cipher_suite; |
12481 | 15.9k | sid->u.ssl3.policy = ss->ssl3.policy; |
12482 | 15.9k | sid->version = ss->version; |
12483 | 15.9k | sid->authType = ss->sec.authType; |
12484 | 15.9k | sid->authKeyBits = ss->sec.authKeyBits; |
12485 | 15.9k | sid->keaType = ss->sec.keaType; |
12486 | 15.9k | sid->keaKeyBits = ss->sec.keaKeyBits; |
12487 | 15.9k | if (ss->sec.keaGroup) { |
12488 | 13.8k | sid->keaGroup = ss->sec.keaGroup->name; |
12489 | 13.8k | } else { |
12490 | 2.09k | sid->keaGroup = ssl_grp_none; |
12491 | 2.09k | } |
12492 | 15.9k | sid->sigScheme = ss->sec.signatureScheme; |
12493 | 15.9k | sid->lastAccessTime = sid->creationTime = ssl_Time(ss); |
12494 | 15.9k | sid->expirationTime = sid->creationTime + (ssl_ticket_lifetime * PR_USEC_PER_SEC); |
12495 | 15.9k | if (sid->localCert) { |
12496 | 0 | CERT_DestroyCertificate(sid->localCert); |
12497 | 0 | } |
12498 | 15.9k | sid->localCert = CERT_DupCertificate(ss->sec.localCert); |
12499 | 15.9k | if (ss->sec.isServer) { |
12500 | 0 | sid->namedCurve = ss->sec.serverCert->namedCurve; |
12501 | 0 | } |
12502 | | |
12503 | 15.9k | if (ss->xtnData.nextProtoState != SSL_NEXT_PROTO_NO_SUPPORT && |
12504 | 15.9k | ss->xtnData.nextProto.data) { |
12505 | 0 | SECITEM_FreeItem(&sid->u.ssl3.alpnSelection, PR_FALSE); |
12506 | 0 | if (SECITEM_CopyItem( |
12507 | 0 | NULL, &sid->u.ssl3.alpnSelection, &ss->xtnData.nextProto) != SECSuccess) { |
12508 | 0 | return SECFailure; /* error already set. */ |
12509 | 0 | } |
12510 | 0 | } |
12511 | | |
12512 | | /* Copy the master secret (wrapped or unwrapped) into the sid */ |
12513 | 15.9k | return ssl3_CacheWrappedSecret(ss, ss->sec.ci.sid, secret); |
12514 | 15.9k | } |
12515 | | |
12516 | | /* The return type is SECStatus instead of void because this function needs |
12517 | | * to have type sslRestartTarget. |
12518 | | */ |
12519 | | SECStatus |
12520 | | ssl3_FinishHandshake(sslSocket *ss) |
12521 | 31.2k | { |
12522 | 31.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
12523 | 31.2k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
12524 | 31.2k | PORT_Assert(ss->ssl3.hs.restartTarget == NULL); |
12525 | 31.2k | sslSessionID *sid = ss->sec.ci.sid; |
12526 | 31.2k | SECStatus sidRv = SECFailure; |
12527 | | |
12528 | | /* The first handshake is now completed. */ |
12529 | 31.2k | ss->handshake = NULL; |
12530 | | |
12531 | 31.2k | if (sid->cached == never_cached && !ss->opt.noCache) { |
12532 | | /* If the wrap fails, don't cache the sid. The connection proceeds |
12533 | | * normally, so the rv is only used to determine whether we cache. */ |
12534 | 15.9k | sidRv = ssl3_FillInCachedSID(ss, sid, ss->ssl3.crSpec->masterSecret); |
12535 | 15.9k | } |
12536 | | |
12537 | | /* RFC 5077 Section 3.3: "The client MUST NOT treat the ticket as valid |
12538 | | * until it has verified the server's Finished message." When the server |
12539 | | * sends a NewSessionTicket in a resumption handshake, we must wait until |
12540 | | * the handshake is finished (we have verified the server's Finished |
12541 | | * AND the server's certificate) before we update the ticket in the sid. |
12542 | | * |
12543 | | * This must be done before we call ssl_CacheSessionID(ss) |
12544 | | * because CacheSID requires the session ticket to already be set, and also |
12545 | | * because of the lazy lock creation scheme used by CacheSID and |
12546 | | * ssl3_SetSIDSessionTicket. */ |
12547 | 31.2k | if (ss->ssl3.hs.receivedNewSessionTicket) { |
12548 | 0 | PORT_Assert(!ss->sec.isServer); |
12549 | 0 | if (sidRv == SECSuccess) { |
12550 | | /* The sid takes over the ticket data */ |
12551 | 0 | ssl3_SetSIDSessionTicket(ss->sec.ci.sid, |
12552 | 0 | &ss->ssl3.hs.newSessionTicket); |
12553 | 0 | } else { |
12554 | 0 | PORT_Assert(ss->ssl3.hs.newSessionTicket.ticket.data); |
12555 | 0 | SECITEM_FreeItem(&ss->ssl3.hs.newSessionTicket.ticket, |
12556 | 0 | PR_FALSE); |
12557 | 0 | } |
12558 | 0 | PORT_Assert(!ss->ssl3.hs.newSessionTicket.ticket.data); |
12559 | 0 | ss->ssl3.hs.receivedNewSessionTicket = PR_FALSE; |
12560 | 0 | } |
12561 | 31.2k | if (sidRv == SECSuccess) { |
12562 | 15.9k | PORT_Assert(ss->sec.ci.sid->cached == never_cached); |
12563 | 15.9k | ssl_CacheSessionID(ss); |
12564 | 15.9k | } |
12565 | | |
12566 | 31.2k | ss->ssl3.hs.canFalseStart = PR_FALSE; /* False Start phase is complete */ |
12567 | 31.2k | ss->ssl3.hs.ws = idle_handshake; |
12568 | | |
12569 | 31.2k | return ssl_FinishHandshake(ss); |
12570 | 31.2k | } |
12571 | | |
12572 | | SECStatus |
12573 | | ssl_HashHandshakeMessageInt(sslSocket *ss, SSLHandshakeType ct, |
12574 | | PRUint32 dtlsSeq, |
12575 | | const PRUint8 *b, PRUint32 length, |
12576 | | sslUpdateHandshakeHashes updateHashes) |
12577 | 153k | { |
12578 | 153k | PRUint8 hdr[4]; |
12579 | 153k | PRUint8 dtlsData[8]; |
12580 | 153k | SECStatus rv; |
12581 | | |
12582 | 153k | PRINT_BUF(50, (ss, "Hash handshake message:", b, length)); |
12583 | | |
12584 | 153k | hdr[0] = (PRUint8)ct; |
12585 | 153k | hdr[1] = (PRUint8)(length >> 16); |
12586 | 153k | hdr[2] = (PRUint8)(length >> 8); |
12587 | 153k | hdr[3] = (PRUint8)(length); |
12588 | | |
12589 | 153k | rv = updateHashes(ss, (unsigned char *)hdr, 4); |
12590 | 153k | if (rv != SECSuccess) |
12591 | 0 | return rv; /* err code already set. */ |
12592 | | |
12593 | | /* Extra data to simulate a complete DTLS handshake fragment */ |
12594 | 153k | if (IS_DTLS_1_OR_12(ss)) { |
12595 | | /* Sequence number */ |
12596 | 0 | dtlsData[0] = MSB(dtlsSeq); |
12597 | 0 | dtlsData[1] = LSB(dtlsSeq); |
12598 | | |
12599 | | /* Fragment offset */ |
12600 | 0 | dtlsData[2] = 0; |
12601 | 0 | dtlsData[3] = 0; |
12602 | 0 | dtlsData[4] = 0; |
12603 | | |
12604 | | /* Fragment length */ |
12605 | 0 | dtlsData[5] = (PRUint8)(length >> 16); |
12606 | 0 | dtlsData[6] = (PRUint8)(length >> 8); |
12607 | 0 | dtlsData[7] = (PRUint8)(length); |
12608 | |
|
12609 | 0 | rv = updateHashes(ss, (unsigned char *)dtlsData, sizeof(dtlsData)); |
12610 | 0 | if (rv != SECSuccess) |
12611 | 0 | return rv; /* err code already set. */ |
12612 | 0 | } |
12613 | | |
12614 | | /* The message body */ |
12615 | 153k | rv = updateHashes(ss, b, length); |
12616 | 153k | if (rv != SECSuccess) |
12617 | 0 | return rv; /* err code already set. */ |
12618 | | |
12619 | 153k | return SECSuccess; |
12620 | 153k | } |
12621 | | |
12622 | | SECStatus |
12623 | | ssl_HashHandshakeMessage(sslSocket *ss, SSLHandshakeType ct, |
12624 | | const PRUint8 *b, PRUint32 length) |
12625 | 152k | { |
12626 | 152k | return ssl_HashHandshakeMessageInt(ss, ct, ss->ssl3.hs.recvMessageSeq, |
12627 | 152k | b, length, ssl3_UpdateHandshakeHashes); |
12628 | 152k | } |
12629 | | |
12630 | | SECStatus |
12631 | | ssl_HashHandshakeMessageDefault(sslSocket *ss, SSLHandshakeType ct, |
12632 | | const PRUint8 *b, PRUint32 length) |
12633 | 322 | { |
12634 | 322 | return ssl_HashHandshakeMessageInt(ss, ct, ss->ssl3.hs.recvMessageSeq, |
12635 | 322 | b, length, ssl3_UpdateDefaultHandshakeHashes); |
12636 | 322 | } |
12637 | | SECStatus |
12638 | | ssl_HashHandshakeMessageEchInner(sslSocket *ss, SSLHandshakeType ct, |
12639 | | const PRUint8 *b, PRUint32 length) |
12640 | 156 | { |
12641 | 156 | return ssl_HashHandshakeMessageInt(ss, ct, ss->ssl3.hs.recvMessageSeq, |
12642 | 156 | b, length, ssl3_UpdateInnerHandshakeHashes); |
12643 | 156 | } |
12644 | | |
12645 | | SECStatus |
12646 | | ssl_HashPostHandshakeMessage(sslSocket *ss, SSLHandshakeType ct, |
12647 | | const PRUint8 *b, PRUint32 length) |
12648 | 0 | { |
12649 | 0 | return ssl_HashHandshakeMessageInt(ss, ct, ss->ssl3.hs.recvMessageSeq, |
12650 | 0 | b, length, ssl3_UpdatePostHandshakeHashes); |
12651 | 0 | } |
12652 | | |
12653 | | /* Called from ssl3_HandleHandshake() when it has gathered a complete ssl3 |
12654 | | * handshake message. |
12655 | | * Caller must hold Handshake and RecvBuf locks. |
12656 | | */ |
12657 | | SECStatus |
12658 | | ssl3_HandleHandshakeMessage(sslSocket *ss, PRUint8 *b, PRUint32 length, |
12659 | | PRBool endOfRecord) |
12660 | 188k | { |
12661 | 188k | SECStatus rv = SECSuccess; |
12662 | 188k | PRUint16 epoch; |
12663 | | |
12664 | 188k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
12665 | 188k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
12666 | | |
12667 | 188k | SSL_TRC(30, ("%d: SSL3[%d]: handle handshake message: %s", SSL_GETPID(), |
12668 | 188k | ss->fd, ssl3_DecodeHandshakeType(ss->ssl3.hs.msg_type))); |
12669 | | |
12670 | | /* Start new handshake hashes when we start a new handshake. */ |
12671 | 188k | if (ss->ssl3.hs.msg_type == ssl_hs_client_hello) { |
12672 | 4 | ssl3_RestartHandshakeHashes(ss); |
12673 | 4 | } |
12674 | 188k | switch (ss->ssl3.hs.msg_type) { |
12675 | 33.7k | case ssl_hs_hello_request: |
12676 | 33.7k | case ssl_hs_hello_verify_request: |
12677 | | /* We don't include hello_request and hello_verify_request messages |
12678 | | * in the handshake hashes */ |
12679 | 33.7k | break; |
12680 | | |
12681 | | /* Defer hashing of these messages until the message handlers. */ |
12682 | 4 | case ssl_hs_client_hello: |
12683 | 40.3k | case ssl_hs_server_hello: |
12684 | 40.3k | case ssl_hs_certificate_verify: |
12685 | 71.7k | case ssl_hs_finished: |
12686 | 71.7k | break; |
12687 | | |
12688 | 82.5k | default: |
12689 | 82.5k | if (!tls13_IsPostHandshake(ss)) { |
12690 | 81.5k | rv = ssl_HashHandshakeMessage(ss, ss->ssl3.hs.msg_type, b, length); |
12691 | 81.5k | if (rv != SECSuccess) { |
12692 | 0 | return SECFailure; |
12693 | 0 | } |
12694 | 81.5k | } |
12695 | 188k | } |
12696 | | |
12697 | 188k | PORT_SetError(0); /* each message starts with no error. */ |
12698 | | |
12699 | 188k | if (ss->ssl3.hs.ws == wait_certificate_status && |
12700 | 188k | ss->ssl3.hs.msg_type != ssl_hs_certificate_status) { |
12701 | | /* If we negotiated the certificate_status extension then we deferred |
12702 | | * certificate validation until we get the CertificateStatus messsage. |
12703 | | * But the CertificateStatus message is optional. If the server did |
12704 | | * not send it then we need to validate the certificate now. If the |
12705 | | * server does send the CertificateStatus message then we will |
12706 | | * authenticate the certificate in ssl3_HandleCertificateStatus. |
12707 | | */ |
12708 | 9 | rv = ssl3_AuthCertificate(ss); /* sets ss->ssl3.hs.ws */ |
12709 | 9 | if (rv != SECSuccess) { |
12710 | | /* This can't block. */ |
12711 | 7 | PORT_Assert(PORT_GetError() != PR_WOULD_BLOCK_ERROR); |
12712 | 7 | return SECFailure; |
12713 | 7 | } |
12714 | 9 | } |
12715 | | |
12716 | 188k | epoch = ss->ssl3.crSpec->epoch; |
12717 | 188k | switch (ss->ssl3.hs.msg_type) { |
12718 | 4 | case ssl_hs_client_hello: |
12719 | 4 | if (!ss->sec.isServer) { |
12720 | 4 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12721 | 4 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CLIENT_HELLO); |
12722 | 4 | return SECFailure; |
12723 | 4 | } |
12724 | 0 | rv = ssl3_HandleClientHello(ss, b, length); |
12725 | 0 | break; |
12726 | 40.3k | case ssl_hs_server_hello: |
12727 | 40.3k | if (ss->sec.isServer) { |
12728 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12729 | 0 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_SERVER_HELLO); |
12730 | 0 | return SECFailure; |
12731 | 0 | } |
12732 | 40.3k | rv = ssl3_HandleServerHello(ss, b, length); |
12733 | 40.3k | break; |
12734 | 147k | default: |
12735 | 147k | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
12736 | 146k | rv = ssl3_HandlePostHelloHandshakeMessage(ss, b, length); |
12737 | 146k | } else { |
12738 | 1.68k | rv = tls13_HandlePostHelloHandshakeMessage(ss, b, length); |
12739 | 1.68k | } |
12740 | 147k | break; |
12741 | 188k | } |
12742 | 188k | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
12743 | 188k | (epoch != ss->ssl3.crSpec->epoch) && !endOfRecord) { |
12744 | | /* If we changed read cipher states, there must not be any |
12745 | | * data in the input queue. */ |
12746 | 25 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12747 | 25 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HANDSHAKE); |
12748 | 25 | return SECFailure; |
12749 | 25 | } |
12750 | | /* We consider the record to have been handled if SECSuccess or else WOULD_BLOCK is set |
12751 | | * Whoever set WOULD_BLOCK must handle any remaining actions required to finsih processing the record. |
12752 | | * e.g. by setting restartTarget. |
12753 | | */ |
12754 | 188k | if (IS_DTLS(ss) && (rv == SECSuccess || (rv == SECFailure && PR_GetError() == PR_WOULD_BLOCK_ERROR))) { |
12755 | | /* Increment the expected sequence number */ |
12756 | 0 | ss->ssl3.hs.recvMessageSeq++; |
12757 | 0 | } |
12758 | | |
12759 | | /* Taint the message so that it's easier to detect UAFs. */ |
12760 | 188k | PORT_Memset(b, 'N', length); |
12761 | | |
12762 | 188k | return rv; |
12763 | 188k | } |
12764 | | |
12765 | | static SECStatus |
12766 | | ssl3_HandlePostHelloHandshakeMessage(sslSocket *ss, PRUint8 *b, |
12767 | | PRUint32 length) |
12768 | 146k | { |
12769 | 146k | SECStatus rv; |
12770 | 146k | PORT_Assert(ss->version < SSL_LIBRARY_VERSION_TLS_1_3); |
12771 | | |
12772 | 146k | switch (ss->ssl3.hs.msg_type) { |
12773 | 33.7k | case ssl_hs_hello_request: |
12774 | 33.7k | if (length != 0) { |
12775 | 19 | (void)ssl3_DecodeError(ss); |
12776 | 19 | PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_REQUEST); |
12777 | 19 | return SECFailure; |
12778 | 19 | } |
12779 | 33.7k | if (ss->sec.isServer) { |
12780 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12781 | 0 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_REQUEST); |
12782 | 0 | return SECFailure; |
12783 | 0 | } |
12784 | 33.7k | rv = ssl3_HandleHelloRequest(ss); |
12785 | 33.7k | break; |
12786 | | |
12787 | 2 | case ssl_hs_hello_verify_request: |
12788 | 2 | if (!IS_DTLS(ss) || ss->sec.isServer) { |
12789 | 2 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12790 | 2 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_VERIFY_REQUEST); |
12791 | 2 | return SECFailure; |
12792 | 2 | } |
12793 | 0 | rv = dtls_HandleHelloVerifyRequest(ss, b, length); |
12794 | 0 | break; |
12795 | 38.4k | case ssl_hs_certificate: |
12796 | 38.4k | rv = ssl3_HandleCertificate(ss, b, length); |
12797 | 38.4k | break; |
12798 | 51 | case ssl_hs_certificate_status: |
12799 | 51 | rv = ssl3_HandleCertificateStatus(ss, b, length); |
12800 | 51 | break; |
12801 | 8.78k | case ssl_hs_server_key_exchange: |
12802 | 8.78k | if (ss->sec.isServer) { |
12803 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12804 | 0 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_SERVER_KEY_EXCH); |
12805 | 0 | return SECFailure; |
12806 | 0 | } |
12807 | 8.78k | rv = ssl3_HandleServerKeyExchange(ss, b, length); |
12808 | 8.78k | break; |
12809 | 206 | case ssl_hs_certificate_request: |
12810 | 206 | if (ss->sec.isServer) { |
12811 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12812 | 0 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERT_REQUEST); |
12813 | 0 | return SECFailure; |
12814 | 0 | } |
12815 | 206 | rv = ssl3_HandleCertificateRequest(ss, b, length); |
12816 | 206 | break; |
12817 | 33.4k | case ssl_hs_server_hello_done: |
12818 | 33.4k | if (length != 0) { |
12819 | 12 | (void)ssl3_DecodeError(ss); |
12820 | 12 | PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_DONE); |
12821 | 12 | return SECFailure; |
12822 | 12 | } |
12823 | 33.4k | if (ss->sec.isServer) { |
12824 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12825 | 0 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_HELLO_DONE); |
12826 | 0 | return SECFailure; |
12827 | 0 | } |
12828 | 33.4k | rv = ssl3_HandleServerHelloDone(ss); |
12829 | 33.4k | break; |
12830 | 3 | case ssl_hs_certificate_verify: |
12831 | 3 | if (!ss->sec.isServer) { |
12832 | 3 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12833 | 3 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CERT_VERIFY); |
12834 | 3 | return SECFailure; |
12835 | 3 | } |
12836 | 0 | rv = ssl3_HandleCertificateVerify(ss, b, length); |
12837 | 0 | break; |
12838 | 3 | case ssl_hs_client_key_exchange: |
12839 | 3 | if (!ss->sec.isServer) { |
12840 | 3 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12841 | 3 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_CLIENT_KEY_EXCH); |
12842 | 3 | return SECFailure; |
12843 | 3 | } |
12844 | 0 | rv = ssl3_HandleClientKeyExchange(ss, b, length); |
12845 | 0 | break; |
12846 | 42 | case ssl_hs_new_session_ticket: |
12847 | 42 | if (ss->sec.isServer) { |
12848 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12849 | 0 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_NEW_SESSION_TICKET); |
12850 | 0 | return SECFailure; |
12851 | 0 | } |
12852 | 42 | rv = ssl3_HandleNewSessionTicket(ss, b, length); |
12853 | 42 | break; |
12854 | 31.2k | case ssl_hs_finished: |
12855 | 31.2k | rv = ssl3_HandleFinished(ss, b, length); |
12856 | 31.2k | break; |
12857 | 28 | default: |
12858 | 28 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
12859 | 28 | PORT_SetError(SSL_ERROR_RX_UNKNOWN_HANDSHAKE); |
12860 | 28 | rv = SECFailure; |
12861 | 146k | } |
12862 | | |
12863 | 146k | return rv; |
12864 | 146k | } |
12865 | | |
12866 | | /* Called only from ssl3_HandleRecord, for each (deciphered) ssl3 record. |
12867 | | * origBuf is the decrypted ssl record content. |
12868 | | * Caller must hold the handshake and RecvBuf locks. |
12869 | | */ |
12870 | | static SECStatus |
12871 | | ssl3_HandleHandshake(sslSocket *ss, sslBuffer *origBuf) |
12872 | 159k | { |
12873 | 159k | sslBuffer buf = *origBuf; /* Work from a copy. */ |
12874 | 159k | SECStatus rv; |
12875 | | |
12876 | 159k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
12877 | 159k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
12878 | | |
12879 | 1.00M | while (buf.len > 0) { |
12880 | 877k | if (ss->ssl3.hs.header_bytes < 4) { |
12881 | 755k | PRUint8 t; |
12882 | 755k | t = *(buf.buf++); |
12883 | 755k | buf.len--; |
12884 | 755k | if (ss->ssl3.hs.header_bytes++ == 0) |
12885 | 189k | ss->ssl3.hs.msg_type = (SSLHandshakeType)t; |
12886 | 566k | else |
12887 | 566k | ss->ssl3.hs.msg_len = (ss->ssl3.hs.msg_len << 8) + t; |
12888 | 755k | if (ss->ssl3.hs.header_bytes < 4) |
12889 | 566k | continue; |
12890 | | |
12891 | 188k | #define MAX_HANDSHAKE_MSG_LEN 0x1ffff /* 128k - 1 */ |
12892 | 188k | if (ss->ssl3.hs.msg_len > MAX_HANDSHAKE_MSG_LEN) { |
12893 | 284 | (void)ssl3_DecodeError(ss); |
12894 | 284 | PORT_SetError(SSL_ERROR_RX_MALFORMED_HANDSHAKE); |
12895 | 284 | goto loser; |
12896 | 284 | } |
12897 | 188k | #undef MAX_HANDSHAKE_MSG_LEN |
12898 | | |
12899 | | /* If msg_len is zero, be sure we fall through, |
12900 | | ** even if buf.len is zero. |
12901 | | */ |
12902 | 188k | if (ss->ssl3.hs.msg_len > 0) |
12903 | 89.7k | continue; |
12904 | 188k | } |
12905 | | |
12906 | | /* |
12907 | | * Header has been gathered and there is at least one byte of new |
12908 | | * data available for this message. If it can be done right out |
12909 | | * of the original buffer, then use it from there. |
12910 | | */ |
12911 | 220k | if (ss->ssl3.hs.msg_body.len == 0 && buf.len >= ss->ssl3.hs.msg_len) { |
12912 | | /* handle it from input buffer */ |
12913 | 176k | rv = ssl3_HandleHandshakeMessage(ss, buf.buf, ss->ssl3.hs.msg_len, |
12914 | 176k | buf.len == ss->ssl3.hs.msg_len); |
12915 | 176k | buf.buf += ss->ssl3.hs.msg_len; |
12916 | 176k | buf.len -= ss->ssl3.hs.msg_len; |
12917 | 176k | ss->ssl3.hs.msg_len = 0; |
12918 | 176k | ss->ssl3.hs.header_bytes = 0; |
12919 | 176k | if (rv != SECSuccess) { |
12920 | 2.17k | goto loser; |
12921 | 2.17k | } |
12922 | 176k | } else { |
12923 | | /* must be copied to msg_body and dealt with from there */ |
12924 | 44.2k | unsigned int bytes; |
12925 | | |
12926 | 44.2k | PORT_Assert(ss->ssl3.hs.msg_body.len < ss->ssl3.hs.msg_len); |
12927 | 44.2k | bytes = PR_MIN(buf.len, ss->ssl3.hs.msg_len - ss->ssl3.hs.msg_body.len); |
12928 | | |
12929 | | /* Grow the buffer if needed */ |
12930 | 44.2k | rv = sslBuffer_Grow(&ss->ssl3.hs.msg_body, ss->ssl3.hs.msg_len); |
12931 | 44.2k | if (rv != SECSuccess) { |
12932 | | /* sslBuffer_Grow has set a memory error code. */ |
12933 | 0 | goto loser; |
12934 | 0 | } |
12935 | | |
12936 | 44.2k | PORT_Memcpy(ss->ssl3.hs.msg_body.buf + ss->ssl3.hs.msg_body.len, |
12937 | 44.2k | buf.buf, bytes); |
12938 | 44.2k | ss->ssl3.hs.msg_body.len += bytes; |
12939 | 44.2k | buf.buf += bytes; |
12940 | 44.2k | buf.len -= bytes; |
12941 | | |
12942 | 44.2k | PORT_Assert(ss->ssl3.hs.msg_body.len <= ss->ssl3.hs.msg_len); |
12943 | | |
12944 | | /* if we have a whole message, do it */ |
12945 | 44.2k | if (ss->ssl3.hs.msg_body.len == ss->ssl3.hs.msg_len) { |
12946 | 11.8k | rv = ssl3_HandleHandshakeMessage( |
12947 | 11.8k | ss, ss->ssl3.hs.msg_body.buf, ss->ssl3.hs.msg_len, |
12948 | 11.8k | buf.len == 0); |
12949 | 11.8k | ss->ssl3.hs.msg_body.len = 0; |
12950 | 11.8k | ss->ssl3.hs.msg_len = 0; |
12951 | 11.8k | ss->ssl3.hs.header_bytes = 0; |
12952 | 11.8k | if (rv != SECSuccess) { |
12953 | 818 | goto loser; |
12954 | 818 | } |
12955 | 32.3k | } else { |
12956 | 32.3k | PORT_Assert(buf.len == 0); |
12957 | 32.3k | break; |
12958 | 32.3k | } |
12959 | 44.2k | } |
12960 | 220k | } /* end loop */ |
12961 | | |
12962 | 156k | origBuf->len = 0; /* So ssl3_GatherAppDataRecord will keep looping. */ |
12963 | 156k | return SECSuccess; |
12964 | | |
12965 | 3.27k | loser : { |
12966 | | /* Make sure to remove any data that was consumed. */ |
12967 | 3.27k | unsigned int consumed = origBuf->len - buf.len; |
12968 | 3.27k | PORT_Assert(consumed == buf.buf - origBuf->buf); |
12969 | 3.27k | if (consumed > 0) { |
12970 | 3.27k | memmove(origBuf->buf, origBuf->buf + consumed, buf.len); |
12971 | 3.27k | origBuf->len = buf.len; |
12972 | 3.27k | } |
12973 | 3.27k | } |
12974 | 3.27k | return SECFailure; |
12975 | 159k | } |
12976 | | |
12977 | | /* SECStatusToMask returns, in constant time, a mask value of all ones if |
12978 | | * rv == SECSuccess. Otherwise it returns zero. */ |
12979 | | static unsigned int |
12980 | | SECStatusToMask(SECStatus rv) |
12981 | 0 | { |
12982 | 0 | return PORT_CT_EQ(rv, SECSuccess); |
12983 | 0 | } |
12984 | | |
12985 | | /* ssl_ConstantTimeGE returns 0xffffffff if a>=b and 0x00 otherwise. */ |
12986 | | static unsigned char |
12987 | | ssl_ConstantTimeGE(unsigned int a, unsigned int b) |
12988 | 0 | { |
12989 | 0 | return PORT_CT_GE(a, b); |
12990 | 0 | } |
12991 | | |
12992 | | /* ssl_ConstantTimeEQ returns 0xffffffff if a==b and 0x00 otherwise. */ |
12993 | | static unsigned char |
12994 | | ssl_ConstantTimeEQ(unsigned char a, unsigned char b) |
12995 | 0 | { |
12996 | 0 | return PORT_CT_EQ(a, b); |
12997 | 0 | } |
12998 | | |
12999 | | /* ssl_constantTimeSelect return a if mask is 0xFF and b if mask is 0x00 */ |
13000 | | static unsigned char |
13001 | | ssl_constantTimeSelect(unsigned char mask, unsigned char a, unsigned char b) |
13002 | 0 | { |
13003 | 0 | return (mask & a) | (~mask & b); |
13004 | 0 | } |
13005 | | |
13006 | | static SECStatus |
13007 | | ssl_RemoveSSLv3CBCPadding(sslBuffer *plaintext, |
13008 | | unsigned int blockSize, |
13009 | | unsigned int macSize) |
13010 | 0 | { |
13011 | 0 | unsigned int paddingLength, good; |
13012 | 0 | const unsigned int overhead = 1 /* padding length byte */ + macSize; |
13013 | 0 |
|
13014 | 0 | /* These lengths are all public so we can test them in non-constant |
13015 | 0 | * time. */ |
13016 | 0 | if (overhead > plaintext->len) { |
13017 | 0 | return SECFailure; |
13018 | 0 | } |
13019 | 0 |
|
13020 | 0 | paddingLength = plaintext->buf[plaintext->len - 1]; |
13021 | 0 | /* SSLv3 padding bytes are random and cannot be checked. */ |
13022 | 0 | good = PORT_CT_GE(plaintext->len, paddingLength + overhead); |
13023 | 0 | /* SSLv3 requires that the padding is minimal. */ |
13024 | 0 | good &= PORT_CT_GE(blockSize, paddingLength + 1); |
13025 | 0 | plaintext->len -= good & (paddingLength + 1); |
13026 | 0 | return (good & SECSuccess) | (~good & SECFailure); |
13027 | 0 | } |
13028 | | |
13029 | | SECStatus |
13030 | | ssl_RemoveTLSCBCPadding(sslBuffer *plaintext, unsigned int macSize) |
13031 | 0 | { |
13032 | 0 | unsigned int paddingLength, good, toCheck, i; |
13033 | 0 | const unsigned int overhead = 1 /* padding length byte */ + macSize; |
13034 | | |
13035 | | /* These lengths are all public so we can test them in non-constant |
13036 | | * time. */ |
13037 | 0 | if (overhead > plaintext->len) { |
13038 | 0 | return SECFailure; |
13039 | 0 | } |
13040 | | |
13041 | 0 | paddingLength = plaintext->buf[plaintext->len - 1]; |
13042 | 0 | good = PORT_CT_GE(plaintext->len, paddingLength + overhead); |
13043 | | |
13044 | | /* The padding consists of a length byte at the end of the record and then |
13045 | | * that many bytes of padding, all with the same value as the length byte. |
13046 | | * Thus, with the length byte included, there are paddingLength+1 bytes of |
13047 | | * padding. |
13048 | | * |
13049 | | * We can't check just |paddingLength+1| bytes because that leaks |
13050 | | * decrypted information. Therefore we always have to check the maximum |
13051 | | * amount of padding possible. (Again, the length of the record is |
13052 | | * public information so we can use it.) */ |
13053 | 0 | toCheck = 256; /* maximum amount of padding + 1. */ |
13054 | 0 | if (toCheck > plaintext->len) { |
13055 | 0 | toCheck = plaintext->len; |
13056 | 0 | } |
13057 | |
|
13058 | 0 | for (i = 0; i < toCheck; i++) { |
13059 | | /* If i <= paddingLength then the MSB of t is zero and mask is |
13060 | | * 0xff. Otherwise, mask is 0. */ |
13061 | 0 | unsigned char mask = PORT_CT_LE(i, paddingLength); |
13062 | 0 | unsigned char b = plaintext->buf[plaintext->len - 1 - i]; |
13063 | | /* The final |paddingLength+1| bytes should all have the value |
13064 | | * |paddingLength|. Therefore the XOR should be zero. */ |
13065 | 0 | good &= ~(mask & (paddingLength ^ b)); |
13066 | 0 | } |
13067 | | |
13068 | | /* If any of the final |paddingLength+1| bytes had the wrong value, |
13069 | | * one or more of the lower eight bits of |good| will be cleared. We |
13070 | | * AND the bottom 8 bits together and duplicate the result to all the |
13071 | | * bits. */ |
13072 | 0 | good &= good >> 4; |
13073 | 0 | good &= good >> 2; |
13074 | 0 | good &= good >> 1; |
13075 | 0 | good <<= sizeof(good) * 8 - 1; |
13076 | 0 | good = PORT_CT_DUPLICATE_MSB_TO_ALL(good); |
13077 | |
|
13078 | 0 | plaintext->len -= good & (paddingLength + 1); |
13079 | 0 | return (good & SECSuccess) | (~good & SECFailure); |
13080 | 0 | } |
13081 | | |
13082 | | /* On entry: |
13083 | | * originalLength >= macSize |
13084 | | * macSize <= MAX_MAC_LENGTH |
13085 | | * plaintext->len >= macSize |
13086 | | */ |
13087 | | static void |
13088 | | ssl_CBCExtractMAC(sslBuffer *plaintext, |
13089 | | unsigned int originalLength, |
13090 | | PRUint8 *out, |
13091 | | unsigned int macSize) |
13092 | 0 | { |
13093 | 0 | unsigned char rotatedMac[MAX_MAC_LENGTH]; |
13094 | 0 | /* macEnd is the index of |plaintext->buf| just after the end of the |
13095 | 0 | * MAC. */ |
13096 | 0 | unsigned macEnd = plaintext->len; |
13097 | 0 | unsigned macStart = macEnd - macSize; |
13098 | 0 | /* scanStart contains the number of bytes that we can ignore because |
13099 | 0 | * the MAC's position can only vary by 255 bytes. */ |
13100 | 0 | unsigned scanStart = 0; |
13101 | 0 | unsigned i, j; |
13102 | 0 | unsigned char rotateOffset; |
13103 | 0 |
|
13104 | 0 | if (originalLength > macSize + 255 + 1) { |
13105 | 0 | scanStart = originalLength - (macSize + 255 + 1); |
13106 | 0 | } |
13107 | 0 |
|
13108 | 0 | /* We want to compute |
13109 | 0 | * rotateOffset = (macStart - scanStart) % macSize |
13110 | 0 | * But the time to compute this varies based on the amount of padding. Thus |
13111 | 0 | * we explicitely handle all mac sizes with (hopefully) constant time modulo |
13112 | 0 | * using Barrett reduction: |
13113 | 0 | * q := (rotateOffset * m) >> k |
13114 | 0 | * rotateOffset -= q * n |
13115 | 0 | * if (n <= rotateOffset) rotateOffset -= n |
13116 | 0 | */ |
13117 | 0 | rotateOffset = macStart - scanStart; |
13118 | 0 | /* rotateOffset < 255 + 1 + 48 = 304 */ |
13119 | 0 | if (macSize == 16) { |
13120 | 0 | rotateOffset &= 15; |
13121 | 0 | } else if (macSize == 20) { |
13122 | 0 | /* |
13123 | 0 | * Correctness: rotateOffset * ( 1/20 - 25/2^9 ) < 1 |
13124 | 0 | * with rotateOffset <= 853 |
13125 | 0 | */ |
13126 | 0 | unsigned q = (rotateOffset * 25) >> 9; |
13127 | 0 | rotateOffset -= q * 20; |
13128 | 0 | rotateOffset -= ssl_constantTimeSelect(ssl_ConstantTimeGE(rotateOffset, 20), |
13129 | 0 | 20, 0); |
13130 | 0 | } else if (macSize == 32) { |
13131 | 0 | rotateOffset &= 31; |
13132 | 0 | } else if (macSize == 48) { |
13133 | 0 | /* |
13134 | 0 | * Correctness: rotateOffset * ( 1/48 - 10/2^9 ) < 1 |
13135 | 0 | * with rotateOffset < 768 |
13136 | 0 | */ |
13137 | 0 | unsigned q = (rotateOffset * 10) >> 9; |
13138 | 0 | rotateOffset -= q * 48; |
13139 | 0 | rotateOffset -= ssl_constantTimeSelect(ssl_ConstantTimeGE(rotateOffset, 48), |
13140 | 0 | 48, 0); |
13141 | 0 | } else { |
13142 | 0 | /* |
13143 | 0 | * SHA384 (macSize == 48) is the largest we support. We should never |
13144 | 0 | * get here. |
13145 | 0 | */ |
13146 | 0 | PORT_Assert(0); |
13147 | 0 | rotateOffset = rotateOffset % macSize; |
13148 | 0 | } |
13149 | 0 |
|
13150 | 0 | memset(rotatedMac, 0, macSize); |
13151 | 0 | for (i = scanStart; i < originalLength;) { |
13152 | 0 | for (j = 0; j < macSize && i < originalLength; i++, j++) { |
13153 | 0 | unsigned char macStarted = ssl_ConstantTimeGE(i, macStart); |
13154 | 0 | unsigned char macEnded = ssl_ConstantTimeGE(i, macEnd); |
13155 | 0 | unsigned char b = 0; |
13156 | 0 | b = plaintext->buf[i]; |
13157 | 0 | rotatedMac[j] |= b & macStarted & ~macEnded; |
13158 | 0 | } |
13159 | 0 | } |
13160 | 0 |
|
13161 | 0 | /* Now rotate the MAC. If we knew that the MAC fit into a CPU cache line |
13162 | 0 | * we could line-align |rotatedMac| and rotate in place. */ |
13163 | 0 | memset(out, 0, macSize); |
13164 | 0 | rotateOffset = macSize - rotateOffset; |
13165 | 0 | rotateOffset = ssl_constantTimeSelect(ssl_ConstantTimeGE(rotateOffset, macSize), |
13166 | 0 | 0, rotateOffset); |
13167 | 0 | for (i = 0; i < macSize; i++) { |
13168 | 0 | for (j = 0; j < macSize; j++) { |
13169 | 0 | out[j] |= rotatedMac[i] & ssl_ConstantTimeEQ(j, rotateOffset); |
13170 | 0 | } |
13171 | 0 | rotateOffset++; |
13172 | 0 | rotateOffset = ssl_constantTimeSelect(ssl_ConstantTimeGE(rotateOffset, macSize), |
13173 | 0 | 0, rotateOffset); |
13174 | 0 | } |
13175 | 0 | } |
13176 | | |
13177 | | /* MAX_EXPANSION is the amount by which a record might plausibly be expanded |
13178 | | * when protected. It's the worst case estimate, so the sum of block cipher |
13179 | | * padding (up to 256 octets), HMAC (48 octets for SHA-384), and IV (16 |
13180 | | * octets for AES). */ |
13181 | | #define MAX_EXPANSION (256 + 48 + 16) |
13182 | | |
13183 | | /* Unprotect an SSL3 record and leave the result in plaintext. |
13184 | | * |
13185 | | * If SECFailure is returned, we: |
13186 | | * 1. Set |*alert| to the alert to be sent. |
13187 | | * 2. Call PORT_SetError() with an appropriate code. |
13188 | | * |
13189 | | * Called by ssl3_HandleRecord. Caller must hold the spec read lock. |
13190 | | * Therefore, we MUST not call SSL3_SendAlert(). |
13191 | | * |
13192 | | */ |
13193 | | static SECStatus |
13194 | | ssl3_UnprotectRecord(sslSocket *ss, |
13195 | | ssl3CipherSpec *spec, |
13196 | | SSL3Ciphertext *cText, sslBuffer *plaintext, |
13197 | | SSL3AlertDescription *alert) |
13198 | 0 | { |
13199 | 0 | const ssl3BulkCipherDef *cipher_def = spec->cipherDef; |
13200 | 0 | PRBool isTLS; |
13201 | 0 | unsigned int good; |
13202 | 0 | unsigned int ivLen = 0; |
13203 | 0 | SSLContentType rType; |
13204 | 0 | SSL3ProtocolVersion rVersion; |
13205 | 0 | unsigned int minLength; |
13206 | 0 | unsigned int originalLen = 0; |
13207 | 0 | PRUint8 headerBuf[13]; |
13208 | 0 | sslBuffer header = SSL_BUFFER(headerBuf); |
13209 | 0 | PRUint8 hash[MAX_MAC_LENGTH]; |
13210 | 0 | PRUint8 givenHashBuf[MAX_MAC_LENGTH]; |
13211 | 0 | PRUint8 *givenHash; |
13212 | 0 | unsigned int hashBytes = MAX_MAC_LENGTH + 1; |
13213 | 0 | SECStatus rv; |
13214 | 0 |
|
13215 | 0 | PORT_Assert(spec->direction == ssl_secret_read); |
13216 | 0 |
|
13217 | 0 | good = ~0U; |
13218 | 0 | minLength = spec->macDef->mac_size; |
13219 | 0 | if (cipher_def->type == type_block) { |
13220 | 0 | /* CBC records have a padding length byte at the end. */ |
13221 | 0 | minLength++; |
13222 | 0 | if (spec->version >= SSL_LIBRARY_VERSION_TLS_1_1) { |
13223 | 0 | /* With >= TLS 1.1, CBC records have an explicit IV. */ |
13224 | 0 | minLength += cipher_def->iv_size; |
13225 | 0 | } |
13226 | 0 | } else if (cipher_def->type == type_aead) { |
13227 | 0 | minLength = cipher_def->explicit_nonce_size + cipher_def->tag_size; |
13228 | 0 | } |
13229 | 0 |
|
13230 | 0 | /* We can perform this test in variable time because the record's total |
13231 | 0 | * length and the ciphersuite are both public knowledge. */ |
13232 | 0 | if (cText->buf->len < minLength) { |
13233 | 0 | goto decrypt_loser; |
13234 | 0 | } |
13235 | 0 |
|
13236 | 0 | if (cipher_def->type == type_block && |
13237 | 0 | spec->version >= SSL_LIBRARY_VERSION_TLS_1_1) { |
13238 | 0 | /* Consume the per-record explicit IV. RFC 4346 Section 6.2.3.2 states |
13239 | 0 | * "The receiver decrypts the entire GenericBlockCipher structure and |
13240 | 0 | * then discards the first cipher block corresponding to the IV |
13241 | 0 | * component." Instead, we decrypt the first cipher block and then |
13242 | 0 | * discard it before decrypting the rest. |
13243 | 0 | */ |
13244 | 0 | PRUint8 iv[MAX_IV_LENGTH]; |
13245 | 0 | unsigned int decoded; |
13246 | 0 |
|
13247 | 0 | ivLen = cipher_def->iv_size; |
13248 | 0 | if (ivLen < 8 || ivLen > sizeof(iv)) { |
13249 | 0 | *alert = internal_error; |
13250 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
13251 | 0 | return SECFailure; |
13252 | 0 | } |
13253 | 0 |
|
13254 | 0 | PRINT_BUF(80, (ss, "IV (ciphertext):", cText->buf->buf, ivLen)); |
13255 | 0 |
|
13256 | 0 | /* The decryption result is garbage, but since we just throw away |
13257 | 0 | * the block it doesn't matter. The decryption of the next block |
13258 | 0 | * depends only on the ciphertext of the IV block. |
13259 | 0 | */ |
13260 | 0 | rv = spec->cipher(spec->cipherContext, iv, &decoded, |
13261 | 0 | sizeof(iv), cText->buf->buf, ivLen); |
13262 | 0 |
|
13263 | 0 | good &= SECStatusToMask(rv); |
13264 | 0 | } |
13265 | 0 |
|
13266 | 0 | PRINT_BUF(80, (ss, "ciphertext:", cText->buf->buf + ivLen, |
13267 | 0 | cText->buf->len - ivLen)); |
13268 | 0 |
|
13269 | 0 | /* Check if the ciphertext can be valid if we assume maximum plaintext and |
13270 | 0 | * add the maximum possible ciphersuite expansion. |
13271 | 0 | * This way we detect overlong plaintexts/padding before decryption. |
13272 | 0 | * This check enforces size limitations more strict than the RFC. |
13273 | 0 | * [RFC5246, Section 6.2.3] */ |
13274 | 0 | if (cText->buf->len > (spec->recordSizeLimit + MAX_EXPANSION)) { |
13275 | 0 | *alert = record_overflow; |
13276 | 0 | PORT_SetError(SSL_ERROR_RX_RECORD_TOO_LONG); |
13277 | 0 | return SECFailure; |
13278 | 0 | } |
13279 | 0 |
|
13280 | 0 | isTLS = (PRBool)(spec->version > SSL_LIBRARY_VERSION_3_0); |
13281 | 0 | rType = (SSLContentType)cText->hdr[0]; |
13282 | 0 | rVersion = ((SSL3ProtocolVersion)cText->hdr[1] << 8) | |
13283 | 0 | (SSL3ProtocolVersion)cText->hdr[2]; |
13284 | 0 | if (cipher_def->type == type_aead) { |
13285 | 0 | /* XXX For many AEAD ciphers, the plaintext is shorter than the |
13286 | 0 | * ciphertext by a fixed byte count, but it is not true in general. |
13287 | 0 | * Each AEAD cipher should provide a function that returns the |
13288 | 0 | * plaintext length for a given ciphertext. */ |
13289 | 0 | const unsigned int explicitNonceLen = cipher_def->explicit_nonce_size; |
13290 | 0 | const unsigned int tagLen = cipher_def->tag_size; |
13291 | 0 | unsigned int nonceLen = explicitNonceLen; |
13292 | 0 | unsigned int decryptedLen = cText->buf->len - nonceLen - tagLen; |
13293 | 0 | /* even though read doesn't return and IV, we still need a space to put |
13294 | 0 | * the combined iv/nonce n the gcm 1.2 case*/ |
13295 | 0 | unsigned char ivOut[MAX_IV_LENGTH]; |
13296 | 0 | unsigned char *iv = NULL; |
13297 | 0 | unsigned char *nonce = NULL; |
13298 | 0 |
|
13299 | 0 | ivLen = cipher_def->iv_size; |
13300 | 0 |
|
13301 | 0 | rv = ssl3_BuildRecordPseudoHeader( |
13302 | 0 | spec->epoch, cText->seqNum, |
13303 | 0 | rType, isTLS, rVersion, IS_DTLS(ss), decryptedLen, &header, spec->version); |
13304 | 0 | PORT_Assert(rv == SECSuccess); |
13305 | 0 |
|
13306 | 0 | /* build the iv */ |
13307 | 0 | if (explicitNonceLen == 0) { |
13308 | 0 | nonceLen = sizeof(cText->seqNum); |
13309 | 0 | iv = spec->keyMaterial.iv; |
13310 | 0 | nonce = SSL_BUFFER_BASE(&header); |
13311 | 0 | } else { |
13312 | 0 | PORT_Memcpy(ivOut, spec->keyMaterial.iv, ivLen); |
13313 | 0 | PORT_Memset(ivOut + ivLen, 0, explicitNonceLen); |
13314 | 0 | iv = ivOut; |
13315 | 0 | nonce = cText->buf->buf; |
13316 | 0 | nonceLen = explicitNonceLen; |
13317 | 0 | } |
13318 | 0 | rv = tls13_AEAD(spec->cipherContext, PR_TRUE, |
13319 | 0 | CKG_NO_GENERATE, 0, /* iv generator params |
13320 | 0 | * (not used in decrypt)*/ |
13321 | 0 | iv, /* iv in */ |
13322 | 0 | NULL, /* iv out */ |
13323 | 0 | ivLen + explicitNonceLen, /* full iv length */ |
13324 | 0 | nonce, nonceLen, /* nonce in */ |
13325 | 0 | SSL_BUFFER_BASE(&header), /* aad */ |
13326 | 0 | SSL_BUFFER_LEN(&header), /* aadlen */ |
13327 | 0 | plaintext->buf, /* output */ |
13328 | 0 | &plaintext->len, /* out len */ |
13329 | 0 | plaintext->space, /* max out */ |
13330 | 0 | tagLen, |
13331 | 0 | cText->buf->buf + explicitNonceLen, /* input */ |
13332 | 0 | cText->buf->len - explicitNonceLen); /* input len */ |
13333 | 0 | if (rv != SECSuccess) { |
13334 | 0 | good = 0; |
13335 | 0 | } |
13336 | 0 | } else { |
13337 | 0 | if (cipher_def->type == type_block && |
13338 | 0 | ((cText->buf->len - ivLen) % cipher_def->block_size) != 0) { |
13339 | 0 | goto decrypt_loser; |
13340 | 0 | } |
13341 | 0 |
|
13342 | 0 | /* decrypt from cText buf to plaintext. */ |
13343 | 0 | rv = spec->cipher( |
13344 | 0 | spec->cipherContext, plaintext->buf, &plaintext->len, |
13345 | 0 | plaintext->space, cText->buf->buf + ivLen, cText->buf->len - ivLen); |
13346 | 0 | if (rv != SECSuccess) { |
13347 | 0 | goto decrypt_loser; |
13348 | 0 | } |
13349 | 0 |
|
13350 | 0 | PRINT_BUF(80, (ss, "cleartext:", plaintext->buf, plaintext->len)); |
13351 | 0 |
|
13352 | 0 | originalLen = plaintext->len; |
13353 | 0 |
|
13354 | 0 | /* If it's a block cipher, check and strip the padding. */ |
13355 | 0 | if (cipher_def->type == type_block) { |
13356 | 0 | const unsigned int blockSize = cipher_def->block_size; |
13357 | 0 | const unsigned int macSize = spec->macDef->mac_size; |
13358 | 0 |
|
13359 | 0 | if (!isTLS) { |
13360 | 0 | good &= SECStatusToMask(ssl_RemoveSSLv3CBCPadding( |
13361 | 0 | plaintext, blockSize, macSize)); |
13362 | 0 | } else { |
13363 | 0 | good &= SECStatusToMask(ssl_RemoveTLSCBCPadding( |
13364 | 0 | plaintext, macSize)); |
13365 | 0 | } |
13366 | 0 | } |
13367 | 0 |
|
13368 | 0 | /* compute the MAC */ |
13369 | 0 | rv = ssl3_BuildRecordPseudoHeader( |
13370 | 0 | spec->epoch, cText->seqNum, |
13371 | 0 | rType, isTLS, rVersion, IS_DTLS(ss), |
13372 | 0 | plaintext->len - spec->macDef->mac_size, &header, spec->version); |
13373 | 0 | PORT_Assert(rv == SECSuccess); |
13374 | 0 | if (cipher_def->type == type_block) { |
13375 | 0 | rv = ssl3_ComputeRecordMACConstantTime( |
13376 | 0 | spec, SSL_BUFFER_BASE(&header), SSL_BUFFER_LEN(&header), |
13377 | 0 | plaintext->buf, plaintext->len, originalLen, |
13378 | 0 | hash, &hashBytes); |
13379 | 0 |
|
13380 | 0 | ssl_CBCExtractMAC(plaintext, originalLen, givenHashBuf, |
13381 | 0 | spec->macDef->mac_size); |
13382 | 0 | givenHash = givenHashBuf; |
13383 | 0 |
|
13384 | 0 | /* plaintext->len will always have enough space to remove the MAC |
13385 | 0 | * because in ssl_Remove{SSLv3|TLS}CBCPadding we only adjust |
13386 | 0 | * plaintext->len if the result has enough space for the MAC and we |
13387 | 0 | * tested the unadjusted size against minLength, above. */ |
13388 | 0 | plaintext->len -= spec->macDef->mac_size; |
13389 | 0 | } else { |
13390 | 0 | /* This is safe because we checked the minLength above. */ |
13391 | 0 | plaintext->len -= spec->macDef->mac_size; |
13392 | 0 |
|
13393 | 0 | rv = ssl3_ComputeRecordMAC( |
13394 | 0 | spec, SSL_BUFFER_BASE(&header), SSL_BUFFER_LEN(&header), |
13395 | 0 | plaintext->buf, plaintext->len, hash, &hashBytes); |
13396 | 0 |
|
13397 | 0 | /* We can read the MAC directly from the record because its location |
13398 | 0 | * is public when a stream cipher is used. */ |
13399 | 0 | givenHash = plaintext->buf + plaintext->len; |
13400 | 0 | } |
13401 | 0 |
|
13402 | 0 | good &= SECStatusToMask(rv); |
13403 | 0 |
|
13404 | 0 | if (hashBytes != (unsigned)spec->macDef->mac_size || |
13405 | 0 | NSS_SecureMemcmp(givenHash, hash, spec->macDef->mac_size) != 0) { |
13406 | 0 | /* We're allowed to leak whether or not the MAC check was correct */ |
13407 | 0 | good = 0; |
13408 | 0 | } |
13409 | 0 | } |
13410 | 0 |
|
13411 | 0 | if (good == 0) { |
13412 | 0 | decrypt_loser: |
13413 | 0 | /* always log mac error, in case attacker can read server logs. */ |
13414 | 0 | PORT_SetError(SSL_ERROR_BAD_MAC_READ); |
13415 | 0 | *alert = bad_record_mac; |
13416 | 0 | return SECFailure; |
13417 | 0 | } |
13418 | 0 | return SECSuccess; |
13419 | 0 | } |
13420 | | |
13421 | | SECStatus |
13422 | | ssl3_HandleNonApplicationData(sslSocket *ss, SSLContentType rType, |
13423 | | DTLSEpoch epoch, sslSequenceNumber seqNum, |
13424 | | sslBuffer *databuf) |
13425 | 202k | { |
13426 | 202k | SECStatus rv; |
13427 | | |
13428 | | /* check for Token Presence */ |
13429 | 202k | if (!ssl3_ClientAuthTokenPresent(ss->sec.ci.sid)) { |
13430 | 0 | PORT_SetError(SSL_ERROR_TOKEN_INSERTION_REMOVAL); |
13431 | 0 | return SECFailure; |
13432 | 0 | } |
13433 | | |
13434 | 202k | ssl_GetSSL3HandshakeLock(ss); |
13435 | | |
13436 | | /* All the functions called in this switch MUST set error code if |
13437 | | ** they return SECFailure. |
13438 | | */ |
13439 | 202k | switch (rType) { |
13440 | 31.9k | case ssl_ct_change_cipher_spec: |
13441 | 31.9k | rv = ssl3_HandleChangeCipherSpecs(ss, databuf); |
13442 | 31.9k | break; |
13443 | 11.1k | case ssl_ct_alert: |
13444 | 11.1k | rv = ssl3_HandleAlert(ss, databuf); |
13445 | 11.1k | break; |
13446 | 159k | case ssl_ct_handshake: |
13447 | 159k | if (!IS_DTLS(ss)) { |
13448 | 159k | rv = ssl3_HandleHandshake(ss, databuf); |
13449 | 159k | } else { |
13450 | 0 | rv = dtls_HandleHandshake(ss, epoch, seqNum, databuf); |
13451 | 0 | } |
13452 | 159k | break; |
13453 | 1 | case ssl_ct_ack: |
13454 | 1 | if (IS_DTLS(ss) && tls13_MaybeTls13(ss)) { |
13455 | 0 | rv = dtls13_HandleAck(ss, databuf); |
13456 | 0 | break; |
13457 | 0 | } |
13458 | | /* Fall through. */ |
13459 | 109 | default: |
13460 | | /* If a TLS implementation receives an unexpected record type, |
13461 | | * it MUST terminate the connection with an "unexpected_message" |
13462 | | * alert [RFC8446, Section 5]. |
13463 | | * |
13464 | | * For TLS 1.3 the outer content type is checked before in |
13465 | | * tls13con.c/tls13_UnprotectRecord(), |
13466 | | * For DTLS 1.3 the outer content type is checked before in |
13467 | | * ssl3gthr.c/dtls_GatherData. |
13468 | | * The inner content types will be checked here. |
13469 | | * |
13470 | | * In DTLS generally invalid records SHOULD be silently discarded, |
13471 | | * no alert is sent [RFC6347, Section 4.1.2.7]. |
13472 | | */ |
13473 | 109 | if (!IS_DTLS(ss)) { |
13474 | 109 | SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
13475 | 109 | } |
13476 | 109 | PORT_SetError(SSL_ERROR_RX_UNKNOWN_RECORD_TYPE); |
13477 | 109 | SSL_DBG(("%d: SSL3[%d]: bogus content type=%d", |
13478 | 109 | SSL_GETPID(), ss->fd, rType)); |
13479 | 109 | rv = SECFailure; |
13480 | 109 | break; |
13481 | 202k | } |
13482 | | |
13483 | 202k | ssl_ReleaseSSL3HandshakeLock(ss); |
13484 | 202k | return rv; |
13485 | 202k | } |
13486 | | |
13487 | | /* Find the cipher spec to use for a given record. For TLS, this |
13488 | | * is the current cipherspec. For DTLS, we look up by epoch. |
13489 | | * In DTLS < 1.3 this just means the current epoch or nothing, |
13490 | | * but in DTLS >= 1.3, we keep multiple reading cipherspecs. |
13491 | | * Returns NULL if no appropriate cipher spec is found. |
13492 | | */ |
13493 | | static ssl3CipherSpec * |
13494 | | ssl3_GetCipherSpec(sslSocket *ss, SSL3Ciphertext *cText) |
13495 | 207k | { |
13496 | 207k | ssl3CipherSpec *crSpec = ss->ssl3.crSpec; |
13497 | 207k | ssl3CipherSpec *newSpec = NULL; |
13498 | 207k | DTLSEpoch epoch; |
13499 | | |
13500 | 207k | if (!IS_DTLS(ss)) { |
13501 | 207k | return crSpec; |
13502 | 207k | } |
13503 | 0 | epoch = dtls_ReadEpoch(crSpec->version, crSpec->epoch, cText->hdr); |
13504 | 0 | if (crSpec->epoch == epoch) { |
13505 | 0 | return crSpec; |
13506 | 0 | } |
13507 | 0 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
13508 | | /* Try to find the cipher spec. */ |
13509 | 0 | newSpec = ssl_FindCipherSpecByEpoch(ss, ssl_secret_read, |
13510 | 0 | epoch); |
13511 | 0 | if (newSpec != NULL) { |
13512 | 0 | return newSpec; |
13513 | 0 | } |
13514 | 0 | } |
13515 | 0 | SSL_TRC(10, ("%d: DTLS[%d]: %s couldn't find cipherspec from epoch %d", |
13516 | 0 | SSL_GETPID(), ss->fd, SSL_ROLE(ss), epoch)); |
13517 | 0 | return NULL; |
13518 | 0 | } |
13519 | | |
13520 | | /* if cText is non-null, then decipher and check the MAC of the |
13521 | | * SSL record from cText->buf (typically gs->inbuf) |
13522 | | * into databuf (typically gs->buf), and any previous contents of databuf |
13523 | | * is lost. Then handle databuf according to its SSL record type, |
13524 | | * unless it's an application record. |
13525 | | * |
13526 | | * If cText is NULL, then the ciphertext has previously been deciphered and |
13527 | | * checked, and is already sitting in databuf. It is processed as an SSL |
13528 | | * Handshake message. |
13529 | | * |
13530 | | * DOES NOT process the decrypted application data. |
13531 | | * On return, databuf contains the decrypted record. |
13532 | | * |
13533 | | * Called from ssl3_GatherCompleteHandshake |
13534 | | * ssl3_RestartHandshakeAfterCertReq |
13535 | | * |
13536 | | * Caller must hold the RecvBufLock. |
13537 | | * |
13538 | | * This function aquires and releases the SSL3Handshake Lock, holding the |
13539 | | * lock around any calls to functions that handle records other than |
13540 | | * Application Data records. |
13541 | | */ |
13542 | | SECStatus |
13543 | | ssl3_HandleRecord(sslSocket *ss, SSL3Ciphertext *cText) |
13544 | 207k | { |
13545 | 207k | SECStatus rv = SECFailure; |
13546 | 207k | PRBool isTLS, isTLS13; |
13547 | 207k | DTLSEpoch epoch; |
13548 | 207k | ssl3CipherSpec *spec = NULL; |
13549 | 207k | PRUint16 recordSizeLimit, cTextSizeLimit; |
13550 | 207k | PRBool outOfOrderSpec = PR_FALSE; |
13551 | 207k | SSLContentType rType; |
13552 | 207k | sslBuffer *plaintext = &ss->gs.buf; |
13553 | 207k | SSL3AlertDescription alert = internal_error; |
13554 | 207k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
13555 | | |
13556 | | /* check for Token Presence */ |
13557 | 207k | if (!ssl3_ClientAuthTokenPresent(ss->sec.ci.sid)) { |
13558 | 0 | PORT_SetError(SSL_ERROR_TOKEN_INSERTION_REMOVAL); |
13559 | 0 | return SECFailure; |
13560 | 0 | } |
13561 | | |
13562 | | /* Clear out the buffer in case this exits early. Any data then won't be |
13563 | | * processed twice. */ |
13564 | 207k | plaintext->len = 0; |
13565 | | |
13566 | | /* We're waiting for another ClientHello, which will appear unencrypted. |
13567 | | * Use the content type to tell whether this should be discarded. */ |
13568 | 207k | if (ss->ssl3.hs.zeroRttIgnore == ssl_0rtt_ignore_hrr && |
13569 | 207k | cText->hdr[0] == ssl_ct_application_data) { |
13570 | 0 | PORT_Assert(ss->ssl3.hs.ws == wait_client_hello); |
13571 | 0 | return SECSuccess; |
13572 | 0 | } |
13573 | | |
13574 | 207k | ssl_GetSpecReadLock(ss); /******************************************/ |
13575 | 207k | spec = ssl3_GetCipherSpec(ss, cText); |
13576 | 207k | if (!spec) { |
13577 | 0 | PORT_Assert(IS_DTLS(ss)); |
13578 | 0 | ssl_ReleaseSpecReadLock(ss); /*****************************/ |
13579 | 0 | return SECSuccess; |
13580 | 0 | } |
13581 | 207k | if (spec != ss->ssl3.crSpec) { |
13582 | 0 | PORT_Assert(IS_DTLS(ss)); |
13583 | 0 | SSL_TRC(3, ("%d: DTLS[%d]: Handling out-of-epoch record from epoch=%d", |
13584 | 0 | SSL_GETPID(), ss->fd, spec->epoch)); |
13585 | 0 | outOfOrderSpec = PR_TRUE; |
13586 | 0 | } |
13587 | 207k | isTLS = (PRBool)(spec->version > SSL_LIBRARY_VERSION_3_0); |
13588 | 207k | if (IS_DTLS(ss)) { |
13589 | 0 | if (dtls13_MaskSequenceNumber(ss, spec, cText->hdr, |
13590 | 0 | SSL_BUFFER_BASE(cText->buf), SSL_BUFFER_LEN(cText->buf)) != SECSuccess) { |
13591 | 0 | ssl_ReleaseSpecReadLock(ss); /*****************************/ |
13592 | | /* code already set. */ |
13593 | 0 | return SECFailure; |
13594 | 0 | } |
13595 | 0 | if (!dtls_IsRelevant(ss, spec, cText, &cText->seqNum)) { |
13596 | 0 | ssl_ReleaseSpecReadLock(ss); /*****************************/ |
13597 | 0 | return SECSuccess; |
13598 | 0 | } |
13599 | 207k | } else { |
13600 | 207k | cText->seqNum = spec->nextSeqNum; |
13601 | 207k | } |
13602 | 207k | if (cText->seqNum >= spec->cipherDef->max_records) { |
13603 | 0 | ssl_ReleaseSpecReadLock(ss); /*****************************/ |
13604 | 0 | SSL_TRC(3, ("%d: SSL[%d]: read sequence number at limit 0x%0llx", |
13605 | 0 | SSL_GETPID(), ss->fd, cText->seqNum)); |
13606 | 0 | PORT_SetError(SSL_ERROR_TOO_MANY_RECORDS); |
13607 | 0 | return SECFailure; |
13608 | 0 | } |
13609 | | |
13610 | 207k | isTLS13 = (PRBool)(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
13611 | 207k | recordSizeLimit = spec->recordSizeLimit; |
13612 | 207k | cTextSizeLimit = recordSizeLimit; |
13613 | 207k | cTextSizeLimit += (isTLS13) ? TLS_1_3_MAX_EXPANSION : TLS_1_2_MAX_EXPANSION; |
13614 | | |
13615 | | /* Check if the specified recordSizeLimit and the RFC8446 specified max |
13616 | | * expansion are respected. recordSizeLimit is probably at the default for |
13617 | | * the first (hello) handshake message and then set to a smaller size by |
13618 | | * the Record Size Limit Extension. |
13619 | | * Stricter expansion size checks dependent on implemented cipher suites |
13620 | | * are performed in ssl3con.c/ssl3_UnprotectRecord() OR |
13621 | | * tls13con.c/tls13_UnprotextRecord(). |
13622 | | * After Decryption the plaintext size is checked (l. 13424). This also |
13623 | | * applies to unencrypted records. */ |
13624 | 207k | if (cText->buf->len > cTextSizeLimit) { |
13625 | 0 | ssl_ReleaseSpecReadLock(ss); /*****************************/ |
13626 | | /* Drop DTLS Record Errors silently [RFC6347, Section 4.1.2.7] */ |
13627 | 0 | if (IS_DTLS(ss)) { |
13628 | 0 | return SECSuccess; |
13629 | 0 | } |
13630 | 0 | SSL3_SendAlert(ss, alert_fatal, record_overflow); |
13631 | 0 | PORT_SetError(SSL_ERROR_RX_RECORD_TOO_LONG); |
13632 | 0 | return SECFailure; |
13633 | 0 | } |
13634 | | |
13635 | 207k | #ifdef DEBUG |
13636 | | /* In debug builds the gather buffers are freed after the handling of each |
13637 | | * record for advanced ASAN coverage. Allocate the buffer again to the |
13638 | | * maximum possibly needed size as on gather initialization in |
13639 | | * ssl3gthr.c/ssl3_InitGather(). */ |
13640 | 207k | PR_ASSERT(sslBuffer_Grow(plaintext, TLS_1_2_MAX_CTEXT_LENGTH) == SECSuccess); |
13641 | 207k | #endif |
13642 | | /* This replaces a dynamic plaintext buffer size check, since the buffer is |
13643 | | * allocated to the maximum size in ssl3gthr.c/ssl3_InitGather(). The buffer |
13644 | | * was always grown to the maximum size at first record gathering before. */ |
13645 | 207k | PR_ASSERT(plaintext->space >= cTextSizeLimit); |
13646 | | |
13647 | | /* Most record types aside from protected TLS 1.3 records carry the content |
13648 | | * type in the first octet. TLS 1.3 will override this value later. */ |
13649 | 207k | rType = cText->hdr[0]; |
13650 | | /* Encrypted application data records could arrive before the handshake |
13651 | | * completes in DTLS 1.3. These can look like valid TLS 1.2 application_data |
13652 | | * records in epoch 0, which is never valid. Pretend they didn't decrypt. */ |
13653 | 207k | if (spec->epoch == 0 && ((IS_DTLS(ss) && |
13654 | 49.9k | dtls_IsDtls13Ciphertext(0, rType)) || |
13655 | 49.9k | rType == ssl_ct_application_data)) { |
13656 | 14 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_APPLICATION_DATA); |
13657 | 14 | alert = unexpected_message; |
13658 | 14 | rv = SECFailure; |
13659 | 207k | } else { |
13660 | 207k | #ifdef UNSAFE_FUZZER_MODE |
13661 | 207k | rv = Null_Cipher(NULL, plaintext->buf, &plaintext->len, |
13662 | 207k | plaintext->space, cText->buf->buf, cText->buf->len); |
13663 | | #else |
13664 | | /* IMPORTANT: |
13665 | | * Unprotect functions MUST NOT send alerts |
13666 | | * because we still hold the spec read lock. Instead, if they |
13667 | | * return SECFailure, they set *alert to the alert to be sent. |
13668 | | * Additionaly, this is used to silently drop DTLS encryption/record |
13669 | | * errors/alerts using the error handling below as suggested in the |
13670 | | * DTLS specification [RFC6347, Section 4.1.2.7]. */ |
13671 | | if (spec->cipherDef->cipher == cipher_null && cText->buf->len == 0) { |
13672 | | /* Handle a zero-length unprotected record |
13673 | | * In this case, we treat it as a no-op and let later functions decide |
13674 | | * whether to ignore or alert accordingly. */ |
13675 | | PR_ASSERT(plaintext->len == 0); |
13676 | | rv = SECSuccess; |
13677 | | } else if (spec->version < SSL_LIBRARY_VERSION_TLS_1_3 || spec->epoch == 0) { |
13678 | | rv = ssl3_UnprotectRecord(ss, spec, cText, plaintext, &alert); |
13679 | | } else { |
13680 | | rv = tls13_UnprotectRecord(ss, spec, cText, plaintext, &rType, |
13681 | | &alert); |
13682 | | } |
13683 | | #endif |
13684 | 207k | } |
13685 | | |
13686 | | /* Error/Alert handling for ssl3/tls13_UnprotectRecord */ |
13687 | 207k | if (rv != SECSuccess) { |
13688 | 14 | ssl_ReleaseSpecReadLock(ss); /***************************/ |
13689 | | |
13690 | 14 | SSL_DBG(("%d: SSL3[%d]: decryption failed", SSL_GETPID(), ss->fd)); |
13691 | | |
13692 | | /* Ensure that we don't process this data again. */ |
13693 | 14 | plaintext->len = 0; |
13694 | | |
13695 | | /* Ignore a CCS if compatibility mode is negotiated. Note that this |
13696 | | * will fail if the server fails to negotiate compatibility mode in a |
13697 | | * 0-RTT session that is resumed from a session that did negotiate it. |
13698 | | * We don't care about that corner case right now. */ |
13699 | 14 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
13700 | 14 | cText->hdr[0] == ssl_ct_change_cipher_spec && |
13701 | 14 | ss->ssl3.hs.ws != idle_handshake && |
13702 | 14 | cText->buf->len == 1 && |
13703 | 14 | cText->buf->buf[0] == change_cipher_spec_choice) { |
13704 | 0 | if (!ss->ssl3.hs.rejectCcs) { |
13705 | | /* Allow only the first CCS. */ |
13706 | 0 | ss->ssl3.hs.rejectCcs = PR_TRUE; |
13707 | 0 | return SECSuccess; |
13708 | 0 | } else { |
13709 | 0 | alert = unexpected_message; |
13710 | 0 | PORT_SetError(SSL_ERROR_RX_MALFORMED_CHANGE_CIPHER); |
13711 | 0 | } |
13712 | 0 | } |
13713 | | |
13714 | | /* All errors/alerts that might occur during unprotection are related |
13715 | | * to invalid records (e.g. invalid formatting, length, MAC, ...). |
13716 | | * Following the DTLS specification such errors/alerts SHOULD be |
13717 | | * dropped silently [RFC9147, Section 4.5.2]. |
13718 | | * This is done below. */ |
13719 | | |
13720 | 14 | if ((IS_DTLS(ss) && !dtls13_AeadLimitReached(spec)) || |
13721 | 14 | (!IS_DTLS(ss) && ss->sec.isServer && |
13722 | 14 | ss->ssl3.hs.zeroRttIgnore == ssl_0rtt_ignore_trial)) { |
13723 | | /* Silently drop the packet unless we set ss->ssl3.fatalAlertSent. |
13724 | | * (Manually or by using functions like |
13725 | | * SSL3_SendAlert(.., alert_fatal,..)) |
13726 | | * This is not currently used in the unprotection functions since |
13727 | | * all TLS and DTLS errors are propagated to this handler. */ |
13728 | 0 | if (ss->ssl3.fatalAlertSent) { |
13729 | 0 | return SECFailure; |
13730 | 0 | } |
13731 | 0 | return SECSuccess; |
13732 | 0 | } |
13733 | | |
13734 | 14 | int errCode = PORT_GetError(); |
13735 | 14 | SSL3_SendAlert(ss, alert_fatal, alert); |
13736 | | /* Reset the error code in case SSL3_SendAlert called |
13737 | | * PORT_SetError(). */ |
13738 | 14 | PORT_SetError(errCode); |
13739 | 14 | return SECFailure; |
13740 | 14 | } |
13741 | | |
13742 | | /* SECSuccess */ |
13743 | 207k | if (IS_DTLS(ss)) { |
13744 | 0 | dtls_RecordSetRecvd(&spec->recvdRecords, cText->seqNum); |
13745 | 0 | spec->nextSeqNum = PR_MAX(spec->nextSeqNum, cText->seqNum + 1); |
13746 | 207k | } else { |
13747 | 207k | ++spec->nextSeqNum; |
13748 | 207k | } |
13749 | 207k | epoch = spec->epoch; |
13750 | | |
13751 | 207k | ssl_ReleaseSpecReadLock(ss); /*****************************************/ |
13752 | | |
13753 | | /* |
13754 | | * The decrypted data is now in plaintext. |
13755 | | */ |
13756 | | |
13757 | | /* IMPORTANT: We are in DTLS 1.3 mode and we have processed something |
13758 | | * from the wrong epoch. Divert to a divert processing function to make |
13759 | | * sure we don't accidentally use the data unsafely. */ |
13760 | | |
13761 | | /* We temporary allowed reading the records from the previous epoch n-1 |
13762 | | until the moment we get a message from the new epoch n. */ |
13763 | | |
13764 | 207k | if (outOfOrderSpec) { |
13765 | 0 | PORT_Assert(IS_DTLS(ss) && ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
13766 | 0 | ssl_GetSSL3HandshakeLock(ss); |
13767 | 0 | if (ss->ssl3.hs.allowPreviousEpoch && spec->epoch == ss->ssl3.crSpec->epoch - 1) { |
13768 | 0 | SSL_TRC(30, ("%d: DTLS13[%d]: Out of order message %d is accepted", |
13769 | 0 | SSL_GETPID(), ss->fd, spec->epoch)); |
13770 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
13771 | 0 | } else { |
13772 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
13773 | 0 | return dtls13_HandleOutOfEpochRecord(ss, spec, rType, plaintext); |
13774 | 0 | } |
13775 | 207k | } else { |
13776 | 207k | ssl_GetSSL3HandshakeLock(ss); |
13777 | | /* Forbid (application) messages from the previous epoch. |
13778 | | From now, messages that arrive out of order will be discarded. */ |
13779 | 207k | ss->ssl3.hs.allowPreviousEpoch = PR_FALSE; |
13780 | 207k | ssl_ReleaseSSL3HandshakeLock(ss); |
13781 | 207k | } |
13782 | | |
13783 | | /* Check the length of the plaintext. */ |
13784 | 207k | if (isTLS && plaintext->len > recordSizeLimit) { |
13785 | 9 | plaintext->len = 0; |
13786 | | /* Drop DTLS Record Errors silently [RFC6347, Section 4.1.2.7] */ |
13787 | 9 | if (IS_DTLS(ss)) { |
13788 | 0 | return SECSuccess; |
13789 | 0 | } |
13790 | 9 | SSL3_SendAlert(ss, alert_fatal, record_overflow); |
13791 | 9 | PORT_SetError(SSL_ERROR_RX_RECORD_TOO_LONG); |
13792 | 9 | return SECFailure; |
13793 | 9 | } |
13794 | | |
13795 | | /* Application data records are processed by the caller of this |
13796 | | ** function, not by this function. |
13797 | | */ |
13798 | 207k | if (rType == ssl_ct_application_data) { |
13799 | 4.44k | if (ss->firstHsDone) |
13800 | 4.44k | return SECSuccess; |
13801 | 2 | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
13802 | 2 | ss->sec.isServer && |
13803 | 2 | ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted) { |
13804 | 0 | return tls13_HandleEarlyApplicationData(ss, plaintext); |
13805 | 0 | } |
13806 | 2 | plaintext->len = 0; |
13807 | 2 | (void)SSL3_SendAlert(ss, alert_fatal, unexpected_message); |
13808 | 2 | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_APPLICATION_DATA); |
13809 | 2 | return SECFailure; |
13810 | 2 | } |
13811 | | |
13812 | 202k | rv = ssl3_HandleNonApplicationData(ss, rType, epoch, cText->seqNum, |
13813 | 202k | plaintext); |
13814 | | |
13815 | 202k | #ifdef DEBUG |
13816 | | /* In Debug builds free and zero gather plaintext buffer after its content |
13817 | | * has been used/copied for advanced ASAN coverage/utilization. |
13818 | | * This frees buffer for non application data records, for application data |
13819 | | * records it is freed in sslsecur.c/DoRecv(). */ |
13820 | 202k | sslBuffer_Clear(&ss->gs.buf); |
13821 | 202k | #endif |
13822 | | |
13823 | 202k | return rv; |
13824 | 207k | } |
13825 | | |
13826 | | /* |
13827 | | * Initialization functions |
13828 | | */ |
13829 | | |
13830 | | void |
13831 | | ssl_InitSecState(sslSecurityInfo *sec) |
13832 | 10.1k | { |
13833 | 10.1k | sec->authType = ssl_auth_null; |
13834 | 10.1k | sec->authKeyBits = 0; |
13835 | 10.1k | sec->signatureScheme = ssl_sig_none; |
13836 | 10.1k | sec->keaType = ssl_kea_null; |
13837 | 10.1k | sec->keaKeyBits = 0; |
13838 | 10.1k | sec->keaGroup = NULL; |
13839 | 10.1k | } |
13840 | | |
13841 | | SECStatus |
13842 | | ssl3_InitState(sslSocket *ss) |
13843 | 10.1k | { |
13844 | 10.1k | SECStatus rv; |
13845 | | |
13846 | 10.1k | ss->ssl3.policy = SSL_ALLOWED; |
13847 | | |
13848 | 10.1k | ssl_InitSecState(&ss->sec); |
13849 | | |
13850 | 10.1k | ssl_GetSpecWriteLock(ss); |
13851 | 10.1k | PR_INIT_CLIST(&ss->ssl3.hs.cipherSpecs); |
13852 | 10.1k | rv = ssl_SetupNullCipherSpec(ss, ssl_secret_read); |
13853 | 10.1k | rv |= ssl_SetupNullCipherSpec(ss, ssl_secret_write); |
13854 | 10.1k | ss->ssl3.pwSpec = ss->ssl3.prSpec = NULL; |
13855 | 10.1k | ssl_ReleaseSpecWriteLock(ss); |
13856 | 10.1k | if (rv != SECSuccess) { |
13857 | | /* Rely on ssl_CreateNullCipherSpec() to set error code. */ |
13858 | 0 | return SECFailure; |
13859 | 0 | } |
13860 | | |
13861 | 10.1k | ss->ssl3.hs.sendingSCSV = PR_FALSE; |
13862 | 10.1k | ss->ssl3.hs.preliminaryInfo = 0; |
13863 | 10.1k | ss->ssl3.hs.ws = (ss->sec.isServer) ? wait_client_hello : idle_handshake; |
13864 | | |
13865 | 10.1k | ssl3_ResetExtensionData(&ss->xtnData, ss); |
13866 | 10.1k | PR_INIT_CLIST(&ss->ssl3.hs.remoteExtensions); |
13867 | 10.1k | PR_INIT_CLIST(&ss->ssl3.hs.echOuterExtensions); |
13868 | 10.1k | if (IS_DTLS(ss)) { |
13869 | 0 | ss->ssl3.hs.sendMessageSeq = 0; |
13870 | 0 | ss->ssl3.hs.recvMessageSeq = 0; |
13871 | 0 | ss->ssl3.hs.rtTimer->timeout = DTLS_RETRANSMIT_INITIAL_MS; |
13872 | 0 | ss->ssl3.hs.rtRetries = 0; |
13873 | 0 | ss->ssl3.hs.recvdHighWater = -1; |
13874 | 0 | PR_INIT_CLIST(&ss->ssl3.hs.lastMessageFlight); |
13875 | 0 | dtls_SetMTU(ss, 0); /* Set the MTU to the highest plateau */ |
13876 | 0 | } |
13877 | | |
13878 | 10.1k | ss->ssl3.hs.currentSecret = NULL; |
13879 | 10.1k | ss->ssl3.hs.resumptionMasterSecret = NULL; |
13880 | 10.1k | ss->ssl3.hs.dheSecret = NULL; |
13881 | 10.1k | ss->ssl3.hs.clientEarlyTrafficSecret = NULL; |
13882 | 10.1k | ss->ssl3.hs.clientHsTrafficSecret = NULL; |
13883 | 10.1k | ss->ssl3.hs.serverHsTrafficSecret = NULL; |
13884 | 10.1k | ss->ssl3.hs.clientTrafficSecret = NULL; |
13885 | 10.1k | ss->ssl3.hs.serverTrafficSecret = NULL; |
13886 | 10.1k | ss->ssl3.hs.echHpkeCtx = NULL; |
13887 | 10.1k | ss->ssl3.hs.greaseEchSize = 100; |
13888 | 10.1k | ss->ssl3.hs.echAccepted = PR_FALSE; |
13889 | 10.1k | ss->ssl3.hs.echDecided = PR_FALSE; |
13890 | | |
13891 | 10.1k | ss->ssl3.hs.clientAuthSignatureSchemes = NULL; |
13892 | 10.1k | ss->ssl3.hs.clientAuthSignatureSchemesLen = 0; |
13893 | | |
13894 | 10.1k | PORT_Assert(!ss->ssl3.hs.messages.buf && !ss->ssl3.hs.messages.space); |
13895 | 10.1k | ss->ssl3.hs.messages.buf = NULL; |
13896 | 10.1k | ss->ssl3.hs.messages.space = 0; |
13897 | | |
13898 | 10.1k | ss->ssl3.hs.receivedNewSessionTicket = PR_FALSE; |
13899 | 10.1k | PORT_Memset(&ss->ssl3.hs.newSessionTicket, 0, |
13900 | 10.1k | sizeof(ss->ssl3.hs.newSessionTicket)); |
13901 | | |
13902 | 10.1k | ss->ssl3.hs.zeroRttState = ssl_0rtt_none; |
13903 | 10.1k | return SECSuccess; |
13904 | 10.1k | } |
13905 | | |
13906 | | /* record the export policy for this cipher suite */ |
13907 | | SECStatus |
13908 | | ssl3_SetPolicy(ssl3CipherSuite which, int policy) |
13909 | 0 | { |
13910 | 0 | ssl3CipherSuiteCfg *suite; |
13911 | |
|
13912 | 0 | suite = ssl_LookupCipherSuiteCfgMutable(which, cipherSuites); |
13913 | 0 | if (suite == NULL) { |
13914 | 0 | return SECFailure; /* err code was set by ssl_LookupCipherSuiteCfg */ |
13915 | 0 | } |
13916 | 0 | suite->policy = policy; |
13917 | |
|
13918 | 0 | return SECSuccess; |
13919 | 0 | } |
13920 | | |
13921 | | SECStatus |
13922 | | ssl3_GetPolicy(ssl3CipherSuite which, PRInt32 *oPolicy) |
13923 | 0 | { |
13924 | 0 | const ssl3CipherSuiteCfg *suite; |
13925 | 0 | PRInt32 policy; |
13926 | 0 | SECStatus rv; |
13927 | |
|
13928 | 0 | suite = ssl_LookupCipherSuiteCfg(which, cipherSuites); |
13929 | 0 | if (suite) { |
13930 | 0 | policy = suite->policy; |
13931 | 0 | rv = SECSuccess; |
13932 | 0 | } else { |
13933 | 0 | policy = SSL_NOT_ALLOWED; |
13934 | 0 | rv = SECFailure; /* err code was set by Lookup. */ |
13935 | 0 | } |
13936 | 0 | *oPolicy = policy; |
13937 | 0 | return rv; |
13938 | 0 | } |
13939 | | |
13940 | | /* record the user preference for this suite */ |
13941 | | SECStatus |
13942 | | ssl3_CipherPrefSetDefault(ssl3CipherSuite which, PRBool enabled) |
13943 | 0 | { |
13944 | 0 | ssl3CipherSuiteCfg *suite; |
13945 | |
|
13946 | 0 | suite = ssl_LookupCipherSuiteCfgMutable(which, cipherSuites); |
13947 | 0 | if (suite == NULL) { |
13948 | 0 | return SECFailure; /* err code was set by ssl_LookupCipherSuiteCfg */ |
13949 | 0 | } |
13950 | 0 | suite->enabled = enabled; |
13951 | 0 | return SECSuccess; |
13952 | 0 | } |
13953 | | |
13954 | | /* return the user preference for this suite */ |
13955 | | SECStatus |
13956 | | ssl3_CipherPrefGetDefault(ssl3CipherSuite which, PRBool *enabled) |
13957 | 0 | { |
13958 | 0 | const ssl3CipherSuiteCfg *suite; |
13959 | 0 | PRBool pref; |
13960 | 0 | SECStatus rv; |
13961 | |
|
13962 | 0 | suite = ssl_LookupCipherSuiteCfg(which, cipherSuites); |
13963 | 0 | if (suite) { |
13964 | 0 | pref = suite->enabled; |
13965 | 0 | rv = SECSuccess; |
13966 | 0 | } else { |
13967 | 0 | pref = SSL_NOT_ALLOWED; |
13968 | 0 | rv = SECFailure; /* err code was set by Lookup. */ |
13969 | 0 | } |
13970 | 0 | *enabled = pref; |
13971 | 0 | return rv; |
13972 | 0 | } |
13973 | | |
13974 | | SECStatus |
13975 | | ssl3_CipherPrefSet(sslSocket *ss, ssl3CipherSuite which, PRBool enabled) |
13976 | 721k | { |
13977 | 721k | ssl3CipherSuiteCfg *suite; |
13978 | | |
13979 | 721k | suite = ssl_LookupCipherSuiteCfgMutable(which, ss->cipherSuites); |
13980 | 721k | if (suite == NULL) { |
13981 | 0 | return SECFailure; /* err code was set by ssl_LookupCipherSuiteCfg */ |
13982 | 0 | } |
13983 | 721k | suite->enabled = enabled; |
13984 | 721k | return SECSuccess; |
13985 | 721k | } |
13986 | | |
13987 | | SECStatus |
13988 | | ssl3_CipherPrefGet(const sslSocket *ss, ssl3CipherSuite which, PRBool *enabled) |
13989 | 56.0k | { |
13990 | 56.0k | const ssl3CipherSuiteCfg *suite; |
13991 | 56.0k | PRBool pref; |
13992 | 56.0k | SECStatus rv; |
13993 | | |
13994 | 56.0k | suite = ssl_LookupCipherSuiteCfg(which, ss->cipherSuites); |
13995 | 56.0k | if (suite) { |
13996 | 56.0k | pref = suite->enabled; |
13997 | 56.0k | rv = SECSuccess; |
13998 | 56.0k | } else { |
13999 | 0 | pref = SSL_NOT_ALLOWED; |
14000 | 0 | rv = SECFailure; /* err code was set by Lookup. */ |
14001 | 0 | } |
14002 | 56.0k | *enabled = pref; |
14003 | 56.0k | return rv; |
14004 | 56.0k | } |
14005 | | |
14006 | | SECStatus |
14007 | | SSL_SignatureSchemePrefSet(PRFileDesc *fd, const SSLSignatureScheme *schemes, |
14008 | | unsigned int count) |
14009 | 0 | { |
14010 | 0 | sslSocket *ss; |
14011 | 0 | unsigned int i; |
14012 | 0 | unsigned int supported = 0; |
14013 | |
|
14014 | 0 | ss = ssl_FindSocket(fd); |
14015 | 0 | if (!ss) { |
14016 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SignatureSchemePrefSet", |
14017 | 0 | SSL_GETPID(), fd)); |
14018 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
14019 | 0 | return SECFailure; |
14020 | 0 | } |
14021 | | |
14022 | 0 | if (!count) { |
14023 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
14024 | 0 | return SECFailure; |
14025 | 0 | } |
14026 | | |
14027 | 0 | for (i = 0; i < count; ++i) { |
14028 | 0 | if (ssl_IsSupportedSignatureScheme(schemes[i])) { |
14029 | 0 | ++supported; |
14030 | 0 | } |
14031 | 0 | } |
14032 | | /* We don't check for duplicates, so it's possible to get too many. */ |
14033 | 0 | if (supported > MAX_SIGNATURE_SCHEMES) { |
14034 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
14035 | 0 | return SECFailure; |
14036 | 0 | } |
14037 | | |
14038 | 0 | ss->ssl3.signatureSchemeCount = 0; |
14039 | 0 | for (i = 0; i < count; ++i) { |
14040 | 0 | if (!ssl_IsSupportedSignatureScheme(schemes[i])) { |
14041 | 0 | SSL_DBG(("%d: SSL[%d]: invalid signature scheme %d ignored", |
14042 | 0 | SSL_GETPID(), fd, schemes[i])); |
14043 | 0 | continue; |
14044 | 0 | } |
14045 | | |
14046 | 0 | ss->ssl3.signatureSchemes[ss->ssl3.signatureSchemeCount++] = schemes[i]; |
14047 | 0 | } |
14048 | |
|
14049 | 0 | if (ss->ssl3.signatureSchemeCount == 0) { |
14050 | 0 | PORT_SetError(SSL_ERROR_NO_SUPPORTED_SIGNATURE_ALGORITHM); |
14051 | 0 | return SECFailure; |
14052 | 0 | } |
14053 | 0 | return SECSuccess; |
14054 | 0 | } |
14055 | | |
14056 | | SECStatus |
14057 | | SSL_SignaturePrefSet(PRFileDesc *fd, const SSLSignatureAndHashAlg *algorithms, |
14058 | | unsigned int count) |
14059 | 0 | { |
14060 | 0 | SSLSignatureScheme schemes[MAX_SIGNATURE_SCHEMES]; |
14061 | 0 | unsigned int i; |
14062 | |
|
14063 | 0 | count = PR_MIN(PR_ARRAY_SIZE(schemes), count); |
14064 | 0 | for (i = 0; i < count; ++i) { |
14065 | 0 | schemes[i] = (algorithms[i].hashAlg << 8) | algorithms[i].sigAlg; |
14066 | 0 | } |
14067 | 0 | return SSL_SignatureSchemePrefSet(fd, schemes, count); |
14068 | 0 | } |
14069 | | |
14070 | | SECStatus |
14071 | | SSL_SignatureSchemePrefGet(PRFileDesc *fd, SSLSignatureScheme *schemes, |
14072 | | unsigned int *count, unsigned int maxCount) |
14073 | 0 | { |
14074 | 0 | sslSocket *ss; |
14075 | |
|
14076 | 0 | ss = ssl_FindSocket(fd); |
14077 | 0 | if (!ss) { |
14078 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SignatureSchemePrefGet", |
14079 | 0 | SSL_GETPID(), fd)); |
14080 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
14081 | 0 | return SECFailure; |
14082 | 0 | } |
14083 | | |
14084 | 0 | if (!schemes || !count || |
14085 | 0 | maxCount < ss->ssl3.signatureSchemeCount) { |
14086 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
14087 | 0 | return SECFailure; |
14088 | 0 | } |
14089 | | |
14090 | 0 | PORT_Memcpy(schemes, ss->ssl3.signatureSchemes, |
14091 | 0 | ss->ssl3.signatureSchemeCount * sizeof(SSLSignatureScheme)); |
14092 | 0 | *count = ss->ssl3.signatureSchemeCount; |
14093 | 0 | return SECSuccess; |
14094 | 0 | } |
14095 | | |
14096 | | SECStatus |
14097 | | SSL_SignaturePrefGet(PRFileDesc *fd, SSLSignatureAndHashAlg *algorithms, |
14098 | | unsigned int *count, unsigned int maxCount) |
14099 | 0 | { |
14100 | 0 | sslSocket *ss; |
14101 | 0 | unsigned int i; |
14102 | |
|
14103 | 0 | ss = ssl_FindSocket(fd); |
14104 | 0 | if (!ss) { |
14105 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SignaturePrefGet", |
14106 | 0 | SSL_GETPID(), fd)); |
14107 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
14108 | 0 | return SECFailure; |
14109 | 0 | } |
14110 | | |
14111 | 0 | if (!algorithms || !count || |
14112 | 0 | maxCount < ss->ssl3.signatureSchemeCount) { |
14113 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
14114 | 0 | return SECFailure; |
14115 | 0 | } |
14116 | | |
14117 | 0 | for (i = 0; i < ss->ssl3.signatureSchemeCount; ++i) { |
14118 | 0 | algorithms[i].hashAlg = (ss->ssl3.signatureSchemes[i] >> 8) & 0xff; |
14119 | 0 | algorithms[i].sigAlg = ss->ssl3.signatureSchemes[i] & 0xff; |
14120 | 0 | } |
14121 | 0 | *count = ss->ssl3.signatureSchemeCount; |
14122 | 0 | return SECSuccess; |
14123 | 0 | } |
14124 | | |
14125 | | unsigned int |
14126 | | SSL_SignatureMaxCount(void) |
14127 | 0 | { |
14128 | 0 | return MAX_SIGNATURE_SCHEMES; |
14129 | 0 | } |
14130 | | |
14131 | | /* copy global default policy into socket. */ |
14132 | | void |
14133 | | ssl3_InitSocketPolicy(sslSocket *ss) |
14134 | 10.1k | { |
14135 | 10.1k | PORT_Memcpy(ss->cipherSuites, cipherSuites, sizeof(cipherSuites)); |
14136 | 10.1k | PORT_Memcpy(ss->ssl3.signatureSchemes, defaultSignatureSchemes, |
14137 | 10.1k | sizeof(defaultSignatureSchemes)); |
14138 | 10.1k | ss->ssl3.signatureSchemeCount = PR_ARRAY_SIZE(defaultSignatureSchemes); |
14139 | 10.1k | } |
14140 | | |
14141 | | /* |
14142 | | ** If ssl3 socket has completed the first handshake, and is in idle state, |
14143 | | ** then start a new handshake. |
14144 | | ** If flushCache is true, the SID cache will be flushed first, forcing a |
14145 | | ** "Full" handshake (not a session restart handshake), to be done. |
14146 | | ** |
14147 | | ** called from SSL_RedoHandshake(), which already holds the handshake locks. |
14148 | | */ |
14149 | | SECStatus |
14150 | | ssl3_RedoHandshake(sslSocket *ss, PRBool flushCache) |
14151 | 0 | { |
14152 | 0 | sslSessionID *sid = ss->sec.ci.sid; |
14153 | 0 | SECStatus rv; |
14154 | |
|
14155 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
14156 | |
|
14157 | 0 | if (!ss->firstHsDone || (ss->ssl3.hs.ws != idle_handshake)) { |
14158 | 0 | PORT_SetError(SSL_ERROR_HANDSHAKE_NOT_COMPLETED); |
14159 | 0 | return SECFailure; |
14160 | 0 | } |
14161 | | |
14162 | 0 | if (IS_DTLS(ss)) { |
14163 | 0 | dtls_RehandshakeCleanup(ss); |
14164 | 0 | } |
14165 | |
|
14166 | 0 | if (ss->opt.enableRenegotiation == SSL_RENEGOTIATE_NEVER || |
14167 | 0 | ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
14168 | 0 | PORT_SetError(SSL_ERROR_RENEGOTIATION_NOT_ALLOWED); |
14169 | 0 | return SECFailure; |
14170 | 0 | } |
14171 | 0 | if (ss->version > ss->vrange.max || ss->version < ss->vrange.min) { |
14172 | 0 | PORT_SetError(SSL_ERROR_UNSUPPORTED_VERSION); |
14173 | 0 | return SECFailure; |
14174 | 0 | } |
14175 | | |
14176 | 0 | if (sid && flushCache) { |
14177 | 0 | ssl_UncacheSessionID(ss); /* remove it from whichever cache it's in. */ |
14178 | 0 | ssl_FreeSID(sid); /* dec ref count and free if zero. */ |
14179 | 0 | ss->sec.ci.sid = NULL; |
14180 | 0 | } |
14181 | |
|
14182 | 0 | ssl_GetXmitBufLock(ss); /**************************************/ |
14183 | | |
14184 | | /* start off a new handshake. */ |
14185 | 0 | if (ss->sec.isServer) { |
14186 | 0 | rv = ssl3_SendHelloRequest(ss); |
14187 | 0 | } else { |
14188 | 0 | rv = ssl3_SendClientHello(ss, client_hello_renegotiation); |
14189 | 0 | } |
14190 | |
|
14191 | 0 | ssl_ReleaseXmitBufLock(ss); /**************************************/ |
14192 | 0 | return rv; |
14193 | 0 | } |
14194 | | |
14195 | | /* Called from ssl_DestroySocketContents() in sslsock.c */ |
14196 | | void |
14197 | | ssl3_DestroySSL3Info(sslSocket *ss) |
14198 | 10.1k | { |
14199 | | |
14200 | 10.1k | if (ss->ssl3.clientCertificate != NULL) |
14201 | 0 | CERT_DestroyCertificate(ss->ssl3.clientCertificate); |
14202 | | |
14203 | 10.1k | if (ss->ssl3.clientPrivateKey != NULL) |
14204 | 0 | SECKEY_DestroyPrivateKey(ss->ssl3.clientPrivateKey); |
14205 | | |
14206 | 10.1k | if (ss->ssl3.hs.clientAuthSignatureSchemes != NULL) { |
14207 | 0 | PORT_Free(ss->ssl3.hs.clientAuthSignatureSchemes); |
14208 | 0 | ss->ssl3.hs.clientAuthSignatureSchemes = NULL; |
14209 | 0 | ss->ssl3.hs.clientAuthSignatureSchemesLen = 0; |
14210 | 0 | } |
14211 | | |
14212 | 10.1k | if (ss->ssl3.peerCertArena != NULL) |
14213 | 7.84k | ssl3_CleanupPeerCerts(ss); |
14214 | | |
14215 | 10.1k | if (ss->ssl3.clientCertChain != NULL) { |
14216 | 0 | CERT_DestroyCertificateList(ss->ssl3.clientCertChain); |
14217 | 0 | ss->ssl3.clientCertChain = NULL; |
14218 | 0 | } |
14219 | 10.1k | if (ss->ssl3.ca_list) { |
14220 | 0 | CERT_FreeDistNames(ss->ssl3.ca_list); |
14221 | 0 | } |
14222 | | |
14223 | | /* clean up handshake */ |
14224 | 10.1k | if (ss->ssl3.hs.md5) { |
14225 | 3.25k | PK11_DestroyContext(ss->ssl3.hs.md5, PR_TRUE); |
14226 | 3.25k | } |
14227 | 10.1k | if (ss->ssl3.hs.sha) { |
14228 | 3.91k | PK11_DestroyContext(ss->ssl3.hs.sha, PR_TRUE); |
14229 | 3.91k | } |
14230 | 10.1k | if (ss->ssl3.hs.shaEchInner) { |
14231 | 87 | PK11_DestroyContext(ss->ssl3.hs.shaEchInner, PR_TRUE); |
14232 | 87 | } |
14233 | 10.1k | if (ss->ssl3.hs.shaPostHandshake) { |
14234 | 0 | PK11_DestroyContext(ss->ssl3.hs.shaPostHandshake, PR_TRUE); |
14235 | 0 | } |
14236 | 10.1k | if (ss->ssl3.hs.messages.buf) { |
14237 | 7.35k | sslBuffer_Clear(&ss->ssl3.hs.messages); |
14238 | 7.35k | } |
14239 | 10.1k | if (ss->ssl3.hs.echInnerMessages.buf) { |
14240 | 3.26k | sslBuffer_Clear(&ss->ssl3.hs.echInnerMessages); |
14241 | 3.26k | } |
14242 | 10.1k | if (ss->ssl3.hs.dtls13ClientMessageBuffer.buf) { |
14243 | 0 | sslBuffer_Clear(&ss->ssl3.hs.dtls13ClientMessageBuffer); |
14244 | 0 | } |
14245 | | |
14246 | | /* free the SSL3Buffer (msg_body) */ |
14247 | 10.1k | PORT_Free(ss->ssl3.hs.msg_body.buf); |
14248 | | |
14249 | 10.1k | SECITEM_FreeItem(&ss->ssl3.hs.newSessionTicket.ticket, PR_FALSE); |
14250 | 10.1k | SECITEM_FreeItem(&ss->ssl3.hs.srvVirtName, PR_FALSE); |
14251 | 10.1k | SECITEM_FreeItem(&ss->ssl3.hs.fakeSid, PR_FALSE); |
14252 | | |
14253 | | /* Destroy the DTLS data */ |
14254 | 10.1k | if (IS_DTLS(ss)) { |
14255 | 0 | dtls_FreeHandshakeMessages(&ss->ssl3.hs.lastMessageFlight); |
14256 | 0 | if (ss->ssl3.hs.recvdFragments.buf) { |
14257 | 0 | PORT_Free(ss->ssl3.hs.recvdFragments.buf); |
14258 | 0 | } |
14259 | 0 | } |
14260 | | |
14261 | | /* Destroy remote extensions */ |
14262 | 10.1k | ssl3_DestroyRemoteExtensions(&ss->ssl3.hs.remoteExtensions); |
14263 | 10.1k | ssl3_DestroyRemoteExtensions(&ss->ssl3.hs.echOuterExtensions); |
14264 | 10.1k | ssl3_DestroyExtensionData(&ss->xtnData); |
14265 | | |
14266 | | /* Destroy cipher specs */ |
14267 | 10.1k | ssl_DestroyCipherSpecs(&ss->ssl3.hs.cipherSpecs); |
14268 | | |
14269 | | /* Destroy TLS 1.3 keys */ |
14270 | 10.1k | if (ss->ssl3.hs.currentSecret) |
14271 | 4.34k | PK11_FreeSymKey(ss->ssl3.hs.currentSecret); |
14272 | 10.1k | if (ss->ssl3.hs.resumptionMasterSecret) |
14273 | 170 | PK11_FreeSymKey(ss->ssl3.hs.resumptionMasterSecret); |
14274 | 10.1k | if (ss->ssl3.hs.dheSecret) |
14275 | 0 | PK11_FreeSymKey(ss->ssl3.hs.dheSecret); |
14276 | 10.1k | if (ss->ssl3.hs.clientEarlyTrafficSecret) |
14277 | 0 | PK11_FreeSymKey(ss->ssl3.hs.clientEarlyTrafficSecret); |
14278 | 10.1k | if (ss->ssl3.hs.clientHsTrafficSecret) |
14279 | 316 | PK11_FreeSymKey(ss->ssl3.hs.clientHsTrafficSecret); |
14280 | 10.1k | if (ss->ssl3.hs.serverHsTrafficSecret) |
14281 | 316 | PK11_FreeSymKey(ss->ssl3.hs.serverHsTrafficSecret); |
14282 | 10.1k | if (ss->ssl3.hs.clientTrafficSecret) |
14283 | 170 | PK11_FreeSymKey(ss->ssl3.hs.clientTrafficSecret); |
14284 | 10.1k | if (ss->ssl3.hs.serverTrafficSecret) |
14285 | 170 | PK11_FreeSymKey(ss->ssl3.hs.serverTrafficSecret); |
14286 | 10.1k | if (ss->ssl3.hs.earlyExporterSecret) |
14287 | 0 | PK11_FreeSymKey(ss->ssl3.hs.earlyExporterSecret); |
14288 | 10.1k | if (ss->ssl3.hs.exporterSecret) |
14289 | 170 | PK11_FreeSymKey(ss->ssl3.hs.exporterSecret); |
14290 | | |
14291 | 10.1k | ss->ssl3.hs.zeroRttState = ssl_0rtt_none; |
14292 | | /* Destroy TLS 1.3 buffered early data. */ |
14293 | 10.1k | tls13_DestroyEarlyData(&ss->ssl3.hs.bufferedEarlyData); |
14294 | | |
14295 | | /* Destroy TLS 1.3 PSKs. */ |
14296 | 10.1k | tls13_DestroyPskList(&ss->ssl3.hs.psks); |
14297 | | |
14298 | | /* TLS 1.3 ECH state. */ |
14299 | 10.1k | PK11_HPKE_DestroyContext(ss->ssl3.hs.echHpkeCtx, PR_TRUE); |
14300 | 10.1k | PORT_Free((void *)ss->ssl3.hs.echPublicName); /* CONST */ |
14301 | 10.1k | sslBuffer_Clear(&ss->ssl3.hs.greaseEchBuf); |
14302 | | |
14303 | | /* TLS 1.3 GREASE (client) state. */ |
14304 | 10.1k | tls13_ClientGreaseDestroy(ss); |
14305 | | |
14306 | | /* TLS ClientHello Extension Permutation state. */ |
14307 | 10.1k | tls_ClientHelloExtensionPermutationDestroy(ss); |
14308 | 10.1k | } |
14309 | | |
14310 | | /* check if the current cipher spec is FIPS. We only need to |
14311 | | * check the contexts here, if the kea, prf or keys were not FIPS, |
14312 | | * that status would have been rolled up in the create context |
14313 | | * call */ |
14314 | | static PRBool |
14315 | | ssl_cipherSpecIsFips(ssl3CipherSpec *spec) |
14316 | 0 | { |
14317 | 0 | if (!spec || !spec->cipherDef) { |
14318 | 0 | return PR_FALSE; |
14319 | 0 | } |
14320 | | |
14321 | 0 | if (spec->cipherDef->type != type_aead) { |
14322 | 0 | if (spec->keyMaterial.macContext == NULL) { |
14323 | 0 | return PR_FALSE; |
14324 | 0 | } |
14325 | 0 | if (!PK11_ContextGetFIPSStatus(spec->keyMaterial.macContext)) { |
14326 | 0 | return PR_FALSE; |
14327 | 0 | } |
14328 | 0 | } |
14329 | 0 | if (!spec->cipherContext) { |
14330 | 0 | return PR_FALSE; |
14331 | 0 | } |
14332 | 0 | return PK11_ContextGetFIPSStatus(spec->cipherContext); |
14333 | 0 | } |
14334 | | |
14335 | | /* return true if the current operation is running in FIPS mode */ |
14336 | | PRBool |
14337 | | ssl_isFIPS(sslSocket *ss) |
14338 | 0 | { |
14339 | 0 | if (!ssl_cipherSpecIsFips(ss->ssl3.crSpec)) { |
14340 | 0 | return PR_FALSE; |
14341 | 0 | } |
14342 | 0 | return ssl_cipherSpecIsFips(ss->ssl3.cwSpec); |
14343 | 0 | } |
14344 | | |
14345 | | /* |
14346 | | * parse the policy value for a single algorithm in a cipher_suite, |
14347 | | * return TRUE if we disallow by the cipher suite by policy |
14348 | | * (we don't have to parse any more algorithm policies on this cipher suite), |
14349 | | * otherwise return FALSE. |
14350 | | * 1. If we don't have the required policy, disable by default, disallow by |
14351 | | * policy and return TRUE (no more processing needed). |
14352 | | * 2. If we have the required policy, and we are disabled, return FALSE, |
14353 | | * (if we are disabled, we only need to parse policy, not default). |
14354 | | * 3. If we have the required policy, and we aren't adjusting the defaults |
14355 | | * return FALSE. (only parsing the policy, not default). |
14356 | | * 4. We have the required policy and we are adjusting the defaults. |
14357 | | * If we are setting default = FALSE, set isDisabled to true so that |
14358 | | * we don't try to re-enable the cipher suite based on a different |
14359 | | * algorithm. |
14360 | | */ |
14361 | | PRBool |
14362 | | ssl_HandlePolicy(int cipher_suite, SECOidTag policyOid, |
14363 | | PRUint32 requiredPolicy, PRBool *isDisabled) |
14364 | 0 | { |
14365 | 0 | PRUint32 policy; |
14366 | 0 | SECStatus rv; |
14367 | | |
14368 | | /* first fetch the policy for this algorithm */ |
14369 | 0 | rv = NSS_GetAlgorithmPolicy(policyOid, &policy); |
14370 | 0 | if (rv != SECSuccess) { |
14371 | 0 | return PR_FALSE; /* no policy value, continue to the next algorithm */ |
14372 | 0 | } |
14373 | | /* first, are we allowed by policy, if not turn off allow and disable */ |
14374 | 0 | if (!(policy & requiredPolicy)) { |
14375 | 0 | ssl_CipherPrefSetDefault(cipher_suite, PR_FALSE); |
14376 | 0 | ssl_CipherPolicySet(cipher_suite, SSL_NOT_ALLOWED); |
14377 | 0 | return PR_TRUE; |
14378 | 0 | } |
14379 | | /* If we are already disabled, or the policy isn't setting a default |
14380 | | * we are done processing this algorithm */ |
14381 | 0 | if (*isDisabled || (policy & NSS_USE_DEFAULT_NOT_VALID)) { |
14382 | 0 | return PR_FALSE; |
14383 | 0 | } |
14384 | | /* set the default value for the cipher suite. If we disable the cipher |
14385 | | * suite, remember that so we don't process the next default. This has |
14386 | | * the effect of disabling the whole cipher suite if any of the |
14387 | | * algorithms it uses are disabled by default. We still have to |
14388 | | * process the upper level because the cipher suite is still allowed |
14389 | | * by policy, and we may still have to disallow it based on other |
14390 | | * algorithms in the cipher suite. */ |
14391 | 0 | if (policy & NSS_USE_DEFAULT_SSL_ENABLE) { |
14392 | 0 | ssl_CipherPrefSetDefault(cipher_suite, PR_TRUE); |
14393 | 0 | } else { |
14394 | 0 | *isDisabled = PR_TRUE; |
14395 | 0 | ssl_CipherPrefSetDefault(cipher_suite, PR_FALSE); |
14396 | 0 | } |
14397 | 0 | return PR_FALSE; |
14398 | 0 | } |
14399 | | |
14400 | 0 | #define MAP_NULL(x) (((x) != 0) ? (x) : SEC_OID_NULL_CIPHER) |
14401 | | |
14402 | | SECStatus |
14403 | | ssl3_ApplyNSSPolicy(void) |
14404 | 1 | { |
14405 | 1 | unsigned i; |
14406 | 1 | SECStatus rv; |
14407 | 1 | PRUint32 policy = 0; |
14408 | | |
14409 | 1 | rv = NSS_GetAlgorithmPolicy(SEC_OID_APPLY_SSL_POLICY, &policy); |
14410 | 1 | if (rv != SECSuccess || !(policy & NSS_USE_POLICY_IN_SSL)) { |
14411 | 1 | return SECSuccess; /* do nothing */ |
14412 | 1 | } |
14413 | | |
14414 | | /* disable every ciphersuite */ |
14415 | 0 | for (i = 1; i < PR_ARRAY_SIZE(cipher_suite_defs); ++i) { |
14416 | 0 | const ssl3CipherSuiteDef *suite = &cipher_suite_defs[i]; |
14417 | 0 | SECOidTag policyOid; |
14418 | 0 | PRBool isDisabled = PR_FALSE; |
14419 | | |
14420 | | /* if we haven't explicitly disabled it below enable by policy */ |
14421 | 0 | ssl_CipherPolicySet(suite->cipher_suite, SSL_ALLOWED); |
14422 | | |
14423 | | /* now check the various key exchange, ciphers and macs and |
14424 | | * if we ever disallow by policy, we are done, go to the next cipher |
14425 | | */ |
14426 | 0 | policyOid = MAP_NULL(kea_defs[suite->key_exchange_alg].oid); |
14427 | 0 | if (ssl_HandlePolicy(suite->cipher_suite, policyOid, |
14428 | 0 | NSS_USE_ALG_IN_SSL_KX, &isDisabled)) { |
14429 | 0 | continue; |
14430 | 0 | } |
14431 | | |
14432 | 0 | policyOid = MAP_NULL(ssl_GetBulkCipherDef(suite)->oid); |
14433 | 0 | if (ssl_HandlePolicy(suite->cipher_suite, policyOid, |
14434 | 0 | NSS_USE_ALG_IN_SSL, &isDisabled)) { |
14435 | 0 | continue; |
14436 | 0 | } |
14437 | | |
14438 | 0 | if (ssl_GetBulkCipherDef(suite)->type != type_aead) { |
14439 | 0 | policyOid = MAP_NULL(ssl_GetMacDefByAlg(suite->mac_alg)->oid); |
14440 | 0 | if (ssl_HandlePolicy(suite->cipher_suite, policyOid, |
14441 | 0 | NSS_USE_ALG_IN_SSL, &isDisabled)) { |
14442 | 0 | continue; |
14443 | 0 | } |
14444 | 0 | } |
14445 | 0 | } |
14446 | |
|
14447 | 0 | rv = ssl3_ConstrainRangeByPolicy(); |
14448 | |
|
14449 | 0 | return rv; |
14450 | 1 | } |
14451 | | |
14452 | | /* End of ssl3con.c */ |