Search in sources :

Example 1 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.

the class WeGooJavaKeyStore method engineLoad.

public void engineLoad(InputStream var1, char[] var2) throws IOException, NoSuchAlgorithmException, CertificateException {
    synchronized (this.entries) {
        MessageDigest var5 = null;
        CertificateFactory var6 = null;
        Hashtable var7 = null;
        ByteArrayInputStream var8 = null;
        Object var9 = null;
        if (var1 != null) {
            DataInputStream var4;
            if (var2 != null) {
                var5 = this.getPreKeyedHash(var2);
                var4 = new DataInputStream(new DigestInputStream(var1, var5));
            } else {
                var4 = new DataInputStream(var1);
            }
            int var10 = var4.readInt();
            int var11 = var4.readInt();
            if (var10 != -17957139 || var11 != 1 && var11 != 2) {
                throw new IOException("Invalid keystore format");
            } else {
                if (var11 == 1) {
                    var6 = CertificateFactory.getInstance("X509", new WeGooProvider());
                } else {
                    var7 = new Hashtable(3);
                }
                this.entries.clear();
                int var12 = var4.readInt();
                for (int var13 = 0; var13 < var12; ++var13) {
                    int var14 = var4.readInt();
                    String var15;
                    byte[] var23;
                    if (var14 != 1) {
                        if (var14 != 2) {
                            throw new IOException("Unrecognized keystore entry");
                        }
                        TrustedCertEntry var27 = new TrustedCertEntry();
                        var15 = var4.readUTF();
                        var27.date = new Date(var4.readLong());
                        if (var11 == 2) {
                            String var28 = var4.readUTF();
                            if (var7.containsKey(var28)) {
                                var6 = (CertificateFactory) var7.get(var28);
                            } else {
                                var6 = CertificateFactory.getInstance(var28, new WeGooProvider());
                                var7.put(var28, var6);
                            }
                        }
                        var23 = IOUtils.readExactlyNBytes(var4, var4.readInt());
                        var8 = new ByteArrayInputStream(var23);
                        var27.cert = var6.generateCertificate(var8);
                        var8.close();
                        this.entries.put(var15, var27);
                    } else {
                        KeyEntry var16 = new KeyEntry();
                        var15 = var4.readUTF();
                        var16.date = new Date(var4.readLong());
                        var16.protectedPrivKey = IOUtils.readExactlyNBytes(var4, var4.readInt());
                        int var17 = var4.readInt();
                        if (var17 > 0) {
                            ArrayList var18 = new ArrayList(var17 > 10 ? 10 : var17);
                            for (int var19 = 0; var19 < var17; ++var19) {
                                if (var11 == 2) {
                                    String var20 = var4.readUTF();
                                    if (var7.containsKey(var20)) {
                                        var6 = (CertificateFactory) var7.get(var20);
                                    } else {
                                        var6 = CertificateFactory.getInstance(var20, new WeGooProvider());
                                        var7.put(var20, var6);
                                    }
                                }
                                var23 = IOUtils.readExactlyNBytes(var4, var4.readInt());
                                var8 = new ByteArrayInputStream(var23);
                                var18.add(var6.generateCertificate(var8));
                                var8.close();
                            }
                            var16.chain = (Certificate[]) var18.toArray(new Certificate[var17]);
                        }
                        this.entries.put(var15, var16);
                    }
                }
                if (var2 != null) {
                    byte[] var24 = var5.digest();
                    byte[] var25 = IOUtils.readExactlyNBytes(var4, var24.length);
                    if (!MessageDigest.isEqual(var24, var25)) {
                        UnrecoverableKeyException var26 = new UnrecoverableKeyException("Password verification failed");
                        throw (IOException) (new IOException("Keystore was tampered with, or password was incorrect")).initCause(var26);
                    }
                }
            }
        }
    }
}
Also used : WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) CertificateFactory(java.security.cert.CertificateFactory) Certificate(java.security.cert.Certificate)

Example 2 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.

the class WeGooKeyProtector method recover.

public Key recover(EncryptedPrivateKeyInfo var1) throws UnrecoverableKeyException {
    AlgorithmId var7 = var1.getAlgorithm();
    if (!var7.getOID().toString().equals("1.3.6.1.4.1.42.2.17.1.1")) {
        throw new UnrecoverableKeyException("Unsupported key protection algorithm");
    } else {
        byte[] var8 = var1.getEncryptedData();
        byte[] var9 = new byte[20];
        System.arraycopy(var8, 0, var9, 0, 20);
        int var6 = var8.length - 20 - 20;
        int var4 = var6 / 20;
        if (var6 % 20 != 0) {
            ++var4;
        }
        byte[] var10 = new byte[var6];
        System.arraycopy(var8, 20, var10, 0, var6);
        byte[] var11 = new byte[var10.length];
        int var2 = 0;
        int var5 = 0;
        byte[] var3;
        for (var3 = var9; var2 < var4; var5 += 20) {
            this.md.update(this.passwdBytes);
            this.md.update(var3);
            var3 = this.md.digest();
            this.md.reset();
            if (var2 < var4 - 1) {
                System.arraycopy(var3, 0, var11, var5, var3.length);
            } else {
                System.arraycopy(var3, 0, var11, var5, var11.length - var5);
            }
            ++var2;
        }
        byte[] var12 = new byte[var10.length];
        for (var2 = 0; var2 < var12.length; ++var2) {
            var12[var2] = (byte) (var10[var2] ^ var11[var2]);
        }
        this.md.update(this.passwdBytes);
        Arrays.fill(this.passwdBytes, (byte) 0);
        this.passwdBytes = null;
        this.md.update(var12);
        var3 = this.md.digest();
        this.md.reset();
        for (var2 = 0; var2 < var3.length; ++var2) {
            if (var3[var2] != var8[20 + var6 + var2]) {
                throw new UnrecoverableKeyException("Cannot recover key");
            }
        }
        try {
            // return PKCS8Key.parseKey(new DerValue(var12));
            PrivateKeyInfo info = PrivateKeyInfo.getInstance(var12);
            if (info == null) {
                throw new UnrecoverableKeyException("Recover key can not null");
            }
            KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
            PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(var12);
            KeyFactory factory = KeyFactory.getInstance(algEnum.getAlg(), new WeGooProvider());
            return factory.generatePrivate(spec);
        } catch (Exception var14) {
            throw new UnrecoverableKeyException(var14.getMessage());
        }
    }
}
Also used : AlgorithmId(sun.security.x509.AlgorithmId) KeyPairAlgEnum(com.github.zhenwei.core.enums.KeyPairAlgEnum) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) EncryptedPrivateKeyInfo(sun.security.pkcs.EncryptedPrivateKeyInfo) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) IOException(java.io.IOException)

Example 3 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.

the class KeyBuilder method convertPrivateKey.

/**
 * @param [privateKey]
 * @return java.security.PrivateKey
 * @author zhangzhenwei
 * @description 私钥转换  byte[]  to  {@link PrivateKey}
 * @date 2022/2/11 22:34
 * @since 1.0
 */
public PrivateKey convertPrivateKey(byte[] privateKey) throws Exception {
    try {
        PrivateKeyInfo info = PrivateKeyInfo.getInstance(privateKey);
        if (info == null) {
            throw new WeGooKeyException(IExceptionEnum.params_err);
        }
        KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory factory = KeyFactory.getInstance(algEnum.getAlg(), new WeGooProvider());
        return factory.generatePrivate(spec);
    } catch (WeGooCryptoException e) {
        throw e;
    } catch (Exception e) {
        throw new WeGooKeyException(KeyExceptionMessageEnum.structure_private_key_err, e);
    }
}
Also used : WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) KeyPairAlgEnum(com.github.zhenwei.core.enums.KeyPairAlgEnum) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) BaseWeGooException(com.github.zhenwei.core.exception.BaseWeGooException)

Example 4 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.

the class P7Builder method enveloped.

public byte[] enveloped(X509Certificate certificate, byte[] data) throws BaseWeGooException, IOException {
    ASN1EncodableVector envelopedDataVector = new ASN1EncodableVector();
    ASN1EncodableVector recipientInfosVector = new ASN1EncodableVector();
    ASN1EncodableVector recipientInfoVector = new ASN1EncodableVector();
    ASN1EncodableVector encryptedContentInfoVector = new ASN1EncodableVector();
    ASN1Integer version = new ASN1Integer(0);
    envelopedDataVector.add(version);
    // 版本
    recipientInfoVector.add(version);
    IssuerAndSerialNumber issuerAndSerialNumber = new IssuerAndSerialNumber(new X500Name(certificate.getSubjectDN().getName()), certificate.getSerialNumber());
    // 序列号
    recipientInfoVector.add(issuerAndSerialNumber);
    // 匹配算法标识
    PublicKey publicKey = certificate.getPublicKey();
    String algorithm = publicKey.getAlgorithm();
    // todo 算法oid
    ASN1ObjectIdentifier identifier = GMObjectIdentifiers.sms4_cbc;
    boolean isEc = algorithm.equalsIgnoreCase("EC");
    ASN1ObjectIdentifier p7Oid = isEc ? new ASN1ObjectIdentifier("1.2.156.10197.6.1.4.2.3") : ContentInfo.envelopedData;
    // RSA/SM2加解密算法
    AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(identifier);
    recipientInfoVector.add(algorithmIdentifier);
    WeGooProvider provider = new WeGooProvider();
    KeyBuilder keyBuilder = new KeyBuilder(provider);
    Key key = keyBuilder.buildKey(KeyEnum.SM4_128);
    CipherBuilder cipherBuilder = new CipherBuilder(provider);
    // 加密
    byte[] encData = cipherBuilder.cipher(CipherAlgEnum.SM2, publicKey, key.getEncoded(), null, true);
    recipientInfoVector.add(new DEROctetString(encData));
    DERSequence recipientInfo = new DERSequence(recipientInfoVector);
    recipientInfosVector.add(recipientInfo);
    DERSet recipientInfos = new DERSet(recipientInfosVector);
    // recipientInfos
    envelopedDataVector.add(recipientInfos);
    // ContentType
    encryptedContentInfoVector.add(isEc ? new ASN1ObjectIdentifier("1.2.156.10197.6.1.4.2.1") : ContentInfo.data);
    // contentEncryptionAlgorithm
    CipherAlgEnum encAlg = CipherAlgEnum.SM4_ECB_PKCS7Padding;
    ASN1ObjectIdentifier sms4_ecb = GMObjectIdentifiers.sms4_ecb;
    if (encAlg.getModeEnum().isNeedIV()) {
        // todo
        encryptedContentInfoVector.add(new AlgorithmIdentifier(sms4_ecb, new DEROctetString(new byte[16])));
    } else {
        encryptedContentInfoVector.add(new AlgorithmIdentifier(sms4_ecb));
    }
    // todo iv
    byte[] symEncData = cipherBuilder.cipher(encAlg, key, data, null, true);
    // encryptedContent
    encryptedContentInfoVector.add(new DERTaggedObject(false, 0, new DEROctetString(symEncData)));
    DERSequence encryptedContentInfo = new DERSequence(encryptedContentInfoVector);
    // encryptedContentInfo
    envelopedDataVector.add(encryptedContentInfo);
    // envelopedData
    DERSequence envelopedData = new DERSequence(envelopedDataVector);
    return new ContentInfo(p7Oid, envelopedData).getEncoded();
}
Also used : PublicKey(java.security.PublicKey) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) X500Name(com.github.zhenwei.core.asn1.x500.X500Name) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) PublicKey(java.security.PublicKey) Key(java.security.Key)

Example 5 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.

the class CipherTest method encDecDataTest.

@Test
public void encDecDataTest() throws BaseWeGooException {
    WeGooProvider provider = new WeGooProvider();
    CipherBuilder builder = new CipherBuilder(provider);
    KeyBuilder keyBuilder = new KeyBuilder(provider);
    // SM2加解密
    KeyPair keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.SM2_256);
    byte[] data = "this is my encrypt data test".getBytes(StandardCharsets.UTF_8);
    IvParameterSpec spec = new IvParameterSpec(new byte[16]);
    byte[] encryptedData = builder.cipher(CipherAlgEnum.SM2, keyPair.getPublic(), data, spec, true);
    data = builder.cipher(CipherAlgEnum.SM2, keyPair.getPrivate(), encryptedData, spec, false);
    System.out.println("解密结果: " + new String(data));
    // RSA加解密
    keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.RSA_2048);
    encryptedData = builder.cipher(CipherAlgEnum.RSA, keyPair.getPublic(), data, spec, true);
    data = builder.cipher(CipherAlgEnum.RSA, keyPair.getPrivate(), encryptedData, spec, false);
    System.out.println("解密结果: " + new String(data));
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) KeyPair(java.security.KeyPair) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) IvParameterSpec(javax.crypto.spec.IvParameterSpec) CipherBuilder(com.github.zhenwei.sdk.builder.CipherBuilder) Test(org.junit.Test)

Aggregations

WeGooProvider (com.github.zhenwei.provider.jce.provider.WeGooProvider)18 KeyBuilder (com.github.zhenwei.sdk.builder.KeyBuilder)11 Test (org.junit.Test)10 KeyPair (java.security.KeyPair)6 WeGooCryptoException (com.github.zhenwei.core.exception.WeGooCryptoException)4 BCRSAPublicKey (com.github.zhenwei.provider.jcajce.provider.asymmetric.rsa.BCRSAPublicKey)3 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)2 KeyPairAlgEnum (com.github.zhenwei.core.enums.KeyPairAlgEnum)2 BaseWeGooException (com.github.zhenwei.core.exception.BaseWeGooException)2 WeGooKeyException (com.github.zhenwei.core.exception.WeGooKeyException)2 P10Builder (com.github.zhenwei.sdk.builder.P10Builder)2 SignBuilder (com.github.zhenwei.sdk.builder.SignBuilder)2 KeyStore (java.security.KeyStore)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 X500Name (com.github.zhenwei.core.asn1.x500.X500Name)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 CertBuilder (com.github.zhenwei.sdk.builder.CertBuilder)1 CipherBuilder (com.github.zhenwei.sdk.builder.CipherBuilder)1 HashBuilder (com.github.zhenwei.sdk.builder.HashBuilder)1