SignedObject.java 7.1 KB

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