Merge branch 'java_xml_timestamp' into issue-493

This commit is contained in:
chubtub 2022-12-19 18:32:40 -05:00
commit 6a84bb50c5
5 changed files with 55 additions and 18 deletions

View File

@ -80,14 +80,15 @@ public class Main {
gateway.setRimEventLog(rimEventLog);
}
String filename = commander.getRfc3852Filename();
if (!filename.isEmpty() && commander.isRfc3339()) {
if (!filename.isEmpty() && commander.getRfc3339() != null) {
System.out.println("Only one timestamp format can be specified");
System.exit(1);
} else if (!filename.isEmpty()) {
gateway.setTimestampFormat("RFC3852");
gateway.setRfc3852Filename(filename);
} else if (commander.isRfc3339()) {
gateway.setTimestampArgument(filename);
} else if (commander.getRfc3339() != null) {
gateway.setTimestampFormat("RFC3339");
gateway.setTimestampArgument(commander.getRfc3339());
}
gateway.generateSwidTag(commander.getOutFile());
break;

View File

@ -90,7 +90,7 @@ public class SwidTagGateway {
private boolean embeddedCert;
private String rimEventLog;
private String timestampFormat;
private String rfc3852Filename;
private String timestampArgument;
private String errorRequiredFields;
/**
@ -106,7 +106,7 @@ public class SwidTagGateway {
embeddedCert = false;
rimEventLog = "";
timestampFormat = "";
rfc3852Filename = "";
timestampArgument = "";
errorRequiredFields = "";
} catch (JAXBException e) {
System.out.println("Error initializing jaxbcontext: " + e.getMessage());
@ -186,11 +186,11 @@ public class SwidTagGateway {
}
/**
* Setter for RFC3852 file path
* @param rfc3852Filename
* Setter for timestamp input - RFC3852 + file or RFC3339 + value
* @param timestampArgument
*/
public void setRfc3852Filename(String rfc3852Filename) {
this.rfc3852Filename = rfc3852Filename;
public void setTimestampArgument(String timestampArgument) {
this.timestampArgument = timestampArgument;
}
/**
@ -656,7 +656,7 @@ public class SwidTagGateway {
case "RFC3852":
try {
byte[] counterSignature = Base64.getEncoder().encode(
Files.readAllBytes(Paths.get(rfc3852Filename)));
Files.readAllBytes(Paths.get(timestampArgument)));
timeStampElement.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:" + SwidTagConstants.RFC3852_PFX,
SwidTagConstants.RFC3852_NS);
@ -671,8 +671,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

@ -50,7 +50,10 @@ public class Commander {
private String rfc3852Filename = "";
@Parameter(names = {"--rfc3339"}, order = 11,
description = "Add a timestamp to the signature that is compliant with RFC3339.")
private boolean rfc3339 = false;
private boolean rfc3852 = false;
@Parameter(names = {"--rfc3339"}, order = 11, validateWith = Rfc3339Format.class,
description = "Add a timestamp to the signature that is compliant with RFC3339.")
private String rfc3339 = "";
public boolean isHelp() {
return help;
@ -90,7 +93,9 @@ public class Commander {
public String getRfc3852Filename() { return rfc3852Filename; }
public boolean isRfc3339() { return rfc3339; }
public boolean isRfc3852() { return rfc3852; }
public String getRfc3339() { return rfc3339; }
public String printHelpExamples() {
StringBuilder sb = new StringBuilder();
@ -135,10 +140,12 @@ public class Commander {
sb.append("Event log support RIM: " + this.getRimEventLog() + System.lineSeparator());
if (!this.getRfc3852Filename().isEmpty()) {
sb.append("Timestamp format: RFC3852, " + this.getRfc3852Filename());
} else if (this.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 specified");
sb.append("No timestamp included");
}
return sb.toString();
}

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;
}
}
}

View File

@ -6,7 +6,7 @@ validate : validate a signed base rim's signature (NOTE: cryptographic validatio
# Build and package
- Install Visual Studio
- The recommended project name is "xml_dsig_tool" so that the resulting executable file will be appropriately named xml_dsig_tool.exe.
- The recommended project name is "xml_dsig_tool" so that the resulting executable file will be appropriately named xml_dsig_tool.exe.
- Install NuGet packages:
- System.CommandLine.2.0.0-beta4 (check "Include Prerelease" next to search bar)
- System.Security.Cryptography.X509Certificates