/src/openssl/crypto/buffer/buffer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/buffer.h> |
13 | | |
14 | | /* |
15 | | * LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That |
16 | | * function is applied in several functions in this file and this limit |
17 | | * ensures that the result fits in an int. |
18 | | */ |
19 | 562k | #define LIMIT_BEFORE_EXPANSION 0x5ffffffc |
20 | | |
21 | | BUF_MEM *BUF_MEM_new_ex(unsigned long flags) |
22 | 0 | { |
23 | 0 | BUF_MEM *ret; |
24 | |
|
25 | 0 | ret = BUF_MEM_new(); |
26 | 0 | if (ret != NULL) |
27 | 0 | ret->flags = flags; |
28 | 0 | return ret; |
29 | 0 | } |
30 | | |
31 | | BUF_MEM *BUF_MEM_new(void) |
32 | 386k | { |
33 | 386k | BUF_MEM *ret; |
34 | | |
35 | 386k | ret = OPENSSL_zalloc(sizeof(*ret)); |
36 | 386k | if (ret == NULL) { |
37 | 0 | BUFerr(BUF_F_BUF_MEM_NEW, ERR_R_MALLOC_FAILURE); |
38 | 0 | return NULL; |
39 | 0 | } |
40 | 386k | return ret; |
41 | 386k | } |
42 | | |
43 | | void BUF_MEM_free(BUF_MEM *a) |
44 | 328k | { |
45 | 328k | if (a == NULL) |
46 | 0 | return; |
47 | 328k | if (a->data != NULL) { |
48 | 164k | if (a->flags & BUF_MEM_FLAG_SECURE) |
49 | 0 | OPENSSL_secure_clear_free(a->data, a->max); |
50 | 164k | else |
51 | 164k | OPENSSL_clear_free(a->data, a->max); |
52 | 164k | } |
53 | 328k | OPENSSL_free(a); |
54 | 328k | } |
55 | | |
56 | | /* Allocate a block of secure memory; copy over old data if there |
57 | | * was any, and then free it. */ |
58 | | static char *sec_alloc_realloc(BUF_MEM *str, size_t len) |
59 | 0 | { |
60 | 0 | char *ret; |
61 | |
|
62 | 0 | ret = OPENSSL_secure_malloc(len); |
63 | 0 | if (str->data != NULL) { |
64 | 0 | if (ret != NULL) { |
65 | 0 | memcpy(ret, str->data, str->length); |
66 | 0 | OPENSSL_secure_clear_free(str->data, str->length); |
67 | 0 | str->data = NULL; |
68 | 0 | } |
69 | 0 | } |
70 | 0 | return ret; |
71 | 0 | } |
72 | | |
73 | | size_t BUF_MEM_grow(BUF_MEM *str, size_t len) |
74 | 368k | { |
75 | 368k | char *ret; |
76 | 368k | size_t n; |
77 | | |
78 | 368k | if (str->length >= len) { |
79 | 21.1k | str->length = len; |
80 | 21.1k | return len; |
81 | 21.1k | } |
82 | 347k | if (str->max >= len) { |
83 | 120k | if (str->data != NULL) |
84 | 120k | memset(&str->data[str->length], 0, len - str->length); |
85 | 120k | str->length = len; |
86 | 120k | return len; |
87 | 120k | } |
88 | | /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ |
89 | 227k | if (len > LIMIT_BEFORE_EXPANSION) { |
90 | 0 | BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE); |
91 | 0 | return 0; |
92 | 0 | } |
93 | 227k | n = (len + 3) / 3 * 4; |
94 | 227k | if ((str->flags & BUF_MEM_FLAG_SECURE)) |
95 | 0 | ret = sec_alloc_realloc(str, n); |
96 | 227k | else |
97 | 227k | ret = OPENSSL_realloc(str->data, n); |
98 | 227k | if (ret == NULL) { |
99 | 0 | BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE); |
100 | 0 | len = 0; |
101 | 227k | } else { |
102 | 227k | str->data = ret; |
103 | 227k | str->max = n; |
104 | 227k | memset(&str->data[str->length], 0, len - str->length); |
105 | 227k | str->length = len; |
106 | 227k | } |
107 | 227k | return len; |
108 | 227k | } |
109 | | |
110 | | size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len) |
111 | 535k | { |
112 | 535k | char *ret; |
113 | 535k | size_t n; |
114 | | |
115 | 535k | if (str->length >= len) { |
116 | 0 | if (str->data != NULL) |
117 | 0 | memset(&str->data[len], 0, str->length - len); |
118 | 0 | str->length = len; |
119 | 0 | return len; |
120 | 0 | } |
121 | 535k | if (str->max >= len) { |
122 | 200k | memset(&str->data[str->length], 0, len - str->length); |
123 | 200k | str->length = len; |
124 | 200k | return len; |
125 | 200k | } |
126 | | /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ |
127 | 334k | if (len > LIMIT_BEFORE_EXPANSION) { |
128 | 0 | BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE); |
129 | 0 | return 0; |
130 | 0 | } |
131 | 334k | n = (len + 3) / 3 * 4; |
132 | 334k | if ((str->flags & BUF_MEM_FLAG_SECURE)) |
133 | 0 | ret = sec_alloc_realloc(str, n); |
134 | 334k | else |
135 | 334k | ret = OPENSSL_clear_realloc(str->data, str->max, n); |
136 | 334k | if (ret == NULL) { |
137 | 0 | BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE); |
138 | 0 | len = 0; |
139 | 334k | } else { |
140 | 334k | str->data = ret; |
141 | 334k | str->max = n; |
142 | 334k | memset(&str->data[str->length], 0, len - str->length); |
143 | 334k | str->length = len; |
144 | 334k | } |
145 | 334k | return len; |
146 | 334k | } |
147 | | |
148 | | void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size) |
149 | 0 | { |
150 | 0 | size_t i; |
151 | 0 | if (in) { |
152 | 0 | out += size - 1; |
153 | 0 | for (i = 0; i < size; i++) |
154 | 0 | *out-- = *in++; |
155 | 0 | } else { |
156 | 0 | unsigned char *q; |
157 | 0 | char c; |
158 | 0 | q = out + size - 1; |
159 | 0 | for (i = 0; i < size / 2; i++) { |
160 | 0 | c = *q; |
161 | 0 | *q-- = *out; |
162 | 0 | *out++ = c; |
163 | 0 | } |
164 | 0 | } |
165 | 0 | } |