Skip to content

Commit

Permalink
feat: display certificate information in help
Browse files Browse the repository at this point in the history
  • Loading branch information
lart2150 committed Apr 28, 2024
1 parent 6767fcf commit 7812270
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 29 deletions.
43 changes: 42 additions & 1 deletion src/com/tivo/kmttg/gui/help.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.LinkedHashMap;

import org.apache.hc.client5.http.classic.HttpClient;
Expand All @@ -43,13 +51,37 @@
import javafx.stage.Stage;

import com.tivo.kmttg.main.config;
import com.tivo.kmttg.util.GetKeyStore;
import com.tivo.kmttg.util.debug;
import com.tivo.kmttg.util.log;

public class help {
private static Stage dialog = null;
private static VBox content = null;

static String getKeyExpires() {
GetKeyStore getKeyStore;
try {
getKeyStore = new GetKeyStore(null, config.programDir);
KeyStore keyStore = getKeyStore.getKeyStore();

Enumeration<String> aliases = keyStore.aliases();

while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
X509Certificate crt = (X509Certificate) keyStore.getCertificate(alias);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM d YYYY");
return simpleDateFormat.format(crt.getNotAfter());
}

return "No certs in cstore";
} catch (Exception e) {
System.out.println("Error Loading cert");
System.out.println(e);
return "Error Loading Cert";
}
}

static void showHelp() {
debug.print("");
if (dialog == null) {
Expand Down Expand Up @@ -83,7 +115,16 @@ public void handle(ActionEvent e) {
});
row.getChildren().addAll(lab1, link1);
content.getChildren().add(row);


HBox certRow = new HBox();
certRow.setSpacing(5);
certRow.setAlignment(Pos.CENTER);
certRow.getChildren().addAll(
new Label("Certificate Expires: "),
new Label(help.getKeyExpires())
);
content.getChildren().add(certRow);

final LinkedHashMap<String,String> links = new LinkedHashMap<String,String>();
links.put("kmttg Home Page", "http://sourceforge.net/p/kmttg/wiki/Home");
links.put("kmttg downloads", "http://sourceforge.net/projects/kmttg/files");
Expand Down
35 changes: 7 additions & 28 deletions src/com/tivo/kmttg/rpc/TiVoRPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Random;
import java.util.Scanner;

Expand All @@ -28,6 +29,7 @@
import javax.net.ssl.X509TrustManager;

import com.tivo.kmttg.JSON.JSONObject;
import com.tivo.kmttg.util.GetKeyStore;

/**
* Establish an RPC connection route with a TiVo using the provided cdata files.
Expand Down Expand Up @@ -209,35 +211,12 @@ public X509Certificate[] getAcceptedIssuers () {
private final void createSocketFactory() {
if ( sslSocketFactory == null ) {
try {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
// This is default USA password
String password = "KllX3KygL9"; // expires 1/24/2026
//String password = "vlZaKoduom"; // expires 5/3/2024
InputStream keyInput;
if (cdata == null) {
// Installation dir cdata.p12 file takes priority if it exists
String cdata = programDir + "/cdata.p12";
if ( new File(cdata).isFile() ) {
keyInput = new FileInputStream(cdata);
cdata = programDir + "/cdata.password";
if (new File(cdata).isFile()) {
Scanner s = new Scanner(new File(cdata));
password = s.useDelimiter("\\A").next();
s.close();
} else {
error("cdata.p12 file present, but cdata.password is not");
}
} else {
// Read default USA cdata.p12 from kmttg.jar
keyInput = getClass().getResourceAsStream("/cdata.p12");
}
}
else
keyInput = new FileInputStream(cdata);
keyStore.load(keyInput, password.toCharArray());
keyInput.close();
GetKeyStore getKeyStore = new GetKeyStore(cdata, programDir);
KeyStore keyStore = getKeyStore.getKeyStore();
String keyPassword = getKeyStore.getKeyPassword();

KeyManagerFactory fac = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
fac.init(keyStore, password.toCharArray());
fac.init(keyStore, keyPassword.toCharArray());
SSLContext context = SSLContext.getInstance("TLS");
TrustManager[] tm = new TrustManager[] { new NaiveTrustManager() };
context.init(fac.getKeyManagers(), tm, new SecureRandom());
Expand Down
55 changes: 55 additions & 0 deletions src/com/tivo/kmttg/util/GetKeyStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.tivo.kmttg.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Scanner;

public class GetKeyStore {
private String keyPassword;
private KeyStore keyStore;
public GetKeyStore(String cdata, String programDir)
throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException
{
keyStore = KeyStore.getInstance("PKCS12");
// This is default USA password
keyPassword = "KllX3KygL9"; // expires 1/24/2026
//String password = "vlZaKoduom"; // expires 5/3/2024
InputStream keyInput;
if (cdata == null) {
// Installation dir cdata.p12 file takes priority if it exists
cdata = programDir + "/cdata.p12";
if ( new File(cdata).isFile() ) {
keyInput = new FileInputStream(cdata);
cdata = programDir + "/cdata.password";
if (new File(cdata).isFile()) {
Scanner s = new Scanner(new File(cdata));
keyPassword = s.useDelimiter("\\A").next();
s.close();
} else {
System.out.println("cdata.p12 file present, but cdata.password is not");
}
} else {
// Read default USA cdata.p12 from kmttg.jar
keyInput = getClass().getResourceAsStream("/cdata.p12");
}
}
else
keyInput = new FileInputStream(cdata);
keyStore.load(keyInput, keyPassword.toCharArray());
keyInput.close();
}
public KeyStore getKeyStore() {
return keyStore;
}

public String getKeyPassword() {
return keyPassword;
}

}

0 comments on commit 7812270

Please sign in to comment.