private static List<String> unpackList(byte[] packedString) throws BufferException { List<String> list = new ArrayList<String>(); Buffer<?> buf = new Buffer.PlainBuffer(packedString); while (buf.available() > 0) { list.add(buf.readString()); } return list; }
public static byte[] encode(Map<PTYMode, Integer> modes) { Buffer.PlainBuffer buf = new Buffer.PlainBuffer(); for (Entry<PTYMode, Integer> entry : modes.entrySet()) { buf.putByte(entry.getKey().getOpcode()); buf.putUInt32(entry.getValue()); } buf.putByte((byte) 0); return buf.getCompactData(); }
private static Map<String, String> unpackMap(byte[] packedString) throws BufferException { Map<String, String> map = new LinkedHashMap<String, String>(); Buffer<?> buf = new Buffer.PlainBuffer(packedString); while (buf.available() > 0) { String name = buf.readString(); String data = unpackString(buf.readStringAsBytes()); map.put(name, data); } return map; }
@Test public void testECDSA521Verifies() throws BufferException { byte[] K_S = fromString("0, 0, 0, 19, 101, 99, 100, 115, 97, 45, 115, 104, 97, 50, 45, 110, 105, 115, 116, 112, 53, 50, 49, 0, 0, 0, 8, 110, 105, 115, 116, 112, 53, 50, 49, 0, 0, 0, -123, 4, 1, -56, 55, 64, -73, -109, 95, 94, -107, -116, -46, -16, 119, -66, -68, 41, -103, -66, 102, -123, -69, 59, -8, 106, 72, 75, 7, 56, -79, 109, -88, 77, 12, -97, -109, -32, -60, 64, -75, -48, 50, -51, -68, -81, 75, 110, -7, -79, -32, -36, -73, -7, -65, -24, 40, -74, 58, 43, -26, -5, -55, 125, -32, -89, -54, -111, 0, 81, 37, -73, 60, 69, 107, -108, 115, 60, -61, 22, 6, -128, -69, -28, 122, -26, -37, -117, 121, -106, -126, 23, -90, 127, 73, -58, -113, -61, 105, 68, 116, 85, -115, -47, 90, 122, 109, -21, 127, 39, -75, -58, -109, 73, -82, -122, -11, -44, -87, 85, -100, -4, -123, -31, 126, -94, 127, 96, 9, -30, 70, -113, -42, 28"); byte[] H = fromString("-36, -107, -95, 2, 93, -111, -19, -107, 118, -7, 116, -33, 58, -90, -63, -60, -5, -23, 7, 56, -128, -22, -15, 26, -97, 2, 50, -93, 21, -21, 69, 105"); byte[] sig = fromString("0, 0, 0, 19, 101, 99, 100, 115, 97, 45, 115, 104, 97, 50, 45, 110, 105, 115, 116, 112, 53, 50, 49, 0, 0, 0, -117, 0, 0, 0, 66, 1, 31, -111, 69, -37, 33, 24, -95, 53, -124, -33, 41, 65, -96, -112, -102, -33, 123, 30, -108, 102, 127, -27, 72, 101, -108, -123, 6, 107, 83, -72, -121, 87, -86, 75, 114, 50, -60, -75, -46, 7, -63, 84, -114, -91, 113, 52, 26, 102, -11, 76, 99, 9, 19, -73, -42, -3, 57, 41, -42, 13, -81, 18, -3, -49, -50, 0, 0, 0, 65, 102, 60, -2, 123, 91, -8, 120, 42, 118, 118, -9, -112, 72, 8, 61, -49, -45, 63, 112, 61, -55, -122, -109, 4, -39, 95, 3, -4, -43, 98, 39, 4, 63, 78, 78, 51, 77, 75, -23, 19, -46, 117, -115, -95, 90, -43, 108, -47, -90, 84, 98, 50, -97, -37, -14, -115, -76, 14, -61, 91, 107, 23, -112, 22, -15"); PublicKey hostKey = new Buffer.PlainBuffer(K_S).readPublicKey(); Signature signature = new SignatureECDSA.Factory521().create(); signature.initVerify(hostKey); signature.update(H, 0, H.length); Assert.assertTrue("ECDSA521 signature verifies", signature.verify(sig)); }
@Test public void testECDSA256Verifies() throws BufferException { byte[] K_S = fromString("0, 0, 0, 19, 101, 99, 100, 115, 97, 45, 115, 104, 97, 50, 45, 110, 105, 115, 116, 112, 50, 53, 54, 0, 0, 0, 8, 110, 105, 115, 116, 112, 50, 53, 54, 0, 0, 0, 65, 4, -8, 35, 96, -97, 65, -33, -128, -58, -64, -73, -51, 10, -28, 20, -59, 86, -88, -24, 126, 29, 115, 26, -88, 31, -115, 87, -109, -4, 61, 108, 28, 31, -66, 79, 107, 17, 24, 93, 114, -25, 121, 57, -58, 10, 26, -36, -100, -120, -7, -103, 86, 72, -109, -82, 111, 73, 4, -98, 58, 28, -3, -91, 28, 84"); byte[] H = fromString("61, 55, -62, -122, -93, 82, -63, 25, -52, -13, -41, -29, 78, 101, 22, -75, 113, 59, -72, -92, -2, 39, -52, -89, 127, 80, -77, -82, 67, 3, -21, -53"); byte[] sig = fromString("0, 0, 0, 19, 101, 99, 100, 115, 97, 45, 115, 104, 97, 50, 45, 110, 105, 115, 116, 112, 50, 53, 54, 0, 0, 0, 73, 0, 0, 0, 33, 0, -19, 50, -123, -35, 93, 50, 3, 40, -79, 110, -99, 6, -78, 40, -31, -26, -119, 113, -101, 109, -27, 12, 47, -119, -83, 107, -7, 116, 2, 97, 84, 32, 0, 0, 0, 32, 69, -44, 52, -119, 22, -60, -33, -105, -41, 45, 36, 112, -59, 49, -90, -110, -13, -114, 115, -86, 29, 30, 127, -44, 96, 57, -49, 39, -83, 50, -8, 123"); PublicKey hostKey = new Buffer.PlainBuffer(K_S).readPublicKey(); Signature signature = new SignatureECDSA.Factory256().create(); signature.initVerify(hostKey); signature.update(H, 0, H.length); Assert.assertTrue("ECDSA256 signature verifies", signature.verify(sig)); }
@Test public void shouldExpandCapacityOfUInt32(){ PlainBuffer buf = new PlainBuffer(); for(int i=0;i<Buffer.DEFAULT_SIZE+1;i+=4) { buf.putUInt32(1l); } /* Buffer should have been expanded at this point*/ assertEquals(Buffer.DEFAULT_SIZE*2,buf.data.length); }
@Test public void shouldHaveSameUInt64EncodingForBigIntegerAndLong() { long[] values = { 0l, 1l, 232634978082517765l, Long.MAX_VALUE - 1, Long.MAX_VALUE }; for (long value : values) { byte[] bytesBigInt = new PlainBuffer().putUInt64(BigInteger.valueOf(value)).getCompactData(); byte[] bytesLong = new PlainBuffer().putUInt64(value).getCompactData(); assertArrayEquals("Value: " + value, bytesLong, bytesBigInt); } }
@Test public void shouldThrowOnPutNegativeBigIntegerUInt64() { try { new PlainBuffer().putUInt64(new BigInteger("-1")); fail("Added negative uint64 to buffer?"); } catch (IllegalArgumentException e) { // success } }
@Test public void shouldThrowOnTooLargeInitialCapacity() { try { new PlainBuffer(Integer.MAX_VALUE); fail("Allegedly created buffer with size " + Integer.MAX_VALUE); } catch (IllegalArgumentException e) { // success } }
@Override public Subsystem startSubsystem(String name) throws ConnectionException, TransportException { checkReuse(); log.info("Will request `{}` subsystem", name); sendChannelRequest("subsystem", true, new Buffer.PlainBuffer().putString(name)) .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); usedUp = true; return this; }
@Override public Command exec(String command) throws ConnectionException, TransportException { checkReuse(); log.debug("Will request to exec `{}`", command); sendChannelRequest("exec", true, new Buffer.PlainBuffer().putString(command, getRemoteCharset())) .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); usedUp = true; return this; }
public ChannelInputStream(Channel chan, Transport trans, Window.Local win) { this.chan = chan; log = chan.getLoggerFactory().getLogger(getClass()); this.trans = trans; this.win = win; buf = new Buffer.PlainBuffer(chan.getLocalMaxPacketSize()); }
private PlainBuffer decryptBuffer(PlainBuffer privateKeyBuffer, String cipherName, String kdfName, byte[] kdfOptions) throws IOException { Cipher cipher = createCipher(cipherName); initializeCipher(kdfName, kdfOptions, cipher); byte[] array = privateKeyBuffer.array(); cipher.update(array, 0, privateKeyBuffer.available()); return new PlainBuffer(array); }
@Test public void shouldExpandCapacityOfUInt64(){ BigInteger bigUint64 = BigInteger.valueOf(Long.MAX_VALUE); PlainBuffer buf = new PlainBuffer(); assertEquals(Buffer.DEFAULT_SIZE,buf.data.length); for(int i=0;i<Buffer.DEFAULT_SIZE+1;i+=8) { buf.putUInt64(bigUint64.longValue()); } /* Buffer should have been expanded at this point*/ assertEquals(Buffer.DEFAULT_SIZE*2,buf.data.length); }
private static byte[] packList(Iterable<String> strings) { Buffer<?> buf = new Buffer.PlainBuffer(); for (String string : strings) { buf.putString(string); } return buf.getCompactData(); }
@Override public void signal(Signal sig) throws TransportException { sendChannelRequest("signal", false, new Buffer.PlainBuffer().putString(sig.toString())); }
@Test public void shouldThrowOnTooLargeCapacity() { PlainBuffer buffer = new PlainBuffer(); try { buffer.ensureCapacity(Integer.MAX_VALUE); fail("Allegedly ensured buffer capacity of size " + Integer.MAX_VALUE); } catch (IllegalArgumentException e) { // success } }
@Override public void setEnvVar(String name, String value) throws ConnectionException, TransportException { sendChannelRequest("env", true, new Buffer.PlainBuffer().putString(name).putString(value)) .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); }
@Test public void testPassword() throws Buffer.BufferException { char[] pass = "lolcatz".toCharArray(); // test if put correctly as a string assertEquals(new Buffer.PlainBuffer().putSensitiveString(pass).readString(), "lolcatz"); // test that char[] was blanked out assertArrayEquals(pass, " ".toCharArray()); }
@Before public void setUp() throws UnsupportedEncodingException, GeneralSecurityException { // for position test byte[] data = "Hello".getBytes(IOUtils.UTF8); posBuf = new Buffer.PlainBuffer(data); handyBuf = new Buffer.PlainBuffer(); }