~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/asn1/pkcs/RSAESOAEPparams.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.asn1.pkcs;
 
2
 
 
3
import org.bouncycastle.asn1.ASN1Encodable;
 
4
import org.bouncycastle.asn1.ASN1EncodableVector;
 
5
import org.bouncycastle.asn1.ASN1Sequence;
 
6
import org.bouncycastle.asn1.ASN1TaggedObject;
 
7
import org.bouncycastle.asn1.DERNull;
 
8
import org.bouncycastle.asn1.DERObject;
 
9
import org.bouncycastle.asn1.DEROctetString;
 
10
import org.bouncycastle.asn1.DERSequence;
 
11
import org.bouncycastle.asn1.DERTaggedObject;
 
12
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
 
13
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
 
14
 
 
15
public class RSAESOAEPparams
 
16
    extends ASN1Encodable
 
17
{
 
18
    private AlgorithmIdentifier hashAlgorithm;
 
19
    private AlgorithmIdentifier maskGenAlgorithm;
 
20
    private AlgorithmIdentifier pSourceAlgorithm;
 
21
    
 
22
    public final static AlgorithmIdentifier DEFAULT_HASH_ALGORITHM = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, new DERNull());
 
23
    public final static AlgorithmIdentifier DEFAULT_MASK_GEN_FUNCTION = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, DEFAULT_HASH_ALGORITHM);
 
24
    public final static AlgorithmIdentifier DEFAULT_P_SOURCE_ALGORITHM = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[0]));
 
25
    
 
26
    public static RSAESOAEPparams getInstance(
 
27
        Object  obj)
 
28
    {
 
29
        if (obj instanceof RSAESOAEPparams)
 
30
        {
 
31
            return (RSAESOAEPparams)obj;
 
32
        }
 
33
        else if (obj instanceof ASN1Sequence)
 
34
        {
 
35
            return new RSAESOAEPparams((ASN1Sequence)obj);
 
36
        }
 
37
 
 
38
        throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
 
39
    }
 
40
    
 
41
    /**
 
42
     * The default version
 
43
     */
 
44
    public RSAESOAEPparams()
 
45
    {
 
46
        hashAlgorithm = DEFAULT_HASH_ALGORITHM;
 
47
        maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
 
48
        pSourceAlgorithm = DEFAULT_P_SOURCE_ALGORITHM;
 
49
    }
 
50
    
 
51
    public RSAESOAEPparams(
 
52
        AlgorithmIdentifier hashAlgorithm,
 
53
        AlgorithmIdentifier maskGenAlgorithm,
 
54
        AlgorithmIdentifier pSourceAlgorithm)
 
55
    {
 
56
        this.hashAlgorithm = hashAlgorithm;
 
57
        this.maskGenAlgorithm = maskGenAlgorithm;
 
58
        this.pSourceAlgorithm = pSourceAlgorithm;
 
59
    }
 
60
    
 
61
    public RSAESOAEPparams(
 
62
        ASN1Sequence seq)
 
63
    {
 
64
        hashAlgorithm = DEFAULT_HASH_ALGORITHM;
 
65
        maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
 
66
        pSourceAlgorithm = DEFAULT_P_SOURCE_ALGORITHM;
 
67
        
 
68
        for (int i = 0; i != seq.size(); i++)
 
69
        {
 
70
            ASN1TaggedObject    o = (ASN1TaggedObject)seq.getObjectAt(i);
 
71
            
 
72
            switch (o.getTagNo())
 
73
            {
 
74
            case 0:
 
75
                hashAlgorithm = AlgorithmIdentifier.getInstance(o, true);
 
76
                break;
 
77
            case 1:
 
78
                maskGenAlgorithm = AlgorithmIdentifier.getInstance(o, true);
 
79
                break;
 
80
            case 2:
 
81
                pSourceAlgorithm = AlgorithmIdentifier.getInstance(o, true);
 
82
                break;
 
83
            default:
 
84
                throw new IllegalArgumentException("unknown tag");
 
85
            }
 
86
        }
 
87
    }
 
88
    
 
89
    public AlgorithmIdentifier getHashAlgorithm()
 
90
    {
 
91
        return hashAlgorithm;
 
92
    }
 
93
    
 
94
    public AlgorithmIdentifier getMaskGenAlgorithm()
 
95
    {
 
96
        return maskGenAlgorithm;
 
97
    }
 
98
    
 
99
    public AlgorithmIdentifier getPSourceAlgorithm()
 
100
    {
 
101
        return pSourceAlgorithm;
 
102
    }
 
103
    
 
104
    /**
 
105
     * <pre>
 
106
     *  RSAES-OAEP-params ::= SEQUENCE {
 
107
     *     hashAlgorithm      [0] OAEP-PSSDigestAlgorithms     DEFAULT sha1,
 
108
     *     maskGenAlgorithm   [1] PKCS1MGFAlgorithms  DEFAULT mgf1SHA1,
 
109
     *     pSourceAlgorithm   [2] PKCS1PSourceAlgorithms  DEFAULT pSpecifiedEmpty
 
110
     *   }
 
111
     *  
 
112
     *   OAEP-PSSDigestAlgorithms    ALGORITHM-IDENTIFIER ::= {
 
113
     *     { OID id-sha1 PARAMETERS NULL   }|
 
114
     *     { OID id-sha256 PARAMETERS NULL }|
 
115
     *     { OID id-sha384 PARAMETERS NULL }|
 
116
     *     { OID id-sha512 PARAMETERS NULL },
 
117
     *     ...  -- Allows for future expansion --
 
118
     *   }
 
119
     *   PKCS1MGFAlgorithms    ALGORITHM-IDENTIFIER ::= {
 
120
     *     { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
 
121
     *    ...  -- Allows for future expansion --
 
122
     *   }
 
123
     *   PKCS1PSourceAlgorithms    ALGORITHM-IDENTIFIER ::= {
 
124
     *     { OID id-pSpecified PARAMETERS OCTET STRING },
 
125
     *     ...  -- Allows for future expansion --
 
126
     *  }
 
127
     * </pre>
 
128
     * @return the asn1 primitive representing the parameters.
 
129
     */
 
130
    public DERObject toASN1Object()
 
131
    {
 
132
        ASN1EncodableVector v = new ASN1EncodableVector();
 
133
        
 
134
        if (!hashAlgorithm.equals(DEFAULT_HASH_ALGORITHM))
 
135
        {
 
136
            v.add(new DERTaggedObject(true, 0, hashAlgorithm));
 
137
        }
 
138
        
 
139
        if (!maskGenAlgorithm.equals(DEFAULT_MASK_GEN_FUNCTION))
 
140
        {
 
141
            v.add(new DERTaggedObject(true, 1, maskGenAlgorithm));
 
142
        }
 
143
        
 
144
        if (!pSourceAlgorithm.equals(DEFAULT_P_SOURCE_ALGORITHM))
 
145
        {
 
146
            v.add(new DERTaggedObject(true, 2, pSourceAlgorithm));
 
147
        }
 
148
        
 
149
        return new DERSequence(v);
 
150
    }
 
151
}