1
/* pksign.c - Generate a keypair
2
* Copyright (C) 2002, 2003, 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33
store_key (gcry_sexp_t private, const char *passphrase, int force)
38
unsigned char grip[20];
40
if ( !gcry_pk_get_keygrip (private, grip) )
42
log_error ("can't calculate keygrip\n");
43
return gpg_error (GPG_ERR_GENERAL);
46
len = gcry_sexp_sprint (private, GCRYSEXP_FMT_CANON, NULL, 0);
48
buf = gcry_malloc_secure (len);
50
return out_of_core ();
51
len = gcry_sexp_sprint (private, GCRYSEXP_FMT_CANON, buf, len);
58
rc = agent_protect (buf, passphrase, &p, &len);
68
rc = agent_write_private_key (grip, buf, len, force);
73
/* Callback function to compare the first entered PIN with the one
74
currently being entered. */
76
reenter_compare_cb (struct pin_entry_info_s *pi)
78
const char *pin1 = pi->check_cb_arg;
80
if (!strcmp (pin1, pi->pin))
87
/* Generate a new keypair according to the parameters given in
90
agent_genkey (CTRL ctrl, const char *keyparam, size_t keyparamlen,
93
gcry_sexp_t s_keyparam, s_key, s_private, s_public;
94
struct pin_entry_info_s *pi, *pi2;
99
rc = gcry_sexp_sscan (&s_keyparam, NULL, keyparam, keyparamlen);
102
log_error ("failed to convert keyparam: %s\n", gpg_strerror (rc));
103
return gpg_error (GPG_ERR_INV_DATA);
106
/* Get the passphrase now, cause key generation may take a while. */
108
const char *text1 = _("Please enter the passphrase to%0A"
109
"to protect your new key");
110
const char *text2 = _("Please re-enter this passphrase");
111
const char *initial_errtext = NULL;
113
pi = gcry_calloc_secure (2, sizeof (*pi) + 100);
114
pi2 = pi + (sizeof *pi + 100);
115
pi->max_length = 100;
117
pi2->max_length = 100;
119
pi2->check_cb = reenter_compare_cb;
120
pi2->check_cb_arg = pi->pin;
123
rc = agent_askpin (ctrl, text1, initial_errtext, pi);
124
initial_errtext = NULL;
127
rc = agent_askpin (ctrl, text2, NULL, pi2);
129
{ /* The re-entered one did not match and the user did not
131
initial_errtext = _("does not match - try again");
140
pi = NULL; /* User does not want a passphrase. */
144
rc = gcry_pk_genkey (&s_key, s_keyparam );
145
gcry_sexp_release (s_keyparam);
148
log_error ("key generation failed: %s\n", gpg_strerror (rc));
153
/* break out the parts */
154
s_private = gcry_sexp_find_token (s_key, "private-key", 0);
157
log_error ("key generation failed: invalid return value\n");
158
gcry_sexp_release (s_key);
160
return gpg_error (GPG_ERR_INV_DATA);
162
s_public = gcry_sexp_find_token (s_key, "public-key", 0);
165
log_error ("key generation failed: invalid return value\n");
166
gcry_sexp_release (s_private);
167
gcry_sexp_release (s_key);
169
return gpg_error (GPG_ERR_INV_DATA);
171
gcry_sexp_release (s_key); s_key = NULL;
173
/* store the secret key */
175
log_debug ("storing private key\n");
176
rc = store_key (s_private, pi? pi->pin:NULL, 0);
177
xfree (pi); pi = NULL;
178
gcry_sexp_release (s_private);
181
gcry_sexp_release (s_public);
185
/* return the public key */
187
log_debug ("returning public key\n");
188
len = gcry_sexp_sprint (s_public, GCRYSEXP_FMT_CANON, NULL, 0);
190
buf = xtrymalloc (len);
193
gpg_error_t tmperr = out_of_core ();
194
gcry_sexp_release (s_private);
195
gcry_sexp_release (s_public);
198
len = gcry_sexp_sprint (s_public, GCRYSEXP_FMT_CANON, buf, len);
200
put_membuf (outbuf, buf, len);
201
gcry_sexp_release (s_public);
209
/* Apply a new passpahrse to the key S_SKEY and store it. */
211
agent_protect_and_store (CTRL ctrl, gcry_sexp_t s_skey)
213
struct pin_entry_info_s *pi, *pi2;
217
const char *text1 = _("Please enter the new passphrase");
218
const char *text2 = _("Please re-enter this passphrase");
219
const char *initial_errtext = NULL;
221
pi = gcry_calloc_secure (2, sizeof (*pi) + 100);
222
pi2 = pi + (sizeof *pi + 100);
223
pi->max_length = 100;
225
pi2->max_length = 100;
227
pi2->check_cb = reenter_compare_cb;
228
pi2->check_cb_arg = pi->pin;
231
rc = agent_askpin (ctrl, text1, initial_errtext, pi);
234
rc = agent_askpin (ctrl, text2, NULL, pi2);
236
{ /* The re-entered one did not match and the user did not
238
initial_errtext = _("does not match - try again");
247
pi = NULL; /* User does not want a passphrase. */
251
rc = store_key (s_skey, pi? pi->pin:NULL, 1);