jdk/src/share/classes/sun/security/jgss/krb5/MessageToken_v2.java
author weijun
Wed, 28 Sep 2011 14:21:10 +0800
changeset 10696 3811a12690ce
parent 7801 814c8359b104
child 10697 ecee258b7d87
permissions -rw-r--r--
7077640: gss wrap for cfx doesn't handle rrc != 0 Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * 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
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.jgss.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ByteArrayInputStream;
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    33
import java.io.ByteArrayOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.MessageDigest;
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    35
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class is a base class for new GSS token definitions, as defined
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    39
 * in RFC 4121, that pertain to per-message GSS-API calls. Conceptually
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    40
 * GSS-API has two types of per-message tokens: WrapToken and MicToken.
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    41
 * They differ in the respect that a WrapToken carries additional plaintext
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    42
 * or ciphertext application data besides just the sequence number and
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    43
 * checksum. This class encapsulates the commonality in the structure of
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    44
 * the WrapToken and the MicToken. This structure can be represented as:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <pre>
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    47
 * Wrap Tokens
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     Octet no   Name        Description
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    ---------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      0..1     TOK_ID     Identification field.  Tokens emitted by
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    52
 *                          GSS_Wrap() contain the hex value 05 04
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    53
 *                          expressed in big-endian order in this field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      2        Flags      Attributes field, as described in section
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *                          4.2.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      3        Filler     Contains the hex value FF.
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    57
 *      4..5     EC         Contains the "extra count" field, in big-
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *                          endian order as described in section 4.2.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *      6..7     RRC        Contains the "right rotation count" in big
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *                          endian order, as described in section 4.2.5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      8..15    SND_SEQ    Sequence number field in clear text,
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    62
 *                          expressed in big-endian order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      16..last Data       Encrypted data for Wrap tokens with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *                          confidentiality, or plaintext data followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *                          by the checksum for Wrap tokens without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *                          confidentiality, as described in section
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *                          4.2.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * MIC Tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     Octet no   Name        Description
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     -----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      0..1     TOK_ID     Identification field.  Tokens emitted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *                          GSS_GetMIC() contain the hex value 04 04
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    74
 *                          expressed in big-endian order in this field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      2        Flags      Attributes field, as described in section
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *                          4.2.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      3..7     Filler     Contains five octets of hex value FF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      8..15    SND_SEQ    Sequence number field in clear text,
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    79
 *                          expressed in big-endian order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      16..last SGN_CKSUM  Checksum of the "to-be-signed" data and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *                          octet 0..15, as described in section 4.2.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p>
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    85
 * This class is the super class of WrapToken_v2 and MicToken_v2. The token's
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    86
 * header (bytes[0..15]) and data (byte[16..]) are saved in tokenHeader and
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    87
 * tokenData fields. Since there is no easy way to find out the exact length
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    88
 * of a WrapToken_v2 token from any header info, in the case of reading from
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    89
 * stream, we read all available() bytes into the token.
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    90
 * <p>
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    91
 * All read actions are performed in this super class. On the write part, the
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    92
 * super class only write the tokenHeader, and the content writing is inside
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
    93
 * child classes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @author Seema Malkani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
abstract class MessageToken_v2 extends Krb5Token {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   100
    protected static final int TOKEN_HEADER_SIZE = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final int TOKEN_ID_POS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final int TOKEN_FLAG_POS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final int TOKEN_EC_POS = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final int TOKEN_RRC_POS = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   106
    /**
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   107
     * The size of the random confounder used in a WrapToken.
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   108
     */
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   109
    protected static final int CONFOUNDER_SIZE = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   111
    // RFC 4121, key usage values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static final int KG_USAGE_ACCEPTOR_SEAL = 22;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static final int KG_USAGE_ACCEPTOR_SIGN = 23;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static final int KG_USAGE_INITIATOR_SEAL = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static final int KG_USAGE_INITIATOR_SIGN = 25;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   117
    // RFC 4121, Flags Field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final int FLAG_SENDER_IS_ACCEPTOR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static final int FLAG_WRAP_CONFIDENTIAL  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final int FLAG_ACCEPTOR_SUBKEY    = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final int FILLER = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   123
    private MessageTokenHeader tokenHeader = null;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   124
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   125
    // Common field
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   126
    private int tokenId = 0;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   127
    private int seqNumber;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   128
    protected byte[] tokenData; // content of token, without the header
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   129
    protected int tokenDataLen;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   130
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   131
    // Key usage number for crypto action
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   132
    private int key_usage = 0;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   133
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   134
    // EC and RRC fields, WrapToken only
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   135
    private int ec = 0;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   136
    private int rrc = 0;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   137
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   138
    // Checksum. Always in MicToken, might be in WrapToken
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   139
    byte[] checksum = null;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   140
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   141
    // Context properties
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   142
    private boolean confState = true;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   143
    private boolean initiator = true;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   144
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   145
    /* cipher instance used by the corresponding GSSContext */
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   146
    CipherHelper cipherHelper = null;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   147
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   149
     * Constructs a MessageToken from a byte array.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param tokenId the token id that should be contained in this token as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * it is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param context the Kerberos context associated with this token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param tokenBytes the byte array containing the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param tokenOffset the offset where the token begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param tokenLen the length of the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param prop the MessageProp structure in which the properties of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * token should be stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws GSSException if there is a problem parsing the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    MessageToken_v2(int tokenId, Krb5Context context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                 byte[] tokenBytes, int tokenOffset, int tokenLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                 MessageProp prop) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this(tokenId, context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
             new ByteArrayInputStream(tokenBytes, tokenOffset, tokenLen),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
             prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Constructs a MessageToken from an InputStream. Bytes will be read on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * demand and the thread might block if there are not enough bytes to
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   172
     * complete the token. Please note there is no accurate way to find out
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   173
     * the size of a token, but we try our best to make sure there is
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   174
     * enough bytes to construct one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param tokenId the token id that should be contained in this token as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * it is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param context the Kerberos context associated with this token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param is the InputStream from which to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param prop the MessageProp structure in which the properties of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * token should be stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws GSSException if there is a problem reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * InputStream or parsing the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    MessageToken_v2(int tokenId, Krb5Context context, InputStream is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                 MessageProp prop) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        init(tokenId, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (!confState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                prop.setPrivacy(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            tokenHeader = new MessageTokenHeader(is, prop, tokenId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // set key_usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (tokenId == Krb5Token.WRAP_ID_v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                key_usage = (!initiator ? KG_USAGE_INITIATOR_SEAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                : KG_USAGE_ACCEPTOR_SEAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            } else if (tokenId == Krb5Token.MIC_ID_v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                key_usage = (!initiator ? KG_USAGE_INITIATOR_SIGN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                : KG_USAGE_ACCEPTOR_SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   204
            int minSize = 0;    // minimal size for token data
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   205
            if (tokenId == Krb5Token.WRAP_ID_v2 && prop.getPrivacy()) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   206
                minSize = CONFOUNDER_SIZE +
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   207
                        TOKEN_HEADER_SIZE + cipherHelper.getChecksumLength();
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   208
            } else {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   209
                minSize = cipherHelper.getChecksumLength();
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   210
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   212
            // Read token data
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   213
            if (tokenId == Krb5Token.MIC_ID_v2) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   214
                // The only case we can precisely predict the token data length
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   215
                tokenDataLen = minSize;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   216
                tokenData = new byte[minSize];
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   217
                readFully(is, tokenData);
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   218
            } else {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   219
                tokenDataLen = is.available();
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   220
                if (tokenDataLen >= minSize) {  // read in one shot
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   221
                    tokenData = new byte[tokenDataLen];
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   222
                    readFully(is, tokenData);
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   223
                } else {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   224
                    byte[] tmp = new byte[minSize];
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   225
                    readFully(is, tmp);
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   226
                    // Hope while blocked in the read above, more data would
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   227
                    // come and is.available() below contains the whole token.
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   228
                    int more = is.available();
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   229
                    tokenDataLen = minSize + more;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   230
                    tokenData = Arrays.copyOf(tmp, tokenDataLen);
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   231
                    readFully(is, tokenData, minSize, more);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   235
            if (tokenId == Krb5Token.WRAP_ID_v2) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   236
                rotate();
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   237
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   239
            if (tokenId == Krb5Token.MIC_ID_v2 ||
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   240
                    (tokenId == Krb5Token.WRAP_ID_v2 && !prop.getPrivacy())) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   241
                // Read checksum
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   242
                int chkLen = cipherHelper.getChecksumLength();
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   243
                checksum = new byte[chkLen];
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   244
                System.arraycopy(tokenData, tokenDataLen-chkLen,
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   245
                        checksum, 0, chkLen);
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   246
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   247
                // validate EC for Wrap tokens without confidentiality
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   248
                if (tokenId == Krb5Token.WRAP_ID_v2 && !prop.getPrivacy()) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   249
                    if (chkLen != ec) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   250
                        throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   251
                            getTokenName(tokenId) + ":" + "EC incorrect!");
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   252
                    }
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   253
                }
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   254
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                getTokenName(tokenId) + ":" + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Used to obtain the token id that was contained in this token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return the token id in the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public final int getTokenId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return tokenId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Used to obtain the key_usage type for this token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @return the key_usage for the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public final int getKeyUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return key_usage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Used to determine if this token contains any encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return true if it contains any encrypted data, false if there is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * plaintext data or if there is no data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public final boolean getConfState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return confState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Generates the checksum field and the sequence number field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param prop the MessageProp structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param data the application data to checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param offset the offset where the data starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param len the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws GSSException if an error occurs in the checksum calculation or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * sequence number calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public void genSignAndSeqNumber(MessageProp prop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                    byte[] data, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        //    debug("Inside MessageToken.genSignAndSeqNumber:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int qop = prop.getQOP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (qop != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            qop = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            prop.setQOP(qop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (!confState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            prop.setPrivacy(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   313
        // Create a new gss token header as defined in RFC 4121
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        tokenHeader = new MessageTokenHeader(tokenId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                prop.getPrivacy(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // debug("\n\t Message Header = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // getHexBytes(tokenHeader.getBytes(), tokenHeader.getBytes().length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // set key_usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (tokenId == Krb5Token.WRAP_ID_v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            key_usage = (initiator ? KG_USAGE_INITIATOR_SEAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                : KG_USAGE_ACCEPTOR_SEAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } else if (tokenId == Krb5Token.MIC_ID_v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            key_usage = (initiator ? KG_USAGE_INITIATOR_SIGN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                : KG_USAGE_ACCEPTOR_SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // Calculate SGN_CKSUM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if ((tokenId == MIC_ID_v2) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            (!prop.getPrivacy() && (tokenId == WRAP_ID_v2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
           checksum = getChecksum(data, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
           // debug("\n\tCalc checksum=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
           //  getHexBytes(checksum, checksum.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // In Wrap tokens without confidentiality, the EC field SHALL be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // to encode the number of octets in the trailing checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (!prop.getPrivacy() && (tokenId == WRAP_ID_v2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            byte[] tok_header = tokenHeader.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            tok_header[4] = (byte) (checksum.length >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            tok_header[5] = (byte) (checksum.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Verifies the validity of checksum field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param data the application data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param offset the offset where the data begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param len the length of the application data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws GSSException if an error occurs in the checksum calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public final boolean verifySign(byte[] data, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // debug("\t====In verifySign:====\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // debug("\t\t checksum:   [" + getHexBytes(checksum) + "]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // debug("\t\t data = [" + getHexBytes(data) + "]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        byte[] myChecksum = getChecksum(data, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // debug("\t\t mychecksum: [" + getHexBytes(myChecksum) +"]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (MessageDigest.isEqual(checksum, myChecksum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // debug("\t\t====Checksum PASS:====\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Rotate bytes as per the "RRC" (Right Rotation Count) received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Our implementation does not do any rotates when sending, only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * when receiving, we rotate left as per the RRC count, to revert it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   376
    private void rotate() {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   377
        if (rrc % tokenDataLen != 0) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   378
           rrc = rrc % tokenDataLen;
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   379
           byte[] newBytes = new byte[tokenDataLen];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   381
           System.arraycopy(tokenData, rrc, newBytes, 0, tokenDataLen-rrc);
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   382
           System.arraycopy(tokenData, 0, newBytes, tokenDataLen-rrc, rrc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   384
           tokenData = newBytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public final int getSequenceNumber() {
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   389
        return seqNumber;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Computes the checksum based on the algorithm stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * tokenHeader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @param data the application data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param offset the offset where the data begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param len the length of the application data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @throws GSSException if an error occurs in the checksum calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    byte[] getChecksum(byte[] data, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        //      debug("Will do getChecksum:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         * For checksum calculation the token header bytes i.e., the first 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * bytes following the GSSHeader, are logically prepended to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * application data to bind the data to this particular token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * Note: There is no such requirement wrt adding padding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * application data for checksumming, although the cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * algorithm used might itself apply some padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        byte[] tokenHeaderBytes = tokenHeader.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // check confidentiality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int conf_flag = tokenHeaderBytes[TOKEN_FLAG_POS] &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                FLAG_WRAP_CONFIDENTIAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
10696
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 7801
diff changeset
   423
        // clear EC and RRC in token header for checksum calculation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if ((conf_flag == 0) && (tokenId == WRAP_ID_v2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            tokenHeaderBytes[4] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            tokenHeaderBytes[5] = 0;
10696
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 7801
diff changeset
   427
            tokenHeaderBytes[6] = 0;
3811a12690ce 7077640: gss wrap for cfx doesn't handle rrc != 0
weijun
parents: 7801
diff changeset
   428
            tokenHeaderBytes[7] = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return cipherHelper.calculateChecksum(tokenHeaderBytes, data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                                offset, len, key_usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Constructs an empty MessageToken for the local context to send to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * the peer. It also increments the local sequence number in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Krb5Context instance it uses after obtaining the object lock for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param tokenId the token id that should be contained in this token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param context the Kerberos context associated with this token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    MessageToken_v2(int tokenId, Krb5Context context) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
          debug("\n============================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
          debug("\nMySessionKey=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
          getHexBytes(context.getMySessionKey().getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
          debug("\nPeerSessionKey=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
          getHexBytes(context.getPeerSessionKey().getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
          debug("\n============================\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        init(tokenId, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        this.seqNumber = context.incrementMySequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private void init(int tokenId, Krb5Context context) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        this.tokenId = tokenId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // Just for consistency check in Wrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        this.confState = context.getConfState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        this.initiator = context.isInitiator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        this.cipherHelper = context.getCipherHelper(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        //    debug("In MessageToken.Cons");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   469
     * Encodes a MessageTokenHeader onto an OutputStream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param os the OutputStream to which this should be written
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   472
     * @throws IOException is an error occurs while writing to the OutputStream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   474
    protected void encodeHeader(OutputStream os) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        tokenHeader.encode(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   479
     * Encodes a MessageToken_v2 onto an OutputStream.
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   480
     *
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   481
     * @param os the OutputStream to which this should be written
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   482
     * @throws IOException is an error occurs while encoding the token
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   484
    public abstract void encode(OutputStream os) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    protected final byte[] getTokenHeader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return (tokenHeader.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    // ******************************************* //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    //  I N N E R    C L A S S E S    F O L L O W
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    // ******************************************* //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * This inner class represents the initial portion of the message token.
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   496
     * It constitutes the first 16 bytes of the message token.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    class MessageTokenHeader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         private int tokenId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         private byte[] bytes = new byte[TOKEN_HEADER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   503
         // Writes a new token header
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         public MessageTokenHeader(int tokenId, boolean conf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                boolean have_acceptor_subkey) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            this.tokenId = tokenId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            bytes[0] = (byte) (tokenId >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            bytes[1] = (byte) (tokenId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // Flags (Note: MIT impl requires subkey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            int flags = 0;
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   514
            flags = (initiator ? 0 : FLAG_SENDER_IS_ACCEPTOR) |
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                     ((conf && tokenId != MIC_ID_v2) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                FLAG_WRAP_CONFIDENTIAL : 0) |
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   517
                     (have_acceptor_subkey ? FLAG_ACCEPTOR_SUBKEY : 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            bytes[2] = (byte) flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            // filler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            bytes[3] = (byte) FILLER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if (tokenId == WRAP_ID_v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                // EC field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                bytes[4] = (byte) 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                bytes[5] = (byte) 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                // RRC field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                bytes[6] = (byte) 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                bytes[7] = (byte) 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            } else if (tokenId == MIC_ID_v2) {
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   531
                // more filler for MicToken
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                for (int i = 4; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    bytes[i] = (byte) FILLER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   537
            // Calculate SND_SEQ, only write 4 bytes from the 12th position
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   538
            writeBigEndian(seqNumber, bytes, 12);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        /**
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   542
         * Reads a MessageTokenHeader from an InputStream and sets the
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   543
         * appropriate confidentiality and quality of protection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         * values in a MessageProp structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
         * @param is the InputStream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         * @param prop the MessageProp to populate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
         * @throws IOException is an error occurs while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
         * InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        public MessageTokenHeader(InputStream is, MessageProp prop, int tokId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            throws IOException, GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            readFully(is, bytes, 0, TOKEN_HEADER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            tokenId = readInt(bytes, TOKEN_ID_POS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   557
            // validate Token ID
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   558
            if (tokenId != tokId) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   559
                throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   560
                    getTokenName(tokenId) + ":" + "Defective Token ID!");
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   561
            }
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   562
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
             * Validate new GSS TokenHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
             */
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   566
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   567
            // valid acceptor_flag
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   568
            // If I am initiator, the received token should have ACCEPTOR on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            int acceptor_flag = (initiator ? FLAG_SENDER_IS_ACCEPTOR : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            int flag = bytes[TOKEN_FLAG_POS] & FLAG_SENDER_IS_ACCEPTOR;
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   571
            if (flag != acceptor_flag) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   573
                        getTokenName(tokenId) + ":" + "Acceptor Flag Error!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            // check for confidentiality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            int conf_flag = bytes[TOKEN_FLAG_POS] & FLAG_WRAP_CONFIDENTIAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            if ((conf_flag == FLAG_WRAP_CONFIDENTIAL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                (tokenId == WRAP_ID_v2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                prop.setPrivacy(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                prop.setPrivacy(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   585
            if (tokenId == WRAP_ID_v2) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   586
                // validate filler
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   587
                if ((bytes[3] & 0xff) != FILLER) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   588
                    throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   589
                        getTokenName(tokenId) + ":" + "Defective Token Filler!");
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   590
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   592
                // read EC field
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   593
                ec = readBigEndian(bytes, TOKEN_EC_POS, 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   595
                // read RRC field
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   596
                rrc = readBigEndian(bytes, TOKEN_RRC_POS, 2);
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   597
            } else if (tokenId == MIC_ID_v2) {
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   598
                for (int i = 3; i < 8; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    if ((bytes[i] & 0xff) != FILLER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        throw new GSSException(GSSException.DEFECTIVE_TOKEN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                -1, getTokenName(tokenId) + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                                "Defective Token Filler!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            // set default QOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            prop.setQOP(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            // sequence number
7801
814c8359b104 6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents: 5506
diff changeset
   611
            seqNumber = readBigEndian(bytes, 0, 8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * Encodes this MessageTokenHeader onto an OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * @param os the OutputStream to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * @throws IOException is an error occurs while writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        public final void encode(OutputStream os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            os.write(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * Returns the token id for the message token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * @return the token id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * @see sun.security.jgss.krb5.Krb5Token#MIC_ID_v2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * @see sun.security.jgss.krb5.Krb5Token#WRAP_ID_v2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        public final int getTokenId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            return tokenId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         * Returns the bytes of this header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
         * @return 8 bytes that form this header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        public final byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    } // end of class MessageTokenHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
}