GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/bio/bss_mem.c Lines: 97 117 82.9 %
Date: 2017-11-13 Branches: 48 67 71.6 %

Line Branch Exec Source
1
/* $OpenBSD: bss_mem.c,v 1.15 2017/01/29 17:49:22 beck Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
59
#include <errno.h>
60
#include <stdio.h>
61
#include <string.h>
62
63
#include <openssl/bio.h>
64
#include <openssl/err.h>
65
#include <openssl/buffer.h>
66
67
static int mem_write(BIO *h, const char *buf, int num);
68
static int mem_read(BIO *h, char *buf, int size);
69
static int mem_puts(BIO *h, const char *str);
70
static int mem_gets(BIO *h, char *str, int size);
71
static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
72
static int mem_new(BIO *h);
73
static int mem_free(BIO *data);
74
75
static BIO_METHOD mem_method = {
76
	.type = BIO_TYPE_MEM,
77
	.name = "memory buffer",
78
	.bwrite = mem_write,
79
	.bread = mem_read,
80
	.bputs = mem_puts,
81
	.bgets = mem_gets,
82
	.ctrl = mem_ctrl,
83
	.create = mem_new,
84
	.destroy = mem_free
85
};
86
87
/* bio->num is used to hold the value to return on 'empty', if it is
88
 * 0, should_retry is not set */
89
90
BIO_METHOD *
91
BIO_s_mem(void)
92
{
93
36492
	return (&mem_method);
94
}
95
96
BIO *
97
BIO_new_mem_buf(void *buf, int len)
98
{
99
	BIO *ret;
100
	BUF_MEM *b;
101
	size_t sz;
102
103
824
	if (!buf) {
104
		BIOerror(BIO_R_NULL_PARAMETER);
105
		return NULL;
106
	}
107
1236
	sz = (len < 0) ? strlen(buf) : (size_t)len;
108
412
	if (!(ret = BIO_new(BIO_s_mem())))
109
		return NULL;
110
412
	b = (BUF_MEM *)ret->ptr;
111
412
	b->data = buf;
112
412
	b->length = sz;
113
412
	b->max = sz;
114
412
	ret->flags |= BIO_FLAGS_MEM_RDONLY;
115
	/* Since this is static data retrying wont help */
116
412
	ret->num = 0;
117
412
	return ret;
118
412
}
119
120
static int
121
mem_new(BIO *bi)
122
{
123
	BUF_MEM *b;
124
125
36492
	if ((b = BUF_MEM_new()) == NULL)
126
		return (0);
127
18246
	bi->shutdown = 1;
128
18246
	bi->init = 1;
129
18246
	bi->num = -1;
130
18246
	bi->ptr = (char *)b;
131
18246
	return (1);
132
18246
}
133
134
static int
135
mem_free(BIO *a)
136
{
137
36474
	if (a == NULL)
138
		return (0);
139
18237
	if (a->shutdown) {
140

36474
		if ((a->init) && (a->ptr != NULL)) {
141
			BUF_MEM *b;
142
18237
			b = (BUF_MEM *)a->ptr;
143
18237
			if (a->flags & BIO_FLAGS_MEM_RDONLY)
144
422
				b->data = NULL;
145
18237
			BUF_MEM_free(b);
146
18237
			a->ptr = NULL;
147
18237
		}
148
	}
149
18237
	return (1);
150
18237
}
151
152
static int
153
mem_read(BIO *b, char *out, int outl)
154
{
155
	int ret = -1;
156
	BUF_MEM *bm;
157
158
32458
	bm = (BUF_MEM *)b->ptr;
159
16229
	BIO_clear_retry_flags(b);
160

64916
	ret = (outl >=0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
161
16229
	if ((out != NULL) && (ret > 0)) {
162
15467
		memcpy(out, bm->data, ret);
163
15467
		bm->length -= ret;
164
15467
		if (b->flags & BIO_FLAGS_MEM_RDONLY)
165
2653
			bm->data += ret;
166
		else {
167
12814
			memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
168
		}
169
762
	} else if (bm->length == 0) {
170
762
		ret = b->num;
171
762
		if (ret != 0)
172
432
			BIO_set_retry_read(b);
173
	}
174
16229
	return (ret);
175
}
176
177
static int
178
mem_write(BIO *b, const char *in, int inl)
179
{
180
	int ret = -1;
181
	int blen;
182
	BUF_MEM *bm;
183
184
135846
	bm = (BUF_MEM *)b->ptr;
185
67923
	if (in == NULL) {
186
		BIOerror(BIO_R_NULL_PARAMETER);
187
		goto end;
188
	}
189
190
67923
	if (b->flags & BIO_FLAGS_MEM_RDONLY) {
191
		BIOerror(BIO_R_WRITE_TO_READ_ONLY_BIO);
192
		goto end;
193
	}
194
195
67923
	BIO_clear_retry_flags(b);
196
67923
	blen = bm->length;
197
67923
	if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
198
		goto end;
199
67923
	memcpy(&(bm->data[blen]), in, inl);
200
67923
	ret = inl;
201
end:
202
67923
	return (ret);
203
}
204
205
static long
206
mem_ctrl(BIO *b, int cmd, long num, void *ptr)
207
{
208
	long ret = 1;
209
	char **pptr;
210
211
76174
	BUF_MEM *bm = (BUF_MEM *)b->ptr;
212
213



38087
	switch (cmd) {
214
	case BIO_CTRL_RESET:
215
14
		if (bm->data != NULL) {
216
			/* For read only case reset to the start again */
217
14
			if (b->flags & BIO_FLAGS_MEM_RDONLY) {
218
12
				bm->data -= bm->max - bm->length;
219
12
				bm->length = bm->max;
220
12
			} else {
221
2
				memset(bm->data, 0, bm->max);
222
				bm->length = 0;
223
			}
224
14
		}
225
		break;
226
	case BIO_CTRL_EOF:
227
		ret = (long)(bm->length == 0);
228
		break;
229
	case BIO_C_SET_BUF_MEM_EOF_RETURN:
230
20
		b->num = (int)num;
231
20
		break;
232
	case BIO_CTRL_INFO:
233
17907
		ret = (long)bm->length;
234
17907
		if (ptr != NULL) {
235
17905
			pptr = (char **)ptr;
236
17905
			*pptr = (char *)&(bm->data[0]);
237
17905
		}
238
		break;
239
	case BIO_C_SET_BUF_MEM:
240
		mem_free(b);
241
		b->shutdown = (int)num;
242
		b->ptr = ptr;
243
		break;
244
	case BIO_C_GET_BUF_MEM_PTR:
245
12
		if (ptr != NULL) {
246
12
			pptr = (char **)ptr;
247
12
			*pptr = (char *)bm;
248
12
		}
249
		break;
250
	case BIO_CTRL_GET_CLOSE:
251
		ret = (long)b->shutdown;
252
		break;
253
	case BIO_CTRL_SET_CLOSE:
254
16102
		b->shutdown = (int)num;
255
16102
		break;
256
257
	case BIO_CTRL_WPENDING:
258
		ret = 0L;
259
147
		break;
260
	case BIO_CTRL_PENDING:
261
1728
		ret = (long)bm->length;
262
1728
		break;
263
	case BIO_CTRL_DUP:
264
	case BIO_CTRL_FLUSH:
265
		ret = 1;
266
536
		break;
267
	case BIO_CTRL_PUSH:
268
	case BIO_CTRL_POP:
269
	default:
270
		ret = 0;
271
1621
		break;
272
	}
273
38087
	return (ret);
274
}
275
276
static int
277
mem_gets(BIO *bp, char *buf, int size)
278
{
279
	int i, j;
280
	int ret = -1;
281
	char *p;
282
24572
	BUF_MEM *bm = (BUF_MEM *)bp->ptr;
283
284
12286
	BIO_clear_retry_flags(bp);
285
12286
	j = bm->length;
286
12286
	if ((size - 1) < j)
287
7163
		j = size - 1;
288
12286
	if (j <= 0) {
289
33
		*buf = '\0';
290
33
		return 0;
291
	}
292
12253
	p = bm->data;
293
1345914
	for (i = 0; i < j; i++) {
294
672948
		if (p[i] == '\n') {
295
			i++;
296
			break;
297
		}
298
	}
299
300
	/*
301
	 * i is now the max num of bytes to copy, either j or up to
302
	 * and including the first newline
303
	 */
304
305
12253
	i = mem_read(bp, buf, i);
306
12253
	if (i > 0)
307
12253
		buf[i] = '\0';
308
	ret = i;
309
12253
	return (ret);
310
12286
}
311
312
static int
313
mem_puts(BIO *bp, const char *str)
314
{
315
	int n, ret;
316
317
	n = strlen(str);
318
	ret = mem_write(bp, str, n);
319
	/* memory semantics is that it will always work */
320
	return (ret);
321
}