* gpg.texi (GPG Configuration Options): Make http_proxy option
[gnupg.git] / g10 / skclist.c
blob5aeaa78ff6d924d663557f82c33cc2c9998284b1
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
29 #include "gpg.h"
30 #include "options.h"
31 #include "packet.h"
32 #include "errors.h"
33 #include "keydb.h"
34 #include "util.h"
35 #include "i18n.h"
36 #include "cipher.h"
38 #ifndef GCRYCTL_FAKED_RANDOM_P
39 #define GCRYCTL_FAKED_RANDOM_P 51
40 #endif
42 /* Return true if Libgcrypt's RNG is in faked mode. */
43 int
44 random_is_faked (void)
46 /* We use a runtime check to allow for slow migrattion of libgcrypt.
47 We can't use the constant becuase that one is actually an enum
48 value. */
49 gpg_error_t err = gcry_control ( 51 /*GCRYCTL_FAKED_RANDOM_P*/, 0);
51 if (!err)
52 return 0;
53 if (gpg_err_code (err) != GPG_ERR_INV_OP)
54 return 1;
55 log_info ("WARNING: libgcrypt too old.\n");
56 log_info (" can't check whether we are in faked RNG mode\n");
57 return 0; /* Need to return false. */
62 void
63 release_sk_list( SK_LIST sk_list )
65 SK_LIST sk_rover;
67 for( ; sk_list; sk_list = sk_rover ) {
68 sk_rover = sk_list->next;
69 free_secret_key( sk_list->sk );
70 xfree( sk_list );
75 /* Check that we are only using keys which don't have
76 * the string "(insecure!)" or "not secure" or "do not use"
77 * in one of the user ids
79 static int
80 is_insecure( PKT_secret_key *sk )
82 u32 keyid[2];
83 KBNODE node = NULL, u;
84 int insecure = 0;
86 keyid_from_sk( sk, keyid );
87 node = get_pubkeyblock( keyid );
88 for ( u = node; u; u = u->next ) {
89 if ( u->pkt->pkttype == PKT_USER_ID ) {
90 PKT_user_id *id = u->pkt->pkt.user_id;
91 if ( id->attrib_data )
92 continue; /* skip attribute packets */
93 if ( strstr( id->name, "(insecure!)" )
94 || strstr( id->name, "not secure" )
95 || strstr( id->name, "do not use" )
96 || strstr( id->name, "(INSECURE!)" ) ) {
97 insecure = 1;
98 break;
102 release_kbnode( node );
104 return insecure;
107 static int
108 key_present_in_sk_list(SK_LIST sk_list, PKT_secret_key *sk)
110 for (; sk_list; sk_list = sk_list->next) {
111 if ( !cmp_secret_keys(sk_list->sk, sk) )
112 return 0;
114 return -1;
117 static int
118 is_duplicated_entry (strlist_t list, strlist_t item)
120 for(; list && list != item; list = list->next) {
121 if ( !strcmp (list->d, item->d) )
122 return 1;
124 return 0;
129 build_sk_list( strlist_t locusr, SK_LIST *ret_sk_list,
130 int unlock, unsigned int use )
132 SK_LIST sk_list = NULL;
133 int rc;
135 if( !locusr )
136 { /* use the default one */
137 PKT_secret_key *sk;
139 sk = xmalloc_clear( sizeof *sk );
140 sk->req_usage = use;
141 if( (rc = get_seckey_byname( sk, NULL, unlock )) ) {
142 free_secret_key( sk ); sk = NULL;
143 log_error("no default secret key: %s\n", g10_errstr(rc) );
145 else if( !(rc=openpgp_pk_test_algo2 (sk->pubkey_algo, use)) )
147 SK_LIST r;
149 if( random_is_faked() && !is_insecure( sk ) )
151 log_info(_("key is not flagged as insecure - "
152 "can't use it with the faked RNG!\n"));
153 free_secret_key( sk ); sk = NULL;
155 else
157 r = xmalloc( sizeof *r );
158 r->sk = sk; sk = NULL;
159 r->next = sk_list;
160 r->mark = 0;
161 sk_list = r;
164 else
166 free_secret_key( sk ); sk = NULL;
167 log_error("invalid default secret key: %s\n", g10_errstr(rc) );
170 else {
171 strlist_t locusr_orig = locusr;
172 for(; locusr; locusr = locusr->next ) {
173 PKT_secret_key *sk;
175 rc = 0;
176 /* Do an early check agains duplicated entries. However this
177 * won't catch all duplicates because the user IDs may be
178 * specified in different ways.
180 if ( is_duplicated_entry ( locusr_orig, locusr ) )
182 log_error(_("skipped \"%s\": duplicated\n"), locusr->d );
183 continue;
185 sk = xmalloc_clear( sizeof *sk );
186 sk->req_usage = use;
187 if( (rc = get_seckey_byname( sk, locusr->d, 0 )) )
189 free_secret_key( sk ); sk = NULL;
190 log_error(_("skipped \"%s\": %s\n"),
191 locusr->d, g10_errstr(rc) );
193 else if ( key_present_in_sk_list(sk_list, sk) == 0) {
194 free_secret_key(sk); sk = NULL;
195 log_info(_("skipped: secret key already present\n"));
197 else if ( unlock && (rc = check_secret_key( sk, 0 )) )
199 free_secret_key( sk ); sk = NULL;
200 log_error(_("skipped \"%s\": %s\n"),
201 locusr->d, g10_errstr(rc) );
203 else if( !(rc=openpgp_pk_test_algo2 (sk->pubkey_algo, use)) ) {
204 SK_LIST r;
206 if( sk->version == 4 && (use & PUBKEY_USAGE_SIG)
207 && sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E )
209 log_info(_("skipped \"%s\": %s\n"),locusr->d,
210 _("this is a PGP generated Elgamal key which"
211 " is not secure for signatures!"));
212 free_secret_key( sk ); sk = NULL;
214 else if( random_is_faked() && !is_insecure( sk ) ) {
215 log_info(_("key is not flagged as insecure - "
216 "can't use it with the faked RNG!\n"));
217 free_secret_key( sk ); sk = NULL;
219 else {
220 r = xmalloc( sizeof *r );
221 r->sk = sk; sk = NULL;
222 r->next = sk_list;
223 r->mark = 0;
224 sk_list = r;
227 else {
228 free_secret_key( sk ); sk = NULL;
229 log_error("skipped \"%s\": %s\n", locusr->d, g10_errstr(rc) );
235 if( !rc && !sk_list ) {
236 log_error("no valid signators\n");
237 rc = G10ERR_NO_USER_ID;
240 if( rc )
241 release_sk_list( sk_list );
242 else
243 *ret_sk_list = sk_list;
244 return rc;