~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to g10/skclist.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* skclist.c
 
2
 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
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.
 
10
 *
 
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.
 
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 <config.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <assert.h>
 
27
 
 
28
#include "options.h"
 
29
#include "packet.h"
 
30
#include "errors.h"
 
31
#include "keydb.h"
 
32
#include "memory.h"
 
33
#include "util.h"
 
34
#include "i18n.h"
 
35
#include "cipher.h"
 
36
 
 
37
 
 
38
void
 
39
release_sk_list( SK_LIST sk_list )
 
40
{
 
41
    SK_LIST sk_rover;
 
42
 
 
43
    for( ; sk_list; sk_list = sk_rover ) {
 
44
        sk_rover = sk_list->next;
 
45
        free_secret_key( sk_list->sk );
 
46
        xfree ( sk_list );
 
47
    }
 
48
}
 
49
 
 
50
 
 
51
 
 
52
static int
 
53
key_present_in_sk_list(SK_LIST sk_list, PKT_secret_key *sk)
 
54
{
 
55
    for (; sk_list; sk_list = sk_list->next) {
 
56
        if ( !cmp_secret_keys(sk_list->sk, sk) )
 
57
            return 0;
 
58
    }
 
59
    return -1;
 
60
}
 
61
 
 
62
static int
 
63
is_duplicated_entry (STRLIST list, STRLIST item)
 
64
{
 
65
    for(; list && list != item; list = list->next) {
 
66
        if ( !strcmp (list->d, item->d) )
 
67
            return 1;
 
68
    }
 
69
    return 0;
 
70
}
 
71
 
 
72
 
 
73
int
 
74
build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
 
75
               int unlock, unsigned int use )
 
76
{
 
77
    SK_LIST sk_list = NULL;
 
78
    int rc;
 
79
 
 
80
    if( !locusr ) { /* use the default one */
 
81
        PKT_secret_key *sk;
 
82
 
 
83
        sk = xcalloc (1, sizeof *sk );
 
84
        sk->req_usage = use;
 
85
        if( (rc = get_seckey_byname( sk, NULL, unlock )) ) {
 
86
            free_secret_key( sk ); sk = NULL;
 
87
            log_error("no default secret key: %s\n", gpg_strerror (rc) );
 
88
        }
 
89
        else if( !(rc=openpgp_pk_test_algo (sk->pubkey_algo, use)) ) {
 
90
            SK_LIST r;
 
91
 
 
92
            if( sk->version == 4 && (use & PUBKEY_USAGE_SIG)
 
93
                && sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E ) {
 
94
                log_info("this is a PGP generated "
 
95
                    "ElGamal key which is NOT secure for signatures!\n");
 
96
                free_secret_key( sk ); sk = NULL;
 
97
            }
 
98
            else {
 
99
                r = xmalloc ( sizeof *r );
 
100
                r->sk = sk; sk = NULL;
 
101
                r->next = sk_list;
 
102
                r->mark = 0;
 
103
                sk_list = r;
 
104
            }
 
105
        }
 
106
        else {
 
107
            free_secret_key( sk ); sk = NULL;
 
108
            log_error("invalid default secret key: %s\n", gpg_strerror (rc) );
 
109
        }
 
110
    }
 
111
    else {
 
112
        STRLIST locusr_orig = locusr;
 
113
        for(; locusr; locusr = locusr->next ) {
 
114
            PKT_secret_key *sk;
 
115
            
 
116
            rc = 0;
 
117
            /* Do an early check agains duplicated entries.  However this
 
118
             * won't catch all duplicates because the user IDs may be
 
119
             * specified in different ways.
 
120
             */
 
121
            if ( is_duplicated_entry ( locusr_orig, locusr ) ) {
 
122
                log_error(_("skipped `%s': duplicated\n"), locusr->d );
 
123
                continue;
 
124
            }
 
125
            sk = xcalloc (1, sizeof *sk );
 
126
            sk->req_usage = use;
 
127
            if( (rc = get_seckey_byname( sk, locusr->d, 0 )) ) {
 
128
                free_secret_key( sk ); sk = NULL;
 
129
                log_error(_("skipped `%s': %s\n"), locusr->d, gpg_strerror (rc) );
 
130
            }
 
131
            else if ( key_present_in_sk_list(sk_list, sk) == 0) {
 
132
                free_secret_key(sk); sk = NULL;
 
133
                log_info(_("skipped: secret key already present\n"));
 
134
            }
 
135
            else if ( unlock && (rc = check_secret_key( sk, 0 )) ) {
 
136
                free_secret_key( sk ); sk = NULL;
 
137
                log_error(_("skipped `%s': %s\n"), locusr->d, gpg_strerror (rc) );
 
138
            }
 
139
            else if( !(rc=openpgp_pk_test_algo (sk->pubkey_algo, use)) ) {
 
140
                SK_LIST r;
 
141
 
 
142
                if( sk->version == 4 && (use & PUBKEY_USAGE_SIG)
 
143
                    && sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E ) {
 
144
                    log_info(_("skipped `%s': this is a PGP generated "
 
145
                        "ElGamal key which is not secure for signatures!\n"),
 
146
                        locusr->d );
 
147
                    free_secret_key( sk ); sk = NULL;
 
148
                }
 
149
                else {
 
150
                    r = xmalloc ( sizeof *r );
 
151
                    r->sk = sk; sk = NULL;
 
152
                    r->next = sk_list;
 
153
                    r->mark = 0;
 
154
                    sk_list = r;
 
155
                }
 
156
            }
 
157
            else {
 
158
                free_secret_key( sk ); sk = NULL;
 
159
                log_error("skipped `%s': %s\n", locusr->d, gpg_strerror (rc) );
 
160
            }
 
161
        }
 
162
    }
 
163
 
 
164
 
 
165
    if( !rc && !sk_list ) {
 
166
        log_error("no valid signators\n");
 
167
        rc = GPG_ERR_NO_USER_ID;
 
168
    }
 
169
 
 
170
    if( rc )
 
171
        release_sk_list( sk_list );
 
172
    else
 
173
        *ret_sk_list = sk_list;
 
174
    return rc;
 
175
}
 
176