Skip to content

Latest commit

 

History

History
1515 lines (1240 loc) · 38.6 KB

jni.c

File metadata and controls

1515 lines (1240 loc) · 38.6 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2013 Kevin Cernekee <cernekee@gmail.com>
*
* 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.
*/
Jul 1, 2014
Jul 1, 2014
16
17
#include <config.h>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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;
Apr 26, 2014
Apr 26, 2014
33
jobject async_lock;
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
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;
}
Oct 9, 2014
Oct 9, 2014
83
static int get_cstring(JNIEnv *jenv, jstring in, const char **out)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
{
const char *tmp;
if (in == NULL) {
*out = NULL;
return 0;
}
tmp = (*jenv)->GetStringUTFChars(jenv, in, NULL);
if (!tmp) {
OOM(jenv);
return -1;
}
Oct 9, 2014
Oct 9, 2014
98
*out = tmp;
99
100
101
return 0;
}
Oct 9, 2014
Oct 9, 2014
102
103
104
105
106
107
static void release_cstring(JNIEnv *jenv, jstring jstr, const char *cstr)
{
if (cstr)
(*jenv)->ReleaseStringUTFChars(jenv, jstr, cstr);
}
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
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;
}
Nov 3, 2014
Nov 3, 2014
197
static int validate_peer_cert_cb(void *privdata, const char *reason)
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{
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;
}
Oct 9, 2014
Oct 9, 2014
220
static int write_new_config_cb(void *privdata, const char *buf, int buflen)
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
{
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);
}
Mar 8, 2016
Mar 8, 2016
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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);
}
May 6, 2016
May 6, 2016
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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);
}
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
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) ||
Oct 9, 2014
Oct 9, 2014
422
set_string(ctx, jopt, "value", opt->_value) ||
423
424
425
426
427
428
429
430
431
432
433
434
435
436
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;
Oct 9, 2014
Oct 9, 2014
437
/* opt->_value is NOT a caller-allocated string for OC_FORM_OPT_SELECT */
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
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)
Oct 9, 2014
Oct 9, 2014
496
opt->_value = lookup_choice_name((void *)opt, tmp);
497
else {
Oct 9, 2014
Oct 9, 2014
498
499
500
free(opt->_value);
opt->_value = strdup(tmp);
if (!opt->_value)
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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;
}
Oct 29, 2014
Oct 29, 2014
515
516
static void __attribute__ ((format(printf, 3, 4)))
progress_cb(void *privdata, int level, const char *fmt, ...)
517
518
519
520
521
{
struct libctx *ctx = privdata;
va_list ap;
char *msg;
jstring jmsg;
Apr 26, 2014
Apr 26, 2014
522
int ret, loglevel;
523
524
jmethodID mid;
Apr 26, 2014
Apr 26, 2014
525
526
527
528
529
(*ctx->jenv)->MonitorEnter(ctx->jenv, ctx->async_lock);
loglevel = ctx->loglevel;
(*ctx->jenv)->MonitorExit(ctx->jenv, ctx->async_lock);
if (level > loglevel)
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
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);
}
Aug 12, 2014
Aug 12, 2014
557
558
559
560
561
562
563
564
565
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;
Aug 17, 2014
Aug 17, 2014
566
567
568
mid = get_obj_mid(ctx, ctx->jobj, "onTokenLock", "()I");
if (mid)
ret = (*ctx->jenv)->CallIntMethod(ctx->jenv, ctx->jobj, mid);
Aug 12, 2014
Aug 12, 2014
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
(*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;
}
597
598
/* Library init/uninit */
Apr 26, 2014
Apr 26, 2014
599
600
601
602
603
604
605
606
607
608
609
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;
}
610
611
612
613
614
615
616
617
618
619
620
621
622
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;
Apr 26, 2014
Apr 26, 2014
623
624
625
ctx->async_lock = init_async_lock(ctx);
if (!ctx->async_lock)
goto bad_delete_obj_ref;
626
627
628
629
630
631
632
633
634
635
636
637
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;
Aug 12, 2014
Aug 12, 2014
638
639
openconnect_set_token_callbacks(ctx->vpninfo, ctx, lock_token_cb,
unlock_token_cb);
640
641
openconnect_set_protect_socket_handler(ctx->vpninfo, protect_socket_cb);
openconnect_set_stats_handler(ctx->vpninfo, stats_cb);
Mar 8, 2016
Mar 8, 2016
642
openconnect_set_setup_tun_handler(ctx->vpninfo, setup_tun_cb);
May 6, 2016
May 6, 2016
643
openconnect_set_reconnected_handler(ctx->vpninfo, reconnected_cb);
644
645
646
647
648
649
650
651
652
653
654
655
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:
Apr 26, 2014
Apr 26, 2014
656
657
(*jenv)->DeleteGlobalRef(jenv, ctx->async_lock);
bad_delete_obj_ref:
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
(*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);
Apr 26, 2014
Apr 26, 2014
674
(*jenv)->DeleteGlobalRef(jenv, ctx->async_lock);
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
(*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;
Nov 3, 2014
Nov 3, 2014
721
return openconnect_obtain_cookie(ctx->vpninfo);
722
723
724
}
/* special handling: caller-allocated buffer */
Nov 6, 2014
Nov 6, 2014
725
JNIEXPORT jstring JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertHash(
726
727
728
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
Nov 3, 2014
Nov 3, 2014
729
const char *hash;
730
731
jstring jresult = NULL;
Nov 3, 2014
Nov 3, 2014
732
if (!ctx)
733
return NULL;
Nov 3, 2014
Nov 3, 2014
734
735
hash = openconnect_get_peer_cert_hash(ctx->vpninfo);
if (!hash)
736
return NULL;
Nov 3, 2014
Nov 3, 2014
737
jresult = dup_to_jstring(ctx->jenv, hash);
738
739
740
741
742
743
if (!jresult)
OOM(ctx->jenv);
return jresult;
}
/* special handling: callee-allocated, caller-freed string */
Nov 6, 2014
Nov 6, 2014
744
JNIEXPORT jstring JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertDetails(
745
746
747
748
749
750
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
char *buf = NULL;
jstring jresult = NULL;
Nov 3, 2014
Nov 3, 2014
751
if (!ctx)
752
return NULL;
Nov 3, 2014
Nov 3, 2014
753
buf = openconnect_get_peer_cert_details(ctx->vpninfo);
754
755
756
757
758
759
760
if (!buf)
return NULL;
jresult = dup_to_jstring(ctx->jenv, buf);
if (!jresult)
OOM(ctx->jenv);
Oct 27, 2014
Oct 27, 2014
761
openconnect_free_cert_info(ctx->vpninfo, buf);
762
763
764
765
return jresult;
}
/* special handling: callee-allocated, caller-freed binary buffer */
Nov 6, 2014
Nov 6, 2014
766
JNIEXPORT jbyteArray JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_getPeerCertDER(
767
768
769
770
771
772
773
JNIEnv *jenv, jobject jobj)
{
struct libctx *ctx = getctx(jenv, jobj);
unsigned char *buf = NULL;
int ret;
jbyteArray jresult = NULL;
Nov 3, 2014
Nov 3, 2014
774
if (!ctx)
775
return NULL;
Nov 3, 2014
Nov 3, 2014
776
ret = openconnect_get_peer_cert_DER(ctx->vpninfo, &buf);
777
778
779
780
781
782
783
if (ret < 0)
return NULL;
jresult = (*ctx->jenv)->NewByteArray(ctx->jenv, ret);
if (jresult)
(*ctx->jenv)->SetByteArrayRegion(ctx->jenv, jresult, 0, ret, (jbyte *) buf);
Oct 27, 2014
Oct 27, 2014
784
openconnect_free_cert_info(ctx->vpninfo, buf);
785
786
787
return jresult;
}
May 6, 2016
May 6, 2016
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
/* 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;
}
837
838
839
840
841
/* 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);
Oct 9, 2014
Oct 9, 2014
842
const char *cert = NULL, *sslkey = NULL;
843
Oct 9, 2019
Oct 9, 2019
844
845
846
847
if (!ctx)
return;
if (!get_cstring(ctx->jenv, jcert, &cert) &&
Oct 9, 2014
Oct 9, 2014
848
849
!get_cstring(ctx->jenv, jsslkey, &sslkey))
openconnect_set_client_cert(ctx->vpninfo, cert, sslkey);
850
Oct 9, 2014
Oct 9, 2014
851
852
853
release_cstring(ctx->jenv, jcert, cert);
release_cstring(ctx->jenv, jsslkey, sslkey);
return;
854
855
856
857
858
859
860
}
/* 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);
Oct 9, 2014
Oct 9, 2014
861
862
863
const char *arg0 = NULL, *arg1 = NULL;
int ret = -ENOMEM;
Oct 9, 2019
Oct 9, 2019
864
865
866
867
if (!ctx)
return -EINVAL;
if (!get_cstring(ctx->jenv, jarg0, &arg0) &&
Oct 9, 2014
Oct 9, 2014
868
869
870
871
872
873
!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;
874
875
876
877
878
879
}
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);
Oct 9, 2014
Oct 9, 2014
880
const char *arg0 = NULL, *arg1 = NULL, *arg2 = NULL;
Mar 19, 2014
Mar 19, 2014
881
Oct 9, 2019
Oct 9, 2019
882
883
884
885
if (!ctx)
return;
if (!get_cstring(ctx->jenv, jarg0, &arg0) &&
Oct 9, 2014
Oct 9, 2014
886
887
888
889
!get_cstring(ctx->jenv, jarg1, &arg1) &&
!get_cstring(ctx->jenv, jarg2, &arg2)) {
openconnect_setup_csd(ctx->vpninfo, getuid(), 1, arg0);
Aug 5, 2018
Aug 5, 2018
890
891
if (arg1) openconnect_set_csd_environ(ctx->vpninfo, "TMPDIR", arg1);
if (arg2) openconnect_set_csd_environ(ctx->vpninfo, "PATH", arg2);
Oct 9, 2014
Oct 9, 2014
892
}
Mar 19, 2014
Mar 19, 2014
893
Oct 9, 2014
Oct 9, 2014
894
895
896
release_cstring(ctx->jenv, jarg0, arg0);
release_cstring(ctx->jenv, jarg1, arg1);
release_cstring(ctx->jenv, jarg2, arg2);
897
898
899
900
901
902
}
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);
Oct 9, 2014
Oct 9, 2014
903
904
const char *arg0 = NULL, *arg1 = NULL, *arg2 = NULL;
Oct 9, 2019
Oct 9, 2019
905
906
907
908
if (!ctx)
return;
if (!get_cstring(ctx->jenv, jarg0, &arg0) &&
Oct 9, 2014
Oct 9, 2014
909
910
911
912
913
914
915
!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);
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
}
/* 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();
}
Oct 11, 2018
Oct 11, 2018
938
939
940
941
942
943
JNIEXPORT jboolean JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_hasTSS2BlobSupport(
JNIEnv *jenv, jclass jcls)
{
return openconnect_has_tss2_blob_support();
}
944
945
946
947
948
949
950
951
952
953
954
955
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();
}
Nov 17, 2014
Nov 17, 2014
956
957
958
959
960
961
JNIEXPORT jboolean JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_hasYubiOATHSupport(
JNIEnv *jenv, jclass jcls)
{
return openconnect_has_yubioath_support();
}
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
/* 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;