123
124 /*
125 * The original content is "deep copied" in its serialized format
126 * and stored in a byte array. The signature field is also in the
127 * form of byte array.
128 */
129
130 private byte[] content;
131 private byte[] signature;
132 private String thealgorithm;
133
134 /**
135 * Constructs a SignedObject from any Serializable object.
136 * The given object is signed with the given signing key, using the
137 * designated signature engine.
138 *
139 * @param object the object to be signed.
140 * @param signingKey the private key for signing.
141 * @param signingEngine the signature signing engine.
142 *
143 * @exception IOException if an error occurs during serialization
144 * @exception InvalidKeyException if the key is invalid.
145 * @exception SignatureException if signing fails.
146 */
147 public SignedObject(Serializable object, PrivateKey signingKey,
148 Signature signingEngine)
149 throws IOException, InvalidKeyException, SignatureException {
150 // creating a stream pipe-line, from a to b
151 ByteArrayOutputStream b = new ByteArrayOutputStream();
152 ObjectOutput a = new ObjectOutputStream(b);
153
154 // write and flush the object content to byte array
155 a.writeObject(object);
156 a.flush();
157 a.close();
158 this.content = b.toByteArray();
159 b.close();
160
161 // now sign the encapsulated object
162 this.sign(signingKey, signingEngine);
163 }
164
165 /**
166 * Retrieves the encapsulated object.
167 * The encapsulated object is de-serialized before it is returned.
168 *
169 * @return the encapsulated object.
170 *
171 * @exception IOException if an error occurs during de-serialization
172 * @exception ClassNotFoundException if an error occurs during
173 * de-serialization
174 */
175 public Object getObject()
176 throws IOException, ClassNotFoundException
177 {
178 // creating a stream pipe-line, from b to a
179 ByteArrayInputStream b = new ByteArrayInputStream(this.content);
180 ObjectInput a = new ObjectInputStream(b);
181 Object obj = a.readObject();
182 b.close();
183 a.close();
184 return obj;
185 }
186
187 /**
188 * Retrieves the signature on the signed object, in the form of a
189 * byte array.
190 *
191 * @return the signature. Returns a new array each time this
192 * method is called.
195 return this.signature.clone();
196 }
197
198 /**
199 * Retrieves the name of the signature algorithm.
200 *
201 * @return the signature algorithm name.
202 */
203 public String getAlgorithm() {
204 return this.thealgorithm;
205 }
206
207 /**
208 * Verifies that the signature in this SignedObject is the valid
209 * signature for the object stored inside, with the given
210 * verification key, using the designated verification engine.
211 *
212 * @param verificationKey the public key for verification.
213 * @param verificationEngine the signature verification engine.
214 *
215 * @exception SignatureException if signature verification failed (an
216 * exception prevented the signature verification engine from completing
217 * normally).
218 * @exception InvalidKeyException if the verification key is invalid.
219 *
220 * @return {@code true} if the signature
221 * is valid, {@code false} otherwise
222 */
223 public boolean verify(PublicKey verificationKey,
224 Signature verificationEngine)
225 throws InvalidKeyException, SignatureException {
226 verificationEngine.initVerify(verificationKey);
227 verificationEngine.update(this.content.clone());
228 return verificationEngine.verify(this.signature.clone());
229 }
230
231 /*
232 * Signs the encapsulated object with the given signing key, using the
233 * designated signature engine.
234 *
235 * @param signingKey the private key for signing.
236 * @param signingEngine the signature signing engine.
237 *
238 * @exception InvalidKeyException if the key is invalid.
239 * @exception SignatureException if signing fails.
240 */
241 private void sign(PrivateKey signingKey, Signature signingEngine)
242 throws InvalidKeyException, SignatureException {
243 // initialize the signing engine
244 signingEngine.initSign(signingKey);
245 signingEngine.update(this.content.clone());
246 this.signature = signingEngine.sign().clone();
247 this.thealgorithm = signingEngine.getAlgorithm();
248 }
249
250 /**
251 * readObject is called to restore the state of the SignedObject from
252 * a stream.
253 */
254 @java.io.Serial
255 private void readObject(java.io.ObjectInputStream s)
256 throws java.io.IOException, ClassNotFoundException {
257 java.io.ObjectInputStream.GetField fields = s.readFields();
258 content = ((byte[])fields.get("content", null)).clone();
259 signature = ((byte[])fields.get("signature", null)).clone();
|
123
124 /*
125 * The original content is "deep copied" in its serialized format
126 * and stored in a byte array. The signature field is also in the
127 * form of byte array.
128 */
129
130 private byte[] content;
131 private byte[] signature;
132 private String thealgorithm;
133
134 /**
135 * Constructs a SignedObject from any Serializable object.
136 * The given object is signed with the given signing key, using the
137 * designated signature engine.
138 *
139 * @param object the object to be signed.
140 * @param signingKey the private key for signing.
141 * @param signingEngine the signature signing engine.
142 *
143 * @throws IOException if an error occurs during serialization
144 * @throws InvalidKeyException if the key is invalid.
145 * @throws SignatureException if signing fails.
146 */
147 public SignedObject(Serializable object, PrivateKey signingKey,
148 Signature signingEngine)
149 throws IOException, InvalidKeyException, SignatureException {
150 // creating a stream pipe-line, from a to b
151 ByteArrayOutputStream b = new ByteArrayOutputStream();
152 ObjectOutput a = new ObjectOutputStream(b);
153
154 // write and flush the object content to byte array
155 a.writeObject(object);
156 a.flush();
157 a.close();
158 this.content = b.toByteArray();
159 b.close();
160
161 // now sign the encapsulated object
162 this.sign(signingKey, signingEngine);
163 }
164
165 /**
166 * Retrieves the encapsulated object.
167 * The encapsulated object is de-serialized before it is returned.
168 *
169 * @return the encapsulated object.
170 *
171 * @throws IOException if an error occurs during de-serialization
172 * @throws ClassNotFoundException if an error occurs during
173 * de-serialization
174 */
175 public Object getObject()
176 throws IOException, ClassNotFoundException
177 {
178 // creating a stream pipe-line, from b to a
179 ByteArrayInputStream b = new ByteArrayInputStream(this.content);
180 ObjectInput a = new ObjectInputStream(b);
181 Object obj = a.readObject();
182 b.close();
183 a.close();
184 return obj;
185 }
186
187 /**
188 * Retrieves the signature on the signed object, in the form of a
189 * byte array.
190 *
191 * @return the signature. Returns a new array each time this
192 * method is called.
195 return this.signature.clone();
196 }
197
198 /**
199 * Retrieves the name of the signature algorithm.
200 *
201 * @return the signature algorithm name.
202 */
203 public String getAlgorithm() {
204 return this.thealgorithm;
205 }
206
207 /**
208 * Verifies that the signature in this SignedObject is the valid
209 * signature for the object stored inside, with the given
210 * verification key, using the designated verification engine.
211 *
212 * @param verificationKey the public key for verification.
213 * @param verificationEngine the signature verification engine.
214 *
215 * @throws SignatureException if signature verification failed (an
216 * exception prevented the signature verification engine from completing
217 * normally).
218 * @throws InvalidKeyException if the verification key is invalid.
219 *
220 * @return {@code true} if the signature
221 * is valid, {@code false} otherwise
222 */
223 public boolean verify(PublicKey verificationKey,
224 Signature verificationEngine)
225 throws InvalidKeyException, SignatureException {
226 verificationEngine.initVerify(verificationKey);
227 verificationEngine.update(this.content.clone());
228 return verificationEngine.verify(this.signature.clone());
229 }
230
231 /*
232 * Signs the encapsulated object with the given signing key, using the
233 * designated signature engine.
234 *
235 * @param signingKey the private key for signing.
236 * @param signingEngine the signature signing engine.
237 *
238 * @throws InvalidKeyException if the key is invalid.
239 * @throws SignatureException if signing fails.
240 */
241 private void sign(PrivateKey signingKey, Signature signingEngine)
242 throws InvalidKeyException, SignatureException {
243 // initialize the signing engine
244 signingEngine.initSign(signingKey);
245 signingEngine.update(this.content.clone());
246 this.signature = signingEngine.sign().clone();
247 this.thealgorithm = signingEngine.getAlgorithm();
248 }
249
250 /**
251 * readObject is called to restore the state of the SignedObject from
252 * a stream.
253 */
254 @java.io.Serial
255 private void readObject(java.io.ObjectInputStream s)
256 throws java.io.IOException, ClassNotFoundException {
257 java.io.ObjectInputStream.GetField fields = s.readFields();
258 content = ((byte[])fields.get("content", null)).clone();
259 signature = ((byte[])fields.get("signature", null)).clone();
|