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