1 |
twoaday |
2 |
/* keycache-openpgp.c - OpenPGP code for the key cache |
2 |
|
|
* Copyright (C) 2001, 2002, 2003 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of MyGPGME. |
5 |
|
|
* |
6 |
|
|
* MyGPGME is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* MyGPGME is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <stdio.h> |
22 |
|
|
#include <sys/types.h> |
23 |
|
|
#include <string.h> |
24 |
|
|
#include <malloc.h> |
25 |
|
|
|
26 |
|
|
#include "gpgme-config.h" |
27 |
|
|
#ifdef WITH_OPENPGP |
28 |
|
|
#include "gpgme.h" |
29 |
|
|
#include "key.h" |
30 |
|
|
#include "util.h" |
31 |
|
|
#include "openpgp.h" |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
static void |
35 |
|
|
buffer_to_string( char * dst, size_t dlen, const byte * buf, size_t nbytes ) |
36 |
|
|
{ |
37 |
|
|
char dig[3]; |
38 |
|
|
size_t i; |
39 |
|
|
|
40 |
|
|
memset( dst, 0, dlen ); |
41 |
|
|
for( i = 0; i < nbytes && dlen > 0; i++ ) { |
42 |
|
|
sprintf( dig, "%02X", buf[i] ); |
43 |
|
|
strcat( dst, dig ); |
44 |
|
|
dlen -= 2; |
45 |
|
|
} |
46 |
|
|
} /* buffer_to_string */ |
47 |
|
|
|
48 |
|
|
|
49 |
|
|
static void |
50 |
|
|
parse_secring( gpgme_keycache_t cache, const char * secring ) |
51 |
|
|
{ |
52 |
|
|
PACKET * pkt = calloc( 1, sizeof * pkt ); |
53 |
|
|
PKT_secret_key * sk; |
54 |
|
|
gpg_iobuf_t inp; |
55 |
|
|
gpgme_error_t err; |
56 |
|
|
gpgme_key_t key; |
57 |
|
|
char keyid[16+1]; |
58 |
|
|
int rc; |
59 |
|
|
|
60 |
|
|
inp = gpg_iobuf_open( secring ); |
61 |
|
|
if( !inp ) { |
62 |
|
|
safe_free( pkt ); |
63 |
|
|
return; |
64 |
|
|
} |
65 |
|
|
gpg_iobuf_ioctl( inp, 3, 1, NULL ); |
66 |
|
|
gpg_init_packet( pkt ); |
67 |
|
|
while( (rc = gpg_parse_packet( inp, pkt )) != -1 ) { |
68 |
|
|
if( pkt->pkttype == PKT_SECRET_KEY ) { |
69 |
|
|
sk = pkt->pkt.secret_key; |
70 |
|
|
_snprintf( keyid, sizeof keyid-1, "%08lX", |
71 |
|
|
gpg_keyid_from_sk( sk, NULL ) ); |
72 |
|
|
err = gpgme_keycache_find_key (cache, keyid, 0, &key); |
73 |
|
|
if( !err ) { |
74 |
|
|
key->gloflags.is_protected = sk->is_protected; |
75 |
|
|
key->gloflags.divert_to_card = sk->protect.s2k.mode==1002? 1 : 0; |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
gpg_free_packet( pkt ); |
79 |
|
|
gpg_init_packet( pkt ); |
80 |
|
|
} |
81 |
|
|
safe_free( pkt ); |
82 |
|
|
gpg_iobuf_close( inp ); |
83 |
|
|
} /* parse_secring */ |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
gpgme_error_t |
87 |
|
|
gpgme_keycache_prepare( gpgme_keycache_t ctx, |
88 |
|
|
const char * pubring, const char * secring ) |
89 |
|
|
{ |
90 |
|
|
gpgme_error_t err; |
91 |
|
|
gpgme_key_t key = NULL; |
92 |
|
|
gpg_iobuf_t inp; |
93 |
|
|
PACKET * pkt = calloc( 1, sizeof * pkt ); |
94 |
|
|
struct revoker_key_s * r; |
95 |
|
|
const byte *sym_prefs; |
96 |
|
|
char keyid[16+1], * id = NULL; |
97 |
|
|
int rc = 0, key_seen = 0; |
98 |
|
|
size_t nbytes = 0, nsym =0; |
99 |
|
|
|
100 |
|
|
if( secring ) { |
101 |
|
|
parse_secring( ctx, secring ); |
102 |
|
|
if( !pubring ) { |
103 |
|
|
safe_free( pkt ); |
104 |
|
|
return 0; |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
inp = gpg_iobuf_open( pubring ); |
108 |
|
|
if( !inp ) { |
109 |
|
|
safe_free( pkt ); |
110 |
|
|
return mk_error( File_Error ); |
111 |
|
|
} |
112 |
|
|
gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */ |
113 |
|
|
|
114 |
|
|
gpg_init_packet( pkt ); |
115 |
|
|
while( (rc = gpg_parse_packet( inp, pkt )) != -1 ) { |
116 |
|
|
if( pkt->pkttype == PKT_PUBLIC_KEY ) |
117 |
|
|
key_seen = 1; |
118 |
|
|
|
119 |
|
|
if( pkt->pkttype == PKT_SIGNATURE && pkt->pkt.signature->sig_class == 0x1F ) { |
120 |
|
|
if( pkt->pkt.signature->numrevkeys == 0 ) |
121 |
|
|
continue; |
122 |
|
|
nbytes = pkt->pkt.signature->numrevkeys; |
123 |
|
|
_snprintf( keyid, sizeof keyid -1, "%08X", pkt->pkt.signature->keyid[1] ); |
124 |
|
|
err = gpgme_keycache_find_key( ctx, keyid, 0, &key ); |
125 |
|
|
if( err ) |
126 |
|
|
continue; |
127 |
|
|
while( nbytes-- ) { |
128 |
|
|
PKT_signature *sig = pkt->pkt.signature; |
129 |
|
|
r = calloc( 1, sizeof *r ); |
130 |
|
|
r->algo = sig->revkey[nbytes]->algid; |
131 |
|
|
buffer_to_string( r->fpr, sizeof r->fpr-1, sig->revkey[nbytes]->fpr, 20 ); |
132 |
|
|
r->next = key->rvks; |
133 |
|
|
key->rvks = r; |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
if( pkt->pkttype == PKT_SIGNATURE && key_seen == 1 ) { |
137 |
|
|
if( !(sym_prefs=gpg_parse_sig_subpkt( pkt->pkt.signature->hashed, |
138 |
|
|
SIGSUBPKT_PREF_SYM, &nsym )) ) |
139 |
|
|
continue; |
140 |
|
|
_snprintf( keyid, sizeof keyid - 1, "%08X", pkt->pkt.signature->keyid[1] ); |
141 |
|
|
err = gpgme_keycache_find_key( ctx, keyid, 0, &key ); |
142 |
|
|
if( err ) |
143 |
|
|
continue; |
144 |
|
|
else if( nsym ) { |
145 |
|
|
key->sym_prefs = calloc( 1, nsym+1 ); |
146 |
|
|
if( !key->sym_prefs ) |
147 |
|
|
return mk_error( Out_Of_Core ); |
148 |
|
|
memcpy( key->sym_prefs, sym_prefs, nsym ); |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
if( pkt->pkttype == PKT_USER_ID ) { |
152 |
|
|
safe_free( id ); |
153 |
|
|
id = strdup( pkt->pkt.user_id->name ); |
154 |
|
|
} |
155 |
|
|
if( (pkt->pkttype == PKT_USER_ID || pkt->pkttype == PKT_ATTRIBUTE) |
156 |
|
|
&& pkt->pkt.user_id->attrib_data && key ) { |
157 |
|
|
PKT_user_id *id = pkt->pkt.user_id; |
158 |
|
|
key->attrib.used = 1; |
159 |
|
|
key->attrib.len = id->attrib_len; |
160 |
|
|
key->attrib.d = malloc( id->attrib_len + 1 ); |
161 |
|
|
if( !key->attrib.d ) { |
162 |
|
|
err = mk_error( Out_Of_Core ); |
163 |
|
|
goto fail; |
164 |
|
|
} |
165 |
|
|
memcpy( key->attrib.d, id->attrib_data, id->attrib_len ); |
166 |
|
|
key = NULL; |
167 |
|
|
} |
168 |
|
|
gpg_free_packet( pkt ); |
169 |
|
|
gpg_init_packet( pkt ); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
fail: |
173 |
|
|
safe_free( pkt ); |
174 |
|
|
gpg_iobuf_close( inp ); |
175 |
|
|
return err; |
176 |
|
|
} /* gpgme_keycache_prepare */ |
177 |
|
|
|
178 |
|
|
#endif /*WITH_OPENPGP*/ |