/src/openssl32/crypto/buffer/buffer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2020 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 | 26.1M | #define LIMIT_BEFORE_EXPANSION 0x5ffffffc |
20 | | |
21 | | BUF_MEM *BUF_MEM_new_ex(unsigned long flags) |
22 | 3.42M | { |
23 | 3.42M | BUF_MEM *ret; |
24 | | |
25 | 3.42M | ret = BUF_MEM_new(); |
26 | 3.42M | if (ret != NULL) |
27 | 3.42M | ret->flags = flags; |
28 | 3.42M | return ret; |
29 | 3.42M | } |
30 | | |
31 | | BUF_MEM *BUF_MEM_new(void) |
32 | 11.8M | { |
33 | 11.8M | BUF_MEM *ret; |
34 | | |
35 | 11.8M | ret = OPENSSL_zalloc(sizeof(*ret)); |
36 | 11.8M | if (ret == NULL) |
37 | 0 | return NULL; |
38 | 11.8M | return ret; |
39 | 11.8M | } |
40 | | |
41 | | void BUF_MEM_free(BUF_MEM *a) |
42 | 35.8M | { |
43 | 35.8M | if (a == NULL) |
44 | 27.8M | return; |
45 | 8.02M | if (a->data != NULL) { |
46 | 3.04M | if (a->flags & BUF_MEM_FLAG_SECURE) |
47 | 561 | OPENSSL_secure_clear_free(a->data, a->max); |
48 | 3.04M | else |
49 | 3.04M | OPENSSL_clear_free(a->data, a->max); |
50 | 3.04M | } |
51 | 8.02M | OPENSSL_free(a); |
52 | 8.02M | } |
53 | | |
54 | | /* Allocate a block of secure memory; copy over old data if there |
55 | | * was any, and then free it. */ |
56 | | static char *sec_alloc_realloc(BUF_MEM *str, size_t len) |
57 | 3.17k | { |
58 | 3.17k | char *ret; |
59 | | |
60 | 3.17k | ret = OPENSSL_secure_malloc(len); |
61 | 3.17k | if (str->data != NULL) { |
62 | 2.61k | if (ret != NULL) { |
63 | 2.61k | memcpy(ret, str->data, str->length); |
64 | 2.61k | OPENSSL_secure_clear_free(str->data, str->length); |
65 | 2.61k | str->data = NULL; |
66 | 2.61k | } |
67 | 2.61k | } |
68 | 3.17k | return ret; |
69 | 3.17k | } |
70 | | |
71 | | size_t BUF_MEM_grow(BUF_MEM *str, size_t len) |
72 | 35.5M | { |
73 | 35.5M | char *ret; |
74 | 35.5M | size_t n; |
75 | | |
76 | 35.5M | if (str->length >= len) { |
77 | 28.3M | str->length = len; |
78 | 28.3M | return len; |
79 | 28.3M | } |
80 | 7.21M | if (str->max >= len) { |
81 | 4.69M | if (str->data != NULL) |
82 | 4.69M | memset(&str->data[str->length], 0, len - str->length); |
83 | 4.69M | str->length = len; |
84 | 4.69M | return len; |
85 | 4.69M | } |
86 | | /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ |
87 | 2.51M | if (len > LIMIT_BEFORE_EXPANSION) { |
88 | 0 | ERR_raise(ERR_LIB_BUF, ERR_R_PASSED_INVALID_ARGUMENT); |
89 | 0 | return 0; |
90 | 0 | } |
91 | 2.51M | n = (len + 3) / 3 * 4; |
92 | 2.51M | if ((str->flags & BUF_MEM_FLAG_SECURE)) |
93 | 0 | ret = sec_alloc_realloc(str, n); |
94 | 2.51M | else |
95 | 2.51M | ret = OPENSSL_realloc(str->data, n); |
96 | 2.51M | if (ret == NULL) { |
97 | 0 | len = 0; |
98 | 2.51M | } else { |
99 | 2.51M | str->data = ret; |
100 | 2.51M | str->max = n; |
101 | 2.51M | memset(&str->data[str->length], 0, len - str->length); |
102 | 2.51M | str->length = len; |
103 | 2.51M | } |
104 | 2.51M | return len; |
105 | 2.51M | } |
106 | | |
107 | | size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len) |
108 | 1.43G | { |
109 | 1.43G | char *ret; |
110 | 1.43G | size_t n; |
111 | | |
112 | 1.43G | if (str->length >= len) { |
113 | 1.21M | if (str->data != NULL) |
114 | 1.21M | memset(&str->data[len], 0, str->length - len); |
115 | 1.21M | str->length = len; |
116 | 1.21M | return len; |
117 | 1.21M | } |
118 | 1.43G | if (str->max >= len) { |
119 | 1.41G | memset(&str->data[str->length], 0, len - str->length); |
120 | 1.41G | str->length = len; |
121 | 1.41G | return len; |
122 | 1.41G | } |
123 | | /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ |
124 | 23.6M | if (len > LIMIT_BEFORE_EXPANSION) { |
125 | 0 | ERR_raise(ERR_LIB_BUF, ERR_R_PASSED_INVALID_ARGUMENT); |
126 | 0 | return 0; |
127 | 0 | } |
128 | 23.6M | n = (len + 3) / 3 * 4; |
129 | 23.6M | if ((str->flags & BUF_MEM_FLAG_SECURE)) |
130 | 3.17k | ret = sec_alloc_realloc(str, n); |
131 | 23.6M | else |
132 | 23.6M | ret = OPENSSL_clear_realloc(str->data, str->max, n); |
133 | 23.6M | if (ret == NULL) { |
134 | 0 | len = 0; |
135 | 23.6M | } else { |
136 | 23.6M | str->data = ret; |
137 | 23.6M | str->max = n; |
138 | 23.6M | memset(&str->data[str->length], 0, len - str->length); |
139 | 23.6M | str->length = len; |
140 | 23.6M | } |
141 | 23.6M | return len; |
142 | 23.6M | } |
143 | | |
144 | | void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size) |
145 | 0 | { |
146 | 0 | size_t i; |
147 | 0 | if (in) { |
148 | 0 | out += size - 1; |
149 | 0 | for (i = 0; i < size; i++) |
150 | 0 | *out-- = *in++; |
151 | 0 | } else { |
152 | 0 | unsigned char *q; |
153 | 0 | char c; |
154 | 0 | q = out + size - 1; |
155 | 0 | for (i = 0; i < size / 2; i++) { |
156 | 0 | c = *q; |
157 | 0 | *q-- = *out; |
158 | 0 | *out++ = c; |
159 | 0 | } |
160 | 0 | } |
161 | 0 | } |