1 |
twoaday |
2 |
/* keycache-openpgp.c - OpenPGP code for the key cache |
2 |
twoaday |
22 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
3 |
twoaday |
2 |
* |
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 |
twoaday |
22 |
#include "ops.h" |
32 |
twoaday |
2 |
#include "openpgp.h" |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
static void |
36 |
twoaday |
22 |
buffer_to_string (char * dst, size_t dlen, const byte *buf, size_t nbytes) |
37 |
twoaday |
2 |
{ |
38 |
|
|
char dig[3]; |
39 |
|
|
size_t i; |
40 |
|
|
|
41 |
|
|
memset( dst, 0, dlen ); |
42 |
|
|
for( i = 0; i < nbytes && dlen > 0; i++ ) { |
43 |
|
|
sprintf( dig, "%02X", buf[i] ); |
44 |
|
|
strcat( dst, dig ); |
45 |
|
|
dlen -= 2; |
46 |
|
|
} |
47 |
|
|
} /* buffer_to_string */ |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
static void |
51 |
twoaday |
22 |
parse_secring (gpgme_keycache_t cache, const char *kid, const char *secring) |
52 |
twoaday |
2 |
{ |
53 |
twoaday |
22 |
PACKET *pkt = calloc (1, sizeof * pkt); |
54 |
|
|
PKT_secret_key *sk; |
55 |
twoaday |
2 |
gpg_iobuf_t inp; |
56 |
|
|
gpgme_error_t err; |
57 |
twoaday |
22 |
gpgme_key_t key; |
58 |
|
|
struct keycache_s *c=NULL; |
59 |
twoaday |
2 |
char keyid[16+1]; |
60 |
|
|
|
61 |
twoaday |
22 |
inp = gpg_iobuf_open (secring); |
62 |
|
|
if (!inp) { |
63 |
|
|
safe_free (pkt); |
64 |
twoaday |
2 |
return; |
65 |
|
|
} |
66 |
twoaday |
22 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); |
67 |
|
|
gpg_init_packet (pkt); |
68 |
|
|
while (gpg_parse_packet (inp, pkt) != -1) { |
69 |
|
|
if (pkt->pkttype == PKT_SECRET_KEY) { |
70 |
twoaday |
2 |
sk = pkt->pkt.secret_key; |
71 |
twoaday |
22 |
_snprintf (keyid, sizeof keyid-1, "%08lX", |
72 |
|
|
gpg_keyid_from_sk (sk, NULL)); |
73 |
|
|
if (kid && strcmp (kid, keyid) != 0) |
74 |
|
|
goto next; |
75 |
|
|
err = _gpgme_keycache_find_key2 (cache, keyid, 0, &key, &c); |
76 |
twoaday |
23 |
/*DEBUG2 ("parse_secring: keyid=%s %p\r\n", keyid, c);*/ |
77 |
twoaday |
22 |
if (!err) { |
78 |
twoaday |
2 |
key->gloflags.is_protected = sk->is_protected; |
79 |
|
|
key->gloflags.divert_to_card = sk->protect.s2k.mode==1002? 1 : 0; |
80 |
twoaday |
22 |
if (c->pubpart != NULL) { |
81 |
|
|
c->pubpart->gloflags.is_protected = sk->is_protected; |
82 |
|
|
c->pubpart->gloflags.divert_to_card = sk->protect.s2k.mode==1002? 1 : 0; |
83 |
twoaday |
23 |
/*DEBUG1 ("parse_secring: sync %s\r\n", keyid);*/ |
84 |
twoaday |
22 |
} |
85 |
twoaday |
2 |
} |
86 |
|
|
} |
87 |
twoaday |
22 |
next: |
88 |
|
|
gpg_free_packet (pkt); |
89 |
|
|
gpg_init_packet (pkt); |
90 |
twoaday |
2 |
} |
91 |
twoaday |
22 |
safe_free (pkt); |
92 |
|
|
gpg_iobuf_close (inp); |
93 |
twoaday |
2 |
} /* parse_secring */ |
94 |
|
|
|
95 |
|
|
|
96 |
|
|
gpgme_error_t |
97 |
twoaday |
22 |
keycache_prepare2 (gpgme_keycache_t ctx, const char *kid, |
98 |
|
|
const char *pubring, const char *secring) |
99 |
twoaday |
2 |
{ |
100 |
|
|
gpgme_error_t err; |
101 |
|
|
gpgme_key_t key = NULL; |
102 |
|
|
gpg_iobuf_t inp; |
103 |
twoaday |
22 |
PACKET *pkt = calloc (1, sizeof * pkt); |
104 |
|
|
struct revoker_key_s *r; |
105 |
twoaday |
2 |
const byte *sym_prefs; |
106 |
twoaday |
22 |
char keyid[16+1], *id = NULL; |
107 |
|
|
int key_seen = 0; |
108 |
twoaday |
2 |
size_t nbytes = 0, nsym =0; |
109 |
|
|
|
110 |
twoaday |
22 |
if (secring) { |
111 |
|
|
parse_secring (ctx, kid, secring); |
112 |
|
|
if (!pubring) { |
113 |
|
|
safe_free(pkt); |
114 |
twoaday |
2 |
return 0; |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
inp = gpg_iobuf_open( pubring ); |
118 |
|
|
if( !inp ) { |
119 |
|
|
safe_free( pkt ); |
120 |
|
|
return mk_error( File_Error ); |
121 |
|
|
} |
122 |
|
|
gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */ |
123 |
|
|
|
124 |
|
|
gpg_init_packet( pkt ); |
125 |
twoaday |
22 |
while (gpg_parse_packet (inp, pkt) != -1) { |
126 |
|
|
if (pkt->pkttype == PKT_PUBLIC_KEY) { |
127 |
|
|
strcpy (keyid, ""); |
128 |
twoaday |
2 |
key_seen = 1; |
129 |
twoaday |
22 |
} |
130 |
|
|
|
131 |
twoaday |
2 |
if( pkt->pkttype == PKT_SIGNATURE && pkt->pkt.signature->sig_class == 0x1F ) { |
132 |
twoaday |
22 |
if (pkt->pkt.signature->numrevkeys == 0) |
133 |
|
|
goto next; |
134 |
twoaday |
2 |
nbytes = pkt->pkt.signature->numrevkeys; |
135 |
twoaday |
22 |
_snprintf (keyid, sizeof keyid -1, "%08X", pkt->pkt.signature->keyid[1]); |
136 |
|
|
if (kid && strcmp (kid, keyid) != 0) |
137 |
|
|
goto next; |
138 |
|
|
err = gpgme_keycache_find_key (ctx, keyid, 0, &key); |
139 |
|
|
if (err) |
140 |
|
|
goto next; |
141 |
twoaday |
2 |
while( nbytes-- ) { |
142 |
|
|
PKT_signature *sig = pkt->pkt.signature; |
143 |
twoaday |
22 |
r = calloc( 1, sizeof *r ); |
144 |
|
|
if (!r) { |
145 |
|
|
err = mk_error (Out_Of_Core); |
146 |
|
|
goto fail; |
147 |
|
|
} |
148 |
twoaday |
2 |
r->algo = sig->revkey[nbytes]->algid; |
149 |
|
|
buffer_to_string( r->fpr, sizeof r->fpr-1, sig->revkey[nbytes]->fpr, 20 ); |
150 |
|
|
r->next = key->rvks; |
151 |
|
|
key->rvks = r; |
152 |
|
|
} |
153 |
|
|
} |
154 |
|
|
if( pkt->pkttype == PKT_SIGNATURE && key_seen == 1 ) { |
155 |
twoaday |
17 |
if( (sym_prefs=gpg_parse_sig_subpkt( pkt->pkt.signature->hashed, |
156 |
twoaday |
22 |
SIGSUBPKT_PREF_SYM, &nsym )) == NULL) |
157 |
|
|
goto next; |
158 |
|
|
_snprintf (keyid, sizeof keyid - 1, "%08X", pkt->pkt.signature->keyid[1]); |
159 |
|
|
if (kid && strcmp (kid, keyid) != 0) |
160 |
|
|
goto next; |
161 |
|
|
err = gpgme_keycache_find_key (ctx, keyid, 0, &key); |
162 |
|
|
if (err) |
163 |
|
|
goto next; |
164 |
|
|
else if (nsym) { |
165 |
|
|
key->sym_prefs = calloc (1, nsym+1); |
166 |
|
|
if (!key->sym_prefs) |
167 |
|
|
return mk_error (Out_Of_Core); |
168 |
|
|
memcpy (key->sym_prefs, sym_prefs, nsym); |
169 |
twoaday |
2 |
} |
170 |
|
|
} |
171 |
twoaday |
22 |
if (pkt->pkttype == PKT_USER_ID) { |
172 |
|
|
safe_free (id); |
173 |
|
|
id = strdup (pkt->pkt.user_id->name); |
174 |
|
|
if (!id) { |
175 |
|
|
err = mk_error (Out_Of_Core); |
176 |
|
|
goto fail; |
177 |
|
|
} |
178 |
twoaday |
2 |
} |
179 |
|
|
if( (pkt->pkttype == PKT_USER_ID || pkt->pkttype == PKT_ATTRIBUTE) |
180 |
|
|
&& pkt->pkt.user_id->attrib_data && key ) { |
181 |
|
|
PKT_user_id *id = pkt->pkt.user_id; |
182 |
|
|
key->attrib.used = 1; |
183 |
|
|
key->attrib.len = id->attrib_len; |
184 |
twoaday |
22 |
key->attrib.d = malloc (id->attrib_len + 1); |
185 |
twoaday |
2 |
if( !key->attrib.d ) { |
186 |
|
|
err = mk_error( Out_Of_Core ); |
187 |
|
|
goto fail; |
188 |
|
|
} |
189 |
twoaday |
22 |
memcpy (key->attrib.d, id->attrib_data, id->attrib_len); |
190 |
twoaday |
2 |
key = NULL; |
191 |
|
|
} |
192 |
twoaday |
22 |
next: |
193 |
|
|
gpg_free_packet (pkt); |
194 |
|
|
gpg_init_packet(pkt); |
195 |
twoaday |
2 |
} |
196 |
|
|
|
197 |
|
|
fail: |
198 |
twoaday |
22 |
safe_free (id); |
199 |
|
|
safe_free (pkt); |
200 |
|
|
gpg_iobuf_close (inp); |
201 |
twoaday |
2 |
return err; |
202 |
|
|
} /* gpgme_keycache_prepare */ |
203 |
|
|
|
204 |
twoaday |
22 |
|
205 |
|
|
gpgme_error_t |
206 |
|
|
gpgme_keycache_prepare (gpgme_keycache_t ctx, const char *pubr, const char *secr) |
207 |
|
|
{ |
208 |
|
|
return keycache_prepare2 (ctx, NULL, pubr, secr); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
gpgme_error_t |
212 |
|
|
gpgme_keycache_prepare_single (gpgme_keycache_t ctx, const char *keyid, |
213 |
|
|
const char *pubr, const char *secr) |
214 |
|
|
{ |
215 |
|
|
if (!strncmp (keyid, "0x", 2)) |
216 |
|
|
keyid += 2; |
217 |
|
|
DEBUG1 ("keycache_prepare_single: %s\r\n", keyid); |
218 |
|
|
return keycache_prepare2 (ctx, keyid, pubr, secr); |
219 |
|
|
} |
220 |
twoaday |
2 |
#endif /*WITH_OPENPGP*/ |