Skip to content

Latest commit

 

History

History
296 lines (257 loc) · 8.46 KB

sslencode.c

File metadata and controls

296 lines (257 loc) · 8.46 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is PRIVATE to SSL.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nss.h"
#include "prnetdb.h"
#include "ssl.h"
#include "sslimpl.h"
/* Helper function to encode an unsigned integer into a buffer. */
Aug 1, 2017
Aug 1, 2017
15
16
static void
ssl_EncodeUintX(PRUint8 *to, PRUint64 value, unsigned int bytes)
17
18
19
20
21
22
{
PRUint64 encoded;
PORT_Assert(bytes > 0 && bytes <= sizeof(encoded));
encoded = PR_htonll(value);
Aug 1, 2017
Aug 1, 2017
23
24
PORT_Memcpy(to, ((unsigned char *)(&encoded)) + (sizeof(encoded) - bytes),
bytes);
25
26
27
28
29
30
31
}
/* Grow a buffer to hold newLen bytes of data. When used for recv/xmit buffers,
* the caller must hold xmitBufLock or recvBufLock, as appropriate. */
SECStatus
sslBuffer_Grow(sslBuffer *b, unsigned int newLen)
{
Aug 1, 2017
Aug 1, 2017
32
33
34
35
36
37
38
39
40
if (b->fixed) {
PORT_Assert(newLen <= b->space);
if (newLen > b->space) {
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
return SECFailure;
}
return SECSuccess;
}
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
newLen = PR_MAX(newLen, b->len + 1024);
if (newLen > b->space) {
unsigned char *newBuf;
if (b->buf) {
newBuf = (unsigned char *)PORT_Realloc(b->buf, newLen);
} else {
newBuf = (unsigned char *)PORT_Alloc(newLen);
}
if (!newBuf) {
return SECFailure;
}
b->buf = newBuf;
b->space = newLen;
}
return SECSuccess;
}
SECStatus
sslBuffer_Append(sslBuffer *b, const void *data, unsigned int len)
{
SECStatus rv = sslBuffer_Grow(b, b->len + len);
if (rv != SECSuccess) {
Aug 1, 2017
Aug 1, 2017
63
return SECFailure; /* Code already set. */
Aug 1, 2017
Aug 1, 2017
65
PORT_Memcpy(SSL_BUFFER_NEXT(b), data, len);
66
67
68
69
70
71
72
73
74
b->len += len;
return SECSuccess;
}
SECStatus
sslBuffer_AppendNumber(sslBuffer *b, PRUint64 v, unsigned int size)
{
SECStatus rv = sslBuffer_Grow(b, b->len + size);
if (rv != SECSuccess) {
Aug 1, 2017
Aug 1, 2017
75
return SECFailure;
Aug 1, 2017
Aug 1, 2017
77
ssl_EncodeUintX(SSL_BUFFER_NEXT(b), v, size);
78
79
80
81
82
83
84
85
b->len += size;
return SECSuccess;
}
SECStatus
sslBuffer_AppendVariable(sslBuffer *b, const PRUint8 *data, unsigned int len,
unsigned int size)
{
Aug 1, 2017
Aug 1, 2017
86
87
88
89
90
91
92
93
PORT_Assert(size <= 4 && size > 0);
if (len >= (1ULL << (8 * size))) {
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
return SECFailure;
}
if (sslBuffer_Grow(b, b->len + len + size) != SECSuccess) {
return SECFailure;
Aug 1, 2017
Aug 1, 2017
95
96
ssl_EncodeUintX(SSL_BUFFER_NEXT(b), len, size);
97
b->len += size;
Aug 1, 2017
Aug 1, 2017
98
PORT_Memcpy(SSL_BUFFER_NEXT(b), data, len);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
b->len += len;
return SECSuccess;
}
SECStatus
sslBuffer_AppendBuffer(sslBuffer *b, const sslBuffer *append)
{
return sslBuffer_Append(b, append->buf, append->len);
}
SECStatus
sslBuffer_AppendBufferVariable(sslBuffer *b, const sslBuffer *append,
unsigned int size)
{
return sslBuffer_AppendVariable(b, append->buf, append->len, size);
}
Aug 1, 2017
Aug 1, 2017
116
117
SECStatus
sslBuffer_Skip(sslBuffer *b, unsigned int size, unsigned int *savedOffset)
Aug 1, 2017
Aug 1, 2017
119
120
121
122
123
124
if (sslBuffer_Grow(b, b->len + size) != SECSuccess) {
return SECFailure;
}
if (savedOffset) {
*savedOffset = b->len;
Aug 1, 2017
Aug 1, 2017
126
127
b->len += size;
return SECSuccess;
Aug 1, 2017
Aug 1, 2017
130
131
132
133
134
135
136
137
138
/* A common problem is that a buffer is used to construct a variable length
* structure of unknown length. The length field for that structure is then
* populated afterwards. This function makes this process a little easier.
*
* To use this, before encoding the variable length structure, skip the spot
* where the length would be using sslBuffer_Skip(). After encoding the
* structure, and before encoding anything else, call this function passing the
* value returned from sslBuffer_Skip() as |at| to have the length inserted.
*/
Aug 1, 2017
Aug 1, 2017
140
sslBuffer_InsertLength(sslBuffer *b, unsigned int at, unsigned int size)
Aug 1, 2017
Aug 1, 2017
142
143
144
145
146
147
148
149
150
unsigned int len;
PORT_Assert(b->len >= at + size);
PORT_Assert(b->space >= at + size);
len = b->len - (at + size);
PORT_Assert(size <= 4 && size > 0);
if (len >= (1ULL << (8 * size))) {
PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
151
152
153
return SECFailure;
}
Aug 1, 2017
Aug 1, 2017
154
ssl_EncodeUintX(SSL_BUFFER_BASE(b) + at, len, size);
155
156
157
return SECSuccess;
}
Aug 1, 2017
Aug 1, 2017
158
159
void
sslBuffer_Clear(sslBuffer *b)
Aug 1, 2017
Aug 1, 2017
161
162
163
164
165
166
167
168
if (!b->fixed) {
if (b->buf) {
PORT_Free(b->buf);
b->buf = NULL;
}
b->space = 0;
}
b->len = 0;
169
170
171
}
SECStatus
Aug 1, 2017
Aug 1, 2017
172
ssl3_ConsumeFromItem(SECItem *item, unsigned char **buf, unsigned int size)
May 22, 2017
May 22, 2017
174
if (size > item->len) {
175
176
177
178
179
PORT_SetError(SEC_ERROR_BAD_DATA);
return SECFailure;
}
*buf = item->data;
May 22, 2017
May 22, 2017
180
181
item->data += size;
item->len -= size;
182
183
184
185
return SECSuccess;
}
SECStatus
May 22, 2017
May 22, 2017
186
ssl3_ConsumeNumberFromItem(SECItem *item, PRUint32 *num, unsigned int size)
187
188
189
{
int i;
May 22, 2017
May 22, 2017
190
if (size > item->len || size > sizeof(*num)) {
191
192
193
194
195
PORT_SetError(SEC_ERROR_BAD_DATA);
return SECFailure;
}
*num = 0;
May 22, 2017
May 22, 2017
196
for (i = 0; i < size; i++) {
197
198
199
*num = (*num << 8) + item->data[i];
}
May 22, 2017
May 22, 2017
200
201
item->data += size;
item->len -= size;
202
203
204
return SECSuccess;
}
Aug 1, 2017
Aug 1, 2017
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**************************************************************************
* Append Handshake functions.
* All these functions set appropriate error codes.
* Most rely on ssl3_AppendHandshake to set the error code.
**************************************************************************/
#define MAX_SEND_BUF_LENGTH 32000 /* watch for 16-bit integer overflow */
#define MIN_SEND_BUF_LENGTH 4000
SECStatus
ssl3_AppendHandshake(sslSocket *ss, const void *void_src, unsigned int bytes)
{
unsigned char *src = (unsigned char *)void_src;
int room = ss->sec.ci.sendBuf.space - ss->sec.ci.sendBuf.len;
SECStatus rv;
PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); /* protects sendBuf. */
if (!bytes)
return SECSuccess;
if (ss->sec.ci.sendBuf.space < MAX_SEND_BUF_LENGTH && room < bytes) {
rv = sslBuffer_Grow(&ss->sec.ci.sendBuf, PR_MAX(MIN_SEND_BUF_LENGTH,
PR_MIN(MAX_SEND_BUF_LENGTH, ss->sec.ci.sendBuf.len + bytes)));
if (rv != SECSuccess)
return SECFailure; /* sslBuffer_Grow sets a memory error code. */
room = ss->sec.ci.sendBuf.space - ss->sec.ci.sendBuf.len;
}
PRINT_BUF(60, (ss, "Append to Handshake", (unsigned char *)void_src, bytes));
rv = ssl3_UpdateHandshakeHashes(ss, src, bytes);
if (rv != SECSuccess)
return SECFailure; /* error code set by ssl3_UpdateHandshakeHashes */
while (bytes > room) {
if (room > 0)
PORT_Memcpy(ss->sec.ci.sendBuf.buf + ss->sec.ci.sendBuf.len, src,
room);
ss->sec.ci.sendBuf.len += room;
rv = ssl3_FlushHandshake(ss, ssl_SEND_FLAG_FORCE_INTO_BUFFER);
if (rv != SECSuccess) {
return SECFailure; /* error code set by ssl3_FlushHandshake */
}
bytes -= room;
src += room;
room = ss->sec.ci.sendBuf.space;
PORT_Assert(ss->sec.ci.sendBuf.len == 0);
}
PORT_Memcpy(ss->sec.ci.sendBuf.buf + ss->sec.ci.sendBuf.len, src, bytes);
ss->sec.ci.sendBuf.len += bytes;
return SECSuccess;
}
SECStatus
ssl3_AppendHandshakeNumber(sslSocket *ss, PRUint64 num, unsigned int lenSize)
{
PRUint8 b[sizeof(num)];
SSL_TRC(60, ("%d: number:", SSL_GETPID()));
ssl_EncodeUintX(b, num, lenSize);
return ssl3_AppendHandshake(ss, b, lenSize);
}
SECStatus
ssl3_AppendHandshakeVariable(sslSocket *ss, const PRUint8 *src,
unsigned int bytes, unsigned int lenSize)
{
SECStatus rv;
PORT_Assert((bytes < (1 << 8) && lenSize == 1) ||
(bytes < (1L << 16) && lenSize == 2) ||
(bytes < (1L << 24) && lenSize == 3));
SSL_TRC(60, ("%d: append variable:", SSL_GETPID()));
rv = ssl3_AppendHandshakeNumber(ss, bytes, lenSize);
if (rv != SECSuccess) {
return SECFailure; /* error code set by AppendHandshake. */
}
SSL_TRC(60, ("data:"));
return ssl3_AppendHandshake(ss, src, bytes);
}
SECStatus
ssl3_AppendBufferToHandshake(sslSocket *ss, sslBuffer *buf)
{
return ssl3_AppendHandshake(ss, buf->buf, buf->len);
}
SECStatus
ssl3_AppendBufferToHandshakeVariable(sslSocket *ss, sslBuffer *buf,
unsigned int lenSize)
{
return ssl3_AppendHandshakeVariable(ss, buf->buf, buf->len, lenSize);
}