├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src └── main └── java └── org └── vngx └── jsch ├── Buffer.java ├── Channel.java ├── ChannelAgentForwarding.java ├── ChannelDirectTCPIP.java ├── ChannelExec.java ├── ChannelForwardedTCPIP.java ├── ChannelSession.java ├── ChannelSftp.java ├── ChannelShell.java ├── ChannelSubsystem.java ├── ChannelType.java ├── ChannelX11.java ├── ForwardedTCPIPDaemon.java ├── IO.java ├── JSch.java ├── Packet.java ├── PortWatcher.java ├── Request.java ├── RequestAgentForwarding.java ├── RequestEnv.java ├── RequestExec.java ├── RequestPtyReq.java ├── RequestSftp.java ├── RequestShell.java ├── RequestSignal.java ├── RequestSubsystem.java ├── RequestWindowChange.java ├── RequestX11.java ├── Session.java ├── SessionIO.java ├── SftpATTRS.java ├── SftpProgressMonitor.java ├── UIKeyboardInteractive.java ├── UserInfo.java ├── Util.java ├── VersionExchange.java ├── algorithm ├── Algorithm.java ├── AlgorithmFactory.java ├── AlgorithmManager.java ├── Algorithms.java ├── Compression.java ├── CompressionImpl.java ├── DefaultAlgorithmFactory.java ├── KeyPairGenDSA.java ├── KeyPairGenDSAImpl.java ├── KeyPairGenRSA.java ├── KeyPairGenRSAImpl.java ├── Random.java ├── RandomImpl.java ├── SignatureDSA.java ├── SignatureDSAImpl.java ├── SignatureRSA.java ├── SignatureRSAImpl.java └── UnsupportedAlgorithmException.java ├── cipher ├── Cipher.java ├── CipherException.java ├── CipherImpl.java ├── CipherManager.java └── CipherNone.java ├── config ├── BooleanPropertyValidator.java ├── InvalidPropertyException.java ├── JSchConfig.java ├── NumberPropertyValidator.java ├── PropertyValidator.java ├── SSHConfigConstants.java ├── SessionConfig.java └── StringSetPropertyValidator.java ├── constants ├── ConnectionProtocol.java ├── MessageConstants.java ├── SSHConstants.java ├── SftpProtocol.java ├── TransportLayerProtocol.java └── UserAuthProtocol.java ├── exception ├── JSchException.java └── SftpException.java ├── hash ├── Hash.java ├── HashImpl.java ├── HashManager.java ├── MAC.java ├── MACException.java ├── MACImpl.java └── MACImplAlternate.java ├── kex ├── AbstractDHKexAlgorithm.java ├── DHGexSha1KexAlgorithm.java ├── DHGexSha256KexAlgorithm.java ├── DHGroup14KexAlgorithm.java ├── DHGroup1KexAlgorithm.java ├── DHGroupKexAlgorithm.java ├── DiffieHellman.java ├── DiffieHellmanImpl.java ├── KexAlgorithm.java ├── KexException.java ├── KexProposal.java └── KeyExchange.java ├── proxy ├── AbstractProxy.java ├── Proxy.java ├── ProxyHTTP.java ├── ProxySOCKS4.java └── ProxySOCKS5.java ├── userauth ├── AuthCancelException.java ├── GSSContext.java ├── GSSContextKrb5.java ├── Identity.java ├── IdentityFile.java ├── IdentityManager.java ├── PartialAuthException.java ├── UserAuth.java ├── UserAuthGSSAPIWithMIC.java ├── UserAuthKeyboardInteractive.java ├── UserAuthNone.java ├── UserAuthPassword.java └── UserAuthPublicKey.java └── util ├── DataUtil.java ├── HashedHostKey.java ├── HostKey.java ├── HostKeyRepository.java ├── KeyPair.java ├── KeyPairDSA.java ├── KeyPairRSA.java ├── KeyType.java ├── KnownHosts.java ├── Logger.java └── SocketFactory.java /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | target/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 2 | Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | 3. The names of the authors may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, 20 | INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 23 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 26 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | vngx-jsch - A Java SSH implementation 2 | ===================================== 3 | 4 | **vngx-jsch** is an updated version of the popular JSch SSH library 5 | written in pure Java. It has been updated to Java 6 with all the latest 6 | language features and improved code clarity. 7 | 8 | Improvements include: 9 | 10 | * Javadoc comments! Have you ever been frustrated at the lack of comments in the original JSch library? 11 | * Improved error handling - many errors which were silently ignored or masked properly bubble up and offer more detailed descriptions. 12 | * Performance improvements including code optimization and enhanced concurrency practices. 13 | * Enhanced configuration for Sessions and configuration constants for specifying client-defined properties. 14 | * Added support for SHA-256, HMAC-SHA-256, "diffie-hellman-group-exchange-sha256" and "diffie-hellman-group14-sha1" algorithms. 15 | * Added support for more detailed application logging for debugging. 16 | * Updated to more closely follow the official RFC specifications for SSH as well as added detailed documentation from RFCs into the comments. 17 | * Maven build process 18 | * OSGi compatible 19 | 20 | Now available via any central Maven repo 21 | ===================================== 22 | just add the following to your pom.xml under dependencies: 23 | 24 | 25 | org.vngx 26 | vngx-jsch 27 | 0.10 28 | 29 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.vngx 5 | vngx-jsch 6 | bundle 7 | 0.10 8 | vngx-jsch 9 | http://maven.apache.org 10 | **vngx-jsch** (beta) is an updated version of the popular JSch SSH library 11 | written in pure Java. It has been updated to Java 6 with all the latest 12 | language features and improved code clarity. 13 | 14 | 15 | 16 | MIT 17 | http://en.wikipedia.org/wiki/MIT_License 18 | repo 19 | 20 | 21 | 22 | 23 | scm:git:git@github.com:vngx/vngx-jsch.git 24 | scm:git:git@github.com:vngx/vngx-jsch.git 25 | scm:git:git@github.com:vngx/vngx-jsch.git 26 | 27 | 28 | 29 | scott 30 | Scott Rahner 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-compiler-plugin 39 | 2.0.2 40 | 41 | 1.6 42 | 1.6 43 | ${project.build.sourceEncoding} 44 | true 45 | 46 | 47 | 48 | 49 | org.apache.maven.plugins 50 | maven-resources-plugin 51 | 2.2 52 | 53 | ${project.build.sourceEncoding} 54 | 55 | 56 | 57 | 58 | org.apache.felix 59 | maven-bundle-plugin 60 | 1.4.0 61 | true 62 | 63 | 64 | ${project.groupId}.${project.artifactId} 65 | ${project.name} 66 | ${project.version} 67 | ${bundle.namespace}.* 68 | !${bundle.namespace}.*,* 69 | 70 | 71 | 72 | 75 | 76 | org.apache.maven.plugins 77 | maven-dependency-plugin 78 | 2.2 79 | 80 | 81 | copy-dependencies 82 | package 83 | 84 | copy-dependencies 85 | 86 | 87 | ${project.build.directory} 88 | false 89 | false 90 | true 91 | test 92 | provided 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | junit 104 | junit 105 | 4.8.2 106 | test 107 | 108 | 109 | 110 | 111 | com.jcraft.jzlib 112 | com.springsource.com.jcraft.jzlib 113 | 1.0.7 114 | 115 | 116 | 117 | 118 | 119 | 120 | 122 | 123 | com.springsource.repository.bundles.release 124 | SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases 125 | http://repository.springsource.com/maven/bundles/release 126 | 127 | 128 | 129 | com.springsource.repository.bundles.external 130 | SpringSource Enterprise Bundle Repository - External Bundle Releases 131 | http://repository.springsource.com/maven/bundles/external 132 | 133 | 134 | 135 | 136 | 137 | UTF-8 138 | org.vngx.jsch 139 | 140 | 141 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/ChannelExec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | import org.vngx.jsch.exception.JSchException; 33 | import java.io.InputStream; 34 | import java.io.IOException; 35 | import java.io.OutputStream; 36 | 37 | /** 38 | * Implementation of ChannelSession which allows for the execution 39 | * of a single command at a time and pipes the output from command to a stream. 40 | * 41 | * @author Atsuhiko Yamanaka 42 | * @author Michael Laudati 43 | */ 44 | public class ChannelExec extends ChannelSession { 45 | 46 | /** Command to send over channel (by default empty command). */ 47 | private byte[] _command = new byte[0]; 48 | 49 | 50 | /** 51 | * Creates a new instance of ChannelExec. 52 | * 53 | * @param session 54 | */ 55 | ChannelExec(Session session) { 56 | super(session, ChannelType.EXEC); 57 | } 58 | 59 | @Override 60 | public void start() throws JSchException { 61 | try { 62 | sendRequests(); 63 | new RequestExec(_command).request(_session, this); 64 | } catch(JSchException je) { 65 | throw je; 66 | } catch(Exception e) { 67 | throw new JSchException("Failed to start ChannelExec", e); 68 | } 69 | 70 | if( _io.in != null ) { 71 | _thread = new Thread(this, "Exec thread " + _session.getHost()); 72 | _thread.setDaemon(_session.isDaemonThread()); 73 | _thread.start(); 74 | } 75 | } 76 | 77 | /* 78 | * Initializes the channel by setting the input and output streams for the 79 | * channel to the same as used by its session. 80 | */ 81 | @Override 82 | void init() throws JSchException { 83 | _io.setInputStream(_session._in); 84 | _io.setOutputStream(_session._out); 85 | } 86 | 87 | /** 88 | * Sets the command to send over channel. 89 | * 90 | * @param command to send 91 | */ 92 | public void setCommand(String command) { 93 | _command = Util.str2byte(command); 94 | } 95 | 96 | /** 97 | * Sets the command to send over channel. 98 | * 99 | * @param command to send 100 | */ 101 | public void setCommand(byte[] command) { 102 | _command = command; 103 | } 104 | 105 | /** 106 | * Sets the error output stream to use. 107 | * 108 | * @param out 109 | */ 110 | public void setErrStream(OutputStream out) { 111 | setExtOutputStream(out); 112 | } 113 | 114 | /** 115 | * Sets the error output stream to use and specifies if the stream should 116 | * not be closed. 117 | * 118 | * @param out 119 | * @param dontclose 120 | */ 121 | public void setErrStream(OutputStream out, boolean dontclose) { 122 | setExtOutputStream(out, dontclose); 123 | } 124 | 125 | /** 126 | * Returns the error input stream. 127 | * 128 | * @return error input stream 129 | * @throws IOException 130 | */ 131 | public InputStream getErrStream() throws IOException { 132 | return getExtInputStream(); 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/ChannelShell.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | import org.vngx.jsch.exception.JSchException; 33 | 34 | /** 35 | * Implementation of ChannelSession which can be used for creating 36 | * a shell to allow input and output streams for communicating over SSH. 37 | * 38 | * @author Atsuhiko Yamanaka 39 | * @author Michael Laudati 40 | */ 41 | public class ChannelShell extends ChannelSession { 42 | 43 | /** 44 | * Creates a new instance of ChannelShell. 45 | * 46 | * @param session 47 | */ 48 | ChannelShell(Session session) { 49 | super(session, ChannelType.SHELL); 50 | _pty = true; // TODO If pty is always true, setPty() should be overridden to disable 51 | } 52 | 53 | @Override 54 | public void start() throws JSchException { 55 | try { 56 | sendRequests(); 57 | new RequestShell().request(_session, this); 58 | } catch(JSchException e) { 59 | throw e; 60 | } catch(Exception e) { 61 | throw new JSchException("Failed to start ChannelShell", e); 62 | } 63 | 64 | if( _io.in != null ) { 65 | _thread = new Thread(this, "Shell for " + _session.getHost()); 66 | _thread.setDaemon(_session.isDaemonThread()); 67 | _thread.start(); 68 | } 69 | } 70 | 71 | @Override 72 | void init() throws JSchException { 73 | _io.setInputStream(_session._in); 74 | _io.setOutputStream(_session._out); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/ChannelSubsystem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | import org.vngx.jsch.exception.JSchException; 33 | import java.io.IOException; 34 | import java.io.InputStream; 35 | import java.io.OutputStream; 36 | 37 | /** 38 | * Implementation of ChannelSubsystem. 39 | * 40 | * @author Atsuhiko Yamanaka 41 | * @author Michael Laudati 42 | */ 43 | public class ChannelSubsystem extends ChannelSession { 44 | 45 | /** True if channel wants a reply from the server. */ 46 | private boolean _wantReply = true; 47 | /** The subsystem to request from server. */ 48 | private String _subsystem = ""; 49 | 50 | 51 | /** 52 | * Creates a new instance of ChannelSubsystem. 53 | * 54 | * @param session 55 | */ 56 | ChannelSubsystem(Session session) { 57 | super(session, ChannelType.SUBSYSTEM); 58 | } 59 | 60 | @Override 61 | public void start() throws JSchException { 62 | try { 63 | // Send X11 request if x-forwarding is enabled 64 | if( _x11Forwarding ) { 65 | new RequestX11().request(_session, this); 66 | } 67 | // Send Psuedo terminal request if pty is enabled 68 | if( _pty ) { 69 | new RequestPtyReq().request(_session, this); 70 | } 71 | // Send subsystem request 72 | RequestSubsystem subsystemRequest = new RequestSubsystem(); 73 | subsystemRequest.setSubsystem(_subsystem); 74 | subsystemRequest.setReply(_wantReply); 75 | subsystemRequest.request(_session, this); 76 | } catch(JSchException e) { 77 | throw e; 78 | } catch(Exception e) { 79 | throw new JSchException("Failed to start ChannelSubsystem", e); 80 | } 81 | if( _io.in != null ) { 82 | _thread = new Thread(this, "Subsystem for " + _session.getHost()); 83 | _thread.setDaemon(_session.isDaemonThread()); 84 | _thread.start(); 85 | } 86 | } 87 | 88 | /* 89 | * Initializes the IO with the input and output streams from the session. 90 | */ 91 | @Override 92 | void init() throws JSchException { 93 | _io.setInputStream(_session._in); 94 | _io.setOutputStream(_session._out); 95 | } 96 | 97 | /** 98 | * Sets if the channel wants a reply. 99 | * 100 | * @param wantReply 101 | */ 102 | public void setWantReply(boolean wantReply) { 103 | _wantReply = wantReply; 104 | } 105 | 106 | /** 107 | * Sets the subsystem to start. 108 | * 109 | * @param subsystem 110 | */ 111 | public void setSubsystem(String subsystem) { 112 | _subsystem = subsystem; 113 | } 114 | 115 | /** 116 | * Sets the error output stream. 117 | * 118 | * @param out 119 | */ 120 | public void setErrStream(OutputStream out) { 121 | setExtOutputStream(out); 122 | } 123 | 124 | /** 125 | * Returns the error input stream. 126 | * 127 | * @return error input stream 128 | * @throws IOException 129 | */ 130 | public InputStream getErrStream() throws IOException { 131 | return getExtInputStream(); 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/ForwardedTCPIPDaemon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | import java.io.InputStream; 33 | import java.io.OutputStream; 34 | 35 | /** 36 | * 37 | * 38 | * @author Atsuhiko Yamanaka 39 | * @author Michael Laudati 40 | */ 41 | public interface ForwardedTCPIPDaemon extends Runnable { 42 | 43 | void setChannel(ChannelForwardedTCPIP channel, InputStream in, OutputStream out); 44 | 45 | void setArg(Object[] arg); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestAgentForwarding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of Request for sending an agent forwarding 34 | * request to the server. If a channel requests agent forwarding, then the 35 | * Session instance is updated to allow an agent forwarding channel 36 | * request from the server in response.

37 | * 38 | *

RFC 4254 - The Secure Shell 39 | * (SSH) Connection Protocol

40 | * 41 | * @author Atsuhiko Yamanaka 42 | * @author Michael Laudati 43 | */ 44 | final class RequestAgentForwarding extends Request { 45 | 46 | /** Constant name for agent forwarding request. */ 47 | static final String AGENT_FORWARDING_REQUEST = "auth-agent-req@openssh.com"; 48 | 49 | /* 50 | * Create the SSH packet requesting agent forwarding and sends to session. 51 | * This request type does not wait for a response and updates the session 52 | * to indicate agent forwarding. 53 | */ 54 | @Override 55 | void request(Session session, Channel channel) throws Exception { 56 | super.request(session, channel); 57 | 58 | // byte SSH_MSG_CHANNEL_REQUEST(98) 59 | // uint32 recipient channel 60 | // string request type // "auth-agent-req@openssh.com" 61 | // boolean want reply // 0 always false 62 | Buffer buffer = new Buffer(500); 63 | Packet packet = new Packet(buffer); 64 | packet.reset(); 65 | buffer.putByte(SSH_MSG_CHANNEL_REQUEST); 66 | buffer.putInt(channel.getRecipient()); 67 | buffer.putString(AGENT_FORWARDING_REQUEST); 68 | buffer.putBoolean(false); // Want reply HARDCODED to false as per spec 69 | write(packet); 70 | session._agentForwarding = true; // Update session agent forwarding allowed 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestEnv.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of Request for sending requests to set/update 34 | * environment variables.

35 | * 36 | *

Environment variables may be passed to the shell/command to be started 37 | * later. Uncontrolled setting of environment variables in a privileged process 38 | * can be a security hazard. It is recommended that implementations either 39 | * maintain a list of allowable variable names or only set environment variables 40 | * after the server process has dropped sufficient privileges.

41 | * 42 | *

RFC 4254 - The Secure Shell 43 | * (SSH) Connection Protocol

44 | * 45 | * @author Atsuhiko Yamanaka 46 | * @author Michael Laudati 47 | */ 48 | final class RequestEnv extends Request { 49 | 50 | /** Constant name for environment variable request. */ 51 | static final String ENV_REQUEST = "env"; 52 | 53 | /** Environment variable name to set (byte[] instead of String to support different encodings). */ 54 | private byte[] _name = new byte[0]; 55 | /** Environment variable value to set (byte[] instead of String to support different encodings). */ 56 | private byte[] _value = new byte[0]; 57 | 58 | 59 | /** 60 | * Sets the environment variable name and value to send in request. The use 61 | * of byte[] instead of String is to allow for any character encoding. 62 | * 63 | * @param name of environment variable 64 | * @param value of environment variable 65 | */ 66 | void setEnv(byte[] name, byte[] value) { 67 | _name = name; 68 | _value = value; 69 | } 70 | 71 | /** 72 | * Sends a request to update the environment variable. 73 | * 74 | * {@inheritDoc} 75 | * 76 | * @param session 77 | * @param channel 78 | * @throws Exception 79 | */ 80 | @Override 81 | void request(Session session, Channel channel) throws Exception { 82 | super.request(session, channel); 83 | 84 | // byte SSH_MSG_CHANNEL_REQUEST(98) 85 | // uint32 recipient channel 86 | // string request type // "env" 87 | // boolean want reply // 0 88 | // string env name // environment variable name 89 | // string env value // environment variable value 90 | Buffer buffer = new Buffer(200 + _name.length + _value.length); 91 | Packet packet = new Packet(buffer); 92 | packet.reset(); 93 | buffer.putByte(SSH_MSG_CHANNEL_REQUEST); 94 | buffer.putInt(channel.getRecipient()); 95 | buffer.putString(ENV_REQUEST); 96 | buffer.putBoolean(waitForReply()); 97 | buffer.putString(_name); 98 | buffer.putString(_value); 99 | write(packet); 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestExec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of Request for sending an exec request to 34 | * execute a command over a session channel.

35 | * 36 | *

RFC 4254 - The Secure Shell 37 | * (SSH) Connection Protocol

38 | * 39 | * @author Atsuhiko Yamanaka 40 | * @author Michael Laudati 41 | */ 42 | final class RequestExec extends Request { 43 | 44 | /** Constant name for exec request. */ 45 | static final String EXEC_REQUEST = "exec"; 46 | 47 | /** Command value to send as exec request. */ 48 | private final byte[] _command; 49 | 50 | 51 | /** 52 | * Creates a new instance of RequestExec for the specified 53 | * command value. 54 | * 55 | * @param command to send 56 | */ 57 | RequestExec(byte[] command) { 58 | _command = command != null ? command : new byte[0]; 59 | } 60 | 61 | /* 62 | * Sends the exec command request. 63 | */ 64 | @Override 65 | void request(Session session, Channel channel) throws Exception { 66 | super.request(session, channel); 67 | 68 | // send 69 | // byte SSH_MSG_CHANNEL_REQUEST(98) 70 | // uint32 recipient channel 71 | // string request type // "exec" 72 | // boolean want reply // 0 73 | // string command 74 | Buffer buffer = new Buffer(200 + _command.length); 75 | Packet packet = new Packet(buffer); 76 | packet.reset(); 77 | buffer.putByte(SSH_MSG_CHANNEL_REQUEST); 78 | buffer.putInt(channel.getRecipient()); 79 | buffer.putString(EXEC_REQUEST); 80 | buffer.putBoolean(waitForReply()); 81 | buffer.putString(_command); 82 | write(packet); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestSftp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of RequestSubsystem for sending a request for 34 | * SFTP.

35 | * 36 | *

RFC 4254 - The Secure Shell 37 | * (SSH) Connection Protocol

38 | * 39 | * @author Atsuhiko Yamanaka 40 | * @author Michael Laudati 41 | */ 42 | final class RequestSftp extends RequestSubsystem { 43 | 44 | /** Constant name for 'sftp' subsystem name to request. */ 45 | static final String SFTP_REQUEST = "sftp"; 46 | 47 | @Override 48 | void request(Session session, Channel channel) throws Exception { 49 | setSubsystem(SFTP_REQUEST); // Set "sftp" as subsystem to request 50 | setReply(true); // Always wait for reply 51 | super.request(session, channel); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestShell.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of Request to request a shell.

34 | * 35 | *

RFC 4254 - The Secure Shell 36 | * (SSH) Connection Protocol

37 | * 38 | * @author Atsuhiko Yamanaka 39 | * @author Michael Laudati 40 | */ 41 | final class RequestShell extends Request { 42 | 43 | /** Constant name for shell request. */ 44 | static final String SHELL_REQUEST = "shell"; 45 | 46 | @Override 47 | void request(Session session, Channel channel) throws Exception { 48 | super.request(session, channel); 49 | 50 | // send 51 | // byte SSH_MSG_CHANNEL_REQUEST(98) 52 | // uint32 recipient channel 53 | // string request type // "shell" 54 | // boolean want reply // 0 55 | Buffer buffer = new Buffer(150); 56 | Packet packet = new Packet(buffer); 57 | packet.reset(); 58 | buffer.putByte(SSH_MSG_CHANNEL_REQUEST); 59 | buffer.putInt(channel.getRecipient()); 60 | buffer.putString(SHELL_REQUEST); 61 | buffer.putBoolean(waitForReply()); 62 | write(packet); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestSignal.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of Request for sending a signal. A signal can 34 | * be delivered to the remote process/service using the following message. Some 35 | * systems may not implement signals, in which case they SHOULD ignore this 36 | * message.

37 | * 38 | *

RFC 4254 - The Secure Shell 39 | * (SSH) Connection Protocol

40 | * 41 | * @author Atsuhiko Yamanaka 42 | * @author Michael Laudati 43 | */ 44 | final class RequestSignal extends Request { 45 | 46 | /** Constant name for signal request. */ 47 | static final String SIGNAL_REQUEST = "signal"; 48 | 49 | /** Signal value to send in request. */ 50 | private String _signal = "KILL"; // TODO Extract value to signal constants 51 | 52 | 53 | /** 54 | * Sets the signal value to request. 55 | * 56 | * @param signal to send 57 | */ 58 | void setSignal(String signal) { 59 | _signal = signal; 60 | } 61 | 62 | /* 63 | * Sends the signal request. 64 | */ 65 | @Override 66 | void request(Session session, Channel channel) throws Exception { 67 | super.request(session, channel); 68 | 69 | // byte SSH_MSG_CHANNEL_REQUEST(98) 70 | // uint32 recipient channel 71 | // string request type // "signal" 72 | // boolean want reply // 0 73 | // string signal 74 | Buffer buffer = new Buffer(150 + _signal.length()); 75 | Packet packet = new Packet(buffer); 76 | packet.reset(); 77 | buffer.putByte(SSH_MSG_CHANNEL_REQUEST); 78 | buffer.putInt(channel.getRecipient()); 79 | buffer.putString(SIGNAL_REQUEST); 80 | buffer.putBoolean(waitForReply()); 81 | buffer.putString(_signal); 82 | write(packet); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestSubsystem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of Request for requesting to start a subsystem 34 | * over the session.

35 | * 36 | *

RFC 4254 - The Secure Shell 37 | * (SSH) Connection Protocol

38 | * 39 | * @author Atsuhiko Yamanaka 40 | * @author Michael Laudati 41 | */ 42 | class RequestSubsystem extends Request { 43 | 44 | /** Constant name for subsystem request. */ 45 | static final String SUBSYSTEM_REQUEST = "subsystem"; 46 | 47 | /** Subsystem to request to start over session. */ 48 | private String _subsystem = null; 49 | 50 | 51 | /** 52 | * Sets the subsystem to request. 53 | * 54 | * @param subsystem 55 | */ 56 | void setSubsystem(String subsystem) { 57 | _subsystem = subsystem; 58 | } 59 | 60 | @Override 61 | void request(Session session, Channel channel) throws Exception { 62 | super.request(session, channel); 63 | 64 | // byte SSH_MSG_CHANNEL_REQUEST(98) 65 | // uint32 recipient channel 66 | // string request type // "subsystem" 67 | // boolean want reply // 1 68 | // string subsystem // subsystem value to request 69 | Buffer buffer = new Buffer(150 + _subsystem.length()); 70 | Packet packet = new Packet(buffer); 71 | packet.reset(); 72 | buffer.putByte(SSH_MSG_CHANNEL_REQUEST); 73 | buffer.putInt(channel.getRecipient()); 74 | buffer.putString(SUBSYSTEM_REQUEST); 75 | buffer.putBoolean(waitForReply()); 76 | buffer.putString(_subsystem); 77 | write(packet); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestWindowChange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of Request for requesting a window change.

34 | * 35 | *

When the window (terminal) size changes on the client side, it may send a 36 | * message to the other side to inform it of the new dimensions.

37 | * 38 | *

A response SHOULD NOT be sent to this message.

39 | * 40 | *

RFC 4254 - The Secure Shell 41 | * (SSH) Connection Protocol

42 | * 43 | * @author Atsuhiko Yamanaka 44 | * @author Michael Laudati 45 | */ 46 | final class RequestWindowChange extends Request { 47 | 48 | /** Constant name for window change request. */ 49 | static final String WINDOW_CHANGE_REQUEST = "window-change"; 50 | 51 | /** Window width in columns. */ 52 | private int _widthColumns = 80; 53 | /** Window height in rows. */ 54 | private int _heightRows = 24; 55 | /** Window width in pixels. */ 56 | private int _widthPixels = 640; 57 | /** Window height in pixels. */ 58 | private int _heightPixels = 480; 59 | 60 | 61 | /** 62 | * Sets the window size to request. 63 | * 64 | * @param col 65 | * @param row 66 | * @param wp 67 | * @param hp 68 | */ 69 | void setSize(int col, int row, int wp, int hp) { 70 | _widthColumns = col; 71 | _heightRows = row; 72 | _widthPixels = wp; 73 | _heightPixels = hp; 74 | } 75 | 76 | /* 77 | * Sends request to set the window size. 78 | */ 79 | @Override 80 | void request(Session session, Channel channel) throws Exception { 81 | super.request(session, channel); 82 | 83 | //byte SSH_MSG_CHANNEL_REQUEST 84 | //uint32 recipient_channel 85 | //string "window-change" 86 | //boolean FALSE 87 | //uint32 terminal width, columns 88 | //uint32 terminal height, rows 89 | //uint32 terminal width, pixels 90 | //uint32 terminal height, pixels 91 | Buffer buffer = new Buffer(200); 92 | Packet packet = new Packet(buffer); 93 | packet.reset(); 94 | buffer.putByte(SSH_MSG_CHANNEL_REQUEST); 95 | buffer.putInt(channel.getRecipient()); 96 | buffer.putString(WINDOW_CHANGE_REQUEST); 97 | buffer.putBoolean(false); // Reply must always be false as per spec 98 | buffer.putInt(_widthColumns); 99 | buffer.putInt(_heightRows); 100 | buffer.putInt(_widthPixels); 101 | buffer.putInt(_heightPixels); 102 | write(packet); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/RequestX11.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

Implementation of Request to send a X11 request.

34 | * 35 | *

X11 forwarding may be requested for a session by sending a 36 | * SSH_MSG_CHANNEL_REQUEST message. It is recommended that the 37 | * 'x11 authentication cookie' that is sent be a fake, random cookie, and that 38 | * the cookie be checked and replaced by the real cookie when a connection 39 | * request is received.

40 | * 41 | *

X11 connection forwarding should stop when the session channel is closed. 42 | * However, already opened forwardings should not be automatically closed when 43 | * the session channel is closed.

44 | * 45 | *

RFC 4254 - The Secure Shell 46 | * (SSH) Connection Protocol

47 | * 48 | * @author Atsuhiko Yamanaka 49 | * @author Michael Laudati 50 | */ 51 | final class RequestX11 extends Request { 52 | 53 | /** Constant name for X11 forwarding request. */ 54 | static final String X11_REQUEST = "x11-req"; 55 | /** Constant name for X11 authentication protocol. */ 56 | static final String X11_MIT_MAGIC_COOKIE = "MIT-MAGIC-COOKIE-1"; 57 | 58 | @Override 59 | void request(Session session, Channel channel) throws Exception { 60 | super.request(session, channel); 61 | 62 | // byte SSH_MSG_CHANNEL_REQUEST(98) 63 | // uint32 recipient channel 64 | // string request type // "x11-req" 65 | // boolean want reply // 0 66 | // boolean single connection 67 | // string x11 authentication protocol // "MIT-MAGIC-COOKIE-1". 68 | // string x11 authentication cookie 69 | // uint32 x11 screen number 70 | Buffer buffer = new Buffer(1024); 71 | Packet packet = new Packet(buffer); 72 | packet.reset(); 73 | buffer.putByte(SSH_MSG_CHANNEL_REQUEST); 74 | buffer.putInt(channel.getRecipient()); 75 | buffer.putString(X11_REQUEST); 76 | buffer.putBoolean(waitForReply()); 77 | buffer.putByte((byte) 0); // TODO Allow value to be set (single connection) 78 | buffer.putString(X11_MIT_MAGIC_COOKIE); 79 | buffer.putString(ChannelX11.getFakedCookie(session)); 80 | buffer.putInt(0); 81 | write(packet); 82 | session._x11Forwarding = true; // Update session X11 forwarding is allowed 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/SftpProgressMonitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | * Interface for displaying the progress of an SFTP operation. 34 | * 35 | * @author Atsuhiko Yamanaka 36 | * @author Michael Laudati 37 | */ 38 | public interface SftpProgressMonitor { 39 | 40 | /** Constant to represent a SFTP PUT operation. */ 41 | int PUT = 0; 42 | /** Constant to represent a SFTP GET operation. */ 43 | int GET = 1; 44 | 45 | /** 46 | * Initializes the progress monitor with the specified SFTP operation type, 47 | * the source, destination and the maximum size of the progress. 48 | * 49 | * @param operation operation type (PUT or GET) 50 | * @param src 51 | * @param dest 52 | * @param max 53 | */ 54 | void init(int operation, String src, String dest, long max); 55 | 56 | /** 57 | * Updates the progress monitor to the specified count. 58 | * 59 | * @param count of bytes transferred 60 | * @return true if user/application has canceled the operation 61 | */ 62 | boolean count(long count); 63 | 64 | /** 65 | * Indicates the end of the progress operation being monitored. 66 | */ 67 | void end(); 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/UIKeyboardInteractive.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | * 34 | * @author Atsuhiko Yamanaka 35 | * @author Michael Laudati 36 | */ 37 | public interface UIKeyboardInteractive { 38 | 39 | /** 40 | * 41 | * @param destination 42 | * @param name 43 | * @param instruction 44 | * @param prompt 45 | * @param echo 46 | * @return 47 | */ 48 | String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/UserInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch; 31 | 32 | /** 33 | *

An interface defining an API for a user interface to retrieve user input 34 | * and display messages. Implementations should take care to provide the best 35 | * security practices when dealing with passwords and passphrases. Prompts 36 | * should mask any sensitive input data and ensure the values are stored safely. 37 | *

38 | * 39 | * @author Atsuhiko Yamanaka 40 | * @author Michael Laudati 41 | */ 42 | public interface UserInfo { 43 | 44 | /** 45 | *

Returns the passphrase provided by the user after a successful call to 46 | * {@link #promptPassphrase(java.lang.String)}. Implementations should 47 | * take care to mask the passphrase characters to prevent an eavesdropper 48 | * from viewing sensitive credentials. A {@code null} return value 49 | * indicates the user declined to enter a passphrase.

50 | * 51 | * @return passphrase entered by user or null if none provided 52 | */ 53 | String getPassphrase(); // TODO Consider returning byte[] 54 | 55 | /** 56 | *

Returns the password provided by the user after a successful call to 57 | * {@link #promptPassword(java.lang.String)}. Implementations should 58 | * take care to mask the password characters to prevent an eavesdropper 59 | * from viewing sensitive credentials. A {@code null} return value 60 | * indicates the user declined to enter a password.

61 | * 62 | * @return password entered by user or null if none provided 63 | */ 64 | String getPassword(); // TODO Consider returning byte[] 65 | 66 | /** 67 | *

Prompts the user with the specified {@code message} to enter a 68 | * password. The password should be stored in such a way that it can be 69 | * retrieved with {@link #getPassword()}. Implementations should honor 70 | * best security practices by masking the password input. The method should 71 | * return {@code true} if the user successfully provided a password; if the 72 | * user cancels/declines the password prompt, then {@code false} should be 73 | * returned.

74 | * 75 | * @param message to display when prompting user for password 76 | * @return {@code true} if user entered password, {@code false} if user 77 | * canceled/declined password prompt 78 | */ 79 | boolean promptPassword(String message); 80 | 81 | /** 82 | *

Prompts the user with the specified {@code message} to enter a 83 | * passphrase. The passphrase should be stored in such a way that it can be 84 | * retrieved with {@link #getPassphrase()}. Implementations should honor 85 | * best security practices by masking the passphrase input. The method should 86 | * return {@code true} if the user successfully provided a passphrase; if 87 | * the user cancels/declines the passphrase prompt, then {@code false} 88 | * should be returned.

89 | * 90 | * @param message to display when prompting user for passphrase 91 | * @return {@code true} if user entered passphrase, {@code false} if user 92 | * canceled/declined passphrase prompt 93 | */ 94 | boolean promptPassphrase(String message); 95 | 96 | /** 97 | *

Prompts the user with the specified {@code message} and allows for a 98 | * yes ({@code true}) or no ({@code false}) response.

99 | * 100 | * @param message to display 101 | * @return {@code true} if user selected yes 102 | */ 103 | boolean promptYesNo(String message); 104 | 105 | /** 106 | *

Displays the specified {@code message} to the user.

107 | * 108 | * @param message to display 109 | */ 110 | void showMessage(String message); 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/Algorithm.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | /** 32 | * Marker interface for all interfaces which define algorithms. 33 | * 34 | * @author Michael Laudati 35 | */ 36 | public interface Algorithm { } 37 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/AlgorithmFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | import org.vngx.jsch.Session; 32 | 33 | /** 34 | * Interface to define a factory which can generate instances of 35 | * {@code Algorithm} from a specified algorithm name. 36 | * 37 | * @author Michael Laudati 38 | * 39 | * @param type of algorithms created by factory 40 | */ 41 | public interface AlgorithmFactory { 42 | 43 | /** 44 | * Creates a new instance of the specified {@code algorithmName}. If the 45 | * algorithm is not supported, an exception must be thrown. The method 46 | * {@link AlgorithmFactory#isSupported(java.lang.String)} can be called to 47 | * check if a certain algorithm is supported without throwing an exception. 48 | * 49 | * @param algorithmName to create 50 | * @return algorithm instance 51 | * @throws UnsupportedAlgorithmException if algorithm is not supported 52 | */ 53 | T create(String algorithmName) throws UnsupportedAlgorithmException; 54 | 55 | /** 56 | * Creates a new instance of the specified {@code algorithmName} for the 57 | * specified {@code session}. Specifying the {@code Session} instance 58 | * allows for custom logic for creating {@code Algorithm} instances. If the 59 | * algorithm is not supported, an exception must be thrown. The method 60 | * {@link AlgorithmFactory#isSupported(java.lang.String)} can be called to 61 | * check if a certain algorithm is supported without throwing an exception. 62 | * 63 | * @param algorithmName to create 64 | * @param session to create cipher for 65 | * @return algorithm instance 66 | * @throws UnsupportedAlgorithmException if algorithm is not supported 67 | */ 68 | T create(String algorithmName, Session session) throws UnsupportedAlgorithmException; 69 | 70 | /** 71 | * Returns true if the specified algorithm name is supported. This method 72 | * should not throw any exceptions; rather {@code false} should be returned 73 | * if an algorithm is not supported. 74 | * 75 | * @param algorithmName to check if supported 76 | * @return true if algorithm is supported 77 | */ 78 | boolean isSupported(String algorithmName); 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/AlgorithmManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | import org.vngx.jsch.Session; 32 | import org.vngx.jsch.kex.DHGexSha1KexAlgorithm; 33 | import org.vngx.jsch.kex.DHGexSha256KexAlgorithm; 34 | import org.vngx.jsch.kex.DHGroup14KexAlgorithm; 35 | import org.vngx.jsch.kex.DHGroup1KexAlgorithm; 36 | import org.vngx.jsch.kex.DiffieHellmanImpl; 37 | 38 | /** 39 | * Algorithm manager provides a central location for creating {@code Algorithm} 40 | * instances using the manager's {@code AlgorithmFactory}. 41 | * 42 | * @author Michael Laudati 43 | */ 44 | public final class AlgorithmManager { 45 | 46 | /** Singleton instance of algorithm manager. */ 47 | private final static AlgorithmManager INSTANCE = new AlgorithmManager(); 48 | 49 | /** Factory used for creating algorithm instances. */ 50 | private AlgorithmFactory _algorithmFactory; 51 | 52 | 53 | /** 54 | * Private constructor to prevent direct instantiation of singleton. 55 | */ 56 | private AlgorithmManager() { } 57 | 58 | /** 59 | * Returns the singleton instance of {@code AlgorithmManager}. 60 | * 61 | * @return singleton instance 62 | */ 63 | public static AlgorithmManager getManager() { 64 | return INSTANCE; 65 | } 66 | 67 | @SuppressWarnings("unchecked") 68 | public T createAlgorithm(String algorithmName) throws UnsupportedAlgorithmException { 69 | return (T) getAlgorithmFactory().create(algorithmName); 70 | } 71 | 72 | @SuppressWarnings("unchecked") 73 | public T createAlgorithm(String algorithmName, Session session) throws UnsupportedAlgorithmException { 74 | return (T) getAlgorithmFactory().create(algorithmName, session); 75 | } 76 | 77 | /** 78 | * Sets the algorithm factory to use when creating algorithm instances. 79 | * 80 | * @param factory to create algorithm instances 81 | */ 82 | public void setAlgorithmFactory(AlgorithmFactory factory) { 83 | if( factory == null ) { 84 | throw new IllegalArgumentException("Algorithm factory cannot be null"); 85 | } 86 | _algorithmFactory = factory; 87 | } 88 | 89 | /** 90 | * Returns the {@code AlgorithmFactory} used by the manager to create 91 | * instances of {@code Algorithm}. If a factory has not been set, then a 92 | * default factory is initialized. 93 | * 94 | * @return factory for creating algorithms 95 | */ 96 | private AlgorithmFactory getAlgorithmFactory() { 97 | if( _algorithmFactory == null ) { 98 | // Lazy initialization of default algorithm factory 99 | _algorithmFactory = new DefaultAlgorithmFactory(Algorithm.class) { 100 | { 101 | setAlgorithmImpl(Compression.COMPRESSION_ZLIB, CompressionImpl.class); 102 | setAlgorithmImpl(Compression.COMPRESSION_ZLIB_OPENSSH, CompressionImpl.class); 103 | setAlgorithmImpl(Algorithms.DIFFIE_HELLMAN, DiffieHellmanImpl.class); 104 | setAlgorithmImpl(Algorithms.DIFFIE_HELLMAN_GROUP1_SHA1, DHGroup1KexAlgorithm.class); 105 | setAlgorithmImpl(Algorithms.DIFFIE_HELLMAN_GROUP14_SHA1, DHGroup14KexAlgorithm.class); 106 | setAlgorithmImpl(Algorithms.DIFFIE_HELLMAN_GROUP_EXCHANGE_SHA1, DHGexSha1KexAlgorithm.class); 107 | setAlgorithmImpl(Algorithms.DIFFIE_HELLMAN_GROUP_EXCHANGE_SHA256, DHGexSha256KexAlgorithm.class); 108 | setAlgorithmImpl(Algorithms.RANDOM, RandomImpl.class); 109 | setAlgorithmImpl(Algorithms.KEYPAIRGEN_DSA, KeyPairGenDSAImpl.class); 110 | setAlgorithmImpl(Algorithms.KEYPAIRGEN_RSA, KeyPairGenRSAImpl.class); 111 | setAlgorithmImpl(Algorithms.SIGNATURE_DSS, SignatureDSAImpl.class); 112 | setAlgorithmImpl(Algorithms.SIGNATURE_RSA, SignatureRSAImpl.class); 113 | } 114 | }; 115 | } 116 | return _algorithmFactory; 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/Compression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | /** 32 | *

Interface for defining an algorithm which supports compressing and 33 | * decompressing byte data for an SSH session.

34 | * 35 | *

If compression has been negotiated, the 'payload' field (and only it) will 36 | * be compressed using the negotiated algorithm. The 'packet_length' field and 37 | * 'mac' will be computed from the compressed payload. Encryption will be done 38 | * after compression.

39 | * 40 | *

Compression MAY be stateful, depending on the method. Compression MUST be 41 | * independent for each direction, and implementations MUST allow independent 42 | * choosing of the algorithm for each direction. In practice however, it is 43 | * RECOMMENDED that the compression method be the same in both directions.

44 | * 45 | *

The following compression methods are currently defined:

46 | *
 47 |  *		none     REQUIRED        no compression
 48 |  *		zlib     OPTIONAL        ZLIB (LZ77) compression
 49 |  * 
50 | * 51 | *

Note: Implementations may not be thread-safe and should 52 | * be externally synchronized.

53 | * 54 | *

Note: Instances should be created using the 55 | * {@code AlgorithmManager} factory.

56 | * 57 | *

RFC4253 - The 58 | * Secure Shell (SSH) Transport Layer Protocol: 6.2. Compression

59 | * 60 | * @see org.vngx.jsch.algorithm.AlgorithmManager 61 | * 62 | * @author Michael Laudati 63 | */ 64 | public interface Compression extends Algorithm { 65 | 66 | /** Algorithm name {@value} for using 'none' {@code Compression}. */ 67 | String COMPRESSION_NONE = "none"; 68 | /** Algorithm name {@value} for using 'zlib' {@code Compression.} */ 69 | String COMPRESSION_ZLIB = "zlib"; 70 | /** Algorithm name {@value} for using 'zlib@openssh.com' {@code Compression}. */ 71 | String COMPRESSION_ZLIB_OPENSSH = "zlib@openssh.com"; 72 | 73 | /** Constant for initializing decompression mode. */ 74 | int DECOMPRESS_MODE = 0; 75 | /** Constant for initializing compression mode. */ 76 | int COMPRESS_MODE = 1; 77 | 78 | /** 79 | * Initializes the compression stream with the specified mode and level. 80 | * 81 | * @param mode (compress or decompress) 82 | * @param level of compression 83 | */ 84 | void init(int mode, int level); 85 | 86 | /** 87 | * Compresses the specified buffer data from offset through length. 88 | * 89 | * @param buffer to compress 90 | * @param offset position in buffer 91 | * @param length of buffer to compress 92 | * @return compressed output length in bytes 93 | */ 94 | int compress(byte[] buffer, int offset, int length); 95 | 96 | /** 97 | * Decompresses the specified buffer data from offset through length. 98 | * 99 | * @param buffer to decompress 100 | * @param offset position in buffer 101 | * @param length in buffer (updated to hold decompressed length) 102 | * @return decompressed data 103 | */ 104 | byte[] uncompress(byte[] buffer, int offset, int[] length); 105 | 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/KeyPairGenDSA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | /** 32 | *

Interface for defining a key-pair generator using the Digital Signature 33 | * Standard (DSS).

34 | * 35 | *

36 | * Digital Signature Standard (DSS)

37 | * 38 | * @author Michael Laudati 39 | */ 40 | public interface KeyPairGenDSA extends Algorithm { 41 | 42 | /** 43 | * Initializes the key-pair generator and creates the public and private 44 | * keys along with the parameters p, q and g used to generate the keys. 45 | * 46 | * @param keySize to generate keys 47 | * @throws Exception if any errors occur 48 | */ 49 | void init(int keySize) throws Exception; 50 | 51 | /** 52 | * Returns the generated private key. 53 | * 54 | * @return private key 55 | */ 56 | byte[] getX(); 57 | 58 | /** 59 | * Returns the generated public key. 60 | * 61 | * @return public key 62 | */ 63 | byte[] getY(); 64 | 65 | /** 66 | * Returns the prime modulus 'p' used to generate keys. 67 | * 68 | * @return prime module 'p' 69 | */ 70 | byte[] getP(); 71 | 72 | /** 73 | * Returns the prime number 'q' used to generate keys. 74 | * 75 | * @return prime number 'q' 76 | */ 77 | byte[] getQ(); 78 | 79 | /** 80 | * Returns the number whose multiplicative order modulo p is q used for 81 | * generating keys. 82 | * 83 | * @return g value 84 | */ 85 | byte[] getG(); 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/KeyPairGenDSAImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | import java.security.KeyPair; 32 | import java.security.KeyPairGenerator; 33 | import java.security.SecureRandom; 34 | import java.security.interfaces.DSAKey; 35 | import java.security.interfaces.DSAParams; 36 | import java.security.interfaces.DSAPrivateKey; 37 | import java.security.interfaces.DSAPublicKey; 38 | 39 | /** 40 | * Implementation of KeyPairGenDSA for generating key pairs using 41 | * DSA encryption. 42 | * 43 | * @author Michael Laudati 44 | */ 45 | public final class KeyPairGenDSAImpl implements KeyPairGenDSA { 46 | 47 | /** Private key value. */ 48 | private byte[] _x; 49 | /** Public key value. */ 50 | private byte[] _y; 51 | /** Prime modulus 'p'. */ 52 | private byte[] _p; 53 | /** Prime number 'q'. */ 54 | private byte[] _q; 55 | /** Number whose multiplicative order modulo p is q. */ 56 | private byte[] _g; 57 | 58 | 59 | @Override 60 | public void init(int keySize) throws Exception { 61 | KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); 62 | keyGen.initialize(keySize, new SecureRandom()); 63 | KeyPair pair = keyGen.generateKeyPair(); 64 | _x = ((DSAPrivateKey) pair.getPrivate()).getX().toByteArray(); 65 | _y = ((DSAPublicKey) pair.getPublic()).getY().toByteArray(); 66 | 67 | DSAParams params = ((DSAKey) pair.getPrivate()).getParams(); 68 | _p = params.getP().toByteArray(); 69 | _q = params.getQ().toByteArray(); 70 | _g = params.getG().toByteArray(); 71 | } 72 | 73 | @Override 74 | public byte[] getX() { 75 | return _x; 76 | } 77 | 78 | @Override 79 | public byte[] getY() { 80 | return _y; 81 | } 82 | 83 | @Override 84 | public byte[] getP() { 85 | return _p; 86 | } 87 | 88 | @Override 89 | public byte[] getQ() { 90 | return _q; 91 | } 92 | 93 | @Override 94 | public byte[] getG() { 95 | return _g; 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/KeyPairGenRSA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | /** 32 | *

Interface for defining a key-pair generator using the RSA algorithm.

33 | * 34 | *

RSA Cryptography 35 | * Standard

36 | * 37 | * @author Michael Laudati 38 | */ 39 | public interface KeyPairGenRSA extends Algorithm { 40 | 41 | /** 42 | * Initializes the key-pair generator and creates the public and private 43 | * keys along with the parameters p, q, eq, ep, c, n, d and e used to 44 | * generate the keys. 45 | * 46 | * @param keySize to generate keys 47 | * @throws Exception if any errors occur 48 | */ 49 | void init(int keySize) throws Exception; 50 | 51 | /** 52 | * Returns the private key exponent 'd' used for key generation. 53 | * 54 | * @return private key exponent 'd' 55 | */ 56 | byte[] getD(); 57 | 58 | /** 59 | * Returns the public key exponent 'e' used for key generation. 60 | * 61 | * @return public key exponent 'e' 62 | */ 63 | byte[] getE(); 64 | 65 | /** 66 | * Returns the modulus 'n' calculated from (p * q) used for key generation. 67 | * 68 | * @return modulus 'n' 69 | */ 70 | byte[] getN(); 71 | 72 | /** 73 | * Returns the coefficient 'c' used for key generation. 74 | * 75 | * @return coefficient 'c' 76 | */ 77 | byte[] getC(); 78 | 79 | /** 80 | * Returns the exponent of 'p' used for key generation. 81 | * 82 | * @return exponent of 'p' 83 | */ 84 | byte[] getEP(); 85 | 86 | /** 87 | * Returns the exponent of 'q' used for key generation. 88 | * 89 | * @return exponent of 'q' 90 | */ 91 | byte[] getEQ(); 92 | 93 | /** 94 | * Returns the prime number 'p' used for key generation. 95 | * 96 | * @return prime number 'p' 97 | */ 98 | byte[] getP(); 99 | 100 | /** 101 | * Returns the prime number 'q' used for key generation. 102 | * 103 | * @return prime number 'q' 104 | */ 105 | byte[] getQ(); 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/KeyPairGenRSAImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | import java.security.KeyPair; 32 | import java.security.KeyPairGenerator; 33 | import java.security.SecureRandom; 34 | import java.security.interfaces.RSAPrivateCrtKey; 35 | import java.security.interfaces.RSAPublicKey; 36 | 37 | /** 38 | * Implementation of KeyPairGenRSA for generating key pairs using 39 | * RSA encryption. 40 | * 41 | * @author Michael Laudati 42 | */ 43 | public final class KeyPairGenRSAImpl implements KeyPairGenRSA { 44 | 45 | /** Private key value. */ 46 | private byte[] _d; 47 | /** Public key value. */ 48 | private byte[] _e; 49 | /** Modulus 'n' calculated from (p * q). */ 50 | private byte[] _n; 51 | /** Coefficient value. */ 52 | private byte[] _c; 53 | /** Exponent of p value. */ 54 | private byte[] _ep; 55 | /** Exponent of q value. */ 56 | private byte[] _eq; 57 | /** Prime number p. */ 58 | private byte[] _p; 59 | /** Prime number q. */ 60 | private byte[] _q; 61 | 62 | 63 | @Override 64 | public void init(int keySize) throws Exception { 65 | KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); 66 | keyGen.initialize(keySize, new SecureRandom()); 67 | KeyPair pair = keyGen.generateKeyPair(); 68 | RSAPublicKey pubKey = (RSAPublicKey) pair.getPublic(); 69 | RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) pair.getPrivate(); 70 | 71 | _d = prvKey.getPrivateExponent().toByteArray(); 72 | _e = pubKey.getPublicExponent().toByteArray(); 73 | _n = prvKey.getModulus().toByteArray(); 74 | _c = prvKey.getCrtCoefficient().toByteArray(); 75 | _ep = prvKey.getPrimeExponentP().toByteArray(); 76 | _eq = prvKey.getPrimeExponentQ().toByteArray(); 77 | _p = prvKey.getPrimeP().toByteArray(); 78 | _q = prvKey.getPrimeQ().toByteArray(); 79 | } 80 | 81 | @Override 82 | public byte[] getD() { 83 | return _d; 84 | } 85 | 86 | @Override 87 | public byte[] getE() { 88 | return _e; 89 | } 90 | 91 | @Override 92 | public byte[] getN() { 93 | return _n; 94 | } 95 | 96 | @Override 97 | public byte[] getC() { 98 | return _c; 99 | } 100 | 101 | @Override 102 | public byte[] getEP() { 103 | return _ep; 104 | } 105 | 106 | @Override 107 | public byte[] getEQ() { 108 | return _eq; 109 | } 110 | 111 | @Override 112 | public byte[] getP() { 113 | return _p; 114 | } 115 | 116 | @Override 117 | public byte[] getQ() { 118 | return _q; 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/Random.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | /** 32 | *

Interface to define an algorithm which generates pseudo-random data used 33 | * in cryptographic processes.

34 | * 35 | *

The SSH protocol binds each session key to the session by including random 36 | * session specific data in the hash used to produce session keys. Special care 37 | * should be taken to ensure that all of the random numbers are of good quality. 38 | * If the random data here (e.g., Diffie-Hellman (DH) parameters) are pseudo- 39 | * random, then the pseudo-random number generator should be cryptographically 40 | * secure (i.e., its next output not easily guessed even when knowing all 41 | * previous outputs) and, furthermore, proper entropy needs to be added to the 42 | * pseudo-random number generator. [RFC4086] offers suggestions for sources of 43 | random numbers and entropy. Implementers should note the importance of 44 | * entropy and the well-meant, anecdotal warning about the difficulty in 45 | * properly implementing pseudo-random number generating functions.

46 | * 47 | *

The amount of entropy available to a given client or server may sometimes 48 | * be less than what is required. In this case, one must either resort to 49 | * pseudo-random number generation regardless of insufficient entropy or refuse 50 | * to run the protocol. The latter is preferable.

51 | * 52 | *
RFC 4251 - 53 | * The Secure Shell (SSH) Protocol Architecture: Pseudo-Random Number 54 | * Generation 55 | *
RFC 4086 - Randomness 56 | * Requirements for Security 57 | * 58 | *

Note: Implementations may not be thread-safe and should 59 | * be externally synchronized.

60 | * 61 | *

Note: Instances should be created using the 62 | * {@code AlgorithmManager} factory.

63 | * 64 | * @see org.vngx.jsch.algorithm.AlgorithmManager 65 | * 66 | * @author Michael Laudati 67 | */ 68 | public interface Random extends Algorithm { 69 | 70 | /** 71 | * Fills the specified array from the offset through length with randomly 72 | * generated data. 73 | * 74 | * @param buffer array to fill with random bytes 75 | * @param offset position in destination 76 | * @param length to fill 77 | */ 78 | void fill(byte[] buffer, int offset, int length); 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/RandomImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | import java.security.NoSuchAlgorithmException; 32 | import java.security.SecureRandom; 33 | 34 | /** 35 | * Implementation of {@code Random} which wraps a {@code SecureRandom} instance 36 | * to create cryptographically strong random numbers. 37 | * 38 | * @see org.vngx.jsch.Random 39 | * @see java.security.SecureRandom 40 | * 41 | * @author Michael Laudati 42 | */ 43 | public final class RandomImpl implements Random { 44 | 45 | /** Cryptographically strong RNG to create random data. */ 46 | private final SecureRandom _secureRandom; 47 | /** Temporary buffer to fill with random data to copy into specified buffer. */ 48 | private final byte[] _tmp = new byte[256]; 49 | 50 | 51 | /** 52 | * Creates a new instance of {@code RandomImpl}. 53 | */ 54 | public RandomImpl() { 55 | _secureRandom = new SecureRandom(); 56 | } 57 | 58 | /** 59 | * Creates a new instance of {@code RandomImpl} which uses the specified JCE 60 | * algorithm for creating random numbers. 61 | * 62 | * @param algorithm to use 63 | * @throws NoSuchAlgorithmException 64 | */ 65 | public RandomImpl(String algorithm) throws NoSuchAlgorithmException { 66 | _secureRandom = SecureRandom.getInstance(algorithm); 67 | } 68 | 69 | /** 70 | * Fills the specified array with random data from the specified offset 71 | * through length using the wrapped RNG instance. 72 | * 73 | * @param buffer array to fill with random data 74 | * @param offset position 75 | * @param length 76 | */ 77 | @Override 78 | public void fill(byte[] buffer, int offset, int length) { 79 | if( length <= _tmp.length ) { 80 | _secureRandom.nextBytes(_tmp); 81 | System.arraycopy(_tmp, 0, buffer, offset, length); 82 | } else { 83 | byte[] temp = new byte[length]; 84 | _secureRandom.nextBytes(temp); 85 | System.arraycopy(temp, 0, buffer, offset, length); 86 | } 87 | 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/SignatureDSA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | /** 32 | * Interface to define a DSA signature. 33 | * 34 | * @author Michael Laudati 35 | */ 36 | public interface SignatureDSA extends Algorithm { 37 | 38 | void setPubKey(byte[] y, byte[] p, byte[] q, byte[] g) throws Exception; 39 | 40 | void setPrvKey(byte[] x, byte[] p, byte[] q, byte[] g) throws Exception; 41 | 42 | void update(byte[] data) throws Exception; 43 | 44 | boolean verify(byte[] signature) throws Exception; 45 | 46 | byte[] sign() throws Exception; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/SignatureRSA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | /** 32 | * Interface to define a RSA signature. 33 | * 34 | * @author Michael Laudati 35 | */ 36 | public interface SignatureRSA extends Algorithm { 37 | 38 | void setPubKey(byte[] e, byte[] n) throws Exception; 39 | 40 | void setPrvKey(byte[] d, byte[] n) throws Exception; 41 | 42 | void update(byte[] data) throws Exception; 43 | 44 | boolean verify(byte[] signature) throws Exception; 45 | 46 | byte[] sign() throws Exception; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/SignatureRSAImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | import java.math.BigInteger; 32 | import java.security.KeyFactory; 33 | import java.security.NoSuchAlgorithmException; 34 | import java.security.PrivateKey; 35 | import java.security.PublicKey; 36 | import java.security.Signature; 37 | import java.security.spec.RSAPrivateKeySpec; 38 | import java.security.spec.RSAPublicKeySpec; 39 | import org.vngx.jsch.Buffer; 40 | 41 | /** 42 | * Implementation of SignatureRSA. 43 | * 44 | * @author Michael Laudati 45 | */ 46 | public final class SignatureRSAImpl implements SignatureRSA { 47 | 48 | /** Java JCE signature instance. */ 49 | private final Signature _signature; 50 | /** Java JCE key factory. */ 51 | private final KeyFactory _keyFactory; 52 | 53 | 54 | public SignatureRSAImpl() throws NoSuchAlgorithmException { 55 | _signature = Signature.getInstance("SHA1withRSA"); 56 | _keyFactory = KeyFactory.getInstance("RSA"); 57 | } 58 | 59 | @Override 60 | public void setPubKey(byte[] e, byte[] n) throws Exception { 61 | RSAPublicKeySpec rsaPubKeySpec = new RSAPublicKeySpec(new BigInteger(n), new BigInteger(e)); 62 | PublicKey pubKey = _keyFactory.generatePublic(rsaPubKeySpec); 63 | _signature.initVerify(pubKey); 64 | } 65 | 66 | @Override 67 | public void setPrvKey(byte[] d, byte[] n) throws Exception { 68 | RSAPrivateKeySpec rsaPrivKeySpec = new RSAPrivateKeySpec(new BigInteger(n), new BigInteger(d)); 69 | PrivateKey prvKey = _keyFactory.generatePrivate(rsaPrivKeySpec); 70 | _signature.initSign(prvKey); 71 | } 72 | 73 | @Override 74 | public byte[] sign() throws Exception { 75 | return _signature.sign(); 76 | } 77 | 78 | @Override 79 | public void update(byte[] data) throws Exception { 80 | _signature.update(data); 81 | } 82 | 83 | @Override 84 | public boolean verify(byte[] signature) throws Exception { 85 | if( (signature[0] | signature[1] | signature[2]) == 0 ) { 86 | Buffer sigBuffer = new Buffer(signature); 87 | sigBuffer.getString(); // Skip first string 88 | signature = sigBuffer.getString(); // second is signature 89 | } 90 | return _signature.verify(signature); 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/algorithm/UnsupportedAlgorithmException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.algorithm; 30 | 31 | import org.vngx.jsch.exception.JSchException; 32 | 33 | /** 34 | * An implementation of {@code JSchException} to use when an {@code Algorithm} 35 | * implementation is not supported. 36 | * 37 | * @see org.vngx.jsch.algorithm.AlgorithmFactory 38 | * @see org.vngx.jsch.algorithm.AlgorithmManager 39 | * @see org.vngx.jsch.exception.JSchException 40 | * 41 | * @author Michael Laudati 42 | */ 43 | public class UnsupportedAlgorithmException extends JSchException { 44 | 45 | /** 46 | * Creates a new instance of {@code UnsupportedAlgorithmException} without 47 | * detail message. 48 | */ 49 | public UnsupportedAlgorithmException() { } 50 | 51 | /** 52 | * Creates a new instance of {@code UnsupportedAlgorithmException} with the 53 | * specified detail message. 54 | * 55 | * @param msg the detail message 56 | */ 57 | public UnsupportedAlgorithmException(String msg) { 58 | super(msg); 59 | } 60 | 61 | /** 62 | * Creates a new instance of {@code UnsupportedAlgorithmException} with the 63 | * specified detail message and cause. 64 | * 65 | * @param msg the detail message 66 | * @param cause of exception 67 | */ 68 | public UnsupportedAlgorithmException(String msg, Throwable cause) { 69 | super(msg, cause); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/cipher/CipherException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.cipher; 30 | 31 | import org.vngx.jsch.constants.TransportLayerProtocol; 32 | import org.vngx.jsch.exception.JSchException; 33 | 34 | /** 35 | * Implementation of {@code JSchException} for errors relating to the usage of 36 | * {@code Cipher} instances. 37 | * 38 | * @author Michael Laudati 39 | */ 40 | public class CipherException extends JSchException { 41 | 42 | /** 43 | * Creates a new instance of {@code CipherException} without detail message. 44 | */ 45 | public CipherException() { 46 | super(TransportLayerProtocol.SSH_DISCONNECT_PROTOCOL_ERROR); 47 | } 48 | 49 | /** 50 | * Creates a new instance of {@code CipherException} with the specified 51 | * detail message. 52 | * 53 | * @param msg detail 54 | */ 55 | public CipherException(String msg) { 56 | super(msg, TransportLayerProtocol.SSH_DISCONNECT_PROTOCOL_ERROR); 57 | } 58 | 59 | /** 60 | * Creates a new instance of {@code CipherException} with the specified 61 | * detail message and cause. 62 | * 63 | * @param msg detail 64 | * @param cause 65 | */ 66 | public CipherException(String msg, Throwable cause) { 67 | super(msg, cause, TransportLayerProtocol.SSH_DISCONNECT_PROTOCOL_ERROR); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/cipher/CipherNone.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.cipher; 30 | 31 | /** 32 | *

Empty implementation of {@code Cipher} to be used when no cipher is 33 | * required. This should *ONLY* be used for debugging purposes... the RFC spec 34 | * for SSH strongly suggests that the client must notify the user whenever 35 | * CIPHER NONE is being used as data is being sent unencrypted.

36 | * 37 | *

The "none" algorithm specifies that no encryption is to be done. Note that 38 | * this method provides no confidentiality protection and it is NOT RECOMMENDED. 39 | * Some functionality (e.g., password authentication) may be disabled for 40 | * security reasons if this cipher is chosen.

41 | * 42 | *

RFC 4253 - The 43 | * Secure Shell (SSH) Transport Layer Protocol: Encryption

44 | * 45 | * @see org.vngx.jsch.cipher.Cipher 46 | * 47 | * @author Michael Laudati 48 | */ 49 | public final class CipherNone implements Cipher { 50 | 51 | /** Constant IV size for empty cipher. */ 52 | private static final int IV_SIZE = 8; 53 | /** Constant block size for empty cipher. */ 54 | private static final int BLOCK_SIZE = 16; 55 | 56 | 57 | @Override 58 | public int getIVSize() { 59 | return IV_SIZE; 60 | } 61 | 62 | @Override 63 | public int getBlockSize() { 64 | return BLOCK_SIZE; 65 | } 66 | 67 | @Override 68 | public boolean isCBC() { 69 | return false; 70 | } 71 | 72 | @Override 73 | public void init(int mode, byte[] key, byte[] iv) { 74 | // Do nothing 75 | } 76 | 77 | @Override 78 | public void update(byte[] source, int srcOffset, int length, byte[] dest, int destOffset) { 79 | // Do nothing 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/config/BooleanPropertyValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.config; 30 | 31 | /** 32 | * Implementation of {@code PropertyValidator} which validates a property value 33 | * by checking if it's a {@code boolean}. 34 | * 35 | * @author Michael Laudati 36 | */ 37 | public class BooleanPropertyValidator extends PropertyValidator { 38 | 39 | /** Boolean property validator with default value of true. */ 40 | public final static BooleanPropertyValidator DEFAULT_TRUE_VALIDATOR = new BooleanPropertyValidator(true); 41 | /** Boolean property validator with default value of false. */ 42 | public final static BooleanPropertyValidator DEFAULT_FALSE_VALIDATOR = new BooleanPropertyValidator(false); 43 | 44 | 45 | /** 46 | * Creates a new instance of {@code BooleanPropertyValidator} with the 47 | * default boolean value. 48 | * 49 | * @param defaultValue of boolean property 50 | */ 51 | protected BooleanPropertyValidator(boolean defaultValue) { 52 | super(String.valueOf(defaultValue)); 53 | } 54 | 55 | /** 56 | * Returns {@code true} if the specified property {@code value} can be 57 | * parsed by {@link Boolean#valueOf(java.lang.String). 58 | * 59 | * @param value to validate 60 | * @return {@code true} if property value is a boolean 61 | */ 62 | @Override 63 | protected boolean isPropertyValid(String value) { 64 | try { 65 | Boolean.valueOf(value); 66 | return true; 67 | } catch(Exception e) { 68 | return false; 69 | } 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/config/InvalidPropertyException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.config; 30 | 31 | /** 32 | * An exception which can be thrown when an invalid property value has been 33 | * found. 34 | * 35 | * @author Michael Laudati 36 | */ 37 | public class InvalidPropertyException extends IllegalArgumentException { 38 | 39 | /** Property name which has invalid value. */ 40 | protected final String _propertyName; 41 | /** Invalid property value. */ 42 | protected final String _propertyValue; 43 | 44 | 45 | /** 46 | * Creates a new empty instance of {@code InvalidPropertyException}. 47 | */ 48 | public InvalidPropertyException() { 49 | this(null, null, null, null); 50 | } 51 | 52 | /** 53 | * Creates a new instance of {@code InvalidPropertyException} with the 54 | * specified property name and property value. 55 | * 56 | * @param propName which was invalid 57 | * @param propValue which was invalid 58 | */ 59 | public InvalidPropertyException(String propName, String propValue) { 60 | this(propName, propValue, null, null); 61 | } 62 | 63 | /** 64 | * Creates a new instance of {@code InvalidPropertyException} with the 65 | * specified property name and property value and error message. 66 | * 67 | * @param propName which was invalid 68 | * @param propValue which was invalid 69 | * @param message about error 70 | */ 71 | public InvalidPropertyException(String propName, String propValue, String message) { 72 | this(propName, propValue, message, null); 73 | } 74 | 75 | /** 76 | * Creates a new instance of {@code InvalidPropertyException} with the 77 | * specified property name and property value and error message and cause. 78 | * 79 | * @param propName which was invalid 80 | * @param propValue which was invalid 81 | * @param message about error 82 | * @param cause of error 83 | */ 84 | public InvalidPropertyException(String propName, String propValue, String message, Throwable cause) { 85 | super(generateErrorMessage(message, propName, propValue), cause); 86 | _propertyName = propName; 87 | _propertyValue = propValue; 88 | } 89 | 90 | /** 91 | * Returns the name of the invalid property. 92 | * 93 | * @return name of the invalid property 94 | */ 95 | public String getPropertyName() { 96 | return _propertyName; 97 | } 98 | 99 | /** 100 | * Returns the value of the invalid property. 101 | * 102 | * @return value of invalid property 103 | */ 104 | public String getPropertyValue() { 105 | return _propertyValue; 106 | } 107 | 108 | /** 109 | * Generates the message to pass to constructor. 110 | * 111 | * @param message sent by callee to constructor 112 | * @param propName property name 113 | * @param propValue property value 114 | * @return error message to pass to super constuctor 115 | */ 116 | protected static String generateErrorMessage(String message, String propName, String propValue) { 117 | StringBuilder buffer = new StringBuilder(); 118 | if( message != null ) { 119 | buffer.append(message).append(": "); 120 | } 121 | buffer.append("Invalid property '").append(propName); 122 | buffer.append("' with value: ").append(propValue); 123 | return buffer.toString(); 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/config/PropertyValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.config; 30 | 31 | /** 32 | * Simple property value validator which provides two methods; one to check if a 33 | * given property value is valid, and another to return a default value for a 34 | * property. Subclasses can override the {@link #isPropertyValid(java.lang.String)} 35 | * method to provide custom validation logic. 36 | * 37 | * @author Michael Laudati 38 | */ 39 | public class PropertyValidator { 40 | 41 | /** Default property value. */ 42 | protected final String _defaultValue; 43 | 44 | 45 | /** 46 | * Creates a new instance of {@code DefaultPropertyValidator} with 47 | * the specified default value. 48 | * 49 | * @param defaultValue of property 50 | */ 51 | public PropertyValidator(final String defaultValue) { 52 | _defaultValue = defaultValue; 53 | } 54 | 55 | /** 56 | * Returns true if the specified {@code property} value is valid as defined 57 | * by the implementation. By default, the method returns true if the value 58 | * is not null. 59 | * 60 | * @param property value to validate 61 | * @return true if property is not null 62 | */ 63 | protected boolean isPropertyValid(String property) { 64 | return property != null; 65 | } 66 | 67 | /** 68 | * Returns the default value for this property validator. 69 | * 70 | * @return default value 71 | */ 72 | protected String getDefaultValue() { 73 | return _defaultValue; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/config/SessionConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.config; 30 | 31 | import java.util.List; 32 | import java.util.Map; 33 | import org.vngx.jsch.Util; 34 | import org.vngx.jsch.cipher.CipherManager; 35 | 36 | /** 37 | *

{@code SessionConfig} allows the user to specify configuration properties 38 | * for an instance of {@code Session}. Different sessions running in the same 39 | * JVM can each have their own independent configurations by creating different 40 | * instances of {@code SessionConfig}.

41 | * 42 | *

The user only needs to set properties they wish to override which already 43 | * exist in the global parent configuration singleton instance 44 | * {@code JSchConfig}. Any properties which are not overridden in this 45 | * instance will be retrieved from the default parent global configuration.

46 | * 47 | * @see org.vngx.jsch.config.JSchConfig 48 | * 49 | * @author Michael Laudati 50 | */ 51 | public class SessionConfig extends JSchConfig { 52 | 53 | /** 54 | * Creates a new instance of {@code SessionConfig} which uses the global 55 | * {@code JSchConfig} singleton instance as the parent. 56 | */ 57 | public SessionConfig() { 58 | super(null); 59 | } 60 | 61 | /** 62 | * Creates a new instance of {@code SessionConfig} which uses the specified 63 | * parent configuration for retrieving properties not defined in this 64 | * configuration instance. 65 | * 66 | * @param parentConfig 67 | */ 68 | public SessionConfig(SessionConfig parentConfig) { 69 | super(parentConfig); 70 | } 71 | 72 | /** 73 | * Creates a new instance of {@code SessionConfig} with the specified 74 | * properties to load. 75 | * 76 | * @param properties to pre-load 77 | * @throws InvalidPropertyException if any invalid properties are included 78 | */ 79 | public SessionConfig(Map properties) { 80 | super(null); 81 | if( properties != null ) { 82 | for( Map.Entry entry : properties.entrySet() ) { 83 | setProperty(entry.getKey(), entry.getValue()); 84 | } 85 | } 86 | } 87 | 88 | /** 89 | * Returns the checked list of client to server ciphers. Each cipher in the 90 | * list is checked to verify it's available; any unavailable ciphers are 91 | * removed from the list. 92 | * 93 | * @return comma delimited, checked list of ciphers for client to server in 94 | * order by preference 95 | */ 96 | public String getCiphersC2S() { 97 | List ciphers = CipherManager.getManager().supportedCiphers(getList(KEX_CIPHER_C2S)); 98 | if( ciphers.isEmpty() ) { 99 | throw new IllegalStateException("No supported client-to-server ciphers: " + getString(KEX_CIPHER_C2S)); 100 | } 101 | return Util.join(ciphers, ","); 102 | } 103 | 104 | /** 105 | * Returns the checked list of server to client ciphers. Each cipher in the 106 | * list is checked to verify it's available; any unavailable ciphers are 107 | * removed from the list. 108 | * 109 | * @return comma delimited, checked list of ciphers for server to client in 110 | * order by preference 111 | */ 112 | public String getCiphersS2C() { 113 | List ciphers = CipherManager.getManager().supportedCiphers(getList(KEX_CIPHER_S2C)); 114 | if( ciphers.isEmpty() ) { 115 | throw new IllegalStateException("No supported server-to-client ciphers: " + getString(KEX_CIPHER_S2C)); 116 | } 117 | return Util.join(ciphers, ","); 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/config/StringSetPropertyValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.config; 30 | 31 | import java.util.Arrays; 32 | import java.util.HashSet; 33 | import java.util.Set; 34 | 35 | /** 36 | * Implementation of {@code PropertyValidator} which validates by checking if a 37 | * string property is contained in the set of allowed values passed to the 38 | * constructor. 39 | * 40 | * @author Michael Laudati 41 | */ 42 | public class StringSetPropertyValidator extends PropertyValidator { 43 | 44 | /** Set of valid strings to check against for valid properties. */ 45 | protected final Set _validSet = new HashSet(); 46 | 47 | 48 | /** 49 | * Creates a new instance of {@code StringSetPropertyValidator} which only 50 | * returns a property as valid if it matches a String contained in the 51 | * specified {@code validSet} and sets the default value as the first value 52 | * returned by the iterator of the valid set. 53 | * 54 | * @param validSet 55 | */ 56 | public StringSetPropertyValidator(Set validSet) { 57 | this(validSet.iterator().next(), validSet); 58 | } 59 | 60 | /** 61 | * Creates a new instance of {@code StringSetPropertyValidator} which only 62 | * returns a property as valid if it matches a String contained in the 63 | * specified {@code validSet}. 64 | * 65 | * @param defaultValue 66 | * @param validSet 67 | */ 68 | public StringSetPropertyValidator(String defaultValue, Set validSet) { 69 | super(defaultValue); 70 | _validSet.addAll(validSet); 71 | } 72 | 73 | /** 74 | * Creates a new instance of {@code StringSetPropertyValidator} which only 75 | * returns a property as valid if it matches a String contained in the 76 | * specified {@code validSet}. 77 | * 78 | * @param defaultValue 79 | * @param validSet 80 | */ 81 | public StringSetPropertyValidator(String defaultValue, String... validSet) { 82 | super(defaultValue); 83 | _validSet.addAll(Arrays.asList(validSet)); 84 | } 85 | 86 | @Override 87 | protected boolean isPropertyValid(String property) { 88 | return property != null && _validSet.contains(property); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/constants/ConnectionProtocol.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.constants; 30 | 31 | /** 32 | *

SSH message code constants for the SSH Connection Protocol. The 33 | * Message Number is a byte value that describes the payload of a packet.

34 | * 35 | *

RFC 4251 - The 36 | * Secure Shell (SSH) Protocol Architecture: Message Numbers

37 | *

RFC 4250 - The Secure Shell 38 | * (SSH) Protocol Assigned Numbers

39 | * 40 | * @author Michael Laudati 41 | */ 42 | public interface ConnectionProtocol { 43 | 44 | /** SSH message code constant '{@value}' for global request. */ 45 | byte SSH_MSG_GLOBAL_REQUEST = 80; 46 | /** SSH message code constant '{@value}' for request success. */ 47 | byte SSH_MSG_REQUEST_SUCCESS = 81; 48 | /** SSH message code constant '{@value}' for request failure. */ 49 | byte SSH_MSG_REQUEST_FAILURE = 82; 50 | /** SSH message code constant '{@value}' to signal channel open. */ 51 | byte SSH_MSG_CHANNEL_OPEN = 90; 52 | /** SSH message code constant '{@value}' to signal channel open confirmation. */ 53 | byte SSH_MSG_CHANNEL_OPEN_CONFIRMATION = 91; 54 | /** SSH message code constant '{@value}' to signal channel open failure. */ 55 | byte SSH_MSG_CHANNEL_OPEN_FAILURE = 92; 56 | /** SSH message code constant '{@value}' to signal channel window adjust. */ 57 | byte SSH_MSG_CHANNEL_WINDOW_ADJUST = 93; 58 | /** SSH message code constant '{@value}' to signal channel data. */ 59 | byte SSH_MSG_CHANNEL_DATA = 94; 60 | /** SSH message code constant '{@value}' to signal channel extended data. */ 61 | byte SSH_MSG_CHANNEL_EXTENDED_DATA = 95; 62 | /** SSH message code constant '{@value}' to signal channel end of file. */ 63 | byte SSH_MSG_CHANNEL_EOF = 96; 64 | /** SSH message code constant '{@value}' to signal channel close. */ 65 | byte SSH_MSG_CHANNEL_CLOSE = 97; 66 | /** SSH message code constant '{@value}' to signal channel request. */ 67 | byte SSH_MSG_CHANNEL_REQUEST = 98; 68 | /** SSH message code constant '{@value}' to signal channel success. */ 69 | byte SSH_MSG_CHANNEL_SUCCESS = 99; 70 | /** SSH message code constant '{@value}' to signal channel failure. */ 71 | byte SSH_MSG_CHANNEL_FAILURE = 100; 72 | 73 | /* 74 | * 4.3. Channel Connection Failure Reason Codes and Descriptions 75 | * 76 | * The Channel Connection Failure 'reason code' is a uint32 value. The 77 | * associated Channel Connection Failure 'description' text is a human- 78 | * readable message that describes the channel connection failure reason. 79 | * This is described in [SSH-CONNECT]. 80 | * 81 | * 4.3.1. Conventions 82 | * 83 | * Protocol packets containing the SSH_MSG_CHANNEL_OPEN_FAILURE message MUST 84 | * have Channel Connection Failure 'reason code' values in the range of 85 | * 0x00000001 to 0xFFFFFFFF. 86 | */ 87 | /** 88 | * SSH channel connection failure reason code constant for open 89 | * administratively prohibited code. 90 | */ 91 | int SSH_OPEN_ADMINISTRATIVELY_PROHIBITED = 1; 92 | /** 93 | * SSH channel connection failure reason code constant for open connection 94 | * failed code. 95 | */ 96 | int SSH_OPEN_CONNECT_FAILED = 2; 97 | /** 98 | * SSH channel connection failure reason code constant for open unknown 99 | * channel type code. 100 | */ 101 | int SSH_OPEN_UNKNOWN_CHANNEL_TYPE = 3; 102 | /** 103 | * SSH channel connection failure reason code constant for open resource 104 | * shortage code. 105 | */ 106 | int SSH_OPEN_RESOURCE_SHORTAGE = 4; 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/constants/MessageConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.constants; 30 | 31 | /** 32 | * Constants for user messages. 33 | * 34 | * @author Michael Laudati 35 | */ 36 | public interface MessageConstants { 37 | 38 | /** 39 | * Message prompt displayed to user if the host key has changed compared 40 | * to what's stored in locally known hosts. 41 | * 42 | * Args: 43 | * 1) Host key algorithm 44 | * 2) Host key fingerprint 45 | * 3) Known hosts file 46 | */ 47 | String INVALID_SERVER_HOST = 48 | "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!\n" + 49 | "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\n" + 50 | "Someone could be eavesdropping on you right now (man-in-the-middle attack)!\n" + 51 | "It is also possible that the %1$s host key has just been changed.\n" + 52 | "The fingerprint for the %1$s key sent by the remote host is\n" + "%2$s.\n" + 53 | "Please contact your system administrator.\n" + 54 | "Add correct host key in %3$s to get rid of this message."; 55 | 56 | /** Prompt user if they want to replace old key with new key. */ 57 | String PROMPT_REPLACE_KEY = INVALID_SERVER_HOST + 58 | "\nDo you want to delete the old key and insert the new key?"; 59 | 60 | /** 61 | * Message prompt displayed to user if an unknown host key is sent by 62 | * server and StrictHostKeyChecking is set to 'ask'. 63 | * 64 | * Args: 65 | * 1) Host 66 | * 2) Host key algorithm 67 | * 3) Host key fingerprint 68 | */ 69 | String PROMPT_UNKNOWN_KEY = 70 | "The authenticity of host '%1$s' can't be established.\n" + 71 | "%2$s key fingerprint is %3$s.\n" + 72 | "Are you sure you want to continue connecting?"; 73 | 74 | /** 75 | * Message prompt displayed to ask user for password for a given host. 76 | * 77 | * Args: 78 | * 1) Host:port 79 | */ 80 | String PROMPT_PASSWORD = "Password for %1$s"; 81 | 82 | /** 83 | * Message prompt displayed to ask user for passphrase for a given key. 84 | * 85 | * Args: 86 | * 1) Public key name 87 | */ 88 | String PROMPT_PASSPHRASE = "Passphrase for %1$s"; 89 | 90 | /** Message prompt indicating password must be changed. */ 91 | String PASSWORD_MUST_CHANGE = "Password must be changed."; 92 | 93 | /** 94 | * Message prompt to ask user if they approve creating a known hosts 95 | * repository file. 96 | * 97 | * Args: 98 | * 1) known hosts file name 99 | */ 100 | String PROMPT_CREATE_KNOWN_HOSTS = 101 | "%1$s does not exist.\n" + 102 | "Are you sure you want to create it?"; 103 | 104 | /** 105 | * Message prompt to ask user if they approve creating known hosts 106 | * directory. 107 | * 108 | * Args: 109 | * 1) Directory file name 110 | */ 111 | String PROMPT_CREATE_HOSTS_DIR = 112 | "The parent directory %1$s does not exist.\n" + 113 | "Are you sure you want to create it?"; 114 | 115 | String MSG_KNOWN_HOSTS_NOT_CREATED = 116 | "%1$s has not been created."; 117 | 118 | String MSG_KNOWN_HOSTS_CREATED = 119 | "%1$s has been succesfully created.\n" 120 | + "Please check its access permission."; 121 | 122 | } 123 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/constants/SSHConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.constants; 30 | 31 | /** 32 | *

General constants for the SSH library.

33 | * 34 | * @author Michael Laudati 35 | */ 36 | public interface SSHConstants { 37 | 38 | /** 39 | *

Default port for SSH connections over TCP/IP.

40 | * 41 | *

When used over TCP/IP, the server normally listens for connections on 42 | * port 22. This port number has been registered with the IANA, and has 43 | * been officially assigned for SSH.

44 | * 45 | *

RFC 4253 - 46 | * The Secure Shell (SSH) Transport Layer Protocol: Use over TCP/IP

47 | */ 48 | int DEFAULT_SSH_PORT = 22; 49 | /** Constant for localhost address "127.0.0.1". */ 50 | String LOCALHOST = "127.0.0.1"; 51 | /** Default path where known host keys are stored locally. */ 52 | String KNOWN_HOSTS = "known_hosts"; 53 | 54 | /** 55 | *

Constant for standard version SSH 2.0 used during client/server 56 | * version exchange.

57 | * 58 | *

RFC 4253 - 59 | * The Secure Shell (SSH) Transport Layer Protocol: Protocol Version 60 | * Exchange

61 | */ 62 | String SSH_VERSION_2_0 = "SSH-2.0"; 63 | /** 64 | *

Constant for standard version SSH 2.0 with backwards compatability 65 | * with older 1.x SSH versions used during client/server version 66 | * exchange.

67 | * 68 | *

Server implementations MAY support a configurable compatibility flag 69 | * that enables compatibility with old versions. When this flag is on, the 70 | * server SHOULD identify its 'protoversion' as "1.99". Clients using 71 | * protocol 2.0 MUST be able to identify this as identical to "2.0".

72 | * 73 | *

RFC 4253 - 74 | * The Secure Shell (SSH) Transport Layer Protocol: Compatibility With Old 75 | * SSH Versions

76 | */ 77 | String SSH_VERSION_1_99 = "SSH-1.99"; 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/constants/UserAuthProtocol.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.constants; 30 | 31 | /** 32 | *

SSH message code constants for the SSH user authentication protocol. The 33 | * Message Number is a byte value that describes the payload of a packet.

34 | * 35 | *

RFC 4251 - The 36 | * Secure Shell (SSH) Protocol Architecture: Message Numbers

37 | *

RFC 4250 - The Secure Shell 38 | * (SSH) Protocol Assigned Numbers

39 | * 40 | * @author Michael Laudati 41 | */ 42 | public interface UserAuthProtocol { 43 | 44 | /** SSH message code constant '{@value}' for user auth request. */ 45 | byte SSH_MSG_USERAUTH_REQUEST = 50; 46 | /** SSH message code constant '{@value}' for user auth failure. */ 47 | byte SSH_MSG_USERAUTH_FAILURE = 51; 48 | /** SSH message code constant '{@value}' for user auth success. */ 49 | byte SSH_MSG_USERAUTH_SUCCESS = 52; 50 | /** SSH message code constant '{@value}' for user auth banner. */ 51 | byte SSH_MSG_USERAUTH_BANNER = 53; 52 | /** SSH message code constant '{@value}' for user auth info request. */ 53 | byte SSH_MSG_USERAUTH_INFO_REQUEST = 60; 54 | /** SSH message code constant '{@value}' for user auth response. */ 55 | byte SSH_MSG_USERAUTH_INFO_RESPONSE = 61; 56 | /** SSH message code constant '{@value}' to request a password change. */ 57 | byte SSH_MSG_USERAUTH_PASSWD_CHANGEREQ = 60; 58 | /** SSH message code constant '{@value}' for user auth public key OK. */ 59 | byte SSH_MSG_USERAUTH_PK_OK = 60; 60 | /** SSH message code constant '{@value}' for user auth GSS API response. */ 61 | byte SSH_MSG_USERAUTH_GSSAPI_RESPONSE = 60; 62 | /** SSH message code constant '{@value}' for user auth GSS API token. */ 63 | byte SSH_MSG_USERAUTH_GSSAPI_TOKEN = 61; 64 | /** SSH message code constant '{@value}' for user auth GSS API exchange complete. */ 65 | byte SSH_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE = 63; 66 | /** SSH message code constant '{@value}' for user auth GSS API error. */ 67 | byte SSH_MSG_USERAUTH_GSSAPI_ERROR = 64; 68 | /** SSH message code constant '{@value}' for user auth GSS API error token. */ 69 | byte SSH_MSG_USERAUTH_GSSAPI_ERRTOK = 65; 70 | /** SSH message code constant '{@value}' for user auth GSS API mic?. */ 71 | byte SSH_MSG_USERAUTH_GSSAPI_MIC = 66; 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/exception/JSchException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.exception; 30 | 31 | import org.vngx.jsch.constants.TransportLayerProtocol; 32 | 33 | /** 34 | *

General exception class for vngx-jsch library which maintains a reason 35 | * code sent to the server with the SSH_MSG_DISCONNET packet specifying why the 36 | * client is disconnecting from the server. The standard disconnect reason 37 | * codes are specified in RFC 4253.

38 | * 39 | *

RFC 4253 - The 40 | * Secure Shell (SSH) Transport Layer Protocol: Disconnection Message

41 | * 42 | * @author Michael Laudati 43 | */ 44 | public class JSchException extends Exception { 45 | 46 | /** 47 | *

Disconnect reason code sent to server for this error.

48 | * 49 | * @see org.vngx.jsch.constants.TransportLayerProtocol 50 | */ 51 | protected final int _disconnectReason; 52 | 53 | 54 | /** 55 | * Creates a new instance of JSchException. 56 | */ 57 | public JSchException() { 58 | this(TransportLayerProtocol.SSH_DISCONNECT_BY_APPLICATION); 59 | } 60 | 61 | /** 62 | * Creates a new instance of JSchException with the specified 63 | * disconnect reason code. 64 | * 65 | * @param disconnectReason code sent to server 66 | */ 67 | public JSchException(int disconnectReason) { 68 | _disconnectReason = disconnectReason; 69 | } 70 | 71 | /** 72 | * Creates a new instance of JSchException with the specified 73 | * message. 74 | * 75 | * @param message 76 | */ 77 | public JSchException(String message) { 78 | this(message, TransportLayerProtocol.SSH_DISCONNECT_BY_APPLICATION); 79 | } 80 | 81 | /** 82 | * Creates a new instance of JSchException with the specified 83 | * message and disconnect reason code. 84 | * 85 | * @param message 86 | * @param disconnectReason code sent to server 87 | */ 88 | public JSchException(String message, int disconnectReason) { 89 | super(message); 90 | _disconnectReason = disconnectReason; 91 | } 92 | 93 | /** 94 | * Creates a new instance of JSchException with the specified 95 | * message and cause. 96 | * 97 | * @param message 98 | * @param cause 99 | */ 100 | public JSchException(String message, Throwable cause) { 101 | this(message, cause, TransportLayerProtocol.SSH_DISCONNECT_BY_APPLICATION); 102 | } 103 | 104 | /** 105 | * Creates a new instance of JSchException with the specified 106 | * message and cause. 107 | * 108 | * @param message 109 | * @param cause 110 | * @param disconnectReason code sent to server 111 | */ 112 | public JSchException(String message, Throwable cause, int disconnectReason) { 113 | super(message, cause); 114 | _disconnectReason = disconnectReason; 115 | } 116 | 117 | /** 118 | * Returns the disconnect reason code sent to the server for this error. 119 | * 120 | * @return disconnect reason code 121 | */ 122 | public int getDisconnectReason() { 123 | return _disconnectReason; 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/exception/SftpException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.exception; 31 | 32 | /** 33 | * An implementation of Exception for errors which occur during 34 | * an SFTP session. 35 | * 36 | * @author Atsuhiko Yamanaka 37 | * @author Michael Laudati 38 | */ 39 | public class SftpException extends Exception { 40 | 41 | /** ID of specific error which occurred. */ 42 | protected final int _id; 43 | 44 | 45 | /** 46 | * Creates a new instance of SftpException for the specified 47 | * error ID and message. 48 | * 49 | * @param id 50 | * @param message 51 | */ 52 | public SftpException(int id, String message) { 53 | super(message); 54 | _id = id; 55 | } 56 | 57 | /** 58 | * Creates a new instance of SftpException for the specified 59 | * error ID, message and cause. 60 | * 61 | * @param id 62 | * @param message 63 | * @param cause 64 | */ 65 | public SftpException(int id, String message, Throwable cause) { 66 | super(message, cause); 67 | _id = id; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return _id + ": " + super.toString(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/hash/Hash.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.hash; 30 | 31 | import org.vngx.jsch.algorithm.Algorithm; 32 | 33 | /** 34 | *

Interface to define a cryptographic hash algorithm. A cryptographic hash 35 | * function is a deterministic procedure that takes an arbitrary block of data 36 | * and returns a fixed-size bit string, the (cryptographic) hash value, such 37 | * that an accidental or intentional change to the data will change the hash 38 | * value. The data to be encoded is often called the "message", and the hash 39 | * value is sometimes called the message digest or simply digest.

40 | * 41 | *

Note: Implementations may not be thread-safe and should 42 | * be externally synchronized.

43 | * 44 | *

Note: Instances should be created using the 45 | * {@code HashManager} factory.

46 | * 47 | * @see org.vngx.jsch.hash.HashManager 48 | * 49 | * @author Michael Laudati 50 | */ 51 | public interface Hash extends Algorithm { 52 | 53 | /** Algorithm name {@value} for SHA-1 {@code Hash} algorithm. */ 54 | String HASH_SHA1 = "sha-1"; 55 | /** Algorithm name {@value} for MD5 {@code Hash} algorithm. */ 56 | String HASH_MD5 = "md5"; 57 | /** Algorithm name {@value} for SHA-256 {@code Hash} algorithm. */ 58 | String HASH_SHA256 = "sha-256"; 59 | 60 | /** 61 | * Returns the block size for the hash function. 62 | * 63 | * @return block size of message digest 64 | */ 65 | int getBlockSize(); 66 | 67 | /** 68 | * Updates the hash with the specified data. 69 | * 70 | * @param buffer 71 | * @param offset 72 | * @param length 73 | */ 74 | void update(byte[] buffer, int offset, int length); 75 | 76 | /** 77 | * Generates and returns the message digest. 78 | * 79 | * @return message digest 80 | */ 81 | byte[] digest(); 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/hash/HashImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.hash; 30 | 31 | import java.security.MessageDigest; 32 | import java.security.NoSuchAlgorithmException; 33 | import java.security.NoSuchProviderException; 34 | import org.vngx.jsch.config.JSchConfig; 35 | 36 | /** 37 | * Implementation of {@code Hash} providing a wrapper for Java's built in 38 | * message digest algorithms. The security provider for creating instances is 39 | * set with the {@code JSchConfig} property defined as 40 | * {@link org.vngx.jsch.config.JSchConfig#DEFAULT_SECURITY_PROVIDER}; by 41 | * default the default security provider will be used. If another security 42 | * provider has been registered, then the security provider name in the 43 | * configuration will be used when creating instances. 44 | * 45 | * @see java.security.MessageDigest 46 | * @see org.vngx.jsch.hash.Hash 47 | * @see org.vngx.jsch.config.JSchConfig 48 | * @see org.vngx.jsch.config.JSchConfig#DEFAULT_SECURITY_PROVIDER 49 | * 50 | * @author Michael Laudati 51 | */ 52 | public class HashImpl implements Hash { 53 | 54 | /** Message digest provided through Java for hashing. */ 55 | private final MessageDigest _md; 56 | /** Block size of message digest. */ 57 | private final int _blockSize; 58 | 59 | /** 60 | * Creates a new instance of {@code HashImpl}. 61 | * 62 | * @param messageDigest algorithm name 63 | * @param blockSize of hash 64 | * @throws NoSuchAlgorithmException 65 | * @throws NoSuchProviderException 66 | */ 67 | public HashImpl(String messageDigest, int blockSize) throws NoSuchAlgorithmException, NoSuchProviderException { 68 | String provider = JSchConfig.getConfig().getString(JSchConfig.DEFAULT_SECURITY_PROVIDER); 69 | _md = provider.length()==0 ? MessageDigest.getInstance(messageDigest) : 70 | MessageDigest.getInstance(messageDigest, provider); 71 | _blockSize = blockSize; 72 | } 73 | 74 | @Override 75 | public int getBlockSize() { 76 | return _blockSize; 77 | } 78 | 79 | @Override 80 | public void update(byte[] buffer, int offset, int length) { 81 | _md.update(buffer, offset, length); 82 | } 83 | 84 | @Override 85 | public byte[] digest() { 86 | return _md.digest(); 87 | } 88 | 89 | /** 90 | * Implementation of {@code HashImpl} using Java's MD5 message digest. 91 | * 92 | * @author Michael Laudati 93 | */ 94 | public static class MD5 extends HashImpl { 95 | /** 96 | * Creates a new instance of {@code MD5}. 97 | * 98 | * @throws NoSuchAlgorithmException 99 | * @throws NoSuchProviderException 100 | */ 101 | public MD5() throws NoSuchAlgorithmException, NoSuchProviderException { 102 | super("MD5", 16); 103 | } 104 | } 105 | 106 | /** 107 | * Implementation of {@code HashImpl} using Java's SHA1 message digest. 108 | * 109 | * @author Michael Laudati 110 | */ 111 | public static class SHA1 extends HashImpl { 112 | /** 113 | * Creates a new instance of {@code SHA1}. 114 | * 115 | * @throws NoSuchAlgorithmException 116 | * @throws NoSuchProviderException 117 | */ 118 | public SHA1() throws NoSuchAlgorithmException, NoSuchProviderException { 119 | super("SHA-1", 20); 120 | } 121 | } 122 | 123 | /** 124 | * Implementation of {@code HashImpl} using Java's SHA-256 message digest. 125 | * 126 | * @author Michael Laudati 127 | */ 128 | public static class SHA256 extends HashImpl { 129 | /** 130 | * Creates a new instance of {@code SHA256}. 131 | * 132 | * @throws NoSuchAlgorithmException 133 | * @throws NoSuchProviderException 134 | */ 135 | public SHA256() throws NoSuchAlgorithmException, NoSuchProviderException { 136 | super("SHA-256", 32); 137 | } 138 | } 139 | 140 | } 141 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/hash/MACException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.hash; 30 | 31 | import org.vngx.jsch.constants.TransportLayerProtocol; 32 | import org.vngx.jsch.exception.JSchException; 33 | 34 | /** 35 | * Implementation of {@code JSchException} for errors relating to the usage of 36 | * {@code MAC} instances. 37 | * 38 | * @author Michael Laudati 39 | */ 40 | public class MACException extends JSchException { 41 | 42 | /** 43 | * Creates a new instance of {@code MACException} without detail message. 44 | */ 45 | public MACException() { 46 | super(TransportLayerProtocol.SSH_DISCONNECT_MAC_ERROR); 47 | } 48 | 49 | /** 50 | * Creates a new instance of {@code MACException} with the specified detail 51 | * message. 52 | * 53 | * @param msg detail 54 | */ 55 | public MACException(String msg) { 56 | super(msg, TransportLayerProtocol.SSH_DISCONNECT_MAC_ERROR); 57 | } 58 | 59 | /** 60 | * Creates a new instance of {@code MACException} with the specified detail 61 | * message and cause. 62 | * 63 | * @param msg detail 64 | * @param cause 65 | */ 66 | public MACException(String msg, Throwable cause) { 67 | super(msg, cause, TransportLayerProtocol.SSH_DISCONNECT_MAC_ERROR); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/kex/DHGexSha256KexAlgorithm.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.kex; 30 | 31 | import org.vngx.jsch.hash.Hash; 32 | import org.vngx.jsch.exception.JSchException; 33 | import org.vngx.jsch.hash.HashManager; 34 | 35 | /** 36 | * Implementation of {@code KeyExchange} for 37 | * "diffie-hellman-group-exchange-sha256" key exchange for SSH. 38 | * 39 | * The "diffie-hellman-group-exchange-sha56" method specifies Diffie-Hellman 40 | * Group and Key Exchange with SHA-256 [FIPS-180-2] as HASH. 41 | * 42 | * The server keeps a list of safe primes and corresponding generators that it 43 | * can select from. A prime p is safe if p = 2q + 1 and q is prime. New primes 44 | * can be generated in the background. 45 | * 46 | * The generator g should be chosen such that the order of the generated 47 | * subgroup does not factor into small primes; that is, with p = 2q + 1, the 48 | * order has to be either q or p - 1. If the order is p - 1, then the exponents 49 | * generate all possible public values, evenly distributed throughout the range 50 | * of the modulus p, without cycling through a smaller subset. Such a generator 51 | * is called a "primitive root" (which is trivial to find when p is "safe"). 52 | * 53 | * The client requests a modulus from the server indicating the preferred size. 54 | * In the following description (C is the client, S is the server, the modulus 55 | * p is a large safe prime, and g is a generator for a subgroup of GF(p), min is 56 | * the minimal size of p in bits that is acceptable to the client, n is the size 57 | * of the modulus p in bits that the client would like to receive from the 58 | * server, max is the maximal size of p in bits that the client can accept, V_S 59 | * is S's version string, V_C is C's version string, K_S is S's public host key, 60 | * I_C is C's KEXINIT message, and I_S is S's KEXINIT message that has been 61 | * exchanged before this part begins): 62 | * 63 | * 1. C sends "min || n || max" to S, indicating the minimal acceptable 64 | * group size, the preferred size of the group, and the maximal group 65 | * size in bits the client will accept. 66 | * 2. S finds a group that best matches the client's request, and sends 67 | * "p || g" to C. 68 | * 3. C generates a random number x, where 1 < x < (p-1)/2. It 69 | * computes e = g^x mod p, and sends "e" to S. 70 | * 4. S generates a random number y, where 0 < y < (p-1)/2, and 71 | * computes f = g^y mod p. S receives "e". It computes K = e^y mod 72 | * p, H = hash(V_C || V_S || I_C || I_S || K_S || min || n || max || 73 | * p || g || e || f || K) (these elements are encoded according to 74 | * their types; see below), and signature s on H with its private host 75 | * key. S sends "K_S || f || s" to C. The signing operation may involve 76 | * a second hashing operation. 77 | * 5. C verifies that K_S really is the host key for S (e.g., using 78 | * certificates or a local database to obtain the public key). C is 79 | * also allowed to accept the key without verification; however, doing 80 | * so will render the protocol insecure against active attacks (but may 81 | * be desirable for practical reasons in the short term in many 82 | * environments). C then computes K = f^x mod p, H = hash(V_C || V_S 83 | * || I_C || I_S || K_S || min || n || max || p || g || e || f || K), 84 | * and verifies the signature s on H. 85 | * 86 | * @author Atsuhiko Yamanaka 87 | * @author Michael Laudati 88 | */ 89 | public final class DHGexSha256KexAlgorithm extends DHGexSha1KexAlgorithm { 90 | 91 | public DHGexSha256KexAlgorithm() throws JSchException { 92 | super(HashManager.getManager().createHash(Hash.HASH_SHA256)); 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/kex/DiffieHellmanImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.kex; 30 | 31 | import java.math.BigInteger; 32 | import java.security.KeyFactory; 33 | import java.security.KeyPair; 34 | import java.security.KeyPairGenerator; 35 | import java.security.NoSuchAlgorithmException; 36 | import java.security.NoSuchProviderException; 37 | import java.security.PublicKey; 38 | import javax.crypto.KeyAgreement; 39 | import javax.crypto.interfaces.DHPublicKey; 40 | import javax.crypto.spec.DHParameterSpec; 41 | import javax.crypto.spec.DHPublicKeySpec; 42 | import org.vngx.jsch.config.JSchConfig; 43 | 44 | /** 45 | * Implementation of {@code DiffieHellman} using the implementation provided 46 | * through JCE. 47 | * 48 | * @see org.vngx.jsch.DiffieHellman 49 | * 50 | * @author Michael Laudati 51 | */ 52 | public final class DiffieHellmanImpl implements DiffieHellman { 53 | 54 | /** JCE key pair generator. */ 55 | private final KeyPairGenerator _keyPairGenerator; 56 | /** JCE key agreement. */ 57 | private final KeyAgreement _keyAgreement; 58 | 59 | /** Prime number. */ 60 | private BigInteger _p; 61 | /** Generator value. */ 62 | private BigInteger _g; 63 | /** My public key. */ 64 | private BigInteger _e; 65 | /** Byte value of public key e. */ 66 | private byte[] _eArray; 67 | /** Public key. */ 68 | private BigInteger _f; 69 | /** Shared secret key. */ 70 | private BigInteger _K; 71 | /** Byte value of shared secret key. */ 72 | private byte[] _KArray; 73 | 74 | 75 | /** 76 | * Creates a new instance of {@code DiffieHellmanImpl}. 77 | * 78 | * @throws NoSuchAlgorithmException if DH algorithm cannot be found 79 | * @throws NoSuchProviderException if default provider cannot be found 80 | */ 81 | public DiffieHellmanImpl() throws NoSuchAlgorithmException, NoSuchProviderException { 82 | String provider = JSchConfig.getConfig().getString(JSchConfig.DEFAULT_SECURITY_PROVIDER); 83 | if( provider.length()==0 ) { 84 | _keyPairGenerator = KeyPairGenerator.getInstance("DH"); 85 | _keyAgreement = KeyAgreement.getInstance("DH"); 86 | } else { 87 | _keyPairGenerator = KeyPairGenerator.getInstance("DH", provider); 88 | _keyAgreement = KeyAgreement.getInstance("DH", provider); 89 | } 90 | } 91 | 92 | @Override 93 | public byte[] getE() throws Exception { 94 | if( _e == null ) { 95 | _keyPairGenerator.initialize(new DHParameterSpec(_p, _g)); 96 | KeyPair myKpair = _keyPairGenerator.generateKeyPair(); 97 | _keyAgreement.init(myKpair.getPrivate()); 98 | //byte[] myPubKeyEnc = myKpair.getPublic().getEncoded(); 99 | _e = ((DHPublicKey) (myKpair.getPublic())).getY(); 100 | _eArray = _e.toByteArray(); 101 | } 102 | return _eArray; 103 | } 104 | 105 | @Override 106 | public byte[] getK() throws Exception { 107 | if( _K == null ) { 108 | KeyFactory myKeyFac = KeyFactory.getInstance("DH"); 109 | PublicKey yourPubKey = myKeyFac.generatePublic(new DHPublicKeySpec(_f, _p, _g)); 110 | _keyAgreement.doPhase(yourPubKey, true); 111 | byte[] mySharedSecret = _keyAgreement.generateSecret(); 112 | _K = new BigInteger(mySharedSecret); 113 | //_KArray = _K.toByteArray(); // TODO Why assign twice? 114 | _KArray = mySharedSecret; 115 | } 116 | return _KArray; 117 | } 118 | 119 | @Override 120 | public void setP(byte[] p) { 121 | _p = new BigInteger(p); 122 | } 123 | 124 | @Override 125 | public void setG(byte[] g) { 126 | _g = new BigInteger(g); 127 | } 128 | 129 | @Override 130 | public void setF(byte[] f) { 131 | _f = new BigInteger(f); 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/kex/KexException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.kex; 30 | 31 | import org.vngx.jsch.constants.TransportLayerProtocol; 32 | import org.vngx.jsch.exception.JSchException; 33 | 34 | /** 35 | * Implementation of {@code JSchException} for key exchange exceptions. 36 | * 37 | * @see org.vngx.jsch.exception.JSchException 38 | * 39 | * @author Michael Laudati 40 | */ 41 | public class KexException extends JSchException { 42 | 43 | /** 44 | * Creates a new instance of {@code KexException} without detail message. 45 | */ 46 | public KexException() { 47 | super(TransportLayerProtocol.SSH_DISCONNECT_KEY_EXCHANGE_FAILED); 48 | } 49 | 50 | /** 51 | * Creates a new instance of {@code KexException} with the specified detail 52 | * message. 53 | * 54 | * @param msg the detail message. 55 | */ 56 | public KexException(String msg) { 57 | super(msg, TransportLayerProtocol.SSH_DISCONNECT_KEY_EXCHANGE_FAILED); 58 | } 59 | 60 | /** 61 | * Creates a new instance of {@code KexException} with the specified detail 62 | * message and cause. 63 | * 64 | * @param msg detail 65 | * @param cause of exception 66 | */ 67 | public KexException(String msg, Throwable cause) { 68 | super(msg, cause, TransportLayerProtocol.SSH_DISCONNECT_KEY_EXCHANGE_FAILED); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/proxy/Proxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.proxy; 31 | 32 | import org.vngx.jsch.util.SocketFactory; 33 | import java.io.InputStream; 34 | import java.io.OutputStream; 35 | import java.net.Socket; 36 | 37 | /** 38 | * Interface defining a proxy which acts as an intermediary for requests from 39 | * clients seeking resources from other servers. 40 | * 41 | * @author Atsuhiko Yamanaka 42 | * @author Michael Laudati 43 | */ 44 | public interface Proxy { 45 | 46 | /** 47 | * Connects the proxy using the specified socket factory to create sockets 48 | * to the specified host and port with the specified timeout. 49 | * 50 | * @param socketFactory 51 | * @param host 52 | * @param port 53 | * @param timeout 54 | * @throws Exception 55 | */ 56 | void connect(SocketFactory socketFactory, String host, int port, int timeout) throws Exception; 57 | 58 | /** 59 | * Returns the input stream from the connected socket. 60 | * 61 | * @return input stream 62 | */ 63 | InputStream getInputStream(); 64 | 65 | /** 66 | * Returns the output stream from the connected socket. 67 | * 68 | * @return output stream 69 | */ 70 | OutputStream getOutputStream(); 71 | 72 | /** 73 | * Returns the connected socket. 74 | * 75 | * @return connected socket 76 | */ 77 | Socket getSocket(); 78 | 79 | /** 80 | * Closes any open resources including the socket connection. 81 | */ 82 | void close(); 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/userauth/AuthCancelException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.userauth; 31 | 32 | import org.vngx.jsch.exception.JSchException; 33 | 34 | /** 35 | * Implementation of JSchException when user authentication is 36 | * canceled by the user. 37 | * 38 | * @author Atsuhiko Yamanaka 39 | * @author Michael Laudati 40 | */ 41 | public class AuthCancelException extends JSchException { 42 | 43 | /** 44 | * Creates a new instance of AuthCancelException. 45 | */ 46 | public AuthCancelException() { } 47 | 48 | /** 49 | * Creates a new instance of AuthCancelException with the 50 | * specified message. 51 | * 52 | * @param message 53 | */ 54 | public AuthCancelException(String message) { 55 | super(message); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/userauth/GSSContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.userauth; 31 | 32 | import org.vngx.jsch.exception.JSchException; 33 | 34 | /** 35 | * An interface for Generic Security Services (GSS API), an application 36 | * programming interface for programs to access security services 37 | * 38 | * @author Atsuhiko Yamanaka 39 | * @author Michael Laudati 40 | */ 41 | public interface GSSContext { 42 | 43 | /** 44 | * Creates the GSS context for the specified user and host. 45 | * 46 | * @param user 47 | * @param host 48 | * @throws JSchException 49 | */ 50 | void create(String user, String host) throws JSchException; 51 | 52 | /** 53 | * Returns true if the GSS context is established. 54 | * 55 | * @return true if context is established 56 | */ 57 | boolean isEstablished(); 58 | 59 | byte[] init(byte[] token, int offset, int length) throws JSchException; 60 | 61 | byte[] getMIC(byte[] message, int offset, int length); 62 | 63 | /** 64 | * Securely disposes of any data after completion. 65 | */ 66 | void dispose(); 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/userauth/Identity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.userauth; 31 | 32 | import org.vngx.jsch.exception.JSchException; 33 | 34 | /** 35 | * Interface for defining an SSH identity for performing user authentication and 36 | * agent forwarding.

37 | * 38 | * The goal of using Identity/Pubkey authentication is to remove the need for 39 | * static passwords. Instead of providing a password, which could be captured by 40 | * a keystroke logger or witnessed as you type it, you have a key pair on your 41 | * disk that you use to authenticate. Your account on the SSH server has a list 42 | * of Identities/Pubkeys that it trusts, and if you can prove you have the 43 | * public and private key then you are granted access without supplying a 44 | * password.

45 | * 46 | * Some of the nice features of this form of authentication are:

47 | *

    48 | *
  • No one can shoulder-surf your password and log in to your accounts; 49 | * they'd need both your Identity passphrase and the private key from 50 | * your machine.
  • 51 | *
  • The server administrator could disable password authentication 52 | * entirely, to prevent password guessing attacks.
  • 53 | *
  • You can use the ssh-agent and SSH agent forwarding to have your 54 | * authentication credentials 'follow' you.
  • 55 | *
  • You can place restrictions on Identities/Pubkeys, for example 56 | * forbidding port forwards, forcing predetermined commands, regardless 57 | * of what the user wanted to run, and more.
  • 58 | *
59 | * 60 | * @author Atsuhiko Yamanaka 61 | * @author Michael Laudati 62 | */ 63 | public interface Identity { 64 | 65 | /** 66 | * Sets the passphrase used to retrieve the public key. TODO ??? 67 | * 68 | * @param passphrase 69 | * @return 70 | * @throws JSchException 71 | */ 72 | boolean setPassphrase(byte[] passphrase) throws JSchException; 73 | 74 | /** 75 | * Returns the public key blob. 76 | * 77 | * @return pubic key blob 78 | */ 79 | byte[] getPublicKeyBlob(); 80 | 81 | /** 82 | * Returns a generated signature for the specified data using the key. 83 | * 84 | * @param data to sign 85 | * @return signature for data 86 | */ 87 | byte[] getSignature(byte[] data); 88 | 89 | /** 90 | * Decrypts the key blob and returns true if successfully decrypted. 91 | * 92 | * @return true if decrypted 93 | */ 94 | boolean decrypt(); 95 | 96 | /** 97 | * Returns the algorithm name being used for the public key encryption. 98 | * 99 | * @return algorithm name 100 | */ 101 | String getAlgorithmName(); 102 | 103 | /** 104 | * Returns the name of the identity. 105 | * 106 | * @return name of identity 107 | */ 108 | String getName(); 109 | 110 | /** 111 | * Returns true if the identity is encrypted. 112 | * 113 | * @return true if identity is encrypted 114 | */ 115 | boolean isEncrypted(); 116 | 117 | /** 118 | * Clears secure data currently loaded in memory to maintain the highest 119 | * security. 120 | */ 121 | void clear(); 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/userauth/PartialAuthException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.userauth; 31 | 32 | import org.vngx.jsch.exception.JSchException; 33 | import java.util.Arrays; 34 | import java.util.Collections; 35 | import java.util.LinkedHashSet; 36 | import java.util.Set; 37 | 38 | /** 39 | * Implementation of JSchException for partial authentication 40 | * exceptions. When a user authentication method receives a 41 | * SSH_MSG_USERAUTH_FAILURE response from the server, the server may pass back 42 | * a list of user authentication methods which can still proceed. This response 43 | * generates a JSchPartialAuthException which contains the parsed 44 | * set of user auth methods. 45 | * 46 | * @author Atsuhiko Yamanaka 47 | * @author Michael Laudati 48 | */ 49 | final class PartialAuthException extends JSchException { 50 | 51 | /** Set of user authentication methods which can proceed from server. */ 52 | private final Set _nameList; 53 | 54 | /** 55 | * Creates a new instance of JSchPartialAuthException with the 56 | * specified name list of user authentication methods which can proceed. 57 | * 58 | * @param nameList of auth methods from server 59 | */ 60 | PartialAuthException(String nameList) { 61 | if( nameList != null ) { 62 | _nameList = new LinkedHashSet(Arrays.asList(nameList.split(","))); 63 | } else { 64 | _nameList = Collections.emptySet(); 65 | } 66 | } 67 | 68 | /** 69 | * Set of server supported authentication methods which can proceed. 70 | * 71 | * @return set of server supported user auth methods 72 | */ 73 | Set getUserAuthMethods() { 74 | return _nameList; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/userauth/UserAuthNone.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.userauth; 31 | 32 | import org.vngx.jsch.Session; 33 | import org.vngx.jsch.exception.JSchException; 34 | 35 | /** 36 | * Implementation of UserAuth for "none" authentication. 37 | * 38 | * A client may request a list of authentication 'method name' values that may 39 | * continue by using the "none" authentication 'method name'. 40 | * 41 | * If no authentication is needed for the user, the server MUST return 42 | * SSH_MSG_USERAUTH_SUCCESS. Otherwise, the server MUST return 43 | * SSH_MSG_USERAUTH_FAILURE and MAY return with it a list of methods that may 44 | * continue in its 'authentications that can continue' value. 45 | * 46 | * This 'method name' MUST NOT be listed as supported by the server. 47 | * 48 | * @author Atsuhiko Yamanaka 49 | * @author Michael Laudati 50 | */ 51 | public final class UserAuthNone extends UserAuth { 52 | 53 | @Override 54 | protected boolean authUser(Session session, byte[] password) throws Exception { 55 | super.authUser(session, password); 56 | 57 | // send user name for "none" auth request 58 | // byte SSH_MSG_USERAUTH_REQUEST(50) 59 | // string user name 60 | // string service name ("ssh-connection") 61 | // string "none" 62 | _packet.reset(); 63 | _buffer.putByte(SSH_MSG_USERAUTH_REQUEST); 64 | _buffer.putString(session.getUserName()); 65 | _buffer.putString(SSH_CONNECTION); 66 | _buffer.putString(UserAuth.NONE); 67 | session.write(_packet); 68 | 69 | while( true ) { 70 | switch( session.read(_buffer).getCommand() & 0xff ) { 71 | case SSH_MSG_USERAUTH_SUCCESS: 72 | return true; // In case server allows 'none' user auth access! 73 | 74 | case SSH_MSG_USERAUTH_BANNER: 75 | userAuthBanner(); // Process banner message and continue 76 | break; 77 | 78 | case SSH_MSG_USERAUTH_FAILURE: 79 | userAuthFailure(); // Receive methods which can continue 80 | return false; 81 | 82 | default: 83 | throw new JSchException("Invalid UserAuth 'none' response: " + _buffer.getCommand()); 84 | } 85 | } 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/util/DataUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.util; 31 | 32 | 33 | /** 34 | * Static utility class providing some general data manipulation methods. 35 | * 36 | * @author Michael Laudati 37 | * @author Atsuhiko Yamanaka 38 | */ 39 | public final class DataUtil { 40 | 41 | /** Private constructor to prevent instantiation of static utility. */ 42 | private DataUtil() { } 43 | 44 | public static byte a2b(byte c) { 45 | if( '0' <= c && c <= '9' ) { 46 | return (byte) (c - '0'); 47 | } 48 | return (byte) (c - 'a' + 10); 49 | } 50 | 51 | public static byte b2a(byte c) { 52 | if( 0 <= c && c <= 9 ) { 53 | return (byte) (c + '0'); 54 | } 55 | return (byte) (c - 10 + 'A'); 56 | } 57 | 58 | public static byte[] readINTEGER(int[] index, byte[] plain) { 59 | index[0]++; 60 | int length = plain[index[0]++] & 0xff; 61 | if( (length & 0x80) != 0 ) { 62 | int foo = length & 0x7f; 63 | length = 0; 64 | while( foo-- > 0 ) { 65 | length = (length << 8) + (plain[index[0]++] & 0xff); 66 | } 67 | } 68 | byte[] sequence = new byte[length]; 69 | System.arraycopy(plain, index[0], sequence, 0, length); 70 | index[0] += length; 71 | return sequence; 72 | } 73 | 74 | public static int writeSEQUENCE(byte[] buf, int index, int len) { 75 | buf[index++] = 0x30; 76 | index = writeLength(buf, index, len); 77 | return index; 78 | } 79 | 80 | public static int writeINTEGER(byte[] buf, int index, byte[] data) { 81 | buf[index++] = 0x02; 82 | index = writeLength(buf, index, data.length); 83 | System.arraycopy(data, 0, buf, index, data.length); 84 | index += data.length; 85 | return index; 86 | } 87 | 88 | public static int countLength(int len) { 89 | int i = 1; 90 | if( len <= 0x7f ) { 91 | return i; 92 | } 93 | while( len > 0 ) { 94 | len >>>= 8; 95 | i++; 96 | } 97 | return i; 98 | } 99 | 100 | public static int writeLength(byte[] data, int index, int len) { 101 | int i = countLength(len) - 1; 102 | if( i == 0 ) { 103 | data[index++] = (byte) len; 104 | return index; 105 | } 106 | data[index++] = (byte) (0x80 | i); 107 | int j = index + i; 108 | while( i > 0 ) { 109 | data[index + i - 1] = (byte) (len & 0xff); 110 | len >>>= 8; 111 | i--; 112 | } 113 | return j; 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/util/HostKeyRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2002-2010 Atsuhiko Yamanaka, JCraft,Inc. All rights reserved. 3 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 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 notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * 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 org.vngx.jsch.util; 31 | 32 | import org.vngx.jsch.UserInfo; 33 | import java.util.List; 34 | 35 | /** 36 | * An interface for defining a repository which manages and validates host keys 37 | * from the local file system. 38 | * 39 | * @see org.vngx.jsch.util.HostKey 40 | * @see org.vngx.jsch.util.KnownHosts 41 | * 42 | * @author Atsuhiko Yamanaka 43 | * @author Michael Laudati 44 | */ 45 | public interface HostKeyRepository { 46 | 47 | /** 48 | * Enum constants for the available results which can be returned when 49 | * checking repository for host key. 50 | */ 51 | enum Check { 52 | /** Constant indicating host and key are valid (matched in repository). */ 53 | OK, 54 | /** Constant indicating host and key are not included in repository. */ 55 | NOT_INCLUDED, 56 | /** Constant indicating key for host has changed in repository. */ 57 | CHANGED; 58 | } 59 | 60 | /** 61 | * Checks if the specified host and public key is valid by checking against 62 | * the local repository of known hosts. 63 | * 64 | * @param host to check 65 | * @param key from host 66 | * @return validation result 67 | */ 68 | Check check(String host, byte[] key); 69 | 70 | /** 71 | * Adds the specified host key to the repository and uses the specified ui 72 | * for prompting user for input if necessary. 73 | * 74 | * @param hostkey to add 75 | * @param ui if user needs to be prompted 76 | */ 77 | void add(HostKey hostkey, UserInfo ui); 78 | 79 | /** 80 | * Removes the specified host from the repository. 81 | * 82 | * @param host 83 | * @param type 84 | */ 85 | void remove(String host, KeyType type); 86 | 87 | /** 88 | * Removes the specified host from the repository. 89 | * 90 | * @param host 91 | * @param type 92 | * @param key 93 | */ 94 | void remove(String host, KeyType type, byte[] key); 95 | 96 | /** 97 | * Returns a unique ID for the repository instance. Implementations can use 98 | * the known hosts file location from which the keys were loaded. 99 | * 100 | * @return repository ID 101 | */ 102 | String getKnownHostsRepositoryID(); 103 | 104 | /** 105 | * Returns the loaded host keys stored in the repository. 106 | * 107 | * @return loaded host keys 108 | */ 109 | List getHostKeys(); 110 | 111 | /** 112 | * Returns any loaded host keys which match the specified host and type. 113 | * 114 | * @param host 115 | * @param type 116 | * @return loaded host keys 117 | */ 118 | List getHostKeys(String host, KeyType type); 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/util/KeyType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.util; 30 | 31 | import org.vngx.jsch.Util; 32 | 33 | /** 34 | *

Enum constant to represent the different types of host keys. 35 | * Currently the only supported key types are: 36 | * 37 | *

    38 | *
  • ssh-dss - DSA
  • 39 | *
  • ssh-rsa - RSA
  • 40 | *
41 | * 42 | * @author Michael Laudati 43 | */ 44 | public enum KeyType { 45 | 46 | /** Constant for SSH-DSS host key type. */ 47 | SSH_DSS("DSA", "ssh-dss"), 48 | /** Constant for SSH-RSA host key type. */ 49 | SSH_RSA("RSA", "ssh-rsa"), 50 | /** Constant for unknown/unsupported host key type. */ 51 | UNKNOWN("Unknown", ""); 52 | 53 | /** Display name for type. */ 54 | public final String DISPLAY_NAME; 55 | /** SSH constant name for key type. */ 56 | public final String NAME; 57 | 58 | /** 59 | * Creates a new KeyType constant with the specified display 60 | * name and SSH constant name. 61 | * 62 | * @param displayName 63 | * @param sshValue 64 | */ 65 | KeyType(String displayName, String sshValue) { 66 | DISPLAY_NAME = displayName; 67 | NAME = sshValue; 68 | } 69 | 70 | /** 71 | * Returns the bytes for the SSH constant name for key type. 72 | * 73 | * @return bytes for key type name 74 | */ 75 | public byte[] getBytes() { 76 | return Util.str2byte(NAME); 77 | } 78 | 79 | /** 80 | * Returns true if the specified key name String matches this constant. 81 | * 82 | * @param name of key type 83 | * @return true if string matches key type 84 | */ 85 | public boolean equals(String name) { 86 | return NAME.equals(name); 87 | } 88 | 89 | /* Return the display name for type. */ 90 | @Override 91 | public String toString() { 92 | return NAME; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/org/vngx/jsch/util/Logger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Michael Laudati, N1 Concepts LLC. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * 3. The names of the authors may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 19 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL N1 20 | * CONCEPTS LLC OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | package org.vngx.jsch.util; 30 | 31 | import java.util.Arrays; 32 | 33 | /** 34 | *

Interface for defining a simple logger for the SSH library to reduce any 35 | * external dependencies which would be required for logging libraries such as 36 | * log4j, slf4j, et al. Implementations of Logger can be used to 37 | * wrap an external logging framework to allow for logging integration.

38 | * 39 | *

Two default implementations are provided in the interface: 40 | *

    41 | *
  • SIMPLE_LOGGER - Logs all output to System.err
  • 42 | *
  • NULL_LOGGER - Empty logger to ignore output
  • 43 | *
44 | *

45 | * 46 | *

The Logger instance is set by calling 47 | * {@link org.vngx.jsch.JSch#setLogger(org.vngx.jsch.util.Logger)}

48 | * 49 | * @see org.vngx.jsch.JSch 50 | * 51 | * @author Michael Laudati 52 | */ 53 | public interface Logger { 54 | 55 | /** Enum constants for logging levels. */ 56 | enum Level { 57 | /** Debug level for logging. */ 58 | DEBUG, 59 | /** Info level for logging. */ 60 | INFO, 61 | /** Warn level for logging. */ 62 | WARN, 63 | /** Error level for logging. */ 64 | ERROR, 65 | /** Fatal level for logging. */ 66 | FATAL 67 | } 68 | 69 | /** 70 | * Returns true if the specified logging Level is enabled. 71 | * 72 | * @param level to check 73 | * @return true if logging level is enabled 74 | */ 75 | boolean isEnabled(Level level); 76 | 77 | /** 78 | * Logs the specified message at the specified logging level. 79 | * 80 | * @param level to log 81 | * @param message to log 82 | */ 83 | void log(Level level, String message); 84 | 85 | /** 86 | * Logs the specified message and arguments at the specified level. 87 | * 88 | * @param level 89 | * @param message 90 | * @param args 91 | */ 92 | void log(Level level, String message, Object... args); 93 | 94 | /** 95 | * Logs the specified message and exception at the specified level. 96 | * 97 | * @param level 98 | * @param message 99 | * @param exception 100 | */ 101 | void log(Level level, String message, Throwable exception); 102 | 103 | /** 104 | * Simple implementation of Logger interface which logs all 105 | * output to the System.err stream. 106 | */ 107 | Logger SIMPLE_LOGGER = new Logger() { 108 | 109 | @Override public boolean isEnabled(Level level) { return true; } 110 | 111 | @Override public void log(Level level, String message) { System.err.println(message); } 112 | 113 | @Override public void log(Level level, String message, Object... args) { 114 | System.err.print(message); 115 | if( args != null ) { 116 | System.err.print(": "); 117 | System.err.println(Arrays.asList(args)); 118 | } 119 | } 120 | 121 | @Override public void log(Level level, String message, Throwable exception) { 122 | System.err.println(message+": "+exception); 123 | if( exception != null ) { 124 | exception.printStackTrace(System.err); 125 | } 126 | } 127 | }; 128 | 129 | /** 130 | * Null implementation of Logger which ignores all logging 131 | * output. 132 | */ 133 | Logger NULL_LOGGER = new Logger() { 134 | 135 | @Override public boolean isEnabled(Level level) { return false; } 136 | 137 | @Override public void log(Level level, String message) { } 138 | 139 | @Override public void log(Level level, String message, Object... args) { } 140 | 141 | @Override public void log(Level level, String message, Throwable exception) { } 142 | 143 | }; 144 | 145 | } 146 | --------------------------------------------------------------------------------