/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) ansi radix(10) lradix(10)
// Source File Name: CAUtility.java
package ccit.security.bssp;
import ccit.security.bssp.base.CertParseBase;
import ccit.security.bssp.base.DigestBase;
import ccit.security.bssp.base.EcryptBase;
import ccit.security.bssp.base.SignBase;
import ccit.security.bssp.bean.CERT_INFO;
import ccit.security.bssp.bean.KeyPairInfo;
import ccit.security.bssp.bean.RSAKeyPair;
import ccit.security.bssp.ex.CCITSecurityException;
import ccit.security.bssp.ex.CrypException;
import ccit.security.bssp.sm2.Cryption;
import ccit.security.bssp.sm2.Randoms;
import ccit.security.bssp.sm2.SM2PrivateKeyDer;
import ccit.security.bssp.sm2.SM2PublicKeyDer;
import ccit.security.bssp.sm2.SM3Hash;
import ccit.security.bssp.sm2.SMS4;
import ccit.security.bssp.sm2.Signature;
import ccit.security.bssp.util.Constants;
import ccit.security.bssp.util.MAC;
import ccit.security.bssp.util.MiscTools;
import ccit.security.bssp.util.ParseCertExtUtil;
import ccit.security.bssp.util.ParseCertUtil;
import ccit.security.bssp.util.Resource;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.security.InvalidParameterException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Enumeration;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.x509.X509CertificateStructure;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.util.encoders.Base64;
public class CAUtility {
public CAUtility() {
}
public static CAUtility getInstance() {
return new CAUtility();
}
public static RSAKeyPair generateRSAKeyPair(int modulusLen)
throws CrypException {
RSAKeyPair keypair = new RSAKeyPair();
KeyPairGenerator keygen = null;
try {
keygen = KeyPairGenerator.getInstance("RSA");
keygen.initialize(modulusLen);
KeyPair keys = keygen.generateKeyPair();
RSAPublicKey pubkey = (RSAPublicKey) keys.getPublic();
RSAPrivateKey prikey = (RSAPrivateKey) keys.getPrivate();
byte pubkeybyte[] = pubkey.getEncoded();
byte prikeybyte[] = prikey.getEncoded();
ByteArrayInputStream bIn = new ByteArrayInputStream(pubkeybyte);
ASN1InputStream ais = new ASN1InputStream(bIn);
org.bouncycastle.asn1.DERObject dobj = ais.readObject();
DERSequence ass = (DERSequence) dobj;
bIn.close();
bIn = null;
DERBitString pubOct = DERBitString.getInstance(ass.getObjectAt(1));
bIn = new ByteArrayInputStream(prikeybyte);
ASN1InputStream ais1 = new ASN1InputStream(bIn);
org.bouncycastle.asn1.DERObject dobj1 = ais1.readObject();
DERSequence ass1 = (DERSequence) dobj1;
bIn.close();
bIn = null;
ASN1OctetString priOct = DEROctetString.getInstance(ass1
.getObjectAt(2));
keypair.setPrikeyDERString(priOct.getOctets());
keypair.setPubkeyDERString(pubOct.getBytes());
keypair.setPubkey(pubkey);
} catch (InvalidParameterException ex) {
throw new CrypException(-2147483642, (new StringBuilder(
"Don't Support Your Modulus Length!")).append(
ex.getMessage()).toString());
} catch (Exception exc) {
throw new CrypException(-2147483647, (new StringBuilder(
"Operation Fail!")).append(exc.getMessage()).toString());
}
return keypair;
}
public static byte[] digest(byte src[], int algorithm) throws CrypException {
if (src == null) {
throw new CrypException(-2147450877,
"Digest failed! Data to be digested cann't be null!");
} else {
byte digestdata[] = DigestBase.digest(src, algorithm);
return digestdata;
}
}
public static int verify(int type, byte publicKey[], byte indata[],
byte sd[]) throws CrypException {
if (publicKey == null)
throw new CrypException(-2147483641,
"Verify failed! Key cann't be null!");
if (indata == null)
throw new CrypException(-2147483641,
"Verify failed! The data to be signed cann't be null!");
if (sd == null)
throw new CrypException(-2147483641,
"Verify failed! The signed data cann't be null!");
byte signaturepem[] = MiscTools.checkPEM(publicKey);
if (signaturepem != null)
publicKey = Base64.decode(signaturepem);
byte signaturepem1[] = MiscTools.checkPEM(sd);
if (signaturepem1 != null)
sd = Base64.decode(signaturepem1);
String Algorithms = "";
if (type == 257)
Algorithms = "MD2WITHRSA";
else if (type == 258)
Algorithms = "MD5WITHRSA";
else if (type == 259)
Algorithms = "SHA1WITHRSA";
else if (type == 261)
Algorithms = "SHA256WITHRSA";
else
throw new CrypException(-2147483643,
"Don't Support Your Algorithms");
int nFlag = SignBase.verify(Algorithms, publicKey, indata, sd);
return nFlag;
}
public static int verifyWithCert(int type, byte cert[], byte indata[],
byte sd[]) throws CrypException {
if (cert == null)
throw new CrypException(-2147483641,
"Verify failed! Key cann't be null!");
if (indata == null)
throw new CrypException(-2147483641,
"Verify failed! The data to be signed cann't be null!");
if (sd == null)
throw new CrypException(-2147483641,
"Verify failed! The signed data cann't be null!");
byte signaturepem[] = MiscTools.checkPEM(cert);
if (signaturepem != null)
cert = Base64.decode(signaturepem);
byte signaturepem1[] = MiscTools.checkPEM(sd);
if (signaturepem1 != null)
sd = Base64.decode(signaturepem1);
String Algorithms = "";
if (type == 257)
Algorithms = "MD2WITHRSA";
else if (type == 258)
Algorithms = "MD5WITHRSA";
else if (type == 259)
Algorithms = "SHA1WITHRSA";
else if (type == 261)
Algorithms = "SHA256WITHRSA";
else
throw new CrypException(-2147483643,
"Don't Support Your Algorithms");
int nFlag = SignBase.verifyWithCert(Algorithms, cert, indata, sd);
return nFlag;
}
public static byte[] cryption(int type, byte key[], boolean bEncryption,
byte inData[]) throws CrypException {
if (key == null)
throw new CrypException(-2147483641,
"Operation failed! Key cann't be null!");
if (inData == null)
throw new CrypException(-2147483641,
"Operation failed! The data to be encrypted or decrypted cann't be null!");
byte signaturepem[];
if (!bEncryption) {
signaturepem = MiscTools.checkPEM(inData);
if (signaturepem != null)
inData = Base64.decode(signaturepem);
}
signaturepem = MiscTools.checkPEM(key);
if (signaturepem != null)
key = Base64.decode(signaturepem);
String alg = "";
if (type == 1)
alg = "RSA/ECB/PKCS1Padding";
else if (type == 2)
alg = "RSA/ECB/NoPadding";
else
throw new CrypException(-2147483643,
"Don't Support Your Algorithms");
byte outdata[] = EcryptBase.crypto(alg, key, bEncryption, inData);
return outdata;
}
public static byte[] symCrypto(int type, byte key[], boolean bEncrypto,
byte inData[], byte parameter[]) throws CrypException {
if (key == null)
throw new CrypException(-2147483641,
"Operation failed! Key cann't be null!");
if (inData == null)
throw new CrypException(-2147483641,
"Operation failed! The data to be encrypted or decrypted cann't be null!");
if (!bEncrypto) {
byte signaturepem[] = MiscTools.checkPEM(inData);
if (signaturepem != null)
inData =