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"
28 #include "../logger.h"
30 #include "../xalloc.h"
32 // BER decoding functions
34 static int ber_read_id(unsigned char **p, size_t *buflen) {
39 if((**p & 0x1f) == 0x1f) {
46 more = *(*p)++ & 0x80;
57 return *(*p)++ & 0x1f;
61 static size_t ber_read_len(unsigned char **p, size_t *buflen) {
68 size_t len = *(*p)++ & 0x7f;
76 result = (size_t)(result << 8);
88 static bool ber_skip_sequence(unsigned char **p, size_t *buflen) {
89 int tag = ber_read_id(p, buflen);
91 return tag == TAG_SEQUENCE &&
92 ber_read_len(p, buflen) > 0;
95 static bool ber_read_mpi(unsigned char **p, size_t *buflen, gcry_mpi_t *mpi) {
96 int tag = ber_read_id(p, buflen);
97 size_t len = ber_read_len(p, buflen);
100 if(tag != 0x02 || len > *buflen) {
105 err = gcry_mpi_scan(mpi, GCRYMPI_FMT_USG, *p, len, NULL);
111 return mpi ? !err : true;
114 rsa_t *rsa_set_hex_public_key(const char *n, const char *e) {
115 rsa_t *rsa = xzalloc(sizeof(rsa_t));
117 gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL);
120 err = gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
124 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
132 rsa_t *rsa_set_hex_private_key(const char *n, const char *e, const char *d) {
133 rsa_t *rsa = xzalloc(sizeof(rsa_t));
135 gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL);
138 err = gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
142 err = gcry_mpi_scan(&rsa->d, GCRYMPI_FMT_HEX, d, 0, NULL);
146 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
156 rsa_t *rsa_read_pem_public_key(FILE *fp) {
157 uint8_t derbuf[8096], *derp = derbuf;
160 if(!pem_decode(fp, "RSA PUBLIC KEY", derbuf, sizeof(derbuf), &derlen)) {
161 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA public key: %s", strerror(errno));
165 rsa_t *rsa = xzalloc(sizeof(rsa_t));
167 if(!ber_skip_sequence(&derp, &derlen)
168 || !ber_read_mpi(&derp, &derlen, &rsa->n)
169 || !ber_read_mpi(&derp, &derlen, &rsa->e)
171 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA public key");
179 rsa_t *rsa_read_pem_private_key(FILE *fp) {
180 uint8_t derbuf[8096], *derp = derbuf;
183 if(!pem_decode(fp, "RSA PRIVATE KEY", derbuf, sizeof(derbuf), &derlen)) {
184 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA private key: %s", strerror(errno));
188 rsa_t *rsa = xzalloc(sizeof(rsa_t));
190 if(!ber_skip_sequence(&derp, &derlen)
191 || !ber_read_mpi(&derp, &derlen, NULL)
192 || !ber_read_mpi(&derp, &derlen, &rsa->n)
193 || !ber_read_mpi(&derp, &derlen, &rsa->e)
194 || !ber_read_mpi(&derp, &derlen, &rsa->d)
195 || !ber_read_mpi(&derp, &derlen, NULL) // p
196 || !ber_read_mpi(&derp, &derlen, NULL) // q
197 || !ber_read_mpi(&derp, &derlen, NULL)
198 || !ber_read_mpi(&derp, &derlen, NULL)
199 || !ber_read_mpi(&derp, &derlen, NULL) // u
201 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA private key");
206 memzero(derbuf, sizeof(derbuf));
210 size_t rsa_size(const rsa_t *rsa) {
211 return (gcry_mpi_get_nbits(rsa->n) + 7) / 8;
214 static bool check(gcry_error_t err) {
216 logger(DEBUG_ALWAYS, LOG_ERR, "gcrypt error %s/%s", gcry_strsource(err), gcry_strerror(err));
222 /* Well, libgcrypt has functions to handle RSA keys, but they suck.
223 * So we just use libgcrypt's mpi functions, and do the math ourselves.
226 static bool rsa_powm(const gcry_mpi_t ed, const gcry_mpi_t n, const void *in, size_t len, void *out) {
227 gcry_mpi_t inmpi = NULL;
229 if(!check(gcry_mpi_scan(&inmpi, GCRYMPI_FMT_USG, in, len, NULL))) {
233 gcry_mpi_t outmpi = gcry_mpi_snew(len * 8);
234 gcry_mpi_powm(outmpi, inmpi, ed, n);
236 size_t out_bytes = (gcry_mpi_get_nbits(outmpi) + 7) / 8;
237 size_t pad = len - MIN(out_bytes, len);
238 unsigned char *pout = out;
244 bool ok = check(gcry_mpi_print(GCRYMPI_FMT_USG, pout, len, NULL, outmpi));
246 gcry_mpi_release(outmpi);
247 gcry_mpi_release(inmpi);
252 bool rsa_public_encrypt(rsa_t *rsa, const void *in, size_t len, void *out) {
253 return rsa_powm(rsa->e, rsa->n, in, len, out);
256 bool rsa_private_decrypt(rsa_t *rsa, const void *in, size_t len, void *out) {
257 return rsa_powm(rsa->d, rsa->n, in, len, out);
260 void rsa_free(rsa_t *rsa) {
263 gcry_mpi_release(rsa->n);
267 gcry_mpi_release(rsa->e);
271 gcry_mpi_release(rsa->d);