Wipe (some) secrets from memory after use
[tinc] / src / gcrypt / rsa.c
1 /*
2     rsa.c -- RSA key handling
3     Copyright (C) 2007-2022 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "../system.h"
21
22 #include <gcrypt.h>
23
24 #include "pem.h"
25
26 #include "rsa.h"
27 #include "../logger.h"
28 #include "../rsa.h"
29 #include "../xalloc.h"
30
31 // BER decoding functions
32
33 static int ber_read_id(unsigned char **p, size_t *buflen) {
34         if(*buflen <= 0) {
35                 return -1;
36         }
37
38         if((**p & 0x1f) == 0x1f) {
39                 int id = 0;
40                 bool more;
41
42                 while(*buflen > 0) {
43                         id <<= 7;
44                         id |= **p & 0x7f;
45                         more = *(*p)++ & 0x80;
46                         (*buflen)--;
47
48                         if(!more) {
49                                 break;
50                         }
51                 }
52
53                 return id;
54         } else {
55                 (*buflen)--;
56                 return *(*p)++ & 0x1f;
57         }
58 }
59
60 static size_t ber_read_len(unsigned char **p, size_t *buflen) {
61         if(*buflen <= 0) {
62                 return -1;
63         }
64
65         if(**p & 0x80) {
66                 size_t result = 0;
67                 size_t len = *(*p)++ & 0x7f;
68                 (*buflen)--;
69
70                 if(len > *buflen) {
71                         return 0;
72                 }
73
74                 for(; len; --len) {
75                         result = (size_t)(result << 8);
76                         result |= *(*p)++;
77                         (*buflen)--;
78                 }
79
80                 return result;
81         } else {
82                 (*buflen)--;
83                 return *(*p)++;
84         }
85 }
86
87
88 static bool ber_read_sequence(unsigned char **p, size_t *buflen, size_t *result) {
89         int tag = ber_read_id(p, buflen);
90         size_t len = ber_read_len(p, buflen);
91
92         if(tag == 0x10) {
93                 if(result) {
94                         *result = len;
95                 }
96
97                 return true;
98         } else {
99                 return false;
100         }
101 }
102
103 static bool ber_read_mpi(unsigned char **p, size_t *buflen, gcry_mpi_t *mpi) {
104         int tag = ber_read_id(p, buflen);
105         size_t len = ber_read_len(p, buflen);
106         gcry_error_t err = 0;
107
108         if(tag != 0x02 || len > *buflen) {
109                 return false;
110         }
111
112         if(mpi) {
113                 err = gcry_mpi_scan(mpi, GCRYMPI_FMT_USG, *p, len, NULL);
114         }
115
116         *p += len;
117         *buflen -= len;
118
119         return mpi ? !err : true;
120 }
121
122 rsa_t *rsa_set_hex_public_key(const char *n, const char *e) {
123         rsa_t *rsa = xzalloc(sizeof(rsa_t));
124
125         gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL);
126
127         if(!err) {
128                 err = gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
129         }
130
131         if(err) {
132                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
133                 rsa_free(rsa);
134                 return false;
135         }
136
137         return rsa;
138 }
139
140 rsa_t *rsa_set_hex_private_key(const char *n, const char *e, const char *d) {
141         rsa_t *rsa = xzalloc(sizeof(rsa_t));
142
143         gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL);
144
145         if(!err) {
146                 err = gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
147         }
148
149         if(!err) {
150                 err = gcry_mpi_scan(&rsa->d, GCRYMPI_FMT_HEX, d, 0, NULL);
151         }
152
153         if(err) {
154                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
155                 rsa_free(rsa);
156                 return NULL;
157         }
158
159         return rsa;
160 }
161
162 // Read PEM RSA keys
163
164 rsa_t *rsa_read_pem_public_key(FILE *fp) {
165         uint8_t derbuf[8096], *derp = derbuf;
166         size_t derlen;
167
168         if(!pem_decode(fp, "RSA PUBLIC KEY", derbuf, sizeof(derbuf), &derlen)) {
169                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA public key: %s", strerror(errno));
170                 return NULL;
171         }
172
173         rsa_t *rsa = xzalloc(sizeof(rsa_t));
174
175         if(!ber_read_sequence(&derp, &derlen, NULL)
176                         || !ber_read_mpi(&derp, &derlen, &rsa->n)
177                         || !ber_read_mpi(&derp, &derlen, &rsa->e)
178                         || derlen) {
179                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA public key");
180                 rsa_free(rsa);
181                 return NULL;
182         }
183
184         return rsa;
185 }
186
187 rsa_t *rsa_read_pem_private_key(FILE *fp) {
188         uint8_t derbuf[8096], *derp = derbuf;
189         size_t derlen;
190
191         if(!pem_decode(fp, "RSA PRIVATE KEY", derbuf, sizeof(derbuf), &derlen)) {
192                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA private key: %s", strerror(errno));
193                 return NULL;
194         }
195
196         rsa_t *rsa = xzalloc(sizeof(rsa_t));
197
198         if(!ber_read_sequence(&derp, &derlen, NULL)
199                         || !ber_read_mpi(&derp, &derlen, NULL)
200                         || !ber_read_mpi(&derp, &derlen, &rsa->n)
201                         || !ber_read_mpi(&derp, &derlen, &rsa->e)
202                         || !ber_read_mpi(&derp, &derlen, &rsa->d)
203                         || !ber_read_mpi(&derp, &derlen, NULL) // p
204                         || !ber_read_mpi(&derp, &derlen, NULL) // q
205                         || !ber_read_mpi(&derp, &derlen, NULL)
206                         || !ber_read_mpi(&derp, &derlen, NULL)
207                         || !ber_read_mpi(&derp, &derlen, NULL) // u
208                         || derlen) {
209                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA private key");
210                 rsa_free(rsa);
211                 rsa = NULL;
212         }
213
214         memzero(derbuf, sizeof(derbuf));
215         return rsa;
216 }
217
218 size_t rsa_size(const rsa_t *rsa) {
219         return (gcry_mpi_get_nbits(rsa->n) + 7) / 8;
220 }
221
222 static bool check(gcry_error_t err) {
223         if(err) {
224                 logger(DEBUG_ALWAYS, LOG_ERR, "gcrypt error %s/%s", gcry_strsource(err), gcry_strerror(err));
225         }
226
227         return !err;
228 }
229
230 /* Well, libgcrypt has functions to handle RSA keys, but they suck.
231  * So we just use libgcrypt's mpi functions, and do the math ourselves.
232  */
233
234 static bool rsa_powm(const gcry_mpi_t ed, const gcry_mpi_t n, const void *in, size_t len, void *out) {
235         gcry_mpi_t inmpi = NULL;
236
237         if(!check(gcry_mpi_scan(&inmpi, GCRYMPI_FMT_USG, in, len, NULL))) {
238                 return false;
239         }
240
241         gcry_mpi_t outmpi = gcry_mpi_snew(len * 8);
242         gcry_mpi_powm(outmpi, inmpi, ed, n);
243
244         size_t out_bytes = (gcry_mpi_get_nbits(outmpi) + 7) / 8;
245         size_t pad = len - MIN(out_bytes, len);
246         unsigned char *pout = out;
247
248         for(; pad; --pad) {
249                 *pout++ = 0;
250         }
251
252         bool ok = check(gcry_mpi_print(GCRYMPI_FMT_USG, pout, len, NULL, outmpi));
253
254         gcry_mpi_release(outmpi);
255         gcry_mpi_release(inmpi);
256
257         return ok;
258 }
259
260 bool rsa_public_encrypt(rsa_t *rsa, const void *in, size_t len, void *out) {
261         return rsa_powm(rsa->e, rsa->n, in, len, out);
262 }
263
264 bool rsa_private_decrypt(rsa_t *rsa, const void *in, size_t len, void *out) {
265         return rsa_powm(rsa->d, rsa->n, in, len, out);
266 }
267
268 void rsa_free(rsa_t *rsa) {
269         if(rsa) {
270                 if(rsa->n) {
271                         gcry_mpi_release(rsa->n);
272                 }
273
274                 if(rsa->e) {
275                         gcry_mpi_release(rsa->e);
276                 }
277
278                 if(rsa->d) {
279                         gcry_mpi_release(rsa->d);
280                 }
281
282                 free(rsa);
283         }
284 }