jdk/src/share/classes/sun/security/krb5/internal/EncKrbPrivPart.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 73 cf334423502b
permissions -rw-r--r--
Initial load
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
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
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package sun.security.krb5.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.krb5.Asn1Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Implements the ASN.1 EncKrbPrivPart type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * EncKrbPrivPart  ::= [APPLICATION 28] SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *         user-data       [0] OCTET STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *         timestamp       [1] KerberosTime OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *         usec            [2] Microseconds OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *         seq-number      [3] UInt32 OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *         s-address       [4] HostAddress -- sender's addr --,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *         r-address       [5] HostAddress OPTIONAL -- recip's addr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class EncKrbPrivPart {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        public byte[] userData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public KerberosTime timestamp; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public Integer usec; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public Integer seqNumber; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public HostAddress sAddress; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        public HostAddress rAddress; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public EncKrbPrivPart(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                byte[] new_userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                KerberosTime new_timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                Integer new_usec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                Integer new_seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                HostAddress new_sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                HostAddress new_rAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (new_userData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                   userData = new_userData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                timestamp = new_timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                usec = new_usec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                seqNumber = new_seqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                sAddress = new_sAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                rAddress = new_rAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public EncKrbPrivPart(byte[] data) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                init(new DerValue(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public EncKrbPrivPart(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                init(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * Initializes an EncKrbPrivPart object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        private void init(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        DerValue der, subDer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (((encoding.getTag() & (byte)0x1F) != (byte)0x1C)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        || (encoding.isApplication() != true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        || (encoding.isConstructed() != true))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (der.getTag() != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if ((subDer.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        userData = subDer.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                timestamp = KerberosTime.parse(der.getData(), (byte)0x01, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if ((der.getData().peekByte() & 0x1F) == 0x02) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        usec = new Integer(subDer.getData().getBigInteger().intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                else usec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if ((der.getData().peekByte() & 0x1F) == 0x03 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        seqNumber = new Integer(subDer.getData().getBigInteger().intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                else seqNumber = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                sAddress = HostAddress.parse(der.getData(), (byte)0x04, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (der.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        rAddress = HostAddress.parse(der.getData(), (byte)0x05, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (der.getData().available() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        * Encodes an EncKrbPrivPart object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        * @return byte array of encoded EncKrbPrivPart object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                temp.putOctetString(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (timestamp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), timestamp.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                if (usec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        temp.putInteger(BigInteger.valueOf(usec.intValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (seqNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        // encode as an unsigned integer (UInt32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        temp.putInteger(BigInteger.valueOf(seqNumber.longValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), sAddress.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (rAddress != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x05), rAddress.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x1C), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return bytes.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}