author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
child 13361 | bda5c2354fc6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12685 | 2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
29 |
import java.io.*; |
|
30 |
import sun.security.util.*; |
|
31 |
import sun.security.x509.*; |
|
32 |
import java.security.AlgorithmParametersSpi; |
|
33 |
import java.security.NoSuchAlgorithmException; |
|
34 |
import java.security.spec.AlgorithmParameterSpec; |
|
35 |
import java.security.spec.InvalidParameterSpecException; |
|
36 |
import java.security.spec.MGF1ParameterSpec; |
|
37 |
import javax.crypto.spec.PSource; |
|
38 |
import javax.crypto.spec.OAEPParameterSpec; |
|
39 |
||
40 |
/** |
|
41 |
* This class implements the OAEP parameters used with the RSA |
|
42 |
* algorithm in OAEP padding. Here is its ASN.1 definition: |
|
43 |
* RSAES-OAEP-params ::= SEQUENCE { |
|
44 |
* hashAlgorithm [0] HashAlgorithm DEFAULT sha1, |
|
45 |
* maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1, |
|
46 |
* pSourceAlgorithm [2] PSourceAlgorithm DEFAULT pSpecifiedEmpty |
|
47 |
* } |
|
48 |
* |
|
49 |
* @author Valerie Peng |
|
50 |
* |
|
51 |
*/ |
|
52 |
||
53 |
public final class OAEPParameters extends AlgorithmParametersSpi { |
|
54 |
||
55 |
private String mdName; |
|
56 |
private MGF1ParameterSpec mgfSpec; |
|
57 |
private byte[] p; |
|
58 |
private static ObjectIdentifier OID_MGF1; |
|
59 |
private static ObjectIdentifier OID_PSpecified; |
|
60 |
||
61 |
static { |
|
62 |
try { |
|
63 |
OID_MGF1 = new ObjectIdentifier(new int[] {1,2,840,113549,1,1,8}); |
|
64 |
} catch (IOException ioe) { |
|
65 |
// should not happen |
|
66 |
OID_MGF1 = null; |
|
67 |
} |
|
68 |
try { |
|
69 |
OID_PSpecified = |
|
70 |
new ObjectIdentifier(new int[] {1,2,840,113549,1,1,9}); |
|
71 |
} catch (IOException ioe) { |
|
72 |
// should not happen |
|
73 |
OID_PSpecified = null; |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
public OAEPParameters() { |
|
78 |
} |
|
79 |
||
80 |
protected void engineInit(AlgorithmParameterSpec paramSpec) |
|
81 |
throws InvalidParameterSpecException { |
|
82 |
if (!(paramSpec instanceof OAEPParameterSpec)) { |
|
83 |
throw new InvalidParameterSpecException |
|
84 |
("Inappropriate parameter specification"); |
|
85 |
} |
|
86 |
OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec; |
|
87 |
mdName = spec.getDigestAlgorithm(); |
|
88 |
String mgfName = spec.getMGFAlgorithm(); |
|
89 |
if (!mgfName.equalsIgnoreCase("MGF1")) { |
|
90 |
throw new InvalidParameterSpecException("Unsupported mgf " + |
|
91 |
mgfName + "; MGF1 only"); |
|
92 |
} |
|
93 |
AlgorithmParameterSpec mgfSpec = spec.getMGFParameters(); |
|
94 |
if (!(mgfSpec instanceof MGF1ParameterSpec)) { |
|
95 |
throw new InvalidParameterSpecException("Inappropriate mgf " + |
|
96 |
"parameters; non-null MGF1ParameterSpec only"); |
|
97 |
} |
|
98 |
this.mgfSpec = (MGF1ParameterSpec) mgfSpec; |
|
99 |
PSource pSrc = spec.getPSource(); |
|
100 |
if (pSrc.getAlgorithm().equals("PSpecified")) { |
|
101 |
p = ((PSource.PSpecified) pSrc).getValue(); |
|
102 |
} else { |
|
103 |
throw new InvalidParameterSpecException("Unsupported pSource " + |
|
104 |
pSrc.getAlgorithm() + "; PSpecified only"); |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
private static String convertToStandardName(String internalName) { |
|
109 |
if (internalName.equals("SHA")) { |
|
110 |
return "SHA-1"; |
|
12685 | 111 |
} else if (internalName.equals("SHA224")) { |
112 |
return "SHA-224"; |
|
2 | 113 |
} else if (internalName.equals("SHA256")) { |
114 |
return "SHA-256"; |
|
115 |
} else if (internalName.equals("SHA384")) { |
|
116 |
return "SHA-384"; |
|
117 |
} else if (internalName.equals("SHA512")) { |
|
118 |
return "SHA-512"; |
|
119 |
} else { |
|
120 |
return internalName; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
protected void engineInit(byte[] encoded) |
|
125 |
throws IOException { |
|
126 |
DerInputStream der = new DerInputStream(encoded); |
|
127 |
mdName = "SHA-1"; |
|
128 |
mgfSpec = MGF1ParameterSpec.SHA1; |
|
129 |
p = new byte[0]; |
|
130 |
DerValue[] datum = der.getSequence(3); |
|
131 |
for (int i=0; i<datum.length; i++) { |
|
132 |
DerValue data = datum[i]; |
|
133 |
if (data.isContextSpecific((byte) 0x00)) { |
|
134 |
// hash algid |
|
135 |
mdName = convertToStandardName(AlgorithmId.parse |
|
136 |
(data.data.getDerValue()).getName()); |
|
137 |
} else if (data.isContextSpecific((byte) 0x01)) { |
|
138 |
// mgf algid |
|
139 |
AlgorithmId val = AlgorithmId.parse(data.data.getDerValue()); |
|
140 |
if (!val.getOID().equals((Object) OID_MGF1)) { |
|
141 |
throw new IOException("Only MGF1 mgf is supported"); |
|
142 |
} |
|
7043 | 143 |
AlgorithmId params = AlgorithmId.parse( |
144 |
new DerValue(val.getEncodedParams())); |
|
2 | 145 |
String mgfDigestName = convertToStandardName(params.getName()); |
146 |
if (mgfDigestName.equals("SHA-1")) { |
|
147 |
mgfSpec = MGF1ParameterSpec.SHA1; |
|
12685 | 148 |
} else if (mgfDigestName.equals("SHA-224")) { |
149 |
mgfSpec = MGF1ParameterSpec.SHA224; |
|
2 | 150 |
} else if (mgfDigestName.equals("SHA-256")) { |
151 |
mgfSpec = MGF1ParameterSpec.SHA256; |
|
152 |
} else if (mgfDigestName.equals("SHA-384")) { |
|
153 |
mgfSpec = MGF1ParameterSpec.SHA384; |
|
154 |
} else if (mgfDigestName.equals("SHA-512")) { |
|
155 |
mgfSpec = MGF1ParameterSpec.SHA512; |
|
156 |
} else { |
|
7043 | 157 |
throw new IOException( |
158 |
"Unrecognized message digest algorithm"); |
|
2 | 159 |
} |
160 |
} else if (data.isContextSpecific((byte) 0x02)) { |
|
161 |
// pSource algid |
|
162 |
AlgorithmId val = AlgorithmId.parse(data.data.getDerValue()); |
|
163 |
if (!val.getOID().equals((Object) OID_PSpecified)) { |
|
164 |
throw new IOException("Wrong OID for pSpecified"); |
|
165 |
} |
|
166 |
DerInputStream dis = new DerInputStream(val.getEncodedParams()); |
|
167 |
p = dis.getOctetString(); |
|
168 |
if (dis.available() != 0) { |
|
169 |
throw new IOException("Extra data for pSpecified"); |
|
170 |
} |
|
171 |
} else { |
|
172 |
throw new IOException("Invalid encoded OAEPParameters"); |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
protected void engineInit(byte[] encoded, String decodingMethod) |
|
178 |
throws IOException { |
|
179 |
if ((decodingMethod != null) && |
|
180 |
(!decodingMethod.equalsIgnoreCase("ASN.1"))) { |
|
181 |
throw new IllegalArgumentException("Only support ASN.1 format"); |
|
182 |
} |
|
183 |
engineInit(encoded); |
|
184 |
} |
|
185 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
186 |
protected <T extends AlgorithmParameterSpec> |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
187 |
T engineGetParameterSpec(Class<T> paramSpec) |
2 | 188 |
throws InvalidParameterSpecException { |
189 |
if (OAEPParameterSpec.class.isAssignableFrom(paramSpec)) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
190 |
return paramSpec.cast( |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
191 |
new OAEPParameterSpec(mdName, "MGF1", mgfSpec, |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7043
diff
changeset
|
192 |
new PSource.PSpecified(p))); |
2 | 193 |
} else { |
194 |
throw new InvalidParameterSpecException |
|
195 |
("Inappropriate parameter specification"); |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
protected byte[] engineGetEncoded() throws IOException { |
|
200 |
DerOutputStream tmp = new DerOutputStream(); |
|
201 |
DerOutputStream tmp2, tmp3; |
|
202 |
||
203 |
// MD |
|
204 |
AlgorithmId mdAlgId; |
|
205 |
try { |
|
206 |
mdAlgId = AlgorithmId.get(mdName); |
|
207 |
} catch (NoSuchAlgorithmException nsae) { |
|
208 |
throw new IOException("AlgorithmId " + mdName + |
|
209 |
" impl not found"); |
|
210 |
} |
|
211 |
tmp2 = new DerOutputStream(); |
|
212 |
mdAlgId.derEncode(tmp2); |
|
213 |
tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0), |
|
214 |
tmp2); |
|
215 |
||
216 |
// MGF |
|
217 |
tmp2 = new DerOutputStream(); |
|
218 |
tmp2.putOID(OID_MGF1); |
|
219 |
AlgorithmId mgfDigestId; |
|
220 |
try { |
|
221 |
mgfDigestId = AlgorithmId.get(mgfSpec.getDigestAlgorithm()); |
|
222 |
} catch (NoSuchAlgorithmException nase) { |
|
223 |
throw new IOException("AlgorithmId " + |
|
224 |
mgfSpec.getDigestAlgorithm() + " impl not found"); |
|
225 |
} |
|
226 |
mgfDigestId.encode(tmp2); |
|
227 |
tmp3 = new DerOutputStream(); |
|
228 |
tmp3.write(DerValue.tag_Sequence, tmp2); |
|
229 |
tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)1), |
|
230 |
tmp3); |
|
231 |
||
232 |
// PSource |
|
233 |
tmp2 = new DerOutputStream(); |
|
234 |
tmp2.putOID(OID_PSpecified); |
|
235 |
tmp2.putOctetString(p); |
|
236 |
tmp3 = new DerOutputStream(); |
|
237 |
tmp3.write(DerValue.tag_Sequence, tmp2); |
|
238 |
tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)2), |
|
239 |
tmp3); |
|
240 |
||
241 |
// Put all together under a SEQUENCE tag |
|
242 |
DerOutputStream out = new DerOutputStream(); |
|
243 |
out.write(DerValue.tag_Sequence, tmp); |
|
244 |
return out.toByteArray(); |
|
245 |
} |
|
246 |
||
247 |
protected byte[] engineGetEncoded(String encodingMethod) |
|
248 |
throws IOException { |
|
249 |
if ((encodingMethod != null) && |
|
250 |
(!encodingMethod.equalsIgnoreCase("ASN.1"))) { |
|
251 |
throw new IllegalArgumentException("Only support ASN.1 format"); |
|
252 |
} |
|
253 |
return engineGetEncoded(); |
|
254 |
} |
|
255 |
||
256 |
protected String engineToString() { |
|
257 |
StringBuffer sb = new StringBuffer(); |
|
258 |
sb.append("MD: " + mdName + "\n"); |
|
259 |
sb.append("MGF: MGF1" + mgfSpec.getDigestAlgorithm() + "\n"); |
|
260 |
sb.append("PSource: PSpecified " + |
|
261 |
(p.length==0? "":Debug.toHexString(new BigInteger(p))) + "\n"); |
|
262 |
return sb.toString(); |
|
263 |
} |
|
264 |
} |