1. /*
  2. * @(#)KerberosTicket.java 1.16 04/06/02
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.security.auth.kerberos;
  8. import java.io.*;
  9. import java.util.Date;
  10. import java.util.Arrays;
  11. import java.net.InetAddress;
  12. import javax.crypto.SecretKey;
  13. import javax.security.auth.Refreshable;
  14. import javax.security.auth.Destroyable;
  15. import javax.security.auth.RefreshFailedException;
  16. import javax.security.auth.DestroyFailedException;
  17. import sun.misc.HexDumpEncoder;
  18. import sun.security.krb5.EncryptionKey;
  19. import sun.security.krb5.Asn1Exception;
  20. import sun.security.util.*;
  21. /**
  22. * This class encapsulates a Kerberos ticket and associated
  23. * information as viewed from the client's point of view. It captures all
  24. * information that the Key Distribution Center (KDC) sends to the client
  25. * in the reply message KDC-REP defined in the Kerberos Protocol
  26. * Specification (<a href=http://www.ietf.org/rfc/rfc1510.txt>RFC 1510</a>).
  27. * <p>
  28. * All Kerberos JAAS login modules that authenticate a user to a KDC should
  29. * use this class. Where available, the login module might even read this
  30. * information from a ticket cache in the operating system instead of
  31. * directly communicating with the KDC. During the commit phase of the JAAS
  32. * authentication process, the JAAS login module should instantiate this
  33. * class and store the instance in the private credential set of a
  34. * {@link javax.security.auth.Subject Subject}.<p>
  35. *
  36. * It might be necessary for the application to be granted a
  37. * {@link javax.security.auth.PrivateCredentialPermission
  38. * PrivateCredentialPermission} if it needs to access a KerberosTicket
  39. * instance from a Subject. This permission is not needed when the
  40. * application depends on the default JGSS Kerberos mechanism to access the
  41. * KerberosTicket. In that case, however, the application will need an
  42. * appropriate
  43. * {@link javax.security.auth.kerberos.ServicePermission ServicePermission}.
  44. * <p>
  45. * Note that this class is applicable to both ticket granting tickets and
  46. * other regular service tickets. A ticket granting ticket is just a
  47. * special case of a more generalized service ticket.
  48. *
  49. * @see javax.security.auth.Subject
  50. * @see javax.security.auth.PrivateCredentialPermission
  51. * @see javax.security.auth.login.LoginContext
  52. * @see org.ietf.jgss.GSSCredential
  53. * @see org.ietf.jgss.GSSManager
  54. *
  55. * @author Mayank Upadhyay
  56. * @version 1.16, 06/02/04
  57. * @since 1.4
  58. */
  59. public class KerberosTicket implements Destroyable, Refreshable,
  60. java.io.Serializable {
  61. private static final long serialVersionUID = 7395334370157380539L;
  62. // TBD: Make these flag indices public
  63. private static final int FORWARDABLE_TICKET_FLAG = 1;
  64. private static final int FORWARDED_TICKET_FLAG = 2;
  65. private static final int PROXIABLE_TICKET_FLAG = 3;
  66. private static final int PROXY_TICKET_FLAG = 4;
  67. private static final int POSTDATED_TICKET_FLAG = 6;
  68. private static final int RENEWABLE_TICKET_FLAG = 8;
  69. private static final int INITIAL_TICKET_FLAG = 9;
  70. private static final int NUM_FLAGS = 32;
  71. /**
  72. *
  73. * ASN.1 DER Encoding of the Ticket as defined in the
  74. * Kerberos Protocol Specification RFC1510.
  75. *
  76. * @serial
  77. */
  78. private byte[] asn1Encoding;
  79. /**
  80. *<code>KeyImpl</code> is serialized by writing out the ASN1 Encoded bytes
  81. * of the encryption key. The ASN1 encoding is defined in RFC1510 and as
  82. * follows:
  83. * <pre>
  84. * EncryptionKey ::= SEQUENCE {
  85. * keytype[0] INTEGER,
  86. * keyvalue[1] OCTET STRING
  87. * }
  88. * </pre>
  89. *
  90. * @serial
  91. */
  92. private KeyImpl sessionKey;
  93. /**
  94. *
  95. * Ticket Flags as defined in the Kerberos Protocol Specification RFC1510.
  96. *
  97. * @serial
  98. */
  99. private boolean[] flags;
  100. /**
  101. *
  102. * Time of initial authentication
  103. *
  104. * @serial
  105. */
  106. private Date authTime;
  107. /**
  108. *
  109. * Time after which the ticket is valid.
  110. * @serial
  111. */
  112. private Date startTime;
  113. /**
  114. *
  115. * Time after which the ticket will not be honored. (its expiration time).
  116. *
  117. * @serial
  118. */
  119. private Date endTime;
  120. /**
  121. *
  122. * For renewable Tickets it indicates the maximum endtime that may be
  123. * included in a renewal. It can be thought of as the absolute expiration
  124. * time for the ticket, including all renewals. This field may be null
  125. * for tickets that are not renewable.
  126. *
  127. * @serial
  128. */
  129. private Date renewTill;
  130. /**
  131. *
  132. * Client that owns the service ticket
  133. *
  134. * @serial
  135. */
  136. private KerberosPrincipal client;
  137. /**
  138. *
  139. * The service for which the ticket was issued.
  140. *
  141. * @serial
  142. */
  143. private KerberosPrincipal server;
  144. /**
  145. *
  146. * The addresses from where the ticket may be used by the client.
  147. * This field may be null when the ticket is usable from any address.
  148. *
  149. * @serial
  150. */
  151. private InetAddress[] clientAddresses;
  152. private transient boolean destroyed = false;
  153. /**
  154. * Constructs a KerberosTicket using credentials information that a
  155. * client either receives from a KDC or reads from a cache.
  156. *
  157. * @param asn1Encoding the ASN.1 encoding of the ticket as defined by
  158. * the Kerberos protocol specification.
  159. * @param client the client that owns this service
  160. * ticket
  161. * @param server the service that this ticket is for
  162. * @param sessionKey the raw bytes for the session key that must be
  163. * used to encrypt the authenticator that will be sent to the server
  164. * @param keyType the key type for the session key as defined by the
  165. * Kerberos protocol specification.
  166. * @param flags the ticket flags. Each element in this array indicates
  167. * the value for the corresponding bit in the ASN.1 BitString that
  168. * represents the ticket flags. If the number of elements in this array
  169. * is less than the number of flags used by the Kerberos protocol,
  170. * then the missing flags will be filled in with false.
  171. * @param authTime the time of initial authentication for the client
  172. * @param startTime the time after which the ticket will be valid. This
  173. * may be null in which case the value of authTime is treated as the
  174. * startTime.
  175. * @param endTime the time after which the ticket will no longer be
  176. * valid
  177. * @param renewTill an absolute expiration time for the ticket,
  178. * including all renewal that might be possible. This field may be null
  179. * for tickets that are not renewable.
  180. * @param clientAddresses the addresses from where the ticket may be
  181. * used by the client. This field may be null when the ticket is usable
  182. * from any address.
  183. */
  184. public KerberosTicket(byte[] asn1Encoding,
  185. KerberosPrincipal client,
  186. KerberosPrincipal server,
  187. byte[] sessionKey,
  188. int keyType,
  189. boolean[] flags,
  190. Date authTime,
  191. Date startTime,
  192. Date endTime,
  193. Date renewTill,
  194. InetAddress[] clientAddresses) {
  195. init(asn1Encoding, client, server, sessionKey, keyType, flags,
  196. authTime, startTime, endTime, renewTill, clientAddresses);
  197. }
  198. private void init(byte[] asn1Encoding,
  199. KerberosPrincipal client,
  200. KerberosPrincipal server,
  201. byte[] sessionKey,
  202. int keyType,
  203. boolean[] flags,
  204. Date authTime,
  205. Date startTime,
  206. Date endTime,
  207. Date renewTill,
  208. InetAddress[] clientAddresses) {
  209. if (asn1Encoding == null)
  210. throw new IllegalArgumentException("ASN.1 encoding of ticket"
  211. + " cannot be null");
  212. this.asn1Encoding = asn1Encoding.clone();
  213. if (client == null)
  214. throw new IllegalArgumentException("Client name in ticket"
  215. + " cannot be null");
  216. this.client = client;
  217. if (server == null)
  218. throw new IllegalArgumentException("Server name in ticket"
  219. + " cannot be null");
  220. this.server = server;
  221. if (sessionKey == null)
  222. throw new IllegalArgumentException("Session key for ticket"
  223. + " cannot be null");
  224. this.sessionKey = new KeyImpl(sessionKey, keyType);
  225. if (flags != null) {
  226. if (flags.length >= NUM_FLAGS)
  227. this.flags = (boolean[]) flags.clone();
  228. else {
  229. this.flags = new boolean[NUM_FLAGS];
  230. // Fill in whatever we have
  231. for (int i = 0; i < flags.length; i++)
  232. this.flags[i] = flags[i];
  233. }
  234. } else
  235. this.flags = new boolean[NUM_FLAGS];
  236. if (this.flags[RENEWABLE_TICKET_FLAG]) {
  237. if (renewTill == null)
  238. throw new IllegalArgumentException("The renewable period "
  239. + "end time cannot be null for renewable tickets.");
  240. this.renewTill = renewTill;
  241. }
  242. if (authTime == null)
  243. throw new IllegalArgumentException("Authentication time of ticket"
  244. + " cannot be null");
  245. this.authTime = authTime;
  246. this.startTime = (startTime != null? startTime: authTime);
  247. if (endTime == null)
  248. throw new IllegalArgumentException("End time for ticket validity"
  249. + " cannot be null");
  250. this.endTime = endTime;
  251. if (clientAddresses != null)
  252. this.clientAddresses = (InetAddress[]) clientAddresses.clone();
  253. }
  254. /**
  255. * Returns the client principal associated with this ticket.
  256. *
  257. * @return the client principal.
  258. */
  259. public final KerberosPrincipal getClient() {
  260. return client;
  261. }
  262. /**
  263. * Returns the service principal associated with this ticket.
  264. *
  265. * @return the service principal.
  266. */
  267. public final KerberosPrincipal getServer() {
  268. return server;
  269. }
  270. /**
  271. * Returns the session key associated with this ticket.
  272. *
  273. * @return the session key.
  274. */
  275. public final SecretKey getSessionKey() {
  276. if (destroyed)
  277. throw new IllegalStateException("This ticket is no longer valid");
  278. return sessionKey;
  279. }
  280. /**
  281. * Returns the key type of the session key associated with this
  282. * ticket as defined by the Kerberos Protocol Specification.
  283. *
  284. * @return the key type of the session key associated with this
  285. * ticket.
  286. *
  287. * @see #getSessionKey()
  288. */
  289. public final int getSessionKeyType() {
  290. if (destroyed)
  291. throw new IllegalStateException("This ticket is no longer valid");
  292. return sessionKey.getKeyType();
  293. }
  294. /**
  295. * Determines if this ticket is forwardable.
  296. *
  297. * @return true if this ticket is forwardable, false if not.
  298. */
  299. public final boolean isForwardable() {
  300. return flags[FORWARDABLE_TICKET_FLAG];
  301. }
  302. /**
  303. * Determines if this ticket had been forwarded or was issued based on
  304. * authentication involving a forwarded ticket-granting ticket.
  305. *
  306. * @return true if this ticket had been forwarded or was issued based on
  307. * authentication involving a forwarded ticket-granting ticket,
  308. * false otherwise.
  309. */
  310. public final boolean isForwarded() {
  311. return flags[FORWARDED_TICKET_FLAG];
  312. }
  313. /**
  314. * Determines if this ticket is proxiable.
  315. *
  316. * @return true if this ticket is proxiable, false if not.
  317. */
  318. public final boolean isProxiable() {
  319. return flags[PROXIABLE_TICKET_FLAG];
  320. }
  321. /**
  322. * Determines is this ticket is a proxy-ticket.
  323. *
  324. * @return true if this ticket is a proxy-ticket, false if not.
  325. */
  326. public final boolean isProxy() {
  327. return flags[PROXY_TICKET_FLAG];
  328. }
  329. /**
  330. * Determines is this ticket is post-dated.
  331. *
  332. * @return true if this ticket is post-dated, false if not.
  333. */
  334. public final boolean isPostdated() {
  335. return flags[POSTDATED_TICKET_FLAG];
  336. }
  337. /**
  338. * Determines is this ticket is renewable. If so, the {@link #refresh()
  339. * refresh} method can be called, assuming the validity period for
  340. * renewing is not already over.
  341. *
  342. * @return true if this ticket is renewable, false if not.
  343. */
  344. public final boolean isRenewable() {
  345. return flags[RENEWABLE_TICKET_FLAG];
  346. }
  347. /**
  348. * Determines if this ticket was issued using the Kerberos AS-Exchange
  349. * protocol, and not issued based on some ticket-granting ticket.
  350. *
  351. * @return true if this ticket was issued using the Kerberos AS-Exchange
  352. * protocol, false if not.
  353. */
  354. public final boolean isInitial() {
  355. return flags[INITIAL_TICKET_FLAG];
  356. }
  357. /**
  358. * Returns the flags associated with this ticket. Each element in the
  359. * returned array indicates the value for the corresponding bit in the
  360. * ASN.1 BitString that represents the ticket flags.
  361. *
  362. * @return the flags associated with this ticket.
  363. */
  364. public final boolean[] getFlags() {
  365. return (flags == null? null: (boolean[]) flags.clone());
  366. }
  367. /**
  368. * Returns the time that the client was authenticated.
  369. *
  370. * @return the time that the client was authenticated.
  371. */
  372. public final java.util.Date getAuthTime() {
  373. return (authTime == null) ? null : new Date(authTime.getTime());
  374. }
  375. /**
  376. * Returns the start time for this ticket's validity period.
  377. *
  378. * @return the start time for this ticket's validity period.
  379. */
  380. public final java.util.Date getStartTime() {
  381. return (startTime == null) ? null : new Date(startTime.getTime());
  382. }
  383. /**
  384. * Returns the expiration time for this ticket's validity period.
  385. *
  386. * @return the expiration time for this ticket's validity period.
  387. */
  388. public final java.util.Date getEndTime() {
  389. return (endTime == null) ? null : new Date(endTime.getTime());
  390. }
  391. /**
  392. * Returns the latest expiration time for this ticket, including all
  393. * renewals. This will return a null value for non-renewable tickets.
  394. *
  395. * @return the latest expiration time for this ticket.
  396. */
  397. public final java.util.Date getRenewTill() {
  398. return (renewTill == null) ? null: new Date(renewTill.getTime());
  399. }
  400. /**
  401. * Returns a list of addresses from where the ticket can be used.
  402. *
  403. * @return ths list of addresses or null, if the field was not
  404. * provided.
  405. */
  406. public final java.net.InetAddress[] getClientAddresses() {
  407. return (clientAddresses == null?
  408. null: (InetAddress[]) clientAddresses.clone());
  409. }
  410. /**
  411. * Returns an ASN.1 encoding of the entire ticket.
  412. *
  413. * @return an ASN.1 encoding of the entire ticket.
  414. */
  415. public final byte[] getEncoded() {
  416. if (destroyed)
  417. throw new IllegalStateException("This ticket is no longer valid");
  418. return (byte[]) asn1Encoding.clone();
  419. }
  420. /** Determines if this ticket is still current. */
  421. public boolean isCurrent() {
  422. return (System.currentTimeMillis() <= getEndTime().getTime());
  423. }
  424. /**
  425. * Extends the validity period of this ticket. The ticket will contain
  426. * a new session key if the refresh operation succeeds. The refresh
  427. * operation will fail if the ticket is not renewable or the latest
  428. * allowable renew time has passed. Any other error returned by the
  429. * KDC will also cause this method to fail.
  430. *
  431. * Note: This method is not synchronized with the the accessor
  432. * methods of this object. Hence callers need to be aware of multiple
  433. * threads that might access this and try to renew it at the same
  434. * time.
  435. *
  436. * @throws RefreshFailedException if the ticket is not renewable, or
  437. * the latest allowable renew time has passed, or the KDC returns some
  438. * error.
  439. *
  440. * @see #isRenewable()
  441. * @see #getRenewTill()
  442. */
  443. public void refresh() throws RefreshFailedException {
  444. if (destroyed)
  445. throw new RefreshFailedException("A destroyed ticket "
  446. + "cannot be renewd.");
  447. if (!isRenewable())
  448. throw new RefreshFailedException("This ticket is not renewable");
  449. if (System.currentTimeMillis() > getRenewTill().getTime())
  450. throw new RefreshFailedException("This ticket is past "
  451. + "its last renewal time.");
  452. Throwable e = null;
  453. sun.security.krb5.Credentials krb5Creds = null;
  454. try {
  455. krb5Creds = new sun.security.krb5.Credentials(asn1Encoding,
  456. client.toString(),
  457. server.toString(),
  458. sessionKey.getEncoded(),
  459. sessionKey.getKeyType(),
  460. flags,
  461. authTime,
  462. startTime,
  463. endTime,
  464. renewTill,
  465. clientAddresses);
  466. krb5Creds = krb5Creds.renew();
  467. } catch (sun.security.krb5.KrbException krbException) {
  468. e = krbException;
  469. } catch (java.io.IOException ioException) {
  470. e = ioException;
  471. }
  472. if (e != null) {
  473. RefreshFailedException rfException
  474. = new RefreshFailedException("Failed to renew Kerberos Ticket "
  475. + "for client " + client
  476. + " and server " + server
  477. + " - " + e.getMessage());
  478. rfException.initCause(e);
  479. throw rfException;
  480. }
  481. /*
  482. * In case multiple threads try to refresh it at the same time.
  483. */
  484. synchronized (this) {
  485. try {
  486. this.destroy();
  487. } catch (DestroyFailedException dfException) {
  488. // Squelch it since we don't care about the old ticket.
  489. }
  490. init(krb5Creds.getEncoded(),
  491. new KerberosPrincipal(krb5Creds.getClient().getName()),
  492. new KerberosPrincipal(krb5Creds.getServer().getName()),
  493. krb5Creds.getSessionKey().getBytes(),
  494. krb5Creds.getSessionKey().getEType(),
  495. krb5Creds.getFlags(),
  496. krb5Creds.getAuthTime(),
  497. krb5Creds.getStartTime(),
  498. krb5Creds.getEndTime(),
  499. krb5Creds.getRenewTill(),
  500. krb5Creds.getClientAddresses());
  501. destroyed = false;
  502. }
  503. }
  504. /**
  505. * Destroys the ticket and destroys any sensitive information stored in
  506. * it.
  507. */
  508. public void destroy() throws DestroyFailedException {
  509. if (!destroyed) {
  510. Arrays.fill(asn1Encoding, (byte) 0);
  511. client = null;
  512. server = null;
  513. sessionKey.destroy();
  514. flags = null;
  515. authTime = null;
  516. startTime = null;
  517. endTime = null;
  518. renewTill = null;
  519. clientAddresses = null;
  520. destroyed = true;
  521. }
  522. }
  523. /**
  524. * Determines if this ticket has been destroyed.
  525. */
  526. public boolean isDestroyed() {
  527. return destroyed;
  528. }
  529. public String toString() {
  530. if (destroyed)
  531. throw new IllegalStateException("This ticket is no longer valid");
  532. StringBuffer caddrBuf = new StringBuffer();
  533. if (clientAddresses != null) {
  534. for (int i = 0; i < clientAddresses.length; i++) {
  535. caddrBuf.append("clientAddresses[" + i + "] = " +
  536. clientAddresses[i].toString());
  537. }
  538. }
  539. return ("Ticket (hex) = " + "\n" +
  540. (new HexDumpEncoder()).encode(asn1Encoding) + "\n" +
  541. "Client Principal = " + client.toString() + "\n" +
  542. "Server Principal = " + server.toString() + "\n" +
  543. "Session Key = " + sessionKey.toString() + "\n" +
  544. "Forwardable Ticket " + flags[FORWARDABLE_TICKET_FLAG] + "\n" +
  545. "Forwarded Ticket " + flags[FORWARDED_TICKET_FLAG] + "\n" +
  546. "Proxiable Ticket " + flags[PROXIABLE_TICKET_FLAG] + "\n" +
  547. "Proxy Ticket " + flags[PROXY_TICKET_FLAG] + "\n" +
  548. "Postdated Ticket " + flags[POSTDATED_TICKET_FLAG] + "\n" +
  549. "Renewable Ticket " + flags[RENEWABLE_TICKET_FLAG] + "\n" +
  550. "Initial Ticket " + flags[RENEWABLE_TICKET_FLAG] + "\n" +
  551. "Auth Time = " + authTime.toString() + "\n" +
  552. "Start Time = " + startTime.toString() + "\n" +
  553. "End Time = " + endTime.toString() + "\n" +
  554. "Renew Till = " +
  555. (renewTill == null ? "Null " : renewTill.toString()) + "\n" +
  556. "Client Addresses " +
  557. (clientAddresses == null ? " Null " : caddrBuf.toString() +
  558. "\n"));
  559. }
  560. }