Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/KrbSafe.java
41159 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.krb5.EncryptionKey;34import sun.security.krb5.internal.*;35import sun.security.krb5.internal.crypto.*;36import java.io.IOException;3738class KrbSafe extends KrbAppMessage {3940private byte[] obuf;41private byte[] userData;4243public KrbSafe(byte[] userData,44Credentials creds,45EncryptionKey subKey,46KerberosTime timestamp,47SeqNumber seqNumber,48HostAddress saddr,49HostAddress raddr50) throws KrbException, IOException {51EncryptionKey reqKey = null;52if (subKey != null)53reqKey = subKey;54else55reqKey = creds.key;5657obuf = mk_safe(userData,58reqKey,59timestamp,60seqNumber,61saddr,62raddr63);64}6566public KrbSafe(byte[] msg,67Credentials creds,68EncryptionKey subKey,69SeqNumber seqNumber,70HostAddress saddr,71HostAddress raddr,72boolean timestampRequired,73boolean seqNumberRequired74) throws KrbException, IOException {7576KRBSafe krb_safe = new KRBSafe(msg);7778EncryptionKey reqKey = null;79if (subKey != null)80reqKey = subKey;81else82reqKey = creds.key;8384userData = rd_safe(85krb_safe,86reqKey,87seqNumber,88saddr,89raddr,90timestampRequired,91seqNumberRequired,92creds.client93);94}9596public byte[] getMessage() {97return obuf;98}99100public byte[] getData() {101return userData;102}103104private byte[] mk_safe(byte[] userData,105EncryptionKey key,106KerberosTime timestamp,107SeqNumber seqNumber,108HostAddress sAddress,109HostAddress rAddress110) throws Asn1Exception, IOException, KdcErrException,111KrbApErrException, KrbCryptoException {112113Integer usec = null;114Integer seqno = null;115116if (timestamp != null)117usec = timestamp.getMicroSeconds();118119if (seqNumber != null) {120seqno = seqNumber.current();121seqNumber.step();122}123124KRBSafeBody krb_safeBody =125new KRBSafeBody(userData,126timestamp,127usec,128seqno,129sAddress,130rAddress131);132133byte[] temp = krb_safeBody.asn1Encode();134Checksum cksum = new Checksum(135Checksum.SAFECKSUMTYPE_DEFAULT,136temp,137key,138KeyUsage.KU_KRB_SAFE_CKSUM139);140141KRBSafe krb_safe = new KRBSafe(krb_safeBody, cksum);142143temp = krb_safe.asn1Encode();144145return krb_safe.asn1Encode();146}147148private byte[] rd_safe(KRBSafe krb_safe,149EncryptionKey key,150SeqNumber seqNumber,151HostAddress sAddress,152HostAddress rAddress,153boolean timestampRequired,154boolean seqNumberRequired,155PrincipalName cname156) throws Asn1Exception, KdcErrException,157KrbApErrException, IOException, KrbCryptoException {158159byte[] temp = krb_safe.safeBody.asn1Encode();160161if (!krb_safe.cksum.verifyKeyedChecksum(temp, key,162KeyUsage.KU_KRB_SAFE_CKSUM)) {163throw new KrbApErrException(164Krb5.KRB_AP_ERR_MODIFIED);165}166167check(krb_safe.safeBody.timestamp,168krb_safe.safeBody.usec,169krb_safe.safeBody.seqNumber,170krb_safe.safeBody.sAddress,171krb_safe.safeBody.rAddress,172seqNumber,173sAddress,174rAddress,175timestampRequired,176seqNumberRequired,177cname178);179180return krb_safe.safeBody.userData;181}182}183184185