Fixed EOF detection for encrypted packets.
[gnupg.git] / g10 / skclist.c
blob70b375aa3904c8e665d3856605291a04bc69abc6
1 /* skclist.c - Build a list of secret keys
2 * Copyright (C) 1998, 1999, 2000, 2001, 2006 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG 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 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <assert.h>
27 #include "gpg.h"
28 #include "options.h"
29 #include "packet.h"
30 #include "status.h"
31 #include "keydb.h"
32 #include "util.h"
33 #include "i18n.h"
34 #include "cipher.h"
36 #ifndef GCRYCTL_FAKED_RANDOM_P
37 #define GCRYCTL_FAKED_RANDOM_P 51
38 #endif
40 /* Return true if Libgcrypt's RNG is in faked mode. */
41 int
42 random_is_faked (void)
44 return !!gcry_control ( GCRYCTL_FAKED_RANDOM_P, 0);
49 void
50 release_sk_list( SK_LIST sk_list )
52 SK_LIST sk_rover;
54 for( ; sk_list; sk_list = sk_rover ) {
55 sk_rover = sk_list->next;
56 free_secret_key( sk_list->sk );
57 xfree( sk_list );
62 /* Check that we are only using keys which don't have
63 * the string "(insecure!)" or "not secure" or "do not use"
64 * in one of the user ids
66 static int
67 is_insecure( PKT_secret_key *sk )
69 u32 keyid[2];
70 KBNODE node = NULL, u;
71 int insecure = 0;
73 keyid_from_sk( sk, keyid );
74 node = get_pubkeyblock( keyid );
75 for ( u = node; u; u = u->next ) {
76 if ( u->pkt->pkttype == PKT_USER_ID ) {
77 PKT_user_id *id = u->pkt->pkt.user_id;
78 if ( id->attrib_data )
79 continue; /* skip attribute packets */
80 if ( strstr( id->name, "(insecure!)" )
81 || strstr( id->name, "not secure" )
82 || strstr( id->name, "do not use" )
83 || strstr( id->name, "(INSECURE!)" ) ) {
84 insecure = 1;
85 break;
89 release_kbnode( node );
91 return insecure;
94 static int
95 key_present_in_sk_list(SK_LIST sk_list, PKT_secret_key *sk)
97 for (; sk_list; sk_list = sk_list->next) {
98 if ( !cmp_secret_keys(sk_list->sk, sk) )
99 return 0;
101 return -1;
104 static int
105 is_duplicated_entry (strlist_t list, strlist_t item)
107 for(; list && list != item; list = list->next) {
108 if ( !strcmp (list->d, item->d) )
109 return 1;
111 return 0;
116 build_sk_list( strlist_t locusr, SK_LIST *ret_sk_list,
117 int unlock, unsigned int use )
119 SK_LIST sk_list = NULL;
120 int rc;
122 if( !locusr )
123 { /* use the default one */
124 PKT_secret_key *sk;
126 sk = xmalloc_clear( sizeof *sk );
127 sk->req_usage = use;
128 if( (rc = get_seckey_byname( sk, NULL, unlock )) ) {
129 free_secret_key( sk ); sk = NULL;
130 log_error("no default secret key: %s\n", g10_errstr(rc) );
131 write_status_text (STATUS_INV_SGNR,
132 get_inv_recpsgnr_code (GPG_ERR_NO_SECKEY));
134 else if( !(rc=openpgp_pk_test_algo2 (sk->pubkey_algo, use)) )
136 SK_LIST r;
138 if( random_is_faked() && !is_insecure( sk ) )
140 log_info(_("key is not flagged as insecure - "
141 "can't use it with the faked RNG!\n"));
142 free_secret_key( sk ); sk = NULL;
143 write_status_text (STATUS_INV_SGNR,
144 get_inv_recpsgnr_code (GPG_ERR_NOT_TRUSTED));
146 else
148 r = xmalloc( sizeof *r );
149 r->sk = sk; sk = NULL;
150 r->next = sk_list;
151 r->mark = 0;
152 sk_list = r;
155 else
157 free_secret_key( sk ); sk = NULL;
158 log_error("invalid default secret key: %s\n", g10_errstr(rc) );
159 write_status_text (STATUS_INV_SGNR, get_inv_recpsgnr_code (rc));
162 else {
163 strlist_t locusr_orig = locusr;
164 for(; locusr; locusr = locusr->next ) {
165 PKT_secret_key *sk;
167 rc = 0;
168 /* Do an early check agains duplicated entries. However this
169 * won't catch all duplicates because the user IDs may be
170 * specified in different ways.
172 if ( is_duplicated_entry ( locusr_orig, locusr ) )
174 log_info (_("skipped \"%s\": duplicated\n"), locusr->d );
175 continue;
177 sk = xmalloc_clear( sizeof *sk );
178 sk->req_usage = use;
179 if( (rc = get_seckey_byname( sk, locusr->d, 0 )) )
181 free_secret_key( sk ); sk = NULL;
182 log_error(_("skipped \"%s\": %s\n"),
183 locusr->d, g10_errstr(rc) );
184 write_status_text_and_buffer
185 (STATUS_INV_SGNR, get_inv_recpsgnr_code (rc),
186 locusr->d, strlen (locusr->d), -1);
188 else if ( key_present_in_sk_list(sk_list, sk) == 0) {
189 free_secret_key(sk); sk = NULL;
190 log_info(_("skipped: secret key already present\n"));
192 else if ( unlock && (rc = check_secret_key( sk, 0 )) )
194 free_secret_key( sk ); sk = NULL;
195 log_error(_("skipped \"%s\": %s\n"),
196 locusr->d, g10_errstr(rc) );
197 write_status_text_and_buffer
198 (STATUS_INV_SGNR, get_inv_recpsgnr_code (rc),
199 locusr->d, strlen (locusr->d), -1);
201 else if( !(rc=openpgp_pk_test_algo2 (sk->pubkey_algo, use)) ) {
202 SK_LIST r;
204 if( sk->version == 4 && (use & PUBKEY_USAGE_SIG)
205 && sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E )
207 log_info(_("skipped \"%s\": %s\n"),locusr->d,
208 _("this is a PGP generated Elgamal key which"
209 " is not secure for signatures!"));
210 free_secret_key( sk ); sk = NULL;
211 write_status_text_and_buffer
212 (STATUS_INV_SGNR,
213 get_inv_recpsgnr_code (GPG_ERR_WRONG_KEY_USAGE),
214 locusr->d, strlen (locusr->d), -1);
216 else if( random_is_faked() && !is_insecure( sk ) ) {
217 log_info(_("key is not flagged as insecure - "
218 "can't use it with the faked RNG!\n"));
219 free_secret_key( sk ); sk = NULL;
220 write_status_text_and_buffer
221 (STATUS_INV_SGNR,
222 get_inv_recpsgnr_code (GPG_ERR_NOT_TRUSTED),
223 locusr->d, strlen (locusr->d), -1);
225 else {
226 r = xmalloc( sizeof *r );
227 r->sk = sk; sk = NULL;
228 r->next = sk_list;
229 r->mark = 0;
230 sk_list = r;
233 else {
234 free_secret_key( sk ); sk = NULL;
235 log_error("skipped \"%s\": %s\n", locusr->d, g10_errstr(rc) );
236 write_status_text_and_buffer
237 (STATUS_INV_SGNR, get_inv_recpsgnr_code (rc),
238 locusr->d, strlen (locusr->d), -1);
244 if( !rc && !sk_list ) {
245 log_error("no valid signators\n");
246 write_status_text (STATUS_NO_SGNR, "0");
247 rc = G10ERR_NO_USER_ID;
250 if( rc )
251 release_sk_list( sk_list );
252 else
253 *ret_sk_list = sk_list;
254 return rc;