2 rsa.c -- RSA key handling
3 Copyright (C) 2007-2022 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
20 #include "../system.h"
27 #include "../logger.h"
29 #include "../xalloc.h"
31 // BER decoding functions
33 static int ber_read_id(unsigned char **p, size_t *buflen) {
38 if((**p & 0x1f) == 0x1f) {
45 more = *(*p)++ & 0x80;
56 return *(*p)++ & 0x1f;
60 static size_t ber_read_len(unsigned char **p, size_t *buflen) {
67 size_t len = *(*p)++ & 0x7f;
75 result = (size_t)(result << 8);
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);
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;
108 if(tag != 0x02 || len > *buflen) {
113 err = gcry_mpi_scan(mpi, GCRYMPI_FMT_USG, *p, len, NULL);
119 return mpi ? !err : true;
122 rsa_t *rsa_set_hex_public_key(const char *n, const char *e) {
123 rsa_t *rsa = xzalloc(sizeof(rsa_t));
125 gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL);
128 err = gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
132 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
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));
143 gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL);
146 err = gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
150 err = gcry_mpi_scan(&rsa->d, GCRYMPI_FMT_HEX, d, 0, NULL);
154 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
164 rsa_t *rsa_read_pem_public_key(FILE *fp) {
165 uint8_t derbuf[8096], *derp = derbuf;
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));
173 rsa_t *rsa = xzalloc(sizeof(rsa_t));
175 if(!ber_read_sequence(&derp, &derlen, NULL)
176 || !ber_read_mpi(&derp, &derlen, &rsa->n)
177 || !ber_read_mpi(&derp, &derlen, &rsa->e)
179 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA public key");
187 rsa_t *rsa_read_pem_private_key(FILE *fp) {
188 uint8_t derbuf[8096], *derp = derbuf;
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));
196 rsa_t *rsa = xzalloc(sizeof(rsa_t));
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
209 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA private key");
214 memzero(derbuf, sizeof(derbuf));
218 size_t rsa_size(const rsa_t *rsa) {
219 return (gcry_mpi_get_nbits(rsa->n) + 7) / 8;
222 static bool check(gcry_error_t err) {
224 logger(DEBUG_ALWAYS, LOG_ERR, "gcrypt error %s/%s", gcry_strsource(err), gcry_strerror(err));
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.
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;
237 if(!check(gcry_mpi_scan(&inmpi, GCRYMPI_FMT_USG, in, len, NULL))) {
241 gcry_mpi_t outmpi = gcry_mpi_snew(len * 8);
242 gcry_mpi_powm(outmpi, inmpi, ed, n);
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;
252 bool ok = check(gcry_mpi_print(GCRYMPI_FMT_USG, pout, len, NULL, outmpi));
254 gcry_mpi_release(outmpi);
255 gcry_mpi_release(inmpi);
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);
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);
268 void rsa_free(rsa_t *rsa) {
271 gcry_mpi_release(rsa->n);
275 gcry_mpi_release(rsa->e);
279 gcry_mpi_release(rsa->d);