Exemple #1
0
  /**
   * Creates a new SshMsgKexInit object.
   *
   * @param props
   */
  public SshMsgKexInit(SshConnectionProperties props) {
    super(SSH_MSG_KEX_INIT);

    // Create some random data
    cookie = new byte[16];

    // Seed the random number generator
    Random r = ConfigurationLoader.getRND();

    // Get the next random bytes into our cookie
    r.nextBytes(cookie);

    // Get the supported algorithms from the factory objects but adding the
    // preffered algorithm to the top of the list
    supportedKex =
        sortAlgorithmList(SshKeyExchangeFactory.getSupportedKeyExchanges(), props.getPrefKex());
    supportedPK = sortAlgorithmList(SshKeyPairFactory.getSupportedKeys(), props.getPrefPublicKey());
    supportedEncryptCS =
        sortAlgorithmList(SshCipherFactory.getSupportedCiphers(), props.getPrefCSEncryption());
    supportedEncryptSC =
        sortAlgorithmList(SshCipherFactory.getSupportedCiphers(), props.getPrefSCEncryption());
    supportedMacCS = sortAlgorithmList(SshHmacFactory.getSupportedMacs(), props.getPrefCSMac());
    supportedMacSC = sortAlgorithmList(SshHmacFactory.getSupportedMacs(), props.getPrefSCMac());
    supportedCompCS =
        sortAlgorithmList(SshCompressionFactory.getSupportedCompression(), props.getPrefCSComp());
    supportedCompSC =
        sortAlgorithmList(SshCompressionFactory.getSupportedCompression(), props.getPrefSCComp());

    // We currently don't support language preferences
    supportedLangCS = new ArrayList();
    supportedLangSC = new ArrayList();

    // We don't guess (I don't see the point of this in the protocol!)
    firstKexFollows = false;
  }
  /**
   * @param msg
   * @param sender
   * @throws IOException
   * @throws TransportProtocolException
   */
  public synchronized void sendMessage(SshMessage msg, Object sender) throws IOException {
    // Send a message, if were in key exchange then add it to
    // the list unless of course it is a transport protocol or key
    // exchange message
    if (log.isDebugEnabled()) {
      log.info("Sending " + msg.getMessageName());
    }

    int currentState = state.getValue();

    if (sender instanceof SshKeyExchange
        || sender instanceof TransportProtocolCommon
        || (currentState == TransportProtocolState.CONNECTED)) {
      sshOut.sendMessage(msg);

      if (currentState == TransportProtocolState.CONNECTED) {
        if (sendIgnore) {
          byte[] count = new byte[1];
          ConfigurationLoader.getRND().nextBytes(count);

          byte[] rand = new byte[(count[0] & 0xFF) + 1];
          ConfigurationLoader.getRND().nextBytes(rand);

          SshMsgIgnore ignore = new SshMsgIgnore(new String(rand));

          if (log.isDebugEnabled()) {
            log.debug("Sending " + ignore.getMessageName());
          }

          sshOut.sendMessage(ignore);
        }
      }
    } else if (currentState == TransportProtocolState.PERFORMING_KEYEXCHANGE) {
      log.debug("Adding to message queue whilst in key exchange");

      synchronized (messageStack) {
        // Add this message to the end of the list
        messageStack.add(msg);
      }
    } else {
      throw new TransportProtocolException("The transport protocol is disconnected");
    }
  }