-
Notifications
You must be signed in to change notification settings - Fork 2
/
jni.c
1383 lines (1131 loc) · 35.4 KB
/
jni.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
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2013 Kevin Cernekee <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include <config.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <jni.h>
#include "openconnect.h"
struct libctx {
JNIEnv *jenv;
jobject jobj;
jobject async_lock;
struct openconnect_info *vpninfo;
int cmd_fd;
int loglevel;
};
static void throw_excep(JNIEnv *jenv, const char *exc, int line)
{
jclass excep;
char msg[64];
snprintf(msg, 64, "%s:%d", __FILE__, line);
(*jenv)->ExceptionClear(jenv);
excep = (*jenv)->FindClass(jenv, exc);
if (excep)
(*jenv)->ThrowNew(jenv, excep, msg);
}
#define OOM(jenv) do { throw_excep(jenv, "java/lang/OutOfMemoryError", __LINE__); } while (0)
static struct libctx *getctx(JNIEnv *jenv, jobject jobj)
{
jclass jcls = (*jenv)->GetObjectClass(jenv, jobj);
jfieldID jfld = (*jenv)->GetFieldID(jenv, jcls, "libctx", "J");
if (!jfld)
return NULL;
return (void *)(unsigned long)(*jenv)->GetLongField(jenv, jobj, jfld);
}
/*
* GetMethodID() and GetFieldID() and NewStringUTF() will automatically throw exceptions on error
*/
static jmethodID get_obj_mid(struct libctx *ctx, jobject jobj, const char *name, const char *sig)
{
jclass jcls = (*ctx->jenv)->GetObjectClass(ctx->jenv, jobj);
jmethodID mid = (*ctx->jenv)->GetMethodID(ctx->jenv, jcls, name, sig);
return mid;
}
static jstring dup_to_jstring(JNIEnv *jenv, const char *in)
{
/*
* Many implementations of NewStringUTF() will return NULL on
* NULL input, but that isn't guaranteed:
* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35979
*/
return in ? (*jenv)->NewStringUTF(jenv, in) : NULL;
}
static int get_cstring(JNIEnv *jenv, jstring in, const char **out)
{
const char *tmp;
if (in == NULL) {
*out = NULL;
return 0;
}
tmp = (*jenv)->GetStringUTFChars(jenv, in, NULL);
if (!tmp) {
OOM(jenv);
return -1;
}
*out = tmp;
return 0;
}
static void release_cstring(JNIEnv *jenv, jstring jstr, const char *cstr)
{
if (cstr)
(*jenv)->ReleaseStringUTFChars(jenv, jstr, cstr);
}
static int set_int(struct libctx *ctx, jobject jobj, const char *name, int value)
{
jclass jcls = (*ctx->jenv)->GetObjectClass(ctx->jenv, jobj);
jfieldID jfld = (*ctx->jenv)->GetFieldID(ctx->jenv, jcls, name, "I");
if (!jfld)
return -1;
(*ctx->jenv)->SetIntField(ctx->jenv, jobj, jfld, value);
return 0;
}
static int set_long(struct libctx *ctx, jobject jobj, const char *name, uint64_t value)
{
jclass jcls = (*ctx->jenv)->GetObjectClass(ctx->jenv, jobj);
jfieldID jfld = (*ctx->jenv)->GetFieldID(ctx->jenv, jcls, name, "J");
if (!jfld)
return -1;
(*ctx->jenv)->SetLongField(ctx->jenv, jobj, jfld, (jlong)value);
return 0;
}
static int set_string(struct libctx *ctx, jobject jobj, const char *name, const char *value)
{
jclass jcls = (*ctx->jenv)->GetObjectClass(ctx->jenv, jobj);
jfieldID jfld = (*ctx->jenv)->GetFieldID(ctx->jenv, jcls, name, "Ljava/lang/String;");
jstring jarg;
if (!jfld)
return -1;
jarg = dup_to_jstring(ctx->jenv, value);
if (value && !jarg)
return -1;
(*ctx->jenv)->SetObjectField(ctx->jenv, jobj, jfld, jarg);
return 0;
}
static int add_string(struct libctx *ctx, jclass jcls, jobject jobj,
const char *name, const char *value)
{
jmethodID mid = (*ctx->jenv)->GetMethodID(ctx->jenv, jcls, name, "(Ljava/lang/String;)V");
jstring jarg;
if (!value)
return 0;
if (!mid)
return -1;
jarg = dup_to_jstring(ctx->jenv, value);
if (!jarg)
return -1;
(*ctx->jenv)->CallVoidMethod(ctx->jenv, jobj, mid, jarg);
(*ctx->jenv)->DeleteLocalRef(ctx->jenv, jarg);
return 0;
}
static int add_string_pair(struct libctx *ctx, jclass jcls, jobject jobj,
const char *name, const char *key, const char *value)
{
jmethodID mid = (*ctx->jenv)->GetMethodID(ctx->jenv, jcls, name, "(Ljava/lang/String;Ljava/lang/String;)V");
jstring jarg0, jarg1;
if (!key || !value)
return -1;
if (!mid)
return -1;
jarg0 = dup_to_jstring(ctx->jenv, key);
if (!jarg0)
return -1;
jarg1 = dup_to_jstring(ctx->jenv, value);
if (!jarg1) {
(*ctx->jenv)->DeleteLocalRef(ctx->jenv, jarg0);
return -1;
}
(*ctx->jenv)->CallVoidMethod(ctx->jenv, jobj, mid, jarg0, jarg1);
(*ctx->jenv)->DeleteLocalRef(ctx->jenv, jarg1);
(*ctx->jenv)->DeleteLocalRef(ctx->jenv, jarg0);
return 0;
}
static int validate_peer_cert_cb(void *privdata, const char *reason)
{
struct libctx *ctx = privdata;
jstring jreason;
int ret = -1;
jmethodID mid;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return -1;
jreason = dup_to_jstring(ctx->jenv, reason);
if (!jreason)
goto out;
mid = get_obj_mid(ctx, ctx->jobj, "onValidatePeerCert", "(Ljava/lang/String;)I");
if (mid)
ret = (*ctx->jenv)->CallIntMethod(ctx->jenv, ctx->jobj, mid, jreason);
out:
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
return ret;
}
static int write_new_config_cb(void *privdata, const char *buf, int buflen)
{
struct libctx *ctx = privdata;
jmethodID mid;
jbyteArray jbuf;
int ret = -1;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return -1;
mid = get_obj_mid(ctx, ctx->jobj, "onWriteNewConfig", "([B)I");
if (!mid)
goto out;
jbuf = (*ctx->jenv)->NewByteArray(ctx->jenv, buflen);
if (!jbuf)
goto out;
(*ctx->jenv)->SetByteArrayRegion(ctx->jenv, jbuf, 0, buflen, (jbyte *)buf);
ret = (*ctx->jenv)->CallIntMethod(ctx->jenv, ctx->jobj, mid, jbuf);
out:
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
return ret;
}
static void protect_socket_cb(void *privdata, int fd)
{
struct libctx *ctx = privdata;
jmethodID mid;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return;
mid = get_obj_mid(ctx, ctx->jobj, "onProtectSocket", "(I)V");
if (mid)
(*ctx->jenv)->CallVoidMethod(ctx->jenv, ctx->jobj, mid, fd);
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
}
static void stats_cb(void *privdata, const struct oc_stats *stats)
{
struct libctx *ctx = privdata;
jmethodID mid;
jclass jcls;
jobject jobj = NULL;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return;
jcls = (*ctx->jenv)->FindClass(ctx->jenv, "org/infradead/libopenconnect/LibOpenConnect$VPNStats");
if (jcls == NULL)
goto out;
mid = (*ctx->jenv)->GetMethodID(ctx->jenv, jcls, "<init>", "()V");
if (!mid)
goto out;
jobj = (*ctx->jenv)->NewObject(ctx->jenv, jcls, mid);
if (!jobj)
goto out;
if (set_long(ctx, jobj, "txPkts", stats->tx_pkts) ||
set_long(ctx, jobj, "txBytes", stats->tx_bytes) ||
set_long(ctx, jobj, "rxPkts", stats->rx_pkts) ||
set_long(ctx, jobj, "rxBytes", stats->rx_bytes))
goto out;
mid = get_obj_mid(ctx, ctx->jobj, "onStatsUpdate",
"(Lorg/infradead/libopenconnect/LibOpenConnect$VPNStats;)V");
if (mid)
(*ctx->jenv)->CallVoidMethod(ctx->jenv, ctx->jobj, mid, jobj);
out:
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
}
static void setup_tun_cb(void *privdata)
{
struct libctx *ctx = privdata;
jmethodID mid;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return;
mid = get_obj_mid(ctx, ctx->jobj, "onSetupTun", "()V");
if (mid)
(*ctx->jenv)->CallVoidMethod(ctx->jenv, ctx->jobj, mid);
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
}
static void reconnected_cb(void *privdata)
{
struct libctx *ctx = privdata;
jmethodID mid;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return;
mid = get_obj_mid(ctx, ctx->jobj, "onReconnected", "()V");
if (mid)
(*ctx->jenv)->CallVoidMethod(ctx->jenv, ctx->jobj, mid);
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
}
static jobject new_auth_form(struct libctx *ctx, struct oc_auth_form *form)
{
jmethodID mid;
jclass jcls;
jobject jobj = NULL;
jcls = (*ctx->jenv)->FindClass(ctx->jenv, "org/infradead/libopenconnect/LibOpenConnect$AuthForm");
if (jcls == NULL)
return NULL;
mid = (*ctx->jenv)->GetMethodID(ctx->jenv, jcls, "<init>", "()V");
if (!mid)
return NULL;
jobj = (*ctx->jenv)->NewObject(ctx->jenv, jcls, mid);
if (!jobj)
return NULL;
if (set_string(ctx, jobj, "banner", form->banner) ||
set_string(ctx, jobj, "message", form->message) ||
set_string(ctx, jobj, "error", form->error) ||
set_string(ctx, jobj, "authID", form->auth_id) ||
set_string(ctx, jobj, "method", form->method) ||
set_string(ctx, jobj, "action", form->action) ||
set_int(ctx, jobj, "authgroupSelection", form->authgroup_selection)) {
return NULL;
}
return jobj;
}
static jobject new_form_choice(struct libctx *ctx, struct oc_choice *choice)
{
jmethodID mid;
jclass jcls;
jobject jobj = NULL;
jcls = (*ctx->jenv)->FindClass(ctx->jenv,
"org/infradead/libopenconnect/LibOpenConnect$FormChoice");
if (jcls == NULL)
return NULL;
mid = (*ctx->jenv)->GetMethodID(ctx->jenv, jcls, "<init>", "()V");
if (!mid)
return NULL;
jobj = (*ctx->jenv)->NewObject(ctx->jenv, jcls, mid);
if (!jobj)
return NULL;
if (set_string(ctx, jobj, "name", choice->name) ||
set_string(ctx, jobj, "label", choice->label) ||
set_string(ctx, jobj, "authType", choice->auth_type) ||
set_string(ctx, jobj, "overrideName", choice->override_name) ||
set_string(ctx, jobj, "overrideLabel", choice->override_label)) {
return NULL;
}
return jobj;
}
static int populate_select_choices(struct libctx *ctx, jobject jopt, struct oc_form_opt_select *opt)
{
jmethodID mid;
int i;
mid = get_obj_mid(ctx, jopt, "addChoice",
"(Lorg/infradead/libopenconnect/LibOpenConnect$FormChoice;)V");
if (!mid)
return -1;
for (i = 0; i < opt->nr_choices; i++) {
jobject jformchoice = new_form_choice(ctx, opt->choices[i]);
if (!jformchoice)
return -1;
(*ctx->jenv)->CallVoidMethod(ctx->jenv, jopt, mid, jformchoice);
}
return 0;
}
static int add_form_option(struct libctx *ctx, jobject jform, struct oc_form_opt *opt, int is_authgroup)
{
jmethodID addOpt;
jobject jopt;
addOpt = get_obj_mid(ctx, jform, "addOpt",
"(Z)Lorg/infradead/libopenconnect/LibOpenConnect$FormOpt;");
if (!addOpt)
return -1;
jopt = (*ctx->jenv)->CallObjectMethod(ctx->jenv, jform, addOpt, is_authgroup);
if (jopt == NULL)
return -1;
if (set_int(ctx, jopt, "type", opt->type) ||
set_string(ctx, jopt, "name", opt->name) ||
set_string(ctx, jopt, "label", opt->label) ||
set_string(ctx, jopt, "value", opt->_value) ||
set_long(ctx, jopt, "flags", opt->flags))
return -1;
if (opt->type == OC_FORM_OPT_SELECT &&
populate_select_choices(ctx, jopt, (struct oc_form_opt_select *)opt))
return -1;
return 0;
}
static char *lookup_choice_name(struct oc_form_opt_select *opt, const char *name)
{
int i;
/* opt->_value is NOT a caller-allocated string for OC_FORM_OPT_SELECT */
for (i = 0; i < opt->nr_choices; i++)
if (!strcmp(opt->choices[i]->name, name))
return opt->choices[i]->name;
return NULL;
}
static int process_auth_form_cb(void *privdata, struct oc_auth_form *form)
{
struct libctx *ctx = privdata;
jobject jform;
jmethodID callback, getOptValue;
struct oc_form_opt *opt;
jint ret;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return -1;
/* create and populate new AuthForm object and option/choice lists */
jform = new_auth_form(ctx, form);
if (!jform)
goto err;
getOptValue = get_obj_mid(ctx, jform, "getOptValue", "(Ljava/lang/String;)Ljava/lang/String;");
if (!getOptValue)
goto err;
for (opt = form->opts; opt; opt = opt->next) {
int is_authgroup = opt == (void *)form->authgroup_opt;
if (add_form_option(ctx, jform, opt, is_authgroup) < 0)
goto err;
}
/* invoke onProcessAuthForm callback */
callback = get_obj_mid(ctx, ctx->jobj, "onProcessAuthForm",
"(Lorg/infradead/libopenconnect/LibOpenConnect$AuthForm;)I");
if (!callback)
goto err;
ret = (*ctx->jenv)->CallIntMethod(ctx->jenv, ctx->jobj, callback, jform);
/* copy any populated form fields back into the C structs */
for (opt = form->opts; opt; opt = opt->next) {
jstring jname, jvalue;
jname = dup_to_jstring(ctx->jenv, opt->name);
if (!jname)
goto err;
jvalue = (*ctx->jenv)->CallObjectMethod(ctx->jenv, jform, getOptValue, jname);
if (jvalue) {
const char *tmp = (*ctx->jenv)->GetStringUTFChars(ctx->jenv, jvalue, NULL);
if (!tmp)
goto err;
if (opt->type == OC_FORM_OPT_SELECT)
opt->_value = lookup_choice_name((void *)opt, tmp);
else {
free(opt->_value);
opt->_value = strdup(tmp);
if (!opt->_value)
OOM(ctx->jenv);
}
(*ctx->jenv)->ReleaseStringUTFChars(ctx->jenv, jvalue, tmp);
}
}
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
return ret;
err:
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
return -1;
}
static void __attribute__ ((format(printf, 3, 4)))
progress_cb(void *privdata, int level, const char *fmt, ...)
{
struct libctx *ctx = privdata;
va_list ap;
char *msg;
jstring jmsg;
int ret, loglevel;
jmethodID mid;
(*ctx->jenv)->MonitorEnter(ctx->jenv, ctx->async_lock);
loglevel = ctx->loglevel;
(*ctx->jenv)->MonitorExit(ctx->jenv, ctx->async_lock);
if (level > loglevel)
return;
va_start(ap, fmt);
ret = vasprintf(&msg, fmt, ap);
va_end(ap);
if (ret < 0) {
OOM(ctx->jenv);
return;
}
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return;
jmsg = dup_to_jstring(ctx->jenv, msg);
free(msg);
if (!jmsg)
goto out;
mid = get_obj_mid(ctx, ctx->jobj, "onProgress", "(ILjava/lang/String;)V");
if (mid)
(*ctx->jenv)->CallVoidMethod(ctx->jenv, ctx->jobj, mid, level, jmsg);
out:
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
}
static int lock_token_cb(void *privdata)
{
struct libctx *ctx = privdata;
jmethodID mid;
int ret = -1;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return -1;
mid = get_obj_mid(ctx, ctx->jobj, "onTokenLock", "()I");
if (mid)
ret = (*ctx->jenv)->CallIntMethod(ctx->jenv, ctx->jobj, mid);
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
return ret;
}
static int unlock_token_cb(void *privdata, const char *new_token)
{
struct libctx *ctx = privdata;
jstring jtoken;
int ret = -1;
jmethodID mid;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return -1;
jtoken = dup_to_jstring(ctx->jenv, new_token);
if (!jtoken)
goto out;
mid = get_obj_mid(ctx, ctx->jobj, "onTokenUnlock", "(Ljava/lang/String;)I");
if (mid)
ret = (*ctx->jenv)->CallIntMethod(ctx->jenv, ctx->jobj, mid, jtoken);
out:
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
return ret;
}
/* Library init/uninit */
static jobject init_async_lock(struct libctx *ctx)
{
jclass jcls = (*ctx->jenv)->GetObjectClass(ctx->jenv, ctx->jobj);
jfieldID jfld = (*ctx->jenv)->GetFieldID(ctx->jenv, jcls, "asyncLock", "Ljava/lang/Object;");
jobject jobj = (*ctx->jenv)->GetObjectField(ctx->jenv, ctx->jobj, jfld);
if (jobj)
jobj = (*ctx->jenv)->NewGlobalRef(ctx->jenv, jobj);
return jobj;
}
JNIEXPORT jlong JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_init(
JNIEnv *jenv, jobject jobj, jstring juseragent)
{
char *useragent;
struct libctx *ctx = calloc(1, sizeof(*ctx));
if (!ctx)
goto bad;
ctx->jenv = jenv;
ctx->jobj = (*jenv)->NewGlobalRef(jenv, jobj);
if (!ctx->jobj)
goto bad_free_ctx;
ctx->async_lock = init_async_lock(ctx);
if (!ctx->async_lock)
goto bad_delete_obj_ref;
useragent = (char *)(*jenv)->GetStringUTFChars(jenv, juseragent, NULL);
if (!useragent)
goto bad_delete_ref;
ctx->vpninfo = openconnect_vpninfo_new(useragent, validate_peer_cert_cb,
write_new_config_cb, process_auth_form_cb,
progress_cb, ctx);
(*jenv)->ReleaseStringUTFChars(jenv, juseragent, useragent);
if (!ctx->vpninfo)
goto bad_delete_ref;
openconnect_set_token_callbacks(ctx->vpninfo, ctx, lock_token_cb,
unlock_token_cb);
openconnect_set_protect_socket_handler(ctx->vpninfo, protect_socket_cb);
openconnect_set_stats_handler(ctx->vpninfo, stats_cb);
openconnect_set_setup_tun_handler(ctx->vpninfo, setup_tun_cb);
openconnect_set_reconnected_handler(ctx->vpninfo, reconnected_cb);
ctx->cmd_fd = openconnect_setup_cmd_pipe(ctx->vpninfo);
if (ctx->cmd_fd < 0)
goto bad_free_vpninfo;
ctx->loglevel = PRG_DEBUG;
return (jlong)(unsigned long)ctx;
bad_free_vpninfo:
openconnect_vpninfo_free(ctx->vpninfo);
bad_delete_ref:
(*jenv)->DeleteGlobalRef(jenv, ctx->async_lock);
bad_delete_obj_ref:
(*jenv)->DeleteGlobalRef(jenv, ctx->jobj);
bad_free_ctx:
free(ctx);
bad:
OOM(jenv);
return 0;
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_free(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
if (!ctx)
return;
openconnect_vpninfo_free(ctx->vpninfo);
(*jenv)->DeleteGlobalRef(jenv, ctx->async_lock);
(*jenv)->DeleteGlobalRef(jenv, ctx->jobj);
free(ctx);
}
static void write_cmd_pipe(JNIEnv *jenv, jobject jobj, char cmd)
{
struct libctx *ctx = getctx(jenv, jobj);
if (!ctx)
return;
if (write(ctx->cmd_fd, &cmd, 1) < 0) {
/* probably dead already */
}
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_doCancel(
JNIEnv *jenv, jobject jobj)
{
write_cmd_pipe(jenv, jobj, OC_CMD_CANCEL);
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_pause(
JNIEnv *jenv, jobject jobj)
{
write_cmd_pipe(jenv, jobj, OC_CMD_PAUSE);
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_requestStats(
JNIEnv *jenv, jobject jobj)
{
write_cmd_pipe(jenv, jobj, OC_CMD_STATS);
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_globalInit(
JNIEnv *jenv, jclass jcls)
{
openconnect_init_ssl();
}
JNIEXPORT jint JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_obtainCookie(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
if (!ctx)
return 0;
return openconnect_obtain_cookie(ctx->vpninfo);
}
/* special handling: caller-allocated buffer */
JNIEXPORT jstring JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertHash(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
const char *hash;
jstring jresult = NULL;
if (!ctx)
return NULL;
hash = openconnect_get_peer_cert_hash(ctx->vpninfo);
if (!hash)
return NULL;
jresult = dup_to_jstring(ctx->jenv, hash);
if (!jresult)
OOM(ctx->jenv);
return jresult;
}
/* special handling: callee-allocated, caller-freed string */
JNIEXPORT jstring JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertDetails(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
char *buf = NULL;
jstring jresult = NULL;
if (!ctx)
return NULL;
buf = openconnect_get_peer_cert_details(ctx->vpninfo);
if (!buf)
return NULL;
jresult = dup_to_jstring(ctx->jenv, buf);
if (!jresult)
OOM(ctx->jenv);
openconnect_free_cert_info(ctx->vpninfo, buf);
return jresult;
}
/* special handling: callee-allocated, caller-freed binary buffer */
JNIEXPORT jbyteArray JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertDER(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
unsigned char *buf = NULL;
int ret;
jbyteArray jresult = NULL;
if (!ctx)
return NULL;
ret = openconnect_get_peer_cert_DER(ctx->vpninfo, &buf);
if (ret < 0)
return NULL;
jresult = (*ctx->jenv)->NewByteArray(ctx->jenv, ret);
if (jresult)
(*ctx->jenv)->SetByteArrayRegion(ctx->jenv, jresult, 0, ret, (jbyte *) buf);
openconnect_free_cert_info(ctx->vpninfo, buf);
return jresult;
}
/* special handling: callee-allocated, caller-freed binary buffer */
JNIEXPORT jbyteArray JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertChain(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
struct oc_cert *chain = NULL, *p;
int cert_list_size, i;
jobjectArray jresult = NULL;
jclass jcls;
if (!ctx)
goto err;
cert_list_size = openconnect_get_peer_cert_chain(ctx->vpninfo, &chain);
if (cert_list_size <= 0)
goto err;
jcls = (*ctx->jenv)->FindClass(ctx->jenv, "[B");
if (!jcls)
goto err;
jresult = (*ctx->jenv)->NewObjectArray(ctx->jenv, cert_list_size, jcls, NULL);
if (!jresult)
goto err;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
goto err;
for (i = 0, p = chain; i < cert_list_size; i++, p++) {
jbyteArray cert = (*ctx->jenv)->NewByteArray(ctx->jenv, p->der_len);
if (!cert)
goto err2;
(*ctx->jenv)->SetByteArrayRegion(ctx->jenv, cert, 0, p->der_len, (jbyte *)p->der_data);
(*ctx->jenv)->SetObjectArrayElement(ctx->jenv, jresult, i, cert);
}
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
openconnect_free_peer_cert_chain(ctx->vpninfo, chain);
return jresult;
err2:
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
err:
if (jresult)
(*ctx->jenv)->DeleteLocalRef(ctx->jenv, jresult);
if (chain)
openconnect_free_peer_cert_chain(ctx->vpninfo, chain);
return NULL;
}
/* special handling: two string arguments */
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setClientCert(
JNIEnv *jenv, jobject jobj, jstring jcert, jstring jsslkey)
{
struct libctx *ctx = getctx(jenv, jobj);
const char *cert = NULL, *sslkey = NULL;
if (ctx && !get_cstring(ctx->jenv, jcert, &cert) &&
!get_cstring(ctx->jenv, jsslkey, &sslkey))
openconnect_set_client_cert(ctx->vpninfo, cert, sslkey);
release_cstring(ctx->jenv, jcert, cert);
release_cstring(ctx->jenv, jsslkey, sslkey);
return;
}
/* special handling: multiple string arguments */
JNIEXPORT jint JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setupTunDevice(
JNIEnv *jenv, jobject jobj, jstring jarg0, jstring jarg1)
{
struct libctx *ctx = getctx(jenv, jobj);
const char *arg0 = NULL, *arg1 = NULL;
int ret = -ENOMEM;
if (ctx && !get_cstring(ctx->jenv, jarg0, &arg0) &&
!get_cstring(ctx->jenv, jarg1, &arg1))
ret = openconnect_setup_tun_device(ctx->vpninfo, arg0, arg1);
release_cstring(ctx->jenv, jarg0, arg0);
release_cstring(ctx->jenv, jarg1, arg1);
return ret;
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setCSDWrapper(
JNIEnv *jenv, jobject jobj, jstring jarg0, jstring jarg1, jstring jarg2)
{
struct libctx *ctx = getctx(jenv, jobj);
const char *arg0 = NULL, *arg1 = NULL, *arg2 = NULL;
if (ctx &&
!get_cstring(ctx->jenv, jarg0, &arg0) &&
!get_cstring(ctx->jenv, jarg1, &arg1) &&
!get_cstring(ctx->jenv, jarg2, &arg2)) {
openconnect_setup_csd(ctx->vpninfo, getuid(), 1, arg0);
openconnect_set_csd_environ(ctx->vpninfo, "TMPDIR", arg1);
openconnect_set_csd_environ(ctx->vpninfo, "PATH", arg2);
}
release_cstring(ctx->jenv, jarg0, arg0);
release_cstring(ctx->jenv, jarg1, arg1);
release_cstring(ctx->jenv, jarg2, arg2);
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setMobileInfo(
JNIEnv *jenv, jobject jobj, jstring jarg0, jstring jarg1, jstring jarg2)
{
struct libctx *ctx = getctx(jenv, jobj);
const char *arg0 = NULL, *arg1 = NULL, *arg2 = NULL;
if (ctx &&
!get_cstring(ctx->jenv, jarg0, &arg0) &&
!get_cstring(ctx->jenv, jarg1, &arg1) &&
!get_cstring(ctx->jenv, jarg2, &arg2))
openconnect_set_mobile_info(ctx->vpninfo, arg0, arg1, arg2);
release_cstring(ctx->jenv, jarg0, arg0);
release_cstring(ctx->jenv, jarg1, arg1);
release_cstring(ctx->jenv, jarg2, arg2);
}
/* class methods (general library info) */
JNIEXPORT jstring JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getVersion(
JNIEnv *jenv, jclass jcls)
{
return dup_to_jstring(jenv, openconnect_get_version());
}
JNIEXPORT jboolean JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_hasPKCS11Support(
JNIEnv *jenv, jclass jcls)
{
return openconnect_has_pkcs11_support();
}
JNIEXPORT jboolean JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_hasTSSBlobSupport(
JNIEnv *jenv, jclass jcls)
{
return openconnect_has_tss_blob_support();
}
JNIEXPORT jboolean JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_hasStokenSupport(
JNIEnv *jenv, jclass jcls)
{
return openconnect_has_stoken_support();
}
JNIEXPORT jboolean JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_hasOATHSupport(
JNIEnv *jenv, jclass jcls)
{
return openconnect_has_oath_support();
}
JNIEXPORT jboolean JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_hasYubiOATHSupport(
JNIEnv *jenv, jclass jcls)
{
return openconnect_has_yubioath_support();
}
/* simple cases: void or int params */
JNIEXPORT jint JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPort(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
if (!ctx)
return -EINVAL;
return openconnect_get_port(ctx->vpninfo);
}
JNIEXPORT jint JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_passphraseFromFSID(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
if (!ctx)
return -EINVAL;
return openconnect_passphrase_from_fsid(ctx->vpninfo);
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_clearCookie(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
if (!ctx)
return;
openconnect_clear_cookie(ctx->vpninfo);
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_resetSSL(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
if (!ctx)
return;
openconnect_reset_ssl(ctx->vpninfo);
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_disableIPv6(
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
if (!ctx)
return;
openconnect_disable_ipv6(ctx->vpninfo);
}
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setCertExpiryWarning(
JNIEnv *jenv, jobject jobj, jint arg)
{