forked from babelouest/glewlwyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertificate.c
1744 lines (1661 loc) · 82.9 KB
/
certificate.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.
*
* TLS client certificate authentication scheme module
*
* Copyright 2019-2020 Nicolas Mora <mail@babelouest.org>
*
* 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 <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gnutls/abstract.h>
#include <gnutls/pkcs12.h>
#include <string.h>
#include <jansson.h>
#include <yder.h>
#include <orcania.h>
#include "../glewlwyd-common.h"
#define GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE "gs_user_certificate"
#define G_CERT_SOURCE_TLS 0x01
#define G_CERT_SOURCE_HEADER 0x10
int user_auth_scheme_module_validate(struct config_module * config, const struct _u_request * http_request, const char * username, json_t * j_scheme_data, void * cls);
static int add_user_certificate_scheme_storage(struct config_module * config, json_t * j_parameters, const char * x509_data, const char * username, const char * user_agent);
static json_t * parse_certificate(const char * x509_data, int der_format);
static json_t * get_user_certificate_from_id_scheme_storage(struct config_module * config, json_t * j_parameters, const char * username, const char * cert_id);
/**
*
* How-To generate a cert chain with cient certificates
*
* OpenSSL
* =======
*
* Root cert/key
* openssl genrsa -out root.key 4096
* openssl req -x509 -new -nodes -key root.key -sha256 -days 1024 -out root.crt
*
* Client cert/key/pfx
* openssl genrsa -out client.key 4096
* openssl req -new -key client.key -out client.csr
* openssl x509 -req -in client.csr -CA root.crt -CAkey root.key -CAcreateserial -out client.crt -days 500 -sha256
* openssl pkcs12 -export -out client.pfx -inkey client.key -in client.crt
*
* GnuTLS
* ======
*
* Root cert/key
* certtool --generate-privkey --outfile root.key --bits=4096
* certtool --generate-request --load-privkey root.key --outfile root.csr
* certtool --generate-self-signed --load-privkey root.key --outfile root.crt
*
* Client cert/key/pfx
* certtool --generate-privkey --outfile client.key --bits=4096
* certtool --generate-request --load-privkey client.key --outfile client.csr
* certtool --generate-certificate --load-request client.csr --load-ca-certificate root.crt --load-ca-privkey root.key --outfile client.crt
* certtool --load-certificate client.crt --load-privkey client.key --to-p12 --outder --outfile client.pfx
*
*/
struct _cert_chain_element {
gnutls_x509_crt_t cert;
char * dn;
struct _cert_chain_element * issuer_cert;
char * issuer_dn;
};
struct _cert_param {
json_t * j_parameters;
size_t cert_array_len;
struct _cert_chain_element ** cert_array;
ushort cert_source;
pthread_mutex_t cert_request_lock;
};
static int get_certificate_id(gnutls_x509_crt_t cert, unsigned char * cert_id, size_t * cert_id_len) {
int ret;
unsigned char cert_digest[64];
size_t cert_digest_len = 64;
gnutls_datum_t dat;
dat.data = NULL;
if (gnutls_x509_crt_export2(cert, GNUTLS_X509_FMT_DER, &dat) >= 0) {
if (gnutls_fingerprint(GNUTLS_DIG_SHA256, &dat, cert_digest, &cert_digest_len) == GNUTLS_E_SUCCESS) {
if (o_base64_encode(cert_digest, cert_digest_len, cert_id, cert_id_len)) {
cert_id[*cert_id_len] = '\0';
ret = G_OK;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_certificate_id - Error o_base64_encode");
ret = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_certificate_id - Error gnutls_fingerprint");
ret = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_certificate_id - Error gnutls_x509_crt_export2");
ret = G_ERROR;
}
gnutls_free(dat.data);
return ret;
}
static json_t * parse_certificate(const char * x509_data, int der_format) {
json_t * j_return;
gnutls_x509_crt_t cert = NULL;
gnutls_datum_t cert_dat;
char * dn = NULL, * issuer_dn = NULL;
size_t key_id_enc_len = 256, dn_len = 0, issuer_dn_len = 0;
time_t expires_at = 0, issued_at = 0;
int ret;
unsigned char * der_dec = NULL, key_id_enc[257] = {0};
size_t der_dec_len = 0;
if (o_strlen(x509_data)) {
if (!gnutls_x509_crt_init(&cert)) {
if (der_format) {
cert_dat.data = NULL;
cert_dat.size = 0;
if (o_base64_decode((const unsigned char *)x509_data, o_strlen(x509_data), NULL, &der_dec_len)) {
if ((der_dec = o_malloc(der_dec_len+4)) != NULL) {
if (o_base64_decode((const unsigned char *)x509_data, o_strlen(x509_data), der_dec, &der_dec_len)) {
cert_dat.data = der_dec;
cert_dat.size = der_dec_len;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error o_base64_decode (2)");
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error allocating resources for der_dec");
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error o_base64_decode (1)");
}
} else {
cert_dat.data = (unsigned char *)x509_data;
cert_dat.size = o_strlen(x509_data);
}
if (gnutls_x509_crt_import(cert, &cert_dat, der_format?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM) >= 0) {
ret = gnutls_x509_crt_get_issuer_dn(cert, NULL, &issuer_dn_len);
if (gnutls_x509_crt_get_dn(cert, NULL, &dn_len) == GNUTLS_E_SHORT_MEMORY_BUFFER && (ret == GNUTLS_E_SHORT_MEMORY_BUFFER || ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)) {
if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
if ((issuer_dn = o_malloc(issuer_dn_len +1)) != NULL) {
if (gnutls_x509_crt_get_issuer_dn(cert, issuer_dn, &issuer_dn_len) < 0) {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error gnutls_x509_crt_get_issuer_dn");
o_free(issuer_dn);
issuer_dn = NULL;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error o_malloc issuer_dn");
}
}
if ((dn = o_malloc(dn_len +1)) != NULL) {
if (gnutls_x509_crt_get_dn(cert, dn, &dn_len) >= 0) {
dn[dn_len] = '\0';
if (get_certificate_id(cert, key_id_enc, &key_id_enc_len) == G_OK && (expires_at = gnutls_x509_crt_get_expiration_time(cert)) != (time_t)-1 && (issued_at = gnutls_x509_crt_get_activation_time(cert)) != (time_t)-1) {
j_return = json_pack("{sis{sssisisssssissss}}",
"result",
G_OK,
"certificate",
"certificate_id",
key_id_enc,
"activation",
issued_at,
"expiration",
expires_at,
"certificate_dn",
dn,
"certificate_issuer_dn",
issuer_dn!=NULL?issuer_dn:"",
"last_used",
0,
"last_user_agent",
"",
"x509",
x509_data);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error gnutls_x509_crt_get_key_id or gnutls_x509_crt_get_expiration_time or gnutls_x509_crt_get_activation_time");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error gnutls_x509_crt_get_dn (2)");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error o_malloc dn");
j_return = json_pack("{si}", "result", G_ERROR);
}
o_free(dn);
o_free(issuer_dn);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error gnutls_x509_crt_get_dn (1)");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_DEBUG, "parse_certificate - Error gnutls_x509_crt_import");
j_return = json_pack("{si}", "result", G_ERROR_PARAM);
}
gnutls_x509_crt_deinit(cert);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_certificate - Error gnutls_x509_crt_init");
j_return = json_pack("{si}", "result", G_ERROR);
}
o_free(der_dec);
} else {
j_return = json_pack("{si}", "result", G_ERROR_PARAM);
}
return j_return;
}
static int update_user_certificate_enabled_scheme_storage(struct config_module * config, json_t * j_parameters, const char * username, const char * cert_id, int enabled) {
json_t * j_query;
int res, ret;
j_query = json_pack("{sss{si}s{sOssss}}",
"table",
GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE,
"set",
"gsuc_enabled",
enabled,
"where",
"gsuc_mod_name",
json_object_get(j_parameters, "mod_name"),
"gsuc_username",
username,
"gsuc_x509_certificate_id",
cert_id);
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_DEBUG, "toggle_enabled_user_certificate_scheme_storage - Error executing j_query");
ret = G_ERROR_DB;
}
return ret;
}
static int update_user_certificate_last_used_scheme_storage(struct config_module * config, json_t * j_parameters, const char * username, const char * cert_id, const char * user_agent) {
json_t * j_query;
int res, ret;
char * last_used_clause;
if (config->conn->type==HOEL_DB_TYPE_MARIADB) {
last_used_clause = msprintf("FROM_UNIXTIME(%u)", time(NULL));
} else if (config->conn->type==HOEL_DB_TYPE_PGSQL) {
last_used_clause = msprintf("TO_TIMESTAMP(%u)", time(NULL));
} else { // HOEL_DB_TYPE_SQLITE
last_used_clause = msprintf("%u", time(NULL));
}
j_query = json_pack("{sss{s{ss}ss}s{sOssss}}",
"table",
GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE,
"set",
"gsuc_last_used",
"raw",
last_used_clause,
"gsuc_last_user_agent",
user_agent!=NULL?user_agent:"",
"where",
"gsuc_mod_name",
json_object_get(j_parameters, "mod_name"),
"gsuc_username",
username,
"gsuc_x509_certificate_id",
cert_id);
o_free(last_used_clause);
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_DEBUG, "toggle_enabled_user_certificate_scheme_storage - Error executing j_query");
ret = G_ERROR_DB;
}
return ret;
}
static int delete_user_certificate_scheme_storage(struct config_module * config, json_t * j_parameters, const char * username, const char * cert_id) {
json_t * j_query;
int res, ret;
j_query = json_pack("{sss{sOssss}}",
"table",
GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE,
"where",
"gsuc_mod_name",
json_object_get(j_parameters, "mod_name"),
"gsuc_username",
username,
"gsuc_x509_certificate_id",
cert_id);
res = h_delete(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
ret = G_OK;
} else {
y_log_message(Y_LOG_LEVEL_DEBUG, "delete_user_certificate_scheme_storage - Error executing j_query");
ret = G_ERROR_DB;
}
return ret;
}
static json_t * get_user_certificate_from_id_user_property(struct config_module * config, json_t * j_parameters, const char * username, const char * cert_id) {
json_t * j_user, * j_user_certificate, * j_parsed_certificate, * j_return = NULL, * j_element = NULL;
size_t index = 0;
j_user = config->glewlwyd_module_callback_get_user(config, username);
if (check_result_value(j_user, G_OK)) {
j_user_certificate = json_object_get(json_object_get(j_user, "user"), json_string_value(json_object_get(j_parameters, "user-certificate-property")));
if (json_is_string(j_user_certificate)) {
j_parsed_certificate = parse_certificate(json_string_value(j_user_certificate), (0 == o_strcmp("DER", json_string_value(json_object_get(j_parameters, "user-certificate-format")))));
if (check_result_value(j_parsed_certificate, G_OK)) {
if (0 == o_strcmp(cert_id, json_string_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "certificate_id")))) {
j_return = json_pack("{sisO}", "result", G_OK, "certificate", json_object_get(j_parsed_certificate, "certificate"));
} else {
j_return = json_pack("{si}", "result", G_ERROR_NOT_FOUND);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_certificate_from_id_user_property certificate - Error parse_certificate (1)");
j_return = json_pack("{si}", "result", G_ERROR);
}
json_decref(j_parsed_certificate);
} else if (json_is_array(j_user_certificate)) {
json_array_foreach(j_user_certificate, index, j_element) {
j_parsed_certificate = parse_certificate(json_string_value(j_element), (0 == o_strcmp("DER", json_string_value(json_object_get(j_parameters, "user-certificate-format")))));
if (check_result_value(j_parsed_certificate, G_OK)) {
if (0 == o_strcmp(cert_id, json_string_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "certificate_id")))) {
j_return = json_pack("{sisO}", "result", G_OK, "certificate", json_object_get(j_parsed_certificate, "certificate"));
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_certificate_from_id_user_property certificate - Error parse_certificate (2)");
}
json_decref(j_parsed_certificate);
}
if (j_return == NULL) {
j_return = json_pack("{si}", "result", G_ERROR_NOT_FOUND);
}
} else {
j_return = json_pack("{sis[]}", "result", G_OK, "certificate");
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_certificate_from_id_user_property certificate - Error glewlwyd_module_callback_get_user");
j_return = json_pack("{si}", "result", G_ERROR);
}
json_decref(j_user);
return j_return;
}
static json_t * get_user_certificate_list_user_property(struct config_module * config, json_t * j_parameters, const char * username) {
json_t * j_user, * j_user_certificate, * j_parsed_certificate = NULL, * j_certificate_array = NULL, * j_return = NULL, * j_element = NULL, * j_user_dn = NULL;
size_t index = 0;
j_user = config->glewlwyd_module_callback_get_user(config, username);
if (check_result_value(j_user, G_OK)) {
if (json_string_length(json_object_get(j_parameters, "user-certificate-property"))) {
if ((j_certificate_array = json_array()) != NULL) {
j_user_certificate = json_object_get(json_object_get(j_user, "user"), json_string_value(json_object_get(j_parameters, "user-certificate-property")));
if (json_is_string(j_user_certificate)) {
j_parsed_certificate = parse_certificate(json_string_value(j_user_certificate), (0 == o_strcmp("DER", json_string_value(json_object_get(j_parameters, "user-certificate-format")))));
if (check_result_value(j_parsed_certificate, G_OK)) {
json_object_del(json_object_get(j_parsed_certificate, "certificate"), "x509");
json_array_append(j_certificate_array, json_object_get(j_parsed_certificate, "certificate"));
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "user_auth_scheme_module_can_use certificate - Error parse_certificate (1)");
}
json_decref(j_parsed_certificate);
} else if (json_is_array(j_user_certificate)) {
json_array_foreach(j_user_certificate, index, j_element) {
j_parsed_certificate = parse_certificate(json_string_value(j_element), (0 == o_strcmp("DER", json_string_value(json_object_get(j_parameters, "user-certificate-format")))));
if (check_result_value(j_parsed_certificate, G_OK)) {
json_object_del(json_object_get(j_parsed_certificate, "certificate"), "x509");
json_array_append(j_certificate_array, json_object_get(j_parsed_certificate, "certificate"));
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "user_auth_scheme_module_can_use certificate - Error parse_certificate (2)");
}
json_decref(j_parsed_certificate);
}
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "user_auth_scheme_module_can_use certificate - Error allocating resources for j_certificate_array");
}
}
if (json_string_length(json_object_get(j_parameters, "user-dn-property"))) {
j_user_dn = json_object_get(json_object_get(j_user, "user"), json_string_value(json_object_get(j_parameters, "user-dn-property")));
if (!json_string_length(j_user_dn)) {
j_user_dn = NULL;
}
}
if (json_array_size(j_certificate_array) || json_string_length(j_user_dn)) {
j_return = json_pack("{si}", "result", G_OK);
if (json_array_size(j_certificate_array)) {
json_object_set(j_return, "certificate", j_certificate_array);
}
if (json_string_length(j_user_dn)) {
json_object_set(j_return, "dn", j_user_dn);
}
} else {
j_return = json_pack("{si}", "result", G_ERROR_UNAUTHORIZED);
}
json_decref(j_certificate_array);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "user_auth_scheme_module_can_use certificate - Error glewlwyd_module_callback_get_user");
j_return = json_pack("{si}", "result", G_ERROR);
}
json_decref(j_user);
return j_return;
}
static json_t * get_user_certificate_from_id_scheme_storage(struct config_module * config, json_t * j_parameters, const char * username, const char * cert_id) {
json_t * j_query, * j_result, * j_return;
int res;
j_query = json_pack("{sss[ssssssss]s{sOssss}}",
"table",
GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE,
"columns",
"gsuc_x509_certificate_dn AS certificate_dn",
"gsuc_x509_certificate_issuer_dn AS certificate_issuer_dn",
"gsuc_x509_certificate_id AS certificate_id",
SWITCH_DB_TYPE(config->conn->type, "UNIX_TIMESTAMP(gsuc_activation) AS activation", "strftime('%s', gsuc_activation) AS activation", "EXTRACT(EPOCH FROM gsuc_activation)::integer AS activation"),
SWITCH_DB_TYPE(config->conn->type, "UNIX_TIMESTAMP(gsuc_expiration) AS expiration", "strftime('%s', gsuc_expiration) AS expiration", "EXTRACT(EPOCH FROM gsuc_expiration)::integer AS expiration"),
"gsuc_enabled",
SWITCH_DB_TYPE(config->conn->type, "UNIX_TIMESTAMP(gsuc_last_used) AS last_used", "strftime('%s', gsuc_last_used) AS last_used", "EXTRACT(EPOCH FROM gsuc_last_used)::integer AS last_used"),
"gsuc_last_user_agent AS last_user_agent",
"where",
"gsuc_mod_name",
json_object_get(j_parameters, "mod_name"),
"gsuc_username",
username,
"gsuc_x509_certificate_id",
cert_id);
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), "gsuc_enabled"))) {
json_object_set(json_array_get(j_result, 0), "enabled", json_true());
} else {
json_object_set(json_array_get(j_result, 0), "enabled", json_false());
}
json_object_del(json_array_get(j_result, 0), "gsuc_enabled");
j_return = json_pack("{sisO}", "result", G_OK, "certificate", 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_DEBUG, "get_user_certificate_from_id_scheme_storage - Error executing j_query");
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
return j_return;
}
static json_t * get_user_certificate_list_scheme_storage(struct config_module * config, json_t * j_parameters, const char * username, int enabled) {
json_t * j_query, * j_result, * j_return, * j_element = NULL;
int res;
size_t index = 0;
j_query = json_pack("{sss[ssssssss]s{sOss}ss}",
"table",
GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE,
"columns",
"gsuc_x509_certificate_dn AS certificate_dn",
"gsuc_x509_certificate_issuer_dn AS certificate_issuer_dn",
"gsuc_x509_certificate_id AS certificate_id",
SWITCH_DB_TYPE(config->conn->type, "UNIX_TIMESTAMP(gsuc_activation) AS activation", "strftime('%s', gsuc_activation) AS activation", "EXTRACT(EPOCH FROM gsuc_activation)::integer AS activation"),
SWITCH_DB_TYPE(config->conn->type, "UNIX_TIMESTAMP(gsuc_expiration) AS expiration", "strftime('%s', gsuc_expiration) AS expiration", "EXTRACT(EPOCH FROM gsuc_expiration)::integer AS expiration"),
"gsuc_enabled",
SWITCH_DB_TYPE(config->conn->type, "UNIX_TIMESTAMP(gsuc_last_used) AS last_used", "strftime('%s', gsuc_last_used) AS last_used", "EXTRACT(EPOCH FROM gsuc_last_used)::integer AS last_used"),
"gsuc_last_user_agent AS last_user_agent",
"where",
"gsuc_mod_name",
json_object_get(j_parameters, "mod_name"),
"gsuc_username",
username,
"order_by",
"gsuc_id");
if (enabled) {
json_object_set_new(json_object_get(j_query, "where"), "gsuc_enabled", json_integer(1));
}
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
json_array_foreach(j_result, index, j_element) {
if (json_integer_value(json_object_get(j_element, "gsuc_enabled"))) {
json_object_set(j_element, "enabled", json_true());
} else {
json_object_set(j_element, "enabled", json_false());
}
json_object_del(j_element, "gsuc_enabled");
}
j_return = json_pack("{sisO}", "result", G_OK, "certificate", j_result);
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "get_user_certificate_list - Error executing j_query");
j_return = json_pack("{si}", "result", G_ERROR_DB);
}
return j_return;
}
static struct _cert_chain_element * get_cert_chain_element_from_dn(struct _cert_param * cert_params, const char * dn) {
size_t i;
struct _cert_chain_element * cert_chain_element = NULL;
for (i=0; i<cert_params->cert_array_len; i++) {
if (0 == o_strcmp(dn, cert_params->cert_array[i]->dn)) {
cert_chain_element = cert_params->cert_array[i];
break;
}
}
return cert_chain_element;
}
static int add_user_certificate_scheme_storage(struct config_module * config, json_t * j_parameters, const char * x509_data, const char * username, const char * user_agent) {
json_t * j_query, * j_parsed_certificate, * j_result;
char * expiration_clause, * activation_clause;
int res, ret;
if (o_strlen(x509_data)) {
j_parsed_certificate = parse_certificate(x509_data, 0);
if (check_result_value(j_parsed_certificate, G_OK)) {
j_result = get_user_certificate_from_id_scheme_storage(config, j_parameters, username, json_string_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "certificate_id")));
if (check_result_value(j_result, G_ERROR_NOT_FOUND)) {
if (config->conn->type==HOEL_DB_TYPE_MARIADB) {
expiration_clause = msprintf("FROM_UNIXTIME(%"JSON_INTEGER_FORMAT")", json_integer_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "expiration")));
activation_clause = msprintf("FROM_UNIXTIME(%"JSON_INTEGER_FORMAT")", json_integer_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "activation")));
} else if (config->conn->type==HOEL_DB_TYPE_PGSQL) {
expiration_clause = msprintf("TO_TIMESTAMP(%"JSON_INTEGER_FORMAT")", json_integer_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "expiration")));
activation_clause = msprintf("TO_TIMESTAMP(%"JSON_INTEGER_FORMAT")", json_integer_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "activation")));
} else { // HOEL_DB_TYPE_SQLITE
expiration_clause = msprintf("%"JSON_INTEGER_FORMAT"", json_integer_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "expiration")));
activation_clause = msprintf("%"JSON_INTEGER_FORMAT"", json_integer_value(json_object_get(json_object_get(j_parsed_certificate, "certificate"), "activation")));
}
j_query = json_pack("{ss s{sO ss sO sO sO sO s{ss} s{ss} so}}",
"table",
GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE,
"values",
"gsuc_mod_name",
json_object_get(j_parameters, "mod_name"),
"gsuc_username",
username,
"gsuc_x509_certificate_id",
json_object_get(json_object_get(j_parsed_certificate, "certificate"), "certificate_id"),
"gsuc_x509_certificate_content",
json_object_get(json_object_get(j_parsed_certificate, "certificate"), "x509"),
"gsuc_x509_certificate_dn",
json_object_get(json_object_get(j_parsed_certificate, "certificate"), "certificate_dn"),
"gsuc_x509_certificate_issuer_dn",
json_object_get(json_object_get(j_parsed_certificate, "certificate"), "certificate_issuer_dn"),
"gsuc_expiration",
"raw",
expiration_clause,
"gsuc_activation",
"raw",
activation_clause,
"gsuc_last_used",
json_null());
o_free(expiration_clause);
o_free(activation_clause);
if (o_strlen(user_agent)) {
json_object_set_new(json_object_get(j_query, "values"), "gsuc_last_user_agent", json_string(user_agent));
}
res = h_insert(config->conn, j_query, NULL);
json_decref(j_query);
if (res == H_OK) {
ret = G_OK;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "add_user_certificate_scheme_storage - Error executing j_query");
ret = G_ERROR_DB;
}
} else if (check_result_value(j_result, G_OK)) {
y_log_message(Y_LOG_LEVEL_DEBUG, "add_user_certificate_scheme_storage - get_user_certificate_from_id_scheme_storage error param");
ret = G_ERROR_PARAM;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "add_user_certificate_scheme_storage - Error get_user_certificate_from_id_scheme_storage");
ret = G_ERROR;
}
json_decref(j_result);
} else if (check_result_value(j_parsed_certificate, G_ERROR_PARAM)) {
y_log_message(Y_LOG_LEVEL_DEBUG, "add_user_certificate_scheme_storage - parse_certificate error param");
ret = G_ERROR_PARAM;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "add_user_certificate_scheme_storage - Error parse_certificate");
ret = G_ERROR;
}
json_decref(j_parsed_certificate);
} else {
y_log_message(Y_LOG_LEVEL_DEBUG, "add_user_certificate_scheme_storage - x509 empty");
ret = G_ERROR_PARAM;
}
return ret;
}
static void scm_gnutls_certificate_status_to_c_string (gnutls_certificate_status_t c_obj) {
static const struct {
gnutls_certificate_status_t value;
const char* name;
} table[] =
{
{ GNUTLS_CERT_INVALID, "invalid" },
{ GNUTLS_CERT_REVOKED, "revoked" },
{ GNUTLS_CERT_SIGNER_NOT_FOUND, "signer-not-found" },
{ GNUTLS_CERT_SIGNER_NOT_CA, "signer-not-ca" },
{ GNUTLS_CERT_INSECURE_ALGORITHM, "insecure-algorithm" },
};
unsigned i;
for (i = 0; i < 5; i++)
{
if (table[i].value & c_obj)
{
y_log_message(Y_LOG_LEVEL_DEBUG, "%s", table[i].name);
}
}
}
static int is_certificate_valid_from_ca_chain(struct _cert_param * cert_params, gnutls_x509_crt_t cert) {
int ret = G_OK, res;
unsigned int result = 0;
gnutls_x509_crt_t * cert_chain = NULL, root_x509 = NULL;
gnutls_x509_trust_list_t tlist = NULL;
size_t cert_chain_len = 0, issuer_dn_len = 0;
char * issuer_dn = NULL;
struct _cert_chain_element * cert_chain_element;
if ((res = gnutls_x509_crt_get_issuer_dn(cert, NULL, &issuer_dn_len)) == GNUTLS_E_SHORT_MEMORY_BUFFER) {
if ((issuer_dn = o_malloc(issuer_dn_len+1)) != NULL && gnutls_x509_crt_get_issuer_dn(cert, issuer_dn, &issuer_dn_len) >= 0) {
// Calculate ca chain length
cert_chain_len = 1;
cert_chain_element = get_cert_chain_element_from_dn(cert_params, issuer_dn);
while (cert_chain_element != NULL) {
if (cert_chain_element->issuer_cert == NULL) {
root_x509 = cert_chain_element->cert;
}
cert_chain_len++;
cert_chain_element = cert_chain_element->issuer_cert;
}
if (root_x509 != NULL) {
if ((cert_chain = o_malloc(cert_chain_len*sizeof(gnutls_x509_crt_t))) != NULL) {
cert_chain[0] = cert;
cert_chain_len = 1;
cert_chain_element = get_cert_chain_element_from_dn(cert_params, issuer_dn);
while (cert_chain_element != NULL) {
cert_chain[cert_chain_len] = cert_chain_element->cert;
cert_chain_len++;
cert_chain_element = cert_chain_element->issuer_cert;
}
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_chain, cert_chain_len, 0, &result, NULL) >= 0) {
if (!result) {
ret = G_OK;
} else {
y_log_message(Y_LOG_LEVEL_DEBUG, "is_certificate_valid_from_ca_chain - certificate chain invalid");
scm_gnutls_certificate_status_to_c_string(result);
ret = G_ERROR_UNAUTHORIZED;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_certificate_valid_from_ca_chain - Error gnutls_x509_trust_list_verify_crt");
ret = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_certificate_valid_from_ca_chain - Error gnutls_x509_trust_list_add_cas");
ret = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_certificate_valid_from_ca_chain - Error gnutls_x509_trust_list_init");
ret = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_certificate_valid_from_ca_chain - Error allocating resources for cert_chain");
ret = G_ERROR;
}
o_free(cert_chain);
} else {
y_log_message(Y_LOG_LEVEL_DEBUG, "is_certificate_valid_from_ca_chain - no root certificate found");
ret = G_ERROR_UNAUTHORIZED;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_certificate_valid_from_ca_chain - Error gnutls_x509_crt_get_issuer_dn (2)");
ret = G_ERROR;
}
} else if (res == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
ret = G_ERROR_UNAUTHORIZED;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_certificate_valid_from_ca_chain - Error gnutls_x509_crt_get_issuer_dn (1)");
ret = G_ERROR;
}
o_free(issuer_dn);
gnutls_x509_trust_list_deinit(tlist, 0);
return ret;
}
static int is_user_certificate_valid_user_property(struct config_module * config, json_t * j_parameters, const char * username, gnutls_x509_crt_t cert) {
json_t * j_user_list = get_user_certificate_list_user_property(config, j_parameters, username), * j_element = NULL;
int ret;
unsigned char key_id_enc[256] = {0};
size_t index = 0, key_id_enc_len = 0;
gnutls_datum_t cert_dn = {NULL, 0};
if (check_result_value(j_user_list, G_OK)) {
if (json_string_length(json_object_get(j_user_list, "dn"))) {
if (gnutls_x509_crt_get_dn2(cert, &cert_dn) == GNUTLS_E_SUCCESS) {
if (cert_dn.size == json_string_length(json_object_get(j_user_list, "dn")) &&
0 == o_strncasecmp(json_string_value(json_object_get(j_user_list, "dn")), (const char *)cert_dn.data, cert_dn.size)) {
ret = G_OK;
} else {
ret = G_ERROR_UNAUTHORIZED;
}
gnutls_free(cert_dn.data);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_user_certificate_valid_user_property - Error gnutls_x509_crt_get_dn2");
ret = G_ERROR;
}
} else {
if (get_certificate_id(cert, key_id_enc, &key_id_enc_len) == G_OK) {
ret = G_ERROR_UNAUTHORIZED;
json_array_foreach(json_object_get(j_user_list, "certificate"), index, j_element) {
if (0 == o_strcmp((const char *)key_id_enc, json_string_value(json_object_get(j_element, "certificate_id")))) {
ret = G_OK;
}
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_user_certificate_valid_user_property - Error gnutls_x509_crt_get_key_id");
ret = G_ERROR;
}
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_user_certificate_valid_user_property - Error get_user_certificate_list_user_property");
ret = G_ERROR;
}
json_decref(j_user_list);
return ret;
}
static int is_user_certificate_valid_scheme_storage(struct config_module * config, json_t * j_parameters, const char * username, gnutls_x509_crt_t cert) {
int ret, res;
json_t * j_query, * j_result;
unsigned char key_id_enc[256] = {0};
size_t key_id_enc_len = 0;
if (get_certificate_id(cert, key_id_enc, &key_id_enc_len) == G_OK) {
key_id_enc[key_id_enc_len] = '\0';
j_query = json_pack("{sss[s]s{sOsssssi}}",
"table",
GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE,
"columns",
"gsuc_id",
"where",
"gsuc_mod_name",
json_object_get(j_parameters, "mod_name"),
"gsuc_username",
username,
"gsuc_x509_certificate_id",
key_id_enc,
"gsuc_enabled",
1);
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
if (json_array_size(j_result)) {
ret = G_OK;
} else {
ret = G_ERROR_UNAUTHORIZED;
}
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_user_certificate_valid_scheme_storage - Error executing j_query");
ret = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "is_user_certificate_valid_scheme_storage - Error get_certificate_id");
ret = G_ERROR;
}
return ret;
}
static int is_user_certificate_valid(struct config_module * config, json_t * j_parameters, const char * username, gnutls_x509_crt_t cert) {
time_t now, exp;
time(&now);
exp = gnutls_x509_crt_get_expiration_time(cert);
if (exp != (time_t)-1 && now < exp) {
if (json_object_get(j_parameters, "use-scheme-storage") == json_true()) {
return is_user_certificate_valid_scheme_storage(config, j_parameters, username, cert);
} else {
return is_user_certificate_valid_user_property(config, j_parameters, username, cert);
}
} else {
y_log_message(Y_LOG_LEVEL_DEBUG, "is_user_certificate_valid - Certificate expired");
return G_ERROR_UNAUTHORIZED;
}
}
static json_t * identify_certificate(struct config_module * config, json_t * j_parameters, gnutls_x509_crt_t cert) {
time_t now, exp;
int res;
json_t * j_query, * j_result, * j_return;
unsigned char key_id_enc[256] = {0};
size_t key_id_enc_len = 0;
time(&now);
exp = gnutls_x509_crt_get_expiration_time(cert);
if (exp != (time_t)-1 && now < exp) {
if (json_object_get(j_parameters, "use-scheme-storage") == json_true()) {
if (get_certificate_id(cert, key_id_enc, &key_id_enc_len) == G_OK) {
key_id_enc[key_id_enc_len] = '\0';
j_query = json_pack("{sss[s]s{sOsssi}}",
"table",
GLEWLWYD_SCHEME_CERTIFICATE_TABLE_USER_CERTIFICATE,
"columns",
"gsuc_username AS username",
"where",
"gsuc_mod_name",
json_object_get(j_parameters, "mod_name"),
"gsuc_x509_certificate_id",
key_id_enc,
"gsuc_enabled",
1);
res = h_select(config->conn, j_query, &j_result, NULL);
json_decref(j_query);
if (res == H_OK) {
if (json_array_size(j_result) == 1) {
j_return = json_pack("{sisO}", "result", G_OK, "username", json_object_get(json_array_get(j_result, 0), "username"));
} else {
j_return = json_pack("{si}", "result", G_ERROR_UNAUTHORIZED);
}
json_decref(j_result);
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "identify_certificate - Error executing j_query");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "identify_certificate - Error get_certificate_id");
j_return = json_pack("{si}", "result", G_ERROR);
}
} else {
j_return = json_pack("{si}", "result", G_ERROR_UNAUTHORIZED);
}
} else {
y_log_message(Y_LOG_LEVEL_DEBUG, "identify_certificate - Certificate expired");
j_return = json_pack("{si}", "result", G_ERROR_UNAUTHORIZED);
}
return j_return;
}
static void update_cert_chain_issuer(struct _cert_chain_element ** ca_chain, size_t cert_array_len, struct _cert_chain_element * cur_ca) {
size_t i;
for (i=0; i<cert_array_len; i++) {
if (0 == o_strcmp(ca_chain[i]->dn, cur_ca->issuer_dn)) {
cur_ca->issuer_cert = ca_chain[i];
}
if (0 == o_strcmp(ca_chain[i]->issuer_dn, cur_ca->dn)) {
ca_chain[i]->issuer_cert = cur_ca;
}
}
}
static int parse_ca_chain(json_t * j_ca_chain, struct _cert_chain_element *** ca_chain, size_t * cert_array_len) {
json_t * j_element = NULL;
size_t index = 0, len = 0;
int ret = G_OK, cur_status, res;
gnutls_x509_crt_t cert;
struct _cert_chain_element * cur_ca;
gnutls_datum_t cert_dat;
*ca_chain = NULL;
*cert_array_len = 0;
UNUSED(j_ca_chain);
if (j_ca_chain != NULL) {
json_array_foreach(j_ca_chain, index, j_element) {
cert = NULL;
cur_status = G_OK;
if (!gnutls_x509_crt_init(&cert)) {
cert_dat.data = (unsigned char *)json_string_value(json_object_get(j_element, "cert-file"));
cert_dat.size = json_string_length(json_object_get(j_element, "cert-file"));
if ((res = gnutls_x509_crt_import(cert, &cert_dat, GNUTLS_X509_FMT_PEM)) < 0) {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_ca_chain - Error gnutls_x509_crt_import: %d", res);
cur_status = G_ERROR;
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_ca_chain - Error gnutls_x509_crt_init");
cur_status = G_ERROR;
}
if (cur_status == G_OK) {
cur_ca = o_malloc(sizeof(struct _cert_chain_element));
cur_ca->cert = cert;
cur_ca->dn = NULL;
cur_ca->issuer_dn = NULL;
cur_ca->issuer_cert = NULL;
len = 0;
if (gnutls_x509_crt_get_dn(cert, NULL, &len) == GNUTLS_E_SHORT_MEMORY_BUFFER) {
if ((cur_ca->dn = o_malloc(len+1)) == NULL || gnutls_x509_crt_get_dn(cert, cur_ca->dn, &len) < 0) {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_ca_chain - Error gnutls_x509_crt_get_dn (2) on cert at index %zu", index);
cur_status = G_ERROR;
} else {
cur_ca->dn[len] = '\0';
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_ca_chain - Error gnutls_x509_crt_get_dn (1) on cert at index %zu", index);
cur_status = G_ERROR;
}
if (cur_status == G_OK) {
len = 0;
if ((res = gnutls_x509_crt_get_issuer_dn(cert, NULL, &len)) == GNUTLS_E_SHORT_MEMORY_BUFFER) {
if ((cur_ca->issuer_dn = o_malloc(len+1)) == NULL || gnutls_x509_crt_get_issuer_dn(cert, cur_ca->issuer_dn, &len) < 0) {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_ca_chain - Error gnutls_x509_crt_get_issuer_dn (2) on cert at index %zu", index);
cur_status = G_ERROR;
} else {
cur_ca->issuer_dn[len] = '\0';
}
} else if (res != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_ca_chain - Error gnutls_x509_crt_get_issuer_dn (1) on cert at index %zu", index);
cur_status = G_ERROR;
}
}
if (cur_status == G_OK) {
update_cert_chain_issuer(*ca_chain, *cert_array_len, cur_ca);
*ca_chain = o_realloc(*ca_chain, ((*cert_array_len)+1)*sizeof(struct _cert_chain_element *));
if (*ca_chain != NULL) {
(*ca_chain)[*cert_array_len] = cur_ca;
(*cert_array_len)++;
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "parse_ca_chain - Error alocatig resources for ca_chain at index %zu", index);
gnutls_x509_crt_deinit(cert);
o_free(cur_ca->issuer_dn);
o_free(cur_ca->dn);
o_free(cur_ca);
}
} else {
gnutls_x509_crt_deinit(cert);
o_free(cur_ca->issuer_dn);
o_free(cur_ca->dn);
o_free(cur_ca);
}
} else {
gnutls_x509_crt_deinit(cert);
}
if (cur_status != G_OK) {
ret = cur_status;
break;
}
}
}
return ret;
}
static json_t * is_certificate_parameters_valid(json_t * j_parameters) {
json_t * j_array = json_array(), * j_return, * j_element = NULL;
size_t index = 0;
if (j_array != NULL) {
if (json_is_object(j_parameters)) {
if (json_object_get(j_parameters, "cert-source") != NULL && 0 != o_strcmp("TLS", json_string_value(json_object_get(j_parameters, "cert-source"))) && 0 != o_strcmp("header", json_string_value(json_object_get(j_parameters, "cert-source"))) && 0 != o_strcmp("both", json_string_value(json_object_get(j_parameters, "cert-source")))) {
json_array_append_new(j_array, json_string("cert-source is optional and must be one of the following values: 'TLS', 'header' or 'both'"));
}
if ((0 == o_strcmp("header", json_string_value(json_object_get(j_parameters, "cert-source"))) || 0 == o_strcmp("both", json_string_value(json_object_get(j_parameters, "cert-source")))) && !json_string_length(json_object_get(j_parameters, "header-name"))) {
json_array_append_new(j_array, json_string("header-name is mandatory when cert-source is 'header' or 'both' and must be a non empty string"));
}
if (json_object_get(j_parameters, "use-scheme-storage") != NULL && !json_is_boolean(json_object_get(j_parameters, "use-scheme-storage"))) {
json_array_append_new(j_array, json_string("use-scheme-storage is optional and must be a boolean"));
}
if (json_object_get(j_parameters, "use-scheme-storage") != json_true()) {
if (!json_string_length(json_object_get(j_parameters, "user-certificate-property")) && !json_string_length(json_object_get(j_parameters, "user-dn-property"))) {
json_array_append_new(j_array, json_string("user-certificate-property or user-dn-property is mandatory and must be a non empty string"));
}
if (json_string_length(json_object_get(j_parameters, "user-certificate-property")) &&
json_object_get(j_parameters, "user-certificate-format") != NULL &&
0 != o_strcmp("PEM", json_string_value(json_object_get(j_parameters, "user-certificate-format"))) &&
0 != o_strcmp("DER", json_string_value(json_object_get(j_parameters, "user-certificate-format")))) {