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

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

Issue 9764001: Add DTLS support to NSS, contributed by Eric Rescorla. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Made changes suggested by rsleevi Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Gather (Read) entire SSL3 records from socket into buffer. 2 * Gather (Read) entire SSL3 records from socket into buffer.
3 * 3 *
4 * ***** BEGIN LICENSE BLOCK ***** 4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * 6 *
7 * The contents of this file are subject to the Mozilla Public License Version 7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with 8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at 9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/ 10 * http://www.mozilla.org/MPL/
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 #include "sslimpl.h" 43 #include "sslimpl.h"
44 #include "ssl3prot.h" 44 #include "ssl3prot.h"
45 45
46 /* 46 /*
47 * Attempt to read in an entire SSL3 record. 47 * Attempt to read in an entire SSL3 record.
48 * Blocks here for blocking sockets, otherwise returns -1 with 48 * Blocks here for blocking sockets, otherwise returns -1 with
49 * PR_WOULD_BLOCK_ERROR when socket would block. 49 * PR_WOULD_BLOCK_ERROR when socket would block.
50 * 50 *
51 * returns 1 if received a complete SSL3 record. 51 * returns 1 if received a complete SSL3 record.
52 * returns 0 if recv returns EOF 52 * returns 0 if recv returns EOF
53 * returns -1 if recv returns <0 53 * returns -1 if recv returns < 0
54 * (The error value may have already been set to PR_WOULD_BLOCK_ERROR) 54 * (The error value may have already been set to PR_WOULD_BLOCK_ERROR)
55 * 55 *
56 * Caller must hold the recv buf lock. 56 * Caller must hold the recv buf lock.
57 * 57 *
58 * The Gather state machine has 3 states: GS_INIT, GS_HEADER, GS_DATA. 58 * The Gather state machine has 3 states: GS_INIT, GS_HEADER, GS_DATA.
59 * GS_HEADER: waiting for the 5-byte SSL3 record header to come in. 59 * GS_HEADER: waiting for the 5-byte SSL3 record header to come in.
60 * GS_DATA: waiting for the body of the SSL3 record to come in. 60 * GS_DATA: waiting for the body of the SSL3 record to come in.
61 * 61 *
62 * This loop returns when either (a) an error or EOF occurs, 62 * This loop returns when either
63 * (a) an error or EOF occurs,
63 * (b) PR_WOULD_BLOCK_ERROR, 64 * (b) PR_WOULD_BLOCK_ERROR,
64 * (c) data (entire SSL3 record) has been received. 65 * (c) data (entire SSL3 record) has been received.
65 */ 66 */
66 static int 67 static int
67 ssl3_GatherData(sslSocket *ss, sslGather *gs, int flags) 68 ssl3_GatherData(sslSocket *ss, sslGather *gs, int flags)
68 { 69 {
69 unsigned char *bp; 70 unsigned char *bp;
70 unsigned char *lbp; 71 unsigned char *lbp;
71 int nb; 72 int nb;
72 int err; 73 int err;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 ** SSL3 record has been completely received. 161 ** SSL3 record has been completely received.
161 */ 162 */
162 gs->state = GS_INIT; 163 gs->state = GS_INIT;
163 return 1; 164 return 1;
164 } 165 }
165 } 166 }
166 167
167 return rv; 168 return rv;
168 } 169 }
169 170
171 /*
172 * Read in an entire DTLS record.
173 *
174 * Blocks here for blocking sockets, otherwise returns -1 with
175 * PR_WOULD_BLOCK_ERROR when socket would block.
176 *
177 * This is simpler than SSL because we are reading on a datagram socket
178 * and datagrams must contain >=1 complete records.
179 *
180 * returns 1 if received a complete DTLS record.
181 * returns 0 if recv returns EOF
182 * returns -1 if recv returns < 0
183 * (The error value may have already been set to PR_WOULD_BLOCK_ERROR)
184 *
185 * Caller must hold the recv buf lock.
186 *
187 * This loop returns when either (a) an error or EOF occurs,
188 * (b) PR_WOULD_BLOCK_ERROR,
189 * (c) data (entire DTLS record) has been received.
190 */
191 static int
192 dtls_GatherData(sslSocket *ss, sslGather *gs, int flags)
193 {
194 int nb;
195 int err;
196 int rv = 1;
197
198 SSL_TRC(30, ("dtls_GatherData"));
199
200 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
201
202 gs->state = GS_HEADER;
203 gs->offset = 0;
204
205 if (gs->dtlsPacketOffset == gs->dtlsPacket.len) { /* No data left */
206 gs->dtlsPacketOffset = 0;
207 gs->dtlsPacket.len = 0;
208
209 /* Resize to the maximum possible size so we can fit a full datagram */
210 /* This is the max fragment length for an encrypted fragment
211 ** plus the size of the record header.
212 ** XXX this magic constant is copied from ssl3_GatherData. For DTLS,
213 ** 5 probably should be changed to 13 (the size of the record header).
214 */
215 if (gs->dtlsPacket.space < MAX_FRAGMENT_LENGTH + 2048 + 5) {
216 err = sslBuffer_Grow(&gs->dtlsPacket,
217 MAX_FRAGMENT_LENGTH + 2048 + 5);
218 if (err) { /* realloc has set error code to no mem. */
219 return err;
220 }
221 }
222
223 /* recv() needs to read a full datagram at a time */
224 nb = ssl_DefRecv(ss, gs->dtlsPacket.buf, gs->dtlsPacket.space, flags);
225
226 if (nb > 0) {
227 PRINT_BUF(60, (ss, "raw gather data:", gs->dtlsPacket.buf, nb));
228 } else if (nb == 0) {
229 /* EOF */
230 SSL_TRC(30, ("%d: SSL3[%d]: EOF", SSL_GETPID(), ss->fd));
231 rv = 0;
232 return rv;
233 } else /* if (nb < 0) */ {
234 SSL_DBG(("%d: SSL3[%d]: recv error %d", SSL_GETPID(), ss->fd,
235 PR_GetError()));
236 rv = SECFailure;
237 return rv;
238 }
239
240 gs->dtlsPacket.len = nb;
241 }
242
243 /* At this point we should have >=1 complete records lined up in
244 * dtlsPacket. Read off the header.
245 */
246 if ((gs->dtlsPacket.len - gs->dtlsPacketOffset) < 13) {
247 SSL_DBG(("%d: SSL3[%d]: rest of DTLS packet "
248 "too short to contain header", SSL_GETPID(), ss->fd));
249 PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
250 gs->dtlsPacketOffset = 0;
251 gs->dtlsPacket.len = 0;
252 rv = SECFailure;
253 return rv;
254 }
255 memcpy(gs->hdr, gs->dtlsPacket.buf + gs->dtlsPacketOffset, 13);
256 gs->dtlsPacketOffset += 13;
257
258 /* Have received SSL3 record header in gs->hdr. */
259 gs->remainder = (gs->hdr[11] << 8) | gs->hdr[12];
260
261 if ((gs->dtlsPacket.len - gs->dtlsPacketOffset) < gs->remainder) {
262 SSL_DBG(("%d: SSL3[%d]: rest of DTLS packet too short "
263 "to contain rest of body", SSL_GETPID(), ss->fd));
264 PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
265 gs->dtlsPacketOffset = 0;
266 gs->dtlsPacket.len = 0;
267 rv = SECFailure;
268 return rv;
269 }
270
271 /* OK, we have at least one complete packet, copy into inbuf */
272 if (gs->remainder > gs->inbuf.space) {
273 err = sslBuffer_Grow(&gs->inbuf, gs->remainder);
274 if (err) { /* realloc has set error code to no mem. */
275 return err;
276 }
277 }
278
279 memcpy(gs->inbuf.buf, gs->dtlsPacket.buf + gs->dtlsPacketOffset,
280 gs->remainder);
281 gs->inbuf.len = gs->remainder;
282 gs->offset = gs->remainder;
283 gs->dtlsPacketOffset += gs->remainder;
284 gs->state = GS_INIT;
285
286 return 1;
287 }
288
170 /* Gather in a record and when complete, Handle that record. 289 /* Gather in a record and when complete, Handle that record.
171 * Repeat this until the handshake is complete, 290 * Repeat this until the handshake is complete,
172 * or until application data is available. 291 * or until application data is available.
173 * 292 *
174 * Returns 1 when the handshake is completed without error, or 293 * Returns 1 when the handshake is completed without error, or
175 * application data is available. 294 * application data is available.
176 * Returns 0 if ssl3_GatherData hits EOF. 295 * Returns 0 if ssl3_GatherData hits EOF.
177 * Returns -1 on read error, or PR_WOULD_BLOCK_ERROR, or handleRecord error. 296 * Returns -1 on read error, or PR_WOULD_BLOCK_ERROR, or handleRecord error.
178 * Returns -2 on SECWouldBlock return from ssl3_HandleRecord. 297 * Returns -2 on SECWouldBlock return from ssl3_HandleRecord.
179 * 298 *
180 * Called from ssl_GatherRecord1stHandshake in sslcon.c, 299 * Called from ssl_GatherRecord1stHandshake in sslcon.c,
181 * and from SSL_ForceHandshake in sslsecur.c 300 * and from SSL_ForceHandshake in sslsecur.c
182 * and from ssl3_GatherAppDataRecord below (<- DoRecv in sslsecur.c). 301 * and from ssl3_GatherAppDataRecord below (<- DoRecv in sslsecur.c).
183 * 302 *
184 * Caller must hold the recv buf lock. 303 * Caller must hold the recv buf lock.
185 */ 304 */
186 int 305 int
187 ssl3_GatherCompleteHandshake(sslSocket *ss, int flags) 306 ssl3_GatherCompleteHandshake(sslSocket *ss, int flags)
188 { 307 {
189 SSL3Ciphertext cText; 308 SSL3Ciphertext cText;
190 int rv; 309 int rv;
191 PRBool canFalseStart = PR_FALSE; 310 PRBool canFalseStart = PR_FALSE;
192 311
312 SSL_TRC(30, ("ssl3_GatherCompleteHandshake"));
313
193 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) ); 314 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
194 do { 315 do {
195 /* Without this, we may end up wrongly reporting 316 /* Without this, we may end up wrongly reporting
196 * SSL_ERROR_RX_UNEXPECTED_* errors if we receive any records from the 317 * SSL_ERROR_RX_UNEXPECTED_* errors if we receive any records from the
197 * peer while we are waiting to be restarted. 318 * peer while we are waiting to be restarted.
198 */ 319 */
199 ssl_GetSSL3HandshakeLock(ss); 320 ssl_GetSSL3HandshakeLock(ss);
200 rv = ss->ssl3.hs.restartTarget == NULL ? SECSuccess : SECFailure; 321 rv = ss->ssl3.hs.restartTarget == NULL ? SECSuccess : SECFailure;
201 ssl_ReleaseSSL3HandshakeLock(ss); 322 ssl_ReleaseSSL3HandshakeLock(ss);
202 if (rv != SECSuccess) { 323 if (rv != SECSuccess) {
(...skipping 14 matching lines...) Expand all
217 338
218 if (ss->ssl3.hs.msgState.buf != NULL) { 339 if (ss->ssl3.hs.msgState.buf != NULL) {
219 /* ssl3_HandleHandshake previously returned SECWouldBlock and the 340 /* ssl3_HandleHandshake previously returned SECWouldBlock and the
220 * as-yet-unprocessed plaintext of that previous handshake record. 341 * as-yet-unprocessed plaintext of that previous handshake record.
221 * We need to process it now before we overwrite it with the next 342 * We need to process it now before we overwrite it with the next
222 * handshake record. 343 * handshake record.
223 */ 344 */
224 rv = ssl3_HandleRecord(ss, NULL, &ss->gs.buf); 345 rv = ssl3_HandleRecord(ss, NULL, &ss->gs.buf);
225 } else { 346 } else {
226 /* bring in the next sslv3 record. */ 347 /* bring in the next sslv3 record. */
227 » rv = ssl3_GatherData(ss, &ss->gs, flags); 348 » if (IS_DTLS(ss)) {
349 » » rv = dtls_GatherData(ss, &ss->gs, flags);
350 » »
351 » » /* If we got a would block error, that means that no data was
352 » » * available, so we check the timer to see if it's time to
353 » » * retransmit */
354 » » if (rv == SECFailure &&
355 » » (PORT_GetError() == PR_WOULD_BLOCK_ERROR)) {
356 » » ssl_GetSSL3HandshakeLock(ss);
357 » » dtls_CheckTimer(ss);
358 » » ssl_ReleaseSSL3HandshakeLock(ss);
359 » » /* Restore the error in case something succeeded */
360 » » PORT_SetError(PR_WOULD_BLOCK_ERROR);
361 » » }
362 » } else {
363 » » rv = ssl3_GatherData(ss, &ss->gs, flags);
364 » }
365
228 if (rv <= 0) { 366 if (rv <= 0) {
229 return rv; 367 return rv;
230 } 368 }
231 369
232 /* decipher it, and handle it if it's a handshake. 370 /* decipher it, and handle it if it's a handshake.
233 * If it's application data, ss->gs.buf will not be empty upon retur n. 371 * If it's application data, ss->gs.buf will not be empty upon retur n.
234 * If it's a change cipher spec, alert, or handshake message, 372 * If it's a change cipher spec, alert, or handshake message,
235 * ss->gs.buf.len will be 0 when ssl3_HandleRecord returns SECSucces s. 373 * ss->gs.buf.len will be 0 when ssl3_HandleRecord returns SECSucces s.
236 */ 374 */
237 cText.type = (SSL3ContentType)ss->gs.hdr[0]; 375 cText.type = (SSL3ContentType)ss->gs.hdr[0];
238 cText.version = (ss->gs.hdr[1] << 8) | ss->gs.hdr[2]; 376 cText.version = (ss->gs.hdr[1] << 8) | ss->gs.hdr[2];
377
378 /* As above, we need to test datagram mode */
379 if (IS_DTLS(ss)) {
380 int i;
381
382 cText.version = dtls_DTLSVersionToTLSVersion(cText.version);
383 /* DTLS sequence number */
384 cText.seq_num.high = 0; cText.seq_num.low = 0;
385 for (i = 0; i < 4; i++) {
386 cText.seq_num.high <<= 8; cText.seq_num.low <<= 8;
387 cText.seq_num.high |= ss->gs.hdr[3 + i];
388 cText.seq_num.low |= ss->gs.hdr[7 + i];
389 }
390 }
391
239 cText.buf = &ss->gs.inbuf; 392 cText.buf = &ss->gs.inbuf;
240 rv = ssl3_HandleRecord(ss, &cText, &ss->gs.buf); 393 rv = ssl3_HandleRecord(ss, &cText, &ss->gs.buf);
241 } 394 }
242 if (rv < 0) { 395 if (rv < 0) {
243 return ss->recvdCloseNotify ? 0 : rv; 396 return ss->recvdCloseNotify ? 0 : rv;
244 } 397 }
245 398
246 /* If we kicked off a false start in ssl3_HandleServerHelloDone, break 399 /* If we kicked off a false start in ssl3_HandleServerHelloDone, break
247 * out of this loop early without finishing the handshake. 400 * out of this loop early without finishing the handshake.
248 */ 401 */
(...skipping 29 matching lines...) Expand all
278 { 431 {
279 int rv; 432 int rv;
280 433
281 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) ); 434 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
282 do { 435 do {
283 rv = ssl3_GatherCompleteHandshake(ss, flags); 436 rv = ssl3_GatherCompleteHandshake(ss, flags);
284 } while (rv > 0 && ss->gs.buf.len == 0); 437 } while (rv > 0 && ss->gs.buf.len == 0);
285 438
286 return rv; 439 return rv;
287 } 440 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698