This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
webauthn.c
3731 lines (3507 loc) · 184 KB
/
webauthn.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
/**
*
* Glewlwyd SSO Server
*
* Authentiation server
* Users are authenticated via various backend available: database, ldap
* Using various authentication methods available: password, OTP, send code, etc.
*
* WebAuthn scheme module
*
* Copyright 2019-2020 Nicolas Mora <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation;
* version 3 of the License.
*
* 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 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/>.
*
*/
#include <string.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include <gnutls/abstract.h>
#include <jansson.h>
#include <cbor.h>
#include <ldap.h>
#include <yder.h>
#include <orcania.h>
#include <rhonabwy.h>
#include "glewlwyd-common.h"
const char * iso_3166_list[] = {"AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BH", "BS", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", "CI", "HR", "CU", "CW", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU", "RW", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW", NULL};
#define G_PACKED_CERT_O_KEY "O="
#define G_PACKED_CERT_OU_KEY "OU="
#define G_PACKED_CERT_C_KEY "C="
#define G_PACKED_CERT_CN_KEY "CN="
#define G_PACKED_CERT_OU_VALUE "Authenticator Attestation"
#define G_PACKED_OID_AAGUID "1.3.6.1.4.1.45724.1.1.4"
#define G_APPLE_ANONYMOUS_ATTESTATION_OID "1.2.840.113635.100.8.2"
#define G_TABLE_WEBAUTHN_USER "gs_webauthn_user"
#define G_TABLE_WEBAUTHN_CREDENTIAL "gs_webauthn_credential"
#define G_TABLE_WEBAUTHN_ASSERTION "gs_webauthn_assertion"
#define SESSION_LENGTH 32
#define USER_ID_LENGTH 32
#define FLAG_USER_PRESENT 0x01
#define FLAG_USER_VERIFY 0x04
#define FLAG_AT 0x40
#define FLAG_ED 0x80
#define COUNTER_LEN 4
#define AAGUID_LEN 16
#define CRED_ID_L_LEN 2
#define FLAGS_OFFSET 32
#define COUNTER_OFFSET (FLAGS_OFFSET+1)
#define ATTESTED_CRED_DATA_OFFSET (COUNTER_OFFSET+COUNTER_LEN)
#define CRED_ID_L_OFFSET (ATTESTED_CRED_DATA_OFFSET+AAGUID_LEN)
#define CREDENTIAL_ID_OFFSET (ATTESTED_CRED_DATA_OFFSET+AAGUID_LEN+CRED_ID_L_LEN)
#define ECDSA256 -7
#define ECDSA384 -35
#define ECDSA512 -36
#define SAFETYNET_ISSUED_TO "CN=attest.android.com"
struct _webauthn_config {
pthread_mutex_t insert_lock;
json_t * j_parameters;
};
static json_t * get_cert_from_file_path(const char * path) {
gnutls_x509_crt_t cert = NULL;
gnutls_datum_t cert_dat = {NULL, 0}, export_dat = {NULL, 0};
FILE * fl;
size_t len, issued_for_len = 128;
char * cert_content, issued_for[128] = {0};
json_t * j_return = NULL;
fl = fopen(path, "r");
if (fl != NULL) {
fseek(fl, 0, SEEK_END);
len = (size_t)ftell(fl);
if (len) {
cert_content = o_malloc(len);
if (cert_content != NULL) {
if (fseek(fl, 0, SEEK_SET) == -1) {
y_log_message(Y_LOG_LEVEL_ERROR, "get_cert_from_file_path - Error fseek");
j_return = json_pack("{si}", "result", G_ERROR);
} else if (fread(cert_content, 1, len, fl) != len) {
y_log_message(Y_LOG_LEVEL_ERROR, "get_cert_from_file_path - Error fread");
j_return = json_pack("{si}", "result", G_ERROR);
} else {
cert_dat.data = (unsigned char *)cert_content;
cert_dat.size = (unsigned int)len;
if (!gnutls_x509_crt_init(&cert)) {
if (gnutls_x509_crt_import(cert, &cert_dat, GNUTLS_X509_FMT_DER) >= 0 || gnutls_x509_crt_import(cert, &cert_dat, GNUTLS_X509_FMT_PEM) >= 0) {
if (!gnutls_x509_crt_get_dn(cert, issued_for, &issued_for_len)) {
if (gnutls_x509_crt_export2(cert, GNUTLS_X509_FMT_PEM, &export_dat) >= 0) {
j_return = json_pack("{sis{ss%ss%}}", "result", G_OK, "certificate", "dn", issued_for, issued_for_len, "x509", export_dat.data, (size_t)export_dat.size);
gnutls_free(export_dat.data);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_cert_from_file_path - Error gnutls_x509_crt_export2");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_cert_from_file_path - Error gnutls_x509_crt_get_dn");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_cert_from_file_path - Error gnutls_x509_crt_import");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_cert_from_file_path - Error gnutls_x509_crt_init");
j_return = json_pack("{si}", "result", G_ERROR);
}
gnutls_x509_crt_deinit(cert);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_cert_from_file_path - Error o_malloc cert_content");
j_return = json_pack("{si}", "result", G_ERROR_MEMORY);
}
o_free(cert_content);
}
fclose(fl);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_cert_from_file_path - Error fopen %s", path);
j_return = json_pack("{si}", "result", G_ERROR);
}
return j_return;
}
static json_t * is_scheme_parameters_valid(json_t * j_params) {
json_t * j_return, * j_error, * j_element = NULL, * j_cert;
size_t index = 0;
json_int_t pubkey;
char * message;
if (json_is_object(j_params)) {
j_error = json_array();
if (j_error != NULL) {
if (!json_is_boolean(json_object_get(j_params, "session-mandatory"))) {
json_array_append_new(j_error, json_string("session-mandatory is mandatory and must be a boolean"));
}
if (json_object_get(j_params, "seed") != NULL && !json_is_string(json_object_get(j_params, "seed"))) {
json_array_append_new(j_error, json_string("seed is optional and must be a string"));
}
if (json_integer_value(json_object_get(j_params, "challenge-length")) <= 0) {
json_array_append_new(j_error, json_string("challenge-length is mandatory and must be a positive integer"));
}
if (json_integer_value(json_object_get(j_params, "credential-expiration")) <= 0) {
json_array_append_new(j_error, json_string("credential-expiration is mandatory and must be a positive integer"));
}
if (json_integer_value(json_object_get(j_params, "credential-assertion")) <= 0) {
json_array_append_new(j_error, json_string("credential-assertion is mandatory and must be a positive integer"));
}
if (json_string_null_or_empty(json_object_get(j_params, "rp-origin"))) {
json_array_append_new(j_error, json_string("rp-origin is mandatory and must be a non empty string"));
}
if (!json_array_size(json_object_get(j_params, "pubKey-cred-params"))) {
json_array_append_new(j_error, json_string("pubKey-cred-params is mandatory and must be a non empty JSON array"));
} else {
json_array_foreach(json_object_get(j_params, "pubKey-cred-params"), index, j_element) {
pubkey = json_integer_value(j_element);
//if (pubkey != -7 && pubkey != -35 && pubkey != -36 && pubkey != -257 && pubkey != -258 && pubkey != -259) {
if (pubkey != ECDSA256 && pubkey != ECDSA384 && pubkey != ECDSA512) {
//json_array_append_new(j_error, json_string("pubKey-cred-params elements values available are -7, -35, -36 (ECDSA) or -257, -258, -259 (RSA)"));
json_array_append_new(j_error, json_string("pubKey-cred-params elements values available are -7, -35, -36 (ECDSA)"));
}
}
}
if (json_object_get(j_params, "ctsProfileMatch") != NULL && (!json_is_integer(json_object_get(j_params, "ctsProfileMatch")) || json_integer_value(json_object_get(j_params, "ctsProfileMatch")) < -1 || json_integer_value(json_object_get(j_params, "ctsProfileMatch")) > 1)) {
json_array_append_new(j_error, json_string("ctsProfileMatch is optional and must be an integer between -1 and 1"));
}
if (json_object_get(j_params, "basicIntegrity") != NULL && (!json_is_integer(json_object_get(j_params, "basicIntegrity")) || json_integer_value(json_object_get(j_params, "basicIntegrity")) < -1 || json_integer_value(json_object_get(j_params, "basicIntegrity")) > 1)) {
json_array_append_new(j_error, json_string("basicIntegrity is optional and must be an integer between -1 and 1"));
}
if (json_object_get(j_params, "google-root-ca-r2") != NULL && !json_is_string(json_object_get(j_params, "google-root-ca-r2"))) {
json_array_append_new(j_error, json_string("google-root-ca-r2 is optional and must be a string"));
} else if (!json_string_null_or_empty(json_object_get(j_params, "google-root-ca-r2"))) {
j_cert = get_cert_from_file_path(json_string_value(json_object_get(j_params, "google-root-ca-r2")));
if (check_result_value(j_cert, G_OK)) {
json_object_set(j_params, "google-root-ca-r2-content", json_object_get(j_cert, "certificate"));
} else {
message = msprintf("Error parsing google-root-ca-r2 certificate file %s", json_string_value(json_object_get(j_params, "google-root-ca-r2")));
json_array_append_new(j_error, json_string(message));
o_free(message);
}
json_decref(j_cert);
}
if (json_object_get(j_params, "apple-root-ca") != NULL && !json_is_string(json_object_get(j_params, "apple-root-ca"))) {
json_array_append_new(j_error, json_string("apple-root-ca is optional and must be a string"));
} else if (!json_string_null_or_empty(json_object_get(j_params, "apple-root-ca"))) {
j_cert = get_cert_from_file_path(json_string_value(json_object_get(j_params, "apple-root-ca")));
if (check_result_value(j_cert, G_OK)) {
json_object_set(j_params, "apple-root-ca-content", json_object_get(j_cert, "certificate"));
} else {
message = msprintf("Error parsing apple-root-ca certificate file %s", json_string_value(json_object_get(j_params, "apple-root-ca")));
json_array_append_new(j_error, json_string(message));
o_free(message);
}
json_decref(j_cert);
}
if (json_object_get(j_params, "root-ca-list") != NULL) {
if (!json_is_array(json_object_get(j_params, "root-ca-list"))) {
json_array_append_new(j_error, json_string("root-ca-list is optional and must be an array of strings"));
} else {
json_object_set_new(j_params, "root-ca-array", json_array());
json_array_foreach(json_object_get(j_params, "root-ca-list"), index, j_element) {
if (json_string_null_or_empty(j_element)) {
json_array_append_new(j_error, json_string("root-ca-list is optional and must be an array of strings"));
} else {
j_cert = get_cert_from_file_path(json_string_value(j_element));
if (check_result_value(j_cert, G_OK)) {
json_array_append(json_object_get(j_params, "root-ca-array"), json_object_get(j_cert, "certificate"));
} else {
message = msprintf("Error parsing certificate file %s", json_string_value(j_element));
json_array_append_new(j_error, json_string(message));
o_free(message);
}
json_decref(j_cert);
}
}
}
}
if (json_object_get(j_params, "force-fmt-none") != NULL && !json_is_boolean(json_object_get(j_params, "force-fmt-none"))) {
json_array_append_new(j_error, json_string("allow-fmt-none is optional and must be a boolean"));
}
if (json_object_get(j_params, "fmt") != NULL && (!json_is_object(json_object_get(j_params, "fmt")) || (json_object_get(json_object_get(j_params, "fmt"), "packed") != json_true() && json_object_get(json_object_get(j_params, "fmt"), "tpm") != json_true() && json_object_get(json_object_get(j_params, "fmt"), "android-key") != json_true() && json_object_get(json_object_get(j_params, "fmt"), "android-safetynet") != json_true() && json_object_get(json_object_get(j_params, "fmt"), "fido-u2f") != json_true() && json_object_get(json_object_get(j_params, "fmt"), "none") != json_true()))) {
json_array_append_new(j_error, json_string("fmt must be a JSON object filled with supported formats: 'packed' 'tpm', 'android-key', 'android-safetynet', 'fido-u2f', 'none'"));
}
if (json_array_size(j_error)) {
j_return = json_pack("{sisO}", "result", G_ERROR_PARAM, "error", j_error);
} else {
j_return = json_pack("{si}", "result", G_OK);
}
json_decref(j_error);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_scheme_parameters_valid - Error allocating resources for j_error");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
j_return = json_pack("{sis[s]}", "result", G_ERROR_PARAM, "error", "parameters must be a JSON object");
}
return j_return;
}
/**
* Get the user_id associated with the username in the table G_TABLE_WEBAUTHN_USER
* If user_id doesn't exist, create one, stores it, and return the new user_id
*/
static json_t * get_user_id_from_username(struct config_module * config, struct _webauthn_config * webauthn_config, const char * username, int create) {
json_t * j_query, * j_result, * j_return;
int res;
char * username_escaped, * username_clause;
unsigned char new_user_id[USER_ID_LENGTH] = {0}, new_user_id_b64[USER_ID_LENGTH*2] = {0};
size_t new_user_id_b64_len;
if (!pthread_mutex_lock(&webauthn_config->insert_lock)) {
username_escaped = h_escape_string_with_quotes(config->conn, username);
username_clause = msprintf(" = UPPER(%s)", username_escaped);
j_query = json_pack("{sss[s]s{s{ssss}sO}}",
"table",
G_TABLE_WEBAUTHN_USER,
"columns",
"gswu_user_id AS user_id",
"where",
"UPPER(gswu_username)",
"operator",
"raw",
"value",
username_clause,
"gswu_mod_name",
json_object_get(webauthn_config->j_parameters, "mod_name"));
o_free(username_clause);
o_free(username_escaped);
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
if (json_array_size(j_result)) {
j_return = json_pack("{siss}", "result", G_OK, "user_id", json_string_value(json_object_get(json_array_get(j_result, 0), "user_id")));
} else if (create) {
// Generates a new user_id, and stores it in the database
if (!gnutls_rnd(GNUTLS_RND_KEY, new_user_id, USER_ID_LENGTH)) {
if (o_base64_encode(new_user_id, USER_ID_LENGTH, new_user_id_b64, &new_user_id_b64_len)) {
j_query = json_pack("{sss{sOssss}}",
"table",
G_TABLE_WEBAUTHN_USER,
"values",
"gswu_mod_name",
json_object_get(webauthn_config->j_parameters, "mod_name"),
"gswu_username",
username,
"gswu_user_id",
new_user_id_b64);
res = h_insert(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
j_return = json_pack("{siss}", "result", G_OK, "user_id", new_user_id_b64);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_id_from_username - Error executing j_query insert");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_id_from_username - Error o_base64_encode");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_id_from_username - Error gnutls_rnd");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
j_return = json_pack("{si}", "result", G_ERROR_NOT_FOUND);
}
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_id_from_username - Error executing j_query select");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
pthread_mutex_unlock(&webauthn_config->insert_lock);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_id_from_username - Error pthread_mutex_lock");
j_return = json_pack("{si}", "result", G_ERROR);
}
return j_return;
}
static json_t * get_credential_list(struct config_module * config, json_t * j_params, const char * username, int restrict_to_registered) {
json_t * j_query, * j_result, * j_return, * j_element = NULL;
int res;
char * username_escaped, * mod_name_escaped, * username_clause;
size_t index = 0;
username_escaped = h_escape_string_with_quotes(config->conn, username);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(j_params, "mod_name")));
username_clause = msprintf(" = (SELECT gswu_id FROM "G_TABLE_WEBAUTHN_USER" WHERE UPPER(gswu_username) = UPPER(%s) AND gswu_mod_name = %s)", username_escaped, mod_name_escaped);
j_query = json_pack("{sss[ssss]s{s{ssss}}}",
"table",
G_TABLE_WEBAUTHN_CREDENTIAL,
"columns",
"gswc_credential_id AS credential_id",
"gswc_name AS name",
SWITCH_DB_TYPE(config->conn->type, "UNIX_TIMESTAMP(gswc_created_at) AS created_at", "strftime('%s', gswc_created_at) AS created_at", "EXTRACT(EPOCH FROM gswc_created_at)::integer AS created_at"),
"gswc_status",
"where",
"gswu_id",
"operator",
"raw",
"value",
username_clause);
o_free(username_clause);
o_free(username_escaped);
o_free(mod_name_escaped);
if (restrict_to_registered) {
json_object_set_new(json_object_get(j_query, "where"), "gswc_status", json_integer(1));
} else {
json_object_set_new(json_object_get(j_query, "where"), "gswc_status", json_pack("{ssss}", "operator", "raw", "value", " IN (1,3)"));
}
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
if (json_array_size(j_result)) {
j_return = json_pack("{sis[]}", "result", G_OK, "credential");
if (j_return != NULL) {
json_array_foreach(j_result, index, j_element) {
switch (json_integer_value(json_object_get(j_element, "gswc_status"))) {
case 1:
json_object_set_new(j_element, "status", json_string("registered"));
break;
case 3:
json_object_set_new(j_element, "status", json_string("disabled"));
break;
default:
break;
}
json_object_del(j_element, "gswc_status");
json_array_append(json_object_get(j_return, "credential"), j_element);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_credential_list - Error json_pack");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
j_return = json_pack("{si}", "result", G_ERROR_NOT_FOUND);
}
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_credential_list - Error executing j_query");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
return j_return;
}
static json_t * generate_new_credential(struct config_module * config, struct _webauthn_config * webauthn_config, const char * username) {
json_t * j_query, * j_return;
char * username_escaped, * mod_name_escaped, * username_clause, * challenge_hash;
int res;
size_t challenge_b64_len, challenge_len = (size_t)json_integer_value(json_object_get(webauthn_config->j_parameters, "challenge-length"));
unsigned char challenge_b64[challenge_len*2], challenge[challenge_len+1];
char session[SESSION_LENGTH+1] = {0}, * session_hash;
if (!pthread_mutex_lock(&webauthn_config->insert_lock)) {
if (!gnutls_rnd(GNUTLS_RND_NONCE, challenge, challenge_len)) {
if (o_base64_encode(challenge, challenge_len, challenge_b64, &challenge_b64_len)) {
challenge_b64[challenge_b64_len] = '\0';
if ((challenge_hash = generate_hash(config->hash_algorithm, (const char *)challenge_b64)) != NULL) {
if (rand_string(session, SESSION_LENGTH) != NULL) {
if ((session_hash = generate_hash(config->hash_algorithm, session)) != NULL) {
username_escaped = h_escape_string_with_quotes(config->conn, username);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(webauthn_config->j_parameters, "mod_name")));
username_clause = msprintf(" (SELECT gswu_id FROM "G_TABLE_WEBAUTHN_USER" WHERE UPPER(gswu_username) = UPPER(%s) AND gswu_mod_name = %s)", username_escaped, mod_name_escaped);
// Disable all credential with status 0 (new) of the same user
j_query = json_pack("{sss{si}s{s{ssss+}si}}",
"table",
G_TABLE_WEBAUTHN_CREDENTIAL,
"set",
"gswc_status",
2,
"where",
"gswu_id",
"operator",
"raw",
"value",
" =",
username_clause,
"gswc_status",
0);
res = h_update(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
// Insert new credential
j_query = json_pack("{sss{s{ss}sssssi}}",
"table",
G_TABLE_WEBAUTHN_CREDENTIAL,
"values",
"gswu_id",
"raw",
username_clause,
"gswc_session_hash",
session_hash,
"gswc_challenge_hash",
challenge_hash,
"gswc_status",
0);
res = h_insert(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
j_return = json_pack("{sis{ssss}}", "result", G_OK, "credential", "session", session, "challenge", challenge_b64);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_credential - Error executing j_query insert");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_credential - Error executing j_query update");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
o_free(username_clause);
o_free(username_escaped);
o_free(mod_name_escaped);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_credential - Error generate_hash session");
j_return = json_pack("{si}", "result", G_ERROR);
}
o_free(session_hash);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_credential - Error rand_string");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_credential - Error generate_hash challenge");
j_return = json_pack("{si}", "result", G_ERROR);
}
o_free(challenge_hash);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_credential - Error o_base64_encode challenge");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_credential - Error gnutls_rnd");
j_return = json_pack("{si}", "result", G_ERROR);
}
pthread_mutex_unlock(&webauthn_config->insert_lock);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_credential - Error pthread_mutex_lock");
j_return = json_pack("{si}", "result", G_ERROR);
}
return j_return;
}
static json_t * generate_new_assertion(struct config_module * config, struct _webauthn_config * webauthn_config, const char * username, int mock) {
json_t * j_query, * j_return;
char * username_escaped, * username_clause, * mod_name_escaped, * challenge_hash;
int res;
size_t challenge_b64_len, challenge_len = (size_t)json_integer_value(json_object_get(webauthn_config->j_parameters, "challenge-length"));
unsigned char challenge_b64[challenge_len*2], challenge[challenge_len+1];
char session[SESSION_LENGTH+1] = {0}, * session_hash;
if (!pthread_mutex_lock(&webauthn_config->insert_lock)) {
if (!gnutls_rnd(GNUTLS_RND_NONCE, challenge, challenge_len)) {
if (o_base64_encode(challenge, challenge_len, challenge_b64, &challenge_b64_len)) {
challenge_b64[challenge_b64_len] = '\0';
if ((challenge_hash = generate_hash(config->hash_algorithm, (const char *)challenge_b64)) != NULL) {
if (rand_string(session, SESSION_LENGTH) != NULL) {
if ((session_hash = generate_hash(config->hash_algorithm, session)) != NULL) {
if (mock < 2) {
username_escaped = h_escape_string_with_quotes(config->conn, username);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(webauthn_config->j_parameters, "mod_name")));
username_clause = msprintf(" (SELECT gswu_id FROM "G_TABLE_WEBAUTHN_USER" WHERE UPPER(gswu_username) = UPPER(%s) AND gswu_mod_name = %s)", username_escaped, mod_name_escaped);
// Disable all assertions with status 0 (new) of the same user
j_query = json_pack("{sss{si}s{s{ssss+}si}}",
"table",
G_TABLE_WEBAUTHN_ASSERTION,
"set",
"gswa_status",
3,
"where",
"gswu_id",
"operator",
"raw",
"value",
" =",
username_clause,
"gswa_status",
0);
res = h_update(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
// Insert new assertion
j_query = json_pack("{sss{s{ss}sssssisi}}",
"table",
G_TABLE_WEBAUTHN_ASSERTION,
"values",
"gswu_id",
"raw",
username_clause,
"gswa_session_hash",
session_hash,
"gswa_challenge_hash",
challenge_hash,
"gswa_status",
0,
"gswa_mock",
mock);
res = h_insert(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
j_return = json_pack("{sis{ssss}}", "result", G_OK, "assertion", "session", session, "challenge", challenge_b64);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_assertion - Error executing j_query insert");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_assertion - Error executing j_query update");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
o_free(username_clause);
o_free(mod_name_escaped);
o_free(username_escaped);
} else {
j_return = json_pack("{sis{ssss}}", "result", G_OK, "assertion", "session", session, "challenge", challenge_b64);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_assertion - Error generate_hash session");
j_return = json_pack("{si}", "result", G_ERROR);
}
o_free(session_hash);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_assertion - Error rand_string");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_assertion - Error generate_hash challenge");
j_return = json_pack("{si}", "result", G_ERROR);
}
o_free(challenge_hash);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_assertion - Error o_base64_encode challenge");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_assertion - Error gnutls_rnd");
j_return = json_pack("{si}", "result", G_ERROR);
}
pthread_mutex_unlock(&webauthn_config->insert_lock);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "generate_new_assertion - Error pthread_mutex_lock");
j_return = json_pack("{si}", "result", G_ERROR);
}
return j_return;
}
static json_t * get_credential_from_session(struct config_module * config, json_t * j_params, const char * username, const char * session) {
json_t * j_query, * j_result, * j_return;
char * username_escaped, * mod_name_escaped, * username_clause, * expiration_clause;
char * session_hash;
int res;
time_t now;
if (!o_strnullempty(session)) {
if ((session_hash = generate_hash(config->hash_algorithm, session)) != NULL) {
time(&now);
username_escaped = h_escape_string_with_quotes(config->conn, username);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(j_params, "mod_name")));
username_clause = msprintf(" = (SELECT gswu_id FROM "G_TABLE_WEBAUTHN_USER" WHERE UPPER(gswu_username) = UPPER(%s) AND gswu_mod_name = %s)", username_escaped, mod_name_escaped);
if (config->conn->type==HOEL_DB_TYPE_MARIADB) {
expiration_clause = msprintf("> FROM_UNIXTIME(%ld)", (now - (time_t)json_integer_value(json_object_get(j_params, "credential-expiration"))));
} else if (config->conn->type==HOEL_DB_TYPE_PGSQL) {
expiration_clause = msprintf("> TO_TIMESTAMP(%ld)", (now - (time_t)json_integer_value(json_object_get(j_params, "credential-expiration"))));
} else { // HOEL_DB_TYPE_SQLITE
expiration_clause = msprintf("> %ld", (now - (time_t)json_integer_value(json_object_get(j_params, "credential-expiration"))));
}
j_query = json_pack("{sss[ssssss]s{sss{ssss}sis{ssss}}}",
"table",
G_TABLE_WEBAUTHN_CREDENTIAL,
"columns",
"gswc_id",
"gswu_id",
"gswc_session_hash AS session_hash",
"gswc_challenge_hash AS challenge_hash",
"gswc_credential_id AS credential_id",
"gswc_public_key AS public_key",
"where",
"gswc_session_hash",
session_hash,
"gswu_id",
"operator",
"raw",
"value",
username_clause,
"gswc_status",
0,
"gswc_created_at",
"operator",
"raw",
"value",
expiration_clause);
o_free(username_clause);
o_free(username_escaped);
o_free(mod_name_escaped);
o_free(expiration_clause);
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
if (json_array_size(j_result)) {
j_return = json_pack("{sisO}", "result", G_OK, "credential", json_array_get(j_result, 0));
} else {
j_return = json_pack("{si}", "result", G_ERROR_NOT_FOUND);
}
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_credential_from_session - Error executing j_query");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_credential_from_session - Error generate_hash");
j_return = json_pack("{si}", "result", G_ERROR);
}
o_free(session_hash);
} else {
j_return = json_pack("{si}", "result", G_ERROR_PARAM);
}
return j_return;
}
static json_t * get_credential(struct config_module * config, json_t * j_params, const char * username, const char * credential_id) {
json_t * j_query, * j_result, * j_return;
char * username_escaped, * mod_name_escaped, * username_clause;
int res;
username_escaped = h_escape_string_with_quotes(config->conn, username);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(j_params, "mod_name")));
username_clause = msprintf(" = (SELECT gswu_id FROM "G_TABLE_WEBAUTHN_USER" WHERE UPPER(gswu_username) = UPPER(%s) AND gswu_mod_name = %s)", username_escaped, mod_name_escaped);
j_query = json_pack("{sss[sss]s{sss{ssss}s{ssss}}}",
"table",
G_TABLE_WEBAUTHN_CREDENTIAL,
"columns",
"gswc_id",
"gswc_public_key AS public_key",
"gswc_counter AS counter",
"where",
"gswc_credential_id",
credential_id,
"gswu_id",
"operator",
"raw",
"value",
username_clause,
"gswc_status",
"operator",
"raw",
"value",
" IN (1,3)");
o_free(username_clause);
o_free(username_escaped);
o_free(mod_name_escaped);
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
if (json_array_size(j_result)) {
j_return = json_pack("{sisO}", "result", G_OK, "credential", json_array_get(j_result, 0));
} else {
j_return = json_pack("{si}", "result", G_ERROR_NOT_FOUND);
}
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_credential - Error executing j_query");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
return j_return;
}
static int update_credential(struct config_module * config, json_t * j_params, const char * username, const char * credential_id, int status) {
json_t * j_query;
char * username_escaped, * mod_name_escaped, * username_clause;
int res, ret;
username_escaped = h_escape_string_with_quotes(config->conn, username);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(j_params, "mod_name")));
username_clause = msprintf(" = (SELECT gswu_id FROM "G_TABLE_WEBAUTHN_USER" WHERE UPPER(gswu_username) = UPPER(%s) AND gswu_mod_name = %s)", username_escaped, mod_name_escaped);
j_query = json_pack("{sss{si}s{sss{ssss}}}",
"table",
G_TABLE_WEBAUTHN_CREDENTIAL,
"set",
"gswc_status",
status,
"where",
"gswc_credential_id",
credential_id,
"gswu_id",
"operator",
"raw",
"value",
username_clause);
o_free(username_clause);
o_free(username_escaped);
o_free(mod_name_escaped);
res = h_update(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
ret = G_OK;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_credential - Error executing j_query");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
ret = G_ERROR_DB;
}
return ret;
}
static int update_credential_name(struct config_module * config, json_t * j_params, const char * username, const char * credential_id, const char * name) {
json_t * j_query;
char * username_escaped, * mod_name_escaped, * username_clause;
int res, ret;
username_escaped = h_escape_string_with_quotes(config->conn, username);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(j_params, "mod_name")));
username_clause = msprintf(" = (SELECT gswu_id FROM "G_TABLE_WEBAUTHN_USER" WHERE UPPER(gswu_username) = UPPER(%s) AND gswu_mod_name = %s)", username_escaped, mod_name_escaped);
j_query = json_pack("{sss{ss}s{sss{ssss}}}",
"table",
G_TABLE_WEBAUTHN_CREDENTIAL,
"set",
"gswc_name",
name,
"where",
"gswc_credential_id",
credential_id,
"gswu_id",
"operator",
"raw",
"value",
username_clause);
o_free(username_clause);
o_free(username_escaped);
o_free(mod_name_escaped);
res = h_update(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
ret = G_OK;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_credential - Error executing j_query");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
ret = G_ERROR_DB;
}
return ret;
}
static json_t * get_assertion_from_session(struct config_module * config, json_t * j_params, const char * username, const char * session, int mock) {
json_t * j_query, * j_result, * j_return;
char * username_escaped, * mod_name_escaped, * username_clause, * expiration_clause;
char * session_hash;
int res;
time_t now;
if (!o_strnullempty(session)) {
if ((session_hash = generate_hash(config->hash_algorithm, session)) != NULL) {
time(&now);
username_escaped = h_escape_string_with_quotes(config->conn, username);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(j_params, "mod_name")));
username_clause = msprintf(" = (SELECT gswu_id FROM "G_TABLE_WEBAUTHN_USER" WHERE UPPER(gswu_username) = UPPER(%s) AND gswu_mod_name = %s)", username_escaped, mod_name_escaped);
if (config->conn->type==HOEL_DB_TYPE_MARIADB) {
expiration_clause = msprintf("> FROM_UNIXTIME(%ld)", (now - (time_t)json_integer_value(json_object_get(j_params, "credential-assertion"))));
} else if (config->conn->type==HOEL_DB_TYPE_PGSQL) {
expiration_clause = msprintf("> TO_TIMESTAMP(%ld)", (now - (time_t)json_integer_value(json_object_get(j_params, "credential-assertion"))));
} else { // HOEL_DB_TYPE_SQLITE
expiration_clause = msprintf("> %ld", (now - (time_t)json_integer_value(json_object_get(j_params, "credential-assertion"))));
}
j_query = json_pack("{sss[ssss]s{sss{ssss}sis{ssss}si}}",
"table",
G_TABLE_WEBAUTHN_ASSERTION,
"columns",
"gswa_id",
"gswu_id",
"gswa_session_hash AS session_hash",
"gswa_challenge_hash AS challenge_hash",
"where",
"gswa_session_hash",
session_hash,
"gswu_id",
"operator",
"raw",
"value",
username_clause,
"gswa_status",
0,
"gswa_issued_at",
"operator",
"raw",
"value",
expiration_clause,
"gswa_mock",
mock);
o_free(username_clause);
o_free(username_escaped);
o_free(mod_name_escaped);
o_free(expiration_clause);
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
if (json_array_size(j_result)) {
j_return = json_pack("{sisO}", "result", G_OK, "assertion", json_array_get(j_result, 0));
} else {
j_return = json_pack("{si}", "result", G_ERROR_NOT_FOUND);
}
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_assertion_from_session - Error executing j_query");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_assertion_from_session - Error generate_hash");
j_return = json_pack("{si}", "result", G_ERROR);
}
o_free(session_hash);
} else {
j_return = json_pack("{si}", "result", G_ERROR_PARAM);
}
return j_return;
}
static int check_certificate(struct config_module * config, json_t * j_params, const char * credential_id, json_int_t gswu_id) {
json_t * j_query, * j_result;
int res, ret;
char * credential_id_escaped, * mod_name_escaped, * where_clause;
credential_id_escaped = h_escape_string_with_quotes(config->conn, credential_id);
mod_name_escaped = h_escape_string_with_quotes(config->conn, json_string_value(json_object_get(j_params, "mod_name")));
where_clause = msprintf(" IN (SELECT gswu_id FROM " G_TABLE_WEBAUTHN_CREDENTIAL " WHERE gswc_credential_id=%s AND gswc_status=1 AND gswu_id IN (SELECT gswu_id FROM " G_TABLE_WEBAUTHN_USER " WHERE gswu_mod_name=%s))", credential_id_escaped, mod_name_escaped);
j_query = json_pack("{sss[s]s{s{ssss}si}}",
"table",
G_TABLE_WEBAUTHN_CREDENTIAL,
"columns",
"gswu_id",
"where",
"gswu_id",
"operator",
"raw",
"value",
where_clause,
"gswc_status",
1);
o_free(where_clause);
o_free(mod_name_escaped);
o_free(credential_id_escaped);
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
if (json_array_size(j_result)) {
if (json_integer_value(json_object_get(json_array_get(j_result, 0), "gswu_id")) == gswu_id) {
ret = G_OK;
} else {
ret = G_ERROR_UNAUTHORIZED;
}
} else {
ret = G_ERROR_NOT_FOUND;
}
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "check_credential_id - Error executing j_query");
config->glewlwyd_module_callback_metrics_increment_counter(config, GLWD_METRICS_DATABSE_ERROR, 1, NULL);
ret = G_ERROR_DB;
}
return ret;
}
static int validate_apple_certificate_chain(json_t * j_params, gnutls_x509_crt_t cert_leaf, cbor_item_t * x5c_array) {
int ret = G_OK;
size_t i, x5c_array_size = cbor_array_size(x5c_array);
gnutls_x509_crt_t cert_x509[x5c_array_size], root_x509 = NULL;
gnutls_x509_trust_list_t tlist = NULL;
gnutls_datum_t cert_dat;
cbor_item_t * cbor_cert = NULL;
unsigned int result;
cert_x509[0] = cert_leaf;
for (i=1; i<x5c_array_size; i++) {
cbor_cert = cbor_array_get(x5c_array, i);
cert_dat.data = cbor_bytestring_handle(cbor_cert);
cert_dat.size = (unsigned int)cbor_bytestring_length(cbor_cert);
if (gnutls_x509_crt_init(&cert_x509[i]) < 0 || gnutls_x509_crt_import(cert_x509[i], &cert_dat, GNUTLS_X509_FMT_DER) < 0) {
y_log_message(Y_LOG_LEVEL_ERROR, "validate_apple_certificate_chain - Error import chain cert at index %zu", i);
ret = G_ERROR;
}
cbor_decref(&cbor_cert);
}
if (ret == G_OK) {
cert_dat.data = (unsigned char *)json_string_value(json_object_get(json_object_get(j_params, "apple-root-ca-content"), "x509"));
cert_dat.size = (unsigned int)json_string_length(json_object_get(json_object_get(j_params, "apple-root-ca-content"), "x509"));
if (gnutls_x509_crt_init(&root_x509) || gnutls_x509_crt_import(root_x509, &cert_dat, GNUTLS_X509_FMT_PEM)) {
y_log_message(Y_LOG_LEVEL_ERROR, "validate_apple_certificate_chain - Error import root cert");
ret = G_ERROR;
}
}
if (ret == G_OK) {
if (!gnutls_x509_trust_list_init(&tlist, 0)) {
if (gnutls_x509_trust_list_add_cas(tlist, &root_x509, 1, 0) >= 0) {
if (gnutls_x509_trust_list_verify_crt(tlist, cert_x509, (unsigned int)x5c_array_size, 0, &result, NULL) >= 0) {
if (result) {
y_log_message(Y_LOG_LEVEL_DEBUG, "validate_apple_certificate_chain - certificate chain invalid");
ret = G_ERROR_UNAUTHORIZED;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "validate_apple_certificate_chain - Error gnutls_x509_trust_list_verify_crt");
ret = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "validate_certificate_from_root - Error gnutls_x509_trust_list_add_cas");
ret = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "validate_certificate_from_root - Error gnutls_x509_trust_list_init");
ret = G_ERROR;
}
}
gnutls_x509_crt_deinit(root_x509);
for (i=1; i<x5c_array_size; i++) {
gnutls_x509_crt_deinit(cert_x509[i]);
}
gnutls_x509_trust_list_deinit(tlist, 0);
return ret;
}
static int validate_certificate_from_root(json_t * j_params, gnutls_x509_crt_t cert_leaf, cbor_item_t * x5c_array) {
int ret = G_ERROR_NOT_FOUND, res;
unsigned int result;
gnutls_datum_t cert_dat = {NULL, 0}, issuer_dat = {NULL, 0};
gnutls_x509_trust_list_t tlist = NULL;
gnutls_x509_crt_t cert_x509[cbor_array_size(x5c_array)], root_x509 = NULL;
json_t * j_cert = NULL;
cbor_item_t * cbor_cert = NULL;
size_t index = 0, i = 0, x5c_array_size = cbor_array_size(x5c_array);
char * issuer;
for (i=0; i<x5c_array_size+1; i++) {
cert_x509[i] = NULL;
}