jdk/src/share/classes/sun/security/krb5/EncryptedData.java
author weijun
Thu, 24 Jun 2010 14:26:35 +0800
changeset 5975 076cd013e5e4
parent 5506 202f599c92aa
child 14413 e954df027393
permissions -rw-r--r--
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false) Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
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.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.internal.*;
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 Kerberos encrypted data. It allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * callers access to both the ASN.1 encoded form of the EncryptedData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * type as well as the raw cipher text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class EncryptedData implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int eType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    Integer kvno; // optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    byte[] cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    byte[] plain; // not part of ASN.1 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // ----------------+-----------+----------+----------------+---------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Encryption type |etype value|block size|minimum pad size|confounder size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // ----------------+-----------+----------+----------------+---------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ETYPE_NULL        = 0;       // 1          0                0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        ETYPE_DES_CBC_CRC = 1;       // 8          4                8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        ETYPE_DES_CBC_MD4 = 2;       // 8          0                8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ETYPE_DES_CBC_MD5 = 3;       // 8          0                8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // draft-brezak-win2k-krb-rc4-hmac-04.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        ETYPE_ARCFOUR_HMAC = 23;     // 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // NOTE: the exportable RC4-HMAC is not supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // it is no longer a usable encryption type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        ETYPE_ARCFOUR_HMAC_EXP = 24; // 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     // draft-ietf-krb-wg-crypto-07.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ETYPE_DES3_CBC_HMAC_SHA1_KD = 16; // 8     0                8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // draft-raeburn-krb-rijndael-krb-07.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         ETYPE_AES128_CTS_HMAC_SHA1_96 = 17; // 16      0           16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         ETYPE_AES256_CTS_HMAC_SHA1_96 = 18; // 16      0           16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /* used by self */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private EncryptedData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        EncryptedData new_encryptedData = new EncryptedData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        new_encryptedData.eType = eType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (kvno != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            new_encryptedData.kvno = new Integer(kvno.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (cipher != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            new_encryptedData.cipher = new byte[cipher.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.arraycopy(cipher, 0, new_encryptedData.cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                             0, cipher.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return new_encryptedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     // Used in JSSE (com.sun.net.ssl.internal.KerberosPreMasterSecret)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public EncryptedData(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                         int new_eType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                         Integer new_kvno,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                         byte[] new_cipher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        eType = new_eType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        kvno = new_kvno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        cipher = new_cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // Not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public EncryptedData(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                         EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                         byte[] plaintext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        throws KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        EType etypeEngine = EType.getInstance(key.getEType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        cipher = etypeEngine.encrypt(plaintext, key.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        eType = key.getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        kvno = key.getKeyVersionNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     // used in KrbApRep, KrbApReq, KrbAsReq, KrbCred, KrbPriv
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     // Used in JSSE (com.sun.net.ssl.internal.KerberosPreMasterSecret)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public EncryptedData(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                         EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                         byte[] plaintext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                         int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        throws KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        EType etypeEngine = EType.getInstance(key.getEType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        cipher = etypeEngine.encrypt(plaintext, key.getBytes(), usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        eType = key.getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        kvno = key.getKeyVersionNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // Not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public EncryptedData(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                         EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                         byte[] ivec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                         byte[] plaintext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        throws KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        EType etypeEngine = EType.getInstance(key.getEType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        cipher = etypeEngine.encrypt(plaintext, key.getBytes(), ivec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        eType = key.getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        kvno = key.getKeyVersionNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // Not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    EncryptedData(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                  StringBuffer password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                  byte[] plaintext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        throws KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        EncryptionKey key = new EncryptionKey(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        EType etypeEngine = EType.getInstance(key.getEType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        cipher = etypeEngine.encrypt(plaintext, key.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        eType = key.getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        kvno = key.getKeyVersionNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // currently destructive on cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public byte[] decrypt(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                          EncryptionKey key, int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        throws KdcErrException, KrbApErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (eType != key.getEType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new KrbCryptoException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    "EncryptedData is encrypted using keytype " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    EType.toString(eType) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    " but decryption key is of type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    EType.toString(key.getEType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            EType etypeEngine = EType.getInstance(eType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            plain = etypeEngine.decrypt(cipher, key.getBytes(), usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            cipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return etypeEngine.decryptedData(plain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    // currently destructive on cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // Not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public byte[] decrypt(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                          EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                          byte[] ivec, int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        throws KdcErrException, KrbApErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // XXX check for matching eType and kvno here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            EType etypeEngine = EType.getInstance(eType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            plain = etypeEngine.decrypt(cipher, key.getBytes(), ivec, usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            cipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return etypeEngine.decryptedData(plain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // currently destructive on cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // Not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    byte[] decrypt(StringBuffer password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        throws KdcErrException, KrbApErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            EncryptionKey key = new EncryptionKey(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // XXX check for matching eType here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            EType etypeEngine = EType.getInstance(eType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            plain = etypeEngine.decrypt(cipher, key.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            cipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return etypeEngine.decryptedData(plain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private byte[] decryptedData() throws KdcErrException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (plain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            EType etypeEngine = EType.getInstance(eType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return etypeEngine.decryptedData(plain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Constructs an instance of EncryptedData type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /* Used by self */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private EncryptedData(DerValue encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        DerValue der = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            eType = (der.getData().getBigInteger()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if ((encoding.getData().peekByte() & 0x1F) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            int i = (der.getData().getBigInteger()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            kvno = new Integer(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            kvno = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if ((der.getTag() & (byte)0x1F) == (byte)0x02) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            cipher = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Returns an ASN.1 encoded EncryptedData type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * EncryptedData   ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *     etype   [0] Int32 -- EncryptionType --,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *     kvno    [1] UInt32 OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *     cipher  [2] OCTET STRING -- ciphertext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return byte array of encoded EncryptedData object.
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        temp.putInteger(BigInteger.valueOf(this.eType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                       true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (kvno != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // encode as an unsigned integer (UInt32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            temp.putInteger(BigInteger.valueOf(this.kvno.longValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                           true, (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        temp.putOctetString(this.cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        (byte)0x02), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return temp.toByteArray();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Parse (unmarshal) an EncryptedData from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *        marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param optional indicate if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @return an instance of EncryptedData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public static EncryptedData parse(DerInputStream data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                      byte explicitTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                      boolean optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if ((optional) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            (((byte)data.peekByte() & (byte)0x1F) != explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return new EncryptedData(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   339
     * Reset asn.1 data stream after decryption, remove redundant bytes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param data the decrypted data from decrypt().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @return the reset byte array which holds exactly one asn1 datum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * including its tag and length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   345
    public byte[] reset(byte[] data) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        byte[]  bytes = null;
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   347
        // for asn.1 encoded data, we use length field to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // determine the data length and remove redundant paddings.
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   349
        if ((data[1] & 0xFF) < 128) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   350
            bytes = new byte[data[1] + 2];
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   351
            System.arraycopy(data, 0, bytes, 0, data[1] + 2);
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   352
        } else {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   353
            if ((data[1] & 0xFF) > 128) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   354
                int len = data[1] & (byte)0x7F;
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   355
                int result = 0;
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   356
                for (int i = 0; i < len; i++) {
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   357
                    result |= (data[i + 2] & 0xFF) << (8 * (len - i - 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   359
                bytes = new byte[result + len + 2];
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   360
                System.arraycopy(data, 0, bytes, 0, result + len + 2);
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   361
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public int getEType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return eType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public Integer getKeyVersionNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return kvno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns the raw cipher text bytes, not in ASN.1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}