Top Description Inners Fields Constructors Methods
org.apache.sshd.common.config.keys.writer.openssh

public Class OpenSSHKeyPairResourceWriter

extends Object
implements KeyPairResourceWriter<OpenSSHKeyEncryptionContext>
Class Inheritance
All Implemented Interfaces
org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter
Imports
java.io.ByteArrayOutputStream, .IOException, .OutputStream, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.StandardCharsets, java.security.GeneralSecurityException, .KeyPair, .NoSuchAlgorithmException, .PrivateKey, .PublicKey, java.util.Arrays, .Base64, .Objects, java.util.regex.Matcher, .Pattern, org.apache.sshd.common.cipher.BuiltinCiphers, .CipherInformation, org.apache.sshd.common.config.keys.KeyEntryResolver, .KeyUtils, .PrivateKeyEntryDecoder, .PublicKeyEntry, .PublicKeyEntryDecoder, org.apache.sshd.common.config.keys.loader.AESPrivateKeyObfuscator, .PrivateKeyEncryptionContext, org.apache.sshd.common.config.keys.loader.openssh.OpenSSHKeyPairResourceParser, .OpenSSHParserContext, org.apache.sshd.common.config.keys.loader.openssh.kdf.BCrypt, .BCryptKdfOptions, org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter, org.apache.sshd.common.random.JceRandom, .JceRandomFactory, .Random, org.apache.sshd.common.util.GenericUtils, org.apache.sshd.common.util.io.output.SecureByteArrayOutputStream, org.apache.sshd.common.util.security.SecurityUtils

A KeyPairResourceWriter for writing keys in the modern OpenSSH format, using the OpenBSD bcrypt KDF for passphrase-protected encrypted private keys.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
OpenSSHKeyPairResourceWriter.KeyEncryptor

A key encryptor for modern-style OpenSSH private keys using the bcrypt KDF.

Field Summary

Modifier and TypeField and Description
public static final String
public static final OpenSSHKeyPairResourceWriter
public static final int
private static final Pattern

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static OpenSSHKeyEncryptionContext
public static byte[]
encodePrivateKey(KeyPair key, String keyType, int blockSize, String comment)

public static byte[]
public static String
public static void
write(OutputStream out, byte[] bytes, int lineLength)

public static void
public void
writePrivateKey(KeyPair
to write the private key of
key
,
String
to write with the private key
comment
,
OpenSSHKeyEncryptionContext
for writing the key; may be null if no encryption is wanted. The caller is responsible for clearing the options when no longer needed. If the passphrase obtained from the context is null or an empty/blank string (length zero or containing only whitespace), the key is written unencrypted.
options
,
OutputStream
The OutputStream to write to - recommend using a SecureByteArrayOutputStream in order to reduce sensitive data exposure in memory
out
)

Implements org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter.writePrivateKey.

Writes a serialization of a private key from a given KeyPair to a given OutputStream.

public void
writePublicKey(PublicKey
to write
key
,
String
to write with the key
comment
,
OutputStream
The OutputStream to write to - recommend using a SecureByteArrayOutputStream in order to reduce sensitive data exposure in memory
out
)

Implements org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter.writePublicKey.

Writes a serialization of a PublicKey to a given OutputStream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DASHESback to summary
public static final String DASHES
INSTANCEback to summary
public static final OpenSSHKeyPairResourceWriter INSTANCE
LINE_LENGTHback to summary
public static final int LINE_LENGTH
VERTICALSPACEback to summary
private static final Pattern VERTICALSPACE

Constructor Detail

OpenSSHKeyPairResourceWriterback to summary
public OpenSSHKeyPairResourceWriter()

Method Detail

determineEncryptionback to summary
public static OpenSSHKeyEncryptionContext determineEncryption(OpenSSHKeyEncryptionContext options)
encodePrivateKeyback to summary
public static byte[] encodePrivateKey(KeyPair key, String keyType, int blockSize, String comment) throws IOException, GeneralSecurityException
encodePublicKeyback to summary
public static byte[] encodePublicKey(PublicKey key, String keyType) throws IOException, GeneralSecurityException
firstLineback to summary
public static String firstLine(String text)
writeback to summary
public static void write(OutputStream out, byte[] bytes, int lineLength) throws IOException
writeback to summary
public static void write(OutputStream out, String s) throws IOException
writePrivateKeyback to summary
public void writePrivateKey(KeyPair key, String comment, OpenSSHKeyEncryptionContext options, OutputStream out) throws IOException, GeneralSecurityException

Implements org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter.writePrivateKey.

Doc from org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter.writePrivateKey.

Writes a serialization of a private key from a given KeyPair to a given OutputStream.

Parameters
key:KeyPair

to write the private key of

comment:String

to write with the private key

options:OpenSSHKeyEncryptionContext

for writing the key; may be null if no encryption is wanted. The caller is responsible for clearing the options when no longer needed. If the passphrase obtained from the context is null or an empty/blank string (length zero or containing only whitespace), the key is written unencrypted.

out:OutputStream

The OutputStream to write to - recommend using a SecureByteArrayOutputStream in order to reduce sensitive data exposure in memory

Annotations
@Override
Exceptions
IOException:
if the key cannot be written
GeneralSecurityException:
if the key is inconsistent or unknown, or the encryption specified cannot be applied
writePublicKeyback to summary
public void writePublicKey(PublicKey key, String comment, OutputStream out) throws IOException, GeneralSecurityException

Implements org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter.writePublicKey.

Doc from org.apache.sshd.common.config.keys.writer.KeyPairResourceWriter.writePublicKey.

Writes a serialization of a PublicKey to a given OutputStream. Writes the public key in the single-line OpenSSH format "key-type pub-key comment" without terminating line ending. If the comment has multiple lines, only the first line is written.

Parameters
key:PublicKey

to write

comment:String

to write with the key

out:OutputStream

The OutputStream to write to - recommend using a SecureByteArrayOutputStream in order to reduce sensitive data exposure in memory

Annotations
@Override
Exceptions
IOException:
if the key cannot be written
GeneralSecurityException:
if the key is unknown
org.apache.sshd.common.config.keys.writer.openssh back to summary

public Class OpenSSHKeyPairResourceWriter.KeyEncryptor

extends AESPrivateKeyObfuscator
Class Inheritance

A key encryptor for modern-style OpenSSH private keys using the bcrypt KDF.

Field Summary

Modifier and TypeField and Description
public static final int
private byte[]
protected final OpenSSHKeyEncryptionContext
Inherited from org.apache.sshd.common.config.keys.loader.AESPrivateKeyObfuscator:
CIPHER_NAMEINSTANCE

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected byte[]
convert(String password)

protected byte[]

Returns:

keyLength bytes to use as encryption key
deriveEncryptionKey
(PrivateKeyEncryptionContext
for the encryption, provides the passphrase and transports other encryption-related information including the IV
context
,
int
number of key bytes to generate
keyLength
)

Overrides org.apache.sshd.common.config.keys.loader.AbstractPrivateKeyObfuscator.deriveEncryptionKey.

Derives an encryption key and set the IV on the context from the passphase provided by the context using the OpenBSD BCrypt KDF.

public byte[]

Returns:

the number of KDF rounds applied
getKdfOptions
()

Retrieves the KDF options used.

Inherited from org.apache.sshd.common.config.keys.loader.AESPrivateKeyObfuscator:
applyPrivateKeyCiphercreateCipherSelectorgetAvailableKeyLengthsgetSupportedKeySizesresolveCipherInformationresolveInitializationVectorLengthresolveKeyLength

Field Detail

BCRYPT_SALT_LENGTHback to summary
public static final int BCRYPT_SALT_LENGTH
kdfOptionsback to summary
private byte[] kdfOptions
optionsback to summary
protected final OpenSSHKeyEncryptionContext options

Constructor Detail

KeyEncryptorback to summary
public KeyEncryptor(OpenSSHKeyEncryptionContext options)

Method Detail

convertback to summary
protected byte[] convert(String password)
deriveEncryptionKeyback to summary
protected byte[] deriveEncryptionKey(PrivateKeyEncryptionContext context, int keyLength) throws IOException, GeneralSecurityException

Overrides org.apache.sshd.common.config.keys.loader.AbstractPrivateKeyObfuscator.deriveEncryptionKey.

Derives an encryption key and set the IV on the context from the passphase provided by the context using the OpenBSD BCrypt KDF.

Parameters
context:PrivateKeyEncryptionContext

for the encryption, provides the passphrase and transports other encryption-related information including the IV

keyLength:int

number of key bytes to generate

Returns:byte[]

keyLength bytes to use as encryption key

Annotations
@Override
getKdfOptionsback to summary
public byte[] getKdfOptions()

Retrieves the KDF options used. Valid only after deriveEncryptionKey(PrivateKeyEncryptionContext, int) has been called.

Returns:byte[]

the number of KDF rounds applied