fixing incorrect OID of signingCertificateV2
[openssl.git] / crypto / mem.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <stdlib.h>
12 #include <limits.h>
13 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
15 #include "internal/cryptlib_int.h"
16 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
17 # include <execinfo.h>
18 #endif
19
20 /*
21  * the following pointers may be changed as long as 'allow_customize' is set
22  */
23 static int allow_customize = 1;
24
25 static void *(*malloc_impl)(size_t, const char *, int)
26     = CRYPTO_malloc;
27 static void *(*realloc_impl)(void *, size_t, const char *, int)
28     = CRYPTO_realloc;
29 static void (*free_impl)(void *, const char *, int)
30     = CRYPTO_free;
31
32 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
33 static char *md_failstring;
34 static long md_count;
35 static int md_fail_percent = 0;
36 static int md_tracefd = -1;
37 static int call_malloc_debug = 1;
38
39 static void parseit(void);
40 static int shouldfail(void);
41
42 # define FAILTEST() if (shouldfail()) return NULL
43
44 #else
45 static int call_malloc_debug = 0;
46
47 # define FAILTEST() /* empty */
48 #endif
49
50 int CRYPTO_set_mem_functions(
51         void *(*m)(size_t, const char *, int),
52         void *(*r)(void *, size_t, const char *, int),
53         void (*f)(void *, const char *, int))
54 {
55     if (!allow_customize)
56         return 0;
57     if (m)
58         malloc_impl = m;
59     if (r)
60         realloc_impl = r;
61     if (f)
62         free_impl = f;
63     return 1;
64 }
65
66 int CRYPTO_set_mem_debug(int flag)
67 {
68     if (!allow_customize)
69         return 0;
70     call_malloc_debug = flag;
71     return 1;
72 }
73
74 void CRYPTO_get_mem_functions(
75         void *(**m)(size_t, const char *, int),
76         void *(**r)(void *, size_t, const char *, int),
77         void (**f)(void *, const char *, int))
78 {
79     if (m != NULL)
80         *m = malloc_impl;
81     if (r != NULL)
82         *r = realloc_impl;
83     if (f != NULL)
84         *f = free_impl;
85 }
86
87 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
88 /*
89  * Parse a "malloc failure spec" string.  This likes like a set of fields
90  * separated by semicolons.  Each field has a count and an optional failure
91  * percentage.  For example:
92  *          100@0;100@25;0@0
93  *    or    100;100@25;0
94  * This means 100 mallocs succeed, then next 100 fail 25% of the time, and
95  * all remaining (count is zero) succeed.
96  */
97 static void parseit(void)
98 {
99     char *semi = strchr(md_failstring, ';');
100     char *atsign;
101
102     if (semi != NULL)
103         *semi++ = '\0';
104
105     /* Get the count (atol will stop at the @ if there), and percentage */
106     md_count = atol(md_failstring);
107     atsign = strchr(md_failstring, '@');
108     md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
109
110     if (semi != NULL)
111         md_failstring = semi;
112 }
113
114 /*
115  * See if the current malloc should fail.
116  */
117 static int shouldfail(void)
118 {
119     int roll = (int)(random() % 100);
120     int shoulditfail = roll < md_fail_percent;
121     char buff[80];
122
123     if (md_tracefd > 0) {
124         BIO_snprintf(buff, sizeof(buff),
125                      "%c C%ld %%%d R%d\n",
126                      shoulditfail ? '-' : '+', md_count, md_fail_percent, roll);
127         write(md_tracefd, buff, strlen(buff));
128 #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
129         if (shoulditfail) {
130             void *addrs[30];
131             int num = backtrace(addrs, OSSL_NELEM(addrs));
132
133             backtrace_symbols_fd(addrs, num, md_tracefd);
134         }
135 #endif
136     }
137
138     if (md_count) {
139         /* If we used up this one, go to the next. */
140         if (--md_count == 0)
141             parseit();
142     }
143
144     return shoulditfail;
145 }
146
147 void ossl_malloc_setup_failures(void)
148 {
149     const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
150
151     if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
152         parseit();
153     if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
154         md_tracefd = atoi(cp);
155 }
156 #endif
157
158 void *CRYPTO_malloc(size_t num, const char *file, int line)
159 {
160     void *ret = NULL;
161
162     if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
163         return malloc_impl(num, file, line);
164
165     if (num == 0)
166         return NULL;
167
168     FAILTEST();
169     allow_customize = 0;
170 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
171     if (call_malloc_debug) {
172         CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
173         ret = malloc(num);
174         CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
175     } else {
176         ret = malloc(num);
177     }
178 #else
179     osslargused(file); osslargused(line);
180     ret = malloc(num);
181 #endif
182
183     return ret;
184 }
185
186 void *CRYPTO_zalloc(size_t num, const char *file, int line)
187 {
188     void *ret = CRYPTO_malloc(num, file, line);
189
190     FAILTEST();
191     if (ret != NULL)
192         memset(ret, 0, num);
193     return ret;
194 }
195
196 void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
197 {
198     if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
199         return realloc_impl(str, num, file, line);
200
201     FAILTEST();
202     if (str == NULL)
203         return CRYPTO_malloc(num, file, line);
204
205     if (num == 0) {
206         CRYPTO_free(str, file, line);
207         return NULL;
208     }
209
210     allow_customize = 0;
211 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
212     if (call_malloc_debug) {
213         void *ret;
214         CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
215         ret = realloc(str, num);
216         CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
217         return ret;
218     }
219 #else
220     osslargused(file); osslargused(line);
221 #endif
222     return realloc(str, num);
223
224 }
225
226 void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
227                            const char *file, int line)
228 {
229     void *ret = NULL;
230
231     if (str == NULL)
232         return CRYPTO_malloc(num, file, line);
233
234     if (num == 0) {
235         CRYPTO_clear_free(str, old_len, file, line);
236         return NULL;
237     }
238
239     /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
240     if (num < old_len) {
241         OPENSSL_cleanse((char*)str + num, old_len - num);
242         return str;
243     }
244
245     ret = CRYPTO_malloc(num, file, line);
246     if (ret != NULL) {
247         memcpy(ret, str, old_len);
248         CRYPTO_clear_free(str, old_len, file, line);
249     }
250     return ret;
251 }
252
253 void CRYPTO_free(void *str, const char *file, int line)
254 {
255     if (free_impl != NULL && free_impl != &CRYPTO_free) {
256         free_impl(str, file, line);
257         return;
258     }
259
260 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
261     if (call_malloc_debug) {
262         CRYPTO_mem_debug_free(str, 0, file, line);
263         free(str);
264         CRYPTO_mem_debug_free(str, 1, file, line);
265     } else {
266         free(str);
267     }
268 #else
269     free(str);
270 #endif
271 }
272
273 void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
274 {
275     if (str == NULL)
276         return;
277     if (num)
278         OPENSSL_cleanse(str, num);
279     CRYPTO_free(str, file, line);
280 }