Drop localisation and checkpoint tracing in files not covered by the merge.
[tinc] / src / gcrypt / rsa.c
1 /*
2     rsa.c -- RSA key handling
3     Copyright (C) 2007 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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include "system.h"
23
24 #include <gcrypt.h>
25
26 #include "logger.h"
27 #include "rsa.h"
28
29 // Base64 decoding table
30
31 static const uint8_t b64d[128] = {
32   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
34   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
35   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39   0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
40   0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
41   0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff,
42   0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
43   0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
44   0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
45   0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
46   0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
47   0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
48   0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
49   0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
50   0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
51   0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
52   0x31, 0x32, 0x33, 0xff, 0xff, 0xff,
53   0xff, 0xff
54 };
55
56 // PEM encoding/decoding functions
57
58 static bool pem_decode(FILE *fp, const char *header, uint8_t *buf, size_t size, size_t *outsize) {
59         bool decode = false;
60         char line[1024];
61         uint16_t word = 0;
62         int shift = 10;
63         size_t i, j = 0;
64
65         while(!feof(fp)) {
66                 if(!fgets(line, sizeof line, fp))
67                         return false;
68
69                 if(!decode && !strncmp(line, "-----BEGIN ", 11)) {
70                         if(!strncmp(line + 11, header, strlen(header)))
71                                 decode = true;
72                         continue;
73                 }
74
75                 if(decode && !strncmp(line, "-----END", 8)) {
76                         break;
77                 }
78
79                 if(!decode)
80                         continue;
81
82                 for(i = 0; line[i] >= ' '; i++) {
83                         if((signed char)line[i] < 0 || b64d[(int)line[i]] == 0xff)
84                                 break;
85                         word |= b64d[(int)line[i]] << shift;
86                         shift -= 6;
87                         if(shift <= 2) {
88                                 if(j > size) {
89                                         errno = ENOMEM;
90                                         return false;
91                                 }
92
93                                 buf[j++] = word >> 8;
94                                 word <<= 8;
95                                 shift += 8;
96                         }
97                 }
98         }
99
100         if(outsize)
101                 *outsize = j;
102         return true;
103 }
104
105
106 // BER decoding functions
107
108 static int ber_read_id(unsigned char **p, size_t *buflen) {
109         if(*buflen <= 0)
110                 return -1;
111
112         if((**p & 0x1f) == 0x1f) {
113                 int id = 0;
114                 bool more;
115                 while(*buflen > 0) {
116                         id <<= 7;
117                         id |= **p & 0x7f;
118                         more = *(*p)++ & 0x80;
119                         (*buflen)--;
120                         if(!more)
121                                 break;
122                 }
123                 return id;
124         } else {
125                 (*buflen)--;
126                 return *(*p)++ & 0x1f;
127         }
128 }
129
130 static size_t ber_read_len(unsigned char **p, size_t *buflen) {
131         if(*buflen <= 0)
132                 return -1;
133
134         if(**p & 0x80) {
135                 size_t result = 0;
136                 int len = *(*p)++ & 0x7f;
137                 (*buflen)--;
138                 if(len > *buflen)
139                         return 0;
140
141                 while(len--) {
142                         result <<= 8;
143                         result |= *(*p)++;
144                         (*buflen)--;
145                 }
146
147                 return result;
148         } else {
149                 (*buflen)--;
150                 return *(*p)++;
151         }
152 }
153         
154
155 static bool ber_read_sequence(unsigned char **p, size_t *buflen, size_t *result) {
156         int tag = ber_read_id(p, buflen);
157         size_t len = ber_read_len(p, buflen);
158
159         if(tag == 0x10) {
160                 if(result)
161                         *result = len;
162                 return true;
163         } else {
164                 return false;
165         }
166 }
167
168 static bool ber_read_mpi(unsigned char **p, size_t *buflen, gcry_mpi_t *mpi) {
169         int tag = ber_read_id(p, buflen);
170         size_t len = ber_read_len(p, buflen);
171         gcry_error_t err = 0;
172
173         if(tag != 0x02 || len > *buflen)
174                 return false;
175
176         if(mpi)
177                 err = gcry_mpi_scan(mpi, GCRYMPI_FMT_USG, *p, len, NULL);
178         
179         *p += len;
180         *buflen -= len;
181
182         return mpi ? !err : true;
183 }
184
185 bool rsa_set_hex_public_key(rsa_t *rsa, char *n, char *e) {
186         gcry_error_t err = 0;
187
188         err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL)
189                 ?: gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, n, 0, NULL);
190
191         if(err) {
192                 logger(LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
193                 return false;
194         }
195
196         return true;
197 }
198
199 bool rsa_set_hex_private_key(rsa_t *rsa, char *n, char *e, char *d) {
200         gcry_error_t err = 0;
201
202         err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL)
203                 ?: gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, n, 0, NULL)
204                 ?: gcry_mpi_scan(&rsa->d, GCRYMPI_FMT_HEX, n, 0, NULL);
205
206         if(err) {
207                 logger(LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
208                 return false;
209         }
210
211         return true;
212 }
213
214 // Read PEM RSA keys
215
216 bool rsa_read_pem_public_key(rsa_t *rsa, FILE *fp) {
217         uint8_t derbuf[8096], *derp = derbuf;
218         size_t derlen;
219
220         if(!pem_decode(fp, "RSA PUBLIC KEY", derbuf, sizeof derbuf, &derlen)) {
221                 logger(LOG_ERR, "Unable to read RSA public key: %s", strerror(errno));
222                 return NULL;
223         }
224
225         if(!ber_read_sequence(&derp, &derlen, NULL)
226                         || !ber_read_mpi(&derp, &derlen, &rsa->n)
227                         || !ber_read_mpi(&derp, &derlen, &rsa->e)
228                         || derlen) {
229                 logger(LOG_ERR, "Error while decoding RSA public key");
230                 return NULL;
231         }
232
233         return true;
234 }
235
236 bool rsa_read_pem_private_key(rsa_t *rsa, FILE *fp) {
237         uint8_t derbuf[8096], *derp = derbuf;
238         size_t derlen;
239
240         if(!pem_decode(fp, "RSA PRIVATE KEY", derbuf, sizeof derbuf, &derlen)) {
241                 logger(LOG_ERR, "Unable to read RSA private key: %s", strerror(errno));
242                 return NULL;
243         }
244
245         if(!ber_read_sequence(&derp, &derlen, NULL)
246                         || !ber_read_mpi(&derp, &derlen, NULL)
247                         || !ber_read_mpi(&derp, &derlen, &rsa->n)
248                         || !ber_read_mpi(&derp, &derlen, &rsa->e)
249                         || !ber_read_mpi(&derp, &derlen, &rsa->d)
250                         || !ber_read_mpi(&derp, &derlen, NULL) // p
251                         || !ber_read_mpi(&derp, &derlen, NULL) // q
252                         || !ber_read_mpi(&derp, &derlen, NULL)
253                         || !ber_read_mpi(&derp, &derlen, NULL)
254                         || !ber_read_mpi(&derp, &derlen, NULL) // u
255                         || derlen) {
256                 logger(LOG_ERR, "Error while decoding RSA private key");
257                 return NULL;
258         }
259
260         return true;
261 }
262
263 size_t rsa_size(rsa_t *rsa) {
264         return (gcry_mpi_get_nbits(rsa->n) + 7) / 8;
265 }
266
267 /* Well, libgcrypt has functions to handle RSA keys, but they suck.
268  * So we just use libgcrypt's mpi functions, and do the math ourselves.
269  */
270
271 // TODO: get rid of this macro, properly clean up gcry_ structures after use
272 #define check(foo) { gcry_error_t err = (foo); if(err) {logger(LOG_ERR, "gcrypt error %s/%s at %s:%d\n", gcry_strsource(err), gcry_strerror(err), __FILE__, __LINE__); return false; }}
273
274 bool rsa_public_encrypt(rsa_t *rsa, void *in, size_t len, void *out) {
275         gcry_mpi_t inmpi;
276         check(gcry_mpi_scan(&inmpi, GCRYMPI_FMT_USG, in, len, NULL));
277
278         gcry_mpi_t outmpi = gcry_mpi_new(len * 8);
279         gcry_mpi_powm(outmpi, inmpi, rsa->e, rsa->n);
280
281         check(gcry_mpi_print(GCRYMPI_FMT_USG, out,len, NULL, outmpi));
282
283         return true;
284 }
285
286 bool rsa_private_decrypt(rsa_t *rsa, void *in, size_t len, void *out) {
287         gcry_mpi_t inmpi;
288         check(gcry_mpi_scan(&inmpi, GCRYMPI_FMT_USG, in, len, NULL));
289
290         gcry_mpi_t outmpi = gcry_mpi_new(len * 8);
291         gcry_mpi_powm(outmpi, inmpi, rsa->d, rsa->n);
292
293         check(gcry_mpi_print(GCRYMPI_FMT_USG, out,len, NULL, outmpi));
294
295         return true;
296 }