/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.security.cert.impl;
import java.io.OutputStream;
import java.io.Serializable;
import java.security.cert.Certificate;
import org.bouncycastle.cms.CMSAbsentContent;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;
import org.bouncycastle.cms.CMSTypedData;
import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.security.cert.CertificateException;
import br.net.woodstock.rockframework.security.cert.CertificateWriter;
import br.net.woodstock.rockframework.security.util.BouncyCastleProviderHelper;
public class BouncyCastlePKCS7CertificateWriter implements CertificateWriter, Serializable {
private static final long serialVersionUID = RockFrameworkVersion.VERSION;
public BouncyCastlePKCS7CertificateWriter() {
super();
}
@Override
public void write(final Certificate[] chain, final OutputStream outputStream) {
Assert.notEmpty(chain, "chain");
Assert.notNull(outputStream, "outputStream");
try {
CMSSignedDataGenerator signedDataGenerator = new CMSSignedDataGenerator();
CMSTypedData content = new CMSAbsentContent();
signedDataGenerator.addCertificates(BouncyCastleProviderHelper.toStore(chain));
CMSSignedData signedData = signedDataGenerator.generate(content, false);
byte[] bytes = signedData.getEncoded();
outputStream.write(bytes);
} catch (Exception e) {
throw new CertificateException(e);
}
}
}