Identity.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Identity.java --- Identity 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.Serializable;
  33. import java.util.Vector;
  34. /**
  35. * The <code>Identity</code> class is used to represent people and companies
  36. * that can be authenticated using public key encryption. The identities can
  37. * also be abstract objects such as smart cards.
  38. *
  39. * <p><code>Identity</code> objects store a name and public key for each
  40. * identity. The names cannot be changed and the identities can be scoped. Each
  41. * identity (name and public key) within a scope are unique to that scope.</p>
  42. *
  43. * <p>Each identity has a set of ceritificates which all specify the same
  44. * public key, but not necessarily the same name.</p>
  45. *
  46. * <p>The <code>Identity</code> class can be subclassed to allow additional
  47. * information to be attached to it.</p>
  48. *
  49. * @author Mark Benvenuto
  50. * @see IdentityScope
  51. * @see Signer
  52. * @see Principal
  53. * @deprecated Replaced by <code>java.security.KeyStore</code>, the
  54. * <code>java.security.cert</code> package, and
  55. * <code>java.security.Principal</code>.
  56. */
  57. public abstract class Identity implements Principal, Serializable
  58. {
  59. private static final long serialVersionUID = 3609922007826600659L;
  60. private String name;
  61. private IdentityScope scope;
  62. private PublicKey publicKey;
  63. private String info;
  64. private Vector certificates;
  65. /** Constructor for serialization only. */
  66. protected Identity()
  67. {
  68. }
  69. /**
  70. * Constructs a new instance of <code>Identity</code> with the specified
  71. * name and scope.
  72. *
  73. * @param name
  74. * the name to use.
  75. * @param scope
  76. * the scope to use.
  77. * @throws KeyManagementException
  78. * if the identity is already present.
  79. */
  80. public Identity(String name, IdentityScope scope)
  81. throws KeyManagementException
  82. {
  83. this.name = name;
  84. this.scope = scope;
  85. }
  86. /**
  87. * Constructs a new instance of <code>Identity</code> with the specified
  88. * name and no scope.
  89. *
  90. * @param name
  91. * the name to use.
  92. */
  93. public Identity(String name)
  94. {
  95. this.name = name;
  96. this.scope = null;
  97. }
  98. /** @return the name of this identity. */
  99. public final String getName()
  100. {
  101. return name;
  102. }
  103. /** @return the scope of this identity. */
  104. public final IdentityScope getScope()
  105. {
  106. return scope;
  107. }
  108. /**
  109. * @return the public key of this identity.
  110. * @see #setPublicKey(java.security.PublicKey)
  111. */
  112. public PublicKey getPublicKey()
  113. {
  114. return publicKey;
  115. }
  116. /**
  117. * Sets the public key for this identity. The old key and all certificates
  118. * are removed.
  119. *
  120. * @param key
  121. * the public key to use.
  122. * @throws KeyManagementException
  123. * if this public key is used by another identity in the current
  124. * scope.
  125. * @throws SecurityException
  126. * if a {@link SecurityManager} is installed which disallows this
  127. * operation.
  128. */
  129. public void setPublicKey(PublicKey key) throws KeyManagementException
  130. {
  131. SecurityManager sm = System.getSecurityManager();
  132. if (sm != null)
  133. sm.checkSecurityAccess("setIdentityPublicKey");
  134. this.publicKey = key;
  135. }
  136. /**
  137. * Sets the general information string.
  138. *
  139. * @param info
  140. * the general information string.
  141. * @throws SecurityException
  142. * if a {@link SecurityManager} is installed which disallows this
  143. * operation.
  144. */
  145. public void setInfo(String info)
  146. {
  147. SecurityManager sm = System.getSecurityManager();
  148. if (sm != null)
  149. sm.checkSecurityAccess("setIdentityInfo");
  150. this.info = info;
  151. }
  152. /**
  153. * @return the general information string of this identity.
  154. * @see #setInfo(String)
  155. */
  156. public String getInfo()
  157. {
  158. return info;
  159. }
  160. /**
  161. * Adds a certificate to the list of ceritificates for this identity. The
  162. * public key in this certificate must match the existing public key if it
  163. * exists.
  164. *
  165. * @param certificate
  166. * the certificate to add.
  167. * @throws KeyManagementException
  168. * if the certificate is invalid, or the public key conflicts.
  169. * @throws SecurityException
  170. * if a {@link SecurityManager} is installed which disallows this
  171. * operation.
  172. */
  173. public void addCertificate(Certificate certificate)
  174. throws KeyManagementException
  175. {
  176. SecurityManager sm = System.getSecurityManager();
  177. if (sm != null)
  178. sm.checkSecurityAccess("addIdentityCertificate");
  179. // Check public key of this certificate against the first one in the vector
  180. if (certificates.size() > 0)
  181. {
  182. if (((Certificate) certificates.firstElement()).getPublicKey() != publicKey)
  183. throw new KeyManagementException("Public key does not match");
  184. }
  185. certificates.addElement(certificate);
  186. }
  187. /**
  188. * Removes a certificate from the list of ceritificates for this identity.
  189. *
  190. * @param certificate
  191. * the certificate to remove.
  192. * @throws KeyManagementException
  193. * if the certificate is invalid.
  194. * @throws SecurityException
  195. * if a {@link SecurityManager} is installed which disallows this
  196. * operation.
  197. */
  198. public void removeCertificate(Certificate certificate)
  199. throws KeyManagementException
  200. {
  201. SecurityManager sm = System.getSecurityManager();
  202. if (sm != null)
  203. sm.checkSecurityAccess("removeIdentityCertificate");
  204. if (certificates.contains(certificate) == false)
  205. throw new KeyManagementException("Certificate not found");
  206. certificates.removeElement(certificate);
  207. }
  208. /** @return an array of {@link Certificate}s for this identity. */
  209. public Certificate[] certificates()
  210. {
  211. Certificate[] certs = new Certificate[certificates.size()];
  212. int max = certificates.size();
  213. for (int i = 0; i < max; i++)
  214. certs[i] = (Certificate) certificates.elementAt(i);
  215. return certs;
  216. }
  217. /**
  218. * Checks for equality between this Identity and a specified object. It first
  219. * checks if they are the same object, then if the name and scope match and
  220. * returns <code>true</code> if successful. If these tests fail, the
  221. * {@link #identityEquals(Identity)} method is called.
  222. *
  223. * @return <code>true</code> if they are equal, <code>false</code>
  224. * otherwise.
  225. */
  226. public final boolean equals(Object identity)
  227. {
  228. if (identity instanceof Identity)
  229. {
  230. if (identity == this)
  231. return true;
  232. if ((((Identity) identity).getName().equals(this.name)) &&
  233. (((Identity) identity).getScope().equals(this.scope)))
  234. return true;
  235. return identityEquals((Identity) identity);
  236. }
  237. return false;
  238. }
  239. /**
  240. * Checks for equality between this Identity and a specified object. A
  241. * subclass should override this method. The default behavior is to return
  242. * <code>true</code> if the public key and names match.
  243. *
  244. * @return <code>true</code> if they are equal, <code>false</code>
  245. * otherwise.
  246. */
  247. protected boolean identityEquals(Identity identity)
  248. {
  249. return ((identity.getName().equals(this.name)) &&
  250. (identity.getPublicKey().equals(this.publicKey)));
  251. }
  252. /**
  253. * Returns a string representation of this Identity.
  254. *
  255. * @return a string representation of this Identity.
  256. * @throws SecurityException
  257. * if a {@link SecurityManager} is installed which disallows this
  258. * operation.
  259. */
  260. public String toString()
  261. {
  262. SecurityManager sm = System.getSecurityManager();
  263. if (sm != null)
  264. sm.checkSecurityAccess("printIdentity");
  265. /* TODO: Insert proper format here */
  266. return (name + ":@" + scope + " Public Key: " + publicKey);
  267. }
  268. /**
  269. * Returns a detailed string representation of this Identity.
  270. *
  271. * @param detailed
  272. * indicates whether or detailed information is desired.
  273. * @return a string representation of this Identity.
  274. * @throws SecurityException
  275. * if a {@link SecurityManager} is installed which disallows this
  276. * operation.
  277. */
  278. public String toString(boolean detailed)
  279. {
  280. SecurityManager sm = System.getSecurityManager();
  281. if (sm != null)
  282. sm.checkSecurityAccess("printIdentity");
  283. if (detailed)
  284. {
  285. /* TODO: Insert proper detailed format here */
  286. return (name + ":@" + scope + " Public Key: " + publicKey);
  287. }
  288. else
  289. {
  290. /* TODO: Insert proper format here */
  291. return (name + ":@" + scope + " Public Key: " + publicKey);
  292. }
  293. }
  294. /** @return a hashcode of this identity. */
  295. public int hashCode()
  296. {
  297. int ret = name.hashCode();
  298. if (publicKey != null)
  299. ret |= publicKey.hashCode();
  300. if (scope != null)
  301. ret |= scope.hashCode();
  302. if (info != null)
  303. ret |= info.hashCode();
  304. if (certificates != null)
  305. ret |= certificates.hashCode();
  306. return ret;
  307. }
  308. }