1
package org.bouncycastle.cms;
3
import java.io.IOException;
4
import java.security.InvalidKeyException;
5
import java.security.Key;
6
import java.security.NoSuchAlgorithmException;
7
import java.security.NoSuchProviderException;
8
import java.security.Provider;
10
import javax.crypto.Cipher;
11
import javax.crypto.NoSuchPaddingException;
13
import org.bouncycastle.asn1.cms.KEKIdentifier;
14
import org.bouncycastle.asn1.cms.KEKRecipientInfo;
15
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
18
* the RecipientInfo class for a recipient who has been sent a message
19
* encrypted using a secret key known to the other side.
21
public class KEKRecipientInformation
22
extends RecipientInformation
24
private KEKRecipientInfo info;
26
KEKRecipientInformation(
27
KEKRecipientInfo info,
28
AlgorithmIdentifier messageAlgorithm,
29
CMSSecureReadable secureReadable,
30
AuthAttributesProvider additionalData)
32
super(info.getKeyEncryptionAlgorithm(), messageAlgorithm, secureReadable, additionalData);
36
KEKIdentifier kekId = info.getKekid();
38
this.rid = new KEKRecipientId(kekId.getKeyIdentifier().getOctets());
42
* decrypt the content and return an input stream.
44
public CMSTypedStream getContentStream(
47
throws CMSException, NoSuchProviderException
49
return getContentStream(key, CMSUtils.getProvider(prov));
53
* decrypt the content and return an input stream.
55
public CMSTypedStream getContentStream(
62
Cipher keyCipher = CMSEnvelopedHelper.INSTANCE.createSymmetricCipher(
63
keyEncAlg.getObjectId().getId(), prov);
64
keyCipher.init(Cipher.UNWRAP_MODE, key);
65
Key sKey = keyCipher.unwrap(info.getEncryptedKey().getOctets(), getContentAlgorithmName(),
68
return getContentFromSessionKey(sKey, prov);
70
catch (NoSuchAlgorithmException e)
72
throw new CMSException("can't find algorithm.", e);
74
catch (InvalidKeyException e)
76
throw new CMSException("key invalid in message.", e);
78
catch (NoSuchPaddingException e)
80
throw new CMSException("required padding not supported.", e);
84
protected RecipientOperator getRecipientOperator(Recipient recipient)
85
throws CMSException, IOException
87
return ((KEKRecipient)recipient).getRecipientOperator(keyEncAlg, messageAlgorithm, info.getEncryptedKey().getOctets());