author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
child 14405 | e7fff80005c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1998, 2011, 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.io.*; |
|
29 |
import java.math.BigInteger; |
|
30 |
import java.security.AlgorithmParametersSpi; |
|
31 |
import java.security.spec.AlgorithmParameterSpec; |
|
32 |
import java.security.spec.InvalidParameterSpecException; |
|
33 |
import javax.crypto.spec.PBEParameterSpec; |
|
34 |
import sun.misc.HexDumpEncoder; |
|
35 |
import sun.security.util.*; |
|
36 |
||
37 |
||
38 |
/** |
|
39 |
* This class implements the parameter set used with password-based |
|
40 |
* encryption, which is defined in PKCS#5 as follows: |
|
41 |
* |
|
42 |
* <pre> |
|
43 |
* PBEParameter ::= SEQUENCE { |
|
44 |
* salt OCTET STRING SIZE(8), |
|
45 |
* iterationCount INTEGER } |
|
46 |
* </pre> |
|
47 |
* |
|
48 |
* @author Jan Luehe |
|
49 |
* |
|
50 |
*/ |
|
51 |
||
52 |
public final class PBEParameters extends AlgorithmParametersSpi { |
|
53 |
||
54 |
// the salt |
|
55 |
private byte[] salt = null; |
|
56 |
||
57 |
// the iteration count |
|
58 |
private int iCount = 0; |
|
59 |
||
60 |
protected void engineInit(AlgorithmParameterSpec paramSpec) |
|
61 |
throws InvalidParameterSpecException |
|
62 |
{ |
|
63 |
if (!(paramSpec instanceof PBEParameterSpec)) { |
|
64 |
throw new InvalidParameterSpecException |
|
65 |
("Inappropriate parameter specification"); |
|
66 |
} |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
67 |
this.salt = ((PBEParameterSpec)paramSpec).getSalt().clone(); |
2 | 68 |
this.iCount = ((PBEParameterSpec)paramSpec).getIterationCount(); |
69 |
} |
|
70 |
||
71 |
protected void engineInit(byte[] encoded) |
|
72 |
throws IOException |
|
73 |
{ |
|
74 |
try { |
|
75 |
DerValue val = new DerValue(encoded); |
|
76 |
if (val.tag != DerValue.tag_Sequence) { |
|
77 |
throw new IOException("PBE parameter parsing error: " |
|
78 |
+ "not a sequence"); |
|
79 |
} |
|
80 |
val.data.reset(); |
|
81 |
||
82 |
this.salt = val.data.getOctetString(); |
|
83 |
this.iCount = val.data.getInteger(); |
|
84 |
||
85 |
if (val.data.available() != 0) { |
|
86 |
throw new IOException |
|
87 |
("PBE parameter parsing error: extra data"); |
|
88 |
} |
|
89 |
} catch (NumberFormatException e) { |
|
90 |
throw new IOException("iteration count too big"); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
protected void engineInit(byte[] encoded, String decodingMethod) |
|
95 |
throws IOException |
|
96 |
{ |
|
97 |
engineInit(encoded); |
|
98 |
} |
|
99 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
100 |
protected <T extends AlgorithmParameterSpec> |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
101 |
T engineGetParameterSpec(Class<T> paramSpec) |
2 | 102 |
throws InvalidParameterSpecException |
103 |
{ |
|
104 |
if (PBEParameterSpec.class.isAssignableFrom(paramSpec)) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
105 |
return paramSpec.cast(new PBEParameterSpec(this.salt, this.iCount)); |
2 | 106 |
} else { |
107 |
throw new InvalidParameterSpecException |
|
108 |
("Inappropriate parameter specification"); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
protected byte[] engineGetEncoded() throws IOException { |
|
113 |
DerOutputStream out = new DerOutputStream(); |
|
114 |
DerOutputStream bytes = new DerOutputStream(); |
|
115 |
||
116 |
bytes.putOctetString(this.salt); |
|
117 |
bytes.putInteger(this.iCount); |
|
118 |
||
119 |
out.write(DerValue.tag_Sequence, bytes); |
|
120 |
return out.toByteArray(); |
|
121 |
} |
|
122 |
||
123 |
protected byte[] engineGetEncoded(String encodingMethod) |
|
124 |
throws IOException |
|
125 |
{ |
|
126 |
return engineGetEncoded(); |
|
127 |
} |
|
128 |
||
129 |
/* |
|
130 |
* Returns a formatted string describing the parameters. |
|
131 |
*/ |
|
132 |
protected String engineToString() { |
|
133 |
String LINE_SEP = System.getProperty("line.separator"); |
|
134 |
String saltString = LINE_SEP + " salt:" + LINE_SEP + "["; |
|
135 |
HexDumpEncoder encoder = new HexDumpEncoder(); |
|
136 |
saltString += encoder.encodeBuffer(salt); |
|
137 |
saltString += "]"; |
|
138 |
||
139 |
return saltString + LINE_SEP + " iterationCount:" |
|
140 |
+ LINE_SEP + Debug.toHexString(BigInteger.valueOf(iCount)) |
|
141 |
+ LINE_SEP; |
|
142 |
} |
|
143 |
} |