/src/nss/lib/ssl/sslsecur.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 | | * Various SSL functions. |
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 | | #include "cert.h" |
9 | | #include "secitem.h" |
10 | | #include "keyhi.h" |
11 | | #include "ssl.h" |
12 | | #include "sslimpl.h" |
13 | | #include "sslproto.h" |
14 | | #include "secoid.h" /* for SECOID_GetALgorithmTag */ |
15 | | #include "pk11func.h" /* for PK11_GenerateRandom */ |
16 | | #include "nss.h" /* for NSS_RegisterShutdown */ |
17 | | #include "prinit.h" /* for PR_CallOnceWithArg */ |
18 | | #include "tls13ech.h" |
19 | | #include "tls13psk.h" |
20 | | |
21 | | /* Step through the handshake functions. |
22 | | * |
23 | | * Called from: SSL_ForceHandshake (below), |
24 | | * ssl_SecureRecv (below) and |
25 | | * ssl_SecureSend (below) |
26 | | * from: WaitForResponse in sslsocks.c |
27 | | * ssl_SocksRecv in sslsocks.c |
28 | | * ssl_SocksSend in sslsocks.c |
29 | | * |
30 | | * Caller must hold the (write) handshakeLock. |
31 | | */ |
32 | | SECStatus |
33 | | ssl_Do1stHandshake(sslSocket *ss) |
34 | 9.71k | { |
35 | 9.71k | SECStatus rv = SECSuccess; |
36 | | |
37 | 29.1k | while (ss->handshake && rv == SECSuccess) { |
38 | 19.4k | PORT_Assert(ss->opt.noLocks || ssl_Have1stHandshakeLock(ss)); |
39 | 19.4k | PORT_Assert(ss->opt.noLocks || !ssl_HaveRecvBufLock(ss)); |
40 | 19.4k | PORT_Assert(ss->opt.noLocks || !ssl_HaveXmitBufLock(ss)); |
41 | 19.4k | PORT_Assert(ss->opt.noLocks || !ssl_HaveSSL3HandshakeLock(ss)); |
42 | | |
43 | 19.4k | rv = (*ss->handshake)(ss); |
44 | 19.4k | }; |
45 | | |
46 | 9.71k | PORT_Assert(ss->opt.noLocks || !ssl_HaveRecvBufLock(ss)); |
47 | 9.71k | PORT_Assert(ss->opt.noLocks || !ssl_HaveXmitBufLock(ss)); |
48 | 9.71k | PORT_Assert(ss->opt.noLocks || !ssl_HaveSSL3HandshakeLock(ss)); |
49 | | |
50 | 9.71k | return rv; |
51 | 9.71k | } |
52 | | |
53 | | SECStatus |
54 | | ssl_FinishHandshake(sslSocket *ss) |
55 | 0 | { |
56 | 0 | PORT_Assert(ss->opt.noLocks || ssl_Have1stHandshakeLock(ss)); |
57 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
58 | 0 | PORT_Assert(ss->ssl3.hs.echAccepted || |
59 | 0 | (ss->opt.enableTls13BackendEch && |
60 | 0 | ss->xtnData.ech && |
61 | 0 | ss->xtnData.ech->receivedInnerXtn) == |
62 | 0 | ssl3_ExtensionNegotiated(ss, ssl_tls13_encrypted_client_hello_xtn)); |
63 | | |
64 | | /* If ECH was OFFERED to (echHpkeCtx is set on the client) DISABLED by the |
65 | | * server through negotiation of a TLS version < 1.3, an 'ech_required' |
66 | | * alert MUST be sent to inform the server about the intention / possible |
67 | | * misconfiguration. */ |
68 | 0 | if (!ss->sec.isServer && ss->ssl3.hs.echHpkeCtx && !ss->ssl3.hs.echAccepted) { |
69 | 0 | SSL3_SendAlert(ss, alert_fatal, ech_required); |
70 | | /* "If [one, none] of the retry_configs contains a supported version, |
71 | | * the client can regard ECH as securely [replaced, disabled] by the |
72 | | * server." */ |
73 | 0 | if (ss->xtnData.ech && ss->xtnData.ech->retryConfigs.len) { |
74 | 0 | PORT_SetError(SSL_ERROR_ECH_RETRY_WITH_ECH); |
75 | 0 | ss->xtnData.ech->retryConfigsValid = PR_TRUE; |
76 | 0 | } else { |
77 | 0 | PORT_SetError(SSL_ERROR_ECH_RETRY_WITHOUT_ECH); |
78 | 0 | } |
79 | 0 | return SECFailure; |
80 | 0 | } |
81 | | |
82 | 0 | SSL_TRC(3, ("%d: SSL[%d]: handshake is completed", SSL_GETPID(), ss->fd)); |
83 | |
|
84 | 0 | ss->firstHsDone = PR_TRUE; |
85 | 0 | ss->enoughFirstHsDone = PR_TRUE; |
86 | 0 | ss->gs.writeOffset = 0; |
87 | 0 | ss->gs.readOffset = 0; |
88 | |
|
89 | 0 | if (ss->handshakeCallback) { |
90 | 0 | PORT_Assert((ss->ssl3.hs.preliminaryInfo & ssl_preinfo_all) == |
91 | 0 | ssl_preinfo_all); |
92 | 0 | (ss->handshakeCallback)(ss->fd, ss->handshakeCallbackData); |
93 | 0 | } |
94 | |
|
95 | 0 | ssl_FreeEphemeralKeyPairs(ss); |
96 | |
|
97 | 0 | return SECSuccess; |
98 | 0 | } |
99 | | |
100 | | /* |
101 | | * Handshake function that blocks. Used to force a |
102 | | * retry on a connection on the next read/write. |
103 | | */ |
104 | | static SECStatus |
105 | | ssl3_AlwaysBlock(sslSocket *ss) |
106 | 0 | { |
107 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
108 | 0 | return SECFailure; |
109 | 0 | } |
110 | | |
111 | | /* |
112 | | * set the initial handshake state machine to block |
113 | | */ |
114 | | void |
115 | | ssl3_SetAlwaysBlock(sslSocket *ss) |
116 | 0 | { |
117 | 0 | if (!ss->firstHsDone) { |
118 | 0 | ss->handshake = ssl3_AlwaysBlock; |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | static SECStatus |
123 | | ssl_SetTimeout(PRFileDesc *fd, PRIntervalTime timeout) |
124 | 0 | { |
125 | 0 | sslSocket *ss; |
126 | |
|
127 | 0 | ss = ssl_FindSocket(fd); |
128 | 0 | if (!ss) { |
129 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SetTimeout", SSL_GETPID(), fd)); |
130 | 0 | return SECFailure; |
131 | 0 | } |
132 | 0 | SSL_LOCK_READER(ss); |
133 | 0 | ss->rTimeout = timeout; |
134 | 0 | if (ss->opt.fdx) { |
135 | 0 | SSL_LOCK_WRITER(ss); |
136 | 0 | } |
137 | 0 | ss->wTimeout = timeout; |
138 | 0 | if (ss->opt.fdx) { |
139 | 0 | SSL_UNLOCK_WRITER(ss); |
140 | 0 | } |
141 | 0 | SSL_UNLOCK_READER(ss); |
142 | 0 | return SECSuccess; |
143 | 0 | } |
144 | | |
145 | | /* Acquires and releases HandshakeLock. |
146 | | */ |
147 | | SECStatus |
148 | | SSL_ResetHandshake(PRFileDesc *s, PRBool asServer) |
149 | 9.71k | { |
150 | 9.71k | sslSocket *ss; |
151 | 9.71k | SECStatus status; |
152 | 9.71k | PRNetAddr addr; |
153 | | |
154 | 9.71k | ss = ssl_FindSocket(s); |
155 | 9.71k | if (!ss) { |
156 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in ResetHandshake", SSL_GETPID(), s)); |
157 | 0 | return SECFailure; |
158 | 0 | } |
159 | | |
160 | | /* Don't waste my time */ |
161 | 9.71k | if (!ss->opt.useSecurity) |
162 | 0 | return SECSuccess; |
163 | | |
164 | 9.71k | SSL_LOCK_READER(ss); |
165 | 9.71k | SSL_LOCK_WRITER(ss); |
166 | | |
167 | | /* Reset handshake state */ |
168 | 9.71k | ssl_Get1stHandshakeLock(ss); |
169 | | |
170 | 9.71k | ss->firstHsDone = PR_FALSE; |
171 | 9.71k | ss->enoughFirstHsDone = PR_FALSE; |
172 | 9.71k | if (asServer) { |
173 | 0 | ss->handshake = ssl_BeginServerHandshake; |
174 | 0 | ss->handshaking = sslHandshakingAsServer; |
175 | 9.71k | } else { |
176 | 9.71k | ss->handshake = ssl_BeginClientHandshake; |
177 | 9.71k | ss->handshaking = sslHandshakingAsClient; |
178 | 9.71k | } |
179 | | |
180 | 9.71k | ssl_GetRecvBufLock(ss); |
181 | 9.71k | status = ssl3_InitGather(&ss->gs); |
182 | 9.71k | ssl_ReleaseRecvBufLock(ss); |
183 | 9.71k | if (status != SECSuccess) |
184 | 0 | goto loser; |
185 | | |
186 | 9.71k | ssl_GetSSL3HandshakeLock(ss); |
187 | 9.71k | ss->ssl3.hs.canFalseStart = PR_FALSE; |
188 | 9.71k | ss->ssl3.hs.restartTarget = NULL; |
189 | | |
190 | | /* |
191 | | ** Blow away old security state and get a fresh setup. |
192 | | */ |
193 | 9.71k | ssl_GetXmitBufLock(ss); |
194 | 9.71k | ssl_ResetSecurityInfo(&ss->sec, PR_TRUE); |
195 | 9.71k | status = ssl_CreateSecurityInfo(ss); |
196 | 9.71k | ssl_ReleaseXmitBufLock(ss); |
197 | | |
198 | 9.71k | ssl_ReleaseSSL3HandshakeLock(ss); |
199 | 9.71k | ssl_Release1stHandshakeLock(ss); |
200 | | |
201 | 9.71k | ssl3_DestroyRemoteExtensions(&ss->ssl3.hs.remoteExtensions); |
202 | 9.71k | ssl3_DestroyRemoteExtensions(&ss->ssl3.hs.echOuterExtensions); |
203 | 9.71k | ssl3_ResetExtensionData(&ss->xtnData, ss); |
204 | 9.71k | tls13_ResetHandshakePsks(ss, &ss->ssl3.hs.psks); |
205 | | |
206 | 9.71k | if (ss->ssl3.hs.echHpkeCtx) { |
207 | 0 | PK11_HPKE_DestroyContext(ss->ssl3.hs.echHpkeCtx, PR_TRUE); |
208 | 0 | ss->ssl3.hs.echHpkeCtx = NULL; |
209 | 0 | PORT_Assert(ss->ssl3.hs.echPublicName); |
210 | 0 | PORT_Free((void *)ss->ssl3.hs.echPublicName); /* CONST */ |
211 | 0 | ss->ssl3.hs.echPublicName = NULL; |
212 | 0 | } |
213 | | /* Make sure greaseEchBuf is freed in ECH setups without echHpkeCtx. */ |
214 | 9.71k | if (ss->ssl3.hs.echHpkeCtx || |
215 | 9.71k | ss->opt.enableTls13BackendEch || |
216 | 9.71k | ss->opt.enableTls13GreaseEch) { |
217 | 0 | sslBuffer_Clear(&ss->ssl3.hs.greaseEchBuf); |
218 | 0 | } |
219 | | |
220 | 9.71k | tls13_ClientGreaseDestroy(ss); |
221 | | |
222 | 9.71k | tls_ClientHelloExtensionPermutationDestroy(ss); |
223 | | |
224 | 9.71k | if (!ss->TCPconnected) |
225 | 0 | ss->TCPconnected = (PR_SUCCESS == ssl_DefGetpeername(ss, &addr)); |
226 | | |
227 | 9.71k | loser: |
228 | 9.71k | SSL_UNLOCK_WRITER(ss); |
229 | 9.71k | SSL_UNLOCK_READER(ss); |
230 | | |
231 | 9.71k | return status; |
232 | 9.71k | } |
233 | | |
234 | | /* For SSLv2, does nothing but return an error. |
235 | | ** For SSLv3, flushes SID cache entry (if requested), |
236 | | ** and then starts new client hello or hello request. |
237 | | ** Acquires and releases HandshakeLock. |
238 | | */ |
239 | | SECStatus |
240 | | SSL_ReHandshake(PRFileDesc *fd, PRBool flushCache) |
241 | 0 | { |
242 | 0 | sslSocket *ss; |
243 | 0 | SECStatus rv; |
244 | |
|
245 | 0 | ss = ssl_FindSocket(fd); |
246 | 0 | if (!ss) { |
247 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in RedoHandshake", SSL_GETPID(), fd)); |
248 | 0 | return SECFailure; |
249 | 0 | } |
250 | | |
251 | 0 | if (!ss->opt.useSecurity) |
252 | 0 | return SECSuccess; |
253 | | |
254 | 0 | ssl_Get1stHandshakeLock(ss); |
255 | |
|
256 | 0 | ssl_GetSSL3HandshakeLock(ss); |
257 | 0 | rv = ssl3_RedoHandshake(ss, flushCache); /* force full handshake. */ |
258 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
259 | |
|
260 | 0 | ssl_Release1stHandshakeLock(ss); |
261 | |
|
262 | 0 | return rv; |
263 | 0 | } |
264 | | |
265 | | /* |
266 | | ** Same as above, but with an I/O timeout. |
267 | | */ |
268 | | SSL_IMPORT SECStatus |
269 | | SSL_ReHandshakeWithTimeout(PRFileDesc *fd, |
270 | | PRBool flushCache, |
271 | | PRIntervalTime timeout) |
272 | 0 | { |
273 | 0 | if (SECSuccess != ssl_SetTimeout(fd, timeout)) { |
274 | 0 | return SECFailure; |
275 | 0 | } |
276 | 0 | return SSL_ReHandshake(fd, flushCache); |
277 | 0 | } |
278 | | |
279 | | SECStatus |
280 | | SSL_RedoHandshake(PRFileDesc *fd) |
281 | 0 | { |
282 | 0 | return SSL_ReHandshake(fd, PR_TRUE); |
283 | 0 | } |
284 | | |
285 | | /* Register an application callback to be called when SSL handshake completes. |
286 | | ** Acquires and releases HandshakeLock. |
287 | | */ |
288 | | SECStatus |
289 | | SSL_HandshakeCallback(PRFileDesc *fd, SSLHandshakeCallback cb, |
290 | | void *client_data) |
291 | 0 | { |
292 | 0 | sslSocket *ss; |
293 | |
|
294 | 0 | ss = ssl_FindSocket(fd); |
295 | 0 | if (!ss) { |
296 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in HandshakeCallback", |
297 | 0 | SSL_GETPID(), fd)); |
298 | 0 | return SECFailure; |
299 | 0 | } |
300 | | |
301 | 0 | if (!ss->opt.useSecurity) { |
302 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
303 | 0 | return SECFailure; |
304 | 0 | } |
305 | | |
306 | 0 | ssl_Get1stHandshakeLock(ss); |
307 | 0 | ssl_GetSSL3HandshakeLock(ss); |
308 | |
|
309 | 0 | ss->handshakeCallback = cb; |
310 | 0 | ss->handshakeCallbackData = client_data; |
311 | |
|
312 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
313 | 0 | ssl_Release1stHandshakeLock(ss); |
314 | |
|
315 | 0 | return SECSuccess; |
316 | 0 | } |
317 | | |
318 | | /* Register an application callback to be called when false start may happen. |
319 | | ** Acquires and releases HandshakeLock. |
320 | | */ |
321 | | SECStatus |
322 | | SSL_SetCanFalseStartCallback(PRFileDesc *fd, SSLCanFalseStartCallback cb, |
323 | | void *arg) |
324 | 9.71k | { |
325 | 9.71k | sslSocket *ss; |
326 | | |
327 | 9.71k | ss = ssl_FindSocket(fd); |
328 | 9.71k | if (!ss) { |
329 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetCanFalseStartCallback", |
330 | 0 | SSL_GETPID(), fd)); |
331 | 0 | return SECFailure; |
332 | 0 | } |
333 | | |
334 | 9.71k | if (!ss->opt.useSecurity) { |
335 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
336 | 0 | return SECFailure; |
337 | 0 | } |
338 | | |
339 | 9.71k | ssl_Get1stHandshakeLock(ss); |
340 | 9.71k | ssl_GetSSL3HandshakeLock(ss); |
341 | | |
342 | 9.71k | ss->canFalseStartCallback = cb; |
343 | 9.71k | ss->canFalseStartCallbackData = arg; |
344 | | |
345 | 9.71k | ssl_ReleaseSSL3HandshakeLock(ss); |
346 | 9.71k | ssl_Release1stHandshakeLock(ss); |
347 | | |
348 | 9.71k | return SECSuccess; |
349 | 9.71k | } |
350 | | |
351 | | SECStatus |
352 | | SSL_RecommendedCanFalseStart(PRFileDesc *fd, PRBool *canFalseStart) |
353 | 0 | { |
354 | 0 | sslSocket *ss; |
355 | |
|
356 | 0 | *canFalseStart = PR_FALSE; |
357 | 0 | ss = ssl_FindSocket(fd); |
358 | 0 | if (!ss) { |
359 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_RecommendedCanFalseStart", |
360 | 0 | SSL_GETPID(), fd)); |
361 | 0 | return SECFailure; |
362 | 0 | } |
363 | | |
364 | | /* Require a forward-secret key exchange. */ |
365 | 0 | *canFalseStart = ss->ssl3.hs.kea_def->kea == kea_dhe_dss || |
366 | 0 | ss->ssl3.hs.kea_def->kea == kea_dhe_rsa || |
367 | 0 | ss->ssl3.hs.kea_def->kea == kea_ecdhe_ecdsa || |
368 | 0 | ss->ssl3.hs.kea_def->kea == kea_ecdhe_rsa; |
369 | |
|
370 | 0 | return SECSuccess; |
371 | 0 | } |
372 | | |
373 | | /* Try to make progress on an SSL handshake by attempting to read the |
374 | | ** next handshake from the peer, and sending any responses. |
375 | | ** For non-blocking sockets, returns PR_ERROR_WOULD_BLOCK if it cannot |
376 | | ** read the next handshake from the underlying socket. |
377 | | ** Returns when handshake is complete, or application data has |
378 | | ** arrived that must be taken by application before handshake can continue, |
379 | | ** or a fatal error occurs. |
380 | | ** Application should use handshake completion callback to tell which. |
381 | | */ |
382 | | SECStatus |
383 | | SSL_ForceHandshake(PRFileDesc *fd) |
384 | 9.71k | { |
385 | 9.71k | sslSocket *ss; |
386 | 9.71k | SECStatus rv = SECFailure; |
387 | | |
388 | 9.71k | ss = ssl_FindSocket(fd); |
389 | 9.71k | if (!ss) { |
390 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in ForceHandshake", |
391 | 0 | SSL_GETPID(), fd)); |
392 | 0 | return rv; |
393 | 0 | } |
394 | | |
395 | | /* Don't waste my time */ |
396 | 9.71k | if (!ss->opt.useSecurity) |
397 | 0 | return SECSuccess; |
398 | | |
399 | 9.71k | if (!ssl_SocketIsBlocking(ss)) { |
400 | 9.71k | ssl_GetXmitBufLock(ss); |
401 | 9.71k | if (ss->pendingBuf.len != 0) { |
402 | 0 | int sent = ssl_SendSavedWriteData(ss); |
403 | 0 | if ((sent < 0) && (PORT_GetError() != PR_WOULD_BLOCK_ERROR)) { |
404 | 0 | ssl_ReleaseXmitBufLock(ss); |
405 | 0 | return SECFailure; |
406 | 0 | } |
407 | 0 | } |
408 | 9.71k | ssl_ReleaseXmitBufLock(ss); |
409 | 9.71k | } |
410 | | |
411 | 9.71k | ssl_Get1stHandshakeLock(ss); |
412 | | |
413 | 9.71k | if (ss->version >= SSL_LIBRARY_VERSION_3_0) { |
414 | 0 | int gatherResult; |
415 | |
|
416 | 0 | ssl_GetRecvBufLock(ss); |
417 | 0 | gatherResult = ssl3_GatherCompleteHandshake(ss, 0); |
418 | 0 | ssl_ReleaseRecvBufLock(ss); |
419 | 0 | if (gatherResult > 0) { |
420 | 0 | rv = SECSuccess; |
421 | 0 | } else { |
422 | 0 | if (gatherResult == 0) { |
423 | 0 | PORT_SetError(PR_END_OF_FILE_ERROR); |
424 | 0 | } |
425 | | /* We can rely on ssl3_GatherCompleteHandshake to set |
426 | | * PR_WOULD_BLOCK_ERROR as needed here. */ |
427 | 0 | rv = SECFailure; |
428 | 0 | } |
429 | 9.71k | } else { |
430 | 9.71k | PORT_Assert(!ss->firstHsDone); |
431 | 9.71k | rv = ssl_Do1stHandshake(ss); |
432 | 9.71k | } |
433 | | |
434 | 9.71k | ssl_Release1stHandshakeLock(ss); |
435 | | |
436 | 9.71k | return rv; |
437 | 9.71k | } |
438 | | |
439 | | /* |
440 | | ** Same as above, but with an I/O timeout. |
441 | | */ |
442 | | SSL_IMPORT SECStatus |
443 | | SSL_ForceHandshakeWithTimeout(PRFileDesc *fd, |
444 | | PRIntervalTime timeout) |
445 | 0 | { |
446 | 0 | if (SECSuccess != ssl_SetTimeout(fd, timeout)) { |
447 | 0 | return SECFailure; |
448 | 0 | } |
449 | 0 | return SSL_ForceHandshake(fd); |
450 | 0 | } |
451 | | |
452 | | /************************************************************************/ |
453 | | |
454 | | /* |
455 | | ** Save away write data that is trying to be written before the security |
456 | | ** handshake has been completed. When the handshake is completed, we will |
457 | | ** flush this data out. |
458 | | ** Caller must hold xmitBufLock |
459 | | */ |
460 | | SECStatus |
461 | | ssl_SaveWriteData(sslSocket *ss, const void *data, unsigned int len) |
462 | 1.72k | { |
463 | 1.72k | SECStatus rv; |
464 | | |
465 | 1.72k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
466 | 1.72k | rv = sslBuffer_Append(&ss->pendingBuf, data, len); |
467 | 1.72k | SSL_TRC(5, ("%d: SSL[%d]: saving %u bytes of data (%u total saved so far)", |
468 | 1.72k | SSL_GETPID(), ss->fd, len, ss->pendingBuf.len)); |
469 | 1.72k | return rv; |
470 | 1.72k | } |
471 | | |
472 | | /* |
473 | | ** Send saved write data. This will flush out data sent prior to a |
474 | | ** complete security handshake. Hopefully there won't be too much of it. |
475 | | ** Returns count of the bytes sent, NOT a SECStatus. |
476 | | ** Caller must hold xmitBufLock |
477 | | */ |
478 | | int |
479 | | ssl_SendSavedWriteData(sslSocket *ss) |
480 | 577 | { |
481 | 577 | int rv = 0; |
482 | | |
483 | 577 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
484 | 577 | if (ss->pendingBuf.len != 0) { |
485 | 577 | SSL_TRC(5, ("%d: SSL[%d]: sending %d bytes of saved data", |
486 | 577 | SSL_GETPID(), ss->fd, ss->pendingBuf.len)); |
487 | 577 | rv = ssl_DefSend(ss, ss->pendingBuf.buf, ss->pendingBuf.len, 0); |
488 | 577 | if (rv < 0) { |
489 | 0 | return rv; |
490 | 0 | } |
491 | 577 | if (rv > ss->pendingBuf.len) { |
492 | 0 | PORT_Assert(0); /* This shouldn't happen */ |
493 | 0 | ss->pendingBuf.len = 0; |
494 | 577 | } else { |
495 | 577 | ss->pendingBuf.len -= rv; |
496 | 577 | } |
497 | 577 | if (ss->pendingBuf.len > 0 && rv > 0) { |
498 | | /* UGH !! This shifts the whole buffer down by copying it */ |
499 | 0 | PORT_Memmove(ss->pendingBuf.buf, ss->pendingBuf.buf + rv, |
500 | 0 | ss->pendingBuf.len); |
501 | 0 | } |
502 | 577 | } |
503 | 577 | return rv; |
504 | 577 | } |
505 | | |
506 | | /************************************************************************/ |
507 | | |
508 | | /* |
509 | | ** Receive some application data on a socket. Reads SSL records from the input |
510 | | ** stream, decrypts them and then copies them to the output buffer. |
511 | | ** Called from ssl_SecureRecv() below. |
512 | | ** |
513 | | ** Caller does NOT hold 1stHandshakeLock because that handshake is over. |
514 | | ** Caller doesn't call this until initial handshake is complete. |
515 | | ** The call to ssl3_GatherAppDataRecord may encounter handshake |
516 | | ** messages from a subsequent handshake. |
517 | | ** |
518 | | ** This code is similar to, and easily confused with, |
519 | | ** ssl_GatherRecord1stHandshake() in sslcon.c |
520 | | */ |
521 | | static int |
522 | | DoRecv(sslSocket *ss, unsigned char *out, int len, int flags) |
523 | 190 | { |
524 | 190 | int rv; |
525 | 190 | int amount; |
526 | 190 | int available; |
527 | | |
528 | | /* ssl3_GatherAppDataRecord may call ssl_FinishHandshake, which needs the |
529 | | * 1stHandshakeLock. */ |
530 | 190 | ssl_Get1stHandshakeLock(ss); |
531 | 190 | ssl_GetRecvBufLock(ss); |
532 | | |
533 | 190 | available = ss->gs.writeOffset - ss->gs.readOffset; |
534 | 190 | if (available == 0) { |
535 | | /* Wait for application data to arrive. */ |
536 | 190 | rv = ssl3_GatherAppDataRecord(ss, 0); |
537 | 190 | if (rv <= 0) { |
538 | 190 | if (rv == 0) { |
539 | | /* EOF */ |
540 | 70 | SSL_TRC(10, ("%d: SSL[%d]: ssl_recv EOF", |
541 | 70 | SSL_GETPID(), ss->fd)); |
542 | 70 | goto done; |
543 | 70 | } |
544 | 120 | if (PR_GetError() != PR_WOULD_BLOCK_ERROR) { |
545 | | /* Some random error */ |
546 | 120 | goto done; |
547 | 120 | } |
548 | | |
549 | | /* |
550 | | ** Gather record is blocked waiting for more record data to |
551 | | ** arrive. Try to process what we have already received |
552 | | */ |
553 | 120 | } else { |
554 | | /* Gather record has finished getting a complete record */ |
555 | 0 | } |
556 | | |
557 | | /* See if any clear data is now available */ |
558 | 0 | available = ss->gs.writeOffset - ss->gs.readOffset; |
559 | 0 | if (available == 0) { |
560 | | /* |
561 | | ** No partial data is available. Force error code to |
562 | | ** EWOULDBLOCK so that caller will try again later. Note |
563 | | ** that the error code is probably EWOULDBLOCK already, |
564 | | ** but if it isn't (for example, if we received a zero |
565 | | ** length record) then this will force it to be correct. |
566 | | */ |
567 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
568 | 0 | rv = SECFailure; |
569 | 0 | goto done; |
570 | 0 | } |
571 | 0 | SSL_TRC(30, ("%d: SSL[%d]: partial data ready, available=%d", |
572 | 0 | SSL_GETPID(), ss->fd, available)); |
573 | 0 | } |
574 | | |
575 | 0 | if (IS_DTLS(ss) && (len < available)) { |
576 | | /* DTLS does not allow you to do partial reads */ |
577 | 0 | SSL_TRC(30, ("%d: SSL[%d]: DTLS short read. len=%d available=%d", |
578 | 0 | SSL_GETPID(), ss->fd, len, available)); |
579 | 0 | ss->gs.readOffset += available; |
580 | 0 | PORT_SetError(SSL_ERROR_RX_SHORT_DTLS_READ); |
581 | 0 | rv = SECFailure; |
582 | 0 | goto done; |
583 | 0 | } |
584 | | |
585 | | /* Dole out clear data to reader */ |
586 | 0 | amount = PR_MIN(len, available); |
587 | 0 | PORT_Memcpy(out, ss->gs.buf.buf + ss->gs.readOffset, amount); |
588 | 0 | if (!(flags & PR_MSG_PEEK)) { |
589 | 0 | ss->gs.readOffset += amount; |
590 | 0 | } |
591 | 0 | PORT_Assert(ss->gs.readOffset <= ss->gs.writeOffset); |
592 | 0 | rv = amount; |
593 | |
|
594 | 0 | #ifdef DEBUG |
595 | | /* In Debug builds free and zero gather plaintext buffer after its content |
596 | | * has been used/copied for advanced ASAN coverage/utilization. |
597 | | * This frees the buffer after reception of application data, |
598 | | * non-application data is freed at the end of |
599 | | * ssl3con.c/ssl3_HandleRecord(). */ |
600 | 0 | if (ss->gs.writeOffset == ss->gs.readOffset) { |
601 | 0 | sslBuffer_Clear(&ss->gs.buf); |
602 | 0 | } |
603 | 0 | #endif |
604 | |
|
605 | 0 | SSL_TRC(30, ("%d: SSL[%d]: amount=%d available=%d", |
606 | 0 | SSL_GETPID(), ss->fd, amount, available)); |
607 | 0 | PRINT_BUF(4, (ss, "DoRecv receiving plaintext:", out, amount)); |
608 | |
|
609 | 190 | done: |
610 | 190 | ssl_ReleaseRecvBufLock(ss); |
611 | 190 | ssl_Release1stHandshakeLock(ss); |
612 | 190 | return rv; |
613 | 0 | } |
614 | | |
615 | | /************************************************************************/ |
616 | | |
617 | | SECStatus |
618 | | ssl_CreateSecurityInfo(sslSocket *ss) |
619 | 19.4k | { |
620 | 19.4k | SECStatus status; |
621 | | |
622 | 19.4k | ssl_GetXmitBufLock(ss); |
623 | 19.4k | status = sslBuffer_Grow(&ss->sec.writeBuf, 4096); |
624 | 19.4k | ssl_ReleaseXmitBufLock(ss); |
625 | | |
626 | 19.4k | return status; |
627 | 19.4k | } |
628 | | |
629 | | SECStatus |
630 | | ssl_CopySecurityInfo(sslSocket *ss, sslSocket *os) |
631 | 0 | { |
632 | 0 | ss->sec.isServer = os->sec.isServer; |
633 | |
|
634 | 0 | ss->sec.peerCert = CERT_DupCertificate(os->sec.peerCert); |
635 | 0 | if (os->sec.peerCert && !ss->sec.peerCert) |
636 | 0 | goto loser; |
637 | | |
638 | 0 | return SECSuccess; |
639 | | |
640 | 0 | loser: |
641 | 0 | return SECFailure; |
642 | 0 | } |
643 | | |
644 | | /* Reset sec back to its initial state. |
645 | | ** Caller holds any relevant locks. |
646 | | */ |
647 | | void |
648 | | ssl_ResetSecurityInfo(sslSecurityInfo *sec, PRBool doMemset) |
649 | 19.4k | { |
650 | 19.4k | if (sec->localCert) { |
651 | 0 | CERT_DestroyCertificate(sec->localCert); |
652 | 0 | sec->localCert = NULL; |
653 | 0 | } |
654 | 19.4k | if (sec->peerCert) { |
655 | 5.59k | CERT_DestroyCertificate(sec->peerCert); |
656 | 5.59k | sec->peerCert = NULL; |
657 | 5.59k | } |
658 | 19.4k | if (sec->peerKey) { |
659 | 0 | SECKEY_DestroyPublicKey(sec->peerKey); |
660 | 0 | sec->peerKey = NULL; |
661 | 0 | } |
662 | | |
663 | | /* cleanup the ci */ |
664 | 19.4k | if (sec->ci.sid != NULL) { |
665 | 9.71k | ssl_FreeSID(sec->ci.sid); |
666 | 9.71k | } |
667 | 19.4k | PORT_ZFree(sec->ci.sendBuf.buf, sec->ci.sendBuf.space); |
668 | 19.4k | if (doMemset) { |
669 | 9.71k | memset(&sec->ci, 0, sizeof sec->ci); |
670 | 9.71k | } |
671 | 19.4k | } |
672 | | |
673 | | /* |
674 | | ** Called from SSL_ResetHandshake (above), and |
675 | | ** from ssl_FreeSocket in sslsock.c |
676 | | ** Caller should hold relevant locks (e.g. XmitBufLock) |
677 | | */ |
678 | | void |
679 | | ssl_DestroySecurityInfo(sslSecurityInfo *sec) |
680 | 9.71k | { |
681 | 9.71k | ssl_ResetSecurityInfo(sec, PR_FALSE); |
682 | | |
683 | 9.71k | PORT_ZFree(sec->writeBuf.buf, sec->writeBuf.space); |
684 | 9.71k | sec->writeBuf.buf = 0; |
685 | | |
686 | 9.71k | memset(sec, 0, sizeof *sec); |
687 | 9.71k | } |
688 | | |
689 | | /************************************************************************/ |
690 | | |
691 | | int |
692 | | ssl_SecureConnect(sslSocket *ss, const PRNetAddr *sa) |
693 | 0 | { |
694 | 0 | PRFileDesc *osfd = ss->fd->lower; |
695 | 0 | int rv; |
696 | |
|
697 | 0 | if (ss->opt.handshakeAsServer) { |
698 | 0 | ss->handshake = ssl_BeginServerHandshake; |
699 | 0 | ss->handshaking = sslHandshakingAsServer; |
700 | 0 | } else { |
701 | 0 | ss->handshake = ssl_BeginClientHandshake; |
702 | 0 | ss->handshaking = sslHandshakingAsClient; |
703 | 0 | } |
704 | | |
705 | | /* connect to server */ |
706 | 0 | rv = osfd->methods->connect(osfd, sa, ss->cTimeout); |
707 | 0 | if (rv == PR_SUCCESS) { |
708 | 0 | ss->TCPconnected = 1; |
709 | 0 | } else { |
710 | 0 | int err = PR_GetError(); |
711 | 0 | SSL_DBG(("%d: SSL[%d]: connect failed, errno=%d", |
712 | 0 | SSL_GETPID(), ss->fd, err)); |
713 | 0 | if (err == PR_IS_CONNECTED_ERROR) { |
714 | 0 | ss->TCPconnected = 1; |
715 | 0 | } |
716 | 0 | } |
717 | |
|
718 | 0 | SSL_TRC(5, ("%d: SSL[%d]: secure connect completed, rv == %d", |
719 | 0 | SSL_GETPID(), ss->fd, rv)); |
720 | 0 | return rv; |
721 | 0 | } |
722 | | |
723 | | /* |
724 | | * Also, in the unlikely event that the TCP pipe is full and the peer stops |
725 | | * reading, the SSL3_SendAlert call in ssl_SecureClose and ssl_SecureShutdown |
726 | | * may block indefinitely in blocking mode, and may fail (without retrying) |
727 | | * in non-blocking mode. |
728 | | */ |
729 | | |
730 | | int |
731 | | ssl_SecureClose(sslSocket *ss) |
732 | 9.71k | { |
733 | 9.71k | int rv; |
734 | | |
735 | 9.71k | if (!(ss->shutdownHow & ssl_SHUTDOWN_SEND) && |
736 | 9.71k | ss->firstHsDone) { |
737 | | |
738 | | /* We don't want the final alert to be Nagle delayed. */ |
739 | 0 | if (!ss->delayDisabled) { |
740 | 0 | ssl_EnableNagleDelay(ss, PR_FALSE); |
741 | 0 | ss->delayDisabled = 1; |
742 | 0 | } |
743 | |
|
744 | 0 | (void)SSL3_SendAlert(ss, alert_warning, close_notify); |
745 | 0 | } |
746 | 9.71k | rv = ssl_DefClose(ss); |
747 | 9.71k | return rv; |
748 | 9.71k | } |
749 | | |
750 | | /* Caller handles all locking */ |
751 | | int |
752 | | ssl_SecureShutdown(sslSocket *ss, int nsprHow) |
753 | 0 | { |
754 | 0 | PRFileDesc *osfd = ss->fd->lower; |
755 | 0 | int rv; |
756 | 0 | PRIntn sslHow = nsprHow + 1; |
757 | |
|
758 | 0 | if ((unsigned)nsprHow > PR_SHUTDOWN_BOTH) { |
759 | 0 | PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
760 | 0 | return PR_FAILURE; |
761 | 0 | } |
762 | | |
763 | 0 | if ((sslHow & ssl_SHUTDOWN_SEND) != 0 && |
764 | 0 | !(ss->shutdownHow & ssl_SHUTDOWN_SEND) && |
765 | 0 | ss->firstHsDone) { |
766 | |
|
767 | 0 | (void)SSL3_SendAlert(ss, alert_warning, close_notify); |
768 | 0 | } |
769 | |
|
770 | 0 | rv = osfd->methods->shutdown(osfd, nsprHow); |
771 | |
|
772 | 0 | ss->shutdownHow |= sslHow; |
773 | |
|
774 | 0 | return rv; |
775 | 0 | } |
776 | | |
777 | | /************************************************************************/ |
778 | | |
779 | | static SECStatus |
780 | | tls13_CheckKeyUpdate(sslSocket *ss, SSLSecretDirection dir) |
781 | 0 | { |
782 | 0 | PRBool keyUpdate; |
783 | 0 | ssl3CipherSpec *spec; |
784 | 0 | sslSequenceNumber seqNum; |
785 | 0 | sslSequenceNumber margin; |
786 | 0 | tls13KeyUpdateRequest keyUpdateRequest; |
787 | 0 | SECStatus rv = SECSuccess; |
788 | |
|
789 | 0 | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
790 | 0 | return SECSuccess; |
791 | 0 | } |
792 | | |
793 | | /* If both sides update at the same number, then this will cause two updates |
794 | | * to happen at once. The problem is that the KeyUpdate itself consumes a |
795 | | * sequence number, and that will trigger the reading side to request an |
796 | | * update. |
797 | | * |
798 | | * If we have the writing side update first, the writer will be the one that |
799 | | * drives the update. An update by the writer doesn't need a response, so |
800 | | * it is more efficient overall. The margins here are pretty arbitrary, but |
801 | | * having the write margin larger reduces the number of times that a |
802 | | * KeyUpdate is sent by a reader. */ |
803 | 0 | ssl_GetSpecReadLock(ss); |
804 | 0 | if (dir == ssl_secret_read) { |
805 | 0 | spec = ss->ssl3.crSpec; |
806 | 0 | margin = spec->cipherDef->max_records / 8; |
807 | 0 | } else { |
808 | 0 | spec = ss->ssl3.cwSpec; |
809 | 0 | margin = spec->cipherDef->max_records / 4; |
810 | 0 | } |
811 | 0 | seqNum = spec->nextSeqNum; |
812 | 0 | keyUpdate = seqNum > spec->cipherDef->max_records - margin; |
813 | 0 | ssl_ReleaseSpecReadLock(ss); |
814 | 0 | if (!keyUpdate) { |
815 | 0 | return SECSuccess; |
816 | 0 | } |
817 | | |
818 | 0 | SSL_TRC(5, ("%d: SSL[%d]: automatic key update at %llx for %s cipher spec", |
819 | 0 | SSL_GETPID(), ss->fd, seqNum, |
820 | 0 | (dir == ssl_secret_read) ? "read" : "write")); |
821 | 0 | keyUpdateRequest = (dir == ssl_secret_read) ? update_requested : update_not_requested; |
822 | 0 | ssl_GetSSL3HandshakeLock(ss); |
823 | 0 | if (ss->ssl3.clientCertRequested) { |
824 | 0 | ss->ssl3.hs.keyUpdateDeferred = PR_TRUE; |
825 | 0 | ss->ssl3.hs.deferredKeyUpdateRequest = keyUpdateRequest; |
826 | 0 | } else { |
827 | 0 | rv = tls13_SendKeyUpdate(ss, keyUpdateRequest, |
828 | 0 | dir == ssl_secret_write /* buffer */); |
829 | 0 | } |
830 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
831 | 0 | return rv; |
832 | 0 | } |
833 | | |
834 | | int |
835 | | ssl_SecureRecv(sslSocket *ss, unsigned char *buf, int len, int flags) |
836 | 190 | { |
837 | 190 | int rv = 0; |
838 | | |
839 | 190 | if (ss->shutdownHow & ssl_SHUTDOWN_RCV) { |
840 | 0 | PORT_SetError(PR_SOCKET_SHUTDOWN_ERROR); |
841 | 0 | return PR_FAILURE; |
842 | 0 | } |
843 | 190 | if (flags & ~PR_MSG_PEEK) { |
844 | 0 | PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
845 | 0 | return PR_FAILURE; |
846 | 0 | } |
847 | | |
848 | 190 | if (!ssl_SocketIsBlocking(ss) && !ss->opt.fdx) { |
849 | 190 | ssl_GetXmitBufLock(ss); |
850 | 190 | if (ss->pendingBuf.len != 0) { |
851 | 0 | rv = ssl_SendSavedWriteData(ss); |
852 | 0 | if ((rv < 0) && (PORT_GetError() != PR_WOULD_BLOCK_ERROR)) { |
853 | 0 | ssl_ReleaseXmitBufLock(ss); |
854 | 0 | return SECFailure; |
855 | 0 | } |
856 | 0 | } |
857 | 190 | ssl_ReleaseXmitBufLock(ss); |
858 | 190 | } |
859 | | |
860 | 190 | rv = 0; |
861 | 190 | if (!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.bufferedEarlyData)) { |
862 | 0 | PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
863 | 0 | return tls13_Read0RttData(ss, buf, len); |
864 | 0 | } |
865 | | |
866 | | /* If any of these is non-zero, the initial handshake is not done. */ |
867 | 190 | if (!ss->firstHsDone) { |
868 | 190 | ssl_Get1stHandshakeLock(ss); |
869 | 190 | if (ss->handshake) { |
870 | 0 | rv = ssl_Do1stHandshake(ss); |
871 | 0 | } |
872 | 190 | ssl_Release1stHandshakeLock(ss); |
873 | 190 | } else { |
874 | 0 | if (tls13_CheckKeyUpdate(ss, ssl_secret_read) != SECSuccess) { |
875 | 0 | rv = PR_FAILURE; |
876 | 0 | } |
877 | 0 | } |
878 | 190 | if (rv < 0) { |
879 | 0 | if (PORT_GetError() == PR_WOULD_BLOCK_ERROR && |
880 | 0 | !PR_CLIST_IS_EMPTY(&ss->ssl3.hs.bufferedEarlyData)) { |
881 | 0 | PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
882 | 0 | return tls13_Read0RttData(ss, buf, len); |
883 | 0 | } |
884 | 0 | return rv; |
885 | 0 | } |
886 | | |
887 | 190 | if (len == 0) |
888 | 0 | return 0; |
889 | | |
890 | 190 | rv = DoRecv(ss, (unsigned char *)buf, len, flags); |
891 | 190 | SSL_TRC(2, ("%d: SSL[%d]: recving %d bytes securely (errno=%d)", |
892 | 190 | SSL_GETPID(), ss->fd, rv, PORT_GetError())); |
893 | 190 | return rv; |
894 | 190 | } |
895 | | |
896 | | int |
897 | | ssl_SecureRead(sslSocket *ss, unsigned char *buf, int len) |
898 | 190 | { |
899 | 190 | return ssl_SecureRecv(ss, buf, len, 0); |
900 | 190 | } |
901 | | |
902 | | /* Caller holds the SSL Socket's write lock. SSL_LOCK_WRITER(ss) */ |
903 | | int |
904 | | ssl_SecureSend(sslSocket *ss, const unsigned char *buf, int len, int flags) |
905 | 0 | { |
906 | 0 | int rv = 0; |
907 | 0 | PRBool zeroRtt = PR_FALSE; |
908 | |
|
909 | 0 | SSL_TRC(2, ("%d: SSL[%d]: SecureSend: sending %d bytes", |
910 | 0 | SSL_GETPID(), ss->fd, len)); |
911 | |
|
912 | 0 | if (ss->shutdownHow & ssl_SHUTDOWN_SEND) { |
913 | 0 | PORT_SetError(PR_SOCKET_SHUTDOWN_ERROR); |
914 | 0 | rv = PR_FAILURE; |
915 | 0 | goto done; |
916 | 0 | } |
917 | 0 | if (flags) { |
918 | 0 | PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
919 | 0 | rv = PR_FAILURE; |
920 | 0 | goto done; |
921 | 0 | } |
922 | | |
923 | 0 | ssl_GetXmitBufLock(ss); |
924 | 0 | if (ss->pendingBuf.len != 0) { |
925 | 0 | PORT_Assert(ss->pendingBuf.len > 0); |
926 | 0 | rv = ssl_SendSavedWriteData(ss); |
927 | 0 | if (rv >= 0 && ss->pendingBuf.len != 0) { |
928 | 0 | PORT_Assert(ss->pendingBuf.len > 0); |
929 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
930 | 0 | rv = SECFailure; |
931 | 0 | } |
932 | 0 | } |
933 | 0 | ssl_ReleaseXmitBufLock(ss); |
934 | 0 | if (rv < 0) { |
935 | 0 | goto done; |
936 | 0 | } |
937 | | |
938 | 0 | if (len > 0) |
939 | 0 | ss->writerThread = PR_GetCurrentThread(); |
940 | | |
941 | | /* Check to see if we can write even though we're not finished. |
942 | | * |
943 | | * Case 1: False start |
944 | | * Case 2: TLS 1.3 0-RTT |
945 | | */ |
946 | 0 | if (!ss->firstHsDone) { |
947 | 0 | PRBool allowEarlySend = PR_FALSE; |
948 | 0 | PRBool firstClientWrite = PR_FALSE; |
949 | |
|
950 | 0 | ssl_Get1stHandshakeLock(ss); |
951 | | /* The client can sometimes send before the handshake is fully |
952 | | * complete. In TLS 1.2: false start; in TLS 1.3: 0-RTT. */ |
953 | 0 | if (!ss->sec.isServer && |
954 | 0 | (ss->opt.enableFalseStart || ss->opt.enable0RttData)) { |
955 | 0 | ssl_GetSSL3HandshakeLock(ss); |
956 | 0 | zeroRtt = ss->ssl3.hs.zeroRttState == ssl_0rtt_sent || |
957 | 0 | ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted; |
958 | 0 | allowEarlySend = ss->ssl3.hs.canFalseStart || zeroRtt; |
959 | 0 | firstClientWrite = ss->ssl3.hs.ws == idle_handshake; |
960 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
961 | 0 | } |
962 | | /* Allow the server to send 0.5 RTT data in TLS 1.3. Requesting a |
963 | | * certificate implies that the server might condition its sending on |
964 | | * client authentication, so force servers that do that to wait. |
965 | | * |
966 | | * What might not be obvious here is that this allows 0.5 RTT when doing |
967 | | * PSK-based resumption. As a result, 0.5 RTT is always enabled when |
968 | | * early data is accepted. |
969 | | * |
970 | | * This check might be more conservative than absolutely necessary. |
971 | | * It's possible that allowing 0.5 RTT data when the server requests, |
972 | | * but does not require client authentication is safe because we can |
973 | | * expect the server to check for a client certificate properly. */ |
974 | 0 | if (ss->sec.isServer && |
975 | 0 | ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
976 | 0 | !tls13_ShouldRequestClientAuth(ss)) { |
977 | 0 | ssl_GetSSL3HandshakeLock(ss); |
978 | 0 | allowEarlySend = TLS13_IN_HS_STATE(ss, wait_finished); |
979 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
980 | 0 | } |
981 | 0 | if (!allowEarlySend && ss->handshake) { |
982 | 0 | rv = ssl_Do1stHandshake(ss); |
983 | 0 | } |
984 | 0 | if (firstClientWrite) { |
985 | | /* Wait until after sending ClientHello and double-check 0-RTT. */ |
986 | 0 | ssl_GetSSL3HandshakeLock(ss); |
987 | 0 | zeroRtt = ss->ssl3.hs.zeroRttState == ssl_0rtt_sent || |
988 | 0 | ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted; |
989 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
990 | 0 | } |
991 | 0 | ssl_Release1stHandshakeLock(ss); |
992 | 0 | } |
993 | |
|
994 | 0 | if (rv < 0) { |
995 | 0 | ss->writerThread = NULL; |
996 | 0 | goto done; |
997 | 0 | } |
998 | | |
999 | 0 | if (ss->firstHsDone) { |
1000 | 0 | if (tls13_CheckKeyUpdate(ss, ssl_secret_write) != SECSuccess) { |
1001 | 0 | rv = PR_FAILURE; |
1002 | 0 | goto done; |
1003 | 0 | } |
1004 | 0 | } |
1005 | | |
1006 | 0 | if (zeroRtt) { |
1007 | | /* There's a limit to the number of early data octets we can send. |
1008 | | * |
1009 | | * Note that taking this lock doesn't prevent the cipher specs from |
1010 | | * being changed out between here and when records are ultimately |
1011 | | * encrypted. The only effect of that is to occasionally do an |
1012 | | * unnecessary short write when data is identified as 0-RTT here but |
1013 | | * 1-RTT later. |
1014 | | */ |
1015 | 0 | ssl_GetSpecReadLock(ss); |
1016 | 0 | len = tls13_LimitEarlyData(ss, ssl_ct_application_data, len); |
1017 | 0 | ssl_ReleaseSpecReadLock(ss); |
1018 | 0 | } |
1019 | | |
1020 | | /* Check for zero length writes after we do housekeeping so we make forward |
1021 | | * progress. |
1022 | | */ |
1023 | 0 | if (len == 0) { |
1024 | 0 | rv = 0; |
1025 | 0 | goto done; |
1026 | 0 | } |
1027 | 0 | PORT_Assert(buf != NULL); |
1028 | 0 | if (!buf) { |
1029 | 0 | PORT_SetError(PR_INVALID_ARGUMENT_ERROR); |
1030 | 0 | rv = PR_FAILURE; |
1031 | 0 | goto done; |
1032 | 0 | } |
1033 | | |
1034 | 0 | ssl_GetXmitBufLock(ss); |
1035 | 0 | rv = ssl3_SendApplicationData(ss, buf, len, flags); |
1036 | 0 | ssl_ReleaseXmitBufLock(ss); |
1037 | 0 | ss->writerThread = NULL; |
1038 | 0 | done: |
1039 | 0 | if (rv < 0) { |
1040 | 0 | SSL_TRC(2, ("%d: SSL[%d]: SecureSend: returning %d count, error %d", |
1041 | 0 | SSL_GETPID(), ss->fd, rv, PORT_GetError())); |
1042 | 0 | } else { |
1043 | 0 | SSL_TRC(2, ("%d: SSL[%d]: SecureSend: returning %d count", |
1044 | 0 | SSL_GETPID(), ss->fd, rv)); |
1045 | 0 | } |
1046 | 0 | return rv; |
1047 | 0 | } |
1048 | | |
1049 | | int |
1050 | | ssl_SecureWrite(sslSocket *ss, const unsigned char *buf, int len) |
1051 | 0 | { |
1052 | 0 | return ssl_SecureSend(ss, buf, len, 0); |
1053 | 0 | } |
1054 | | |
1055 | | SECStatus |
1056 | | SSLExp_RecordLayerWriteCallback(PRFileDesc *fd, SSLRecordWriteCallback cb, |
1057 | | void *arg) |
1058 | 0 | { |
1059 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
1060 | 0 | if (!ss) { |
1061 | 0 | SSL_DBG(("%d: SSL[%d]: invalid socket for SSL_RecordLayerWriteCallback", |
1062 | 0 | SSL_GETPID(), fd)); |
1063 | 0 | return SECFailure; |
1064 | 0 | } |
1065 | 0 | if (IS_DTLS(ss)) { |
1066 | 0 | SSL_DBG(("%d: SSL[%d]: DTLS socket for SSL_RecordLayerWriteCallback", |
1067 | 0 | SSL_GETPID(), fd)); |
1068 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1069 | 0 | return SECFailure; |
1070 | 0 | } |
1071 | | |
1072 | | /* This needs both HS and Xmit locks because this value is checked under |
1073 | | * both locks. HS to disable reading from the underlying IO layer; Xmit to |
1074 | | * prevent writing. */ |
1075 | 0 | ssl_GetSSL3HandshakeLock(ss); |
1076 | 0 | ssl_GetXmitBufLock(ss); |
1077 | 0 | ss->recordWriteCallback = cb; |
1078 | 0 | ss->recordWriteCallbackArg = arg; |
1079 | 0 | ssl_ReleaseXmitBufLock(ss); |
1080 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
1081 | 0 | return SECSuccess; |
1082 | 0 | } |
1083 | | |
1084 | | SECStatus |
1085 | | SSL_AlertReceivedCallback(PRFileDesc *fd, SSLAlertCallback cb, void *arg) |
1086 | 0 | { |
1087 | 0 | sslSocket *ss; |
1088 | |
|
1089 | 0 | ss = ssl_FindSocket(fd); |
1090 | 0 | if (!ss) { |
1091 | 0 | SSL_DBG(("%d: SSL[%d]: unable to find socket in SSL_AlertReceivedCallback", |
1092 | 0 | SSL_GETPID(), fd)); |
1093 | 0 | return SECFailure; |
1094 | 0 | } |
1095 | | |
1096 | 0 | ss->alertReceivedCallback = cb; |
1097 | 0 | ss->alertReceivedCallbackArg = arg; |
1098 | |
|
1099 | 0 | return SECSuccess; |
1100 | 0 | } |
1101 | | |
1102 | | SECStatus |
1103 | | SSL_AlertSentCallback(PRFileDesc *fd, SSLAlertCallback cb, void *arg) |
1104 | 0 | { |
1105 | 0 | sslSocket *ss; |
1106 | |
|
1107 | 0 | ss = ssl_FindSocket(fd); |
1108 | 0 | if (!ss) { |
1109 | 0 | SSL_DBG(("%d: SSL[%d]: unable to find socket in SSL_AlertSentCallback", |
1110 | 0 | SSL_GETPID(), fd)); |
1111 | 0 | return SECFailure; |
1112 | 0 | } |
1113 | | |
1114 | 0 | ss->alertSentCallback = cb; |
1115 | 0 | ss->alertSentCallbackArg = arg; |
1116 | |
|
1117 | 0 | return SECSuccess; |
1118 | 0 | } |
1119 | | |
1120 | | SECStatus |
1121 | | SSL_BadCertHook(PRFileDesc *fd, SSLBadCertHandler f, void *arg) |
1122 | 0 | { |
1123 | 0 | sslSocket *ss; |
1124 | |
|
1125 | 0 | ss = ssl_FindSocket(fd); |
1126 | 0 | if (!ss) { |
1127 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSLBadCertHook", |
1128 | 0 | SSL_GETPID(), fd)); |
1129 | 0 | return SECFailure; |
1130 | 0 | } |
1131 | | |
1132 | 0 | ss->handleBadCert = f; |
1133 | 0 | ss->badCertArg = arg; |
1134 | |
|
1135 | 0 | return SECSuccess; |
1136 | 0 | } |
1137 | | |
1138 | | /* |
1139 | | * Allow the application to pass the url or hostname into the SSL library |
1140 | | * so that we can do some checking on it. It will be used for the value in |
1141 | | * SNI extension of client hello message. |
1142 | | */ |
1143 | | SECStatus |
1144 | | SSL_SetURL(PRFileDesc *fd, const char *url) |
1145 | 9.71k | { |
1146 | 9.71k | sslSocket *ss = ssl_FindSocket(fd); |
1147 | 9.71k | SECStatus rv = SECSuccess; |
1148 | | |
1149 | 9.71k | if (!ss) { |
1150 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSLSetURL", |
1151 | 0 | SSL_GETPID(), fd)); |
1152 | 0 | return SECFailure; |
1153 | 0 | } |
1154 | 9.71k | ssl_Get1stHandshakeLock(ss); |
1155 | 9.71k | ssl_GetSSL3HandshakeLock(ss); |
1156 | | |
1157 | 9.71k | if (ss->url) { |
1158 | 0 | PORT_Free((void *)ss->url); /* CONST */ |
1159 | 0 | } |
1160 | | |
1161 | 9.71k | ss->url = (const char *)PORT_Strdup(url); |
1162 | 9.71k | if (ss->url == NULL) { |
1163 | 0 | rv = SECFailure; |
1164 | 0 | } |
1165 | | |
1166 | 9.71k | ssl_ReleaseSSL3HandshakeLock(ss); |
1167 | 9.71k | ssl_Release1stHandshakeLock(ss); |
1168 | | |
1169 | 9.71k | return rv; |
1170 | 9.71k | } |
1171 | | |
1172 | | /* |
1173 | | * Allow the application to pass the set of trust anchors |
1174 | | */ |
1175 | | SECStatus |
1176 | | SSL_SetTrustAnchors(PRFileDesc *fd, CERTCertList *certList) |
1177 | 0 | { |
1178 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
1179 | 0 | CERTDistNames *names = NULL; |
1180 | |
|
1181 | 0 | if (!certList) { |
1182 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1183 | 0 | return SECFailure; |
1184 | 0 | } |
1185 | 0 | if (!ss) { |
1186 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetTrustAnchors", |
1187 | 0 | SSL_GETPID(), fd)); |
1188 | 0 | return SECFailure; |
1189 | 0 | } |
1190 | | |
1191 | 0 | names = CERT_DistNamesFromCertList(certList); |
1192 | 0 | if (names == NULL) { |
1193 | 0 | return SECFailure; |
1194 | 0 | } |
1195 | 0 | ssl_Get1stHandshakeLock(ss); |
1196 | 0 | ssl_GetSSL3HandshakeLock(ss); |
1197 | 0 | if (ss->ssl3.ca_list) { |
1198 | 0 | CERT_FreeDistNames(ss->ssl3.ca_list); |
1199 | 0 | } |
1200 | 0 | ss->ssl3.ca_list = names; |
1201 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
1202 | 0 | ssl_Release1stHandshakeLock(ss); |
1203 | |
|
1204 | 0 | return SECSuccess; |
1205 | 0 | } |
1206 | | |
1207 | | /* |
1208 | | ** Returns Negative number on error, zero or greater on success. |
1209 | | ** Returns the amount of data immediately available to be read. |
1210 | | */ |
1211 | | int |
1212 | | SSL_DataPending(PRFileDesc *fd) |
1213 | 0 | { |
1214 | 0 | sslSocket *ss; |
1215 | 0 | int rv = 0; |
1216 | |
|
1217 | 0 | ss = ssl_FindSocket(fd); |
1218 | |
|
1219 | 0 | if (ss && ss->opt.useSecurity) { |
1220 | 0 | ssl_GetRecvBufLock(ss); |
1221 | 0 | rv = ss->gs.writeOffset - ss->gs.readOffset; |
1222 | 0 | ssl_ReleaseRecvBufLock(ss); |
1223 | 0 | } |
1224 | |
|
1225 | 0 | return rv; |
1226 | 0 | } |
1227 | | |
1228 | | SECStatus |
1229 | | SSL_InvalidateSession(PRFileDesc *fd) |
1230 | 0 | { |
1231 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
1232 | 0 | SECStatus rv = SECFailure; |
1233 | |
|
1234 | 0 | if (ss) { |
1235 | 0 | ssl_Get1stHandshakeLock(ss); |
1236 | 0 | ssl_GetSSL3HandshakeLock(ss); |
1237 | |
|
1238 | 0 | if (ss->sec.ci.sid) { |
1239 | 0 | ssl_UncacheSessionID(ss); |
1240 | 0 | rv = SECSuccess; |
1241 | 0 | } |
1242 | |
|
1243 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
1244 | 0 | ssl_Release1stHandshakeLock(ss); |
1245 | 0 | } |
1246 | 0 | return rv; |
1247 | 0 | } |
1248 | | |
1249 | | SECItem * |
1250 | | SSL_GetSessionID(PRFileDesc *fd) |
1251 | 0 | { |
1252 | 0 | sslSocket *ss; |
1253 | 0 | SECItem *item = NULL; |
1254 | |
|
1255 | 0 | ss = ssl_FindSocket(fd); |
1256 | 0 | if (ss) { |
1257 | 0 | ssl_Get1stHandshakeLock(ss); |
1258 | 0 | ssl_GetSSL3HandshakeLock(ss); |
1259 | |
|
1260 | 0 | if (ss->opt.useSecurity && ss->firstHsDone && ss->sec.ci.sid) { |
1261 | 0 | item = (SECItem *)PORT_Alloc(sizeof(SECItem)); |
1262 | 0 | if (item) { |
1263 | 0 | sslSessionID *sid = ss->sec.ci.sid; |
1264 | 0 | item->len = sid->u.ssl3.sessionIDLength; |
1265 | 0 | item->data = (unsigned char *)PORT_Alloc(item->len); |
1266 | 0 | PORT_Memcpy(item->data, sid->u.ssl3.sessionID, item->len); |
1267 | 0 | } |
1268 | 0 | } |
1269 | |
|
1270 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
1271 | 0 | ssl_Release1stHandshakeLock(ss); |
1272 | 0 | } |
1273 | 0 | return item; |
1274 | 0 | } |
1275 | | |
1276 | | SECStatus |
1277 | | SSL_CertDBHandleSet(PRFileDesc *fd, CERTCertDBHandle *dbHandle) |
1278 | 0 | { |
1279 | 0 | sslSocket *ss; |
1280 | |
|
1281 | 0 | ss = ssl_FindSocket(fd); |
1282 | 0 | if (!ss) |
1283 | 0 | return SECFailure; |
1284 | 0 | if (!dbHandle) { |
1285 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1286 | 0 | return SECFailure; |
1287 | 0 | } |
1288 | 0 | ss->dbHandle = dbHandle; |
1289 | 0 | return SECSuccess; |
1290 | 0 | } |
1291 | | |
1292 | | /* DO NOT USE. This function was exported in ssl.def with the wrong signature; |
1293 | | * this implementation exists to maintain link-time compatibility. |
1294 | | */ |
1295 | | int |
1296 | | SSL_RestartHandshakeAfterCertReq(sslSocket *ss, |
1297 | | CERTCertificate *cert, |
1298 | | SECKEYPrivateKey *key, |
1299 | | CERTCertificateList *certChain) |
1300 | 0 | { |
1301 | 0 | PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); |
1302 | 0 | return -1; |
1303 | 0 | } |
1304 | | |
1305 | | /* DO NOT USE. This function was exported in ssl.def with the wrong signature; |
1306 | | * this implementation exists to maintain link-time compatibility. |
1307 | | */ |
1308 | | int |
1309 | | SSL_RestartHandshakeAfterServerCert(sslSocket *ss) |
1310 | 0 | { |
1311 | 0 | PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); |
1312 | 0 | return -1; |
1313 | 0 | } |
1314 | | |
1315 | | /* See documentation in ssl.h */ |
1316 | | SECStatus |
1317 | | SSL_AuthCertificateComplete(PRFileDesc *fd, PRErrorCode error) |
1318 | 0 | { |
1319 | 0 | SECStatus rv; |
1320 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
1321 | |
|
1322 | 0 | if (!ss) { |
1323 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_AuthCertificateComplete", |
1324 | 0 | SSL_GETPID(), fd)); |
1325 | 0 | return SECFailure; |
1326 | 0 | } |
1327 | | |
1328 | 0 | ssl_Get1stHandshakeLock(ss); |
1329 | 0 | rv = ssl3_AuthCertificateComplete(ss, error); |
1330 | 0 | ssl_Release1stHandshakeLock(ss); |
1331 | |
|
1332 | 0 | return rv; |
1333 | 0 | } |
1334 | | |
1335 | | SECStatus |
1336 | | SSL_ClientCertCallbackComplete(PRFileDesc *fd, SECStatus outcome, SECKEYPrivateKey *clientPrivateKey, |
1337 | | CERTCertificate *clientCertificate) |
1338 | 0 | { |
1339 | 0 | SECStatus rv; |
1340 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
1341 | |
|
1342 | 0 | if (!ss) { |
1343 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_ClientCertCallbackComplete", |
1344 | 0 | SSL_GETPID(), fd)); |
1345 | 0 | return SECFailure; |
1346 | 0 | } |
1347 | | |
1348 | | /* There exists a codepath which exercises each lock. |
1349 | | * Socket is blocked whilst waiting on this callback anyway. */ |
1350 | 0 | ssl_Get1stHandshakeLock(ss); |
1351 | 0 | ssl_GetRecvBufLock(ss); |
1352 | 0 | ssl_GetSSL3HandshakeLock(ss); |
1353 | |
|
1354 | 0 | if (!ss->ssl3.hs.clientCertificatePending) { |
1355 | | /* Application invoked callback at wrong time */ |
1356 | 0 | SSL_DBG(("%d: SSL[%d]: socket not waiting for SSL_ClientCertCallbackComplete", |
1357 | 0 | SSL_GETPID(), fd)); |
1358 | 0 | PORT_SetError(PR_INVALID_STATE_ERROR); |
1359 | 0 | rv = SECFailure; |
1360 | 0 | goto cleanup; |
1361 | 0 | } |
1362 | | |
1363 | 0 | rv = ssl3_ClientCertCallbackComplete(ss, outcome, clientPrivateKey, clientCertificate); |
1364 | |
|
1365 | 0 | cleanup: |
1366 | 0 | ssl_ReleaseRecvBufLock(ss); |
1367 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
1368 | 0 | ssl_Release1stHandshakeLock(ss); |
1369 | 0 | return rv; |
1370 | 0 | } |
1371 | | |
1372 | | /* For more info see ssl.h */ |
1373 | | SECStatus |
1374 | | SSL_SNISocketConfigHook(PRFileDesc *fd, SSLSNISocketConfig func, |
1375 | | void *arg) |
1376 | 0 | { |
1377 | 0 | sslSocket *ss; |
1378 | |
|
1379 | 0 | ss = ssl_FindSocket(fd); |
1380 | 0 | if (!ss) { |
1381 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SNISocketConfigHook", |
1382 | 0 | SSL_GETPID(), fd)); |
1383 | 0 | return SECFailure; |
1384 | 0 | } |
1385 | | |
1386 | 0 | ss->sniSocketConfig = func; |
1387 | 0 | ss->sniSocketConfigArg = arg; |
1388 | 0 | return SECSuccess; |
1389 | 0 | } |