~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/cms/KEKRecipientInformation.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.cms;
 
2
 
 
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;
 
9
 
 
10
import javax.crypto.Cipher;
 
11
import javax.crypto.NoSuchPaddingException;
 
12
 
 
13
import org.bouncycastle.asn1.cms.KEKIdentifier;
 
14
import org.bouncycastle.asn1.cms.KEKRecipientInfo;
 
15
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
 
16
 
 
17
/**
 
18
 * the RecipientInfo class for a recipient who has been sent a message
 
19
 * encrypted using a secret key known to the other side.
 
20
 */
 
21
public class KEKRecipientInformation
 
22
    extends RecipientInformation
 
23
{
 
24
    private KEKRecipientInfo      info;
 
25
 
 
26
    KEKRecipientInformation(
 
27
        KEKRecipientInfo        info,
 
28
        AlgorithmIdentifier     messageAlgorithm,
 
29
        CMSSecureReadable       secureReadable,
 
30
        AuthAttributesProvider  additionalData)
 
31
    {
 
32
        super(info.getKeyEncryptionAlgorithm(), messageAlgorithm, secureReadable, additionalData);
 
33
 
 
34
        this.info = info;
 
35
 
 
36
        KEKIdentifier kekId = info.getKekid();
 
37
 
 
38
        this.rid = new KEKRecipientId(kekId.getKeyIdentifier().getOctets());
 
39
    }
 
40
 
 
41
    /**
 
42
     * decrypt the content and return an input stream.
 
43
     */
 
44
    public CMSTypedStream getContentStream(
 
45
        Key      key,
 
46
        String   prov)
 
47
        throws CMSException, NoSuchProviderException
 
48
    {
 
49
        return getContentStream(key, CMSUtils.getProvider(prov));
 
50
    }
 
51
 
 
52
    /**
 
53
     * decrypt the content and return an input stream.
 
54
     */
 
55
    public CMSTypedStream getContentStream(
 
56
        Key      key,
 
57
        Provider prov)
 
58
        throws CMSException
 
59
    {
 
60
        try
 
61
        {
 
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(),
 
66
                Cipher.SECRET_KEY);
 
67
 
 
68
            return getContentFromSessionKey(sKey, prov);
 
69
        }
 
70
        catch (NoSuchAlgorithmException e)
 
71
        {
 
72
            throw new CMSException("can't find algorithm.", e);
 
73
        }
 
74
        catch (InvalidKeyException e)
 
75
        {
 
76
            throw new CMSException("key invalid in message.", e);
 
77
        }
 
78
        catch (NoSuchPaddingException e)
 
79
        {
 
80
            throw new CMSException("required padding not supported.", e);
 
81
        }
 
82
    }
 
83
 
 
84
    protected RecipientOperator getRecipientOperator(Recipient recipient)
 
85
        throws CMSException, IOException
 
86
    {
 
87
        return ((KEKRecipient)recipient).getRecipientOperator(keyEncAlg, messageAlgorithm, info.getEncryptedKey().getOctets());
 
88
    }
 
89
}