1 2 29 30 package com.jcraft.jsch; 31 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; 39 40 private static final byte[][] supported_oid={ 41 {(byte)0x6,(byte)0x9,(byte)0x2a,(byte)0x86,(byte)0x48, 43 (byte)0x86,(byte)0xf7,(byte)0x12,(byte)0x1,(byte)0x2, 44 (byte)0x2} 45 }; 46 47 private static final String [] supported_method={ 48 "gssapi-with-mic.krb5" 49 }; 50 51 public boolean start(Session session, UserInfo userinfo)throws Exception { 52 this.userinfo=userinfo; 53 Packet packet=session.packet; 54 Buffer buf=session.buf; 55 final String username=session.username; 56 57 byte[] _username=Util.str2byte(username); 58 59 packet.reset(); 60 61 buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); 68 buf.putString(_username); 69 buf.putString("ssh-connection".getBytes()); 70 buf.putString("gssapi-with-mic".getBytes()); 71 buf.putInt(supported_oid.length); 72 for(int i=0; i<supported_oid.length; i++){ 73 buf.putString(supported_oid[i]); 74 } 75 session.write(packet); 76 77 String method=null; 78 while(true){ 79 buf=session.read(buf); 80 81 if(buf.buffer[5]==SSH_MSG_USERAUTH_FAILURE){ 82 return false; 83 } 84 85 if(buf.buffer[5]==SSH_MSG_USERAUTH_GSSAPI_RESPONSE){ 86 buf.getInt(); buf.getByte(); buf.getByte(); 87 byte[] message=buf.getString(); 88 89 for(int i=0; i<supported_oid.length; i++){ 90 if(Util.array_equals(message, supported_oid[i])){ 91 method=supported_method[i]; 92 break; 93 } 94 } 95 96 if(method==null){ 97 return false; 98 } 99 100 break; } 102 103 if(buf.buffer[5]==SSH_MSG_USERAUTH_BANNER){ 104 buf.getInt(); buf.getByte(); buf.getByte(); 105 byte[] _message=buf.getString(); 106 byte[] lang=buf.getString(); 107 String message=Util.byte2str(_message); 108 if(userinfo!=null){ 109 userinfo.showMessage(message); 110 } 111 continue; 112 } 113 return false; 114 } 115 116 GSSContext context=null; 117 try{ 118 Class c=Class.forName(session.getConfig(method)); 119 context=(GSSContext)(c.newInstance()); 120 } 121 catch(Exception e){ 122 return false; 123 } 124 125 try{ 126 context.create(username, session.host); 127 } 128 catch(JSchException e){ 129 return false; 130 } 131 132 byte[] token=new byte[0]; 133 134 while(!context.isEstablished()){ 135 try{ 136 token=context.init(token, 0, token.length); 137 } 138 catch(JSchException e){ 139 return false; 144 } 145 146 if(token!=null){ 147 packet.reset(); 148 buf.putByte((byte)SSH_MSG_USERAUTH_GSSAPI_TOKEN); 149 buf.putString(token); 150 session.write(packet); 151 } 152 153 if(!context.isEstablished()){ 154 buf=session.read(buf); 155 156 if(buf.buffer[5]==SSH_MSG_USERAUTH_GSSAPI_ERROR){ 157 162 buf=session.read(buf); 163 } 165 else if(buf.buffer[5]==SSH_MSG_USERAUTH_GSSAPI_ERRTOK){ 166 168 buf=session.read(buf); 169 } 171 172 if(buf.buffer[5]==SSH_MSG_USERAUTH_FAILURE){ 173 return false; 174 } 175 176 buf.getInt(); buf.getByte(); buf.getByte(); 177 token=buf.getString(); 178 } 179 } 180 181 Buffer mbuf=new Buffer(); 182 mbuf.putString(session.getSessionId()); 188 mbuf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); 189 mbuf.putString(_username); 190 mbuf.putString("ssh-connection".getBytes()); 191 mbuf.putString("gssapi-with-mic".getBytes()); 192 193 byte[] mic=context.getMIC(mbuf.buffer, 0, mbuf.getLength()); 194 195 if(mic==null){ 196 return false; 197 } 198 199 packet.reset(); 200 buf.putByte((byte)SSH_MSG_USERAUTH_GSSAPI_MIC); 201 buf.putString(mic); 202 session.write(packet); 203 204 context.dispose(); 205 206 buf=session.read(buf); 207 if(buf.buffer[5]==SSH_MSG_USERAUTH_SUCCESS){ 208 return true; 209 } 210 if(buf.buffer[5]==SSH_MSG_USERAUTH_FAILURE){ 211 buf.getInt(); buf.getByte(); buf.getByte(); 212 byte[] foo=buf.getString(); 213 int partial_success=buf.getByte(); 214 if(partial_success!=0){ 217 throw new JSchPartialAuthException(new String (foo)); 218 } 219 } 220 return false; 221 } 222 } 223 224 225 | Popular Tags |