public final class SecurityHelper extends Object
Modifier and Type | Method and Description |
---|---|
static KeyInfoCredentialResolver |
buildBasicInlineKeyInfoResolver()
Get a basic KeyInfo credential resolver which can process standard inline
data - RSAKeyValue, DSAKeyValue, X509Data.
|
static EncryptionParameters |
buildDataEncryptionParams(Credential encryptionCredential,
SecurityConfiguration config,
String keyInfoGenName)
Build an instance of
EncryptionParameters suitable for passing to an
Encrypter . |
static DSAPrivateKey |
buildJavaDSAPrivateKey(String base64EncodedKey)
Build Java DSA private key from base64 encoding.
|
static DSAPublicKey |
buildJavaDSAPublicKey(String base64EncodedKey)
Build Java DSA public key from base64 encoding.
|
static ECPublicKey |
buildJavaECPublicKey(String base64EncodedKey)
Build Java EC public key from base64 encoding.
|
static PrivateKey |
buildJavaPrivateKey(String base64EncodedKey)
Build Java private key from base64 encoding.
|
static RSAPrivateKey |
buildJavaRSAPrivateKey(String base64EncodedKey)
Build Java RSA private key from base64 encoding.
|
static RSAPublicKey |
buildJavaRSAPublicKey(String base64EncodedKey)
Build Java RSA public key from base64 encoding.
|
static X509Certificate |
buildJavaX509Cert(String base64Cert)
Build Java certificate from base64 encoding.
|
static X509CRL |
buildJavaX509CRL(String base64CRL)
Build Java CRL from base64 encoding.
|
static PublicKey |
buildKey(KeySpec keySpec,
String keyAlgorithm)
Generates a public key from the given key spec.
|
static KeyEncryptionParameters |
buildKeyEncryptionParams(Credential encryptionCredential,
String wrappedKeyAlgorithm,
SecurityConfiguration config,
String keyInfoGenName,
String recipient)
Build an instance of
KeyEncryptionParameters suitable for passing to an
Encrypter . |
static PrivateKey |
decodePrivateKey(byte[] key,
char[] password)
Decodes RSA/DSA private keys in DER, PEM, or PKCS#8 (encrypted or unencrypted) formats.
|
static PrivateKey |
decodePrivateKey(File key,
char[] password)
Decodes RSA/DSA private keys in DER, PEM, or PKCS#8 (encrypted or unencrypted) formats.
|
static PublicKey |
decodePublicKey(byte[] key,
char[] password)
Decodes RSA/DSA public keys in DER-encoded "SubjectPublicKeyInfo" format.
|
static SecretKey |
decodeSecretKey(byte[] key,
char[] password)
Decodes secret keys in DER and PEM format.
|
static PublicKey |
derivePublicKey(PrivateKey key)
Derives the public key from either a DSA or RSA private key.
|
static Key |
extractDecryptionKey(Credential credential)
Extract the decryption key from the credential.
|
static Key |
extractEncryptionKey(Credential credential)
Extract the encryption key from the credential.
|
static Key |
extractSigningKey(Credential credential)
Extract the signing key from the credential.
|
static Key |
extractVerificationKey(Credential credential)
Extract the verification key from the credential.
|
static SecretKey |
generateKey(String algo,
int keyLength,
String provider)
Generate a random symmetric key.
|
static Credential |
generateKeyAndCredential(String algorithmURI)
Generate a random symmetric key and return in a BasicCredential.
|
static SecretKey |
generateKeyFromURI(String algoURI)
Randomly generates a Java JCE symmetric Key object from the specified XML Encryption algorithm URI.
|
static KeyPair |
generateKeyPair(String algo,
int keyLength,
String provider)
Generate a random asymmetric key pair.
|
static Credential |
generateKeyPairAndCredential(String algorithmURI,
int keyLength,
boolean includePrivate)
Generate a random asymmetric key pair and return in a BasicCredential.
|
static KeyPair |
generateKeyPairFromURI(String algoURI,
int keyLength)
Randomly generates a Java JCE KeyPair object from the specified XML Encryption algorithm URI.
|
static SecretKey |
generateSymmetricKey(String algoURI)
Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI.
|
static String |
getAlgorithmIDFromURI(String algorithmURI)
Get the Java security JCA/JCE algorithm identifier associated with an algorithm URI.
|
static String |
getKeyAlgorithmFromURI(String algorithmURI)
Get the Java security JCA/JCE key algorithm specifier associated with an algorithm URI.
|
static KeyInfoGenerator |
getKeyInfoGenerator(Credential credential,
SecurityConfiguration config,
String keyInfoGenName)
Obtains a
KeyInfoGenerator for the specified Credential . |
static Integer |
getKeyLength(Key key)
Get the key length in bits of the specified key.
|
static Integer |
getKeyLengthFromURI(String algorithmURI)
Get the length of the key indicated by the algorithm URI, if applicable and available.
|
static BasicCredential |
getSimpleCredential(PublicKey publicKey,
PrivateKey privateKey)
Get a simple, minimal credential containing a public key, and optionally a private key.
|
static BasicCredential |
getSimpleCredential(SecretKey secretKey)
Get a simple, minimal credential containing a secret (symmetric) key.
|
static BasicX509Credential |
getSimpleCredential(X509Certificate cert,
PrivateKey privateKey)
Get a simple, minimal credential containing an end-entity X.509 certificate, and optionally a private key.
|
static boolean |
isHMAC(String signatureAlgorithm)
Check whether the signature method algorithm URI indicates HMAC.
|
static boolean |
matchKeyPair(PublicKey pubKey,
PrivateKey privKey)
Compare the supplied public and private keys, and determine if they correspond to the same key pair.
|
static void |
prepareSignatureParams(Signature signature,
Credential signingCredential,
SecurityConfiguration config,
String keyInfoGenName)
Prepare a
Signature with necessary additional information prior to signing. |
public static String getAlgorithmIDFromURI(String algorithmURI)
algorithmURI
- the algorithm URI to evaluatepublic static boolean isHMAC(String signatureAlgorithm)
signatureAlgorithm
- the signature method algorithm URIpublic static String getKeyAlgorithmFromURI(String algorithmURI)
algorithmURI
- the algorithm URI to evaluatepublic static Integer getKeyLengthFromURI(String algorithmURI)
algorithmURI
- the algorithm URI to evaluatepublic static SecretKey generateSymmetricKey(String algoURI) throws NoSuchAlgorithmException, KeyException
algoURI
- The XML Encryption algorithm URINoSuchAlgorithmException
- thrown if the specified algorithm is invalidKeyException
- thrown if the length of the key to generate could not be determinedpublic static Key extractEncryptionKey(Credential credential)
credential
- the credential containing the encryption keypublic static Key extractDecryptionKey(Credential credential)
credential
- the credential containing the decryption keypublic static Key extractSigningKey(Credential credential)
credential
- the credential containing the signing keypublic static Key extractVerificationKey(Credential credential)
credential
- the credential containing the verification keypublic static Integer getKeyLength(Key key)
key
- the key to evaluatepublic static BasicCredential getSimpleCredential(SecretKey secretKey)
secretKey
- the symmetric key to wrappublic static BasicCredential getSimpleCredential(PublicKey publicKey, PrivateKey privateKey)
publicKey
- the public key to wrapprivateKey
- the private key to wrap, which may be nullpublic static BasicX509Credential getSimpleCredential(X509Certificate cert, PrivateKey privateKey)
cert
- the end-entity certificate to wrapprivateKey
- the private key to wrap, which may be nullpublic static SecretKey decodeSecretKey(byte[] key, char[] password) throws KeyException
key
- secret keypassword
- password if the key is encrypted or null if notKeyException
- thrown if the key can not be decodedpublic static PublicKey decodePublicKey(byte[] key, char[] password) throws KeyException
key
- encoded keypassword
- password if the key is encrypted or null if notKeyException
- thrown if the key can not be decodedpublic static PublicKey derivePublicKey(PrivateKey key) throws KeyException
key
- the private key to derive the public key fromKeyException
- thrown if the given private key is not a DSA or RSA key or there is a problem generating the
public keypublic static PrivateKey decodePrivateKey(File key, char[] password) throws KeyException
key
- encoded keypassword
- decryption password or null if the key is not encryptedKeyException
- thrown if the key can not be decodedpublic static PrivateKey decodePrivateKey(byte[] key, char[] password) throws KeyException
key
- encoded keypassword
- decryption password or null if the key is not encryptedKeyException
- thrown if the key can not be decodedpublic static X509Certificate buildJavaX509Cert(String base64Cert) throws CertificateException
base64Cert
- base64-encoded certificateCertificateException
- thrown if there is an error constructing certificatepublic static X509CRL buildJavaX509CRL(String base64CRL) throws CertificateException, CRLException
base64CRL
- base64-encoded CRLCertificateException
- thrown if there is an error constructing certificateCRLException
- thrown if there is an error constructing CRLpublic static DSAPublicKey buildJavaDSAPublicKey(String base64EncodedKey) throws KeyException
base64EncodedKey
- base64-encoded DSA public keyKeyException
- thrown if there is an error constructing keypublic static RSAPublicKey buildJavaRSAPublicKey(String base64EncodedKey) throws KeyException
base64EncodedKey
- base64-encoded RSA public keyKeyException
- thrown if there is an error constructing keypublic static ECPublicKey buildJavaECPublicKey(String base64EncodedKey) throws KeyException
base64EncodedKey
- base64-encoded EC public keyKeyException
- thrown if there is an error constructing keypublic static RSAPrivateKey buildJavaRSAPrivateKey(String base64EncodedKey) throws KeyException
base64EncodedKey
- base64-encoded RSA private keyKeyException
- thrown if there is an error constructing keypublic static DSAPrivateKey buildJavaDSAPrivateKey(String base64EncodedKey) throws KeyException
base64EncodedKey
- base64-encoded DSA private keyKeyException
- thrown if there is an error constructing keypublic static PrivateKey buildJavaPrivateKey(String base64EncodedKey) throws KeyException
base64EncodedKey
- base64-encoded private keyKeyException
- thrown if there is an error constructing keypublic static PublicKey buildKey(KeySpec keySpec, String keyAlgorithm) throws KeyException
keySpec
- KeySpec
specification for the keykeyAlgorithm
- key generation algorithm, only DSA and RSA supportedPublicKey
KeyException
- thrown if the key algorithm is not supported by the JCE or the key spec does not
contain valid informationpublic static SecretKey generateKeyFromURI(String algoURI) throws NoSuchAlgorithmException, NoSuchProviderException
algoURI
- The XML Encryption algorithm URINoSuchProviderException
- provider not foundNoSuchAlgorithmException
- algorithm not foundpublic static KeyPair generateKeyPairFromURI(String algoURI, int keyLength) throws NoSuchAlgorithmException, NoSuchProviderException
algoURI
- The XML Encryption algorithm URIkeyLength
- the length of key to generateNoSuchProviderException
- provider not foundNoSuchAlgorithmException
- algorithm not foundpublic static SecretKey generateKey(String algo, int keyLength, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
algo
- key algorithmkeyLength
- key lengthprovider
- JCA providerNoSuchAlgorithmException
- algorithm not foundNoSuchProviderException
- provider not foundpublic static KeyPair generateKeyPair(String algo, int keyLength, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
algo
- key algorithmkeyLength
- key lengthprovider
- JCA providerNoSuchAlgorithmException
- algorithm not foundNoSuchProviderException
- provider not foundpublic static Credential generateKeyAndCredential(String algorithmURI) throws NoSuchAlgorithmException, NoSuchProviderException
algorithmURI
- The XML Encryption algorithm URINoSuchAlgorithmException
- algorithm not foundNoSuchProviderException
- provider not foundpublic static Credential generateKeyPairAndCredential(String algorithmURI, int keyLength, boolean includePrivate) throws NoSuchAlgorithmException, NoSuchProviderException
algorithmURI
- The XML Encryption algorithm URIkeyLength
- key lengthincludePrivate
- if true, the private key will be included as wellNoSuchAlgorithmException
- algorithm not foundNoSuchProviderException
- provider not foundpublic static KeyInfoCredentialResolver buildBasicInlineKeyInfoResolver()
public static boolean matchKeyPair(PublicKey pubKey, PrivateKey privKey) throws SecurityException
pubKey
- the public keyprivKey
- the private keySecurityException
- if the keys can not be evaluated, or if the key algorithm is unsupported or unknownpublic static void prepareSignatureParams(Signature signature, Credential signingCredential, SecurityConfiguration config, String keyInfoGenName) throws SecurityException
Signature
with necessary additional information prior to signing.
NOTE:Since this operation modifies the specified Signature object, it should be called prior to marshalling the Signature object.
The following Signature values will be added:
KeyInfo
element representing the signing credentialExisting (non-null) values of these parameters on the specified signature will NOT be overwritten, however.
All values are determined by the specified SecurityConfiguration
. If a security configuration is not
supplied, the global security configuration (Configuration.getGlobalSecurityConfiguration()
) will be
used.
The signature algorithm URI and optional HMAC output length are derived from the signing credential.
The KeyInfo to be generated is based on the NamedKeyInfoGeneratorManager
defined in the security
configuration, and is determined by the type of the signing credential and an optional KeyInfo generator manager
name. If the latter is ommited, the default manager (NamedKeyInfoGeneratorManager.getDefaultManager()
)
of the security configuration's named generator manager will be used.
signature
- the Signature to be updatedsigningCredential
- the credential with which the Signature will be computedconfig
- the SecurityConfiguration to use (may be null)keyInfoGenName
- the named KeyInfoGeneratorManager configuration to use (may be null)SecurityException
- thrown if there is an error generating the KeyInfo from the signing credentialpublic static EncryptionParameters buildDataEncryptionParams(Credential encryptionCredential, SecurityConfiguration config, String keyInfoGenName)
EncryptionParameters
suitable for passing to an
Encrypter
.
The following parameter values will be added:
KeyInfoGenerator
instance which will be used to generate a KeyInfo
element
from the encryption credential
All values are determined by the specified SecurityConfiguration
. If a security configuration is not
supplied, the global security configuration (Configuration.getGlobalSecurityConfiguration()
) will be
used.
The encryption algorithm URI is derived from the optional supplied encryption credential. If omitted, the value
of SecurityConfiguration.getAutoGeneratedDataEncryptionKeyAlgorithmURI()
will be used.
The KeyInfoGenerator to be used is based on the NamedKeyInfoGeneratorManager
defined in the security
configuration, and is determined by the type of the signing credential and an optional KeyInfo generator manager
name. If the latter is ommited, the default manager (NamedKeyInfoGeneratorManager.getDefaultManager()
)
of the security configuration's named generator manager will be used.
encryptionCredential
- the credential with which the data will be encrypted (may be null)config
- the SecurityConfiguration to use (may be null)keyInfoGenName
- the named KeyInfoGeneratorManager configuration to use (may be null)public static KeyEncryptionParameters buildKeyEncryptionParams(Credential encryptionCredential, String wrappedKeyAlgorithm, SecurityConfiguration config, String keyInfoGenName, String recipient) throws SecurityException
KeyEncryptionParameters
suitable for passing to an
Encrypter
.
The following parameter values will be added:
KeyInfoGenerator
instance which will be used to generate a KeyInfo
element
from the key encryption credential
All values are determined by the specified SecurityConfiguration
. If a security configuration is not
supplied, the global security configuration (Configuration.getGlobalSecurityConfiguration()
) will be
used.
The encryption algorithm URI is derived from the optional supplied encryption credential. If omitted, the value
of SecurityConfiguration.getAutoGeneratedDataEncryptionKeyAlgorithmURI()
will be used.
The KeyInfoGenerator to be used is based on the NamedKeyInfoGeneratorManager
defined in the security
configuration, and is determined by the type of the signing credential and an optional KeyInfo generator manager
name. If the latter is ommited, the default manager (NamedKeyInfoGeneratorManager.getDefaultManager()
)
of the security configuration's named generator manager will be used.
encryptionCredential
- the credential with which the key will be encryptedwrappedKeyAlgorithm
- the JCA key algorithm name of the key to be encrypted (may be null)config
- the SecurityConfiguration to use (may be null)keyInfoGenName
- the named KeyInfoGeneratorManager configuration to use (may be null)recipient
- the intended recipient of the resultant encrypted key, typically the owner of the key encryption
key (may be null)SecurityException
- if encryption credential is not suppliedpublic static KeyInfoGenerator getKeyInfoGenerator(Credential credential, SecurityConfiguration config, String keyInfoGenName)
KeyInfoGenerator
for the specified Credential
.
The KeyInfoGenerator returned is based on the NamedKeyInfoGeneratorManager
defined by the specified
security configuration via SecurityConfiguration.getKeyInfoGeneratorManager()
, and is determined by the
type of the signing credential and an optional KeyInfo generator manager name. If the latter is ommited, the
default manager (NamedKeyInfoGeneratorManager.getDefaultManager()
) of the security configuration's
named generator manager will be used.
The generator is determined by the specified SecurityConfiguration
. If a security configuration is not
supplied, the global security configuration (Configuration.getGlobalSecurityConfiguration()
) will be
used.
credential
- the credential for which a generator is desiredconfig
- the SecurityConfiguration to use (may be null)keyInfoGenName
- the named KeyInfoGeneratorManager configuration to use (may be null)Copyright © 2018 JBoss by Red Hat. All rights reserved.