2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
1. Redistributions of source code must retain the above copyright notice,
8
this list of conditions and the following disclaimer.
10
2. Redistributions in binary form must reproduce the above copyright
11
notice, this list of conditions and the following disclaimer in
12
the documentation and/or other materials provided with the distribution.
14
3. The names of the authors may not be used to endorse or promote products
15
derived from this software without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
This code is based on jsch (http://www.jcraft.com/jsch).
29
All credit should go to the authors of jsch.
38
public class UserAuthGSSAPIWithMIC : UserAuth
40
private const int SSH_MSG_USERAUTH_GSSAPI_RESPONSE = 60;
42
private const int SSH_MSG_USERAUTH_GSSAPI_TOKEN = 61;
44
private const int SSH_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE = 63;
46
private const int SSH_MSG_USERAUTH_GSSAPI_ERROR = 64;
48
private const int SSH_MSG_USERAUTH_GSSAPI_ERRTOK = 65;
50
private const int SSH_MSG_USERAUTH_GSSAPI_MIC = 66;
52
private static readonly byte[][] supported_oid = new byte[][] { new byte[] { unchecked(
53
(byte)unchecked((int)(0x6))), unchecked((byte)unchecked((int)(0x9))), unchecked(
54
(byte)unchecked((int)(0x2a))), unchecked((byte)unchecked((int)(0x86))), unchecked(
55
(byte)unchecked((int)(0x48))), unchecked((byte)unchecked((int)(0x86))), unchecked(
56
(byte)unchecked((int)(0xf7))), unchecked((byte)unchecked((int)(0x12))), unchecked(
57
(byte)unchecked((int)(0x1))), unchecked((byte)unchecked((int)(0x2))), unchecked(
58
(byte)unchecked((int)(0x2))) } };
60
private static readonly string[] supported_method = new string[] { "gssapi-with-mic.krb5"
63
// OID 1.2.840.113554.1.2.2 in DER
64
/// <exception cref="System.Exception"></exception>
65
public override bool Start(Session session)
68
byte[] _username = Util.Str2byte(username);
70
// byte SSH_MSG_USERAUTH_REQUEST(50)
71
// string user name(in ISO-10646 UTF-8 encoding)
72
// string service name(in US-ASCII)
73
// string "gssapi"(US-ASCII)
74
// uint32 n, the number of OIDs client supports
75
// string[n] mechanism OIDS
76
buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
77
buf.PutString(_username);
78
buf.PutString(Util.Str2byte("ssh-connection"));
79
buf.PutString(Util.Str2byte("gssapi-with-mic"));
80
buf.PutInt(supported_oid.Length);
81
for (int i = 0; i < supported_oid.Length; i++)
83
buf.PutString(supported_oid[i]);
85
session.Write(packet);
90
buf = session.Read(buf);
91
command = buf.GetCommand() & unchecked((int)(0xff));
92
if (command == SSH_MSG_USERAUTH_FAILURE)
96
if (command == SSH_MSG_USERAUTH_GSSAPI_RESPONSE)
101
byte[] message = buf.GetString();
102
for (int i_1 = 0; i_1 < supported_oid.Length; i_1++)
104
if (Util.Array_equals(message, supported_oid[i_1]))
106
method = supported_method[i_1];
117
if (command == SSH_MSG_USERAUTH_BANNER)
122
byte[] _message = buf.GetString();
123
byte[] lang = buf.GetString();
124
string message = Util.Byte2str(_message);
125
if (userinfo != null)
127
userinfo.ShowMessage(message);
133
NSch.GSSContext context = null;
136
Type c = Sharpen.Runtime.GetType(session.GetConfig(method));
137
context = (NSch.GSSContext)(System.Activator.CreateInstance(c));
145
context.Create(username, session.host);
147
catch (JSchException)
151
byte[] token = new byte[0];
152
while (!context.IsEstablished())
156
token = context.Init(token, 0, token.Length);
158
catch (JSchException)
161
// ERRTOK should be sent?
162
// byte SSH_MSG_USERAUTH_GSSAPI_ERRTOK
163
// string error token
169
buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_GSSAPI_TOKEN));
170
buf.PutString(token);
171
session.Write(packet);
173
if (!context.IsEstablished())
175
buf = session.Read(buf);
176
command = buf.GetCommand() & unchecked((int)(0xff));
177
if (command == SSH_MSG_USERAUTH_GSSAPI_ERROR)
179
// uint32 major_status
180
// uint32 minor_status
182
// string language tag
183
buf = session.Read(buf);
184
command = buf.GetCommand() & unchecked((int)(0xff));
189
if (command == SSH_MSG_USERAUTH_GSSAPI_ERRTOK)
191
// string error token
192
buf = session.Read(buf);
193
command = buf.GetCommand() & unchecked((int)(0xff));
197
if (command == SSH_MSG_USERAUTH_FAILURE)
204
token = buf.GetString();
207
Buffer mbuf = new Buffer();
208
// string session identifier
209
// byte SSH_MSG_USERAUTH_REQUEST
212
// string "gssapi-with-mic"
213
mbuf.PutString(session.GetSessionId());
214
mbuf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
215
mbuf.PutString(_username);
216
mbuf.PutString(Util.Str2byte("ssh-connection"));
217
mbuf.PutString(Util.Str2byte("gssapi-with-mic"));
218
byte[] mic = context.GetMIC(mbuf.buffer, 0, mbuf.GetLength());
224
buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_GSSAPI_MIC));
226
session.Write(packet);
228
buf = session.Read(buf);
229
command = buf.GetCommand() & unchecked((int)(0xff));
230
if (command == SSH_MSG_USERAUTH_SUCCESS)
236
if (command == SSH_MSG_USERAUTH_FAILURE)
241
byte[] foo = buf.GetString();
242
int partial_success = buf.GetByte();
243
//System.err.println(new String(foo)+
244
// " partial_success:"+(partial_success!=0));
245
if (partial_success != 0)
247
throw new JSchPartialAuthException(Util.Byte2str(foo));