src/java.security.jgss/share/classes/sun/security/krb5/internal/KDCReqBody.java
author mbalao
Wed, 05 Jun 2019 01:42:11 -0300
changeset 55258 d65d3c37232c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215032: Support Kerberos cross-realm referrals (RFC 6806) Reviewed-by: weijun
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: 2
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: 2
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: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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;
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 KDC-REQ-BODY type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    42
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * KDC-REQ-BODY ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      kdc-options             [0] KDCOptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      cname                   [1] PrincipalName OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *                                    -- Used only in AS-REQ --,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      realm                   [2] Realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *                                    -- Server's realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *                                    -- Also client's in AS-REQ --,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      sname                   [3] PrincipalName OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      from                    [4] KerberosTime OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *      till                    [5] KerberosTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      rtime                   [6] KerberosTime OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      nonce                   [7] UInt32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *      etype                   [8] SEQUENCE OF Int32 -- EncryptionType
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *                                    -- in preference order --,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      addresses               [9] HostAddresses OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *      enc-authorization-data  [10] EncryptedData OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *                                    -- AuthorizationData --,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *      additional-tickets      [11] SEQUENCE OF Ticket OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *                                       -- NOTE: not empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * }
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    63
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public class KDCReqBody {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public KDCOptions kdcOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public PrincipalName cname; //optional in ASReq only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public PrincipalName sname; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public KerberosTime from; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public KerberosTime till;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public KerberosTime rtime; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public HostAddresses addresses; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private int nonce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private int[] eType = null; //a sequence; not optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private EncryptedData encAuthorizationData; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private Ticket[] additionalTickets; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public KDCReqBody(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            KDCOptions new_kdcOptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            PrincipalName new_cname, //optional in ASReq only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            PrincipalName new_sname, //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            KerberosTime new_from, //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            KerberosTime new_till,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            KerberosTime new_rtime, //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int new_nonce,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int[] new_eType, //a sequence; not optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            HostAddresses new_addresses, //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            EncryptedData new_encAuthorizationData, //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            Ticket[] new_additionalTickets //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            ) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        kdcOptions = new_kdcOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        cname = new_cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        sname = new_sname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        from = new_from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        till = new_till;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        rtime = new_rtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        nonce = new_nonce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (new_eType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            eType = new_eType.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        addresses = new_addresses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        encAuthorizationData = new_encAuthorizationData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (new_additionalTickets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            additionalTickets = new Ticket[new_additionalTickets.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            for (int i = 0; i < new_additionalTickets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (new_additionalTickets[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    throw new IOException("Cannot create a KDCReqBody");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    additionalTickets[i] = (Ticket)new_additionalTickets[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Constructs a KDCReqBody object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param encoding a DER-encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param msgType an int indicating whether it's KRB_AS_REQ or KRB_TGS_REQ type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @exception RealmException if an error occurs while constructing a Realm object from the encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public KDCReqBody(DerValue encoding, int msgType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throws Asn1Exception, RealmException, KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        DerValue der, subDer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        addresses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        encAuthorizationData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        additionalTickets = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        kdcOptions = KDCOptions.parse(encoding.getData(), (byte)0x00, false);
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   142
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   143
        // cname only appears in AS-REQ and it shares the realm field with
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   144
        // sname. This is the only place where realm comes after the name.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   145
        // We first give cname a fake realm and reassign it the correct
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   146
        // realm after the realm field is read.
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   147
        cname = PrincipalName.parse(encoding.getData(), (byte)0x01, true,
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   148
                new Realm("PLACEHOLDER"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if ((msgType != Krb5.KRB_AS_REQ) && (cname != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   152
        Realm realm = Realm.parse(encoding.getData(), (byte)0x02, false);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   153
        if (cname != null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   154
            cname = new PrincipalName(
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   155
                    cname.getNameType(), cname.getNameStrings(), realm);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   156
        }
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   157
        sname = PrincipalName.parse(encoding.getData(), (byte)0x03, true, realm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        from = KerberosTime.parse(encoding.getData(), (byte)0x04, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        till = KerberosTime.parse(encoding.getData(), (byte)0x05, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        rtime = KerberosTime.parse(encoding.getData(), (byte)0x06, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if ((der.getTag() & (byte)0x1F) == (byte)0x07) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            nonce = der.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        der = encoding.getData().getDerValue();
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   168
        Vector<Integer> v = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if ((der.getTag() & (byte)0x1F) == (byte)0x08) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (subDer.getTag() == DerValue.tag_SequenceOf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                while(subDer.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    v.addElement(subDer.getData().getBigInteger().intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                eType = new int[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                for (int i = 0; i < v.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    eType[i] = v.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            addresses = HostAddresses.parse(encoding.getData(), (byte)0x09, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            encAuthorizationData = EncryptedData.parse(encoding.getData(), (byte)0x0A, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (encoding.getData().available() > 0) {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   193
            Vector<Ticket> tempTickets = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if ((der.getTag() & (byte)0x1F) == (byte)0x0B) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (subDer.getTag() == DerValue.tag_SequenceOf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    while (subDer.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        tempTickets.addElement(new Ticket(subDer.getData().getDerValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                if (tempTickets.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    additionalTickets = new Ticket[tempTickets.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    tempTickets.copyInto(additionalTickets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Encodes this object to an OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return an byte array of encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public byte[] asn1Encode(int msgType) throws Asn1Exception, IOException {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   226
        Vector<DerValue> v = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), kdcOptions.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (msgType == Krb5.KRB_AS_REQ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), cname.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (sname != null) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   234
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), sname.getRealm().asn1Encode()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), sname.asn1Encode()));
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   236
        } else if (cname != null) {
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 7977
diff changeset
   237
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), cname.getRealm().asn1Encode()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (from != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), from.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x05), till.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (rtime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x06), rtime.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        temp.putInteger(BigInteger.valueOf(nonce));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x07), temp.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        //revisit, if empty eType sequences are allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        for (int i = 0; i < eType.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            temp.putInteger(BigInteger.valueOf(eType[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        DerOutputStream eTypetemp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        eTypetemp.write(DerValue.tag_SequenceOf, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x08), eTypetemp.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x09), addresses.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (encAuthorizationData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x0A), encAuthorizationData.asn1Encode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (additionalTickets != null && additionalTickets.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            for (int i = 0; i < additionalTickets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                temp.write(additionalTickets[i].asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            DerOutputStream ticketsTemp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            ticketsTemp.write(DerValue.tag_SequenceOf, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x0B), ticketsTemp.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
   272
        DerValue[] der = new DerValue[v.size()];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        v.copyInto(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        temp.putSequence(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public int getNonce() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return nonce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}