Class Buffer
- java.lang.Object
-
- org.apache.sshd.common.util.buffer.Buffer
-
- All Implemented Interfaces:
Readable
- Direct Known Subclasses:
ByteArrayBuffer
public abstract class Buffer extends java.lang.Object implements Readable
Provides an abstract message buffer for encoding SSH messages
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
workBuf
-
Constructor Summary
Constructors Modifier Constructor Description protected
Buffer()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract byte[]
array()
abstract int
capacity()
Buffer
clear()
Reset read/write positions to zero - Note: zeroes any previously existing dataabstract Buffer
clear(boolean wipeData)
Reset read/write positions to zeroabstract void
compact()
"Shift" the internal data so that reading starts from position zero.protected abstract void
copyRawBytes(int offset, byte[] buf, int pos, int len)
void
dumpHex(SimplifiedLog logger, java.lang.String prefix, PropertyResolver resolver)
void
dumpHex(SimplifiedLog logger, java.util.logging.Level level, java.lang.String prefix, PropertyResolver resolver)
int
ensureAvailable(int reqLen)
Makes sure the buffer contains enough data to accommodate the requested lengthBuffer
ensureCapacity(int capacity)
abstract Buffer
ensureCapacity(int capacity, java.util.function.IntUnaryOperator growthFactor)
protected java.security.KeyPair
extractEC(java.lang.String expectedCurveName, java.security.spec.ECParameterSpec spec)
java.util.Collection<java.lang.String>
getAvailableStrings()
java.util.Collection<java.lang.String>
getAvailableStrings(java.nio.charset.Charset charset)
boolean
getBoolean()
byte
getByte()
byte[]
getBytes()
abstract byte[]
getBytesConsumed()
java.util.List<OpenSshCertificate.CertificateOption>
getCertificateOptions()
java.util.List<OpenSshCertificate.CertificateOption>
getCertificateOptions(java.nio.charset.Charset charset)
According to PROTOCOL.certkeys: Critical Options is a set of bytes that is [overall length][name(string)][[length of buffer][[length of string][data(string)]]]...byte[]
getCompactData()
int
getInt()
java.security.KeyPair
getKeyPair()
long
getLong()
java.math.BigInteger
getMPInt()
byte[]
getMPIntAsBytes()
java.util.List<java.lang.String>
getNameList()
According to RFC 4251: A name-list is represented as a uint32 containing its length (number of bytes that follow) followed by a comma-separated list of zero or more names.java.util.List<java.lang.String>
getNameList(char separator)
java.util.List<java.lang.String>
getNameList(java.nio.charset.Charset charset)
java.util.List<java.lang.String>
getNameList(java.nio.charset.Charset charset, char separator)
Parses a string that contains values separated by a delimiterjava.security.PublicKey
getPublicKey()
java.security.PublicKey
getPublicKey(BufferPublicKeyParser<? extends java.security.PublicKey> parser)
void
getRawBytes(byte[] buf)
java.security.PublicKey
getRawPublicKey()
java.security.PublicKey
getRawPublicKey(BufferPublicKeyParser<? extends java.security.PublicKey> parser)
short
getShort()
java.lang.String
getString()
abstract java.lang.String
getString(java.nio.charset.Charset charset)
Reads a string using a given charset.java.util.Collection<java.lang.String>
getStringList(boolean usePrependedLength)
java.util.Collection<java.lang.String>
getStringList(boolean usePrependedLength, java.nio.charset.Charset charset)
java.util.List<java.lang.String>
getStringList(int count)
java.util.List<java.lang.String>
getStringList(int count, java.nio.charset.Charset charset)
int
getUByte()
long
getUInt()
int
getUShort()
private static boolean
isSensitiveData(int cmd)
boolean
isValidMessageStructure(java.lang.Class<?>... fieldTypes)
boolean
isValidMessageStructure(java.util.Collection<java.lang.Class<?>> fieldTypes)
void
putAndWipeBytes(byte[] b)
Adds the bytes to the buffer and wipes the data from the input buffer after having added it - useful for sensitive information such as passwordvoid
putAndWipeBytes(byte[] b, int off, int len)
void
putAndWipeChars(char[] chars)
Zeroes the input array after having put the characters in the buffer - useful for sensitive information such as passwordsvoid
putAndWipeChars(char[] chars, int offset, int len)
void
putAndWipeChars(char[] chars, int offset, int len, java.nio.charset.Charset charset)
void
putAndWipeChars(char[] chars, java.nio.charset.Charset charset)
void
putBoolean(boolean b)
abstract void
putBuffer(java.nio.ByteBuffer buffer)
void
putBuffer(Readable buffer)
abstract int
putBuffer(Readable buffer, boolean expand)
void
putBufferedData(java.lang.Object buffer)
Checks if the buffer argument is an array of bytes, aReadable
instance or aByteBuffer
and invokes the appropriateputXXX
method.void
putByte(byte b)
void
putBytes(byte[] b)
void
putBytes(byte[] b, int off, int len)
void
putCertificateOptions(java.util.List<OpenSshCertificate.CertificateOption> options)
void
putCertificateOptions(java.util.List<OpenSshCertificate.CertificateOption> options, java.nio.charset.Charset charset)
According to PROTOCOL.certkeys: Certificate Options is a set of bytes that is [overall length][name(string)][[length of buffer][[length of string][data(string)]]]...void
putChars(char[] chars)
void
putChars(char[] chars, int offset, int len)
void
putChars(char[] chars, int offset, int len, java.nio.charset.Charset charset)
void
putChars(char[] chars, java.nio.charset.Charset charset)
void
putInt(long i)
void
putKeyPair(java.security.KeyPair kp)
void
putLong(long i)
Writes 64 bitsvoid
putMPInt(byte[] mpInt)
void
putMPInt(java.math.BigInteger bigint)
void
putNameList(java.util.Collection<java.lang.String> names)
According to RFC 4251:A name-list is represented as a uint32 containing its length (number of bytes that follow) followed by a comma-separated list of zero or more names.
void
putNameList(java.util.Collection<java.lang.String> names, char separator)
void
putNameList(java.util.Collection<java.lang.String> names, java.nio.charset.Charset charset)
void
putNameList(java.util.Collection<java.lang.String> names, java.nio.charset.Charset charset, char separator)
Adds a string that contains values separated by a delimitervoid
putOptionalBufferedData(java.lang.Object buffer)
Checks if the buffer argument is an array of bytes, aReadable
instance or aByteBuffer
and invokes the appropriateputXXX
method.void
putPublicKey(java.security.PublicKey key)
void
putRawBytes(byte[] d)
abstract void
putRawBytes(byte[] d, int off, int len)
void
putRawPublicKey(java.security.PublicKey key)
void
putRawPublicKeyBytes(java.security.PublicKey key)
void
putShort(int i)
Writes 16 bitsvoid
putString(java.lang.String string)
void
putString(java.lang.String string, java.nio.charset.Charset charset)
void
putStringList(java.util.Collection<?> objects, boolean prependLength)
Encodes thetoString
value of each member.void
putStringList(java.util.Collection<?> objects, java.nio.charset.Charset charset, boolean prependLength)
Encodes thetoString
value of each membervoid
putUInt(long i)
Writes 32 bitsbyte
rawByte(int pos)
long
rawUInt(int pos)
abstract int
rpos()
abstract void
rpos(int rpos)
protected abstract int
size()
java.lang.String
toHex()
java.lang.String
toString()
abstract int
wpos()
abstract void
wpos(int wpos)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.sshd.common.util.Readable
available, getRawBytes
-
-
-
-
Method Detail
-
rpos
public abstract int rpos()
- Returns:
- Current reading position
-
rpos
public abstract void rpos(int rpos)
- Parameters:
rpos
- Set current reading position
-
wpos
public abstract int wpos()
- Returns:
- Current writing position
-
wpos
public abstract void wpos(int wpos)
- Parameters:
wpos
- Set current writing position - Note: if necessary, the underlying data buffer will be increased so as to allow writing from the new position
-
capacity
public abstract int capacity()
- Returns:
- Number of bytes that can still be written without re-sizing the internal buffer
-
array
public abstract byte[] array()
- Returns:
- The raw underlying data bytes
-
getBytesConsumed
public abstract byte[] getBytesConsumed()
- Returns:
- The bytes consumed so far
-
rawByte
public byte rawByte(int pos)
- Parameters:
pos
- A position in the raw underlying data bytes- Returns:
- The byte at the specified position without changing the current
read position
. Note: no validation is made whether the position lies within array boundaries
-
rawUInt
public long rawUInt(int pos)
- Parameters:
pos
- A position in the raw underlying data bytes- Returns:
- The unsigned 32 bit integer at the specified position without changing the current
read position
. Note: no validation is made whether the position and the required extra 4 bytes lie within array boundaries
-
compact
public abstract void compact()
"Shift" the internal data so that reading starts from position zero.
-
getCompactData
public byte[] getCompactData()
-
clear
public Buffer clear()
Reset read/write positions to zero - Note: zeroes any previously existing data- Returns:
- Reference to this buffer
- See Also:
clear(boolean)
-
clear
public abstract Buffer clear(boolean wipeData)
Reset read/write positions to zero- Parameters:
wipeData
- Whether to zero any previously existing data- Returns:
- Reference to this buffer
-
isValidMessageStructure
public boolean isValidMessageStructure(java.lang.Class<?>... fieldTypes)
-
isValidMessageStructure
public boolean isValidMessageStructure(java.util.Collection<java.lang.Class<?>> fieldTypes)
-
copyRawBytes
protected abstract void copyRawBytes(int offset, byte[] buf, int pos, int len)
-
toHex
public java.lang.String toHex()
-
dumpHex
public void dumpHex(SimplifiedLog logger, java.lang.String prefix, PropertyResolver resolver)
-
dumpHex
public void dumpHex(SimplifiedLog logger, java.util.logging.Level level, java.lang.String prefix, PropertyResolver resolver)
-
isSensitiveData
private static boolean isSensitiveData(int cmd)
-
getUByte
public int getUByte()
-
getByte
public byte getByte()
-
getShort
public short getShort()
-
getUShort
public int getUShort()
-
getInt
public int getInt()
-
getUInt
public long getUInt()
-
getLong
public long getLong()
-
getBoolean
public boolean getBoolean()
-
getString
public java.lang.String getString()
- Returns:
- Reads a UTF-8 encoded string
-
getNameList
public java.util.List<java.lang.String> getNameList()
According to RFC 4251: A name-list is represented as a uint32 containing its length (number of bytes that follow) followed by a comma-separated list of zero or more names.- Returns:
- The parsed result
-
getNameList
public java.util.List<java.lang.String> getNameList(java.nio.charset.Charset charset)
-
getNameList
public java.util.List<java.lang.String> getNameList(char separator)
-
getNameList
public java.util.List<java.lang.String> getNameList(java.nio.charset.Charset charset, char separator)
Parses a string that contains values separated by a delimiter- Parameters:
charset
- TheCharset
to use to read the stringseparator
- The separator- Returns:
- A
List
of the parsed values
-
getCertificateOptions
public java.util.List<OpenSshCertificate.CertificateOption> getCertificateOptions()
-
getCertificateOptions
public java.util.List<OpenSshCertificate.CertificateOption> getCertificateOptions(java.nio.charset.Charset charset)
According to PROTOCOL.certkeys: Critical Options is a set of bytes that is [overall length][name(string)][[length of buffer][[length of string][data(string)]]]... Where each Certificate Option is encoded as a name (string) and buffer of data (string packed in a buffer) Then the entire name (string) + data (buffer) are added as bytes (which will get a length prefix)- Parameters:
charset
-Charset
to use for converting bytes to characters- Returns:
- the parsed result, never
null
, but possibly empty
-
getStringList
public java.util.Collection<java.lang.String> getStringList(boolean usePrependedLength)
- Parameters:
usePrependedLength
- Iftrue
then there is a 32-bit value indicating the number of strings to read. Otherwise, the method will use a "greedy" reading of strings while more data available.- Returns:
- A
Collection
of the read strings - See Also:
getStringList(boolean, Charset)
-
getStringList
public java.util.Collection<java.lang.String> getStringList(boolean usePrependedLength, java.nio.charset.Charset charset)
- Parameters:
usePrependedLength
- Iftrue
then there is a 32-bit value indicating the number of strings to read. Otherwise, the method will use a "greedy" reading of strings while more data available.charset
- TheCharset
to use for the strings- Returns:
- A
Collection
of the read strings - See Also:
getStringList(int, Charset)
,getAvailableStrings()
-
getAvailableStrings
public java.util.Collection<java.lang.String> getAvailableStrings()
- Returns:
- The remaining data as a list of strings
- See Also:
getAvailableStrings(Charset)
-
getAvailableStrings
public java.util.Collection<java.lang.String> getAvailableStrings(java.nio.charset.Charset charset)
- Parameters:
charset
- TheCharset
to use for the strings- Returns:
- The remaining data as a list of strings
- See Also:
Readable.available()
,getString(Charset)
-
getStringList
public java.util.List<java.lang.String> getStringList(int count)
- Parameters:
count
- The exact number of strings to read - can be zero- Returns:
- A
List
with the specified number of strings - See Also:
getStringList(int, Charset)
-
getStringList
public java.util.List<java.lang.String> getStringList(int count, java.nio.charset.Charset charset)
- Parameters:
count
- The exact number of strings to read - can be zerocharset
- TheCharset
of the strings- Returns:
- A
List
with the specified number of strings - See Also:
getString(Charset)
-
getString
public abstract java.lang.String getString(java.nio.charset.Charset charset)
Reads a string using a given charset.- Parameters:
charset
- TheCharset
to use for the string bytes- Returns:
- The read string
-
getMPInt
public java.math.BigInteger getMPInt()
-
getMPIntAsBytes
public byte[] getMPIntAsBytes()
-
getBytes
public byte[] getBytes()
-
getRawBytes
public void getRawBytes(byte[] buf)
-
getPublicKey
public java.security.PublicKey getPublicKey() throws SshException
- Throws:
SshException
-
getPublicKey
public java.security.PublicKey getPublicKey(BufferPublicKeyParser<? extends java.security.PublicKey> parser) throws SshException
- Parameters:
parser
- ABufferPublicKeyParser
to extract the key from the buffer - nevernull
- Returns:
- The extracted
PublicKey
- may benull
if the parser so decided - Throws:
SshException
- If failed to extract the key- See Also:
getRawPublicKey(BufferPublicKeyParser)
-
getRawPublicKey
public java.security.PublicKey getRawPublicKey() throws SshException
- Throws:
SshException
-
getRawPublicKey
public java.security.PublicKey getRawPublicKey(BufferPublicKeyParser<? extends java.security.PublicKey> parser) throws SshException
- Parameters:
parser
- ABufferPublicKeyParser
to extract the key from the buffer - nevernull
- Returns:
- The extracted
PublicKey
- may benull
if the parser so decided - Throws:
SshException
- If failed to extract the key
-
getKeyPair
public java.security.KeyPair getKeyPair() throws SshException
- Throws:
SshException
-
extractEC
protected java.security.KeyPair extractEC(java.lang.String expectedCurveName, java.security.spec.ECParameterSpec spec) throws java.security.GeneralSecurityException
- Throws:
java.security.GeneralSecurityException
-
ensureAvailable
public int ensureAvailable(int reqLen) throws BufferException
Makes sure the buffer contains enough data to accommodate the requested length- Parameters:
reqLen
- Requested data in bytes- Returns:
- Same as input if validation successful
- Throws:
BufferException
- If negative length or beyond available requested
-
putByte
public void putByte(byte b)
-
putOptionalBufferedData
public void putOptionalBufferedData(java.lang.Object buffer)
Checks if the buffer argument is an array of bytes, aReadable
instance or aByteBuffer
and invokes the appropriateputXXX
method. Ifnull
then puts an empty byte array value- Parameters:
buffer
- The buffered data object to inspect- See Also:
putBufferedData(Object)
-
putBufferedData
public void putBufferedData(java.lang.Object buffer)
Checks if the buffer argument is an array of bytes, aReadable
instance or aByteBuffer
and invokes the appropriateputXXX
method.- Parameters:
buffer
- The (nevernull
) buffer object to put- Throws:
java.lang.IllegalArgumentException
- If buffer is none of the supported types
-
putBuffer
public void putBuffer(Readable buffer)
-
putBuffer
public abstract int putBuffer(Readable buffer, boolean expand)
-
putBuffer
public abstract void putBuffer(java.nio.ByteBuffer buffer)
-
putShort
public void putShort(int i)
Writes 16 bits- Parameters:
i
- The 16-bit value
-
putInt
public void putInt(long i)
-
putUInt
public void putUInt(long i)
Writes 32 bits- Parameters:
i
- The 32-bit value
-
putLong
public void putLong(long i)
Writes 64 bits- Parameters:
i
- The 64-bit value
-
putBoolean
public void putBoolean(boolean b)
-
putAndWipeBytes
public void putAndWipeBytes(byte[] b)
Adds the bytes to the buffer and wipes the data from the input buffer after having added it - useful for sensitive information such as password- Parameters:
b
- The buffer to add - OK ifnull
-
putAndWipeBytes
public void putAndWipeBytes(byte[] b, int off, int len)
-
putBytes
public void putBytes(byte[] b)
-
putBytes
public void putBytes(byte[] b, int off, int len)
-
putStringList
public void putStringList(java.util.Collection<?> objects, boolean prependLength)
Encodes thetoString
value of each member.- Parameters:
objects
- The objects to be encoded in the buffer - OK ifnull
/emptyprependLength
- Iftrue
then the list is preceded by a 32-bit count of the number of members in the list- See Also:
putStringList(Collection, Charset, boolean)
-
putStringList
public void putStringList(java.util.Collection<?> objects, java.nio.charset.Charset charset, boolean prependLength)
Encodes thetoString
value of each member- Parameters:
objects
- The objects to be encoded in the buffer - OK ifnull
/emptycharset
- TheCharset
to use for encodingprependLength
- Iftrue
then the list is preceded by a 32-bit count of the number of members in the list- See Also:
putString(String, Charset)
-
putCertificateOptions
public void putCertificateOptions(java.util.List<OpenSshCertificate.CertificateOption> options)
-
putCertificateOptions
public void putCertificateOptions(java.util.List<OpenSshCertificate.CertificateOption> options, java.nio.charset.Charset charset)
According to PROTOCOL.certkeys: Certificate Options is a set of bytes that is [overall length][name(string)][[length of buffer][[length of string][data(string)]]]... Where each Certificate Option is encoded as a name (string) and data (string packed in a buffer) Then the entire name (string) + data (buffer) are added as bytes (which will get a length prefix)- Parameters:
options
- to write into the buffer, may benull
or empty but must not containnull
elementscharset
- TheCharset
to use for string options
-
putNameList
public void putNameList(java.util.Collection<java.lang.String> names)
According to RFC 4251:A name-list is represented as a uint32 containing its length (number of bytes that follow) followed by a comma-separated list of zero or more names.
- Parameters:
names
- The name list to put
-
putNameList
public void putNameList(java.util.Collection<java.lang.String> names, java.nio.charset.Charset charset)
-
putNameList
public void putNameList(java.util.Collection<java.lang.String> names, char separator)
-
putNameList
public void putNameList(java.util.Collection<java.lang.String> names, java.nio.charset.Charset charset, char separator)
Adds a string that contains values separated by a delimiter- Parameters:
names
- The names to setcharset
- TheCharset
to use to encode the stringseparator
- The separator
-
putString
public void putString(java.lang.String string)
-
putString
public void putString(java.lang.String string, java.nio.charset.Charset charset)
-
putAndWipeChars
public void putAndWipeChars(char[] chars)
Zeroes the input array after having put the characters in the buffer - useful for sensitive information such as passwords- Parameters:
chars
- The characters to put in the buffer - may benull
/empty- See Also:
putAndWipeChars(char[], Charset)
,putChars(char[], Charset)
-
putAndWipeChars
public void putAndWipeChars(char[] chars, int offset, int len)
-
putAndWipeChars
public void putAndWipeChars(char[] chars, java.nio.charset.Charset charset)
-
putAndWipeChars
public void putAndWipeChars(char[] chars, int offset, int len, java.nio.charset.Charset charset)
-
putChars
public void putChars(char[] chars)
-
putChars
public void putChars(char[] chars, int offset, int len)
-
putChars
public void putChars(char[] chars, java.nio.charset.Charset charset)
-
putChars
public void putChars(char[] chars, int offset, int len, java.nio.charset.Charset charset)
-
putMPInt
public void putMPInt(java.math.BigInteger bigint)
-
putMPInt
public void putMPInt(byte[] mpInt)
-
putRawBytes
public void putRawBytes(byte[] d)
-
putRawBytes
public abstract void putRawBytes(byte[] d, int off, int len)
-
putPublicKey
public void putPublicKey(java.security.PublicKey key)
-
putRawPublicKey
public void putRawPublicKey(java.security.PublicKey key)
-
putRawPublicKeyBytes
public void putRawPublicKeyBytes(java.security.PublicKey key)
-
putKeyPair
public void putKeyPair(java.security.KeyPair kp)
-
ensureCapacity
public Buffer ensureCapacity(int capacity)
-
ensureCapacity
public abstract Buffer ensureCapacity(int capacity, java.util.function.IntUnaryOperator growthFactor)
- Parameters:
capacity
- The required capacitygrowthFactor
- AnIntUnaryOperator
that is invoked if the current capacity is insufficient. The argument is the minimum required new data length, the function result should be the effective new data length to be allocated - if less than minimum then an exception is thrown- Returns:
- This buffer instance
-
size
protected abstract int size()
- Returns:
- Current size of underlying backing data bytes array
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-