/* * Copyright 2016 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.sdk.client; 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; public class KeyStoreLoader { private static final String CLIENT_ALIAS = "client-ai"; private static final String SERVER_ALIAS = "server-ai"; private static final char[] PASSWORD = "password".toCharArray(); private X509Certificate clientCertificate; private KeyPair clientKeyPair; private X509Certificate serverCertificate; private KeyPair serverKeyPair; public KeyStoreLoader load() throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(getClass().getClassLoader().getResourceAsStream("example-certs.pfx"), PASSWORD); Key clientPrivateKey = keyStore.getKey(CLIENT_ALIAS, PASSWORD); if (clientPrivateKey instanceof PrivateKey) { clientCertificate = (X509Certificate) keyStore.getCertificate(CLIENT_ALIAS); 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); PublicKey serverPublicKey = serverCertificate.getPublicKey(); serverKeyPair = new KeyPair(serverPublicKey, (PrivateKey) serverPrivateKey); } return this; } public X509Certificate getClientCertificate() { return clientCertificate; } public KeyPair getClientKeyPair() { return clientKeyPair; } public X509Certificate getServerCertificate() { return serverCertificate; } public KeyPair getServerKeyPair() { return serverKeyPair; } }