Skip to content

Commit

Permalink
moving up key decryption to handle openssh v1 format
Browse files Browse the repository at this point in the history
  • Loading branch information
mwiede authored and wiedemam-VU committed Mar 4, 2021
1 parent 6b2cb4a commit 7439f7a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 37 deletions.
77 changes: 41 additions & 36 deletions src/main/java/com/jcraft/jsch/UserAuthPublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public boolean start(Session session) throws Exception{

Vector<Identity> identities=session.getIdentityRepository().getIdentities();

byte[] passphrase=null;
byte[] _username=null;

int command;
Expand Down Expand Up @@ -89,6 +88,10 @@ public boolean start(Session session) throws Exception{

Identity identity=identities.elementAt(i);

//System.err.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted());
decryptKey(session, identity);
//System.err.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted());

String ipkmethod=identity.getAlgName();
String[] ipkmethoda=null;
if(ipkmethod.equals("ssh-rsa")){
Expand Down Expand Up @@ -182,42 +185,7 @@ else if(command==SSH_MSG_USERAUTH_BANNER){
}
}

//System.err.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted());

int count=5;
while(true){
if((identity.isEncrypted() && passphrase==null)){
if(userinfo==null) throw new JSchException("USERAUTH fail");
if(identity.isEncrypted() &&
!userinfo.promptPassphrase("Passphrase for "+identity.getName())){
throw new JSchAuthCancelException("publickey");
//throw new JSchException("USERAUTH cancel");
//break;
}
String _passphrase=userinfo.getPassphrase();
if(_passphrase!=null){
passphrase=Util.str2byte(_passphrase);
}
}

if(!identity.isEncrypted() || passphrase!=null){
if(identity.setPassphrase(passphrase)){
if(passphrase!=null &&
(session.getIdentityRepository() instanceof IdentityRepository.Wrapper)){
((IdentityRepository.Wrapper)session.getIdentityRepository()).check();
}
break;
}
}
Util.bzero(passphrase);
passphrase=null;
count--;
if(count==0)break;
}

Util.bzero(passphrase);
passphrase=null;
//System.err.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted());

if(identity.isEncrypted()) continue;
if(pubkeyblob==null) pubkeyblob=identity.getPublicKeyBlob();
Expand Down Expand Up @@ -322,4 +290,41 @@ else if(command==SSH_MSG_USERAUTH_FAILURE){
}
return false;
}

private void decryptKey(Session session, Identity identity) throws JSchException {
byte[] passphrase=null;
int count=5;
while(true){
if((identity.isEncrypted() && passphrase==null)){
if(userinfo==null) throw new JSchException("USERAUTH fail");
if(identity.isEncrypted() &&
!userinfo.promptPassphrase("Passphrase for "+identity.getName())){
throw new JSchAuthCancelException("publickey");
//throw new JSchException("USERAUTH cancel");
//break;
}
String _passphrase=userinfo.getPassphrase();
if(_passphrase!=null){
passphrase= Util.str2byte(_passphrase);
}
}

if(!identity.isEncrypted() || passphrase!=null){
if(identity.setPassphrase(passphrase)){
if(passphrase!=null &&
(session.getIdentityRepository() instanceof IdentityRepository.Wrapper)){
((IdentityRepository.Wrapper)session.getIdentityRepository()).check();
}
break;
}
}
Util.bzero(passphrase);
passphrase=null;
count--;
if(count==0)break;
}

Util.bzero(passphrase);
passphrase=null;
}
}
53 changes: 53 additions & 0 deletions src/test/java/com/jcraft/jsch/KeyPairIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,59 @@ void connectWithPublicKey(String path, String password, String keyType) throws E

}

@ParameterizedTest
@MethodSource("com.jcraft.jsch.KeyPairTest#keyArgs")
void connectWithPublicKeyAndUserInfo(String path, String password, String keyType) throws Exception {

final JSch jSch = new JSch();

jSch.addIdentity(Paths.get(ClassLoader.getSystemResource(path).toURI()).toFile().getAbsolutePath());

Session session = createSession(jSch);
session.setUserInfo(new UserInfo() {
@Override
public String getPassphrase() {
return password;
}

@Override
public String getPassword() {
return null;
}

@Override
public boolean promptPassword(String message) {
return false;
}

@Override
public boolean promptPassphrase(String message) {
return true;
}

@Override
public boolean promptYesNo(String message) {
return false;
}

@Override
public void showMessage(String message) {

}
});

if (keyType != null) {
session.setConfig("PubkeyAcceptedKeyTypes", keyType);
}
try {
session.connect(2000);
assertTrue(session.isConnected());
} finally {
session.disconnect();
}

}

private JSch createIdentity(String path, String password) throws JSchException, URISyntaxException {
JSch ssh = new JSch();
if (password != null) {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/jcraft/jsch/KeyPairTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.stream.Stream;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down

0 comments on commit 7439f7a

Please sign in to comment.