-
Notifications
You must be signed in to change notification settings - Fork 38
/
crypto.c
4713 lines (4360 loc) · 132 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
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
/*
* Copyright (C) 2019-2024 Nicola Di Lieto <nicola.dilieto@gmail.com>
*
* This file is part of uacme.
*
* uacme is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* uacme is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "base64.h"
#include "crypto.h"
#include "curlwrap.h"
#include "msg.h"
#if !defined(USE_OPENSSL)
#include "read-file.h"
#endif
#if defined(USE_GNUTLS)
#include <gnutls/crypto.h>
#include <gnutls/ocsp.h>
#if HAVE_GNUTLS_X509_CRQ_SET_TLSFEATURES
#include <gnutls/x509-ext.h>
#endif
#if !HAVE_GNUTLS_DECODE_RS_VALUE
#include <libtasn1.h>
#endif
#elif defined(USE_OPENSSL)
#include <openssl/asn1.h>
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/core_names.h>
#endif
#include <openssl/engine.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/ocsp.h>
#include <openssl/opensslv.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#elif defined(USE_MBEDTLS)
#include <mbedtls/asn1write.h>
#include <mbedtls/base64.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#include <mbedtls/error.h>
#include <mbedtls/md.h>
#include <mbedtls/oid.h>
#include <mbedtls/pem.h>
#include <mbedtls/pk.h>
#include <mbedtls/platform.h>
#include <mbedtls/sha1.h>
#include <mbedtls/version.h>
#include <mbedtls/x509_crt.h>
#include <mbedtls/x509_csr.h>
#endif
#if defined(USE_GNUTLS)
#if GNUTLS_VERSION_NUMBER < 0x03031e
#error GnuTLS version 3.3.30 or later is required
#endif
bool crypto_init(void)
{
#if HAVE_GNUTLS_DECODE_RS_VALUE
if (!gnutls_check_version("3.6.0")) {
warnx("crypto_init: GnuTLS version 3.6.0 or later is required");
#elif HAVE_GNUTLS_X509_CRQ_SET_TLSFEATURES
if (!gnutls_check_version("3.5.1")) {
warnx("crypto_init: GnuTLS version 3.5.1 or later is required");
#else
if (!gnutls_check_version("3.3.30")) {
warnx("crypto_init: GnuTLS version 3.3.30 or later is required");
#endif
return false;
}
gnutls_global_init();
return true;
}
void crypto_deinit(void)
{
gnutls_global_deinit();
}
#elif defined(USE_OPENSSL)
#if OPENSSL_VERSION_NUMBER < 0x1010100fL
#error OpenSSL version 1.1.1 or later is required
#endif
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3040200fL
#error LibreSSL version 3.4.2 or later is required
#endif
bool crypto_init(void)
{
if (OpenSSL_version_num() < 0x1010100fL) {
warnx("crypto_init: openssl version 1.1.1 or later is required");
return false;
}
return true;
}
void crypto_deinit(void)
{
}
static void openssl_error(const char *prefix)
{
unsigned long e;
while ((e = ERR_get_error()) != 0) {
warnx("%s: openssl %s", prefix, ERR_error_string(e, NULL));
return;
}
}
static bool openssl_hash_fast(const EVP_MD *type,
const void *input, size_t len, unsigned char *output)
{
bool success = false;
EVP_MD_CTX *emc = EVP_MD_CTX_create();
if (!emc) {
openssl_error("openssl_hash_fast");
goto out;
}
if (!EVP_DigestInit_ex(emc, type, NULL)) {
openssl_error("openssl_hash_fast");
goto out;
}
if (!EVP_DigestUpdate(emc, input, len)) {
openssl_error("openssl_hash_fast");
goto out;
}
if (!EVP_DigestFinal_ex(emc, output, NULL)) {
openssl_error("openssl_hash_fast");
goto out;
}
success = true;
out:
if (emc)
EVP_MD_CTX_destroy(emc);
return success;
}
static bool openssl_hmac_fast(const EVP_MD *type, const void *key,
size_t keylen, const void *input, size_t len, unsigned char *output)
{
if (HMAC(type, key, keylen, input, len, output, NULL) == NULL) {
openssl_error("openssl_hmac_fast");
return false;
}
return true;
}
#elif defined(USE_MBEDTLS)
#if MBEDTLS_VERSION_NUMBER < 0x02100000
#error mbedTLS 2.x version 2.16 or later is required
#endif
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 && MBEDTLS_VERSION_NUMBER < 0x03020000
#error mbedTLS 3.x version 3.2 or later is required
#endif
static mbedtls_entropy_context entropy;
static mbedtls_ctr_drbg_context ctr_drbg;
static const char *_mbedtls_strerror(int code)
{
static char buf[0x100];
mbedtls_strerror(code, buf, sizeof(buf));
return buf;
}
bool crypto_init(void)
{
#if defined(MBEDTLS_VERSION_C)
unsigned int version = mbedtls_version_get_number();
if (version < 0x02100000) {
warnx("crypto_init: mbedTLS 2.x version 2.16 or later is required");
return false;
}
if (version >= 0x03000000 && version < 0x03020000) {
warnx("crypto_init: mbedTLS 3.x version 3.2 or later is required");
return false;
}
#endif
mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg);
int r = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
&entropy, NULL, 0);
if (r) {
warnx("crypto_init: mbedtls_ctr_dbg_seed failed: %s",
_mbedtls_strerror(r));
return false;
}
return true;
}
void crypto_deinit(void)
{
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
}
static int mbedtls_hash_fast(mbedtls_md_type_t md_type,
const void *input, size_t len, unsigned char *output)
{
const mbedtls_md_info_t *mdi = mbedtls_md_info_from_type(md_type);
if (!mdi) {
warnx("mbedtls_hash_fast: md_info not found");
return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
}
return mbedtls_md(mdi, input, len, output);
}
static int mbedtls_hmac_fast(mbedtls_md_type_t md_type, const void *key,
size_t keylen, const void *input, size_t len, unsigned char *output)
{
const mbedtls_md_info_t *mdi = mbedtls_md_info_from_type(md_type);
if (!mdi) {
warnx("mbedtls_hmac_fast: md_info not found");
return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
}
return mbedtls_md_hmac(mdi, key, keylen, input, len, output);
}
#endif
char *sha2_base64url(size_t bits, const char *format, ...)
{
char *input = NULL;
size_t encoded_hash_len;
char *encoded_hash = NULL;
const unsigned int hash_len = (bits+7)/8;
unsigned char *hash = NULL;
va_list ap;
va_start(ap, format);
if (vasprintf(&input, format, ap) < 0) {
warnx("sha2_base64url: vasprintf failed");
input = NULL;
goto out;
}
hash = calloc(1, hash_len);
if (!hash) {
warn("sha2_base64url: calloc failed");
goto out;
}
#if defined(USE_GNUTLS)
gnutls_digest_algorithm_t type;
#elif defined(USE_OPENSSL)
const EVP_MD *type;
#elif defined(USE_MBEDTLS)
mbedtls_md_type_t type;
#endif
switch (bits) {
case 256:
#if defined(USE_GNUTLS)
type = GNUTLS_DIG_SHA256;
#elif defined(USE_OPENSSL)
type = EVP_sha256();
#elif defined(USE_MBEDTLS)
type = MBEDTLS_MD_SHA256;
#endif
break;
case 384:
#if defined(USE_GNUTLS)
type = GNUTLS_DIG_SHA384;
#elif defined(USE_OPENSSL)
type = EVP_sha384();
#elif defined(USE_MBEDTLS)
type = MBEDTLS_MD_SHA384;
#endif
break;
default:
warnx("sha2_base64url: invalid hash bit length");
goto out;
}
#if defined(USE_GNUTLS)
int r = gnutls_hash_fast(type, input, strlen(input), hash);
if (r != GNUTLS_E_SUCCESS) {
warnx("sha2_base64url: gnutls_hash_fast failed: %s",
gnutls_strerror(r));
goto out;
}
#elif defined(USE_OPENSSL)
if (!openssl_hash_fast(type, input, strlen(input), hash)) {
warnx("sha2_base64url: openssl_hash_fast failed");
goto out;
}
#elif defined(USE_MBEDTLS)
int r = mbedtls_hash_fast(type, input, strlen(input), hash);
if (r != 0) {
warnx("sha2_base64url: mbedtls_hash_fast failed: %s",
_mbedtls_strerror(r));
goto out;
}
#endif
encoded_hash_len = base64_ENCODED_LEN(hash_len,
base64_VARIANT_URLSAFE_NO_PADDING);
encoded_hash = calloc(1, encoded_hash_len);
if (!encoded_hash) {
warn("sha2_base64url: calloc failed");
goto out;
}
if (!bin2base64(encoded_hash, encoded_hash_len,
hash, hash_len, base64_VARIANT_URLSAFE_NO_PADDING)) {
warnx("sha2_base64url: bin2base64 failed");
free(encoded_hash);
encoded_hash = NULL;
goto out;
}
out:
va_end(ap);
free(input);
free(hash);
return encoded_hash;
}
char *hmac_base64url(size_t bits, const char *key, const char *format, ...)
{
char *input = NULL;
size_t encoded_hash_len;
char *encoded_hash = NULL;
const unsigned int hash_len = (bits+7)/8;
unsigned char *hash = NULL;
size_t keylen = strlen(key);
void *keybin = NULL;
va_list ap;
va_start(ap, format);
if (vasprintf(&input, format, ap) < 0) {
warnx("hmac_base64url: vasprintf failed");
input = NULL;
goto out;
}
hash = calloc(1, hash_len);
if (!hash) {
warn("hmac_base64url: calloc failed");
goto out;
}
keybin = calloc(1, keylen);
if (!keybin) {
warn("hmac_base64url: calloc failed");
goto out;
}
if (base642bin(keybin, keylen, key, keylen, NULL, &keylen, NULL,
base64_VARIANT_URLSAFE_NO_PADDING)) {
warnx("hmac_base64url: failed to decode key");
goto out;
}
#if defined(USE_GNUTLS)
gnutls_mac_algorithm_t type;
#elif defined(USE_OPENSSL)
const EVP_MD *type;
#elif defined(USE_MBEDTLS)
mbedtls_md_type_t type;
#endif
switch (bits) {
case 256:
#if defined(USE_GNUTLS)
type = GNUTLS_MAC_SHA256;
#elif defined(USE_OPENSSL)
type = EVP_sha256();
#elif defined(USE_MBEDTLS)
type = MBEDTLS_MD_SHA256;
#endif
break;
case 384:
#if defined(USE_GNUTLS)
type = GNUTLS_MAC_SHA384;
#elif defined(USE_OPENSSL)
type = EVP_sha384();
#elif defined(USE_MBEDTLS)
type = MBEDTLS_MD_SHA384;
#endif
break;
case 512:
#if defined(USE_GNUTLS)
type = GNUTLS_MAC_SHA512;
#elif defined(USE_OPENSSL)
type = EVP_sha512();
#elif defined(USE_MBEDTLS)
type = MBEDTLS_MD_SHA512;
#endif
break;
default:
warnx("hmac_base64url: invalid hmac bit length");
goto out;
}
#if defined(USE_GNUTLS)
int r = gnutls_hmac_fast(type, keybin, keylen, input, strlen(input), hash);
if (r != GNUTLS_E_SUCCESS) {
warnx("hmac_base64url: gnutls_hmac_fast failed: %s",
gnutls_strerror(r));
goto out;
}
#elif defined(USE_OPENSSL)
if (!openssl_hmac_fast(type, keybin, keylen, input, strlen(input), hash)) {
warnx("hmac_base64url: openssl_hmac_fast failed");
goto out;
}
#elif defined(USE_MBEDTLS)
int r = mbedtls_hmac_fast(type, keybin, keylen, input, strlen(input), hash);
if (r != 0) {
warnx("hmac_base64url: mbedtls_hmac_fast failed: %s",
_mbedtls_strerror(r));
goto out;
}
#endif
encoded_hash_len = base64_ENCODED_LEN(hash_len,
base64_VARIANT_URLSAFE_NO_PADDING);
encoded_hash = calloc(1, encoded_hash_len);
if (!encoded_hash) {
warn("hmac_base64url: calloc failed");
goto out;
}
if (!bin2base64(encoded_hash, encoded_hash_len,
hash, hash_len, base64_VARIANT_URLSAFE_NO_PADDING)) {
warnx("hmac_base64url: bin2base64 failed");
free(encoded_hash);
encoded_hash = NULL;
goto out;
}
out:
va_end(ap);
free(input);
free(hash);
free(keybin);
return encoded_hash;
}
static char *bn2str(const unsigned char *data, size_t data_len, size_t pad_len)
{
char *ret = NULL;
unsigned char *buf = NULL;
while (data_len && !*data) {
data++;
data_len--;
}
if (pad_len == 0)
pad_len = data_len;
else if (pad_len < data_len) {
warnx("bn2str: insufficient pad_len");
goto out;
}
buf = calloc(1, pad_len);
if (!buf) {
warn("bn2str: calloc failed");
goto out;
}
memcpy(buf + pad_len - data_len, data, data_len);
size_t encoded_len = base64_ENCODED_LEN(pad_len,
base64_VARIANT_URLSAFE_NO_PADDING);
ret = calloc(1, encoded_len);
if (!ret) {
warn("bn2str: calloc failed");
goto out;
}
if (!bin2base64(ret, encoded_len, buf, pad_len,
base64_VARIANT_URLSAFE_NO_PADDING)) {
free(ret);
ret = NULL;
}
out:
free(buf);
return ret;
}
static bool rsa_params(privkey_t key, char **m, char **e)
{
int r;
char *_m = NULL;
char *_e = NULL;
#if defined(USE_GNUTLS)
gnutls_datum_t mod = {NULL, 0};
gnutls_datum_t exp = {NULL, 0};
if (gnutls_privkey_get_pk_algorithm(key, NULL) != GNUTLS_PK_RSA) {
warnx("rsa_params: not a RSA key");
goto out;
}
r = gnutls_privkey_export_rsa_raw(key, &mod, &exp,
NULL, NULL, NULL, NULL, NULL, NULL);
if (r < 0) {
warnx("rsa_params: gnutls_privkey_export_rsa_raw: %s",
gnutls_strerror(r));
goto out;
}
_m = bn2str(mod.data, mod.size, 0);
if (!_m) {
warnx("rsa_params: bn2str failed");
goto out;
}
_e = bn2str(exp.data, exp.size, 0);
if (!_e) {
warnx("rsa_params: bn2str failed");
goto out;
}
#elif defined(USE_OPENSSL)
unsigned char *data = NULL;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
BIGNUM *bm = NULL;
BIGNUM *be = NULL;
if (!EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_RSA_N, &bm)) {
openssl_error("rsa_params");
goto out;
}
if (!EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_RSA_E, &be)) {
openssl_error("rsa_params");
goto out;
}
#else
const BIGNUM *bm = NULL;
const BIGNUM *be = NULL;
RSA *rsa = EVP_PKEY_get0_RSA(key);
if (!rsa) {
openssl_error("rsa_params");
goto out;
}
RSA_get0_key(rsa, &bm, &be, NULL);
#endif
r = BN_num_bytes(bm);
data = calloc(1, r);
if (!data) {
warn("rsa_params: calloc failed");
goto out;
}
if (BN_bn2bin(bm, data) != r) {
openssl_error("rsa_params");
goto out;
}
_m = bn2str(data, r, 0);
if (!_m) {
warnx("rsa_params: bn2str failed");
goto out;
}
free(data);
r = BN_num_bytes(be);
data = calloc(1, r);
if (!data) {
warn("rsa_params: calloc failed");
goto out;
}
if (BN_bn2bin(be, data) != r) {
openssl_error("rsa_params");
goto out;
}
_e = bn2str(data, r, 0);
if (!_e) {
warnx("rsa_params: bn2str failed");
goto out;
}
#elif defined(USE_MBEDTLS)
unsigned char *data = NULL;
size_t len;
mbedtls_mpi mn, me;
mbedtls_mpi_init(&mn);
mbedtls_mpi_init(&me);
if (!mbedtls_pk_can_do(key, MBEDTLS_PK_RSA)) {
warnx("rsa_params: not a RSA key");
goto out;
}
const mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key);
r = mbedtls_rsa_export(rsa, &mn, NULL, NULL, NULL, &me);
if (r) {
warnx("rsa_params: mbedtls_rsa_export failed: %s",
_mbedtls_strerror(r));
goto out;
}
len = mbedtls_mpi_size(&mn);
data = calloc(1, len);
if (!data) {
warnx("rsa_params: calloc failed");
goto out;
}
r = mbedtls_mpi_write_binary(&mn, data, len);
if (r) {
warnx("rsa_params: mbedtls_mpi_write_binary failed: %s",
_mbedtls_strerror(r));
goto out;
}
_m = bn2str(data, len, 0);
if (!_m) {
warnx("rsa_params: bn2str failed");
goto out;
}
free(data);
len = mbedtls_mpi_size(&me);
data = calloc(1, len);
if (!data) {
warnx("rsa_params: calloc failed");
goto out;
}
r = mbedtls_mpi_write_binary(&me, data, len);
if (r) {
warnx("rsa_params: mbedtls_mpi_write_binary failed: %s",
_mbedtls_strerror(r));
goto out;
}
_e = bn2str(data, len, 0);
if (!_e) {
warnx("rsa_params: bn2str failed");
goto out;
}
#endif
out:
#if defined(USE_GNUTLS)
free(mod.data);
free(exp.data);
#elif defined(USE_OPENSSL)
free(data);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
if (bm)
BN_free(bm);
if (be)
BN_free(be);
#endif
#elif defined(USE_MBEDTLS)
free(data);
mbedtls_mpi_free(&mn);
mbedtls_mpi_free(&me);
#endif
if (_e && _m) {
if (e)
*e = _e;
else
free(_e);
if (m)
*m = _m;
else
free(_m);
return true;
} else {
free(_e);
free(_m);
return false;
}
}
static size_t ec_params(privkey_t key, char **x, char **y)
{
int r;
size_t bits = 0;
char *_x = NULL;
char *_y = NULL;
#if defined(USE_GNUTLS)
gnutls_ecc_curve_t curve;
gnutls_datum_t dx = {NULL, 0};
gnutls_datum_t dy = {NULL, 0};
if (gnutls_privkey_get_pk_algorithm(key, NULL) != GNUTLS_PK_EC) {
warnx("ec_params: not a EC key");
goto out;
}
r = gnutls_privkey_export_ecc_raw(key, &curve, &dx, &dy, NULL);
if (r < 0) {
warnx("ec_params: gnutls_privkey_export_ecc_raw: %s",
gnutls_strerror(r));
goto out;
}
switch (curve) {
case GNUTLS_ECC_CURVE_SECP256R1:
bits = 256;
break;
case GNUTLS_ECC_CURVE_SECP384R1:
bits = 384;
break;
default:
warnx("ec_params: only \"prime256v1\" and \"secp384r1\" "
"Elliptic Curves supported");
goto out;
}
_x = bn2str(dx.data, dx.size, (bits+7)/8);
if (!_x) {
warnx("ec_params: bn2str failed");
goto out;
}
_y = bn2str(dy.data, dy.size, (bits+7)/8);
if (!_y) {
warnx("ec_params: bn2str failed");
goto out;
}
#elif defined(USE_OPENSSL)
unsigned char *data = NULL;
BIGNUM *bx = BN_new();
BIGNUM *by = BN_new();
if (!bx || !by) {
openssl_error("ec_params");
goto out;
}
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
char group[0x20];
if (!EVP_PKEY_get_utf8_string_param(key, OSSL_PKEY_PARAM_GROUP_NAME,
group, sizeof(group), NULL)) {
openssl_error("ec_params");
goto out;
}
if (strcasecmp(group, "prime256v1") == 0)
bits = 256;
else if (strcasecmp(group, "secp384r1") == 0)
bits = 384;
else {
warnx("ec_params: only \"prime256v1\" and \"secp384r1\" "
"Elliptic Curves supported");
goto out;
}
if (!EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_EC_PUB_X, &bx)) {
openssl_error("ec_params");
goto out;
}
if (!EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_EC_PUB_Y, &by)) {
openssl_error("ec_params");
goto out;
}
#else
EC_KEY *ec = EVP_PKEY_get0_EC_KEY(key);
if (!ec) {
openssl_error("ec_params");
goto out;
}
const EC_GROUP *g = EC_KEY_get0_group(ec);
if (!g) {
openssl_error("ec_params");
}
switch (EC_GROUP_get_curve_name(g)) {
case NID_X9_62_prime256v1:
bits = 256;
break;
case NID_secp384r1:
bits = 384;
break;
default:
warnx("ec_params: only \"prime256v1\" and \"secp384r1\" "
"Elliptic Curves supported");
goto out;
}
const EC_POINT *pubkey = EC_KEY_get0_public_key(ec);
if (!pubkey) {
openssl_error("ec_params");
goto out;
}
if (!EC_POINT_get_affine_coordinates(g, pubkey, bx, by, NULL)) {
openssl_error("ec_params");
goto out;
}
#endif
r = BN_num_bytes(bx);
data = calloc(1, r);
if (!data) {
warn("ec_params: calloc failed");
goto out;
}
if (BN_bn2bin(bx, data) != r) {
openssl_error("ec_params");
goto out;
}
_x = bn2str(data, r, (bits+7)/8);
if (!_x) {
warnx("ec_params: bn2str failed");
goto out;
}
free(data);
r = BN_num_bytes(by);
data = calloc(1, r);
if (!data) {
warn("ec_params: calloc failed");
goto out;
}
if (BN_bn2bin(by, data) != r) {
openssl_error("ec_params");
goto out;
}
_y = bn2str(data, r, (bits+7)/8);
if (!_y) {
warnx("ec_params: bn2str failed");
goto out;
}
#elif defined(USE_MBEDTLS)
unsigned char *data = NULL;
size_t len, olen, plen;
mbedtls_ecp_group grp;
mbedtls_ecp_point Q;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&Q);
if (!mbedtls_pk_can_do(key, MBEDTLS_PK_ECKEY)) {
warnx("ec_params: not a EC key");
goto out;
}
const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key);
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
mbedtls_mpi d;
mbedtls_mpi_init(&d);
r = mbedtls_ecp_export(ec, &grp, &d, &Q);
mbedtls_mpi_free(&d);
if (r) {
warnx("ec_params: mbedtls_ecp_export failed: %s",
_mbedtls_strerror(r));
goto out;
}
#else
r = mbedtls_ecp_group_copy(&grp, &ec->grp);
if (r) {
warnx("ec_params: mbedtls_ecp_group_copy failed: %s",
_mbedtls_strerror(r));
goto out;
}
r = mbedtls_ecp_copy(&Q, &ec->Q);
if (r) {
warnx("ec_params: mbedtls_ecp_copy failed: %s",
_mbedtls_strerror(r));
goto out;
}
#endif
switch (grp.id) {
case MBEDTLS_ECP_DP_SECP256R1:
bits = 256;
break;
case MBEDTLS_ECP_DP_SECP384R1:
bits = 384;
break;
default:
warnx("ec_params: only \"prime256v1\" and \"secp384r1\" "
"Elliptic Curves supported");
goto out;
}
plen = mbedtls_mpi_size(&grp.P);
len = 1 + 2 * plen;
data = calloc(1, len);
if (!data) {
warnx("ec_params: calloc failed");
goto out;
}
r = mbedtls_ecp_point_write_binary(&grp, &Q, MBEDTLS_ECP_PF_UNCOMPRESSED,
&olen, data, len);
if (r) {
warnx("ec_params: mbedtls_ecp_point_write_binary failed: %s",
_mbedtls_strerror(r));
goto out;
}
if (olen != len) {
warnx("ec_params: wrong length (actual %zu, expected %zu)", olen, len);
goto out;
}
if (data[0] != 0x04) {
warnx("ec_params: key data corruption");
goto out;
}
_x = bn2str(data + 1, plen, (bits+7)/8);
if (!_x) {
warnx("ec_params: bn2str failed");
goto out;
}
_y = bn2str(data + 1 + plen, plen, (bits+7)/8);
if (!_y) {
warnx("ec_params: bn2str failed");
goto out;
}
#endif
out:
#if defined(USE_GNUTLS)
free(dx.data);
free(dy.data);
#elif defined(USE_OPENSSL)
if (bx)
BN_free(bx);
if (by)
BN_free(by);
free(data);
#elif defined(USE_MBEDTLS)
free(data);
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&Q);
#endif
if (_x && _y) {
if (x)
*x = _x;
else
free(_x);
if (y)
*y = _y;
else
free(_y);
return bits;
} else {
free(_x);
free(_y);
return 0;
}
}
keytype_t key_type(privkey_t key)
{
#if defined(USE_GNUTLS)
switch (gnutls_privkey_get_pk_algorithm(key, NULL)) {
case GNUTLS_PK_RSA:
return PK_RSA;
case GNUTLS_PK_EC:
return PK_EC;
#elif defined(USE_OPENSSL)
switch (EVP_PKEY_base_id(key)) {
case EVP_PKEY_RSA:
return PK_RSA;
case EVP_PKEY_EC:
return PK_EC;
#elif defined(USE_MBEDTLS)
switch (mbedtls_pk_get_type(key)) {
case MBEDTLS_PK_RSA:
return PK_RSA;
case MBEDTLS_PK_ECKEY:
return PK_EC;
#endif
default:
return PK_NONE;
}
}
char *jws_jwk(privkey_t key, const char **crv, const char **alg)
{
char *ret = NULL;
char *p1 = NULL;
char *p2 = NULL;
const char *_crv = NULL;
switch (key_type(key)) {
case PK_RSA:
if (!rsa_params(key, &p1, &p2)) {
warnx("jws_jwk: rsa_params failed");
goto out;
}
if (asprintf(&ret, "{\"kty\":\"RSA\",\"n\":\"%s\",\"e\":\"%s\"}",
p1, p2) < 0) {
warnx("jws_jwk: asprintf failed");
ret = NULL;
goto out;
}
if (alg) *alg = "RS256";
break;
case PK_EC:
switch (ec_params(key, &p1, &p2)) {
case 0:
warnx("jws_jwk: ec_params failed");
goto out;
case 256:
_crv = "P-256";
if (crv) *crv = _crv;
if (alg) *alg = "ES256";
break;
case 384:
_crv = "P-384";
if (crv) *crv = _crv;