author | coleenp |
Wed, 30 Aug 2017 19:18:22 -0400 | |
changeset 47098 | e704f55561c3 |
parent 46053 | 6f7e93cb432a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
46053
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
2 |
* Copyright (c) 1997, 2017, 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 java.security; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
||
30 |
/** |
|
31 |
* <p> SignedObject is a class for the purpose of creating authentic |
|
32 |
* runtime objects whose integrity cannot be compromised without being |
|
33 |
* detected. |
|
34 |
* |
|
35 |
* <p> More specifically, a SignedObject contains another Serializable |
|
36 |
* object, the (to-be-)signed object and its signature. |
|
37 |
* |
|
38 |
* <p> The signed object is a "deep copy" (in serialized form) of an |
|
39 |
* original object. Once the copy is made, further manipulation of |
|
40 |
* the original object has no side effect on the copy. |
|
41 |
* |
|
42 |
* <p> The underlying signing algorithm is designated by the Signature |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
43 |
* object passed to the constructor and the {@code verify} method. |
2 | 44 |
* A typical usage for signing is the following: |
45 |
* |
|
21334 | 46 |
* <pre>{@code |
2 | 47 |
* Signature signingEngine = Signature.getInstance(algorithm, |
48 |
* provider); |
|
49 |
* SignedObject so = new SignedObject(myobject, signingKey, |
|
50 |
* signingEngine); |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
51 |
* }</pre> |
2 | 52 |
* |
53 |
* <p> A typical usage for verification is the following (having |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
54 |
* received SignedObject {@code so}): |
2 | 55 |
* |
21334 | 56 |
* <pre>{@code |
2 | 57 |
* Signature verificationEngine = |
58 |
* Signature.getInstance(algorithm, provider); |
|
59 |
* if (so.verify(publickey, verificationEngine)) |
|
60 |
* try { |
|
61 |
* Object myobj = so.getObject(); |
|
62 |
* } catch (java.lang.ClassNotFoundException e) {}; |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
63 |
* }</pre> |
2 | 64 |
* |
65 |
* <p> Several points are worth noting. First, there is no need to |
|
66 |
* initialize the signing or verification engine, as it will be |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
67 |
* re-initialized inside the constructor and the {@code verify} |
2 | 68 |
* method. Secondly, for verification to succeed, the specified |
69 |
* public key must be the public key corresponding to the private key |
|
70 |
* used to generate the SignedObject. |
|
71 |
* |
|
72 |
* <p> More importantly, for flexibility reasons, the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
73 |
* constructor and {@code verify} method allow for |
2 | 74 |
* customized signature engines, which can implement signature |
75 |
* algorithms that are not installed formally as part of a crypto |
|
76 |
* provider. However, it is crucial that the programmer writing the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
77 |
* verifier code be aware what {@code Signature} engine is being |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
78 |
* used, as its own implementation of the {@code verify} method |
2 | 79 |
* is invoked to verify a signature. In other words, a malicious |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
80 |
* {@code Signature} may choose to always return true on |
2 | 81 |
* verification in an attempt to bypass a security check. |
82 |
* |
|
83 |
* <p> The signature algorithm can be, among others, the NIST standard |
|
46053
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
84 |
* DSA, using DSA and SHA-256. The algorithm is specified using the |
2 | 85 |
* same convention as that for signatures. The DSA algorithm using the |
46053
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
86 |
* SHA-256 message digest algorithm can be specified, for example, as |
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
87 |
* "SHA256withDSA". In the case of |
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
88 |
* RSA the signing algorithm could be specified as, for example, |
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
89 |
* "SHA256withRSA". The algorithm name must be |
2 | 90 |
* specified, as there is no default. |
91 |
* |
|
92 |
* <p> The name of the Cryptography Package Provider is designated |
|
93 |
* also by the Signature parameter to the constructor and the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
94 |
* {@code verify} method. If the provider is not |
2 | 95 |
* specified, the default provider is used. Each installation can |
96 |
* be configured to use a particular provider as default. |
|
97 |
* |
|
98 |
* <p> Potential applications of SignedObject include: |
|
99 |
* <ul> |
|
100 |
* <li> It can be used |
|
101 |
* internally to any Java runtime as an unforgeable authorization |
|
102 |
* token -- one that can be passed around without the fear that the |
|
103 |
* token can be maliciously modified without being detected. |
|
104 |
* <li> It |
|
105 |
* can be used to sign and serialize data/object for storage outside |
|
106 |
* the Java runtime (e.g., storing critical access control data on |
|
107 |
* disk). |
|
108 |
* <li> Nested SignedObjects can be used to construct a logical |
|
109 |
* sequence of signatures, resembling a chain of authorization and |
|
110 |
* delegation. |
|
111 |
* </ul> |
|
112 |
* |
|
113 |
* @see Signature |
|
114 |
* |
|
115 |
* @author Li Gong |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
25859
diff
changeset
|
116 |
* @since 1.2 |
2 | 117 |
*/ |
118 |
||
119 |
public final class SignedObject implements Serializable { |
|
120 |
||
121 |
private static final long serialVersionUID = 720502720485447167L; |
|
122 |
||
123 |
/* |
|
124 |
* The original content is "deep copied" in its serialized format |
|
125 |
* and stored in a byte array. The signature field is also in the |
|
126 |
* form of byte array. |
|
127 |
*/ |
|
128 |
||
129 |
private byte[] content; |
|
130 |
private byte[] signature; |
|
131 |
private String thealgorithm; |
|
132 |
||
133 |
/** |
|
134 |
* Constructs a SignedObject from any Serializable object. |
|
135 |
* The given object is signed with the given signing key, using the |
|
136 |
* designated signature engine. |
|
137 |
* |
|
138 |
* @param object the object to be signed. |
|
139 |
* @param signingKey the private key for signing. |
|
140 |
* @param signingEngine the signature signing engine. |
|
141 |
* |
|
142 |
* @exception IOException if an error occurs during serialization |
|
143 |
* @exception InvalidKeyException if the key is invalid. |
|
144 |
* @exception SignatureException if signing fails. |
|
145 |
*/ |
|
146 |
public SignedObject(Serializable object, PrivateKey signingKey, |
|
147 |
Signature signingEngine) |
|
148 |
throws IOException, InvalidKeyException, SignatureException { |
|
149 |
// creating a stream pipe-line, from a to b |
|
150 |
ByteArrayOutputStream b = new ByteArrayOutputStream(); |
|
151 |
ObjectOutput a = new ObjectOutputStream(b); |
|
152 |
||
153 |
// write and flush the object content to byte array |
|
154 |
a.writeObject(object); |
|
155 |
a.flush(); |
|
156 |
a.close(); |
|
157 |
this.content = b.toByteArray(); |
|
158 |
b.close(); |
|
159 |
||
160 |
// now sign the encapsulated object |
|
161 |
this.sign(signingKey, signingEngine); |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Retrieves the encapsulated object. |
|
166 |
* The encapsulated object is de-serialized before it is returned. |
|
167 |
* |
|
168 |
* @return the encapsulated object. |
|
169 |
* |
|
170 |
* @exception IOException if an error occurs during de-serialization |
|
171 |
* @exception ClassNotFoundException if an error occurs during |
|
172 |
* de-serialization |
|
173 |
*/ |
|
174 |
public Object getObject() |
|
175 |
throws IOException, ClassNotFoundException |
|
176 |
{ |
|
177 |
// creating a stream pipe-line, from b to a |
|
178 |
ByteArrayInputStream b = new ByteArrayInputStream(this.content); |
|
179 |
ObjectInput a = new ObjectInputStream(b); |
|
180 |
Object obj = a.readObject(); |
|
181 |
b.close(); |
|
182 |
a.close(); |
|
183 |
return obj; |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* Retrieves the signature on the signed object, in the form of a |
|
188 |
* byte array. |
|
189 |
* |
|
190 |
* @return the signature. Returns a new array each time this |
|
191 |
* method is called. |
|
192 |
*/ |
|
193 |
public byte[] getSignature() { |
|
194 |
return this.signature.clone(); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Retrieves the name of the signature algorithm. |
|
199 |
* |
|
200 |
* @return the signature algorithm name. |
|
201 |
*/ |
|
202 |
public String getAlgorithm() { |
|
203 |
return this.thealgorithm; |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* Verifies that the signature in this SignedObject is the valid |
|
208 |
* signature for the object stored inside, with the given |
|
209 |
* verification key, using the designated verification engine. |
|
210 |
* |
|
211 |
* @param verificationKey the public key for verification. |
|
212 |
* @param verificationEngine the signature verification engine. |
|
213 |
* |
|
25524
e623ca817d50
4867890: Clarify the return value/exception for java.security.SignedObject.verify
mullan
parents:
21334
diff
changeset
|
214 |
* @exception SignatureException if signature verification failed (an |
e623ca817d50
4867890: Clarify the return value/exception for java.security.SignedObject.verify
mullan
parents:
21334
diff
changeset
|
215 |
* exception prevented the signature verification engine from completing |
e623ca817d50
4867890: Clarify the return value/exception for java.security.SignedObject.verify
mullan
parents:
21334
diff
changeset
|
216 |
* normally). |
2 | 217 |
* @exception InvalidKeyException if the verification key is invalid. |
218 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
219 |
* @return {@code true} if the signature |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9826
diff
changeset
|
220 |
* is valid, {@code false} otherwise |
2 | 221 |
*/ |
222 |
public boolean verify(PublicKey verificationKey, |
|
223 |
Signature verificationEngine) |
|
224 |
throws InvalidKeyException, SignatureException { |
|
225 |
verificationEngine.initVerify(verificationKey); |
|
226 |
verificationEngine.update(this.content.clone()); |
|
227 |
return verificationEngine.verify(this.signature.clone()); |
|
228 |
} |
|
229 |
||
230 |
/* |
|
231 |
* Signs the encapsulated object with the given signing key, using the |
|
232 |
* designated signature engine. |
|
233 |
* |
|
234 |
* @param signingKey the private key for signing. |
|
235 |
* @param signingEngine the signature signing engine. |
|
236 |
* |
|
237 |
* @exception InvalidKeyException if the key is invalid. |
|
238 |
* @exception SignatureException if signing fails. |
|
239 |
*/ |
|
240 |
private void sign(PrivateKey signingKey, Signature signingEngine) |
|
241 |
throws InvalidKeyException, SignatureException { |
|
242 |
// initialize the signing engine |
|
243 |
signingEngine.initSign(signingKey); |
|
244 |
signingEngine.update(this.content.clone()); |
|
245 |
this.signature = signingEngine.sign().clone(); |
|
246 |
this.thealgorithm = signingEngine.getAlgorithm(); |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* readObject is called to restore the state of the SignedObject from |
|
251 |
* a stream. |
|
252 |
*/ |
|
253 |
private void readObject(java.io.ObjectInputStream s) |
|
9826
0f553990ca93
6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
5506
diff
changeset
|
254 |
throws java.io.IOException, ClassNotFoundException { |
0f553990ca93
6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
5506
diff
changeset
|
255 |
java.io.ObjectInputStream.GetField fields = s.readFields(); |
0f553990ca93
6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
5506
diff
changeset
|
256 |
content = ((byte[])fields.get("content", null)).clone(); |
0f553990ca93
6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
5506
diff
changeset
|
257 |
signature = ((byte[])fields.get("signature", null)).clone(); |
0f553990ca93
6618658: Deserialization allows creation of mutable SignedObject
weijun
parents:
5506
diff
changeset
|
258 |
thealgorithm = (String)fields.get("thealgorithm", null); |
2 | 259 |
} |
260 |
} |