1 7 8 package javax.security.auth.kerberos; 9 10 import java.io.*; 11 import java.util.Date ; 12 import java.util.Arrays ; 13 import java.net.InetAddress ; 14 import javax.crypto.SecretKey; 15 import javax.security.auth.Refreshable ; 16 import javax.security.auth.Destroyable ; 17 import javax.security.auth.RefreshFailedException ; 18 import javax.security.auth.DestroyFailedException ; 19 import sun.misc.HexDumpEncoder; 20 import sun.security.krb5.EncryptionKey; 21 import sun.security.krb5.Asn1Exception; 22 import sun.security.util.*; 23 24 62 public class KerberosTicket implements Destroyable , Refreshable , 63 java.io.Serializable { 64 65 private static final long serialVersionUID = 7395334370157380539L; 66 67 private static final int FORWARDABLE_TICKET_FLAG = 1; 69 private static final int FORWARDED_TICKET_FLAG = 2; 70 private static final int PROXIABLE_TICKET_FLAG = 3; 71 private static final int PROXY_TICKET_FLAG = 4; 72 private static final int POSTDATED_TICKET_FLAG = 6; 73 private static final int RENEWABLE_TICKET_FLAG = 8; 74 private static final int INITIAL_TICKET_FLAG = 9; 75 76 private static final int NUM_FLAGS = 32; 77 78 85 86 private byte[] asn1Encoding; 87 88 101 102 private KeyImpl sessionKey; 103 104 110 111 private boolean[] flags; 112 113 119 120 private Date authTime; 121 122 127 private Date startTime; 128 129 135 136 private Date endTime; 137 138 147 148 private Date renewTill; 149 150 156 157 private KerberosPrincipal client; 158 159 165 166 private KerberosPrincipal server; 167 168 175 176 177 private InetAddress [] clientAddresses; 178 179 private transient boolean destroyed = false; 180 181 212 public KerberosTicket(byte[] asn1Encoding, 213 KerberosPrincipal client, 214 KerberosPrincipal server, 215 byte[] sessionKey, 216 int keyType, 217 boolean[] flags, 218 Date authTime, 219 Date startTime, 220 Date endTime, 221 Date renewTill, 222 InetAddress [] clientAddresses) { 223 224 init(asn1Encoding, client, server, sessionKey, keyType, flags, 225 authTime, startTime, endTime, renewTill, clientAddresses); 226 } 227 228 private void init(byte[] asn1Encoding, 229 KerberosPrincipal client, 230 KerberosPrincipal server, 231 byte[] sessionKey, 232 int keyType, 233 boolean[] flags, 234 Date authTime, 235 Date startTime, 236 Date endTime, 237 Date renewTill, 238 InetAddress [] clientAddresses) { 239 240 if (asn1Encoding == null) 241 throw new IllegalArgumentException ("ASN.1 encoding of ticket" 242 + " cannot be null"); 243 this.asn1Encoding = asn1Encoding.clone(); 244 245 if (client == null) 246 throw new IllegalArgumentException ("Client name in ticket" 247 + " cannot be null"); 248 this.client = client; 249 250 if (server == null) 251 throw new IllegalArgumentException ("Server name in ticket" 252 + " cannot be null"); 253 this.server = server; 254 255 if (sessionKey == null) 256 throw new IllegalArgumentException ("Session key for ticket" 257 + " cannot be null"); 258 this.sessionKey = new KeyImpl (sessionKey, keyType); 259 260 if (flags != null) { 261 if (flags.length >= NUM_FLAGS) 262 this.flags = (boolean[]) flags.clone(); 263 else { 264 this.flags = new boolean[NUM_FLAGS]; 265 for (int i = 0; i < flags.length; i++) 267 this.flags[i] = flags[i]; 268 } 269 } else 270 this.flags = new boolean[NUM_FLAGS]; 271 272 if (this.flags[RENEWABLE_TICKET_FLAG]) { 273 if (renewTill == null) 274 throw new IllegalArgumentException ("The renewable period " 275 + "end time cannot be null for renewable tickets."); 276 277 this.renewTill = renewTill; 278 } 279 280 this.authTime = authTime; 281 282 this.startTime = (startTime != null? startTime: authTime); 283 284 if (endTime == null) 285 throw new IllegalArgumentException ("End time for ticket validity" 286 + " cannot be null"); 287 this.endTime = endTime; 288 289 if (clientAddresses != null) 290 this.clientAddresses = (InetAddress []) clientAddresses.clone(); 291 } 292 293 298 public final KerberosPrincipal getClient() { 299 return client; 300 } 301 302 307 public final KerberosPrincipal getServer() { 308 return server; 309 } 310 311 316 public final SecretKey getSessionKey() { 317 if (destroyed) 318 throw new IllegalStateException ("This ticket is no longer valid"); 319 return sessionKey; 320 } 321 322 331 public final int getSessionKeyType() { 332 if (destroyed) 333 throw new IllegalStateException ("This ticket is no longer valid"); 334 return sessionKey.getKeyType(); 335 } 336 337 342 public final boolean isForwardable() { 343 return flags[FORWARDABLE_TICKET_FLAG]; 344 } 345 346 354 public final boolean isForwarded() { 355 return flags[FORWARDED_TICKET_FLAG]; 356 } 357 358 363 public final boolean isProxiable() { 364 return flags[PROXIABLE_TICKET_FLAG]; 365 } 366 367 372 public final boolean isProxy() { 373 return flags[PROXY_TICKET_FLAG]; 374 } 375 376 377 382 public final boolean isPostdated() { 383 return flags[POSTDATED_TICKET_FLAG]; 384 } 385 386 393 public final boolean isRenewable() { 394 return flags[RENEWABLE_TICKET_FLAG]; 395 } 396 397 404 public final boolean isInitial() { 405 return flags[INITIAL_TICKET_FLAG]; 406 } 407 408 415 public final boolean[] getFlags() { 416 return (flags == null? null: (boolean[]) flags.clone()); 417 } 418 419 425 public final java.util.Date getAuthTime() { 426 return (authTime == null) ? null : new Date (authTime.getTime()); 427 } 428 429 435 public final java.util.Date getStartTime() { 436 return (startTime == null) ? null : new Date (startTime.getTime()); 437 } 438 439 444 public final java.util.Date getEndTime() { 445 return endTime; 446 } 447 448 454 public final java.util.Date getRenewTill() { 455 return (renewTill == null) ? null: new Date (renewTill.getTime()); 456 } 457 458 464 public final java.net.InetAddress [] getClientAddresses() { 465 return (clientAddresses == null? 466 null: (InetAddress []) clientAddresses.clone()); 467 } 468 469 474 public final byte[] getEncoded() { 475 if (destroyed) 476 throw new IllegalStateException ("This ticket is no longer valid"); 477 return (byte[]) asn1Encoding.clone(); 478 } 479 480 481 public boolean isCurrent() { 482 return (System.currentTimeMillis() <= getEndTime().getTime()); 483 } 484 485 504 public void refresh() throws RefreshFailedException { 505 506 if (destroyed) 507 throw new RefreshFailedException ("A destroyed ticket " 508 + "cannot be renewd."); 509 510 if (!isRenewable()) 511 throw new RefreshFailedException ("This ticket is not renewable"); 512 513 if (System.currentTimeMillis() > getRenewTill().getTime()) 514 throw new RefreshFailedException ("This ticket is past " 515 + "its last renewal time."); 516 Throwable e = null; 517 sun.security.krb5.Credentials krb5Creds = null; 518 519 try { 520 krb5Creds = new sun.security.krb5.Credentials(asn1Encoding, 521 client.toString(), 522 server.toString(), 523 sessionKey.getEncoded(), 524 sessionKey.getKeyType(), 525 flags, 526 authTime, 527 startTime, 528 endTime, 529 renewTill, 530 clientAddresses); 531 krb5Creds = krb5Creds.renew(); 532 } catch (sun.security.krb5.KrbException krbException) { 533 e = krbException; 534 } catch (java.io.IOException ioException) { 535 e = ioException; 536 } 537 538 if (e != null) { 539 RefreshFailedException rfException 540 = new RefreshFailedException ("Failed to renew Kerberos Ticket " 541 + "for client " + client 542 + " and server " + server 543 + " - " + e.getMessage()); 544 rfException.initCause(e); 545 throw rfException; 546 } 547 548 551 synchronized (this) { 552 try { 553 this.destroy(); 554 } catch (DestroyFailedException dfException) { 555 } 557 init(krb5Creds.getEncoded(), 558 new KerberosPrincipal (krb5Creds.getClient().getName()), 559 new KerberosPrincipal (krb5Creds.getServer().getName()), 560 krb5Creds.getSessionKey().getBytes(), 561 krb5Creds.getSessionKey().getEType(), 562 krb5Creds.getFlags(), 563 krb5Creds.getAuthTime(), 564 krb5Creds.getStartTime(), 565 krb5Creds.getEndTime(), 566 krb5Creds.getRenewTill(), 567 krb5Creds.getClientAddresses()); 568 destroyed = false; 569 } 570 } 571 572 576 public void destroy() throws DestroyFailedException { 577 if (!destroyed) { 578 Arrays.fill(asn1Encoding, (byte) 0); 579 client = null; 580 server = null; 581 sessionKey.destroy(); 582 flags = null; 583 authTime = null; 584 startTime = null; 585 endTime = null; 586 renewTill = null; 587 clientAddresses = null; 588 destroyed = true; 589 } 590 } 591 592 595 public boolean isDestroyed() { 596 return destroyed; 597 } 598 599 public String toString() { 600 if (destroyed) 601 throw new IllegalStateException ("This ticket is no longer valid"); 602 StringBuffer caddrBuf = new StringBuffer (); 603 if (clientAddresses != null) { 604 for (int i = 0; i < clientAddresses.length; i++) { 605 caddrBuf.append("clientAddresses[" + i + "] = " + 606 clientAddresses[i].toString()); 607 } 608 } 609 return ("Ticket (hex) = " + "\n" + 610 (new HexDumpEncoder()).encode(asn1Encoding) + "\n" + 611 "Client Principal = " + client.toString() + "\n" + 612 "Server Principal = " + server.toString() + "\n" + 613 "Session Key = " + sessionKey.toString() + "\n" + 614 "Forwardable Ticket " + flags[FORWARDABLE_TICKET_FLAG] + "\n" + 615 "Forwarded Ticket " + flags[FORWARDED_TICKET_FLAG] + "\n" + 616 "Proxiable Ticket " + flags[PROXIABLE_TICKET_FLAG] + "\n" + 617 "Proxy Ticket " + flags[PROXY_TICKET_FLAG] + "\n" + 618 "Postdated Ticket " + flags[POSTDATED_TICKET_FLAG] + "\n" + 619 "Renewable Ticket " + flags[RENEWABLE_TICKET_FLAG] + "\n" + 620 "Initial Ticket " + flags[RENEWABLE_TICKET_FLAG] + "\n" + 621 "Auth Time = " + String.valueOf(authTime) + "\n" + 622 "Start Time = " + String.valueOf(startTime) + "\n" + 623 "End Time = " + endTime.toString() + "\n" + 624 "Renew Till = " + String.valueOf(renewTill) + "\n" + 625 "Client Addresses " + 626 (clientAddresses == null ? " Null " : caddrBuf.toString() + 627 "\n")); 628 } 629 630 } 631 | Popular Tags |