| 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 <[email protected]>
|
|---|
| 77 |
|
|---|
| 78 | @since JDK 1.2
|
|---|
| 79 | */
|
|---|
| 80 | public final class SignedObject implements Serializable
|
|---|
| 81 | {
|
|---|
| 82 | private byte[] content;
|
|---|
| 83 | private byte[] signature;
|
|---|
| 84 | private String thealgorithm;
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | Constructs a new SignedObject from a Serializeable object. The
|
|---|
| 88 | object is signed with private key and signature engine
|
|---|
| 89 |
|
|---|
| 90 | @param object the object to sign
|
|---|
| 91 | @param signingKey the key to sign with
|
|---|
| 92 | @param signingEngine the signature engine to use
|
|---|
| 93 |
|
|---|
| 94 | @throws IOException serialization error occurred
|
|---|
| 95 | @throws InvalidKeyException invalid key
|
|---|
| 96 | @throws SignatureException signing error
|
|---|
| 97 | */
|
|---|
| 98 | public SignedObject(Serializable object, PrivateKey signingKey,
|
|---|
| 99 | Signature signingEngine) throws IOException,
|
|---|
| 100 | InvalidKeyException, SignatureException
|
|---|
| 101 | {
|
|---|
| 102 | thealgorithm = signingEngine.getAlgorithm();
|
|---|
| 103 |
|
|---|
| 104 | ByteArrayOutputStream ostream = new ByteArrayOutputStream();
|
|---|
| 105 | ObjectOutputStream p = new ObjectOutputStream(ostream);
|
|---|
| 106 | p.writeObject(object);
|
|---|
| 107 | p.flush();
|
|---|
| 108 |
|
|---|
| 109 | content = ostream.toByteArray();
|
|---|
| 110 |
|
|---|
| 111 | signingEngine.initSign(signingKey);
|
|---|
| 112 | signingEngine.update(content);
|
|---|
| 113 | signature = signingEngine.sign();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | Returns the encapsulated object. The object is
|
|---|
| 118 | de-serialized before being returned.
|
|---|
| 119 |
|
|---|
| 120 | @return the encapsulated object
|
|---|
| 121 |
|
|---|
| 122 | @throws IOException de-serialization error occurred
|
|---|
| 123 | @throws ClassNotFoundException de-serialization error occurred
|
|---|
| 124 | */
|
|---|
| 125 | public Object getObject() throws IOException, ClassNotFoundException
|
|---|
| 126 | {
|
|---|
| 127 | ByteArrayInputStream istream = new ByteArrayInputStream(content);
|
|---|
| 128 |
|
|---|
| 129 | return new ObjectInputStream(istream).readObject();
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | Returns the signature of the encapsulated object.
|
|---|
| 134 |
|
|---|
| 135 | @return a byte array containing the signature
|
|---|
| 136 | */
|
|---|
| 137 | public byte[] getSignature()
|
|---|
| 138 | {
|
|---|
| 139 | return signature;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | Returns the name of the signature algorithm.
|
|---|
| 144 |
|
|---|
| 145 | @return the name of the signature algorithm.
|
|---|
| 146 | */
|
|---|
| 147 | public String getAlgorithm()
|
|---|
| 148 | {
|
|---|
| 149 | return thealgorithm;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | Verifies the SignedObject by checking that the signature that
|
|---|
| 154 | this class contains for the encapsulated object.
|
|---|
| 155 |
|
|---|
| 156 | @param verificationKey the public key to use
|
|---|
| 157 | @param verificationEngine the signature engine to use
|
|---|
| 158 |
|
|---|
| 159 | @return true if signature is correct, false otherwise
|
|---|
| 160 |
|
|---|
| 161 | @throws InvalidKeyException invalid key
|
|---|
| 162 | @throws SignatureException signature verification failed
|
|---|
| 163 | */
|
|---|
| 164 | public boolean verify(PublicKey verificationKey,
|
|---|
| 165 | Signature verificationEngine) throws
|
|---|
| 166 | InvalidKeyException, SignatureException
|
|---|
| 167 | {
|
|---|
| 168 | verificationEngine.initVerify(verificationKey);
|
|---|
| 169 | verificationEngine.update(content);
|
|---|
| 170 | return verificationEngine.verify(signature);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | // readObject is called to restore the state of the SignedObject from a
|
|---|
| 174 | // stream.
|
|---|
| 175 | //private void readObject(ObjectInputStream s)
|
|---|
| 176 | // throws IOException, ClassNotFoundException
|
|---|
| 177 | }
|
|---|