security/nss/lib/ssl/sslgathr.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Gather (Read) entire SSL2 records from socket into buffer.
michael@0 3 *
michael@0 4 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7 #include "cert.h"
michael@0 8 #include "ssl.h"
michael@0 9 #include "sslimpl.h"
michael@0 10 #include "sslproto.h"
michael@0 11
michael@0 12 /* Forward static declarations */
michael@0 13 static SECStatus ssl2_HandleV3HandshakeRecord(sslSocket *ss);
michael@0 14
michael@0 15 /*
michael@0 16 ** Gather a single record of data from the receiving stream. This code
michael@0 17 ** first gathers the header (2 or 3 bytes long depending on the value of
michael@0 18 ** the most significant bit in the first byte) then gathers up the data
michael@0 19 ** for the record into gs->buf. This code handles non-blocking I/O
michael@0 20 ** and is to be called multiple times until ss->sec.recordLen != 0.
michael@0 21 ** This function decrypts the gathered record in place, in gs_buf.
michael@0 22 *
michael@0 23 * Caller must hold RecvBufLock.
michael@0 24 *
michael@0 25 * Returns +1 when it has gathered a complete SSLV2 record.
michael@0 26 * Returns 0 if it hits EOF.
michael@0 27 * Returns -1 (SECFailure) on any error
michael@0 28 * Returns -2 (SECWouldBlock) when it gathers an SSL v3 client hello header.
michael@0 29 **
michael@0 30 ** The SSL2 Gather State machine has 4 states:
michael@0 31 ** GS_INIT - Done reading in previous record. Haven't begun to read in
michael@0 32 ** next record. When ssl2_GatherData is called with the machine
michael@0 33 ** in this state, the machine will attempt to read the first 3
michael@0 34 ** bytes of the SSL2 record header, and will advance the state
michael@0 35 ** to GS_HEADER.
michael@0 36 **
michael@0 37 ** GS_HEADER - The machine is in this state while waiting for the completion
michael@0 38 ** of the first 3 bytes of the SSL2 record. When complete, the
michael@0 39 ** machine will compute the remaining unread length of this record
michael@0 40 ** and will initiate a read of that many bytes. The machine will
michael@0 41 ** advance to one of two states, depending on whether the record
michael@0 42 ** is encrypted (GS_MAC), or unencrypted (GS_DATA).
michael@0 43 **
michael@0 44 ** GS_MAC - The machine is in this state while waiting for the remainder
michael@0 45 ** of the SSL2 record to be read in. When the read is completed,
michael@0 46 ** the machine checks the record for valid length, decrypts it,
michael@0 47 ** and checks and discards the MAC, then advances to GS_INIT.
michael@0 48 **
michael@0 49 ** GS_DATA - The machine is in this state while waiting for the remainder
michael@0 50 ** of the unencrypted SSL2 record to be read in. Upon completion,
michael@0 51 ** the machine advances to the GS_INIT state and returns the data.
michael@0 52 */
michael@0 53 int
michael@0 54 ssl2_GatherData(sslSocket *ss, sslGather *gs, int flags)
michael@0 55 {
michael@0 56 unsigned char * bp;
michael@0 57 unsigned char * pBuf;
michael@0 58 int nb, err, rv;
michael@0 59
michael@0 60 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
michael@0 61
michael@0 62 if (gs->state == GS_INIT) {
michael@0 63 /* Initialize gathering engine */
michael@0 64 gs->state = GS_HEADER;
michael@0 65 gs->remainder = 3;
michael@0 66 gs->count = 3;
michael@0 67 gs->offset = 0;
michael@0 68 gs->recordLen = 0;
michael@0 69 gs->recordPadding = 0;
michael@0 70 gs->hdr[2] = 0;
michael@0 71
michael@0 72 gs->writeOffset = 0;
michael@0 73 gs->readOffset = 0;
michael@0 74 }
michael@0 75 if (gs->encrypted) {
michael@0 76 PORT_Assert(ss->sec.hash != 0);
michael@0 77 }
michael@0 78
michael@0 79 pBuf = gs->buf.buf;
michael@0 80 for (;;) {
michael@0 81 SSL_TRC(30, ("%d: SSL[%d]: gather state %d (need %d more)",
michael@0 82 SSL_GETPID(), ss->fd, gs->state, gs->remainder));
michael@0 83 bp = ((gs->state != GS_HEADER) ? pBuf : gs->hdr) + gs->offset;
michael@0 84 nb = ssl_DefRecv(ss, bp, gs->remainder, flags);
michael@0 85 if (nb > 0) {
michael@0 86 PRINT_BUF(60, (ss, "raw gather data:", bp, nb));
michael@0 87 }
michael@0 88 if (nb == 0) {
michael@0 89 /* EOF */
michael@0 90 SSL_TRC(30, ("%d: SSL[%d]: EOF", SSL_GETPID(), ss->fd));
michael@0 91 rv = 0;
michael@0 92 break;
michael@0 93 }
michael@0 94 if (nb < 0) {
michael@0 95 SSL_DBG(("%d: SSL[%d]: recv error %d", SSL_GETPID(), ss->fd,
michael@0 96 PR_GetError()));
michael@0 97 rv = SECFailure;
michael@0 98 break;
michael@0 99 }
michael@0 100
michael@0 101 gs->offset += nb;
michael@0 102 gs->remainder -= nb;
michael@0 103
michael@0 104 if (gs->remainder > 0) {
michael@0 105 continue;
michael@0 106 }
michael@0 107
michael@0 108 /* Probably finished this piece */
michael@0 109 switch (gs->state) {
michael@0 110 case GS_HEADER:
michael@0 111 if (!SSL3_ALL_VERSIONS_DISABLED(&ss->vrange) && !ss->firstHsDone) {
michael@0 112
michael@0 113 PORT_Assert( ss->opt.noLocks || ssl_Have1stHandshakeLock(ss) );
michael@0 114
michael@0 115 /* If this looks like an SSL3 handshake record,
michael@0 116 ** and we're expecting an SSL2 Hello message from our peer,
michael@0 117 ** handle it here.
michael@0 118 */
michael@0 119 if (gs->hdr[0] == content_handshake) {
michael@0 120 if ((ss->nextHandshake == ssl2_HandleClientHelloMessage) ||
michael@0 121 (ss->nextHandshake == ssl2_HandleServerHelloMessage)) {
michael@0 122 rv = ssl2_HandleV3HandshakeRecord(ss);
michael@0 123 if (rv == SECFailure) {
michael@0 124 return SECFailure;
michael@0 125 }
michael@0 126 }
michael@0 127 /* XXX_1 The call stack to here is:
michael@0 128 * ssl_Do1stHandshake -> ssl_GatherRecord1stHandshake ->
michael@0 129 * ssl2_GatherRecord -> here.
michael@0 130 * We want to return all the way out to ssl_Do1stHandshake,
michael@0 131 * and have it call ssl_GatherRecord1stHandshake again.
michael@0 132 * ssl_GatherRecord1stHandshake will call
michael@0 133 * ssl3_GatherCompleteHandshake when it is called again.
michael@0 134 *
michael@0 135 * Returning SECWouldBlock here causes
michael@0 136 * ssl_GatherRecord1stHandshake to return without clearing
michael@0 137 * ss->handshake, ensuring that ssl_Do1stHandshake will
michael@0 138 * call it again immediately.
michael@0 139 *
michael@0 140 * If we return 1 here, ssl_GatherRecord1stHandshake will
michael@0 141 * clear ss->handshake before returning, and thus will not
michael@0 142 * be called again by ssl_Do1stHandshake.
michael@0 143 */
michael@0 144 return SECWouldBlock;
michael@0 145 } else if (gs->hdr[0] == content_alert) {
michael@0 146 if (ss->nextHandshake == ssl2_HandleServerHelloMessage) {
michael@0 147 /* XXX This is a hack. We're assuming that any failure
michael@0 148 * XXX on the client hello is a failure to match
michael@0 149 * XXX ciphers.
michael@0 150 */
michael@0 151 PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP);
michael@0 152 return SECFailure;
michael@0 153 }
michael@0 154 }
michael@0 155 }
michael@0 156
michael@0 157 /* we've got the first 3 bytes. The header may be two or three. */
michael@0 158 if (gs->hdr[0] & 0x80) {
michael@0 159 /* This record has a 2-byte header, and no padding */
michael@0 160 gs->count = ((gs->hdr[0] & 0x7f) << 8) | gs->hdr[1];
michael@0 161 gs->recordPadding = 0;
michael@0 162 } else {
michael@0 163 /* This record has a 3-byte header that is all read in now. */
michael@0 164 gs->count = ((gs->hdr[0] & 0x3f) << 8) | gs->hdr[1];
michael@0 165 /* is_escape = (gs->hdr[0] & 0x40) != 0; */
michael@0 166 gs->recordPadding = gs->hdr[2];
michael@0 167 }
michael@0 168 if (!gs->count) {
michael@0 169 PORT_SetError(SSL_ERROR_RX_RECORD_TOO_LONG);
michael@0 170 goto cleanup;
michael@0 171 }
michael@0 172
michael@0 173 if (gs->count > gs->buf.space) {
michael@0 174 err = sslBuffer_Grow(&gs->buf, gs->count);
michael@0 175 if (err) {
michael@0 176 return err;
michael@0 177 }
michael@0 178 pBuf = gs->buf.buf;
michael@0 179 }
michael@0 180
michael@0 181
michael@0 182 if (gs->hdr[0] & 0x80) {
michael@0 183 /* we've already read in the first byte of the body.
michael@0 184 ** Put it into the buffer.
michael@0 185 */
michael@0 186 pBuf[0] = gs->hdr[2];
michael@0 187 gs->offset = 1;
michael@0 188 gs->remainder = gs->count - 1;
michael@0 189 } else {
michael@0 190 gs->offset = 0;
michael@0 191 gs->remainder = gs->count;
michael@0 192 }
michael@0 193
michael@0 194 if (gs->encrypted) {
michael@0 195 gs->state = GS_MAC;
michael@0 196 gs->recordLen = gs->count - gs->recordPadding
michael@0 197 - ss->sec.hash->length;
michael@0 198 } else {
michael@0 199 gs->state = GS_DATA;
michael@0 200 gs->recordLen = gs->count;
michael@0 201 }
michael@0 202
michael@0 203 break;
michael@0 204
michael@0 205
michael@0 206 case GS_MAC:
michael@0 207 /* Have read in entire rest of the ciphertext.
michael@0 208 ** Check for valid length.
michael@0 209 ** Decrypt it.
michael@0 210 ** Check the MAC.
michael@0 211 */
michael@0 212 PORT_Assert(gs->encrypted);
michael@0 213
michael@0 214 {
michael@0 215 unsigned int macLen;
michael@0 216 int nout;
michael@0 217 unsigned char mac[SSL_MAX_MAC_BYTES];
michael@0 218
michael@0 219 ssl_GetSpecReadLock(ss); /**********************************/
michael@0 220
michael@0 221 /* If this is a stream cipher, blockSize will be 1,
michael@0 222 * and this test will always be false.
michael@0 223 * If this is a block cipher, this will detect records
michael@0 224 * that are not a multiple of the blocksize in length.
michael@0 225 */
michael@0 226 if (gs->count & (ss->sec.blockSize - 1)) {
michael@0 227 /* This is an error. Sender is misbehaving */
michael@0 228 SSL_DBG(("%d: SSL[%d]: sender, count=%d blockSize=%d",
michael@0 229 SSL_GETPID(), ss->fd, gs->count,
michael@0 230 ss->sec.blockSize));
michael@0 231 PORT_SetError(SSL_ERROR_BAD_BLOCK_PADDING);
michael@0 232 rv = SECFailure;
michael@0 233 goto spec_locked_done;
michael@0 234 }
michael@0 235 PORT_Assert(gs->count == gs->offset);
michael@0 236
michael@0 237 if (gs->offset == 0) {
michael@0 238 rv = 0; /* means EOF. */
michael@0 239 goto spec_locked_done;
michael@0 240 }
michael@0 241
michael@0 242 /* Decrypt the portion of data that we just received.
michael@0 243 ** Decrypt it in place.
michael@0 244 */
michael@0 245 rv = (*ss->sec.dec)(ss->sec.readcx, pBuf, &nout, gs->offset,
michael@0 246 pBuf, gs->offset);
michael@0 247 if (rv != SECSuccess) {
michael@0 248 goto spec_locked_done;
michael@0 249 }
michael@0 250
michael@0 251
michael@0 252 /* Have read in all the MAC portion of record
michael@0 253 **
michael@0 254 ** Prepare MAC by resetting it and feeding it the shared secret
michael@0 255 */
michael@0 256 macLen = ss->sec.hash->length;
michael@0 257 if (gs->offset >= macLen) {
michael@0 258 PRUint32 sequenceNumber = ss->sec.rcvSequence++;
michael@0 259 unsigned char seq[4];
michael@0 260
michael@0 261 seq[0] = (unsigned char) (sequenceNumber >> 24);
michael@0 262 seq[1] = (unsigned char) (sequenceNumber >> 16);
michael@0 263 seq[2] = (unsigned char) (sequenceNumber >> 8);
michael@0 264 seq[3] = (unsigned char) (sequenceNumber);
michael@0 265
michael@0 266 (*ss->sec.hash->begin)(ss->sec.hashcx);
michael@0 267 (*ss->sec.hash->update)(ss->sec.hashcx, ss->sec.rcvSecret.data,
michael@0 268 ss->sec.rcvSecret.len);
michael@0 269 (*ss->sec.hash->update)(ss->sec.hashcx, pBuf + macLen,
michael@0 270 gs->offset - macLen);
michael@0 271 (*ss->sec.hash->update)(ss->sec.hashcx, seq, 4);
michael@0 272 (*ss->sec.hash->end)(ss->sec.hashcx, mac, &macLen, macLen);
michael@0 273
michael@0 274 PORT_Assert(macLen == ss->sec.hash->length);
michael@0 275
michael@0 276 ssl_ReleaseSpecReadLock(ss); /******************************/
michael@0 277
michael@0 278 if (NSS_SecureMemcmp(mac, pBuf, macLen) != 0) {
michael@0 279 /* MAC's didn't match... */
michael@0 280 SSL_DBG(("%d: SSL[%d]: mac check failed, seq=%d",
michael@0 281 SSL_GETPID(), ss->fd, ss->sec.rcvSequence));
michael@0 282 PRINT_BUF(1, (ss, "computed mac:", mac, macLen));
michael@0 283 PRINT_BUF(1, (ss, "received mac:", pBuf, macLen));
michael@0 284 PORT_SetError(SSL_ERROR_BAD_MAC_READ);
michael@0 285 rv = SECFailure;
michael@0 286 goto cleanup;
michael@0 287 }
michael@0 288 } else {
michael@0 289 ssl_ReleaseSpecReadLock(ss); /******************************/
michael@0 290 }
michael@0 291
michael@0 292 if (gs->recordPadding + macLen <= gs->offset) {
michael@0 293 gs->recordOffset = macLen;
michael@0 294 gs->readOffset = macLen;
michael@0 295 gs->writeOffset = gs->offset - gs->recordPadding;
michael@0 296 rv = 1;
michael@0 297 } else {
michael@0 298 PORT_SetError(SSL_ERROR_BAD_BLOCK_PADDING);
michael@0 299 cleanup:
michael@0 300 /* nothing in the buffer any more. */
michael@0 301 gs->recordOffset = 0;
michael@0 302 gs->readOffset = 0;
michael@0 303 gs->writeOffset = 0;
michael@0 304 rv = SECFailure;
michael@0 305 }
michael@0 306
michael@0 307 gs->recordLen = gs->writeOffset - gs->readOffset;
michael@0 308 gs->recordPadding = 0; /* forget we did any padding. */
michael@0 309 gs->state = GS_INIT;
michael@0 310
michael@0 311
michael@0 312 if (rv > 0) {
michael@0 313 PRINT_BUF(50, (ss, "recv clear record:",
michael@0 314 pBuf + gs->recordOffset, gs->recordLen));
michael@0 315 }
michael@0 316 return rv;
michael@0 317
michael@0 318 spec_locked_done:
michael@0 319 ssl_ReleaseSpecReadLock(ss);
michael@0 320 return rv;
michael@0 321 }
michael@0 322
michael@0 323 case GS_DATA:
michael@0 324 /* Have read in all the DATA portion of record */
michael@0 325
michael@0 326 gs->recordOffset = 0;
michael@0 327 gs->readOffset = 0;
michael@0 328 gs->writeOffset = gs->offset;
michael@0 329 PORT_Assert(gs->recordLen == gs->writeOffset - gs->readOffset);
michael@0 330 gs->recordLen = gs->offset;
michael@0 331 gs->recordPadding = 0;
michael@0 332 gs->state = GS_INIT;
michael@0 333
michael@0 334 ++ss->sec.rcvSequence;
michael@0 335
michael@0 336 PRINT_BUF(50, (ss, "recv clear record:",
michael@0 337 pBuf + gs->recordOffset, gs->recordLen));
michael@0 338 return 1;
michael@0 339
michael@0 340 } /* end switch gs->state */
michael@0 341 } /* end gather loop. */
michael@0 342 return rv;
michael@0 343 }
michael@0 344
michael@0 345 /*
michael@0 346 ** Gather a single record of data from the receiving stream. This code
michael@0 347 ** first gathers the header (2 or 3 bytes long depending on the value of
michael@0 348 ** the most significant bit in the first byte) then gathers up the data
michael@0 349 ** for the record into the readBuf. This code handles non-blocking I/O
michael@0 350 ** and is to be called multiple times until ss->sec.recordLen != 0.
michael@0 351 *
michael@0 352 * Returns +1 when it has gathered a complete SSLV2 record.
michael@0 353 * Returns 0 if it hits EOF.
michael@0 354 * Returns -1 (SECFailure) on any error
michael@0 355 * Returns -2 (SECWouldBlock)
michael@0 356 *
michael@0 357 * Called by ssl_GatherRecord1stHandshake in sslcon.c,
michael@0 358 * and by DoRecv in sslsecur.c
michael@0 359 * Caller must hold RecvBufLock.
michael@0 360 */
michael@0 361 int
michael@0 362 ssl2_GatherRecord(sslSocket *ss, int flags)
michael@0 363 {
michael@0 364 return ssl2_GatherData(ss, &ss->gs, flags);
michael@0 365 }
michael@0 366
michael@0 367 /* Caller should hold RecvBufLock. */
michael@0 368 SECStatus
michael@0 369 ssl_InitGather(sslGather *gs)
michael@0 370 {
michael@0 371 SECStatus status;
michael@0 372
michael@0 373 gs->state = GS_INIT;
michael@0 374 gs->writeOffset = 0;
michael@0 375 gs->readOffset = 0;
michael@0 376 gs->dtlsPacketOffset = 0;
michael@0 377 gs->dtlsPacket.len = 0;
michael@0 378 status = sslBuffer_Grow(&gs->buf, 4096);
michael@0 379 return status;
michael@0 380 }
michael@0 381
michael@0 382 /* Caller must hold RecvBufLock. */
michael@0 383 void
michael@0 384 ssl_DestroyGather(sslGather *gs)
michael@0 385 {
michael@0 386 if (gs) { /* the PORT_*Free functions check for NULL pointers. */
michael@0 387 PORT_ZFree(gs->buf.buf, gs->buf.space);
michael@0 388 PORT_Free(gs->inbuf.buf);
michael@0 389 PORT_Free(gs->dtlsPacket.buf);
michael@0 390 }
michael@0 391 }
michael@0 392
michael@0 393 /* Caller must hold RecvBufLock. */
michael@0 394 static SECStatus
michael@0 395 ssl2_HandleV3HandshakeRecord(sslSocket *ss)
michael@0 396 {
michael@0 397 SECStatus rv;
michael@0 398
michael@0 399 PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
michael@0 400 PORT_Assert( ss->opt.noLocks || ssl_Have1stHandshakeLock(ss) );
michael@0 401
michael@0 402 /* We've read in 3 bytes, there are 2 more to go in an ssl3 header. */
michael@0 403 ss->gs.remainder = 2;
michael@0 404 ss->gs.count = 0;
michael@0 405
michael@0 406 /* Clearing these handshake pointers ensures that
michael@0 407 * ssl_Do1stHandshake won't call ssl2_HandleMessage when we return.
michael@0 408 */
michael@0 409 ss->nextHandshake = 0;
michael@0 410 ss->securityHandshake = 0;
michael@0 411
michael@0 412 /* Setting ss->version to an SSL 3.x value will cause
michael@0 413 ** ssl_GatherRecord1stHandshake to invoke ssl3_GatherCompleteHandshake()
michael@0 414 ** the next time it is called.
michael@0 415 **/
michael@0 416 rv = ssl3_NegotiateVersion(ss, SSL_LIBRARY_VERSION_MAX_SUPPORTED,
michael@0 417 PR_TRUE);
michael@0 418 if (rv != SECSuccess) {
michael@0 419 return rv;
michael@0 420 }
michael@0 421
michael@0 422 ss->sec.send = ssl3_SendApplicationData;
michael@0 423
michael@0 424 return SECSuccess;
michael@0 425 }

mercurial