Top Description Fields Constructors Methods
org.apache.sshd.common.util.security

public final Class SecurityUtils

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, java.math.BigInteger, java.nio.file.Path, java.security.GeneralSecurityException, .InvalidKeyException, .Key, .KeyFactory, .KeyPair, .KeyPairGenerator, .MessageDigest, .NoSuchAlgorithmException, .NoSuchProviderException, .PrivateKey, .PublicKey, .Signature, java.security.cert.CertificateFactory, java.util.Arrays, .Collection, .Collections, .HashMap, .LinkedHashMap, .List, .Map, .Objects, .Optional, .Set, .TreeMap, .TreeSet, java.util.concurrent.atomic.AtomicBoolean, .AtomicInteger, .AtomicReference, java.util.function.Predicate, javax.crypto.Cipher, .KeyAgreement, .Mac, javax.crypto.spec.DHParameterSpec, org.apache.sshd.common.NamedResource, .PropertyResolverUtils, org.apache.sshd.common.config.keys.FilePasswordProvider, .KeyUtils, .PrivateKeyEntryDecoder, .PublicKeyEntryDecoder, org.apache.sshd.common.config.keys.loader.KeyPairResourceParser, org.apache.sshd.common.config.keys.loader.openssh.OpenSSHKeyPairResourceParser, org.apache.sshd.common.config.keys.loader.pem.PEMResourceParserUtils, org.apache.sshd.common.keyprovider.KeyPairProvider, org.apache.sshd.common.random.JceRandomFactory, .RandomFactory, org.apache.sshd.common.session.SessionContext, org.apache.sshd.common.util.ExceptionUtils, .GenericUtils, .ValidateUtils, org.apache.sshd.common.util.buffer.Buffer, org.apache.sshd.common.util.security.bouncycastle.BouncyCastleEncryptedPrivateKeyInfoDecryptor, .BouncyCastleGeneratorHostKeyProvider, .BouncyCastleKeyPairResourceParser, .BouncyCastleRandomFactory, org.apache.sshd.common.util.security.eddsa.generic.EdDSASupport, org.apache.sshd.common.util.threads.ThreadUtils, org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider, org.slf4j.Logger, .LoggerFactory

Specific security providers related code
Author
Apache MINA SSHD Project

Field Summary

Modifier and TypeField and Description
private static final Set<String>
public static final String
public static final String
private static final AtomicReference<SecurityProviderChoice>
public static final List<String>
public static final String
ECC_SUPPORTED_PROP

System property used to control whether Elliptic Curves are supported or not.

public static final String
public static final String
EDDSA

EDDSA support - should match EdDSAKey.KEY_ALGORITHM

public static final String
EDDSA_SUPPORTED_PROP

Deprecated Please use "org.apache.sshd.security.provider.EdDSA.enabled&qupt;
System property used to decide whether EDDSA curves are supported or not (in addition or even in spite of isEDDSACurveSupported()).
public static final String
FIPS_ENABLED

A boolean system property that can be set to "true" to enable FIPS mode.

private static final AtomicReference<Boolean>
private static Boolean
private static final AtomicReference<KeyPairResourceParser>
private static final AtomicInteger
public static final int
public static final String
MAX_DHGEX_KEY_SIZE_PROP

System property used to configure the value for the maximum supported Diffie-Hellman Group Exchange key size.

private static final AtomicInteger
public static final int
public static final String
MIN_DHGEX_KEY_SIZE_PROP

System property used to configure the value for the minimum supported Diffie-Hellman Group Exchange key size.

public static final int
public static final String
public static final String
REGISTER_BOUNCY_CASTLE_PROP

Deprecated Please use "org.apache.sshd.security.provider.BC.enabled"
System property used to control whether to automatically register the Bouncyastle JCE provider
private static final Map<String, SecurityProviderRegistrar>
private static final AtomicBoolean
private static final Map<Class<?>, Map<String, SecurityEntityFactory<?>>>
public static final String
SECURITY_PROVIDER_REGISTRARS

Comma separated list of fully qualified SecurityProviderRegistrars to automatically register

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static boolean
public static boolean
public static AbstractGeneratorHostKeyProvider
public static <T> SecurityEntityFactory<T>
createSecurityEntityFactory(Class<T> entityType, Predicate<? super SecurityProviderRegistrar> entitySelector)

public static KeyPair
public static PrivateKey
generateEDDSAPrivateKey(String keyType, byte[] seed)

public static PublicKey
generateEDDSAPublicKey(String keyType, byte[] seed)

public static Set<String>

Returns:

A copy if the current a-priori disabled providers names
getAPrioriDisabledProviders
()

public static Decryptor
public static KeyPairResourceParser
public static CertificateFactory
public static Cipher
getCipher(String transformation)

public static SecurityProviderChoice
public static int
public static Class<? extends PrivateKey>
public static PublicKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey>
public static Class<? extends PublicKey>
public static Signature
public static Optional<EdDSASupport<?, ?>>
public static KeyAgreement
public static KeyFactory
getKeyFactory(String algorithm)

public static KeyPairGenerator
public static KeyPairResourceParser
public static Mac
getMac(String algorithm)

public static int

Returns:

The maximum supported Diffie-Hellman Group Exchange key size, or non-positive if not supported
getMaxDHGroupExchangeKeySize
()

public static MessageDigest
public static int

Returns:

The minimum supported Diffie-Hellman Group Exchange key size, or non-positive if not supported
getMinDHGroupExchangeKeySize
()

public static PrivateKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey>
public static RandomFactory
public static SecurityProviderRegistrar
public static Set<String>

Returns:

A copy of the currently registered security providers
getRegisteredProviders
()

public static Signature
getSignature(String algorithm)

public static boolean

Returns:

true if the provider is marked as disabled a-priori
isAPrioriDisabledProvider
(String
The provider's name - never null/empty
name
)

public static boolean
public static boolean

Returns:

true if Diffie-Hellman Group Exchange is supported
isDHGroupExchangeSupported
()

public static boolean
public static boolean

Returns:

true if Oakely Diffie-Hellman Group Exchange is supported for the specified key size
isDHOakelyGroupSupported
(int
The expected key size
keySize
)

public static boolean

Returns:

true if Elliptic Curve Cryptography is supported
isECCSupported
()

public static boolean

Returns:

true if EDDSA curves (e.g., ed25519) are supported
isEDDSACurveSupported
()

public static boolean

Returns:

true if FIPS mode is enabled, false otherwise.
isFipsMode
()

Tells whether FIPS mode is enabled, either through the system property FIPS_ENABLED or via setFipsMode().

public static boolean
public static boolean
public static boolean
public static Iterable<KeyPair>

Returns:

The loaded KeyPair-s - or null if none loaded
loadKeyPairIdentities
(SessionContext
The SessionContext for invoking this load command - may be null if not invoked within a session context (e.g., offline tool).
session
,
NamedResource
An identifier of the key being loaded - used as argument to the FilePasswordProvider#getPassword invocation
resourceKey
,
InputStream
The InputStream for the private key
inputStream
,
FilePasswordProvider
A FilePasswordProvider - may be null if the loaded key is guaranteed not to be encrypted
provider
)

public static <B extends Buffer> B
putEDDSAKeyPair(B buffer, KeyPair kp)

public static <B extends Buffer> B
putEDDSAKeyPair(B buffer, PublicKey pubKey, PrivateKey prvKey)

public static <B extends Buffer> B
public static PublicKey
private static void
public static SecurityProviderRegistrar

Returns:

The registered instance - may be different than required if already registered. Returns null if not already registered and not enabled or not supported registrar.
registerSecurityProvider
(SecurityProviderRegistrar
The registrar instance to register
registrar
)

private static int
resolveDHGEXKeySizeValue(AtomicInteger holder, String propName, int maxKeySize)

public static <T> SecurityEntityFactory<T>
resolveSecurityEntityFactory(Class<T> entityType, String algorithm, Predicate<? super SecurityProviderRegistrar> entitySelector)

public static void
setAPrioriDisabledProvider(String
The provider's name - never null/empty
name
,
boolean
true whether to disable it a-priori
disabled
)

Marks a provider's registrar as "a-priori" programatically so that when its SecurityProviderRegistrar#isEnabled() is eventually consulted it will return false regardless of the configured value for the specific provider registrar instance

Note

has no effect if the provider has already been registered.

public static void
public static void
setFipsMode()

Unconditionally set FIPS mode, overriding the FIPS_ENABLED system property.

public static void
setKeyPairResourceParser(KeyPairResourceParser
The system-wide KeyPairResourceParser to use. If set to null, then the default parser will be re-constructed on next call to getKeyPairResourceParser()
parser
)

public static void
setMaxDHGroupExchangeKeySize(int
The reported key size - if zero, then it will be auto-detected, if negative then DH group exchange will be disabled
keySize
)

Set programmatically the reported value for getMaxDHGroupExchangeKeySize()

public static void
setMinDHGroupExchangeKeySize(int
The reported key size - if zero, then it will be auto-detected, if negative then DH group exchange will be disabled
keySize
)

Set programmatically the reported value for getMinDHGroupExchangeKeySize()

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

APRIORI_DISABLED_PROVIDERSback to summary
private static final Set<String> APRIORI_DISABLED_PROVIDERS
BOUNCY_CASTLEback to summary
public static final String BOUNCY_CASTLE

Bouncy Castle SecurityProviderRegistrar name.

CURVE_ED25519_SHA512back to summary
public static final String CURVE_ED25519_SHA512
DEFAULT_PROVIDER_HOLDERback to summary
private static final AtomicReference<SecurityProviderChoice> DEFAULT_PROVIDER_HOLDER
DEFAULT_SECURITY_PROVIDER_REGISTRARSback to summary
public static final List<String> DEFAULT_SECURITY_PROVIDER_REGISTRARS
ECC_SUPPORTED_PROPback to summary
public static final String ECC_SUPPORTED_PROP

System property used to control whether Elliptic Curves are supported or not. If not set then the support is auto-detected

Note

if set to true it is up to the user to make sure that indeed there is a provider for them

ED25519back to summary
public static final String ED25519
EDDSAback to summary
public static final String EDDSA

EDDSA support - should match EdDSAKey.KEY_ALGORITHM

EDDSA_SUPPORTED_PROPback to summary
public static final String EDDSA_SUPPORTED_PROP

Deprecated

Please use "org.apache.sshd.security.provider.EdDSA.enabled&qupt;

System property used to decide whether EDDSA curves are supported or not (in addition or even in spite of isEDDSACurveSupported()). If not set or set to true, then the existence of the optional support classes determines the support.

Annotations
@Deprecated
FIPS_ENABLEDback to summary
public static final String FIPS_ENABLED

A boolean system property that can be set to "true" to enable FIPS mode. In FIPS mode, crypto-algorithms not approved in FIPS-140 will not be available.

Note

if this system property is not "true", it can be overridden via setFipsMode().

FIPS_MODEback to summary
private static final AtomicReference<Boolean> FIPS_MODE
hasEccback to summary
private static Boolean hasEcc
KEYPAIRS_PARSER_HODLERback to summary
private static final AtomicReference<KeyPairResourceParser> KEYPAIRS_PARSER_HODLER
MAX_DHG_KEY_SIZE_HOLDERback to summary
private static final AtomicInteger MAX_DHG_KEY_SIZE_HOLDER
MAX_DHGEX_KEY_SIZEback to summary
public static final int MAX_DHGEX_KEY_SIZE
MAX_DHGEX_KEY_SIZE_PROPback to summary
public static final String MAX_DHGEX_KEY_SIZE_PROP

System property used to configure the value for the maximum supported Diffie-Hellman Group Exchange key size. If not set, then an internal auto-discovery mechanism is employed. If set to negative value then Diffie-Hellman Group Exchange is disabled. If set to a negative value then Diffie-Hellman Group Exchange is disabled

MIN_DHG_KEY_SIZE_HOLDERback to summary
private static final AtomicInteger MIN_DHG_KEY_SIZE_HOLDER
MIN_DHGEX_KEY_SIZEback to summary
public static final int MIN_DHGEX_KEY_SIZE

The min. key size value used for testing whether Diffie-Hellman Group Exchange is supported or not. According to RFC 4419 section 3: "Servers and clients SHOULD support groups with a modulus length of k bits, where 1024 <= k <= 8192". Note: this has been amended by RFC 8270

MIN_DHGEX_KEY_SIZE_PROPback to summary
public static final String MIN_DHGEX_KEY_SIZE_PROP

System property used to configure the value for the minimum supported Diffie-Hellman Group Exchange key size. If not set, then an internal auto-discovery mechanism is employed. If set to negative value then Diffie-Hellman Group Exchange is disabled. If set to a negative value then Diffie-Hellman Group Exchange is disabled

PREFERRED_DHGEX_KEY_SIZEback to summary
public static final int PREFERRED_DHGEX_KEY_SIZE
PROP_DEFAULT_SECURITY_PROVIDERback to summary
public static final String PROP_DEFAULT_SECURITY_PROVIDER
REGISTER_BOUNCY_CASTLE_PROPback to summary
public static final String REGISTER_BOUNCY_CASTLE_PROP

Deprecated

Please use "org.apache.sshd.security.provider.BC.enabled"

System property used to control whether to automatically register the Bouncyastle JCE provider

Annotations
@Deprecated
REGISTERED_PROVIDERSback to summary
private static final Map<String, SecurityProviderRegistrar> REGISTERED_PROVIDERS
REGISTRATION_STATE_HOLDERback to summary
private static final AtomicBoolean REGISTRATION_STATE_HOLDER
SECURITY_ENTITY_FACTORIESback to summary
private static final Map<Class<?>, Map<String, SecurityEntityFactory<?>>> SECURITY_ENTITY_FACTORIES
SECURITY_PROVIDER_REGISTRARSback to summary
public static final String SECURITY_PROVIDER_REGISTRARS

Comma separated list of fully qualified SecurityProviderRegistrars to automatically register

Constructor Detail

SecurityUtilsback to summary
private SecurityUtils()

Method Detail

compareEDDSAPPublicKeysback to summary
public static boolean compareEDDSAPPublicKeys(PublicKey k1, PublicKey k2)
compareEDDSAPrivateKeysback to summary
public static boolean compareEDDSAPrivateKeys(PrivateKey k1, PrivateKey k2)
createGeneratorHostKeyProviderback to summary
public static AbstractGeneratorHostKeyProvider createGeneratorHostKeyProvider(Path path)
createSecurityEntityFactoryback to summary
public static <T> SecurityEntityFactory<T> createSecurityEntityFactory(Class<T> entityType, Predicate<? super SecurityProviderRegistrar> entitySelector)
extractEDDSAKeyPairback to summary
public static KeyPair extractEDDSAKeyPair(Buffer buffer, String keyType) throws GeneralSecurityException
generateEDDSAPrivateKeyback to summary
public static PrivateKey generateEDDSAPrivateKey(String keyType, byte[] seed) throws GeneralSecurityException, IOException
generateEDDSAPublicKeyback to summary
public static PublicKey generateEDDSAPublicKey(String keyType, byte[] seed) throws GeneralSecurityException
getAPrioriDisabledProvidersback to summary
public static Set<String> getAPrioriDisabledProviders()
Returns:Set<String>

A copy if the current a-priori disabled providers names

getBouncycastleEncryptedPrivateKeyInfoDecryptorback to summary
public static Decryptor getBouncycastleEncryptedPrivateKeyInfoDecryptor()
getBouncycastleKeyPairResourceParserback to summary
public static KeyPairResourceParser getBouncycastleKeyPairResourceParser()
getCertificateFactoryback to summary
public static CertificateFactory getCertificateFactory(String type) throws GeneralSecurityException
getCipherback to summary
public static Cipher getCipher(String transformation) throws GeneralSecurityException
getDefaultProviderChoiceback to summary
public static SecurityProviderChoice getDefaultProviderChoice()
getEDDSAKeySizeback to summary
public static int getEDDSAKeySize(Key key)
getEDDSAPrivateKeyTypeback to summary
public static Class<? extends PrivateKey> getEDDSAPrivateKeyType()
getEDDSAPublicKeyEntryDecoderback to summary
public static PublicKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey> getEDDSAPublicKeyEntryDecoder()
getEDDSAPublicKeyTypeback to summary
public static Class<? extends PublicKey> getEDDSAPublicKeyType()
getEDDSASignerback to summary
public static Signature getEDDSASigner()
getEdDSASupportback to summary
public static Optional<EdDSASupport<?, ?>> getEdDSASupport()
getKeyAgreementback to summary
public static KeyAgreement getKeyAgreement(String algorithm) throws GeneralSecurityException
getKeyFactoryback to summary
public static KeyFactory getKeyFactory(String algorithm) throws GeneralSecurityException
getKeyPairGeneratorback to summary
public static KeyPairGenerator getKeyPairGenerator(String algorithm) throws GeneralSecurityException
getKeyPairResourceParserback to summary
public static KeyPairResourceParser getKeyPairResourceParser()
getMacback to summary
public static Mac getMac(String algorithm) throws GeneralSecurityException
getMaxDHGroupExchangeKeySizeback to summary
public static int getMaxDHGroupExchangeKeySize()
Returns:int

The maximum supported Diffie-Hellman Group Exchange key size, or non-positive if not supported

getMessageDigestback to summary
public static MessageDigest getMessageDigest(String algorithm) throws GeneralSecurityException
getMinDHGroupExchangeKeySizeback to summary
public static int getMinDHGroupExchangeKeySize()
Returns:int

The minimum supported Diffie-Hellman Group Exchange key size, or non-positive if not supported

getOpenSSHEDDSAPrivateKeyEntryDecoderback to summary
public static PrivateKeyEntryDecoder<? extends PublicKey, ? extends PrivateKey> getOpenSSHEDDSAPrivateKeyEntryDecoder()
getRandomFactoryback to summary
public static RandomFactory getRandomFactory()
Returns:RandomFactory

If isBouncyCastleRegistered() then a BouncyCastleRandomFactory instance, otherwise a JceRandomFactory one

getRegisteredProviderback to summary
public static SecurityProviderRegistrar getRegisteredProvider(String provider)
getRegisteredProvidersback to summary
public static Set<String> getRegisteredProviders()
Returns:Set<String>

A copy of the currently registered security providers

getSignatureback to summary
public static Signature getSignature(String algorithm) throws GeneralSecurityException
isAPrioriDisabledProviderback to summary
public static boolean isAPrioriDisabledProvider(String name)
Parameters
name:String

The provider's name - never null/empty

Returns:boolean

true if the provider is marked as disabled a-priori

See Also
setAPrioriDisabledProvider(String, boolean)
isBouncyCastleRegisteredback to summary
public static boolean isBouncyCastleRegistered()
isDHGroupExchangeSupportedback to summary
public static boolean isDHGroupExchangeSupported()
Returns:boolean

true if Diffie-Hellman Group Exchange is supported

See Also
getMinDHGroupExchangeKeySize(), getMaxDHGroupExchangeKeySize()
isDHGroupExchangeSupportedback to summary
public static boolean isDHGroupExchangeSupported(int maxKeySize)
isDHOakelyGroupSupportedback to summary
public static boolean isDHOakelyGroupSupported(int keySize)
Parameters
keySize:int

The expected key size

Returns:boolean

true if Oakely Diffie-Hellman Group Exchange is supported for the specified key size

See Also
isDHGroupExchangeSupported(), getMaxDHGroupExchangeKeySize()
isECCSupportedback to summary
public static boolean isECCSupported()
Returns:boolean

true if Elliptic Curve Cryptography is supported

See Also
ECC_SUPPORTED_PROP
isEDDSACurveSupportedback to summary
public static boolean isEDDSACurveSupported()
Returns:boolean

true if EDDSA curves (e.g., ed25519) are supported

isFipsModeback to summary
public static boolean isFipsMode()

Tells whether FIPS mode is enabled, either through the system property FIPS_ENABLED or via setFipsMode().

Returns:boolean

true if FIPS mode is enabled, false otherwise.

isNetI2pCryptoEdDSARegisteredback to summary
public static boolean isNetI2pCryptoEdDSARegistered()
isProviderRegisteredback to summary
public static boolean isProviderRegistered(String provider)
isRegistrationCompletedback to summary
public static boolean isRegistrationCompleted()
loadKeyPairIdentitiesback to summary
public static Iterable<KeyPair> loadKeyPairIdentities(SessionContext session, NamedResource resourceKey, InputStream inputStream, FilePasswordProvider provider) throws IOException, GeneralSecurityException
Parameters
session:SessionContext

The SessionContext for invoking this load command - may be null if not invoked within a session context (e.g., offline tool).

resourceKey:NamedResource

An identifier of the key being loaded - used as argument to the FilePasswordProvider#getPassword invocation

inputStream:InputStream

The InputStream for the private key

provider:FilePasswordProvider

A FilePasswordProvider - may be null if the loaded key is guaranteed not to be encrypted

Returns:Iterable<KeyPair>

The loaded KeyPair-s - or null if none loaded

Exceptions
IOException:
If failed to read/parse the input stream
GeneralSecurityException:
If failed to generate the keys
putEDDSAKeyPairback to summary
public static <B extends Buffer> B putEDDSAKeyPair(B buffer, KeyPair kp)
putEDDSAKeyPairback to summary
public static <B extends Buffer> B putEDDSAKeyPair(B buffer, PublicKey pubKey, PrivateKey prvKey)
putRawEDDSAPublicKeyback to summary
public static <B extends Buffer> B putRawEDDSAPublicKey(B buffer, PublicKey key)
recoverEDDSAPublicKeyback to summary
public static PublicKey recoverEDDSAPublicKey(PrivateKey key) throws GeneralSecurityException
registerback to summary
private static void register()
registerSecurityProviderback to summary
public static SecurityProviderRegistrar registerSecurityProvider(SecurityProviderRegistrar registrar)
Parameters
registrar:SecurityProviderRegistrar

The registrar instance to register

Returns:SecurityProviderRegistrar

The registered instance - may be different than required if already registered. Returns null if not already registered and not enabled or not supported registrar.

resolveDHGEXKeySizeValueback to summary
private static int resolveDHGEXKeySizeValue(AtomicInteger holder, String propName, int maxKeySize)
resolveSecurityEntityFactoryback to summary
public static <T> SecurityEntityFactory<T> resolveSecurityEntityFactory(Class<T> entityType, String algorithm, Predicate<? super SecurityProviderRegistrar> entitySelector)
Annotations
@SuppressWarnings:unchecked
setAPrioriDisabledProviderback to summary
public static void setAPrioriDisabledProvider(String name, boolean disabled)

Marks a provider's registrar as "a-priori" programatically so that when its SecurityProviderRegistrar#isEnabled() is eventually consulted it will return false regardless of the configured value for the specific provider registrar instance

Note

has no effect if the provider has already been registered.

Parameters
name:String

The provider's name - never null/empty

disabled:boolean

true whether to disable it a-priori

See Also
isAPrioriDisabledProvider(String)
setDefaultProviderChoiceback to summary
public static void setDefaultProviderChoice(SecurityProviderChoice choice)
setFipsModeback to summary
public static void setFipsMode()

Unconditionally set FIPS mode, overriding the FIPS_ENABLED system property.

Exceptions
IllegalStateException:
if a call to isFipsMode() had already occurred and returned false.
setKeyPairResourceParserback to summary
public static void setKeyPairResourceParser(KeyPairResourceParser parser)
Parameters
parser:KeyPairResourceParser

The system-wide KeyPairResourceParser to use. If set to null, then the default parser will be re-constructed on next call to getKeyPairResourceParser()

setMaxDHGroupExchangeKeySizeback to summary
public static void setMaxDHGroupExchangeKeySize(int keySize)

Set programmatically the reported value for getMaxDHGroupExchangeKeySize()

Parameters
keySize:int

The reported key size - if zero, then it will be auto-detected, if negative then DH group exchange will be disabled

setMinDHGroupExchangeKeySizeback to summary
public static void setMinDHGroupExchangeKeySize(int keySize)

Set programmatically the reported value for getMinDHGroupExchangeKeySize()

Parameters
keySize:int

The reported key size - if zero, then it will be auto-detected, if negative then DH group exchange will be disabled