Search in sources :

Example 96 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project LinLong-Java by zhenwei1108.

the class BCXDHPrivateKey method getEncoded.

public byte[] getEncoded() {
    try {
        ASN1Set attrSet = ASN1Set.getInstance(attributes);
        PrivateKeyInfo privInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(xdhPrivateKey, attrSet);
        if (hasPublicKey && !Properties.isOverrideSet("com.github.zhenwei.pkix.pkcs8.v1_info_only")) {
            return privInfo.getEncoded();
        } else {
            return new PrivateKeyInfo(privInfo.getPrivateKeyAlgorithm(), privInfo.parsePrivateKey(), attrSet).getEncoded();
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) IOException(java.io.IOException) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)

Example 97 with ASN1Set

use of com.unboundid.asn1.ASN1Set 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 98 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project LinLong-Java by zhenwei1108.

the class CMSSignedData method replaceCertificatesAndCRLs.

/**
 * Replace the certificate and CRL information associated with this CMSSignedData object with the
 * new one passed in.
 *
 * @param signedData   the signed data object to be used as a base.
 * @param certificates the new certificates to be used.
 * @param attrCerts    the new attribute certificates to be used.
 * @param revocations  the new CRLs to be used - a collection of X509CRLHolder objects,
 *                     OtherRevocationInfoFormat, or both.
 * @return a new signed data object.
 * @throws CMSException if there is an error processing the CertStore
 */
public static CMSSignedData replaceCertificatesAndCRLs(CMSSignedData signedData, Store certificates, Store attrCerts, Store revocations) throws CMSException {
    // 
    // copy
    // 
    CMSSignedData cms = new CMSSignedData(signedData);
    // 
    // replace the certs and revocations in the SignedData object
    // 
    ASN1Set certSet = null;
    ASN1Set crlSet = null;
    if (certificates != null || attrCerts != null) {
        List certs = new ArrayList();
        if (certificates != null) {
            certs.addAll(CMSUtils.getCertificatesFromStore(certificates));
        }
        if (attrCerts != null) {
            certs.addAll(CMSUtils.getAttributeCertificatesFromStore(attrCerts));
        }
        ASN1Set set = CMSUtils.createBerSetFromList(certs);
        if (set.size() != 0) {
            certSet = set;
        }
    }
    if (revocations != null) {
        ASN1Set set = CMSUtils.createBerSetFromList(CMSUtils.getCRLsFromStore(revocations));
        if (set.size() != 0) {
            crlSet = set;
        }
    }
    // 
    // replace the CMS structure.
    // 
    cms.signedData = new SignedData(signedData.signedData.getDigestAlgorithms(), signedData.signedData.getEncapContentInfo(), certSet, crlSet, signedData.signedData.getSignerInfos());
    // 
    // replace the contentInfo with the new one
    // 
    cms.contentInfo = new ContentInfo(cms.contentInfo.getContentType(), cms.signedData);
    return cms;
}
Also used : ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) SignedData(com.github.zhenwei.pkix.util.asn1.cms.SignedData) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 99 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project LinLong-Java by zhenwei1108.

the class CMSSignedData method replaceSigners.

/**
 * Replace the SignerInformation store associated with this CMSSignedData object with the new one
 * passed in. You would probably only want to do this if you wanted to change the unsigned
 * attributes associated with a signer, or perhaps delete one.
 *
 * @param signedData             the signed data object to be used as a base.
 * @param signerInformationStore the new signer information store to use.
 * @return a new signed data object.
 */
public static CMSSignedData replaceSigners(CMSSignedData signedData, SignerInformationStore signerInformationStore) {
    // 
    // copy
    // 
    CMSSignedData cms = new CMSSignedData(signedData);
    // 
    // replace the store
    // 
    cms.signerInfoStore = signerInformationStore;
    // 
    // replace the signers in the SignedData object
    // 
    Set<AlgorithmIdentifier> digestAlgs = new HashSet<AlgorithmIdentifier>();
    ASN1EncodableVector vec = new ASN1EncodableVector();
    Iterator it = signerInformationStore.getSigners().iterator();
    while (it.hasNext()) {
        SignerInformation signer = (SignerInformation) it.next();
        CMSUtils.addDigestAlgs(digestAlgs, signer, dgstAlgFinder);
        vec.add(signer.toASN1Structure());
    }
    ASN1Set digests = CMSUtils.convertToBERSet(digestAlgs);
    ASN1Set signers = new DLSet(vec);
    ASN1Sequence sD = (ASN1Sequence) signedData.signedData.toASN1Primitive();
    vec = new ASN1EncodableVector();
    // 
    // signers are the last item in the sequence.
    // 
    // version
    vec.add(sD.getObjectAt(0));
    vec.add(digests);
    for (int i = 2; i != sD.size() - 1; i++) {
        vec.add(sD.getObjectAt(i));
    }
    vec.add(signers);
    cms.signedData = SignedData.getInstance(new BERSequence(vec));
    // 
    // replace the contentInfo with the new one
    // 
    cms.contentInfo = new ContentInfo(cms.contentInfo.getContentType(), cms.signedData);
    return cms;
}
Also used : BERSequence(com.github.zhenwei.core.asn1.BERSequence) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) Iterator(java.util.Iterator) DLSet(com.github.zhenwei.core.asn1.DLSet) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) HashSet(java.util.HashSet)

Example 100 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project LinLong-Java by zhenwei1108.

the class CMSSignedDataParser method replaceCertificatesAndCRLs.

/**
 * Replace the certificate and CRL information associated with this CMSSignedData object with the
 * new one passed in.
 * <p>
 * The output stream is returned unclosed.
 * </p>
 *
 * @param original  the signed data stream to be used as a base.
 * @param certs     new certificates to be used, if any.
 * @param crls      new CRLs to be used, if any.
 * @param attrCerts new attribute certificates to be used, if any.
 * @param out       the stream to write the new signed data object to.
 * @return out.
 * @throws CMSException if there is an error processing the CertStore
 */
public static OutputStream replaceCertificatesAndCRLs(InputStream original, Store certs, Store crls, Store attrCerts, OutputStream out) throws CMSException, IOException {
    ASN1StreamParser in = new ASN1StreamParser(original);
    ContentInfoParser contentInfo = new ContentInfoParser((ASN1SequenceParser) in.readObject());
    SignedDataParser signedData = SignedDataParser.getInstance(contentInfo.getContent(BERTags.SEQUENCE));
    BERSequenceGenerator sGen = new BERSequenceGenerator(out);
    sGen.addObject(CMSObjectIdentifiers.signedData);
    BERSequenceGenerator sigGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
    // version number
    sigGen.addObject(signedData.getVersion());
    // digests
    sigGen.getRawOutputStream().write(signedData.getDigestAlgorithms().toASN1Primitive().getEncoded());
    // encap content info
    ContentInfoParser encapContentInfo = signedData.getEncapContentInfo();
    BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());
    eiGen.addObject(encapContentInfo.getContentType());
    pipeEncapsulatedOctetString(encapContentInfo, eiGen.getRawOutputStream());
    eiGen.close();
    // 
    // skip existing certs and CRLs
    // 
    getASN1Set(signedData.getCertificates());
    getASN1Set(signedData.getCrls());
    // 
    if (certs != null || attrCerts != null) {
        List certificates = new ArrayList();
        if (certs != null) {
            certificates.addAll(CMSUtils.getCertificatesFromStore(certs));
        }
        if (attrCerts != null) {
            certificates.addAll(CMSUtils.getAttributeCertificatesFromStore(attrCerts));
        }
        ASN1Set asn1Certs = CMSUtils.createBerSetFromList(certificates);
        if (asn1Certs.size() > 0) {
            sigGen.getRawOutputStream().write(new DERTaggedObject(false, 0, asn1Certs).getEncoded());
        }
    }
    if (crls != null) {
        ASN1Set asn1Crls = CMSUtils.createBerSetFromList(CMSUtils.getCRLsFromStore(crls));
        if (asn1Crls.size() > 0) {
            sigGen.getRawOutputStream().write(new DERTaggedObject(false, 1, asn1Crls).getEncoded());
        }
    }
    sigGen.getRawOutputStream().write(signedData.getSignerInfos().toASN1Primitive().getEncoded());
    sigGen.close();
    sGen.close();
    return out;
}
Also used : ContentInfoParser(com.github.zhenwei.pkix.util.asn1.cms.ContentInfoParser) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) SignedDataParser(com.github.zhenwei.pkix.util.asn1.cms.SignedDataParser) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) BERSequenceGenerator(com.github.zhenwei.core.asn1.BERSequenceGenerator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ASN1StreamParser(com.github.zhenwei.core.asn1.ASN1StreamParser)

Aggregations

ASN1Set (org.bouncycastle.asn1.ASN1Set)67 ArrayList (java.util.ArrayList)51 ASN1Set (com.unboundid.asn1.ASN1Set)33 IOException (java.io.IOException)32 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)30 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)30 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)26 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)22 ASN1Element (com.unboundid.asn1.ASN1Element)21 NotNull (com.unboundid.util.NotNull)21 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)19 List (java.util.List)17 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)17 DEROctetString (org.bouncycastle.asn1.DEROctetString)16 Enumeration (java.util.Enumeration)14 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)14 OutputStream (java.io.OutputStream)12 Test (org.testng.annotations.Test)12 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)11 X509Certificate (java.security.cert.X509Certificate)11