summaryrefslogtreecommitdiff
path: root/src/anastasis/anastasis-gtk_handle-method-totp.c
blob: 34cdbc47042c1758be0e6d9dd042d4fca851e0fe (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
/*
     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/platform.h>
#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>


/**
 * Random secret used in the current dialog.
 */
static char totp[32];


/**
 * 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);
  if (wpos < val_size)
    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) * 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,
             sizeof (totp),
             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));
  if (NULL != pb)
  {
    gtk_image_set_from_pixbuf (img,
                               pb);
    g_object_unref (pb);
    gtk_widget_set_sensitive (
      GTK_WIDGET (gtk_builder_get_object (builder,
                                          "anastasis_gtk_b_totp_dialog_btn_ok")),
      true);
  }
}


/**
 * 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,
                                           sizeof (totp)),
                    "instructions",
                    name);
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (G_OBJECT (builder));
  memset (totp,
          0,
          sizeof (totp));
  AG_freeze ();
  AG_ra = ANASTASIS_redux_action (AG_redux_state,
                                  "add_authentication",
                                  args,
                                  &AG_action_cb,
                                  NULL);
  json_decref (args);
}


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

  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,
                              sizeof (totp));
  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));
  }
}