1
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3
Copyright (c) 2006-2009 ymnk, JCraft,Inc. All rights reserved.
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
8
1. Redistributions of source code must retain the above copyright notice,
9
this list of conditions and the following disclaimer.
11
2. Redistributions in binary form must reproduce the above copyright
12
notice, this list of conditions and the following disclaimer in
13
the documentation and/or other materials provided with the distribution.
15
3. The names of the authors may not be used to endorse or promote products
16
derived from this software without specific prior written permission.
18
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT
23
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24
OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY OF
25
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
26
NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package com.jcraft.jsch;
32
public class UserAuthGSSAPIWithMIC extends UserAuth {
33
private static final int SSH_MSG_USERAUTH_GSSAPI_RESPONSE= 60;
34
private static final int SSH_MSG_USERAUTH_GSSAPI_TOKEN= 61;
35
private static final int SSH_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE=63;
36
private static final int SSH_MSG_USERAUTH_GSSAPI_ERROR= 64;
37
private static final int SSH_MSG_USERAUTH_GSSAPI_ERRTOK= 65;
38
private static final int SSH_MSG_USERAUTH_GSSAPI_MIC= 66;
40
private static final byte[][] supported_oid={
41
// OID 1.2.840.113554.1.2.2 in DER
42
{(byte)0x6,(byte)0x9,(byte)0x2a,(byte)0x86,(byte)0x48,
43
(byte)0x86,(byte)0xf7,(byte)0x12,(byte)0x1,(byte)0x2,
47
private static final String[] supported_method={
48
"gssapi-with-mic.krb5"
51
public boolean start(Session session)throws Exception{
54
byte[] _username=Util.str2byte(username);
58
// byte SSH_MSG_USERAUTH_REQUEST(50)
59
// string user name(in ISO-10646 UTF-8 encoding)
60
// string service name(in US-ASCII)
61
// string "gssapi"(US-ASCII)
62
// uint32 n, the number of OIDs client supports
63
// string[n] mechanism OIDS
64
buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST);
65
buf.putString(_username);
66
buf.putString("ssh-connection".getBytes());
67
buf.putString("gssapi-with-mic".getBytes());
68
buf.putInt(supported_oid.length);
69
for(int i=0; i<supported_oid.length; i++){
70
buf.putString(supported_oid[i]);
72
session.write(packet);
77
buf=session.read(buf);
78
command=buf.getCommand()&0xff;
80
if(command==SSH_MSG_USERAUTH_FAILURE){
84
if(command==SSH_MSG_USERAUTH_GSSAPI_RESPONSE){
85
buf.getInt(); buf.getByte(); buf.getByte();
86
byte[] message=buf.getString();
88
for(int i=0; i<supported_oid.length; i++){
89
if(Util.array_equals(message, supported_oid[i])){
90
method=supported_method[i];
102
if(command==SSH_MSG_USERAUTH_BANNER){
103
buf.getInt(); buf.getByte(); buf.getByte();
104
byte[] _message=buf.getString();
105
byte[] lang=buf.getString();
106
String message=Util.byte2str(_message);
108
userinfo.showMessage(message);
115
GSSContext context=null;
117
Class c=Class.forName(session.getConfig(method));
118
context=(GSSContext)(c.newInstance());
125
context.create(username, session.host);
127
catch(JSchException e){
131
byte[] token=new byte[0];
133
while(!context.isEstablished()){
135
token=context.init(token, 0, token.length);
137
catch(JSchException e){
139
// ERRTOK should be sent?
140
// byte SSH_MSG_USERAUTH_GSSAPI_ERRTOK
141
// string error token
147
buf.putByte((byte)SSH_MSG_USERAUTH_GSSAPI_TOKEN);
148
buf.putString(token);
149
session.write(packet);
152
if(!context.isEstablished()){
153
buf=session.read(buf);
154
command=buf.getCommand()&0xff;
155
if(command==SSH_MSG_USERAUTH_GSSAPI_ERROR){
156
// uint32 major_status
157
// uint32 minor_status
159
// string language tag
161
buf=session.read(buf);
162
command=buf.getCommand()&0xff;
165
else if(command==SSH_MSG_USERAUTH_GSSAPI_ERRTOK){
166
// string error token
168
buf=session.read(buf);
169
command=buf.getCommand()&0xff;
173
if(command==SSH_MSG_USERAUTH_FAILURE){
177
buf.getInt(); buf.getByte(); buf.getByte();
178
token=buf.getString();
182
Buffer mbuf=new Buffer();
183
// string session identifier
184
// byte SSH_MSG_USERAUTH_REQUEST
187
// string "gssapi-with-mic"
188
mbuf.putString(session.getSessionId());
189
mbuf.putByte((byte)SSH_MSG_USERAUTH_REQUEST);
190
mbuf.putString(_username);
191
mbuf.putString("ssh-connection".getBytes());
192
mbuf.putString("gssapi-with-mic".getBytes());
194
byte[] mic=context.getMIC(mbuf.buffer, 0, mbuf.getLength());
201
buf.putByte((byte)SSH_MSG_USERAUTH_GSSAPI_MIC);
203
session.write(packet);
207
buf=session.read(buf);
208
command=buf.getCommand()&0xff;
210
if(command==SSH_MSG_USERAUTH_SUCCESS){
213
else if(command==SSH_MSG_USERAUTH_FAILURE){
214
buf.getInt(); buf.getByte(); buf.getByte();
215
byte[] foo=buf.getString();
216
int partial_success=buf.getByte();
217
//System.err.println(new String(foo)+
218
// " partial_success:"+(partial_success!=0));
219
if(partial_success!=0){
220
throw new JSchPartialAuthException(new String(foo));