From 5b561c2f11249624dd7fac5dcbd3ed4b69512a48 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 15 Mar 2001 13:09:41 +0000 Subject: [PATCH] Give access to key capabilities --- gpgme/ChangeLog | 17 +++++++++++++++++ gpgme/gpgme.h | 6 +++++- gpgme/key.c | 36 ++++++++++++++++++++++++++++++++++++ gpgme/key.h | 6 ++++++ gpgme/keylist.c | 40 +++++++++++++++++++++++++++++++++++++--- gpgme/posix-util.c | 3 +++ gpgme/rungpg.c | 1 + tests/t-keylist.c | 25 +++++++++++++++++++++---- 8 files changed, 126 insertions(+), 8 deletions(-) diff --git a/gpgme/ChangeLog b/gpgme/ChangeLog index 9f1f83ba..45607f9e 100644 --- a/gpgme/ChangeLog +++ b/gpgme/ChangeLog @@ -1,3 +1,20 @@ +2001-03-15 Werner Koch + + * rungpg.c: Included time.h. + + * key.h: New keyflags for capabilities. + * keylist.c (set_mainkey_capability, set_subkey_capability): New. + (keylist_colon_handler): Parse them. + * gpgme.h: New attribute values for capabilties. + * key.c (gpgme_key_get_string_attr): Return them. + (capabilities_to_string): New. + (gpgme_key_get_ulong_attr): Return the global caps. + +2001-03-14 Werner Koch + + * w32-io.c (destroy_reader,destroy_writer): Fixed syntax error. + Thanks to Jan Oliver Wagner. + 2001-03-13 Werner Koch * context.h: Add invalid and revoke flags to user_id structure. diff --git a/gpgme/gpgme.h b/gpgme/gpgme.h index 562af198..f1744f02 100644 --- a/gpgme/gpgme.h +++ b/gpgme/gpgme.h @@ -133,7 +133,11 @@ typedef enum { GPGME_ATTR_KEY_REVOKED = 16, GPGME_ATTR_KEY_INVALID = 17, GPGME_ATTR_UID_REVOKED = 18, - GPGME_ATTR_UID_INVALID = 19 + GPGME_ATTR_UID_INVALID = 19, + GPGME_ATTR_KEY_CAPS = 20, + GPGME_ATTR_CAN_ENCRYPT = 21, + GPGME_ATTR_CAN_SIGN = 22, + GPGME_ATTR_CAN_CERTIFY = 23 } GpgmeAttr; typedef enum { diff --git a/gpgme/key.c b/gpgme/key.c index 35a54043..6f8f03b6 100644 --- a/gpgme/key.c +++ b/gpgme/key.c @@ -455,6 +455,24 @@ gpgme_key_get_as_xml ( GpgmeKey key ) } +static const char * +capabilities_to_string (struct subkey_s *k) +{ + static char *strings[8] = { + "", + "c", + "s", + "sc", + "e", + "ec", + "es", + "esc" + }; + return strings[ (!!k->flags.can_encrypt << 2) + | (!!k->flags.can_sign << 1) + | (!!k->flags.can_certify ) ]; +} + const char * gpgme_key_get_string_attr ( GpgmeKey key, GpgmeAttr what, const void *reserved, int idx ) @@ -536,11 +554,20 @@ gpgme_key_get_string_attr ( GpgmeKey key, GpgmeAttr what, case GPGME_ATTR_KEY_INVALID: case GPGME_ATTR_UID_REVOKED: case GPGME_ATTR_UID_INVALID: + case GPGME_ATTR_CAN_ENCRYPT: + case GPGME_ATTR_CAN_SIGN: + case GPGME_ATTR_CAN_CERTIFY: break; case GPGME_ATTR_IS_SECRET: if (key->secret) val = "1"; break; + case GPGME_ATTR_KEY_CAPS: + for (k=&key->keys; k && idx; k=k->next, idx-- ) + ; + if (k) + val = capabilities_to_string (k); + break; } return val; } @@ -613,6 +640,15 @@ gpgme_key_get_ulong_attr ( GpgmeKey key, GpgmeAttr what, if (u) val = u->invalid; break; + case GPGME_ATTR_CAN_ENCRYPT: + val = key->gloflags.can_encrypt; + break; + case GPGME_ATTR_CAN_SIGN: + val = key->gloflags.can_sign; + break; + case GPGME_ATTR_CAN_CERTIFY: + val = key->gloflags.can_encrypt; + break; default: break; } diff --git a/gpgme/key.h b/gpgme/key.h index 83d46ed2..5e9d4c6d 100644 --- a/gpgme/key.h +++ b/gpgme/key.h @@ -33,6 +33,9 @@ struct subkey_s { unsigned int expired:1 ; unsigned int disabled:1 ; unsigned int invalid:1 ; + unsigned int can_encrypt:1; + unsigned int can_sign:1; + unsigned int can_certify:1; } flags; unsigned int key_algo; unsigned int key_len; @@ -47,6 +50,9 @@ struct gpgme_key_s { unsigned int expired:1 ; unsigned int disabled:1 ; unsigned int invalid:1 ; + unsigned int can_encrypt:1; + unsigned int can_sign:1; + unsigned int can_certify:1; } gloflags; unsigned int ref_count; unsigned int secret:1; diff --git a/gpgme/keylist.c b/gpgme/keylist.c index a42cde85..19eaa2a3 100644 --- a/gpgme/keylist.c +++ b/gpgme/keylist.c @@ -135,6 +135,34 @@ set_subkey_trust_info ( struct subkey_s *k, const char *s ) } } +static void +set_mainkey_capability ( GpgmeKey key, const char *s ) +{ + for (; *s ; s++ ) { + switch (*s) { + case 'e': key->keys.flags.can_encrypt = 1; break; + case 's': key->keys.flags.can_sign = 1; break; + case 'c': key->keys.flags.can_certify = 1; break; + case 'E': key->gloflags.can_encrypt = 1; break; + case 'S': key->gloflags.can_sign = 1; break; + case 'C': key->gloflags.can_certify = 1; break; + } + } +} + +static void +set_subkey_capability ( struct subkey_s *k, const char *s ) +{ + for (; *s; s++ ) { + switch (*s) { + case 'e': k->flags.can_encrypt = 1; break; + case 's': k->flags.can_sign = 1; break; + case 'c': k->flags.can_certify = 1; break; + } + } +} + + /* Note: we are allowed to modify line */ static void @@ -245,9 +273,12 @@ keylist_colon_handler ( GpgmeCtx ctx, char *line ) break; case 10: /* not used due to --fixed-list-mode option */ break; - case 11: /* signature class */ + case 11: /* signature class */ break; - case 12: + case 12: /* capabilities */ + set_mainkey_capability (key, p ); + break; + case 13: pend = NULL; /* we can stop here */ break; } @@ -284,7 +315,10 @@ keylist_colon_handler ( GpgmeCtx ctx, char *line ) break; case 11: /* signature class */ break; - case 12: + case 12: /* capability */ + set_subkey_capability ( sk, p ); + break; + case 13: pend = NULL; /* we can stop here */ break; } diff --git a/gpgme/posix-util.c b/gpgme/posix-util.c index 3c0c692a..38fb2f03 100644 --- a/gpgme/posix-util.c +++ b/gpgme/posix-util.c @@ -33,6 +33,9 @@ const char * _gpgme_get_gpg_path (void) { + /* #warning Forced to development version + return "/home/wk/work/gnupg-stable/g10/gpg"; + */ return GPG_PATH; } diff --git a/gpgme/rungpg.c b/gpgme/rungpg.c index 404f7fec..bed8aaca 100644 --- a/gpgme/rungpg.c +++ b/gpgme/rungpg.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/tests/t-keylist.c b/tests/t-keylist.c index fb7b79d5..317990c6 100644 --- a/tests/t-keylist.c +++ b/tests/t-keylist.c @@ -54,10 +54,17 @@ doit ( GpgmeCtx ctx, const char *pattern ) else fputs("\n", stdout ); - s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0 ); - printf ("\n", s ); - s = gpgme_key_get_string_attr (key, GPGME_ATTR_ALGO, NULL, 0 ); - printf ("\n", s ); + + for (i=0; ; i++ ) { + s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, i ); + if (!s) + break; + printf ("\n", i, s ); + s = gpgme_key_get_string_attr (key, GPGME_ATTR_ALGO, NULL, i ); + printf ("\n", i, s ); + s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEY_CAPS, NULL, i ); + printf ("\n", i, s ); + } for (i=0; ; i++ ) { s = gpgme_key_get_string_attr (key, GPGME_ATTR_NAME, NULL, i ); if (!s) @@ -68,6 +75,15 @@ doit ( GpgmeCtx ctx, const char *pattern ) s = gpgme_key_get_string_attr (key, GPGME_ATTR_COMMENT, NULL, i ); printf ("\n", i, s ); } + + fputs ("\n", stdout ); printf ("\n", key ); gpgme_key_release (key); @@ -97,6 +113,7 @@ main (int argc, char **argv ) err = gpgme_new (&ctx); fail_if_err (err); + gpgme_set_keylist_mode (ctx, 1); /* no validity calculation */ do { fprintf (stderr, "** pattern=`%s'\n", pattern ); doit ( ctx, pattern );