1. /*
  2. * @(#)SignedObject.java 1.43 03/12/19
  3. *
  4. * Copyright 2004 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.43, 12/19/03
  96. * @author Li Gong
  97. */
  98. public final class SignedObject implements Serializable {
  99. private static final long serialVersionUID = 720502720485447167L;
  100. /*
  101. * The original content is "deep copied" in its serialized format
  102. * and stored in a byte array. The signature field is also in the
  103. * form of byte array.
  104. */
  105. private byte[] content;
  106. private byte[] signature;
  107. private String thealgorithm;
  108. /**
  109. * Constructs a SignedObject from any Serializable object.
  110. * The given object is signed with the given signing key, using the
  111. * designated signature engine.
  112. *
  113. * @param object the object to be signed.
  114. * @param signingKey the private key for signing.
  115. * @param signingEngine the signature signing engine.
  116. *
  117. * @exception IOException if an error occurs during serialization
  118. * @exception InvalidKeyException if the key is invalid.
  119. * @exception SignatureException if signing fails.
  120. */
  121. public SignedObject(Serializable object, PrivateKey signingKey,
  122. Signature signingEngine)
  123. throws IOException, InvalidKeyException, SignatureException {
  124. // creating a stream pipe-line, from a to b
  125. ByteArrayOutputStream b = new ByteArrayOutputStream();
  126. ObjectOutput a = new ObjectOutputStream(b);
  127. // write and flush the object content to byte array
  128. a.writeObject(object);
  129. a.flush();
  130. a.close();
  131. this.content = b.toByteArray();
  132. b.close();
  133. // now sign the encapsulated object
  134. this.sign(signingKey, signingEngine);
  135. }
  136. /**
  137. * Retrieves the encapsulated object.
  138. * The encapsulated object is de-serialized before it is returned.
  139. *
  140. * @return the encapsulated object.
  141. *
  142. * @exception IOException if an error occurs during de-serialization
  143. * @exception ClassNotFoundException if an error occurs during
  144. * de-serialization
  145. */
  146. public Object getObject()
  147. throws IOException, ClassNotFoundException
  148. {
  149. // creating a stream pipe-line, from b to a
  150. ByteArrayInputStream b = new ByteArrayInputStream(this.content);
  151. ObjectInput a = new ObjectInputStream(b);
  152. Object obj = a.readObject();
  153. b.close();
  154. a.close();
  155. return obj;
  156. }
  157. /**
  158. * Retrieves the signature on the signed object, in the form of a
  159. * byte array.
  160. *
  161. * @return the signature. Returns a new array each time this
  162. * method is called.
  163. */
  164. public byte[] getSignature() {
  165. byte[] sig = (byte[])this.signature.clone();
  166. return sig;
  167. }
  168. /**
  169. * Retrieves the name of the signature algorithm.
  170. *
  171. * @return the signature algorithm name.
  172. */
  173. public String getAlgorithm() {
  174. return this.thealgorithm;
  175. }
  176. /**
  177. * Verifies that the signature in this SignedObject is the valid
  178. * signature for the object stored inside, with the given
  179. * verification key, using the designated verification engine.
  180. *
  181. * @param verificationKey the public key for verification.
  182. * @param verificationEngine the signature verification engine.
  183. *
  184. * @exception SignatureException if signature verification failed.
  185. * @exception InvalidKeyException if the verification key is invalid.
  186. *
  187. * @return <tt>true</tt> if the signature
  188. * is valid, <tt>false</tt> otherwise
  189. */
  190. public boolean verify(PublicKey verificationKey,
  191. Signature verificationEngine)
  192. throws InvalidKeyException, SignatureException {
  193. verificationEngine.initVerify(verificationKey);
  194. verificationEngine.update((byte[])this.content.clone());
  195. return verificationEngine.verify((byte[])this.signature.clone());
  196. }
  197. /*
  198. * Signs the encapsulated object with the given signing key, using the
  199. * designated signature engine.
  200. *
  201. * @param signingKey the private key for signing.
  202. * @param signingEngine the signature signing engine.
  203. *
  204. * @exception InvalidKeyException if the key is invalid.
  205. * @exception SignatureException if signing fails.
  206. */
  207. private void sign(PrivateKey signingKey, Signature signingEngine)
  208. throws InvalidKeyException, SignatureException {
  209. // initialize the signing engine
  210. signingEngine.initSign(signingKey);
  211. signingEngine.update((byte[])this.content.clone());
  212. this.signature = (byte[])signingEngine.sign().clone();
  213. this.thealgorithm = signingEngine.getAlgorithm();
  214. }
  215. /**
  216. * readObject is called to restore the state of the SignedObject from
  217. * a stream.
  218. */
  219. private void readObject(java.io.ObjectInputStream s)
  220. throws java.io.IOException, ClassNotFoundException
  221. {
  222. s.defaultReadObject();
  223. content = (byte[])content.clone();
  224. signature = (byte[])signature.clone();
  225. }
  226. }