Package org.vngx.jsch

Examples of org.vngx.jsch.Buffer


  public void sendKexInit() throws KexException {
    // Check if already in middle of a kex
    if( _inKeyExchange.getAndSet(true) ) {  // Flip state flag entering kex
      return// Return if already in process of kex
    }
    Buffer kexBuffer = new Buffer();      // Use a separate packet and buffer since
    Packet kexPacket = new Packet(kexBuffer)// kex may be invoked by user thread
    try {
      // Random instance for generating the kex cookie.  The 'cookie' MUST
      // be a random value generated by the sender. Its purpose is to make
      // it impossible for either side to fully determine the keys and the
      // session identifier.
      final Random random = AlgorithmManager.getManager().createAlgorithm(Algorithms.RANDOM, _session);

      // Construct the KEX INIT message packet
      // byte    SSH_MSG_KEXINIT(20)
      // byte[16]  cookie (random bytes)
      // string  kex_algorithms
      // string  server_host_key_algorithms
      // string  encryption_algorithms_client_to_server
      // string  encryption_algorithms_server_to_client
      // string  mac_algorithms_client_to_server
      // string  mac_algorithms_server_to_client
      // string  compression_algorithms_client_to_server
      // string  compression_algorithms_server_to_client
      // string  languages_client_to_server
      // string  languages_server_to_client
      // byte    boolean first_kex_packet_follows
      // uint32  0 (reserved for future extension)
      kexPacket.reset();
      kexBuffer.putByte(SSH_MSG_KEXINIT);
      random.fill(kexBuffer.getArray(), kexBuffer.getIndex(), KEX_COOKIE_LENGTH);
      kexBuffer.skip(KEX_COOKIE_LENGTH)// Move index forward
      kexBuffer.putString(_session.getConfig().getString(SessionConfig.KEX_ALGORITHMS));
      kexBuffer.putString(_session.getConfig().getString(SessionConfig.KEX_SERVER_HOST_KEY));
      kexBuffer.putString(_session.getConfig().getCiphersC2S())// Checked list of client-to-server ciphers
      kexBuffer.putString(_session.getConfig().getCiphersS2C())// Checked list of server-to-client ciphers
      kexBuffer.putString(_session.getConfig().getString(SessionConfig.KEX_MAC_C2S));
      kexBuffer.putString(_session.getConfig().getString(SessionConfig.KEX_MAC_S2C));
      kexBuffer.putString(_session.getConfig().getString(SessionConfig.KEX_COMPRESSION_C2S));
      kexBuffer.putString(_session.getConfig().getString(SessionConfig.KEX_COMPRESSION_S2C));
      kexBuffer.putString(_session.getConfig().getString(SessionConfig.KEX_LANG_C2S));
      kexBuffer.putString(_session.getConfig().getString(SessionConfig.KEX_LANG_S2C));
      kexBuffer.putByte((byte) 0)// 0 is false, not sending guessed packet
      kexBuffer.putInt(0);

      // Set the client's kex algorithm initialization message
      I_C = new byte[kexBuffer.getIndex()-5];
      System.arraycopy(kexBuffer.getArray(), 5, I_C, 0, I_C.length);
      _session.write(kexPacket)// Send key exchange init message to server
      JSch.getLogger().log(Logger.Level.INFO, "SSH_MSG_KEXINIT sent");
    } catch(Exception e) {
      throw new KexException("Failed to send SSH_MSG_KEXINIT", e);
    } finally {
      kexBuffer.clear()// Clear buffer to ensure sensitive data is wiped
    }
  }
View Full Code Here


   * @throws KexException if any errors occur
   */
  public void sendNewKeys() throws KexException {
    try {
      // Send SSH_MSG_NEWKEYS request to server
      Buffer buffer = new Buffer(500);
      Packet packet = new Packet(buffer);
      packet.reset();
      buffer.putByte(SSH_MSG_NEWKEYS);
      _session.write(packet);
      JSch.getLogger().log(Logger.Level.INFO, "SSH_MSG_NEWKEYS sent");
    } catch(Exception e) {
      throw new KexException("Failed to send SSH_MSG_NEWKEYS request", e);
    }
View Full Code Here

          _encodedData[0] == (byte) 0x3f
          && _encodedData[1] == (byte) 0x6f
          && _encodedData[2] == (byte) 0xf9
          && _encodedData[3] == (byte) 0xeb ) {

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

      if( pubkey == null ) {
View Full Code Here

      case SSH_RSA:
        if( _eRSA == null ) { return null; }
        keyBlob = new byte[KeyType.SSH_RSA.toString().length() + 4
            + _eRSA.length + 4
            + _nRSA.length + 4];
        Buffer rsaBuf = new Buffer(keyBlob);
        rsaBuf.putString(KeyType.SSH_RSA.getBytes());
        rsaBuf.putString(_eRSA);
        rsaBuf.putString(_nRSA);
        return keyBlob;

      case SSH_DSS:
        if( _pDSA == null ) { return null; }
        keyBlob = new byte[KeyType.SSH_DSS.toString().length() + 4
            + _pDSA.length + 4
            + _qDSA.length + 4
            + _gDSA.length + 4
            + _pubKeyDSA.length + 4];
        Buffer dsaBuf = new Buffer(keyBlob);
        dsaBuf.putString(KeyType.SSH_DSS.getBytes());
        dsaBuf.putString(_pDSA);
        dsaBuf.putString(_qDSA);
        dsaBuf.putString(_gDSA);
        dsaBuf.putString(_pubKeyDSA);
        return keyBlob;

      default:
        throw new IllegalStateException("Failed to generate public key blob, invalid key type: "+_keyType);
    }
View Full Code Here

          SignatureRSA rsa = AlgorithmManager.getManager().createAlgorithm(Algorithms.SIGNATURE_RSA);
          rsa.setPrvKey(_dRSA, _nRSA);
          rsa.update(data);
          byte[] sig = rsa.sign();
          byte[] buffer = new byte[KeyType.SSH_RSA.toString().length() + 4 + sig.length + 4];
          Buffer buf = new Buffer(buffer);
          buf.putString(KeyType.SSH_RSA.getBytes());
          buf.putString(sig);
          return buffer;
        } catch(Exception e) {
          // TODO Error handling?
        }
        return null;
      }
      case SSH_DSS: {
        try {
          SignatureDSA dsa = AlgorithmManager.getManager().createAlgorithm(Algorithms.SIGNATURE_DSS);
          dsa.setPrvKey(_prvKeyDSA, _pDSA, _qDSA, _gDSA);
          dsa.update(data);
          byte[] sig = dsa.sign();
          byte[] buffer = new byte[KeyType.SSH_DSS.toString().length() + 4 + sig.length + 4];
          Buffer buf = new Buffer(buffer);
          buf.putString(KeyType.SSH_DSS.getBytes());
          buf.putString(sig);
          return buffer;
        } catch(Exception e) {
          // TODO Error handling?
        }
        return null;
View Full Code Here

        }
        plain = _encodedData;
      }

      if( _vendor == FSECURE ) {              // FSecure
        Buffer buf = new Buffer(plain);
        int foo = buf.getInt();
        if( plain.length != foo + 4 ) {
          return false;
        }
        _eRSA = buf.getMPIntBits();
        _dRSA = buf.getMPIntBits();
        _nRSA = buf.getMPIntBits();
        buf.getMPIntBits()// u_array
        buf.getMPIntBits()// p_array
        buf.getMPIntBits()// q_array
        return true;
      }

      int[] index = new int[1];
      int length = 0;
View Full Code Here

        }
        plain = _encodedData;
      }

      if( _vendor == FSECURE ) {              // FSecure
        Buffer buf = new Buffer(plain);
        int foo = buf.getInt();
        if( plain.length != foo + 4 ) {
          return false;
        }
        _pDSA = buf.getMPIntBits();
        _gDSA = buf.getMPIntBits();
        _qDSA = buf.getMPIntBits();
        _pubKeyDSA = buf.getMPIntBits();
        _prvKeyDSA = buf.getMPIntBits();
        return true;
      }

      int[] index = new int[1];
      int length = 0;
View Full Code Here

        _buffer.putString(identity.getAlgorithmName());
        _buffer.putString(pubkeyblob);

        byte[] sid = session.getSessionId();
        byte[] tmpData = new byte[4 + sid.length + _buffer.getLength() - 5];
        Buffer tmp = new Buffer(tmpData);
        tmp.putString(sid);
        tmp.putBytes(_buffer, 5, _buffer.getLength()-5);
        byte[] signature = identity.getSignature(tmpData);
        if( signature == null ) {  // for example, too long key length.
          break;
        }
        _buffer.putString(signature);
View Full Code Here

   */
  static void sendUserAuthInit(Session session) throws Exception {
    // send user auth request
    // byte      SSH_MSG_SERVICE_REQUEST(5)
    // string    service name "ssh-userauth"
    Buffer buffer = new Buffer(100);
    Packet packet = new Packet(buffer);
    packet.reset();
    buffer.putByte(SSH_MSG_SERVICE_REQUEST);
    buffer.putString(SSH_USERAUTH);
    session.write(packet);
    JSch.getLogger().log(Level.INFO, "SSH_MSG_SERVICE_REQUEST for UserAuth sent");

    // receive user auth response
    // byte      SSH_MSG_SERVICE_ACCEPT(6)
    // string    service name
    if( session.read(buffer).getCommand() != SSH_MSG_SERVICE_ACCEPT ) {
      throw new JSchException("UserAuth service failed, expected SSH_MSG_SERVICE_ACCEPT(6): "+buffer.getCommand());
    }
    JSch.getLogger().log(Level.INFO, "SSH_MSG_SERVICE_ACCEPT for UserAuth received");
  }
View Full Code Here

        token = _buffer.getString();
      }
    }

    byte[] data = new byte[1024 * 20];
    Buffer mbuf = new Buffer(data);
    // string    session identifier
    // byte      SSH_MSG_USERAUTH_REQUEST
    // string    user name
    // string    service
    // string    "gssapi-with-mic"
    mbuf.putString(session.getSessionId());
    mbuf.putByte(SSH_MSG_USERAUTH_REQUEST);
    mbuf.putString(session.getUserName());
    mbuf.putString(SSH_CONNECTION);
    mbuf.putString(UserAuth.GSSAPI_WITH_MIC);

    byte[] mic = context.getMIC(data, 0, mbuf.getLength());
    if( mic == null ) {
      return false;
    }

    _packet.reset();
View Full Code Here

TOP

Related Classes of org.vngx.jsch.Buffer

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.