Class SecurityUtils
java.lang.Object
com.inductiveautomation.ignition.common.util.SecurityUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
asymmetricSign
(RSAPrivateKey key, byte[] source, int offset, int length) Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3static boolean
asymmetricVerify
(RSAPublicKey key, byte[] source, int offset, int length, byte[] signature, int sigOff, int sigLen) Verifies that the signature is valid based on the provided key and returns true if the it is valid, false if it is not.static boolean
certificateIsCa
(X509Certificate certificate) Check ifcertificate
's BasicConstraints and KeyUsage extensions indicate it is a CA.static boolean
Verifies that the given cert is signed with its own public key and that the subject and issuer are the same.static String
certificateThumbprintToString
(byte[] buf) Takes a sha1 digest byte array and returns a formatted string hash of the encoded bytes for comparison purposes.static byte[]
decryptDESede
(byte[] toDecrypt, byte[] keyBytes) Performs symmetric decryption using DESede.static byte[]
decryptRSA
(byte[] source, Key key) Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3static byte[]
encryptDESede
(byte[] toEncrypt, byte[] keyBytes) Performs symmetric encryption using DESede.static byte[]
Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3static String
getCertificateThumbprint
(X509Certificate certificate) Returns a to-stringed hash of the certificate encoded bytes.static byte[]
getCertificateThumbprintBytes
(X509Certificate certificate) Returns a hash of the certificate encoded bytes.static File
Grabs the default trust store set on using the 'javax.net.ssl.trustStore' property or the ${JAVA_HOME}/lib /security/cacerts file.static File
Grabs the default trust store set on using the 'javax.net.ssl.trustStore' property with '.bak' appended or the ${JAVA_HOME}/lib/security/cacerts.bak file.static org.python.bouncycastle.crypto.params.RSAKeyParameters
getKeyParameter
(RSAKey key) Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3static org.python.bouncycastle.crypto.params.RSAKeyParameters
Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3static org.python.bouncycastle.crypto.params.RSAKeyParameters
Returns anRSAKeyParameters
object suitable for initializing a Bouncy Castle RSAEngine asymmetric block cipher when given anRSAPublicKey
.static byte[]
Performs an md5 digest of a provided file and returns a byte array of the digest.static byte[]
md5
(InputStream stream) Performs an md5 digest of a providedInputStream
and returns a byte array of the digest.static List<X509Certificate>
parseX509Certificates
(byte[] input) Generates a List ofX509Certificate
objects and initializes them with the data read from the byte array input.static Map<String,
X509Certificate> parseX509Certificates
(File certificateDirectory) Generates a Map of filenames andX509Certificate
objects and initializes them with the data read from theFileInputStream
inputStreams of each file in the supplied directory.static List<X509Certificate>
parseX509Certificates
(InputStream inputStream) Generates a List ofX509Certificate
objects and initializes them with the data read from theInputStream
inputStream.static byte[]
sha1
(byte[] input) Performs a sha1 digest on the given input array, returning the digest as a byte array.static String
sha1String
(String input) Performs a sha1 digest on the input string encoded as UTF-8, returns the digest as Base64-ed bytes.static String
Performs a sha256 encryption on the provided String and returns a String representation of the encrypted data.static String
sha256PasswordSalt
(String password) Returns a string containing the salted passwordstatic boolean
sha256PasswordSaltVerify
(String password, String hashedPass) Verifies the salted password by comparing with the unsalted password and returning true if the password matches the salted version.
-
Method Details
-
getKeyParameter
@Deprecated(forRemoval=true) public static org.python.bouncycastle.crypto.params.RSAKeyParameters getKeyParameter(RSAKey key) Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3Returns a parameter object suitable for initializing a Bouncy Castle RSAEngine asymmetric block cipher. Either anRSAPublicKey
orRSAPrivateKey
can be passed and the subsequentRSAKeyParameters
will be returned.- Parameters:
key
- an RSAKey of typeRSAPublicKey
orRSAPrivateKey
, must not be null- Returns:
- an
RSAKeyParameters
object suitable for initializing a public or private Bouncy Castle RSAEngine asymmetric block cipher based on the type ofRSAKey
provided - Throws:
ClassCastException
- if the key is not anRSAPublicKey
orRSAPrivateKey
-
getPublicKeyParameter
public static org.python.bouncycastle.crypto.params.RSAKeyParameters getPublicKeyParameter(RSAPublicKey key) Returns anRSAKeyParameters
object suitable for initializing a Bouncy Castle RSAEngine asymmetric block cipher when given anRSAPublicKey
.- Parameters:
key
- anRSAPublicKey
object, must not be null- Returns:
- an
RSAKeyParameters
object suitable for initializing a Bouncy Castle RSAEngine asymmetric block cipher
-
getPrivateKeyParameter
@Deprecated(forRemoval=true) public static org.python.bouncycastle.crypto.params.RSAKeyParameters getPrivateKeyParameter(RSAPrivateKey key) Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3Returns aRSAKeyParameters
object suitable for initializing a Bouncy Castle RSAEngine asymmetric block cipher when given anRSAPrivateKey
. If the passed key is anRSAPrivateCrtKey
it will return anRSAPrivateCrtKeyParameter
.- Parameters:
key
- anRSAPrivateKey
object, must not be null- Returns:
- an
RSAKeyParameter
object suitable for initializing a Bouncy Castle RSAEngine asymmetric block cipher. If the passed key is anRSAPrivateCrtKey
it will return anRSAPrivateCrtKeyParameter
-
sha1
public static byte[] sha1(byte[] input) Performs a sha1 digest on the given input array, returning the digest as a byte array.- Parameters:
input
- a byte array of the input needing the digest, must not be null- Returns:
- a byte array of the SHA1 digest
-
sha1String
Performs a sha1 digest on the input string encoded as UTF-8, returns the digest as Base64-ed bytes.- Parameters:
input
- a UTF-8 encoded String, must not be null- Returns:
- the digest in a String of Base64 Encoded bytes
- Throws:
RuntimeException
- if the passed String cannot be encoded due to String format issues
-
md5
Performs an md5 digest of a provided file and returns a byte array of the digest.- Parameters:
file
-File
needed to be digested, must not be null- Returns:
- an md5 digest in a
byte
array - Throws:
IOException
- if theFile
cannot be accessed or found
-
md5
Performs an md5 digest of a providedInputStream
and returns a byte array of the digest.- Parameters:
stream
- theInputStream
that needs to be digested, must not be null- Returns:
- the digest in a byte array
- Throws:
IOException
- if theInputStream
cannot be accessed
-
parseX509Certificates
Generates a List ofX509Certificate
objects and initializes them with the data read from the byte array input. The certificate provided in the byte array containing DER encoded binary or PEM encoded ASCII. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by -----END CERTIFICATE-----.- Parameters:
input
- abyte
array containing DER encoded binary or PEM encoded ASCII, must not be null and must be properly formed- Returns:
- a List of
X509Certificate
objects - Throws:
CertificateException
- If the data in the byte array does not contain an inherent end-of-certificate marker (other than EOF) and there is trailing data after the certificate is parsed, a CertificateException is thrown.
-
parseX509Certificates
public static List<X509Certificate> parseX509Certificates(InputStream inputStream) throws CertificateException Generates a List ofX509Certificate
objects and initializes them with the data read from theInputStream
inputStream. the certificate provided in the byte array containing DER encoded binary or PEM encoded ASCII. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by -----END CERTIFICATE-----.- Parameters:
inputStream
- anInputStream
containing DER encoded binary or PEM encoded ASCII, must not be null and must be properly formed- Returns:
- a List of
X509Certificate
objects - Throws:
CertificateException
- If the data in the stream does not contain an inherent end-of-certificate marker (other than EOF) and there is trailing data after the certificate is parsed, a CertificateException is thrown.
-
parseX509Certificates
Generates a Map of filenames andX509Certificate
objects and initializes them with the data read from theFileInputStream
inputStreams of each file in the supplied directory. Nested directories are not searched. The certificate provided in the byte array containing DER encoded binary or PEM encoded ASCII. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by -----END CERTIFICATE-----.- Parameters:
certificateDirectory
- aFile
containing DER encoded binary or PEM encoded ASCII certificate files, must not be null and must be a directory.- Returns:
- a Map of filenames to
X509Certificate
objects. If anIOExecption
orCertificateException
is thrown while reading the file null will be returned for the certificate in the map.
-
asymmetricSign
@Deprecated(forRemoval=true) public static byte[] asymmetricSign(RSAPrivateKey key, byte[] source, int offset, int length) throws Exception Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3Takes anRSAPrivateKey
, byte array to be signed, an offset, and length to sign asymmetrically using SHA1 and returns a byte array containing the signature.- Parameters:
key
- anRSAPrivateKey
that will be used to sign the byte array, must not be nullsource
- the byte array used to generate the signature, must not be nulloffset
- the specified offset, must not be nulllength
- the specified length, must not be null- Returns:
- a byte array containing the signature
- Throws:
Exception
- if unable to generate a signature
-
asymmetricVerify
public static boolean asymmetricVerify(RSAPublicKey key, byte[] source, int offset, int length, byte[] signature, int sigOff, int sigLen) Verifies that the signature is valid based on the provided key and returns true if the it is valid, false if it is not.- Parameters:
key
- an used to verify the signature, must not be nullsource
- a byte array of the source data, must not be nulloffset
- the offset of the source data, must not be nulllength
- the length of the source data, must not be nullsignature
- a byte array of the signature, must not be nullsigOff
- the signatures offset, must not be nullsigLen
- the signature length, must not be null- Returns:
- true if the signature can be verified against the provided data, false if it cannot
-
decryptRSA
@Deprecated(forRemoval=true) public static byte[] decryptRSA(byte[] source, Key key) throws Exception Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3Decrypts a byte array and RSA encrypted data using the supplied key. Note: Does not perform any data validation.- Parameters:
source
- a byte array containing the data to decrypt, must not be nullkey
- theKey
to use for decryption, must not be null- Returns:
- a byte array containing the decrypted data, must not be null
- Throws:
Exception
- if theKey
is invalid, the block size is incorrect, or the padding is bad
-
encryptDESede
Performs symmetric encryption using DESede. Uses ECB mode and PKCS5 padding.- Parameters:
toEncrypt
- A byte array to encrypt. Must not be nullkeyBytes
- The key to use. must be 24 bytes long- Returns:
- a byte array containing the encrypted data
- Throws:
Exception
- if the key is invalid, the block size is incorrect, or the padding is bad
-
decryptDESede
Performs symmetric decryption using DESede. Uses ECB mode and PKCS5 padding.- Parameters:
toDecrypt
- a byte[] to decrypt. Must not be nullkeyBytes
- The key to use. must be 24 bytes long- Returns:
- a byte array containing the decrypted data
- Throws:
Exception
- if the key is invalid, the block size is incorrect, or the padding is bad
-
generateDESedKey
Deprecated, for removal: This API element is subject to removal in a future version.Deprecated for removal in 8.3Generates a DESede Key- Returns:
- a byte array containing the DESede key
- Throws:
Exception
- if key generation fails
-
getCertificateThumbprint
Returns a to-stringed hash of the certificate encoded bytes. Each certificate's hash is unique, and so certificates can be compared using their hashes to check equality.- Parameters:
certificate
- theX509Certificate
whose thumbprint is needed, must not be null- Returns:
- a stringed hash of the certificate
- Throws:
Exception
- if the certificate is invalid or the String cannot be built
-
certificateThumbprintToString
Takes a sha1 digest byte array and returns a formatted string hash of the encoded bytes for comparison purposes.- Parameters:
buf
- a byte array containing a sha1 digest, must not be null- Returns:
- a Stringed hash of the byte array. If buf is null, "[ ]=null" will be returned.
- Throws:
Exception
- if the byte array cannot be formatted
-
getCertificateThumbprintBytes
public static byte[] getCertificateThumbprintBytes(X509Certificate certificate) throws CertificateEncodingException Returns a hash of the certificate encoded bytes. Each certificate's hash is unique, and so certificates can be compared using their hashes to check equality.- Parameters:
certificate
- theX509Certificate
to be hashed, must not be null- Returns:
- a byte array containing the hash of the certificate
- Throws:
CertificateEncodingException
- if the certificate cannot be encoded
-
sha256
Performs a sha256 encryption on the provided String and returns a String representation of the encrypted data. The String should be encoded as UTF-8.- Parameters:
source
- the String to be encrypted, if null the String "" will be used- Returns:
- a String of encrypted data
-
sha256PasswordSalt
Returns a string containing the salted password- Parameters:
password
- the password to be salted, must not be null- Returns:
- a String containing the salted password
-
sha256PasswordSaltVerify
Verifies the salted password by comparing with the unsalted password and returning true if the password matches the salted version.- Parameters:
password
- the unsalted password to compare against the salted passwordhashedPass
- the salted password, must not be null- Returns:
- true if the password matches the hashed password thus confirming the identity and false if the password does not match.
-
getDefaultTrustStore
Grabs the default trust store set on using the 'javax.net.ssl.trustStore' property or the ${JAVA_HOME}/lib /security/cacerts file. -
getDefaultTrustStoreBackup
Grabs the default trust store set on using the 'javax.net.ssl.trustStore' property with '.bak' appended or the ${JAVA_HOME}/lib/security/cacerts.bak file. -
certificateIsSelfSigned
Verifies that the given cert is signed with its own public key and that the subject and issuer are the same.- Returns:
true
if a givenX509Certificate
is self-signed.- Throws:
Exception
-
certificateIsCa
Check if
certificate
's BasicConstraints and KeyUsage extensions indicate it is a CA.Following is the truth table for this method:
Basic Constraints Key Usage Result Missing Missing true Missing keyCertSign set true Missing keyCertSign unset false CA flag set Missing true CA flag set keyCertSign set true CA flag set keyCertSign unset false CA flag unset Missing false CA flag unset keyCertSign set false CA flag unset keyCertSign unset false - Parameters:
certificate
- theX509Certificate
to check.- Returns:
true
ifcertificate
's BasicConstraints and KeyUsage extensions indicate it is a CA.
-