| | | |
Offset 1, 84 lines modified | Offset 1, 143 lines modified |
| |
1 | package·com.android.internal.net.ipsec.ike.crypto; | 1 | package·com.android.internal.net.ipsec.ike.crypto; |
| |
2 | import·java.security.GeneralSecurityException; | 2 | import·java.security.InvalidKeyException; |
3 | import·java.security.Key; | 3 | import·java.security.Key; |
4 | import·java.nio.ByteBuffer; | 4 | import·java.nio.ByteBuffer; |
5 | import·javax.crypto.spec.SecretKeySpec; | 5 | import·javax.crypto.spec.SecretKeySpec; |
| 6 | import·java.util.Arrays; |
| 7 | import·javax.crypto.NoSuchPaddingException; |
6 | import·java.security.NoSuchAlgorithmException; | 8 | import·java.security.NoSuchAlgorithmException; |
7 | import·javax.crypto.Mac; | 9 | import·javax.crypto.Mac; |
| 10 | import·javax.crypto.Cipher; |
8 | import·com.android.internal.net.crypto.KeyGenerationUtils$ByteSigner; | 11 | import·com.android.internal.net.crypto.KeyGenerationUtils$ByteSigner; |
| |
9 | abstract·class·IkeMac·extends·IkeCrypto·implements·KeyGenerationUtils$ByteSigner | 12 | abstract·class·IkeMac·extends·IkeCrypto·implements·KeyGenerationUtils$ByteSigner |
10 | { | 13 | { |
| 14 | ····private·final·Cipher·mCipher; |
11 | ····private·final·boolean·mIsJceSupported; | 15 | ····private·final·boolean·mIsEncryptAlgo; |
12 | ····private·final·Mac·mMac; | 16 | ····private·final·Mac·mMac; |
13 | ···· | 17 | ···· |
14 | ····protected·IkeMac(final·int·n,·final·int·n2,·final·String·s,·final·boolean·mIsJceSupported)·{ | 18 | ····protected·IkeMac(final·int·n,·final·int·n2,·final·String·s,·final·boolean·mIsEncryptAlgo)·{ |
15 | ········super(n,·n2,·s); | 19 | ········super(n,·n2,·s); |
16 | ········this.mIsJceSupported·=·mIsJceSupported; | 20 | ········this.mIsEncryptAlgo·=·mIsEncryptAlgo; |
17 | ········Label_0040:·{ | 21 | ········Label_0048:·{ |
18 | ············if·(!mIsJceSupported)·{ | 22 | ············if·(!mIsEncryptAlgo)·{ |
19 | ················break·Label_0040; | 23 | ················break·Label_0048; |
20 | ············} | 24 | ············} |
21 | ············try·{ | 25 | ············try·{ |
| 26 | ················this.mMac·=·null; |
22 | ················final·String·algorithmName·=·this.getAlgorithmName(); | 27 | ················final·String·algorithmName·=·this.getAlgorithmName(); |
23 | ················try·{ | 28 | ················try·{ |
24 | ····················this.mMac·=·Mac.getInstance(algorithmName); | 29 | ····················this.mCipher·=·Cipher.getInstance(algorithmName); |
25 | ····················return; | 30 | ····················return; |
| 31 | ····················final·String·algorithmName2·=·this.getAlgorithmName(); |
| 32 | ····················try·{ |
| 33 | ························this.mMac·=·Mac.getInstance(algorithmName2); |
| 34 | ························try·{ |
26 | ····················this.mMac·=·null; | 35 | ····························this.mCipher·=·null; |
27 | ················} | 36 | ························} |
28 | ················catch·(NoSuchAlgorithmException·cause)·{ | 37 | ························catch·(NoSuchAlgorithmException·|·NoSuchPaddingException·ex2)·{ |
| 38 | ····························final·NoSuchPaddingException·ex; |
| 39 | ····························final·NoSuchPaddingException·cause·=·ex; |
29 | ····················final·StringBuilder·sb·=·new·StringBuilder(); | 40 | ····························final·StringBuilder·sb·=·new·StringBuilder(); |
30 | ····················sb.append("Failed·to·construct·"); | 41 | ····························sb.append("Failed·to·construct·"); |
31 | ····················sb.append(this.getTypeString()); | 42 | ····························sb.append(this.getTypeString()); |
32 | ····················throw·new·IllegalArgumentException(sb.toString(),·cause); | 43 | ····························throw·new·IllegalArgumentException(sb.toString(),·cause); |
| 44 | ························} |
| 45 | ····················} |
| 46 | ····················catch·(NoSuchPaddingException·ex3)·{} |
33 | ················} | 47 | ················} |
| 48 | ················catch·(NoSuchAlgorithmException·ex4)·{} |
| 49 | ················catch·(NoSuchPaddingException·ex5)·{} |
34 | ············} | 50 | ············} |
35 | ············catch·(NoSuchAlgorithmException·ex)·{} | 51 | ············catch·(NoSuchAlgorithmException·ex6)·{} |
| 52 | ············catch·(NoSuchPaddingException·ex7)·{} |
36 | ········} | 53 | ········} |
37 | ····} | 54 | ····} |
38 | ···· | 55 | ···· |
| 56 | ····private·byte[]·modifyKeyIfNeeded(byte[]·original)·{ |
| 57 | ········final·int·length·=·original.length; |
| 58 | ········final·int·newLength·=·16; |
| 59 | ········if·(length·<·newLength)·{ |
| 60 | ············original·=·Arrays.copyOf(original,·newLength); |
| 61 | ········} |
| 62 | ········else·if·(original.length·>·newLength)·{ |
| 63 | ············original·=·new·AesXCbcImpl(this.mCipher).signBytes(new·byte[newLength],·original,·false); |
| 64 | ········} |
| 65 | ········return·original; |
| 66 | ····} |
| 67 | ···· |
39 | ····public·byte[]·signBytes(final·byte[]·key,·final·byte[]·array)·{ | 68 | ····public·byte[]·signBytes(byte[]·modifyKeyIfNeeded,·final·byte[]·array)·{ |
40 | ········try·{ | 69 | ········try·{ |
41 | ············Label_0072:·{ | 70 | ············Label_0157:·{ |
42 | ················if·(!this.mIsJceSupported)·{ | 71 | ················if·(!this.mIsEncryptAlgo)·{ |
43 | ····················break·Label_0072; | 72 | ····················break·Label_0157; |
44 | ················} | 73 | ················} |
45 | ················try·{ | |
46 | ····················final·SecretKeySpec·key2·=·new·SecretKeySpec(key,·this.getAlgorithmName()); | |
47 | ····················final·ByteBuffer·wrap·=·ByteBuffer.wrap(array); | 74 | ················final·int·algorithmId·=·this.getAlgorithmId(); |
| 75 | ················Label_0114:·{ |
| 76 | ····················if·(algorithmId·==·4)·{ |
| 77 | ························break·Label_0114; |
48 | ····················try·{ | 78 | ····················} |
49 | ························this.mMac.init(key2); | |
50 | ························this.mMac.update(wrap); | |
51 | ························final·Mac·mMac·=·this.mMac; | 79 | ····················Label_0063:·{ |
| 80 | ························if·(algorithmId·!=·5)·{ |
| 81 | ····························break·Label_0063; |
| 82 | ························} |
52 | ························try·{ | 83 | ························try·{ |
53 | ····························return·mMac.doFinal(); | 84 | ····························return·new·AesXCbcImpl(this.mCipher).signBytes(modifyKeyIfNeeded,·array,·true); |
54 | ····························try·{ | 85 | ····························try·{ |
55 | ································try·{ | 86 | ································try·{ |
56 | ····································final·StringBuilder·sb·=·new·StringBuilder(); | 87 | ····································final·StringBuilder·sb·=·new·StringBuilder(); |
57 | ····································sb.append("Invalid·algorithm:·"); | 88 | ····································sb.append("Invalid·algorithm:·"); |
| 89 | ····································final·StringBuilder·sb2·=·sb; |
| 90 | ····································try·{ |
58 | ····································sb.append(this.getAlgorithmId()); | 91 | ········································sb2.append(algorithmId); |
59 | ····································throw·new·IllegalStateException(sb.toString()); | 92 | ········································throw·new·IllegalStateException(sb.toString()); |
| 93 | ········································try·{ |
| 94 | ············································modifyKeyIfNeeded·=·this.modifyKeyIfNeeded(modifyKeyIfNeeded); |
| 95 | ············································try·{ |
| 96 | ················································return·new·AesXCbcImpl(this.mCipher).signBytes(modifyKeyIfNeeded,·array,·false); |
| 97 | ················································try·{ |
| 98 | ····················································final·SecretKeySpec·key·=·new·SecretKeySpec(modifyKeyIfNeeded,·this.getAlgorithmName()); |
| 99 | ····················································final·ByteBuffer·wrap·=·ByteBuffer.wrap(array); |
| 100 | ····················································try·{ |
| 101 | ························································this.mMac.init(key); |
| 102 | ························································this.mMac.update(wrap); |
| 103 | ························································final·Mac·mMac·=·this.mMac; |
| 104 | ························································try·{ |
| 105 | ····························································return·mMac.doFinal(); |
| 106 | ························································} |
| 107 | ························································catch·(InvalidKeyException·|·IllegalStateException·ex)·{ |
| 108 | ····························································final·Object·cause; |
| 109 | ····························································throw·new·IllegalArgumentException("Failed·to·generate·MAC:·",·(Throwable)cause); |
| 110 | ························································} |
| 111 | ····················································} |
| 112 | ····················································catch·(InvalidKeyException·ex2)·{} |
| 113 | ····················································catch·(IllegalStateException·ex3)·{} |
| 114 | ················································} |
| 115 | ················································catch·(IllegalStateException·ex4)·{} |
| 116 | ············································} |
| 117 | ············································catch·(InvalidKeyException·ex5)·{} |
| 118 | ············································catch·(IllegalStateException·ex6)·{} |
| 119 | ········································} |
| 120 | ········································catch·(InvalidKeyException·ex7)·{} |
| 121 | ········································catch·(IllegalStateException·ex8)·{} |
60 | ································} | 122 | ····································} |
| 123 | ····································catch·(InvalidKeyException·ex9)·{} |
61 | ································catch·(GeneralSecurityException·|·IllegalStateException·ex)·{ | 124 | ····································catch·(IllegalStateException·ex10)·{} |
62 | ····································final·Object·cause; | |
63 | ····································throw·new·IllegalArgumentException("Failed·to·generate·MAC:·",·(Throwable)cause); | |
64 | ································} | 125 | ································} |
| 126 | ································catch·(InvalidKeyException·ex11)·{} |
| 127 | ································catch·(IllegalStateException·ex12)·{} |
65 | ····························} | 128 | ····························} |
66 | ····························catch·(IllegalStateException·ex2)·{} | 129 | ····························catch·(IllegalStateException·ex13)·{} |
67 | ························} | 130 | ························} |
68 | ························catch·(GeneralSecurityException·ex3)·{} | 131 | ························catch·(InvalidKeyException·ex14)·{} |
69 | ························catch·(IllegalStateException·ex4)·{} | 132 | ························catch·(IllegalStateException·ex15)·{} |
70 | ····················} | 133 | ····················} |
71 | ····················catch·(GeneralSecurityException·ex5)·{} | |
72 | ····················catch·(IllegalStateException·ex6)·{} | |
73 | ················} | 134 | ················} |
74 | ················catch·(GeneralSecurityException·ex7)·{} | |
75 | ················catch·(IllegalStateException·ex8)·{} | |
76 | ············} | 135 | ············} |
77 | ········} | 136 | ········} |
78 | ········catch·(GeneralSecurityException·ex9)·{} | 137 | ········catch·(InvalidKeyException·ex16)·{} |
79 | ········catch·(IllegalStateException·ex10)·{} | 138 | ········catch·(IllegalStateException·ex17)·{} |
80 | ····} | 139 | ····} |
81 | } | 140 | } |