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