]>
Commit | Line | Data |
---|---|---|
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ | |
2 | /* | |
3 | Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | 1. Redistributions of source code must retain the above copyright notice, | |
9 | this list of conditions and the following disclaimer. | |
10 | ||
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. | |
14 | ||
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. | |
17 | ||
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. | |
28 | */ | |
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) throws Exception{ | |
37 | super.start(session); | |
38 | ||
39 | Vector identities=session.jsch.identities; | |
40 | ||
41 | byte[] passphrase=null; | |
42 | byte[] _username=null; | |
43 | ||
44 | int command; | |
45 | ||
46 | synchronized(identities){ | |
47 | if(identities.size()<=0){ | |
48 | return false; | |
49 | } | |
50 | ||
51 | _username=Util.str2byte(username); | |
52 | ||
53 | for(int i=0; i<identities.size(); i++){ | |
54 | Identity identity=(Identity)(identities.elementAt(i)); | |
55 | byte[] pubkeyblob=identity.getPublicKeyBlob(); | |
56 | ||
57 | //System.err.println("UserAuthPublicKey: "+identity+" "+pubkeyblob); | |
58 | ||
59 | if(pubkeyblob!=null){ | |
60 | // send | |
61 | // byte SSH_MSG_USERAUTH_REQUEST(50) | |
62 | // string user name | |
63 | // string service name ("ssh-connection") | |
64 | // string "publickey" | |
65 | // boolen FALSE | |
66 | // string plaintext password (ISO-10646 UTF-8) | |
67 | packet.reset(); | |
68 | buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); | |
69 | buf.putString(_username); | |
70 | buf.putString(Util.str2byte("ssh-connection")); | |
71 | buf.putString(Util.str2byte("publickey")); | |
72 | buf.putByte((byte)0); | |
73 | buf.putString(Util.str2byte(identity.getAlgName())); | |
74 | buf.putString(pubkeyblob); | |
75 | session.write(packet); | |
76 | ||
77 | loop1: | |
78 | while(true){ | |
79 | buf=session.read(buf); | |
80 | command=buf.getCommand()&0xff; | |
81 | ||
82 | if(command==SSH_MSG_USERAUTH_PK_OK){ | |
83 | break; | |
84 | } | |
85 | else if(command==SSH_MSG_USERAUTH_FAILURE){ | |
86 | break; | |
87 | } | |
88 | else if(command==SSH_MSG_USERAUTH_BANNER){ | |
89 | buf.getInt(); buf.getByte(); buf.getByte(); | |
90 | byte[] _message=buf.getString(); | |
91 | byte[] lang=buf.getString(); | |
92 | String message=Util.byte2str(_message); | |
93 | if(userinfo!=null){ | |
94 | userinfo.showMessage(message); | |
95 | } | |
96 | continue loop1; | |
97 | } | |
98 | else{ | |
99 | //System.err.println("USERAUTH fail ("+command+")"); | |
100 | //throw new JSchException("USERAUTH fail ("+command+")"); | |
101 | break; | |
102 | } | |
103 | } | |
104 | ||
105 | if(command!=SSH_MSG_USERAUTH_PK_OK){ | |
106 | continue; | |
107 | } | |
108 | } | |
109 | ||
110 | //System.err.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted()); | |
111 | ||
112 | int count=5; | |
113 | while(true){ | |
114 | if((identity.isEncrypted() && passphrase==null)){ | |
115 | if(userinfo==null) throw new JSchException("USERAUTH fail"); | |
116 | if(identity.isEncrypted() && | |
117 | !userinfo.promptPassphrase("Passphrase for "+identity.getName())){ | |
118 | throw new JSchAuthCancelException("publickey"); | |
119 | //throw new JSchException("USERAUTH cancel"); | |
120 | //break; | |
121 | } | |
122 | String _passphrase=userinfo.getPassphrase(); | |
123 | if(_passphrase!=null){ | |
124 | passphrase=Util.str2byte(_passphrase); | |
125 | } | |
126 | } | |
127 | ||
128 | if(!identity.isEncrypted() || passphrase!=null){ | |
129 | if(identity.setPassphrase(passphrase)) | |
130 | break; | |
131 | } | |
132 | Util.bzero(passphrase); | |
133 | passphrase=null; | |
134 | count--; | |
135 | if(count==0)break; | |
136 | } | |
137 | ||
138 | Util.bzero(passphrase); | |
139 | passphrase=null; | |
140 | //System.err.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted()); | |
141 | ||
142 | if(identity.isEncrypted()) continue; | |
143 | if(pubkeyblob==null) pubkeyblob=identity.getPublicKeyBlob(); | |
144 | ||
145 | //System.err.println("UserAuthPublicKey: pubkeyblob="+pubkeyblob); | |
146 | ||
147 | if(pubkeyblob==null) continue; | |
148 | ||
149 | // send | |
150 | // byte SSH_MSG_USERAUTH_REQUEST(50) | |
151 | // string user name | |
152 | // string service name ("ssh-connection") | |
153 | // string "publickey" | |
154 | // boolen TRUE | |
155 | // string plaintext password (ISO-10646 UTF-8) | |
156 | packet.reset(); | |
157 | buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); | |
158 | buf.putString(_username); | |
159 | buf.putString(Util.str2byte("ssh-connection")); | |
160 | buf.putString(Util.str2byte("publickey")); | |
161 | buf.putByte((byte)1); | |
162 | buf.putString(Util.str2byte(identity.getAlgName())); | |
163 | buf.putString(pubkeyblob); | |
164 | ||
165 | // byte[] tmp=new byte[buf.index-5]; | |
166 | // System.arraycopy(buf.buffer, 5, tmp, 0, tmp.length); | |
167 | // buf.putString(signature); | |
168 | ||
169 | byte[] sid=session.getSessionId(); | |
170 | int sidlen=sid.length; | |
171 | byte[] tmp=new byte[4+sidlen+buf.index-5]; | |
172 | tmp[0]=(byte)(sidlen>>>24); | |
173 | tmp[1]=(byte)(sidlen>>>16); | |
174 | tmp[2]=(byte)(sidlen>>>8); | |
175 | tmp[3]=(byte)(sidlen); | |
176 | System.arraycopy(sid, 0, tmp, 4, sidlen); | |
177 | System.arraycopy(buf.buffer, 5, tmp, 4+sidlen, buf.index-5); | |
178 | byte[] signature=identity.getSignature(tmp); | |
179 | if(signature==null){ // for example, too long key length. | |
180 | break; | |
181 | } | |
182 | buf.putString(signature); | |
183 | session.write(packet); | |
184 | ||
185 | loop2: | |
186 | while(true){ | |
187 | buf=session.read(buf); | |
188 | command=buf.getCommand()&0xff; | |
189 | ||
190 | if(command==SSH_MSG_USERAUTH_SUCCESS){ | |
191 | return true; | |
192 | } | |
193 | else if(command==SSH_MSG_USERAUTH_BANNER){ | |
194 | buf.getInt(); buf.getByte(); buf.getByte(); | |
195 | byte[] _message=buf.getString(); | |
196 | byte[] lang=buf.getString(); | |
197 | String message=Util.byte2str(_message); | |
198 | if(userinfo!=null){ | |
199 | userinfo.showMessage(message); | |
200 | } | |
201 | continue loop2; | |
202 | } | |
203 | else if(command==SSH_MSG_USERAUTH_FAILURE){ | |
204 | buf.getInt(); buf.getByte(); buf.getByte(); | |
205 | byte[] foo=buf.getString(); | |
206 | int partial_success=buf.getByte(); | |
207 | //System.err.println(new String(foo)+ | |
208 | // " partial_success:"+(partial_success!=0)); | |
209 | if(partial_success!=0){ | |
210 | throw new JSchPartialAuthException(Util.byte2str(foo)); | |
211 | } | |
212 | break; | |
213 | } | |
214 | //System.err.println("USERAUTH fail ("+command+")"); | |
215 | //throw new JSchException("USERAUTH fail ("+command+")"); | |
216 | break; | |
217 | } | |
218 | } | |
219 | } | |
220 | return false; | |
221 | } | |
222 | } |