Search in sources :

Example 6 with WeGooCryptoException

use of com.github.zhenwei.core.exception.WeGooCryptoException in project LinLong-Java by zhenwei1108.

the class KeyBuilder method convertPublicKey.

/**
 * @param [publicKey]
 * @return java.security.PublicKey
 * @author zhangzhenwei
 * @description 公钥转换  byte[] to {@link PublicKey}
 * @date 2022/2/11 22:34
 * @since 1.0
 */
public PublicKey convertPublicKey(byte[] publicKey) throws WeGooKeyException {
    try {
        SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(publicKey);
        if (keyInfo == null) {
            throw new WeGooKeyException(IExceptionEnum.params_err);
        }
        X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
        KeyFactory factory = KeyFactory.getInstance(keyInfo.getAlgorithm().getAlgorithm().toString(), new WeGooProvider());
        return factory.generatePublic(spec);
    } catch (WeGooCryptoException e) {
        throw e;
    } catch (Exception e) {
        throw new WeGooKeyException(KeyExceptionMessageEnum.structure_public_key_err, e);
    }
}
Also used : WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) BaseWeGooException(com.github.zhenwei.core.exception.BaseWeGooException)

Example 7 with WeGooCryptoException

use of com.github.zhenwei.core.exception.WeGooCryptoException in project LinLong-Java by zhenwei1108.

the class KeyStoreBuilder method gen.

public byte[] gen(String type, PrivateKey privateKey, String alias, String passWd, Certificate[] certChain) throws WeGooCryptoException {
    try {
        KeyStore store = KeyStore.getInstance(type, new WeGooProvider());
        store.load(null);
        store.setKeyEntry(alias, privateKey, passWd.toCharArray(), certChain);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        store.store(outputStream, passWd.toCharArray());
        return outputStream.toByteArray();
    } catch (Exception e) {
        throw new WeGooCryptoException(CryptoExceptionMassageEnum.generate_jks_err, e);
    }
}
Also used : WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyStore(java.security.KeyStore) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException)

Example 8 with WeGooCryptoException

use of com.github.zhenwei.core.exception.WeGooCryptoException in project LinLong-Java by zhenwei1108.

the class KeyStoreBuilder method parse.

public KeyStore parse(String type, String passWd, byte[] jks) throws WeGooCryptoException {
    try {
        KeyStore store = KeyStore.getInstance(type, new WeGooProvider());
        ByteArrayInputStream stream = new ByteArrayInputStream(jks);
        store.load(stream, passWd.toCharArray());
        return store;
    } catch (Exception e) {
        throw new WeGooCryptoException(CryptoExceptionMassageEnum.parse_jks_err, e);
    }
}
Also used : WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) ByteArrayInputStream(java.io.ByteArrayInputStream) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) KeyStore(java.security.KeyStore) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException)

Example 9 with WeGooCryptoException

use of com.github.zhenwei.core.exception.WeGooCryptoException in project LinLong-Java by zhenwei1108.

the class P7bBuilder method buildP7b.

public static ArrayList<X509Certificate> buildP7b(byte[] data) throws WeGooCryptoException {
    ContentInfo contentInfo = ContentInfo.getInstance(data);
    // P7B 的 contentType应该是这个
    // ASN1ObjectIdentifier signedData = PKCSObjectIdentifiers.signedData;
    SignedData signedData = SignedData.getInstance(contentInfo.getContent());
    if (signedData == null)
        throw new WeGooCryptoException(CryptoExceptionMassageEnum.build_err);
    ASN1Set certs = signedData.getCertificates();
    ArrayList<X509Certificate> list = new ArrayList<>();
    for (ASN1Encodable cert : certs) {
        list.add(CertBuilder.getInstance(cert).getCert());
    }
    return list;
}
Also used : WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) SignedData(com.github.zhenwei.core.asn1.pkcs.SignedData) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) ContentInfo(com.github.zhenwei.core.asn1.pkcs.ContentInfo) ArrayList(java.util.ArrayList) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) X509Certificate(java.security.cert.X509Certificate)

Example 10 with WeGooCryptoException

use of com.github.zhenwei.core.exception.WeGooCryptoException in project LinLong-Java by zhenwei1108.

the class WeGooBuilder method forceAuth.

/**
 * @param [provider]
 * @return void
 * @author zhangzhenwei
 * @description 强制认证, 自定义provider需要使用
 * CN=JCE Code Signing CA, OU=Java Software Code Signing, O=Oracle Corporation
 * 签名.
 * @date 2022/2/6 21:40
 */
private void forceAuth(Provider provider) throws WeGooCryptoException {
    try {
        var verificationResults = new IdentityHashMap<>();
        verificationResults.put(provider, true);
        var field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("verificationResults");
        field.setAccessible(true);
        var modifiers = field.getClass().getDeclaredField("modifiers");
        modifiers.setAccessible(true);
        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(verificationResults, verificationResults);
    } catch (Exception e) {
        throw new WeGooCryptoException(e);
    }
}
Also used : WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) lombok.var(lombok.var) IdentityHashMap(java.util.IdentityHashMap) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException)

Aggregations

WeGooCryptoException (com.github.zhenwei.core.exception.WeGooCryptoException)11 BaseWeGooException (com.github.zhenwei.core.exception.BaseWeGooException)4 WeGooProvider (com.github.zhenwei.provider.jce.provider.WeGooProvider)4 WeGooKeyException (com.github.zhenwei.core.exception.WeGooKeyException)3 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)2 KeyPairAlgEnum (com.github.zhenwei.core.enums.KeyPairAlgEnum)2 BigInteger (java.math.BigInteger)2 KeyStore (java.security.KeyStore)2 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 DLSequence (com.github.zhenwei.core.asn1.DLSequence)1 ContentInfo (com.github.zhenwei.core.asn1.pkcs.ContentInfo)1 SignedData (com.github.zhenwei.core.asn1.pkcs.SignedData)1 X500Name (com.github.zhenwei.core.asn1.x500.X500Name)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 DigestInfo (com.github.zhenwei.core.asn1.x509.DigestInfo)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)1