summaryrefslogtreecommitdiff
path: root/src/anastasis/anastasis-gtk_handle-method-totp.c
blob: 49aff755726068eaedf733812ca88bb05be60dfb (plain)
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
/*
     This file is part of anastasis-gtk.
     Copyright (C) 2021 Anastasis SARL

     Anastasis 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, or (at your
     option) any later version.

     Anastasis 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 Anastasis; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file src/anastasis/anastasis-gtk_handle-method-totp.c
 * @brief Handle dialogs for TOTP (RFC 6238)
 * @author Christian Grothoff
 */
#include <gnunet/gnunet_util_lib.h>
#include "anastasis-gtk_action.h"
#include "anastasis-gtk_helper.h"
#include "anastasis-gtk_handle-identity-changed.h"
#include <jansson.h>
#include <gcrypt.h>

/**
 * How long is a TOTP code valid?
 */
#define TOTP_VALIDITY_PERIOD GNUNET_TIME_relative_multiply ( \
    GNUNET_TIME_UNIT_SECONDS, 30)

/**
 * Range of time we allow (plus-minus).
 */
#define TIME_INTERVAL_RANGE 2

/**
 * How long is the shared secret in bytes?
 */
#define SECRET_LEN 32

/**
 * Random secret used in the current dialog.
 */
static char totp_key[SECRET_LEN];


/**
 * Compute TOTP code at current time with offset
 * @a time_off for the @a key.
 *
 * @param time_off offset to apply when computing the code
 * @return TOTP code at this time
 */
static uint64_t
compute_totp (int time_off)
{
  struct GNUNET_TIME_Absolute now;
  time_t t;
  uint64_t ctr;
  uint8_t hmac[20]; /* SHA1: 20 bytes */

  now = GNUNET_TIME_absolute_get ();
  while (time_off < 0)
  {
    now = GNUNET_TIME_absolute_subtract (now,
                                         TOTP_VALIDITY_PERIOD);
    time_off++;
  }
  while (time_off > 0)
  {
    now = GNUNET_TIME_absolute_add (now,
                                    TOTP_VALIDITY_PERIOD);
    time_off--;
  }
  t = now.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
  ctr = GNUNET_htonll (t / 30LLU);

  {
    gcry_md_hd_t md;
    const unsigned char *mc;

    GNUNET_assert (GPG_ERR_NO_ERROR ==
                   gcry_md_open (&md,
                                 GCRY_MD_SHA1,
                                 GCRY_MD_FLAG_HMAC));
    gcry_md_setkey (md,
                    totp_key,
                    sizeof (totp_key));
    gcry_md_write (md,
                   &ctr,
                   sizeof (ctr));
    mc = gcry_md_read (md,
                       GCRY_MD_SHA1);
    GNUNET_assert (NULL != mc);
    memcpy (hmac,
            mc,
            sizeof (hmac));
    gcry_md_close (md);
  }

  {
    uint32_t code = 0;
    int offset;

    offset = hmac[sizeof (hmac) - 1] & 0x0f;
    for (int count = 0; count < 4; count++)
      code |= hmac[offset + 3 - count] << (8 * count);
    code &= 0x7fffffff;
    /* always use 8 digits (maximum) */
    code = code % 100000000;
    return code;
  }
}


/**
 * Compute RFC 3548 base32 encoding of @a val and write
 * result to @a enc.
 *
 * @param val value to encode
 * @param val_size number of bytes in @a val
 * @param[out] enc where to write the 0-terminated result
 */
static void
base32enc (const void *val,
           size_t val_size,
           char *enc)
{
  /**
   * 32 characters for encoding, using RFC 3548.
   */
  static char *encTable__ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  unsigned int wpos;
  unsigned int rpos;
  unsigned int bits;
  unsigned int vbit;
  const unsigned char *udata;

  udata = val;
  vbit = 0;
  wpos = 0;
  rpos = 0;
  bits = 0;
  while ((rpos < val_size) || (vbit > 0))
  {
    if ((rpos < val_size) && (vbit < 5))
    {
      bits = (bits << 8) | udata[rpos++];     /* eat 8 more bits */
      vbit += 8;
    }
    if (vbit < 5)
    {
      bits <<= (5 - vbit);     /* zero-padding */
      GNUNET_assert (vbit == ((val_size * 8) % 5));
      vbit = 5;
    }
    enc[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
    vbit -= 5;
  }
  GNUNET_assert (0 == vbit);
  enc[wpos] = '\0';
}


/**
 * Recompute the QR code shown in @a builder from
 * totp and the user's name for the secret.
 *
 * @param builder the dialog builder
 */
static void
refresh_totp (GtkBuilder *builder)
{
  GtkEntry *q;
  const char *name;
  char *u_name;
  char *uri;
  char base_sec[sizeof (totp_key) * 2];
  GdkPixbuf *pb;
  GtkImage *img;

  gtk_widget_set_sensitive (
    GTK_WIDGET (gtk_builder_get_object (builder,
                                        "anastasis_gtk_b_totp_dialog_btn_ok")),
    FALSE);
  q = GTK_ENTRY (gtk_builder_get_object (builder,
                                         "anastasis_gtk_b_totp_dialog_name_entry"));
  name = gtk_entry_get_text (q);
  u_name = TALER_urlencode (name);
  base32enc (totp_key,
             sizeof (totp_key),
             base_sec);
  GNUNET_asprintf (&uri,
                   "otpauth://totp/%s?digits=8&secret=%s",
                   u_name,
                   base_sec);
  GNUNET_free (u_name);
  img = GTK_IMAGE (gtk_builder_get_object (builder,
                                           "qr_image"));
  pb = AG_setup_qrcode (GTK_WIDGET (img),
                        uri,
                        strlen (uri));
  GNUNET_free (uri);
  if (NULL != pb)
  {
    gtk_image_set_from_pixbuf (img,
                               pb);
    g_object_unref (pb);
  }
}


/**
 * Function called from the totp dialog upon completion.
 *
 * @param dialog the pseudonym selection dialog
 * @param response_id response code from the dialog
 * @param user_data the builder of the dialog
 */
void
anastasis_gtk_b_totp_dialog_response_cb (GtkDialog *dialog,
                                         gint response_id,
                                         gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);
  GtkEntry *q;
  const char *name;
  json_t *args;

  if (GTK_RESPONSE_OK != response_id)
  {
    gtk_widget_destroy (GTK_WIDGET (dialog));
    g_object_unref (G_OBJECT (builder));
    return;
  }
  q = GTK_ENTRY (gtk_builder_get_object (builder,
                                         "anastasis_gtk_b_totp_dialog_name_entry"));
  name = gtk_entry_get_text (q);
  args = json_pack ("{ s:{s:s, s:o, s:s}}",
                    "authentication_method",
                    "type",
                    "totp",
                    "challenge",
                    GNUNET_JSON_from_data (totp_key,
                                           sizeof (totp_key)),
                    "instructions",
                    name);
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (G_OBJECT (builder));
  memset (totp_key,
          0,
          sizeof (totp_key));
  AG_freeze ();
  AG_ra = ANASTASIS_redux_action (AG_redux_state,
                                  "add_authentication",
                                  args,
                                  &AG_action_cb,
                                  NULL);
  json_decref (args);
}


void
totp_entry_changed_cb (GtkEntry *entry,
                       gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);
  GtkWidget *but;
  GtkEntry *q;
  const char *code;
  unsigned int val;
  char dummy;
  bool found = false;

  q = GTK_ENTRY (gtk_builder_get_object (builder,
                                         "totp_entry"));
  code = gtk_entry_get_text (q);
  if (1 != sscanf (code,
                   "%u%c",
                   &val,
                   &dummy))
    return;
  for (int i = -TIME_INTERVAL_RANGE;
       i <= TIME_INTERVAL_RANGE;
       i++)
  {
    if (val == compute_totp (i))
    {
      found = true;
      break;
    }
  }
  if (! found)
    return;
  but = GTK_WIDGET (gtk_builder_get_object (builder,
                                            "anastasis_gtk_b_totp_dialog_btn_ok"));
  gtk_widget_set_sensitive (but,
                            TRUE);
}


void
anastasis_gtk_b_totp_dialog_name_entry_changed_cb (GtkEntry *entry,
                                                   gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);
  GtkEntry *e;

  /* clear code user already entered, if any */
  e = GTK_ENTRY (gtk_builder_get_object (builder,
                                         "totp_entry"));
  gtk_entry_set_text (e, "");
  refresh_totp (builder);
}


/**
 * Callback invoked if the the "totp"-button is clicked.
 *
 * @param object
 * @param user_data unused
 */
void
anastasis_gtk_btn_add_auth_totp_clicked_cb (GObject *object,
                                            gpointer user_data)
{
  GtkWidget *ad;
  GtkBuilder *builder;

  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                              totp_key,
                              sizeof (totp_key));
  builder = GNUNET_GTK_get_new_builder ("anastasis_gtk_auth_add_totp.glade",
                                        NULL);
  if (NULL == builder)
  {
    GNUNET_break (0);
    return;
  }
  ad = GTK_WIDGET (gtk_builder_get_object (builder,
                                           "anastasis_gtk_b_totp_dialog"));
  refresh_totp (builder);
  {
    GtkWidget *toplevel;

    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (object));
    gtk_window_set_transient_for (GTK_WINDOW (ad),
                                  GTK_WINDOW (toplevel));
    gtk_window_present (GTK_WINDOW (ad));
  }
}