-
Notifications
You must be signed in to change notification settings - Fork 272
/
dcrypt-openssl.c
2027 lines (1786 loc) · 60.1 KB
/
dcrypt-openssl.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "lib.h"
#include "buffer.h"
#include "str.h"
#include "hex-binary.h"
#include "safe-memset.h"
#include "randgen.h"
#include "array.h"
#include "module-dir.h"
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/ec.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/engine.h>
#include <openssl/hmac.h>
#include <openssl/objects.h>
#include "dcrypt.h"
#include "dcrypt-private.h"
/**
key format documentation:
=========================
v1 key
------
algo id = openssl NID
enctype = 0 = none, 1 = ecdhe, 2 = password
key id = sha256(hex encoded public point)
public key
----------
1<tab>algo id<tab>public point
private key
-----------
- enctype none
1<tab>algo id<tab>0<tab>private point<tab>key id
- enctype ecdh (algorithm AES-256-CTR, key = SHA256(shared secret), IV = \0\0\0...)
1<tab>algo id<tab>1<tab>private point<tab>ephemeral public key<tab>encryption key id<tab>key id
- enctype password (algorithm AES-256-CTR, key = PBKDF2(SHA1, 16, password, salt), IV = \0\0\0...)
1<tab>algo id<tab>2<tab>private point<tab>salt<tab>key id
v2 key
------
algo oid = ASN1 OID of key algorithm (RSA or EC curve)
enctype = 0 = none, 1 = ecdhe, 2 = password
key id = SHA256(i2d_PUBKEY)
public key
----------
2<tab>HEX(i2d_PUBKEY)
- enctype none
2<tab>key algo oid<tab>0<tab>(RSA = i2d_PrivateKey, EC=Private Point)<tab>key id
- enctype ecdh, key,iv = PBKDF2(hash algo, rounds, shared secret, salt)
2<tab>key algo oid<tab>1<tab>symmetric algo name<tab>salt<tab>hash algo<tab>rounds<tab>E(RSA = i2d_PrivateKey, EC=Private Point)<tab>ephemeral public key<tab>encryption key id<tab>key id
- enctype password, key,iv = PBKDF2(hash algo, rounds, password, salt)
2<tab>key algo oid<tab>1<tab>symmetric algo name<tab>salt<tab>hash algo<tab>rounds<tab>E(RSA = i2d_PrivateKey, EC=Private Point)<tab>key id
**/
#if SSLEAY_VERSION_NUMBER < 0x1010000fL
#define EVP_PKEY_get0_EC_KEY(x) x->pkey.ec
#define EVP_PKEY_get0_RSA(x) x->pkey.rsa
#endif
struct dcrypt_context_symmetric {
pool_t pool;
const EVP_CIPHER *cipher;
EVP_CIPHER_CTX *ctx;
unsigned char *key;
unsigned char *iv;
unsigned char *aad;
size_t aad_len;
unsigned char *tag;
size_t tag_len;
int padding;
int mode;
};
struct dcrypt_context_hmac {
pool_t pool;
const EVP_MD *md;
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
HMAC_CTX *ctx;
#else
HMAC_CTX ctx;
#endif
unsigned char *key;
size_t klen;
};
struct dcrypt_public_key {
void *ctx;
};
struct dcrypt_private_key {
void *ctx;
};
static
bool dcrypt_openssl_private_to_public_key(struct dcrypt_private_key *priv_key, struct dcrypt_public_key **pub_key, const char **error_r);
static
bool dcrypt_openssl_public_key_id(struct dcrypt_public_key *key, const char *algorithm, buffer_t *result, const char **error_r);
static
bool dcrypt_openssl_public_key_id_old(struct dcrypt_public_key *key, buffer_t *result, const char **error_r);
static
bool dcrypt_openssl_private_to_public_key(struct dcrypt_private_key *priv_key, struct dcrypt_public_key **pub_key_r, const char **error_r ATTR_UNUSED);
static
void dcrypt_openssl_free_private_key(struct dcrypt_private_key **key);
static
void dcrypt_openssl_free_public_key(struct dcrypt_public_key **key);
static
bool dcrypt_openssl_rsa_decrypt(struct dcrypt_private_key *key, const unsigned char *data, size_t data_len, buffer_t *result, const char **error_r);
static
bool dcrypt_openssl_error(const char **error_r)
{
if(error_r == NULL) return FALSE; /* caller is not really interested */
unsigned long ec = ERR_get_error();
*error_r = t_strdup_printf("%s", ERR_error_string(ec, NULL));
return FALSE;
}
/* legacy function for old formats that generates
hex encoded point from EC public key
*/
static
char *ec_key_get_pub_point_hex(const EC_KEY *key)
{
const EC_POINT *p;
const EC_GROUP *g;
p = EC_KEY_get0_public_key(key);
g = EC_KEY_get0_group(key);
return EC_POINT_point2hex(g, p, POINT_CONVERSION_COMPRESSED, NULL);
}
static
bool dcrypt_openssl_ctx_sym_create(const char *algorithm, enum dcrypt_sym_mode mode, struct dcrypt_context_symmetric **ctx_r, const char **error_r)
{
struct dcrypt_context_symmetric *ctx;
pool_t pool;
const EVP_CIPHER *cipher;
cipher = EVP_get_cipherbyname(algorithm);
if (cipher == NULL) {
if (error_r != NULL)
*error_r = t_strdup_printf("Invalid cipher %s", algorithm);
return FALSE;
}
/* allocate context */
pool = pool_alloconly_create("dcrypt openssl", 1024);
ctx = p_new(pool, struct dcrypt_context_symmetric, 1);
ctx->pool = pool;
ctx->cipher = cipher;
ctx->padding = 1;
ctx->mode =( mode == DCRYPT_MODE_ENCRYPT ? 1 : 0 );
*ctx_r = ctx;
return TRUE;
}
static
void dcrypt_openssl_ctx_sym_destroy(struct dcrypt_context_symmetric **ctx)
{
pool_t pool = (*ctx)->pool;
if ((*ctx)->ctx) EVP_CIPHER_CTX_free((*ctx)->ctx);
pool_unref(&pool);
*ctx = NULL;
}
static
void dcrypt_openssl_ctx_sym_set_key(struct dcrypt_context_symmetric *ctx, const unsigned char *key, size_t key_len)
{
if(ctx->key != NULL) p_free(ctx->pool, ctx->key);
ctx->key = p_malloc(ctx->pool, EVP_CIPHER_key_length(ctx->cipher));
memcpy(ctx->key, key, I_MIN(key_len,(size_t)EVP_CIPHER_key_length(ctx->cipher)));
}
static
void dcrypt_openssl_ctx_sym_set_iv(struct dcrypt_context_symmetric *ctx, const unsigned char *iv, size_t iv_len)
{
if(ctx->iv != NULL) p_free(ctx->pool, ctx->iv);
ctx->iv = p_malloc(ctx->pool, EVP_CIPHER_iv_length(ctx->cipher));
memcpy(ctx->iv, iv, I_MIN(iv_len,(size_t)EVP_CIPHER_iv_length(ctx->cipher)));
}
static
void dcrypt_openssl_ctx_sym_set_key_iv_random(struct dcrypt_context_symmetric *ctx)
{
if(ctx->key != NULL) p_free(ctx->pool, ctx->key);
if(ctx->iv != NULL) p_free(ctx->pool, ctx->iv);
ctx->key = p_malloc(ctx->pool, EVP_CIPHER_key_length(ctx->cipher));
random_fill(ctx->key, EVP_CIPHER_key_length(ctx->cipher));
ctx->iv = p_malloc(ctx->pool, EVP_CIPHER_iv_length(ctx->cipher));
random_fill(ctx->iv, EVP_CIPHER_iv_length(ctx->cipher));
}
static
void dcrypt_openssl_ctx_sym_set_padding(struct dcrypt_context_symmetric *ctx, bool padding)
{
ctx->padding = (padding?1:0);
}
static
bool dcrypt_openssl_ctx_sym_get_key(struct dcrypt_context_symmetric *ctx, buffer_t *key)
{
if(ctx->key == NULL) return FALSE;
buffer_append(key, ctx->key, EVP_CIPHER_key_length(ctx->cipher));
return TRUE;
}
static
bool dcrypt_openssl_ctx_sym_get_iv(struct dcrypt_context_symmetric *ctx, buffer_t *iv)
{
if(ctx->iv == NULL) return FALSE;
buffer_append(iv, ctx->iv, EVP_CIPHER_iv_length(ctx->cipher));
return TRUE;
}
static
void dcrypt_openssl_ctx_sym_set_aad(struct dcrypt_context_symmetric *ctx, const unsigned char *aad, size_t aad_len)
{
if (ctx->aad != NULL) p_free(ctx->pool, ctx->aad);
/* allow empty aad */
ctx->aad = p_malloc(ctx->pool, I_MAX(1,aad_len));
memcpy(ctx->aad, aad, aad_len);
ctx->aad_len = aad_len;
}
static
bool dcrypt_openssl_ctx_sym_get_aad(struct dcrypt_context_symmetric *ctx, buffer_t *aad)
{
if (ctx->aad == NULL) return FALSE;
buffer_append(aad, ctx->aad, ctx->aad_len);
return TRUE;
}
static
void dcrypt_openssl_ctx_sym_set_tag(struct dcrypt_context_symmetric *ctx, const unsigned char *tag, size_t tag_len)
{
if (ctx->tag != NULL) p_free(ctx->pool, ctx->tag);
/* unlike aad, tag cannot be empty */
ctx->tag = p_malloc(ctx->pool, tag_len);
memcpy(ctx->tag, tag, tag_len);
ctx->tag_len = tag_len;
}
static
bool dcrypt_openssl_ctx_sym_get_tag(struct dcrypt_context_symmetric *ctx, buffer_t *tag)
{
if (ctx->tag == NULL) return FALSE;
buffer_append(tag, ctx->tag, ctx->tag_len);
return TRUE;
}
static
unsigned int dcrypt_openssl_ctx_sym_get_key_length(struct dcrypt_context_symmetric *ctx)
{
return EVP_CIPHER_iv_length(ctx->cipher);
}
static
unsigned int dcrypt_openssl_ctx_sym_get_iv_length(struct dcrypt_context_symmetric *ctx)
{
return EVP_CIPHER_iv_length(ctx->cipher);
}
static
unsigned int dcrypt_openssl_ctx_sym_get_block_size(struct dcrypt_context_symmetric *ctx)
{
return EVP_CIPHER_block_size(ctx->cipher);
}
static
bool dcrypt_openssl_ctx_sym_init(struct dcrypt_context_symmetric *ctx, const char **error_r)
{
int ec;
int len;
i_assert(ctx->key != NULL);
i_assert(ctx->iv != NULL);
i_assert(ctx->ctx == NULL);
if((ctx->ctx = EVP_CIPHER_CTX_new()) == NULL)
return dcrypt_openssl_error(error_r);
ec = EVP_CipherInit_ex(ctx->ctx, ctx->cipher, NULL, ctx->key, ctx->iv, ctx->mode);
if (ec != 1) return dcrypt_openssl_error(error_r);
EVP_CIPHER_CTX_set_padding(ctx->ctx, ctx->padding);
len = 0;
if (ctx->aad != NULL) ec = EVP_CipherUpdate(ctx->ctx, NULL, &len, ctx->aad, ctx->aad_len);
if (ec != 1) return dcrypt_openssl_error(error_r);
return TRUE;
}
static
bool dcrypt_openssl_ctx_sym_update(struct dcrypt_context_symmetric *ctx, const unsigned char *data, size_t data_len, buffer_t *result, const char **error_r)
{
const size_t block_size = (size_t)EVP_CIPHER_block_size(ctx->cipher);
size_t buf_used = result->used;
unsigned char *buf;
int outl;
i_assert(ctx->ctx != NULL);
/* From `man 3 evp_cipherupdate`:
EVP_EncryptUpdate() encrypts inl bytes from the buffer in and writes
the encrypted version to out. This function can be called multiple
times to encrypt successive blocks of data. The amount of data written
depends on the block alignment of the encrypted data: as a result the
amount of data written may be anything from zero bytes to
(inl + cipher_block_size - 1) so out should contain sufficient room.
The actual number of bytes written is placed in outl.
*/
buf = buffer_append_space_unsafe(result, data_len + block_size);
outl = 0;
if (EVP_CipherUpdate
(ctx->ctx, buf, &outl, data, data_len) != 1)
return dcrypt_openssl_error(error_r);
buffer_set_used_size(result, buf_used + outl);
return TRUE;
}
static
bool dcrypt_openssl_ctx_sym_final(struct dcrypt_context_symmetric *ctx, buffer_t *result, const char **error_r)
{
const size_t block_size = (size_t)EVP_CIPHER_block_size(ctx->cipher);
size_t buf_used = result->used;
unsigned char *buf;
int outl;
int ec;
i_assert(ctx->ctx != NULL);
/* From `man 3 evp_cipherupdate`:
If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts
the "final" data, that is any data that remains in a partial block. It
uses standard block padding (aka PKCS padding). The encrypted final data
is written to out which should have sufficient space for one cipher
block. The number of bytes written is placed in outl. After this
function is called the encryption operation is finished and no further
calls to EVP_EncryptUpdate() should be made.
*/
buf = buffer_append_space_unsafe(result, block_size);
outl = 0;
/* when **DECRYPTING** set expected tag */
if (ctx->mode == 0 && ctx->tag != NULL) {
ec = EVP_CIPHER_CTX_ctrl(ctx->ctx, EVP_CTRL_GCM_SET_TAG, ctx->tag_len, ctx->tag);
} else ec = 1;
if (ec == 1)
ec = EVP_CipherFinal_ex(ctx->ctx, buf, &outl);
if (ec == 1) {
buffer_set_used_size(result, buf_used + outl);
/* when **ENCRYPTING** recover tag */
if (ctx->mode == 1 && ctx->aad != NULL) {
/* tag should be NULL here */
i_assert(ctx->tag == NULL);
/* openssl claims taglen is always 16, go figure .. */
ctx->tag = p_malloc(ctx->pool, EVP_GCM_TLS_TAG_LEN);
ec = EVP_CIPHER_CTX_ctrl(ctx->ctx, EVP_CTRL_GCM_GET_TAG, EVP_GCM_TLS_TAG_LEN, ctx->tag);
ctx->tag_len = EVP_GCM_TLS_TAG_LEN;
}
}
if (ec == 0 && error_r != NULL)
*error_r = "data authentication failed";
else if (ec < 0) dcrypt_openssl_error(error_r);
EVP_CIPHER_CTX_free(ctx->ctx);
ctx->ctx = NULL;
return ec == 1;
}
static
bool dcrypt_openssl_ctx_hmac_create(const char *algorithm, struct dcrypt_context_hmac **ctx_r, const char **error_r)
{
struct dcrypt_context_hmac *ctx;
pool_t pool;
const EVP_MD *md;
md = EVP_get_digestbyname(algorithm);
if(md == NULL) {
if (error_r != NULL)
*error_r = t_strdup_printf("Invalid digest %s", algorithm);
return FALSE;
}
/* allocate context */
pool = pool_alloconly_create("dcrypt openssl", 1024);
ctx = p_new(pool, struct dcrypt_context_hmac, 1);
ctx->pool = pool;
ctx->md = md;
*ctx_r = ctx;
return TRUE;
}
static
void dcrypt_openssl_ctx_hmac_destroy(struct dcrypt_context_hmac **ctx)
{
pool_t pool = (*ctx)->pool;
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
if ((*ctx)->ctx) HMAC_CTX_free((*ctx)->ctx);
#else
HMAC_cleanup(&((*ctx)->ctx));
#endif
pool_unref(&pool);
*ctx = NULL;
}
static
void dcrypt_openssl_ctx_hmac_set_key(struct dcrypt_context_hmac *ctx, const unsigned char *key, size_t key_len)
{
if(ctx->key != NULL) p_free(ctx->pool, ctx->key);
ctx->klen = I_MIN(key_len, HMAC_MAX_MD_CBLOCK);
ctx->key = p_malloc(ctx->pool, ctx->klen);
memcpy(ctx->key, key, ctx->klen);
}
static
bool dcrypt_openssl_ctx_hmac_get_key(struct dcrypt_context_hmac *ctx, buffer_t *key)
{
if(ctx->key == NULL) return FALSE;
buffer_append(key, ctx->key, ctx->klen);
return TRUE;
}
static
void dcrypt_openssl_ctx_hmac_set_key_random(struct dcrypt_context_hmac *ctx)
{
ctx->klen = HMAC_MAX_MD_CBLOCK;
ctx->key = p_malloc(ctx->pool, ctx->klen);
random_fill(ctx->key, ctx->klen);
}
static
unsigned int dcrypt_openssl_ctx_hmac_get_digest_length(struct dcrypt_context_hmac *ctx)
{
return EVP_MD_size(ctx->md);
}
static
bool dcrypt_openssl_ctx_hmac_init(struct dcrypt_context_hmac *ctx, const char **error_r)
{
int ec;
i_assert(ctx->md != NULL);
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
ctx->ctx = HMAC_CTX_new();
if (ctx->ctx == NULL) return FALSE;
ec = HMAC_Init_ex(ctx->ctx, ctx->key, ctx->klen, ctx->md, NULL);
#else
HMAC_CTX_init(&ctx->ctx);
ec = HMAC_Init_ex(&(ctx->ctx), ctx->key, ctx->klen, ctx->md, NULL);
#endif
if (ec != 1) return dcrypt_openssl_error(error_r);
return TRUE;
}
static
bool dcrypt_openssl_ctx_hmac_update(struct dcrypt_context_hmac *ctx, const unsigned char *data, size_t data_len, const char **error_r)
{
int ec;
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
ec = HMAC_Update(ctx->ctx, data, data_len);
#else
ec = HMAC_Update(&(ctx->ctx), data, data_len);
#endif
if (ec != 1) return dcrypt_openssl_error(error_r);
return TRUE;
}
static
bool dcrypt_openssl_ctx_hmac_final(struct dcrypt_context_hmac *ctx, buffer_t *result, const char **error_r)
{
int ec;
unsigned char buf[HMAC_MAX_MD_CBLOCK];
unsigned int outl;
#if SSLEAY_VERSION_NUMBER >= 0x1010000fL
ec = HMAC_Final(ctx->ctx, buf, &outl);
HMAC_CTX_free(ctx->ctx);
ctx->ctx = NULL;
#else
ec = HMAC_Final(&(ctx->ctx), buf, &outl);
HMAC_cleanup(&(ctx->ctx));
#endif
if (ec == 1) {
buffer_append(result, buf, outl);
} else return dcrypt_openssl_error(error_r);
return TRUE;
}
static
bool dcrypt_openssl_generate_ec_key(int nid, EVP_PKEY **key, const char **error_r)
{
EVP_PKEY_CTX *pctx;
EVP_PKEY_CTX *ctx;
EVP_PKEY *params = NULL;
/* generate parameters for EC */
pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
if (pctx == NULL ||
EVP_PKEY_paramgen_init(pctx) < 1 ||
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, nid) < 1 ||
EVP_PKEY_paramgen(pctx, ¶ms) < 1)
{
dcrypt_openssl_error(error_r);
EVP_PKEY_CTX_free(pctx);
return FALSE;
}
/* generate key from parameters */
ctx = EVP_PKEY_CTX_new(params, NULL);
if (EVP_PKEY_keygen_init(ctx) < 1 ||
EVP_PKEY_keygen(ctx, key) < 1)
{
dcrypt_openssl_error(error_r);
EVP_PKEY_free(params);
EVP_PKEY_CTX_free(pctx);
EVP_PKEY_CTX_free(ctx);
return FALSE;
}
EVP_PKEY_free(params);
EVP_PKEY_CTX_free(pctx);
EVP_PKEY_CTX_free(ctx);
EC_KEY_set_asn1_flag(EVP_PKEY_get0_EC_KEY((*key)), OPENSSL_EC_NAMED_CURVE);
EC_KEY_set_conv_form(EVP_PKEY_get0_EC_KEY((*key)), POINT_CONVERSION_COMPRESSED);
return TRUE;
}
static
bool dcrypt_openssl_generate_rsa_key(int bits, EVP_PKEY **key, const char **error_r)
{
int ec = 0;
EVP_PKEY_CTX *ctx;
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (ctx == NULL ||
EVP_PKEY_keygen_init(ctx) < 1 ||
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) < 1 ||
EVP_PKEY_keygen(ctx, key) < 1) {
dcrypt_openssl_error(error_r);
ec = -1;
}
EVP_PKEY_CTX_free(ctx);
return ec == 0;
}
static
bool dcrypt_openssl_ecdh_derive_secret_local(struct dcrypt_private_key *local_key, buffer_t *R, buffer_t *S, const char **error_r)
{
EVP_PKEY *local = (EVP_PKEY*)local_key;
BN_CTX *bn_ctx = BN_CTX_new();
const EC_GROUP *grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(local));
EC_POINT *pub = EC_POINT_new(grp);
/* convert ephemeral key data EC point */
if (EC_POINT_oct2point(grp, pub, R->data, R->used, bn_ctx) != 1)
{
EC_POINT_free(pub);
BN_CTX_free(bn_ctx);
return dcrypt_openssl_error(error_r);
}
EC_KEY *ec_key = EC_KEY_new();
/* convert point to public key */
EC_KEY_set_conv_form(ec_key, POINT_CONVERSION_COMPRESSED);
EC_KEY_set_group(ec_key, grp);
EC_KEY_set_public_key(ec_key, pub);
EC_POINT_free(pub);
BN_CTX_free(bn_ctx);
/* make sure it looks like a valid key */
if (EC_KEY_check_key(ec_key) != 1) {
EC_KEY_free(ec_key);
return dcrypt_openssl_error(error_r);
}
EVP_PKEY *peer = EVP_PKEY_new();
EVP_PKEY_set1_EC_KEY(peer, ec_key);
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(local, NULL);
/* initialize derivation */
if (pctx == NULL ||
EVP_PKEY_derive_init(pctx) != 1 ||
EVP_PKEY_derive_set_peer(pctx, peer) != 1) {
EVP_PKEY_CTX_free(pctx);
EC_KEY_free(ec_key);
return dcrypt_openssl_error(error_r);
}
/* have to do it twice to get the data length */
size_t len;
if (EVP_PKEY_derive(pctx, NULL, &len) != 1) {
EVP_PKEY_CTX_free(pctx);
EC_KEY_free(ec_key);
return dcrypt_openssl_error(error_r);
}
unsigned char buf[len];
memset(buf,0,len);
if (EVP_PKEY_derive(pctx, buf, &len) != 1) {
EVP_PKEY_CTX_free(pctx);
EC_KEY_free(ec_key);
return dcrypt_openssl_error(error_r);
}
EVP_PKEY_CTX_free(pctx);
buffer_append(S, buf, len);
EC_KEY_free(ec_key);
EVP_PKEY_free(peer);
return TRUE;
}
static
bool dcrypt_openssl_ecdh_derive_secret_peer(struct dcrypt_public_key *peer_key, buffer_t *R, buffer_t *S, const char **error_r)
{
/* ensure peer_key is EC key */
EVP_PKEY *local = NULL;
EVP_PKEY *peer = (EVP_PKEY*)peer_key;
if (EVP_PKEY_base_id(peer) != EVP_PKEY_EC) {
if (error_r != NULL)
*error_r = "Only ECC key can be used";
return FALSE;
}
/* generate another key from same group */
int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(peer)));
if (!dcrypt_openssl_generate_ec_key(nid, &local, error_r)) return FALSE;
/* initialize */
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(local, NULL);
if (pctx == NULL ||
EVP_PKEY_derive_init(pctx) != 1 ||
EVP_PKEY_derive_set_peer(pctx, peer) != 1) {
EVP_PKEY_CTX_free(pctx);
return dcrypt_openssl_error(error_r);
}
/* derive */
size_t len;
if (EVP_PKEY_derive(pctx, NULL, &len) != 1) {
EVP_PKEY_CTX_free(pctx);
return dcrypt_openssl_error(error_r);
}
unsigned char buf[len];
if (EVP_PKEY_derive(pctx, buf, &len) != 1) {
EVP_PKEY_CTX_free(pctx);
return dcrypt_openssl_error(error_r);
}
EVP_PKEY_CTX_free(pctx);
buffer_append(S, buf, len);
/* get ephemeral key (=R) */
BN_CTX *bn_ctx = BN_CTX_new();
const EC_POINT *pub = EC_KEY_get0_public_key(EVP_PKEY_get0_EC_KEY(local));
const EC_GROUP *grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(local));
len = EC_POINT_point2oct(grp, pub, POINT_CONVERSION_COMPRESSED, NULL, 0, bn_ctx);
unsigned char R_buf[len];
EC_POINT_point2oct(grp, pub, POINT_CONVERSION_COMPRESSED, R_buf, len, bn_ctx);
BN_CTX_free(bn_ctx);
buffer_append(R, R_buf, len);
EVP_PKEY_free(local);
return TRUE;
}
static
bool dcrypt_openssl_pbkdf2(const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len,
const char *hash, unsigned int rounds, buffer_t *result, unsigned int result_len, const char **error_r)
{
int ret;
i_assert(rounds > 0);
i_assert(result_len > 0);
i_assert(result != NULL);
T_BEGIN {
/* determine MD */
const EVP_MD* md = EVP_get_digestbyname(hash);
if (md == NULL) {
if (error_r != NULL)
*error_r = t_strdup_printf("Invalid digest %s", hash);
return FALSE;
}
unsigned char buffer[result_len];
if ((ret = PKCS5_PBKDF2_HMAC((const char*)password, password_len, salt, salt_len, rounds,
md, result_len, buffer)) == 1) {
buffer_append(result, buffer, result_len);
}
} T_END;
if (ret != 1) return dcrypt_openssl_error(error_r);
return TRUE;
}
static
bool dcrypt_openssl_generate_keypair(struct dcrypt_keypair *pair_r, enum dcrypt_key_type kind, unsigned int bits, const char *curve, const char **error_r)
{
EVP_PKEY *pkey = NULL;
if (kind == DCRYPT_KEY_RSA) {
if (dcrypt_openssl_generate_rsa_key(bits, &pkey, error_r) == 0) {
pair_r->priv = (struct dcrypt_private_key*)pkey;
return dcrypt_openssl_private_to_public_key(pair_r->priv, &(pair_r->pub), error_r);
} else return dcrypt_openssl_error(error_r);
} else if (kind == DCRYPT_KEY_EC) {
int nid = OBJ_sn2nid(curve);
if (nid == NID_undef) {
if (error_r != NULL)
*error_r = t_strdup_printf("Unknown EC curve %s", curve);
return FALSE;
}
if (dcrypt_openssl_generate_ec_key(nid, &pkey, error_r) == 0) {
pair_r->priv = (struct dcrypt_private_key*)pkey;
return dcrypt_openssl_private_to_public_key(pair_r->priv, &(pair_r->pub), error_r);
} else return dcrypt_openssl_error(error_r);
}
if (error_r != NULL)
*error_r = "Key type not supported in this build";
return FALSE;
}
static
bool dcrypt_openssl_decrypt_point_v1(buffer_t *data, buffer_t *key, BIGNUM **point_r, const char **error_r)
{
struct dcrypt_context_symmetric *dctx;
buffer_t *tmp = buffer_create_dynamic(pool_datastack_create(), 64);
if (!dcrypt_openssl_ctx_sym_create("aes-256-ctr", DCRYPT_MODE_DECRYPT, &dctx, error_r)) {
return FALSE;
}
/* v1 KEYS have all-zero IV - have to use it ourselves too */
dcrypt_openssl_ctx_sym_set_iv(dctx, (const unsigned char*)"\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16);
dcrypt_openssl_ctx_sym_set_key(dctx, key->data, key->used);
if (!dcrypt_openssl_ctx_sym_init(dctx, error_r) ||
!dcrypt_openssl_ctx_sym_update(dctx, data->data, data->used, tmp, error_r) ||
!dcrypt_openssl_ctx_sym_final(dctx, tmp, error_r)) {
dcrypt_openssl_ctx_sym_destroy(&dctx);
return FALSE;
}
dcrypt_openssl_ctx_sym_destroy(&dctx);
*point_r = BN_bin2bn(tmp->data, tmp->used, NULL);
safe_memset(buffer_get_modifiable_data(tmp, NULL), 0,tmp->used);
buffer_set_used_size(key, 0);
if (*point_r == FALSE)
return dcrypt_openssl_error(error_r);
return TRUE;
}
static
bool dcrypt_openssl_decrypt_point_ec_v1(struct dcrypt_private_key *dec_key,
const char *data_hex, const char *peer_key_hex, BIGNUM **point_r, const char **error_r)
{
buffer_t *peer_key, *data, key, *secret;
bool res;
data = buffer_create_dynamic(pool_datastack_create(), 128);
peer_key = buffer_create_dynamic(pool_datastack_create(), 64);
hex_to_binary(data_hex, data);
hex_to_binary(peer_key_hex, peer_key);
secret = buffer_create_dynamic(pool_datastack_create(), 64);
if (!dcrypt_openssl_ecdh_derive_secret_local(dec_key, peer_key, secret, error_r))
return FALSE;
/* run it thru SHA256 once */
unsigned char digest[SHA256_DIGEST_LENGTH];
SHA256(secret->data, secret->used, digest);
safe_memset(buffer_get_modifiable_data(secret, NULL), 0, secret->used);
buffer_set_used_size(secret, 0);
buffer_create_from_const_data(&key, digest, SHA256_DIGEST_LENGTH);
/* then use this as key */
res = dcrypt_openssl_decrypt_point_v1(data, &key, point_r, error_r);
memset(digest, 0, sizeof(digest));
safe_memset(digest, 0, SHA256_DIGEST_LENGTH);
return res;
}
static
bool dcrypt_openssl_decrypt_point_password_v1(const char *data_hex, const char *password_hex,
const char *salt_hex, BIGNUM **point_r, const char **error_r)
{
buffer_t *salt, *data, *password, *key;
struct dcrypt_context_symmetric *dctx;
data = buffer_create_dynamic(pool_datastack_create(), 128);
salt = buffer_create_dynamic(pool_datastack_create(), 16);
password = buffer_create_dynamic(pool_datastack_create(), 32);
key = buffer_create_dynamic(pool_datastack_create(), 32);
hex_to_binary(data_hex, data);
hex_to_binary(salt_hex, salt);
hex_to_binary(password_hex, password);
/* aes-256-ctr uses 32 byte key, and v1 uses all-zero IV */
if (!dcrypt_openssl_pbkdf2(password->data, password->used, salt->data, salt->used,
"sha256", 16, key, 32, error_r)) {
dcrypt_ctx_sym_destroy(&dctx);
return FALSE;
}
return dcrypt_openssl_decrypt_point_v1(data, key, point_r, error_r);
}
static
bool dcrypt_openssl_load_private_key_dovecot_v1(struct dcrypt_private_key **key_r,
int len, const char **input, const char *password, struct dcrypt_private_key *dec_key,
const char **error_r)
{
int nid, ec, enctype;
EC_KEY *eckey = NULL;
BIGNUM *point = NULL;
if (str_to_int(input[1], &nid) != 0) {
if (error_r != NULL)
*error_r = "Corrupted data";
return FALSE;
}
if (str_to_int(input[2], &enctype) != 0) {
if (error_r != NULL)
*error_r = "Corrupted data";
return FALSE;
}
eckey = EC_KEY_new_by_curve_name(nid);
if (eckey == NULL) return dcrypt_openssl_error(error_r);
/* decode and optionally decipher private key value */
if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_NONE) {
point = BN_new();
if (BN_hex2bn(&point, input[3]) < 1) {
BN_free(point);
EC_KEY_free(eckey);
return dcrypt_openssl_error(error_r);
}
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PASSWORD) {
/* by password */
const char *enc_priv_pt = input[3];
const char *salt = input[4];
if (!dcrypt_openssl_decrypt_point_password_v1(enc_priv_pt, password, salt, &point, error_r)) {
EC_KEY_free(eckey);
return FALSE;
}
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PK) {
/* by key */
const char *enc_priv_pt = input[3];
const char *peer_key = input[4];
if (!dcrypt_openssl_decrypt_point_ec_v1(dec_key, enc_priv_pt, peer_key, &point, error_r)) {
EC_KEY_free(eckey);
return FALSE;
}
} else {
if (error_r != NULL)
*error_r = "Invalid key data";
EC_KEY_free(eckey);
return FALSE;
}
/* assign private key */
BN_CTX *bnctx = BN_CTX_new();
EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
EC_KEY_set_private_key(eckey, point);
EC_KEY_precompute_mult(eckey, bnctx);
EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
EC_POINT *pub = EC_POINT_new(EC_KEY_get0_group(eckey));
/* calculate public key */
ec = EC_POINT_mul(EC_KEY_get0_group(eckey), pub, point, NULL, NULL, bnctx);
EC_KEY_set_public_key(eckey, pub);
BN_free(point);
EC_POINT_free(pub);
BN_CTX_free(bnctx);
/* make sure it looks OK and is correct */
if (ec == 1 && EC_KEY_check_key(eckey) == 1) {
unsigned char digest[SHA256_DIGEST_LENGTH];
/* validate that the key was loaded correctly */
char *id = ec_key_get_pub_point_hex(eckey);
SHA256((unsigned char*)id, strlen(id), digest);
OPENSSL_free(id);
const char *digest_hex = binary_to_hex(digest, SHA256_DIGEST_LENGTH);
if (strcmp(digest_hex, input[len-1]) != 0) {
if (error_r != NULL)
*error_r = "Key id mismatch after load";
EC_KEY_free(eckey);
return FALSE;
}
EVP_PKEY *key = EVP_PKEY_new();
EVP_PKEY_set1_EC_KEY(key, eckey);
EC_KEY_free(eckey);
*key_r = (struct dcrypt_private_key *)key;
return TRUE;
}
EC_KEY_free(eckey);
return dcrypt_openssl_error(error_r);
}
/* encrypt/decrypt private keys */
static
bool dcrypt_openssl_cipher_key_dovecot_v2(const char *cipher, enum dcrypt_sym_mode mode,
buffer_t *input, buffer_t *secret, buffer_t *salt, const char *digalgo, unsigned int rounds,
buffer_t *result_r, const char **error_r)
{
struct dcrypt_context_symmetric *dctx;
bool res;
if (!dcrypt_openssl_ctx_sym_create(cipher, mode, &dctx, error_r)) {
return FALSE;
}
/* generate encryption key/iv based on secret/salt */
buffer_t *key_data = buffer_create_dynamic(pool_datastack_create(), 128);
res = dcrypt_openssl_pbkdf2(secret->data, secret->used, salt->data, salt->used,
digalgo, rounds, key_data,
dcrypt_openssl_ctx_sym_get_key_length(dctx)+dcrypt_openssl_ctx_sym_get_iv_length(dctx), error_r);
if (!res) {
dcrypt_openssl_ctx_sym_destroy(&dctx);
return FALSE;
}
buffer_t *tmp = buffer_create_dynamic(pool_datastack_create(), 128);
const unsigned char *kd = buffer_free_without_data(&key_data);
/* perform ciphering */
dcrypt_openssl_ctx_sym_set_key(dctx, kd, dcrypt_openssl_ctx_sym_get_key_length(dctx));
dcrypt_openssl_ctx_sym_set_iv(dctx, kd+dcrypt_openssl_ctx_sym_get_key_length(dctx), dcrypt_openssl_ctx_sym_get_iv_length(dctx));
if (!dcrypt_openssl_ctx_sym_init(dctx, error_r) ||
!dcrypt_openssl_ctx_sym_update(dctx, input->data, input->used, tmp, error_r) ||
!dcrypt_openssl_ctx_sym_final(dctx, tmp, error_r)) {
res = FALSE;
} else {
/* provide result if succeeded */
buffer_append_buf(result_r, tmp, 0, (size_t)-1);
res = TRUE;
}
/* and ensure no data leaks */
safe_memset(buffer_get_modifiable_data(tmp, NULL), 0, tmp->used);
dcrypt_openssl_ctx_sym_destroy(&dctx);
return res;
}
static
bool dcrypt_openssl_load_private_key_dovecot_v2(struct dcrypt_private_key **key_r,
int len, const char **input, const char *password, struct dcrypt_private_key *dec_key,
const char **error_r)
{
int enctype;
buffer_t *key_data = buffer_create_dynamic(pool_datastack_create(), 256);
/* check for encryption type */
if (str_to_int(input[2], &enctype) != 0) {
if (error_r != NULL)
*error_r = "Corrupted data";
return FALSE;
}
if (enctype < 0 || enctype > 2) {
if (error_r != NULL)
*error_r = "Corrupted data";
return FALSE;
}
/* match encryption type to field counts */
if ((enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_NONE && len != 5) ||
(enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PASSWORD && len != 9) ||
(enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PK && len != 11)) {
if (error_r != NULL)
*error_r = "Corrupted data";
return FALSE;
}
/* get key type */
int nid = OBJ_txt2nid(input[1]);
if (nid == NID_undef)
return dcrypt_openssl_error(error_r);
/* decode and possibly decipher private key value */
if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_NONE) {
if (hex_to_binary(input[3], key_data) != 0) {
if (error_r != NULL)
*error_r = "Corrupted data";
}
} else if (enctype == DCRYPT_DOVECOT_KEY_ENCRYPT_PK) {
unsigned int rounds;