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 Deprecated 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 java.lang.StringencodeSkiAsString(org.bouncycastle.asn1.x509.SubjectKeyIdentifier ski)returns the hex string that represents the SKI valuestatic org.bouncycastle.asn1.x509.SubjectKeyIdentifiergenerateSki(java.security.PublicKey publicKey)CertificateInfogetCert()org.bouncycastle.asn1.x509.SubjectKeyIdentifiergetOwnSki()java.lang.StringgetOwnSkiAsStr()static java.util.Map<java.lang.String,SkiManagementInfo>getTrustedSkis()static booleanisValidSki(java.lang.String ski)checks if a given string represents a valid SKI.static booleanremoveTrustedSki(java.lang.String ski)removes a ski from the trustedSkis mapstatic voidsetTrustedSkiAuthenticated(java.lang.String ski)voidstoreSymKeyInKeyStore(java.lang.String alias)Deprecated.
-
-
-
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)
-
encodeSkiAsString
public static java.lang.String encodeSkiAsString(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()
-
storeSymKeyInKeyStore
@Deprecated public void storeSymKeyInKeyStore(java.lang.String alias) throws java.security.NoSuchProviderException, java.security.KeyStoreException, java.security.cert.CertificateException, java.security.NoSuchAlgorithmException, java.io.IOExceptionDeprecated.We are not sure what the symmetric key methods are for. Maybe this logic concerns SHIP 1.1.0. As they are used nowhere right now, they may change or be removed in the future.TODO: figure this out.
- 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)
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
-
getOwnSki
public org.bouncycastle.asn1.x509.SubjectKeyIdentifier getOwnSki()
-
getOwnSkiAsStr
public java.lang.String getOwnSkiAsStr()
-
getCert
public CertificateInfo getCert()
-
-