File: authenticate.c

package info (click to toggle)
libu2f-host 1.1.10-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,816 kB
  • sloc: sh: 4,508; ansic: 4,481; makefile: 135; xml: 28
file content (282 lines) | stat: -rw-r--r-- 7,233 bytes parent folder | download | duplicates (4)
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
/*
  Copyright (C) 2013-2015 Yubico AB

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

  This program 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 Lesser
  General Public License for more details.

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

#include <config.h>
#include "internal.h"

#include <json.h>
#include "b64/cencode.h"
#include "b64/cdecode.h"
#include "sha256.h"

static int
prepare_response2 (const char *encstr, const char *bdstr, const char *input,
		   char **response, size_t * response_len)
{
  int rc = U2FH_JSON_ERROR;
  struct json_object *jo = NULL, *enc = NULL, *bd = NULL, *key = NULL;
  char keyb64[256];
  size_t keylen = sizeof (keyb64);
  const char *reply;

  enc = json_object_new_string (encstr);
  if (enc == NULL)
    goto done;
  bd = json_object_new_string (bdstr);
  if (bd == NULL)
    goto done;

  rc = get_fixed_json_data (input, "keyHandle", keyb64, &keylen);
  if (rc != U2FH_OK)
    {
      goto done;
    }

  rc = U2FH_JSON_ERROR;
  key = json_object_new_string (keyb64);
  if (key == NULL)
    goto done;

  jo = json_object_new_object ();
  if (jo == NULL)
    goto done;

  json_object_object_add (jo, "signatureData", json_object_get (enc));
  json_object_object_add (jo, "clientData", json_object_get (bd));
  json_object_object_add (jo, "keyHandle", json_object_get (key));

  reply = json_object_to_json_string (jo);
  if (*response == NULL)
    {
      *response = strdup (reply);
    }
  else
    {
      if (strlen (reply) >= *response_len)
	{
	  rc = U2FH_SIZE_ERROR;
	  *response_len = strlen (reply) + 1;
	  goto done;
	}
      strcpy (*response, reply);
    }
  *response_len = strlen (reply);
  if (*response == NULL)
    rc = U2FH_MEMORY_ERROR;
  else
    rc = U2FH_OK;

done:
  json_object_put (jo);
  json_object_put (enc);
  json_object_put (bd);
  json_object_put (key);

  return rc;
}

static int
prepare_response (const unsigned char *buf, int len, const char *bd,
		  const char *input, char **response, size_t * response_len)
{
  base64_encodestate b64ctx;
  char b64enc[2048];
  char bdstr[2048];
  int cnt;

  if (len > 2048)
    return U2FH_MEMORY_ERROR;
  if (strlen (bd) > 2048)
    return U2FH_MEMORY_ERROR;

  base64_init_encodestate (&b64ctx);
  cnt = base64_encode_block (buf, len, b64enc, &b64ctx);
  base64_encode_blockend (b64enc + cnt, &b64ctx);

  base64_init_encodestate (&b64ctx);
  cnt = base64_encode_block (bd, strlen (bd), bdstr, &b64ctx);
  base64_encode_blockend (bdstr + cnt, &b64ctx);

  return prepare_response2 (b64enc, bdstr, input, response, response_len);
}

#define CHALLBINLEN 32
#define HOSIZE 32
#define MAXKHLEN 128
#define NOTSATISFIED "\x69\x85"

static u2fh_rc
_u2fh_authenticate (u2fh_devs * devs,
		    const char *challenge,
		    const char *origin, char **response,
		    size_t * response_len, u2fh_cmdflags flags)
{
  unsigned char data[CHALLBINLEN + HOSIZE + MAXKHLEN + 1];
  unsigned char buf[MAXDATASIZE];
  char bd[2048];
  size_t bdlen = sizeof (bd);
  size_t len;
  int rc;
  char chalb64[256];
  size_t challen = sizeof (chalb64);
  char khb64[256];
  size_t kh64len = sizeof (khb64);
  base64_decodestate b64;
  size_t khlen;
  int iterations = 0;

  rc = get_fixed_json_data (challenge, "challenge", chalb64, &challen);
  if (rc != U2FH_OK)
    return rc;

  rc = prepare_browserdata (chalb64, origin, AUTHENTICATE_TYP, bd, &bdlen);
  if (rc != U2FH_OK)
    return rc;

  sha256_buffer (bd, bdlen, data);

  prepare_origin (challenge, data + CHALLBINLEN);

  /* confusion between key_handle and keyHandle */
  rc = get_fixed_json_data (challenge, "keyHandle", khb64, &kh64len);
  if (rc != U2FH_OK)
    return rc;

  base64_init_decodestate (&b64);
  khlen = base64_decode_block (khb64, kh64len,
			       data + HOSIZE + CHALLBINLEN + 1, &b64);
  data[HOSIZE + CHALLBINLEN] = khlen;

  /* FIXME: Support asynchronous usage, through a new u2fh_cmdflags
     flag. */

  do
    {
      struct u2fdevice *dev;

      if (iterations > 0 && len == 2 && memcmp (buf, NOTSATISFIED, 2) == 0)
	{
	  Sleep (1000);
	}
      for (dev = devs->first; dev != NULL; dev = dev->next)
	{
	  unsigned char tmp_buf[MAXDATASIZE];
	  if (iterations == 0)
	    {
	      dev->skipped = 0;
	    }
	  else if (dev->skipped != 0)
	    {
	      continue;
	    }
	  len = MAXDATASIZE;
	  rc = send_apdu (devs, dev->id, U2F_AUTHENTICATE, data,
			  HOSIZE + CHALLBINLEN + khlen + 1,
			  flags & U2FH_REQUEST_USER_PRESENCE ? 3 : 7, tmp_buf,
			  &len);
	  if (rc != U2FH_OK)
	    {
	      return rc;
	    }
	  else if (len != 2)
	    {
	      memcpy (buf, tmp_buf, len);
	      break;
	    }
	  else if (memcmp (tmp_buf, NOTSATISFIED, 2) != 0)
	    {
	      dev->skipped = 1;
	    }
	  else
	    {
	      memcpy (buf, tmp_buf, len);
	    }
	}
      if (iterations++ > 15)
	{
	  return U2FH_TIMEOUT_ERROR;
	}
    }
  while ((flags & U2FH_REQUEST_USER_PRESENCE)
	 && len == 2 && memcmp (buf, NOTSATISFIED, 2) == 0);

  if (len == 2 && memcmp (buf, NOTSATISFIED, 2) != 0)
    {
      return U2FH_AUTHENTICATOR_ERROR;
    }
  else if ((flags & U2FH_REQUEST_USER_PRESENCE) == 0
	   && len == 2 && memcmp (buf, NOTSATISFIED, 2) == 0)
    {
      return U2FH_OK;
    }
  if (len != 2)
    {
      prepare_response (buf, len - 2, bd, challenge, response, response_len);
      return U2FH_OK;
    }

  return U2FH_TRANSPORT_ERROR;
}

/**
 * u2fh_authenticate2:
 * @devs: a device handle, from u2fh_devs_init() and u2fh_devs_discover().
 * @challenge: string with JSON data containing the challenge.
 * @origin: U2F origin URL.
 * @response: pointer to string for output data
 * @response_len: pointer to length of @response
 * @flags: set of ORed #u2fh_cmdflags values.
 *
 * Perform the U2F Authenticate operation.
 *
 * Returns: On success %U2FH_OK (integer 0) is returned, and on errors
 * an #u2fh_rc error code.
 */
u2fh_rc
u2fh_authenticate2 (u2fh_devs * devs,
		    const char *challenge,
		    const char *origin, char *response, size_t * response_len,
		    u2fh_cmdflags flags)
{
  return _u2fh_authenticate (devs, challenge, origin, &response, response_len,
			     flags);
}

/**
 * u2fh_authenticate:
 * @devs: a device handle, from u2fh_devs_init() and u2fh_devs_discover().
 * @challenge: string with JSON data containing the challenge.
 * @origin: U2F origin URL.
 * @response: pointer to pointer for output data
 * @flags: set of ORed #u2fh_cmdflags values.
 *
 * Perform the U2F Authenticate operation.
 *
 * Returns: On success %U2FH_OK (integer 0) is returned, and on errors
 * an #u2fh_rc error code.
 */
u2fh_rc
u2fh_authenticate (u2fh_devs * devs,
		   const char *challenge,
		   const char *origin, char **response, u2fh_cmdflags flags)
{
  size_t response_len = 0;

  *response = NULL;
  return _u2fh_authenticate (devs, challenge, origin, response, &response_len,
			     flags);
}