File: SSLEngineTestCase.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (1174 lines) | stat: -rw-r--r-- 48,780 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
/*
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SNIHostName;
import javax.net.ssl.SNIMatcher;
import javax.net.ssl.SNIServerName;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Basic class to inherit SSLEngine test cases from it. Tests apply for
 * the TLS or DTLS security protocols and their versions.
 */
abstract public class SSLEngineTestCase {

    public enum Ciphers {

        /**
         * Ciphers supported by the tested SSLEngine without those with
         * kerberos authentication.
         */
        SUPPORTED_NON_KRB_CIPHERS(SSLEngineTestCase.SUPPORTED_NON_KRB_CIPHERS,
                "Supported non kerberos"),
        /**
         * Ciphers supported by the tested SSLEngine without those with
         * kerberos authentication and without those with SHA256 ans SHA384.
         */
        SUPPORTED_NON_KRB_NON_SHA_CIPHERS(
                SSLEngineTestCase.SUPPORTED_NON_KRB_NON_SHA_CIPHERS,
                "Supported non kerberos non SHA256 and SHA384"),
        /**
         * Ciphers supported by the tested SSLEngine with kerberos
         * authentication.
         */
        SUPPORTED_KRB_CIPHERS(SSLEngineTestCase.SUPPORTED_KRB_CIPHERS,
                "Supported kerberos"),
        /**
         * Ciphers enabled by default for the tested SSLEngine without kerberos
         * and anon.
         */
        ENABLED_NON_KRB_NOT_ANON_CIPHERS(
                SSLEngineTestCase.ENABLED_NON_KRB_NOT_ANON_CIPHERS,
                "Enabled by default non kerberos not anonymous"),
        /**
         * Ciphers supported by TLS 1.3 only.
         */
        TLS13_CIPHERS(
                SSLEngineTestCase.TLS13_CIPHERS,
                "Supported by TLS 1.3 only"),
        /**
         * Ciphers unsupported by the tested SSLEngine.
         */
        UNSUPPORTED_CIPHERS(SSLEngineTestCase.UNSUPPORTED_CIPHERS,
                "Unsupported");

        Ciphers(String[] ciphers, String description) {
            this.ciphers = ciphers;
            this.description = description;
        }

        final String[] ciphers;
        final String description;
    }

    /**
     * Enumeration used to distinguish handshake mode in
     * {@link SSLEngineTestCase#doHandshake(javax.net.ssl.SSLEngine,
     * javax.net.ssl.SSLEngine, int, SSLEngineTestCase.HandshakeMode, boolean)
     * SSLEngineTestCase.doHandshake} method.
     */
    public enum HandshakeMode {

        /**
         * Initial handshake done for the first time: both engines call
         * {@link SSLEngine#beginHandshake()} method.
         */
        INITIAL_HANDSHAKE,
        /**
         * Repeated handshake done by client: client engine calls
         * {@link SSLEngine#beginHandshake()} method.
         */
        REHANDSHAKE_BEGIN_CLIENT,
        /**
         * Repeated handshake done by server: server engine calls
         * {@link SSLEngine#beginHandshake()} method.
         */
        REHANDSHAKE_BEGIN_SERVER;
    }
    /**
     * Security protocol to be tested: "TLS" or "DTLS" or their versions,
     * e.g. "TLSv1", "TLSv1.1", "TLSv1.2", "DTLSv1.0", "DTLSv1.2".
     */
    public static final String TESTED_SECURITY_PROTOCOL
            = System.getProperty("test.security.protocol", "TLS");
    /**
     * Test mode: "norm", "norm_sni" or "krb".
     * Modes "norm" and "norm_sni" are used to run
     * with all supported non-kerberos ciphers.
     * Mode "krb" is used to run with kerberos ciphers.
     */
    public static final String TEST_MODE
            = System.getProperty("test.mode", "norm");

    private static final String FS = System.getProperty("file.separator", "/");
    private static final String PATH_TO_STORES = ".." + FS + "etc";
    private static final String KEY_STORE_FILE = "keystore";
    private static final String TRUST_STORE_FILE = "truststore";
    private static final String PASSWD = "passphrase";

    private static final String KEY_FILE_NAME
            = System.getProperty("test.src", ".") + FS + PATH_TO_STORES
            + FS + KEY_STORE_FILE;
    private static final String TRUST_FILE_NAME
            = System.getProperty("test.src", ".") + FS + PATH_TO_STORES
            + FS + TRUST_STORE_FILE;

    // Need an enhancement to use none-static mutable global variables.
    private static ByteBuffer net;
    private static boolean doUnwrapForNotHandshakingStatus;
    private static boolean endHandshakeLoop = false;

    private static final int MAX_HANDSHAKE_LOOPS = 100;
    private static final String EXCHANGE_MSG_SENT = "Hello, peer!";
    private static final String TEST_SRC = System.getProperty("test.src", ".");
    private static final String KTAB_FILENAME = "krb5.keytab.data";
    private static final String KRB_REALM = "TEST.REALM";
    private static final String KRBTGT_PRINCIPAL = "krbtgt/" + KRB_REALM;
    private static final String KRB_USER = "USER";
    private static final String KRB_USER_PASSWORD = "password";
    private static final String KRB_USER_PRINCIPAL = KRB_USER + "@" + KRB_REALM;
    private static final String KRB5_CONF_FILENAME = "krb5.conf";
    private static final String PATH_TO_COMMON = ".." + FS + "TLSCommon";
    private static final String JAAS_CONF_FILE = PATH_TO_COMMON
            + FS + "jaas.conf";
    private static final int DELAY = 1000;
    private static final String HOST = "localhost";
    private static final String SERVER_NAME = "service.localhost";
    private static final String SNI_PATTERN = ".*";

    private static final String[] TLS13_CIPHERS = {
            "TLS_AES_256_GCM_SHA384",
            "TLS_AES_128_GCM_SHA256"
    };

    private static final String[] SUPPORTED_NON_KRB_CIPHERS;

    static {
        try {
            String[] allSupportedCiphers = getContext()
                    .createSSLEngine().getSupportedCipherSuites();
            List<String> supportedCiphersList = new LinkedList<>();
            for (String cipher : allSupportedCiphers) {
                if (!cipher.contains("KRB5")
                        && !isTLS13Cipher(cipher)
                        && !cipher.contains("TLS_EMPTY_RENEGOTIATION_INFO_SCSV")) {
                    supportedCiphersList.add(cipher);
                }
            }
            SUPPORTED_NON_KRB_CIPHERS =
                    supportedCiphersList.toArray(new String[0]);
        } catch (Exception ex) {
            throw new Error("Unexpected issue", ex);
        }
    }

    private static final String[] SUPPORTED_NON_KRB_NON_SHA_CIPHERS;

    static {
        try {
            String[] allSupportedCiphers = getContext()
                    .createSSLEngine().getSupportedCipherSuites();
            List<String> supportedCiphersList = new LinkedList<>();
            for (String cipher : allSupportedCiphers) {
                if (!cipher.contains("KRB5")
                        && !isTLS13Cipher(cipher)
                        && !cipher.contains("TLS_EMPTY_RENEGOTIATION_INFO_SCSV")
                        && !cipher.endsWith("_SHA256")
                        && !cipher.endsWith("_SHA384")) {
                    supportedCiphersList.add(cipher);
                }
            }
            SUPPORTED_NON_KRB_NON_SHA_CIPHERS
                    = supportedCiphersList.toArray(new String[0]);
        } catch (Exception ex) {
            throw new Error("Unexpected issue", ex);
        }
    }

    private static final String[] SUPPORTED_KRB_CIPHERS;

    static {
        try {
            String[] allSupportedCiphers = getContext()
                    .createSSLEngine().getSupportedCipherSuites();
            List<String> supportedCiphersList = new LinkedList<>();
            for (String cipher : allSupportedCiphers) {
                if (cipher.contains("KRB5")
                        && !isTLS13Cipher(cipher)
                        && !cipher.contains("TLS_EMPTY_RENEGOTIATION_INFO_SCSV")) {
                    supportedCiphersList.add(cipher);
                }
            }
            SUPPORTED_KRB_CIPHERS = supportedCiphersList.toArray(new String[0]);
        } catch (Exception ex) {
            throw new Error("Unexpected issue", ex);
        }
    }

    private static final String[] ENABLED_NON_KRB_NOT_ANON_CIPHERS;

    static {
        try {
            SSLEngine temporary = getContext().createSSLEngine();
            temporary.setUseClientMode(true);
            String[] enabledCiphers = temporary.getEnabledCipherSuites();
            List<String> enabledCiphersList = new LinkedList<>();
            for (String cipher : enabledCiphers) {
                if (!cipher.contains("anon") && !cipher.contains("KRB5")
                        && !isTLS13Cipher(cipher)
                        && !cipher.contains("TLS_EMPTY_RENEGOTIATION_INFO_SCSV")) {
                    enabledCiphersList.add(cipher);
                }
            }
            ENABLED_NON_KRB_NOT_ANON_CIPHERS =
                    enabledCiphersList.toArray(new String[0]);
        } catch (Exception ex) {
            throw new Error("Unexpected issue", ex);
        }
    }

    private static final String[] UNSUPPORTED_CIPHERS = {
            "SSL_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA",
            "SSL_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA",
            "SSL_DHE_DSS_WITH_RC4_128_SHA",
            "SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA",
            "SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA",
            "SSL_DH_DSS_WITH_DES_CBC_SHA",
            "SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA",
            "SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA",
            "SSL_DH_RSA_WITH_DES_CBC_SHA",
            "SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA",
            "SSL_FORTEZZA_DMS_WITH_NULL_SHA",
            "SSL_RSA_EXPORT1024_WITH_DES_CBC_SHA",
            "SSL_RSA_EXPORT1024_WITH_RC4_56_SHA",
            "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5",
            "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA",
            "SSL_RSA_FIPS_WITH_DES_CBC_SHA",
            "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5",
            "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA",
            "TLS_KRB5_WITH_IDEA_CBC_MD5",
            "TLS_KRB5_WITH_IDEA_CBC_SHA",
            "SSL_RSA_WITH_IDEA_CBC_SHA",
            "TLS_DH_RSA_WITH_AES_128_GCM_SHA256",
            "TLS_DH_RSA_WITH_AES_256_GCM_SHA384",
            "TLS_DH_DSS_WITH_AES_128_GCM_SHA256",
            "TLS_DH_DSS_WITH_AES_256_GCM_SHA384"
    };

    private final int maxPacketSize;

    /**
     * Constructs test case with the given MFLN maxMacketSize.
     *
     * @param maxPacketSize - MLFN extension max packet size.
     */
    public SSLEngineTestCase(int maxPacketSize) {
        this.maxPacketSize = maxPacketSize;
    }

    /**
     * Constructs test case with {@code maxPacketSize = 0}.
     */
    public SSLEngineTestCase() {
        this.maxPacketSize = 0;
    }

    private static boolean isTLS13Cipher(String cipher) {
        for (String cipherSuite : TLS13_CIPHERS) {
            if (cipherSuite.equals(cipher)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Wraps data with the specified engine.
     *
     * @param engine        - SSLEngine that wraps data.
     * @param wrapper       - Set wrapper id, e.g. "server" of "client".
     *                        Used for logging only.
     * @param maxPacketSize - Max packet size to check that MFLN extension
     *                        works or zero for no check.
     * @param app           - Buffer with data to wrap.
     * @return - Buffer with wrapped data.
     * @throws SSLException - thrown on engine errors.
     */
    public static ByteBuffer doWrap(SSLEngine engine, String wrapper,
                                    int maxPacketSize, ByteBuffer app)
            throws SSLException {
        return doWrap(engine, wrapper, maxPacketSize,
                app, SSLEngineResult.Status.OK, null);
    }

    /**
     * Wraps data with the specified engine.
     *
     * @param engine        - SSLEngine that wraps data.
     * @param wrapper       - Set wrapper id, e.g. "server" of "client".
     *                        Used for logging only.
     * @param maxPacketSize - Max packet size to check that MFLN extension
     *                        works or zero for no check.
     * @param app           - Buffer with data to wrap.
     * @param result        - Array which first element will be used to
     *                        output wrap result object.
     * @return - Buffer with wrapped data.
     * @throws SSLException - thrown on engine errors.
     */
    public static ByteBuffer doWrap(SSLEngine engine, String wrapper,
                                    int maxPacketSize, ByteBuffer app,
                                    SSLEngineResult[] result)
            throws SSLException {
        return doWrap(engine, wrapper, maxPacketSize,
                app, SSLEngineResult.Status.OK, result);
    }

    /**
     * Wraps data with the specified engine.
     *
     * @param engine        - SSLEngine that wraps data.
     * @param wrapper       - Set wrapper id, e.g. "server" of "client".
     *                        Used for logging only.
     * @param maxPacketSize - Max packet size to check that MFLN extension
     *                        works or zero for no check.
     * @param app           - Buffer with data to wrap.
     * @param wantedStatus  - Specifies expected result status of wrapping.
     * @return - Buffer with wrapped data.
     * @throws SSLException - thrown on engine errors.
     */
    public static ByteBuffer doWrap(SSLEngine engine, String wrapper,
                                    int maxPacketSize, ByteBuffer app,
                                    SSLEngineResult.Status wantedStatus)
            throws SSLException {
        return doWrap(engine, wrapper, maxPacketSize,
                app, wantedStatus, null);
    }

    /**
     * Wraps data with the specified engine.
     *
     * @param engine        - SSLEngine that wraps data.
     * @param wrapper       - Set wrapper id, e.g. "server" of "client".
     *                        Used for logging only.
     * @param maxPacketSize - Max packet size to check that MFLN extension
     *                        works or zero for no check.
     * @param app           - Buffer with data to wrap.
     * @param wantedStatus  - Specifies expected result status of wrapping.
     * @param result        - Array which first element will be used to output
     *                        wrap result object.
     * @return - Buffer with wrapped data.
     * @throws SSLException - thrown on engine errors.
     */
    public static ByteBuffer doWrap(SSLEngine engine, String wrapper,
                                    int maxPacketSize, ByteBuffer app,
                                    SSLEngineResult.Status wantedStatus,
                                    SSLEngineResult[] result)
            throws SSLException {
        ByteBuffer net = ByteBuffer.allocate(engine.getSession()
                .getPacketBufferSize());
        SSLEngineResult r = engine.wrap(app, net);
        net.flip();
        int length = net.remaining();
        System.out.println(wrapper + " wrapped " + length + " bytes.");
        System.out.println(wrapper + " handshake status is "
                + engine.getHandshakeStatus() + " Result is " + r.getStatus());
        if (maxPacketSize < length && maxPacketSize != 0) {
            throw new AssertionError("Handshake wrapped net buffer length "
                    + length + " exceeds maximum packet size "
                    + maxPacketSize);
        }
        checkResult(r, wantedStatus);
        if (result != null && result.length > 0) {
            result[0] = r;
        }
        return net;
    }

    /**
     * Unwraps data with the specified engine.
     *
     * @param engine    - SSLEngine that unwraps data.
     * @param unwrapper - Set unwrapper id, e.g. "server" of "client". Used for
     *                  logging only.
     * @param net       - Buffer with data to unwrap.
     * @return - Buffer with unwrapped data.
     * @throws SSLException - thrown on engine errors.
     */
    public static ByteBuffer doUnWrap(SSLEngine engine, String unwrapper,
            ByteBuffer net) throws SSLException {
        return doUnWrap(engine, unwrapper,
                net, SSLEngineResult.Status.OK, null);
    }

    /**
     * Unwraps data with the specified engine.
     *
     * @param engine    - SSLEngine that unwraps data.
     * @param unwrapper - Set unwrapper id, e.g. "server" of "client". Used for
     *                  logging only.
     * @param net       - Buffer with data to unwrap.
     * @param result    - Array which first element will be used to output wrap
     *                  result object.
     * @return - Buffer with unwrapped data.
     * @throws SSLException - thrown on engine errors.
     */
    public static ByteBuffer doUnWrap(SSLEngine engine, String unwrapper,
            ByteBuffer net, SSLEngineResult[] result) throws SSLException {
        return doUnWrap(engine, unwrapper,
                net, SSLEngineResult.Status.OK, result);
    }

    /**
     * Unwraps data with the specified engine.
     *
     * @param engine       - SSLEngine that unwraps data.
     * @param unwrapper    - Set unwrapper id, e.g. "server" of "client".
     *                     Used for logging only.
     * @param net          - Buffer with data to unwrap.
     * @param wantedStatus - Specifies expected result status of wrapping.
     * @return - Buffer with unwrapped data.
     * @throws SSLException - thrown on engine errors.
     */
    public static ByteBuffer doUnWrap(SSLEngine engine, String unwrapper,
            ByteBuffer net,
            SSLEngineResult.Status wantedStatus) throws SSLException {
        return doUnWrap(engine, unwrapper, net, wantedStatus, null);
    }

    /**
     * Unwraps data with the specified engine.
     *
     * @param engine       - SSLEngine that unwraps data.
     * @param unwrapper    - Set unwrapper id, e.g. "server" of "client".
     *                       Used for logging only.
     * @param net          - Buffer with data to unwrap.
     * @param wantedStatus - Specifies expected result status of wrapping.
     * @param result       - Array which first element will be used to output
     *                       wrap result object.
     * @return - Buffer with unwrapped data.
     * @throws SSLException - thrown on engine errors.
     */
    public static ByteBuffer doUnWrap(SSLEngine engine, String unwrapper,
            ByteBuffer net, SSLEngineResult.Status wantedStatus,
            SSLEngineResult[] result) throws SSLException {

        ByteBuffer app = ByteBuffer.allocate(
                engine.getSession().getApplicationBufferSize());
        int length = net.remaining();
        System.out.println(unwrapper + " unwrapping " + length + " bytes...");
        SSLEngineResult r = engine.unwrap(net, app);
        app.flip();
        System.out.println(unwrapper + " handshake status is "
                + engine.getHandshakeStatus() + " Result is " + r.getStatus());
        checkResult(r, wantedStatus);
        if (result != null && result.length > 0) {
            result[0] = r;
        }
        return app;
    }

    /**
     * Does the handshake of the two specified engines according to the
     * {@code mode} specified.
     *
     * @param clientEngine  - Client SSLEngine.
     * @param serverEngine  - Server SSLEngine.
     * @param maxPacketSize - Maximum packet size for MFLN of zero for no limit.
     * @param mode          - Handshake mode according to
     *                        {@link HandshakeMode} enum.
     * @throws SSLException - thrown on engine errors.
     */
    public static void doHandshake(SSLEngine clientEngine,
        SSLEngine serverEngine,
        int maxPacketSize, HandshakeMode mode) throws SSLException {

        doHandshake(clientEngine, serverEngine, maxPacketSize, mode, false);
    }

    /**
     * Does the handshake of the two specified engines according to the
     * {@code mode} specified.
     *
     * @param clientEngine          - Client SSLEngine.
     * @param serverEngine          - Server SSLEngine.
     * @param maxPacketSize         - Maximum packet size for MFLN of zero
     *                                for no limit.
     * @param mode                  - Handshake mode according to
     *                                {@link HandshakeMode} enum.
     * @param enableReplicatedPacks - Set {@code true} to enable replicated
     *                                packet sending.
     * @throws SSLException - thrown on engine errors.
     */
    public static void doHandshake(SSLEngine clientEngine,
            SSLEngine serverEngine, int maxPacketSize,
            HandshakeMode mode,
            boolean enableReplicatedPacks) throws SSLException {

        System.out.println("=============================================");
        System.out.println("Starting handshake " + mode.name());
        int loop = 0;
        if (maxPacketSize < 0) {
            throw new Error("Test issue: maxPacketSize is less than zero!");
        }
        SSLParameters params = clientEngine.getSSLParameters();
        params.setMaximumPacketSize(maxPacketSize);
        clientEngine.setSSLParameters(params);
        params = serverEngine.getSSLParameters();
        params.setMaximumPacketSize(maxPacketSize);
        serverEngine.setSSLParameters(params);
        SSLEngine firstEngine;
        SSLEngine secondEngine;
        switch (mode) {
            case INITIAL_HANDSHAKE:
                firstEngine = clientEngine;
                secondEngine = serverEngine;
                doUnwrapForNotHandshakingStatus = false;
                clientEngine.beginHandshake();
                serverEngine.beginHandshake();
                break;
            case REHANDSHAKE_BEGIN_CLIENT:
                firstEngine = clientEngine;
                secondEngine = serverEngine;
                doUnwrapForNotHandshakingStatus = true;
                clientEngine.beginHandshake();
                break;
            case REHANDSHAKE_BEGIN_SERVER:
                firstEngine = serverEngine;
                secondEngine = clientEngine;
                doUnwrapForNotHandshakingStatus = true;
                serverEngine.beginHandshake();
                break;
            default:
                throw new Error("Test issue: unknown handshake mode");
        }
        endHandshakeLoop = false;
        while (!endHandshakeLoop) {
            if (++loop > MAX_HANDSHAKE_LOOPS) {
                throw new Error("Too much loops for handshaking");
            }
            System.out.println("============================================");
            System.out.println("Handshake loop " + loop + ": round 1");
            System.out.println("==========================");
            handshakeProcess(firstEngine, secondEngine, maxPacketSize,
                    enableReplicatedPacks);
            if (endHandshakeLoop) {
                break;
            }
            System.out.println("Handshake loop " + loop + ": round 2");
            System.out.println("==========================");
            handshakeProcess(secondEngine, firstEngine, maxPacketSize,
                    enableReplicatedPacks);
        }
    }

    /**
     * Routine to send application data from one SSLEngine to another.
     *
     * @param fromEngine - Sending engine.
     * @param toEngine   - Receiving engine.
     * @return - Result of unwrap method of the receiving engine.
     * @throws SSLException - thrown on engine errors.
     */
    public static SSLEngineResult sendApplicationData(SSLEngine fromEngine,
                                                      SSLEngine toEngine)
            throws SSLException {
        String sender = null;
        String reciever = null;
        String excMsgSent = EXCHANGE_MSG_SENT;
        if (fromEngine.getUseClientMode() && !toEngine.getUseClientMode()) {
            sender = "Client";
            reciever = "Server";
            excMsgSent += " Client.";
        } else if (toEngine.getUseClientMode() &&
                !fromEngine.getUseClientMode()) {
            sender = "Server";
            reciever = "Client";
            excMsgSent += " Server.";
        } else {
            throw new Error("Test issue: both engines are in the same mode");
        }
        System.out.println("=============================================");
        System.out.println("Trying to send application data from " + sender
                + " to " + reciever);
        ByteBuffer clientAppSent
                = ByteBuffer.wrap(excMsgSent.getBytes());
        net = doWrap(fromEngine, sender, 0, clientAppSent);
        SSLEngineResult[] r = new SSLEngineResult[1];
        ByteBuffer serverAppRecv = doUnWrap(toEngine, reciever, net, r);
        byte[] serverAppRecvTrunc = Arrays.copyOf(serverAppRecv.array(),
                serverAppRecv.limit());
        String msgRecv = new String(serverAppRecvTrunc);
        if (!msgRecv.equals(excMsgSent)) {
            throw new AssertionError(sender + " to " + reciever
                    + ": application data"
                    + " has been altered while sending."
                    + " Message sent: " + "\"" + excMsgSent + "\"."
                    + " Message recieved: " + "\"" + msgRecv + "\".");
        }
        System.out.println("Successful sending application data from " + sender
                + " to " + reciever);
        return r[0];
    }

    /**
     * Close engines by sending "close outbound" message from one SSLEngine to
     * another.
     *
     * @param fromEngine - Sending engine.
     * @param toEngine   - Receiving engine.
     * @throws SSLException - thrown on engine errors.
     */
    public static void closeEngines(SSLEngine fromEngine,
                                    SSLEngine toEngine) throws SSLException {
        String from = null;
        String to = null;
        ByteBuffer app;
        if (fromEngine.getUseClientMode() && !toEngine.getUseClientMode()) {
            from = "Client";
            to = "Server";
        } else if (toEngine.getUseClientMode() &&
                !fromEngine.getUseClientMode()) {
            from = "Server";
            to = "Client";
        } else {
            throw new Error("Both engines are in the same mode");
        }
        System.out.println("=============================================");
        System.out.println(
                "Trying to close engines from " + from + " to " + to);
        // Sending close outbound request to peer
        fromEngine.closeOutbound();
        app = ByteBuffer.allocate(
                fromEngine.getSession().getApplicationBufferSize());
        net = doWrap(fromEngine, from, 0, app, SSLEngineResult.Status.CLOSED);
        doUnWrap(toEngine, to, net, SSLEngineResult.Status.CLOSED);
        app = ByteBuffer.allocate(
                fromEngine.getSession().getApplicationBufferSize());
        net = doWrap(toEngine, to, 0, app, SSLEngineResult.Status.CLOSED);
        doUnWrap(fromEngine, from, net, SSLEngineResult.Status.CLOSED);
        if (!toEngine.isInboundDone()) {
            throw new AssertionError(from + " sent close request to " + to
                    + ", but " + to + "did not close inbound.");
        }
        // Executing close inbound
        fromEngine.closeInbound();
        app = ByteBuffer.allocate(
                fromEngine.getSession().getApplicationBufferSize());
        net = doWrap(fromEngine, from, 0, app, SSLEngineResult.Status.CLOSED);
        doUnWrap(toEngine, to, net, SSLEngineResult.Status.CLOSED);
        if (!toEngine.isOutboundDone()) {
            throw new AssertionError(from + "sent close request to " + to
                    + ", but " + to + "did not close outbound.");
        }
        System.out.println("Successful closing from " + from + " to " + to);
    }

    /**
     * Runs the same test case for all given {@code ciphers}. Method counts all
     * failures and throws {@code AssertionError} if one or more tests fail.
     *
     * @param ciphers - Ciphers that should be tested.
     */
    public void runTests(Ciphers ciphers) {
        int total = ciphers.ciphers.length;
        int failed = testSomeCiphers(ciphers);
        if (failed > 0) {
            throw new AssertionError("" + failed + " of " + total
                    + " tests failed!");
        }
        System.out.println("All tests passed!");
    }

    /**
     * Runs test cases for ciphers defined by the test mode.
     */
    public void runTests() {
        switch (TEST_MODE) {
            case "norm":
            case "norm_sni":
                switch (TESTED_SECURITY_PROTOCOL) {
                    case "DTLSv1.0":
                    case "TLSv1":
                    case "TLSv1.1":
                        runTests(Ciphers.SUPPORTED_NON_KRB_NON_SHA_CIPHERS);
                        break;
                    case "DTLSv1.1":
                    case "TLSv1.2":
                        runTests(Ciphers.SUPPORTED_NON_KRB_CIPHERS);
                        break;
                    case "TLSv1.3":
                        runTests(Ciphers.TLS13_CIPHERS);
                        break;
                }
                break;
            case "krb":
                runTests(Ciphers.SUPPORTED_KRB_CIPHERS);
                break;
            default:
                throw new Error(
                        "Test error: unexpected test mode: " + TEST_MODE);
        }
    }

    /**
     * Returns maxPacketSize value used for MFLN extension testing
     *
     * @return - MLFN extension max packet size.
     */
    public int getMaxPacketSize() {
        return maxPacketSize;
    }

    /**
     * Checks that status of result {@code r} is {@code wantedStatus}.
     *
     * @param r            - Result.
     * @param wantedStatus - Wanted status of the result.
     * @throws AssertionError - if status or {@code r} is not
     *                        {@code wantedStatus}.
     */
    public static void checkResult(SSLEngineResult r,
                                   SSLEngineResult.Status wantedStatus) {
        SSLEngineResult.Status rs = r.getStatus();
        if (!rs.equals(wantedStatus)) {
            throw new AssertionError("Unexpected status " + rs.name()
                    + ", should be " + wantedStatus.name());
        }
    }

    /**
     * Returns SSLContext with TESTED_SECURITY_PROTOCOL protocol and
     * sets up keys.
     *
     * @return - SSLContext with a protocol specified by
     *           TESTED_SECURITY_PROTOCOL.
     */
    public static SSLContext getContext() {
        try {
            java.security.Security.setProperty(
                    "jdk.tls.disabledAlgorithms", "");
            java.security.Security.setProperty(
                    "jdk.certpath.disabledAlgorithms", "");
            KeyStore ks = KeyStore.getInstance("JKS");
            KeyStore ts = KeyStore.getInstance("JKS");
            char[] passphrase = PASSWD.toCharArray();
            try (FileInputStream keyFileStream =
                    new FileInputStream(KEY_FILE_NAME)) {
                ks.load(keyFileStream, passphrase);
            }
            try (FileInputStream trustFileStream =
                    new FileInputStream(TRUST_FILE_NAME)) {
                ts.load(trustFileStream, passphrase);
            }
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            kmf.init(ks, passphrase);
            TrustManagerFactory tmf =
                    TrustManagerFactory.getInstance("SunX509");
            tmf.init(ts);
            SSLContext sslCtx =
                    SSLContext.getInstance(TESTED_SECURITY_PROTOCOL);
            sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
            return sslCtx;
        } catch (KeyStoreException | IOException | NoSuchAlgorithmException |
                CertificateException | UnrecoverableKeyException |
                KeyManagementException ex) {
            throw new Error("Unexpected exception", ex);
        }
    }

    /**
     * Sets up and starts kerberos KDC server.
     */
    public static void setUpAndStartKDC() {
        String servicePrincipal = "host/" + SERVER_NAME + "@" + KRB_REALM;
        Map<String, String> principals = new HashMap<>();
        principals.put(KRB_USER_PRINCIPAL, KRB_USER_PASSWORD);
        principals.put(KRBTGT_PRINCIPAL, null);
        principals.put(servicePrincipal, null);
        System.setProperty("java.security.krb5.conf", KRB5_CONF_FILENAME);
        startKDC(KRB_REALM, principals, KTAB_FILENAME);
        System.setProperty("java.security.auth.login.config",
                TEST_SRC + FS + JAAS_CONF_FILE);
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    }

    /**
     * Sets up and starts kerberos KDC server if
     * SSLEngineTestCase.TEST_MODE is "krb".
     */
    public static void setUpAndStartKDCIfNeeded() {
        if (TEST_MODE.equals("krb")) {
            setUpAndStartKDC();
        }
    }

    /**
     * Returns client ssl engine.
     *
     * @param context - SSLContext to get SSLEngine from.
     * @param useSNI  - flag used to enable or disable using SNI extension.
     *                Needed for Kerberos.
     */
    public static SSLEngine getClientSSLEngine(
            SSLContext context, boolean useSNI) {

        SSLEngine clientEngine = context.createSSLEngine(HOST, 80);
        clientEngine.setUseClientMode(true);
        if (useSNI) {
            SNIHostName serverName = new SNIHostName(SERVER_NAME);
            List<SNIServerName> serverNames = new ArrayList<>();
            serverNames.add(serverName);
            SSLParameters params = clientEngine.getSSLParameters();
            params.setServerNames(serverNames);
            clientEngine.setSSLParameters(params);
        }
        return clientEngine;
    }

    /**
     * Returns server ssl engine.
     *
     * @param context - SSLContext to get SSLEngine from.
     * @param useSNI  - flag used to enable or disable using SNI extension.
     *                Needed for Kerberos.
     */
    public static SSLEngine getServerSSLEngine(
            SSLContext context, boolean useSNI) {

        SSLEngine serverEngine = context.createSSLEngine();
        serverEngine.setUseClientMode(false);
        if (useSNI) {
            SNIMatcher matcher = SNIHostName.createSNIMatcher(SNI_PATTERN);
            List<SNIMatcher> matchers = new ArrayList<>();
            matchers.add(matcher);
            SSLParameters params = serverEngine.getSSLParameters();
            params.setSNIMatchers(matchers);
            serverEngine.setSSLParameters(params);
        }
        return serverEngine;
    }

    /**
     * Runs the test case for one cipher suite.
     *
     * @param cipher - Cipher suite name.
     * @throws SSLException - If tests fails.
     */
    abstract protected void testOneCipher(String cipher)
            throws SSLException;

    /**
     * Iterates through an array of ciphers and runs the same test case for
     * every entry.
     *
     * @param ciphers - Array of cipher names.
     * @return - Number of tests failed.
     */
    protected int testSomeCiphers(Ciphers ciphers) {
        int failedNum = 0;
        String description = ciphers.description;
        System.out.println("===============================================");
        System.out.println(description + " ciphers testing");
        System.out.println("===========================================");
        for (String cs : ciphers.ciphers) {
            System.out.println("---------------------------------------");
            System.out.println("Testing cipher suite " + cs);
            System.out.println("---------------------------------------");
            Throwable error = null;

            // Reset global mutable static variables
            net = null;
            doUnwrapForNotHandshakingStatus = false;
            endHandshakeLoop = false;

            try {
                testOneCipher(cs);
            } catch (Throwable t) {
                error = t;
            }
            switch (ciphers) {
                case SUPPORTED_NON_KRB_CIPHERS:
                case SUPPORTED_NON_KRB_NON_SHA_CIPHERS:
                case SUPPORTED_KRB_CIPHERS:
                case ENABLED_NON_KRB_NOT_ANON_CIPHERS:
                case TLS13_CIPHERS:
                    if (error != null) {
                        System.out.println("Test Failed: " + cs);
                        System.err.println("Test Exception for " + cs);
                        error.printStackTrace();
                        failedNum++;
                    } else {
                        System.out.println("Test Passed: " + cs);
                    }
                    break;
                case UNSUPPORTED_CIPHERS:
                    if (error == null) {
                        System.out.println("Test Failed: " + cs);
                        System.err.println("Test for " + cs +
                                " should have thrown " +
                                "IllegalArgumentException, but it has not!");
                        failedNum++;
                    } else if (!(error instanceof IllegalArgumentException)) {
                        System.out.println("Test Failed: " + cs);
                        System.err.println("Test Exception for " + cs);
                        error.printStackTrace();
                        failedNum++;
                    } else {
                        System.out.println("Test Passed: " + cs);
                    }
                    break;
                default:
                    throw new Error("Test issue: unexpected ciphers: "
                            + ciphers.name());
            }
        }

        return failedNum;
    }

    /**
     * Method used for the handshake routine.
     *
     * @param wrapingEngine         - Engine that is expected to wrap data.
     * @param unwrapingEngine       - Engine that is expected to unwrap data.
     * @param maxPacketSize         - Maximum packet size for MFLN of zero
     *                                for no limit.
     * @param enableReplicatedPacks - Set {@code true} to enable replicated
     *                                packet sending.
     * @throws SSLException - thrown on engine errors.
     */
    private static void handshakeProcess(SSLEngine wrapingEngine,
            SSLEngine unwrapingEngine,
            int maxPacketSize,
            boolean enableReplicatedPacks) throws SSLException {

        HandshakeStatus wrapingHSStatus = wrapingEngine.getHandshakeStatus();
        HandshakeStatus unwrapingHSStatus =
                unwrapingEngine.getHandshakeStatus();
        SSLEngineResult r;
        String wrapper, unwrapper;
        if (wrapingEngine.getUseClientMode()
                && !unwrapingEngine.getUseClientMode()) {
            wrapper = "Client";
            unwrapper = "Server";
        } else if (unwrapingEngine.getUseClientMode()
                && !wrapingEngine.getUseClientMode()) {
            wrapper = "Server";
            unwrapper = "Client";
        } else {
            throw new Error("Both engines are in the same mode");
        }
        System.out.println(
                wrapper + " handshake (wrap) status " + wrapingHSStatus);
        System.out.println(
                unwrapper + " handshake (unwrap) status " + unwrapingHSStatus);

        ByteBuffer netReplicatedClient = null;
        ByteBuffer netReplicatedServer = null;
        switch (wrapingHSStatus) {
            case NEED_WRAP:
                if (enableReplicatedPacks) {
                    if (net != null) {
                        net.flip();
                        if (net.remaining() != 0) {
                            if (wrapingEngine.getUseClientMode()) {
                                netReplicatedServer = net;
                            } else {
                                netReplicatedClient = net;
                            }
                        }
                    }
                }
                ByteBuffer app = ByteBuffer.allocate(
                        wrapingEngine.getSession().getApplicationBufferSize());
                net = doWrap(wrapingEngine, wrapper, maxPacketSize, app);
                wrapingHSStatus = wrapingEngine.getHandshakeStatus();
                // No break, falling into unwrapping.
            case NOT_HANDSHAKING:
                switch (unwrapingHSStatus) {
                    case NEED_TASK:
                        runDelegatedTasks(unwrapingEngine);
                    case NEED_UNWRAP:
                        doUnWrap(unwrapingEngine, unwrapper, net);
                        if (enableReplicatedPacks) {
                            System.out.println(unwrapper +
                                    " unwrapping replicated packet...");
                            if (unwrapingEngine.getHandshakeStatus()
                                    .equals(HandshakeStatus.NEED_TASK)) {
                                runDelegatedTasks(unwrapingEngine);
                            }
                            ByteBuffer netReplicated;
                            if (unwrapingEngine.getUseClientMode()) {
                                netReplicated = netReplicatedClient;
                            } else {
                                netReplicated = netReplicatedServer;
                            }
                            if (netReplicated != null) {
                                doUnWrap(unwrapingEngine,
                                        unwrapper, netReplicated);
                            } else {
                                net.flip();
                                doUnWrap(unwrapingEngine, unwrapper, net);
                            }
                        }
                        break;
                    case NEED_UNWRAP_AGAIN:
                        break;
                    case NOT_HANDSHAKING:
                        if (doUnwrapForNotHandshakingStatus) {
                            System.out.println("Not handshake status unwrap");
                            doUnWrap(unwrapingEngine, unwrapper, net);
                            doUnwrapForNotHandshakingStatus = false;
                            break;
                        } else {
                            if (wrapingHSStatus ==
                                        HandshakeStatus.NOT_HANDSHAKING) {
                                System.out.println("Handshake is completed");
                                endHandshakeLoop = true;
                            }
                        }
                        break;
                    case NEED_WRAP:
                        SSLSession session = unwrapingEngine.getSession();
                        int bufferSize = session.getApplicationBufferSize();
                        ByteBuffer b = ByteBuffer.allocate(bufferSize);
                        net = doWrap(unwrapingEngine,
                                        unwrapper, maxPacketSize, b);
                        unwrapingHSStatus =
                                unwrapingEngine.getHandshakeStatus();
                        if ((wrapingHSStatus ==
                                    HandshakeStatus.NOT_HANDSHAKING) &&
                            (unwrapingHSStatus ==
                                    HandshakeStatus.NOT_HANDSHAKING)) {

                            System.out.println("Handshake is completed");
                            endHandshakeLoop = true;
                        }

                        break;
                    default:
                        throw new Error(
                                "Unexpected unwraping engine handshake status "
                                + unwrapingHSStatus.name());
                }
                break;
            case NEED_UNWRAP:
                break;
            case NEED_UNWRAP_AGAIN:
                net.flip();
                doUnWrap(wrapingEngine, wrapper, net);
                break;
            case NEED_TASK:
                runDelegatedTasks(wrapingEngine);
                break;
            default:
                throw new Error("Unexpected wraping engine handshake status "
                        + wrapingHSStatus.name());
        }
    }

    private static void runDelegatedTasks(SSLEngine engine) {
        Runnable runnable;
        System.out.println("Running delegated tasks...");
        while ((runnable = engine.getDelegatedTask()) != null) {
            runnable.run();
        }
        HandshakeStatus hs = engine.getHandshakeStatus();
        if (hs == HandshakeStatus.NEED_TASK) {
            throw new Error("Handshake shouldn't need additional tasks.");
        }
    }

    /**
     * Start a KDC server:
     * - create a KDC instance
     * - create Kerberos principals
     * - save Kerberos configuration
     * - save keys to keytab file
     * - no pre-auth is required
     */
    private static void startKDC(String realm, Map<String, String> principals,
                                 String ktab) {
        try {
            KDC kdc = KDC.create(realm, HOST, 0, true);
            kdc.setOption(KDC.Option.PREAUTH_REQUIRED, Boolean.FALSE);
            if (principals != null) {
                principals.entrySet().stream().forEach((entry) -> {
                    String name = entry.getKey();
                    String password = entry.getValue();
                    if (password == null || password.isEmpty()) {
                        System.out.println("KDC: add a principal '" + name
                                + "' with a random password");
                        kdc.addPrincipalRandKey(name);
                    } else {
                        System.out.println("KDC: add a principal '" + name
                                + "' with '" + password + "' password");
                        kdc.addPrincipal(name, password.toCharArray());
                    }
                });
            }
            KDC.saveConfig(KRB5_CONF_FILENAME, kdc);
            if (ktab != null) {
                File ktabFile = new File(ktab);
                if (ktabFile.exists()) {
                    System.out.println("KDC: append keys to an exising "
                            + "keytab file " + ktab);
                    kdc.appendKtab(ktab);
                } else {
                    System.out.println("KDC: create a new keytab file "
                            + ktab);
                    kdc.writeKtab(ktab);
                }
            }
            System.out.println("KDC: started on " + HOST + ":" + kdc.getPort()
                    + " with '" + realm + "' realm");
        } catch (Exception e) {
            throw new RuntimeException("KDC: unexpected exception", e);
        }
    }
}