• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Gather (Read) entire SSL3 records from socket into buffer.
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
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
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is the Netscape security libraries.
18  *
19  * The Initial Developer of the Original Code is
20  * Netscape Communications Corporation.
21  * Portions created by the Initial Developer are Copyright (C) 1994-2000
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 /* $Id: ssl3gthr.c,v 1.9 2008/11/20 07:37:25 nelson%bolyard.com Exp $ */
40 
41 #include "cert.h"
42 #include "ssl.h"
43 #include "sslimpl.h"
44 #include "ssl3prot.h"
45 
46 /*
47  * Attempt to read in an entire SSL3 record.
48  * Blocks here for blocking sockets, otherwise returns -1 with
49  * 	PR_WOULD_BLOCK_ERROR when socket would block.
50  *
51  * returns  1 if received a complete SSL3 record.
52  * returns  0 if recv returns EOF
53  * returns -1 if recv returns <0
54  *	(The error value may have already been set to PR_WOULD_BLOCK_ERROR)
55  *
56  * Caller must hold the recv buf lock.
57  *
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.
60  * GS_DATA:   waiting for the body of the SSL3 record   to come in.
61  *
62  * This loop returns when either (a) an error or EOF occurs,
63  *	(b) PR_WOULD_BLOCK_ERROR,
64  * 	(c) data (entire SSL3 record) has been received.
65  */
66 static int
ssl3_GatherData(sslSocket * ss,sslGather * gs,int flags)67 ssl3_GatherData(sslSocket *ss, sslGather *gs, int flags)
68 {
69     unsigned char *bp;
70     unsigned char *lbp;
71     int            nb;
72     int            err;
73     int            rv		= 1;
74 
75     PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
76     if (gs->state == GS_INIT) {
77 	gs->state       = GS_HEADER;
78 	gs->remainder   = 5;
79 	gs->offset      = 0;
80 	gs->writeOffset = 0;
81 	gs->readOffset  = 0;
82 	gs->inbuf.len   = 0;
83     }
84 
85     lbp = gs->inbuf.buf;
86     for(;;) {
87 	SSL_TRC(30, ("%d: SSL3[%d]: gather state %d (need %d more)",
88 		SSL_GETPID(), ss->fd, gs->state, gs->remainder));
89 	bp = ((gs->state != GS_HEADER) ? lbp : gs->hdr) + gs->offset;
90 	nb = ssl_DefRecv(ss, bp, gs->remainder, flags);
91 
92 	if (nb > 0) {
93 	    PRINT_BUF(60, (ss, "raw gather data:", bp, nb));
94 	} else if (nb == 0) {
95 	    /* EOF */
96 	    SSL_TRC(30, ("%d: SSL3[%d]: EOF", SSL_GETPID(), ss->fd));
97 	    rv = 0;
98 	    break;
99 	} else /* if (nb < 0) */ {
100 	    SSL_DBG(("%d: SSL3[%d]: recv error %d", SSL_GETPID(), ss->fd,
101 		     PR_GetError()));
102 	    rv = SECFailure;
103 	    break;
104 	}
105 
106 	PORT_Assert( nb <= gs->remainder );
107 	if (nb > gs->remainder) {
108 	    /* ssl_DefRecv is misbehaving!  this error is fatal to SSL. */
109 	    gs->state = GS_INIT;         /* so we don't crash next time */
110 	    rv = SECFailure;
111 	    break;
112 	}
113 
114 	gs->offset    += nb;
115 	gs->remainder -= nb;
116 	if (gs->state == GS_DATA)
117 	    gs->inbuf.len += nb;
118 
119 	/* if there's more to go, read some more. */
120 	if (gs->remainder > 0) {
121 	    continue;
122 	}
123 
124 	/* have received entire record header, or entire record. */
125 	switch (gs->state) {
126 	case GS_HEADER:
127 	    /*
128 	    ** Have received SSL3 record header in gs->hdr.
129 	    ** Now extract the length of the following encrypted data,
130 	    ** and then read in the rest of the SSL3 record into gs->inbuf.
131 	    */
132 	    gs->remainder = (gs->hdr[3] << 8) | gs->hdr[4];
133 
134 	    /* This is the max fragment length for an encrypted fragment
135 	    ** plus the size of the record header.
136 	    */
137 	    if(gs->remainder > (MAX_FRAGMENT_LENGTH + 2048 + 5)) {
138 		SSL3_SendAlert(ss, alert_fatal, unexpected_message);
139 		gs->state = GS_INIT;
140 		PORT_SetError(SSL_ERROR_RX_RECORD_TOO_LONG);
141 		return SECFailure;
142 	    }
143 
144 	    gs->state     = GS_DATA;
145 	    gs->offset    = 0;
146 	    gs->inbuf.len = 0;
147 
148 	    if (gs->remainder > gs->inbuf.space) {
149 		err = sslBuffer_Grow(&gs->inbuf, gs->remainder);
150 		if (err) {	/* realloc has set error code to no mem. */
151 		    return err;
152 		}
153 		lbp = gs->inbuf.buf;
154 	    }
155 	    break;	/* End this case.  Continue around the loop. */
156 
157 
158 	case GS_DATA:
159 	    /*
160 	    ** SSL3 record has been completely received.
161 	    */
162 	    gs->state = GS_INIT;
163 	    return 1;
164 	}
165     }
166 
167     return rv;
168 }
169 
170 /* Gather in a record and when complete, Handle that record.
171  * Repeat this until the handshake is complete,
172  * or until application data is available.
173  *
174  * Returns  1 when the handshake is completed without error, or
175  *                 application data is available.
176  * Returns  0 if ssl3_GatherData hits EOF.
177  * Returns -1 on read error, or PR_WOULD_BLOCK_ERROR, or handleRecord error.
178  * Returns -2 on SECWouldBlock return from ssl3_HandleRecord.
179  *
180  * Called from ssl_GatherRecord1stHandshake       in sslcon.c,
181  *    and from SSL_ForceHandshake in sslsecur.c
182  *    and from ssl3_GatherAppDataRecord below (<- DoRecv in sslsecur.c).
183  *
184  * Caller must hold the recv buf lock.
185  */
186 int
ssl3_GatherCompleteHandshake(sslSocket * ss,int flags)187 ssl3_GatherCompleteHandshake(sslSocket *ss, int flags)
188 {
189     SSL3Ciphertext cText;
190     int            rv;
191 
192     PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
193     do {
194 	/* bring in the next sslv3 record. */
195 	rv = ssl3_GatherData(ss, &ss->gs, flags);
196 	if (rv <= 0) {
197 	    return rv;
198 	}
199 
200 	/* decipher it, and handle it if it's a handshake.
201 	 * If it's application data, ss->gs.buf will not be empty upon return.
202 	 */
203 	cText.type    = (SSL3ContentType)ss->gs.hdr[0];
204 	cText.version = (ss->gs.hdr[1] << 8) | ss->gs.hdr[2];
205 	cText.buf     = &ss->gs.inbuf;
206 	rv = ssl3_HandleRecord(ss, &cText, &ss->gs.buf);
207 	if (rv < 0) {
208 	    return ss->recvdCloseNotify ? 0 : rv;
209 	}
210     } while (ss->ssl3.hs.ws != idle_handshake && ss->gs.buf.len == 0);
211 
212     ss->gs.readOffset = 0;
213     ss->gs.writeOffset = ss->gs.buf.len;
214     return 1;
215 }
216 
217 /* Repeatedly gather in a record and when complete, Handle that record.
218  * Repeat this until some application data is received.
219  *
220  * Returns  1 when application data is available.
221  * Returns  0 if ssl3_GatherData hits EOF.
222  * Returns -1 on read error, or PR_WOULD_BLOCK_ERROR, or handleRecord error.
223  * Returns -2 on SECWouldBlock return from ssl3_HandleRecord.
224  *
225  * Called from DoRecv in sslsecur.c
226  * Caller must hold the recv buf lock.
227  */
228 int
ssl3_GatherAppDataRecord(sslSocket * ss,int flags)229 ssl3_GatherAppDataRecord(sslSocket *ss, int flags)
230 {
231     int            rv;
232 
233     PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
234     do {
235 	rv = ssl3_GatherCompleteHandshake(ss, flags);
236     } while (rv > 0 && ss->gs.buf.len == 0);
237 
238     return rv;
239 }
240