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
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    // BEGIN android-changed
23c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public final static AlgorithmIdentifier DEFAULT_HASH_ALGORITHM = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE);
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    // END android-changed
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public final static AlgorithmIdentifier DEFAULT_MASK_GEN_FUNCTION = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, DEFAULT_HASH_ALGORITHM);
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public final static AlgorithmIdentifier DEFAULT_P_SOURCE_ALGORITHM = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[0]));
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static RSAESOAEPparams getInstance(
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj instanceof RSAESOAEPparams)
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (RSAESOAEPparams)obj;
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
354c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        else if (obj != null)
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
374c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return new RSAESOAEPparams(ASN1Sequence.getInstance(obj));
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
404c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return null;
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * The default version
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public RSAESOAEPparams()
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        hashAlgorithm = DEFAULT_HASH_ALGORITHM;
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        pSourceAlgorithm = DEFAULT_P_SOURCE_ALGORITHM;
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public RSAESOAEPparams(
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        AlgorithmIdentifier hashAlgorithm,
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        AlgorithmIdentifier maskGenAlgorithm,
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        AlgorithmIdentifier pSourceAlgorithm)
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.hashAlgorithm = hashAlgorithm;
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.maskGenAlgorithm = maskGenAlgorithm;
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.pSourceAlgorithm = pSourceAlgorithm;
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public RSAESOAEPparams(
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Sequence seq)
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        hashAlgorithm = DEFAULT_HASH_ALGORITHM;
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        pSourceAlgorithm = DEFAULT_P_SOURCE_ALGORITHM;
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != seq.size(); i++)
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            ASN1TaggedObject    o = (ASN1TaggedObject)seq.getObjectAt(i);
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            switch (o.getTagNo())
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case 0:
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                hashAlgorithm = AlgorithmIdentifier.getInstance(o, true);
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                break;
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case 1:
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                maskGenAlgorithm = AlgorithmIdentifier.getInstance(o, true);
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                break;
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case 2:
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                pSourceAlgorithm = AlgorithmIdentifier.getInstance(o, true);
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                break;
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            default:
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                throw new IllegalArgumentException("unknown tag");
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public AlgorithmIdentifier getHashAlgorithm()
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return hashAlgorithm;
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public AlgorithmIdentifier getMaskGenAlgorithm()
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return maskGenAlgorithm;
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public AlgorithmIdentifier getPSourceAlgorithm()
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return pSourceAlgorithm;
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <pre>
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *  RSAES-OAEP-params ::= SEQUENCE {
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     hashAlgorithm      [0] OAEP-PSSDigestAlgorithms     DEFAULT sha1,
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     maskGenAlgorithm   [1] PKCS1MGFAlgorithms  DEFAULT mgf1SHA1,
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     pSourceAlgorithm   [2] PKCS1PSourceAlgorithms  DEFAULT pSpecifiedEmpty
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *   }
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *   OAEP-PSSDigestAlgorithms    ALGORITHM-IDENTIFIER ::= {
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     { OID id-sha1 PARAMETERS NULL   }|
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     { OID id-sha256 PARAMETERS NULL }|
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     { OID id-sha384 PARAMETERS NULL }|
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     { OID id-sha512 PARAMETERS NULL },
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     ...  -- Allows for future expansion --
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *   }
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *   PKCS1MGFAlgorithms    ALGORITHM-IDENTIFIER ::= {
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *    ...  -- Allows for future expansion --
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *   }
125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *   PKCS1PSourceAlgorithms    ALGORITHM-IDENTIFIER ::= {
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     { OID id-pSpecified PARAMETERS OCTET STRING },
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     ...  -- Allows for future expansion --
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *  }
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * </pre>
130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @return the asn1 primitive representing the parameters.
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
1324c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1EncodableVector v = new ASN1EncodableVector();
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!hashAlgorithm.equals(DEFAULT_HASH_ALGORITHM))
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            v.add(new DERTaggedObject(true, 0, hashAlgorithm));
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!maskGenAlgorithm.equals(DEFAULT_MASK_GEN_FUNCTION))
142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            v.add(new DERTaggedObject(true, 1, maskGenAlgorithm));
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!pSourceAlgorithm.equals(DEFAULT_P_SOURCE_ALGORITHM))
147b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            v.add(new DERTaggedObject(true, 2, pSourceAlgorithm));
149b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
150b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
151b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERSequence(v);
152b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
153b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
154