src/java.security.jgss/share/classes/sun/security/krb5/internal/KRBSafeBody.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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.Asn1Exception;
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 KRBSafeBody type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
    42
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * KRB-SAFE-BODY   ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *         user-data       [0] OCTET STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *         timestamp       [1] KerberosTime OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *         usec            [2] Microseconds OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *         seq-number      [3] UInt32 OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *         s-address       [4] HostAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *         r-address       [5] HostAddress OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * }
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
    51
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class KRBSafeBody {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public byte[] userData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public KerberosTime timestamp; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public Integer usec; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public Integer seqNumber; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public HostAddress sAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public HostAddress rAddress; //optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public KRBSafeBody(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                       byte[] new_userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                       KerberosTime new_timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                       Integer new_usec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                       Integer new_seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                       HostAddress new_sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                       HostAddress new_rAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                           ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (new_userData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            userData = new_userData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        timestamp = new_timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        usec = new_usec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        seqNumber = new_seqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        sAddress = new_sAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        rAddress = new_rAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Constructs a KRBSafeBody object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param encoding a Der-encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public KRBSafeBody(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        DerValue der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if ((der.getTag() & 0x1F) == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            userData = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        timestamp = KerberosTime.parse(encoding.getData(), (byte)0x01, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if ((encoding.getData().peekByte() & 0x1F) == 0x02) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            der = encoding.getData().getDerValue();
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   107
            usec = der.getData().getBigInteger().intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if ((encoding.getData().peekByte() & 0x1F) == 0x03) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            der = encoding.getData().getDerValue();
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   111
            seqNumber = der.getData().getBigInteger().intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        sAddress = HostAddress.parse(encoding.getData(), (byte)0x04, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (encoding.getData().available() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            rAddress = HostAddress.parse(encoding.getData(), (byte)0x05, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (encoding.getData().available() > 0)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Encodes an KRBSafeBody object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @return the byte array of encoded KRBSafeBody object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        temp.putOctetString(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (timestamp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), timestamp.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (usec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            temp.putInteger(BigInteger.valueOf(usec.intValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (seqNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // encode as an unsigned integer (UInt32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            temp.putInteger(BigInteger.valueOf(seqNumber.longValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), sAddress.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (rAddress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Parse (unmarshal) a KRBSafeBody from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception Asn1Exception on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param data the Der input stream value, which contains one or more marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param optional indicates if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return an instance of KRBSafeBody.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static KRBSafeBody parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (explicitTag != (der.getTag() & (byte)0x1F))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return new KRBSafeBody(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}