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