blob: 713483fe9f40454d95c59b344e3054571b661ff7 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.jgss.krb5;
27
28import org.ietf.jgss.*;
29import java.io.InputStream;
30import java.io.OutputStream;
31import java.io.IOException;
32import java.io.ByteArrayInputStream;
33import sun.security.krb5.*;
34
35class AcceptSecContextToken extends InitialToken {
36
37 private KrbApRep apRep = null;
38
39 /**
40 * Creates an AcceptSecContextToken for the context acceptor to send to
41 * the context initiator.
42 */
43 public AcceptSecContextToken(Krb5Context context,
44 KrbApReq apReq)
45 throws KrbException, IOException {
46
47 /*
48 * RFC 1964, section 1.2 states:
49 * (1) context key: uses Kerberos session key (or subkey, if
50 * present in authenticator emitted by context initiator) directly
51 *
52 * This does not mention context acceptor. Hence we will not
53 * generate a subkey on the acceptor side. Note: Our initiator will
54 * still allow another acceptor to generate a subkey, even though
55 * our acceptor does not do so.
56 */
57 boolean useSubkey = false;
58
59 boolean useSequenceNumber = true;
60
61 apRep = new KrbApRep(apReq, useSequenceNumber, useSubkey);
62
63 context.resetMySequenceNumber(apRep.getSeqNumber().intValue());
64
65 /*
66 * Note: The acceptor side context key was set when the
67 * InitSecContextToken was received.
68 */
69 }
70
71 /**
72 * Creates an AcceptSecContextToken at the context initiator's side
73 * using the bytes received from the acceptor.
74 */
75 public AcceptSecContextToken(Krb5Context context,
76 Credentials serviceCreds, KrbApReq apReq,
77 InputStream is)
78 throws IOException, GSSException, KrbException {
79
80 int tokenId = ((is.read()<<8) | is.read());
81
82 if (tokenId != Krb5Token.AP_REP_ID)
83 throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
84 "AP_REP token id does not match!");
85
86 byte[] apRepBytes =
87 new sun.security.util.DerValue(is).toByteArray();
88
89 KrbApRep apRep = new KrbApRep(apRepBytes, serviceCreds, apReq);
90
91 /*
92 * Allow the context acceptor to set a subkey if desired, even
93 * though our context acceptor will not do so.
94 */
95 EncryptionKey subKey = apRep.getSubKey();
96 if (subKey != null) {
97 context.setKey(subKey);
98 /*
99 System.out.println("\n\nSub-Session key from AP-REP is: " +
100 getHexBytes(subKey.getBytes()) + "\n");
101 */
102 }
103
104 Integer apRepSeqNumber = apRep.getSeqNumber();
105 int peerSeqNumber = (apRepSeqNumber != null ?
106 apRepSeqNumber.intValue() :
107 0);
108 context.resetPeerSequenceNumber(peerSeqNumber);
109 }
110
111 public final byte[] encode() throws IOException {
112 byte[] apRepBytes = apRep.getMessage();
113 byte[] retVal = new byte[2 + apRepBytes.length];
114 writeInt(Krb5Token.AP_REP_ID, retVal, 0);
115 System.arraycopy(apRepBytes, 0, retVal, 2, apRepBytes.length);
116 return retVal;
117 }
118}