Skip to content

Commit 13f9800

Browse files
ylangiscdkocher
authored andcommitted
ByteBuffer.array() must not be used as it does not take the real buffer size into account and returns the whole buffer up to its capacity. Fixes #745.
1 parent 7c14098 commit 13f9800

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/main/java/net/schmizz/sshj/userauth/password/PasswordUtils.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.schmizz.sshj.userauth.password;
1717

18+
import java.nio.ByteBuffer;
1819
import java.nio.CharBuffer;
1920
import java.nio.charset.StandardCharsets;
2021
import java.util.Arrays;
@@ -64,6 +65,9 @@ public boolean shouldRetry(Resource<?> resource) {
6465
*/
6566
public static byte[] toByteArray(char[] password) {
6667
CharBuffer charBuffer = CharBuffer.wrap(password);
67-
return StandardCharsets.UTF_8.encode(charBuffer).array();
68+
final ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(charBuffer);
69+
byte[] bytes = new byte[byteBuffer.remaining()];
70+
byteBuffer.get(bytes, 0, bytes.length);
71+
return bytes;
6872
}
6973
}

src/test/java/net/schmizz/sshj/keyprovider/PuTTYKeyFileTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ public class PuTTYKeyFileTest {
209209
"oYhmT2+0DKBuBVCAM4qRdA==\n" +
210210
"Private-MAC: 40ccc8b9a7291ec64e5be0c99badbc8a012bf220\n";
211211

212+
final static String ppk1024_umlaut_passphrase = "PuTTY-User-Key-File-2: ssh-rsa\n" +
213+
"Encryption: aes256-cbc\n" +
214+
"Comment: user@host\n" +
215+
"Public-Lines: 4\n" +
216+
"AAAAB3NzaC1yc2EAAAADAQABAAAAgQDsQv60HaW0301hX/xV3AUcutbDDAJp7KWc\n" +
217+
"6swL+H6jhwe3N7FK/SA4492bK5oHwU3ea3X6moLuapTMawMQbRy1kfQm99wcYc7C\n" +
218+
"6PJO3uouzjDatc/aByDejbo5OL9kK4Vy7qm6tw1hC0JIM+TCvItKu+t6Myl7xzv4\n" +
219+
"KbSHiMzulQ==\n" +
220+
"Private-Lines: 8\n" +
221+
"hPS6HYs4t8WChglZzo5G/B0ohnw2DQS19HMPllyVr9XfDyT2Xk8ZSTye84r5CtMP\n" +
222+
"xF4Qc0nkoStyw9p9Tm762FhkM0iGghLWeCdTyqXVlAA9l3sr0BMJ9AoMvjQBqqns\n" +
223+
"gjfPvmtNPFn8sfApHVOv1qSLSGOMZFm/q6KtGuR+IyTnMuZ71b/cQYYHbsAQxt09\n" +
224+
"96I7jDhup/4uoi/tcPYhe998wRFSSldkAtcmYGUnDWCiivlP+gZsXvOI2zs2gCxx\n" +
225+
"ECEwZNTR/j3G0muRUMf91iZSMBije+41j345F+ZHJ43gYXW6lxjFtI5jr9LRGWF1\n" +
226+
"hTeY6IlLt4EBBGNrO8Rn0oGVuQdFQAZaredlt1V5FsgcSaMgg3rlScoz0IHHD66Q\n" +
227+
"Hglp/IYN6Sx6OEGjh3oLGImag+Mz9/9WWGXPLhZ4MUpFAWqcTD4qPK0jYxTCM6QC\n" +
228+
"TybFqMeCSEKiHSOiOGf2oQ==\n" +
229+
"Private-MAC: 6aec23b6267edcb87b05ddef52a80894e3a246c4";
230+
212231
final static String ppkdsa_passphrase = "PuTTY-User-Key-File-2: ssh-dss\n" +
213232
"Encryption: aes256-cbc\n" +
214233
"Comment: dsa-key-20140507\n" +
@@ -502,6 +521,15 @@ public void testCorrectPassphraseRsa() throws Exception {
502521
assertNotNull(key.getPublic());
503522
}
504523

524+
@Test
525+
public void testCorrectPassphraseUmlautRsa() throws Exception {
526+
PuTTYKeyFile key = new PuTTYKeyFile();
527+
key.init(new StringReader(ppk1024_umlaut_passphrase), new UnitTestPasswordFinder("äöü"));
528+
// Install JCE Unlimited Strength Jurisdiction Policy Files if we get java.security.InvalidKeyException: Illegal key size
529+
assertNotNull(key.getPrivate());
530+
assertNotNull(key.getPublic());
531+
}
532+
505533
@Test(expected = IOException.class)
506534
public void testWrongPassphraseRsa() throws Exception {
507535
PuTTYKeyFile key = new PuTTYKeyFile();

0 commit comments

Comments
 (0)