Java tutorial
/* * Copyright (c) 2017 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you 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 org.wso2.carbon.identity.test.common.testng.utils; import org.apache.commons.codec.binary.Base64; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.StringReader; import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PrivateKey; import java.security.SecureRandom; import java.security.SignatureException; import java.security.cert.CertificateException; import java.security.spec.PKCS8EncodedKeySpec; public class ReadCertStoreSampleUtil { private final static String PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" + "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAM7t8Ub1DP+B91NJ\n" + "nC45zqIvd1QXkQ5Ac1EJl8mUglWFzUyFbhjSuF4mEjrcecwERfRummASbLoyeMXl\n" + "eiPg7jvSaz2szpuV+afoUo9c1T+ORNUzq31NvM7IW6+4KhtttwbMq4wbbPpBfVXA\n" + "IAhvnLnCp/VyY/npkkjAid4c7RoVAgMBAAECgYBcCuy6kj+g20+G5YQp756g95oN\n" + "dpoYC8T/c9PnXz6GCgkik2tAcWJ+xlJviihG/lObgSL7vtZMEC02YXdtxBxTBNmd\n" + "upkruOkL0ElIu4S8CUwD6It8oNnHFGcIhwXUbdpSCr1cx62A0jDcMVgneQ8vv6vB\n" + "/YKlj2dD2SBq3aaCYQJBAOvc5NDyfrdMYYTY+jJBaj82JLtQ/6K1vFIwdxM0siRF\n" + "UYqSRA7G8A4ga+GobTewgeN6URFwWKvWY8EGb3HTwFkCQQDgmKtjjJlX3BotgnGD\n" + "gdxVgvfYG39BL2GnotSwUbjjce/yZBtrbcClfqrrOWWw7lPcX1d0v8o3hJfLF5dT\n" + "6NAdAkA8qAQYUCSSUwxJM9u0DOqb8vqjSYNUftQ9dsVIpSai+UitEEx8WGDn4SKd\n" + "V8kupy/gJlau22uSVYI148fJSCGRAkBz+GEHFiJX657YwPI8JWHQBcBUJl6fGggi\n" + "t0F7ibceOkbbsjU2U4WV7sHyk8Cei3Fh6RkPf7i60gxPIe9RtHVBAkAnPQD+BmND\n" + "By8q5f0Kwtxgo2+YkxGDP5bxDV6P1vd2C7U5/XxaN53Kc0G8zu9UlcwhZcQ5BljH\n" + "N24cUWZOo+60\n" + "-----END PRIVATE KEY-----"; private static final int keysize = 1024; private static final String commonName = "www.test.de"; private static final String organizationalUnit = "IT"; private static final String organization = "test"; private static final String city = "test"; private static final String state = "test"; private static final String country = "DE"; private static final long validity = 1096; // 3 years private static final String alias = "wso2"; private static final char[] keyPass = "changeit".toCharArray(); public static PrivateKey getSamplePrivateKey() throws Exception { // Read in the key into a String StringBuilder pkcs8Lines = new StringBuilder(); BufferedReader rdr = new BufferedReader(new StringReader(PRIVATE_KEY)); String line; while ((line = rdr.readLine()) != null) { pkcs8Lines.append(line); } // Remove the "BEGIN" and "END" lines, as well as any whitespace String pkcs8Pem = pkcs8Lines.toString(); pkcs8Pem = pkcs8Pem.replace("-----BEGIN PRIVATE KEY-----", ""); pkcs8Pem = pkcs8Pem.replace("-----END PRIVATE KEY-----", ""); pkcs8Pem = pkcs8Pem.replaceAll("\\s+", ""); // Base64 decode the result byte[] pkcs8EncodedBytes = Base64.decodeBase64(pkcs8Pem); // extract the private key PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(keySpec); } public static KeyStore createKeyStore(Class clazz) throws Exception { clazz.getResource(""); File file = new File(clazz.getResource("/repository/resources/security/wso2carbon.jks").getFile()); KeyStore keyStore = KeyStore.getInstance("JKS"); if (file.exists()) { // if exists, load keyStore.load(new FileInputStream(file), "wso2carbon".toCharArray()); } else { // if not exists, create keyStore.load(null, null); keyStore.store(new FileOutputStream(file), "wso2carbon".toCharArray()); } return keyStore; } public static KeyPair getSampleKeyPair() throws CertificateException, NoSuchAlgorithmException, IOException, InvalidKeyException, KeyStoreException, NoSuchProviderException, SignatureException { KeyStore keyStore = KeyStore.getInstance("JKS"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "SHA1WithRSA"); SecureRandom random = SecureRandom.getInstance("RSA", "SHA1WithRSA"); keyGen.initialize(1024, random); KeyPair keypair = keyGen.generateKeyPair(); return keypair; } }