jdk/src/share/classes/sun/security/krb5/internal/APReq.java
author weijun
Fri, 27 Nov 2009 08:51:28 +0800
changeset 4336 4c792c19266e
parent 73 cf334423502b
child 5506 202f599c92aa
permissions -rw-r--r--
6853328: Support OK-AS-DELEGATE flag Reviewed-by: valeriep
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.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 AP-REQ 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
 * AP-REQ               ::= [APPLICATION 14] SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      pvno            [0] INTEGER (5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      msg-type        [1] INTEGER (14),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      ap-options      [2] APOptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      ticket          [3] Ticket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      authenticator   [4] EncryptedData -- Authenticator
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
 */
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    57
public class APReq {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    59
    public int pvno;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    60
    public int msgType;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    61
    public APOptions apOptions;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    62
    public Ticket ticket;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    63
    public EncryptedData authenticator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    65
    public APReq(
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    66
            APOptions new_apOptions,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    67
            Ticket new_ticket,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    68
            EncryptedData new_authenticator) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    69
        pvno = Krb5.PVNO;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    70
        msgType = Krb5.KRB_AP_REQ;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    71
        apOptions = new_apOptions;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    72
        ticket = new_ticket;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    73
        authenticator = new_authenticator;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    74
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    76
    public APReq(byte[] data) throws Asn1Exception, IOException, KrbApErrException, RealmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        init(new DerValue(data));
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    78
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public APReq(DerValue encoding) throws Asn1Exception, IOException, KrbApErrException, RealmException {
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    81
        init(encoding);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    82
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    84
    /**
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    85
     * Initializes an APReq object.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    86
     * @param encoding a single DER-encoded value.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    87
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    88
     * @exception IOException if an I/O error occurs while reading encoded data.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    89
     * @exception KrbApErrException if the value read from the DER-encoded data stream does not match the pre-defined value.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    90
     * @exception RealmException if an error occurs while parsing a Realm object.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    91
     */
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    92
    private void init(DerValue encoding) throws Asn1Exception,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    93
            IOException, KrbApErrException, RealmException {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    94
        DerValue der, subDer;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    95
        if (((encoding.getTag() & (byte) 0x1F) != Krb5.KRB_AP_REQ)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    96
                || (encoding.isApplication() != true)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    97
                || (encoding.isConstructed() != true)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    99
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   100
        der = encoding.getData().getDerValue();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   101
        if (der.getTag() != DerValue.tag_Sequence) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   102
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   103
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   104
        subDer = der.getData().getDerValue();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   105
        if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x00) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   106
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   107
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        pvno = subDer.getData().getBigInteger().intValue();
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   109
        if (pvno != Krb5.PVNO) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   110
            throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   111
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   112
        subDer = der.getData().getDerValue();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   113
        if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x01) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   114
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   116
        msgType = subDer.getData().getBigInteger().intValue();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   117
        if (msgType != Krb5.KRB_AP_REQ) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   118
            throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   119
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   120
        apOptions = APOptions.parse(der.getData(), (byte) 0x02, false);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   121
        ticket = Ticket.parse(der.getData(), (byte) 0x03, false);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   122
        authenticator = EncryptedData.parse(der.getData(), (byte) 0x04, false);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   123
        if (der.getData().available() > 0) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   124
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   125
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   126
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   128
    /**
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   129
     * Encodes an APReq object.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   130
     * @return byte array of encoded APReq object.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   131
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   132
     * @exception IOException if an I/O error occurs while reading encoded data.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   133
     */
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   134
    public byte[] asn1Encode() throws Asn1Exception, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        DerOutputStream bytes = new DerOutputStream();
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   136
        DerOutputStream temp = new DerOutputStream();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   137
        temp.putInteger(BigInteger.valueOf(pvno));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   138
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   139
        temp = new DerOutputStream();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   140
        temp.putInteger(BigInteger.valueOf(msgType));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   141
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   142
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), apOptions.asn1Encode());
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   143
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x03), ticket.asn1Encode());
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   144
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x04), authenticator.asn1Encode());
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   145
        temp = new DerOutputStream();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   146
        temp.write(DerValue.tag_Sequence, bytes);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   147
        DerOutputStream apreq = new DerOutputStream();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   148
        apreq.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x0E), temp);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   149
        return apreq.toByteArray();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   150
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}