~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/cert/crmf/EncryptedValueParser.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.cert.crmf;
 
2
 
 
3
import java.io.ByteArrayInputStream;
 
4
import java.io.IOException;
 
5
import java.io.InputStream;
 
6
 
 
7
import org.bouncycastle.asn1.crmf.EncryptedValue;
 
8
import org.bouncycastle.asn1.x509.X509CertificateStructure;
 
9
import org.bouncycastle.cert.X509CertificateHolder;
 
10
import org.bouncycastle.operator.InputDecryptor;
 
11
import org.bouncycastle.util.Strings;
 
12
import org.bouncycastle.util.io.Streams;
 
13
 
 
14
/**
 
15
 * Parser for EncryptedValue structures.
 
16
 */
 
17
public class EncryptedValueParser
 
18
{
 
19
    private EncryptedValue value;
 
20
    private EncryptedValuePadder padder;
 
21
 
 
22
    /**
 
23
     * Basic constructor - create a parser to read the passed in value.
 
24
     *
 
25
     * @param value the value to be parsed.
 
26
     */
 
27
    public EncryptedValueParser(EncryptedValue value)
 
28
    {
 
29
        this.value = value;
 
30
    }
 
31
 
 
32
    /**
 
33
     * Create a parser to read the passed in value, assuming the padder was
 
34
     * applied to the data prior to encryption.
 
35
     *
 
36
     * @param value  the value to be parsed.
 
37
     * @param padder the padder to be used to remove padding from the decrypted value..
 
38
     */
 
39
    public EncryptedValueParser(EncryptedValue value, EncryptedValuePadder padder)
 
40
    {
 
41
        this.value = value;
 
42
        this.padder = padder;
 
43
    }
 
44
 
 
45
    private byte[] decryptValue(ValueDecryptorGenerator decGen)
 
46
        throws CRMFException
 
47
    {
 
48
        if (value.getIntendedAlg() != null)
 
49
        {
 
50
            throw new UnsupportedOperationException();
 
51
        }
 
52
        if (value.getValueHint() != null)
 
53
        {
 
54
            throw new UnsupportedOperationException();
 
55
        }
 
56
 
 
57
        InputDecryptor decryptor = decGen.getValueDecryptor(value.getKeyAlg(),
 
58
            value.getSymmAlg(), value.getEncSymmKey().getBytes());
 
59
        InputStream dataIn = decryptor.getInputStream(new ByteArrayInputStream(
 
60
            value.getEncValue().getBytes()));
 
61
        try
 
62
        {
 
63
            byte[] data = Streams.readAll(dataIn);
 
64
 
 
65
            if (padder != null)
 
66
            {
 
67
                return padder.getUnpaddedData(data);
 
68
            }
 
69
            
 
70
            return data;
 
71
        }
 
72
        catch (IOException e)
 
73
        {
 
74
            throw new CRMFException("Cannot parse decrypted data: " + e.getMessage(), e);
 
75
        }
 
76
    }
 
77
 
 
78
    /**
 
79
     * Read a X.509 certificate.
 
80
     *
 
81
     * @param decGen the decryptor generator to decrypt the encrypted value.
 
82
     * @return an X509CertificateHolder containing the certificate read.
 
83
     * @throws CRMFException if the decrypted data cannot be parsed, or a decryptor cannot be generated.
 
84
     */
 
85
    public X509CertificateHolder readCertificateHolder(ValueDecryptorGenerator decGen)
 
86
        throws CRMFException
 
87
    {
 
88
        return new X509CertificateHolder(X509CertificateStructure.getInstance(decryptValue(decGen)));
 
89
    }
 
90
 
 
91
    /**
 
92
     * Read a pass phrase.
 
93
     *
 
94
     * @param decGen the decryptor generator to decrypt the encrypted value.
 
95
     * @return a pass phrase as recovered from the encrypted value.
 
96
     * @throws CRMFException if the decrypted data cannot be parsed, or a decryptor cannot be generated.
 
97
     */
 
98
    public char[] readPassphrase(ValueDecryptorGenerator decGen)
 
99
        throws CRMFException
 
100
    {
 
101
        return Strings.fromUTF8ByteArray(decryptValue(decGen)).toCharArray();
 
102
    }
 
103
}