src/java.security.jgss/share/classes/sun/security/krb5/internal/EncAPRepPart.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: 73
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: 73
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: 73
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 73
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 73
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 EncAPRepPart 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
 * 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
 * }
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    49
 * }</pre>
2
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 {
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    58
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    59
    public KerberosTime ctime;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    60
    public int cusec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    EncryptionKey subKey; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    Integer seqNumber; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    64
    public EncAPRepPart(
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    65
            KerberosTime new_ctime,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    66
            int new_cusec,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    67
            EncryptionKey new_subKey,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    68
            Integer new_seqNumber) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    69
        ctime = new_ctime;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    70
        cusec = new_cusec;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    71
        subKey = new_subKey;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    72
        seqNumber = new_seqNumber;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    73
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    75
    public EncAPRepPart(byte[] data)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    76
            throws Asn1Exception, IOException {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    77
        init(new DerValue(data));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    78
    }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    79
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    80
    public EncAPRepPart(DerValue encoding)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    81
            throws Asn1Exception, IOException {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    82
        init(encoding);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    83
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    85
    /**
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    86
     * Initializes an EncaPRepPart object.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    87
     * @param encoding a single DER-encoded value.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    88
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    89
     * @exception IOException if an I/O error occurs while reading encoded data.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    90
     */
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    91
    private void init(DerValue encoding) throws Asn1Exception, IOException {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    92
        DerValue der, subDer;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    93
        if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x1B)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    94
                || (encoding.isApplication() != true)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    95
                || (encoding.isConstructed() != true)) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    96
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        der = encoding.getData().getDerValue();
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    99
        if (der.getTag() != DerValue.tag_Sequence) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   100
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   101
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   102
        ctime = KerberosTime.parse(der.getData(), (byte) 0x00, true);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   103
        subDer = der.getData().getDerValue();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   104
        if ((subDer.getTag() & (byte) 0x1F) == (byte) 0x01) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   105
            cusec = subDer.getData().getBigInteger().intValue();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   106
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   108
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   109
        if (der.getData().available() > 0) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   110
            subKey = EncryptionKey.parse(der.getData(), (byte) 0x02, true);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   111
        } else {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   112
            subKey = null;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   113
            seqNumber = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   115
        if (der.getData().available() > 0) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   116
            subDer = der.getData().getDerValue();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   117
            if ((subDer.getTag() & 0x1F) != 0x03) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   118
                throw new Asn1Exception(Krb5.ASN1_BAD_ID);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   119
            }
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 7977
diff changeset
   120
            seqNumber = subDer.getData().getBigInteger().intValue();
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   121
        } else {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   122
            seqNumber = null;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   123
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   124
        if (der.getData().available() > 0) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   125
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   126
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   127
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   129
    /**
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   130
     * Encodes an EncAPRepPart object.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   131
     * @return byte array of encoded EncAPRepPart object.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   132
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   133
     * @exception IOException if an I/O error occurs while reading encoded data.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   134
     */
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   135
    public byte[] asn1Encode() throws Asn1Exception, IOException {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   136
        Vector<DerValue> v = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        DerOutputStream temp = new DerOutputStream();
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   138
        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   139
                true, (byte) 0x00), ctime.asn1Encode()));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   140
        temp.putInteger(BigInteger.valueOf(cusec));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   141
        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   142
                true, (byte) 0x01), temp.toByteArray()));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   143
        if (subKey != null) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   144
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   145
                    true, (byte) 0x02), subKey.asn1Encode()));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   146
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   147
        if (seqNumber != null) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   148
            temp = new DerOutputStream();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   149
            // encode as an unsigned integer (UInt32)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   150
            temp.putInteger(BigInteger.valueOf(seqNumber.longValue()));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   151
            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   152
                    true, (byte) 0x03), temp.toByteArray()));
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   153
        }
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
   154
        DerValue[] der = new DerValue[v.size()];
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   155
        v.copyInto(der);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   156
        temp = new DerOutputStream();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   157
        temp.putSequence(der);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        DerOutputStream out = new DerOutputStream();
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   159
        out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   160
                true, (byte) 0x1B), temp);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   161
        return out.toByteArray();
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   162
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public final EncryptionKey getSubKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return subKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public final Integer getSeqNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return seqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}