Search in sources :

Example 41 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class PKCS12KeyStoreSpi method createSafeBag.

private SafeBag createSafeBag(String certId, Certificate cert) throws CertificateEncodingException {
    CertBag cBag = new CertBag(x509Certificate, new DEROctetString(cert.getEncoded()));
    ASN1EncodableVector fName = new ASN1EncodableVector();
    boolean cAttrSet = false;
    if (cert instanceof PKCS12BagAttributeCarrier) {
        PKCS12BagAttributeCarrier bagAttrs = (PKCS12BagAttributeCarrier) cert;
        // 
        // make sure we are using the local alias on store
        // 
        ASN1BMPString nm = (ASN1BMPString) bagAttrs.getBagAttribute(pkcs_9_at_friendlyName);
        if (nm == null || !nm.getString().equals(certId)) {
            if (certId != null) {
                bagAttrs.setBagAttribute(pkcs_9_at_friendlyName, new DERBMPString(certId));
            }
        }
        Enumeration e = bagAttrs.getBagAttributeKeys();
        while (e.hasMoreElements()) {
            ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) e.nextElement();
            // If we find one, we'll prune it out.
            if (oid.equals(PKCSObjectIdentifiers.pkcs_9_at_localKeyId)) {
                continue;
            }
            ASN1EncodableVector fSeq = new ASN1EncodableVector();
            fSeq.add(oid);
            fSeq.add(new DERSet(bagAttrs.getBagAttribute(oid)));
            fName.add(new DERSequence(fSeq));
            cAttrSet = true;
        }
    }
    if (!cAttrSet) {
        ASN1EncodableVector fSeq = new ASN1EncodableVector();
        fSeq.add(pkcs_9_at_friendlyName);
        fSeq.add(new DERSet(new DERBMPString(certId)));
        fName.add(new DERSequence(fSeq));
    }
    return new SafeBag(certBag, cBag.toASN1Primitive(), new DERSet(fName));
}
Also used : CertBag(com.github.zhenwei.core.asn1.pkcs.CertBag) DERBMPString(com.github.zhenwei.core.asn1.DERBMPString) Enumeration(java.util.Enumeration) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ASN1BMPString(com.github.zhenwei.core.asn1.ASN1BMPString) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) SafeBag(com.github.zhenwei.core.asn1.pkcs.SafeBag) DERSet(com.github.zhenwei.core.asn1.DERSet) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) PKCS12BagAttributeCarrier(com.github.zhenwei.provider.jce.interfaces.PKCS12BagAttributeCarrier) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 42 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class PKCS12KeyStoreSpi method doStore.

private void doStore(OutputStream stream, char[] password, boolean useDEREncoding) throws IOException {
    if (keys.size() == 0) {
        if (password == null) {
            Enumeration cs = certs.keys();
            ASN1EncodableVector certSeq = new ASN1EncodableVector();
            while (cs.hasMoreElements()) {
                try {
                    String certId = (String) cs.nextElement();
                    Certificate cert = (Certificate) certs.get(certId);
                    SafeBag sBag = createSafeBag(certId, cert);
                    certSeq.add(sBag);
                } catch (CertificateEncodingException e) {
                    throw new IOException("Error encoding certificate: " + e.toString());
                }
            }
            if (useDEREncoding) {
                ContentInfo bagInfo = new ContentInfo(PKCSObjectIdentifiers.data, new DEROctetString(new DERSequence(certSeq).getEncoded()));
                Pfx pfx = new Pfx(new ContentInfo(PKCSObjectIdentifiers.data, new DEROctetString(new DERSequence(bagInfo).getEncoded())), null);
                pfx.encodeTo(stream, ASN1Encoding.DER);
            } else {
                ContentInfo bagInfo = new ContentInfo(PKCSObjectIdentifiers.data, new BEROctetString(new BERSequence(certSeq).getEncoded()));
                Pfx pfx = new Pfx(new ContentInfo(PKCSObjectIdentifiers.data, new BEROctetString(new BERSequence(bagInfo).getEncoded())), null);
                pfx.encodeTo(stream, ASN1Encoding.BER);
            }
            return;
        }
    } else {
        if (password == null) {
            throw new NullPointerException("no password supplied for PKCS#12 KeyStore");
        }
    }
    // 
    // handle the key
    // 
    ASN1EncodableVector keyS = new ASN1EncodableVector();
    Enumeration ks = keys.keys();
    while (ks.hasMoreElements()) {
        byte[] kSalt = new byte[SALT_SIZE];
        random.nextBytes(kSalt);
        String name = (String) ks.nextElement();
        PrivateKey privKey = (PrivateKey) keys.get(name);
        PKCS12PBEParams kParams = new PKCS12PBEParams(kSalt, MIN_ITERATIONS);
        byte[] kBytes = wrapKey(keyAlgorithm.getId(), privKey, kParams, password);
        AlgorithmIdentifier kAlgId = new AlgorithmIdentifier(keyAlgorithm, kParams.toASN1Primitive());
        com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo kInfo = new com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo(kAlgId, kBytes);
        boolean attrSet = false;
        ASN1EncodableVector kName = new ASN1EncodableVector();
        if (privKey instanceof PKCS12BagAttributeCarrier) {
            PKCS12BagAttributeCarrier bagAttrs = (PKCS12BagAttributeCarrier) privKey;
            // 
            // make sure we are using the local alias on store
            // 
            ASN1BMPString nm = (ASN1BMPString) bagAttrs.getBagAttribute(pkcs_9_at_friendlyName);
            if (nm == null || !nm.getString().equals(name)) {
                bagAttrs.setBagAttribute(pkcs_9_at_friendlyName, new DERBMPString(name));
            }
            // 
            if (bagAttrs.getBagAttribute(pkcs_9_at_localKeyId) == null) {
                Certificate ct = engineGetCertificate(name);
                bagAttrs.setBagAttribute(pkcs_9_at_localKeyId, createSubjectKeyId(ct.getPublicKey()));
            }
            Enumeration e = bagAttrs.getBagAttributeKeys();
            while (e.hasMoreElements()) {
                ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) e.nextElement();
                ASN1EncodableVector kSeq = new ASN1EncodableVector();
                kSeq.add(oid);
                kSeq.add(new DERSet(bagAttrs.getBagAttribute(oid)));
                attrSet = true;
                kName.add(new DERSequence(kSeq));
            }
        }
        if (!attrSet) {
            // 
            // set a default friendly name (from the key id) and local id
            // 
            ASN1EncodableVector kSeq = new ASN1EncodableVector();
            Certificate ct = engineGetCertificate(name);
            kSeq.add(pkcs_9_at_localKeyId);
            kSeq.add(new DERSet(createSubjectKeyId(ct.getPublicKey())));
            kName.add(new DERSequence(kSeq));
            kSeq = new ASN1EncodableVector();
            kSeq.add(pkcs_9_at_friendlyName);
            kSeq.add(new DERSet(new DERBMPString(name)));
            kName.add(new DERSequence(kSeq));
        }
        SafeBag kBag = new SafeBag(pkcs8ShroudedKeyBag, kInfo.toASN1Primitive(), new DERSet(kName));
        keyS.add(kBag);
    }
    byte[] keySEncoded = new DERSequence(keyS).getEncoded(ASN1Encoding.DER);
    BEROctetString keyString = new BEROctetString(keySEncoded);
    // 
    // certificate processing
    // 
    byte[] cSalt = new byte[SALT_SIZE];
    random.nextBytes(cSalt);
    ASN1EncodableVector certSeq = new ASN1EncodableVector();
    PKCS12PBEParams cParams = new PKCS12PBEParams(cSalt, MIN_ITERATIONS);
    AlgorithmIdentifier cAlgId = new AlgorithmIdentifier(certAlgorithm, cParams.toASN1Primitive());
    Hashtable doneCerts = new Hashtable();
    Enumeration cs = keys.keys();
    while (cs.hasMoreElements()) {
        try {
            String name = (String) cs.nextElement();
            Certificate cert = engineGetCertificate(name);
            boolean cAttrSet = false;
            CertBag cBag = new CertBag(x509Certificate, new DEROctetString(cert.getEncoded()));
            ASN1EncodableVector fName = new ASN1EncodableVector();
            if (cert instanceof PKCS12BagAttributeCarrier) {
                PKCS12BagAttributeCarrier bagAttrs = (PKCS12BagAttributeCarrier) cert;
                // 
                // make sure we are using the local alias on store
                // 
                ASN1BMPString nm = (ASN1BMPString) bagAttrs.getBagAttribute(pkcs_9_at_friendlyName);
                if (nm == null || !nm.getString().equals(name)) {
                    bagAttrs.setBagAttribute(pkcs_9_at_friendlyName, new DERBMPString(name));
                }
                // 
                if (bagAttrs.getBagAttribute(pkcs_9_at_localKeyId) == null) {
                    bagAttrs.setBagAttribute(pkcs_9_at_localKeyId, createSubjectKeyId(cert.getPublicKey()));
                }
                Enumeration e = bagAttrs.getBagAttributeKeys();
                while (e.hasMoreElements()) {
                    ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) e.nextElement();
                    ASN1EncodableVector fSeq = new ASN1EncodableVector();
                    fSeq.add(oid);
                    fSeq.add(new DERSet(bagAttrs.getBagAttribute(oid)));
                    fName.add(new DERSequence(fSeq));
                    cAttrSet = true;
                }
            }
            if (!cAttrSet) {
                ASN1EncodableVector fSeq = new ASN1EncodableVector();
                fSeq.add(pkcs_9_at_localKeyId);
                fSeq.add(new DERSet(createSubjectKeyId(cert.getPublicKey())));
                fName.add(new DERSequence(fSeq));
                fSeq = new ASN1EncodableVector();
                fSeq.add(pkcs_9_at_friendlyName);
                fSeq.add(new DERSet(new DERBMPString(name)));
                fName.add(new DERSequence(fSeq));
            }
            SafeBag sBag = new SafeBag(certBag, cBag.toASN1Primitive(), new DERSet(fName));
            certSeq.add(sBag);
            doneCerts.put(cert, cert);
        } catch (CertificateEncodingException e) {
            throw new IOException("Error encoding certificate: " + e.toString());
        }
    }
    cs = certs.keys();
    while (cs.hasMoreElements()) {
        try {
            String certId = (String) cs.nextElement();
            Certificate cert = (Certificate) certs.get(certId);
            if (keys.get(certId) != null) {
                continue;
            }
            SafeBag sBag = createSafeBag(certId, cert);
            certSeq.add(sBag);
            doneCerts.put(cert, cert);
        } catch (CertificateEncodingException e) {
            throw new IOException("Error encoding certificate: " + e.toString());
        }
    }
    Set usedSet = getUsedCertificateSet();
    cs = chainCerts.keys();
    while (cs.hasMoreElements()) {
        try {
            CertId certId = (CertId) cs.nextElement();
            Certificate cert = (Certificate) chainCerts.get(certId);
            if (!usedSet.contains(cert)) {
                continue;
            }
            if (doneCerts.get(cert) != null) {
                continue;
            }
            CertBag cBag = new CertBag(x509Certificate, new DEROctetString(cert.getEncoded()));
            ASN1EncodableVector fName = new ASN1EncodableVector();
            if (cert instanceof PKCS12BagAttributeCarrier) {
                PKCS12BagAttributeCarrier bagAttrs = (PKCS12BagAttributeCarrier) cert;
                Enumeration e = bagAttrs.getBagAttributeKeys();
                while (e.hasMoreElements()) {
                    ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) e.nextElement();
                    // If we find one, we'll prune it out.
                    if (oid.equals(PKCSObjectIdentifiers.pkcs_9_at_localKeyId)) {
                        continue;
                    }
                    ASN1EncodableVector fSeq = new ASN1EncodableVector();
                    fSeq.add(oid);
                    fSeq.add(new DERSet(bagAttrs.getBagAttribute(oid)));
                    fName.add(new DERSequence(fSeq));
                }
            }
            SafeBag sBag = new SafeBag(certBag, cBag.toASN1Primitive(), new DERSet(fName));
            certSeq.add(sBag);
        } catch (CertificateEncodingException e) {
            throw new IOException("Error encoding certificate: " + e.toString());
        }
    }
    byte[] certSeqEncoded = new DERSequence(certSeq).getEncoded(ASN1Encoding.DER);
    byte[] certBytes = cryptData(true, cAlgId, password, false, certSeqEncoded);
    EncryptedData cInfo = new EncryptedData(data, cAlgId, new BEROctetString(certBytes));
    ContentInfo[] info = new ContentInfo[] { new ContentInfo(data, keyString), new ContentInfo(encryptedData, cInfo.toASN1Primitive()) };
    AuthenticatedSafe auth = new AuthenticatedSafe(info);
    byte[] pkg = auth.getEncoded(useDEREncoding ? ASN1Encoding.DER : ASN1Encoding.BER);
    ContentInfo mainInfo = new ContentInfo(data, new BEROctetString(pkg));
    // 
    // create the mac
    // 
    byte[] mSalt = new byte[saltLength];
    random.nextBytes(mSalt);
    byte[] data = ((ASN1OctetString) mainInfo.getContent()).getOctets();
    MacData mData;
    try {
        byte[] res = calculatePbeMac(macAlgorithm.getAlgorithm(), mSalt, itCount, password, false, data);
        DigestInfo dInfo = new DigestInfo(macAlgorithm, res);
        mData = new MacData(dInfo, mSalt, itCount);
    } catch (Exception e) {
        throw new IOException("error constructing MAC: " + e.toString());
    }
    // 
    // output the Pfx
    // 
    Pfx pfx = new Pfx(mainInfo, mData);
    pfx.encodeTo(stream, useDEREncoding ? ASN1Encoding.DER : ASN1Encoding.BER);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) PrivateKey(java.security.PrivateKey) Set(java.util.Set) DERSet(com.github.zhenwei.core.asn1.DERSet) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) HashSet(java.util.HashSet) ASN1BMPString(com.github.zhenwei.core.asn1.ASN1BMPString) AuthenticatedSafe(com.github.zhenwei.core.asn1.pkcs.AuthenticatedSafe) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) DERBMPString(com.github.zhenwei.core.asn1.DERBMPString) ASN1BMPString(com.github.zhenwei.core.asn1.ASN1BMPString) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) DERSet(com.github.zhenwei.core.asn1.DERSet) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) PKCS12BagAttributeCarrier(com.github.zhenwei.provider.jce.interfaces.PKCS12BagAttributeCarrier) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) DERSequence(com.github.zhenwei.core.asn1.DERSequence) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ContentInfo(com.github.zhenwei.core.asn1.pkcs.ContentInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) EncryptedData(com.github.zhenwei.core.asn1.pkcs.EncryptedData) MacData(com.github.zhenwei.core.asn1.pkcs.MacData) Enumeration(java.util.Enumeration) Pfx(com.github.zhenwei.core.asn1.pkcs.Pfx) DERBMPString(com.github.zhenwei.core.asn1.DERBMPString) Hashtable(java.util.Hashtable) BERSequence(com.github.zhenwei.core.asn1.BERSequence) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) SafeBag(com.github.zhenwei.core.asn1.pkcs.SafeBag) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CertificateEncodingException(java.security.cert.CertificateEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) EOFException(java.io.EOFException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException) CertBag(com.github.zhenwei.core.asn1.pkcs.CertBag) PKCS12PBEParams(com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams) DigestInfo(com.github.zhenwei.core.asn1.x509.DigestInfo) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 43 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class CscaMasterList method toASN1Primitive.

public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector seq = new ASN1EncodableVector(2);
    seq.add(version);
    seq.add(new DERSet(certList));
    return new DERSequence(seq);
}
Also used : DERSequence(com.github.zhenwei.core.asn1.DERSequence) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) DERSet(com.github.zhenwei.core.asn1.DERSet)

Example 44 with DERSet

use of com.github.zhenwei.core.asn1.DERSet in project LinLong-Java by zhenwei1108.

the class PKIXCertPath method getEncoded.

/**
 * Returns the encoded form of this certification path, using the specified encoding.
 *
 * @param encoding the name of the encoding to use
 * @return the encoded bytes
 * @throws CertificateEncodingException if an encoding error occurs or the encoding requested is
 *                                      not supported
 */
public byte[] getEncoded(String encoding) throws CertificateEncodingException {
    if (encoding.equalsIgnoreCase("PkiPath")) {
        ASN1EncodableVector v = new ASN1EncodableVector();
        ListIterator iter = certificates.listIterator(certificates.size());
        while (iter.hasPrevious()) {
            v.add(toASN1Object((X509Certificate) iter.previous()));
        }
        return toDEREncoded(new DERSequence(v));
    } else if (encoding.equalsIgnoreCase("PKCS7")) {
        ContentInfo encInfo = new ContentInfo(PKCSObjectIdentifiers.data, null);
        ASN1EncodableVector v = new ASN1EncodableVector();
        for (int i = 0; i != certificates.size(); i++) {
            v.add(toASN1Object((X509Certificate) certificates.get(i)));
        }
        SignedData sd = new SignedData(new ASN1Integer(1), new DERSet(), encInfo, new DERSet(v), null, new DERSet());
        return toDEREncoded(new ContentInfo(PKCSObjectIdentifiers.signedData, sd));
    } else if (encoding.equalsIgnoreCase("PEM")) {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut));
        try {
            for (int i = 0; i != certificates.size(); i++) {
                pWrt.writeObject(new PemObject("CERTIFICATE", ((X509Certificate) certificates.get(i)).getEncoded()));
            }
            pWrt.close();
        } catch (Exception e) {
            throw new CertificateEncodingException("can't encode certificate for PEM encoded path");
        }
        return bOut.toByteArray();
    } else {
        throw new CertificateEncodingException("unsupported encoding: " + encoding);
    }
}
Also used : SignedData(com.github.zhenwei.core.asn1.pkcs.SignedData) PemWriter(com.github.zhenwei.core.util.io.pem.PemWriter) CertificateEncodingException(java.security.cert.CertificateEncodingException) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ListIterator(java.util.ListIterator) DERSet(com.github.zhenwei.core.asn1.DERSet) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException) PemObject(com.github.zhenwei.core.util.io.pem.PemObject) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ContentInfo(com.github.zhenwei.core.asn1.pkcs.ContentInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) OutputStreamWriter(java.io.OutputStreamWriter)

Example 45 with DERSet

use of com.github.zhenwei.core.asn1.DERSet 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)

Aggregations

DERSet (org.bouncycastle.asn1.DERSet)59 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)34 IOException (java.io.IOException)29 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)27 DERSequence (org.bouncycastle.asn1.DERSequence)27 DEROctetString (org.bouncycastle.asn1.DEROctetString)22 DERSet (com.github.zhenwei.core.asn1.DERSet)21 Attribute (org.bouncycastle.asn1.cms.Attribute)21 X509Certificate (java.security.cert.X509Certificate)19 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)15 Iterator (java.util.Iterator)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 ArrayList (java.util.ArrayList)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)11 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)11 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)11 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)9 OutputStream (java.io.OutputStream)9