1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.security.spec;
19
20/**
21 * The parameter specification for the RSA-PSS Signature scheme.
22 * <p>
23 * Defined in the <a
24 * href="http://www.rsa.com/rsalabs/pubs/PKCS/html/pkcs-1.html">PKCS #1 v2.1</a>
25 * standard.
26 */
27public class PSSParameterSpec implements AlgorithmParameterSpec {
28
29    /**
30     * The default parameter specification. It specifies the following parameters:
31     * <ul>
32     * <li>message digest: {@code "SHA-1"}</li>
33     * <li>mask generation function (<i>mgf</i>): {@code "MGF1"}</li>
34     * <li>parameters for the <i>mgf</i>: {@link MGF1ParameterSpec#SHA1}</li>
35     * <li>salt length: {@code 20}</li>
36     * <li>trailer field: {@code -1}</li>
37     * </ul>
38     */
39    public static final PSSParameterSpec DEFAULT = new PSSParameterSpec(20);
40
41    // Message digest algorithm name
42    private final String mdName;
43    // Mask generation function algorithm name
44    private final String mgfName;
45    // Mask generation function parameters
46    private final AlgorithmParameterSpec mgfSpec;
47    // Trailer field value
48    private final int trailerField;
49    // Salt length in bits
50    private final int saltLen;
51
52    /**
53     * Creates a new {@code PSSParameterSpec} with the specified salt length
54     * and the default values.
55     *
56     * @param saltLen
57     *            the salt length (in bits).
58     * @throws IllegalArgumentException
59     *             if {@code saltLen} is negative.
60     */
61    public PSSParameterSpec(int saltLen) {
62        if (saltLen < 0) {
63            throw new IllegalArgumentException("saltLen < 0");
64        }
65        this.saltLen = saltLen;
66        this.mdName = "SHA-1";
67        this.mgfName = "MGF1";
68        this.mgfSpec = MGF1ParameterSpec.SHA1;
69        this.trailerField = 1;
70    }
71
72    /**
73     * Creates a new {@code PSSParameterSpec} with the specified message digest
74     * name, mask generation function name, mask generation function parameters,
75     * salt length, and trailer field value.
76     *
77     * @param mdName
78     *            the name of the message digest algorithm.
79     * @param mgfName
80     *            the name of the mask generation function algorithm.
81     * @param mgfSpec
82     *            the parameter for the mask generation function algorithm.
83     * @param saltLen
84     *            the salt length (in bits).
85     * @param trailerField
86     *            the trailer field value.
87     * @throws IllegalArgumentException
88     *             if {@code saltLen} or {@code trailerField} is negative.
89     */
90    public PSSParameterSpec(String mdName, String mgfName,
91            AlgorithmParameterSpec mgfSpec, int saltLen, int trailerField) {
92
93        if (mdName == null) {
94            throw new NullPointerException("mdName == null");
95        }
96        if (mgfName == null) {
97            throw new NullPointerException("mgfName == null");
98        }
99        if (saltLen < 0) {
100            throw new IllegalArgumentException("saltLen < 0");
101        }
102        if (trailerField < 0) {
103            throw new IllegalArgumentException("trailerField < 0");
104        }
105        this.mdName = mdName;
106        this.mgfName = mgfName;
107        this.mgfSpec = mgfSpec;
108        this.saltLen = saltLen;
109        this.trailerField = trailerField;
110    }
111
112    /**
113     * Returns the length of the salt (in bits).
114     *
115     * @return the length of the salt (in bits).
116     */
117    public int getSaltLength() {
118        return saltLen;
119    }
120
121    /**
122     * Returns the name of the message digest algorithm.
123     *
124     * @return the name of the message digest algorithm.
125     */
126    public String getDigestAlgorithm() {
127        return mdName;
128    }
129
130    /**
131     * Returns the name of the mask generation function algorithm.
132     *
133     * @return the name of the mask generation function algorithm.
134     */
135    public String getMGFAlgorithm() {
136        return mgfName;
137    }
138
139    /**
140     * Returns the parameter for the mask generation function algorithm.
141     *
142     * @return the parameter for the mask generation function algorithm, or
143     *         {@code null} if none specified.
144     */
145    public AlgorithmParameterSpec getMGFParameters() {
146        return mgfSpec;
147    }
148
149    /**
150     * Returns the trailer field value.
151     *
152     * @return the trailer field value.
153     */
154    public int getTrailerField() {
155        return trailerField;
156    }
157}
158