Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/krb5/EncryptedData.java
38830 views
/*1* Copyright (c) 2000, 2012, 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/* used by self */82private EncryptedData() {83}8485public Object clone() {86EncryptedData new_encryptedData = new EncryptedData();87new_encryptedData.eType = eType;88if (kvno != null) {89new_encryptedData.kvno = new Integer(kvno.intValue());90}91if (cipher != null) {92new_encryptedData.cipher = new byte[cipher.length];93System.arraycopy(cipher, 0, new_encryptedData.cipher,940, cipher.length);95}96return new_encryptedData;97}9899// Used in JSSE (com.sun.net.ssl.internal.KerberosPreMasterSecret)100public EncryptedData(101int new_eType,102Integer new_kvno,103byte[] new_cipher) {104eType = new_eType;105kvno = new_kvno;106cipher = new_cipher;107}108109/*110// Not used.111public EncryptedData(112EncryptionKey key,113byte[] plaintext)114throws KdcErrException, KrbCryptoException {115EType etypeEngine = EType.getInstance(key.getEType());116cipher = etypeEngine.encrypt(plaintext, key.getBytes());117eType = key.getEType();118kvno = key.getKeyVersionNumber();119}120*/121122// used in KrbApRep, KrbApReq, KrbAsReq, KrbCred, KrbPriv123// Used in JSSE (com.sun.net.ssl.internal.KerberosPreMasterSecret)124public EncryptedData(125EncryptionKey key,126byte[] plaintext,127int usage)128throws KdcErrException, KrbCryptoException {129EType etypeEngine = EType.getInstance(key.getEType());130cipher = etypeEngine.encrypt(plaintext, key.getBytes(), usage);131eType = key.getEType();132kvno = key.getKeyVersionNumber();133}134135/*136// Not used.137public EncryptedData(138EncryptionKey key,139byte[] ivec,140byte[] plaintext)141throws KdcErrException, KrbCryptoException {142EType etypeEngine = EType.getInstance(key.getEType());143cipher = etypeEngine.encrypt(plaintext, key.getBytes(), ivec);144eType = key.getEType();145kvno = key.getKeyVersionNumber();146}147*/148149/*150// Not used.151EncryptedData(152StringBuffer password,153byte[] plaintext)154throws KdcErrException, KrbCryptoException {155EncryptionKey key = new EncryptionKey(password);156EType etypeEngine = EType.getInstance(key.getEType());157cipher = etypeEngine.encrypt(plaintext, key.getBytes());158eType = key.getEType();159kvno = key.getKeyVersionNumber();160}161*/162public byte[] decrypt(163EncryptionKey key, int usage)164throws KdcErrException, KrbApErrException, KrbCryptoException {165if (eType != key.getEType()) {166throw new KrbCryptoException(167"EncryptedData is encrypted using keytype " +168EType.toString(eType) +169" but decryption key is of type " +170EType.toString(key.getEType()));171}172173EType etypeEngine = EType.getInstance(eType);174plain = etypeEngine.decrypt(cipher, key.getBytes(), usage);175// The service ticket will be used in S4U2proxy request. Therefore176// the raw ticket is still needed.177//cipher = null;178return etypeEngine.decryptedData(plain);179}180181/*182// currently destructive on cipher183// Not used.184public byte[] decrypt(185EncryptionKey key,186byte[] ivec, int usage)187throws KdcErrException, KrbApErrException, KrbCryptoException {188// XXX check for matching eType and kvno here189EType etypeEngine = EType.getInstance(eType);190plain = etypeEngine.decrypt(cipher, key.getBytes(), ivec, usage);191cipher = null;192return etypeEngine.decryptedData(plain);193}194195// currently destructive on cipher196// Not used.197byte[] decrypt(StringBuffer password)198throws KdcErrException, KrbApErrException, KrbCryptoException {199EncryptionKey key = new EncryptionKey(password);200// XXX check for matching eType here201EType etypeEngine = EType.getInstance(eType);202plain = etypeEngine.decrypt(cipher, key.getBytes());203cipher = null;204return etypeEngine.decryptedData(plain);205}206*/207208private byte[] decryptedData() throws KdcErrException {209if (plain != null) {210EType etypeEngine = EType.getInstance(eType);211return etypeEngine.decryptedData(plain);212}213return null;214}215216/**217* Constructs an instance of EncryptedData type.218* @param encoding a single DER-encoded value.219* @exception Asn1Exception if an error occurs while decoding an220* ASN1 encoded data.221* @exception IOException if an I/O error occurs while reading encoded222* data.223*224*/225/* Used by self */226private EncryptedData(DerValue encoding)227throws Asn1Exception, IOException {228229DerValue der = null;230if (encoding.getTag() != DerValue.tag_Sequence) {231throw new Asn1Exception(Krb5.ASN1_BAD_ID);232}233der = encoding.getData().getDerValue();234if ((der.getTag() & (byte)0x1F) == (byte)0x00) {235eType = (der.getData().getBigInteger()).intValue();236} else {237throw new Asn1Exception(Krb5.ASN1_BAD_ID);238}239240if ((encoding.getData().peekByte() & 0x1F) == 1) {241der = encoding.getData().getDerValue();242int i = (der.getData().getBigInteger()).intValue();243kvno = new Integer(i);244} else {245kvno = null;246}247der = encoding.getData().getDerValue();248if ((der.getTag() & (byte)0x1F) == (byte)0x02) {249cipher = der.getData().getOctetString();250} else {251throw new Asn1Exception(Krb5.ASN1_BAD_ID);252}253if (encoding.getData().available() > 0) {254throw new Asn1Exception(Krb5.ASN1_BAD_ID);255}256}257258/**259* Returns an ASN.1 encoded EncryptedData type.260*261* <pre>{@code262* EncryptedData ::= SEQUENCE {263* etype [0] Int32 -- EncryptionType --,264* kvno [1] UInt32 OPTIONAL,265* cipher [2] OCTET STRING -- ciphertext266* }267* }</pre>268*269* <p>270* This definition reflects the Network Working Group RFC 4120271* specification available at272* <a href="http://www.ietf.org/rfc/rfc4120.txt">273* http://www.ietf.org/rfc/rfc4120.txt</a>.274*275* @return byte array of encoded EncryptedData object.276* @exception Asn1Exception if an error occurs while decoding an277* ASN1 encoded data.278* @exception IOException if an I/O error occurs while reading279* encoded data.280*281*/282public byte[] asn1Encode() throws Asn1Exception, IOException {283DerOutputStream bytes = new DerOutputStream();284DerOutputStream temp = new DerOutputStream();285temp.putInteger(BigInteger.valueOf(this.eType));286bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,287true, (byte)0x00), temp);288temp = new DerOutputStream();289if (kvno != null) {290// encode as an unsigned integer (UInt32)291temp.putInteger(BigInteger.valueOf(this.kvno.longValue()));292bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,293true, (byte)0x01), temp);294temp = new DerOutputStream();295}296temp.putOctetString(this.cipher);297bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,298(byte)0x02), temp);299temp = new DerOutputStream();300temp.write(DerValue.tag_Sequence, bytes);301return temp.toByteArray();302}303304305/**306* Parse (unmarshal) an EncryptedData from a DER input stream. This form307* parsing might be used when expanding a value which is part of308* a constructed sequence and uses explicitly tagged type.309*310* @param data the Der input stream value, which contains one or more311* marshaled value.312* @param explicitTag tag number.313* @param optional indicate if this data field is optional314* @exception Asn1Exception if an error occurs while decoding an315* ASN1 encoded data.316* @exception IOException if an I/O error occurs while reading317* encoded data.318* @return an instance of EncryptedData.319*320*/321public static EncryptedData parse(DerInputStream data,322byte explicitTag,323boolean optional)324throws Asn1Exception, IOException {325if ((optional) &&326(((byte)data.peekByte() & (byte)0x1F) != explicitTag))327return null;328DerValue der = data.getDerValue();329if (explicitTag != (der.getTag() & (byte)0x1F)) {330throw new Asn1Exception(Krb5.ASN1_BAD_ID);331} else {332DerValue subDer = der.getData().getDerValue();333return new EncryptedData(subDer);334}335}336337/**338* Reset asn.1 data stream after decryption, remove redundant bytes.339* @param data the decrypted data from decrypt().340* @return the reset byte array which holds exactly one asn1 datum341* including its tag and length.342*343*/344public byte[] reset(byte[] data) {345byte[] bytes = null;346// for asn.1 encoded data, we use length field to347// determine the data length and remove redundant paddings.348if ((data[1] & 0xFF) < 128) {349bytes = new byte[data[1] + 2];350System.arraycopy(data, 0, bytes, 0, data[1] + 2);351} else {352if ((data[1] & 0xFF) > 128) {353int len = data[1] & (byte)0x7F;354int result = 0;355for (int i = 0; i < len; i++) {356result |= (data[i + 2] & 0xFF) << (8 * (len - i - 1));357}358bytes = new byte[result + len + 2];359System.arraycopy(data, 0, bytes, 0, result + len + 2);360}361}362return bytes;363}364365public int getEType() {366return eType;367}368369public Integer getKeyVersionNumber() {370return kvno;371}372373/**374* Returns the raw cipher text bytes, not in ASN.1 encoding.375*/376public byte[] getBytes() {377return cipher;378}379}380381382