1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  *
27  *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
28  *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
29  */
30 
31 package sun.security.krb5.internal.ktab;
32 
33 import sun.security.krb5.*;
34 import sun.security.krb5.internal.*;
35 import java.io.UnsupportedEncodingException;
36 
37 /**
38  * This class represents a Key Table entry. Each entry contains the service principal of
39  * the key, time stamp, key version and secret key itself.
40  *
41  * @author Yanni Zhang
42  */
43 public class KeyTabEntry implements KeyTabConstants {
44     PrincipalName service;
45     Realm realm;
46     KerberosTime timestamp;
47     int keyVersion;
48     int keyType;
49     byte[] keyblock = null;
50     boolean DEBUG = Krb5.DEBUG;
51 
KeyTabEntry(PrincipalName new_service, Realm new_realm, KerberosTime new_time, int new_keyVersion, int new_keyType, byte[] new_keyblock)52     public KeyTabEntry (PrincipalName new_service, Realm new_realm, KerberosTime new_time,
53                         int new_keyVersion, int new_keyType, byte[] new_keyblock) {
54         service = new_service;
55         realm = new_realm;
56         timestamp = new_time;
57         keyVersion = new_keyVersion;
58         keyType = new_keyType;
59         if (new_keyblock != null) {
60             keyblock = new_keyblock.clone();
61         }
62     }
63 
getService()64     public PrincipalName getService() {
65         return service;
66     }
67 
getKey()68     public EncryptionKey getKey() {
69         EncryptionKey key = new EncryptionKey(keyblock,
70                                               keyType,
71                                               keyVersion);
72         return key;
73     }
74 
getKeyString()75     public String getKeyString() {
76         StringBuilder sb = new StringBuilder("0x");
77         for (int i = 0; i < keyblock.length; i++) {
78             sb.append(String.format("%02x", keyblock[i]&0xff));
79         }
80         return sb.toString();
81     }
entryLength()82     public int entryLength() {
83         int totalPrincipalLength = 0;
84         String[] names = service.getNameStrings();
85         for (int i = 0; i < names.length; i++) {
86             try {
87                 totalPrincipalLength += principalSize + names[i].getBytes("8859_1").length;
88             } catch (UnsupportedEncodingException exc) {
89             }
90         }
91 
92         int realmLen = 0;
93         try {
94             realmLen = realm.toString().getBytes("8859_1").length;
95         } catch (UnsupportedEncodingException exc) {
96         }
97 
98         int size = principalComponentSize +  realmSize + realmLen
99             + totalPrincipalLength + principalTypeSize
100             + timestampSize + keyVersionSize
101             + keyTypeSize + keySize + keyblock.length;
102 
103         if (DEBUG) {
104             System.out.println(">>> KeyTabEntry: key tab entry size is " + size);
105         }
106         return size;
107     }
108 
getTimeStamp()109     public KerberosTime getTimeStamp() {
110         return timestamp;
111     }
112 }
113