286 lines
12 KiB
Java
286 lines
12 KiB
Java
package org.bouncycastle.jcajce.provider.symmetric.util;
|
|
|
|
import java.security.AlgorithmParameters;
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.Key;
|
|
import java.security.KeyFactory;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchProviderException;
|
|
import java.security.PrivateKey;
|
|
import java.security.SecureRandom;
|
|
import java.security.spec.AlgorithmParameterSpec;
|
|
import java.security.spec.InvalidKeySpecException;
|
|
import java.security.spec.PKCS8EncodedKeySpec;
|
|
import java.security.spec.X509EncodedKeySpec;
|
|
import javax.crypto.BadPaddingException;
|
|
import javax.crypto.CipherSpi;
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
import javax.crypto.NoSuchPaddingException;
|
|
import javax.crypto.ShortBufferException;
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
import javax.crypto.spec.PBEParameterSpec;
|
|
import javax.crypto.spec.RC2ParameterSpec;
|
|
import javax.crypto.spec.RC5ParameterSpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
|
import org.bouncycastle.crypto.CipherParameters;
|
|
import org.bouncycastle.crypto.InvalidCipherTextException;
|
|
import org.bouncycastle.crypto.Wrapper;
|
|
import org.bouncycastle.crypto.params.KeyParameter;
|
|
import org.bouncycastle.crypto.params.ParametersWithIV;
|
|
import org.bouncycastle.crypto.params.ParametersWithRandom;
|
|
import org.bouncycastle.jcajce.provider.symmetric.util.PBE;
|
|
import org.bouncycastle.jcajce.util.BCJcaJceHelper;
|
|
import org.bouncycastle.jcajce.util.JcaJceHelper;
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
import org.bouncycastle.util.Arrays;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public abstract class BaseWrapCipher extends CipherSpi implements PBE {
|
|
private Class[] availableSpecs;
|
|
protected AlgorithmParameters engineParams;
|
|
private final JcaJceHelper helper;
|
|
private byte[] iv;
|
|
private int ivSize;
|
|
protected int pbeHash;
|
|
protected int pbeIvSize;
|
|
protected int pbeKeySize;
|
|
protected int pbeType;
|
|
protected Wrapper wrapEngine;
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected int engineDoFinal(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws IllegalBlockSizeException, BadPaddingException, ShortBufferException {
|
|
return 0;
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected byte[] engineDoFinal(byte[] bArr, int i, int i2) throws IllegalBlockSizeException, BadPaddingException {
|
|
return null;
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected int engineGetBlockSize() {
|
|
return 0;
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected int engineGetOutputSize(int i) {
|
|
return -1;
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected AlgorithmParameters engineGetParameters() {
|
|
return null;
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException {
|
|
byte[] encoded = key.getEncoded();
|
|
if (encoded == null) {
|
|
throw new InvalidKeyException("Cannot wrap key, null encoding.");
|
|
}
|
|
try {
|
|
Wrapper wrapper = this.wrapEngine;
|
|
return wrapper == null ? engineDoFinal(encoded, 0, encoded.length) : wrapper.wrap(encoded, 0, encoded.length);
|
|
} catch (BadPaddingException e) {
|
|
throw new IllegalBlockSizeException(e.getMessage());
|
|
}
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected byte[] engineUpdate(byte[] bArr, int i, int i2) {
|
|
throw new RuntimeException("not supported for wrapping");
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected int engineUpdate(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws ShortBufferException {
|
|
throw new RuntimeException("not supported for wrapping");
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected Key engineUnwrap(byte[] bArr, String str, int i) throws InvalidKeyException, NoSuchAlgorithmException {
|
|
try {
|
|
Wrapper wrapper = this.wrapEngine;
|
|
byte[] engineDoFinal = wrapper == null ? engineDoFinal(bArr, 0, bArr.length) : wrapper.unwrap(bArr, 0, bArr.length);
|
|
if (i == 3) {
|
|
return new SecretKeySpec(engineDoFinal, str);
|
|
}
|
|
if (str.equals("") && i == 2) {
|
|
try {
|
|
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(engineDoFinal);
|
|
PrivateKey privateKey = BouncyCastleProvider.getPrivateKey(privateKeyInfo);
|
|
if (privateKey != null) {
|
|
return privateKey;
|
|
}
|
|
StringBuilder sb = new StringBuilder("algorithm ");
|
|
sb.append(privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm());
|
|
sb.append(" not supported");
|
|
throw new InvalidKeyException(sb.toString());
|
|
} catch (Exception unused) {
|
|
throw new InvalidKeyException("Invalid key encoding.");
|
|
}
|
|
}
|
|
try {
|
|
KeyFactory createKeyFactory = this.helper.createKeyFactory(str);
|
|
if (i == 1) {
|
|
return createKeyFactory.generatePublic(new X509EncodedKeySpec(engineDoFinal));
|
|
}
|
|
if (i == 2) {
|
|
return createKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(engineDoFinal));
|
|
}
|
|
throw new InvalidKeyException("Unknown key type ".concat(String.valueOf(i)));
|
|
} catch (NoSuchProviderException e) {
|
|
StringBuilder sb2 = new StringBuilder("Unknown key type ");
|
|
sb2.append(e.getMessage());
|
|
throw new InvalidKeyException(sb2.toString());
|
|
} catch (InvalidKeySpecException e2) {
|
|
StringBuilder sb3 = new StringBuilder("Unknown key type ");
|
|
sb3.append(e2.getMessage());
|
|
throw new InvalidKeyException(sb3.toString());
|
|
}
|
|
} catch (BadPaddingException e3) {
|
|
throw new InvalidKeyException(e3.getMessage());
|
|
} catch (IllegalBlockSizeException e4) {
|
|
throw new InvalidKeyException(e4.getMessage());
|
|
} catch (InvalidCipherTextException e5) {
|
|
throw new InvalidKeyException(e5.getMessage());
|
|
}
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected void engineSetPadding(String str) throws NoSuchPaddingException {
|
|
StringBuilder sb = new StringBuilder("Padding ");
|
|
sb.append(str);
|
|
sb.append(" unknown.");
|
|
throw new NoSuchPaddingException(sb.toString());
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected void engineSetMode(String str) throws NoSuchAlgorithmException {
|
|
throw new NoSuchAlgorithmException("can't support mode ".concat(String.valueOf(str)));
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected void engineInit(int i, Key key, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
CipherParameters keyParameter;
|
|
Wrapper wrapper;
|
|
int i2;
|
|
if (key instanceof BCPBEKey) {
|
|
BCPBEKey bCPBEKey = (BCPBEKey) key;
|
|
if (algorithmParameterSpec instanceof PBEParameterSpec) {
|
|
keyParameter = PBE.Util.makePBEParameters(bCPBEKey, algorithmParameterSpec, this.wrapEngine.getAlgorithmName());
|
|
} else {
|
|
if (bCPBEKey.getParam() == null) {
|
|
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
|
|
}
|
|
keyParameter = bCPBEKey.getParam();
|
|
}
|
|
} else {
|
|
keyParameter = new KeyParameter(key.getEncoded());
|
|
}
|
|
if (algorithmParameterSpec instanceof IvParameterSpec) {
|
|
keyParameter = new ParametersWithIV(keyParameter, ((IvParameterSpec) algorithmParameterSpec).getIV());
|
|
}
|
|
if ((keyParameter instanceof KeyParameter) && (i2 = this.ivSize) != 0) {
|
|
byte[] bArr = new byte[i2];
|
|
this.iv = bArr;
|
|
secureRandom.nextBytes(bArr);
|
|
keyParameter = new ParametersWithIV(keyParameter, this.iv);
|
|
}
|
|
if (secureRandom != null) {
|
|
keyParameter = new ParametersWithRandom(keyParameter, secureRandom);
|
|
}
|
|
boolean z = true;
|
|
if (i == 1 || i == 2) {
|
|
throw new IllegalArgumentException("engine only valid for wrapping");
|
|
}
|
|
if (i == 3) {
|
|
wrapper = this.wrapEngine;
|
|
} else if (i != 4) {
|
|
System.out.println("eeek!");
|
|
return;
|
|
} else {
|
|
wrapper = this.wrapEngine;
|
|
z = false;
|
|
}
|
|
wrapper.init(z, keyParameter);
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected void engineInit(int i, Key key, SecureRandom secureRandom) throws InvalidKeyException {
|
|
try {
|
|
engineInit(i, key, (AlgorithmParameterSpec) null, secureRandom);
|
|
} catch (InvalidAlgorithmParameterException e) {
|
|
throw new IllegalArgumentException(e.getMessage());
|
|
}
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected void engineInit(int i, Key key, AlgorithmParameters algorithmParameters, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
AlgorithmParameterSpec algorithmParameterSpec = null;
|
|
if (algorithmParameters != null) {
|
|
int i2 = 0;
|
|
while (true) {
|
|
Class[] clsArr = this.availableSpecs;
|
|
if (i2 == clsArr.length) {
|
|
break;
|
|
}
|
|
try {
|
|
algorithmParameterSpec = algorithmParameters.getParameterSpec(clsArr[i2]);
|
|
break;
|
|
} catch (Exception unused) {
|
|
i2++;
|
|
}
|
|
}
|
|
if (algorithmParameterSpec == null) {
|
|
StringBuilder sb = new StringBuilder("can't handle parameter ");
|
|
sb.append(algorithmParameters.toString());
|
|
throw new InvalidAlgorithmParameterException(sb.toString());
|
|
}
|
|
}
|
|
this.engineParams = algorithmParameters;
|
|
engineInit(i, key, algorithmParameterSpec, secureRandom);
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected int engineGetKeySize(Key key) {
|
|
return key.getEncoded().length;
|
|
}
|
|
|
|
@Override // javax.crypto.CipherSpi
|
|
protected byte[] engineGetIV() {
|
|
return Arrays.clone(this.iv);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
public final AlgorithmParameters createParametersInstance(String str) throws NoSuchAlgorithmException, NoSuchProviderException {
|
|
return this.helper.createAlgorithmParameters(str);
|
|
}
|
|
|
|
public BaseWrapCipher(Wrapper wrapper, int i) {
|
|
this.availableSpecs = new Class[]{IvParameterSpec.class, PBEParameterSpec.class, RC2ParameterSpec.class, RC5ParameterSpec.class};
|
|
this.pbeType = 2;
|
|
this.pbeHash = 1;
|
|
this.engineParams = null;
|
|
this.wrapEngine = null;
|
|
this.helper = new BCJcaJceHelper();
|
|
this.wrapEngine = wrapper;
|
|
this.ivSize = i;
|
|
}
|
|
|
|
public BaseWrapCipher(Wrapper wrapper) {
|
|
this(wrapper, 0);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
public BaseWrapCipher() {
|
|
this.availableSpecs = new Class[]{IvParameterSpec.class, PBEParameterSpec.class, RC2ParameterSpec.class, RC5ParameterSpec.class};
|
|
this.pbeType = 2;
|
|
this.pbeHash = 1;
|
|
this.engineParams = null;
|
|
this.wrapEngine = null;
|
|
this.helper = new BCJcaJceHelper();
|
|
}
|
|
}
|