/src/gnutls/lib/nettle/init.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2010-2012 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2022 Tobias Heider <tobias.heider@canonical.com> |
4 | | * |
5 | | * Author: Nikos Mavrogiannopoulos |
6 | | * |
7 | | * This file is part of GnuTLS. |
8 | | * |
9 | | * The GnuTLS is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public License |
11 | | * as published by the Free Software Foundation; either version 2.1 of |
12 | | * the License, or (at your option) any later version. |
13 | | * |
14 | | * This library is distributed in the hope that it will be useful, but |
15 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
21 | | * |
22 | | */ |
23 | | |
24 | | #include "gnutls_int.h" |
25 | | #include "errors.h" |
26 | | #include <num.h> |
27 | | #include <mpi.h> |
28 | | #include <nettle/bignum.h> /* includes gmp.h */ |
29 | | |
30 | | static void gnutls_free_zero(void *data, size_t size); |
31 | | static void *gnutls_realloc_zero(void *data, size_t old_size, size_t new_size); |
32 | | |
33 | | static void *(*allocfunc)(size_t); |
34 | | static void *(*reallocfunc)(void *, size_t, size_t); |
35 | | static void (*freefunc)(void *, size_t); |
36 | | |
37 | | /* Functions that refer to the initialization of the nettle library. |
38 | | */ |
39 | | |
40 | | int gnutls_crypto_init(void) |
41 | 2 | { |
42 | 2 | void *(*defallocfunc)(size_t); |
43 | 2 | void *(*defreallocfunc)(void *, size_t, size_t); |
44 | 2 | void (*deffreefunc)(void *, size_t); |
45 | | |
46 | | /* Check if non-default allocators are being used. |
47 | | * Some applications like guile override GMP allocators |
48 | | * with GC capable alternatives. Do nothing if this is |
49 | | * the case. |
50 | | */ |
51 | 2 | mp_get_memory_functions(&allocfunc, &reallocfunc, &freefunc); |
52 | 2 | mp_set_memory_functions(NULL, NULL, NULL); |
53 | 2 | mp_get_memory_functions(&defallocfunc, &defreallocfunc, &deffreefunc); |
54 | 2 | if (reallocfunc != defreallocfunc || freefunc != deffreefunc) { |
55 | 0 | mp_set_memory_functions(allocfunc, reallocfunc, freefunc); |
56 | 0 | return (0); |
57 | 0 | } |
58 | | |
59 | | /* Overload GMP allocators with safe alternatives */ |
60 | 2 | mp_set_memory_functions(NULL, gnutls_realloc_zero, gnutls_free_zero); |
61 | 2 | return 0; |
62 | 2 | } |
63 | | |
64 | | /* Functions that refer to the deinitialization of the nettle library. |
65 | | */ |
66 | | |
67 | | void gnutls_crypto_deinit(void) |
68 | 0 | { |
69 | 0 | mp_set_memory_functions(allocfunc, reallocfunc, freefunc); |
70 | 0 | } |
71 | | |
72 | | /*- |
73 | | * gnutls_free_zero: |
74 | | * @data: the memory to free |
75 | | * @size: the size of memory |
76 | | * |
77 | | * This function will operate similarly to free(), but will safely |
78 | | * zeroize the memory pointed to by data before freeing. |
79 | | * |
80 | | -*/ |
81 | | static void gnutls_free_zero(void *data, size_t size) |
82 | 0 | { |
83 | 0 | explicit_bzero(data, size); |
84 | 0 | free(data); |
85 | 0 | } |
86 | | |
87 | | /*- |
88 | | * gnutls_realloc_zero: |
89 | | * @data: the memory to free |
90 | | * @old_size: the size of memory before reallocation |
91 | | * @new_size: the size of memory after reallocation |
92 | | * |
93 | | * This function will operate similarly to realloc(), but will safely |
94 | | * zeroize discarded memory. |
95 | | * |
96 | | -*/ |
97 | | static void *gnutls_realloc_zero(void *data, size_t old_size, size_t new_size) |
98 | 0 | { |
99 | 0 | void *p; |
100 | |
|
101 | 0 | if (data == NULL || old_size == 0) { |
102 | 0 | p = realloc(data, new_size); |
103 | 0 | if (p == NULL) |
104 | 0 | abort(); |
105 | 0 | return p; |
106 | 0 | } |
107 | | |
108 | 0 | if (new_size == 0) { |
109 | 0 | explicit_bzero(data, old_size); |
110 | 0 | free(data); |
111 | 0 | return NULL; |
112 | 0 | } |
113 | | |
114 | 0 | if (old_size == new_size) |
115 | 0 | return data; |
116 | | |
117 | 0 | p = malloc(new_size); |
118 | 0 | if (p == NULL) { |
119 | 0 | explicit_bzero(data, old_size); |
120 | 0 | abort(); |
121 | 0 | } |
122 | 0 | memcpy(p, data, MIN(old_size, new_size)); |
123 | 0 | explicit_bzero(data, old_size); |
124 | 0 | free(data); |
125 | |
|
126 | 0 | return p; |
127 | 0 | } |