Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-545] Implements global-requests-ok extension #568

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -23,16 +23,21 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.BiConsumer;

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;
import org.apache.sshd.common.signature.Signature;
import org.apache.sshd.common.util.buffer.Buffer;
import org.apache.sshd.common.util.logging.AbstractLoggingBean;

/**
Expand Down Expand Up @@ -88,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 @@ -133,4 +140,35 @@ protected void handleServerSignatureAlgorithms(Session session, Collection<Strin
session.setSignatureFactories(clientAlgorithms);
}
}

@Override
public void sendKexExtensions(Session session, KexPhase phase) throws Exception {
Map<String, Object> extensions = new LinkedHashMap<>();
collectExtensions(session, phase, extensions::put);
if (!extensions.isEmpty()) {
Buffer buffer = session.createBuffer(KexExtensions.SSH_MSG_EXT_INFO);
KexExtensions.putExtensions(extensions.entrySet(), buffer);
if (log.isDebugEnabled()) {
log.debug("sendKexExtensions({})[{}]: sending SSH_MSG_EXT_INFO with {} info records", session, phase,
extensions.size());
}
session.writePacket(buffer);
}
}

/**
* 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 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, "");
}
}
Loading