1 |
/* key.c - Key and keyList objects |
/* key.c - Key objects |
2 |
* Copyright (C) 2000, 2001 Werner Koch (dd9jn), g10 Code GmbH |
* Copyright (C) 2000, 2001 Werner Koch (dd9jn), g10 Code GmbH |
3 |
* Copyright (C) 2001-2004 Timo Schulz |
* Copyright (C) 2001-2005 Timo Schulz |
4 |
* |
* |
5 |
* This file is part of MyGPGME. |
* This file is part of MyGPGME. |
6 |
* |
* |
31 |
#include "key.h" |
#include "key.h" |
32 |
|
|
33 |
static const char * |
static const char * |
34 |
pkalgo_to_string( int algo ) |
pkalgo_to_string (int algo) |
35 |
{ |
{ |
36 |
switch( algo ) { |
switch (algo) { |
37 |
case 0: |
case 0: |
38 |
case 1: |
case 1: |
39 |
case 2: |
case 2: |
40 |
case 3: return "RSA"; |
case 3: return "RSA"; |
41 |
case 16: |
case 16: |
|
case 20: return "ElG"; |
|
42 |
case 17: return "DSA"; |
case 17: return "DSA"; |
43 |
default: return "Unknown"; |
case 20: return "ELG"; |
44 |
|
default: return "???"; |
45 |
} |
} |
46 |
} /* pkalgo_to_string */ |
} /* pkalgo_to_string */ |
47 |
|
|
48 |
|
|
49 |
static gpgme_error_t |
static gpgme_error_t |
50 |
key_new( gpgme_key_t *r_key, int secret ) |
key_new (gpgme_key_t *r_key, int secret) |
51 |
{ |
{ |
52 |
gpgme_key_t key; |
gpgme_key_t key; |
53 |
|
|
54 |
if( r_key ) |
if (r_key) |
55 |
*r_key = NULL; |
*r_key = NULL; |
56 |
key = calloc ( 1, sizeof *key ); |
key = calloc (1, sizeof *key); |
57 |
if (!key) |
if (!key) |
58 |
return mk_error (Out_Of_Core); |
return mk_error (Out_Of_Core); |
59 |
key->ref_count = 1; |
key->ref_count = 1; |
187 |
|
|
188 |
|
|
189 |
static char * |
static char * |
190 |
set_user_id_part( char *tail, const char *buf, size_t len ) |
set_user_id_part (char *tail, const char *buf, size_t len) |
191 |
{ |
{ |
192 |
while ( len && (buf[len-1] == ' ' || buf[len-1] == '\t') ) |
while (len && (buf[len-1] == ' ' || buf[len-1] == '\t')) |
193 |
len--; |
len--; |
194 |
for ( ; len; len--) |
for (; len; len--) |
195 |
*tail++ = *buf++; |
*tail++ = *buf++; |
196 |
*tail++ = 0; |
*tail++ = 0; |
197 |
return tail; |
return tail; |
306 |
uid->next = NULL; |
uid->next = NULL; |
307 |
dst = uid->name; |
dst = uid->name; |
308 |
|
|
309 |
_gpgme_decode_c_string( s, &dst, strlen( s )+1 ); |
_gpgme_decode_c_string (s, &dst, strlen (s)+1); |
310 |
dst += strlen( s ) + 1; |
dst += strlen (s) + 1; |
311 |
parse_user_id( uid, dst ); |
parse_user_id (uid, dst); |
312 |
|
|
313 |
if (!key->uids) |
if (!key->uids) |
314 |
key->uids = uid; |
key->uids = uid; |
336 |
"es", |
"es", |
337 |
"esc" |
"esc" |
338 |
}; |
}; |
339 |
return strings[ (!!k->flags.can_encrypt << 2) |
return strings[ (!!k->flags.can_encrypt << 2) |
340 |
| (!!k->flags.can_sign << 1) |
| (!!k->flags.can_sign << 1) |
341 |
| (!!k->flags.can_certify ) ]; |
| (!!k->flags.can_certify )]; |
342 |
} |
} |
343 |
|
|
344 |
const char * |
const char * |
345 |
gpgme_key_get_string_attr( gpgme_key_t key, gpgme_attr_t what, |
gpgme_key_get_string_attr (gpgme_key_t key, gpgme_attr_t what, |
346 |
void **reserved, int idx ) |
void **reserved, int idx) |
347 |
{ |
{ |
348 |
const char *val = NULL; |
const char *val = NULL; |
349 |
struct subkey_s * k; |
struct subkey_s * k; |
483 |
case GPGME_ATTR_KEYDAT_VAL: |
case GPGME_ATTR_KEYDAT_VAL: |
484 |
for( m=key->pkey; m && idx; m=m->next, idx-- ) |
for( m=key->pkey; m && idx; m=m->next, idx-- ) |
485 |
; |
; |
486 |
if( m ) |
if (m) |
487 |
val = m->hexval; |
val = m->hexval; |
488 |
break; |
break; |
489 |
|
|
511 |
|
|
512 |
|
|
513 |
unsigned long |
unsigned long |
514 |
gpgme_key_get_ulong_attr( gpgme_key_t key, gpgme_attr_t what, |
gpgme_key_get_ulong_attr (gpgme_key_t key, gpgme_attr_t what, |
515 |
void ** reserved, int idx ) |
void ** reserved, int idx) |
516 |
{ |
{ |
517 |
unsigned long val = 0; |
unsigned long val = 0; |
518 |
struct subkey_s * k; |
struct subkey_s * k; |
712 |
val = key->gloflags.disabled; |
val = key->gloflags.disabled; |
713 |
break; |
break; |
714 |
|
|
715 |
|
case GPGME_ATTR_VERSION: { |
716 |
|
char *fpr = key->keys.fingerprint; |
717 |
|
if (!fpr) |
718 |
|
val = 4; |
719 |
|
else if (key->keys.key_algo == GPGME_PK_RSA && |
720 |
|
strlen (fpr) == 32) |
721 |
|
val = 3; |
722 |
|
} |
723 |
|
break; |
724 |
|
|
725 |
case GPGME_ATTR_KEY_USABLE: |
case GPGME_ATTR_KEY_USABLE: |
726 |
n = 3; |
n = 4; |
727 |
for (k=&key->keys; k && idx; k=k->next, idx--) |
for (k=&key->keys; k && idx; k=k->next, idx--) |
728 |
; |
; |
729 |
if (k) |
if (k) { |
|
{ |
|
730 |
if (!k->flags.revoked) |
if (!k->flags.revoked) |
731 |
n--; |
n--; |
732 |
if (!k->flags.expired) |
if (!k->flags.expired) |
733 |
n--; |
n--; |
734 |
if (!k->flags.invalid) |
if (!k->flags.invalid) |
735 |
n--; |
n--; |
736 |
|
if (!key->gloflags.disabled && !k->flags.disabled) |
737 |
|
n--; |
738 |
} |
} |
739 |
if (n == 0) |
if (n == 0) |
740 |
val = 1; |
val = 1; |
748 |
|
|
749 |
|
|
750 |
const char* |
const char* |
751 |
gpgme_key_expand_attr( int what, unsigned long attr ) |
gpgme_key_expand_attr (int what, unsigned long attr) |
752 |
{ |
{ |
753 |
static char tmpbuf[16+1]; |
static char tmpbuf[16+1]; |
754 |
struct tm *iso_date; |
struct tm *iso_date; |
755 |
|
|
756 |
switch( what ) { |
switch( what ) { |
757 |
case GPGME_ATTR_ALGO: |
case GPGME_ATTR_ALGO: |
758 |
switch( attr ) { |
return pkalgo_to_string (attr); |
|
case 0: |
|
|
case 1: |
|
|
case 2: |
|
|
case 3: return "RSA"; |
|
|
case 16: |
|
|
case 20: return "ELG"; |
|
|
case 17: return "DSA"; |
|
|
default: return "???"; |
|
|
} |
|
|
break; |
|
759 |
|
|
760 |
case GPGME_ATTR_ALGO_SHORT: |
case GPGME_ATTR_ALGO_SHORT: |
761 |
switch( attr ) { |
switch( attr ) { |
765 |
case 2: |
case 2: |
766 |
case 3: return "R"; |
case 3: return "R"; |
767 |
case 20: return "G"; |
case 20: return "G"; |
|
default: return "?"; |
|
768 |
} |
} |
769 |
break; |
return "?"; |
770 |
|
|
771 |
case GPGME_ATTR_VALIDITY: |
case GPGME_ATTR_VALIDITY: |
772 |
switch( attr ) { |
switch( attr ) { |
785 |
case GPGME_VALIDITY_FULL: return "Full"; |
case GPGME_VALIDITY_FULL: return "Full"; |
786 |
case 'u': |
case 'u': |
787 |
case GPGME_VALIDITY_ULTIMATE: return "Ultimate"; |
case GPGME_VALIDITY_ULTIMATE: return "Ultimate"; |
|
default: return "???"; |
|
788 |
} |
} |
789 |
break; |
return "???"; |
790 |
|
|
791 |
case GPGME_ATTR_CREATED: |
case GPGME_ATTR_CREATED: |
792 |
iso_date = localtime((long*) &attr); |
iso_date = localtime((long*) &attr); |
820 |
|
|
821 |
|
|
822 |
int |
int |
823 |
gpgme_key_get_cability( gpgme_key_t key, int attr, int keyidx ) |
gpgme_key_get_cability (gpgme_key_t key, int attr, int keyidx) |
824 |
{ |
{ |
825 |
struct subkey_s *s; |
struct subkey_s *s; |
826 |
|
|
847 |
return 0; |
return 0; |
848 |
} /* gpgme_key_get_cability */ |
} /* gpgme_key_get_cability */ |
849 |
|
|
850 |
|
|
851 |
int |
int |
852 |
gpgme_key_count_items( gpgme_key_t key, int what ) |
gpgme_key_count_items (gpgme_key_t key, int what) |
853 |
{ |
{ |
854 |
union { |
union { |
855 |
struct user_id_s *u; |
struct user_id_s *u; |
859 |
} dat; |
} dat; |
860 |
int count = 0; |
int count = 0; |
861 |
|
|
862 |
if( !key ) |
if (!key) |
863 |
return 0; |
return 0; |
864 |
switch( what ) { |
switch( what ) { |
865 |
case GPGME_ATTR_USERID: |
case GPGME_ATTR_USERID: |
878 |
return count; |
return count; |
879 |
|
|
880 |
case GPGME_ATTR_SIG_KEYID: |
case GPGME_ATTR_SIG_KEYID: |
881 |
for( dat.s = key->sigs; dat.s; dat.s = dat.s->next ) |
for (dat.s = key->sigs; dat.s; dat.s = dat.s->next) |
882 |
count++; |
count++; |
883 |
return count; |
return count; |
884 |
} |
} |
902 |
|
|
903 |
|
|
904 |
int |
int |
905 |
gpgme_key_cability_from_algo( gpgme_pk_cipher_t algo ) |
gpgme_key_cability_from_algo (gpgme_pk_cipher_t algo) |
906 |
{ |
{ |
907 |
switch( algo ) { |
switch (algo) { |
908 |
case GPGME_PK_DSA: |
case GPGME_PK_DSA: |
909 |
case GPGME_PK_RSA_S: |
case GPGME_PK_RSA_S: |
910 |
return GPGME_KEY_CANSIGN; |
return GPGME_KEY_CANSIGN; |
916 |
case GPGME_PK_RSA_E: |
case GPGME_PK_RSA_E: |
917 |
case GPGME_PK_ELG_E: |
case GPGME_PK_ELG_E: |
918 |
return GPGME_KEY_CANENCR; |
return GPGME_KEY_CANENCR; |
|
|
|
|
default: |
|
|
return 0; |
|
919 |
} |
} |
920 |
|
|
921 |
return 0; |
return 0; |
922 |
} /* gpgme_key_cability_from_algo */ |
} /* gpgme_key_cability_from_algo */ |
923 |
|
|
924 |
|
|
925 |
|
gpgme_error_t |
926 |
|
gpgme_key_append (gpgme_key_t dst, gpgme_key_t src, int idx) |
927 |
|
{ |
928 |
|
struct subkey_s *s, *key; |
929 |
|
|
930 |
|
for (s = &src->keys; idx; idx--, s = s->next) |
931 |
|
; |
932 |
|
|
933 |
|
key = _gpgme_key_add_subkey (dst); |
934 |
|
key->expires = s->expires; |
935 |
|
key->flags = s->flags; |
936 |
|
key->key_algo = s->key_algo; |
937 |
|
key->key_len = s->key_len; |
938 |
|
key->timestamp = s->timestamp; |
939 |
|
key->fingerprint = strdup (s->fingerprint); |
940 |
|
strcpy (key->keyid, s->keyid); |
941 |
|
|
942 |
|
return 0; |
943 |
|
} |