Class PublicKeyEntry
- java.lang.Object
-
- org.apache.sshd.common.config.keys.PublicKeyEntry
-
- All Implemented Interfaces:
Serializable
,KeyTypeIndicator
- Direct Known Subclasses:
AuthorizedKeyEntry
public class PublicKeyEntry extends Object implements Serializable, KeyTypeIndicator
Represents a
PublicKey
whose data is formatted according to the OpenSSH format:<key-type> <base64-encoded-public-key-data>
- Author:
- Apache MINA SSHD Project
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static char
COMMENT_CHAR
Character used to denote a comment line in the keys filestatic String
STD_KEYFILE_FOLDER_NAME
Standard folder name used by OpenSSH to hold key files
-
Constructor Summary
Constructors Constructor Description PublicKeyEntry()
PublicKeyEntry(String keyType, byte... keyData)
-
Method Summary
-
-
-
Field Detail
-
COMMENT_CHAR
public static final char COMMENT_CHAR
Character used to denote a comment line in the keys file- See Also:
- Constant Field Values
-
STD_KEYFILE_FOLDER_NAME
public static final String STD_KEYFILE_FOLDER_NAME
Standard folder name used by OpenSSH to hold key files- See Also:
- Constant Field Values
-
-
Constructor Detail
-
PublicKeyEntry
public PublicKeyEntry()
-
PublicKeyEntry
public PublicKeyEntry(String keyType, byte... keyData)
-
-
Method Detail
-
getKeyType
public String getKeyType()
- Specified by:
getKeyType
in interfaceKeyTypeIndicator
- Returns:
- The SSH key type name - e.g., "ssh-rsa", "sshd-dss" etc.
-
setKeyType
public void setKeyType(String value)
-
getKeyData
public byte[] getKeyData()
-
setKeyData
public void setKeyData(byte[] value)
-
getKeyDataResolver
public PublicKeyEntryDataResolver getKeyDataResolver()
-
setKeyDataResolver
public void setKeyDataResolver(PublicKeyEntryDataResolver keyDataResolver)
-
resolvePublicKeyEntryDataResolver
public PublicKeyEntryDataResolver resolvePublicKeyEntryDataResolver()
If aPublicKeyEntryDataResolver
has been set, then uses it - otherwise uses thedefault one
.- Returns:
- The resolved instance
-
resolvePublicKey
public PublicKey resolvePublicKey(SessionContext session, Map<String,String> headers, PublicKeyEntryResolver fallbackResolver) throws IOException, GeneralSecurityException
- Parameters:
session
- TheSessionContext
for invoking this load command - may benull
if not invoked within a session context (e.g., offline tool or session unknown).headers
- Any headers that may have been available when data was readfallbackResolver
- ThePublicKeyEntryResolver
to consult if none of the built-in ones can be used. Ifnull
and no built-in resolver can be used then anInvalidKeySpecException
is thrown.- Returns:
- The resolved
PublicKey
- ornull
if could not be resolved. Note: may be called only after key type and data bytes have been set or exception(s) may be thrown - Throws:
IOException
- If failed to decode the keyGeneralSecurityException
- If failed to generate the key
-
appendPublicKey
public PublicKey appendPublicKey(SessionContext session, Appendable sb, PublicKeyEntryResolver fallbackResolver) throws IOException, GeneralSecurityException
- Parameters:
session
- TheSessionContext
for invoking this command - may benull
if not invoked within a session context (e.g., offline tool or session unknown).sb
- TheAppendable
instance to encode the data intofallbackResolver
- ThePublicKeyEntryResolver
to consult if none of the built-in ones can be used. Ifnull
and no built-in resolver can be used then anInvalidKeySpecException
is thrown.- Returns:
- The
PublicKey
ornull
if could not resolve it - Throws:
IOException
- If failed to decode/encode the keyGeneralSecurityException
- If failed to generate the key- See Also:
resolvePublicKey(SessionContext, Map, PublicKeyEntryResolver)
-
isEquivalent
protected boolean isEquivalent(PublicKeyEntry e)
-
resolvePublicKeyEntries
public static List<PublicKey> resolvePublicKeyEntries(SessionContext session, Collection<? extends PublicKeyEntry> entries, PublicKeyEntryResolver fallbackResolver) throws IOException, GeneralSecurityException
- Parameters:
session
- TheSessionContext
for invoking this command - may benull
if not invoked within a session context (e.g., offline tool or session unknown).entries
- The entries to convert - ignored ifnull
/emptyfallbackResolver
- ThePublicKeyEntryResolver
to consult if none of the built-in ones can be used. Ifnull
and no built-in resolver can be used then anInvalidKeySpecException
is thrown.- Returns:
- The
List
of allPublicKey
-s that have been resolved - Throws:
IOException
- If failed to decode the key dataGeneralSecurityException
- If failed to generate thePublicKey
from the decoded data- See Also:
resolvePublicKey(SessionContext, Map, PublicKeyEntryResolver)
-
registerKeyDataEntryResolver
public static void registerKeyDataEntryResolver(String keyType, PublicKeyEntryDataResolver resolver)
Registers a specialized decoder for the public key entry data bytes instead of thedefault
one.- Parameters:
keyType
- The key-type value (case insensitive) that will trigger the usage of this decoder - e.g., "ssh-rsa", "pgp-sign-dss", etc.resolver
- The decoder to use
-
getKeyDataEntryResolver
public static PublicKeyEntryDataResolver getKeyDataEntryResolver(String keyType)
- Parameters:
keyType
- The key-type value (case insensitive) that may have been previouslyregistered
- e.g., "ssh-rsa", "pgp-sign-dss", etc.- Returns:
- The registered resolver instance -
null
if none was registered
-
unregisterKeyDataEntryResolver
public static PublicKeyEntryDataResolver unregisterKeyDataEntryResolver(String keyType)
- Parameters:
keyType
- The key-type value (case insensitive) that may have been previouslyregistered
- e.g., "ssh-rsa", "pgp-sign-dss", etc.- Returns:
- The un-registered resolver instance -
null
if none was registered
-
resolveKeyDataEntryResolver
public static PublicKeyEntryDataResolver resolveKeyDataEntryResolver(String keyType)
- Parameters:
keyType
- keyType The key-type value (case insensitive) whose data is to be resolved - e.g., "ssh-rsa", "pgp-sign-dss", etc.- Returns:
- If a specific resolver has been previously
registered
then uses it, otherwise thedefault
one.
-
getRegisteredKeyDataEntryResolvers
public static NavigableMap<String,PublicKeyEntryDataResolver> getRegisteredKeyDataEntryResolvers()
- Returns:
- A snapshot of the currently registered specialized
PublicKeyEntryDataResolver
-s, where key=the key-type value (case insensitive) - e.g., "ssh-rsa", "pgp-sign-dss", etc., value=the associatedPublicKeyEntryDataResolver
for the key type
-
parsePublicKeyEntry
public static PublicKeyEntry parsePublicKeyEntry(String encData) throws IllegalArgumentException
- Parameters:
encData
- Assumed to contain at leastkey-type base64-data
(anything beyond the BASE64 data is ignored) - ignored ifnull
/empty- Returns:
- A
PublicKeyEntry
ornull
if no data - Throws:
IllegalArgumentException
- if bad format found- See Also:
parsePublicKeyEntry(String, PublicKeyEntryDataResolver)
-
parsePublicKeyEntry
public static PublicKeyEntry parsePublicKeyEntry(String encData, PublicKeyEntryDataResolver decoder) throws IllegalArgumentException
- Parameters:
encData
- Assumed to contain at leastkey-type base64-data
(anything beyond the BASE64 data is ignored) - ignored ifnull
/emptydecoder
- ThePublicKeyEntryDataResolver
to use in order to decode the key data string into its bytes - ifnull
then one is automaticallyresolved
- Returns:
- A
PublicKeyEntry
ornull
if no data - Throws:
IllegalArgumentException
- if bad format found- See Also:
parsePublicKeyEntry(PublicKeyEntry, String, PublicKeyEntryDataResolver)
-
parsePublicKeyEntry
public static <E extends PublicKeyEntry> E parsePublicKeyEntry(E entry, String encData) throws IllegalArgumentException
- Type Parameters:
E
- The generic entry type- Parameters:
entry
- ThePublicKeyEntry
whose contents are to be updated - ignored ifnull
encData
- Assumed to contain at leastkey-type base64-data
(anything beyond the BASE64 data is ignored) - ignored ifnull
/empty- Returns:
- The updated entry instance
- Throws:
IllegalArgumentException
- if bad format found- See Also:
parsePublicKeyEntry(PublicKeyEntry, String, PublicKeyEntryDataResolver)
-
parsePublicKeyEntry
public static <E extends PublicKeyEntry> E parsePublicKeyEntry(E entry, String encData, PublicKeyEntryDataResolver decoder) throws IllegalArgumentException
- Type Parameters:
E
- The generic entry type- Parameters:
entry
- ThePublicKeyEntry
whose contents are to be updated - ignored ifnull
encData
- Assumed to contain at leastkey-type base64-data
(anything beyond the BASE64 data is ignored) - ignored ifnull
/emptydecoder
- ThePublicKeyEntryDataResolver
to use in order to decode the key data string into its bytes - ifnull
then one is automaticallyresolved
- Returns:
- The updated entry instance
- Throws:
IllegalArgumentException
- if bad format found
-
toString
public static String toString(PublicKey key) throws IllegalArgumentException
- Parameters:
key
- ThePublicKey
- Returns:
- The
OpenSSH
encoded data - Throws:
IllegalArgumentException
- If failed to encode- See Also:
toString(PublicKey, PublicKeyEntryDataResolver)
-
toString
public static String toString(PublicKey key, PublicKeyEntryDataResolver encoder) throws IllegalArgumentException
- Parameters:
key
- ThePublicKey
encoder
- ThePublicKeyEntryDataResolver
to use in order to encode the key data bytes into a string representation - ifnull
then one is automaticallyresolved
- Returns:
- The
OpenSSH
encoded data - Throws:
IllegalArgumentException
- If failed to encode- See Also:
appendPublicKeyEntry(Appendable, PublicKey, PublicKeyEntryDataResolver)
-
appendPublicKeyEntry
public static <A extends Appendable> A appendPublicKeyEntry(A sb, PublicKey key) throws IOException
Encodes a public key data the same way as theparsePublicKeyEntry(String)
expects it- Type Parameters:
A
- The generic appendable class- Parameters:
sb
- TheAppendable
instance to encode the data intokey
- ThePublicKey
- ignored ifnull
- Returns:
- The updated appendable instance
- Throws:
IOException
- If failed to append the data- See Also:
appendPublicKeyEntry(Appendable, PublicKey, PublicKeyEntryDataResolver)
-
appendPublicKeyEntry
public static <A extends Appendable> A appendPublicKeyEntry(A sb, PublicKey key, PublicKeyEntryDataResolver encoder) throws IOException
- Type Parameters:
A
- The generic appendable class- Parameters:
sb
- TheAppendable
instance to encode the data intokey
- ThePublicKey
- ignored ifnull
encoder
- ThePublicKeyEntryDataResolver
to use in order to encode the key data bytes into a string representation - ifnull
then one is automaticallyresolved
- Returns:
- The updated appendable instance
- Throws:
IOException
- If failed to append the data
-
getDefaultKeysFolderPath
public static Path getDefaultKeysFolderPath()
- Returns:
- The default OpenSSH folder used to hold key files - e.g.,
known_hosts
,authorized_keys
, etc.
-
-