if (crl != null) { ArrayList certList = new ArrayList(); log.debug("Adding CRL to response message (outer signer)"); certList.add(crl); CertStore certs = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList), "BC"); gen1.addCertificatesAndCRLs(certs); }
    public boolean create()        throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException {        boolean ret = false;        try {            if (status.equals(ResponseStatus.SUCCESS)) {                log.debug("Creating a STATUS_OK message.");            } else {                log.debug("Creating a STATUS_FAILED message.");            }            CMSProcessable msg;            // The signed data to be enveloped            CMSSignedData s = null;            if (status.equals(ResponseStatus.SUCCESS)) {                CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator();                // Add the issued certificate to the signed portion of the CMS (as signer, degenerate case)                ArrayList certList = new ArrayList();                if (cert != null) {                    log.debug("Adding certificates to response message");                    certList.add(cert);                    certList.add(signCert);                                    }                if (crl != null) {                    log.debug("Adding CRL to response message");                    certList.add(crl);                }                CertStore certs = CertStore.getInstance("Collection",                        new CollectionCertStoreParameters(certList), "BC");                // Create the signed CMS message to be contained inside the envelope                msg = new CMSProcessableByteArray("PrimeKey".getBytes());                CMSSignedDataGenerator gen = new CMSSignedDataGenerator();                gen.addSigner(signKey, signCert, CMSSignedDataGenerator.DIGEST_SHA1);                gen.addCertificatesAndCRLs(certs);                s = gen.generate(msg, true, "BC");                // Envelope the CMS message                if (recipientKeyInfo != null) {                    try {                    X509Certificate rec = CertTools.getCertfromByteArray(recipientKeyInfo);                    log.debug("Added recipient information - issuer: '"+CertTools.getIssuerDN(rec)+"', serno: '"+rec.getSerialNumber().toString(16));                    edGen.addKeyTransRecipient(rec);                    } catch (CertificateException e) {                        throw new IOException("Can not decode recipients self signed certificate!");                    }                } else {                    edGen.addKeyTransRecipient((X509Certificate) cert);                }                CMSEnvelopedData ed = edGen.generate(new CMSProcessableByteArray(s.getEncoded()),                                    SMIMECapability.dES_CBC.getId(), "BC");                log.debug("Signed data is " + ed.getEncoded().length +" bytes long");                msg = new CMSProcessableByteArray(ed.getEncoded());            } else {                // Create an empty message here                msg = new CMSProcessableByteArray("PrimeKey".getBytes());            }            // Create the outermost signed data            CMSSignedDataGenerator gen1 = new CMSSignedDataGenerator();            // add authenticated attributes...status, transactionId, sender- and recipientNonce and more...            Hashtable attributes = new Hashtable();            DERObjectIdentifier oid;            Attribute attr;            DERSet value;                        // Content Type            /* Added automagically by CMSSignedDataGenerator            oid = PKCSObjectIdentifiers.pkcs_9_at_contentType;            value = new DERSet(PKCSObjectIdentifiers.data);            attr = new Attribute(oid, value);            attributes.put(attr.getAttrType(), attr);            */            // Message digest            /* Added automagically by CMSSignedDataGenerator            byte[] digest = null;            if (s != null) {                MessageDigest md = MessageDigest.getInstance("SHA1");                digest = md.digest(s.getEncoded());            } else {                digest = new byte[]{0};            }            oid = PKCSObjectIdentifiers.pkcs_9_at_messageDigest;            value = new DERSet(new DEROctetString(digest));            attr = new Attribute(oid, value);            attributes.put(attr.getAttrType(), attr);            */            // Message type (certrep)            oid = new DERObjectIdentifier(ScepRequestMessage.id_messageType);            value = new DERSet(new DERPrintableString("3"));            attr = new Attribute(oid, value);            attributes.put(attr.getAttrType(), attr);            // TransactionId            if (transactionId != null) {                oid = new DERObjectIdentifier(ScepRequestMessage.id_transId);                value = new DERSet(new DERPrintableString(transactionId));                attr = new Attribute(oid, value);                attributes.put(attr.getAttrType(), attr);            }            // status            oid = new DERObjectIdentifier(ScepRequestMessage.id_pkiStatus);            value = new DERSet(new DERPrintableString(status.getValue()));            attr = new Attribute(oid, value);            attributes.put(attr.getAttrType(), attr);            if (status.equals(ResponseStatus.FAILURE)) {                oid = new DERObjectIdentifier(ScepRequestMessage.id_failInfo);                log.debug("Added failInfo: "+failInfo.getValue());                value = new DERSet(new DERPrintableString(failInfo.getValue()));                attr = new Attribute(oid, value);                attributes.put(attr.getAttrType(), attr);            }            // senderNonce            if (senderNonce != null) {                oid = new DERObjectIdentifier(ScepRequestMessage.id_senderNonce);                log.debug("Added senderNonce: "+senderNonce);                value = new DERSet(new DEROctetString(Base64.decode(senderNonce.getBytes())));                attr = new Attribute(oid, value);                attributes.put(attr.getAttrType(), attr);            }            // recipientNonce            if (recipientNonce != null) {                oid = new DERObjectIdentifier(ScepRequestMessage.id_recipientNonce);                log.debug("Added recipientNonce: "+recipientNonce);                                value = new DERSet(new DEROctetString(Base64.decode(recipientNonce.getBytes())));                attr = new Attribute(oid, value);                attributes.put(attr.getAttrType(), attr);            }            // Put our signer info and all newly generated attributes            gen1.addSigner(signKey, signCert, CMSSignedDataGenerator.DIGEST_SHA1,                new AttributeTable(attributes), null);            signedData = gen1.generate(msg, true, "BC");            responseMessage = signedData.getEncoded();            if (responseMessage != null) {                ret = true;            }        } catch (InvalidAlgorithmParameterException e) {            log.error("Error creating CertStore: ", e);        } catch (CertStoreException e) {            log.error("Error creating CertStore: ", e);        } catch (CMSException e) {            log.error("Error creating CMS message: ", e);        }        return ret;    }