-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathrnp.cpp
More file actions
9005 lines (8340 loc) · 250 KB
/
rnp.cpp
File metadata and controls
9005 lines (8340 loc) · 250 KB
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) 2017-2023, Ribose Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
* CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "crypto/common.h"
#include "key.hpp"
#include "defaults.h"
#include <assert.h>
#include <json_object.h>
#include <json.h>
#include <librepgp/stream-ctx.h>
#include <librepgp/stream-common.h>
#include <librepgp/stream-armor.h>
#include <librepgp/stream-parse.h>
#include <librepgp/stream-write.h>
#include <librepgp/stream-sig.h>
#include <librepgp/stream-packet.h>
#include <librepgp/stream-key.h>
#include <librepgp/stream-dump.h>
#include "rekey/rnp_key_store.h"
#include <rnp/rnp.h>
#include <stdarg.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include "uniwin.h"
#else
#include <unistd.h>
#endif
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <cinttypes>
#include <string.h>
#include <sys/stat.h>
#include <stdexcept>
#include "utils.h"
#include "str-utils.h"
#include "json-utils.h"
#include "version.h"
#include "ffi-priv-types.h"
#include "file-utils.h"
#define FFI_LOG(ffi, ...) \
do { \
FILE *fp = stderr; \
if (ffi && ffi->errs) { \
fp = ffi->errs; \
} \
RNP_LOG_FD(fp, __VA_ARGS__); \
} while (0)
#if defined(RNP_EXPERIMENTAL_CRYPTO_REFRESH) != defined(ENABLE_CRYPTO_REFRESH)
#error "Invalid defines combination."
#endif
#if defined(RNP_EXPERIMENTAL_PQC) != defined(ENABLE_PQC)
#error "Invalid defines combination."
#endif
static rnp::Key *get_key_require_public(rnp_key_handle_t handle);
static rnp::Key *get_key_prefer_public(rnp_key_handle_t handle);
static rnp::Key *get_key_require_secret(rnp_key_handle_t handle);
static bool rnp_password_cb_bounce(const pgp_password_ctx_t *ctx,
char * password,
size_t password_size,
void * userdata_void);
static rnp_result_t rnp_dump_src_to_json(pgp_source_t &src, uint32_t flags, char **result);
static bool
call_key_callback(rnp_ffi_t ffi, const rnp::KeySearch &search, bool secret)
{
if (!ffi->getkeycb) {
return false;
}
ffi->getkeycb(
ffi, ffi->getkeycb_ctx, search.name().c_str(), search.value().c_str(), secret);
return true;
}
static rnp::Key *
find_key(rnp_ffi_t ffi,
const rnp::KeySearch &search,
bool secret,
bool try_key_provider,
rnp::Key * after = nullptr)
{
auto ks = secret ? ffi->secring : ffi->pubring;
rnp::Key *key = ks->search(search, after);
if (!key && try_key_provider && call_key_callback(ffi, search, secret)) {
// recurse and try the store search above once more
return find_key(ffi, search, secret, false, after);
}
return key;
}
static rnp::Key *
ffi_key_provider(const pgp_key_request_ctx_t *ctx, void *userdata)
{
rnp_ffi_t ffi = (rnp_ffi_t) userdata;
return find_key(ffi, ctx->search, ctx->secret, true);
}
static const id_str_pair sig_type_map[] = {{PGP_SIG_BINARY, "binary"},
{PGP_SIG_TEXT, "text"},
{PGP_SIG_STANDALONE, "standalone"},
{PGP_CERT_GENERIC, "certification (generic)"},
{PGP_CERT_PERSONA, "certification (persona)"},
{PGP_CERT_CASUAL, "certification (casual)"},
{PGP_CERT_POSITIVE, "certification (positive)"},
{PGP_SIG_SUBKEY, "subkey binding"},
{PGP_SIG_PRIMARY, "primary key binding"},
{PGP_SIG_DIRECT, "direct"},
{PGP_SIG_REV_KEY, "key revocation"},
{PGP_SIG_REV_SUBKEY, "subkey revocation"},
{PGP_SIG_REV_CERT, "certification revocation"},
{PGP_SIG_TIMESTAMP, "timestamp"},
{PGP_SIG_3RD_PARTY, "third-party"},
{0, NULL}};
static const id_str_pair cert_type_map[] = {{PGP_CERT_GENERIC, RNP_CERTIFICATION_GENERIC},
{PGP_CERT_PERSONA, RNP_CERTIFICATION_PERSONA},
{PGP_CERT_CASUAL, RNP_CERTIFICATION_CASUAL},
{PGP_CERT_POSITIVE, RNP_CERTIFICATION_POSITIVE},
{0, NULL}};
static const id_str_pair pubkey_alg_map[] = {
{PGP_PKA_RSA, RNP_ALGNAME_RSA},
{PGP_PKA_RSA_ENCRYPT_ONLY, RNP_ALGNAME_RSA},
{PGP_PKA_RSA_SIGN_ONLY, RNP_ALGNAME_RSA},
{PGP_PKA_ELGAMAL, RNP_ALGNAME_ELGAMAL},
{PGP_PKA_ELGAMAL_ENCRYPT_OR_SIGN, RNP_ALGNAME_ELGAMAL},
{PGP_PKA_DSA, RNP_ALGNAME_DSA},
{PGP_PKA_ECDH, RNP_ALGNAME_ECDH},
{PGP_PKA_ECDSA, RNP_ALGNAME_ECDSA},
{PGP_PKA_EDDSA, RNP_ALGNAME_EDDSA},
{PGP_PKA_SM2, RNP_ALGNAME_SM2},
#if defined(ENABLE_CRYPTO_REFRESH)
{PGP_PKA_ED25519, RNP_ALGNAME_ED25519},
{PGP_PKA_X25519, RNP_ALGNAME_X25519},
#endif
#if defined(ENABLE_PQC)
{PGP_PKA_KYBER768_X25519, RNP_ALGNAME_KYBER768_X25519},
//{PGP_PKA_KYBER1024_X448, RNP_ALGNAME_KYBER1024_X448},
{PGP_PKA_KYBER768_P256, RNP_ALGNAME_KYBER768_P256},
{PGP_PKA_KYBER1024_P384, RNP_ALGNAME_KYBER1024_P384},
{PGP_PKA_KYBER768_BP256, RNP_ALGNAME_KYBER768_BP256},
{PGP_PKA_KYBER1024_BP384, RNP_ALGNAME_KYBER1024_BP384},
{PGP_PKA_DILITHIUM3_ED25519, RNP_ALGNAME_DILITHIUM3_ED25519},
//{PGP_PKA_DILITHIUM5_ED448, RNP_ALGNAME_DILITHIUM5_ED448},
{PGP_PKA_DILITHIUM3_P256, RNP_ALGNAME_DILITHIUM3_P256},
{PGP_PKA_DILITHIUM5_P384, RNP_ALGNAME_DILITHIUM5_P384},
{PGP_PKA_DILITHIUM3_BP256, RNP_ALGNAME_DILITHIUM3_BP256},
{PGP_PKA_DILITHIUM5_BP384, RNP_ALGNAME_DILITHIUM5_BP384},
{PGP_PKA_SPHINCSPLUS_SHA2, RNP_ALGNAME_SPHINCSPLUS_SHA2},
{PGP_PKA_SPHINCSPLUS_SHAKE, RNP_ALGNAME_SPHINCSPLUS_SHAKE},
#endif
{0, NULL}};
static const id_str_pair symm_alg_map[] = {{PGP_SA_IDEA, RNP_ALGNAME_IDEA},
{PGP_SA_TRIPLEDES, RNP_ALGNAME_TRIPLEDES},
{PGP_SA_CAST5, RNP_ALGNAME_CAST5},
{PGP_SA_BLOWFISH, RNP_ALGNAME_BLOWFISH},
{PGP_SA_AES_128, RNP_ALGNAME_AES_128},
{PGP_SA_AES_192, RNP_ALGNAME_AES_192},
{PGP_SA_AES_256, RNP_ALGNAME_AES_256},
{PGP_SA_TWOFISH, RNP_ALGNAME_TWOFISH},
{PGP_SA_CAMELLIA_128, RNP_ALGNAME_CAMELLIA_128},
{PGP_SA_CAMELLIA_192, RNP_ALGNAME_CAMELLIA_192},
{PGP_SA_CAMELLIA_256, RNP_ALGNAME_CAMELLIA_256},
{PGP_SA_SM4, RNP_ALGNAME_SM4},
{0, NULL}};
static const id_str_pair aead_alg_map[] = {
{PGP_AEAD_NONE, "None"}, {PGP_AEAD_EAX, "EAX"}, {PGP_AEAD_OCB, "OCB"}, {0, NULL}};
static const id_str_pair cipher_mode_map[] = {{PGP_CIPHER_MODE_CFB, "CFB"},
{PGP_CIPHER_MODE_CBC, "CBC"},
{PGP_CIPHER_MODE_OCB, "OCB"},
{0, NULL}};
static const id_str_pair compress_alg_map[] = {{PGP_C_NONE, "Uncompressed"},
{PGP_C_ZIP, "ZIP"},
{PGP_C_ZLIB, "ZLIB"},
{PGP_C_BZIP2, "BZip2"},
{0, NULL}};
static const id_str_pair hash_alg_map[] = {{PGP_HASH_MD5, RNP_ALGNAME_MD5},
{PGP_HASH_SHA1, RNP_ALGNAME_SHA1},
{PGP_HASH_RIPEMD, RNP_ALGNAME_RIPEMD160},
{PGP_HASH_SHA256, RNP_ALGNAME_SHA256},
{PGP_HASH_SHA384, RNP_ALGNAME_SHA384},
{PGP_HASH_SHA512, RNP_ALGNAME_SHA512},
{PGP_HASH_SHA224, RNP_ALGNAME_SHA224},
{PGP_HASH_SHA3_256, RNP_ALGNAME_SHA3_256},
{PGP_HASH_SHA3_512, RNP_ALGNAME_SHA3_512},
{PGP_HASH_SM3, RNP_ALGNAME_SM3},
{0, NULL}};
#if defined(ENABLE_PQC)
static const id_str_pair sphincsplus_params_map[] = {{sphincsplus_simple_128s, "128s"},
{sphincsplus_simple_128f, "128f"},
{sphincsplus_simple_192s, "192s"},
{sphincsplus_simple_192f, "192f"},
{sphincsplus_simple_256s, "256s"},
{sphincsplus_simple_256f, "256f"},
{0, NULL}};
#endif
static const id_str_pair s2k_type_map[] = {
{PGP_S2KS_SIMPLE, "Simple"},
{PGP_S2KS_SALTED, "Salted"},
{PGP_S2KS_ITERATED_AND_SALTED, "Iterated and salted"},
{0, NULL}};
static const id_str_pair key_usage_map[] = {
{PGP_KF_SIGN, "sign"},
{PGP_KF_CERTIFY, "certify"},
{PGP_KF_ENCRYPT, "encrypt"},
{PGP_KF_AUTH, "authenticate"},
{0, NULL},
};
static const id_str_pair key_flags_map[] = {
{PGP_KF_SPLIT, "split"},
{PGP_KF_SHARED, "shared"},
{0, NULL},
};
static const id_str_pair key_server_prefs_map[] = {{PGP_KEY_SERVER_NO_MODIFY, "no-modify"},
{0, NULL}};
static const id_str_pair armor_type_map[] = {{PGP_ARMORED_MESSAGE, "message"},
{PGP_ARMORED_PUBLIC_KEY, "public key"},
{PGP_ARMORED_SECRET_KEY, "secret key"},
{PGP_ARMORED_SIGNATURE, "signature"},
{PGP_ARMORED_CLEARTEXT, "cleartext"},
{0, NULL}};
static const id_str_pair key_import_status_map[] = {
{PGP_KEY_IMPORT_STATUS_UNKNOWN, "unknown"},
{PGP_KEY_IMPORT_STATUS_UNCHANGED, "unchanged"},
{PGP_KEY_IMPORT_STATUS_UPDATED, "updated"},
{PGP_KEY_IMPORT_STATUS_NEW, "new"},
{0, NULL}};
static const id_str_pair sig_import_status_map[] = {
{PGP_SIG_IMPORT_STATUS_UNKNOWN, "unknown"},
{PGP_SIG_IMPORT_STATUS_UNKNOWN_KEY, "unknown key"},
{PGP_SIG_IMPORT_STATUS_UNCHANGED, "unchanged"},
{PGP_SIG_IMPORT_STATUS_NEW, "new"},
{0, NULL}};
static const id_str_pair revocation_code_map[] = {
{PGP_REVOCATION_NO_REASON, "no"},
{PGP_REVOCATION_SUPERSEDED, "superseded"},
{PGP_REVOCATION_COMPROMISED, "compromised"},
{PGP_REVOCATION_RETIRED, "retired"},
{PGP_REVOCATION_NO_LONGER_VALID, "no longer valid"},
{0, NULL}};
static bool
symm_alg_supported(int alg)
{
return pgp_is_sa_supported(alg, true);
}
static bool
hash_alg_supported(int alg)
{
switch (alg) {
case PGP_HASH_MD5:
case PGP_HASH_SHA1:
#if defined(ENABLE_RIPEMD160)
case PGP_HASH_RIPEMD:
#endif
case PGP_HASH_SHA256:
case PGP_HASH_SHA384:
case PGP_HASH_SHA512:
case PGP_HASH_SHA224:
case PGP_HASH_SHA3_256:
case PGP_HASH_SHA3_512:
#if defined(ENABLE_SM2)
case PGP_HASH_SM3:
#endif
return true;
default:
return false;
}
}
static bool
aead_alg_supported(int alg)
{
switch (alg) {
case PGP_AEAD_NONE:
#if defined(ENABLE_AEAD)
#if !defined(CRYPTO_BACKEND_OPENSSL)
case PGP_AEAD_EAX:
#endif
case PGP_AEAD_OCB:
#endif
return true;
default:
return false;
}
}
static bool
pub_alg_supported(int alg)
{
switch (alg) {
case PGP_PKA_RSA:
case PGP_PKA_ELGAMAL:
case PGP_PKA_DSA:
case PGP_PKA_ECDH:
case PGP_PKA_ECDSA:
case PGP_PKA_EDDSA:
#if defined(ENABLE_SM2)
case PGP_PKA_SM2:
#endif
#if defined(ENABLE_CRYPTO_REFRESH)
case PGP_PKA_X25519:
case PGP_PKA_ED25519:
#endif
#if defined(ENABLE_PQC)
case PGP_PKA_KYBER768_X25519:
// case PGP_PKA_KYBER1024_X448:
case PGP_PKA_KYBER768_P256:
case PGP_PKA_KYBER1024_P384:
case PGP_PKA_KYBER768_BP256:
case PGP_PKA_KYBER1024_BP384:
case PGP_PKA_DILITHIUM3_ED25519:
// case PGP_PKA_DILITHIUM5_ED448:
case PGP_PKA_DILITHIUM3_P256:
case PGP_PKA_DILITHIUM5_P384:
case PGP_PKA_DILITHIUM3_BP256:
case PGP_PKA_DILITHIUM5_BP384:
case PGP_PKA_SPHINCSPLUS_SHA2:
case PGP_PKA_SPHINCSPLUS_SHAKE:
#endif
return true;
default:
return false;
}
}
static bool
z_alg_supported(int alg)
{
switch (alg) {
case PGP_C_NONE:
case PGP_C_ZIP:
case PGP_C_ZLIB:
case PGP_C_BZIP2:
return true;
default:
return false;
}
}
static bool
curve_str_to_type(const char *str, pgp_curve_t *value)
{
*value = pgp::ec::Curve::by_name(str);
return pgp::ec::Curve::is_supported(*value);
}
static bool
curve_type_to_str(pgp_curve_t type, const char **str)
{
auto desc = pgp::ec::Curve::get(type);
if (!desc) {
return false;
}
*str = desc->pgp_name;
return true;
}
static bool
str_to_cipher(const char *str, pgp_symm_alg_t *cipher)
{
auto alg = id_str_pair::lookup(symm_alg_map, str, PGP_SA_UNKNOWN);
if (!symm_alg_supported(alg)) {
return false;
}
*cipher = static_cast<pgp_symm_alg_t>(alg);
return true;
}
static bool
str_to_hash_alg(const char *str, pgp_hash_alg_t *hash_alg)
{
auto alg = id_str_pair::lookup(hash_alg_map, str, PGP_HASH_UNKNOWN);
if (!hash_alg_supported(alg)) {
return false;
}
*hash_alg = static_cast<pgp_hash_alg_t>(alg);
return true;
}
static bool
str_to_aead_alg(const char *str, pgp_aead_alg_t *aead_alg)
{
auto alg = id_str_pair::lookup(aead_alg_map, str, PGP_AEAD_UNKNOWN);
if (!aead_alg_supported(alg)) {
return false;
}
*aead_alg = static_cast<pgp_aead_alg_t>(alg);
return true;
}
static bool
str_to_compression_alg(const char *str, pgp_compression_type_t *zalg)
{
auto alg = id_str_pair::lookup(compress_alg_map, str, PGP_C_UNKNOWN);
if (!z_alg_supported(alg)) {
return false;
}
*zalg = static_cast<pgp_compression_type_t>(alg);
return true;
}
static bool
str_to_revocation_type(const char *str, pgp_revocation_type_t *code)
{
pgp_revocation_type_t rev = static_cast<pgp_revocation_type_t>(
id_str_pair::lookup(revocation_code_map, str, PGP_REVOCATION_NO_REASON));
if ((rev == PGP_REVOCATION_NO_REASON) && !rnp::str_case_eq(str, "no")) {
return false;
}
*code = rev;
return true;
}
static bool
str_to_cipher_mode(const char *str, pgp_cipher_mode_t *mode)
{
pgp_cipher_mode_t c_mode = static_cast<pgp_cipher_mode_t>(
id_str_pair::lookup(cipher_mode_map, str, PGP_CIPHER_MODE_NONE));
if (c_mode == PGP_CIPHER_MODE_NONE) {
return false;
}
*mode = c_mode;
return true;
}
static bool
str_to_pubkey_alg(const char *str, pgp_pubkey_alg_t *pub_alg)
{
auto alg = id_str_pair::lookup(pubkey_alg_map, str, PGP_PKA_NOTHING);
if (!pub_alg_supported(alg)) {
return false;
}
*pub_alg = static_cast<pgp_pubkey_alg_t>(alg);
return true;
}
static bool
str_to_key_flag(const char *str, uint8_t *flag)
{
uint8_t _flag = id_str_pair::lookup(key_usage_map, str);
if (!_flag) {
return false;
}
*flag = _flag;
return true;
}
static bool
parse_ks_format(rnp::KeyFormat *key_store_format, const char *format)
{
if (!strcmp(format, RNP_KEYSTORE_GPG)) {
*key_store_format = rnp::KeyFormat::GPG;
} else if (!strcmp(format, RNP_KEYSTORE_KBX)) {
*key_store_format = rnp::KeyFormat::KBX;
} else if (!strcmp(format, RNP_KEYSTORE_G10)) {
*key_store_format = rnp::KeyFormat::G10;
} else {
return false;
}
return true;
}
static rnp_result_t
hex_encode_value(const uint8_t *value, size_t len, char **res)
{
size_t hex_len = len * 2 + 1;
*res = (char *) malloc(hex_len);
if (!*res) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}
if (!rnp::hex_encode(value, len, *res, hex_len, rnp::HexFormat::Uppercase)) {
/* LCOV_EXCL_START */
free(*res);
*res = NULL;
return RNP_ERROR_GENERIC;
/* LCOV_EXCL_END */
}
return RNP_SUCCESS;
}
static rnp_result_t
ret_str_value(const char *str, char **res)
{
if (!str) {
return RNP_ERROR_BAD_PARAMETERS;
}
char *strcp = strdup(str);
if (!strcp) {
*res = NULL; // LCOV_EXCL_LINE
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}
*res = strcp;
return RNP_SUCCESS;
}
static rnp_result_t
ret_vec_value(const std::vector<uint8_t> &vec, uint8_t **buf, size_t *buf_len)
{
*buf = (uint8_t *) calloc(1, vec.size());
if (!*buf) {
return RNP_ERROR_OUT_OF_MEMORY;
}
memcpy(*buf, vec.data(), vec.size());
*buf_len = vec.size();
return RNP_SUCCESS;
}
static rnp_result_t
get_map_value(const id_str_pair *map, int val, char **res)
{
return ret_str_value(id_str_pair::lookup(map, val, NULL), res);
}
static rnp_result_t
ret_fingerprint(const pgp::Fingerprint &fp, char **res)
{
return hex_encode_value(fp.data(), fp.size(), res);
}
static rnp_result_t
ret_keyid(const pgp::KeyID &keyid, char **res)
{
return hex_encode_value(keyid.data(), keyid.size(), res);
}
static rnp_result_t
ret_grip(const pgp::KeyGrip &grip, char **res)
{
return hex_encode_value(grip.data(), grip.size(), res);
}
static uint32_t
ffi_exception(FILE *fp, const char *func, const char *msg, uint32_t ret = RNP_ERROR_GENERIC)
{
if (rnp_log_switch()) {
fprintf(
fp, "[%s()] Error 0x%08X (%s): %s\n", func, ret, rnp_result_to_string(ret), msg);
}
return ret;
}
#define FFI_GUARD_FP(fp) \
catch (rnp::rnp_exception & e) \
{ \
return ffi_exception((fp), __func__, e.what(), e.code()); \
} \
catch (std::bad_alloc &) \
{ \
return ffi_exception((fp), __func__, "bad_alloc", RNP_ERROR_OUT_OF_MEMORY); \
} \
catch (std::exception & e) \
{ \
return ffi_exception((fp), __func__, e.what()); \
} \
catch (...) \
{ \
return ffi_exception((fp), __func__, "unknown exception"); \
}
#define FFI_GUARD FFI_GUARD_FP((stderr))
rnp_ffi_st::rnp_ffi_st(rnp::KeyFormat pub_fmt, rnp::KeyFormat sec_fmt)
{
errs = stderr;
pubring = new rnp::KeyStore("", context, pub_fmt);
secring = new rnp::KeyStore("", context, sec_fmt);
getkeycb = NULL;
getkeycb_ctx = NULL;
getpasscb = NULL;
getpasscb_ctx = NULL;
key_provider.callback = ffi_key_provider;
key_provider.userdata = this;
pass_provider.callback = rnp_password_cb_bounce;
pass_provider.userdata = this;
}
rnp::RNG &
rnp_ffi_st::rng() noexcept
{
return context.rng;
}
rnp::SecurityProfile &
rnp_ffi_st::profile() noexcept
{
return context.profile;
}
rnp_result_t
rnp_ffi_create(rnp_ffi_t *ffi, const char *pub_format, const char *sec_format)
try {
// checks
if (!ffi || !pub_format || !sec_format) {
return RNP_ERROR_NULL_POINTER;
}
auto pub_ks_format = rnp::KeyFormat::Unknown;
auto sec_ks_format = rnp::KeyFormat::Unknown;
if (!parse_ks_format(&pub_ks_format, pub_format) ||
!parse_ks_format(&sec_ks_format, sec_format)) {
return RNP_ERROR_BAD_PARAMETERS;
}
struct rnp_ffi_st *ob = new rnp_ffi_st(pub_ks_format, sec_ks_format);
*ffi = ob;
return RNP_SUCCESS;
}
FFI_GUARD
static bool
is_std_file(FILE *fp)
{
return fp == stdout || fp == stderr;
}
static void
close_io_file(FILE **fp)
{
if (*fp && !is_std_file(*fp)) {
fclose(*fp);
}
*fp = NULL;
}
rnp_ffi_st::~rnp_ffi_st()
{
close_io_file(&errs);
delete pubring;
delete secring;
}
rnp_result_t
rnp_ffi_destroy(rnp_ffi_t ffi)
try {
if (ffi) {
delete ffi;
}
return RNP_SUCCESS;
}
FFI_GUARD
rnp_result_t
rnp_ffi_set_log_fd(rnp_ffi_t ffi, int fd)
try {
// checks
if (!ffi) {
return RNP_ERROR_NULL_POINTER;
}
// open
FILE *errs = rnp_fdopen(fd, "a");
if (!errs) {
return RNP_ERROR_ACCESS;
}
// close previous streams and replace them
close_io_file(&ffi->errs);
ffi->errs = errs;
return RNP_SUCCESS;
}
FFI_GUARD
rnp_result_t
rnp_ffi_set_key_provider(rnp_ffi_t ffi, rnp_get_key_cb getkeycb, void *getkeycb_ctx)
try {
if (!ffi) {
return RNP_ERROR_NULL_POINTER;
}
ffi->getkeycb = getkeycb;
ffi->getkeycb_ctx = getkeycb_ctx;
return RNP_SUCCESS;
}
FFI_GUARD
rnp_result_t
rnp_ffi_set_pass_provider(rnp_ffi_t ffi, rnp_password_cb getpasscb, void *getpasscb_ctx)
try {
if (!ffi) {
return RNP_ERROR_NULL_POINTER;
}
ffi->getpasscb = getpasscb;
ffi->getpasscb_ctx = getpasscb_ctx;
return RNP_SUCCESS;
}
FFI_GUARD
static const char *
operation_description(uint8_t op)
{
switch (op) {
case PGP_OP_ADD_SUBKEY:
return "add subkey";
case PGP_OP_ADD_USERID:
return "add userid";
case PGP_OP_SIGN:
return "sign";
case PGP_OP_DECRYPT:
return "decrypt";
case PGP_OP_UNLOCK:
return "unlock";
case PGP_OP_PROTECT:
return "protect";
case PGP_OP_UNPROTECT:
return "unprotect";
case PGP_OP_DECRYPT_SYM:
return "decrypt (symmetric)";
case PGP_OP_ENCRYPT_SYM:
return "encrypt (symmetric)";
default:
return "unknown";
}
}
static bool
rnp_password_cb_bounce(const pgp_password_ctx_t *ctx,
char * password,
size_t password_size,
void * userdata_void)
{
rnp_ffi_t ffi = (rnp_ffi_t) userdata_void;
if (!ffi || !ffi->getpasscb) {
return false;
}
rnp_key_handle_st key(ffi, nullptr, (rnp::Key *) ctx->key);
return ffi->getpasscb(ffi,
ffi->getpasscb_ctx,
ctx->key ? &key : NULL,
operation_description(ctx->op),
password,
password_size);
}
const char *
rnp_result_to_string(rnp_result_t result)
{
switch (result) {
case RNP_SUCCESS:
return "Success";
case RNP_ERROR_GENERIC:
return "Unknown error";
case RNP_ERROR_BAD_FORMAT:
return "Bad format";
case RNP_ERROR_BAD_PARAMETERS:
return "Bad parameters";
case RNP_ERROR_NOT_IMPLEMENTED:
return "Not implemented";
case RNP_ERROR_NOT_SUPPORTED:
return "Not supported";
case RNP_ERROR_OUT_OF_MEMORY:
return "Out of memory";
case RNP_ERROR_SHORT_BUFFER:
return "Buffer too short";
case RNP_ERROR_NULL_POINTER:
return "Null pointer";
case RNP_ERROR_ACCESS:
return "Error accessing file";
case RNP_ERROR_READ:
return "Error reading file";
case RNP_ERROR_WRITE:
return "Error writing file";
case RNP_ERROR_BAD_STATE:
return "Bad state";
case RNP_ERROR_MAC_INVALID:
return "Invalid MAC";
case RNP_ERROR_SIGNATURE_INVALID:
return "Invalid signature";
case RNP_ERROR_KEY_GENERATION:
return "Error during key generation";
case RNP_ERROR_BAD_PASSWORD:
return "Bad password";
case RNP_ERROR_KEY_NOT_FOUND:
return "Key not found";
case RNP_ERROR_NO_SUITABLE_KEY:
return "No suitable key";
case RNP_ERROR_DECRYPT_FAILED:
return "Decryption failed";
case RNP_ERROR_ENCRYPT_FAILED:
return "Encryption failed";
case RNP_ERROR_RNG:
return "Failure of random number generator";
case RNP_ERROR_SIGNING_FAILED:
return "Signing failed";
case RNP_ERROR_NO_SIGNATURES_FOUND:
return "No signatures found cannot verify";
case RNP_ERROR_SIGNATURE_EXPIRED:
return "Expired signature";
case RNP_ERROR_VERIFICATION_FAILED:
return "Signature verification failed cannot verify";
case RNP_ERROR_SIGNATURE_UNKNOWN:
return "Unknown signature";
case RNP_ERROR_NOT_ENOUGH_DATA:
return "Not enough data";
case RNP_ERROR_UNKNOWN_TAG:
return "Unknown tag";
case RNP_ERROR_PACKET_NOT_CONSUMED:
return "Packet not consumed";
case RNP_ERROR_NO_USERID:
return "No userid";
case RNP_ERROR_EOF:
return "EOF detected";
}
return "Unsupported error code";
}
const char *
rnp_version_string()
{
return RNP_VERSION_STRING;
}
const char *
rnp_version_string_full()
{
return RNP_VERSION_STRING_FULL;
}
uint32_t
rnp_version()
{
return RNP_VERSION_CODE;
}
uint32_t
rnp_version_for(uint32_t major, uint32_t minor, uint32_t patch)
{
if (major > RNP_VERSION_COMPONENT_MASK || minor > RNP_VERSION_COMPONENT_MASK ||
patch > RNP_VERSION_COMPONENT_MASK) {
RNP_LOG("invalid version, out of range: %d.%d.%d", major, minor, patch);
return 0;
}
return RNP_VERSION_CODE_FOR(major, minor, patch);
}
uint32_t
rnp_version_major(uint32_t version)
{
return (version >> RNP_VERSION_MAJOR_SHIFT) & RNP_VERSION_COMPONENT_MASK;
}
uint32_t
rnp_version_minor(uint32_t version)
{
return (version >> RNP_VERSION_MINOR_SHIFT) & RNP_VERSION_COMPONENT_MASK;
}
uint32_t
rnp_version_patch(uint32_t version)
{
return (version >> RNP_VERSION_PATCH_SHIFT) & RNP_VERSION_COMPONENT_MASK;
}
uint64_t
rnp_version_commit_timestamp()
{
return RNP_VERSION_COMMIT_TIMESTAMP;
}
#ifndef RNP_NO_DEPRECATED
/* LCOV_EXCL_START */
rnp_result_t
rnp_enable_debug(const char *file)
try {
return RNP_SUCCESS;
}
FFI_GUARD
/* LCOV_EXCL_END */
#endif
#ifndef RNP_NO_DEPRECATED
/* LCOV_EXCL_START */
rnp_result_t
rnp_disable_debug()
try {
return RNP_SUCCESS;
}
FFI_GUARD
/* LCOV_EXCL_END */
#endif
rnp_result_t
rnp_get_default_homedir(char **homedir)
try {
// checks
if (!homedir) {
return RNP_ERROR_NULL_POINTER;
}
// get the users home dir
auto home = rnp::path::HOME(".rnp");
if (home.empty()) {
return RNP_ERROR_NOT_SUPPORTED;
}
return ret_str_value(home.c_str(), homedir);
}
FFI_GUARD
rnp_result_t
rnp_detect_homedir_info(
const char *homedir, char **pub_format, char **pub_path, char **sec_format, char **sec_path)
try {
// checks
if (!homedir || !pub_format || !pub_path || !sec_format || !sec_path) {
return RNP_ERROR_NULL_POINTER;
}
// we only support the common cases of GPG+GPG or GPG+G10, we don't
// support unused combinations like KBX+KBX
*pub_format = NULL;
*pub_path = NULL;
*sec_format = NULL;
*sec_path = NULL;
// check for pubring.kbx file and for private-keys-v1.d dir
std::string pub = rnp::path::append(homedir, "pubring.kbx");
std::string sec = rnp::path::append(homedir, "private-keys-v1.d");
if (rnp::path::exists(pub) && rnp::path::exists(sec, true)) {
*pub_format = strdup("KBX");
*sec_format = strdup("G10");
} else {
// check for pubring.gpg and secring.gpg
pub = rnp::path::append(homedir, "pubring.gpg");
sec = rnp::path::append(homedir, "secring.gpg");
if (rnp::path::exists(pub) && rnp::path::exists(sec)) {
*pub_format = strdup("GPG");
*sec_format = strdup("GPG");
} else {
// we leave the *formats as NULL if we were not able to determine the format
// (but no error occurred)
return RNP_SUCCESS;
}
}
// set paths
*pub_path = strdup(pub.c_str());
*sec_path = strdup(sec.c_str());
// check for allocation failures
if (*pub_format && *pub_path && *sec_format && *sec_path) {
return RNP_SUCCESS;
}
/* LCOV_EXCL_START */
free(*pub_format);
*pub_format = NULL;
free(*pub_path);
*pub_path = NULL;