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