1 2 29 30 package com.jcraft.jsch; 31 32 import java.util.Vector ; 33 34 class UserAuthPublicKey extends UserAuth{ 35 36 public boolean start(Session session, UserInfo userinfo) throws Exception { 37 this.userinfo=userinfo; 39 40 Vector identities=session.jsch.identities; 41 42 Packet packet=session.packet; 43 Buffer buf=packet.buffer; 44 45 byte[] passphrase=null; 46 final String username=session.username; 47 48 byte[] _username=null; 49 50 synchronized(identities){ 51 if(identities.size()<=0){ 52 return false; 53 } 54 55 _username=Util.str2byte(username); 56 57 for(int i=0; i<identities.size(); i++){ 58 Identity identity=(Identity)(identities.elementAt(i)); 59 byte[] pubkeyblob=identity.getPublicKeyBlob(); 60 61 63 if(pubkeyblob!=null){ 64 packet.reset(); 72 buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); 73 buf.putString(_username); 74 buf.putString("ssh-connection".getBytes()); 75 buf.putString("publickey".getBytes()); 76 buf.putByte((byte)0); 77 buf.putString(identity.getAlgName().getBytes()); 78 buf.putString(pubkeyblob); 79 session.write(packet); 80 81 loop1: 82 while(true){ 83 buf=session.read(buf); 87 if(buf.buffer[5]==SSH_MSG_USERAUTH_PK_OK){ 89 break; 90 } 91 else if(buf.buffer[5]==SSH_MSG_USERAUTH_FAILURE){ 92 break; 95 } 96 else if(buf.buffer[5]==SSH_MSG_USERAUTH_BANNER){ 97 buf.getInt(); buf.getByte(); buf.getByte(); 98 byte[] _message=buf.getString(); 99 byte[] lang=buf.getString(); 100 String message=null; 101 try{ message=new String (_message, "UTF-8"); } 102 catch(java.io.UnsupportedEncodingException e){ 103 message=new String (_message); 104 } 105 if(userinfo!=null){ 106 userinfo.showMessage(message); 107 } 108 continue loop1; 109 } 110 else{ 111 break; 114 } 115 } 116 if(buf.buffer[5]!=SSH_MSG_USERAUTH_PK_OK){ 117 continue; 118 } 119 } 120 121 123 int count=5; 124 while(true){ 125 if((identity.isEncrypted() && passphrase==null)){ 126 if(userinfo==null) throw new JSchException("USERAUTH fail"); 127 if(identity.isEncrypted() && 128 !userinfo.promptPassphrase("Passphrase for "+identity.getName())){ 129 throw new JSchAuthCancelException("publickey"); 130 } 133 String _passphrase=userinfo.getPassphrase(); 134 if(_passphrase!=null){ 135 passphrase=Util.str2byte(_passphrase); 136 } 137 } 138 139 if(!identity.isEncrypted() || passphrase!=null){ 140 if(identity.setPassphrase(passphrase)) 141 break; 142 } 143 Util.bzero(passphrase); 144 passphrase=null; 145 count--; 146 if(count==0)break; 147 } 148 149 Util.bzero(passphrase); 150 passphrase=null; 151 153 if(identity.isEncrypted()) continue; 154 if(pubkeyblob==null) pubkeyblob=identity.getPublicKeyBlob(); 155 156 158 if(pubkeyblob==null) continue; 159 160 packet.reset(); 168 buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); 169 buf.putString(_username); 170 buf.putString("ssh-connection".getBytes()); 171 buf.putString("publickey".getBytes()); 172 buf.putByte((byte)1); 173 buf.putString(identity.getAlgName().getBytes()); 174 buf.putString(pubkeyblob); 175 176 180 byte[] sid=session.getSessionId(); 181 int sidlen=sid.length; 182 byte[] tmp=new byte[4+sidlen+buf.index-5]; 183 tmp[0]=(byte)(sidlen>>>24); 184 tmp[1]=(byte)(sidlen>>>16); 185 tmp[2]=(byte)(sidlen>>>8); 186 tmp[3]=(byte)(sidlen); 187 System.arraycopy(sid, 0, tmp, 4, sidlen); 188 System.arraycopy(buf.buffer, 5, tmp, 4+sidlen, buf.index-5); 189 byte[] signature=identity.getSignature(tmp); 190 if(signature==null){ break; 192 } 193 buf.putString(signature); 194 session.write(packet); 195 196 loop2: 197 while(true){ 198 buf=session.read(buf); 202 if(buf.buffer[5]==SSH_MSG_USERAUTH_SUCCESS){ 204 return true; 205 } 206 else if(buf.buffer[5]==SSH_MSG_USERAUTH_BANNER){ 207 buf.getInt(); buf.getByte(); buf.getByte(); 208 byte[] _message=buf.getString(); 209 byte[] lang=buf.getString(); 210 String message=null; 211 try{ message=new String (_message, "UTF-8"); } 212 catch(java.io.UnsupportedEncodingException e){ 213 message=new String (_message); 214 } 215 if(userinfo!=null){ 216 userinfo.showMessage(message); 217 } 218 continue loop2; 219 } 220 else if(buf.buffer[5]==SSH_MSG_USERAUTH_FAILURE){ 221 buf.getInt(); buf.getByte(); buf.getByte(); 222 byte[] foo=buf.getString(); 223 int partial_success=buf.getByte(); 224 if(partial_success!=0){ 227 throw new JSchPartialAuthException(new String (foo)); 228 } 229 break; 230 } 231 break; 234 } 235 } 236 } 237 return false; 238 } 239 } 240 | Popular Tags |