Suppoort user input for RFC3339 timestamps

This commit is contained in:
chubtub 2022-12-19 10:39:10 -05:00
parent 59ef355326
commit f74bc49120
4 changed files with 50 additions and 8 deletions

View File

@ -81,8 +81,9 @@ public class Main {
}
if (commander.isRfc3852()) {
gateway.setTimestampFormat("RFC3852");
} else if (commander.isRfc3339()) {
} else if (commander.getRfc3339() != null) {
gateway.setTimestampFormat("RFC3339");
gateway.setTimestampArgument(commander.getRfc3339());
}
gateway.generateSwidTag(commander.getOutFile());
break;

View File

@ -87,6 +87,7 @@ public class SwidTagGateway {
private boolean embeddedCert;
private String rimEventLog;
private String timestampFormat;
private String timestampArgument;
private String errorRequiredFields;
/**
@ -102,6 +103,7 @@ public class SwidTagGateway {
embeddedCert = false;
rimEventLog = "";
timestampFormat = "";
timestampArgument = "";
errorRequiredFields = "";
} catch (JAXBException e) {
System.out.println("Error initializing jaxbcontext: " + e.getMessage());
@ -180,6 +182,14 @@ public class SwidTagGateway {
this.timestampFormat = timestampFormat;
}
/**
* Setter for timestamp input - RFC3852 + file or RFC3339 + value
* @param timestampArgument
*/
public void setTimestampArgument(String timestampArgument) {
this.timestampArgument = timestampArgument;
}
/**
* This method generates a base RIM from the values in a JSON file.
*
@ -649,8 +659,13 @@ public class SwidTagGateway {
timeStampElement.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:" + SwidTagConstants.RFC3339_PFX,
SwidTagConstants.RFC3339_NS);
timeStampElement.setAttribute(SwidTagConstants.DATETIME,
LocalDateTime.now().toString());
if (timestampArgument.isEmpty()) {
timeStampElement.setAttribute(SwidTagConstants.DATETIME,
LocalDateTime.now().toString());
} else {
timeStampElement.setAttribute(SwidTagConstants.DATETIME,
timestampArgument);
}
break;
}
DOMStructure timestampObject = new DOMStructure(timeStampElement);

View File

@ -48,9 +48,9 @@ public class Commander {
@Parameter(names = {"--rfc3852"}, order = 10,
description = "Add a placeholder for a base 64-encoded RFC3852 countersignature.")
private boolean rfc3852 = false;
@Parameter(names = {"--rfc3339"}, order = 11,
@Parameter(names = {"--rfc3339"}, order = 11, validateWith = Rfc3339Format.class,
description = "Add a timestamp to the signature that is compliant with RFC3339.")
private boolean rfc3339 = false;
private String rfc3339 = "";
public boolean isHelp() {
return help;
@ -90,7 +90,7 @@ public class Commander {
public boolean isRfc3852() { return rfc3852; }
public boolean isRfc3339() { return rfc3339; }
public String getRfc3339() { return rfc3339; }
public String printHelpExamples() {
StringBuilder sb = new StringBuilder();
@ -135,8 +135,10 @@ public class Commander {
sb.append("Event log support RIM: " + this.getRimEventLog() + System.lineSeparator());
if (isRfc3852()) {
sb.append("Timestamp format: RFC3852");
} else if (isRfc3339()) {
sb.append("Timestamp format: RFC3339");
} else if (getRfc3339().isEmpty()) {
sb.append("Timestamp format: RFC3339 with generated timestamp");
} else if (!getRfc3339().isEmpty()) {
sb.append("Timestamp format: RFC3339 with timestamp input");
} else {
sb.append("No timestamp included");
}

View File

@ -0,0 +1,24 @@
package hirs.swid.utils;
import com.beust.jcommander.IParameterValidator;
import com.beust.jcommander.ParameterException;
import java.time.Instant;
import java.time.format.DateTimeParseException;
public class Rfc3339Format implements IParameterValidator {
public void validate(String name, String value) throws ParameterException {
if (value != null) {
try {
Instant instant = Instant.parse(value);
} catch (DateTimeParseException e) {
e.printStackTrace();
throw new ParameterException("Parameter " + name + "=" + value +
" is not in valid RFC3339 format; " +
"expected format is yyyy-MM-dd'T'hh:mm:ss'Z'");
}
} else {
return;
}
}
}