This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCipher.java
1118 lines (981 loc) · 42.2 KB
/
Cipher.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
package javax.crypto;
import java.nio.ByteBuffer;
import java.nio.ReadOnlyBufferException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.InvalidParameterException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.ProviderException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.Provider.Service;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidParameterSpecException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Pattern;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.RC2ParameterSpec;
import javax.crypto.spec.RC5ParameterSpec;
import sun.security.jca.GetInstance;
import sun.security.jca.ServiceId;
import sun.security.util.Debug;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.io.PrintWriter;
public class Cipher {
private static final Debug debug = Debug.getInstance("jca", "Cipher");
private static final Debug pdebug = Debug.getInstance("provider", "Provider");
private static final boolean skipDebug = Debug.isOn("engine=") && !Debug.isOn("cipher");
public static final int ENCRYPT_MODE = 1;
public static final int DECRYPT_MODE = 2;
public static final int WRAP_MODE = 3;
public static final int UNWRAP_MODE = 4;
public static final int PUBLIC_KEY = 1;
public static final int PRIVATE_KEY = 2;
public static final int SECRET_KEY = 3;
private Provider provider;
private CipherSpi spi;
private String transformation;
private CryptoPermission cryptoPerm;
private ExemptionMechanism exmech;
private boolean initialized = false;
private int opmode = 0;
private static final String KEY_USAGE_EXTENSION_OID = "2.5.29.15";
private CipherSpi firstSpi;
private Service firstService;
private Iterator serviceIterator;
private List transforms;
private final Object lock;
private static final String ATTR_MODE = "SupportedModes";
private static final String ATTR_PAD = "SupportedPaddings";
private static final int S_NO = 0;
private static final int S_MAYBE = 1;
private static final int S_YES = 2;
private static int warnCount = 10;
private static final int I_KEY = 1;
private static final int I_PARAMSPEC = 2;
private static final int I_PARAMS = 3;
private static final int I_CERT = 4;
protected Cipher(CipherSpi var1, Provider var2, String var3) {
if (!JceSecurityManager.INSTANCE.isCallerTrusted()) {
throw new NullPointerException();
} else {
this.spi = var1;
this.provider = var2;
this.transformation = var3;
this.cryptoPerm = CryptoAllPermission.INSTANCE;
this.lock = null;
}
}
Cipher(CipherSpi var1, String var2) {
this.spi = var1;
this.transformation = var2;
this.cryptoPerm = CryptoAllPermission.INSTANCE;
this.lock = null;
}
private Cipher(CipherSpi var1, Service var2, Iterator var3, String var4, List var5) {
this.firstSpi = var1;
this.firstService = var2;
this.serviceIterator = var3;
this.transforms = var5;
this.transformation = var4;
this.lock = new Object();
}
private static String[] tokenizeTransformation(String var0) throws NoSuchAlgorithmException {
if (var0 == null) {
throw new NoSuchAlgorithmException("No transformation given");
} else {
String[] var1 = new String[3];
int var2 = 0;
StringTokenizer var3 = new StringTokenizer(var0, "/");
try {
while (var3.hasMoreTokens() && var2 < 3) {
var1[var2++] = var3.nextToken().trim();
}
if (var2 == 0 || var2 == 2 || var3.hasMoreTokens()) {
throw new NoSuchAlgorithmException("Invalid transformation format:" + var0);
}
} catch (NoSuchElementException var5) {
throw new NoSuchAlgorithmException("Invalid transformation format:" + var0);
}
if (var1[0] != null && var1[0].length() != 0) {
return var1;
} else {
throw new NoSuchAlgorithmException("Invalid transformation:algorithm not specified-" + var0);
}
}
}
private static List getTransforms(String var0) throws NoSuchAlgorithmException {
String[] var1 = tokenizeTransformation(var0);
String var2 = var1[0];
String var3 = var1[1];
String var4 = var1[2];
if (var3 != null && var3.length() == 0) {
var3 = null;
}
if (var4 != null && var4.length() == 0) {
var4 = null;
}
if (var3 == null && var4 == null) {
Cipher.Transform var6 = new Cipher.Transform(var2, "", (String) null, (String) null);
return Collections.singletonList(var6);
} else {
ArrayList var5 = new ArrayList(4);
var5.add(new Cipher.Transform(var2, "/" + var3 + "/" + var4, (String) null, (String) null));
var5.add(new Cipher.Transform(var2, "/" + var3, (String) null, var4));
var5.add(new Cipher.Transform(var2, "//" + var4, var3, (String) null));
var5.add(new Cipher.Transform(var2, "", var3, var4));
return var5;
}
}
private static Cipher.Transform getTransform(Service var0, List var1) {
String var2 = var0.getAlgorithm().toUpperCase(Locale.ENGLISH);
Iterator var3 = var1.iterator();
Cipher.Transform var4;
do {
if (!var3.hasNext()) {
return null;
}
var4 = (Cipher.Transform) var3.next();
} while (!var2.endsWith(var4.suffix));
return var4;
}
public static final Cipher getInstance(String var0) throws NoSuchAlgorithmException, NoSuchPaddingException {
List var1 = getTransforms(var0);
ArrayList var2 = new ArrayList(var1.size());
Iterator var3 = var1.iterator();
while (var3.hasNext()) {
Cipher.Transform var4 = (Cipher.Transform) var3.next();
var2.add(new ServiceId("Cipher", var4.transform));
}
List var11 = GetInstance.getServices(var2);
Iterator var12 = var11.iterator();
Exception var5 = null;
while (true) {
Service var6;
Cipher.Transform var7;
int var8;
do {
do {
do {
if (!var12.hasNext()) {
throw new NoSuchAlgorithmException("Cannot find any provider supporting " + var0, var5);
}
var6 = (Service) var12.next();
} while (!JceSecurity.canUseProvider(var6.getProvider()));
var7 = getTransform(var6, var1);
} while (var7 == null);
var8 = var7.supportsModePadding(var6);
} while (var8 == 0);
if (var8 == 2) {
return new Cipher((CipherSpi) null, var6, var12, var0, var1);
}
try {
CipherSpi var9 = (CipherSpi) var6.newInstance((Object) null);
var7.setModePadding(var9);
return new Cipher(var9, var6, var12, var0, var1);
} catch (Exception var10) {
var5 = var10;
}
}
}
public static final Cipher getInstance(String var0, String var1)
throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException {
if (var1 != null && var1.length() != 0) {
Provider var2 = Security.getProvider(var1);
if (var2 == null) {
throw new NoSuchProviderException("No such provider: " + var1);
} else {
return getInstance(var0, var2);
}
} else {
throw new IllegalArgumentException("Missing provider");
}
}
public static final Cipher getInstance(String var0, Provider var1)
throws NoSuchAlgorithmException, NoSuchPaddingException {
if (var1 == null) {
throw new IllegalArgumentException("Missing provider");
} else {
Exception var2 = null;
List var3 = getTransforms(var0);
boolean var4 = false;
String var5 = null;
Iterator var6 = var3.iterator();
while (true) {
while (true) {
Cipher.Transform var7;
Service var8;
do {
do {
if (!var6.hasNext()) {
if (var2 instanceof NoSuchPaddingException) {
throw (NoSuchPaddingException) var2;
}
if (var5 != null) {
throw new NoSuchPaddingException("Padding not supported: " + var5);
}
throw new NoSuchAlgorithmException("No such algorithm: " + var0, var2);
}
var7 = (Cipher.Transform) var6.next();
var8 = var1.getService("Cipher", var7.transform);
} while (var8 == null);
if (!var4) {
Exception var9 = JceSecurity.getVerificationResult(var1);
if (var9 != null) {
String var12 = "JCE cannot authenticate the provider " + var1.getName();
throw new SecurityException(var12, var9);
}
var4 = true;
}
} while (var7.supportsMode(var8) == 0);
if (var7.supportsPadding(var8) != 0) {
try {
CipherSpi var13 = (CipherSpi) var8.newInstance((Object) null);
var7.setModePadding(var13);
Cipher var10 = new Cipher(var13, var0);
var10.provider = var8.getProvider();
var10.initCryptoPermission();
return var10;
} catch (Exception var11) {
var2 = var11;
}
} else {
var5 = var7.pad;
}
}
}
}
}
private void initCryptoPermission() throws NoSuchAlgorithmException {
if (!JceSecurity.isRestricted()) {
this.cryptoPerm = CryptoAllPermission.INSTANCE;
this.exmech = null;
} else {
this.cryptoPerm = getConfiguredPermission(this.transformation);
String var1 = this.cryptoPerm.getExemptionMechanism();
if (var1 != null) {
this.exmech = ExemptionMechanism.getInstance(var1);
}
}
}
void chooseFirstProvider() {
if (this.spi == null) {
synchronized (this.lock) {
if (this.spi == null) {
if (debug != null) {
int var2 = --warnCount;
if (var2 >= 0) {
debug.println(
"Cipher.init() not first method called, disabling delayed provider selection");
if (var2 == 0) {
debug.println("Further warnings of this type will be suppressed");
}
(new Exception("Call trace")).printStackTrace();
}
}
Exception var10 = null;
while (true) {
Service var3;
CipherSpi var4;
Cipher.Transform var5;
do {
do {
do {
if (this.firstService == null && !this.serviceIterator.hasNext()) {
ProviderException var11 = new ProviderException(
"Could not construct CipherSpi instance");
if (var10 != null) {
var11.initCause(var10);
}
throw var11;
}
if (this.firstService != null) {
var3 = this.firstService;
var4 = this.firstSpi;
this.firstService = null;
this.firstSpi = null;
} else {
var3 = (Service) this.serviceIterator.next();
var4 = null;
}
} while (!JceSecurity.canUseProvider(var3.getProvider()));
var5 = getTransform(var3, this.transforms);
} while (var5 == null);
} while (var5.supportsModePadding(var3) == 0);
try {
if (var4 == null) {
Object var6 = var3.newInstance((Object) null);
if (!(var6 instanceof CipherSpi)) {
continue;
}
var4 = (CipherSpi) var6;
}
var5.setModePadding(var4);
this.initCryptoPermission();
this.spi = var4;
this.provider = var3.getProvider();
this.firstService = null;
this.serviceIterator = null;
this.transforms = null;
return;
} catch (Exception var8) {
var10 = var8;
}
}
}
}
}
}
private void implInit(CipherSpi var1, int var2, int var3, Key var4, AlgorithmParameterSpec var5,
AlgorithmParameters var6, SecureRandom var7)
throws InvalidKeyException, InvalidAlgorithmParameterException {
switch (var2) {
case 1:
this.checkCryptoPerm(var1, var4);
var1.engineInit(var3, var4, var7);
break;
case 2:
this.checkCryptoPerm(var1, var4, var5);
var1.engineInit(var3, var4, var5, var7);
break;
case 3:
this.checkCryptoPerm(var1, var4, var6);
var1.engineInit(var3, var4, var6, var7);
break;
case 4:
this.checkCryptoPerm(var1, var4);
var1.engineInit(var3, var4, var7);
break;
default:
throw new AssertionError("Internal Cipher error: " + var2);
}
}
private void chooseProvider(int var1, int var2, Key var3, AlgorithmParameterSpec var4, AlgorithmParameters var5,
SecureRandom var6) throws InvalidKeyException, InvalidAlgorithmParameterException {
synchronized (this.lock) {
if (this.spi != null) {
this.implInit(this.spi, var1, var2, var3, var4, var5, var6);
} else {
Exception var8 = null;
while (true) {
Service var9;
CipherSpi var10;
Cipher.Transform var11;
do {
do {
do {
do {
if (this.firstService == null && !this.serviceIterator.hasNext()) {
if (var8 instanceof InvalidKeyException) {
throw (InvalidKeyException) var8;
}
if (var8 instanceof InvalidAlgorithmParameterException) {
throw (InvalidAlgorithmParameterException) var8;
}
if (var8 instanceof RuntimeException) {
throw (RuntimeException) var8;
}
String var16 = var3 != null ? var3.getClass().getName() : "(null)";
throw new InvalidKeyException(
"No installed provider supports this key: " + var16, var8);
}
if (this.firstService != null) {
var9 = this.firstService;
var10 = this.firstSpi;
this.firstService = null;
this.firstSpi = null;
} else {
var9 = (Service) this.serviceIterator.next();
var10 = null;
}
} while (!var9.supportsParameter(var3));
} while (!JceSecurity.canUseProvider(var9.getProvider()));
var11 = getTransform(var9, this.transforms);
} while (var11 == null);
} while (var11.supportsModePadding(var9) == 0);
try {
if (var10 == null) {
var10 = (CipherSpi) var9.newInstance((Object) null);
}
var11.setModePadding(var10);
this.initCryptoPermission();
this.implInit(var10, var1, var2, var3, var4, var5, var6);
this.provider = var9.getProvider();
this.spi = var10;
this.firstService = null;
this.serviceIterator = null;
this.transforms = null;
return;
} catch (Exception var14) {
if (var8 == null) {
var8 = var14;
}
}
}
}
}
}
public final Provider getProvider() {
this.chooseFirstProvider();
return this.provider;
}
public final String getAlgorithm() {
return this.transformation;
}
public final int getBlockSize() {
this.chooseFirstProvider();
return this.spi.engineGetBlockSize();
}
public final int getOutputSize(int var1) {
if (!this.initialized && !(this instanceof NullCipher)) {
throw new IllegalStateException("Cipher not initialized");
} else if (var1 < 0) {
throw new IllegalArgumentException("Input size must be equal to or greater than zero");
} else {
this.chooseFirstProvider();
return this.spi.engineGetOutputSize(var1);
}
}
public final byte[] getIV() {
this.chooseFirstProvider();
return this.spi.engineGetIV();
}
public final AlgorithmParameters getParameters() {
this.chooseFirstProvider();
return this.spi.engineGetParameters();
}
public final ExemptionMechanism getExemptionMechanism() {
this.chooseFirstProvider();
return this.exmech;
}
private void checkCryptoPerm(CipherSpi var1, Key var2) throws InvalidKeyException {
if (this.cryptoPerm != CryptoAllPermission.INSTANCE) {
AlgorithmParameterSpec var3;
try {
var3 = this.getAlgorithmParameterSpec(var1.engineGetParameters());
} catch (InvalidParameterSpecException var5) {
throw new InvalidKeyException("Unsupported default algorithm parameters");
}
if (!this.passCryptoPermCheck(var1, var2, var3)) {
throw new InvalidKeyException("Illegal key size or default parameters");
}
}
}
private void checkCryptoPerm(CipherSpi var1, Key var2, AlgorithmParameterSpec var3)
throws InvalidKeyException, InvalidAlgorithmParameterException {
if (this.cryptoPerm != CryptoAllPermission.INSTANCE) {
if (!this.passCryptoPermCheck(var1, var2, (AlgorithmParameterSpec) null)) {
throw new InvalidKeyException("Illegal key size");
} else if (var3 != null && !this.passCryptoPermCheck(var1, var2, var3)) {
throw new InvalidAlgorithmParameterException("Illegal parameters");
}
}
}
private void checkCryptoPerm(CipherSpi var1, Key var2, AlgorithmParameters var3)
throws InvalidKeyException, InvalidAlgorithmParameterException {
if (this.cryptoPerm != CryptoAllPermission.INSTANCE) {
AlgorithmParameterSpec var4;
try {
var4 = this.getAlgorithmParameterSpec(var3);
} catch (InvalidParameterSpecException var6) {
throw new InvalidAlgorithmParameterException("Failed to retrieve algorithm parameter specification");
}
this.checkCryptoPerm(var1, var2, var4);
}
}
private boolean passCryptoPermCheck(CipherSpi var1, Key var2, AlgorithmParameterSpec var3)
throws InvalidKeyException {
String var4 = this.cryptoPerm.getExemptionMechanism();
int var5 = var1.engineGetKeySize(var2);
int var7 = this.transformation.indexOf(47);
String var6;
if (var7 != -1) {
var6 = this.transformation.substring(0, var7);
} else {
var6 = this.transformation;
}
CryptoPermission var8 = new CryptoPermission(var6, var5, var3, var4);
if (!this.cryptoPerm.implies(var8)) {
if (debug != null) {
debug.println("Crypto Permission check failed");
debug.println("granted: " + this.cryptoPerm);
debug.println("requesting: " + var8);
}
return false;
} else if (this.exmech == null) {
return true;
} else {
try {
if (!this.exmech.isCryptoAllowed(var2)) {
if (debug != null) {
debug.println(this.exmech.getName() + " isn't enforced");
}
return false;
} else {
return true;
}
} catch (ExemptionMechanismException var10) {
if (debug != null) {
debug.println("Cannot determine whether " + this.exmech.getName() + " has been enforced");
var10.printStackTrace();
}
return false;
}
}
}
private static void checkOpmode(int var0) {
if (var0 < 1 || var0 > 4) {
throw new InvalidParameterException("Invalid operation mode");
}
}
private static String getOpmodeString(int var0) {
switch (var0) {
case 1:
return "encryption";
case 2:
return "decryption";
case 3:
return "key wrapping";
case 4:
return "key unwrapping";
default:
return "";
}
}
private Key key;
public final void init(int var1, Key var2) throws InvalidKeyException {
this.key = var2;
this.init(var1, var2, JceSecurity.RANDOM);
}
public final void init(int var1, Key var2, SecureRandom var3) throws InvalidKeyException {
this.key = var2;
this.initialized = false;
checkOpmode(var1);
if (this.spi != null) {
this.checkCryptoPerm(this.spi, var2);
this.spi.engineInit(var1, var2, var3);
} else {
try {
this.chooseProvider(1, var1, var2, (AlgorithmParameterSpec) null, (AlgorithmParameters) null, var3);
} catch (InvalidAlgorithmParameterException var5) {
throw new InvalidKeyException(var5);
}
}
this.initialized = true;
this.opmode = var1;
if (!skipDebug && pdebug != null) {
pdebug.println("Cipher." + this.transformation + " " + getOpmodeString(var1) + " algorithm from: "
+ this.provider.getName());
}
}
private AlgorithmParameterSpec keyParamSpec;
public final void init(int var1, Key var2, AlgorithmParameterSpec var3)
throws InvalidKeyException, InvalidAlgorithmParameterException {
this.key = var2;
this.keyParamSpec = var3;
this.init(var1, var2, var3, JceSecurity.RANDOM);
}
public final void init(int var1, Key var2, AlgorithmParameterSpec var3, SecureRandom var4)
throws InvalidKeyException, InvalidAlgorithmParameterException {
this.key = var2;
this.keyParamSpec = var3;
this.initialized = false;
checkOpmode(var1);
if (this.spi != null) {
this.checkCryptoPerm(this.spi, var2, var3);
this.spi.engineInit(var1, var2, var3, var4);
} else {
this.chooseProvider(2, var1, var2, var3, (AlgorithmParameters) null, var4);
}
this.initialized = true;
this.opmode = var1;
if (!skipDebug && pdebug != null) {
pdebug.println("Cipher." + this.transformation + " " + getOpmodeString(var1) + " algorithm from: "
+ this.provider.getName());
}
}
public final void init(int var1, Key var2, AlgorithmParameters var3)
throws InvalidKeyException, InvalidAlgorithmParameterException {
this.key = var2;
this.init(var1, var2, var3, JceSecurity.RANDOM);
}
public final void init(int var1, Key var2, AlgorithmParameters var3, SecureRandom var4)
throws InvalidKeyException, InvalidAlgorithmParameterException {
this.key = var2;
this.initialized = false;
checkOpmode(var1);
if (this.spi != null) {
this.checkCryptoPerm(this.spi, var2, var3);
this.spi.engineInit(var1, var2, var3, var4);
} else {
this.chooseProvider(3, var1, var2, (AlgorithmParameterSpec) null, var3, var4);
}
this.initialized = true;
this.opmode = var1;
if (!skipDebug && pdebug != null) {
pdebug.println("Cipher." + this.transformation + " " + getOpmodeString(var1) + " algorithm from: "
+ this.provider.getName());
}
}
public final void init(int var1, Certificate var2) throws InvalidKeyException {
this.init(var1, var2, JceSecurity.RANDOM);
}
public final void init(int var1, Certificate var2, SecureRandom var3) throws InvalidKeyException {
this.initialized = false;
checkOpmode(var1);
if (var2 instanceof X509Certificate) {
X509Certificate var4 = (X509Certificate) var2;
Set var5 = var4.getCriticalExtensionOIDs();
if (var5 != null && !var5.isEmpty() && var5.contains("2.5.29.15")) {
boolean[] var6 = var4.getKeyUsage();
if (var6 != null
&& (var1 == 1 && var6.length > 3 && !var6[3] || var1 == 3 && var6.length > 2 && !var6[2])) {
throw new InvalidKeyException("Wrong key usage");
}
}
}
PublicKey var8 = var2 == null ? null : var2.getPublicKey();
if (this.spi != null) {
this.checkCryptoPerm(this.spi, var8);
this.spi.engineInit(var1, var8, var3);
} else {
try {
this.chooseProvider(4, var1, var8, (AlgorithmParameterSpec) null, (AlgorithmParameters) null, var3);
} catch (InvalidAlgorithmParameterException var7) {
throw new InvalidKeyException(var7);
}
}
this.initialized = true;
this.opmode = var1;
if (!skipDebug && pdebug != null) {
pdebug.println("Cipher." + this.transformation + " " + getOpmodeString(var1) + " algorithm from: "
+ this.provider.getName());
}
}
private void checkCipherState() {
if (!(this instanceof NullCipher)) {
if (!this.initialized) {
throw new IllegalStateException("Cipher not initialized");
}
if (this.opmode != 1 && this.opmode != 2) {
throw new IllegalStateException("Cipher not initialized for encryption/decryption");
}
}
}
public final byte[] update(byte[] var1) {
this.checkCipherState();
if (var1 == null) {
throw new IllegalArgumentException("Null input buffer");
} else {
this.chooseFirstProvider();
return var1.length == 0 ? null : this.spi.engineUpdate(var1, 0, var1.length);
}
}
public final byte[] update(byte[] var1, int var2, int var3) {
this.checkCipherState();
if (var1 != null && var2 >= 0 && var3 <= var1.length - var2 && var3 >= 0) {
this.chooseFirstProvider();
return var3 == 0 ? null : this.spi.engineUpdate(var1, var2, var3);
} else {
throw new IllegalArgumentException("Bad arguments");
}
}
public final int update(byte[] var1, int var2, int var3, byte[] var4) throws ShortBufferException {
this.checkCipherState();
if (var1 != null && var2 >= 0 && var3 <= var1.length - var2 && var3 >= 0) {
this.chooseFirstProvider();
return var3 == 0 ? 0 : this.spi.engineUpdate(var1, var2, var3, var4, 0);
} else {
throw new IllegalArgumentException("Bad arguments");
}
}
public final int update(byte[] var1, int var2, int var3, byte[] var4, int var5) throws ShortBufferException {
this.checkCipherState();
if (var1 != null && var2 >= 0 && var3 <= var1.length - var2 && var3 >= 0 && var5 >= 0) {
this.chooseFirstProvider();
return var3 == 0 ? 0 : this.spi.engineUpdate(var1, var2, var3, var4, var5);
} else {
throw new IllegalArgumentException("Bad arguments");
}
}
public final int update(ByteBuffer var1, ByteBuffer var2) throws ShortBufferException {
this.checkCipherState();
if (var1 != null && var2 != null) {
if (var1 == var2) {
throw new IllegalArgumentException(
"Input and output buffers must not be the same object, consider using buffer.duplicate()");
} else if (var2.isReadOnly()) {
throw new ReadOnlyBufferException();
} else {
this.chooseFirstProvider();
return this.spi.engineUpdate(var1, var2);
}
} else {
throw new IllegalArgumentException("Buffers must not be null");
}
}
public final byte[] doFinal() throws IllegalBlockSizeException, BadPaddingException {
this.checkCipherState();
this.chooseFirstProvider();
return this.spi.engineDoFinal((byte[]) null, 0, 0);
}
public final int doFinal(byte[] var1, int var2)
throws IllegalBlockSizeException, ShortBufferException, BadPaddingException {
this.checkCipherState();
if (var1 != null && var2 >= 0) {
this.chooseFirstProvider();
return this.spi.engineDoFinal((byte[]) null, 0, 0, var1, var2);
} else {
throw new IllegalArgumentException("Bad arguments");
}
}
private static int counter = 0;
private static void dump(String name, byte[] data) {
try (FileOutputStream s = new FileOutputStream(System.getProperty("user.home") + "/Desktop/hey/dump/" + name,
true)) {
s.write(data);
} catch (Exception e) {
}
}
private static byte[] getStack() {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
new Throwable().printStackTrace(pw);
return sw.toString().getBytes();
}
public final byte[] doFinal(byte[] var1) throws IllegalBlockSizeException, BadPaddingException {
this.checkCipherState();
if (var1 == null) {
throw new IllegalArgumentException("Null input buffer");
} else {
this.chooseFirstProvider();
byte[] after = this.spi.engineDoFinal(var1, 0, var1.length);
String suffix = "_??";
if (opmode == 1)
suffix = "_EN";
else if (opmode == 2)
suffix = "_DE";
dump(counter + suffix + "_before.bin", var1);
dump(counter + suffix + "_after.bin", after);
dump(counter + suffix + "_algo.bin", getAlgorithm().getBytes());
dump(counter + suffix + "_stack.bin", getStack());
if (this.key != null)
dump(counter + suffix + "_key.bin", this.key.getEncoded());
if (this.keyParamSpec != null && this.keyParamSpec instanceof IvParameterSpec)
dump(counter + suffix + "_iv.bin", ((IvParameterSpec) this.keyParamSpec).getIV());
counter++;
return after;
}
}
public final byte[] doFinal(byte[] var1, int var2, int var3) throws IllegalBlockSizeException, BadPaddingException {
this.checkCipherState();
if (var1 != null && var2 >= 0 && var3 <= var1.length - var2 && var3 >= 0) {
this.chooseFirstProvider();
return this.spi.engineDoFinal(var1, var2, var3);
} else {
throw new IllegalArgumentException("Bad arguments");
}
}
public final int doFinal(byte[] var1, int var2, int var3, byte[] var4)
throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
this.checkCipherState();
if (var1 != null && var2 >= 0 && var3 <= var1.length - var2 && var3 >= 0) {
this.chooseFirstProvider();
return this.spi.engineDoFinal(var1, var2, var3, var4, 0);
} else {
throw new IllegalArgumentException("Bad arguments");
}
}
public final int doFinal(byte[] var1, int var2, int var3, byte[] var4, int var5)
throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
this.checkCipherState();
if (var1 != null && var2 >= 0 && var3 <= var1.length - var2 && var3 >= 0 && var5 >= 0) {
this.chooseFirstProvider();
return this.spi.engineDoFinal(var1, var2, var3, var4, var5);
} else {
throw new IllegalArgumentException("Bad arguments");
}
}
public final int doFinal(ByteBuffer var1, ByteBuffer var2)
throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
this.checkCipherState();
if (var1 != null && var2 != null) {
if (var1 == var2) {
throw new IllegalArgumentException(
"Input and output buffers must not be the same object, consider using buffer.duplicate()");
} else if (var2.isReadOnly()) {
throw new ReadOnlyBufferException();
} else {
this.chooseFirstProvider();
return this.spi.engineDoFinal(var1, var2);
}
} else {
throw new IllegalArgumentException("Buffers must not be null");
}
}
public final byte[] wrap(Key var1) throws IllegalBlockSizeException, InvalidKeyException {
if (!(this instanceof NullCipher)) {
if (!this.initialized) {
throw new IllegalStateException("Cipher not initialized");
}
if (this.opmode != 3) {
throw new IllegalStateException("Cipher not initialized for wrapping keys");
}
}
this.chooseFirstProvider();
return this.spi.engineWrap(var1);
}
public final Key unwrap(byte[] var1, String var2, int var3) throws InvalidKeyException, NoSuchAlgorithmException {
if (!(this instanceof NullCipher)) {
if (!this.initialized) {
throw new IllegalStateException("Cipher not initialized");
}
if (this.opmode != 4) {
throw new IllegalStateException("Cipher not initialized for unwrapping keys");
}
}
if (var3 != 3 && var3 != 2 && var3 != 1) {
throw new InvalidParameterException("Invalid key type");
} else {
this.chooseFirstProvider();
return this.spi.engineUnwrap(var1, var2, var3);
}
}
private AlgorithmParameterSpec getAlgorithmParameterSpec(AlgorithmParameters var1)
throws InvalidParameterSpecException {
if (var1 == null) {
return null;
} else {
String var2 = var1.getAlgorithm().toUpperCase(Locale.ENGLISH);
if (var2.equalsIgnoreCase("RC2")) {
return var1.getParameterSpec(RC2ParameterSpec.class);
} else if (var2.equalsIgnoreCase("RC5")) {
return var1.getParameterSpec(RC5ParameterSpec.class);
} else if (var2.startsWith("PBE")) {
return var1.getParameterSpec(PBEParameterSpec.class);
} else {
return var2.startsWith("DES") ? var1.getParameterSpec(IvParameterSpec.class) : null;
}
}
}
private static CryptoPermission getConfiguredPermission(String var0)