Search in sources :

Example 31 with DERSet

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

the class SignerInfoWithPkey method addAttribute.

/**
 * c: static add_attribute
 */
private ASN1Set addAttribute(ASN1Set base, int atrType, ASN1Encodable value) {
    ASN1EncodableVector vector = new ASN1EncodableVector();
    if (base == null)
        base = new DERSet();
    Attribute attr;
    for (Enumeration e = base.getObjects(); e.hasMoreElements(); ) {
        attr = Attribute.getInstance(e.nextElement());
        if (ASN1Registry.oid2nid(attr.getAttrType()) != atrType) {
            vector.add(attr);
        }
    }
    ASN1ObjectIdentifier ident = ASN1Registry.nid2obj(atrType);
    attr = new Attribute(ident, new DERSet(value));
    vector.add(attr);
    return new DERSet(vector);
}
Also used : Enumeration(java.util.Enumeration) Attribute(org.bouncycastle.asn1.pkcs.Attribute) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) DERSet(org.bouncycastle.asn1.DERSet) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 32 with DERSet

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

the class ASN1Dump method _dumpAsString.

/**
 * dump a DER object as a formatted string with indentation
 *
 * @param obj the ASN1Primitive to be dumped out.
 */
static void _dumpAsString(String indent, boolean verbose, ASN1Primitive obj, StringBuffer buf) {
    String nl = Strings.lineSeparator();
    if (obj instanceof ASN1Null) {
        buf.append(indent);
        buf.append("NULL");
        buf.append(nl);
    } else if (obj instanceof ASN1Sequence) {
        buf.append(indent);
        if (obj instanceof BERSequence) {
            buf.append("BER Sequence");
        } else if (obj instanceof DERSequence) {
            buf.append("DER Sequence");
        } else {
            buf.append("Sequence");
        }
        buf.append(nl);
        ASN1Sequence sequence = (ASN1Sequence) obj;
        String elementsIndent = indent + TAB;
        for (int i = 0, count = sequence.size(); i < count; ++i) {
            _dumpAsString(elementsIndent, verbose, sequence.getObjectAt(i).toASN1Primitive(), buf);
        }
    } else if (obj instanceof ASN1Set) {
        buf.append(indent);
        if (obj instanceof BERSet) {
            buf.append("BER Set");
        } else if (obj instanceof DERSet) {
            buf.append("DER Set");
        } else {
            buf.append("Set");
        }
        buf.append(nl);
        ASN1Set set = (ASN1Set) obj;
        String elementsIndent = indent + TAB;
        for (int i = 0, count = set.size(); i < count; ++i) {
            _dumpAsString(elementsIndent, verbose, set.getObjectAt(i).toASN1Primitive(), buf);
        }
    } else if (obj instanceof ASN1ApplicationSpecific) {
        _dumpAsString(indent, verbose, ((ASN1ApplicationSpecific) obj).getTaggedObject(), buf);
    } else if (obj instanceof ASN1TaggedObject) {
        buf.append(indent);
        if (obj instanceof BERTaggedObject) {
            buf.append("BER Tagged ");
        } else if (obj instanceof DERTaggedObject) {
            buf.append("DER Tagged ");
        } else {
            buf.append("Tagged ");
        }
        ASN1TaggedObject o = (ASN1TaggedObject) obj;
        buf.append(ASN1Util.getTagText(o));
        if (!o.isExplicit()) {
            buf.append(" IMPLICIT ");
        }
        buf.append(nl);
        String baseIndent = indent + TAB;
        _dumpAsString(baseIndent, verbose, o.getBaseObject().toASN1Primitive(), buf);
    } else if (obj instanceof ASN1OctetString) {
        ASN1OctetString oct = (ASN1OctetString) obj;
        if (obj instanceof BEROctetString) {
            buf.append(indent + "BER Constructed Octet String" + "[" + oct.getOctets().length + "] ");
        } else {
            buf.append(indent + "DER Octet String" + "[" + oct.getOctets().length + "] ");
        }
        if (verbose) {
            buf.append(dumpBinaryDataAsString(indent, oct.getOctets()));
        } else {
            buf.append(nl);
        }
    } else if (obj instanceof ASN1ObjectIdentifier) {
        buf.append(indent + "ObjectIdentifier(" + ((ASN1ObjectIdentifier) obj).getId() + ")" + nl);
    } else if (obj instanceof ASN1RelativeOID) {
        buf.append(indent + "RelativeOID(" + ((ASN1RelativeOID) obj).getId() + ")" + nl);
    } else if (obj instanceof ASN1Boolean) {
        buf.append(indent + "Boolean(" + ((ASN1Boolean) obj).isTrue() + ")" + nl);
    } else if (obj instanceof ASN1Integer) {
        buf.append(indent + "Integer(" + ((ASN1Integer) obj).getValue() + ")" + nl);
    } else if (obj instanceof ASN1BitString) {
        ASN1BitString bitString = (ASN1BitString) obj;
        byte[] bytes = bitString.getBytes();
        int padBits = bitString.getPadBits();
        if (bitString instanceof DERBitString) {
            buf.append(indent + "DER Bit String" + "[" + bytes.length + ", " + padBits + "] ");
        } else if (bitString instanceof DLBitString) {
            buf.append(indent + "DL Bit String" + "[" + bytes.length + ", " + padBits + "] ");
        } else {
            buf.append(indent + "BER Bit String" + "[" + bytes.length + ", " + padBits + "] ");
        }
        if (verbose) {
            buf.append(dumpBinaryDataAsString(indent, bytes));
        } else {
            buf.append(nl);
        }
    } else if (obj instanceof ASN1IA5String) {
        buf.append(indent + "IA5String(" + ((ASN1IA5String) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1UTF8String) {
        buf.append(indent + "UTF8String(" + ((ASN1UTF8String) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1NumericString) {
        buf.append(indent + "NumericString(" + ((ASN1NumericString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1PrintableString) {
        buf.append(indent + "PrintableString(" + ((ASN1PrintableString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1VisibleString) {
        buf.append(indent + "VisibleString(" + ((ASN1VisibleString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1BMPString) {
        buf.append(indent + "BMPString(" + ((ASN1BMPString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1T61String) {
        buf.append(indent + "T61String(" + ((ASN1T61String) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1GraphicString) {
        buf.append(indent + "GraphicString(" + ((ASN1GraphicString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1VideotexString) {
        buf.append(indent + "VideotexString(" + ((ASN1VideotexString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1UTCTime) {
        buf.append(indent + "UTCTime(" + ((ASN1UTCTime) obj).getTime() + ") " + nl);
    } else if (obj instanceof ASN1GeneralizedTime) {
        buf.append(indent + "GeneralizedTime(" + ((ASN1GeneralizedTime) obj).getTime() + ") " + nl);
    } else if (obj instanceof ASN1Enumerated) {
        ASN1Enumerated en = (ASN1Enumerated) obj;
        buf.append(indent + "DER Enumerated(" + en.getValue() + ")" + nl);
    } else if (obj instanceof ASN1ObjectDescriptor) {
        ASN1ObjectDescriptor od = (ASN1ObjectDescriptor) obj;
        buf.append(indent + "ObjectDescriptor(" + od.getBaseGraphicString().getString() + ") " + nl);
    } else if (obj instanceof ASN1External) {
        ASN1External ext = (ASN1External) obj;
        buf.append(indent + "External " + nl);
        String tab = indent + TAB;
        if (ext.getDirectReference() != null) {
            buf.append(tab + "Direct Reference: " + ext.getDirectReference().getId() + nl);
        }
        if (ext.getIndirectReference() != null) {
            buf.append(tab + "Indirect Reference: " + ext.getIndirectReference().toString() + nl);
        }
        if (ext.getDataValueDescriptor() != null) {
            _dumpAsString(tab, verbose, ext.getDataValueDescriptor(), buf);
        }
        buf.append(tab + "Encoding: " + ext.getEncoding() + nl);
        _dumpAsString(tab, verbose, ext.getExternalContent(), buf);
    } else {
        buf.append(indent + obj.toString() + nl);
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) ASN1TaggedObject(com.github.zhenwei.core.asn1.ASN1TaggedObject) ASN1BMPString(com.github.zhenwei.core.asn1.ASN1BMPString) ASN1UTCTime(com.github.zhenwei.core.asn1.ASN1UTCTime) ASN1GeneralizedTime(com.github.zhenwei.core.asn1.ASN1GeneralizedTime) ASN1IA5String(com.github.zhenwei.core.asn1.ASN1IA5String) ASN1T61String(com.github.zhenwei.core.asn1.ASN1T61String) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ASN1UTF8String(com.github.zhenwei.core.asn1.ASN1UTF8String) ASN1VisibleString(com.github.zhenwei.core.asn1.ASN1VisibleString) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) ASN1NumericString(com.github.zhenwei.core.asn1.ASN1NumericString) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ASN1BMPString(com.github.zhenwei.core.asn1.ASN1BMPString) ASN1VideotexString(com.github.zhenwei.core.asn1.ASN1VideotexString) DERBitString(com.github.zhenwei.core.asn1.DERBitString) ASN1PrintableString(com.github.zhenwei.core.asn1.ASN1PrintableString) DLBitString(com.github.zhenwei.core.asn1.DLBitString) ASN1GraphicString(com.github.zhenwei.core.asn1.ASN1GraphicString) DLBitString(com.github.zhenwei.core.asn1.DLBitString) DERSet(com.github.zhenwei.core.asn1.DERSet) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ASN1External(com.github.zhenwei.core.asn1.ASN1External) ASN1T61String(com.github.zhenwei.core.asn1.ASN1T61String) DERSequence(com.github.zhenwei.core.asn1.DERSequence) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ASN1Enumerated(com.github.zhenwei.core.asn1.ASN1Enumerated) BERTaggedObject(com.github.zhenwei.core.asn1.BERTaggedObject) ASN1ObjectDescriptor(com.github.zhenwei.core.asn1.ASN1ObjectDescriptor) BERSet(com.github.zhenwei.core.asn1.BERSet) ASN1NumericString(com.github.zhenwei.core.asn1.ASN1NumericString) ASN1UTF8String(com.github.zhenwei.core.asn1.ASN1UTF8String) ASN1GraphicString(com.github.zhenwei.core.asn1.ASN1GraphicString) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) BERSequence(com.github.zhenwei.core.asn1.BERSequence) ASN1ApplicationSpecific(com.github.zhenwei.core.asn1.ASN1ApplicationSpecific) DERBitString(com.github.zhenwei.core.asn1.DERBitString) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ASN1RelativeOID(com.github.zhenwei.core.asn1.ASN1RelativeOID) ASN1VideotexString(com.github.zhenwei.core.asn1.ASN1VideotexString) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) ASN1VisibleString(com.github.zhenwei.core.asn1.ASN1VisibleString) ASN1IA5String(com.github.zhenwei.core.asn1.ASN1IA5String) ASN1PrintableString(com.github.zhenwei.core.asn1.ASN1PrintableString) ASN1Boolean(com.github.zhenwei.core.asn1.ASN1Boolean) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ASN1Null(com.github.zhenwei.core.asn1.ASN1Null)

Example 33 with DERSet

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

the class SignerInformation method addCounterSigners.

/**
 * Return a signer information object with passed in SignerInformationStore representing counter
 * signatures attached as an unsigned attribute.
 *
 * @param signerInformation the signerInfo to be used as the basis.
 * @param counterSigners    signer info objects carrying counter signature.
 * @return a copy of the original SignerInformationObject with the changed attributes.
 */
public static SignerInformation addCounterSigners(SignerInformation signerInformation, SignerInformationStore counterSigners) {
    // TODO Perform checks from RFC 3852 11.4
    SignerInfo sInfo = signerInformation.info;
    AttributeTable unsignedAttr = signerInformation.getUnsignedAttributes();
    ASN1EncodableVector v;
    if (unsignedAttr != null) {
        v = unsignedAttr.toASN1EncodableVector();
    } else {
        v = new ASN1EncodableVector();
    }
    ASN1EncodableVector sigs = new ASN1EncodableVector();
    for (Iterator it = counterSigners.getSigners().iterator(); it.hasNext(); ) {
        sigs.add(((SignerInformation) it.next()).toASN1Structure());
    }
    v.add(new Attribute(CMSAttributes.counterSignature, new DERSet(sigs)));
    return new SignerInformation(new SignerInfo(sInfo.getSID(), sInfo.getDigestAlgorithm(), sInfo.getAuthenticatedAttributes(), sInfo.getDigestEncryptionAlgorithm(), sInfo.getEncryptedDigest(), new DERSet(v)), signerInformation.contentType, signerInformation.content, null);
}
Also used : SignerInfo(com.github.zhenwei.pkix.util.asn1.cms.SignerInfo) Attribute(com.github.zhenwei.pkix.util.asn1.cms.Attribute) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) Iterator(java.util.Iterator) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) DERSet(com.github.zhenwei.core.asn1.DERSet)

Example 34 with DERSet

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

the class SignerInformation method replaceUnsignedAttributes.

/**
 * Return a signer information object with the passed in unsigned attributes replacing the ones
 * that are current associated with the object passed in.
 *
 * @param signerInformation  the signerInfo to be used as the basis.
 * @param unsignedAttributes the unsigned attributes to add.
 * @return a copy of the original SignerInformationObject with the changed attributes.
 */
public static SignerInformation replaceUnsignedAttributes(SignerInformation signerInformation, AttributeTable unsignedAttributes) {
    SignerInfo sInfo = signerInformation.info;
    ASN1Set unsignedAttr = null;
    if (unsignedAttributes != null) {
        unsignedAttr = new DERSet(unsignedAttributes.toASN1EncodableVector());
    }
    return new SignerInformation(new SignerInfo(sInfo.getSID(), sInfo.getDigestAlgorithm(), sInfo.getAuthenticatedAttributes(), sInfo.getDigestEncryptionAlgorithm(), sInfo.getEncryptedDigest(), unsignedAttr), signerInformation.contentType, signerInformation.content, null);
}
Also used : SignerInfo(com.github.zhenwei.pkix.util.asn1.cms.SignerInfo) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) DERSet(com.github.zhenwei.core.asn1.DERSet)

Example 35 with DERSet

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

the class CMSSignedDataGenerator method generate.

/**
 * Generate a CMS Signed Data object which can be carrying a detached CMS signature, or have
 * encapsulated data, depending on the value of the encapsulated parameter.
 *
 * @param content     the content to be signed.
 * @param encapsulate true if the content should be encapsulated in the signature, false
 *                    otherwise.
 */
public CMSSignedData generate(// FIXME Avoid accessing more than once to support CMSProcessableInputStream
CMSTypedData content, boolean encapsulate) throws CMSException {
    if (!signerInfs.isEmpty()) {
        throw new IllegalStateException("this method can only be used with SignerInfoGenerator");
    }
    // TODO
    // if (signerInfs.isEmpty())
    // {
    // /* RFC 3852 5.2
    // * "In the degenerate case where there are no signers, the
    // * EncapsulatedContentInfo value being "signed" is irrelevant.  In this
    // * case, the content type within the EncapsulatedContentInfo value being
    // * "signed" MUST be id-data (as defined in section 4), and the content
    // * field of the EncapsulatedContentInfo value MUST be omitted."
    // */
    // if (encapsulate)
    // {
    // throw new IllegalArgumentException("no signers, encapsulate must be false");
    // }
    // if (!DATA.equals(eContentType))
    // {
    // throw new IllegalArgumentException("no signers, eContentType must be id-data");
    // }
    // }
    // 
    // if (!DATA.equals(eContentType))
    // {
    // /* RFC 3852 5.3
    // * [The 'signedAttrs']...
    // * field is optional, but it MUST be present if the content type of
    // * the EncapsulatedContentInfo value being signed is not id-data.
    // */
    // // TODO signedAttrs must be present for all signers
    // }
    Set<AlgorithmIdentifier> digestAlgs = new LinkedHashSet<AlgorithmIdentifier>();
    ASN1EncodableVector signerInfos = new ASN1EncodableVector();
    // clear the current preserved digest state
    digests.clear();
    // 
    for (Iterator it = _signers.iterator(); it.hasNext(); ) {
        SignerInformation signer = (SignerInformation) it.next();
        CMSUtils.addDigestAlgs(digestAlgs, signer, digestAlgIdFinder);
        // TODO Verify the content type and calculated digest match the precalculated SignerInfo
        signerInfos.add(signer.toASN1Structure());
    }
    // 
    // add the SignerInfo objects
    // 
    ASN1ObjectIdentifier contentTypeOID = content.getContentType();
    ASN1OctetString octs = null;
    if (content.getContent() != null) {
        ByteArrayOutputStream bOut = null;
        if (encapsulate) {
            bOut = new ByteArrayOutputStream();
        }
        OutputStream cOut = CMSUtils.attachSignersToOutputStream(signerGens, bOut);
        // Just in case it's unencapsulated and there are no signers!
        cOut = CMSUtils.getSafeOutputStream(cOut);
        try {
            content.write(cOut);
            cOut.close();
        } catch (IOException e) {
            throw new CMSException("data processing exception: " + e.getMessage(), e);
        }
        if (encapsulate) {
            octs = new BEROctetString(bOut.toByteArray());
        }
    }
    for (Iterator it = signerGens.iterator(); it.hasNext(); ) {
        SignerInfoGenerator sGen = (SignerInfoGenerator) it.next();
        SignerInfo inf = sGen.generate(contentTypeOID);
        digestAlgs.add(inf.getDigestAlgorithm());
        signerInfos.add(inf);
        byte[] calcDigest = sGen.getCalculatedDigest();
        if (calcDigest != null) {
            digests.put(inf.getDigestAlgorithm().getAlgorithm().getId(), calcDigest);
        }
    }
    ASN1Set certificates = null;
    if (certs.size() != 0) {
        certificates = CMSUtils.createBerSetFromList(certs);
    }
    ASN1Set certrevlist = null;
    if (crls.size() != 0) {
        certrevlist = CMSUtils.createBerSetFromList(crls);
    }
    ContentInfo encInfo = new ContentInfo(contentTypeOID, octs);
    SignedData sd = new SignedData(CMSUtils.convertToBERSet(digestAlgs), encInfo, certificates, certrevlist, new DERSet(signerInfos));
    ContentInfo contentInfo = new ContentInfo(CMSObjectIdentifiers.signedData, sd);
    return new CMSSignedData(content, contentInfo);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) SignedData(com.github.zhenwei.pkix.util.asn1.cms.SignedData) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DERSet(com.github.zhenwei.core.asn1.DERSet) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) SignerInfo(com.github.zhenwei.pkix.util.asn1.cms.SignerInfo) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) Iterator(java.util.Iterator) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

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