Search in sources :

Example 1 with DLSet

use of com.github.zhenwei.core.asn1.DLSet in project jruby-openssl by jruby.

the class X509Name method to_der.

@JRubyMethod
public RubyString to_der(final ThreadContext context) {
    final Ruby runtime = context.runtime;
    final DLSequence seq;
    if (oids.size() > 0) {
        ASN1EncodableVector vec = new ASN1EncodableVector();
        ASN1EncodableVector sVec = new ASN1EncodableVector();
        ASN1ObjectIdentifier lastOid = null;
        for (int i = 0; i != oids.size(); i++) {
            final ASN1ObjectIdentifier oid = oids.get(i);
            ASN1EncodableVector v = new ASN1EncodableVector();
            v.add(oid);
            // TODO DO NOT USE DL types !
            // final String value = values.get(i);
            // final int type = RubyNumeric.fix2int(types.get(i));
            // v.add( convert(oid, value, type) );
            v.add(values.get(i));
            if (lastOid == null) {
                sVec.add(new DLSequence(v));
            } else {
                vec.add(new DLSet(sVec));
                sVec = new ASN1EncodableVector();
                sVec.add(new DLSequence(v));
            }
            lastOid = oid;
        }
        vec.add(new DLSet(sVec));
        seq = new DLSequence(vec);
    } else {
        seq = new DLSequence();
    }
    try {
        return StringHelper.newString(runtime, seq.getEncoded(ASN1Encoding.DER));
    } catch (IOException e) {
        throw newNameError(runtime, e);
    }
}
Also used : DLSequence(org.bouncycastle.asn1.DLSequence) DLSet(org.bouncycastle.asn1.DLSet) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) IOException(java.io.IOException) Ruby(org.jruby.Ruby) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 2 with DLSet

use of com.github.zhenwei.core.asn1.DLSet in project jmulticard by ctt-gob-es.

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.convertToDlSet(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(org.bouncycastle.asn1.BERSequence) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1Set(org.bouncycastle.asn1.ASN1Set) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) Iterator(java.util.Iterator) DLSet(org.bouncycastle.asn1.DLSet) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) HashSet(java.util.HashSet)

Example 3 with DLSet

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

the class CMSAuthEnvelopedDataGenerator method doGenerate.

private CMSAuthEnvelopedData doGenerate(CMSTypedData content, OutputAEADEncryptor contentEncryptor) throws CMSException {
    ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
    AlgorithmIdentifier encAlgId;
    ASN1OctetString encContent;
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1Set authenticatedAttrSet = null;
    try {
        OutputStream cOut = contentEncryptor.getOutputStream(bOut);
        content.write(cOut);
        if (authAttrsGenerator != null) {
            AttributeTable attrTable = authAttrsGenerator.getAttributes(Collections.EMPTY_MAP);
            authenticatedAttrSet = new DERSet(attrTable.toASN1EncodableVector());
            contentEncryptor.getAADStream().write(authenticatedAttrSet.getEncoded(ASN1Encoding.DER));
        }
        cOut.close();
    } catch (IOException e) {
        throw new CMSException("unable to process authenticated content: " + e.getMessage(), e);
    }
    byte[] encryptedContent = bOut.toByteArray();
    byte[] mac = contentEncryptor.getMAC();
    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 (unauthAttrsGenerator != null) {
        AttributeTable attrTable = unauthAttrsGenerator.getAttributes(Collections.EMPTY_MAP);
        unprotectedAttrSet = new DLSet(attrTable.toASN1EncodableVector());
    }
    ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.authEnvelopedData, new AuthEnvelopedData(originatorInfo, new DERSet(recipientInfos), eci, authenticatedAttrSet, new DEROctetString(mac), unprotectedAttrSet));
    return new CMSAuthEnvelopedData(contentInfo);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AuthEnvelopedData(com.github.zhenwei.pkix.util.asn1.cms.AuthEnvelopedData) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DERSet(com.github.zhenwei.core.asn1.DERSet) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo) 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) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo)

Example 4 with DLSet

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

Aggregations

Iterator (java.util.Iterator)3 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)2 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)2 DLSet (com.github.zhenwei.core.asn1.DLSet)2 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 ContentInfo (com.github.zhenwei.pkix.util.asn1.cms.ContentInfo)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)2 DLSet (org.bouncycastle.asn1.DLSet)2 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)1 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)1 BEROctetString (com.github.zhenwei.core.asn1.BEROctetString)1 BERSequence (com.github.zhenwei.core.asn1.BERSequence)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 DERSet (com.github.zhenwei.core.asn1.DERSet)1 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)1 AttributeTable (com.github.zhenwei.pkix.util.asn1.cms.AttributeTable)1 AuthEnvelopedData (com.github.zhenwei.pkix.util.asn1.cms.AuthEnvelopedData)1 EncryptedContentInfo (com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo)1