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