This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathykclient.c
1592 lines (1356 loc) · 36.5 KB
/
ykclient.c
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ykclient.c --- Implementation of Yubikey OTP validation client library.
*
* Written by Simon Josefsson <[email protected]>.
* Copyright (c) 2006-2013 Yubico AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "ykclient.h"
#include "ykclient_server_response.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <curl/curl.h>
#include "sha.h"
#include "cencode.h"
#include "cdecode.h"
#define NONCE_LEN 32
#define MAX_TEMPLATES 255
#define ADD_NONCE "&nonce="
#define ADD_OTP "&otp="
#define ADD_HASH "&h="
#define ADD_ID "?id="
#define ADD_TS "×tamp=1"
#define TEMPLATE_FORMAT_OLD 1
#define TEMPLATE_FORMAT_NEW 2
struct ykclient_st
{
const char *ca_path;
const char *ca_info;
const char *proxy;
size_t num_templates;
char **url_templates;
int template_format;
char last_url[256];
unsigned int client_id;
size_t keylen;
const char *key;
char *key_buf;
char *nonce;
char nonce_supplied;
int retry;
int max_retries;
int verify_signature;
ykclient_server_response_t *srv_response;
};
struct curl_data
{
char *curl_chunk;
size_t curl_chunk_size;
};
struct ykclient_handle_st
{
CURL **easy;
CURLM *multi;
size_t num_easy;
struct curl_data *data;
char **url_exp;
};
static const char *default_url_templates[] = {
"https://api.yubico.com/wsapi/2.0/verify",
};
static const char *user_agent = PACKAGE "/" PACKAGE_VERSION;
/** Initialise the global context for the library
*
* This function is not thread safe. It must be invoked successfully
* before using any other function.
*
* @return one of the YKCLIENT_* values or YKCLIENT_OK on success.
*/
ykclient_rc
ykclient_global_init (void)
{
if (curl_global_init (CURL_GLOBAL_ALL) != 0)
return YKCLIENT_CURL_INIT_ERROR;
return YKCLIENT_OK;
}
/** Deinitialise the global context for the library
*
* This function is not thread safe. After this function has been
* called, no other library functions may be used reliably.
*
* @return one of the YKCLIENT_* values or YKCLIENT_OK on success.
*/
void
ykclient_global_done (void)
{
curl_global_cleanup ();
}
/** Initialise a new configuration structure
*
* Additional options can be set with ykclient_set_* functions
* after memory for the configuration has been allocated with
* this function.
*
* @param ykc Where to write a pointer to the new configuration.
* @return one of the YKCLIENT_* values or YKCLIENT_OK on success.
*/
ykclient_rc
ykclient_init (ykclient_t ** ykc)
{
ykclient_t *p;
p = malloc (sizeof (*p));
if (!p)
{
return YKCLIENT_OUT_OF_MEMORY;
}
memset (p, 0, (sizeof (*p)));
p->ca_path = NULL;
p->ca_info = NULL;
p->proxy = NULL;
p->key = NULL;
p->keylen = 0;
p->key_buf = NULL;
memset (p->last_url, 0, sizeof (p->last_url));
p->nonce = NULL;
p->nonce_supplied = 0;
p->retry = 0;
p->max_retries = DEFAULT_MAX_RETRIES;
p->srv_response = NULL;
/*
* Verification of server signature can only be done if there is
* an API key provided
*/
p->verify_signature = 0;
/*
* Set the default URLs (these can be overridden later)
*/
ykclient_set_url_bases (p,
sizeof (default_url_templates) /
sizeof (char *), default_url_templates);
*ykc = p;
return YKCLIENT_OK;
}
/** Frees a configuration structure allocated by ykclient_init
*
* Any handles created with ykclient_handle_init must be freed
* separately with ykclient_handle_done.
*
* @param ykc configuration to free.
*/
void
ykclient_done (ykclient_t ** ykc)
{
if (ykc && *ykc)
{
if ((*ykc)->url_templates)
{
size_t i;
for (i = 0; i < (*ykc)->num_templates; i++)
{
free ((*ykc)->url_templates[i]);
}
free ((*ykc)->url_templates);
}
if ((*ykc)->srv_response)
{
ykclient_server_response_free((*ykc)->srv_response);
}
free ((*ykc)->key_buf);
free (*ykc);
}
if (ykc)
{
*ykc = NULL;
}
}
/** Callback for processing CURL data received from the validation server
*
*/
static size_t
curl_callback (void *ptr, size_t size, size_t nmemb, void *data)
{
struct curl_data *curl_data = (struct curl_data *) data;
size_t realsize = size * nmemb;
char *p;
if (curl_data->curl_chunk)
{
p = realloc (curl_data->curl_chunk,
curl_data->curl_chunk_size + realsize + 1);
}
else
{
p = malloc (curl_data->curl_chunk_size + realsize + 1);
}
if (!p)
{
return 0;
}
curl_data->curl_chunk = p;
memcpy (&(curl_data->curl_chunk[curl_data->curl_chunk_size]), ptr,
realsize);
curl_data->curl_chunk_size += realsize;
curl_data->curl_chunk[curl_data->curl_chunk_size] = 0;
return realsize;
}
/** Create a new handle
*
* These handles contain curl easy and multi handles required to
* process a request.
*
* This must be called after configuring template URLs, and handles
* MUST NOT be reused if new template URLs have been set.
*
* If new template URLs have been set all handles must be destroyed
* with ykclient_handle_close and recreated with this function.
*
* Handles must be cleaned with ykclient_handle_cleanup between
* requests, and closed with ykclient_handle_close when they are no
* longer needed.
*
* @param ykc Yubikey client configuration.
* @param[out] ykh where to write pointer to new handle.
* @return one of the YKCLIENT_* values or YKCLIENT_OK on success.
*/
ykclient_rc
ykclient_handle_init (ykclient_t * ykc, ykclient_handle_t ** ykh)
{
ykclient_handle_t *p;
*ykh = NULL;
p = malloc (sizeof (*p));
if (!p)
{
return YKCLIENT_OUT_OF_MEMORY;
}
memset (p, 0, (sizeof (*p)));
p->multi = curl_multi_init ();
if (!p->multi)
{
free (p);
return YKCLIENT_CURL_INIT_ERROR;
}
p->easy = malloc (sizeof (CURL *) * ykc->num_templates);
if (!p->easy)
{
ykclient_handle_done (&p);
return YKCLIENT_OUT_OF_MEMORY;
}
for (p->num_easy = 0; p->num_easy < ykc->num_templates; p->num_easy++)
{
CURL *easy;
struct curl_data *data;
data = malloc (sizeof (*data));
if (!data)
{
ykclient_handle_done (&p);
return YKCLIENT_OUT_OF_MEMORY;
}
data->curl_chunk = NULL;
data->curl_chunk_size = 0;
easy = curl_easy_init ();
if (!easy)
{
free (data);
ykclient_handle_done (&p);
return YKCLIENT_CURL_INIT_ERROR;
}
if (ykc->ca_path)
{
curl_easy_setopt (easy, CURLOPT_CAPATH, ykc->ca_path);
}
if (ykc->ca_info)
{
curl_easy_setopt (easy, CURLOPT_CAINFO, ykc->ca_info);
}
if (ykc->proxy)
{
/*
* The proxy string may be prefixed with [scheme]://ip:port to specify which kind of proxy is used.
* Valid choices are: socks4://, socks4a://, socks5:// or socks5h://
* Use socks5h to ask the proxy to do the dns resolving.
* If no scheme or port is specified HTTP proxy port 1080 will be used.
*/
curl_easy_setopt (easy, CURLOPT_PROXY, ykc->proxy);
}
curl_easy_setopt (easy, CURLOPT_WRITEDATA, (void *) data);
curl_easy_setopt (easy, CURLOPT_PRIVATE, (void *) data);
curl_easy_setopt (easy, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt (easy, CURLOPT_USERAGENT, user_agent);
curl_multi_add_handle (p->multi, easy);
p->easy[p->num_easy] = easy;
}
if(p->num_easy == 0) {
ykclient_handle_done (&p);
return YKCLIENT_BAD_INPUT;
}
/* Take this opportunity to allocate the array for expanded URLs */
p->url_exp = malloc (sizeof (char *) * p->num_easy);
if (!p->url_exp)
{
ykclient_handle_done (&p);
return YKCLIENT_OUT_OF_MEMORY;
}
memset (p->url_exp, 0, (sizeof (char *) * p->num_easy));
*ykh = p;
return YKCLIENT_OK;
}
/** Cleanup memory allocated for requests
*
* Cleans up any memory allocated and held by the handle for a
* request. Must be called after each request.
*
* @param ykh to close.
*/
void
ykclient_handle_cleanup (ykclient_handle_t * ykh)
{
size_t i;
struct curl_data *data;
int requests = 0;
/*
* Curl will not allow a connection to be re-used unless the
* request finished, call curl_multi_perform one last time
* to give libcurl an opportunity to mark the request as
* complete.
*
* If the delay between yk_request_send and
* ykclient_handle_cleanup is sufficient to allow the request
* to complete, the connection can be re-used, else it will
* be re-established on next yk_request_send.
*/
(void) curl_multi_perform (ykh->multi, &requests);
for (i = 0; i < ykh->num_easy; i++)
{
free (ykh->url_exp[i]);
ykh->url_exp[i] = NULL;
curl_easy_getinfo (ykh->easy[i], CURLINFO_PRIVATE, (char **) &data);
free (data->curl_chunk);
data->curl_chunk = NULL;
data->curl_chunk_size = 0;
curl_multi_remove_handle (ykh->multi, ykh->easy[i]);
curl_multi_add_handle (ykh->multi, ykh->easy[i]);
}
}
/** Close a handle freeing memory and destroying connections
*
* Frees any memory allocated for the handle, and calls various CURL
* functions to destroy all curl easy and multi handles created for
* this handle.
*
* @param ykh to close.
*/
void
ykclient_handle_done (ykclient_handle_t ** ykh)
{
struct curl_data *data;
size_t i;
if (ykh && *ykh)
{
ykclient_handle_cleanup (*ykh);
for (i = 0; i < (*ykh)->num_easy; i++)
{
curl_easy_getinfo ((*ykh)->easy[i], CURLINFO_PRIVATE,
(char **) &data);
free (data);
curl_multi_remove_handle ((*ykh)->multi, (*ykh)->easy[i]);
curl_easy_cleanup ((*ykh)->easy[i]);
}
if ((*ykh)->multi)
{
curl_multi_cleanup ((*ykh)->multi);
}
free ((*ykh)->url_exp);
free ((*ykh)->easy);
free (*ykh);
}
if (ykh)
{
*ykh = NULL;
}
}
void
ykclient_set_verify_signature (ykclient_t * ykc, int value)
{
if (ykc == NULL)
{
return;
}
ykc->verify_signature = value;
}
void
ykclient_set_client (ykclient_t * ykc,
unsigned int client_id, size_t keylen, const char *key)
{
ykc->client_id = client_id;
ykc->keylen = keylen;
ykc->key = key;
}
ykclient_rc
ykclient_set_client_hex (ykclient_t * ykc,
unsigned int client_id, const char *key)
{
size_t i;
size_t key_len;
size_t bin_len;
ykc->client_id = client_id;
if (key == NULL)
{
return YKCLIENT_OK;
}
key_len = strlen (key);
if (key_len % 2 != 0)
{
return YKCLIENT_HEX_DECODE_ERROR;
}
bin_len = key_len / 2;
free (ykc->key_buf);
ykc->key_buf = malloc (bin_len);
if (!ykc->key_buf)
{
return YKCLIENT_OUT_OF_MEMORY;
}
for (i = 0; i < bin_len; i++)
{
int tmp;
if (sscanf (&key[2 * i], "%02x", &tmp) != 1)
{
free (ykc->key_buf);
ykc->key_buf = NULL;
return YKCLIENT_HEX_DECODE_ERROR;
}
ykc->key_buf[i] = tmp;
}
ykc->keylen = bin_len;
ykc->key = ykc->key_buf;
return YKCLIENT_OK;
}
ykclient_rc
ykclient_set_client_b64 (ykclient_t * ykc,
unsigned int client_id, const char *key)
{
size_t key_len;
ssize_t dec_len;
base64_decodestate b64;
ykc->client_id = client_id;
if (key == NULL)
{
return YKCLIENT_OK;
}
key_len = strlen (key);
free (ykc->key_buf);
ykc->key_buf = malloc (key_len + 1);
if (!ykc->key_buf)
{
return YKCLIENT_OUT_OF_MEMORY;
}
base64_init_decodestate (&b64);
dec_len = (ssize_t) base64_decode_block (key, key_len, ykc->key_buf, &b64);
if (dec_len < 0)
{
return YKCLIENT_BASE64_DECODE_ERROR;
}
ykc->keylen = (size_t) dec_len;
ykc->key = ykc->key_buf;
/* Now that we have a key the sensible default is to verify signatures */
ykc->verify_signature = 1;
return YKCLIENT_OK;
}
/** Set the CA path
*
* Must be called before creating handles.
*/
void
ykclient_set_ca_path (ykclient_t * ykc, const char *ca_path)
{
ykc->ca_path = ca_path;
}
/** Set the CA info, needed for linking with GnuTLS
*
* Must be called before creating handles.
*/
void
ykclient_set_ca_info (ykclient_t * ykc, const char *ca_info)
{
ykc->ca_info = ca_info;
}
/** Set the proxy
*
* Must be called before creating handles.
*/
void
ykclient_set_proxy (ykclient_t * ykc, const char *proxy)
{
ykc->proxy = proxy;
}
void
ykclient_set_max_retries (ykclient_t *ykc, const int retries)
{
ykc->max_retries = retries;
}
/** Set a single URL template
*
* @param ykc Yubikey client configuration.
* @param url_template to set.
* @return one of the YKCLIENT_* values or YKCLIENT_OK on success.
*/
ykclient_rc
ykclient_set_url_template (ykclient_t * ykc, const char *url_template)
{
return ykclient_set_url_templates (ykc, 1, (const char **) &url_template);
}
/** Set the URLs of the YK validation servers
*
* The URL strings will be copied to the new buffers, so the
* caller may free the original URL strings if they are no
* longer needed.
*
* @note This function MUST be called before calling ykclient_handle_init
*
* @param ykc Yubikey client configuration.
* @param num_templates Number of template URLs passed in url_templates.
* @param url_templates Array of template URL strings.
* @return one of the YKCLIENT_* values or YKCLIENT_OK on success.
*/
ykclient_rc
ykclient_set_url_templates (ykclient_t * ykc, size_t num_templates,
const char **url_templates)
{
ykclient_rc ret =
ykclient_set_url_bases (ykc, num_templates, url_templates);
if (ret == YKCLIENT_OK)
{
ykc->template_format = TEMPLATE_FORMAT_OLD;
}
return ret;
}
ykclient_rc
ykclient_set_url_bases (ykclient_t * ykc, size_t num_templates,
const char **url_templates)
{
size_t i;
if (num_templates > MAX_TEMPLATES)
{
return YKCLIENT_BAD_INPUT;
}
/* Clean out any previously allocated templates */
if (ykc->url_templates)
{
for (i = 0; i < ykc->num_templates; i++)
{
free (ykc->url_templates[i]);
}
free (ykc->url_templates);
}
/* Reallocate the template array */
ykc->url_templates = malloc (sizeof (char *) * num_templates);
if (!ykc->url_templates)
{
return YKCLIENT_OUT_OF_MEMORY;
}
memset (ykc->url_templates, 0, (sizeof (char *) * num_templates));
for (ykc->num_templates = 0; ykc->num_templates < num_templates;
ykc->num_templates++)
{
ykc->url_templates[ykc->num_templates] =
strdup (url_templates[ykc->num_templates]);
if (!ykc->url_templates[ykc->num_templates])
{
return YKCLIENT_OUT_OF_MEMORY;
}
}
ykc->template_format = TEMPLATE_FORMAT_NEW;
return YKCLIENT_OK;
}
/*
* Set the nonce. A default nonce is generated in ykclient_init (), but
* if you either want to specify your own nonce, or want to remove the
* nonce (needed to send signed requests to v1 validation servers),
* you must call this function. Set nonce to NULL to disable it.
*/
void
ykclient_set_nonce (ykclient_t * ykc, char *nonce)
{
ykc->nonce_supplied = 1;
ykc->nonce = nonce;
}
/** Convert a ykclient_rc value to a string
*
* Returns a more verbose error message relating to the ykclient_rc
* value passed as ret.
*
* @param ret the error code to convert.
* @return verbose error string.
*/
const char *
ykclient_strerror (ykclient_rc ret)
{
const char *p;
switch (ret)
{
case YKCLIENT_OK:
p = "Success";
break;
case YKCLIENT_CURL_PERFORM_ERROR:
p = "Error performing curl";
break;
case YKCLIENT_BAD_OTP:
p = "Yubikey OTP was bad (BAD_OTP)";
break;
case YKCLIENT_REPLAYED_OTP:
p = "Yubikey OTP was replayed (REPLAYED_OTP)";
break;
case YKCLIENT_REPLAYED_REQUEST:
p = "Yubikey request was replayed (REPLAYED_REQUEST)";
break;
case YKCLIENT_BAD_SIGNATURE:
p = "Request signature was invalid (BAD_SIGNATURE)";
break;
case YKCLIENT_BAD_SERVER_SIGNATURE:
p = "Server response signature was invalid (BAD_SERVER_SIGNATURE)";
break;
case YKCLIENT_MISSING_PARAMETER:
p = "Request was missing a parameter (MISSING_PARAMETER)";
break;
case YKCLIENT_NO_SUCH_CLIENT:
p = "Client identity does not exist (NO_SUCH_CLIENT)";
break;
case YKCLIENT_OPERATION_NOT_ALLOWED:
p = "Authorization denied (OPERATION_NOT_ALLOWED)";
break;
case YKCLIENT_BACKEND_ERROR:
p = "Internal server error (BACKEND_ERROR)";
break;
case YKCLIENT_NOT_ENOUGH_ANSWERS:
p = "Too few validation servers available (NOT_ENOUGH_ANSWERS)";
break;
case YKCLIENT_OUT_OF_MEMORY:
p = "Out of memory";
break;
case YKCLIENT_PARSE_ERROR:
p = "Could not parse server response";
break;
case YKCLIENT_FORMAT_ERROR:
p = "Internal printf format error";
break;
case YKCLIENT_CURL_INIT_ERROR:
p = "Error initializing curl";
break;
case YKCLIENT_HMAC_ERROR:
p = "HMAC signature validation/generation error";
break;
case YKCLIENT_HEX_DECODE_ERROR:
p = "Error decoding hex string";
break;
case YKCLIENT_BASE64_DECODE_ERROR:
p = "Error decoding base64 string";
break;
case YKCLIENT_NOT_IMPLEMENTED:
p = "Not implemented";
break;
case YKCLIENT_HANDLE_NOT_REINIT:
p = "Request template URLs modified without reinitialising handles";
break;
case YKCLIENT_BAD_INPUT:
p = "Passed invalid or incorrect number of parameters";
break;
default:
p = "Unknown error";
}
return p;
}
/** Generates or duplicates an existing nonce value
*
* If a nonce value was set with ykclient_set_nonce, it will be duplicated
* and a pointer to the memory returned in nonce.
*
* If a nonce value has not been set a new buffer will be allocated and a
* random string of NONCE_LEN will be written to it.
*
* Memory pointed to by nonce must be freed by the called when it is no
* longer requiest.
*
* @param ykc Yubikey client configuration.
* @param[out] nonce where to write the pointer to the nonce value.
* @return one of the YKCLIENT_* values or YKCLIENT_OK on success.
*/
static ykclient_rc
ykclient_generate_nonce (ykclient_t * ykc, char **nonce)
{
*nonce = NULL;
/*
* If we were supplied with a static value just strdup,
* makes memory management easier.
*/
if (ykc->nonce_supplied)
{
if (ykc->nonce)
{
*nonce = strdup (ykc->nonce);
if (*nonce == NULL)
return YKCLIENT_OUT_OF_MEMORY;
}
}
/*
* Generate a random 'nonce' value
*/
else
{
struct timeval tv;
size_t i;
char *p;
p = malloc (NONCE_LEN + 1);
if (!p)
{
return YKCLIENT_OUT_OF_MEMORY;
}
gettimeofday (&tv, 0);
srandom (tv.tv_sec * tv.tv_usec);
for (i = 0; i < NONCE_LEN; ++i)
{
p[i] = (random () % 26) + 'a';
}
p[NONCE_LEN] = 0;
*nonce = p;
}
return YKCLIENT_OK;
}
static ykclient_rc
ykclient_expand_new_url (const char *template,
const char *encoded_otp, const char *nonce,
unsigned int client_id, char **url_exp)
{
size_t len =
strlen (template) + strlen (encoded_otp) + strlen (ADD_OTP) +
strlen (ADD_ID) + strlen(ADD_TS) + 1;
len += snprintf (NULL, 0, "%u", client_id);
if (nonce)
{
len += strlen (nonce) + strlen (ADD_NONCE);
}
*url_exp = malloc (len);
if (!*url_exp)
{
return YKCLIENT_OUT_OF_MEMORY;
}
if (nonce)
{
snprintf (*url_exp, len, "%s" ADD_ID "%u" ADD_NONCE "%s" ADD_OTP "%s" ADD_TS,
template, client_id, nonce, encoded_otp);
}
else
{
snprintf (*url_exp, len, "%s" ADD_ID "%u" ADD_OTP "%s" ADD_TS, template,
client_id, encoded_otp);
}
return YKCLIENT_OK;
}
static ykclient_rc
ykclient_expand_old_url (const char *template,
const char *encoded_otp, const char *nonce,
unsigned int client_id, char **url_exp)
{
{
size_t len;
ssize_t wrote;
len = strlen (template) + strlen (encoded_otp) + 20;
*url_exp = malloc (len);
if (!*url_exp)
{
return YKCLIENT_OUT_OF_MEMORY;
}
wrote = snprintf (*url_exp, len, template, client_id, encoded_otp);
if (wrote < 0 || (size_t) wrote > len)
{
return YKCLIENT_FORMAT_ERROR;
}
}
if (nonce)
{
/* Create new URL with nonce in it. */
char *nonce_url, *otp_offset;
size_t len;
ssize_t wrote;
len = strlen (*url_exp) + strlen (ADD_NONCE) + strlen (nonce) + 1;
nonce_url = malloc (len + 4); /* avoid valgrind complaint */
if (!nonce_url)
{
return YKCLIENT_OUT_OF_MEMORY;
}
/* Find the &otp= in url and insert ?nonce= before otp. Must get
* sorted headers since we calculate HMAC on the result.
*
* XXX this will break if the validation protocol gets a parameter that
* sorts in between "nonce" and "otp", because the headers we sign won't
* be alphabetically sorted if we insert the nonce between "nz" and "otp".
* Also, we assume that everyone will have at least one parameter ("id=")
* before "otp" so there is no need to search for "?otp=".
*/
otp_offset = strstr (*url_exp, ADD_OTP);
if (otp_offset == NULL)
{
/* point at \0 at end of url in case there is no otp */
otp_offset = *url_exp + len;
}
/* break up ykc->url where we want to insert nonce */
*otp_offset = 0;
wrote = snprintf (nonce_url, len, "%s" ADD_NONCE "%s&%s", *url_exp,
nonce, otp_offset + 1);
if (wrote < 0 || (size_t) wrote + 1 != len)
{
free (nonce_url);
return YKCLIENT_FORMAT_ERROR;
}
free (*url_exp);
*url_exp = nonce_url;
}
return YKCLIENT_OK;
}
/** Expand URL templates specified with set_url_templates
*
* Expands placeholders or inserts additional parameters for nonce,
* OTP, and signing values into duplicates of URL templates.
*
* The memory allocated for these duplicates must be freed
* by calling either ykclient_handle_done or ykclient_handle_cleanup
* after they are no longer needed.
*
* @param ykc Yubikey client configuration.
* @param ykh Yubikey client handle.
* @param yubikey OTP string passed to the client.
* @param nonce Random value included in the request and validated in the response.