Add support for OpenSSL 3.0+
[tinc] / src / openssl / rsagen.c
1 /*
2     rsagen.c -- RSA key generation and export
3     Copyright (C) 2008-2022 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 <openssl/pem.h>
23 #include <openssl/err.h>
24
25 #define TINC_RSA_INTERNAL
26
27 #if OPENSSL_VERSION_MAJOR < 3
28 typedef RSA rsa_t;
29 #else
30 typedef EVP_PKEY rsa_t;
31 #include <openssl/encoder.h>
32 #include <openssl/evp.h>
33 #endif
34
35 #include "../logger.h"
36 #include "../rsagen.h"
37 #include "log.h"
38
39 #if OPENSSL_VERSION_MAJOR < 3
40 /* This function prettyprints the key generation process */
41
42 static int indicator(int a, int b, BN_GENCB *cb) {
43         (void)cb;
44
45         switch(a) {
46         case 0:
47                 fprintf(stderr, ".");
48                 break;
49
50         case 1:
51                 fprintf(stderr, "+");
52                 break;
53
54         case 2:
55                 fprintf(stderr, "-");
56                 break;
57
58         case 3:
59                 switch(b) {
60                 case 0:
61                         fprintf(stderr, " p\n");
62                         break;
63
64                 case 1:
65                         fprintf(stderr, " q\n");
66                         break;
67
68                 default:
69                         fprintf(stderr, "?");
70                 }
71
72                 break;
73
74         default:
75                 fprintf(stderr, "?");
76         }
77
78         return 1;
79 }
80 #endif
81
82 // Generate RSA key
83
84 rsa_t *rsa_generate(size_t bits, unsigned long exponent) {
85         BIGNUM *bn_e = BN_new();
86         rsa_t *rsa = NULL;
87
88         if(!bn_e) {
89                 abort();
90         }
91
92         BN_set_word(bn_e, exponent);
93
94 #if OPENSSL_VERSION_MAJOR < 3
95         rsa = RSA_new();
96         BN_GENCB *cb = BN_GENCB_new();
97
98         if(!rsa || !cb) {
99                 abort();
100         }
101
102         BN_GENCB_set(cb, indicator, NULL);
103
104         int result = RSA_generate_key_ex(rsa, (int) bits, bn_e, cb);
105
106         BN_GENCB_free(cb);
107
108         if(!result) {
109                 fprintf(stderr, "Error during key generation!\n");
110                 RSA_free(rsa);
111                 rsa = NULL;
112         }
113
114 #else
115         EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
116
117         bool ok = ctx
118                   && EVP_PKEY_keygen_init(ctx) > 0
119                   && EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn_e) > 0
120                   && EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, (int)bits) > 0
121                   && EVP_PKEY_keygen(ctx, &rsa) > 0;
122
123         if(ctx) {
124                 EVP_PKEY_CTX_free(ctx);
125         }
126
127         if(!ok) {
128                 openssl_err("generate key");
129                 rsa = NULL;
130         }
131
132 #endif
133
134         BN_free(bn_e);
135
136         return rsa;
137 }
138
139 // Write PEM RSA keys
140
141 #if OPENSSL_VERSION_MAJOR >= 3
142 static bool write_key_to_pem(const rsa_t *rsa, FILE *fp, int selection) {
143         OSSL_ENCODER_CTX *enc = OSSL_ENCODER_CTX_new_for_pkey(rsa, selection, "PEM", NULL, NULL);
144
145         if(!enc) {
146                 openssl_err("create encoder context");
147                 return false;
148         }
149
150         bool ok = OSSL_ENCODER_to_fp(enc, fp);
151         OSSL_ENCODER_CTX_free(enc);
152
153         if(!ok) {
154                 openssl_err("write key to file");
155         }
156
157         return ok;
158 }
159 #endif
160
161 bool rsa_write_pem_public_key(rsa_t *rsa, FILE *fp) {
162 #if OPENSSL_VERSION_MAJOR < 3
163         return PEM_write_RSAPublicKey(fp, rsa);
164 #else
165         return write_key_to_pem(rsa, fp, OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
166 #endif
167 }
168
169 bool rsa_write_pem_private_key(rsa_t *rsa, FILE *fp) {
170 #if OPENSSL_VERSION_MAJOR < 3
171         return PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL);
172 #else
173         return write_key_to_pem(rsa, fp, OSSL_KEYMGMT_SELECT_PRIVATE_KEY);
174 #endif
175 }