jdk/src/share/classes/sun/security/krb5/internal/EncAPRepPart.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
 *
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;
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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Implements the ASN.1 EncAPRepPart type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * EncAPRepPart ::= [APPLICATION 27] SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      ctime           [0] KerberosTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      cusec           [1] Microseconds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      subkey          [2] EncryptionKey OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      seq-number      [3] UInt32 OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class EncAPRepPart {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public KerberosTime ctime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public int cusec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    EncryptionKey subKey; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    Integer seqNumber; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public EncAPRepPart(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                KerberosTime new_ctime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                int new_cusec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                EncryptionKey new_subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                Integer new_seqNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                ctime = new_ctime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                cusec = new_cusec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                subKey = new_subKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                seqNumber = new_seqNumber;
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 EncAPRepPart(byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                init(new DerValue(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        public EncAPRepPart(DerValue encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                init(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * Initializes an EncaPRepPart object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        private void init(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                DerValue der, subDer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (((encoding.getTag() & (byte)0x1F) != (byte)0x1B)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             || (encoding.isApplication() != true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        || (encoding.isConstructed() != true))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (der.getTag() != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                ctime = KerberosTime.parse(der.getData(), (byte)0x00, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if ((subDer.getTag() & (byte)0x1F) == (byte)0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        cusec = subDer.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (der.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        subKey = EncryptionKey.parse(der.getData(), (byte)0x02, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        subKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        seqNumber = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (der.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        if ((subDer.getTag() & 0x1F) != 0x03) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        seqNumber = new Integer(subDer.getData().getBigInteger().intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                else seqNumber = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (der.getData().available() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * Encodes an EncAPRepPart object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * @return byte array of encoded EncAPRepPart object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        public byte[] asn1Encode() throws Asn1Exception, IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                Vector<DerValue> v = new Vector<DerValue> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), ctime.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                temp.putInteger(BigInteger.valueOf(cusec));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (subKey != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), subKey.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if (seqNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        // encode as an unsigned integer (UInt32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        temp.putInteger(BigInteger.valueOf(seqNumber.longValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), temp.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                DerValue der[] = new DerValue[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                v.copyInto(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                temp.putSequence(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        out.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x1B), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public final EncryptionKey getSubKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return subKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public final Integer getSeqNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return seqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}