/* * Copyright 2015 Kevin Herron * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.digitalpetri.opcua.stack; import java.security.Key; import java.security.KeyPair; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.X509Certificate; import com.digitalpetri.opcua.stack.core.application.CertificateManager; import com.digitalpetri.opcua.stack.core.application.CertificateValidator; import org.testng.annotations.BeforeTest; public abstract class SecurityFixture { private static final String CLIENT_ALIAS = "client-test-certificate"; private static final String SERVER_ALIAS = "server-test-certificate"; private static final char[] PASSWORD = "test".toCharArray(); protected volatile X509Certificate clientCertificate; protected volatile byte[] clientCertificateBytes; protected volatile KeyPair clientKeyPair; protected volatile X509Certificate serverCertificate; protected volatile byte[] serverCertificateBytes; protected volatile KeyPair serverKeyPair; protected volatile CertificateManager serverCertificateManager; protected volatile CertificateValidator serverCertificateValidator; @BeforeTest public void setUp() throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(getClass().getClassLoader().getResourceAsStream("test-keystore.pfx"), PASSWORD); Key clientPrivateKey = keyStore.getKey(CLIENT_ALIAS, PASSWORD); if (clientPrivateKey instanceof PrivateKey) { clientCertificate = (X509Certificate) keyStore.getCertificate(CLIENT_ALIAS); clientCertificateBytes = clientCertificate.getEncoded(); PublicKey clientPublicKey = clientCertificate.getPublicKey(); clientKeyPair = new KeyPair(clientPublicKey, (PrivateKey) clientPrivateKey); } Key serverPrivateKey = keyStore.getKey(SERVER_ALIAS, PASSWORD); if (serverPrivateKey instanceof PrivateKey) { serverCertificate = (X509Certificate) keyStore.getCertificate(SERVER_ALIAS); serverCertificateBytes = serverCertificate.getEncoded(); PublicKey serverPublicKey = serverCertificate.getPublicKey(); serverKeyPair = new KeyPair(serverPublicKey, (PrivateKey) serverPrivateKey); } serverCertificateManager = new TestCertificateManager( serverKeyPair, serverCertificate ); serverCertificateValidator = new TestCertificateValidator(clientCertificate); } }