1/*
2 * Copyright (c) 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34import org.ietf.jgss.*;
35import java.io.*;
36import java.net.Socket;
37import java.net.ServerSocket;
38
39public class jgssapi_server {
40
41    static byte [] getMessage(DataInputStream inStream)
42	throws IOException
43    {
44	byte[] token;
45	token = new byte[inStream.readInt()];
46	inStream.readFully(token);
47	return token;
48    }
49
50    static void putMessage(DataOutputStream outStream, byte [] token)
51	throws IOException
52    {
53	outStream.writeInt(token.length);
54	outStream.write(token);
55    }
56
57
58    public static void main(String[] args)
59	throws IOException, GSSException {
60
61	GSSManager manager = GSSManager.getInstance();
62
63	GSSContext context = manager.createContext((GSSCredential)null);
64
65	byte[] token = null;
66
67	int port = 4717;
68
69	System.out.println("listen on port " + port);
70
71	Socket s = new ServerSocket(port).accept();
72
73	DataInputStream inStream = new DataInputStream(s.getInputStream());
74	DataOutputStream outStream =  new DataOutputStream(s.getOutputStream());
75
76	System.out.println("negotiate context");
77	while (!context.isEstablished()) {
78	    token = getMessage(inStream);
79
80	    token = context.acceptSecContext(token, 0, token.length);
81	    if (token != null)
82		putMessage(outStream, token);
83	}
84
85	System.out.println("done");
86
87	/*
88	 * mic
89	 */
90	System.out.println("mic test");
91
92	System.out.println("  verify mic");
93
94	byte[] intoken = getMessage(inStream);
95	byte[] outtoken = getMessage(inStream);
96	byte[] bytes = null;
97
98	context.verifyMIC(outtoken, 0, outtoken.length,
99			  intoken, 0, intoken.length, new MessageProp(0, false));
100
101	System.out.println("  create mic");
102
103	bytes = new byte[] { 0x66, 0x6f, 0x6f };
104
105	outtoken = context.getMIC(bytes, 0, bytes.length, new MessageProp(0, false));
106	putMessage(outStream, bytes);
107	putMessage(outStream, outtoken);
108
109	/*
110	 * wrap int
111	 */
112	System.out.println("warp int");
113
114	outtoken = getMessage(inStream);
115
116	bytes = context.unwrap(outtoken, 0, outtoken.length, new MessageProp(0, false));
117
118	if (bytes == null)
119	    System.err.println("wrap int failed");
120
121	/*
122	 * wrap conf
123	 */
124	System.out.println("warp conf");
125
126	outtoken = getMessage(inStream);
127
128	bytes = context.unwrap(outtoken, 0, outtoken.length, new MessageProp(0, true));
129
130	if (bytes == null)
131	    System.err.println("wrap conf failed");
132
133
134	/*
135	 * wrap conf
136	 */
137	System.out.println("warp conf");
138	intoken = new byte[] { 0x66, 0x6f, 0x6f };
139	outtoken = context.wrap(intoken, 0, intoken.length, new MessageProp(0, true));
140	putMessage(outStream, outtoken);
141	outtoken = getMessage(inStream);
142
143	context.dispose();
144
145	System.exit(0);
146    }
147}
148
149