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

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 one pass, dtls1con.c not reviewed yet 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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 ** SSL3 record has been completely received. 160 ** SSL3 record has been completely received.
161 */ 161 */
162 gs->state = GS_INIT; 162 gs->state = GS_INIT;
163 return 1; 163 return 1;
164 } 164 }
165 } 165 }
166 166
167 return rv; 167 return rv;
168 } 168 }
169 169
170 /*
171 * Read in an entire DTLS record.
172 *
173 * Blocks here for blocking sockets, otherwise returns -1 with
174 * PR_WOULD_BLOCK_ERROR when socket would block.
175 *
176 * This is simpler than SSL because we are reading on a datagram socket
177 * and datagrams must contain >=1 complete records.
178 *
179 * returns 1 if received a complete DTLS record.
180 * returns 0 if recv returns EOF
181 * returns -1 if recv returns <0
182 * (The error value may have already been set to PR_WOULD_BLOCK_ERROR)
183 *
184 * Caller must hold the recv buf lock.
185 *
186 * This loop returns when either (a) an error or EOF occurs,
187 * (b) PR_WOULD_BLOCK_ERROR,
188 * (c) data (entire DTLS record) has been received.
189 */
190 static int
191 dtls_GatherData(sslSocket *ss, sslGather *gs, int flags)
192 {
193 int nb;
194 int err;
195 int rv = 1;
196
197 SSL_TRC(30, ("dtls_GatherData"));
198
199 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
200
201 gs->state = GS_HEADER;
202 gs->offset = 0;
203
204 if (gs->dtlsPacketOffset == gs->dtlsPacket.len) { /* No data left */
205 gs->dtlsPacketOffset = 0;
206 gs->dtlsPacket.len = 0;
207
208 /* Resize to the maximum possible size so we can fit a full datagram */
209 if (gs->dtlsPacket.space < MAX_FRAGMENT_LENGTH + 2048 + 5) {
210 err = sslBuffer_Grow(&gs->dtlsPacket,
211 MAX_FRAGMENT_LENGTH + 2048 + 5);
212 if (err) { /* realloc has set error code to no mem. */
213 return err;
214 }
215 }
216
217 /* recv() needs to read a full datagram at a time */
218 nb = ssl_DefRecv(ss, gs->dtlsPacket.buf, gs->dtlsPacket.space, flags);
219
220 if (nb > 0) {
221 PRINT_BUF(60, (ss, "raw gather data:", gs->dtlsPacket.buf, nb));
222 } else if (nb == 0) {
223 /* EOF */
224 SSL_TRC(30, ("%d: SSL3[%d]: EOF", SSL_GETPID(), ss->fd));
225 rv = 0;
226 return rv;
227 } else /* if (nb < 0) */ {
228 SSL_DBG(("%d: SSL3[%d]: recv error %d", SSL_GETPID(), ss->fd,
229 PR_GetError()));
230 rv = SECFailure;
231 return rv;
232 }
233
234 gs->dtlsPacket.len = nb;
235 }
236
237 /* At this point we should have >=1 complete records lined up in
238 * dtlsPacket. Read off the header.
239 */
240 if ((gs->dtlsPacket.len - gs->dtlsPacketOffset) < 13) {
241 SSL_DBG(("%d: SSL3[%d]: rest of DTLS packet "
242 "too short to contain header", SSL_GETPID(), ss->fd));
243 PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
244 gs->dtlsPacketOffset = 0;
245 gs->dtlsPacket.len = 0;
246 rv = SECFailure;
247 return rv;
248 }
249 memcpy(gs->hdr, gs->dtlsPacket.buf + gs->dtlsPacketOffset, 13);
250 gs->dtlsPacketOffset += 13;
251
252
253 /* Have received SSL3 record header in gs->hdr. */
254 gs->remainder = (gs->hdr[11] << 8) | gs->hdr[12];
255
256 if ((gs->dtlsPacket.len - gs->dtlsPacketOffset) < gs->remainder) {
257 SSL_DBG(("%d: SSL3[%d]: rest of DTLS packet too short "
258 "to contain rest of body", SSL_GETPID(), ss->fd));
259 PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
260 gs->dtlsPacketOffset = 0;
261 gs->dtlsPacket.len = 0;
262 rv = SECFailure;
263 return rv;
264 }
265
266 /* OK, we have at least one complete packet, copy into inbuf */
267 if (gs->remainder > gs->inbuf.space) {
268 err = sslBuffer_Grow(&gs->inbuf, gs->remainder);
269 if (err) { /* realloc has set error code to no mem. */
270 return err;
271 }
272 }
273
274 memcpy(gs->inbuf.buf, gs->dtlsPacket.buf + gs->dtlsPacketOffset,
275 gs->remainder);
276 gs->inbuf.len = gs->remainder;
277 gs->offset = gs->remainder;
278 gs->dtlsPacketOffset += gs->remainder;
279 gs->state = GS_INIT;
280
281 return 1;
282 }
283
170 /* Gather in a record and when complete, Handle that record. 284 /* Gather in a record and when complete, Handle that record.
171 * Repeat this until the handshake is complete, 285 * Repeat this until the handshake is complete,
172 * or until application data is available. 286 * or until application data is available.
173 * 287 *
174 * Returns 1 when the handshake is completed without error, or 288 * Returns 1 when the handshake is completed without error, or
175 * application data is available. 289 * application data is available.
176 * Returns 0 if ssl3_GatherData hits EOF. 290 * Returns 0 if ssl3_GatherData hits EOF.
177 * Returns -1 on read error, or PR_WOULD_BLOCK_ERROR, or handleRecord error. 291 * Returns -1 on read error, or PR_WOULD_BLOCK_ERROR, or handleRecord error.
178 * Returns -2 on SECWouldBlock return from ssl3_HandleRecord. 292 * Returns -2 on SECWouldBlock return from ssl3_HandleRecord.
179 * 293 *
180 * Called from ssl_GatherRecord1stHandshake in sslcon.c, 294 * Called from ssl_GatherRecord1stHandshake in sslcon.c,
181 * and from SSL_ForceHandshake in sslsecur.c 295 * and from SSL_ForceHandshake in sslsecur.c
182 * and from ssl3_GatherAppDataRecord below (<- DoRecv in sslsecur.c). 296 * and from ssl3_GatherAppDataRecord below (<- DoRecv in sslsecur.c).
183 * 297 *
184 * Caller must hold the recv buf lock. 298 * Caller must hold the recv buf lock.
185 */ 299 */
186 int 300 int
187 ssl3_GatherCompleteHandshake(sslSocket *ss, int flags) 301 ssl3_GatherCompleteHandshake(sslSocket *ss, int flags)
188 { 302 {
189 SSL3Ciphertext cText; 303 SSL3Ciphertext cText;
190 int rv; 304 int rv;
191 PRBool canFalseStart = PR_FALSE; 305 PRBool canFalseStart = PR_FALSE;
192 306
307 SSL_TRC(30, ("ssl3_GatherCompleteHandshake"));
308
193 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) ); 309 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
194 do { 310 do {
195 /* Without this, we may end up wrongly reporting 311 /* Without this, we may end up wrongly reporting
196 * SSL_ERROR_RX_UNEXPECTED_* errors if we receive any records from the 312 * SSL_ERROR_RX_UNEXPECTED_* errors if we receive any records from the
197 * peer while we are waiting to be restarted. 313 * peer while we are waiting to be restarted.
198 */ 314 */
199 ssl_GetSSL3HandshakeLock(ss); 315 ssl_GetSSL3HandshakeLock(ss);
200 rv = ss->ssl3.hs.restartTarget == NULL ? SECSuccess : SECFailure; 316 rv = ss->ssl3.hs.restartTarget == NULL ? SECSuccess : SECFailure;
201 ssl_ReleaseSSL3HandshakeLock(ss); 317 ssl_ReleaseSSL3HandshakeLock(ss);
202 if (rv != SECSuccess) { 318 if (rv != SECSuccess) {
(...skipping 14 matching lines...) Expand all
217 333
218 if (ss->ssl3.hs.msgState.buf != NULL) { 334 if (ss->ssl3.hs.msgState.buf != NULL) {
219 /* ssl3_HandleHandshake previously returned SECWouldBlock and the 335 /* ssl3_HandleHandshake previously returned SECWouldBlock and the
220 * as-yet-unprocessed plaintext of that previous handshake record. 336 * as-yet-unprocessed plaintext of that previous handshake record.
221 * We need to process it now before we overwrite it with the next 337 * We need to process it now before we overwrite it with the next
222 * handshake record. 338 * handshake record.
223 */ 339 */
224 rv = ssl3_HandleRecord(ss, NULL, &ss->gs.buf); 340 rv = ssl3_HandleRecord(ss, NULL, &ss->gs.buf);
225 } else { 341 } else {
226 /* bring in the next sslv3 record. */ 342 /* bring in the next sslv3 record. */
227 » rv = ssl3_GatherData(ss, &ss->gs, flags); 343 » if (IS_DTLS(ss)) {
344 » » rv = dtls_GatherData(ss, &ss->gs, flags);
345 » »
346 » » /* If we got a would block error, that means that no data was
347 » » * available, so we check the timer to see if it's time to
348 » » * retransmit */
349 » » if (rv == SECFailure && (PORT_GetError() == PR_WOULD_BLOCK_ERROR )) {
350 » » ssl_GetSSL3HandshakeLock(ss);
351 » » dtls_CheckTimer(ss);
352 » » /* Restore the error in case something succeeded */
353 » » PORT_SetError(PR_WOULD_BLOCK_ERROR);
354 » » ssl_ReleaseSSL3HandshakeLock(ss);
355 » » }
356 » } else {
357 » » rv = ssl3_GatherData(ss, &ss->gs, flags);
358 » }
359
228 if (rv <= 0) { 360 if (rv <= 0) {
229 return rv; 361 return rv;
230 } 362 }
231 363
232 /* decipher it, and handle it if it's a handshake. 364 /* 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. 365 * 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, 366 * 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. 367 * ss->gs.buf.len will be 0 when ssl3_HandleRecord returns SECSucces s.
236 */ 368 */
237 cText.type = (SSL3ContentType)ss->gs.hdr[0]; 369 cText.type = (SSL3ContentType)ss->gs.hdr[0];
238 » cText.version = (ss->gs.hdr[1] << 8) | ss->gs.hdr[2]; 370
371 » /* As above, we need to test datagram mode */
372 » if (IS_DTLS(ss)) {
373 » » cText.version = dtls_DTLSVersionToTLSVersion(
374 » » (((unsigned char)ss->gs.hdr[1]) << 8) |
375 » » (unsigned char )ss->gs.hdr[2]);
376 » } else {
377 » » cText.version = (ss->gs.hdr[1] << 8) | ss->gs.hdr[2];
378 » }
379
380 » /* DTLS sequence number */
381 » if (IS_DTLS(ss)) {
382 » » int i;
383
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