jdk/src/share/classes/sun/security/krb5/Checksum.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 14327 c0d86f6f8be8
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2011, 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
/*
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.internal.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class encapsulates the concept of a Kerberos checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class Checksum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private int cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private byte[] checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // ----------------------------------------------+-------------+-----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    //                      Checksum type            |sumtype      |checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    //                                               |value        | size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // ----------------------------------------------+-------------+-----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final int CKSUMTYPE_NULL          = 0;               // 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final int CKSUMTYPE_CRC32         = 1;               // 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final int CKSUMTYPE_RSA_MD4       = 2;               // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final int CKSUMTYPE_RSA_MD4_DES   = 3;               // 24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final int CKSUMTYPE_DES_MAC       = 4;               // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final int CKSUMTYPE_DES_MAC_K     = 5;               // 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final int CKSUMTYPE_RSA_MD4_DES_K = 6;               // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int CKSUMTYPE_RSA_MD5       = 7;               // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final int CKSUMTYPE_RSA_MD5_DES   = 8;               // 24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     // draft-ietf-krb-wg-crypto-07.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final int CKSUMTYPE_HMAC_SHA1_DES3_KD = 12;          // 20
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // draft-raeburn-krb-rijndael-krb-07.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final int CKSUMTYPE_HMAC_SHA1_96_AES128 = 15;        // 96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final int CKSUMTYPE_HMAC_SHA1_96_AES256 = 16;        // 96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // draft-brezak-win2k-krb-rc4-hmac-04.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final int CKSUMTYPE_HMAC_MD5_ARCFOUR = -138;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static int CKSUMTYPE_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static int SAFECKSUMTYPE_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Config cfg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            cfg = Config.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            temp = cfg.getDefault("default_checksum", "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (temp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    CKSUMTYPE_DEFAULT = cfg.getType(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                     * If the default checksum is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                     * specified in the configuration we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                     * set it to RSA_MD5. We follow the MIT and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                     * SEAM implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    CKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                System.out.println("Exception in getting default checksum "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                   "value from the configuration " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                   "Setting default checksum to be RSA-MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                exc.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            CKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            temp = cfg.getDefault("safe_checksum_type", "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (temp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    SAFECKSUMTYPE_DEFAULT = cfg.getType(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    SAFECKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5_DES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                System.out.println("Exception in getting safe default " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                   "checksum value " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                   "from the configuration Setting  " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                   "safe default checksum to be RSA-MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                exc.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            SAFECKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5_DES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Constructs a new Checksum using the raw data and type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @data the byte array of checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @new_cksumType the type of checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         // used in InitialToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public Checksum(byte[] data, int new_cksumType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        cksumType = new_cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        checksum = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Constructs a new Checksum by calculating the checksum over the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * using specified checksum type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @new_cksumType the type of checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @data the data that needs to be performed a checksum calculation on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public Checksum(int new_cksumType, byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        cksumType = new_cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        CksumType cksumEngine = CksumType.getInstance(cksumType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (!cksumEngine.isSafe()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            checksum = cksumEngine.calculateChecksum(data, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new KdcErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Constructs a new Checksum by calculating the keyed checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * over the data using specified checksum type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @new_cksumType the type of checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @data the data that needs to be performed a checksum calculation on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         // KrbSafe, KrbTgsReq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public Checksum(int new_cksumType, byte[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        EncryptionKey key, int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        throws KdcErrException, KrbApErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        cksumType = new_cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        CksumType cksumEngine = CksumType.getInstance(cksumType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (!cksumEngine.isSafe())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new KrbApErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        checksum =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            cksumEngine.calculateKeyedChecksum(data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                data.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                key.getBytes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                usage);
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
     * Verifies the keyed checksum over the data passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public boolean verifyKeyedChecksum(byte[] data, EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                        int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throws KdcErrException, KrbApErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        CksumType cksumEngine = CksumType.getInstance(cksumType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (!cksumEngine.isSafe())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new KrbApErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return cksumEngine.verifyKeyedChecksum(data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                               data.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                               key.getBytes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                               checksum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public Checksum(byte[] data) throws KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        this(Checksum.CKSUMTYPE_DEFAULT, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    boolean isEqual(Checksum cksum) throws KdcErrException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (cksumType != cksum.cksumType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        CksumType cksumEngine = CksumType.getInstance(cksumType);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   202
        return CksumType.isChecksumEqual(checksum, cksum.checksum);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Constructs an instance of Checksum from an ASN.1 encoded representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private Checksum(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        DerValue der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            cksumType = der.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            checksum = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Encodes a Checksum object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Checksum    ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *         cksumtype   [0] Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *         checksum    [1] OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return byte array of enocded Checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        temp.putInteger(BigInteger.valueOf(cksumType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                       true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        temp.putOctetString(checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                       true, (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Parse (unmarshal) a checksum object from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param optional indicates if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return an instance of Checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public static Checksum parse(DerInputStream data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                 byte explicitTag, boolean optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if ((optional) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return new Checksum(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Returns the raw bytes of the checksum, not in ASN.1 encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public final byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public final int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    @Override public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (!(obj instanceof Checksum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return isEqual((Checksum)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } catch (KdcErrException kee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    @Override public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        result = 37 * result + cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (checksum != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            result = 37 * result + Arrays.hashCode(checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}