Skip to content

Commit

Permalink
Add missing user input and create executable jar
Browse files Browse the repository at this point in the history
  • Loading branch information
Wakatem committed Jan 10, 2022
1 parent 5366c06 commit 7b03b14
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 31 deletions.
13 changes: 13 additions & 0 deletions .idea/artifacts/SSLCertificateGenerator_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added exported/SSLCertificateGenerator.zip
Binary file not shown.
1 change: 0 additions & 1 deletion src/CSRGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.io.*;
import java.security.*;
import java.security.cert.CertificateEncodingException;
import java.util.Scanner;

public class CSRGenerator {
Expand Down
14 changes: 2 additions & 12 deletions src/CertificateGenerator.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@

import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.pkcs.CertificationRequest;
import org.bouncycastle.asn1.pkcs.CertificationRequestInfo;

import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.X500NameBuilder;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest;

import java.io.*;
import java.math.BigInteger;
import java.security.*;
Expand All @@ -28,7 +19,6 @@
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Date;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -134,7 +124,7 @@ public void exportCertificate(Certificate certificate, File directory, String ce
}


public boolean CertificateIsVerified(Certificate certificate, PublicKey publicKey) {
public boolean CertificateIsVerified(X509Certificate certificate, PublicKey publicKey) {

boolean verified = false;

Expand Down
5 changes: 5 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Main-Class: Main
Class-Path: libs/bcpkix-jdk15to18-170.jar libs/bcprov-jdk15to18-170.jar libs/bcutil-jdk
15to18-170.jar

148 changes: 130 additions & 18 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
Expand All @@ -16,8 +17,17 @@
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

try {
showConsole();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}

System.out.println("SSLCertificateGenerator\n======================\n");
System.out.println("1. Generate self-signed certificate");
System.out.println("2. Generate signed certificate");
Expand All @@ -31,19 +41,18 @@ public static void main(String[] args) {
System.out.print("--> ");
choice = scanner.nextInt();

CertificateGenerator certificateGenerator = new CertificateGenerator();
CSRGenerator csrGenerator = new CSRGenerator();
switch (choice) {
case 1: {
CertificateGenerator generator = new CertificateGenerator();
X509Certificate certificate = generator.generateSelfSignedCert("RSA", 2048, new SecureRandom(), "SHA256WithRSA", 365);
generator.exportCertificate(certificate, new File("."), "test");
X509Certificate certificate = certificateGenerator.generateSelfSignedCert("RSA", 2048, new SecureRandom(), "SHA256WithRSA", 365);
certificateGenerator.exportCertificate(certificate, new File("."), "new certificate");
break;
}

case 2: {
scanner = new Scanner(System.in);
String pathInput;
CSRGenerator csrGenerator = new CSRGenerator();
CertificateGenerator certificateGenerator = new CertificateGenerator();


//fetching the CSR
Expand All @@ -52,14 +61,14 @@ public static void main(String[] args) {
File csrPath = new File(pathInput);
PKCS10CertificationRequest CSR = csrGenerator.importCSR(csrPath);

//fetching the CA certificate
System.out.print("\n\nEnter CA certificate filepath: ");
//fetching the issuer certificate
System.out.print("\n\nEnter issuer certificate filepath: ");
pathInput = scanner.nextLine();
File caCertificatePath = new File(pathInput);
X509Certificate CACertificate = null;
File issuerCertificatePath = new File(pathInput);
X509Certificate issuerCertificate = null;
try {
CertificateFactory factory = CertificateFactory.getInstance("X509");
CACertificate = (X509Certificate) factory.generateCertificate(new FileInputStream(caCertificatePath));
issuerCertificate = (X509Certificate) factory.generateCertificate(new FileInputStream(issuerCertificatePath));

} catch (CertificateException e) {
e.printStackTrace();
Expand All @@ -68,8 +77,8 @@ public static void main(String[] args) {
}


//fetching CA private key
System.out.print("\n\nEnter CA private key filepath: ");
//fetching issuer private key
System.out.print("\n\nEnter issuer private key filepath: ");
pathInput = scanner.nextLine();
File privateKeyPath = new File(pathInput);
PrivateKey privateKey = null;
Expand All @@ -86,23 +95,104 @@ public static void main(String[] args) {
e.printStackTrace();
}

X500Name issuer = new X500Name(CACertificate.getSubjectX500Principal().toString());
X500Name issuer = new X500Name(issuerCertificate.getSubjectX500Principal().toString());
X509Certificate subjectCertificate = certificateGenerator.generateSignedCertificate(CSR, "SHA256WithRSA", new SecureRandom(), issuer, privateKey, 365);
certificateGenerator.exportCertificate(subjectCertificate, new File("."), "finally");
certificateGenerator.exportCertificate(subjectCertificate, new File("."), "new certificate");
break;
}

case 3: {
CSRGenerator csrGenerator = new CSRGenerator();
PKCS10CertificationRequest CSR = csrGenerator.generateCSR("RSA", 2048, new SecureRandom(), "SHA256WithRSA");
csrGenerator.exportCSR(CSR, new File("."), "testCSR");
csrGenerator.exportCSR(CSR, new File("."), "new CSR");
break;
}

case 4:
scanner = new Scanner(System.in);
String pathInput;

//fetching the subject certificate
System.out.print("\n\nEnter subject certificate filepath: ");
pathInput = scanner.nextLine();
File subjectCertificatePath = new File(pathInput);
X509Certificate subjectCertificate = null;
try {
CertificateFactory factory = CertificateFactory.getInstance("X509");
subjectCertificate = (X509Certificate) factory.generateCertificate(new FileInputStream(subjectCertificatePath));

} catch (CertificateException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

System.out.print("\n\n1. Validate using extracted public key from a certificate");
System.out.print("\n2. Validate using given public key");
System.out.print("\n--> ");
choice = scanner.nextInt();

if (choice == 1) {

//fetching the issuer certificate
System.out.print("\n\nEnter issuer certificate filepath: ");
pathInput = scanner.next();
File issuerCertificatePath = new File(pathInput);
X509Certificate issuerCertificate = null;
PublicKey publicKey = null;
try {
CertificateFactory factory = CertificateFactory.getInstance("X509");
issuerCertificate = (X509Certificate) factory.generateCertificate(new FileInputStream(issuerCertificatePath));
publicKey = issuerCertificate.getPublicKey();
} catch (CertificateException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}


boolean verified = certificateGenerator.CertificateIsVerified(subjectCertificate, publicKey);
if (verified)
System.out.println("certificate is verified");

} else if (choice == 2) {

//fetching the issuer public key
System.out.print("\n\nEnter issuer public key: ");
pathInput = scanner.next();
File publicKeyPath = new File(pathInput);
PublicKey publicKey = null;
try {
FileInputStream fis = new FileInputStream(publicKeyPath);
publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(fis.readAllBytes()));

} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

boolean verified = certificateGenerator.CertificateIsVerified(subjectCertificate, publicKey);
if (verified)
System.out.println("certificate is verified");

} else {
System.out.println("wrong input");
}

break;

case 5: {
CSRGenerator csrGenerator = new CSRGenerator();
PKCS10CertificationRequest request = csrGenerator.importCSR(new File("testCSR.csr"));

//fetching the CSR
System.out.print("\n\nEnter CSR filepath: ");
pathInput = scanner.nextLine();
File csrPath = new File(pathInput);
PKCS10CertificationRequest request = csrGenerator.importCSR(csrPath);

try {
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(request.getSubjectPublicKeyInfo().getEncoded()));
boolean verified = csrGenerator.CSRisVerified(request, publicKey);
Expand All @@ -120,10 +210,32 @@ public static void main(String[] args) {
break;
}

default:
System.out.println("wrong input");
break;

}


System.out.println("enter any character to exit...");
scanner.nextLine();

}


public static void showConsole() throws IOException, URISyntaxException {

//if current process has no window
if (System.console() == null) {

String jarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
String jarName = jarPath.substring(jarPath.lastIndexOf("/")+1);

Runtime.getRuntime().exec("cmd /c start java -jar "+jarName);

return; //System.exit(); or return; to terminate current console-less process
}

}

}

0 comments on commit 7b03b14

Please sign in to comment.