From 198d6a54b83f900f410ecb91b24b2c80d7b7b288 Mon Sep 17 00:00:00 2001 From: chubtub <43381989+chubtub@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:03:11 -0500 Subject: [PATCH 1/2] Applied checkstyles config to rimtool files --- .../src/main/java/hirs/swid/Main.java | 7 +- .../main/java/hirs/swid/SwidTagConstants.java | 11 ++- .../main/java/hirs/swid/utils/Commander.java | 88 +++++++++++-------- .../swid/utils/CreateArgumentValidator.java | 20 ++--- .../utils/CredentialArgumentValidator.java | 14 +-- .../main/java/hirs/swid/utils/CsvParser.java | 68 +++++++------- .../swid/utils/FileArgumentValidator.java | 2 +- .../main/java/hirs/swid/utils/HashSwid.java | 62 +++++++------ .../utils/TimestampArgumentValidator.java | 19 ++-- .../swid/utils/VerifyArgumentValidator.java | 16 ++-- 10 files changed, 154 insertions(+), 153 deletions(-) diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java index 3ac934aa4..1c809c8e1 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java @@ -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; @@ -13,6 +13,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; + @Log4j2 public class Main { @@ -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()); @@ -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 { diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagConstants.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagConstants.java index b7e5ff943..ec524bf69 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagConstants.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagConstants.java @@ -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 { @@ -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"; @@ -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"; @@ -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); diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java index 70a09e17a..607c6ef2d 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java @@ -15,54 +15,54 @@ public class Commander { @Parameter(description = "This parameter catches all unrecognized arguments.") - private List unknownOptions = new ArrayList<>(); - @Parameter(names = {"-h", "--help"}, help = true, description = "Print this help text.") - private boolean help; + private final List 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 ") - private List timestampArguments = new ArrayList(2); + private final List timestampArguments = new ArrayList(2); + @Parameter(names = {"-h", "--help"}, help = true, description = "Print this help text.") + private boolean help; public List getUnknownOptions() { return unknownOptions; @@ -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; } @@ -92,7 +96,9 @@ public String getOutFile() { return outFile; } - public String getTruststoreFile() { return truststoreFile; } + public String getTruststoreFile() { + return truststoreFile; + } public String getPrivateKeyFile() { return privateKeyFile; @@ -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 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 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()); @@ -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 { diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CreateArgumentValidator.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CreateArgumentValidator.java index 51c99f153..c9bd78818 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CreateArgumentValidator.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CreateArgumentValidator.java @@ -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 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) { @@ -44,17 +43,14 @@ public void validate(Map 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 parameters, String key) { Object object = parameters.get(key); - if (object == null) { - return false; - } else { - return true; - } + return object != null; } private void validateSigningCredentials(Map parameters) { @@ -64,8 +60,8 @@ private void validateSigningCredentials(Map 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 { diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CredentialArgumentValidator.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CredentialArgumentValidator.java index 1b1623493..f8c594e4b 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CredentialArgumentValidator.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CredentialArgumentValidator.java @@ -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, @@ -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; diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CsvParser.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CsvParser.java index dab404d08..0da57b2f8 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CsvParser.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CsvParser.java @@ -16,48 +16,17 @@ public class CsvParser { private static final char DEFAULT_SEPARATOR = ','; private static final char DEFAULT_QUOTE = '"'; - - private List content; + + private final List content; public CsvParser(final File file) { this(file.getAbsolutePath()); } - + public CsvParser(final String csvfile) { content = readerCsv(csvfile); } - /** - * This method takes an existing csv file and reads the file by line and - * adds the contents to a list of Strings. - * - * @param file valid path to a csv file - * @return - */ - private List readerCsv(final String file) { - String line = ""; - String csvSplitBy = ","; - List tempList = new LinkedList<>(); - - try (BufferedReader br = new BufferedReader(new FileReader(file))) { - while ((line = br.readLine()) != null) { - if (line.length() > 0 - && line.contains(csvSplitBy)) { - tempList.add(line); - } - } - } catch (IOException ioEx) { - System.out.println(String.format("Error reading in CSV file...(%s)", file)); - System.exit(1); - } - - return tempList; - } - - public final List getContent() { - return Collections.unmodifiableList(content); - } - public static List parseLine(String csvLine) { return parseLine(csvLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE); } @@ -133,4 +102,35 @@ public static List parseLine(String csvLine, char separators, char custo return result; } + + /** + * This method takes an existing csv file and reads the file by line and + * adds the contents to a list of Strings. + * + * @param file valid path to a csv file + * @return + */ + private List readerCsv(final String file) { + String line = ""; + String csvSplitBy = ","; + List tempList = new LinkedList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(file))) { + while ((line = br.readLine()) != null) { + if (line.length() > 0 + && line.contains(csvSplitBy)) { + tempList.add(line); + } + } + } catch (IOException ioEx) { + System.out.printf("Error reading in CSV file...(%s)%n", file); + System.exit(1); + } + + return tempList; + } + + public final List getContent() { + return Collections.unmodifiableList(content); + } } diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/FileArgumentValidator.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/FileArgumentValidator.java index a06c019b2..61e12976e 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/FileArgumentValidator.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/FileArgumentValidator.java @@ -2,9 +2,9 @@ import com.beust.jcommander.IParameterValidator; import com.beust.jcommander.ParameterException; +import lombok.extern.log4j.Log4j2; import java.io.File; -import lombok.extern.log4j.Log4j2; /** * This class validates arguments that take a String path to a file. diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/HashSwid.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/HashSwid.java index 4bc8d7ef1..1b127070e 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/HashSwid.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/HashSwid.java @@ -23,47 +23,30 @@ public class HashSwid { /** * Getter method for the hash that uses 256 bit hash + * * @param filepath the file to hash. - * @return + * @return */ public static String get256Hash(String filepath) throws Exception { return getHashValue(filepath, SHA256); } - /** - * Getter method for the hash that uses 384 bit hash - * @param filepath the file to hash. - * @return - */ - public String get384Hash(String filepath) throws Exception { - return getHashValue(filepath, SHA384); - } - - /** - * Getter method for the hash that uses 512 bit hash - * @param filepath the file to hash. - * @return - */ - public String get512Hash(String filepath) throws Exception { - return getHashValue(filepath, SHA512); - } - /** * This method creates the hash based on the provided algorithm * only accessible through helper methods. - * + *

* This method assumes an input file that is small enough to read in its * entirety. Large files should be handled similarly to the public static * getHashValue() below. - * + * * @param filepath file contents to hash - * @param sha the algorithm to use for the hash - * @return + * @param sha the algorithm to use for the hash + * @return */ private static String getHashValue(String filepath, String sha) throws Exception { String resultString = null; try { - MessageDigest md = MessageDigest.getInstance(sha); + MessageDigest md = MessageDigest.getInstance(sha); byte[] bytes = md.digest(Files.readAllBytes(Paths.get(filepath))); StringBuilder sb = new StringBuilder(); @@ -75,7 +58,7 @@ private static String getHashValue(String filepath, String sha) throws Exception String errorMessage = "Error hashing file " + filepath + ": "; if (e instanceof UnsupportedEncodingException || e instanceof NoSuchAlgorithmException) { - errorMessage += ((Exception) e).getMessage(); + errorMessage += e.getMessage(); } else if (e instanceof IOException) { errorMessage += "error reading file."; } @@ -88,8 +71,9 @@ private static String getHashValue(String filepath, String sha) throws Exception /** * This method is a public access hash function that operates on a string * value and uses default assumptions on the salt and algorithm + * * @param value string object to hash - * @return + * @return */ public static String getHashValue(String value) { byte[] buffer = new byte[8192]; @@ -108,8 +92,8 @@ public static String getHashValue(String value) { } catch (UnsupportedEncodingException | NoSuchAlgorithmException grex) { System.out.println(grex.getMessage()); } catch (IOException ioEx) { - System.out.println(String.format("%s: \n%s is not valid...", - ioEx.getMessage(), value)); + System.out.printf("%s: \n%s is not valid...%n", + ioEx.getMessage(), value); } finally { try { if (bis != null) { @@ -118,7 +102,7 @@ public static String getHashValue(String value) { } catch (IOException ioEx) { // ignored, system issue that won't affect further execution } - + if (hash == null) { return ""; } @@ -126,4 +110,24 @@ public static String getHashValue(String value) { return Base64.getEncoder().encodeToString(hash); } + + /** + * Getter method for the hash that uses 384 bit hash + * + * @param filepath the file to hash. + * @return + */ + public String get384Hash(String filepath) throws Exception { + return getHashValue(filepath, SHA384); + } + + /** + * Getter method for the hash that uses 512 bit hash + * + * @param filepath the file to hash. + * @return + */ + public String get512Hash(String filepath) throws Exception { + return getHashValue(filepath, SHA512); + } } diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/TimestampArgumentValidator.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/TimestampArgumentValidator.java index 1b1be43df..bc9e0a189 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/TimestampArgumentValidator.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/TimestampArgumentValidator.java @@ -11,6 +11,7 @@ public class TimestampArgumentValidator { List args; + /** * This class handles validation of the --timestamp commandline parameter. * Currently only RFC3339 and RFC3852 formats are supported. @@ -30,24 +31,16 @@ public boolean isValid() { if (isExactlyOneFormat(args)) { if (args.get(0).equalsIgnoreCase("RFC3852")) { if (args.size() > 1) { - if (isRfc3852FileValid(args.get(1))) { - return true; - } else { - return false; - } - } else if (args.size() == 1){ + return isRfc3852FileValid(args.get(1)); + } else if (args.size() == 1) { System.out.println("Countersignature file is required for RFC3852 timestamps"); return false; } } else if (args.get(0).equalsIgnoreCase("RFC3339")) { if (args.size() > 1) { - if (isRfc3339Format(args.get(1))) { - return true; - } else { - return false; - } - } else if (args.size() == 1) { - return true; + return isRfc3339Format(args.get(1)); + } else { + return args.size() == 1; } } else { System.out.println("Unsupported timestamp format specified"); diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/VerifyArgumentValidator.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/VerifyArgumentValidator.java index a7b9e92ac..d226dc0bc 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/VerifyArgumentValidator.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/VerifyArgumentValidator.java @@ -18,15 +18,14 @@ public class VerifyArgumentValidator 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 parameters) throws ParameterException { - if (isValueNotNull(parameters,"--verify")) { - if (isValueNotNull(parameters,"--create")) { + if (isValueNotNull(parameters, "--verify")) { + if (isValueNotNull(parameters, "--create")) { throw new ParameterException("Create and verify cannot be called together."); } else { for (String arg : requiredArgs) { @@ -43,16 +42,13 @@ public void validate(Map 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 parameters, String key) { Object object = parameters.get(key); - if (object == null) { - return false; - } else { - return true; - } + return object != null; } } From 98a1d26f1ddaf8a3f61fefb86c893a90e8925bd2 Mon Sep 17 00:00:00 2001 From: chubtub <43381989+chubtub@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:06:10 -0500 Subject: [PATCH 2/2] Checkstyle changes to SwidTagGateway --- .../main/java/hirs/swid/SwidTagGateway.java | 10 +- .../main/java/hirs/swid/utils/CsvParser.java | 136 ------------------ 2 files changed, 8 insertions(+), 138 deletions(-) delete mode 100644 tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CsvParser.java diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java index 9ed060753..c449bd3d9 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java @@ -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; @@ -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": @@ -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( diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CsvParser.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CsvParser.java deleted file mode 100644 index 0da57b2f8..000000000 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/CsvParser.java +++ /dev/null @@ -1,136 +0,0 @@ -package hirs.swid.utils; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -/** - * - */ -public class CsvParser { - - private static final char DEFAULT_SEPARATOR = ','; - private static final char DEFAULT_QUOTE = '"'; - - private final List content; - - public CsvParser(final File file) { - this(file.getAbsolutePath()); - } - - public CsvParser(final String csvfile) { - content = readerCsv(csvfile); - } - - public static List parseLine(String csvLine) { - return parseLine(csvLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE); - } - - public static List parseLine(String csvLine, char separators) { - return parseLine(csvLine, separators, DEFAULT_QUOTE); - } - - public static List parseLine(String csvLine, char separators, char customQuote) { - List result = new ArrayList<>(); - - if (csvLine == null || csvLine.isEmpty()) { - return result; - } - - if (customQuote == ' ') { - customQuote = DEFAULT_QUOTE; - } - - if (separators == ' ') { - separators = DEFAULT_SEPARATOR; - } - - StringBuilder currVal = new StringBuilder(); - boolean inQuotes = false; - boolean startCollectChar = false; - boolean dbleQuotesInCol = false; - - char[] chars = csvLine.toCharArray(); - - for (char ch : chars) { - if (inQuotes) { - startCollectChar = true; - if (ch == customQuote) { - inQuotes = false; - dbleQuotesInCol = false; - } else { - if (ch == '\"') { - if (!dbleQuotesInCol) { - currVal.append(ch); - dbleQuotesInCol = true; - } - } else { - currVal.append(ch); - } - } - } else { - if (ch == customQuote) { - inQuotes = true; - - if (chars[0] != '"' && customQuote == '\"') { - currVal.append('"'); - } - - if (startCollectChar) { - currVal.append('"'); - } - } else if (ch == separators) { - result.add(currVal.toString()); - currVal = new StringBuilder(); - startCollectChar = false; - } else if (ch == '\r') { - continue; - } else if (ch == '\n') { - break; - } else { - currVal.append(ch); - } - } - } - - result.add(currVal.toString()); - - return result; - } - - /** - * This method takes an existing csv file and reads the file by line and - * adds the contents to a list of Strings. - * - * @param file valid path to a csv file - * @return - */ - private List readerCsv(final String file) { - String line = ""; - String csvSplitBy = ","; - List tempList = new LinkedList<>(); - - try (BufferedReader br = new BufferedReader(new FileReader(file))) { - while ((line = br.readLine()) != null) { - if (line.length() > 0 - && line.contains(csvSplitBy)) { - tempList.add(line); - } - } - } catch (IOException ioEx) { - System.out.printf("Error reading in CSV file...(%s)%n", file); - System.exit(1); - } - - return tempList; - } - - public final List getContent() { - return Collections.unmodifiableList(content); - } -}