Search in sources :

Example 71 with ContentInfo

use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project LinLong-Java by zhenwei1108.

the class CMSEncryptedDataGenerator method doGenerate.

private CMSEncryptedData doGenerate(CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException {
    AlgorithmIdentifier encAlgId;
    ASN1OctetString encContent;
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    try {
        OutputStream cOut = contentEncryptor.getOutputStream(bOut);
        content.write(cOut);
        cOut.close();
    } catch (IOException e) {
        throw new CMSException("");
    }
    byte[] encryptedContent = bOut.toByteArray();
    encAlgId = contentEncryptor.getAlgorithmIdentifier();
    encContent = new BEROctetString(encryptedContent);
    EncryptedContentInfo eci = new EncryptedContentInfo(content.getContentType(), encAlgId, encContent);
    ASN1Set unprotectedAttrSet = null;
    if (unprotectedAttributeGenerator != null) {
        AttributeTable attrTable = unprotectedAttributeGenerator.getAttributes(Collections.EMPTY_MAP);
        unprotectedAttrSet = new BERSet(attrTable.toASN1EncodableVector());
    }
    ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.encryptedData, new EncryptedData(eci, unprotectedAttrSet));
    return new CMSEncryptedData(contentInfo);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) BERSet(com.github.zhenwei.core.asn1.BERSet) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) EncryptedData(com.github.zhenwei.pkix.util.asn1.cms.EncryptedData) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo)

Example 72 with ContentInfo

use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project LinLong-Java by zhenwei1108.

the class CMSEnvelopedDataGenerator method doGenerate.

private CMSEnvelopedData doGenerate(CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException {
    ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
    AlgorithmIdentifier encAlgId;
    ASN1OctetString encContent;
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    try {
        OutputStream cOut = contentEncryptor.getOutputStream(bOut);
        content.write(cOut);
        cOut.close();
        if (contentEncryptor instanceof OutputAEADEncryptor) {
            byte[] mac = ((OutputAEADEncryptor) contentEncryptor).getMAC();
            bOut.write(mac, 0, mac.length);
        }
    } catch (IOException e) {
        throw new CMSException("");
    }
    byte[] encryptedContent = bOut.toByteArray();
    encAlgId = contentEncryptor.getAlgorithmIdentifier();
    encContent = new BEROctetString(encryptedContent);
    GenericKey encKey = contentEncryptor.getKey();
    for (Iterator it = recipientInfoGenerators.iterator(); it.hasNext(); ) {
        RecipientInfoGenerator recipient = (RecipientInfoGenerator) it.next();
        recipientInfos.add(recipient.generate(encKey));
    }
    EncryptedContentInfo eci = new EncryptedContentInfo(content.getContentType(), encAlgId, encContent);
    ASN1Set unprotectedAttrSet = null;
    if (unprotectedAttributeGenerator != null) {
        AttributeTable attrTable = unprotectedAttributeGenerator.getAttributes(Collections.EMPTY_MAP);
        unprotectedAttrSet = new BERSet(attrTable.toASN1EncodableVector());
    }
    ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.envelopedData, new EnvelopedData(originatorInfo, new DERSet(recipientInfos), eci, unprotectedAttrSet));
    return new CMSEnvelopedData(contentInfo);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) BERSet(com.github.zhenwei.core.asn1.BERSet) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OutputAEADEncryptor(com.github.zhenwei.pkix.operator.OutputAEADEncryptor) DERSet(com.github.zhenwei.core.asn1.DERSet) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) Iterator(java.util.Iterator) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) EnvelopedData(com.github.zhenwei.pkix.util.asn1.cms.EnvelopedData) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo)

Example 73 with ContentInfo

use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project gdmatrix by gdmatrix.

the class P7MDocument method getSignatures.

public List<P7MSignature> getSignatures() throws Exception {
    ArrayList<P7MSignature> signatures = new ArrayList();
    // CertStore certStore = cms.getCertificatesAndCRLs("Collection", "BC");
    Store certStore = cms.getCertificates();
    SignerInformationStore siStore = cms.getSignerInfos();
    Collection signers = siStore.getSigners();
    for (Object elem : signers) {
        SignerInformation signer = (SignerInformation) elem;
        P7MSignature signature = new P7MSignature();
        signatures.add(signature);
        Collection certCollection = certStore.getMatches(signer.getSID());
        // Collection certCollection = certStore.getCertificates(certSelector);
        X509CertificateHolder certificateHolder = (X509CertificateHolder) certCollection.iterator().next();
        X509Certificate certificate = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder);
        signature.setCertificate(certificate);
        signature.loadProperties();
        signature.setSignature(Base64.getMimeEncoder().encodeToString(signer.getSignature()).toUpperCase());
        // **** signed attributes ****
        AttributeTable table = signer.getSignedAttributes();
        Hashtable attributes = table.toHashtable();
        // signingTime
        Attribute attrib = (Attribute) attributes.get(new ASN1ObjectIdentifier("1.2.840.113549.1.9.5"));
        if (attrib != null) {
            ASN1UTCTime time = (ASN1UTCTime) attrib.getAttrValues().getObjectAt(0);
            String timeString = time.getAdjustedTime();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss'GMT+'00:00");
            signature.setSigningDate(df.parse(timeString));
        }
        // filename
        DEROctetString octet;
        attrib = (Attribute) attributes.get(new ASN1ObjectIdentifier("1.3.6.1.4.1.311.88.2.1"));
        if (attrib != null) {
            octet = (DEROctetString) attrib.getAttrValues().getObjectAt(0);
            if (octet != null) {
                signature.setFilename(new String(octet.getOctets(), "UTF-16LE"));
            }
        }
        // decretNumber
        attrib = (Attribute) attributes.get(new ASN1ObjectIdentifier("1.3.6.1.4.1.311.88.2.2"));
        if (attrib != null) {
            octet = (DEROctetString) attrib.getAttrValues().getObjectAt(0);
            if (octet != null) {
                signature.setDecretNumber(new String(octet.getOctets(), "UTF-16LE"));
            }
        }
        // **** unsigned attributes ****
        table = signer.getUnsignedAttributes();
        if (table != null) {
            attributes = table.toHashtable();
            // timeStampToken
            attrib = (Attribute) attributes.get(new ASN1ObjectIdentifier("1.2.840.113549.1.9.16.2.14"));
            if (attrib != null) {
                ASN1Sequence seq = (ASN1Sequence) attrib.getAttrValues().getObjectAt(0);
                ContentInfo timeStampToken = ContentInfo.getInstance(seq);
                SignedData sd = SignedData.getInstance(timeStampToken.getContent());
                ASN1Set certificates = sd.getCertificates();
                ASN1Primitive derCert = certificates.getObjectAt(0).toASN1Primitive();
                byte[] certBytes = derCert.getEncoded();
                CertificateFactory certFactory = CertificateFactory.getInstance("X509");
                X509Certificate tsCertificate = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(certBytes));
                signature.setTimeStampCertificate(tsCertificate);
                ASN1Encodable content = sd.getEncapContentInfo().getContent();
                // TSTInfo tstInfo = new TSTInfo((ASN1Sequence)
                // new ASN1InputStream(((ASN1OctetString)content).getOctets()).readObject());
                TSTInfo tstInfo = TSTInfo.getInstance(((ASN1OctetString) content).getOctets());
                signature.setTimeStampDate(tstInfo.getGenTime().getDate());
            }
        }
        // signature validation
        signature.setValid(signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(signature.getCertificate())));
    }
    Collections.sort(signatures);
    return signatures;
}
Also used : Attribute(org.bouncycastle.asn1.cms.Attribute) ArrayList(java.util.ArrayList) AttributeTable(org.bouncycastle.asn1.cms.AttributeTable) Store(org.bouncycastle.util.Store) SignerInformationStore(org.bouncycastle.cms.SignerInformationStore) ASN1UTCTime(org.bouncycastle.asn1.ASN1UTCTime) SignerInformation(org.bouncycastle.cms.SignerInformation) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) CertificateFactory(java.security.cert.CertificateFactory) DEROctetString(org.bouncycastle.asn1.DEROctetString) SignerInformationStore(org.bouncycastle.cms.SignerInformationStore) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) SignedData(org.bouncycastle.asn1.cms.SignedData) CMSSignedData(org.bouncycastle.cms.CMSSignedData) Hashtable(java.util.Hashtable) JcaSimpleSignerInfoVerifierBuilder(org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder) X509Certificate(java.security.cert.X509Certificate) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) TSTInfo(org.bouncycastle.asn1.tsp.TSTInfo) ASN1Set(org.bouncycastle.asn1.ASN1Set) ByteArrayInputStream(java.io.ByteArrayInputStream) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) Collection(java.util.Collection) SimpleDateFormat(java.text.SimpleDateFormat) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 74 with ContentInfo

use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project gdmatrix by gdmatrix.

the class P7MUtils method addTimeStamp.

public static CMSSignedData addTimeStamp(String serviceURI, CMSSignedData signedData) throws Exception {
    CMSSignedData newSignedData;
    SignerInformationStore sigStore = signedData.getSignerInfos();
    ArrayList siList = new ArrayList();
    for (Object o : sigStore.getSigners()) {
        // CertStore certStore =
        // signedData.getCertificatesAndCRLs("Collection", "BC");
        Store store = signedData.getCertificates();
        SignerInformation si = (SignerInformation) o;
        SignerId sigId = si.getSID();
        JcaX509CertSelectorConverter converter = new JcaX509CertSelectorConverter();
        CertSelector certSelector = converter.getCertSelector(sigId);
        Collection certCollection = store.getMatches((Selector) certSelector);
        // Collection certCollection = certStore.getCertificates(sigId);
        X509Certificate certificate = (X509Certificate) certCollection.iterator().next();
        System.out.println(certificate.getSubjectDN().getName());
        // get signature
        byte[] signature = si.getSignature();
        // signed attributes
        System.out.println("SignedAttributes:");
        AttributeTable signedAttributes = si.getSignedAttributes();
        printAttributeTable(signedAttributes);
        // unsigned attributes
        System.out.println("UnsignedAttributes:");
        AttributeTable unsignedAttributes = si.getUnsignedAttributes();
        printAttributeTable(unsignedAttributes);
        ASN1ObjectIdentifier tsId = new ASN1ObjectIdentifier("1.2.840.113549.1.9.16.2.14");
        Attribute att = unsignedAttributes == null ? null : unsignedAttributes.get(tsId);
        if (att == null) {
            System.out.println("creating timeStamp...");
            ASN1EncodableVector tsVector = new ASN1EncodableVector();
            ContentInfo timeStampToken = createTimeStamp(serviceURI, signature);
            tsVector.add(timeStampToken);
            DERSet attributeValues = new DERSet(tsVector);
            att = new Attribute(tsId, attributeValues);
            Hashtable attrMap = new Hashtable();
            attrMap.put(tsId, att);
            AttributeTable table = new AttributeTable(attrMap);
            SignerInformation newSi = SignerInformation.replaceUnsignedAttributes(si, table);
            siList.add(newSi);
        } else {
            System.out.println("timeStamp present");
        }
    }
    if (// replace signers
    !siList.isEmpty()) {
        newSignedData = CMSSignedData.replaceSigners(signedData, new SignerInformationStore(siList));
        newSignedData = new CMSSignedData(newSignedData.getEncoded());
    } else
        newSignedData = signedData;
    return newSignedData;
}
Also used : JcaX509CertSelectorConverter(org.bouncycastle.cms.jcajce.JcaX509CertSelectorConverter) Attribute(org.bouncycastle.asn1.cms.Attribute) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeTable(org.bouncycastle.asn1.cms.AttributeTable) SignerInformationStore(org.bouncycastle.cms.SignerInformationStore) Store(org.bouncycastle.util.Store) SignerInformation(org.bouncycastle.cms.SignerInformation) CertSelector(java.security.cert.CertSelector) CMSSignedData(org.bouncycastle.cms.CMSSignedData) DERSet(org.bouncycastle.asn1.DERSet) X509Certificate(java.security.cert.X509Certificate) SignerInformationStore(org.bouncycastle.cms.SignerInformationStore) SignerId(org.bouncycastle.cms.SignerId) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) Collection(java.util.Collection) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 75 with ContentInfo

use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project gdmatrix by gdmatrix.

the class P7MUtils method sendData.

private static TimeStampResp sendData(InputStream dataToBeSent, String serviceURI) throws Exception {
    URL url = new URL(serviceURI);
    URLConnection conn = url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    // post request data
    OutputStream os = conn.getOutputStream();
    byte[] buffer = new byte[4096];
    int numRead = dataToBeSent.read(buffer);
    while (numRead > 0) {
        os.write(buffer, 0, numRead);
        numRead = dataToBeSent.read(buffer);
    }
    os.flush();
    // read response
    InputStream response = conn.getInputStream();
    ASN1InputStream asn1Is = new ASN1InputStream(response);
    Enumeration e = ((ASN1Sequence) asn1Is.readObject()).getObjects();
    PKIStatusInfo pkiStatusInfo = PKIStatusInfo.getInstance(e.nextElement());
    ContentInfo timeStampToken = null;
    if (e.hasMoreElements()) {
        timeStampToken = ContentInfo.getInstance(e.nextElement());
    }
    TimeStampResp tspResp = new TimeStampResp(pkiStatusInfo, timeStampToken);
    return tspResp;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) Enumeration(java.util.Enumeration) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) PKIStatusInfo(org.bouncycastle.asn1.cmp.PKIStatusInfo) TimeStampResp(org.bouncycastle.asn1.tsp.TimeStampResp) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

ContentInfo (org.bouncycastle.asn1.cms.ContentInfo)60 IOException (java.io.IOException)28 CMSSignedData (org.bouncycastle.cms.CMSSignedData)22 ContentInfo (com.github.zhenwei.pkix.util.asn1.cms.ContentInfo)18 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)15 OutputStream (java.io.OutputStream)12 X509Certificate (java.security.cert.X509Certificate)12 ArrayList (java.util.ArrayList)12 SignedData (org.bouncycastle.asn1.cms.SignedData)12 Iterator (java.util.Iterator)11 ASN1Set (org.bouncycastle.asn1.ASN1Set)11 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)10 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)10 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)9 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)9 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)9 DERSet (org.bouncycastle.asn1.DERSet)9