package jdk.incubator.jpackage.internal;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.StandardCopyOption;
import java.nio.file.Files;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public final class MacCertificate {
private final String certificate;
public MacCertificate(String certificate) {
this.certificate = certificate;
}
public boolean isValid() {
return verifyCertificate(this.certificate);
}
private static File findCertificate(String certificate) {
File result = null;
List<String> args = new ArrayList<>();
args.add("security");
args.add("find-certificate");
args.add("-c");
args.add(certificate);
args.add("-a");
args.add("-p");
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos)) {
ProcessBuilder security = new ProcessBuilder(args);
IOUtils.exec(security, false, ps);
File output = File.createTempFile("tempfile", ".tmp");
Files.copy(new ByteArrayInputStream(baos.toByteArray()),
output.toPath(), StandardCopyOption.REPLACE_EXISTING);
result = output;
}
catch (IOException ignored) {}
return result;
}
private static Date findCertificateDate(String filename) {
Date result = null;
List<String> args = new ArrayList<>();
args.add("/usr/bin/openssl");
args.add("x509");
args.add("-noout");
args.add("-enddate");
args.add("-in");
args.add(filename);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos)) {
ProcessBuilder security = new ProcessBuilder(args);
IOUtils.exec(security, false, ps);
String output = baos.toString();
output = output.substring(output.indexOf("=") + 1);
DateFormat df = new SimpleDateFormat(
"MMM dd kk:mm:ss yyyy z", Locale.ENGLISH);
result = df.parse(output);
} catch (IOException | ParseException ex) {
Log.verbose(ex);
}
return result;
}
private static boolean verifyCertificate(String certificate) {
boolean result = false;
try {
File file = null;
Date certificateDate = null;
try {
file = findCertificate(certificate);
if (file != null) {
certificateDate = findCertificateDate(
file.getCanonicalPath());
}
}
finally {
if (file != null) {
file.delete();
}
}
if (certificateDate != null) {
Calendar c = Calendar.getInstance();
Date today = c.getTime();
if (certificateDate.after(today)) {
result = true;
}
}
}
catch (IOException ignored) {}
return result;
}
}