2 rsagen.c -- RSA key generation and export
3 Copyright (C) 2008-2012 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.
27 // Base64 encoding table
29 static const char b64e[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 static bool pem_encode(FILE *fp, const char *header, uint8_t *buf, size_t size) {
40 fprintf(fp, "-----BEGIN %s-----\n", header);
42 for(i = 0; i < size; i += 3) {
44 word = buf[i] << 16 | buf[i + 1] << 8 | buf[i + 2];
49 word |= buf[i + 1] << 8;
53 line[j++] = b64e[(word >> 18) ];
54 line[j++] = b64e[(word >> 12) & 0x3f];
55 line[j++] = b64e[(word >> 6) & 0x3f];
56 line[j++] = b64e[(word) & 0x3f];
80 fprintf(fp, "-----END %s-----\n", header);
86 // BER encoding functions
88 static bool ber_write_id(uint8_t **p, size_t *buflen, int id) {
117 static bool ber_write_len(uint8_t **p, size_t *buflen, size_t len) {
137 static bool ber_write_sequence(uint8_t **p, size_t *buflen, uint8_t *seqbuf, size_t seqlen) {
138 if(!ber_write_id(p, buflen, 0x10) || !ber_write_len(p, buflen, seqlen) || *buflen < seqlen) {
142 memcpy(*p, seqbuf, seqlen);
149 static bool ber_write_mpi(uint8_t **p, size_t *buflen, gcry_mpi_t mpi) {
150 uint8_t tmpbuf[1024];
151 size_t tmplen = sizeof(tmpbuf);
154 err = gcry_mpi_aprint(GCRYMPI_FMT_USG, &tmpbuf, &tmplen, mpi);
160 if(!ber_write_id(p, buflen, 0x02) || !ber_write_len(p, buflen, tmplen) || *buflen < tmplen) {
164 memcpy(*p, tmpbuf, tmplen);
171 // Write PEM RSA keys
173 bool rsa_write_pem_public_key(rsa_t *rsa, FILE *fp) {
174 uint8_t derbuf1[8096];
175 uint8_t derbuf2[8096];
176 uint8_t *derp1 = derbuf1;
177 uint8_t *derp2 = derbuf2;
178 size_t derlen1 = sizeof(derbuf1);
179 size_t derlen2 = sizeof(derbuf2);
181 if(!ber_write_mpi(&derp1, &derlen1, &rsa->n)
182 || !ber_write_mpi(&derp1, &derlen1, &rsa->e)
183 || !ber_write_sequence(&derp2, &derlen2, derbuf1, derlen1)) {
184 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encoding RSA public key");
188 if(!pem_encode(fp, "RSA PUBLIC KEY", derbuf2, derlen2)) {
189 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to write RSA public key: %s", strerror(errno));
196 bool rsa_write_pem_private_key(rsa_t *rsa, FILE *fp) {
197 uint8_t derbuf1[8096];
198 uint8_t derbuf2[8096];
199 uint8_t *derp1 = derbuf1;
200 uint8_t *derp2 = derbuf2;
201 size_t derlen1 = sizeof(derbuf1);
202 size_t derlen2 = sizeof(derbuf2);
204 if(!ber_write_mpi(&derp1, &derlen1, &bits)
205 || ber_write_mpi(&derp1, &derlen1, &rsa->n) // modulus
206 || ber_write_mpi(&derp1, &derlen1, &rsa->e) // public exponent
207 || ber_write_mpi(&derp1, &derlen1, &rsa->d) // private exponent
208 || ber_write_mpi(&derp1, &derlen1, &p)
209 || ber_write_mpi(&derp1, &derlen1, &q)
210 || ber_write_mpi(&derp1, &derlen1, &exp1)
211 || ber_write_mpi(&derp1, &derlen1, &exp2)
212 || ber_write_mpi(&derp1, &derlen1, &coeff)) {
213 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encoding RSA private key");
219 if(!pem_encode(fp, "RSA PRIVATE KEY", derbuf2, derlen2)) {
220 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to write RSA private key: %s", strerror(errno));
228 bool rsa_write_pem_public_key(rsa_t *rsa, FILE *fp) {
232 bool rsa_write_pem_private_key(rsa_t *rsa, FILE *fp) {
236 bool rsa_generate(rsa_t *rsa, size_t bits, unsigned long exponent) {
237 fprintf(stderr, "Generating RSA keys with libgcrypt not implemented yet\n");