/**
   * @param buffer The {@link Buffer}
   * @param target A target path {@link String} or {@link Handle} or {@code byte[]} to be encoded in
   *     the buffer
   * @return The updated buffer
   * @throws UnsupportedOperationException If target is not one of the above supported types
   */
  public Buffer putTarget(Buffer buffer, Object target) {
    if (target instanceof CharSequence) {
      buffer.putString(target.toString());
    } else if (target instanceof byte[]) {
      buffer.putBytes((byte[]) target);
    } else if (target instanceof Handle) {
      buffer.putBytes(((Handle) target).getIdentifier());
    } else {
      throw new UnsupportedOperationException("Unknown target type: " + target);
    }

    return buffer;
  }
예제 #2
0
 public synchronized void write(byte[] buf, int off, int len) {
   if (len == 1) {
     write(buf[off] & 0xFF);
   } else {
     buffer.putBytes(buf, off, len);
   }
 }
예제 #3
0
  @Override
  public Boolean doAuth(Buffer buffer, boolean init) throws Exception {
    ValidateUtils.checkTrue(init, "Instance not initialized");
    boolean hasSig = buffer.getBoolean();
    String alg = buffer.getString();

    int oldLim = buffer.wpos();
    int oldPos = buffer.rpos();
    int len = buffer.getInt();
    buffer.wpos(buffer.rpos() + len);
    PublicKey key = buffer.getRawPublicKey();
    ServerFactoryManager manager = session.getFactoryManager();
    Signature verif =
        ValidateUtils.checkNotNull(
            NamedFactory.Utils.create(manager.getSignatureFactories(), alg),
            "No verifier located for algorithm=%s",
            alg);
    verif.initVerifier(key);
    buffer.wpos(oldLim);

    byte[] sig = hasSig ? buffer.getBytes() : null;

    PublickeyAuthenticator authenticator =
        ValidateUtils.checkNotNull(
            manager.getPublickeyAuthenticator(), "No PublickeyAuthenticator configured");
    if (!authenticator.authenticate(username, key, session)) {
      return Boolean.FALSE;
    }

    if (!hasSig) {
      Buffer buf = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_PK_OK);
      buf.putString(alg);
      buf.putRawBytes(buffer.array(), oldPos, 4 + len);
      session.writePacket(buf);
      return null;
    } else {
      Buffer buf = new ByteArrayBuffer();
      buf.putBytes(session.getKex().getH());
      buf.putByte(SshConstants.SSH_MSG_USERAUTH_REQUEST);
      buf.putString(username);
      buf.putString(service);
      buf.putString(UserAuthPublicKeyFactory.NAME);
      buf.putBoolean(true);
      buf.putString(alg);
      buffer.rpos(oldPos);
      buffer.wpos(oldPos + 4 + len);
      buf.putBuffer(buffer);
      verif.update(buf.array(), buf.rpos(), buf.available());
      if (!verif.verify(sig)) {
        throw new Exception("Key verification failed");
      }
      return Boolean.TRUE;
    }
  }