001 /* SignedObject.java --- Signed Object Class 002 Copyright (C) 1999, 2003, Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package java.security; 039 040 import java.io.ByteArrayInputStream; 041 import java.io.ByteArrayOutputStream; 042 import java.io.IOException; 043 import java.io.ObjectInput; 044 import java.io.ObjectInputStream; 045 import java.io.ObjectOutputStream; 046 import java.io.Serializable; 047 048 /** 049 * <code>SignedObject</code> is used for storing runtime objects whose 050 * integrity cannot be compromised without being detected. 051 * 052 * <p><code>SignedObject</code> contains a {@link Serializable} object which is 053 * yet to be signed and a digital signature of that object.</p> 054 * 055 * <p>The signed copy is a "deep copy" (in serialized form) of an original 056 * object. Any changes to that original instance are not reflected in the 057 * enclosed copy inside this <code>SignedObject</code>.</p> 058 * 059 * <p>Several things to note are that, first there is no need to initialize the 060 * signature engine as this class will handle that automatically. Second, 061 * verification will only succeed if the public key corresponds to the private 062 * key used to generate the digital signature inside this 063 * <code>SignedObject</code>.</p> 064 * 065 * <p>For fexibility, the signature engine can be specified in the constructor 066 * or the <code>verify()</code> method. Programmers wishing to verify 067 * <code>SignedObject</code>s should be aware of the {@link Signature} engine 068 * they use. A malicious or flawed {@link Signature} implementation may always 069 * return true on verification thus circumventing the intended secrity check 070 * provided by the <code>SignedObject</code>.</p> 071 * 072 * <p>The GNU security provider offers an implementation of the standard NIST 073 * DSA which uses "DSA" and "SHA-1". It can be specified by "SHA/DSA", 074 * "SHA-1/DSA" or its OID. If the RSA signature algorithm is provided then it 075 * could be "MD2/RSA". "MD5/RSA", or "SHA-1/RSA". The algorithm must be 076 * specified because there is no default.</p> 077 * 078 * @author Mark Benvenuto (ivymccough@worldnet.att.net) 079 * @since 1.2 080 * @see Signature 081 */ 082 public final class SignedObject implements Serializable 083 { 084 private static final long serialVersionUID = 720502720485447167L; 085 086 /** @serial */ 087 private byte[] content; 088 /** @serial */ 089 private byte[] signature; 090 /** @serial */ 091 private String thealgorithm; 092 093 /** 094 * Constructs a new instance of <code>SignedObject</code> from a 095 * {@link Serializable} object. The object is signed with a designated 096 * private key and a signature engine. 097 * 098 * @param object 099 * the object to sign. 100 * @param signingKey 101 * the key to use. 102 * @param signingEngine 103 * the signature engine to use. 104 * @throws IOException 105 * if a serialization error occurred. 106 * @throws InvalidKeyException 107 * if the key is invalid. 108 * @throws SignatureException 109 * if a signing error occurs. 110 */ 111 public SignedObject(Serializable object, PrivateKey signingKey, 112 Signature signingEngine) 113 throws IOException, InvalidKeyException, SignatureException 114 { 115 thealgorithm = signingEngine.getAlgorithm(); 116 117 ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 118 ObjectOutputStream p = new ObjectOutputStream(ostream); 119 p.writeObject(object); 120 p.flush(); 121 p.close(); 122 123 content = ostream.toByteArray(); 124 125 signingEngine.initSign(signingKey); 126 signingEngine.update(content); 127 signature = signingEngine.sign(); 128 } 129 130 /** 131 * Returns the encapsulated object. The object is de-serialized before being 132 * returned. 133 * 134 * @return the encapsulated object. 135 * @throws IOException 136 * if a de-serialization error occurs. 137 * @throws ClassNotFoundException 138 * if the encapsulated object's class was not found. 139 */ 140 public Object getObject() throws IOException, ClassNotFoundException 141 { 142 ByteArrayInputStream bais = new ByteArrayInputStream(content); 143 ObjectInput oi = new ObjectInputStream(bais); 144 Object obj = oi.readObject(); 145 oi.close(); 146 bais.close(); 147 148 return obj; 149 } 150 151 /** 152 * Returns the signature bytes of the encapsulated object. 153 * 154 * @return the signature bytes of the encapsulated object. 155 */ 156 public byte[] getSignature() 157 { 158 return (byte[]) signature.clone(); 159 160 } 161 162 /** 163 * Returns the name of the signature algorithm. 164 * 165 * @return the name of the signature algorithm. 166 */ 167 public String getAlgorithm() 168 { 169 return thealgorithm; 170 } 171 172 /** 173 * Verifies the encapsulated digital signature by checking that it was 174 * generated by the owner of a designated public key. 175 * 176 * @param verificationKey 177 * the public key to use. 178 * @param verificationEngine 179 * the signature engine to use. 180 * @return <code>true</code> if signature is correct, <code>false</code> 181 * otherwise. 182 * @throws InvalidKeyException 183 * if the key is invalid. 184 * @throws SignatureException 185 * if verification fails. 186 */ 187 public boolean verify(PublicKey verificationKey, Signature verificationEngine) 188 throws InvalidKeyException, SignatureException 189 { 190 verificationEngine.initVerify(verificationKey); 191 verificationEngine.update(content); 192 return verificationEngine.verify(signature); 193 } 194 195 /** Called to restore the state of the SignedObject from a stream. */ 196 private void readObject(ObjectInputStream s) 197 throws IOException, ClassNotFoundException 198 { 199 s.defaultReadObject(); 200 content = (byte[]) content.clone(); 201 signature = (byte[]) signature.clone(); 202 } 203 }