Search in sources :

Example 1 with CertificateKeyPair

use of org.apache.plc4x.java.opcua.context.CertificateKeyPair in project plc4x by apache.

the class OpcuaConfiguration method openKeyStore.

public void openKeyStore() throws Exception {
    this.isEncrypted = true;
    File securityTempDir = new File(certDirectory, "security");
    if (!securityTempDir.exists() && !securityTempDir.mkdirs()) {
        throw new PlcConnectionException("Unable to create directory please confirm folder permissions on " + certDirectory);
    }
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    File serverKeyStore = securityTempDir.toPath().resolve(keyStoreFile).toFile();
    File pkiDir = FileSystems.getDefault().getPath(certDirectory).resolve("pki").toFile();
    if (!serverKeyStore.exists()) {
        ckp = CertificateGenerator.generateCertificate();
        LOGGER.info("Creating new KeyStore at {}", serverKeyStore);
        keyStore.load(null, keyStorePassword.toCharArray());
        keyStore.setKeyEntry("plc4x-certificate-alias", ckp.getKeyPair().getPrivate(), keyStorePassword.toCharArray(), new X509Certificate[] { ckp.getCertificate() });
        keyStore.store(new FileOutputStream(serverKeyStore), keyStorePassword.toCharArray());
    } else {
        LOGGER.info("Loading KeyStore at {}", serverKeyStore);
        keyStore.load(new FileInputStream(serverKeyStore), keyStorePassword.toCharArray());
        String alias = keyStore.aliases().nextElement();
        KeyPair kp = new KeyPair(keyStore.getCertificate(alias).getPublicKey(), (PrivateKey) keyStore.getKey(alias, keyStorePassword.toCharArray()));
        ckp = new CertificateKeyPair(kp, (X509Certificate) keyStore.getCertificate(alias));
    }
}
Also used : CertificateKeyPair(org.apache.plc4x.java.opcua.context.CertificateKeyPair) CertificateKeyPair(org.apache.plc4x.java.opcua.context.CertificateKeyPair) FileOutputStream(java.io.FileOutputStream) PascalByteString(org.apache.plc4x.java.opcua.readwrite.PascalByteString) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) File(java.io.File) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 X509Certificate (java.security.cert.X509Certificate)1 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)1 CertificateKeyPair (org.apache.plc4x.java.opcua.context.CertificateKeyPair)1 PascalByteString (org.apache.plc4x.java.opcua.readwrite.PascalByteString)1