1 | /* SignedObject.java --- Signed Object Class
|
---|
2 | Copyright (C) 1999 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package java.security;
|
---|
39 | import java.io.ByteArrayInputStream;
|
---|
40 | import java.io.ByteArrayOutputStream;
|
---|
41 | import java.io.IOException;
|
---|
42 | import java.io.ObjectInputStream;
|
---|
43 | import java.io.ObjectOutputStream;
|
---|
44 | import java.io.Serializable;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | SignedObject is used for storing rutime objects whose integrity
|
---|
48 | cannot be compromised without being detected.
|
---|
49 |
|
---|
50 | SignedObject contains a Serializable object which is yet to be
|
---|
51 | signed and its signature.
|
---|
52 |
|
---|
53 | The signed copy is a "deep copy" (in serialized form) of the
|
---|
54 | original object. Any changes to the original will not affect
|
---|
55 | the original.
|
---|
56 |
|
---|
57 | Several things to note are that, first there is no need to
|
---|
58 | initialize the signature engine as this class will handle that
|
---|
59 | automatically. Second, verification will only succeed if the
|
---|
60 | public key corresponds to the private key used to generate
|
---|
61 | the SignedObject.
|
---|
62 |
|
---|
63 | For fexibility, the signature engine can be specified in the
|
---|
64 | constructor or the verify method. The programmer who writes
|
---|
65 | code that verifies the SignedObject has not changed should be
|
---|
66 | aware of the Signature engine they use. A malicious Signature
|
---|
67 | may choose to always return true on verification and
|
---|
68 | bypass the secrity check.
|
---|
69 |
|
---|
70 | The GNU provider provides the NIST standard DSA which uses DSA
|
---|
71 | and SHA-1. It can be specified by SHA/DSA, SHA-1/DSA or its
|
---|
72 | OID. If the RSA signature algorithm is provided then
|
---|
73 | it could be MD2/RSA. MD5/RSA, or SHA-1/RSA. The algorithm must
|
---|
74 | be specified because there is no default.
|
---|
75 |
|
---|
76 | @author Mark Benvenuto <ivymccough@worldnet.att.net>
|
---|
77 |
|
---|
78 | @since JDK 1.2
|
---|
79 | */
|
---|
80 | public final class SignedObject implements Serializable
|
---|
81 | {
|
---|
82 | static final long serialVersionUID = 720502720485447167L;
|
---|
83 |
|
---|
84 | private byte[] content;
|
---|
85 | private byte[] signature;
|
---|
86 | private String thealgorithm;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | Constructs a new SignedObject from a Serializeable object. The
|
---|
90 | object is signed with private key and signature engine
|
---|
91 |
|
---|
92 | @param object the object to sign
|
---|
93 | @param signingKey the key to sign with
|
---|
94 | @param signingEngine the signature engine to use
|
---|
95 |
|
---|
96 | @throws IOException serialization error occurred
|
---|
97 | @throws InvalidKeyException invalid key
|
---|
98 | @throws SignatureException signing error
|
---|
99 | */
|
---|
100 | public SignedObject(Serializable object, PrivateKey signingKey,
|
---|
101 | Signature signingEngine) throws IOException,
|
---|
102 | InvalidKeyException, SignatureException
|
---|
103 | {
|
---|
104 | thealgorithm = signingEngine.getAlgorithm();
|
---|
105 |
|
---|
106 | ByteArrayOutputStream ostream = new ByteArrayOutputStream();
|
---|
107 | ObjectOutputStream p = new ObjectOutputStream(ostream);
|
---|
108 | p.writeObject(object);
|
---|
109 | p.flush();
|
---|
110 |
|
---|
111 | content = ostream.toByteArray();
|
---|
112 |
|
---|
113 | signingEngine.initSign(signingKey);
|
---|
114 | signingEngine.update(content);
|
---|
115 | signature = signingEngine.sign();
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | Returns the encapsulated object. The object is
|
---|
120 | de-serialized before being returned.
|
---|
121 |
|
---|
122 | @return the encapsulated object
|
---|
123 |
|
---|
124 | @throws IOException de-serialization error occurred
|
---|
125 | @throws ClassNotFoundException de-serialization error occurred
|
---|
126 | */
|
---|
127 | public Object getObject() throws IOException, ClassNotFoundException
|
---|
128 | {
|
---|
129 | ByteArrayInputStream istream = new ByteArrayInputStream(content);
|
---|
130 |
|
---|
131 | return new ObjectInputStream(istream).readObject();
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | Returns the signature of the encapsulated object.
|
---|
136 |
|
---|
137 | @return a byte array containing the signature
|
---|
138 | */
|
---|
139 | public byte[] getSignature()
|
---|
140 | {
|
---|
141 | return signature;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | Returns the name of the signature algorithm.
|
---|
146 |
|
---|
147 | @return the name of the signature algorithm.
|
---|
148 | */
|
---|
149 | public String getAlgorithm()
|
---|
150 | {
|
---|
151 | return thealgorithm;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | Verifies the SignedObject by checking that the signature that
|
---|
156 | this class contains for the encapsulated object.
|
---|
157 |
|
---|
158 | @param verificationKey the public key to use
|
---|
159 | @param verificationEngine the signature engine to use
|
---|
160 |
|
---|
161 | @return true if signature is correct, false otherwise
|
---|
162 |
|
---|
163 | @throws InvalidKeyException invalid key
|
---|
164 | @throws SignatureException signature verification failed
|
---|
165 | */
|
---|
166 | public boolean verify(PublicKey verificationKey,
|
---|
167 | Signature verificationEngine) throws
|
---|
168 | InvalidKeyException, SignatureException
|
---|
169 | {
|
---|
170 | verificationEngine.initVerify(verificationKey);
|
---|
171 | verificationEngine.update(content);
|
---|
172 | return verificationEngine.verify(signature);
|
---|
173 | }
|
---|
174 |
|
---|
175 | // readObject is called to restore the state of the SignedObject from a
|
---|
176 | // stream.
|
---|
177 | //private void readObject(ObjectInputStream s)
|
---|
178 | // throws IOException, ClassNotFoundException
|
---|
179 | }
|
---|