Codebase list gpa / upstream/0.10.0+git20210415.1.77a0693 src / keygendlg.c
upstream/0.10.0+git20210415.1.77a0693

Tree @upstream/0.10.0+git20210415.1.77a0693 (Download .tar.gz)

keygendlg.c @upstream/0.10.0+git20210415.1.77a0693raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* keygendlg.c - The GNU Privacy Assistant
   Copyright (C) 2009 g10 Code GmbH

   This file is part of GPA.

   GPA is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   GPA is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <gtk/gtk.h>

#include "gpa.h"
#include "gpawidgets.h"
#include "gtktools.h"
#include "gpadatebox.h"
#include "keygendlg.h"



/* A table of algorithm combinations we offer to create.  */
static struct
{
  gpa_keygen_algo_t algo;
  const char *name;
} algorithm_table[] =
  {
    { GPA_KEYGEN_ALGO_RSA_RSA,     N_("RSA")},
    { GPA_KEYGEN_ALGO_RSA,         N_("RSA (sign only)")},
    { GPA_KEYGEN_ALGO_DSA_ELGAMAL, N_("DSA")},
    { GPA_KEYGEN_ALGO_DSA,         N_("DSA (sign only)")},
    { 0, NULL}
  };



struct _GpaKeyGenDlg
{
  gboolean forcard;	    /* Specifies if this is a dialog for
			       on-card key generation or not. */
  GtkWidget *dialog;        /* The dialog object.  */

  GtkWidget *entry_algo;    /* Maybe NULL.  */
  GtkWidget *entry_keysize; /* Maybe NULL.  */
  GtkWidget *entry_name;
  GtkWidget *entry_email;
  GtkWidget *entry_comment;
  GtkWidget *entry_expire;
  GtkWidget *entry_backup;  /* Maybe NULL.  */

  GtkWidget *label_userid;
};
typedef struct _GpaKeyGenDlg GpaKeyGenDlg;


static gboolean
validate_name (GpaKeyGenDlg *self)
{
  const char *s;

  s = gpa_validate_gpg_name (gtk_entry_get_text
                             (GTK_ENTRY (self->entry_name)));
  if (s)
    gpa_window_error (s, self->dialog);
  return !s;
}

static gboolean
validate_email (GpaKeyGenDlg *self)
{
  const char *s;

  s = gpa_validate_gpg_email (gtk_entry_get_text
                              (GTK_ENTRY (self->entry_email)));
  if (s)
    gpa_window_error (s, self->dialog);
  return !s;
}

static gboolean
validate_comment (GpaKeyGenDlg *self)
{
  const char *s;

  s = gpa_validate_gpg_comment (gtk_entry_get_text
                                (GTK_ENTRY (self->entry_comment)));
  if (s)
    gpa_window_error (s, self->dialog);
  return !s;
}


/* This callback gets called each time the user clicks on the [OK] or
   [Cancel] buttons.  If the button was [OK], it verifies that the
   input makes sense.  */
static void
response_cb (GtkDialog *dlg, gint response, gpointer user_data)
{
  GpaKeyGenDlg *self = user_data;
  const char *temp;
  int   keysize;

  if (response != GTK_RESPONSE_OK)
    return;

  temp = (self->entry_keysize
          ? gtk_combo_box_get_active_text (GTK_COMBO_BOX
                                           (self->entry_keysize))
          :  NULL);
  keysize = temp? atoi (temp):0;

  if (!validate_name (self)
      || !validate_email (self)
      || !validate_comment (self))
    {
      g_signal_stop_emission_by_name (dlg, "response");
    }
  else if (self->forcard)
    ;
  else if (keysize < 1024)
    {
      gpa_window_error (_("You must enter a key size."), self->dialog);
      g_signal_stop_emission_by_name (dlg, "response");
    }

  /* FIXME: check that the expire date is not in the past.  */
}


static void
update_preview_cb (void *widget, void *user_data)
{
  GpaKeyGenDlg *self = user_data;
  const char *name, *email, *comment;
  char *uid;
  (void)widget;

  name = gtk_entry_get_text (GTK_ENTRY (self->entry_name));
  if (!name)
    name = "";
  email = gtk_entry_get_text (GTK_ENTRY (self->entry_email));
  if (!email)
    email = "";
  comment = gtk_entry_get_text (GTK_ENTRY (self->entry_comment));
  if (!comment)
    comment = "";

  uid = g_strdup_printf ("%s%s%s%s%s%s%s",
                         name,
                         *comment? " (":"", comment, *comment? ")":"",
                         *email? " <":"", email, *email? ">":"");
  gtk_label_set_text (GTK_LABEL (self->label_userid), uid);
  g_free (uid);
}



/* Helper to create the dialog.  PARENT is the parent window.  */
static void
create_dialog (GpaKeyGenDlg *self, GtkWidget *parent, const char *forcard)
{
  GtkWidget *dialog;
  GtkWidget *vbox;
  GtkWidget *table;

  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *combo;
  GtkWidget *entry;
  GtkWidget *button;

  int rowidx, idx;


  dialog = gtk_dialog_new_with_buttons
    (forcard ? _("Generate card key") : _("Generate key"),
     GTK_WINDOW (parent),
     GTK_DIALOG_MODAL,
     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
     GTK_STOCK_OK, GTK_RESPONSE_OK,
     NULL);
  self->dialog = dialog;
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);

  vbox = GTK_DIALOG (dialog)->vbox;
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);

  table = gtk_table_new (9, 2, FALSE);
  gtk_container_set_border_width (GTK_CONTAINER (table), 5);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
  rowidx = 0;

  label = gtk_label_new_with_mnemonic (_("_Algorithm: "));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, rowidx, rowidx+1,
                    GTK_FILL, GTK_SHRINK, 0, 0);

  if (forcard)
    {
      label = gtk_label_new (forcard);
      gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
      gtk_table_attach (GTK_TABLE (table), label, 1, 2, rowidx, rowidx+1,
                        GTK_FILL, GTK_SHRINK, 0, 0);
      rowidx++;
    }
  else
    {
      combo = gtk_combo_box_new_text ();
      for (idx=0; algorithm_table[idx].name; idx++)
	gtk_combo_box_append_text (GTK_COMBO_BOX (combo),
				   algorithm_table[idx].name);
      gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
      gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
      gtk_table_attach (GTK_TABLE (table), combo, 1, 2, rowidx, rowidx+1,
                        GTK_FILL, GTK_SHRINK, 0, 0);
      self->entry_algo = combo;
      rowidx++;

      label = gtk_label_new_with_mnemonic (_("_Key size (bits): "));
      gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
      gtk_table_attach (GTK_TABLE (table), label, 0, 1, rowidx, rowidx+1,
                        GTK_FILL, GTK_SHRINK, 0, 0);
      combo = gtk_combo_box_new_text ();
      gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "1024");
      gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "1536");
      gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "2048");
      gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "3072");
      gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "4096");
      gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 2);
      gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);

      gtk_table_attach (GTK_TABLE (table), combo, 1, 2, rowidx, rowidx+1,
                        GTK_FILL, GTK_SHRINK, 0, 0);
      self->entry_keysize = combo;
      rowidx++;
    }


  label = gtk_label_new (NULL);  /* Dummy label.  */
  gtk_table_attach (GTK_TABLE (table), label, 0, 2, rowidx, rowidx+1,
                    GTK_FILL, GTK_FILL, 0, 0);
  rowidx++;


  label = gtk_label_new_with_mnemonic (_("User ID: "));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, rowidx, rowidx+1,
		    GTK_FILL, GTK_SHRINK, 0, 0);
  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 1, 2, rowidx, rowidx+1,
                    GTK_FILL, GTK_SHRINK, 0, 0);
  self->label_userid = label;
  rowidx++;

  label = gtk_label_new (NULL);  /* Dummy label.  */
  gtk_table_attach (GTK_TABLE (table), label, 0, 2, rowidx, rowidx+1,
                    GTK_FILL, GTK_FILL, 0, 0);
  rowidx++;

  label = gtk_label_new_with_mnemonic (_("_Name: "));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, rowidx, rowidx+1,
		    GTK_FILL, GTK_SHRINK, 0, 0);
  entry = gtk_entry_new ();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
  gtk_table_attach (GTK_TABLE (table), entry, 1, 2, rowidx, rowidx+1,
                    GTK_FILL|GTK_EXPAND, GTK_SHRINK, 0, 0);
  self->entry_name = entry;
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (update_preview_cb), self);
  rowidx++;

  label = gtk_label_new_with_mnemonic (_("_Email: "));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, rowidx, rowidx+1,
		    GTK_FILL, GTK_SHRINK, 0, 0);
  entry = gtk_entry_new ();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
  gtk_table_attach (GTK_TABLE (table), entry, 1, 2, rowidx, rowidx+1,
                    GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
  self->entry_email = entry;
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (update_preview_cb), self);
  rowidx++;

  label = gtk_label_new_with_mnemonic (_("_Comment: "));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, rowidx, rowidx+1,
		    GTK_FILL, GTK_SHRINK, 0, 0);
  entry = gtk_entry_new ();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
  gtk_table_attach (GTK_TABLE (table), entry, 1, 2, rowidx, rowidx+1,
                    GTK_FILL|GTK_EXPAND, GTK_SHRINK, 0, 0);
  self->entry_comment = entry;
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (update_preview_cb), self);
  rowidx++;

  label = gtk_label_new_with_mnemonic (_("_Expires: "));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, rowidx, rowidx+1,
                    GTK_FILL, GTK_SHRINK, 0, 0);
  button = gpa_date_box_new ();
  gtk_table_attach (GTK_TABLE (table), button, 1, 2, rowidx, rowidx+1,
                    GTK_FILL, GTK_SHRINK, 0, 0);
  self->entry_expire = button;
  rowidx++;

  if (forcard)
    {
      label = gtk_label_new_with_mnemonic (_("Backup: "));
      gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
      gtk_table_attach (GTK_TABLE (table), label, 0, 1, rowidx, rowidx+1,
                        GTK_FILL, GTK_SHRINK, 0, 0);
      button = gtk_check_button_new ();
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
      hbox = gtk_hbox_new (FALSE, 0);
      gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
      gtk_table_attach (GTK_TABLE (table), hbox, 1, 2, rowidx, rowidx+1,
                        GTK_FILL, GTK_SHRINK, 0, 0);
      self->entry_backup = button;
      gpa_add_tooltip (hbox,
                       _("If checked the encryption key will be created "
                         "and stored to a backup file and then loaded into "
                         "the card.  This is recommended so that encrypted "
                         "messages can be decrypted even if the card has a "
                         "malfunction."));
    }
  else
    self->entry_backup = NULL;

}


/* Run the "Generate Key" dialog and if the user presses OK, return
   the values from the dialog in a newly allocated gpa_keygen_para_t
   struct.  If FORCARD is not NULL display a dialog suitable for
   generation of keys on the OpenPGP smartcard; thye string will be
   shown to identify the capabilities of the card.  If the user
   pressed "Cancel", return NULL.  The returned struct has to be freed
   with gpa_keygen_para_free.  */
gpa_keygen_para_t *
gpa_key_gen_run_dialog (GtkWidget *parent, const char *forcard)
{
  GpaKeyGenDlg *self;
  gpa_keygen_para_t *params;

  self = xcalloc (1, sizeof *self);
  self->forcard = !!forcard;
  create_dialog (self, parent, forcard);
  g_signal_connect (G_OBJECT (self->dialog), "response",
                    G_CALLBACK (response_cb), self);


  gtk_widget_show_all (self->dialog);
  if (gtk_dialog_run (GTK_DIALOG (self->dialog)) != GTK_RESPONSE_OK)
    {
      gtk_widget_destroy (self->dialog);
      g_free (self);
      return NULL;
    }

  /* The user pressed OK: Populate gpa_keygen_para_t struct and
     return it.  */
  params = gpa_keygen_para_new ();
  params->name   = g_strdup (gtk_entry_get_text
                             (GTK_ENTRY (self->entry_name)));
  params->email  = g_strdup (gtk_entry_get_text
                             (GTK_ENTRY (self->entry_email)));
  params->comment= g_strdup (gtk_entry_get_text
                             (GTK_ENTRY (self->entry_comment)));

  if (forcard)
    params->algo = GPA_KEYGEN_ALGO_VIA_CARD;
  else
    {
      char *temp;
      int idx;

      idx = gtk_combo_box_get_active (GTK_COMBO_BOX (self->entry_algo));
      if (idx < 0 || idx >= DIM (algorithm_table)
          || !algorithm_table[idx].name)
        {
	  gpa_keygen_para_free (params);
          gtk_widget_destroy (self->dialog);
          g_free (self);
          g_return_val_if_reached (NULL);
        }
      params->algo = algorithm_table[idx].algo;
      temp = gtk_combo_box_get_active_text
        (GTK_COMBO_BOX (self->entry_keysize));
      params->keysize = temp? atoi (temp) : 0;
    }

  gpa_date_box_get_date (GPA_DATE_BOX (self->entry_expire), &params->expire);

  params->backup = self->entry_backup
                   ? gtk_toggle_button_get_active
                        (GTK_TOGGLE_BUTTON (self->entry_backup))
                   : 0;

  gtk_widget_destroy (self->dialog);
  g_free (self);

  return params;
}