]> git.meshlink.io Git - meshlink/blob - src/gcrypt/rsagen.c
Started the implementation of route_meshlink that at the moment routes packets based...
[meshlink] / src / gcrypt / rsagen.c
1 /*
2     rsagen.c -- RSA key generation and export
3     Copyright (C) 2008-2012 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 "rsagen.h"
25
26 #if 0
27 // Base64 encoding table
28
29 static const char b64e[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
30
31 // PEM encoding
32
33 static bool pem_encode(FILE *fp, const char *header, uint8_t *buf, size_t size) {
34         bool decode = false;
35         char line[1024];
36         uint32_t word = 0;
37         int shift = 0;
38         size_t i, j = 0;
39
40         fprintf(fp, "-----BEGIN %s-----\n", header);
41
42         for(i = 0; i < size; i += 3) {
43                 if(i <= size - 3) {
44                         word = buf[i] << 16 | buf[i + 1] << 8 | buf[i + 2];
45                 } else {
46                         word = buf[i] << 16;
47                         if(i == size - 2)
48                                 word |= buf[i + 1] << 8;
49                 }
50
51                 line[j++] = b64e[(word >> 18)       ];
52                 line[j++] = b64e[(word >> 12) & 0x3f];
53                 line[j++] = b64e[(word >>  6) & 0x3f];
54                 line[j++] = b64e[(word      ) & 0x3f];
55
56                 if(j >= 64) {
57                         line[j++] = '\n';
58                         line[j] = 0;
59                         fputs(line, fp);
60                         j = 0;
61                 }
62         }
63
64         if(size % 3 > 0) {
65                 if(size % 3 > 1)
66                         line[j++] = '=';
67                 line[j++] = '=';
68         }
69
70         if(j) {
71                 line[j++] = '\n';
72                 line[j] = 0;
73                 fputs(line, fp);
74         }
75
76         fprintf(fp, "-----END %s-----\n", header);
77
78         return true;
79 }
80
81
82 // BER encoding functions
83
84 static bool ber_write_id(uint8_t **p, size_t *buflen, int id) {
85         if(*buflen <= 0)
86                 return false;
87
88         if(id >= 0x1f) {
89                 while(id) {
90                         if(*buflen <= 0)
91                                 return false;
92
93                         (*buflen)--;
94                         **p = id & 0x7f;
95                         id >>= 7;
96                         if(id)
97                                 **p |= 0x80;
98                         (*p)++;
99                 }
100         } else {
101                 (*buflen)--;
102                 *(*p)++ = id;
103         }
104
105         return true;
106 }
107
108 static bool ber_write_len(uint8_t **p, size_t *buflen, size_t len) {
109         do {
110                 if(*buflen <= 0)
111                         return false;
112
113                 (*buflen)--;
114                 **p = len & 0x7f;
115                 len >>= 7;
116                 if(len)
117                         **p |= 0x80;
118                 (*p)++;
119         } while(len);
120
121         return true;
122 }
123
124 static bool ber_write_sequence(uint8_t **p, size_t *buflen, uint8_t *seqbuf, size_t seqlen) {
125         if(!ber_write_id(p, buflen, 0x10) || !ber_write_len(p, buflen, seqlen) || *buflen < seqlen)
126                 return false;
127
128         memcpy(*p, seqbuf, seqlen);
129         *p += seqlen;
130         *buflen -= seqlen;
131
132         return true;
133 }
134
135 static bool ber_write_mpi(uint8_t **p, size_t *buflen, gcry_mpi_t mpi) {
136         uint8_t tmpbuf[1024];
137         size_t tmplen = sizeof tmpbuf;
138         gcry_error_t err;
139
140         err = gcry_mpi_aprint(GCRYMPI_FMT_USG, &tmpbuf, &tmplen, mpi);
141         if(err)
142                 return false;
143
144         if(!ber_write_id(p, buflen, 0x02) || !ber_write_len(p, buflen, tmplen) || *buflen < tmplen)
145                 return false;
146
147         memcpy(*p, tmpbuf, tmplen);
148         *p += tmplen;
149         *buflen -= tmplen;
150
151         return true;
152 }
153
154 // Write PEM RSA keys
155
156 bool rsa_write_pem_public_key(rsa_t *rsa, FILE *fp) {
157         uint8_t derbuf1[8096];
158         uint8_t derbuf2[8096];
159         uint8_t *derp1 = derbuf1;
160         uint8_t *derp2 = derbuf2;
161         size_t derlen1 = sizeof derbuf1;
162         size_t derlen2 = sizeof derbuf2;
163
164         if(!ber_write_mpi(&derp1, &derlen1, &rsa->n)
165                         || !ber_write_mpi(&derp1, &derlen1, &rsa->e)
166                         || !ber_write_sequence(&derp2, &derlen2, derbuf1, derlen1)) {
167                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encoding RSA public key");
168                 return false;
169         }
170
171         if(!pem_encode(fp, "RSA PUBLIC KEY", derbuf2, derlen2)) {
172                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to write RSA public key: %s", strerror(errno));
173                 return false;
174         }
175
176         return true;
177 }
178
179 bool rsa_write_pem_private_key(rsa_t *rsa, FILE *fp) {
180         uint8_t derbuf1[8096];
181         uint8_t derbuf2[8096];
182         uint8_t *derp1 = derbuf1;
183         uint8_t *derp2 = derbuf2;
184         size_t derlen1 = sizeof derbuf1;
185         size_t derlen2 = sizeof derbuf2;
186
187         if(!ber_write_mpi(&derp1, &derlen1, &bits)
188                         || ber_write_mpi(&derp1, &derlen1, &rsa->n) // modulus
189                         || ber_write_mpi(&derp1, &derlen1, &rsa->e) // public exponent
190                         || ber_write_mpi(&derp1, &derlen1, &rsa->d) // private exponent
191                         || ber_write_mpi(&derp1, &derlen1, &p)
192                         || ber_write_mpi(&derp1, &derlen1, &q)
193                         || ber_write_mpi(&derp1, &derlen1, &exp1)
194                         || ber_write_mpi(&derp1, &derlen1, &exp2)
195                         || ber_write_mpi(&derp1, &derlen1, &coeff))
196                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encoding RSA private key");
197                 return false;
198         }
199
200         if(!pem_encode(fp, "RSA PRIVATE KEY", derbuf2, derlen2)) {
201                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to write RSA private key: %s", strerror(errno));
202                 return false;
203         }
204
205         return true;
206 }
207 #endif
208
209 bool rsa_write_pem_public_key(rsa_t *rsa, FILE *fp) {
210         return false;
211 }
212
213 bool rsa_write_pem_private_key(rsa_t *rsa, FILE *fp) {
214         return false;
215 }
216
217 bool rsa_generate(rsa_t *rsa, size_t bits, unsigned long exponent) {
218         fprintf(stderr, "Generating RSA keys with libgcrypt not implemented yet\n");
219         return false;
220 }