• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.bouncycastle.asn1.pkcs;
2 
3 import java.math.BigInteger;
4 
5 import org.bouncycastle.asn1.ASN1EncodableVector;
6 import org.bouncycastle.asn1.ASN1Integer;
7 import org.bouncycastle.asn1.ASN1Object;
8 import org.bouncycastle.asn1.ASN1Primitive;
9 import org.bouncycastle.asn1.ASN1Sequence;
10 import org.bouncycastle.asn1.ASN1TaggedObject;
11 import org.bouncycastle.asn1.DERNull;
12 import org.bouncycastle.asn1.DERSequence;
13 import org.bouncycastle.asn1.DERTaggedObject;
14 import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
15 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
16 
17 public class RSASSAPSSparams
18     extends ASN1Object
19 {
20     private AlgorithmIdentifier hashAlgorithm;
21     private AlgorithmIdentifier maskGenAlgorithm;
22     private ASN1Integer          saltLength;
23     private ASN1Integer          trailerField;
24 
25     public final static AlgorithmIdentifier DEFAULT_HASH_ALGORITHM = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE);
26     public final static AlgorithmIdentifier DEFAULT_MASK_GEN_FUNCTION = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, DEFAULT_HASH_ALGORITHM);
27     public final static ASN1Integer          DEFAULT_SALT_LENGTH = new ASN1Integer(20);
28     public final static ASN1Integer          DEFAULT_TRAILER_FIELD = new ASN1Integer(1);
29 
getInstance( Object obj)30     public static RSASSAPSSparams getInstance(
31         Object  obj)
32     {
33         if (obj instanceof RSASSAPSSparams)
34         {
35             return (RSASSAPSSparams)obj;
36         }
37         else if (obj != null)
38         {
39             return new RSASSAPSSparams(ASN1Sequence.getInstance(obj));
40         }
41 
42         return null;
43     }
44 
45     /**
46      * The default version
47      */
RSASSAPSSparams()48     public RSASSAPSSparams()
49     {
50         hashAlgorithm = DEFAULT_HASH_ALGORITHM;
51         maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
52         saltLength = DEFAULT_SALT_LENGTH;
53         trailerField = DEFAULT_TRAILER_FIELD;
54     }
55 
RSASSAPSSparams( AlgorithmIdentifier hashAlgorithm, AlgorithmIdentifier maskGenAlgorithm, ASN1Integer saltLength, ASN1Integer trailerField)56     public RSASSAPSSparams(
57         AlgorithmIdentifier hashAlgorithm,
58         AlgorithmIdentifier maskGenAlgorithm,
59         ASN1Integer          saltLength,
60         ASN1Integer          trailerField)
61     {
62         this.hashAlgorithm = hashAlgorithm;
63         this.maskGenAlgorithm = maskGenAlgorithm;
64         this.saltLength = saltLength;
65         this.trailerField = trailerField;
66     }
67 
RSASSAPSSparams( ASN1Sequence seq)68     private RSASSAPSSparams(
69         ASN1Sequence seq)
70     {
71         hashAlgorithm = DEFAULT_HASH_ALGORITHM;
72         maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
73         saltLength = DEFAULT_SALT_LENGTH;
74         trailerField = DEFAULT_TRAILER_FIELD;
75 
76         for (int i = 0; i != seq.size(); i++)
77         {
78             ASN1TaggedObject    o = (ASN1TaggedObject)seq.getObjectAt(i);
79 
80             switch (o.getTagNo())
81             {
82             case 0:
83                 hashAlgorithm = AlgorithmIdentifier.getInstance(o, true);
84                 break;
85             case 1:
86                 maskGenAlgorithm = AlgorithmIdentifier.getInstance(o, true);
87                 break;
88             case 2:
89                 saltLength = ASN1Integer.getInstance(o, true);
90                 break;
91             case 3:
92                 trailerField = ASN1Integer.getInstance(o, true);
93                 break;
94             default:
95                 throw new IllegalArgumentException("unknown tag");
96             }
97         }
98     }
99 
getHashAlgorithm()100     public AlgorithmIdentifier getHashAlgorithm()
101     {
102         return hashAlgorithm;
103     }
104 
getMaskGenAlgorithm()105     public AlgorithmIdentifier getMaskGenAlgorithm()
106     {
107         return maskGenAlgorithm;
108     }
109 
getSaltLength()110     public BigInteger getSaltLength()
111     {
112         return saltLength.getValue();
113     }
114 
getTrailerField()115     public BigInteger getTrailerField()
116     {
117         return trailerField.getValue();
118     }
119 
120     /**
121      * <pre>
122      * RSASSA-PSS-params ::= SEQUENCE {
123      *   hashAlgorithm      [0] OAEP-PSSDigestAlgorithms  DEFAULT sha1,
124      *    maskGenAlgorithm   [1] PKCS1MGFAlgorithms  DEFAULT mgf1SHA1,
125      *    saltLength         [2] INTEGER  DEFAULT 20,
126      *    trailerField       [3] TrailerField  DEFAULT trailerFieldBC
127      *  }
128      *
129      * OAEP-PSSDigestAlgorithms    ALGORITHM-IDENTIFIER ::= {
130      *    { OID id-sha1 PARAMETERS NULL   }|
131      *    { OID id-sha256 PARAMETERS NULL }|
132      *    { OID id-sha384 PARAMETERS NULL }|
133      *    { OID id-sha512 PARAMETERS NULL },
134      *    ...  -- Allows for future expansion --
135      * }
136      *
137      * PKCS1MGFAlgorithms    ALGORITHM-IDENTIFIER ::= {
138      *   { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
139      *    ...  -- Allows for future expansion --
140      * }
141      *
142      * TrailerField ::= INTEGER { trailerFieldBC(1) }
143      * </pre>
144      * @return the asn1 primitive representing the parameters.
145      */
toASN1Primitive()146     public ASN1Primitive toASN1Primitive()
147     {
148         ASN1EncodableVector v = new ASN1EncodableVector();
149 
150         if (!hashAlgorithm.equals(DEFAULT_HASH_ALGORITHM))
151         {
152             v.add(new DERTaggedObject(true, 0, hashAlgorithm));
153         }
154 
155         if (!maskGenAlgorithm.equals(DEFAULT_MASK_GEN_FUNCTION))
156         {
157             v.add(new DERTaggedObject(true, 1, maskGenAlgorithm));
158         }
159 
160         if (!saltLength.equals(DEFAULT_SALT_LENGTH))
161         {
162             v.add(new DERTaggedObject(true, 2, saltLength));
163         }
164 
165         if (!trailerField.equals(DEFAULT_TRAILER_FIELD))
166         {
167             v.add(new DERTaggedObject(true, 3, trailerField));
168         }
169 
170         return new DERSequence(v);
171     }
172 }
173