Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/EncryptedData.java
41159 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*27* (C) Copyright IBM Corp. 1999 All Rights Reserved.28* Copyright 1997 The Open Group Research Institute. All rights reserved.29*/3031package sun.security.krb5;3233import sun.security.util.*;34import sun.security.krb5.internal.crypto.*;35import sun.security.krb5.internal.*;36import java.io.IOException;37import java.math.BigInteger;3839/**40* This class encapsulates Kerberos encrypted data. It allows41* callers access to both the ASN.1 encoded form of the EncryptedData42* type as well as the raw cipher text.43*/4445public class EncryptedData implements Cloneable {46int eType;47Integer kvno; // optional48byte[] cipher;49byte[] plain; // not part of ASN.1 encoding5051// ----------------+-----------+----------+----------------+---------------52// Encryption type |etype value|block size|minimum pad size|confounder size53// ----------------+-----------+----------+----------------+---------------54public static final int55ETYPE_NULL = 0; // 1 0 056public static final int57ETYPE_DES_CBC_CRC = 1; // 8 4 858public static final int59ETYPE_DES_CBC_MD4 = 2; // 8 0 860public static final int61ETYPE_DES_CBC_MD5 = 3; // 8 0 86263// draft-brezak-win2k-krb-rc4-hmac-04.txt64public static final int65ETYPE_ARCFOUR_HMAC = 23; // 166// NOTE: the exportable RC4-HMAC is not supported;67// it is no longer a usable encryption type68public static final int69ETYPE_ARCFOUR_HMAC_EXP = 24; // 17071// draft-ietf-krb-wg-crypto-07.txt72public static final int73ETYPE_DES3_CBC_HMAC_SHA1_KD = 16; // 8 0 87475// draft-raeburn-krb-rijndael-krb-07.txt76public static final int77ETYPE_AES128_CTS_HMAC_SHA1_96 = 17; // 16 0 1678public static final int79ETYPE_AES256_CTS_HMAC_SHA1_96 = 18; // 16 0 168081// rfc800982public static final int83ETYPE_AES128_CTS_HMAC_SHA256_128 = 19; // 16 0 1684public static final int85ETYPE_AES256_CTS_HMAC_SHA384_192 = 20; // 16 0 168687/* used by self */88private EncryptedData() {89}9091public Object clone() {92EncryptedData new_encryptedData = new EncryptedData();93new_encryptedData.eType = eType;94if (kvno != null) {95new_encryptedData.kvno = kvno.intValue();96}97if (cipher != null) {98new_encryptedData.cipher = new byte[cipher.length];99System.arraycopy(cipher, 0, new_encryptedData.cipher,1000, cipher.length);101}102return new_encryptedData;103}104105// Used by test106public EncryptedData(107int new_eType,108Integer new_kvno,109byte[] new_cipher) {110eType = new_eType;111kvno = new_kvno;112cipher = new_cipher;113}114115/*116// Not used.117public EncryptedData(118EncryptionKey key,119byte[] plaintext)120throws KdcErrException, KrbCryptoException {121EType etypeEngine = EType.getInstance(key.getEType());122cipher = etypeEngine.encrypt(plaintext, key.getBytes());123eType = key.getEType();124kvno = key.getKeyVersionNumber();125}126*/127128// used in KrbApRep, KrbApReq, KrbAsReq, KrbCred, KrbPriv129public EncryptedData(130EncryptionKey key,131byte[] plaintext,132int usage)133throws KdcErrException, KrbCryptoException {134EType etypeEngine = EType.getInstance(key.getEType());135cipher = etypeEngine.encrypt(plaintext, key.getBytes(), usage);136eType = key.getEType();137kvno = key.getKeyVersionNumber();138}139140/*141// Not used.142public EncryptedData(143EncryptionKey key,144byte[] ivec,145byte[] plaintext)146throws KdcErrException, KrbCryptoException {147EType etypeEngine = EType.getInstance(key.getEType());148cipher = etypeEngine.encrypt(plaintext, key.getBytes(), ivec);149eType = key.getEType();150kvno = key.getKeyVersionNumber();151}152*/153154/*155// Not used.156EncryptedData(157StringBuffer password,158byte[] plaintext)159throws KdcErrException, KrbCryptoException {160EncryptionKey key = new EncryptionKey(password);161EType etypeEngine = EType.getInstance(key.getEType());162cipher = etypeEngine.encrypt(plaintext, key.getBytes());163eType = key.getEType();164kvno = key.getKeyVersionNumber();165}166*/167public byte[] decrypt(168EncryptionKey key, int usage)169throws KdcErrException, KrbApErrException, KrbCryptoException {170if (eType != key.getEType()) {171throw new KrbCryptoException(172"EncryptedData is encrypted using keytype " +173EType.toString(eType) +174" but decryption key is of type " +175EType.toString(key.getEType()));176}177178EType etypeEngine = EType.getInstance(eType);179plain = etypeEngine.decrypt(cipher, key.getBytes(), usage);180// The service ticket will be used in S4U2proxy request. Therefore181// the raw ticket is still needed.182//cipher = null;183return etypeEngine.decryptedData(plain);184}185186/*187// currently destructive on cipher188// Not used.189public byte[] decrypt(190EncryptionKey key,191byte[] ivec, int usage)192throws KdcErrException, KrbApErrException, KrbCryptoException {193// XXX check for matching eType and kvno here194EType etypeEngine = EType.getInstance(eType);195plain = etypeEngine.decrypt(cipher, key.getBytes(), ivec, usage);196cipher = null;197return etypeEngine.decryptedData(plain);198}199200// currently destructive on cipher201// Not used.202byte[] decrypt(StringBuffer password)203throws KdcErrException, KrbApErrException, KrbCryptoException {204EncryptionKey key = new EncryptionKey(password);205// XXX check for matching eType here206EType etypeEngine = EType.getInstance(eType);207plain = etypeEngine.decrypt(cipher, key.getBytes());208cipher = null;209return etypeEngine.decryptedData(plain);210}211*/212213private byte[] decryptedData() throws KdcErrException {214if (plain != null) {215EType etypeEngine = EType.getInstance(eType);216return etypeEngine.decryptedData(plain);217}218return null;219}220221/**222* Constructs an instance of EncryptedData type.223* @param encoding a single DER-encoded value.224* @exception Asn1Exception if an error occurs while decoding an225* ASN1 encoded data.226* @exception IOException if an I/O error occurs while reading encoded227* data.228*229*/230/* Used by self */231private EncryptedData(DerValue encoding)232throws Asn1Exception, IOException {233234DerValue der = null;235if (encoding.getTag() != DerValue.tag_Sequence) {236throw new Asn1Exception(Krb5.ASN1_BAD_ID);237}238der = encoding.getData().getDerValue();239if ((der.getTag() & (byte)0x1F) == (byte)0x00) {240eType = (der.getData().getBigInteger()).intValue();241} else {242throw new Asn1Exception(Krb5.ASN1_BAD_ID);243}244245if ((encoding.getData().peekByte() & 0x1F) == 1) {246der = encoding.getData().getDerValue();247int i = (der.getData().getBigInteger()).intValue();248kvno = i;249} else {250kvno = null;251}252der = encoding.getData().getDerValue();253if ((der.getTag() & (byte)0x1F) == (byte)0x02) {254cipher = der.getData().getOctetString();255} else {256throw new Asn1Exception(Krb5.ASN1_BAD_ID);257}258if (encoding.getData().available() > 0) {259throw new Asn1Exception(Krb5.ASN1_BAD_ID);260}261}262263/**264* Returns an ASN.1 encoded EncryptedData type.265*266* <pre>{@code267* EncryptedData ::= SEQUENCE {268* etype [0] Int32 -- EncryptionType --,269* kvno [1] UInt32 OPTIONAL,270* cipher [2] OCTET STRING -- ciphertext271* }272* }</pre>273*274* <p>275* This definition reflects the Network Working Group RFC 4120276* specification available at277* <a href="http://www.ietf.org/rfc/rfc4120.txt">278* http://www.ietf.org/rfc/rfc4120.txt</a>.279*280* @return byte array of encoded EncryptedData object.281* @exception Asn1Exception if an error occurs while decoding an282* ASN1 encoded data.283* @exception IOException if an I/O error occurs while reading284* encoded data.285*286*/287public byte[] asn1Encode() throws Asn1Exception, IOException {288DerOutputStream bytes = new DerOutputStream();289DerOutputStream temp = new DerOutputStream();290temp.putInteger(BigInteger.valueOf(this.eType));291bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,292true, (byte)0x00), temp);293temp = new DerOutputStream();294if (kvno != null) {295// encode as an unsigned integer (UInt32)296temp.putInteger(BigInteger.valueOf(this.kvno.longValue()));297bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,298true, (byte)0x01), temp);299temp = new DerOutputStream();300}301temp.putOctetString(this.cipher);302bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,303(byte)0x02), temp);304temp = new DerOutputStream();305temp.write(DerValue.tag_Sequence, bytes);306return temp.toByteArray();307}308309310/**311* Parse (unmarshal) an EncryptedData from a DER input stream. This form312* parsing might be used when expanding a value which is part of313* a constructed sequence and uses explicitly tagged type.314*315* @param data the Der input stream value, which contains one or more316* marshaled value.317* @param explicitTag tag number.318* @param optional indicate if this data field is optional319* @exception Asn1Exception if an error occurs while decoding an320* ASN1 encoded data.321* @exception IOException if an I/O error occurs while reading322* encoded data.323* @return an instance of EncryptedData.324*325*/326public static EncryptedData parse(DerInputStream data,327byte explicitTag,328boolean optional)329throws Asn1Exception, IOException {330if ((optional) &&331(((byte)data.peekByte() & (byte)0x1F) != explicitTag))332return null;333DerValue der = data.getDerValue();334if (explicitTag != (der.getTag() & (byte)0x1F)) {335throw new Asn1Exception(Krb5.ASN1_BAD_ID);336} else {337DerValue subDer = der.getData().getDerValue();338return new EncryptedData(subDer);339}340}341342/**343* Reset asn.1 data stream after decryption, remove redundant bytes.344* @param data the decrypted data from decrypt().345* @return the reset byte array which holds exactly one asn1 datum346* including its tag and length.347*348*/349public byte[] reset(byte[] data) {350byte[] bytes = null;351// for asn.1 encoded data, we use length field to352// determine the data length and remove redundant paddings.353if ((data[1] & 0xFF) < 128) {354bytes = new byte[data[1] + 2];355System.arraycopy(data, 0, bytes, 0, data[1] + 2);356} else {357if ((data[1] & 0xFF) > 128) {358int len = data[1] & (byte)0x7F;359int result = 0;360for (int i = 0; i < len; i++) {361result |= (data[i + 2] & 0xFF) << (8 * (len - i - 1));362}363bytes = new byte[result + len + 2];364System.arraycopy(data, 0, bytes, 0, result + len + 2);365}366}367return bytes;368}369370public int getEType() {371return eType;372}373374public Integer getKeyVersionNumber() {375return kvno;376}377378/**379* Returns the raw cipher text bytes, not in ASN.1 encoding.380*/381public byte[] getBytes() {382return cipher;383}384}385386387