Example usage for org.bouncycastle.cms CMSException CMSException

List of usage examples for org.bouncycastle.cms CMSException CMSException

Introduction

In this page you can find the example usage for org.bouncycastle.cms CMSException CMSException.

Prototype

public CMSException(String msg, Exception e) 

Source Link

Usage

From source file:it.trento.comune.j4sign.cms.ExternalSignatureCMSSignedDataGenerator.java

License:Open Source License

/**
 * add the certificates and CRLs contained in the given CertStore to the
 * pool that will be included in the encoded signature block.
 * <p>//from  w w w.j  a  v a  2 s . c o  m
 * Note: this assumes the CertStore will support null in the get methods.
 * 
 * @param certStore
 * @throws CertStoreException
 * @throws CMSException
 */
public void addCertificatesAndCRLs(CertStore certStore) throws CertStoreException, CMSException {
    //
    // divide up the certs and crls.
    //
    try {
        Iterator it = certStore.getCertificates(null).iterator();

        while (it.hasNext()) {
            X509Certificate c = (X509Certificate) it.next();

            certs.add(new X509CertificateStructure((ASN1Sequence) makeObj(c.getEncoded())));
        }
    } catch (IOException e) {
        throw new CMSException("error processing certs", e);
    } catch (CertificateEncodingException e) {
        throw new CMSException("error encoding certs", e);
    }

    try {
        Iterator it = certStore.getCRLs(null).iterator();

        while (it.hasNext()) {
            X509CRL c = (X509CRL) it.next();

            crls.add(new CertificateList((ASN1Sequence) makeObj(c.getEncoded())));
        }
    } catch (IOException e) {
        throw new CMSException("error processing crls", e);
    } catch (CRLException e) {
        throw new CMSException("error encoding crls", e);
    }
}

From source file:it.trento.comune.j4sign.cms.ExternalSignatureCMSSignedDataGenerator.java

License:Open Source License

/**
 * generate a CMS Signed Data object using the previously passed {@link ExternalSignatureSignerInfoGenerator}
 * objects; if encapsulate is true a copy of the message will be
 * included in the signature.//from ww w . j  a  v  a 2 s .co  m
 */
public CMSSignedData generate(CMSProcessable content, boolean encapsulate)

        throws NoSuchAlgorithmException, NoSuchProviderException, CMSException,
        InvalidAlgorithmParameterException, CertStoreException {

    //DEREncodableVector signerInfos = new DEREncodableVector();
    //DEREncodableVector digestAlgs = new DEREncodableVector();

    ASN1EncodableVector digestAlgs = new ASN1EncodableVector();
    ASN1EncodableVector signerInfos = new ASN1EncodableVector();

    ASN1ObjectIdentifier contentTypeOID = new ASN1ObjectIdentifier(CMSSignedDataGenerator.DATA);

    //
    // add the SignerInfo objects
    //
    Iterator it = signerInfs.iterator();

    //raccoglier i certificati dei firmatari
    //ArrayList certList = new ArrayList();

    while (it.hasNext()) {
        AlgorithmIdentifier digAlgId, encAlgId;
        ExternalSignatureSignerInfoGenerator externalSigner = (ExternalSignatureSignerInfoGenerator) it.next();
        try {
            digAlgId = makeAlgId(externalSigner.getDigestAlgOID(), externalSigner.getDigestAlgParams());

            digestAlgs.add(digAlgId);

            signerInfos.add(externalSigner.generate());

            //certList.add(externalSigner.getCertificate());
        } catch (IOException e) {
            throw new CMSException("encoding error.", e);
        } catch (CertificateEncodingException e) {
            throw new CMSException("error creating sid.", e);
        }
    }

    ASN1Set certificates = null;

    if (certs.size() != 0) {
        certificates = createBerSetFromList(certs);
    }
    /*
            if (certs.size() != 0) {
    DEREncodableVector v = new DEREncodableVector();
            
    it = certs.iterator();
    while (it.hasNext()) {
        v.add((DEREncodable) it.next());
    }
            
    certificates = new DERSet(v);
            }
    */
    ASN1Set certrevlist = null;

    if (crls.size() != 0) {
        certrevlist = createBerSetFromList(crls);
    }
    /*        
            if (crls.size() != 0) {
    DEREncodableVector v = new DEREncodableVector();
            
    it = crls.iterator();
    while (it.hasNext()) {
        v.add((DEREncodable) it.next());
    }
            
    certrevlist = new DERSet(v);
            }
    */

    ASN1OctetString octs = null;
    if (encapsulate) {

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        try {
            content.write(bOut);
        } catch (IOException e) {
            throw new CMSException("encapsulation error.", e);
        }

        octs = new BERConstructedOctetString(bOut.toByteArray());

    }

    ContentInfo encInfo = new ContentInfo(contentTypeOID, octs);

    SignedData sd = new SignedData(new DERSet(digestAlgs), encInfo, certificates, certrevlist,
            new DERSet(signerInfos));

    ContentInfo contentInfo = new ContentInfo(PKCSObjectIdentifiers.signedData, sd);

    return new CMSSignedData(content, contentInfo);
}

From source file:org.votingsystem.signature.util.CMSUtils.java

License:Open Source License

static List getCertificatesFromStore(CertStore certStore) throws CertStoreException, CMSException {
    List certs = new ArrayList();
    try {/*from   w  w w .ja  v  a 2s  . c o m*/
        for (Iterator it = certStore.getCertificates(null).iterator(); it.hasNext();) {
            X509Certificate c = (X509Certificate) it.next();
            certs.add(X509CertificateStructure.getInstance(ASN1Object.fromByteArray(c.getEncoded())));
        }
        return certs;
    } catch (IllegalArgumentException e) {
        throw new CMSException("error processing certs", e);
    } catch (IOException e) {
        throw new CMSException("error processing certs", e);
    } catch (CertificateEncodingException e) {
        throw new CMSException("error encoding certs", e);
    }
}

From source file:org.votingsystem.signature.util.CMSUtils.java

License:Open Source License

static List getCRLsFromStore(CertStore certStore) throws CertStoreException, CMSException {
    List crls = new ArrayList();
    try {/* w w  w  .ja  va2  s .  c om*/
        for (Iterator it = certStore.getCRLs(null).iterator(); it.hasNext();) {
            X509CRL c = (X509CRL) it.next();
            crls.add(CertificateList.getInstance(ASN1Object.fromByteArray(c.getEncoded())));
        }
        return crls;
    } catch (IllegalArgumentException e) {
        throw new CMSException("error processing crls", e);
    } catch (IOException e) {
        throw new CMSException("error processing crls", e);
    } catch (CRLException e) {
        throw new CMSException("error encoding crls", e);
    }
}

From source file:org.votingsystem.signature.util.CMSUtils.java

License:Open Source License

private static ContentInfo readContentInfo(ASN1InputStream in) throws CMSException {
    try {//from ww w  .jav  a 2s .  com
        return ContentInfo.getInstance(in.readObject());
    } catch (IOException e) {
        throw new CMSException("IOException reading content.", e);
    } catch (ClassCastException e) {
        throw new CMSException("Malformed content.", e);
    } catch (IllegalArgumentException e) {
        throw new CMSException("Malformed content.", e);
    }
}