-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcrypto.c
321 lines (270 loc) · 9.19 KB
/
crypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "chaosvpn.h"
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
/*
This checks data in a struct string against a signature in a second
struct string against a RSA public key.
We use a 4096 bit RSA key to sign the data using a SHA512 hash, which
is hopefully enough for the next few years.
# key created using:
openssl genrsa -aes256 -out privkey.pem 4096
# public key exported:
openssl rsa -in privkey.pem -pubout -out pubkey.pem
# signing command used on the backend:
openssl dgst \
-sha512 \
-sign privkey.pem \
-out tinc-chaosvpn.txt.sig \
tinc-chaosvpn.txt
# how to verify the signature using a commandline tool:
# (essentially the same as the c code below does)
openssl dgst \
-sha512 \
-verify pubkey.pem \
-signature tinc-chaosvpn.txt.sig \
tinc-chaosvpn.txt
*/
static int crypto_log_err_cb(const char *str, size_t len, void *u)
{
log_err("openssl returned error: %s", str);
return 1;
}
EVP_PKEY *
crypto_load_key(const char *key, const bool is_private)
{
BIO *bio;
EVP_PKEY *pkey;
bio = BIO_new_mem_buf((void *)key, strlen(key));
if (bio == NULL) {
log_err("crypto_load_key: BIO_new_mem_buf() failed");
return NULL;
}
/* read and parse key */
if (is_private) {
pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
} else {
pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
}
BIO_free(bio);
if (pkey == NULL) {
log_err("crypto_load_key: loading and parsing key failed");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
return NULL;
}
return pkey;
}
bool
crypto_rsa_verify_signature(struct string *databuffer, struct string *signature, const char *pubkey)
{
int err;
bool retval;
EVP_MD_CTX *md_ctx;
EVP_PKEY *pkey;
/* load public key into openssl structure */
pkey = crypto_load_key(pubkey, false);
if (pkey == NULL) {
log_err("crypto_verify_signature: key loading failed");
return false;
}
md_ctx = EVP_MD_CTX_create();
if (!md_ctx) {
log_err("crypto_verify_signature: md_ctx alloc failed");
return false;
}
/* Verify the signature */
if (EVP_VerifyInit(md_ctx, EVP_sha512()) != 1) {
log_err("crypto_verify_signature: libcrypto verify init failed");
EVP_MD_CTX_destroy(md_ctx);
EVP_PKEY_free(pkey);
return false;
}
EVP_VerifyUpdate(md_ctx, string_get(databuffer), string_length(databuffer));
err = EVP_VerifyFinal(md_ctx, (unsigned char*)string_get(signature), string_length(signature), pkey);
EVP_PKEY_free(pkey);
if (err != 1) {
log_err("crypto_verify_signature: signature verify failed, received bogus data from backend.");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
retval = false;
goto bailout_ctx_cleanup;
}
retval = true;
bailout_ctx_cleanup:
EVP_MD_CTX_destroy(md_ctx);
//log_info("Signature Verified Ok.");
return retval;
}
bool
crypto_rsa_decrypt(struct string *ciphertext, const char *privkey, struct string *decrypted)
{
bool retval = false;
size_t len;
EVP_PKEY *pkey;
EVP_PKEY_CTX *ctx;
/* load private key into openssl */
pkey = crypto_load_key(privkey, true);
if (pkey == NULL) {
log_err("crypto_rsa_decrypt: key loading failed.");
return false;
}
/* check length of ciphertext */
if (string_length(ciphertext) != EVP_PKEY_size(pkey)) {
log_err("crypto_rsa_decrypt: ciphertext should match length of key (%" PRIuPTR " vs %d).",
string_length(ciphertext),EVP_PKEY_size(pkey));
goto bail_out;
}
string_free(decrypted); /* just to be sure */
string_init(decrypted, EVP_PKEY_size(pkey), 512);
if (string_size(decrypted) < EVP_PKEY_size(pkey)) {
log_err("crypto_rsa_decrypt: malloc error.");
goto bail_out;
}
ctx = EVP_PKEY_CTX_new(pkey, NULL);
if (!ctx) {
retval = false;
log_err("crypto_rsa_decrypt: EVP_PKEY_CTX_new() failed.");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
goto bail_out;
}
if (EVP_PKEY_decrypt_init(ctx) <= 0) {
retval = false;
log_err("crypto_rsa_decrypt: EVP_PKEY_decrypt_init() failed.");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
goto bail_out_ctx;
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) {
retval = false;
log_err("crypto_rsa_decrypt: EVP_PKEY_CTX_set_rsa_padding() failed.");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
goto bail_out_ctx;
}
/* determine output size */
EVP_PKEY_decrypt(ctx, NULL, &len, (unsigned char*)string_get(ciphertext), string_length(ciphertext));
if (len != string_length(ciphertext)) {
retval = false;
log_err("crypto_rsa_decrypt: EVP_PKEY_decrypt output len != input len");
goto bail_out_ctx;
}
if (EVP_PKEY_decrypt(ctx,
(unsigned char*)string_get(decrypted),
&len,
(unsigned char*)string_get(ciphertext),
string_length(ciphertext)) <= 0) {
retval = false;
log_err("crypto_rsa_decrypt: EVP_PKEY_decrypt() failed.");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
goto bail_out_ctx;
}
if (len >= 0) {
/* TODO: need cleaner way: */
decrypted->length = len;
retval = true;
} else {
retval = false;
log_err("crypto_rsa_decrypt: rsa decrypt failed.");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
}
bail_out_ctx:
EVP_PKEY_CTX_free(ctx);
bail_out:
EVP_PKEY_free(pkey);
return retval;
}
bool
crypto_aes_decrypt(struct string *ciphertext, struct string *aes_key, struct string *aes_iv, struct string *decrypted)
{
bool retval = false;
EVP_CIPHER_CTX *ctx;
int decryptspace;
int decryptdone;
ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
log_err("crypto_aes_decrypt: ctx alloc failed");
goto bail_out;
}
if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
(unsigned char *)string_get(aes_key),
(unsigned char *)string_get(aes_iv))) {
log_err("crypto_aes_decrypt: init failed");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
goto bail_out;
}
EVP_CIPHER_CTX_set_padding(ctx, 1);
if (string_length(aes_key) != EVP_CIPHER_CTX_key_length(ctx)) {
log_err("crypto_aes_decrypt: invalid key size (%" PRIuPTR " vs expected %d)",
string_length(aes_key), EVP_CIPHER_CTX_key_length(ctx));
goto bail_out;
}
if (string_length(aes_iv) != EVP_CIPHER_CTX_iv_length(ctx)) {
log_err("crypto_aes_decrypt: invalid iv size (%" PRIuPTR " vs expected %d)",
string_length(aes_iv), EVP_CIPHER_CTX_iv_length(ctx));
goto bail_out;
}
decryptspace = string_length(ciphertext) + EVP_MAX_BLOCK_LENGTH;
string_free(decrypted); /* free previous buffer */
string_init(decrypted, decryptspace, 1024);
if (string_size(decrypted) < decryptspace) {
log_err("crypto_aes_decrypt: decrypt buffer malloc error");
goto bail_out;
}
if (EVP_DecryptUpdate(ctx, (unsigned char*)string_get(decrypted),
&decryptdone, (unsigned char*)string_get(ciphertext),
string_length(ciphertext))) {
/* TODO: need cleaner way: */
decrypted->length = decryptdone;
} else {
log_err("crypto_aes_decrypt: decrypt failed");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
goto bail_out;
}
if (EVP_DecryptFinal_ex(ctx,
(unsigned char*)string_get(decrypted)+string_length(decrypted),
&decryptdone)) {
/* TODO: need cleaner way: */
decrypted->length += decryptdone;
} else {
log_err("crypto_aes_decrypt: decrypt final failed");
ERR_print_errors_cb(&crypto_log_err_cb, NULL);
goto bail_out;
}
retval = true;
bail_out:
if (ctx)
EVP_CIPHER_CTX_free(ctx);
return retval;
}
void
crypto_init(void)
{
/* Just load the crypto library error strings, not SSL */
ERR_load_crypto_strings();
}
void
crypto_finish(void)
{
ERR_free_strings();
}
void
crypto_warn_openssl_version_changed(void)
{
/*
* Check that the OpenSSL headers used match the version of the
* OpenSSL library used.
* Output a warning if not.
*/
if (SSLeay() != OPENSSL_VERSION_NUMBER) {
log_info("Note: compiled using OpenSSL version '%s' headers, but linked to "
"OpenSSL version '%s' library",
OPENSSL_VERSION_TEXT,
SSLeay_version(SSLEAY_VERSION));
}
}