no.difi.sdp.client2.internal.CreateCMSDocument Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sikker-digital-post-klient-java Show documentation
Show all versions of sikker-digital-post-klient-java Show documentation
Klient for sending av sikker digital post fra offentlige virksomheter.
package no.difi.sdp.client2.internal;
import no.difi.sdp.client2.domain.Sertifikat;
import no.difi.sdp.client2.domain.exceptions.KonfigurasjonException;
import no.difi.sdp.client2.domain.exceptions.RuntimeIOException;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.RSAESOAEPparams;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.cms.CMSAlgorithm;
import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder;
import org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.OutputEncryptor;
import java.io.IOException;
import java.security.Security;
import java.security.cert.CertificateEncodingException;
public class CreateCMSDocument {
private final ASN1ObjectIdentifier cmsEncryptionAlgorithm;
private final AlgorithmIdentifier keyEncryptionScheme;
public CreateCMSDocument() {
Security.addProvider(new BouncyCastleProvider());
keyEncryptionScheme = rsaesOaepIdentifier();
cmsEncryptionAlgorithm = CMSAlgorithm.AES256_CBC;
}
private AlgorithmIdentifier rsaesOaepIdentifier() {
AlgorithmIdentifier hash = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256, DERNull.INSTANCE);
AlgorithmIdentifier mask = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, hash);
AlgorithmIdentifier p_source = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[0]));
ASN1Encodable parameters = new RSAESOAEPparams(hash, mask, p_source);
return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_RSAES_OAEP, parameters);
}
public CMSDocument createCMS(byte[] bytes, Sertifikat sertifikat) {
try {
JceKeyTransRecipientInfoGenerator recipientInfoGenerator = new JceKeyTransRecipientInfoGenerator(sertifikat.getX509Certificate(), keyEncryptionScheme)
.setProvider(BouncyCastleProvider.PROVIDER_NAME);
CMSEnvelopedDataGenerator envelopedDataGenerator = new CMSEnvelopedDataGenerator();
envelopedDataGenerator.addRecipientInfoGenerator(recipientInfoGenerator);
OutputEncryptor contentEncryptor = new JceCMSContentEncryptorBuilder(cmsEncryptionAlgorithm).build();
CMSEnvelopedData cmsData = envelopedDataGenerator.generate(new CMSProcessableByteArray(bytes),
contentEncryptor);
return new CMSDocument(cmsData.getEncoded());
} catch (CertificateEncodingException e) {
throw new KonfigurasjonException("Feil med mottakers sertifikat", e);
} catch (CMSException e) {
throw new KonfigurasjonException("Kunne ikke generere Cryptographic Message Syntax for dokumentpakke", e);
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
}