Class PublicKeyEntry
- java.lang.Object
-
- org.apache.sshd.common.config.keys.PublicKeyEntry
-
- All Implemented Interfaces:
java.io.Serializable,KeyTypeIndicator
- Direct Known Subclasses:
AuthorizedKeyEntry
public class PublicKeyEntry extends java.lang.Object implements java.io.Serializable, KeyTypeIndicator
Represents a
PublicKeywhose data is formatted according to the OpenSSH format:<key-type> <base64-encoded-public-key-data>
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPublicKeyEntry.LazyDefaultKeysFolderHolder
-
Field Summary
Fields Modifier and Type Field Description static charCOMMENT_CHARCharacter used to denote a comment line in the keys fileprivate static java.util.NavigableMap<java.lang.String,PublicKeyEntryDataResolver>KEY_DATA_RESOLVERSprivate byte[]keyDataprivate PublicKeyEntryDataResolverkeyDataResolverprivate java.lang.StringkeyTypestatic java.lang.StringPUBKEY_FILE_SUFFIXStandard suffix for SSH public key filesprivate static longserialVersionUIDstatic java.lang.StringSTD_KEYFILE_FOLDER_NAMEStandard folder name used by OpenSSH to hold key files
-
Constructor Summary
Constructors Constructor Description PublicKeyEntry()PublicKeyEntry(java.lang.String keyType, byte... keyData)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.security.PublicKeyappendPublicKey(SessionContext session, java.lang.Appendable sb, PublicKeyEntryResolver fallbackResolver)static <A extends java.lang.Appendable>
AappendPublicKeyEntry(A sb, java.security.PublicKey key)Encodes a public key data the same way as theparsePublicKeyEntry(String)expects itstatic <A extends java.lang.Appendable>
AappendPublicKeyEntry(A sb, java.security.PublicKey key, PublicKeyEntryDataResolver encoder)booleanequals(java.lang.Object obj)static java.nio.file.PathgetDefaultKeysFolderPath()byte[]getKeyData()static PublicKeyEntryDataResolvergetKeyDataEntryResolver(java.lang.String keyType)PublicKeyEntryDataResolvergetKeyDataResolver()java.lang.StringgetKeyType()static java.util.NavigableMap<java.lang.String,PublicKeyEntryDataResolver>getRegisteredKeyDataEntryResolvers()inthashCode()protected booleanisEquivalent(PublicKeyEntry e)static <E extends PublicKeyEntry>
EparsePublicKeyEntry(E entry, java.lang.String encData)static <E extends PublicKeyEntry>
EparsePublicKeyEntry(E entry, java.lang.String encData, PublicKeyEntryDataResolver decoder)static PublicKeyEntryparsePublicKeyEntry(java.lang.String encData)static PublicKeyEntryparsePublicKeyEntry(java.lang.String encData, PublicKeyEntryDataResolver decoder)static voidregisterKeyDataEntryResolver(java.lang.String keyType, PublicKeyEntryDataResolver resolver)Registers a specialized decoder for the public key entry data bytes instead of thedefaultone.static PublicKeyEntryDataResolverresolveKeyDataEntryResolver(java.lang.String keyType)java.security.PublicKeyresolvePublicKey(SessionContext session, java.util.Map<java.lang.String,java.lang.String> headers, PublicKeyEntryResolver fallbackResolver)static java.util.List<java.security.PublicKey>resolvePublicKeyEntries(SessionContext session, java.util.Collection<? extends PublicKeyEntry> entries, PublicKeyEntryResolver fallbackResolver)PublicKeyEntryDataResolverresolvePublicKeyEntryDataResolver()If aPublicKeyEntryDataResolverhas been set, then uses it - otherwise uses thedefault one.voidsetKeyData(byte[] value)voidsetKeyDataResolver(PublicKeyEntryDataResolver keyDataResolver)voidsetKeyType(java.lang.String value)java.lang.StringtoString()static java.lang.StringtoString(java.security.PublicKey key)static java.lang.StringtoString(java.security.PublicKey key, PublicKeyEntryDataResolver encoder)static PublicKeyEntryDataResolverunregisterKeyDataEntryResolver(java.lang.String keyType)
-
-
-
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 java.lang.String STD_KEYFILE_FOLDER_NAME
Standard folder name used by OpenSSH to hold key files- See Also:
- Constant Field Values
-
PUBKEY_FILE_SUFFIX
public static final java.lang.String PUBKEY_FILE_SUFFIX
Standard suffix for SSH public key files- See Also:
- Constant Field Values
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
KEY_DATA_RESOLVERS
private static final java.util.NavigableMap<java.lang.String,PublicKeyEntryDataResolver> KEY_DATA_RESOLVERS
-
keyType
private java.lang.String keyType
-
keyData
private byte[] keyData
-
keyDataResolver
private PublicKeyEntryDataResolver keyDataResolver
-
-
Method Detail
-
getKeyType
public java.lang.String getKeyType()
- Specified by:
getKeyTypein interfaceKeyTypeIndicator- Returns:
- The SSH key type name - e.g., "ssh-rsa", "sshd-dss" etc.
-
setKeyType
public void setKeyType(java.lang.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 aPublicKeyEntryDataResolverhas been set, then uses it - otherwise uses thedefault one.- Returns:
- The resolved instance
-
resolvePublicKey
public java.security.PublicKey resolvePublicKey(SessionContext session, java.util.Map<java.lang.String,java.lang.String> headers, PublicKeyEntryResolver fallbackResolver) throws java.io.IOException, java.security.GeneralSecurityException
- Parameters:
session- TheSessionContextfor invoking this load command - may benullif 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- ThePublicKeyEntryResolverto consult if none of the built-in ones can be used. Ifnulland no built-in resolver can be used then anInvalidKeySpecExceptionis thrown.- Returns:
- The resolved
PublicKey- ornullif 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:
java.io.IOException- If failed to decode the keyjava.security.GeneralSecurityException- If failed to generate the key
-
appendPublicKey
public java.security.PublicKey appendPublicKey(SessionContext session, java.lang.Appendable sb, PublicKeyEntryResolver fallbackResolver) throws java.io.IOException, java.security.GeneralSecurityException
- Parameters:
session- TheSessionContextfor invoking this command - may benullif not invoked within a session context (e.g., offline tool or session unknown).sb- TheAppendableinstance to encode the data intofallbackResolver- ThePublicKeyEntryResolverto consult if none of the built-in ones can be used. Ifnulland no built-in resolver can be used then anInvalidKeySpecExceptionis thrown.- Returns:
- The
PublicKeyornullif could not resolve it - Throws:
java.io.IOException- If failed to decode/encode the keyjava.security.GeneralSecurityException- If failed to generate the key- See Also:
resolvePublicKey(SessionContext, Map, PublicKeyEntryResolver)
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
isEquivalent
protected boolean isEquivalent(PublicKeyEntry e)
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
resolvePublicKeyEntries
public static java.util.List<java.security.PublicKey> resolvePublicKeyEntries(SessionContext session, java.util.Collection<? extends PublicKeyEntry> entries, PublicKeyEntryResolver fallbackResolver) throws java.io.IOException, java.security.GeneralSecurityException
- Parameters:
session- TheSessionContextfor invoking this command - may benullif not invoked within a session context (e.g., offline tool or session unknown).entries- The entries to convert - ignored ifnull/emptyfallbackResolver- ThePublicKeyEntryResolverto consult if none of the built-in ones can be used. Ifnulland no built-in resolver can be used then anInvalidKeySpecExceptionis thrown.- Returns:
- The
Listof allPublicKey-s that have been resolved - Throws:
java.io.IOException- If failed to decode the key datajava.security.GeneralSecurityException- If failed to generate thePublicKeyfrom the decoded data- See Also:
resolvePublicKey(SessionContext, Map, PublicKeyEntryResolver)
-
registerKeyDataEntryResolver
public static void registerKeyDataEntryResolver(java.lang.String keyType, PublicKeyEntryDataResolver resolver)Registers a specialized decoder for the public key entry data bytes instead of thedefaultone.- 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(java.lang.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 -
nullif none was registered
-
unregisterKeyDataEntryResolver
public static PublicKeyEntryDataResolver unregisterKeyDataEntryResolver(java.lang.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 -
nullif none was registered
-
resolveKeyDataEntryResolver
public static PublicKeyEntryDataResolver resolveKeyDataEntryResolver(java.lang.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
registeredthen uses it, otherwise thedefaultone.
-
getRegisteredKeyDataEntryResolvers
public static java.util.NavigableMap<java.lang.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 associatedPublicKeyEntryDataResolverfor the key type
-
parsePublicKeyEntry
public static PublicKeyEntry parsePublicKeyEntry(java.lang.String encData) throws java.lang.IllegalArgumentException
- Parameters:
encData- Assumed to contain at leastkey-type base64-data(anything beyond the BASE64 data is ignored) - ignored ifnull/empty- Returns:
- A
PublicKeyEntryornullif no data - Throws:
java.lang.IllegalArgumentException- if bad format found- See Also:
parsePublicKeyEntry(String, PublicKeyEntryDataResolver)
-
parsePublicKeyEntry
public static PublicKeyEntry parsePublicKeyEntry(java.lang.String encData, PublicKeyEntryDataResolver decoder) throws java.lang.IllegalArgumentException
- Parameters:
encData- Assumed to contain at leastkey-type base64-data(anything beyond the BASE64 data is ignored) - ignored ifnull/emptydecoder- ThePublicKeyEntryDataResolverto use in order to decode the key data string into its bytes - ifnullthen one is automaticallyresolved- Returns:
- A
PublicKeyEntryornullif no data - Throws:
java.lang.IllegalArgumentException- if bad format found- See Also:
parsePublicKeyEntry(PublicKeyEntry, String, PublicKeyEntryDataResolver)
-
parsePublicKeyEntry
public static <E extends PublicKeyEntry> E parsePublicKeyEntry(E entry, java.lang.String encData) throws java.lang.IllegalArgumentException
- Type Parameters:
E- The generic entry type- Parameters:
entry- ThePublicKeyEntrywhose contents are to be updated - ignored ifnullencData- Assumed to contain at leastkey-type base64-data(anything beyond the BASE64 data is ignored) - ignored ifnull/empty- Returns:
- The updated entry instance
- Throws:
java.lang.IllegalArgumentException- if bad format found- See Also:
parsePublicKeyEntry(PublicKeyEntry, String, PublicKeyEntryDataResolver)
-
parsePublicKeyEntry
public static <E extends PublicKeyEntry> E parsePublicKeyEntry(E entry, java.lang.String encData, PublicKeyEntryDataResolver decoder) throws java.lang.IllegalArgumentException
- Type Parameters:
E- The generic entry type- Parameters:
entry- ThePublicKeyEntrywhose contents are to be updated - ignored ifnullencData- Assumed to contain at leastkey-type base64-data(anything beyond the BASE64 data is ignored) - ignored ifnull/emptydecoder- ThePublicKeyEntryDataResolverto use in order to decode the key data string into its bytes - ifnullthen one is automaticallyresolved- Returns:
- The updated entry instance
- Throws:
java.lang.IllegalArgumentException- if bad format found
-
toString
public static java.lang.String toString(java.security.PublicKey key) throws java.lang.IllegalArgumentException- Parameters:
key- ThePublicKey- Returns:
- The
OpenSSHencoded data - Throws:
java.lang.IllegalArgumentException- If failed to encode- See Also:
toString(PublicKey, PublicKeyEntryDataResolver)
-
toString
public static java.lang.String toString(java.security.PublicKey key, PublicKeyEntryDataResolver encoder) throws java.lang.IllegalArgumentException- Parameters:
key- ThePublicKeyencoder- ThePublicKeyEntryDataResolverto use in order to encode the key data bytes into a string representation - ifnullthen one is automaticallyresolved- Returns:
- The
OpenSSHencoded data - Throws:
java.lang.IllegalArgumentException- If failed to encode- See Also:
appendPublicKeyEntry(Appendable, PublicKey, PublicKeyEntryDataResolver)
-
appendPublicKeyEntry
public static <A extends java.lang.Appendable> A appendPublicKeyEntry(A sb, java.security.PublicKey key) throws java.io.IOExceptionEncodes a public key data the same way as theparsePublicKeyEntry(String)expects it- Type Parameters:
A- The generic appendable class- Parameters:
sb- TheAppendableinstance to encode the data intokey- ThePublicKey- ignored ifnull- Returns:
- The updated appendable instance
- Throws:
java.io.IOException- If failed to append the data- See Also:
appendPublicKeyEntry(Appendable, PublicKey, PublicKeyEntryDataResolver)
-
appendPublicKeyEntry
public static <A extends java.lang.Appendable> A appendPublicKeyEntry(A sb, java.security.PublicKey key, PublicKeyEntryDataResolver encoder) throws java.io.IOException- Type Parameters:
A- The generic appendable class- Parameters:
sb- TheAppendableinstance to encode the data intokey- ThePublicKey- ignored ifnullencoder- ThePublicKeyEntryDataResolverto use in order to encode the key data bytes into a string representation - ifnullthen one is automaticallyresolved- Returns:
- The updated appendable instance
- Throws:
java.io.IOException- If failed to append the data
-
getDefaultKeysFolderPath
public static java.nio.file.Path getDefaultKeysFolderPath()
- Returns:
- The default OpenSSH folder used to hold key files - e.g.,
known_hosts,authorized_keys, etc.
-
-