Skip to content

Commit

Permalink
Merge pull request #667 from nsacyber/v3_issue-660
Browse files Browse the repository at this point in the history
[#660] Add -V|--version for easy access to rimtool version number
  • Loading branch information
chubtub authored Jan 24, 2024
2 parents 29dd125 + 5e3ab59 commit b12f065
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
31 changes: 31 additions & 0 deletions tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import hirs.utils.rim.ReferenceManifestValidator;
import com.beust.jcommander.JCommander;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

Expand All @@ -19,6 +25,14 @@ public static void main(String[] args) {
if (commander.isHelp()) {
jc.usage();
System.out.println(commander.printHelpExamples());
} else if (commander.isVersion()) {
try {
byte[] content = Files.readAllBytes(Paths.get(SwidTagConstants.VERSION_FILE));
String version = new String(content);
System.out.println("TCG rimtool version: " + version);
} catch (IOException e) {
parseVersionFromJar();
}
} else {
if (!commander.getVerifyFile().isEmpty()) {
validator = new ReferenceManifestValidator();
Expand Down Expand Up @@ -103,4 +117,21 @@ public static void main(String[] args) {
}
}
}

/**
* This method parses the version number from the jar filename in the absence of
* the VERSION file expected with an rpm installation.
*/
private static void parseVersionFromJar() {
System.out.println("Installation file VERSION not found.");
String filename = new File(Main.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath()).getName();
Pattern pattern = Pattern.compile("(?<=tcg_rim_tool-)[0-9]\\.[0-9]\\.[0-9]");
Matcher matcher = pattern.matcher(filename);
if (matcher.find()) {
System.out.println("TCG rimtool version: " + matcher.group());
}
}
}
11 changes: 8 additions & 3 deletions tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
*/
public class SwidTagConstants {

public static final String DEFAULT_KEYSTORE_FILE = "/opt/hirs/rimtool/data/keystore.jks";
public static final String INSTALLATION_DIRECTORY = "/opt/rimtool";
public static final String VERSION_FILE = INSTALLATION_DIRECTORY + "/VERSION";
public static final String DEFAULT_KEYSTORE_FILE =
INSTALLATION_DIRECTORY + "/data/keystore.jks";
public static final String DEFAULT_KEYSTORE_PASSWORD = "password";
public static final String DEFAULT_PRIVATE_KEY_ALIAS = "selfsigned";
public static final String DEFAULT_ATTRIBUTES_FILE = "/opt/hirs/rimtool/data/rim_fields.json";
public static final String DEFAULT_ATTRIBUTES_FILE =
INSTALLATION_DIRECTORY + "/data/rim_fields.json";
public static final String DEFAULT_ENGLISH = "en";

public static final String SIGNATURE_ALGORITHM_RSA_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
public static final String SIGNATURE_ALGORITHM_RSA_SHA256 =
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";

public static final String SCHEMA_PACKAGE = "hirs.utils.xjc";
public static final String SCHEMA_LANGUAGE = XMLConstants.W3C_XML_SCHEMA_NS_URI;
Expand Down
19 changes: 12 additions & 7 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 @@ -17,6 +17,11 @@ public class Commander {
@Parameter(names = {"-c", "--create \"base\""}, order = 0,
description = "The type of RIM to create. A base RIM will be created by default.")
private String createType = "";
@Parameter(names = {"-v", "--verify <path>"}, order = 3,
description = "Specify a RIM file to verify.")
private String verifyFile = "";
@Parameter(names = {"-V", "--version"}, description = "Output the current version.")
private boolean version = false;
@Parameter(names = {"-a", "--attributes <path>"}, order = 1,
description = "The configuration file holding attributes "
+ "to populate the base RIM with.")
Expand All @@ -25,9 +30,6 @@ public class Commander {
description = "The file to write the RIM out to. "
+ "The RIM will be written to stdout by default.")
private String outFile = "";
@Parameter(names = {"-v", "--verify <path>"}, order = 3,
description = "Specify a RIM file to verify.")
private String verifyFile = "";
@Parameter(names = {"-t", "--truststore <path>"}, order = 4,
description = "The truststore to sign the base RIM created "
+ "or to validate the signed base RIM.")
Expand Down Expand Up @@ -62,6 +64,13 @@ public String getCreateType() {
return createType;
}

public String getVerifyFile() {
return verifyFile;
}

public boolean isVersion() {
return version;
}
public String getAttributesFile() {
return attributesFile;
}
Expand All @@ -70,10 +79,6 @@ public String getOutFile() {
return outFile;
}

public String getVerifyFile() {
return verifyFile;
}

public String getTruststoreFile() { return truststoreFile; }

public String getPrivateKeyFile() {
Expand Down

0 comments on commit b12f065

Please sign in to comment.