Package org.openmuc.jeebus.ship.node
Class KeyManagement
- java.lang.Object
-
- org.openmuc.jeebus.ship.node.KeyManagement
-
public class KeyManagement extends java.lang.Objecteverything related to key management and key encryption
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.slf4j.Loggerlog
-
Constructor Summary
Constructors Constructor Description KeyManagement(java.lang.String pathToKeyStore, java.lang.String alias, char[] keyStorePassphrase, char[] keyPairPassphrase, java.lang.String distinguishedName, int days)Creates a new key store, key pair and certificate and stores it in the specified path.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidaddBCProvider()adds provider only if it's not already in the JVMstatic voidaddTrustedSki(java.lang.String ski, java.lang.Integer trustLevel)adds the given SKI to the trusted list if it was not added already.static voidclearTrustedSkis()CertificateInfocreateCertificate(java.security.KeyPair keyPair, java.lang.String distinguishedName, int days, CertificateInfo issuer)generates a self-signed X.509 Certificatestatic org.bouncycastle.asn1.x509.SubjectKeyIdentifiergenerateSki(java.security.PublicKey publicKey)CertificateInfogetCert()java.security.KeyPairgetKeyPair()char[]getKeyPairPass()char[]getKeyStorePass()java.security.KeyStoregetKs()org.bouncycastle.asn1.x509.SubjectKeyIdentifiergetOwnSki()java.lang.StringgetOwnSkiAsStr()java.lang.StringgetPathToKeyStoreFile()static java.util.Map<java.lang.String,SkiManagementInfo>getTrustedSkis()static booleanisValidSki(java.lang.String ski)checks if a given string represents a valid SKI.voidloadKeyStore()static java.lang.StringparseSkiToString(org.bouncycastle.asn1.x509.SubjectKeyIdentifier ski)returns the hex string that represents the SKI valuestatic booleanremoveTrustedSki(java.lang.String ski)removes a ski from the trustedSkis mapvoidsetCert(CertificateInfo cert)voidsetKeyPair(java.security.KeyPair keyPair)voidsetKeyPairPass(char[] keyPairPass)voidsetKeyStorePass(char[] keyStorePass)voidsetKs(java.security.KeyStore ks)voidsetPathToKeyStoreFile(java.lang.String pathToKeyStore)static voidsetTrustedSkiAuthenticated(java.lang.String ski)voidstoreKeyPairInKeyStore(java.lang.String alias)voidstoreSymKeyInKeyStore(java.lang.String alias)
-
-
-
Constructor Detail
-
KeyManagement
public KeyManagement(java.lang.String pathToKeyStore, java.lang.String alias, char[] keyStorePassphrase, char[] keyPairPassphrase, java.lang.String distinguishedName, int days) throws java.security.KeyStoreException, java.security.NoSuchProviderException, java.io.IOException, java.security.cert.CertificateException, java.security.NoSuchAlgorithmException, java.security.UnrecoverableKeyExceptionCreates a new key store, key pair and certificate and stores it in the specified path. Should the path already contain a key store with a valid key pair and certificate, then the key store will be loaded instead.- Parameters:
pathToKeyStore- path where the key store exists or should be created, a temporary key store will be created and will not be stored should the path benullalias- the alias for either the existing key pair or for the key pair to be createdkeyStorePassphrase- passphrase for the key storekeyPairPassphrase- passphrase for the key pair to be generateddistinguishedName- X.509 Distinguished Name, eg "CN=Test, L=London, C=GB". For IoT devices, usually the DeviceIDdays- how many days the certificate should be valid for- Throws:
java.security.KeyStoreException- if a KeyStoreSpi implementation for the specified type is not available from the specified providerjava.security.NoSuchProviderException- if the specified provider is not registered in the security provider listjava.io.IOException- if there is an I/O or format problem with the keystore data, if a password is required but not given, or if the given password was incorrectjava.security.cert.CertificateException- if any of the certificates in the keystore could not be loadedjava.security.NoSuchAlgorithmException- if the appropriate data integrity algorithm could not be foundjava.security.UnrecoverableKeyException- if the key cannot be recovered (e.g., the given password is wrong).
-
-
Method Detail
-
generateSki
public static org.bouncycastle.asn1.x509.SubjectKeyIdentifier generateSki(java.security.PublicKey publicKey)
-
parseSkiToString
public static java.lang.String parseSkiToString(org.bouncycastle.asn1.x509.SubjectKeyIdentifier ski)
returns the hex string that represents the SKI value- Parameters:
ski- SubjectKeyIdentifier value- Returns:
- SKI value as hex string in lower case
-
isValidSki
public static boolean isValidSki(java.lang.String ski)
checks if a given string represents a valid SKI. Note that this method removes whitespaces prior to checking- Parameters:
ski- the string to check- Returns:
trueif the string only uses hex digits and has a length of exactly 40
-
addBCProvider
public static void addBCProvider()
adds provider only if it's not already in the JVM
-
addTrustedSki
public static void addTrustedSki(java.lang.String ski, java.lang.Integer trustLevel)adds the given SKI to the trusted list if it was not added already. This method does not override previous entries. For an override remove the previous entry withremoveTrustedSkibefore adding it again with the new trust level.- Parameters:
ski- the SKI to be added to the trusted SKIs listtrustLevel- the trust level of the SKI to be added
-
setTrustedSkiAuthenticated
public static void setTrustedSkiAuthenticated(java.lang.String ski)
-
removeTrustedSki
public static boolean removeTrustedSki(java.lang.String ski)
removes a ski from the trustedSkis map- Parameters:
ski- the ski to remove- Returns:
trueif the map contained the ski, otherwisefalse
-
getTrustedSkis
public static java.util.Map<java.lang.String,SkiManagementInfo> getTrustedSkis()
-
clearTrustedSkis
public static void clearTrustedSkis()
-
loadKeyStore
public void loadKeyStore() throws java.security.KeyStoreException, java.security.NoSuchProviderException, java.security.cert.CertificateException, java.io.IOException, java.security.NoSuchAlgorithmException- Throws:
java.security.KeyStoreExceptionjava.security.NoSuchProviderExceptionjava.security.cert.CertificateExceptionjava.io.IOExceptionjava.security.NoSuchAlgorithmException
-
storeKeyPairInKeyStore
public void storeKeyPairInKeyStore(java.lang.String alias) throws java.security.KeyStoreException- Throws:
java.security.KeyStoreException
-
storeSymKeyInKeyStore
public void storeSymKeyInKeyStore(java.lang.String alias) throws java.security.NoSuchProviderException, java.security.KeyStoreException, java.security.cert.CertificateException, java.security.NoSuchAlgorithmException, java.io.IOException- Throws:
java.security.NoSuchProviderExceptionjava.security.KeyStoreExceptionjava.security.cert.CertificateExceptionjava.security.NoSuchAlgorithmExceptionjava.io.IOException
-
createCertificate
public CertificateInfo createCertificate(java.security.KeyPair keyPair, java.lang.String distinguishedName, int days, CertificateInfo issuer) throws org.bouncycastle.operator.OperatorCreationException, java.security.cert.CertificateException, java.io.IOException
generates a self-signed X.509 Certificate- Parameters:
keyPair- passphrase for the key pair to be generateddistinguishedName- the X.509 Distinguished Name, eg "CN=TEst, L=London, C=GB". For IoT devices, usually the DeviceIDdays- how many days the Certificate is valid forissuer- certificate content, consists of privateKey and X509Certificate- Returns:
- A self-signed certificate
- Throws:
org.bouncycastle.operator.OperatorCreationException- if certificate generation goes wrongorg.bouncycastle.cert.CertIOException- if certificate generation goes wrongjava.security.cert.CertificateException- if certificate generation goes wrongjava.io.IOException
-
getOwnSki
public org.bouncycastle.asn1.x509.SubjectKeyIdentifier getOwnSki()
-
getOwnSkiAsStr
public java.lang.String getOwnSkiAsStr()
-
getKeyPair
public java.security.KeyPair getKeyPair()
-
setKeyPair
public void setKeyPair(java.security.KeyPair keyPair)
-
getCert
public CertificateInfo getCert()
-
setCert
public void setCert(CertificateInfo cert)
-
getPathToKeyStoreFile
public java.lang.String getPathToKeyStoreFile()
-
setPathToKeyStoreFile
public void setPathToKeyStoreFile(java.lang.String pathToKeyStore)
-
getKs
public java.security.KeyStore getKs()
-
setKs
public void setKs(java.security.KeyStore ks)
-
getKeyStorePass
public char[] getKeyStorePass()
-
setKeyStorePass
public void setKeyStorePass(char[] keyStorePass)
-
getKeyPairPass
public char[] getKeyPairPass()
-
setKeyPairPass
public void setKeyPairPass(char[] keyPairPass)
-
-