forked from wolfSSL/wolfssljni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWolfSSL.java
More file actions
1959 lines (1740 loc) · 74.3 KB
/
WolfSSL.java
File metadata and controls
1959 lines (1740 loc) · 74.3 KB
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
/* WolfSSL.java
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package com.wolfssl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
/**
* Base class which wraps the native WolfSSL embedded SSL library.
* This class contains library init and cleanup methods, general callback
* methods, as well as error codes and general wolfSSL codes.
*
* @author wolfSSL
*/
public class WolfSSL {
/* If this enum is changed, also change switch statement cases in
* ./native/com_wolfssl_WolfSSL.c,
* Java_com_wolfssl_WolfSSL_getAvailableCipherSuitesIana() */
/** TLS protocol versions */
public enum TLS_VERSION {
/** invalid TLS version */
INVALID,
/** TLS 1.0 */
TLSv1,
/** TLS 1.1 */
TLSv1_1,
/** TLS 1.2 */
TLSv1_2,
/** TLS 1.3 */
TLSv1_3,
/** Downgrade starting from highest supported SSL/TLS version */
SSLv23,
/** DTLS 1.0 */
DTLSv1,
/** DTLS 1.2 */
DTLSv1_2,
/** DTLS 1.3 */
DTLSv1_3
}
/* ------------------ wolfSSL JNI error codes ----------------------- */
/** Session unavailable */
public static final int JNI_SESSION_UNAVAILABLE = -10001;
/**
* Socket select/poll() failed, matches com_wolfssl_WolfSSLSession.c
* socketSelect() and socketPoll() return value.
*/
public static final int WOLFJNI_IO_EVENT_FAIL = -10;
/**
* Socket timed out, matches com_wolfssl_WolfSSLSession.c
* socketSelect() and socketPoll() return value.
*/
public static final int WOLFJNI_IO_EVENT_TIMEOUT = -11;
/**
* Socket poll() exceptional error, matches com_wolfssl_WolfSSLSession.c
* socketPoll() return value */
public static final int WOLFJNI_IO_EVENT_ERROR = -14;
/**
* Socket file descriptor closed, matches com_wolfssl_WolfSSLSession.c
* socketPoll() return value */
public static final int WOLFJNI_IO_EVENT_FD_CLOSED = -15;
/**
* Socket disconnected during poll(), matches
* com_wolfssl_WolfSSLSession.c socketPoll() return value */
public static final int WOLFJNI_IO_EVENT_POLLHUP = -16;
/**
* Socket invalid timeout during poll/select(), matches
* com_wolfssl_WolfSSLSession.c socketPoll/socketSelect() return value */
public static final int WOLFJNI_IO_EVENT_INVALID_TIMEOUT = -17;
/* ----------------------- wolfSSL codes ---------------------------- */
/** Error code: no error */
public static final int SSL_ERROR_NONE = 0;
/** Error code: failure */
public static final int SSL_FAILURE = 0;
/** Error code: success */
public static final int SSL_SUCCESS = 1;
/** Error code: TLS shutdown not done */
public static final int SSL_SHUTDOWN_NOT_DONE = 2;
/** Error code: bad certificate */
public static final int SSL_BAD_CERTTYPE = -8;
/** Error code: bad file stat */
public static final int SSL_BAD_STAT = -7;
/** Error code: bad path */
public static final int SSL_BAD_PATH = -6;
/** Error code: bad file type */
public static final int SSL_BAD_FILETYPE = -5;
/** Error code: bad file */
public static final int SSL_BAD_FILE = -4;
/** Error code: not implemented */
public static final int SSL_NOT_IMPLEMENTED = -3;
/** Error code: unknown */
public static final int SSL_UNKNOWN = -2;
/** Error code: fatal error */
public static final int SSL_FATAL_ERROR = -1;
/** wolfSSL file type: ASN.1/DER */
public static final int SSL_FILETYPE_ASN1 = 2;
/** wolfSSL file type: PEM */
public static final int SSL_FILETYPE_PEM = 1;
/** ASN1 */
public static final int SSL_FILETYPE_DEFAULT = 2;
/** NTRU raw key blog */
public static final int SSL_FILETYPE_RAW = 3;
/**
* Verification mode for peer certificates.
* <p>
* <b>Client mode:</b> the client will not verify the certificate
* received from the server and the handshake will continue as normal.
* <br>
* <b>Server mode:</b> the server will not send a certificate request
* to the client. As such, client verification will not be enabled.
*
* @see WolfSSLContext#setVerify(long, int, WolfSSLVerifyCallback)
*/
public static final int SSL_VERIFY_NONE = 0;
/**
* Verification mode for peer certificates.
* <p>
* <b>Client mode:</b> the client will verify the certificate received
* from the server during the handshake. This is turned on by default
* in wolfSSL, therefore, using this option has no effect.
* <br>
* <b>Server mode:</b> the server will send a certificate request to the
* client and verify the client certificate which is received.
*
* @see WolfSSLContext#setVerify(long, int, WolfSSLVerifyCallback)
*/
public static final int SSL_VERIFY_PEER = 1;
/**
* Verification mode for peer certificates.
* <p>
* <b>Client mode:</b> no effect when used on the client side.
* <br>
* <b>Server mode:</b> the verification will fail on the server side
* if the client fails to send a certificate when requested to do so
* (when using SSL_VERIFY_PEER on the SSL server).
*
* @see WolfSSLContext#setVerify(long, int, WolfSSLVerifyCallback)
*/
public static final int SSL_VERIFY_FAIL_IF_NO_PEER_CERT = 2;
/**
* Verification mode for peer certificates.
* Currently not supported by native wolfSSL.
*
* @see WolfSSLContext#setVerify(long, int, WolfSSLVerifyCallback)
*/
public static final int SSL_VERIFY_CLIENT_ONCE = 4;
/** Disable session cache */
public static final int SSL_SESS_CACHE_OFF = 30;
/** currently unused */
public static final int SSL_SESS_CACHE_CLIENT = 31;
/** Native session cache mode: server */
public static final int SSL_SESS_CACHE_SERVER = 32;
/** currently unused */
public static final int SSL_SESS_CACHE_BOTH = 33;
/** Native session cache mode: auto flush */
public static final int SSL_SESS_CACHE_NO_AUTO_CLEAR = 34;
/** currently unused */
public static final int SSL_SESS_CACHE_NO_INTERNAL_LOOKUP = 35;
/** I/O read would block, wolfSSL needs more data */
public static final int SSL_ERROR_WANT_READ = 2;
/** I/O send would block, wolfSSL needs to write data */
public static final int SSL_ERROR_WANT_WRITE = 3;
/** currently unused */
public static final int SSL_ERROR_WANT_CONNECT = 7;
/** currently unused */
public static final int SSL_ERROR_WANT_ACCEPT = 8;
/** Error with underlying I/O */
public static final int SSL_ERROR_SYSCALL = 5;
/** I/O operation should be called again when client cert is available */
public static final int SSL_ERROR_WANT_X509_LOOKUP = 83;
/** I/O error, zero return, no more data */
public static final int SSL_ERROR_ZERO_RETURN = 6;
/** General SSL error */
public static final int SSL_ERROR_SSL = 85;
/** Error state on socket */
public static final int SOCKET_ERROR_E = -308;
/** Received fatal alert error */
public static final int FATAL_ERROR = -313;
/** Out of order message */
public static final int OUT_OF_ORDER_E = -373;
/** Peer closed socket */
public static final int SSL_ERROR_SOCKET_PEER_CLOSED = -397;
/** Unrecognized ALPN protocol name */
public static final int UNKNOWN_ALPN_PROTOCOL_NAME_E = -405;
/** DTLS application data ready for read */
public static final int APP_DATA_READY = -441;
/* extra definitions from ssl.h */
/** CertManager: check all cert CRLs */
public static final int WOLFSSL_CRL_CHECKALL = 1;
/** CertManager: use override URL instead of URL in certificates */
public static final int WOLFSSL_OCSP_URL_OVERRIDE = 1;
/** CertManager: disable sending OCSP nonce */
public static final int WOLFSSL_OCSP_NO_NONCE = 2;
/* ALPN definitions from ssl.h */
/** ALPN: no match found */
public static final int WOLFSSL_ALPN_NO_MATCH = 0;
/** ALPN: found match */
public static final int WOLFSSL_ALPN_MATCH = 1;
/** ALPN: continue on protocol mismatch */
public static final int WOLFSSL_ALPN_CONTINUE_ON_MISMATCH = 2;
/** ALPN: failed on protocol mismatch */
public static final int WOLFSSL_ALPN_FAILED_ON_MISMATCH = 4;
/* I/O callback default errors, pulled from wolfssl/ssl.h IOerrors */
/** I/O callback error: general error */
public static final int WOLFSSL_CBIO_ERR_GENERAL = -1;
/** I/O callback error: want read */
public static final int WOLFSSL_CBIO_ERR_WANT_READ = -2;
/** I/O callback error: want write */
public static final int WOLFSSL_CBIO_ERR_WANT_WRITE = -2;
/** I/O callback error: connection reset */
public static final int WOLFSSL_CBIO_ERR_CONN_RST = -3;
/** I/O callback error: socket interrupted */
public static final int WOLFSSL_CBIO_ERR_ISR = -4;
/** I/O callback error: connection closed */
public static final int WOLFSSL_CBIO_ERR_CONN_CLOSE = -5;
/** I/O callback error: timeout */
public static final int WOLFSSL_CBIO_ERR_TIMEOUT = -6;
/* Atomic User Needs, from ssl.h */
/** Represents server side */
public static final int WOLFSSL_SERVER_END = 0;
/** Represents Client side */
public static final int WOLFSSL_CLIENT_END = 1;
/** wolfSSL block algorithm type */
public static final int WOLFSSL_BLOCK_TYPE = 2;
/** wolfSSL stream algorithm type */
public static final int WOLFSSL_STREAM_TYPE = 3;
/** wolfSSL AEAD algorithm type */
public static final int WOLFSSL_AEAD_TYPE = 4;
/** wolfSSL TLS HMAC inner size */
public static final int WOLFSSL_TLS_HMAC_INNER_SZ = 13;
/* GetBulkCipher enum, pulled in from ssl.h for Atomic Record layer */
/** Bulk cipher algorithm enum: NULL */
public static int wolfssl_cipher_null;
/** Bulk cipher algorithm enum: RC4 */
public static int wolfssl_rc4;
/** Bulk cipher algorithm enum: RC2 */
public static int wolfssl_rc2;
/** Bulk cipher algorithm enum: DES */
public static int wolfssl_des;
/** Bulk cipher algorithm enum: 3DES */
public static int wolfssl_triple_des;
/** Bulk cipher algorithm enum: DES40 */
public static int wolfssl_des40;
/** Bulk cipher algorithm enum: AES */
public static int wolfssl_aes;
/** Bulk cipher algorithm enum: AES-GCM */
public static int wolfssl_aes_gcm;
/** Bulk cipher algorithm enum: AES-CCM */
public static int wolfssl_aes_ccm;
/* wolfSSL error codes, pulled in from wolfssl/error.h wolfSSL_ErrorCodes */
/** Generate Cookie Error */
public static final int GEN_COOKIE_E = -277;
/** Close notify alert sent */
public static final int SSL_SENT_SHUTDOWN = 1;
/** Close notify alert received */
public static final int SSL_RECEIVED_SHUTDOWN = 2;
/** Make it possible to return SSL write with changed buffer location */
public static final int SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER = 4;
/** Disable SSL 2.0. wolfSSL does not support SSL 2.0. */
public static final int SSL_OP_NO_SSLv2 = 8;
/** Disable SSL 3.0 */
public static final int SSL_OP_NO_SSLv3 = 0x00001000;
/** Disable TLS 1.0 */
public static final int SSL_OP_NO_TLSv1 = 0x00002000;
/** Disable TLS 1.1 */
public static final int SSL_OP_NO_TLSv1_1 = 0x04000000;
/** Disable TLS 1.2 */
public static final int SSL_OP_NO_TLSv1_2 = 0x08000000;
/** Disable TLS compression. Off by default */
public static final int SSL_OP_NO_COMPRESSION = 0x10000000;
/** Disable TLS 1.3 */
public static final int SSL_OP_NO_TLSv1_3 = 0x20000000;
/** SSL/TLS handshake failure */
public static final int SSL_HANDSHAKE_FAILURE = 101;
/** Alert: Unknown CA */
public static final int SSL_R_TLSV1_ALERT_UNKNOWN_CA = 102;
/** Alert: Certificate Unknown */
public static final int SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN = 103;
/** Alert: Bad certificate */
public static final int SSL_R_SSLV3_ALERT_BAD_CERTIFICATE = 104;
/** Monitor this CRL directory flag */
public static final int WOLFSSL_CRL_MONITOR = 0x01;
/** Start CRL monitoring flag */
public static final int WOLFSSL_CRL_START_MON = 0x02;
/** Bad mutex */
public static final int BAD_MUTEX_ERROR = -256;
/** Bad path for opendir */
public static final int BAD_PATH_ERROR = -258;
/** CRL Monitor already running */
public static final int MONITOR_RUNNING_E = -263;
/** Thread create error */
public static final int THREAD_CREATE_E = -264;
/** Cache header match error */
public static final int CACHE_MATCH_ERROR = -280;
/** Maximum SSL record size (16KB) as defined by the protocol. */
public static final int MAX_RECORD_SIZE = 16384;
/* ------------------ TLS extension specific ------------------------ */
/** SNI Host name type, for UseSNI() */
public static final int WOLFSSL_SNI_HOST_NAME = 0;
/** ALPN ERR OK, ALPN protocol match */
public static final int SSL_TLSEXT_ERR_OK = 0;
/** ALPN ERR NOACK, ALPN callback no match but not fatal */
public static final int SSL_TLSEXT_ERR_NOACK = 3;
/** ALPN ERR FATAL, ALPN callback no match and fatal */
public static final int SSL_TLSEXT_ERR_ALERT_FATAL = 2;
/* ----------------- TLS 1.3 secret callback IDs -------------------- */
/** TLS 1.3 secret ID: client early traffic secret */
public static int CLIENT_EARLY_TRAFFIC_SECRET;
/** TLS 1.3 secret ID: client handshake traffic secret */
public static int CLIENT_HANDSHAKE_TRAFFIC_SECRET;
/** TLS 1.3 secret ID: server handshake traffic secret */
public static int SERVER_HANDSHAKE_TRAFFIC_SECRET;
/** TLS 1.3 secret ID: client traffic secret */
public static int CLIENT_TRAFFIC_SECRET;
/** TLS 1.3 secret ID: server traffic secret */
public static int SERVER_TRAFFIC_SECRET;
/** TLS 1.3 secret ID: early exporter secret */
public static int EARLY_EXPORTER_SECRET;
/** TLS 1.3 secret ID: exporter secret */
public static int EXPORTER_SECRET;
/* ---------------------- wolfCrypt codes ---------------------------- */
/** Out of memory error */
public static final int MEMORY_E = -125;
/** Output buffer too small or input too large */
public static final int BUFFER_E = -132;
/** ASN input error, not enough data */
public static final int ASN_INPUT_E = -154;
/** Bad function argument provided */
public static final int BAD_FUNC_ARG = -173;
/** Feature not compiled in */
public static final int NOT_COMPILED_IN = -174;
/** No password provided by user */
public static final int NO_PASSWORD = -176;
/** TLS 1.3 secret callback function failure */
public static final int TLS13_SECRET_CB_E = -438;
/* HMAC codes, from wolfssl/wolfcrypt/hmac.h. These values
* are set via JNI calls in static class block since they can change
* depending on if wolfSSL is a FIPS or non-FIPS build. */
/** Md5 HMAC type */
public static int MD5;
/** SHA-1 HMAC type */
public static int SHA;
/** SHA2-256 HMAC type */
public static int SHA256;
/** SHA2-512 HMAC type */
public static int SHA512;
/** SHA2-384 HMAC type */
public static int SHA384;
/* key types */
/** DSA key type */
public static int DSAk;
/** RSA key type */
public static int RSAk;
/** ECDSA key type */
public static int ECDSAk;
/** Ed25519 key type */
public static int ED25519k;
/* GeneralName types. Match native values in asn.h */
/** ASN other type */
public static final int ASN_OTHER_TYPE = 0x00;
/** ASN RFC822 type */
public static final int ASN_RFC822_TYPE = 0x01;
/** ASN DNS type */
public static final int ASN_DNS_TYPE = 0x02;
/** ASN DIR/directory type */
public static final int ASN_DIR_TYPE = 0x04;
/** ASN URI type */
public static final int ASN_URI_TYPE = 0x06;
/** ASN IP type */
public static final int ASN_IP_TYPE = 0x07;
/* NIDs, from native asn.h */
/** Surname NID */
public static int NID_surname;
/** Serial number NID */
public static int NID_serialNumber;
/** PKCS9 Unstructured name NID */
public static int NID_pkcs9_unstructuredName;
/** PKCS9 contentType NID */
public static int NID_pkcs9_contentType;
/** PKCS9 challenge password NID */
public static int NID_pkcs9_challengePassword;
/** Given name NID */
public static int NID_givenName;
/** Initials NID */
public static int NID_initials;
/** Key Usage NID */
public static int NID_key_usage;
/** Subject Alternative Name NID */
public static int NID_subject_alt_name;
/** Basic Constraints NID */
public static int NID_basic_constraints;
/** Extended Key Usage NID */
public static int NID_ext_key_usage;
/** Domain name qualifier NID */
public static int NID_dnQualifier;
/** Subject Key Identifier NID */
public static int NID_subject_key_identifier;
/** Authority Key Identifier NID */
public static int NID_authority_key_identifier;
/** CRL Distribution Points NID */
public static int NID_crl_distribution_points;
/** Netscape Certificate Type NID */
public static int NID_netscape_cert_type;
/* Netscape Certificate Type bit flags */
/** Netscape Cert Type: SSL Client */
public static final int NS_CERT_TYPE_SSL_CLIENT = 0x80;
/** Netscape Cert Type: SSL Server */
public static final int NS_CERT_TYPE_SSL_SERVER = 0x40;
/** Netscape Cert Type: S/MIME */
public static final int NS_CERT_TYPE_EMAIL = 0x20;
/** Netscape Cert Type: Object Signing */
public static final int NS_CERT_TYPE_OBJECT_SIGNING = 0x10;
/** Netscape Cert Type: SSL CA */
public static final int NS_CERT_TYPE_SSL_CA = 0x04;
/** Netscape Cert Type: S/MIME CA */
public static final int NS_CERT_TYPE_EMAIL_CA = 0x02;
/** Netscape Cert Type: Object Signing CA */
public static final int NS_CERT_TYPE_OBJECT_CA = 0x01;
/* -------------- Named Groups (from enum in ssl.h) ----------------- */
/** Invalid named group */
public static final int WOLFSSL_NAMED_GROUP_INVALID = 0;
/** ECC SECT163K1 */
public static final int WOLFSSL_ECC_SECT163K1 = 1;
/** ECC SECT163R1 */
public static final int WOLFSSL_ECC_SECT163R1 = 2;
/** ECC SECT163R2 */
public static final int WOLFSSL_ECC_SECT163R2 = 3;
/** ECC SECT193R1 */
public static final int WOLFSSL_ECC_SECT193R1 = 4;
/** ECC SECT193R2 */
public static final int WOLFSSL_ECC_SECT193R2 = 5;
/** ECC SECT233K1 */
public static final int WOLFSSL_ECC_SECT233K1 = 6;
/** ECC SECT233R1 */
public static final int WOLFSSL_ECC_SECT233R1 = 7;
/** ECC SECT239K1 */
public static final int WOLFSSL_ECC_SECT239K1 = 8;
/** ECC SECT283K1 */
public static final int WOLFSSL_ECC_SECT283K1 = 9;
/** ECC SECT283R1 */
public static final int WOLFSSL_ECC_SECT283R1 = 10;
/** ECC SECT409K1 */
public static final int WOLFSSL_ECC_SECT409K1 = 11;
/** ECC SECT409R1 */
public static final int WOLFSSL_ECC_SECT409R1 = 12;
/** ECC SECT571K1 */
public static final int WOLFSSL_ECC_SECT571K1 = 13;
/** ECC SECT571R1 */
public static final int WOLFSSL_ECC_SECT571R1 = 14;
/** ECC SECP160K1 */
public static final int WOLFSSL_ECC_SECP160K1 = 15;
/** ECC SECP160R1 */
public static final int WOLFSSL_ECC_SECP160R1 = 16;
/** ECC SECP160R2 */
public static final int WOLFSSL_ECC_SECP160R2 = 17;
/** ECC SECP192K1 */
public static final int WOLFSSL_ECC_SECP192K1 = 18;
/** ECC SECP192R1 */
public static final int WOLFSSL_ECC_SECP192R1 = 19;
/** ECC SECP224K1 */
public static final int WOLFSSL_ECC_SECP224K1 = 20;
/** ECC SECP224R1 */
public static final int WOLFSSL_ECC_SECP224R1 = 21;
/** ECC SECP256K1 */
public static final int WOLFSSL_ECC_SECP256K1 = 22;
/** ECC SECP256R1 */
public static final int WOLFSSL_ECC_SECP256R1 = 23;
/** ECC SECP384R1 */
public static final int WOLFSSL_ECC_SECP384R1 = 24;
/** ECC SECP521R1 */
public static final int WOLFSSL_ECC_SECP521R1 = 25;
/** ECC BRAINPOOLP256R1 */
public static final int WOLFSSL_ECC_BRAINPOOLP256R1 = 26;
/** ECC BRAINPOOLP384R1 */
public static final int WOLFSSL_ECC_BRAINPOOLP384R1 = 27;
/** ECC BRAINPOOLP512R1 */
public static final int WOLFSSL_ECC_BRAINPOOLP512R1 = 28;
/** ECC X25519 */
public static final int WOLFSSL_ECC_X25519 = 29;
/** ECC X448 */
public static final int WOLFSSL_ECC_X448 = 30;
/** ECC SM2P256V1 */
public static final int WOLFSSL_ECC_SM2P256V1 = 41;
/** FFDHE 2048 */
public static final int WOLFSSL_FFDHE_2048 = 256;
/** FFDHE 3072 */
public static final int WOLFSSL_FFDHE_3072 = 257;
/** FFDHE 4096 */
public static final int WOLFSSL_FFDHE_4096 = 258;
/** FFDHE 6144 */
public static final int WOLFSSL_FFDHE_6144 = 259;
/** FFDHE 8192 */
public static final int WOLFSSL_FFDHE_8192 = 260;
/* -------------------- Crypto Callback DevID ----------------------- */
/** Invalid DevID value, when used as devId software crypto is used */
public static final int INVALID_DEVID = -2;
/** Crypto callback devId to be used by wolfSSL for WOLFSSL and
* WOLFSSL_CTX. This static devId will be used by wolfJSSE and set for all
* WolfSSLContext objects, if set to something besides
* WolfSSL.INVALID_DEVID. Applications can set this in wolfJSSE via
* WolfSSLProvider.setDevId(), or on a per SSLContext and SSLSession
* level with WolfSSLContext.setDevId() and WolfSSLSession.setDevId() */
public static int devId = WolfSSL.INVALID_DEVID;
/* ------------------------- Flag Values ---------------------------- */
/** WolfSSLCertificate.checkHost() match only wildcards in left-most
* position, used for LDAPS hostname verification. */
public static int WOLFSSL_LEFT_MOST_WILDCARD_ONLY = 0x40;
/* ------------------------ Internal state -------------------------- */
/* is this object active, or has it been cleaned up? */
private boolean active = false;
/* Track if library loading was skipped via system property */
private static volatile boolean libraryLoadSkipped = false;
/* ---------------------------- locks ------------------------------- */
/* lock for cleanup */
private final Object cleanupLock = new Object();
/* ------------------------ constructors ---------------------------- */
/**
* Initializes the wolfSSL library for use.
*
* @throws com.wolfssl.WolfSSLException if wolfSSL library fails to
* initialize correctly
*/
public WolfSSL() throws WolfSSLException {
int ret;
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, () -> "initializing wolfSSL library");
ret = init();
if (ret != SSL_SUCCESS) {
throw new WolfSSLException(
"Failed to initialize wolfSSL library: " + ret);
}
/* Populate NID values from native wolfSSL enums */
NID_surname = getNID_surname();
NID_serialNumber = getNID_serialNumber();
NID_pkcs9_unstructuredName = getNID_pkcs9_unstructuredName();
NID_pkcs9_contentType = getNID_pkcs9_contentType();
NID_pkcs9_challengePassword = getNID_pkcs9_challengePassword();
NID_givenName = getNID_givenName();
NID_initials = getNID_initials();
NID_key_usage = getNID_key_usage();
NID_subject_alt_name = getNID_subject_alt_name();
NID_basic_constraints = getNID_basic_constraints();
NID_ext_key_usage = getNID_ext_key_usage();
NID_dnQualifier = getNID_dnQualifier();
NID_subject_key_identifier = getNID_subject_key_identifier();
NID_authority_key_identifier = getNID_authority_key_identifier();
NID_crl_distribution_points = getNID_crl_distribution_points();
NID_netscape_cert_type = getNID_netscape_cert_type();
/* initialize cipher enum values */
wolfssl_aes = getBulkCipherAlgorithmEnumAES();
wolfssl_cipher_null = getBulkCipherAlgorithmEnumNULL();
wolfssl_rc4 = getBulkCipherAlgorithmEnumRC4();
wolfssl_rc2 = getBulkCipherAlgorithmEnumRC2();
wolfssl_des = getBulkCipherAlgorithmEnumDES();
wolfssl_triple_des = getBulkCipherAlgorithmEnumDES();
wolfssl_des40 = getBulkCipherAlgorithmEnumDES40();
wolfssl_aes_gcm = getBulkCipherAlgorithmEnumAESGCM();
wolfssl_aes_ccm = getBulkCipherAlgorithmEnumAESCCM();
/* initialize cipher enum values */
MD5 = getHmacEnumMD5();
SHA = getHmacEnumSHA1();
SHA256 = getHmacEnumSHA256();
SHA384 = getHmacEnumSHA384();
SHA512 = getHmacEnumSHA512();
/* initialize key type enum values */
DSAk = getKeyTypeEnumDSA();
RSAk = getKeyTypeEnumRSA();
ECDSAk = getKeyTypeEnumECDSA();
ED25519k = getKeyTypeEnumED25519();
/* initialize TLS 1.3 secret callback ID enums */
CLIENT_EARLY_TRAFFIC_SECRET =
getTls13SecretEnum_CLIENT_EARLY_TRAFFIC_SECRET();
CLIENT_HANDSHAKE_TRAFFIC_SECRET =
getTls13SecretEnum_CLIENT_HANDSHAKE_TRAFFIC_SECRET();
SERVER_HANDSHAKE_TRAFFIC_SECRET =
getTls13SecretEnum_SERVER_HANDSHAKE_TRAFFIC_SECRET();
CLIENT_TRAFFIC_SECRET = getTls13SecretEnum_CLIENT_TRAFFIC_SECRET();
SERVER_TRAFFIC_SECRET = getTls13SecretEnum_SERVER_TRAFFIC_SECRET();
EARLY_EXPORTER_SECRET = getTls13SecretEnum_EARLY_EXPORTER_SECRET();
EXPORTER_SECRET = getTls13SecretEnum_EXPORTER_SECRET();
this.active = true;
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, () -> "wolfSSL library initialization done");
}
/* ------------------- private/protected methods -------------------- */
private native int init();
/**
* Free native memory allocated at pointer provided.
* @param ptr native pointer
*/
public static native void nativeFree(long ptr);
static native int getNID_surname();
static native int getNID_serialNumber();
static native int getNID_pkcs9_unstructuredName();
static native int getNID_pkcs9_contentType();
static native int getNID_pkcs9_challengePassword();
static native int getNID_givenName();
static native int getNID_initials();
static native int getNID_key_usage();
static native int getNID_subject_alt_name();
static native int getNID_basic_constraints();
static native int getNID_ext_key_usage();
static native int getNID_dnQualifier();
static native int getNID_subject_key_identifier();
static native int getNID_authority_key_identifier();
static native int getNID_crl_distribution_points();
static native int getNID_netscape_cert_type();
static native int getBulkCipherAlgorithmEnumNULL();
static native int getBulkCipherAlgorithmEnumRC4();
static native int getBulkCipherAlgorithmEnumRC2();
static native int getBulkCipherAlgorithmEnumDES();
static native int getBulkCipherAlgorithmEnum3DES();
static native int getBulkCipherAlgorithmEnumDES40();
static native int getBulkCipherAlgorithmEnumAES();
static native int getBulkCipherAlgorithmEnumAESGCM();
static native int getBulkCipherAlgorithmEnumAESCCM();
static native int getBulkCipherAlgorithmEnumCHACHA();
static native int getBulkCipherAlgorithmEnumCAMELLIA();
static native int getHmacEnumMD5();
static native int getHmacEnumSHA1();
static native int getHmacEnumSHA256();
static native int getHmacEnumSHA384();
static native int getHmacEnumSHA512();
static native int getKeyTypeEnumDSA();
static native int getKeyTypeEnumRSA();
static native int getKeyTypeEnumECDSA();
static native int getKeyTypeEnumED25519();
static native int getTls13SecretEnum_CLIENT_EARLY_TRAFFIC_SECRET();
static native int getTls13SecretEnum_CLIENT_HANDSHAKE_TRAFFIC_SECRET();
static native int getTls13SecretEnum_SERVER_HANDSHAKE_TRAFFIC_SECRET();
static native int getTls13SecretEnum_CLIENT_TRAFFIC_SECRET();
static native int getTls13SecretEnum_SERVER_TRAFFIC_SECRET();
static native int getTls13SecretEnum_EARLY_EXPORTER_SECRET();
static native int getTls13SecretEnum_EXPORTER_SECRET();
static native String getEnabledCipherSuites();
static native String getEnabledCipherSuitesIana();
static native String getAvailableCipherSuitesIana(int version);
/** Native wrapper to set wolfSSL crypto callback, only passing in devId
* and allowing native code to set up and manage callback and context */
private static native int wc_CryptoCb_RegisterDevice(int devId);
/** Native wrapper to unregister wolfSSL crypto callback */
private static native void wc_CryptoCb_UnRegisterDevice(int devId);
/* ------------------------- Java methods --------------------------- */
/**
* Loads JNI library; must be called prior to any other calls in this
* class.
*
* The native library is expected to be called "wolfssljni", and
* must be on the system library search path.
*
* "wolfssljni" links against the wolfSSL native C library
* ("wolfssl"), and for Windows compatibility "wolfssl" needs to be
* explicitly loaded first here.
*
* If the system property "wolfssl.skipLibraryLoad" is set to
* "true", this method will skip loading the native library. This
* allows applications to load the native library themselves using
* custom logic (for example extracting from a JAR at runtime).
* The property must be set before this method is called (either
* directly or via WolfSSLProvider constructor).
*
* Applications can check if library loading was skipped by calling
* WolfSSL.isLibraryLoadSkipped().
*
* @throws UnsatisfiedLinkError if the library is not found.
*/
public static void loadLibrary() throws UnsatisfiedLinkError {
int fipsLoaded = 0;
String skipLoad = System.getProperty("wolfssl.skipLibraryLoad");
if (skipLoad != null && skipLoad.equalsIgnoreCase("true")) {
/* User will load native libraries manually */
libraryLoadSkipped = true;
WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, () -> "skipping native library load, " +
"wolfssl.skipLibraryLoad system property set to true");
return;
}
WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, () -> "loading native library: wolfssl");
String osName = System.getProperty("os.name");
if (osName != null && osName.toLowerCase().contains("win")) {
try {
/* Default wolfCrypt FIPS library on Windows is compiled
* as "wolfssl-fips" by Visual Studio solution */
System.loadLibrary("wolfssl-fips");
fipsLoaded = 1;
} catch (UnsatisfiedLinkError e) {
/* wolfCrypt FIPS not available */
}
if (fipsLoaded == 0) {
/* FIPS library not loaded, try normal libwolfssl */
System.loadLibrary("wolfssl");
}
}
WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, () -> "loading native library: wolfssljni");
/* Load wolfssljni library */
System.loadLibrary("wolfssljni");
}
/**
* Load JNI library with a specific name; must be called prior to any
* other calls in this package.
*
* The native library needs to be located on the system library search
* path.
*
* Note: this method does not check the
* "wolfssl.skipLibraryLoad" system property. That property is only
* respected by the no-argument {@link #loadLibrary()} method.
*
* @param libName name of native JNI library
* @throws UnsatisfiedLinkError if the library is not found.
*/
public static void loadLibrary(String libName) throws UnsatisfiedLinkError {
WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, () -> "loading native lib by name: " + libName);
System.loadLibrary(libName);
}
/**
* Loads dynamic JNI library from a specific path; must be called
* prior to any other calls in this package.
*
* This function gives the application more control over the exact
* native library being loaded, as both WolfSSL.loadLibrary() and
* WolfSSL.loadLibrary(String libName) search for a library on the
* system library search path. This function allows the appliation
* to specify a specific absolute path to the native library file
* to load, thus guaranteeing the exact library loaded and helping
* to prevent against malicious attackers from attempting to
* override the library being loaded.
*
* Note: this method does not check the
* "wolfssl.skipLibraryLoad" system property. That property is only
* respected by the no-argument {@link #loadLibrary()} method.
*
* @param libPath complete path name to the native dynamic JNI
* library
* @throws UnsatisfiedLinkError if the library is not found.
*/
public static void loadLibraryAbsolute(String libPath)
throws UnsatisfiedLinkError {
WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, () -> "loading native lib by path: " + libPath);
System.load(libPath);
}
/**
* Check if native library loading was skipped.
*
* Library loading is skipped when the System property
* "wolfssl.skipLibraryLoad" is set to "true" and loadLibrary() has
* been called.
*
* @return true if library loading was skipped, false otherwise
*/
public static boolean isLibraryLoadSkipped() {
return libraryLoadSkipped;
}
/* ----------------- generic static helper functions ---------------- */
/**
* Read a File into byte array.
*
* This method can't use the java.nio package since we have users
* on Android API 24 which does not support java.nio.
*
* @param file File to read into byte array
*
* @return byte array representing input File, or null if file is null
*
* @throws FileNotFoundException if file is not found
* @throws IOException if unable to read entire file
*/
protected static byte[] fileToBytes(File file)
throws FileNotFoundException, IOException {
int bytesRead = 0;
long fileLen = 0;
byte[] fileBytes = null;
FileInputStream fis = null;
if (file == null) {
return null;
}
fileLen = file.length();
if (fileLen == 0) {
return new byte[0];
}
try {
fis = new FileInputStream(file);
if (fis != null) {
fileBytes = new byte[(int)fileLen];
bytesRead = fis.read(fileBytes);
if (bytesRead != fileLen) {
throw new IOException("Unable to read entire file: " +
file.getAbsolutePath());
}
}
} finally {
if (fis != null) {
fis.close();
}
}
return fileBytes;
}
/* --------------- native feature detection functions --------------- */
/**
* Tests if TLS 1.0 has been compiled into the native wolfSSL library.
* TLS 1.0 is disabled by default in native wolfSSL, unless the user
* has configured wolfSSL with "--enable-tls10".
*
* @return true if enabled, otherwise false if not compiled in.
*/
public static native boolean TLSv1Enabled();
/**
* Tests if TLS 1.1 has been compiled into the native wolfSSL library.
*
* @return true if enabled, otherwise false if not compiled in.
*/
public static native boolean TLSv11Enabled();
/**
* Tests if TLS 1.2 has been compiled into the native wolfSSL library.
*
* @return true if enabled, otherwise false if not compiled in.
*/
public static native boolean TLSv12Enabled();
/**
* Tests if TLS 1.3 has been compiled into the native wolfSSL library.
*
* @return true if enabled, otherwise false if not compiled in.
*/
public static native boolean TLSv13Enabled();
/**
* Tests if DTLS 1.3 has been compiled into the native wolfSSL library.
*
* @return true if enabled, otherwise false if not compiled in.
*/
public static native boolean DTLSv13Enabled();
/**
* Tests if SHA-1 is enabled in the native wolfSSL library.
*
* @return true if enabled, otherwise false if not compiled in.
*/
public static native boolean ShaEnabled();
/**
* Tests if SHA-224 is enabled in the native wolfSSL library.
*
* @return true if enabled, otherwise false if not compiled in.
*/
public static native boolean Sha224Enabled();
/**
* Tests if SHA-256 is enabled in the native wolfSSL library.
*
* @return true if enabled, otherwise false if not compiled in.
*/
public static native boolean Sha256Enabled();
/**
* Tests if SHA-384 is enabled in the native wolfSSL library.