Skip to content

Commit

Permalink
Implements global-requests-ok extension (fixes #545)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jul 26, 2024
1 parent a8bee7e commit 7f124a9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.kex.extension.parser.DelayCompression;
import org.apache.sshd.common.kex.extension.parser.Elevation;
import org.apache.sshd.common.kex.extension.parser.GlobalRequestsOk;
import org.apache.sshd.common.kex.extension.parser.NoFlowControl;
import org.apache.sshd.common.kex.extension.parser.ServerSignatureAlgorithms;
import org.apache.sshd.common.util.GenericUtils;
Expand Down Expand Up @@ -84,7 +85,8 @@ public final class KexExtensions {
ServerSignatureAlgorithms.INSTANCE,
NoFlowControl.INSTANCE,
Elevation.INSTANCE,
DelayCompression.INSTANCE)
DelayCompression.INSTANCE,
GlobalRequestsOk.INSTANCE)
.collect(Collectors.toMap(
NamedResource::getName, Function.identity(),
MapEntryUtils.throwingMerger(), () -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sshd.common.kex.extension.parser;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.sshd.common.util.buffer.Buffer;

/**
* @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a>
* @see <A HREF=
* "https://datatracker.ietf.org/doc/html/draft-ssh-global-requests-ok-00">draft-ssh-global-requests-ok-00</A>
*/
public class GlobalRequestsOk extends AbstractKexExtensionParser<String> {

public static final String NAME = "global-requests-ok";

public static final GlobalRequestsOk INSTANCE = new GlobalRequestsOk();

public GlobalRequestsOk() {
super(NAME);
}

@Override
protected void encode(String value, Buffer buffer) throws IOException {
buffer.putString(value);
}

@Override
public String parseExtension(byte[] data, int off, int len) throws IOException {
return (len <= 0) ? "" : new String(data, off, len, StandardCharsets.UTF_8);
}

@Override
public String parseExtension(Buffer buffer) throws IOException {
return parseExtension(buffer.array(), buffer.rpos(), buffer.available());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import org.apache.sshd.common.AttributeRepository.AttributeKey;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.kex.extension.parser.GlobalRequestsOk;
import org.apache.sshd.common.kex.extension.parser.HostBoundPubkeyAuthentication;
import org.apache.sshd.common.kex.extension.parser.ServerSignatureAlgorithms;
import org.apache.sshd.common.session.Session;
Expand Down Expand Up @@ -92,6 +93,8 @@ public boolean handleKexExtensionRequest(
} else {
session.setAttribute(HOSTBOUND_AUTHENTICATION, version);
}
} else if (GlobalRequestsOk.NAME.equals(name)) {
GlobalRequestsOk.INSTANCE.parseExtension(data);
}
return true;
}
Expand Down Expand Up @@ -157,13 +160,15 @@ public void sendKexExtensions(Session session, KexPhase phase) throws Exception
* Collects extension info records, handing them off to the given {@code marshaller} for writing into an
* {@link KexExtensions#SSH_MSG_EXT_INFO} message.
* <p>
* This default implementation does not marshal any extension.
* This default implementation marshals a {@link GlobalRequestsOk} extension}.
* </p>
*
* @param session {@link Session} to send the KEX extension information for
* @param phase {@link KexPhase} of the SSH protocol
* @param marshaller {@link BiConsumer} writing the extensions into an SSH message
*/
public void collectExtensions(Session session, KexPhase phase, BiConsumer<String, Object> marshaller) {
// global-requests-ok
marshaller.accept(GlobalRequestsOk.NAME, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.sshd.common.kex.extension;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
Expand All @@ -27,6 +28,7 @@

import org.apache.sshd.common.AttributeRepository.AttributeKey;
import org.apache.sshd.common.kex.KexProposalOption;
import org.apache.sshd.common.kex.extension.parser.GlobalRequestsOk;
import org.apache.sshd.common.kex.extension.parser.ServerSignatureAlgorithms;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.GenericUtils;
Expand Down Expand Up @@ -130,6 +132,16 @@ public void sendKexExtensions(Session session, KexPhase phase) throws Exception
}
}

@Override
public boolean handleKexExtensionRequest(
Session session, int index, int count, String name, byte[] data)
throws IOException {
if (GlobalRequestsOk.NAME.equals(name)) {
GlobalRequestsOk.INSTANCE.parseExtension(data);
}
return true;
}

/**
* Collects extension info records, handing them off to the given {@code marshaller} for writing into an
* {@link KexExtensions#SSH_MSG_EXT_INFO} message.
Expand Down Expand Up @@ -157,5 +169,7 @@ public void collectExtensions(Session session, KexPhase phase, BiConsumer<String
ServerSignatureAlgorithms.NAME);
}
}
// global-requests-ok
marshaller.accept(GlobalRequestsOk.NAME, "");
}
}

0 comments on commit 7f124a9

Please sign in to comment.