All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.cms.CMSEnvelopedDataGenerator Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java CMS and S/MIME APIs for handling the CMS and S/MIME protocols. This jar contains CMS and S/MIME APIs for JDK 1.6. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs. If the S/MIME API is used, the JavaMail API and the Java activation framework will also be needed.

There is a newer version: 1.46
Show newest version
package org.bouncycastle.cms;

import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.BERConstructedOctetString;
import org.bouncycastle.asn1.DERSet;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.cms.EncryptedContentInfo;
import org.bouncycastle.asn1.cms.EnvelopedData;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Iterator;

/**
 * General class for generating a CMS enveloped-data message.
 *
 * A simple example of usage.
 *
 * 
 *      CMSEnvelopedDataGenerator  fact = new CMSEnvelopedDataGenerator();
 *
 *      fact.addKeyTransRecipient(cert);
 *
 *      CMSEnvelopedData         data = fact.generate(content, algorithm, "BC");
 * 
*/ public class CMSEnvelopedDataGenerator extends CMSEnvelopedGenerator { /** * base constructor */ public CMSEnvelopedDataGenerator() { } /** * generate an enveloped object that contains an CMS Enveloped Data * object using the given provider and the passed in key generator. */ private CMSEnvelopedData generate( CMSProcessable content, String encryptionOID, KeyGenerator keyGen, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, CMSException { String encProviderName = keyGen.getProvider().getName(); ASN1EncodableVector recipientInfos = new ASN1EncodableVector(); AlgorithmIdentifier encAlgId; SecretKey encKey; ASN1OctetString encContent; try { Cipher cipher = CMSEnvelopedHelper.INSTANCE.getSymmetricCipher(encryptionOID, encProviderName); AlgorithmParameters params; encKey = keyGen.generateKey(); params = generateParameters(encryptionOID, encKey, encProviderName); cipher.init(Cipher.ENCRYPT_MODE, encKey, params); // // If params are null we try and second guess on them as some providers don't provide // algorithm parameter generation explicity but instead generate them under the hood. // if (params == null) { params = cipher.getParameters(); } encAlgId = getAlgorithmIdentifier(encryptionOID, params); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); CipherOutputStream cOut = new CipherOutputStream(bOut, cipher); content.write(cOut); cOut.close(); encContent = new BERConstructedOctetString(bOut.toByteArray()); } catch (NoSuchAlgorithmException e) { throw new CMSException("can't find algorithm.", e); } catch (InvalidKeyException e) { throw new CMSException("key invalid in message.", e); } catch (NoSuchPaddingException e) { throw new CMSException("required padding not supported.", e); } catch (InvalidAlgorithmParameterException e) { throw new CMSException("algorithm parameters invalid.", e); } catch (IOException e) { throw new CMSException("exception decoding algorithm parameters.", e); } Iterator it = recipientInfs.iterator(); while (it.hasNext()) { RecipientInf recipient = (RecipientInf)it.next(); try { recipientInfos.add(recipient.toRecipientInfo(encKey, provider)); } catch (IOException e) { throw new CMSException("encoding error.", e); } catch (InvalidKeyException e) { throw new CMSException("key inappropriate for algorithm.", e); } catch (GeneralSecurityException e) { throw new CMSException("error making encrypted content.", e); } } EncryptedContentInfo eci = new EncryptedContentInfo( PKCSObjectIdentifiers.data, encAlgId, encContent); ContentInfo contentInfo = new ContentInfo( PKCSObjectIdentifiers.envelopedData, new EnvelopedData(null, new DERSet(recipientInfos), eci, null)); return new CMSEnvelopedData(contentInfo); } /** * generate an enveloped object that contains an CMS Enveloped Data * object using the given provider. */ public CMSEnvelopedData generate( CMSProcessable content, String encryptionOID, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, CMSException { KeyGenerator keyGen = CMSEnvelopedHelper.INSTANCE.createSymmetricKeyGenerator(encryptionOID, provider); return generate(content, encryptionOID, keyGen, provider); } /** * generate an enveloped object that contains an CMS Enveloped Data * object using the given provider. */ public CMSEnvelopedData generate( CMSProcessable content, String encryptionOID, int keySize, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, CMSException { KeyGenerator keyGen = CMSEnvelopedHelper.INSTANCE.createSymmetricKeyGenerator(encryptionOID, provider); keyGen.init(keySize); return generate(content, encryptionOID, keyGen, provider); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy