@@ -100,6 +100,7 @@ __RCSID("$NetBSD: packet-print.c,v 1.42 2012/02/22 06:29:40 agc Exp $");
100
100
#define SIGNATURE_PADDING " "
101
101
102
102
/* static functions */
103
+ static bool format_key_usage (char * buffer , size_t size , uint8_t flags );
103
104
extern ec_curve_desc_t ec_curves [PGP_CURVE_MAX ];
104
105
105
106
static void
@@ -385,14 +386,17 @@ psubkeybinding(char *buf, size_t size, const pgp_key_t *key, const char *expired
385
386
{
386
387
char keyid [512 ];
387
388
char t [32 ];
389
+ char key_usage [8 ];
388
390
391
+ format_key_usage (key_usage , sizeof (key_usage ), key -> flags );
389
392
return snprintf (buf ,
390
393
size ,
391
- "encryption %d/%s %s %s %s\n" ,
394
+ "encryption %d/%s %s %s [%s] %s\n" ,
392
395
numkeybits (& key -> enckey ),
393
396
pgp_show_pka (key -> enckey .alg ),
394
397
rnp_strhexdump (keyid , key -> encid , PGP_KEY_ID_SIZE , "" ),
395
398
ptimestr (t , sizeof (t ), key -> enckey .birthtime ),
399
+ key_usage ,
396
400
expired );
397
401
}
398
402
@@ -557,6 +561,29 @@ format_uid_notice(char * buffer,
557
561
return n ;
558
562
}
559
563
564
+ static bool
565
+ format_key_usage (char * buffer , size_t size , uint8_t flags )
566
+ {
567
+ static const pgp_bit_map_t flags_map [] = {
568
+ {PGP_KF_ENCRYPT_COMMS | PGP_KF_ENCRYPT_STORAGE , "E" },
569
+ {PGP_KF_SIGN , "S" },
570
+ {PGP_KF_CERTIFY , "C" },
571
+ {PGP_KF_AUTH , "A" },
572
+ };
573
+
574
+ * buffer = '\0' ;
575
+ for (size_t i = 0 ; i < PGP_ARRAY_SIZE (flags_map ); i ++ ) {
576
+ if (flags & flags_map [i ].mask ) {
577
+ const size_t current_length = strlen (buffer );
578
+ if (current_length == size - 1 ) {
579
+ return false;
580
+ }
581
+ strncat (buffer , flags_map [i ].string , size - current_length - 1 );
582
+ }
583
+ }
584
+ return true;
585
+ }
586
+
560
587
#ifndef KB
561
588
#define KB (x ) ((x) *1024)
562
589
#endif
@@ -584,6 +611,7 @@ pgp_sprint_keydata(pgp_io_t * io,
584
611
char fingerprint [(PGP_FINGERPRINT_SIZE * 3 ) + 1 ];
585
612
char expiration_notice [128 ];
586
613
char birthtime [32 ];
614
+ char key_usage [8 ];
587
615
588
616
if (key -> revoked )
589
617
return -1 ;
@@ -628,6 +656,10 @@ pgp_sprint_keydata(pgp_io_t * io,
628
656
629
657
ptimestr (birthtime , sizeof (birthtime ), pubkey -> birthtime );
630
658
659
+ if (!format_key_usage (key_usage , sizeof (key_usage ), key -> flags )) {
660
+ return -1 ;
661
+ }
662
+
631
663
/* XXX: For now we assume that the output string won't exceed 16KiB
632
664
* in length but this is completely arbitrary. What this
633
665
* really needs is some objective facts to base this
@@ -639,12 +671,13 @@ pgp_sprint_keydata(pgp_io_t * io,
639
671
if (string != NULL ) {
640
672
total_length = snprintf (string ,
641
673
KB (16 ),
642
- "%s %d/%s %s %s %s\nKey fingerprint: %s\n%s" ,
674
+ "%s %d/%s %s %s [%s] %s\nKey fingerprint: %s\n%s" ,
643
675
header ,
644
676
numkeybits (pubkey ),
645
677
pgp_show_pka (pubkey -> alg ),
646
678
keyid ,
647
679
birthtime ,
680
+ key_usage ,
648
681
expiration_notice ,
649
682
fingerprint ,
650
683
uid_notices );
@@ -668,6 +701,7 @@ pgp_sprint_json(pgp_io_t * io,
668
701
{
669
702
char keyid [PGP_KEY_ID_SIZE * 3 ];
670
703
char fp [(PGP_FINGERPRINT_SIZE * 3 ) + 1 ];
704
+ char key_usage [8 ];
671
705
int r ;
672
706
unsigned i ;
673
707
unsigned j ;
@@ -676,6 +710,10 @@ pgp_sprint_json(pgp_io_t * io,
676
710
return -1 ;
677
711
}
678
712
713
+ if (!format_key_usage (key_usage , sizeof (key_usage ), key -> flags )) {
714
+ return -1 ;
715
+ }
716
+
679
717
// add the top-level values
680
718
json_object_object_add (keyjson , "header" , json_object_new_string (header ));
681
719
json_object_object_add (keyjson , "key bits" , json_object_new_int (numkeybits (pubkey )));
@@ -691,6 +729,8 @@ pgp_sprint_json(pgp_io_t * io,
691
729
rnp_strhexdump (fp , key -> sigfingerprint .fingerprint , key -> sigfingerprint .length , "" )));
692
730
json_object_object_add (keyjson , "birthtime" , json_object_new_int (pubkey -> birthtime ));
693
731
json_object_object_add (keyjson , "duration" , json_object_new_int (pubkey -> duration ));
732
+ json_object_object_add (keyjson , "flags" , json_object_new_int (key -> flags ));
733
+ json_object_object_add (keyjson , "usage" , json_object_new_string (key_usage ));
694
734
695
735
// iterating through the uids
696
736
for (i = 0 ; i < key -> uidc ; i ++ ) {
0 commit comments