forked from wolfSSL/wolfssljni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWolfSSLContext.java
More file actions
2408 lines (2093 loc) · 101 KB
/
WolfSSLContext.java
File metadata and controls
2408 lines (2093 loc) · 101 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
/* WolfSSLContext.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.util.Arrays;
import java.nio.ByteBuffer;
/**
* Wraps a native WolfSSL context object and contains methods directly related
* to the SSL/TLS context.
*
* @author wolfSSL
*/
public class WolfSSLContext {
/* internal native WOLFSSL_CTX pointer */
private long sslCtxPtr = 0;
/* user-registerd I/O callbacks, called by internal WolfSSLContext
* I/O callback. This is done in order to pass references to
* WolfSSLSession object */
private WolfSSLIORecvCallback internRecvCb;
private WolfSSLIOSendCallback internSendCb;
/* user-registered DTLS cookie generation callback */
private WolfSSLGenCookieCallback internCookieCb = null;
/* user-registered MAC/encrypt, dec/verify, verify/dec callbacks */
private WolfSSLMacEncryptCallback internMacEncryptCb = null;
private WolfSSLDecryptVerifyCallback internDecryptVerifyCb = null;
private WolfSSLVerifyDecryptCallback internVerifyDecryptCb = null;
/* user-registered ECC sign/verify callbacks */
private WolfSSLEccSignCallback internEccSignCb = null;
private WolfSSLEccVerifyCallback internEccVerifyCb = null;
/* user-registered ECC shared secret callback */
private WolfSSLEccSharedSecretCallback internEccSharedSecretCb = null;
/* user-registered RSA sign/verify callbacks */
private WolfSSLRsaSignCallback internRsaSignCb = null;
private WolfSSLRsaVerifyCallback internRsaVerifyCb = null;
/* user-registered RSA enc/dec callbacks */
private WolfSSLRsaEncCallback internRsaEncCb = null;
private WolfSSLRsaDecCallback internRsaDecCb = null;
/* user-registered PSK callbacks */
private WolfSSLPskClientCallback internPskClientCb = null;
private WolfSSLPskServerCallback internPskServerCb = null;
/* is this context active, or has it been freed? */
private boolean active = false;
/* lock around active state */
private final Object stateLock = new Object();
/* lock around native WOLFSSL_CTX pointer use */
private final Object ctxLock = new Object();
/**
* Creates a new SSL/TLS context for the desired SSL/TLS protocol level.
*
* @param method a pointer (long) to the desired WOLFSSL_METHOD for
* use in the SSL context. This WOLFSSL_METHOD pointer
* is created with one of the protocol-specific methods
* (ex: TLSv1_2_ClientMethod()) matching to the desired
* SSL/TLS/DTLS protocol level.
*
* @throws com.wolfssl.WolfSSLException when creation of SSL context fails
*/
public WolfSSLContext(long method) throws WolfSSLException {
sslCtxPtr = newContext(method);
if (sslCtxPtr == 0) {
throw new WolfSSLException("Failed to create SSL Context");
}
this.active = true;
WolfSSLDebug.log(WolfSSL.class, WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, sslCtxPtr, () -> "creating new WolfSSLContext");
/* Enable native wolfSSL debug logging if 'wolfssl.debug'
* System property is set. Also attempted in WolfSSLProvider
* but System property may not have been set by user yet at that
* point. */
WolfSSLDebug.setNativeWolfSSLDebugging();
}
/* ------------------- private/protected methods -------------------- */
/**
* Return native WOLFSSL_CTX pointer.
*
* @return pointer to native WOLFSSL_CTX structure for this object
*/
protected long getContextPtr() {
synchronized (this.ctxLock) {
return sslCtxPtr;
}
}
/* used by JNI native recv Cb */
synchronized WolfSSLIORecvCallback getInternRecvCb() {
return internRecvCb;
}
/* used by JNI native send Cb */
synchronized WolfSSLIOSendCallback getInternSendCb() {
return internSendCb;
}
/* used by JNI native cookie Cb */
synchronized WolfSSLGenCookieCallback getInternCookieCb() {
return internCookieCb;
}
/* used by JNI native MAC/encrypt Cb */
synchronized WolfSSLMacEncryptCallback getInternMacEncryptCb() {
return internMacEncryptCb;
}
/* used by JNI native decrypt/verify Cb */
synchronized WolfSSLDecryptVerifyCallback getInternDecryptVerifyCb() {
return internDecryptVerifyCb;
}
/* used by JNI native verify/decrypt Cb */
synchronized WolfSSLVerifyDecryptCallback getInternVerifyDecryptCb() {
return internVerifyDecryptCb;
}
/* this will be registered with native wolfSSL library */
private int internalIORecvCallback(WolfSSLSession ssl, byte[] buf, int sz)
{
int ret;
/* call user-registered recv method */
ret = internRecvCb.receiveCallback(ssl, buf, sz, ssl.getIOReadCtx());
return ret;
}
private int internalIOSendCallback(WolfSSLSession ssl, byte[] buf, int sz)
{
int ret;
/* call user-registered recv method */
ret = internSendCb.sendCallback(ssl, buf, sz, ssl.getIOWriteCtx());
return ret;
}
private int internalGenCookieCallback(WolfSSLSession ssl, byte[] buf,
int sz)
{
int ret;
/* call user-registered cookie gen method */
ret = internCookieCb.genCookieCallback(ssl, buf, sz,
ssl.getGenCookieCtx());
return ret;
}
private int internalMacEncryptCallback(WolfSSLSession ssl,
ByteBuffer macOut, byte[] macIn, long macInSz, int macContent,
int macVerify, ByteBuffer encOut, ByteBuffer encIn, long encSz)
{
int ret;
/* call user-registered MAC/encrypt method */
ret = internMacEncryptCb.macEncryptCallback(ssl, macOut, macIn,
macInSz, macContent, macVerify, encOut, encIn, encSz,
ssl.getMacEncryptCtx());
return ret;
}
private int internalDecryptVerifyCallback(WolfSSLSession ssl,
ByteBuffer decOut, byte[] decIn, long decSz, int content,
int verify, long[] padSz)
{
int ret;
/* call user-registered decrypt/verify method */
ret = internDecryptVerifyCb.decryptVerifyCallback(ssl, decOut,
decIn, decSz, content, verify, padSz,
ssl.getDecryptVerifyCtx());
return ret;
}
private int internalVerifyDecryptCallback(WolfSSLSession ssl,
ByteBuffer decOut, byte[] decIn, long decSz, int content,
int macVerify, long[] padSz)
{
int ret;
/* call user-registered verify/decrypt method */
ret = internVerifyDecryptCb.verifyDecryptCallback(ssl, decOut,
decIn, decSz, content, macVerify, padSz,
ssl.getVerifyDecryptCtx());
return ret;
}
private int internalEccSignCallback(WolfSSLSession ssl, ByteBuffer in,
long inSz, ByteBuffer out, long[] outSz, ByteBuffer keyDer,
long keySz)
{
int ret;
/* call user-registered ecc sign method */
ret = internEccSignCb.eccSignCallback(ssl, in, inSz, out, outSz,
keyDer, keySz, ssl.getEccSignCtx());
return ret;
}
private int internalEccVerifyCallback(WolfSSLSession ssl, ByteBuffer sig,
long sigSz, ByteBuffer hash, long hashSz, ByteBuffer keyDer,
long keySz, int[] result)
{
int ret;
/* call user-registered ecc verify method */
ret = internEccVerifyCb.eccVerifyCallback(ssl, sig, sigSz, hash,
hashSz, keyDer, keySz, result, ssl.getEccVerifyCtx());
return ret;
}
private int internalEccSharedSecretCallback(WolfSSLSession ssl,
WolfCryptEccKey otherKey, ByteBuffer pubKeyDer, long[] pubKeyDerSz,
ByteBuffer out, long[] outSz, int side)
{
int ret;
/* call user-registered ecc shared secret method */
ret = internEccSharedSecretCb.eccSharedSecretCallback(ssl,
otherKey, pubKeyDer, pubKeyDerSz, out, outSz, side,
ssl.getEccSharedSecretCtx());
return ret;
}
private int internalRsaSignCallback(WolfSSLSession ssl, ByteBuffer in,
long inSz, ByteBuffer out, int[] outSz, ByteBuffer keyDer,
long keySz)
{
int ret;
/* call user-registered rsa sign method */
ret = internRsaSignCb.rsaSignCallback(ssl, in, inSz, out, outSz,
keyDer, keySz, ssl.getRsaSignCtx());
return ret;
}
private int internalRsaVerifyCallback(WolfSSLSession ssl, ByteBuffer sig,
long sigSz, ByteBuffer out, long outSz, ByteBuffer keyDer,
long keySz)
{
int ret;
/* call user-registered rsa verify method */
ret = internRsaVerifyCb.rsaVerifyCallback(ssl, sig, sigSz, out,
outSz, keyDer, keySz, ssl.getRsaVerifyCtx());
return ret;
}
private int internalRsaEncCallback(WolfSSLSession ssl, ByteBuffer in,
long inSz, ByteBuffer out, int[] outSz, ByteBuffer keyDer,
long keySz)
{
int ret;
/* call user-registered rsa public encrypt method */
ret = internRsaEncCb.rsaEncCallback(ssl, in, inSz, out, outSz,
keyDer, keySz, ssl.getRsaEncCtx());
return ret;
}
private int internalRsaDecCallback(WolfSSLSession ssl, ByteBuffer in,
long inSz, ByteBuffer out, long outSz, ByteBuffer keyDer,
long keySz)
{
int ret;
/* call user-registered rsa private decrypt method */
ret = internRsaDecCb.rsaDecCallback(ssl, in, inSz, out, outSz, keyDer,
keySz, ssl.getRsaDecCtx());
return ret;
}
private long internalPskClientCallback(WolfSSLSession ssl, String hint,
StringBuffer identity, long idMaxLen, byte[] key, long keyMaxLen)
{
long ret;
/* call user-registered PSK client callback method */
ret = internPskClientCb.pskClientCallback(ssl, hint, identity,
idMaxLen, key, keyMaxLen);
return ret;
}
private long internalPskServerCallback(WolfSSLSession ssl,
String identity, byte[] key, long keyMaxLen)
{
long ret;
/* call user-registered PSK server callback method */
ret = internPskServerCb.pskServerCallback(ssl, identity,
key, keyMaxLen);
return ret;
}
/**
* Verifies that the current WolfSSLContext object is active.
*
* @throws IllegalStateException if object has been freed
*/
private synchronized void confirmObjectIsActive()
throws IllegalStateException {
synchronized (stateLock) {
if (this.active == false) {
throw new IllegalStateException(
"WolfSSLContext object has been freed");
}
}
}
/* ------------------ native method declarations -------------------- */
private native long newContext(long method);
private native int setTmpDH(long ctx, byte[] p, int pSz, byte[] g, int gSz);
private native int setTmpDHFile(long ctx, String fname, int format);
private native int useCertificateFile(long ctx, String file, int format);
private native int usePrivateKeyFile(long ctx, String file, int format);
private native int loadVerifyLocations(long ctx, String file, String path);
private native int useCertificateChainFile(long ctx, String file);
private native void freeContext(long ctx);
private native void setVerify(long ctx, int mode, WolfSSLVerifyCallback vc);
private native long setOptions(long ctx, long op);
private native long getOptions(long ctx);
private native int memsaveCertCache(long ctx, byte[] mem, int sz,
int[] used);
private native int memrestoreCertCache(long ctx, byte[] mem, int sz);
private native int getCertCacheMemsize(long ctx);
private native long setCacheSize(long ctx, long sz);
private native long getCacheSize(long ctx);
private native int setCipherList(long ctx, String list);
private native int loadVerifyBuffer(long ctx, byte[] in, long sz,
int format);
private native int useCertificateBuffer(long ctx, byte[] in, long sz,
int format);
private native int usePrivateKeyBuffer(long ctx, byte[] in, long sz,
int format);
private native int useCertificateChainBuffer(long ctx, byte[] in, long sz);
private native int useCertificateChainBufferFormat(long ctx, byte[] in,
long sz, int format);
private native int setGroupMessages(long ctx);
private native void setIORecv(long ctx);
private native void setIOSend(long ctx);
private native void setGenCookie(long ctx);
private native int enableCRL(long ctx, int options);
private native int disableCRL(long ctx);
private native int loadCRL(long ctx, String path, int type, int monitor);
private native int setCRLCb(long ctx, WolfSSLMissingCRLCallback cb);
private native int enableOCSP(long ctx, long options);
private native int disableOCSP(long ctx);
private native int setOCSPOverrideUrl(long ctx, String url);
private native void setMacEncryptCb(long ctx);
private native void setDecryptVerifyCb(long ctx);
private native void setVerifyDecryptCb(long ctx);
private native void setEccSignCb(long ctx);
private native void setEccVerifyCb(long ctx);
private native void setEccSharedSecretCb(long ctx);
private native void setRsaSignCb(long ctx);
private native void setRsaVerifyCb(long ctx);
private native void setRsaEncCb(long ctx);
private native void setRsaDecCb(long ctx);
private native void setPskClientCb(long ctx);
private native void setPskServerCb(long ctx);
private native int usePskIdentityHint(long ssl, String hint);
private native int useSupportedCurve(long ctx, int name);
private native int setGroups(long ctx, int[] groups);
private native int set1SigAlgsList(long ctx, String list);
private native int useSecureRenegotiation(long ctx);
private native int setMinDhKeySz(long ctx, int keySzBits);
private native int setMinRsaKeySz(long ctx, int keySzBits);
private native int setMinEccKeySz(long ctx, int keySzBits);
private native int setDevId(long ctx, int devId);
private native void flushSessions(long ctx, int tm);
/* ------------------- context-specific methods --------------------- */
/**
* Loads a certificate file into the SSL context.
* This file is provided by the <b>file</b> parameter. The <b>format</b>
* paramenter specifies the format type of the file - either
* <b>SSL_FILETYPE_ASN1</b> or <b>SSL_FILETYPE_PEM</b>. Please see the
* wolfSSL examples for proper usage.
*
* @param file a file containing the certificate to be loaded into
* the wolfSSL SSL context.
* @param format format of the certificates pointed to by <code>file
* </code>. Possible options are <b>SSL_FILETYPE_ASN1</b>,
* for DER-encoded certificates, or <b>SSL_FILETYPE_PEM
* </b> for PEM-encoded certificates.
* @return <code>SSL_SUCCESS</code> upon success, otherwise
* <code>SSL_FAILURE</code>. Possible failure causes
* may be that the file is in the wrong format, the
* format argument was given incorrectly, the file
* doesn't exist, can't be read, or is corrupted,
* an out of memory condition occurs, or the Base16
* decoding fails on the file.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws NullPointerException Input file is null
* @see WolfSSLSession#useCertificateFile(String, int)
*/
public int useCertificateFile(String file, int format)
throws IllegalStateException, NullPointerException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered useCertificateFile(" + file + ", " +
format +")");
return useCertificateFile(getContextPtr(), file, format);
}
}
/**
* Loads a private key file into the SSL context.
* This file is provided by the <b>file</b> parameter. The <b>format</b>
* paramenter specifies the format type of the file - either
* <b>SSL_FILETYPE_ASN1</b> or <b>SSL_FILETYPE_PEM</b>. Please see the
* wolfSSL examples for proper usage.
*
* @param file a file containing the private key to be loaded into
* the wolfSSL SSL context.
* @param format format of the private key pointed to by <code>file
* </code>. Possible options are <b>SSL_FILETYPE_ASN1</b>,
* for a DER-encoded key, or <b>SSL_FILETYPE_PEM
* </b> for a PEM-encoded key.
* @return <code>SSL_SUCCESS</code> upon success, otherwise
* <code>SSL_FAILURE</code>. Possible failure causes
* may be that the file is in the wrong format, the
* format argument was given incorrectly, the file
* doesn't exist, can't be read, or is corrupted,
* an out of memory condition occurs, the Base16
* decoding fails on the file, or the key file is
* encrypted but no password is provided.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws NullPointerException Input file is null
* @see WolfSSLSession#usePrivateKeyFile(String, int)
*/
public int usePrivateKeyFile(String file, int format)
throws IllegalStateException, NullPointerException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered usePrivateKeyFile(" + file + ", " + format +")");
return usePrivateKeyFile(getContextPtr(), file, format);
}
}
/**
* Loads PEM-formatted CA certificates into the SSL context.
* These certificates will be treated as trusted root certificates and
* used to verify certs received from peers during the SSL handshake.
* <p>
* The root certificate provided by the <b>file</b> paramter may be a
* single certificate or a file containing multiple certificates. If
* multiple CA certs are included in the same file, wolfSSL will load them
* in the same order which they are presented in the file. The <b>path</b>
* parameter is a directory path which contains certificates of trusted
* root CAs. If the value of <b>file</b> is not NULL, <b>path</b> may be
* specified as <code>null</code> if not needed. If <b>path</b> is
* specified, and <code>NO_WOLFSSL_DIR</code> is defined when building the
* library, wolfSSL will load all CA certificates located in the given
* directory.
*
* @param file path to the file containing PEM-formatted CA certificates
* @param path path to directory containing PEM-formatted CA certificates
* to load
* @return <code>SSL_SUCCESS</code> on success. Otherwise<br>
* <code>SSL_FAILURE</code> if <b>ctx</b> is null, or if
* both <b>file</b> and <b>path</b> are null.<br>
* <code>SSL_BAD_FILETYPE</code> if the file is in the
* wrong format.<br>
* <code>SSL_BAD_FILE</code> if the file doesn't exist, can't
* be read, or is corrupted.<br>
* <code>MEMORY_E</code> if an out of memory condition
* occurs.<br>
* <code>ASN_INPUT_E</code> if Base16 decoding fails on the
* file.<br>
* <code>BUFFER_E</code> if a chain buffer is bigger than the
* recieving buffer.<br>
* <code>BAD_PATH_ERROR</code> if the native opendir()
* function call fails when trying to open <b>path</b>.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws NullPointerException Input file and path are null
* @see #useCertificateFile(String, int)
* @see #usePrivateKeyFile(String, int)
* @see #useCertificateChainFile(String)
* @see WolfSSLSession#useCertificateFile(String, int)
* @see WolfSSLSession#usePrivateKeyFile(String, int)
* @see WolfSSLSession#useCertificateChainFile(String)
*/
public int loadVerifyLocations(String file, String path)
throws IllegalStateException, NullPointerException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered loadVerifyLocations(" + file + ", " + path +")");
return loadVerifyLocations(getContextPtr(), file, path);
}
}
/**
* Loads a chain of certificates into the SSL context.
* The file containing the certificate chain is provided by the <b>file</b>
* parameter and must contain PEM-formatted certificates. This function
* will process up to <code>MAX_CHAIN_DEPTH</code> (default = 9, defined
* in internal.h) certificates, plus the subject cert.
*
* @param file path to the file containing the chain of certificates
* to be loaded into the wolfSSL SSL context. Certificates
* must be in PEM format.
* @return <code>SSL_SUCCESS</code> on success, otherwise <code>
* SSL_FAILURE</code>. If the function call fails, possible
* causes might include: the file is in the wrong format,
* the file doesn't exist, can't be read, or is corrupted, or
* an out of memory condition occurs.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws NullPointerException Input file is null
* @see #useCertificateFile(String, int)
* @see WolfSSLSession#useCertificateFile(String, int)
*/
public int useCertificateChainFile(String file)
throws IllegalStateException, NullPointerException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered useCertificateChainFile(" + file + ")");
return useCertificateChainFile(getContextPtr(), file);
}
}
/**
* Sets the verification method for remote peers and also allows a
* verify callback to be registered with the SSL session.
* If no verify callback is desired, null can be used for <code>
* callback</code>.
* <p>
* The verification <b>mode</b> of peer certificates is a logically
* OR'd list of flags. The possible flag values include:
* <p>
* <code>SSL_VERIFY_NONE</code><br>
* <b>Client mode:</b> the client will not verify the certificate
* received from teh 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.
* <p>
* <code>SSL_VERIFY_PEER</code><br>
* <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 received.
* <p>
* <code>SSL_VERIFY_FAIL_IF_NO_PEER_CERT</code><br>
* <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).
*
* @param mode verification type
* @param callback custom verification callback to register with the SSL
* session. If no callback is desired, <code>null</code>
* may be used.
* @throws IllegalStateException WolfSSLContext has been freed
*/
public void setVerify(int mode, WolfSSLVerifyCallback callback)
throws IllegalStateException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered setVerify(" + mode + ", " + callback + ")");
setVerify(getContextPtr(), mode, callback);
}
}
/**
* Sets the options to use for the WOLFSSL structure.
* Example options are WolfSSL.SSL_OP_NO_SSLv3
*
*
* @param op bit mask of options to set
* @return returns the revised options bit mask on success
* @throws IllegalStateException WolfSSLContext has been freed
*/
public long setOptions(long op)
throws IllegalStateException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered setOptions(" + op + ")");
return setOptions(getContextPtr(), op);
}
}
/**
* Gets the options to use for the WOLFSSL structure.
* Example options are WolfSSL.SSL_OP_NO_SSLv3
*
*
* @return returns options bit mask on success
* @throws IllegalStateException WolfSSLContext has been freed
*/
public long getOptions()
throws IllegalStateException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered getOptions()");
return getOptions(getContextPtr());
}
}
/**
* Frees an allocated SSL context.
* This method decrements the CTX reference count and only frees the
* context when the reference count has reached zero.
*
* @throws IllegalStateException WolfSSLContext has been freed
* @see WolfSSLSession#freeSSL()
*/
public synchronized void free() throws IllegalStateException {
confirmObjectIsActive();
synchronized (stateLock) {
if (this.active == false) {
/* already freed, just return */
return;
}
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.sslCtxPtr, () -> "entered free()");
/* free native resources */
freeContext(this.sslCtxPtr);
/* free Java resources */
this.active = false;
this.sslCtxPtr = 0;
}
}
}
/**
* Persists the certificate cache to memory.
* Use this method to store the current certificate cache to a memory
* buffer.
*
* @param mem the buffer to store the certificate cache in
* @param sz the size of the output buffer, <b>mem</b>
* @param used output parameter, the size of the cert cache in bytes is
* returned in the first element of this array.
* @return <b><code>SSL_SUCCESS</code></b> on success,
* <b><code>SSL_FAILURE</code></b> on general failure,
* <b><code>BAD_FUNC_ARG</code></b> if null or negative
* parameters are passed in,
* <b><code>BAD_MUTEX_ERROR</code></b> if the CA mutex lock
* fails, <b><code>BUFFER_E</code></b> if the output buffer
* is too small.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws WolfSSLJNIException Internal JNI error
* @see WolfSSL#memsaveSessionCache(byte[], int)
* @see WolfSSL#memrestoreSessionCache(byte[], int)
* @see WolfSSL#getSessionCacheMemsize()
* @see #memsaveCertCache(byte[], int, int[])
* @see #memrestoreCertCache(byte[], int)
* @see #getCertCacheMemsize()
*/
public int memsaveCertCache(byte[] mem, int sz, int[] used)
throws IllegalStateException, WolfSSLJNIException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered memsaveCertCache()");
return memsaveCertCache(getContextPtr(), mem, sz, used);
}
}
/**
* Restores the certificate cache from memory.
* This method restores the certificate cache from a saved memory buffer.
*
* @param mem memory buffer containing the stored certificate cache
* to restore
* @param sz size of the input memory buffer, <b>mem</b>
* @return <b><code>SSL_SUCCESS</code></b> upon success,
* <b><code>SSL_FAILURE</code></b> upon general failure,
* <b><code>BAD_FUNC_ARG</code></b> if null or negative
* parameters are passed in,
* <b><code>BUFFER_E</code></b> if the certificate cache
* memory buffer is too small,
* <b><code>CACHE_MATCH_ERROR</code></b> if the cert cache
* memory header match failed,
* <b><code>BAD_MUTEX_ERROR</code></b> if the CA mutex lock
* failed.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws WolfSSLJNIException Internal JNI error
* @see WolfSSL#memsaveSessionCache(byte[], int)
* @see WolfSSL#memrestoreSessionCache(byte[], int)
* @see WolfSSL#getSessionCacheMemsize()
* @see #memsaveCertCache(byte[], int, int[])
* @see #getCertCacheMemsize()
*/
public int memrestoreCertCache(byte[] mem, int sz)
throws IllegalStateException, WolfSSLJNIException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered memrestoreCertCache()");
return memrestoreCertCache(getContextPtr(), mem, sz);
}
}
/**
* Gets how big the certificate cache save buffer needs to be.
* Use this method to get how big the output buffer needs to be in which
* to save the current certifiate cache to memory.
*
* @return size, in bytes, of how large the output buffer should be
* to store the certificate cache into memory.
* @throws IllegalStateException WolfSSLContext has been freed
* @see WolfSSL#memsaveSessionCache(byte[], int)
* @see WolfSSL#memrestoreSessionCache(byte[], int)
* @see WolfSSL#getSessionCacheMemsize()
* @see #memsaveCertCache(byte[], int, int[])
* @see #memrestoreCertCache(byte[], int)
*/
public int getCertCacheMemsize()
throws IllegalStateException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered getCertCacheMemsize()");
return getCertCacheMemsize(getContextPtr());
}
}
/**
* Cache size is set at compile time.This function returns the current cache
* size which has been set at compile time.
* An example of macros to set cache size are HUGE_SESSION_CACHE and
* SMALL_SESSION_CACHE.
*
* @param sz unused size to set cache as
* @return size of compile time cache.
* @throws IllegalStateException WolfSSLContext has been freed
*/
public long setCacheSize(long sz) throws IllegalStateException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered setCacheSize()");
return setCacheSize(getContextPtr(), sz);
}
}
/**
* Gets the cache size is set at compile time.
* This function returns the current cache size which has been set at compile
* time.
*
* @return size of compile time cache.
* @throws IllegalStateException WolfSSLContext has been freed
*/
public long getCacheSize() throws IllegalStateException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered getCacheSize()");
return getCacheSize(getContextPtr());
}
}
/**
* Sets the cipher suite list for a given SSL context.
* This cipher suite list becomes the default list for any new SSL
* sessions created using this context. The ciphers in the list should
* be sorted in order of preference from highest to lowest. Each call
* to <code>ctxSetCipherList()</code> resets the cipher suite list for
* the specific SSL context to the provided list each time time the
* method is called.
* <p>
* The cipher suite list, <b>list</b>, is a null-terminated text String,
* and colon-delimited list. For example, one possible list may be:
* <p>
* <code>"DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:AES256-SHA256"</code>
* <p>
* Valid cipher values are the full name values from the cipher_names[]
* array in the native wolfSSL src/internal.c:
*
* @param list null-terminated text string and colon-delimited list
* of cipher suites to use with the specified SSL
* context.
* @return <code>SSL_SUCCESS</code> upon success. <code>
* SSL_FAILURE</code> upon failure.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws NullPointerException Input list is null
* @see WolfSSLSession#setCipherList(String)
*/
public int setCipherList(String list)
throws IllegalStateException, NullPointerException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered setCipherList(" + list + ")");
return setCipherList(getContextPtr(), list);
}
}
/**
* Sets up the group parameters to be used if the server negotiates
* a cipher suite that uses DHE.
*
* @param p Diffie-Hellman prime number parameter
* @param pSz size of <code>p</code>
* @param g Diffie-Hellman "generator" parameter
* @param gSz size of <code>g</code>
* @return <code>SSL_SUCCESS</code> on success. <code>MEMORY_E
* </code> if a memory error was encountered. <code>
* SIDE_ERROR</code> if this function is called on an
* SSL client instead of an SSL server.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws WolfSSLJNIException Internal JNI error
*/
public int setTmpDH(byte[] p, int pSz, byte[] g, int gSz)
throws IllegalStateException, WolfSSLJNIException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered setTmpDh(pSz: " + pSz + ", gSz: " + gSz + ")");
return setTmpDH(getContextPtr(), p, pSz, g, gSz);
}
}
/**
* Sets up the group parameters from the specified file to be used if the
* server negotiates a cipher suite that uses DHE.
*
* @param fname path to Diffie-Hellman parameter file
* @param format format of DH parameter file, either
* <code>SSL_FILETYPE_ASN1</code> or <code>
* SSL_FILETYPE_PEM</code>.
* @return <code>SSL_SUCCESS</code> on success. <code>MEMORY_E
* </code> if a memory error was encountered. <code>
* SIDE_ERROR</code> if this function is called on an
* SSL client instead of an SSL server, <code>
* SSL_BAD_FILETYPE</code> if the specified format is
* incorrect, <code>SSL_BAD_FILE</code> if there is a
* problem with the input file.
* @throws IllegalStateException WolfSSLContext has been freed
* @throws WolfSSLJNIException Internal JNI error
* @see #setTmpDH(byte[], int, byte[], int)
*/
public int setTmpDHFile(String fname, int format)
throws IllegalStateException, WolfSSLJNIException {
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, getContextPtr(),
() -> "entered setTmpDhFile(" + fname + ", " + format + ")");
return setTmpDHFile(getContextPtr(), fname, format);
}
}
/**
* Loads a CA certificate buffer into the SSL context.
* This method behaves like the non-buffered version, only differing in its
* ability to be called with a buffer as input instead of a file. The
* buffer is provided by the <b>in</b> parameter of size <b>sz</b>.
* <b>format</b> specifies the format type of the buffer, either
* <b>SSL_FILETYPE_PEM</b> or <b>SSL_FILETYPE_ASN1</b>. More than one
* CA certificate may be loaded per buffer as long as the format is in
* PEM format.
*
* @param in input buffer containing CA certificate to load
* @param sz size of the input buffer, <b>in</b>
* @param format format of the certificate buffer being loaded - either
* <b>SSL_FILETYPE_PEM</b> or <b>SSL_FILETYPE_ASN1</b>
* @return <b><code>SSL_SUCCESS</code></b> upon success,
* <b><code>SSL_FAILURE</code></b> upon general failure,
* <b><code>SSL_BAD_FILETYPE</code></b> if the file is
* in the wrong format, <b><code>SSL_BAD_FILE</code></b>
* if the file doesn't exist, can't be read, or is
* corrupted. <b><code>MEMORY_E</code></b> if an out of
* memory condition occurs, <b><code>ASN_INPUT_E</code></b>
* if Base16 decoding fails on the file,
* <b><code>BUFFER_E</code></b> will be returned if a
* chain buffer is bigger than the receiving buffer, and
* <b><code>BAD_FUNC_ARG</code></b> will be returned
* if invalid arguments are provided.
*
* @throws IllegalStateException WolfSSLContext has been freed
* @throws WolfSSLJNIException Internal JNI error
* @see #loadVerifyLocations(String, String)