2007-07-05 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / skclist.c
blobe0e1256d63d331a3fd58e188d0be839b34e81e0b
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 "errors.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 /* We use a runtime check to allow for slow migrattion of libgcrypt.
45 We can't use the constant becuase that one is actually an enum
46 value. */
47 gpg_error_t err = gcry_control ( 51 /*GCRYCTL_FAKED_RANDOM_P*/, 0);
49 if (!err)
50 return 0;
51 if (gpg_err_code (err) != GPG_ERR_INV_OP)
52 return 1;
53 log_info ("WARNING: libgcrypt too old.\n");
54 log_info (" can't check whether we are in faked RNG mode\n");
55 return 0; /* Need to return false. */
60 void
61 release_sk_list( SK_LIST sk_list )
63 SK_LIST sk_rover;
65 for( ; sk_list; sk_list = sk_rover ) {
66 sk_rover = sk_list->next;
67 free_secret_key( sk_list->sk );
68 xfree( sk_list );
73 /* Check that we are only using keys which don't have
74 * the string "(insecure!)" or "not secure" or "do not use"
75 * in one of the user ids
77 static int
78 is_insecure( PKT_secret_key *sk )
80 u32 keyid[2];
81 KBNODE node = NULL, u;
82 int insecure = 0;
84 keyid_from_sk( sk, keyid );
85 node = get_pubkeyblock( keyid );
86 for ( u = node; u; u = u->next ) {
87 if ( u->pkt->pkttype == PKT_USER_ID ) {
88 PKT_user_id *id = u->pkt->pkt.user_id;
89 if ( id->attrib_data )
90 continue; /* skip attribute packets */
91 if ( strstr( id->name, "(insecure!)" )
92 || strstr( id->name, "not secure" )
93 || strstr( id->name, "do not use" )
94 || strstr( id->name, "(INSECURE!)" ) ) {
95 insecure = 1;
96 break;
100 release_kbnode( node );
102 return insecure;
105 static int
106 key_present_in_sk_list(SK_LIST sk_list, PKT_secret_key *sk)
108 for (; sk_list; sk_list = sk_list->next) {
109 if ( !cmp_secret_keys(sk_list->sk, sk) )
110 return 0;
112 return -1;
115 static int
116 is_duplicated_entry (strlist_t list, strlist_t item)
118 for(; list && list != item; list = list->next) {
119 if ( !strcmp (list->d, item->d) )
120 return 1;
122 return 0;
127 build_sk_list( strlist_t locusr, SK_LIST *ret_sk_list,
128 int unlock, unsigned int use )
130 SK_LIST sk_list = NULL;
131 int rc;
133 if( !locusr )
134 { /* use the default one */
135 PKT_secret_key *sk;
137 sk = xmalloc_clear( sizeof *sk );
138 sk->req_usage = use;
139 if( (rc = get_seckey_byname( sk, NULL, unlock )) ) {
140 free_secret_key( sk ); sk = NULL;
141 log_error("no default secret key: %s\n", g10_errstr(rc) );
143 else if( !(rc=openpgp_pk_test_algo2 (sk->pubkey_algo, use)) )
145 SK_LIST r;
147 if( random_is_faked() && !is_insecure( sk ) )
149 log_info(_("key is not flagged as insecure - "
150 "can't use it with the faked RNG!\n"));
151 free_secret_key( sk ); sk = NULL;
153 else
155 r = xmalloc( sizeof *r );
156 r->sk = sk; sk = NULL;
157 r->next = sk_list;
158 r->mark = 0;
159 sk_list = r;
162 else
164 free_secret_key( sk ); sk = NULL;
165 log_error("invalid default secret key: %s\n", g10_errstr(rc) );
168 else {
169 strlist_t locusr_orig = locusr;
170 for(; locusr; locusr = locusr->next ) {
171 PKT_secret_key *sk;
173 rc = 0;
174 /* Do an early check agains duplicated entries. However this
175 * won't catch all duplicates because the user IDs may be
176 * specified in different ways.
178 if ( is_duplicated_entry ( locusr_orig, locusr ) )
180 log_error(_("skipped \"%s\": duplicated\n"), locusr->d );
181 continue;
183 sk = xmalloc_clear( sizeof *sk );
184 sk->req_usage = use;
185 if( (rc = get_seckey_byname( sk, locusr->d, 0 )) )
187 free_secret_key( sk ); sk = NULL;
188 log_error(_("skipped \"%s\": %s\n"),
189 locusr->d, g10_errstr(rc) );
191 else if ( key_present_in_sk_list(sk_list, sk) == 0) {
192 free_secret_key(sk); sk = NULL;
193 log_info(_("skipped: secret key already present\n"));
195 else if ( unlock && (rc = check_secret_key( sk, 0 )) )
197 free_secret_key( sk ); sk = NULL;
198 log_error(_("skipped \"%s\": %s\n"),
199 locusr->d, g10_errstr(rc) );
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;
212 else if( random_is_faked() && !is_insecure( sk ) ) {
213 log_info(_("key is not flagged as insecure - "
214 "can't use it with the faked RNG!\n"));
215 free_secret_key( sk ); sk = NULL;
217 else {
218 r = xmalloc( sizeof *r );
219 r->sk = sk; sk = NULL;
220 r->next = sk_list;
221 r->mark = 0;
222 sk_list = r;
225 else {
226 free_secret_key( sk ); sk = NULL;
227 log_error("skipped \"%s\": %s\n", locusr->d, g10_errstr(rc) );
233 if( !rc && !sk_list ) {
234 log_error("no valid signators\n");
235 rc = G10ERR_NO_USER_ID;
238 if( rc )
239 release_sk_list( sk_list );
240 else
241 *ret_sk_list = sk_list;
242 return rc;