1. /*
  2. * @(#)Identity.java 1.61 04/05/18
  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.Serializable;
  9. import java.util.*;
  10. /**
  11. * <p>This class represents identities: real-world objects such as people,
  12. * companies or organizations whose identities can be authenticated using
  13. * their public keys. Identities may also be more abstract (or concrete)
  14. * constructs, such as daemon threads or smart cards.
  15. *
  16. * <p>All Identity objects have a name and a public key. Names are
  17. * immutable. Identities may also be scoped. That is, if an Identity is
  18. * specified to have a particular scope, then the name and public
  19. * key of the Identity are unique within that scope.
  20. *
  21. * <p>An Identity also has a set of certificates (all certifying its own
  22. * public key). The Principal names specified in these certificates need
  23. * not be the same, only the key.
  24. *
  25. * <p>An Identity can be subclassed, to include postal and email addresses,
  26. * telephone numbers, images of faces and logos, and so on.
  27. *
  28. * @see IdentityScope
  29. * @see Signer
  30. * @see Principal
  31. *
  32. * @version 1.61
  33. * @author Benjamin Renaud
  34. * @deprecated This class is no longer used. Its functionality has been
  35. * replaced by <code>java.security.KeyStore</code>, the
  36. * <code>java.security.cert</code> package, and
  37. * <code>java.security.Principal</code>.
  38. */
  39. @Deprecated
  40. public abstract class Identity implements Principal, Serializable {
  41. /** use serialVersionUID from JDK 1.1.x for interoperability */
  42. private static final long serialVersionUID = 3609922007826600659L;
  43. /**
  44. * The name for this identity.
  45. *
  46. * @serial
  47. */
  48. private String name;
  49. /**
  50. * The public key for this identity.
  51. *
  52. * @serial
  53. */
  54. private PublicKey publicKey;
  55. /**
  56. * Generic, descriptive information about the identity.
  57. *
  58. * @serial
  59. */
  60. String info = "No further information available.";
  61. /**
  62. * The scope of the identity.
  63. *
  64. * @serial
  65. */
  66. IdentityScope scope;
  67. /**
  68. * The certificates for this identity.
  69. *
  70. * @serial
  71. */
  72. Vector certificates;
  73. /**
  74. * Constructor for serialization only.
  75. */
  76. protected Identity() {
  77. this("restoring...");
  78. }
  79. /**
  80. * Constructs an identity with the specified name and scope.
  81. *
  82. * @param name the identity name.
  83. * @param scope the scope of the identity.
  84. *
  85. * @exception KeyManagementException if there is already an identity
  86. * with the same name in the scope.
  87. */
  88. public Identity(String name, IdentityScope scope) throws
  89. KeyManagementException {
  90. this(name);
  91. if (scope != null) {
  92. scope.addIdentity(this);
  93. }
  94. this.scope = scope;
  95. }
  96. /**
  97. * Constructs an identity with the specified name and no scope.
  98. *
  99. * @param name the identity name.
  100. */
  101. public Identity(String name) {
  102. this.name = name;
  103. }
  104. /**
  105. * Returns this identity's name.
  106. *
  107. * @return the name of this identity.
  108. */
  109. public final String getName() {
  110. return name;
  111. }
  112. /**
  113. * Returns this identity's scope.
  114. *
  115. * @return the scope of this identity.
  116. */
  117. public final IdentityScope getScope() {
  118. return scope;
  119. }
  120. /**
  121. * Returns this identity's public key.
  122. *
  123. * @return the public key for this identity.
  124. *
  125. * @see #setPublicKey
  126. */
  127. public PublicKey getPublicKey() {
  128. return publicKey;
  129. }
  130. /**
  131. * Sets this identity's public key. The old key and all of this
  132. * identity's certificates are removed by this operation.
  133. *
  134. * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
  135. * method is called with <code>"setIdentityPublicKey"</code>
  136. * as its argument to see if it's ok to set the public key.
  137. *
  138. * @param key the public key for this identity.
  139. *
  140. * @exception KeyManagementException if another identity in the
  141. * identity's scope has the same public key, or if another exception occurs.
  142. *
  143. * @exception SecurityException if a security manager exists and its
  144. * <code>checkSecurityAccess</code> method doesn't allow
  145. * setting the public key.
  146. *
  147. * @see #getPublicKey
  148. * @see SecurityManager#checkSecurityAccess
  149. */
  150. /* Should we throw an exception if this is already set? */
  151. public void setPublicKey(PublicKey key) throws KeyManagementException {
  152. check("setIdentityPublicKey");
  153. this.publicKey = key;
  154. certificates = new Vector();
  155. }
  156. /**
  157. * Specifies a general information string for this identity.
  158. *
  159. * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
  160. * method is called with <code>"setIdentityInfo"</code>
  161. * as its argument to see if it's ok to specify the information string.
  162. *
  163. * @param info the information string.
  164. *
  165. * @exception SecurityException if a security manager exists and its
  166. * <code>checkSecurityAccess</code> method doesn't allow
  167. * setting the information string.
  168. *
  169. * @see #getInfo
  170. * @see SecurityManager#checkSecurityAccess
  171. */
  172. public void setInfo(String info) {
  173. check("setIdentityInfo");
  174. this.info = info;
  175. }
  176. /**
  177. * Returns general information previously specified for this identity.
  178. *
  179. * @return general information about this identity.
  180. *
  181. * @see #setInfo
  182. */
  183. public String getInfo() {
  184. return info;
  185. }
  186. /**
  187. * Adds a certificate for this identity. If the identity has a public
  188. * key, the public key in the certificate must be the same, and if
  189. * the identity does not have a public key, the identity's
  190. * public key is set to be that specified in the certificate.
  191. *
  192. * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
  193. * method is called with <code>"addIdentityCertificate"</code>
  194. * as its argument to see if it's ok to add a certificate.
  195. *
  196. * @param certificate the certificate to be added.
  197. *
  198. * @exception KeyManagementException if the certificate is not valid,
  199. * if the public key in the certificate being added conflicts with
  200. * this identity's public key, or if another exception occurs.
  201. *
  202. * @exception SecurityException if a security manager exists and its
  203. * <code>checkSecurityAccess</code> method doesn't allow
  204. * adding a certificate.
  205. *
  206. * @see SecurityManager#checkSecurityAccess
  207. */
  208. public void addCertificate(Certificate certificate)
  209. throws KeyManagementException {
  210. check("addIdentityCertificate");
  211. if (certificates == null) {
  212. certificates = new Vector();
  213. }
  214. if (publicKey != null) {
  215. if (!keyEquals(publicKey, certificate.getPublicKey())) {
  216. throw new KeyManagementException(
  217. "public key different from cert public key");
  218. }
  219. } else {
  220. publicKey = certificate.getPublicKey();
  221. }
  222. certificates.addElement(certificate);
  223. }
  224. private boolean keyEquals(Key aKey, Key anotherKey) {
  225. String aKeyFormat = aKey.getFormat();
  226. String anotherKeyFormat = anotherKey.getFormat();
  227. if ((aKeyFormat == null) ^ (anotherKeyFormat == null))
  228. return false;
  229. if (aKeyFormat != null && anotherKeyFormat != null)
  230. if (!aKeyFormat.equalsIgnoreCase(anotherKeyFormat))
  231. return false;
  232. return java.util.Arrays.equals(aKey.getEncoded(),
  233. anotherKey.getEncoded());
  234. }
  235. /**
  236. * Removes a certificate from this identity.
  237. *
  238. * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
  239. * method is called with <code>"removeIdentityCertificate"</code>
  240. * as its argument to see if it's ok to remove a certificate.
  241. *
  242. * @param certificate the certificate to be removed.
  243. *
  244. * @exception KeyManagementException if the certificate is
  245. * missing, or if another exception occurs.
  246. *
  247. * @exception SecurityException if a security manager exists and its
  248. * <code>checkSecurityAccess</code> method doesn't allow
  249. * removing a certificate.
  250. *
  251. * @see SecurityManager#checkSecurityAccess
  252. */
  253. public void removeCertificate(Certificate certificate)
  254. throws KeyManagementException {
  255. check("removeIdentityCertificate");
  256. if (certificates != null) {
  257. certificates.removeElement(certificate);
  258. }
  259. }
  260. /**
  261. * Returns a copy of all the certificates for this identity.
  262. *
  263. * @return a copy of all the certificates for this identity.
  264. */
  265. public Certificate[] certificates() {
  266. if (certificates == null) {
  267. return new Certificate[0];
  268. }
  269. int len = certificates.size();
  270. Certificate[] certs = new Certificate[len];
  271. certificates.copyInto(certs);
  272. return certs;
  273. }
  274. /**
  275. * Tests for equality between the specified object and this identity.
  276. * This first tests to see if the entities actually refer to the same
  277. * object, in which case it returns true. Next, it checks to see if
  278. * the entities have the same name and the same scope. If they do,
  279. * the method returns true. Otherwise, it calls
  280. * {@link #identityEquals(Identity) identityEquals}, which subclasses should
  281. * override.
  282. *
  283. * @param identity the object to test for equality with this identity.
  284. *
  285. * @return true if the objects are considered equal, false otherwise.
  286. *
  287. * @see #identityEquals
  288. */
  289. public final boolean equals(Object identity) {
  290. if (identity == this) {
  291. return true;
  292. }
  293. if (identity instanceof Identity) {
  294. Identity i = (Identity)identity;
  295. if (this.fullName().equals(i.fullName())) {
  296. return true;
  297. } else {
  298. return identityEquals(i);
  299. }
  300. }
  301. return false;
  302. }
  303. /**
  304. * Tests for equality between the specified identity and this identity.
  305. * This method should be overriden by subclasses to test for equality.
  306. * The default behavior is to return true if the names and public keys
  307. * are equal.
  308. *
  309. * @param identity the identity to test for equality with this identity.
  310. *
  311. * @return true if the identities are considered equal, false
  312. * otherwise.
  313. *
  314. * @see #equals
  315. */
  316. protected boolean identityEquals(Identity identity) {
  317. if (!name.equalsIgnoreCase(identity.name))
  318. return false;
  319. if ((publicKey == null) ^ (identity.publicKey == null))
  320. return false;
  321. if (publicKey != null && identity.publicKey != null)
  322. if (!publicKey.equals(identity.publicKey))
  323. return false;
  324. return true;
  325. }
  326. /**
  327. * Returns a parsable name for identity: identityName.scopeName
  328. */
  329. String fullName() {
  330. String parsable = name;
  331. if (scope != null) {
  332. parsable += "." + scope.getName();
  333. }
  334. return parsable;
  335. }
  336. /**
  337. * Returns a short string describing this identity, telling its
  338. * name and its scope (if any).
  339. *
  340. * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
  341. * method is called with <code>"printIdentity"</code>
  342. * as its argument to see if it's ok to return the string.
  343. *
  344. * @return information about this identity, such as its name and the
  345. * name of its scope (if any).
  346. *
  347. * @exception SecurityException if a security manager exists and its
  348. * <code>checkSecurityAccess</code> method doesn't allow
  349. * returning a string describing this identity.
  350. *
  351. * @see SecurityManager#checkSecurityAccess
  352. */
  353. public String toString() {
  354. check("printIdentity");
  355. String printable = name;
  356. if (scope != null) {
  357. printable += "[" + scope.getName() + "]";
  358. }
  359. return printable;
  360. }
  361. /**
  362. * Returns a string representation of this identity, with
  363. * optionally more details than that provided by the
  364. * <code>toString</code> method without any arguments.
  365. *
  366. * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
  367. * method is called with <code>"printIdentity"</code>
  368. * as its argument to see if it's ok to return the string.
  369. *
  370. * @param detailed whether or not to provide detailed information.
  371. *
  372. * @return information about this identity. If <code>detailed</code>
  373. * is true, then this method returns more information than that
  374. * provided by the <code>toString</code> method without any arguments.
  375. *
  376. * @exception SecurityException if a security manager exists and its
  377. * <code>checkSecurityAccess</code> method doesn't allow
  378. * returning a string describing this identity.
  379. *
  380. * @see #toString
  381. * @see SecurityManager#checkSecurityAccess
  382. */
  383. public String toString(boolean detailed) {
  384. String out = toString();
  385. if (detailed) {
  386. out += "\n";
  387. out += printKeys();
  388. out += "\n" + printCertificates();
  389. if (info != null) {
  390. out += "\n\t" + info;
  391. } else {
  392. out += "\n\tno additional information available.";
  393. }
  394. }
  395. return out;
  396. }
  397. String printKeys() {
  398. String key = "";
  399. if (publicKey != null) {
  400. key = "\tpublic key initialized";
  401. } else {
  402. key = "\tno public key";
  403. }
  404. return key;
  405. }
  406. String printCertificates() {
  407. String out = "";
  408. if (certificates == null) {
  409. return "\tno certificates";
  410. } else {
  411. out += "\tcertificates: \n";
  412. Enumeration e = certificates.elements();
  413. int i = 1;
  414. while (e.hasMoreElements()) {
  415. Certificate cert = (Certificate)e.nextElement();
  416. out += "\tcertificate " + i++ +
  417. "\tfor : " + cert.getPrincipal() + "\n";
  418. out += "\t\t\tfrom : " +
  419. cert.getGuarantor() + "\n";
  420. }
  421. }
  422. return out;
  423. }
  424. /**
  425. * Returns a hashcode for this identity.
  426. *
  427. * @return a hashcode for this identity.
  428. */
  429. public int hashCode() {
  430. return name.hashCode();
  431. }
  432. private static void check(String directive) {
  433. SecurityManager security = System.getSecurityManager();
  434. if (security != null) {
  435. security.checkSecurityAccess(directive);
  436. }
  437. }
  438. }