// Path: keystore-service/src/main/java/com/stnetix/ariaddna/keystore/exceptions/KeyStoreException.java
// public class KeyStoreException extends AriaddnaException {
//     public KeyStoreException(Throwable cause) {
//         super(cause);
//     }
// 
//     public KeyStoreException(String traceMessage) {
//         super(traceMessage);
//     }
// 
//     public KeyStoreException(String traceMessage, String errorMessage) {
//         super(traceMessage, errorMessage);
//     }
// 
//     public KeyStoreException(String traceMessage, Throwable cause) {
//         super(traceMessage, cause);
//     }
// 
//     public KeyStoreException(String traceMessage, String errorMessage, Throwable cause) {
//         super(traceMessage, errorMessage, cause);
//     }
// }


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.util.Date;
import sun.security.x509.AlgorithmId;
import sun.security.x509.CertificateAlgorithmId;
import sun.security.x509.CertificateSerialNumber;
import sun.security.x509.CertificateValidity;
import sun.security.x509.CertificateVersion;
import sun.security.x509.CertificateX509Key;
import sun.security.x509.X500Name;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;
import com.stnetix.ariaddna.commonutils.dto.CertificateDTO;
import com.stnetix.ariaddna.commonutils.logger.AriaddnaLogger;
import com.stnetix.ariaddna.keystore.exceptions.KeyStoreException;
        CRYPTO_ALGORITHM_RSA = "RSA";
        CRYPTO_ALGORITHM_SHA1RSA = "SHA1withRSA";
        CERTIFICATE_SIZE = 1024;
        SUBJECT_CN = "CN=";
        SUBJECT_L_C = "L=Russia, C=RU";

    }

    public File getNewCertificate(String alias) throws KeyStoreException {
        KeyPairGenerator keyPairGenerator = null;
        try {
            keyPairGenerator = KeyPairGenerator.getInstance(CRYPTO_ALGORITHM_RSA);
            keyPairGenerator.initialize(CERTIFICATE_SIZE);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();

            PrivateKey privateKey = keyPair.getPrivate();
            X509CertInfo certInfo = new X509CertInfo();
            CertificateValidity interval = new CertificateValidity(FROM, TO);
            BigInteger sn = new BigInteger(64, new SecureRandom());
            X500Name owner = new X500Name(SUBJECT_CN + alias + ", " + SUBJECT_L_C);

            certInfo.set(X509CertInfo.VALIDITY, interval);
            certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
            certInfo.set(X509CertInfo.SUBJECT, owner);
            certInfo.set(X509CertInfo.ISSUER, owner);
            certInfo.set(X509CertInfo.KEY, new CertificateX509Key(keyPair.getPublic()));
            certInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
            AlgorithmId algorithm = new AlgorithmId(AlgorithmId.md2WithRSAEncryption_oid);
            certInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algorithm));

// Path: keystore-service/src/main/java/com/stnetix/ariaddna/keystore/exceptions/KeyStoreException.java
// public class KeyStoreException extends AriaddnaException {
//     public KeyStoreException(Throwable cause) {
//         super(cause);
//     }
// 
//     public KeyStoreException(String traceMessage) {
//         super(traceMessage);
//     }
// 
//     public KeyStoreException(String traceMessage, String errorMessage) {
//         super(traceMessage, errorMessage);
//     }
// 
//     public KeyStoreException(String traceMessage, Throwable cause) {
//         super(traceMessage, cause);
//     }
// 
//     public KeyStoreException(String traceMessage, String errorMessage, Throwable cause) {
//         super(traceMessage, errorMessage, cause);
//     }
// }


// Path: keystore-service/src/main/java/com/stnetix/ariaddna/keystore/utils/CertFactory.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.util.Date;
import sun.security.x509.AlgorithmId;
import sun.security.x509.CertificateAlgorithmId;
import sun.security.x509.CertificateSerialNumber;
import sun.security.x509.CertificateValidity;
import sun.security.x509.CertificateVersion;
import sun.security.x509.CertificateX509Key;
import sun.security.x509.X500Name;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;
import com.stnetix.ariaddna.commonutils.dto.CertificateDTO;
import com.stnetix.ariaddna.commonutils.logger.AriaddnaLogger;
import com.stnetix.ariaddna.keystore.exceptions.KeyStoreException;

        CRYPTO_ALGORITHM_RSA = "RSA";
        CRYPTO_ALGORITHM_SHA1RSA = "SHA1withRSA";
        CERTIFICATE_SIZE = 1024;
        SUBJECT_CN = "CN=";
        SUBJECT_L_C = "L=Russia, C=RU";

    }

    public File getNewCertificate(String alias) throws KeyStoreException {
        KeyPairGenerator keyPairGenerator = null;
        try {
            keyPairGenerator = KeyPairGenerator.getInstance(CRYPTO_ALGORITHM_RSA);
            keyPairGenerator.initialize(CERTIFICATE_SIZE);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();

            PrivateKey privateKey = keyPair.getPrivate();
            X509CertInfo certInfo = new X509CertInfo();
            CertificateValidity interval = new CertificateValidity(FROM, TO);
            BigInteger sn = new BigInteger(64, new SecureRandom());
            X500Name owner = new X500Name(SUBJECT_CN + alias + ", " + SUBJECT_L_C);

            certInfo.set(X509CertInfo.VALIDITY, interval);
            certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
            certInfo.set(X509CertInfo.SUBJECT, owner);
            certInfo.set(X509CertInfo.ISSUER, owner);
            certInfo.set(X509CertInfo.KEY, new CertificateX509Key(keyPair.getPublic()));
            certInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
            AlgorithmId algorithm = new AlgorithmId(AlgorithmId.md2WithRSAEncryption_oid);
            certInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algorithm));

            X509CertImpl cert = new X509CertImpl(certInfo);