| | | | |
| 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 | { |
| 11 | ····private·final·boolean·mIsJceSupported; | 14 | ····private·final·Cipher·mCipher; |
| | 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; |
| 26 | ····················this.mMac·=·null; | 31 | ····················final·String·algorithmName2·=·this.getAlgorithmName(); |
| 27 | ················} | 32 | ····················try·{ |
| 28 | ················catch·(NoSuchAlgorithmException·ex)·{ | 33 | ························this.mMac·=·Mac.getInstance(algorithmName2); |
| 29 | ····················final·StringBuilder·sb·=·new·StringBuilder(); | 34 | ························try·{ |
| 30 | ····················sb.append("Failed·to·construct·"); | 35 | ····························this.mCipher·=·null; |
| 31 | ····················sb.append(this.getTypeString()); | 36 | ························} |
| 32 | ····················throw·new·IllegalArgumentException(sb.toString(),·ex); | 37 | ························catch·(NoSuchAlgorithmException·|·NoSuchPaddingException·ex3)·{ |
| | 38 | ····························final·NoSuchPaddingException·ex2; |
| | 39 | ····························final·NoSuchPaddingException·ex·=·ex2; |
| | 40 | ····························final·StringBuilder·sb·=·new·StringBuilder(); |
| | 41 | ····························sb.append("Failed·to·construct·"); |
| | 42 | ····························sb.append(this.getTypeString()); |
| | 43 | ····························throw·new·IllegalArgumentException(sb.toString(),·ex); |
| | 44 | ························} |
| | 45 | ····················} |
| | 46 | ····················catch·(NoSuchPaddingException·ex4)·{} |
| 33 | ················} | 47 | ················} |
| | 48 | ················catch·(NoSuchAlgorithmException·ex5)·{} |
| | 49 | ················catch·(NoSuchPaddingException·ex6)·{} |
| 34 | ············} | 50 | ············} |
| 35 | ············catch·(NoSuchAlgorithmException·ex2)·{} | 51 | ············catch·(NoSuchAlgorithmException·ex7)·{} |
| | 52 | ············catch·(NoSuchPaddingException·ex8)·{} |
| 36 | ········} | 53 | ········} |
| 37 | ····} | 54 | ····} |
| 38 | ···· | 55 | ···· |
| 39 | ····public·byte[]·signBytes(final·byte[]·array,·final·byte[]·array2)·{ | 56 | ····private·byte[]·modifyKeyIfNeeded(byte[]·array)·{ |
| | 57 | ········final·int·length·=·array.length; |
| | 58 | ········final·int·n·=·16; |
| | 59 | ········if·(length·<·n)·{ |
| | 60 | ············array·=·Arrays.copyOf(array,·n); |
| | 61 | ········} |
| | 62 | ········else·if·(array.length·>·n)·{ |
| | 63 | ············array·=·new·AesXCbcImpl(this.mCipher).signBytes(new·byte[n],·array,·false); |
| | 64 | ········} |
| | 65 | ········return·array; |
| | 66 | ····} |
| | 67 | ···· |
| | 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·{ | 74 | ················final·int·algorithmId·=·this.getAlgorithmId(); |
| 46 | ····················final·SecretKeySpec·secretKeySpec·=·new·SecretKeySpec(array,·this.getAlgorithmName()); | 75 | ················Label_0114:·{ |
| 47 | ····················final·ByteBuffer·wrap·=·ByteBuffer.wrap(array2); | 76 | ····················if·(algorithmId·==·4)·{ |
| 48 | ····················try·{ | 77 | ························break·Label_0114; |
| 49 | ························this.mMac.init(secretKeySpec); | 78 | ····················} |
| 50 | ························this.mMac.update(wrap); | 79 | ····················Label_0063:·{ |
| 51 | ························final·Mac·mMac·=·this.mMac; | 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:·"); |
| 58 | ····································sb.append(this.getAlgorithmId()); | 89 | ····································final·StringBuilder·sb2·=·sb; |
| 59 | ····································throw·new·IllegalStateException(sb.toString()); | 90 | ····································try·{ |
| 60 | ································} | 91 | ········································sb2.append(algorithmId); |
| 61 | ································catch·(GeneralSecurityException·|·IllegalStateException·ex)·{ | 92 | ········································throw·new·IllegalStateException(sb.toString()); |
| 62 | ····································final·Object·o; | 93 | ········································try·{ |
| 63 | ····································throw·new·IllegalArgumentException("Failed·to·generate·MAC:·",·(Throwable)o); | 94 | ············································modifyKeyIfNeeded·=·this.modifyKeyIfNeeded(modifyKeyIfNeeded); |
| | 95 | ············································try·{ |
| | 96 | ················································return·new·AesXCbcImpl(this.mCipher).signBytes(modifyKeyIfNeeded,·array,·false); |
| | 97 | ················································try·{ |
| | 98 | ····················································final·SecretKeySpec·secretKeySpec·=·new·SecretKeySpec(modifyKeyIfNeeded,·this.getAlgorithmName()); |
| | 99 | ····················································final·ByteBuffer·wrap·=·ByteBuffer.wrap(array); |
| | 100 | ····················································try·{ |
| | 101 | ························································this.mMac.init(secretKeySpec); |
| | 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·o; |
| | 109 | ····························································throw·new·IllegalArgumentException("Failed·to·generate·MAC:·",·(Throwable)o); |
| | 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)·{} |
| | 122 | ····································} |
| | 123 | ····································catch·(InvalidKeyException·ex9)·{} |
| | 124 | ····································catch·(IllegalStateException·ex10)·{} |
| 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 | } |