-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
sslcontext.c
3152 lines (2651 loc) · 109 KB
/
sslcontext.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
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "tcn.h"
#include "apr_thread_rwlock.h"
#include "apr_atomic.h"
#include "ssl_private.h"
#include <stdint.h>
#include "sslcontext.h"
#include "cert_compress.h"
#define SSLCONTEXT_CLASSNAME "io/netty/internal/tcnative/SSLContext"
static jweak sslTask_class_weak;
static jfieldID sslTask_returnValue;
static jfieldID sslTask_complete;
static jweak certificateCallbackTask_class_weak;
static jmethodID certificateCallbackTask_init;
static jweak certificateVerifierTask_class_weak;
static jmethodID certificateVerifierTask_init;
static jweak sslPrivateKeyMethodTask_class_weak;
static jfieldID sslPrivateKeyMethodTask_resultBytes;
static jweak sslPrivateKeyMethodSignTask_class_weak;
static jmethodID sslPrivateKeyMethodSignTask_init;
static jweak sslPrivateKeyMethodDecryptTask_class_weak;
static jmethodID sslPrivateKeyMethodDecryptTask_init;
static const char* staticPackagePrefix = NULL;
extern apr_pool_t *tcn_global_pool;
static apr_status_t ssl_context_cleanup(void *data)
{
tcn_ssl_ctxt_t *c = (tcn_ssl_ctxt_t *)data;
JNIEnv *e = NULL;
if (c != NULL) {
SSL_CTX_free(c->ctx); // this function is safe to call with NULL
c->ctx = NULL;
tcn_get_java_env(&e);
#ifdef OPENSSL_IS_BORINGSSL
if (c->ssl_private_key_method != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->ssl_private_key_method);
}
c->ssl_private_key_method = NULL;
}
if (c->ssl_cert_compression_zlib_algorithm != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->ssl_cert_compression_zlib_algorithm);
}
c->ssl_cert_compression_zlib_algorithm = NULL;
}
if (c->ssl_cert_compression_brotli_algorithm != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->ssl_cert_compression_brotli_algorithm);
}
c->ssl_cert_compression_brotli_algorithm = NULL;
}
if (c->ssl_cert_compression_zstd_algorithm != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->ssl_cert_compression_zstd_algorithm);
}
c->ssl_cert_compression_zstd_algorithm = NULL;
}
if (c->keylog_callback != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->keylog_callback);
}
c->keylog_callback = NULL;
}
c->keylog_callback_method = NULL;
#endif // OPENSSL_IS_BORINGSSL
if (c->ssl_session_cache != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->ssl_session_cache);
}
c->ssl_session_cache = NULL;
}
c->ssl_session_cache_creation_method = NULL;
c->ssl_session_cache_get_method = NULL;
if (c->verifier != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->verifier);
}
c->verifier = NULL;
}
c->verifier_method = NULL;
if (c->cert_requested_callback != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->cert_requested_callback);
}
c->cert_requested_callback = NULL;
}
c->cert_requested_callback_method = NULL;
if (c->certificate_callback != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->certificate_callback);
}
c->certificate_callback = NULL;
}
c->certificate_callback_method = NULL;
if (c->sni_hostname_matcher != NULL) {
if (e != NULL) {
(*e)->DeleteGlobalRef(e, c->sni_hostname_matcher);
}
c->sni_hostname_matcher = NULL;
}
c->sni_hostname_matcher_method = NULL;
if (c->next_proto_data != NULL) {
OPENSSL_free(c->next_proto_data);
c->next_proto_data = NULL;
}
c->next_proto_len = 0;
if (c->alpn_proto_data != NULL) {
OPENSSL_free(c->alpn_proto_data);
c->alpn_proto_data = NULL;
}
c->alpn_proto_len = 0;
apr_thread_rwlock_destroy(c->mutex);
if (c->ticket_keys != NULL) {
OPENSSL_free(c->ticket_keys);
c->ticket_keys = NULL;
}
c->ticket_keys_len = 0;
if (c->password != NULL) {
// Just use free(...) as we used strdup(...) to create the stored password.
free(c->password);
c->password = NULL;
}
}
return APR_SUCCESS;
}
/* Initialize server context */
TCN_IMPLEMENT_CALL(jlong, SSLContext, make)(TCN_STDARGS, jint protocol, jint mode)
{
apr_pool_t *p = NULL;
tcn_ssl_ctxt_t *c = NULL;
SSL_CTX *ctx = NULL;
#ifdef OPENSSL_IS_BORINGSSL
// When using BoringSSL we want to use CRYPTO_BUFFER to reduce memory usage and minimize overhead as we do not need
// X509* at all and just need the raw bytes of the certificates to construct our Java X509Certificate.
//
// See https://github.com/google/boringssl/blob/chromium-stable/PORTING.md#crypto_buffer
ctx = SSL_CTX_new(TLS_with_buffers_method());
// Needed in BoringSSL to be able to use TLSv1.3
//
// See http://hg.nginx.org/nginx/rev/7ad0f4ace359
SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
#elif OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
// TODO this is very hacky as io.netty.handler.ssl.OpenSsl#doesSupportProtocol also uses this method to test for supported protocols. Furthermore
// in OpenSSL 1.1.0 the way protocols are enable/disabled changes
// (SSL_OP_NO_SSLv3,... are deprecated and you should use: https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_set_max_proto_version.html)
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(TLS_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(TLS_server_method());
} else {
ctx = SSL_CTX_new(TLS_method());
}
#else
switch (protocol) {
case SSL_PROTOCOL_TLS:
case SSL_PROTOCOL_ALL:
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(SSLv23_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(SSLv23_server_method());
} else {
ctx = SSL_CTX_new(SSLv23_method());
}
break;
case SSL_PROTOCOL_TLSV1_2:
#ifndef OPENSSL_NO_TLS1
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(TLSv1_2_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(TLSv1_2_server_method());
} else {
ctx = SSL_CTX_new(TLSv1_2_method());
}
#endif
break;
case SSL_PROTOCOL_TLSV1_1:
#ifndef OPENSSL_NO_TLS1
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(TLSv1_1_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(TLSv1_1_server_method());
} else {
ctx = SSL_CTX_new(TLSv1_1_method());
}
#endif
break;
case SSL_PROTOCOL_TLSV1:
#ifndef OPENSSL_NO_TLS1
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(TLSv1_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(TLSv1_server_method());
} else {
ctx = SSL_CTX_new(TLSv1_method());
}
#endif
break;
case SSL_PROTOCOL_SSLV3:
#ifndef OPENSSL_NO_SSL3
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(SSLv3_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(SSLv3_server_method());
} else {
ctx = SSL_CTX_new(SSLv3_method());
}
#endif
break;
case SSL_PROTOCOL_SSLV2:
#ifndef OPENSSL_NO_SSL2
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(SSLv2_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(SSLv2_server_method());
} else {
ctx = SSL_CTX_new(SSLv2_method());
}
#endif
break;
default:
// Try to give the user the highest supported protocol.
#ifndef OPENSSL_NO_TLS1
if (protocol & SSL_PROTOCOL_TLSV1_2) {
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(TLSv1_2_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(TLSv1_2_server_method());
} else {
ctx = SSL_CTX_new(TLSv1_2_method());
}
break;
} else if (protocol & SSL_PROTOCOL_TLSV1_1) {
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(TLSv1_1_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(TLSv1_1_server_method());
} else {
ctx = SSL_CTX_new(TLSv1_1_method());
}
break;
} else if (protocol & SSL_PROTOCOL_TLSV1) {
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(TLSv1_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(TLSv1_server_method());
} else {
ctx = SSL_CTX_new(TLSv1_method());
}
break;
}
#endif
#ifndef OPENSSL_NO_SSL3
if (protocol & SSL_PROTOCOL_SSLV3) {
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(SSLv3_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(SSLv3_server_method());
} else {
ctx = SSL_CTX_new(SSLv3_method());
}
break;
}
#endif
#ifndef OPENSSL_NO_SSL2
if (protocol & SSL_PROTOCOL_SSLV2) {
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(SSLv2_client_method());
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(SSLv2_server_method());
} else {
ctx = SSL_CTX_new(SSLv2_method());
}
break;
}
#endif
tcn_Throw(e, "Unsupported SSL protocol (%d)", protocol);
goto cleanup;
}
#endif /* OPENSSL_IS_BORINGSSL */
if (ctx == NULL) {
char err[ERR_LEN];
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
tcn_Throw(e, "Failed to initialize SSL_CTX (%s)", err);
goto cleanup;
}
TCN_THROW_IF_ERR(apr_pool_create(&p, tcn_global_pool), p);
if ((c = apr_pcalloc(p, sizeof(tcn_ssl_ctxt_t))) == NULL) {
tcn_ThrowAPRException(e, apr_get_os_error());
goto cleanup;
}
c->protocol = protocol;
c->mode = mode;
c->ctx = ctx;
c->pool = p;
if (!(protocol & SSL_PROTOCOL_SSLV2)) {
SSL_CTX_set_options(c->ctx, SSL_OP_NO_SSLv2);
}
if (!(protocol & SSL_PROTOCOL_SSLV3)) {
SSL_CTX_set_options(c->ctx, SSL_OP_NO_SSLv3);
}
if (!(protocol & SSL_PROTOCOL_TLSV1)) {
SSL_CTX_set_options(c->ctx, SSL_OP_NO_TLSv1);
}
#ifdef SSL_OP_NO_TLSv1_1
if (!(protocol & SSL_PROTOCOL_TLSV1_1)) {
SSL_CTX_set_options(c->ctx, SSL_OP_NO_TLSv1_1);
}
#endif
#ifdef SSL_OP_NO_TLSv1_2
if (!(protocol & SSL_PROTOCOL_TLSV1_2)) {
SSL_CTX_set_options(c->ctx, SSL_OP_NO_TLSv1_2);
}
#endif
/*
* Configure additional context ingredients
*/
SSL_CTX_set_options(c->ctx, SSL_OP_SINGLE_DH_USE);
#ifdef HAVE_ECC
SSL_CTX_set_options(c->ctx, SSL_OP_SINGLE_ECDH_USE);
#endif
SSL_CTX_set_options(c->ctx, SSL_OP_NO_COMPRESSION);
/*
* Disallow a session from being resumed during a renegotiation,
* so that an acceptable cipher suite can be negotiated.
*/
SSL_CTX_set_options(c->ctx, SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
/**
* These options may be set by default but can be dangerous in practice [1].
* [1] https://www.openssl.org/docs/man1.0.1/ssl/SSL_CTX_set_options.html
*/
SSL_CTX_clear_options(c->ctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION | SSL_OP_LEGACY_SERVER_CONNECT);
/*
* - Release idle buffers to the SSL_CTX free list
* - Always do retries (which is also the default in BoringSSL) to fix various possible bugs.
* See:
* - https://github.com/openssl/openssl/issues/6234
* - https://github.com/apple/swift-nio-ssl/pull/14
*/
SSL_CTX_set_mode(c->ctx, SSL_MODE_RELEASE_BUFFERS | SSL_MODE_AUTO_RETRY);
/* Default session context id and cache size */
SSL_CTX_sess_set_cache_size(c->ctx, SSL_DEFAULT_CACHE_SIZE);
/* Session cache is disabled by default */
SSL_CTX_set_session_cache_mode(c->ctx, SSL_SESS_CACHE_OFF);
/* Longer session timeout */
SSL_CTX_set_timeout(c->ctx, 14400);
EVP_Digest((const unsigned char *)SSL_DEFAULT_VHOST_NAME,
(unsigned long)((sizeof SSL_DEFAULT_VHOST_NAME) - 1),
&(c->context_id[0]), NULL, EVP_sha1(), NULL);
if (mode) {
#if defined(HAVE_ECC) && (OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER))
/* Set default (nistp256) elliptic curve for ephemeral ECDH keys */
EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
SSL_CTX_set_tmp_ecdh(c->ctx, ecdh);
EC_KEY_free(ecdh);
#endif
#if OPENSSL_VERSION_NUMBER < 0x30000000L
SSL_CTX_set_tmp_dh_callback(c->ctx, tcn_SSL_callback_tmp_DH);
#else
SSL_CTX_set_dh_auto(c->ctx, 1);
#endif
}
// Default depth is 100 and disabled according to https://www.openssl.org/docs/man1.0.2/ssl/SSL_set_verify.html.
c->verify_config.verify_depth = 100;
c->verify_config.verify_mode = SSL_CVERIFY_NONE;
/* Set default password callback */
SSL_CTX_set_default_passwd_cb(c->ctx, (pem_password_cb *) tcn_SSL_password_callback);
SSL_CTX_set_default_passwd_cb_userdata(c->ctx, (void *) c->password);
#if defined(OPENSSL_IS_BORINGSSL)
if (mode != SSL_MODE_SERVER) {
// Set this to make the behaviour consistent with openssl / libressl
SSL_CTX_set_allow_unknown_alpn_protos(ctx, 1);
}
#endif
apr_thread_rwlock_create(&c->mutex, p);
/*
* Let us cleanup the ssl context when the pool is destroyed
*/
apr_pool_cleanup_register(p, (const void *)c,
ssl_context_cleanup,
apr_pool_cleanup_null);
tcn_SSL_CTX_set_app_state(c->ctx, c);
return P2J(c);
cleanup:
if (p != NULL) {
apr_pool_destroy(p);
}
SSL_CTX_free(ctx); // this function is safe to call with NULL.
return 0;
}
TCN_IMPLEMENT_CALL(jint, SSLContext, free)(TCN_STDARGS, jlong ctx)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, 0);
/* Run and destroy the cleanup callback */
int result = apr_pool_cleanup_run(c->pool, c, ssl_context_cleanup);
apr_pool_destroy(c->pool);
return result;
}
TCN_IMPLEMENT_CALL(void, SSLContext, setContextId)(TCN_STDARGS, jlong ctx,
jstring id)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, /* void */);
TCN_ALLOC_CSTRING(id);
if (J2S(id)) {
EVP_Digest((const unsigned char *)J2S(id),
(unsigned long)strlen(J2S(id)),
&(c->context_id[0]), NULL, EVP_sha1(), NULL);
}
TCN_FREE_CSTRING(id);
}
TCN_IMPLEMENT_CALL(void, SSLContext, setOptions)(TCN_STDARGS, jlong ctx,
jint opt)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, /* void */);
SSL_CTX_set_options(c->ctx, opt);
}
TCN_IMPLEMENT_CALL(jint, SSLContext, getOptions)(TCN_STDARGS, jlong ctx)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, 0);
return SSL_CTX_get_options(c->ctx);
}
TCN_IMPLEMENT_CALL(void, SSLContext, clearOptions)(TCN_STDARGS, jlong ctx,
jint opt)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, /* void */);
SSL_CTX_clear_options(c->ctx, opt);
}
TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCipherSuite)(TCN_STDARGS, jlong ctx,
jstring ciphers, jboolean tlsv13)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
jboolean rv = JNI_TRUE;
TCN_CHECK_NULL(c, ctx, JNI_FALSE);
#ifdef OPENSSL_NO_TLS1_3
if (tlsv13 == JNI_TRUE) {
tcn_Throw(e, "TLSv1.3 not supported");
return JNI_FALSE;
}
#endif
if (ciphers == NULL || (*e)->GetStringUTFLength(e, ciphers) == 0) {
return JNI_FALSE;
}
TCN_ALLOC_CSTRING(ciphers);
if (!J2S(ciphers)) {
return JNI_FALSE;
}
#ifdef OPENSSL_NO_TLS1_3
rv = SSL_CTX_set_cipher_list(c->ctx, J2S(ciphers)) == 0 ? JNI_FALSE : JNI_TRUE;
#else
if (tlsv13 == JNI_TRUE) {
#ifdef OPENSSL_IS_BORINGSSL
// BoringSSL does not support setting TLSv1.3 cipher suites explicit for now.
rv = JNI_TRUE;
#else
rv = SSL_CTX_set_ciphersuites(c->ctx, J2S(ciphers)) == 0 ? JNI_FALSE : JNI_TRUE;
#endif // OPENSSL_IS_BORINGSSL
} else {
rv = SSL_CTX_set_cipher_list(c->ctx, J2S(ciphers)) == 0 ? JNI_FALSE : JNI_TRUE;
}
#endif // OPENSSL_NO_TLS1_3
if (rv == JNI_FALSE) {
char err[ERR_LEN];
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
tcn_Throw(e, "Unable to configure permitted SSL ciphers (%s)", err);
}
TCN_FREE_CSTRING(ciphers);
return rv;
}
TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCertificateChainFile)(TCN_STDARGS, jlong ctx,
jstring file,
jboolean skipfirst)
{
#ifdef OPENSSL_IS_BORINGSSL
tcn_Throw(e, "Not supported using BoringSSL");
return JNI_FALSE;
#else
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
jboolean rv = JNI_FALSE;
TCN_CHECK_NULL(c, ctx, JNI_FALSE);
TCN_ALLOC_CSTRING(file);
if (!J2S(file)) {
return JNI_FALSE;
}
if (tcn_SSL_CTX_use_certificate_chain(c->ctx, J2S(file), skipfirst) > 0) {
rv = JNI_TRUE;
}
TCN_FREE_CSTRING(file);
return rv;
#endif // OPENSSL_IS_BORINGSSL
}
TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCertificateChainBio)(TCN_STDARGS, jlong ctx,
jlong chain,
jboolean skipfirst)
{
#ifdef OPENSSL_IS_BORINGSSL
tcn_Throw(e, "Not supported using BoringSSL");
return JNI_FALSE;
#else
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
BIO *b = J2P(chain, BIO *);
TCN_CHECK_NULL(c, ctx, JNI_FALSE);
if (b == NULL) {
return JNI_FALSE;
}
if (tcn_SSL_CTX_use_certificate_chain_bio(c->ctx, b, skipfirst) > 0) {
return JNI_TRUE;
}
return JNI_FALSE;
#endif // OPENSSL_IS_BORINGSSL
}
TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCACertificateBio)(TCN_STDARGS, jlong ctx, jlong certs)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, JNI_FALSE);
BIO *b = J2P(certs, BIO *);
return b != NULL && c->mode != SSL_MODE_CLIENT && tcn_SSL_CTX_use_client_CA_bio(c->ctx, b) > 0 ? JNI_TRUE : JNI_FALSE;
}
TCN_IMPLEMENT_CALL(jboolean, SSLContext, setNumTickets)(TCN_STDARGS, jlong ctx, jint num)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, JNI_FALSE);
#ifdef OPENSSL_IS_BORINGSSL
// Not supported by BoringSSL
return JNI_FALSE;
#else
// Only supported with GCC
#if defined(__GNUC__) || defined(__GNUG__)
if (!SSL_CTX_set_num_tickets) {
return JNI_FALSE;
}
#endif
// We can only support it when either use openssl version >= 1.1.1 or GCC as this way we can use weak linking
#if OPENSSL_VERSION_NUMBER >= 0x10101000L || defined(__GNUC__) || defined(__GNUG__)
return SSL_CTX_set_num_tickets(c->ctx, num) > 0 ? JNI_TRUE : JNI_FALSE;
#else
return JNI_FALSE;
#endif
#endif
}
TCN_IMPLEMENT_CALL(void, SSLContext, setTmpDHLength)(TCN_STDARGS, jlong ctx, jint length)
{
#if OPENSSL_VERSION_NUMBER < 0x30000000L
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, /* void */);
switch (length) {
case 512:
SSL_CTX_set_tmp_dh_callback(c->ctx, tcn_SSL_callback_tmp_DH_512);
return;
case 1024:
SSL_CTX_set_tmp_dh_callback(c->ctx, tcn_SSL_callback_tmp_DH_1024);
return;
case 2048:
SSL_CTX_set_tmp_dh_callback(c->ctx, tcn_SSL_callback_tmp_DH_2048);
return;
case 4096:
SSL_CTX_set_tmp_dh_callback(c->ctx, tcn_SSL_callback_tmp_DH_4096);
return;
default:
tcn_Throw(e, "Unsupported length %s", length);
return;
}
#endif // OPENSSL_VERSION_NUMBER < 0x30000000L
}
#ifndef OPENSSL_IS_BORINGSSL
static EVP_PKEY *load_pem_key(tcn_ssl_ctxt_t *c, const char *file)
{
BIO *bio = NULL;
EVP_PKEY *key = NULL;
if ((bio = BIO_new(BIO_s_file())) == NULL) {
return NULL;
}
if (BIO_read_filename(bio, file) <= 0) {
BIO_free(bio);
return NULL;
}
key = PEM_read_bio_PrivateKey(bio, NULL, (pem_password_cb *) tcn_SSL_password_callback, (void *)c->password);
BIO_free(bio);
return key;
}
static X509 *load_pem_cert(tcn_ssl_ctxt_t *c, const char *file)
{
BIO *bio = NULL;
X509 *cert = NULL;
if ((bio = BIO_new(BIO_s_file())) == NULL) {
return NULL;
}
if (BIO_read_filename(bio, file) <= 0) {
BIO_free(bio);
return NULL;
}
cert = PEM_read_bio_X509_AUX(bio, NULL,
(pem_password_cb *) tcn_SSL_password_callback,
(void *)c->password);
if (cert == NULL &&
(ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE)) {
ERR_clear_error();
BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
cert = d2i_X509_bio(bio, NULL);
}
BIO_free(bio);
return cert;
}
static int ssl_load_pkcs12(tcn_ssl_ctxt_t *c, const char *file,
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
{
const char *pass = NULL;
char buff[PEM_BUFSIZE];
int len, rc = 0;
PKCS12 *p12 = NULL;
BIO *in = NULL;
if ((in = BIO_new(BIO_s_file())) == 0) {
return 0;
}
if (BIO_read_filename(in, file) <= 0) {
BIO_free(in);
return 0;
}
p12 = d2i_PKCS12_bio(in, 0);
if (p12 == 0) {
/* Error loading PKCS12 file */
goto cleanup;
}
/* See if an empty password will do */
if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, 0, 0)) {
pass = "";
} else {
len = tcn_SSL_password_callback(buff, PEM_BUFSIZE, 0, (void *) c->password);
if (len < 0) {
/* Passpharse callback error */
goto cleanup;
}
if (!PKCS12_verify_mac(p12, buff, len)) {
/* Mac verify error (wrong password?) in PKCS12 file */
goto cleanup;
}
pass = buff;
}
rc = PKCS12_parse(p12, pass, pkey, cert, ca);
cleanup:
if (p12 != 0) {
PKCS12_free(p12);
}
BIO_free(in);
return rc;
}
static void free_and_reset_pass(tcn_ssl_ctxt_t *c, char* old_password, const jboolean rv) {
if (!rv) {
if (c->password != NULL) {
free(c->password);
c->password = NULL;
}
// Restore old password
c->password = old_password;
} else if (old_password != NULL) {
free(old_password);
}
}
#endif // OPENSSL_IS_BORINGSSL
TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCertificate)(TCN_STDARGS, jlong ctx,
jstring cert, jstring key,
jstring password)
{
#ifdef OPENSSL_IS_BORINGSSL
tcn_Throw(e, "Not supported using BoringSSL");
return JNI_FALSE;
#else
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, JNI_FALSE);
jboolean rv = JNI_TRUE;
TCN_ALLOC_CSTRING(cert);
TCN_ALLOC_CSTRING(key);
TCN_ALLOC_CSTRING(password);
EVP_PKEY *pkey = NULL;
X509 *xcert = NULL;
const char *key_file = NULL;
const char *cert_file = NULL;
const char *p = NULL;
char *old_password = NULL;
char err[ERR_LEN];
if (J2S(password)) {
old_password = c->password;
c->password = strdup(cpassword);
if (c->password == NULL) {
rv = JNI_FALSE;
goto cleanup;
}
}
key_file = J2S(key);
cert_file = J2S(cert);
if (!key_file) {
key_file = cert_file;
}
if (!key_file || !cert_file) {
tcn_Throw(e, "No Certificate file specified or invalid file format");
rv = JNI_FALSE;
goto cleanup;
}
if ((p = strrchr(cert_file, '.')) != NULL && strcmp(p, ".pkcs12") == 0) {
if (!ssl_load_pkcs12(c, cert_file, &pkey, &xcert, 0)) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
tcn_Throw(e, "Unable to load certificate %s (%s)",
cert_file, err);
rv = JNI_FALSE;
goto cleanup;
}
} else {
if ((pkey = load_pem_key(c, key_file)) == NULL) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
tcn_Throw(e, "Unable to load certificate key %s (%s)",
key_file, err);
rv = JNI_FALSE;
goto cleanup;
}
if ((xcert = load_pem_cert(c, cert_file)) == NULL) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
tcn_Throw(e, "Unable to load certificate %s (%s)",
cert_file, err);
rv = JNI_FALSE;
goto cleanup;
}
}
if (SSL_CTX_use_certificate(c->ctx, xcert) <= 0) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
tcn_Throw(e, "Error setting certificate (%s)", err);
rv = JNI_FALSE;
goto cleanup;
}
if (SSL_CTX_use_PrivateKey(c->ctx, pkey) <= 0) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
tcn_Throw(e, "Error setting private key (%s)", err);
rv = JNI_FALSE;
goto cleanup;
}
if (SSL_CTX_check_private_key(c->ctx) <= 0) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
tcn_Throw(e, "Private key does not match the certificate public key (%s)",
err);
rv = JNI_FALSE;
goto cleanup;
}
cleanup:
TCN_FREE_CSTRING(cert);
TCN_FREE_CSTRING(key);
TCN_FREE_CSTRING(password);
EVP_PKEY_free(pkey); // this function is safe to call with NULL
X509_free(xcert); // this function is safe to call with NULL
free_and_reset_pass(c, old_password, rv);
return rv;
#endif // OPENSSL_IS_BORINGSSL
}
TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCertificateBio)(TCN_STDARGS, jlong ctx,
jlong cert, jlong key,
jstring password)
{
#ifdef OPENSSL_IS_BORINGSSL
tcn_Throw(e, "Not supported using BoringSSL");
return JNI_FALSE;
#else
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, JNI_FALSE);
BIO *cert_bio = J2P(cert, BIO *);
BIO *key_bio = J2P(key, BIO *);
EVP_PKEY *pkey = NULL;
X509 *xcert = NULL;
jboolean rv = JNI_TRUE;
TCN_ALLOC_CSTRING(password);
char *old_password = NULL;
char err[ERR_LEN];
if (J2S(password)) {
old_password = c->password;
c->password = strdup(cpassword);
if (c->password == NULL) {
rv = JNI_FALSE;
goto cleanup;
}
}
if (!key) {
key = cert;
}
if (!cert || !key) {
tcn_Throw(e, "No Certificate file specified or invalid file format");
rv = JNI_FALSE;
goto cleanup;
}
if ((pkey = tcn_load_pem_key_bio(c->password, key_bio)) == NULL) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
ERR_clear_error();
tcn_Throw(e, "Unable to load certificate key (%s)",err);
rv = JNI_FALSE;
goto cleanup;
}
if ((xcert = tcn_load_pem_cert_bio(c->password, cert_bio)) == NULL) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
ERR_clear_error();
tcn_Throw(e, "Unable to load certificate (%s) ", err);
rv = JNI_FALSE;
goto cleanup;
}
if (SSL_CTX_use_certificate(c->ctx, xcert) <= 0) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
ERR_clear_error();
tcn_Throw(e, "Error setting certificate (%s)", err);
rv = JNI_FALSE;
goto cleanup;
}
if (SSL_CTX_use_PrivateKey(c->ctx, pkey) <= 0) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
ERR_clear_error();
tcn_Throw(e, "Error setting private key (%s)", err);
rv = JNI_FALSE;
goto cleanup;
}
if (SSL_CTX_check_private_key(c->ctx) <= 0) {
ERR_error_string_n(ERR_get_error(), err, ERR_LEN);
ERR_clear_error();
tcn_Throw(e, "Private key does not match the certificate public key (%s)",
err);
rv = JNI_FALSE;
goto cleanup;
}
cleanup:
TCN_FREE_CSTRING(password);
EVP_PKEY_free(pkey); // this function is safe to call with NULL
X509_free(xcert); // this function is safe to call with NULL
free_and_reset_pass(c, old_password, rv);
return rv;
#endif // OPENSSL_IS_BORINGSSL
}
TCN_IMPLEMENT_CALL(void, SSLContext, setNpnProtos0)(TCN_STDARGS, jlong ctx, jbyteArray next_protos,
jint selectorFailureBehavior)
{
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
TCN_CHECK_NULL(c, ctx, /* void */);
if (next_protos != NULL) {
OPENSSL_free(c->next_proto_data);
int next_protos_len = (*e)->GetArrayLength(e, next_protos);
c->next_proto_data = OPENSSL_malloc(next_protos_len);
c->next_proto_len = next_protos_len;
(*e)->GetByteArrayRegion(e, next_protos, 0, next_protos_len, (jbyte*) c->next_proto_data);
// depending on if it's client mode or not we need to call different functions.
if (c->mode == SSL_MODE_CLIENT) {
SSL_CTX_set_next_proto_select_cb(c->ctx, tcn_SSL_callback_select_next_proto, (void *)c);
} else {
SSL_CTX_set_next_protos_advertised_cb(c->ctx, tcn_SSL_callback_next_protos, (void *)c);
}
}
}
TCN_IMPLEMENT_CALL(void, SSLContext, setAlpnProtos0)(TCN_STDARGS, jlong ctx, jbyteArray alpn_protos,