Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/APReq.java
41161 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25*26* (C) Copyright IBM Corp. 1999 All Rights Reserved.27* Copyright 1997 The Open Group Research Institute. All rights reserved.28*/2930package sun.security.krb5.internal;3132import sun.security.krb5.*;33import sun.security.util.*;34import java.io.IOException;35import java.math.BigInteger;3637/**38* Implements the ASN.1 AP-REQ type.39*40* <pre>{@code41* AP-REQ ::= [APPLICATION 14] SEQUENCE {42* pvno [0] INTEGER (5),43* msg-type [1] INTEGER (14),44* ap-options [2] APOptions,45* ticket [3] Ticket,46* authenticator [4] EncryptedData -- Authenticator47* }48* }</pre>49*50* <p>51* This definition reflects the Network Working Group RFC 412052* specification available at53* <a href="http://www.ietf.org/rfc/rfc4120.txt">54* http://www.ietf.org/rfc/rfc4120.txt</a>.55*/56public class APReq {5758public int pvno;59public int msgType;60public APOptions apOptions;61public Ticket ticket;62public EncryptedData authenticator;6364public APReq(65APOptions new_apOptions,66Ticket new_ticket,67EncryptedData new_authenticator) {68pvno = Krb5.PVNO;69msgType = Krb5.KRB_AP_REQ;70apOptions = new_apOptions;71ticket = new_ticket;72authenticator = new_authenticator;73}7475public APReq(byte[] data) throws Asn1Exception, IOException, KrbApErrException, RealmException {76init(new DerValue(data));77}7879public APReq(DerValue encoding) throws Asn1Exception, IOException, KrbApErrException, RealmException {80init(encoding);81}8283/**84* Initializes an APReq object.85* @param encoding a single DER-encoded value.86* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.87* @exception IOException if an I/O error occurs while reading encoded data.88* @exception KrbApErrException if the value read from the DER-encoded data stream does not match the pre-defined value.89* @exception RealmException if an error occurs while parsing a Realm object.90*/91private void init(DerValue encoding) throws Asn1Exception,92IOException, KrbApErrException, RealmException {93DerValue der, subDer;94if (((encoding.getTag() & (byte) 0x1F) != Krb5.KRB_AP_REQ)95|| (encoding.isApplication() != true)96|| (encoding.isConstructed() != true)) {97throw new Asn1Exception(Krb5.ASN1_BAD_ID);98}99der = encoding.getData().getDerValue();100if (der.getTag() != DerValue.tag_Sequence) {101throw new Asn1Exception(Krb5.ASN1_BAD_ID);102}103subDer = der.getData().getDerValue();104if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x00) {105throw new Asn1Exception(Krb5.ASN1_BAD_ID);106}107pvno = subDer.getData().getBigInteger().intValue();108if (pvno != Krb5.PVNO) {109throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);110}111subDer = der.getData().getDerValue();112if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x01) {113throw new Asn1Exception(Krb5.ASN1_BAD_ID);114}115msgType = subDer.getData().getBigInteger().intValue();116if (msgType != Krb5.KRB_AP_REQ) {117throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);118}119apOptions = APOptions.parse(der.getData(), (byte) 0x02, false);120ticket = Ticket.parse(der.getData(), (byte) 0x03, false);121authenticator = EncryptedData.parse(der.getData(), (byte) 0x04, false);122if (der.getData().available() > 0) {123throw new Asn1Exception(Krb5.ASN1_BAD_ID);124}125}126127/**128* Encodes an APReq object.129* @return byte array of encoded APReq object.130* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.131* @exception IOException if an I/O error occurs while reading encoded data.132*/133public byte[] asn1Encode() throws Asn1Exception, IOException {134DerOutputStream bytes = new DerOutputStream();135DerOutputStream temp = new DerOutputStream();136temp.putInteger(BigInteger.valueOf(pvno));137bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);138temp = new DerOutputStream();139temp.putInteger(BigInteger.valueOf(msgType));140bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);141bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), apOptions.asn1Encode());142bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x03), ticket.asn1Encode());143bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x04), authenticator.asn1Encode());144temp = new DerOutputStream();145temp.write(DerValue.tag_Sequence, bytes);146DerOutputStream apreq = new DerOutputStream();147apreq.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x0E), temp);148return apreq.toByteArray();149}150}151152153