what-the-bank/sources/org/bouncycastle/jce/netscape/NetscapeCertRequest.java

180 lines
7.6 KiB
Java

package org.bouncycastle.jce.netscape;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERIA5String;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/* loaded from: classes6.dex */
public class NetscapeCertRequest extends ASN1Object {
String challenge;
DERBitString content;
AlgorithmIdentifier keyAlg;
PublicKey pubkey;
AlgorithmIdentifier sigAlg;
byte[] sigBits;
public boolean verify(String str) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, NoSuchProviderException {
if (!str.equals(this.challenge)) {
return false;
}
Signature signature = Signature.getInstance(this.sigAlg.getAlgorithm().getId(), BouncyCastleProvider.PROVIDER_NAME);
signature.initVerify(this.pubkey);
signature.update(this.content.getBytes());
return signature.verify(this.sigBits);
}
@Override // org.bouncycastle.asn1.ASN1Object, org.bouncycastle.asn1.ASN1Encodable
public ASN1Primitive toASN1Primitive() {
ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector();
ASN1EncodableVector aSN1EncodableVector2 = new ASN1EncodableVector();
try {
aSN1EncodableVector2.add(getKeySpec());
} catch (Exception unused) {
}
aSN1EncodableVector2.add(new DERIA5String(this.challenge));
aSN1EncodableVector.add(new DERSequence(aSN1EncodableVector2));
aSN1EncodableVector.add(this.sigAlg);
aSN1EncodableVector.add(new DERBitString(this.sigBits));
return new DERSequence(aSN1EncodableVector);
}
public void sign(PrivateKey privateKey, SecureRandom secureRandom) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, NoSuchProviderException, InvalidKeySpecException {
Signature signature = Signature.getInstance(this.sigAlg.getAlgorithm().getId(), BouncyCastleProvider.PROVIDER_NAME);
if (secureRandom != null) {
signature.initSign(privateKey, secureRandom);
} else {
signature.initSign(privateKey);
}
ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector();
aSN1EncodableVector.add(getKeySpec());
aSN1EncodableVector.add(new DERIA5String(this.challenge));
try {
signature.update(new DERSequence(aSN1EncodableVector).getEncoded(ASN1Encoding.DER));
this.sigBits = signature.sign();
} catch (IOException e) {
throw new SignatureException(e.getMessage());
}
}
public void sign(PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, NoSuchProviderException, InvalidKeySpecException {
sign(privateKey, null);
}
public void setSigningAlgorithm(AlgorithmIdentifier algorithmIdentifier) {
this.sigAlg = algorithmIdentifier;
}
public void setPublicKey(PublicKey publicKey) {
this.pubkey = publicKey;
}
public void setKeyAlgorithm(AlgorithmIdentifier algorithmIdentifier) {
this.keyAlg = algorithmIdentifier;
}
public void setChallenge(String str) {
this.challenge = str;
}
public AlgorithmIdentifier getSigningAlgorithm() {
return this.sigAlg;
}
public PublicKey getPublicKey() {
return this.pubkey;
}
public AlgorithmIdentifier getKeyAlgorithm() {
return this.keyAlg;
}
public String getChallenge() {
return this.challenge;
}
private static ASN1Sequence getReq(byte[] bArr) throws IOException {
return ASN1Sequence.getInstance(new ASN1InputStream(new ByteArrayInputStream(bArr)).readObject());
}
private ASN1Primitive getKeySpec() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
byteArrayOutputStream.write(this.pubkey.getEncoded());
byteArrayOutputStream.close();
return new ASN1InputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())).readObject();
} catch (IOException e) {
throw new InvalidKeySpecException(e.getMessage());
}
}
public NetscapeCertRequest(byte[] bArr) throws IOException {
this(getReq(bArr));
}
public NetscapeCertRequest(ASN1Sequence aSN1Sequence) {
try {
if (aSN1Sequence.size() != 3) {
StringBuilder sb = new StringBuilder("invalid SPKAC (size):");
sb.append(aSN1Sequence.size());
throw new IllegalArgumentException(sb.toString());
}
this.sigAlg = AlgorithmIdentifier.getInstance(aSN1Sequence.getObjectAt(1));
this.sigBits = ((DERBitString) aSN1Sequence.getObjectAt(2)).getOctets();
ASN1Sequence aSN1Sequence2 = (ASN1Sequence) aSN1Sequence.getObjectAt(0);
if (aSN1Sequence2.size() != 2) {
StringBuilder sb2 = new StringBuilder("invalid PKAC (len): ");
sb2.append(aSN1Sequence2.size());
throw new IllegalArgumentException(sb2.toString());
}
this.challenge = ((DERIA5String) aSN1Sequence2.getObjectAt(1)).getString();
this.content = new DERBitString(aSN1Sequence2);
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(aSN1Sequence2.getObjectAt(0));
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(new DERBitString(subjectPublicKeyInfo).getBytes());
AlgorithmIdentifier algorithm = subjectPublicKeyInfo.getAlgorithm();
this.keyAlg = algorithm;
this.pubkey = KeyFactory.getInstance(algorithm.getAlgorithm().getId(), BouncyCastleProvider.PROVIDER_NAME).generatePublic(x509EncodedKeySpec);
} catch (Exception e) {
throw new IllegalArgumentException(e.toString());
}
}
public NetscapeCertRequest(String str, AlgorithmIdentifier algorithmIdentifier, PublicKey publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
this.challenge = str;
this.sigAlg = algorithmIdentifier;
this.pubkey = publicKey;
ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector();
aSN1EncodableVector.add(getKeySpec());
aSN1EncodableVector.add(new DERIA5String(str));
try {
this.content = new DERBitString(new DERSequence(aSN1EncodableVector));
} catch (IOException e) {
StringBuilder sb = new StringBuilder("exception encoding key: ");
sb.append(e.toString());
throw new InvalidKeySpecException(sb.toString());
}
}
}