Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#874] WIP: Checkstyle changes for tcg_rim_tool #900

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package hirs.swid;

import com.beust.jcommander.JCommander;
import hirs.swid.utils.Commander;
import hirs.swid.utils.TimestampArgumentValidator;
import hirs.utils.rim.ReferenceManifestValidator;
import com.beust.jcommander.JCommander;
import lombok.extern.log4j.Log4j2;

import java.io.File;
Expand All @@ -13,6 +13,7 @@
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Log4j2
public class Main {

Expand All @@ -33,7 +34,7 @@ public static void main(String[] args) {
for (String opt : unknownOpts) {
sb.append(opt + ", ");
}
exitWithErrorCode(sb.substring(0,sb.lastIndexOf(",")));
exitWithErrorCode(sb.substring(0, sb.lastIndexOf(",")));
} else if (commander.isHelp()) {
jc.usage();
System.out.println(commander.printHelpExamples());
Expand Down Expand Up @@ -79,7 +80,7 @@ public static void main(String[] args) {
case "BASE":
gateway.setAttributesFile(attributesFile);
gateway.setRimEventLog(rimEventLog);
if (defaultKey){
if (defaultKey) {
gateway.setDefaultCredentials(true);
gateway.setJksTruststoreFile(SwidTagConstants.DEFAULT_KEYSTORE_FILE);
} else {
Expand Down
11 changes: 5 additions & 6 deletions tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* This class contains the String constants that are referenced by the gateway
* class. It is expected that member properties of this class will expand as
* more functionality is added to SwidTagGateway.
*
*/
public class SwidTagConstants {

Expand Down Expand Up @@ -49,7 +48,7 @@ public class SwidTagConstants {
public static final String ROLE = "role";
public static final String THUMBPRINT = "thumbprint";
public static final String HREF = "href";
public static final String REL = "rel";
public static final String REL = "rel";
public static final String COLLOQUIAL_VERSION = "colloquialVersion";
public static final String EDITION = "edition";
public static final String PRODUCT = "product";
Expand Down Expand Up @@ -78,12 +77,12 @@ public class SwidTagConstants {
public static final String SUPPORT_RIM_FORMAT_MISSING = "supportRIMFormat missing";
public static final String SUPPORT_RIM_URI_GLOBAL = "supportRIMURIGlobal";
public static final String DATETIME = "dateTime";

public static final String NIST_NS = "http://csrc.nist.gov/ns/swid/2015-extensions/1.0";
public static final String TCG_NS = "https://trustedcomputinggroup.org/wp-content/uploads/TCG_RIM_Model";
public static final String TCG_NS = "https://trustedcomputinggroup.org/wp-content/uploads/TCG_RIM_Model";
public static final String RFC3852_NS = "https://www.ietf.org/rfc/rfc3852.txt";
public static final String RFC3339_NS = "https://www.ietf.org/rfc/rfc3339.txt";

public static final String N8060_PFX = "n8060";
public static final String RIM_PFX = "rim";
public static final String RFC3852_PFX = "rcf3852";
Expand Down Expand Up @@ -125,7 +124,7 @@ public class SwidTagConstants {
TCG_NS, PC_URI_LOCAL, RIM_PFX);
public static final QName _PC_URI_GLOBAL = new QName(
TCG_NS, PC_URI_GLOBAL, RIM_PFX);
public static final QName _RIM_LINK_HASH = new QName(
public static final QName _RIM_LINK_HASH = new QName(
TCG_NS, RIM_LINK_HASH, RIM_PFX);
public static final QName _SUPPORT_RIM_TYPE = new QName(
TCG_NS, SUPPORT_RIM_TYPE, RIM_PFX);
Expand Down
10 changes: 8 additions & 2 deletions tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,11 @@ private Document convertToDocument(JAXBElement element) {
/**
* This method signs a SoftwareIdentity with an xmldsig in compatibility mode.
* Current assumptions: digest method SHA256, signature method SHA256, enveloped signature
*
* @param doc The document to sign
* @return Document the signed document
*/
private Document signXMLDocument(Document doc) {
private Document signXMLDocument(final Document doc) {
XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance("DOM");
List xmlObjectList = null;
String signatureId = null;
Expand Down Expand Up @@ -681,7 +684,7 @@ private Document signXMLDocument(Document doc) {
* @param sigFactory the SignatureFactory object
* @return an XMLObject containing the timestamp element
*/
private XMLObject createXmlTimestamp(Document doc, XMLSignatureFactory sigFactory) {
private XMLObject createXmlTimestamp(final Document doc, final XMLSignatureFactory sigFactory) {
Element timeStampElement = null;
switch (timestampFormat.toUpperCase()) {
case "RFC3852":
Expand Down Expand Up @@ -716,6 +719,9 @@ private XMLObject createXmlTimestamp(Document doc, XMLSignatureFactory sigFactor
timestampArgument);
}
break;
default:
System.out.println("A timestamp format must be specified.");
System.exit(1);
}
DOMStructure timestampObject = new DOMStructure(timeStampElement);
SignatureProperty signatureProperty = sigFactory.newSignatureProperty(
Expand Down
88 changes: 50 additions & 38 deletions tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,54 @@
public class Commander {

@Parameter(description = "This parameter catches all unrecognized arguments.")
private List<String> unknownOptions = new ArrayList<>();
@Parameter(names = {"-h", "--help"}, help = true, description = "Print this help text.")
private boolean help;
private final List<String> unknownOptions = new ArrayList<>();
@Parameter(names = {"-c", "--create"}, order = 0,
description = "The type of RIM to create. A base RIM will be created by default.")
private String createType = "";
private final String createType = "";
@Parameter(names = {"-v", "--verify"}, validateWith = FileArgumentValidator.class,
description = "Specify a RIM file to verify.")
private String verifyFile = "";
private final String verifyFile = "";
@Parameter(names = {"-V", "--version"}, description = "Output the current version.")
private boolean version = false;
private final boolean version = false;
@Parameter(names = {"-a", "--attributes"}, validateWith = FileArgumentValidator.class,
description = "The configuration file holding attributes "
+ "to populate the base RIM with. An example file can be found in /opt/rimtool/data.")
private String attributesFile = "";
+ "to populate the base RIM with. An example file can be found in /opt/rimtool/data.")
private final String attributesFile = "";
@Parameter(names = {"-o", "--out"}, order = 2,
description = "The file to write the RIM out to. "
+ "The RIM will be written to stdout by default.")
private String outFile = "";
+ "The RIM will be written to stdout by default.")
private final String outFile = "";
@Parameter(names = {"--verbose"}, description = "Control output verbosity.")
private boolean verbose = false;
private final boolean verbose = false;
@Parameter(names = {"-t", "--truststore"}, validateWith = FileArgumentValidator.class,
description = "The truststore to sign the base RIM created "
+ "or to validate the signed base RIM.")
private String truststoreFile = "";
+ "or to validate the signed base RIM.")
private final String truststoreFile = "";
@Parameter(names = {"-k", "--privateKeyFile"},
validateWith = FileArgumentValidator.class,
description = "The private key used to sign the base RIM created by this tool.")
private String privateKeyFile = "";
private final String privateKeyFile = "";
@Parameter(names = {"-p", "--publicCertificate"},
validateWith = FileArgumentValidator.class,
description = "The public key certificate to embed in the base RIM created by "
+ "this tool.")
private String publicCertificate = "";
+ "this tool.")
private final String publicCertificate = "";
@Parameter(names = {"-e", "--embed-cert"}, order = 7,
description = "Embed the provided certificate in the signed swidtag.")
private boolean embedded = false;
private final boolean embedded = false;
@Parameter(names = {"-d", "--default-key"}, order = 8,
description = "Use the JKS keystore installed in /opt/rimtool/data.")
private boolean defaultKey = false;
private final boolean defaultKey = false;
@Parameter(names = {"-l", "--rimel"}, validateWith = FileArgumentValidator.class,
description = "The TCG eventlog file to use as a support RIM.")
private String rimEventLog = "";
private final String rimEventLog = "";
@Parameter(names = {"--timestamp"}, order = 10, variableArity = true,
description = "Add a timestamp to the signature. " +
"Currently only RFC3339 and RFC3852 are supported:\n" +
"\tRFC3339 [yyyy-MM-ddThh:mm:ssZ]\n\tRFC3852 <counterSignature.bin>")
private List<String> timestampArguments = new ArrayList<String>(2);
private final List<String> timestampArguments = new ArrayList<String>(2);
@Parameter(names = {"-h", "--help"}, help = true, description = "Print this help text.")
private boolean help;

public List<String> getUnknownOptions() {
return unknownOptions;
Expand All @@ -83,7 +83,11 @@ public String getVerifyFile() {
public boolean isVersion() {
return version;
}
public boolean isVerbose() { return verbose; }

public boolean isVerbose() {
return verbose;
}

public String getAttributesFile() {
return attributesFile;
}
Expand All @@ -92,7 +96,9 @@ public String getOutFile() {
return outFile;
}

public String getTruststoreFile() { return truststoreFile; }
public String getTruststoreFile() {
return truststoreFile;
}

public String getPrivateKeyFile() {
return privateKeyFile;
Expand All @@ -102,32 +108,38 @@ public String getPublicCertificate() {
return publicCertificate;
}

public boolean isEmbedded() { return embedded; }
public boolean isEmbedded() {
return embedded;
}

public boolean isDefaultKey() { return defaultKey; }
public boolean isDefaultKey() {
return defaultKey;
}

public String getRimEventLog() { return rimEventLog; }
public String getRimEventLog() {
return rimEventLog;
}

public List<String> getTimestampArguments() {
return timestampArguments;
}

public String printHelpExamples() {
StringBuilder sb = new StringBuilder();
sb.append("Create a base RIM: use the values in attributes.json; ");
sb.append("add support_rim.bin to the payload; ");
sb.append("sign it using privateKey.pem and cert.pem; embed cert.pem in the signature; ");
sb.append("add a RFC3852 timestamp; and write the data to base_rim.swidtag:\n\n");
sb.append("\t\t-c base -a attributes.json -l support_rim.bin "
String sb = "Create a base RIM: use the values in attributes.json; " +
"add support_rim.bin to the payload; " +
"sign it using privateKey.pem and cert.pem; embed cert.pem in the signature; " +
"add a RFC3852 timestamp; and write the data to base_rim.swidtag:\n\n" +
"\t\t-c base -a attributes.json -l support_rim.bin "
+ "-k privateKey.pem -p cert.pem -e --timestamp RFC3852 counterSignature.bin "
+ "-o base_rim.swidtag\n\n\n");
sb.append("Validate base_rim.swidtag: "
+ "-o base_rim.swidtag\n\n\n" +
"Validate base_rim.swidtag: "
+ "the payload <File> is validated with support_rim.bin; "
+ "and the signature is validated with ca.crt:\n\n");
sb.append("\t\t-v base_rim.swidtag -l support_rim.bin -t ca.crt\n\n\n");
+ "and the signature is validated with ca.crt:\n\n" +
"\t\t-v base_rim.swidtag -l support_rim.bin -t ca.crt\n\n\n";

return sb.toString();
return sb;
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Creating: " + this.getCreateType() + System.lineSeparator());
Expand All @@ -137,12 +149,12 @@ public String toString() {
if (!this.getTruststoreFile().isEmpty()) {
sb.append("Truststore file: " + this.getTruststoreFile() + System.lineSeparator());
} else if (!this.getPrivateKeyFile().isEmpty() &&
!this.getPublicCertificate().isEmpty()) {
!this.getPublicCertificate().isEmpty()) {
sb.append("Private key file: " + this.getPrivateKeyFile() + System.lineSeparator());
sb.append("Public certificate: " + this.getPublicCertificate()
+ System.lineSeparator());
sb.append("Embedded certificate: " + this.isEmbedded() + System.lineSeparator());
} else if (this.isDefaultKey()){
} else if (this.isDefaultKey()) {
sb.append("Truststore file: default (" + SwidTagConstants.DEFAULT_KEYSTORE_FILE + ")"
+ System.lineSeparator());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ public class CreateArgumentValidator implements IParametersValidator {

/**
* This method validates the input parameter map.
* @param parameters
* Name-value-pairs of all parameters (e.g. "-host":"localhost").
*
* @param parameters Name-value-pairs of all parameters (e.g. "-host":"localhost").
* @throws ParameterException
*/
@Override
public void validate(Map<String, Object> parameters) throws ParameterException {
if (isValueNotNull(parameters,"--create")) {
if (isValueNotNull(parameters,"--verify")) {
if (isValueNotNull(parameters, "--create")) {
if (isValueNotNull(parameters, "--verify")) {
throw new ParameterException("Create and verify cannot be called together.");
} else {
for (String arg : requiredArgs) {
Expand All @@ -44,17 +43,14 @@ public void validate(Map<String, Object> parameters) throws ParameterException {

/**
* This method checks the given key for a null value
*
* @param parameters map
* @param key the key to check
* @param key the key to check
* @return true if not null, else false
*/
private boolean isValueNotNull(Map<String, Object> parameters, String key) {
Object object = parameters.get(key);
if (object == null) {
return false;
} else {
return true;
}
return object != null;
}

private void validateSigningCredentials(Map<String, Object> parameters) {
Expand All @@ -64,8 +60,8 @@ private void validateSigningCredentials(Map<String, Object> parameters) {
errorMessage += "Too many signing credentials given, either choose --default-key OR " +
"provide --privateKeyFile and --publicCertificate";
} else if (!isValueNotNull(parameters, "--default-key") &&
!isValueNotNull(parameters, "--privateKeyFile") &&
!isValueNotNull(parameters, "--publicCertificate")) {
!isValueNotNull(parameters, "--privateKeyFile") &&
!isValueNotNull(parameters, "--publicCertificate")) {
errorMessage += "No signing credentials given, either choose --default-key OR " +
"provide --privateKeyFile and --publicCertificate";
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package hirs.swid.utils;

public class CredentialArgumentValidator {
private String truststoreFile;
private String certificateFile;
private String privateKeyFile;
private static final String PEM = "PEM";
private final String truststoreFile;
private final String certificateFile;
private final String privateKeyFile;
private final boolean isValidating;
private String format;
private boolean isValidating;
private String errorMessage;
private static final String PEM = "PEM";

public CredentialArgumentValidator(String truststoreFile,
String certificateFile,
Expand Down Expand Up @@ -62,11 +62,11 @@ public boolean isValid() {
return true;
} else {
if (certificateFile.isEmpty()) {
errorMessage = "A public certificate must be specified by \'-p\' " +
errorMessage = "A public certificate must be specified by '-p' " +
"for signing operations.";
}
if (privateKeyFile.isEmpty()) {
errorMessage = "A private key must be specified by \'-k\' " +
errorMessage = "A private key must be specified by '-k' " +
"for signing operations.";
}
return false;
Expand Down
Loading
Loading