public class CertificateUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_KEY_SIZE |
| Constructor and Description |
|---|
CertificateUtils() |
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
base64Decode(String string)
Decodes a String having Base64 encoding to an array of bytes.
|
static String |
base64Encode(byte[] bytes)
Base64 encodes a byte array to string.
|
static KeyPair |
createApplicationInstanceCertificate(String commonName,
String organisation,
String applicationUri,
int validityTime,
int keySize,
String... hostNames)
createApplicationInstanceCertificate.
|
static KeyPair |
createApplicationInstanceCertificate(String commonName,
String organisation,
String applicationUri,
int validityTime,
KeyPair issuerKeys,
int keySize,
String... hostNames)
createApplicationInstanceCertificate.
|
static KeyPair |
createApplicationInstanceCertificate(String commonName,
String organisation,
String applicationUri,
int validityTime,
KeyPair issuerKeys,
String... hostNames)
Deprecated.
this method depends on static state; instead call
createApplicationInstanceCertificate(String, String, String, int, com.prosysopc.ua.stack.transport.security.KeyPair, int, String...). |
static KeyPair |
createApplicationInstanceCertificate(String commonName,
String organisation,
String applicationUri,
int validityTime,
String... hostNames)
Deprecated.
this method depends on static state, call
createApplicationInstanceCertificate(String, String, String, int, int, String...)
instead. |
static KeyPair |
createHttpsCertificate(String hostName,
String applicationUri,
int days,
KeyPair issuerCert)
Deprecated.
this method depends on static state. Call
createHttpsCertificate(String, String, int, com.prosysopc.ua.stack.transport.security.KeyPair, int)
instead. |
static KeyPair |
createHttpsCertificate(String hostName,
String applicationUri,
int days,
KeyPair issuerCert,
int keySize)
Create a new certificate that can be used with the HTTPS protocol.
|
static KeyPair |
createIssuerCertificate(String commonName,
int days,
KeyPair issuerCert)
Deprecated.
this method depends on static state. Call
createIssuerCertificate(String, int, com.prosysopc.ua.stack.transport.security.KeyPair, int)
instead. |
static KeyPair |
createIssuerCertificate(String commonName,
int days,
KeyPair issuerCert,
int keySize)
Create a new issuer certificate that can be used to issue certificates built with
createApplicationInstanceCertificate(String, String, String, int, String...) or
createHttpsCertificate(String, String, int, com.prosysopc.ua.stack.transport.security.KeyPair). |
static byte[] |
createThumbprint(byte[] data)
Create SHA-1 Thumbprint.
|
static RSAPrivateKey |
decodePKCS8EncodedRSAPrivateKey(ByteString encoded)
Decodes a PKCS8 encoded key to a
RSAPrivateKey object. |
static X509Certificate |
decodeX509Certificate(byte[] encodedCertificate)
Decodes X509 Certificate from the given encoded certificate data.
|
static String |
getApplicationUriOfCertificate(Cert certificate)
getApplicationUriOfCertificate.
|
static String |
getApplicationUriOfCertificate(X509Certificate certificate)
getApplicationUriOfCertificate.
|
static CertificateProvider |
getCertificateProvider()
Getter for the field
certificateProvider. |
static String |
getCertificateSignatureAlgorithm()
Getter for the field
certificateSignatureAlgorithm. |
static List<String> |
getDnsOfCertificate(Cert certificate)
Returns the DNS names (in lower case and punycode resolved) of the certificate as defined in
the SubjectAlternativeName field.
|
static List<String> |
getDnsOfCertificate(X509Certificate certificate)
Returns the DNS names (in lower case and punycode resolved) of the certificate as defined in
the SubjectAlternativeName field.
|
static List<String> |
getIpOfCertificate(Cert certificate)
getIpOfCertificate.
|
static List<String> |
getIpOfCertificate(X509Certificate certificate)
getIpOfCertificate.
|
static int |
getKeySize()
Deprecated.
this method depends on static state; instead call methods that take key size as
parameter.
|
protected static Collection<List<?>> |
getSubjectAlternativeNames(X509Certificate cert)
Returns the raw SAN fields of the given certificate.
|
static RSAPrivateKey |
loadFromKeyStore(URL keystoreUrl,
String password)
Load private key from a key store.
|
static KeyPair |
loadKeyPairFromProtectedStore(String storeLocation,
String alias,
String storePW,
String privatePW)
Load a KeyPair from a Java Key Store.
|
static PrivateKey |
readPrivateKey(File file,
String password,
String algorithm)
Read a private key from a PEM encoded file with a password.
|
static PrivateKey |
readPrivateKey(InputStream stream,
String password,
String algorithm)
Read a private key from a PEM encoded stream with a password.
|
static X509Certificate |
readX509Certificate(File file)
Load X.509 Certificate from a file
|
static X509Certificate |
readX509Certificate(URL url)
Load X.509 Certificate from an url
|
static KeyPair |
renewApplicationInstanceCertificate(String commonName,
String organisation,
String applicationUri,
int validityTime,
KeyPair oldKeys,
KeyPair issuerKeys,
String... hostNames)
Renew a certificate KeyPair using the old keys.
|
static KeyPair |
renewApplicationInstanceCertificate(String commonName,
String organisation,
String applicationUri,
int validityTime,
KeyPair oldKeys,
String... hostNames)
Renew a certificate KeyPair.
|
static boolean |
saveKeyPairToProtectedStore(KeyPair keyPairToSave,
String storeLocation,
String alias,
String storePW,
String privatePW)
Save the KeyPair to a Java Key Store.
|
static void |
saveToProtectedStore(PrivateKey privateKey,
Certificate certificate,
File storeLocation,
String alias,
String privateKeyPassword,
String keyStorePassword,
String keyStoreType)
Save the private key to a jks or pfx (PKCS12)-keystore.
|
static void |
setCertificateProvider(CertificateProvider certificateProvider)
Define the preferred CertificateProvider.
|
static void |
setCertificateSignatureAlgorithm(String certificateSignatureAlgorithm)
Define the algorithm to use for certificate signatures.
|
static void |
setKeySize(int keySize)
Deprecated.
this method depends on static state; instead call methods that take key size as
parameter.
|
static SignatureData |
sign(PrivateKey signerKey,
SecurityAlgorithm algorithm,
byte[] dataToSign)
Deprecated.
|
static KeyPair |
toKeyPair(X509Certificate cert,
PrivateKey privateKey)
toKeyPair.
|
static boolean |
verify(X509Certificate certificate,
SecurityAlgorithm algorithm,
byte[] data,
byte[] signature)
Deprecated.
|
static void |
writePrivateKey(PrivateKey key,
File file,
String password,
String algorithm)
Write a private key to a stream in PEM format with a password.
|
static void |
writePrivateKey(PrivateKey key,
OutputStream stream,
String password,
String algorithm)
Write a private key to a PEM file with a password.
|
static void |
writeToPem(PrivateKey key,
File file,
String password,
String algorithm)
writeToPem.
|
public static final int DEFAULT_KEY_SIZE
public static byte[] base64Decode(String string)
public static String base64Encode(byte[] bytes)
public static KeyPair createApplicationInstanceCertificate(String commonName, String organisation, String applicationUri, int validityTime, KeyPair issuerKeys, int keySize, String... hostNames) throws IOException, GeneralSecurityException
createApplicationInstanceCertificate.
commonName - - Common Name (CN) for the generated certificateorganisation - - Organisation (O) for the generated certificateapplicationUri - - Alternative name (one of x509 extensiontype) for generated certificate.
Must not be nullvalidityTime - - the time that the certificate is valid (in days)issuerKeys - the optional issuer certificate and private key to use for signing the
certificate. If null a self-signed certificate is generatedkeySize - - the key sizehostNames - - alternate host names or IP addresses to add to SubjectAlternativeNamesKeyPair object.IOException - if any.GeneralSecurityException - if any.@Deprecated public static KeyPair createApplicationInstanceCertificate(String commonName, String organisation, String applicationUri, int validityTime, KeyPair issuerKeys, String... hostNames) throws IOException, GeneralSecurityException
createApplicationInstanceCertificate(String, String, String, int, com.prosysopc.ua.stack.transport.security.KeyPair, int, String...).createApplicationInstanceCertificate.
commonName - - Common Name (CN) for the generated certificateorganisation - - Organisation (O) for the generated certificateapplicationUri - - Alternative name (one of x509 extensiontype) for generated certificate.
Must not be nullvalidityTime - - the time that the certificate is valid (in days)issuerKeys - the optional issuer certificate and private key to use for signing the
certificate. If null a self-signed certificate is generatedhostNames - - alternate host names or IP addresses to add to SubjectAlternativeNamesKeyPair object.IOException - if any.GeneralSecurityException - if any.public static KeyPair createApplicationInstanceCertificate(String commonName, String organisation, String applicationUri, int validityTime, int keySize, String... hostNames) throws IOException, GeneralSecurityException
createApplicationInstanceCertificate.
commonName - - Common Name (CN) for generated certificateorganisation - - Organisation (O) for generated certificateapplicationUri - - Alternative name (one of x509 extensiontype) for generated certificate.
Must not be nullvalidityTime - - the time that the certificate is valid (in days)keySize - - the key sizehostNames - - alternate host names or IP addresses to add to SubjectAlternativeNamesKeyPair object.IOException - if any.GeneralSecurityException - if any.@Deprecated public static KeyPair createApplicationInstanceCertificate(String commonName, String organisation, String applicationUri, int validityTime, String... hostNames) throws IOException, GeneralSecurityException
createApplicationInstanceCertificate(String, String, String, int, int, String...)
instead.createApplicationInstanceCertificate.
commonName - - Common Name (CN) for generated certificateorganisation - - Organisation (O) for generated certificateapplicationUri - - Alternative name (one of x509 extensiontype) for generated certificate.
Must not be nullvalidityTime - - the time that the certificate is valid (in days)hostNames - - alternate host names or IP addresses to add to SubjectAlternativeNamesKeyPair object.IOException - if any.GeneralSecurityException - if any.@Deprecated public static KeyPair createHttpsCertificate(String hostName, String applicationUri, int days, KeyPair issuerCert) throws IOException, GeneralSecurityException
createHttpsCertificate(String, String, int, com.prosysopc.ua.stack.transport.security.KeyPair, int)
instead.hostName - - HostName of the computer in which the application is running: used to
initialize the Subject field of the certificate. The client applications may validate
this field of the server certificate, so it should match the hostName used in the
ApplicationUri of the application.applicationUri - - The ApplicationUri corresponding to the respective field of the
ApplicationDescription of the application for which the certificate is createddays - - the time that the certificate is valid (in days)issuerCert - - The certificate of the issuer that should sign the certificate. If null, a
self-signed certificate is createdIOException - if any.GeneralSecurityException - if any.public static KeyPair createHttpsCertificate(String hostName, String applicationUri, int days, KeyPair issuerCert, int keySize) throws IOException, GeneralSecurityException
hostName - - HostName of the computer in which the application is running: used to
initialize the Subject field of the certificate. The client applications may validate
this field of the server certificate, so it should match the hostName used in the
ApplicationUri of the application.applicationUri - - The ApplicationUri corresponding to the respective field of the
ApplicationDescription of the application for which the certificate is createddays - - the time that the certificate is valid (in days)issuerCert - - The certificate of the issuer that should sign the certificate. If null, a
self-signed certificate is createdkeySize - - The key sizeIOException - if any.GeneralSecurityException - if any.@Deprecated public static KeyPair createIssuerCertificate(String commonName, int days, KeyPair issuerCert) throws GeneralSecurityException, IOException
createIssuerCertificate(String, int, com.prosysopc.ua.stack.transport.security.KeyPair, int)
instead.createApplicationInstanceCertificate(String, String, String, int, String...) or
createHttpsCertificate(String, String, int, com.prosysopc.ua.stack.transport.security.KeyPair).commonName - The common name to use for the Subject of the certificate (the name will be
prepended with "CN=" if it does not start with it already)days - - the time that the certificate is valid (in days)issuerCert - - The certificate of the issuer that should sign the certificate. If null, a
self-signed certificate is createdGeneralSecurityException - if any.IOException - if any.public static KeyPair createIssuerCertificate(String commonName, int days, KeyPair issuerCert, int keySize) throws GeneralSecurityException, IOException
createApplicationInstanceCertificate(String, String, String, int, String...) or
createHttpsCertificate(String, String, int, com.prosysopc.ua.stack.transport.security.KeyPair).commonName - The common name to use for the Subject of the certificate (the name will be
prepended with "CN=" if it does not start with it already)days - - the time that the certificate is valid (in days)issuerCert - - The certificate of the issuer that should sign the certificate. If null, a
self-signed certificate is createdkeySize - - the key sizeGeneralSecurityException - if any.IOException - if any.public static byte[] createThumbprint(byte[] data)
data - an array of byte.public static RSAPrivateKey decodePKCS8EncodedRSAPrivateKey(ByteString encoded) throws Exception
RSAPrivateKey object.Exception - on failure.public static X509Certificate decodeX509Certificate(byte[] encodedCertificate) throws CertificateException
CertificateException on error or if the given data doesn't represent a x509 certificate
(null/empty data is also treated like this).CertificateExceptionpublic static String getApplicationUriOfCertificate(Cert certificate) throws CertificateParsingException
getApplicationUriOfCertificate.
certificate - a Cert object.String object.CertificateParsingException - if any.public static String getApplicationUriOfCertificate(X509Certificate certificate) throws CertificateParsingException
getApplicationUriOfCertificate.
certificate - a X509Certificate object.String object.CertificateParsingException - if any.public static CertificateProvider getCertificateProvider()
Getter for the field certificateProvider.
CertificateProvider object.public static String getCertificateSignatureAlgorithm()
Getter for the field certificateSignatureAlgorithm.
String object.public static List<String> getDnsOfCertificate(Cert certificate) throws CertificateParsingException
CertificateParsingException if the given
certificate cannot be parsed.CertificateParsingExceptionpublic static List<String> getDnsOfCertificate(X509Certificate certificate) throws CertificateParsingException
CertificateParsingException if the given
certificate cannot be parsed.CertificateParsingExceptionpublic static List<String> getIpOfCertificate(Cert certificate) throws CertificateParsingException
getIpOfCertificate.
certificate - a Cert object.List object.CertificateParsingException - if any.public static List<String> getIpOfCertificate(X509Certificate certificate) throws CertificateParsingException
getIpOfCertificate.
certificate - a X509Certificate object.List object.CertificateParsingException - if any.@Deprecated public static int getKeySize()
Getter for the field keySize.
public static RSAPrivateKey loadFromKeyStore(URL keystoreUrl, String password) throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException, UnrecoverableKeyException
keystoreUrl - url to key storepassword - password to key storeIOException - if any.CertificateException - if any.NoSuchAlgorithmException - if any.KeyStoreException - if any.UnrecoverableKeyException - if any.public static KeyPair loadKeyPairFromProtectedStore(String storeLocation, String alias, String storePW, String privatePW) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException
storeLocation - a String object.alias - a String object.storePW - a String object.privatePW - a String object.KeyPair object.KeyStoreException - if any.IOException - if any.NoSuchAlgorithmException - if any.CertificateException - if any.UnrecoverableKeyException - if any.public static PrivateKey readPrivateKey(File file, String password, String algorithm) throws IOException
Read a private key from a PEM encoded file with a password.
IOException - if any.public static PrivateKey readPrivateKey(InputStream stream, String password, String algorithm) throws IOException
Read a private key from a PEM encoded stream with a password.
IOException - if any.public static X509Certificate readX509Certificate(File file) throws IOException, CertificateException
file - a File object.IOException - if any.CertificateException - In case the certificate is not validpublic static X509Certificate readX509Certificate(URL url) throws IOException, CertificateException
url - a URL object.IOException - if any.CertificateException - In case the certificate is not validpublic static KeyPair renewApplicationInstanceCertificate(String commonName, String organisation, String applicationUri, int validityTime, KeyPair oldKeys, KeyPair issuerKeys, String... hostNames) throws IOException, IllegalStateException, GeneralSecurityException
commonName - - Common Name (CN) for generated certificateorganisation - - Organisation (O) for generated certificateapplicationUri - - Alternative name (one of x509 extensiontype) for generated certificate.
Must not be nullvalidityTime - - the time that the certificate is valid (in days)oldKeys - the old keys to renewissuerKeys - the optional issuer certificate and private key to use for signing the
certificatehostNames - a String object.KeyPair object.IOException - if any.IllegalStateException - if any.GeneralSecurityException - if any.public static KeyPair renewApplicationInstanceCertificate(String commonName, String organisation, String applicationUri, int validityTime, KeyPair oldKeys, String... hostNames) throws IOException, IllegalStateException, GeneralSecurityException
commonName - - Common Name (CN) for generated certificateorganisation - - Organisation (O) for generated certificateapplicationUri - - Alternative name (one of x509 extensiontype) for generated certificate.
Must not be nullvalidityTime - - the time that the certificate is valid (in days)oldKeys - the old keys to renewhostNames - a String object.KeyPair object.IOException - if any.IllegalStateException - if any.GeneralSecurityException - if any.public static boolean saveKeyPairToProtectedStore(KeyPair keyPairToSave, String storeLocation, String alias, String storePW, String privatePW) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException
keyPairToSave - a KeyPair object.storeLocation - a String object.alias - a String object.storePW - a String object.privatePW - a String object.KeyStoreException - if any.IOException - if any.NoSuchAlgorithmException - if any.CertificateException - if any.public static void saveToProtectedStore(PrivateKey privateKey, Certificate certificate, File storeLocation, String alias, String privateKeyPassword, String keyStorePassword, String keyStoreType) throws IOException, KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException
IOException - if storeLocation is not availableNoSuchProviderException - The required security Provider not foundKeyStoreException - keystore failedCertificateException - certificate problemNoSuchAlgorithmException - cryptographic algorithm not foundpublic static void setCertificateProvider(CertificateProvider certificateProvider)
certificateProvider - the certificateProvider to setpublic static void setCertificateSignatureAlgorithm(String certificateSignatureAlgorithm)
The OPC UA specification defines that the algorithm should be (at least) "SHA1WithRSA" for application instance certificates used for security policies Basic128Rsa15 and Basic256. For Basic256Sha256 it should be "SHA256WithRSA".
Default: "SHA256WithRSA"
certificateSignatureAlgorithm - the certificateSignatureAlgorithm to set@Deprecated public static void setKeySize(int keySize)
Default: 2048
keySize - size of the certificates. Good values are multiples of 1024,2048(,3072) and 4096IllegalArgumentException - if the value is not accepted@Deprecated public static SignatureData sign(PrivateKey signerKey, SecurityAlgorithm algorithm, byte[] dataToSign) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException
CryptoUtil.signAsymm(PrivateKey, SecurityAlgorithm, byte[]) instead.signerKey - a PrivateKey object.algorithm - asymmetric signer algorithm, See SecurityAlgorithmdataToSign - an array of byte.NoSuchAlgorithmException - if any.SignatureException - if any.InvalidKeyException - if any.public static KeyPair toKeyPair(X509Certificate cert, PrivateKey privateKey) throws CertificateEncodingException
toKeyPair.
cert - a X509Certificate object.privateKey - a PrivateKey object.KeyPair object.CertificateEncodingException - if any.@Deprecated public static boolean verify(X509Certificate certificate, SecurityAlgorithm algorithm, byte[] data, byte[] signature) throws SignatureException, InvalidKeyException, NoSuchAlgorithmException
CryptoUtil.verifyAsymm(X509Certificate, SecurityAlgorithm, byte[], byte[])
instead.certificate - a X509Certificate object.algorithm - asymmetric signer algorithm, See SecurityAlgorithmdata - an array of byte.signature - an array of byte.SignatureException - if any.InvalidKeyException - if any.NoSuchAlgorithmException - if any.public static void writePrivateKey(PrivateKey key, File file, String password, String algorithm) throws IOException
Write a private key to a stream in PEM format with a password.
key - the private key to savefile - the file to save topassword - the password o use for protecting the keyalgorithm - encryption algorithm to use. For example "AES-128-CBC"IOException - if any.public static void writePrivateKey(PrivateKey key, OutputStream stream, String password, String algorithm) throws IOException
Write a private key to a PEM file with a password.
key - the private key to savestream - the stream (for example FileOutputStream) to save topassword - the password o use for protecting the keyalgorithm - encryption algorithm to use. For example "AES-128-CBC"IOException - if any.public static void writeToPem(PrivateKey key, File file, String password, String algorithm) throws IOException
writeToPem.
key - a X509Certificate object.file - a File object.IOException - if any.protected static Collection<List<?>> getSubjectAlternativeNames(X509Certificate cert) throws CertificateParsingException
CertificateParsingExceptionCopyright © 2026. All rights reserved.