Skip to content

Commit 8458147

Browse files
committed
Implements global-requests-ok extension (fixes #545)
1 parent e58d2f1 commit 8458147

4 files changed

Lines changed: 78 additions & 2 deletions

File tree

sshd-common/src/main/java/org/apache/sshd/common/kex/extension/KexExtensions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.sshd.common.NamedResource;
4040
import org.apache.sshd.common.kex.extension.parser.DelayCompression;
4141
import org.apache.sshd.common.kex.extension.parser.Elevation;
42+
import org.apache.sshd.common.kex.extension.parser.GlobalRequestsOk;
4243
import org.apache.sshd.common.kex.extension.parser.NoFlowControl;
4344
import org.apache.sshd.common.kex.extension.parser.ServerSignatureAlgorithms;
4445
import org.apache.sshd.common.util.GenericUtils;
@@ -84,7 +85,8 @@ public final class KexExtensions {
8485
ServerSignatureAlgorithms.INSTANCE,
8586
NoFlowControl.INSTANCE,
8687
Elevation.INSTANCE,
87-
DelayCompression.INSTANCE)
88+
DelayCompression.INSTANCE,
89+
GlobalRequestsOk.INSTANCE)
8890
.collect(Collectors.toMap(
8991
NamedResource::getName, Function.identity(),
9092
MapEntryUtils.throwingMerger(), () -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER)));
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sshd.common.kex.extension.parser;
20+
21+
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
23+
24+
import org.apache.sshd.common.util.buffer.Buffer;
25+
26+
/**
27+
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
28+
* @see <A HREF=
29+
* "https://datatracker.ietf.org/doc/html/draft-ssh-global-requests-ok-00">draft-ssh-global-requests-ok-00</A>
30+
*/
31+
public class GlobalRequestsOk extends AbstractKexExtensionParser<String> {
32+
33+
public static final String NAME = "global-requests-ok";
34+
35+
public static final GlobalRequestsOk INSTANCE = new GlobalRequestsOk();
36+
37+
public GlobalRequestsOk() {
38+
super(NAME);
39+
}
40+
41+
@Override
42+
protected void encode(String value, Buffer buffer) throws IOException {
43+
buffer.putString(value);
44+
}
45+
46+
@Override
47+
public String parseExtension(byte[] data, int off, int len) throws IOException {
48+
return (len <= 0) ? "" : new String(data, off, len, StandardCharsets.UTF_8);
49+
}
50+
51+
@Override
52+
public String parseExtension(Buffer buffer) throws IOException {
53+
return parseExtension(buffer.array(), buffer.rpos(), buffer.available());
54+
}
55+
}

sshd-core/src/main/java/org/apache/sshd/common/kex/extension/DefaultClientKexExtensionHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import org.apache.sshd.common.AttributeRepository.AttributeKey;
3434
import org.apache.sshd.common.NamedFactory;
35+
import org.apache.sshd.common.kex.extension.parser.GlobalRequestsOk;
3536
import org.apache.sshd.common.kex.extension.parser.HostBoundPubkeyAuthentication;
3637
import org.apache.sshd.common.kex.extension.parser.ServerSignatureAlgorithms;
3738
import org.apache.sshd.common.session.Session;
@@ -92,6 +93,8 @@ public boolean handleKexExtensionRequest(
9293
} else {
9394
session.setAttribute(HOSTBOUND_AUTHENTICATION, version);
9495
}
96+
} else if (GlobalRequestsOk.NAME.equals(name)) {
97+
GlobalRequestsOk.INSTANCE.parseExtension(data);
9598
}
9699
return true;
97100
}
@@ -157,13 +160,15 @@ public void sendKexExtensions(Session session, KexPhase phase) throws Exception
157160
* Collects extension info records, handing them off to the given {@code marshaller} for writing into an
158161
* {@link KexExtensions#SSH_MSG_EXT_INFO} message.
159162
* <p>
160-
* This default implementation does not marshal any extension.
163+
* This default implementation marshals a {@link GlobalRequestsOk} extension}.
161164
* </p>
162165
*
163166
* @param session {@link Session} to send the KEX extension information for
164167
* @param phase {@link KexPhase} of the SSH protocol
165168
* @param marshaller {@link BiConsumer} writing the extensions into an SSH message
166169
*/
167170
public void collectExtensions(Session session, KexPhase phase, BiConsumer<String, Object> marshaller) {
171+
// global-requests-ok
172+
marshaller.accept(GlobalRequestsOk.NAME, "");
168173
}
169174
}

sshd-core/src/main/java/org/apache/sshd/common/kex/extension/DefaultServerKexExtensionHandler.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

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

22+
import java.io.IOException;
2223
import java.util.Arrays;
2324
import java.util.Collection;
2425
import java.util.LinkedHashMap;
@@ -27,6 +28,7 @@
2728

2829
import org.apache.sshd.common.AttributeRepository.AttributeKey;
2930
import org.apache.sshd.common.kex.KexProposalOption;
31+
import org.apache.sshd.common.kex.extension.parser.GlobalRequestsOk;
3032
import org.apache.sshd.common.kex.extension.parser.ServerSignatureAlgorithms;
3133
import org.apache.sshd.common.session.Session;
3234
import org.apache.sshd.common.util.GenericUtils;
@@ -130,6 +132,16 @@ public void sendKexExtensions(Session session, KexPhase phase) throws Exception
130132
}
131133
}
132134

135+
@Override
136+
public boolean handleKexExtensionRequest(
137+
Session session, int index, int count, String name, byte[] data)
138+
throws IOException {
139+
if (GlobalRequestsOk.NAME.equals(name)) {
140+
GlobalRequestsOk.INSTANCE.parseExtension(data);
141+
}
142+
return true;
143+
}
144+
133145
/**
134146
* Collects extension info records, handing them off to the given {@code marshaller} for writing into an
135147
* {@link KexExtensions#SSH_MSG_EXT_INFO} message.
@@ -157,5 +169,7 @@ public void collectExtensions(Session session, KexPhase phase, BiConsumer<String
157169
ServerSignatureAlgorithms.NAME);
158170
}
159171
}
172+
// global-requests-ok
173+
marshaller.accept(GlobalRequestsOk.NAME, "");
160174
}
161175
}

0 commit comments

Comments
 (0)