1. /*
  2. * @(#)SignedObject.java 1.39 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.io.*;
  9. /**
  10. * <p> SignedObject is a class for the purpose of creating authentic
  11. * runtime objects whose integrity cannot be compromised without being
  12. * detected.
  13. *
  14. * <p> More specifically, a SignedObject contains another Serializable
  15. * object, the (to-be-)signed object and its signature.
  16. *
  17. * <p> The signed object is a "deep copy" (in serialized form) of an
  18. * original object. Once the copy is made, further manipulation of
  19. * the original object has no side effect on the copy.
  20. *
  21. * <p> The underlying signing algorithm is designated by the Signature
  22. * object passed to the constructor and the <code>verify</code> method.
  23. * A typical usage for signing is the following:
  24. *
  25. * <p> <code> <pre>
  26. * Signature signingEngine = Signature.getInstance(algorithm,
  27. * provider);
  28. * SignedObject so = new SignedObject(myobject, signingKey,
  29. * signingEngine);
  30. * </pre> </code>
  31. *
  32. * <p> A typical usage for verification is the following (having
  33. * received SignedObject <code>so</code>):
  34. *
  35. * <p> <code> <pre>
  36. * Signature verificationEngine =
  37. * Signature.getInstance(algorithm, provider);
  38. * if (so.verify(publickey, verificationEngine))
  39. * try {
  40. * Object myobj = so.getObject();
  41. * } catch (java.lang.ClassNotFoundException e) {};
  42. * </pre> </code>
  43. *
  44. * <p> Several points are worth noting. First, there is no need to
  45. * initialize the signing or verification engine, as it will be
  46. * re-initialized inside the constructor and the <code>verify</code>
  47. * method. Secondly, for verification to succeed, the specified
  48. * public key must be the public key corresponding to the private key
  49. * used to generate the SignedObject.
  50. *
  51. * <p> More importantly, for flexibility reasons, the
  52. * constructor and <code>verify</code> method allow for
  53. * customized signature engines, which can implement signature
  54. * algorithms that are not installed formally as part of a crypto
  55. * provider. However, it is crucial that the programmer writing the
  56. * verifier code be aware what <code>Signature</code> engine is being
  57. * used, as its own implementation of the <code>verify</code> method
  58. * is invoked to verify a signature. In other words, a malicious
  59. * <code>Signature</code> may choose to always return true on
  60. * verification in an attempt to bypass a security check.
  61. *
  62. * <p> The signature algorithm can be, among others, the NIST standard
  63. * DSA, using DSA and SHA-1. The algorithm is specified using the
  64. * same convention as that for signatures. The DSA algorithm using the
  65. * SHA-1 message digest algorithm can be specified, for example, as
  66. * "SHA/DSA" or "SHA-1/DSA" (they are equivalent). In the case of
  67. * RSA, there are multiple choices for the message digest algorithm,
  68. * so the signing algorithm could be specified as, for example,
  69. * "MD2/RSA", "MD5/RSA" or "SHA-1/RSA". The algorithm name must be
  70. * specified, as there is no default.
  71. *
  72. * <p> The name of the Cryptography Package Provider is designated
  73. * also by the Signature parameter to the constructor and the
  74. * <code>verify</code> method. If the provider is not
  75. * specified, the default provider is used. Each installation can
  76. * be configured to use a particular provider as default.
  77. *
  78. * <p> Potential applications of SignedObject include:
  79. * <ul>
  80. * <li> It can be used
  81. * internally to any Java runtime as an unforgeable authorization
  82. * token -- one that can be passed around without the fear that the
  83. * token can be maliciously modified without being detected.
  84. * <li> It
  85. * can be used to sign and serialize data/object for storage outside
  86. * the Java runtime (e.g., storing critical access control data on
  87. * disk).
  88. * <li> Nested SignedObjects can be used to construct a logical
  89. * sequence of signatures, resembling a chain of authorization and
  90. * delegation.
  91. * </ul>
  92. *
  93. * @see Signature
  94. *
  95. * @version 1.39, 01/23/03
  96. * @author Li Gong
  97. */
  98. public final class SignedObject implements Serializable {
  99. /*
  100. * The original content is "deep copied" in its serialized format
  101. * and stored in a byte array. The signature field is also in the
  102. * form of byte array.
  103. */
  104. private byte[] content;
  105. private byte[] signature;
  106. private String thealgorithm;
  107. /**
  108. * Constructs a SignedObject from any Serializable object.
  109. * The given object is signed with the given signing key, using the
  110. * designated signature engine.
  111. *
  112. * @param object the object to be signed.
  113. * @param signingKey the private key for signing.
  114. * @param signingEngine the signature signing engine.
  115. *
  116. * @exception IOException if an error occurs during serialization
  117. * @exception InvalidKeyException if the key is invalid.
  118. * @exception SignatureException if signing fails.
  119. */
  120. public SignedObject(Serializable object, PrivateKey signingKey,
  121. Signature signingEngine)
  122. throws IOException, InvalidKeyException, SignatureException {
  123. // creating a stream pipe-line, from a to b
  124. ByteArrayOutputStream b = new ByteArrayOutputStream();
  125. ObjectOutput a = new ObjectOutputStream(b);
  126. // write and flush the object content to byte array
  127. a.writeObject(object);
  128. a.flush();
  129. a.close();
  130. this.content = b.toByteArray();
  131. b.close();
  132. // now sign the encapsulated object
  133. this.sign(signingKey, signingEngine);
  134. }
  135. /**
  136. * Retrieves the encapsulated object.
  137. * The encapsulated object is de-serialized before it is returned.
  138. *
  139. * @return the encapsulated object.
  140. *
  141. * @exception IOException if an error occurs during de-serialization
  142. * @exception ClassNotFoundException if an error occurs during
  143. * de-serialization
  144. */
  145. public Object getObject()
  146. throws IOException, ClassNotFoundException
  147. {
  148. // creating a stream pipe-line, from b to a
  149. ByteArrayInputStream b = new ByteArrayInputStream(this.content);
  150. ObjectInput a = new ObjectInputStream(b);
  151. Object obj = a.readObject();
  152. b.close();
  153. a.close();
  154. return obj;
  155. }
  156. /**
  157. * Retrieves the signature on the signed object, in the form of a
  158. * byte array.
  159. *
  160. * @return the signature.
  161. */
  162. public byte[] getSignature() {
  163. // return only a clone, for integrity reasons
  164. byte[] sig = (byte[])this.signature.clone();
  165. return sig;
  166. }
  167. /**
  168. * Retrieves the name of the signature algorithm.
  169. *
  170. * @return the signature algorithm name.
  171. */
  172. public String getAlgorithm() {
  173. return this.thealgorithm;
  174. }
  175. /**
  176. * Verifies that the signature in this SignedObject is the valid
  177. * signature for the object stored inside, with the given
  178. * verification key, using the designated verification engine.
  179. *
  180. * @param verificationKey the public key for verification.
  181. * @param verificationEngine the signature verification engine.
  182. *
  183. * @exception SignatureException if signature verification failed.
  184. * @exception InvalidKeyException if the verification key is invalid.
  185. *
  186. * @return <tt>true</tt> if the signature
  187. * is valid, <tt>false</tt> otherwise
  188. */
  189. public boolean verify(PublicKey verificationKey,
  190. Signature verificationEngine)
  191. throws InvalidKeyException, SignatureException {
  192. verificationEngine.initVerify(verificationKey);
  193. verificationEngine.update(this.content);
  194. return verificationEngine.verify(this.signature);
  195. }
  196. /*
  197. * Signs the encapsulated object with the given signing key, using the
  198. * designated signature engine.
  199. *
  200. * @param signingKey the private key for signing.
  201. * @param signingEngine the signature signing engine.
  202. *
  203. * @exception InvalidKeyException if the key is invalid.
  204. * @exception SignatureException if signing fails.
  205. */
  206. private void sign(PrivateKey signingKey, Signature signingEngine)
  207. throws InvalidKeyException, SignatureException {
  208. // initialize the signing engine
  209. signingEngine.initSign(signingKey);
  210. signingEngine.update(this.content);
  211. this.signature = signingEngine.sign();
  212. this.thealgorithm = signingEngine.getAlgorithm();
  213. }
  214. /**
  215. * readObject is called to restore the state of the SignedObject from
  216. * a stream.
  217. */
  218. private void readObject(java.io.ObjectInputStream s)
  219. throws java.io.IOException, ClassNotFoundException
  220. {
  221. s.defaultReadObject();
  222. content = (byte[])content.clone();
  223. signature = (byte[])signature.clone();
  224. }
  225. }