From ef44c8d97026f1d10d73bfffb5e5b17a0393635c Mon Sep 17 00:00:00 2001 From: chubtub <43381989+chubtub@users.noreply.github.com> Date: Mon, 22 May 2023 12:35:34 -0400 Subject: [PATCH 1/2] Added support for RFC2315 --- .../main/java/hirs/swid/SwidTagConstants.java | 2 ++ .../main/java/hirs/swid/SwidTagGateway.java | 16 ++++++++++++- .../main/java/hirs/swid/utils/Commander.java | 2 +- .../utils/TimestampArgumentValidator.java | 24 +++++++++++-------- 4 files changed, 32 insertions(+), 12 deletions(-) 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 1f2331a4..4455c94f 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 @@ -76,11 +76,13 @@ public class SwidTagConstants { 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 RFC2315_NS = "https://www.ietf.org/rfc/rfc2315.txt"; public static final String N8060_PFX = "n8060"; public static final String RIM_PFX = "rim"; public static final String RFC3852_PFX = "rcf3852"; public static final String RFC3339_PFX = "rcf3339"; + public static final String RFC2315_PFX = "rcf2315"; public static final QName _SHA256_HASH = new QName( "http://www.w3.org/2001/04/xmlenc#sha256", HASH, "SHA256"); 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 9c9f5ace..3555a664 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 @@ -194,7 +194,7 @@ public class SwidTagGateway { } /** - * Setter for timestamp input - RFC3852 + file or RFC3339 + value + * Setter for timestamp input - RFC3852|RFC2315 + file or RFC3339 + value * * @param timestampArgument */ @@ -786,6 +786,20 @@ public class SwidTagGateway { private XMLObject createXmlTimestamp(Document doc, XMLSignatureFactory sigFactory) { Element timeStampElement = doc.createElement("TimeStamp"); switch (timestampFormat.toUpperCase()) { + case "RFC2315": + try { + byte[] counterSignature = Base64.getEncoder().encode( + Files.readAllBytes(Paths.get(timestampArgument))); + timeStampElement.setAttributeNS("http://www.w3.org/2000/xmlns/", + "xmlns:" + SwidTagConstants.RFC2315_PFX, + SwidTagConstants.RFC2315_NS); + timeStampElement.setAttribute(SwidTagConstants.DATETIME, + new String(counterSignature)); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + break; case "RFC3852": try { byte[] counterSignature = Base64.getEncoder().encode( 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 da985dc9..07d1d44a 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 @@ -54,7 +54,7 @@ public class Commander { private String rimEventLog = ""; @Parameter(names = {"--timestamp"}, order = 11, variableArity = true, description = "Add a timestamp to the signature. " + - "Currently only RFC3339 and RFC3852 are supported:\n" + + "Currently RFC3339, RFC3852, and RFC2315 (PKCS7) formats are supported:\n" + "\tRFC3339 [yyyy-MM-ddThh:mm:ssZ]\n\tRFC3852 ") private List timestampArguments = new ArrayList(2); 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 5d25a074..1bd2ebf5 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 @@ -14,7 +14,7 @@ public class TimestampArgumentValidator { /** * This class handles validation of the --timestamp commandline parameter. - * Currently only RFC3339 and RFC3852 formats are supported. + * Currently RFC3339, RFC3852, and RFC2315 (PKCS7) formats are supported. * * @param args list of arguments from command line */ @@ -29,15 +29,17 @@ public class TimestampArgumentValidator { */ public boolean isValid() { if (isExactlyOneFormat(args)) { - if (args.get(0).equalsIgnoreCase("RFC3852")) { + if (args.get(0).equalsIgnoreCase("RFC3852") || + args.get(0).equalsIgnoreCase("RFC2315")) { if (args.size() > 1) { - if (isRfc3852FileValid(args.get(1))) { + if (isCountersignatureFileValid(args.get(1))) { return true; } else { return false; } } else if (args.size() == 1) { - System.out.println("Countersignature file is required for RFC3852 timestamps"); + System.out.println("Countersignature file is required for " + + "RFC3852 and RFC2315 (PKCS7) timestamps"); return false; } } else if (args.get(0).equalsIgnoreCase("RFC3339")) { @@ -59,25 +61,26 @@ public class TimestampArgumentValidator { } /** - * This method ensures that exactly one of RFC3339 and RFC3852 are specified. + * This method ensures that exactly one format is specified. * * @param args list of command line arguments * @return true if exactly one format is specified, false otherwise */ private boolean isExactlyOneFormat(List args) { - Pattern pattern = Pattern.compile("(R|r)(F|f)(C|c)(3339|3852)"); + Pattern pattern = Pattern.compile("(R|r)(F|f)(C|c)(3339|3852|2315)"); String format = args.get(0); Matcher formatMatcher = pattern.matcher(format); if (!formatMatcher.matches()) { - System.out.println("Invalid timestamp format specified, expected RFC3339 or RFC3852."); + System.out.println("Invalid timestamp format specified. " + + "Please choose from RFC3339, RFC3852, or RFC2315."); return false; } if (args.size() == 2) { String argument = args.get(1); Matcher argumentMatcher = pattern.matcher(argument); if (argumentMatcher.matches()) { - System.out.println("Exactly one timestamp format must be specified."); + System.out.println("Only one timestamp format may be specified at a time."); return false; } } @@ -108,7 +111,7 @@ public class TimestampArgumentValidator { * @param file the counter signature * @return true if file exists and is valid, false otherwise */ - private boolean isRfc3852FileValid(String file) { + private boolean isCountersignatureFileValid(String file) { if (file != null && !file.isEmpty()) { try { Files.readAllBytes(Paths.get(file)); @@ -117,7 +120,8 @@ public class TimestampArgumentValidator { return false; } } else { - System.out.println("RFC3852 requires a filename input of the countersignature file."); + System.out.println("RFC3852 and RFC2315 (PKCS7) formats require " + + "a filename input of the countersignature file."); return false; } return true; From 2d54e84f6267121d9ee56695cb670f7ec1ec891b Mon Sep 17 00:00:00 2001 From: chubtub <43381989+chubtub@users.noreply.github.com> Date: Wed, 7 Jun 2023 17:21:53 -0400 Subject: [PATCH 2/2] Added unit test for RFC2315 timestamp --- .../java/hirs/swid/TestSwidTagGateway.java | 20 ++++++++- .../generated_timestamp_rfc2315.swidtag | 43 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tools/tcg_rim_tool/src/test/resources/generated_timestamp_rfc2315.swidtag diff --git a/tools/tcg_rim_tool/src/test/java/hirs/swid/TestSwidTagGateway.java b/tools/tcg_rim_tool/src/test/java/hirs/swid/TestSwidTagGateway.java index ce32ef43..8aee86ad 100644 --- a/tools/tcg_rim_tool/src/test/java/hirs/swid/TestSwidTagGateway.java +++ b/tools/tcg_rim_tool/src/test/java/hirs/swid/TestSwidTagGateway.java @@ -23,6 +23,7 @@ public class TestSwidTagGateway { private final String BASE_DEFAULT_CERT = "generated_default_cert.swidtag"; private final String BASE_RFC3339_TIMESTAMP = "generated_timestamp_rfc3339.swidtag"; private final String BASE_RFC3852_TIMESTAMP = "generated_timestamp_rfc3852.swidtag"; + private final String BASE_RFC2315_TIMESTAMP = "generated_timestamp_rfc2315.swidtag"; private final String ATTRIBUTES_FILE = TestSwidTagGateway.class.getClassLoader() .getResource("rim_fields.json").getPath(); private final String JKS_KEYSTORE_FILE = TestSwidTagGateway.class.getClassLoader() @@ -163,7 +164,24 @@ public class TestSwidTagGateway { Assert.assertTrue(validator.validateSwidTag(DEFAULT_OUTPUT, "DEFAULT")); } - /** + /** + * This test corresponds to the arguments: + * -c base -l TpmLog.bin -d --timestamp rfc2315 countersignature.file + */ + @Test + public void testCreateTimestampRfc2315() { + gateway.setDefaultCredentials(true); + gateway.setTruststoreFile(JKS_KEYSTORE_FILE); + gateway.setTimestampFormat("RFC2315"); + gateway.setTimestampArgument(RFC3852_COUNTERSIGNATURE_FILE); + gateway.generateSwidTag(DEFAULT_OUTPUT); + expectedFile = TestSwidTagGateway.class.getClassLoader() + .getResourceAsStream(BASE_RFC2315_TIMESTAMP); + Assert.assertTrue(compareFileBytesToExpectedFile(DEFAULT_OUTPUT)); + Assert.assertTrue(validator.validateSwidTag(DEFAULT_OUTPUT, "DEFAULT")); + } + + /** * This test corresponds to the arguments: * -s -d */ diff --git a/tools/tcg_rim_tool/src/test/resources/generated_timestamp_rfc2315.swidtag b/tools/tcg_rim_tool/src/test/resources/generated_timestamp_rfc2315.swidtag new file mode 100644 index 00000000..5878e7f6 --- /dev/null +++ b/tools/tcg_rim_tool/src/test/resources/generated_timestamp_rfc2315.swidtag @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + f3ulvid12X4b4EqgAQrriXwqvqlNd1GXoSf/wI+zf2A= + + + + ibBq+F0kVcZzamIb0aN2cfyexARJYg0zGFA/T+fZvFY= + + + RYidnj7kzYjZxy22BKR9bjHktaxaKf8XgzvdKX5aG/x4Ieuu9XFDphDahoD1gkWG0WnJzaPZAoXn ++TU25X9As7MTo2CVIcbg09ZRJIg735GlYX28dFphmiYUCEwoJ6bloSdJbt8u/GgrW/dVkldySpci +88Y3dQoqXio2i2R7R5hIBEEdCmuQL8SmPNsAtD9pJRe5YoP8sfHo3IAL9AkYqW/+U4GkqOJyNI8G +/Kxy4TWdzuOz2N6zqNCsDQ2FyzRUVyhQgvsHSDbaJL3IXIobxBpAUHemfVI0tO8MsTS0+v1uNypQ +MvAQALhV43eoBKQyzmFlKHYSGlj8AC0zktXTlg== + + 2fdeb8e7d030a2209daa01861a964fedecf2bcc1 + + + + + + + + + +