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 |
|
|
713 |
break; |
break; |
714 |
|
|
715 |
case GPGME_ATTR_KEY_USABLE: |
case GPGME_ATTR_KEY_USABLE: |
716 |
n = 3; |
n = 4; |
717 |
for (k=&key->keys; k && idx; k=k->next, idx--) |
for (k=&key->keys; k && idx; k=k->next, idx--) |
718 |
; |
; |
719 |
if (k) |
if (k) { |
|
{ |
|
720 |
if (!k->flags.revoked) |
if (!k->flags.revoked) |
721 |
n--; |
n--; |
722 |
if (!k->flags.expired) |
if (!k->flags.expired) |
723 |
n--; |
n--; |
724 |
if (!k->flags.invalid) |
if (!k->flags.invalid) |
725 |
n--; |
n--; |
726 |
|
if (!key->gloflags.disabled && !k->flags.disabled) |
727 |
|
n--; |
728 |
} |
} |
729 |
if (n == 0) |
if (n == 0) |
730 |
val = 1; |
val = 1; |
738 |
|
|
739 |
|
|
740 |
const char* |
const char* |
741 |
gpgme_key_expand_attr( int what, unsigned long attr ) |
gpgme_key_expand_attr (int what, unsigned long attr) |
742 |
{ |
{ |
743 |
static char tmpbuf[16+1]; |
static char tmpbuf[16+1]; |
744 |
struct tm *iso_date; |
struct tm *iso_date; |
745 |
|
|
746 |
switch( what ) { |
switch( what ) { |
747 |
case GPGME_ATTR_ALGO: |
case GPGME_ATTR_ALGO: |
748 |
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"; |
|
|
} |
|
|
return "???"; |
|
749 |
|
|
750 |
case GPGME_ATTR_ALGO_SHORT: |
case GPGME_ATTR_ALGO_SHORT: |
751 |
switch( attr ) { |
switch( attr ) { |
810 |
|
|
811 |
|
|
812 |
int |
int |
813 |
gpgme_key_get_cability( gpgme_key_t key, int attr, int keyidx ) |
gpgme_key_get_cability (gpgme_key_t key, int attr, int keyidx) |
814 |
{ |
{ |
815 |
struct subkey_s *s; |
struct subkey_s *s; |
816 |
|
|
837 |
return 0; |
return 0; |
838 |
} /* gpgme_key_get_cability */ |
} /* gpgme_key_get_cability */ |
839 |
|
|
840 |
|
|
841 |
int |
int |
842 |
gpgme_key_count_items (gpgme_key_t key, int what) |
gpgme_key_count_items (gpgme_key_t key, int what) |
843 |
{ |
{ |
849 |
} dat; |
} dat; |
850 |
int count = 0; |
int count = 0; |
851 |
|
|
852 |
if( !key ) |
if (!key) |
853 |
return 0; |
return 0; |
854 |
switch( what ) { |
switch( what ) { |
855 |
case GPGME_ATTR_USERID: |
case GPGME_ATTR_USERID: |
868 |
return count; |
return count; |
869 |
|
|
870 |
case GPGME_ATTR_SIG_KEYID: |
case GPGME_ATTR_SIG_KEYID: |
871 |
for( dat.s = key->sigs; dat.s; dat.s = dat.s->next ) |
for (dat.s = key->sigs; dat.s; dat.s = dat.s->next) |
872 |
count++; |
count++; |
873 |
return count; |
return count; |
874 |
} |
} |
894 |
int |
int |
895 |
gpgme_key_cability_from_algo (gpgme_pk_cipher_t algo) |
gpgme_key_cability_from_algo (gpgme_pk_cipher_t algo) |
896 |
{ |
{ |
897 |
switch( algo ) { |
switch (algo) { |
898 |
case GPGME_PK_DSA: |
case GPGME_PK_DSA: |
899 |
case GPGME_PK_RSA_S: |
case GPGME_PK_RSA_S: |
900 |
return GPGME_KEY_CANSIGN; |
return GPGME_KEY_CANSIGN; |
912 |
} /* gpgme_key_cability_from_algo */ |
} /* gpgme_key_cability_from_algo */ |
913 |
|
|
914 |
|
|
915 |
int |
gpgme_error_t |
916 |
gpgme_key_append (gpgme_key_t dst, gpgme_key_t src, int idx) |
gpgme_key_append (gpgme_key_t dst, gpgme_key_t src, int idx) |
917 |
{ |
{ |
918 |
struct subkey_s *s, *key; |
struct subkey_s *s, *key; |