public final class SecurityUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static KeyStore |
getDefaultKeyStore()
Returns the default key store using
KeyStore.
|
static KeyStore |
getJavaKeyStore()
Returns the Java KeyStore (JKS).
|
static KeyStore |
getPkcs12KeyStore()
Returns the PKCS12 key store.
|
static PrivateKey |
getPrivateKey(KeyStore
Returns the private key from the key store.
|
static KeyFactory |
getRsaKeyFactory()
Returns the RSA key factory.
|
static Signature |
getSha1WithRsaSignatureAlgorithm()
Returns the SHA-1 with RSA signature algorithm.
|
static Signature |
getSha256WithRsaSignatureAlgorithm()
Returns the SHA-256 with RSA signature algorithm.
|
static CertificateFactory |
getX509CertificateFactory()
Returns the X.509 certificate factory.
|
static void |
loadKeyStore(KeyStore
Loads a key store from a stream.
|
static void |
loadKeyStoreFromCertificates(KeyStore
Loads a key store with certificates generated from the specified stream using
CertificateFactory.
|
static PrivateKey |
loadPrivateKeyFromKeyStore(KeyStore
Retrieves a private key from the specified key store stream and specified key store.
|
static byte[] |
sign(Signature
Signs content using a private key.
|
static boolean |
verify(Signature
Verifies the signature of signed content based on a public key.
|
static X509Certificate |
verify(Signature
Verifies the signature of signed content based on a certificate chain.
|
public static KeyStoregetDefaultKeyStore() throws KeyStoreException
KeyStore.getDefaultType() .
KeyStoreException
public static KeyStoregetJavaKeyStore() throws KeyStoreException
KeyStoreException
public static KeyStoregetPkcs12KeyStore() throws KeyStoreException
KeyStoreException
public static void loadKeyStore(KeyStorekeyStore, InputStream keyStream, String storePass) throws IOException , GeneralSecurityException
Example usage:
KeyStore keyStore = SecurityUtils.getJavaKeyStore();
SecurityUtils.loadKeyStore(keyStore, new FileInputStream("certs.jks"), "password");
keyStore - key store
keyStream - input stream to the key store stream (closed at the end of this method in a finally block)
storePass - password protecting the key store file
IOException
GeneralSecurityException
public static PrivateKeygetPrivateKey(KeyStore keyStore, String alias, String keyPass) throws GeneralSecurityException
keyStore - key store
alias - alias under which the key is stored
keyPass - password protecting the key
GeneralSecurityException
public static PrivateKeyloadPrivateKeyFromKeyStore(KeyStore keyStore, InputStream keyStream, String storePass, String alias, String keyPass) throws IOException , GeneralSecurityException
keyStore - key store
keyStream - input stream to the key store (closed at the end of this method in a finally block)
storePass - password protecting the key store file
alias - alias under which the key is stored
keyPass - password protecting the key
IOException
GeneralSecurityException
public static KeyFactorygetRsaKeyFactory() throws NoSuchAlgorithmException
NoSuchAlgorithmException
public static SignaturegetSha1WithRsaSignatureAlgorithm() throws NoSuchAlgorithmException
NoSuchAlgorithmException
public static SignaturegetSha256WithRsaSignatureAlgorithm() throws NoSuchAlgorithmException
NoSuchAlgorithmException
public static byte[] sign(SignaturesignatureAlgorithm, PrivateKey privateKey, byte[] contentBytes) throws InvalidKeyException , SignatureException
signatureAlgorithm - signature algorithm
privateKey - private key
contentBytes - content to sign
InvalidKeyException
SignatureException
public static boolean verify(SignaturesignatureAlgorithm, PublicKey publicKey, byte[] signatureBytes, byte[] contentBytes) throws InvalidKeyException , SignatureException
signatureAlgorithm - signature algorithm
publicKey - public key
signatureBytes - signature bytes
contentBytes - content bytes
InvalidKeyException
SignatureException
public static X509Certificateverify(Signature signatureAlgorithm, X509TrustManager trustManager, List <String > certChainBase64, byte[] signatureBytes, byte[] contentBytes) throws InvalidKeyException , SignatureException
signatureAlgorithm - signature algorithm
trustManager - trust manager used to verify the certificate chain
certChainBase64 - Certificate chain used for verification. The certificates must be base64 encoded DER, the leaf certificate must be the first element.
signatureBytes - signature bytes
contentBytes - content bytes
InvalidKeyException
SignatureException
public static CertificateFactorygetX509CertificateFactory() throws CertificateException
CertificateException
public static void loadKeyStoreFromCertificates(KeyStorekeyStore, CertificateFactory certificateFactory, InputStream certificateStream) throws GeneralSecurityException
CertificateFactory.generateCertificates(InputStream) .
For each certificate, KeyStore is called with an alias that is the string form of incrementing non-negative integers starting with 0 (0, 1, 2, 3, ...).
Example usage:
KeyStore keyStore = SecurityUtils.getJavaKeyStore();
SecurityUtils.loadKeyStoreFromCertificates(keyStore, SecurityUtils.getX509CertificateFactory(),
new FileInputStream(pemFile));
keyStore - key store (for example
getJavaKeyStore())
certificateFactory - certificate factory (for example
getX509CertificateFactory())
certificateStream - certificate stream
GeneralSecurityException