-
Notifications
You must be signed in to change notification settings - Fork 361
/
CipherSuite.java
1463 lines (1345 loc) · 51.9 KB
/
CipherSuite.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2015, 2017 Institute for Pervasive Computing, ETH Zurich and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Matthias Kovatsch - creator and main architect
* Stefan Jucker - DTLS implementation
* Kai Hudalla (Bosch Software Innovations GmbH) - adapt name of NULL cipher to match
* official IANA name
* Kai Hudalla (Bosch Software Innovations GmbH) - Add getters for conveniently accessing
* a cipher suite's underlying security parameters, add definitions for CBC based
* cipher suites mandatory for LW M2M servers
* Kai Hudalla (Bosch Software Innovations GmbH) - add method for checking if suite requires
* sending of a CERTIFICATE message to the client
* Bosch Software Innovations GmbH - migrate to SLF4J
* Achim Kraus (Bosch Software Innovations GmbH) - add containsEccBasedCipherSuite
* support for certificate-based,
* none ECC-based cipher suites is
* still missing!
* Vikram (University of Rostock) - added CipherSuite TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
* Achim Kraus (Bosch Software Innovations GmbH) - add TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, and
* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
******************************************************************************/
package org.eclipse.californium.scandium.dtls.cipher;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.californium.elements.util.Asn1DerDecoder;
import org.eclipse.californium.elements.util.DatagramReader;
import org.eclipse.californium.elements.util.DatagramWriter;
import org.eclipse.californium.elements.util.PublicAPITypo;
import org.eclipse.californium.scandium.config.DtlsConfig.DtlsSecureRenegotiation;
import org.eclipse.californium.scandium.util.ListUtils;
/**
* A cipher suite defines a key exchange algorithm, a bulk cipher algorithm, a
* MAC algorithm, a pseudo random number (PRF) algorithm and a cipher type.
* <p>
* <b>Note:</b> {@code ordinal()} must not be used!
* The order of the cipher-suites reflects the intended default precedence.
* Extensions may therefore change the related {@code ordinal()} value.
* </p>
* <p>
* See <a href="https://tools.ietf.org/html/rfc5246#appendix-A.6" target="_blank">RFC 5246</a>
* for details.
* See <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml" target="_blank">
* Transport Layer Security Parameters</a> for the official codes for the cipher
* suites.
* </p>
*/
public enum CipherSuite {
// Cipher suites //////////////////////////////////////////////////
// Cipher suites order is based on those statements :
// - ECDHE is preferred as it provides perfect forward secrecy.
// - AES_128 preferred over AES_192/256 because it's secure enough & faster.
// source:https://www.quora.com/Is-AES256-more-secure-than-AES128-Whats-the-different
// source:https://security.stackexchange.com/questions/14068/why-most-people-use-256-bit-encryption-instead-of-128-bit#19762
// - GCM >= CCM_8 ~= CCM >> CBC
// source:https://en.wikipedia.org/wiki/Transport_Layer_Security#Cipher
// source:https://crypto.stackexchange.com/questions/63796/why-does-tls-1-3-support-two-ccm-variants/64809#64809
// - SHA sounds secure enough and so smaller SHA is preferred.
// source:https://security.stackexchange.com/questions/84304/why-were-cbc-sha256-ciphersuites-like-tls-rsa-with-aes-128-cbc-sha256-defined
// source:https://crypto.stackexchange.com/questions/20572/sha1-ssl-tls-cipher-suite
// (In combination with CBC SHA1 is not preferred over CBC-SHA256, see lucky 13))
// See more:
// https://github.com/ssllabs/research/wiki/SSL-and-TLS-Deployment-Best-Practices
//
// /!\ CBC should be avoid /!\ because :
// - Implementing authenticated decryption (checking padding and mac) without any side channel is hard (see Lucky 13 attack and its variants).
// - In fact, the current Scandium CBC implementation is not "processing time stable" according such "padding" attacks.
// - One solution is to use the encrypt then mac extension defined in RFC 7366, which is recommended. (from LWM2M 1.0.2 specification)
// But currently Scandium also does not support RFC 7366.
//
// Therefore the CBC cipher suites are not recommended. If you want to use them, you MUST first disable
// the "recommendedCipherSuitesOnly" in DtlsConnectorConfig.Builder.
// PSK cipher suites, ordered by default preference, see getPskCiperSuites
/**See <a href="https://tools.ietf.org/html/rfc8442#section-3" target="_blank">RFC 8442</a> for details*/
/**Note: compatibility not tested! openssl 1.1.1 seems not supporting them */
TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256(0xD001, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.ECDHE_PSK, CipherSpec.AES_128_GCM, true),
/**
* Wrong cipher suite name! Must be SHA384! Will be changed with the next major version.
*/
@PublicAPITypo(fixedName="TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384")
TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA378(0xD002, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.ECDHE_PSK, CipherSpec.AES_256_GCM, true, PRFAlgorithm.TLS_PRF_SHA384),
TLS_ECDHE_PSK_WITH_AES_128_CCM_8_SHA256(0xD003, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.ECDHE_PSK, CipherSpec.AES_128_CCM_8, true),
TLS_ECDHE_PSK_WITH_AES_128_CCM_SHA256(0xD005, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.ECDHE_PSK, CipherSpec.AES_128_CCM, true),
TLS_PSK_WITH_AES_128_GCM_SHA256(0x00A8, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.AES_128_GCM, true),
/**
* Wrong cipher suite name! Must be SHA384!
*/
@PublicAPITypo(fixedName="TLS_PSK_WITH_AES_256_GCM_SHA384")
TLS_PSK_WITH_AES_256_GCM_SHA378(0x00A9, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.AES_256_GCM, true, PRFAlgorithm.TLS_PRF_SHA384),
TLS_PSK_WITH_AES_128_CCM_8(0xC0A8, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.AES_128_CCM_8, true),
TLS_PSK_WITH_AES_256_CCM_8(0xC0A9, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.AES_256_CCM_8, true),
TLS_PSK_WITH_AES_128_CCM(0xC0A4, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.AES_128_CCM, true),
TLS_PSK_WITH_AES_256_CCM(0xC0A5, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.AES_256_CCM, true),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.3" target=
* "_blank">RFC 6209 - PSK</a> for details.
*
* @since 3.9.0
*/
TLS_PSK_WITH_ARIA_128_GCM_SHA256(0xC06A, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK,
CipherSpec.ARIA_128_GCM, true),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.3" target=
* "_blank">RFC 6209 - PSK</a> for details.
*
* @since 3.9.0
*/
TLS_PSK_WITH_ARIA_256_GCM_SHA384(0xC06B, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK,
CipherSpec.ARIA_256_GCM, true, PRFAlgorithm.TLS_PRF_SHA384),
/**See <a href="https://tools.ietf.org/html/rfc5489#section-3.2" target="_blank">RFC 5489</a> for details*/
TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256(0xC037, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.ECDHE_PSK, CipherSpec.AES_128_CBC, MACAlgorithm.HMAC_SHA256, false),
TLS_PSK_WITH_AES_128_CBC_SHA256(0x00AE, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.AES_128_CBC, MACAlgorithm.HMAC_SHA256, false),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.3" target=
* "_blank">RFC 6209 - PSK</a> for details.
*
* @since 3.9.0
*/
TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256(0xC06C, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.ECDHE_PSK, CipherSpec.ARIA_128_CBC, MACAlgorithm.HMAC_SHA256, false),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.3" target=
* "_blank">RFC 6209 - PSK</a> for details.
*
* @since 3.9.0
*/
TLS_PSK_WITH_ARIA_128_CBC_SHA256(0xC064, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.ARIA_128_CBC, MACAlgorithm.HMAC_SHA256, false),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.3" target=
* "_blank">RFC 6209 - PSK</a> for details.
*
* @since 3.9.0
*/
TLS_PSK_WITH_ARIA_256_CBC_SHA384(0xC065, CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.PSK, CipherSpec.ARIA_256_CBC, MACAlgorithm.HMAC_SHA384, false, PRFAlgorithm.TLS_PRF_SHA384),
// Certificate cipher suites, ordered by default preference, see getCertificateCipherSuites or getEcdsaCipherSuites
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256(0xC02B, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_128_GCM, true),
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384(0xC02C, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_256_GCM, true, PRFAlgorithm.TLS_PRF_SHA384),
TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8(0xC0AE, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_128_CCM_8, true),
TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8(0xC0AF, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_256_CCM_8, true),
TLS_ECDHE_ECDSA_WITH_AES_128_CCM(0xC0AC, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_128_CCM, true),
TLS_ECDHE_ECDSA_WITH_AES_256_CCM(0xC0AD, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_256_CCM, true),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.2" target=
* "_blank">RFC 6209 - GCM</a> for details.
*
* @since 3.9.0
*/
TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256(0xC05C, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.ARIA_128_GCM, true),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.2" target=
* "_blank">RFC 6209 - GCM</a> for details.
*
* @since 3.9.0
*/
TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384(0xC05D, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.ARIA_256_GCM, true, PRFAlgorithm.TLS_PRF_SHA384),
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256(0xC023, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_128_CBC, MACAlgorithm.HMAC_SHA256, false),
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384(0xC024, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_256_CBC, MACAlgorithm.HMAC_SHA384, false, PRFAlgorithm.TLS_PRF_SHA384),
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA(0xC00A, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_256_CBC, MACAlgorithm.HMAC_SHA1, false),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.1" target=
* "_blank">RFC 6209 - CBC</a> for details.
*
* @since 3.9.0
*/
TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256(0xC048, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.ARIA_128_CBC, MACAlgorithm.HMAC_SHA256, false),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.1" target=
* "_blank">RFC 6209 - CBC</a> for details.
*
* @since 3.9.0
*/
TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384(0xC049, CertificateKeyAlgorithm.EC, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.ARIA_256_CBC, MACAlgorithm.HMAC_SHA384, false, PRFAlgorithm.TLS_PRF_SHA384),
// RSA Certificates
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256(0xC02F, CertificateKeyAlgorithm.RSA, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_128_GCM, true),
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384(0xC030, CertificateKeyAlgorithm.RSA, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_256_GCM, true, PRFAlgorithm.TLS_PRF_SHA384),
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256(0xC027, CertificateKeyAlgorithm.RSA, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_128_CBC, MACAlgorithm.HMAC_SHA256, false),
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384(0xC028, CertificateKeyAlgorithm.RSA, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_256_CBC, MACAlgorithm.HMAC_SHA384, false, PRFAlgorithm.TLS_PRF_SHA384),
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA(0xC014, CertificateKeyAlgorithm.RSA, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.AES_256_CBC, MACAlgorithm.HMAC_SHA1, false),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.2" target=
* "_blank">RFC 6209 - GCM</a> for details.
*
* @since 3.9.0
*/
TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256(0xC060, CertificateKeyAlgorithm.RSA, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.ARIA_128_GCM, true),
/**
* See <a href="https://www.rfc-editor.org/rfc/rfc6209#section-2.2" target=
* "_blank">RFC 6209 - GCM</a> for details.
*
* @since 3.9.0
*/
TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384(0xC061, CertificateKeyAlgorithm.RSA, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN, CipherSpec.ARIA_256_GCM, true, PRFAlgorithm.TLS_PRF_SHA384),
// Null cipher suite
TLS_NULL_WITH_NULL_NULL(0x0000),
/**
* Cipher suite indicating client support for secure renegotiation.
*
* Californium doesn't support renegotiation at all, but RFC5746 requests to
* update to a minimal version of RFC 5746.
*
* See <a href="https://tools.ietf.org/html/rfc5746" target="_blank">RFC
* 5746</a> for additional details.
*
* @see DtlsSecureRenegotiation
* @since 3.8 (before that only used for logging since 3.5)
*/
TLS_EMPTY_RENEGOTIATION_INFO_SCSV(0x00FF),
;
// Logging ////////////////////////////////////////////////////////
private static final Logger LOGGER = LoggerFactory.getLogger(CipherSuite.class);
// DTLS-specific constants ////////////////////////////////////////
public static final int CIPHER_SUITE_BITS = 16;
public static final List<CipherSuite> STRONG_ENCRYPTION_PREFERENCE;
static {
List<CipherSuite> secureSuites = new ArrayList<>();
secureSuites.addAll(CipherSuite.getCipherSuitesByKeyExchangeAlgorithm(false, KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN));
secureSuites.addAll(CipherSuite.getCipherSuitesByKeyExchangeAlgorithm(false, KeyExchangeAlgorithm.ECDHE_PSK));
secureSuites.addAll(CipherSuite.getCipherSuitesByKeyExchangeAlgorithm(false, KeyExchangeAlgorithm.PSK));
List<CipherSuite> ccm8 = new ArrayList<>();
Iterator<CipherSuite> iterator = secureSuites.iterator();
while (iterator.hasNext()) {
CipherSuite cipherSuite = iterator.next();
if (cipherSuite.getMacLength() < 16) {
ccm8.add(cipherSuite);
iterator.remove();
}
}
secureSuites.addAll(ccm8);
STRONG_ENCRYPTION_PREFERENCE = Collections.unmodifiableList(secureSuites);
}
// Members ////////////////////////////////////////////////////////
private static int overallMaxCipherTextExpansion = 0;
/**
* 16 bit identification, i.e. 0x0000 for SSL_NULL_WITH_NULL_NULL, see <a
* href="https://tools.ietf.org/html/rfc5246#appendix-A.5" target="_blank">RFC 5246</a>.
*/
private final int code;
/**
* Indicates, that the cipher suite is valid to be negotiated.
*
* @since 3.5
*/
private final boolean validForNegotiation;
private final CertificateKeyAlgorithm certificateKeyAlgorithm;
private final KeyExchangeAlgorithm keyExchange;
private final CipherSpec cipher;
private final MACAlgorithm macAlgorithm;
private final PRFAlgorithm pseudoRandomFunction;
private final int maxCipherTextExpansion;
private final boolean recommendedCipherSuite;
// Constructor ////////////////////////////////////////////////////
/**
* Creates a not negotiable cipher suit.
*
* @param code IANA code.
* @since 3.5
*/
private CipherSuite(int code) {
// CertificateKeyAlgorithm.NONE, KeyExchangeAlgorithm.NULL, CipherSpec.NULL, MACAlgorithm.NULL
this.code = code;
this.validForNegotiation = false;
this.certificateKeyAlgorithm = CertificateKeyAlgorithm.NONE;
this.keyExchange = KeyExchangeAlgorithm.NULL;
this.cipher = CipherSpec.NULL;
this.macAlgorithm = MACAlgorithm.NULL;
this.recommendedCipherSuite = false;
this.pseudoRandomFunction = PRFAlgorithm.TLS_PRF_SHA256;
this.maxCipherTextExpansion = 0;
}
private CipherSuite(int code, CertificateKeyAlgorithm certificate, KeyExchangeAlgorithm keyExchange, CipherSpec cipher, boolean recommendedCipherSuite) {
this(code, certificate, keyExchange, cipher, MACAlgorithm.INTRINSIC, recommendedCipherSuite, PRFAlgorithm.TLS_PRF_SHA256);
}
private CipherSuite(int code, CertificateKeyAlgorithm certificate, KeyExchangeAlgorithm keyExchange, CipherSpec cipher, MACAlgorithm macAlgorithm, boolean recommendedCipherSuite) {
this(code, certificate, keyExchange, cipher, macAlgorithm, recommendedCipherSuite, PRFAlgorithm.TLS_PRF_SHA256);
}
private CipherSuite(int code, CertificateKeyAlgorithm certificate, KeyExchangeAlgorithm keyExchange, CipherSpec cipher, boolean recommendedCipherSuite, PRFAlgorithm prf) {
this(code, certificate, keyExchange, cipher, MACAlgorithm.INTRINSIC, recommendedCipherSuite, prf);
}
private CipherSuite(int code, CertificateKeyAlgorithm certificate, KeyExchangeAlgorithm keyExchange, CipherSpec cipher, MACAlgorithm macAlgorithm, boolean recommendedCipherSuite, PRFAlgorithm prf) {
this.code = code;
this.validForNegotiation = true;
this.certificateKeyAlgorithm = certificate;
this.keyExchange = keyExchange;
this.cipher = cipher;
this.macAlgorithm = macAlgorithm;
this.recommendedCipherSuite = recommendedCipherSuite;
this.pseudoRandomFunction = prf;
switch(this.cipher.getType()) {
case BLOCK:
maxCipherTextExpansion =
cipher.getRecordIvLength() // IV
+ macAlgorithm.getOutputLength() // MAC
+ cipher.getRecordIvLength() // max padding (block size)
+ 1; // padding length
break;
case AEAD:
maxCipherTextExpansion =
cipher.getRecordIvLength() // explicit nonce
+ cipher.getMacLength();
break;
default:
maxCipherTextExpansion = 0;
}
}
// Getters ////////////////////////////////////////////////////////
/**
* Get maximum expansion of cipher text using this cipher suite.
*
* Includes MAC, explicit nonce, and padding.
*
* @return maxnium expansion of this cipher suite
* @see #getMacLength()
* @see #getRecordIvLength()
*/
public int getMaxCiphertextExpansion() {
return maxCipherTextExpansion;
}
/**
* Gets the Java Cryptography Architecture <em>transformation</em> corresponding
* to the suite's underlying cipher algorithm.
*
* The name can be used to instantiate a {@code javax.crypto.Cipher} object
* (if a security provider is available in the JVM supporting the transformation).
* See <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" target="_blank">
* Java Security Documentation</a>.
*
* @return the transformation
*/
public String getTransformation() {
return cipher.getTransformation();
}
/**
* Gets the thread local cipher used by this cipher suite.
*
* @return the cipher, or {@code null}, if the cipher is not supported by
* the java-vm.
*/
public Cipher getThreadLocalCipher() {
return cipher.getCipher();
}
/**
* Gets the 16-bit IANA assigned identification code of the cipher suite.
*
* See <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4" target="_blank">
* TLS Cipher Suite Registry</a>.
*
* @return the identification code
*/
public int getCode() {
return code;
}
/**
* Gets the certificate key algorithm of the cipher suite.
*
* @return the algorithm
*/
public CertificateKeyAlgorithm getCertificateKeyAlgorithm() {
return certificateKeyAlgorithm;
}
/**
* Gets the key exchange algorithm the cipher suite employs to
* generate a pre-master secret.
*
* @return the algorithm
*/
public KeyExchangeAlgorithm getKeyExchange() {
return keyExchange;
}
/**
* Checks whether this cipher suite requires the server
* to send a <em>CERTIFICATE</em> message during the handshake.
*
* @return {@code true} if the message is required
*/
public boolean requiresServerCertificateMessage() {
return !CertificateKeyAlgorithm.NONE.equals(certificateKeyAlgorithm);
}
/**
* Checks whether this cipher suite use <em>PSK</em> key exchange.
* @return {@code true} if <em>PSK</em> key exchange is used
*/
public boolean isPskBased() {
return KeyExchangeAlgorithm.PSK.equals(keyExchange) || KeyExchangeAlgorithm.ECDHE_PSK.equals(keyExchange);
}
/**
* Checks whether this cipher suite uses elliptic curve cryptography (ECC).
*
* @return {@code true} if ECC is used
*/
public boolean isEccBased() {
return CertificateKeyAlgorithm.EC.equals(certificateKeyAlgorithm)
|| KeyExchangeAlgorithm.ECDHE_PSK.equals(keyExchange)
|| KeyExchangeAlgorithm.EC_DIFFIE_HELLMAN.equals(keyExchange);
}
/**
* Checks whether this cipher suite is supported by the jvm implementation.
*
* @return {@code true} if cipher suite is supported
*/
public boolean isSupported() {
return pseudoRandomFunction.getMacAlgorithm().isSupported() && macAlgorithm.isSupported()
&& cipher.isSupported();
}
/**
* Check whether this cipher suite is recommended.
*
* The recommendation is base on security considerations. Currently AES-CBC
* is not recommended. Using RSA is also no recommended for performance
* reasons not for security reasons. Therefore RSA cipher suites may also
* return {@code true}.
*
* @return {@code true} if cipher suite is recommended
*/
public boolean isRecommended() {
return recommendedCipherSuite;
}
/**
* Check whether this cipher suite is valid for negotiation.
*
* {@link #TLS_NULL_WITH_NULL_NULL} and
* {@link #TLS_EMPTY_RENEGOTIATION_INFO_SCSV} are not intended to be
* negotiated.
*
* @return {@code true} if cipher suite is valid for negotiation
* @since 3.5
*/
public boolean isValidForNegotiation() {
return validForNegotiation;
}
/**
* Gets the name of the cipher suite's MAC algorithm.
*
* The name can be used to instantiate a {@code javax.crypto.Mac}
* instance.
*
* See <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Mac" target="_blank">
* Java Security Documentation</a>.
*
* @return the name or {@code null} for the <em>NULL</em> MAC
*/
public String getMacName() {
return macAlgorithm.getName();
}
/**
* Gets the name of the message digest (hash) function used by the cipher
* suite MAC.
*
* The name can be used to instantiate a
* {@code java.security.MessageDigest} instance.
*
* See <a href=
* "http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest">
* Java Security Documentation</a>.
*
* @return the name or {@code null} for the <em>NULL</em> MAC
*/
public String getMessageDigestName() {
return macAlgorithm.getMessageDigestName();
}
/**
* Gets the thread local MAC used by this cipher suite.
*
* @return mac, or {@code null}, if not supported by vm.
*/
public Mac getThreadLocalMac() {
return macAlgorithm.getMac();
}
/**
* Gets the thread local message digest used by this cipher suite.
*
* @return message digest, or {@code null}, if not supported by vm.
*/
public MessageDigest getThreadLocalMacMessageDigest() {
return macAlgorithm.getMessageDigest();
}
/**
* Gets the output length of the cipher suite's MAC algorithm.
*
* @return the length in bytes
*/
public int getMacLength() {
if (macAlgorithm == MACAlgorithm.INTRINSIC) {
return cipher.getMacLength();
} else {
return macAlgorithm.getOutputLength();
}
}
/**
* Gets the key length of the cipher suite's MAC algorithm.
*
* @return the length in bytes
*/
public int getMacKeyLength() {
return macAlgorithm.getKeyLength();
}
/**
* Get the message block length of hash function.
*
* @return message block length in bytes
*/
public int getMacMessageBlockLength() {
return macAlgorithm.getMessageBlockLength();
}
/**
* Get the number of bytes used to encode the message length for hmac
* function.
*
* @return number of bytes for message length
*/
public int getMacMessageLengthBytes() {
return macAlgorithm.getMessageLengthBytes();
}
/**
* Gets the amount of data needed to be generated for the cipher's
* initialization vector.
*
* Zero for stream ciphers; equal to the block size for block ciphers
* (this is equal to SecurityParameters.record_iv_length).
*
* @return the length in bytes
*/
public int getRecordIvLength() {
return cipher.getRecordIvLength();
}
/**
* Gets the length of the fixed initialization vector (IV) of
* the cipher suite's bulk cipher algorithm.
*
* This is only relevant for AEAD based cipher suites.
*
* @return the length in bytes
*/
public int getFixedIvLength() {
return cipher.getFixedIvLength();
}
/**
* Gets the pseudo-random function used by the cipher suite
* to create (pseudo-)random data from a seed.
*
* The name can be used to instantiate a {@code javax.crypto.Mac}
* instance.
*
* See <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Mac" target="_blank">
* Java Security Documentation</a>.
*
* @return the name of the pseudo-random function
*/
public String getPseudoRandomFunctionMacName() {
return pseudoRandomFunction.getMacAlgorithm().getName();
}
/**
* Gets the name of the pseudo-random message digest (hash) function used by
* the cipher suite to create the hash over the handshake messages.
*
* The name can be used to instantiate a
* {@code java.security.MessageDigest} instance.
*
* See <a href=
* "http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest">
* Java Security Documentation</a>.
*
* @return the name of the message digest
*/
public String getPseudoRandomFunctionMessageDigestName() {
return pseudoRandomFunction.getMacAlgorithm().getMessageDigestName();
}
/**
* Gets the thread local MAC used by the pseudo random function of this
* cipher suite.
*
* @return mac, or {@code null}, if not supported by vm.
*/
public Mac getThreadLocalPseudoRandomFunctionMac() {
return pseudoRandomFunction.getMacAlgorithm().getMac();
}
/**
* Gets the thread local message digest used by the pseudo random function
* of this cipher suite.
*
* @return message digest, or {@code null}, if not supported by vm.
*/
public MessageDigest getThreadLocalPseudoRandomFunctionMessageDigest() {
return pseudoRandomFunction.getMacAlgorithm().getMessageDigest();
}
/**
* Gets the type of cipher used for encrypting data.
*
* @return the type
*/
public CipherType getCipherType() {
return cipher.getType();
}
/**
* Gets the length of the bulk cipher algorithm's encoding key.
*
* @return the length in bytes
*/
public int getEncKeyLength() {
return cipher.getKeyLength();
}
/**
* Get the overall maximum ciphertext expansion for all cipher suite.
*
* @return overall maximum ciphertext expansion.
*/
public static int getOverallMaxCiphertextExpansion() {
if (overallMaxCipherTextExpansion == 0) {
int overall = 0;
for (CipherSuite suite : values()) {
if (suite.isSupported()) {
overall = Math.max(overall, suite.getMaxCiphertextExpansion());
}
}
overallMaxCipherTextExpansion = overall;
}
return overallMaxCipherTextExpansion;
}
/**
* Get array of supported cipher suites.
*
* @param recommendedCipherSuitesOnly {@code true} to include only
* recommended cipher suites.
* @param supportedCipherSuitesOnly {@code true} to include only supported
* cipher suites.
* @return array of cipher suites.
* @since 3.0
*/
public static CipherSuite[] getCipherSuites(boolean recommendedCipherSuitesOnly,
boolean supportedCipherSuitesOnly) {
List<CipherSuite> list = new ArrayList<>();
for (CipherSuite suite : values()) {
if (suite.isValidForNegotiation()) {
if (!supportedCipherSuitesOnly || suite.isSupported()) {
if (!recommendedCipherSuitesOnly || suite.isRecommended()) {
list.add(suite);
}
}
}
}
return list.toArray(new CipherSuite[list.size()]);
}
/**
* Get a list of all cipher suites using the provided key exchange
* algorithms.
*
* @param recommendedCipherSuitesOnly {@code true} use only recommended
* cipher suites
* @param keyExchangeAlgorithms list of key exchange algorithms to select
* cipher suites
* @return list of all cipher suites. Ordered by their definition above.
* @throws NullPointerException if keyExchangeAlgorithms is {@code null}
* @throws IllegalArgumentException if keyExchangeAlgorithms is empty
*/
public static List<CipherSuite> getCipherSuitesByKeyExchangeAlgorithm(boolean recommendedCipherSuitesOnly,
KeyExchangeAlgorithm... keyExchangeAlgorithms) {
if (keyExchangeAlgorithms == null) {
throw new NullPointerException("KeyExchangeAlgorithms must not be null!");
} else if (keyExchangeAlgorithms.length == 0) {
throw new IllegalArgumentException("KeyExchangeAlgorithms must not be empty!");
}
return getCipherSuitesByKeyExchangeAlgorithm(recommendedCipherSuitesOnly, false, Arrays.asList(keyExchangeAlgorithms));
}
/**
* Get a list of all cipher suites using the provided key exchange
* algorithms.
*
* @param recommendedCipherSuitesOnly {@code true} use only recommended
* cipher suites
* @param orderedByKeyExchangeAlgorithm {@code true} to order the cipher
* suites by order of key exchange algorithms, {@code false} to
* use the order by their definition above.
* @param keyExchangeAlgorithms list of key exchange algorithms to select
* cipher suites
* @return list of all cipher suites. Ordered as specified by the provided
* orderedByKeyExchangeAlgorithm.
* @throws NullPointerException if keyExchangeAlgorithms is {@code null}
* @throws IllegalArgumentException if keyExchangeAlgorithms is empty
* @since 2.3
*/
public static List<CipherSuite> getCipherSuitesByKeyExchangeAlgorithm(boolean recommendedCipherSuitesOnly,
boolean orderedByKeyExchangeAlgorithm,
List<KeyExchangeAlgorithm> keyExchangeAlgorithms) {
if (keyExchangeAlgorithms == null) {
throw new NullPointerException("KeyExchangeAlgorithms must not be null!");
} else if (keyExchangeAlgorithms.isEmpty()) {
throw new IllegalArgumentException("KeyExchangeAlgorithms must not be empty!");
}
List<CipherSuite> list = new ArrayList<>();
if (orderedByKeyExchangeAlgorithm) {
for (KeyExchangeAlgorithm keyExchange : keyExchangeAlgorithms) {
for (CipherSuite suite : values()) {
if (!recommendedCipherSuitesOnly || suite.recommendedCipherSuite) {
if (suite.isSupported() && keyExchange.equals(suite.keyExchange)) {
ListUtils.addIfAbsent(list, suite);
}
}
}
}
} else {
for (CipherSuite suite : values()) {
if (!recommendedCipherSuitesOnly || suite.recommendedCipherSuite) {
if (suite.isSupported() && keyExchangeAlgorithms.contains(suite.keyExchange)) {
ListUtils.addIfAbsent(list, suite);
}
}
}
}
return list;
}
/**
* Get a list of all supported cipher suites with the provided key
* algorithm.
*
* @param recommendedCipherSuitesOnly {@code true} use only recommended
* cipher suites
* @param key public key
* @return list of all supported cipher suites with the provided key
* algorithm. Ordered by their definition above.
* @throws NullPointerException if public key is {@code null}
* @since 3.0
*/
public static List<CipherSuite> getCertificateCipherSuites(boolean recommendedCipherSuitesOnly, PublicKey key) {
if (key == null) {
throw new NullPointerException("Public key must not be null!");
}
return getCertificateCipherSuites(recommendedCipherSuitesOnly,
Arrays.asList(CertificateKeyAlgorithm.getAlgorithm(key)));
}
/**
* Get a list of all supported cipher suites with the provided key
* algorithms.
*
* @param recommendedCipherSuitesOnly {@code true} use only recommended
* cipher suites
* @param certificateKeyAlgorithms array of certificate key algorithms
* @return list of all supported cipher suites with the provided key
* algorithm. Ordered by their definition above.
* @throws NullPointerException if array of certificate key algorithms is
* {@code null}
* @throws IllegalArgumentException if array of certificate key algorithms
* is empty
* @since 3.0
*/
public static List<CipherSuite> getCertificateCipherSuites(boolean recommendedCipherSuitesOnly,
CertificateKeyAlgorithm... certificateKeyAlgorithms) {
if (certificateKeyAlgorithms == null) {
throw new NullPointerException("Certificate key algorithms must not be null!");
}
if (certificateKeyAlgorithms.length == 0) {
throw new IllegalArgumentException("Certificate key algorithms must not be empty!");
}
return getCertificateCipherSuites(recommendedCipherSuitesOnly, Arrays.asList(certificateKeyAlgorithms));
}
/**
* Get a list of all supported cipher suites with the provided key
* algorithms.
*
* @param recommendedCipherSuitesOnly {@code true} use only recommended
* cipher suites
* @param certificateKeyAlgorithms list of certificate key algorithms
* @return list of all supported cipher suites with the provided key
* algorithm. Ordered by their definition above.
* @throws NullPointerException if list of certificate key algorithms is
* {@code null}
* @throws IllegalArgumentException if list of certificate key algorithms is
* empty
* @since 3.0 (adapted the key algorithm into a list of algorithms)
*/
public static List<CipherSuite> getCertificateCipherSuites(boolean recommendedCipherSuitesOnly,
List<CertificateKeyAlgorithm> certificateKeyAlgorithms) {
if (certificateKeyAlgorithms == null) {
throw new NullPointerException("Certificate key algorithms must not be null!");
}
if (certificateKeyAlgorithms.isEmpty()) {
throw new IllegalArgumentException("Certificate key algorithms must not be empty!");
}
List<CipherSuite> list = new ArrayList<>();
for (CipherSuite suite : values()) {
if (suite.isSupported()) {
if (!recommendedCipherSuitesOnly || suite.recommendedCipherSuite) {
if (certificateKeyAlgorithms.contains(suite.certificateKeyAlgorithm)) {
list.add(suite);
}
}
}
}
return list;
}
/**
* Gets the certificate key algorithms of the cipher suite list.
*
* @param cipherSuites list of ciperh suite
* @return The certificate key algorithms (never {@code null}.
* @since 3.0
*/
public static List<CertificateKeyAlgorithm> getCertificateKeyAlgorithms(List<CipherSuite> cipherSuites) {
List<CertificateKeyAlgorithm> types = new ArrayList<>();
for (CipherSuite suite : cipherSuites) {
if (suite.getCertificateKeyAlgorithm() != CertificateKeyAlgorithm.NONE) {
ListUtils.addIfAbsent(types, suite.getCertificateKeyAlgorithm());
}
}
return types;
}
/**
* Gets a cipher suite by its numeric code.
*
* @param code the cipher's
* <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4" target="_blank">
* IANA assigned code</a>
* @return the cipher suite or {@code null}, if the code is unknown
*/
public static CipherSuite getTypeByCode(int code) {
for (CipherSuite suite : values()) {
if (suite.code == code) {
return suite;
}
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Cannot resolve cipher suite code [{}]", Integer.toHexString(code));
}
return null;
}
/**
* Gets a cipher suite by its (official) name.
*
* @param name the cipher's
* <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4" target="_blank">
* IANA assigned name</a>
* @return the cipher suite or {@code null}, if the name is unknown
*/
public static CipherSuite getTypeByName(String name) {
for (CipherSuite suite : values()) {
if (suite.name().equals(name)) {
return suite;
}
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Cannot resolve cipher suite code [{}]", name);
}
return null;
}
/**
* Gets a list of cipher suites by their (official) names.
*
* @param names the cipher's <a href=
* "http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4">
* IANA assigned names</a>
* @return the list of cipher suite
* @throws IllegalArgumentException if at least one name is not available.
* @since 2.5
*/
public static List<CipherSuite> getTypesByNames(String... names) {
List<CipherSuite> suites = new ArrayList<>(names.length);
for (int i = 0; i < names.length; i++) {
CipherSuite knownSuite = getTypeByName(names[i]);
if (knownSuite != null) {
suites.add(knownSuite);
} else {
throw new IllegalArgumentException(String.format("Cipher suite [%s] is not (yet) supported", names[i]));
}
}
return suites;
}
/**
* Checks if a list of cipher suite contains an PSK based cipher.
*
* @param cipherSuites The cipher suites to check.
* @return {@code true}, if the list contains an PSK based cipher suite,
* {@code false}, otherwise.
*
*/
public static boolean containsPskBasedCipherSuite(List<CipherSuite> cipherSuites) {
if (cipherSuites != null) {
for (CipherSuite cipherSuite : cipherSuites) {
if (cipherSuite.isPskBased()) {
return true;
}
}
}
return false;
}
/**
* Checks if a list of cipher suite contains an ECC based cipher.
*
* @param cipherSuites The cipher suites to check.
* @return {@code true}, if the list contains an ECC based cipher suite,
* {@code false}, otherwise.
*
*/
public static boolean containsEccBasedCipherSuite(List<CipherSuite> cipherSuites) {
if (cipherSuites != null) {
for (CipherSuite cipherSuite : cipherSuites) {
if (cipherSuite.isEccBased()) {
return true;
}
}
}
return false;
}
/**
* Checks if a list of cipher suite contains a cipher suite that requires
* the exchange of certificates.
*
* @param cipherSuites The cipher suites to check.
* @return {@code true} if any of the cipher suites requires the exchange of certificates,