All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.customware.license.support.util.LicenseUtils Maven / Gradle / Ivy

The newest version!
package net.customware.license.support.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.security.auth.x500.X500Principal;

public final class LicenseUtils {

    private LicenseUtils() {
    }

    /**
     * Formats the X500Principal value to a more human-readable format.
     * 
     * @param principal
     *            The principal to format.
     * @return the formatted principal.
     */
    public static String formatX500Principal( X500Principal principal ) {
        String principalValue = principal.toString();
        principalValue = principalValue.replaceAll( ",?\\s*DC\\=[^,]+", "" );
        principalValue = principalValue.replaceAll( "[A-Z]+\\=", "\n" ).trim();
        principalValue = principalValue.replaceAll( "\n", "
" ); return principalValue; } /** * Creates a private key and stores it in the specified keystore. * * @param alias * The alias for the private key. * @param x500Name * The X500 name details. * @param in * The input stream containing the existing keystore. May be * null if a new keystore should be created. * @param out * The output stream where the keystore should be written to. May * not be null. * @param password * The password for both the keystore and the private key. * @return The public certificate for the new private key. * * @throws KeyStoreException * If there is a problem accessing the keystore. * @throws NoSuchAlgorithmException * If the required algorithms are not available. * @throws CertificateException * If there was a problem creating the certificate. * @throws InvalidKeyException * If the key is invalid. * @throws SignatureException * If there was a problem creating the signature. * @throws NoSuchProviderException * If the required provider is not available. * @throws IOException * If there was an I/O problem. */ public static Certificate createCertificate( String alias, String keyPassword, sun.security.x509.X500Name x500Name, InputStream in, OutputStream out, String storePassword ) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, InvalidKeyException, SignatureException, NoSuchProviderException, IOException { KeyStore keyStore = loadKeyStore( in, storePassword ); Certificate cert = createCertificate( alias, keyPassword, x500Name, keyStore ); saveKeyStore( keyStore, storePassword, out ); return cert; } public static KeyStore loadKeyStore( InputStream in, String storePassword ) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { KeyStore keyStore = KeyStore.getInstance( "JKS" ); keyStore.load( in, storePassword.toCharArray() ); return keyStore; } public static Certificate createCertificate( String alias, String keyPassword, sun.security.x509.X500Name x500Name, KeyStore keyStore ) throws NoSuchAlgorithmException, InvalidKeyException, CertificateException, SignatureException, NoSuchProviderException, KeyStoreException { sun.security.x509.CertAndKeyGen keypair = new sun.security.x509.CertAndKeyGen( "DSA", "SHA1WithDSA" ); keypair.generate( 1024 ); PrivateKey privKey = keypair.getPrivateKey(); X509Certificate[] chain = new X509Certificate[1]; chain[0] = keypair.getSelfCertificate( x500Name, 7000 * 24 * 60 * 60 ); keyStore.setKeyEntry( alias, privKey, keyPassword.toCharArray(), chain ); Certificate cert = keyStore.getCertificate( alias ); return cert; } public static void saveKeyStore( KeyStore keyStore, String storePassword, OutputStream out ) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { keyStore.store( out, storePassword.toCharArray() ); } /** * Imports the specified Certificate into the keystore. If in * is null, a new keystore will be created. * * @param alias * The alias for the certificate. * @param certificate * The certificate to add. * @param in * The input stream for the existing store. May be * null to create a new store. * @param out * The output stream to save the store to. May not be * null. * @param storePassword * The store password. * @throws KeyStoreException * If there was a problem with the keystore. * @throws NoSuchAlgorithmException * If the encryption/signing algorithm required is missing. * @throws CertificateException * If there is a problem with the certificate. * @throws IOException * If there is a problem loading or saving the keystore. */ public static void importCertificate( String alias, Certificate certificate, InputStream in, OutputStream out, String storePassword ) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { KeyStore keyStore = loadKeyStore( in, storePassword ); importCertificate( alias, certificate, keyStore ); saveKeyStore( keyStore, storePassword, out ); } public static void importCertificate( String alias, Certificate certificate, KeyStore keyStore ) throws KeyStoreException { keyStore.setCertificateEntry( alias, certificate ); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy