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-562] Implement [email protected] extension #566

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.ExtInfoInAuth;
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,
ExtInfoInAuth.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://github.com/openssh/openssh-portable/blob/V_9_8/PROTOCOL#L167-L184">[email protected]</A>
*/
public class ExtInfoInAuth extends AbstractKexExtensionParser<String> {

public static final String NAME = "[email protected]";

public static final ExtInfoInAuth INSTANCE = new ExtInfoInAuth();

public ExtInfoInAuth() {
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.ExtInfoInAuth;
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 @@ -133,4 +138,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 does not marshal any 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) {
// [email protected]
marshaller.accept(ExtInfoInAuth.NAME, "");
}
}