65 lines
3.4 KiB
Java
65 lines
3.4 KiB
Java
package org.bouncycastle.jce;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.SecretKeyFactory;
|
|
import javax.crypto.spec.PBEKeySpec;
|
|
import javax.crypto.spec.PBEParameterSpec;
|
|
import org.bouncycastle.asn1.ASN1InputStream;
|
|
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
|
import org.bouncycastle.asn1.ASN1OctetString;
|
|
import org.bouncycastle.asn1.DERNull;
|
|
import org.bouncycastle.asn1.DEROctetString;
|
|
import org.bouncycastle.asn1.DEROutputStream;
|
|
import org.bouncycastle.asn1.pkcs.ContentInfo;
|
|
import org.bouncycastle.asn1.pkcs.MacData;
|
|
import org.bouncycastle.asn1.pkcs.Pfx;
|
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
|
|
import org.bouncycastle.asn1.x509.DigestInfo;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class PKCS12Util {
|
|
public static byte[] convertToDefiniteLength(byte[] bArr, char[] cArr, String str) throws IOException {
|
|
Pfx pfx = Pfx.getInstance(bArr);
|
|
ContentInfo authSafe = pfx.getAuthSafe();
|
|
ASN1OctetString aSN1OctetString = ASN1OctetString.getInstance(authSafe.getContent());
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
DEROutputStream dEROutputStream = new DEROutputStream(byteArrayOutputStream);
|
|
dEROutputStream.writeObject(new ASN1InputStream(aSN1OctetString.getOctets()).readObject());
|
|
ContentInfo contentInfo = new ContentInfo(authSafe.getContentType(), new DEROctetString(byteArrayOutputStream.toByteArray()));
|
|
MacData macData = pfx.getMacData();
|
|
try {
|
|
int intValue = macData.getIterationCount().intValue();
|
|
Pfx pfx2 = new Pfx(contentInfo, new MacData(new DigestInfo(new AlgorithmIdentifier(macData.getMac().getAlgorithmId().getAlgorithm(), DERNull.INSTANCE), calculatePbeMac(macData.getMac().getAlgorithmId().getAlgorithm(), macData.getSalt(), intValue, cArr, ASN1OctetString.getInstance(contentInfo.getContent()).getOctets(), str)), macData.getSalt(), intValue));
|
|
byteArrayOutputStream.reset();
|
|
dEROutputStream.writeObject(pfx2);
|
|
return byteArrayOutputStream.toByteArray();
|
|
} catch (Exception e) {
|
|
StringBuilder sb = new StringBuilder("error constructing MAC: ");
|
|
sb.append(e.toString());
|
|
throw new IOException(sb.toString());
|
|
}
|
|
}
|
|
|
|
public static byte[] convertToDefiniteLength(byte[] bArr) throws IOException {
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
DEROutputStream dEROutputStream = new DEROutputStream(byteArrayOutputStream);
|
|
Pfx pfx = Pfx.getInstance(bArr);
|
|
byteArrayOutputStream.reset();
|
|
dEROutputStream.writeObject(pfx);
|
|
return byteArrayOutputStream.toByteArray();
|
|
}
|
|
|
|
private static byte[] calculatePbeMac(ASN1ObjectIdentifier aSN1ObjectIdentifier, byte[] bArr, int i, char[] cArr, byte[] bArr2, String str) throws Exception {
|
|
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(aSN1ObjectIdentifier.getId(), str);
|
|
PBEParameterSpec pBEParameterSpec = new PBEParameterSpec(bArr, i);
|
|
SecretKey generateSecret = secretKeyFactory.generateSecret(new PBEKeySpec(cArr));
|
|
Mac mac = Mac.getInstance(aSN1ObjectIdentifier.getId(), str);
|
|
mac.init(generateSecret, pBEParameterSpec);
|
|
mac.update(bArr2);
|
|
return mac.doFinal();
|
|
}
|
|
}
|