Package org.vngx.jsch.exception

Examples of org.vngx.jsch.exception.JSchException


  public static KeyPair genKeyPair(KeyType type, int keySize) throws JSchException {
    KeyPair keyPair;
    switch( type ) {
      case SSH_DSS: keyPair = new KeyPairDSA(); break;
      case SSH_RSA: keyPair = new KeyPairRSA(); break;
      default: throw new JSchException("Unsupported KeyType: "+type);
    }
    keyPair.generate(keySize)// Generate keys for the specified size
    return keyPair;        // and return key pair
  }
View Full Code Here


        }
        System.arraycopy(hn, 0, key, 0, key.length);
        return key;

      default// No support for other vendor types
        throw new JSchException("Unsupported vendor type: "+_vendor);
    }
  }
View Full Code Here

            type = KeyType.SSH_RSA;
          } else if( buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H' ) { // FSecure
            type = KeyType.UNKNOWN;
            vendor = VENDOR_FSECURE;
          } else {
            throw new JSchException("invalid privatekey: " + prvkey);
          }
          i += 3;
          continue;
        }
        if( buf[i] == 'C' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',' ) {
          i += 4;
          for( int ii = 0; ii < iv.length; ii++ ) {
            iv[ii] = (byte) (((DataUtil.a2b(buf[i++]) << 4) & 0xf0) + (DataUtil.a2b(buf[i++]) & 0xf));
          }
          continue;
        }
        if( buf[i] == 0x0d
            && i + 1 < buf.length && buf[i + 1] == 0x0a ) {
          i++;
          continue;
        }
        if( buf[i] == 0x0a && i + 1 < buf.length ) {
          if( buf[i + 1] == 0x0a ) {
            i += 2;
            break;
          }
          if( buf[i + 1] == 0x0d
              && i + 2 < buf.length && buf[i + 2] == 0x0a ) {
            i += 3;
            break;
          }
          boolean inheader = false;
          for( int j = i + 1; j < buf.length; j++ ) {
            if( buf[j] == 0x0a ) {
              break;
            }
            //if(buf[j]==0x0d) break;
            if( buf[j] == ':' ) {
              inheader = true;
              break;
            }
          }
          if( !inheader ) {
            i++;
            encrypted = false;    // no passphrase
            break;
          }
        }
        i++;
      }

      if( type == null ) {
        throw new JSchException("invalid privatekey: " + prvkey);
      }

      int start = i;
      while( i < len ) {
        if( buf[i] == 0x0a ) {
          boolean xd = (buf[i - 1] == 0x0d);
          System.arraycopy(buf, i + 1,
              buf,
              i - (xd ? 1 : 0),
              len - i - 1 - (xd ? 1 : 0));
          if( xd ) {
            len--;
          }
          len--;
          continue;
        }
        if( buf[i] == '-' ) {
          break;
        }
        i++;
      }
      data = Util.fromBase64(buf, start, i - start);

      if( data.length > 4 && // FSecure
          data[0] == (byte) 0x3f
          && data[1] == (byte) 0x6f
          && data[2] == (byte) 0xf9
          && data[3] == (byte) 0xeb ) {

        Buffer _buf = new Buffer(data);
        _buf.getInt()// 0x3f6ff9be
        _buf.getInt();
        @SuppressWarnings("unused")
        byte[] _type = _buf.getString();
        byte[] _cipher = _buf.getString();
        String cipher = Util.byte2str(_cipher);
        if( cipher.equals("3des-cbc") ) {
          _buf.getInt();
          byte[] foo = new byte[data.length - _buf.getOffSet()];
          _buf.getBytes(foo);
          data = foo;
          encrypted = true;
          throw new JSchException("unknown privatekey format: " + prvkey);
        } else if( cipher.equals("none") ) {
          _buf.getInt();
          _buf.getInt();
          encrypted = false;
          byte[] foo = new byte[data.length - _buf.getOffSet()];
          _buf.getBytes(foo);
          data = foo;
        }
      }

      if( pubkey != null ) {
        try {
          fc = new FileInputStream(pubkey).getChannel();
          len = (int) fc.size();
          fc.read(ByteBuffer.wrap(buf = new byte[len]));
          fc.close();

          if( buf.length > 4 && // FSecure's public key
              buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-' ) {

            boolean valid = true;
            i = 0;
            do {
              i++;
            } while( buf.length > i && buf[i] != 0x0a );
            if( buf.length <= i ) {
              valid = false;
            }

            while( valid ) {
              if( buf[i] == 0x0a ) {
                boolean inheader = false;
                for( int j = i + 1; j < buf.length; j++ ) {
                  if( buf[j] == 0x0a ) {
                    break;
                  }
                  if( buf[j] == ':' ) {
                    inheader = true;
                    break;
                  }
                }
                if( !inheader ) {
                  i++;
                  break;
                }
              }
              i++;
            }
            if( buf.length <= i ) {
              valid = false;
            }

            start = i;
            while( valid && i < len ) {
              if( buf[i] == 0x0a ) {
                System.arraycopy(buf, i + 1, buf, i, len - i - 1);
                len--;
                continue;
              }
              if( buf[i] == '-' ) {
                break;
              }
              i++;
            }
            if( valid ) {
              publickeyblob = Util.fromBase64(buf, start, i - start);
              if( type == KeyType.UNKNOWN ) {
                if( publickeyblob[8] == 'd' ) {
                  type = KeyType.SSH_DSS;
                } else if( publickeyblob[8] == 'r' ) {
                  type = KeyType.SSH_RSA;
                }
              }
            }
          } else {
            if( buf[0] == 's' && buf[1] == 's' && buf[2] == 'h' && buf[3] == '-' ) {
              i = 0;
              while( i < len ) {
                if( buf[i] == ' ' ) {
                  break;
                }
                i++;
              }
              i++;
              if( i < len ) {
                start = i;
                while( i < len ) {
                  if( buf[i] == ' ' ) {
                    break;
                  }
                  i++;
                }
                publickeyblob = Util.fromBase64(buf, start, i - start);
              }
            }
          }
        } catch(Exception ee) {
          // TODO Error handling???
        }
      }
    } catch(JSchException e) {
      throw e;
    } catch(Exception e) {
      throw new JSchException("Failed to load KeyPair: "+e, e);
    }

    KeyPair keyPair = null;
    switch( type ) {
      case SSH_DSS: keyPair = new KeyPairDSA(); break;
      case SSH_RSA: keyPair = new KeyPairRSA(); break;
      default: throw new JSchException("Unsupported key type: "+type);
    }
    keyPair._encrypted = encrypted;
    keyPair._publicKeyBlob = publickeyblob;
    keyPair._vendor = vendor;

    if( encrypted ) {
      keyPair._iv = iv;
      keyPair._data = data;
    } else if( !keyPair.parse(data) ) {
      throw new JSchException("Invalid private key: " + prvkey);
    }
    return keyPair;
  }
View Full Code Here

      _q = keypairgen.getQ();
      _ep = keypairgen.getEP();
      _eq = keypairgen.getEQ();
      _c = keypairgen.getC();
    } catch(Exception e) {
      throw new JSchException("Failed to generate KeyPairRSA: "+e, e);
    }
  }
View Full Code Here

      _prvKey = keypairgen.getX();
      _p = keypairgen.getP();
      _q = keypairgen.getQ();
      _g = keypairgen.getG();
    } catch(Exception e) {
      throw new JSchException("Failed to generate KeyPairDSA: "+e, e);
    }
  }
View Full Code Here

      sendRequests();
      new RequestShell().request(_session, this);
    } catch(JSchException e) {
      throw e;
    } catch(Exception e) {
      throw new JSchException("Failed to start ChannelShell", e);
    }

    if( _io.in != null ) {
      _thread = new Thread(this, "Shell for " + _session.getHost());
      _thread.setDaemon(_session.isDaemonThread());
View Full Code Here

        }
      }
    } catch(JSchException e) {
      throw e;
    } catch(Exception e) {
      throw new JSchException("Failed to load known hosts", e);
    } finally {
      if( hostsStream != null ) {
        try { hostsStream.close(); } catch(Exception e) { /* Ignore Error */ }
      }
    }
View Full Code Here

   * @param connectTimeout in milliseconds
   * @throws JSchException if any errors occur
   */
  public void connect(int connectTimeout) throws JSchException {
    if( isConnected() ) {
      throw new JSchException("Channel is already connected");
    } else if( !_session.isConnected() ) {
      throw new JSchException("Session is not connected");
    }
    _connectTimeout = connectTimeout;  // Set connection timeout
    try {
      Buffer buffer = new Buffer(100);
      Packet packet = new Packet(buffer);
      // send
      // byte   SSH_MSG_CHANNEL_OPEN(90)
      // string channel type         //
      // uint32 sender channel       // 0
      // uint32 initial window size  // 0x100000(65536)
      // uint32 maxmum packet size   // 0x4000(16384)
      packet.reset();
      buffer.putByte(SSH_MSG_CHANNEL_OPEN);
      buffer.putString(_type);
      buffer.putInt(_id);
      buffer.putInt(_localWindowSize);
      buffer.putInt(_localMaxPacketSize);
      _session.write(packet);

      int retry = 1000// Note: Max timeout is 50 seconds (1000 retries * 50ms sleep)
      long start = System.currentTimeMillis();
      while( _recipient == -1 && _session.isConnected() && retry-- > 0 ) {
        if( _connectTimeout > 0L && (System.currentTimeMillis() - start) > _connectTimeout ) {
          throw new JSchException("Failed to open channel: connection timeout after " + _connectTimeout + " ms");
        }
        try { Thread.sleep(50); } catch(Exception ee) { /* Ignore error. */ }
      }
      if( !_session.isConnected() ) {
        throw new JSchException("Failed to open channel: session is not connected");
      } else if( retry == 0 ) {
        throw new JSchException("Failed to open channel: no response");
      }

      /*
       * At the failure in opening the channel on the sshd,
       * 'SSH_MSG_CHANNEL_OPEN_FAILURE' will be sent from sshd and it will
       * be processed in Session#run().
       */
      if( isClosed() ) {
        throw new JSchException("Failed to open channel: "+_exitstatus);
      }
      _connected = true;
      start();
    } catch(JSchException je) {
      _connected = false;
      disconnect();
      throw je;
    } catch(Exception e) {
      _connected = false;
      disconnect();
      throw new JSchException("Failed to open channel "+getClass().getSimpleName()+": "+_exitstatus, e);
    }
  }
View Full Code Here

      _salt = Util.fromBase64(Util.str2byte(salt), 0, salt.length());
      _hashedHost = Util.fromBase64(Util.str2byte(hash), 0, hash.length());

      // If invalid salt/hash, then generate hash and salt for session
      if( _salt.length != 20 || _hashedHost.length != 20 ) {  // SHA-1 block size must be 20!
        throw new JSchException("Invalid format, salt/hashed host lengths are wrong size: "+_host);
      }
    } else {
      // Host is not yet hashed, so generate hash
      generateHash();
    }
View Full Code Here

        macsha1.update(hostBytes, 0, hostBytes.length);
        _hashedHost = new byte[macsha1.getBlockSize()];
        macsha1.doFinal(_hashedHost, 0);
      }
    } catch(Exception e) {
      throw new JSchException("Failed to create HashedHostKey: " + e, e);
    }

    // Update the host value to the proper hashed format
    StringBuilder buffer = new StringBuilder(2000);
    buffer.append(HASH_MAGIC).append(Util.byte2str(Util.toBase64(_salt, 0, _salt.length)));
View Full Code Here

TOP

Related Classes of org.vngx.jsch.exception.JSchException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.