From 14b13ee8a8d9f48b917d3195de2f12ca0295f96b Mon Sep 17 00:00:00 2001 From: iadgovuser58 <124906646+iadgovuser58@users.noreply.github.com> Date: Fri, 31 May 2024 14:47:52 -0400 Subject: [PATCH] accessing vendor-table.json from code --- .../src/main/java/hirs/utils/JsonUtils.java | 31 ++++++ .../utils/tpm/eventlog/uefi/UefiGuid.java | 97 ++++++++++++++----- 2 files changed, 106 insertions(+), 22 deletions(-) diff --git a/HIRS_Utils/src/main/java/hirs/utils/JsonUtils.java b/HIRS_Utils/src/main/java/hirs/utils/JsonUtils.java index bca2e51b..78dfb0d4 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/JsonUtils.java +++ b/HIRS_Utils/src/main/java/hirs/utils/JsonUtils.java @@ -2,14 +2,17 @@ package hirs.utils; import com.eclipsesource.json.Json; import com.eclipsesource.json.JsonObject; +import hirs.utils.tpm.eventlog.uefi.UefiGuid; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.log4j.Log4j2; +import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -97,4 +100,32 @@ public final class JsonUtils { return jsonObject; } + + public static JsonObject getJsonObject(final String jsonFilename, final Charset charset) { + // find the file and load it + JsonObject jsonObject = new JsonObject(); + + try { + InputStream jsonIs = UefiGuid.class + .getClassLoader().getResourceAsStream("vendor-table2.json"); + jsonObject = Json.parse(new InputStreamReader(jsonIs, + charset)).asObject(); + } catch (IOException e) { + throw new RuntimeException(e); + } + // if (Files.notExists(jsonPath)) { +// log.warn(String.format("No file found at %s.", jsonPath.toString())); +// } else { +// try { +// InputStream inputStream = new FileInputStream(jsonPath.toString()); +// jsonObject = Json.parse(new InputStreamReader(inputStream, +// charset)).asObject(); +// } catch (IOException ex) { +// // add log file thing here indication issue with JSON File +// jsonObject = new JsonObject(); +// } +// } + + return jsonObject; + } } diff --git a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/uefi/UefiGuid.java b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/uefi/UefiGuid.java index f77f75bc..c0357e49 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/uefi/UefiGuid.java +++ b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/uefi/UefiGuid.java @@ -3,13 +3,20 @@ package hirs.utils.tpm.eventlog.uefi; import com.eclipsesource.json.JsonObject; import hirs.utils.HexUtils; import hirs.utils.JsonUtils; +import hirs.utils.rim.ReferenceManifestValidator; +import org.apache.commons.io.IOUtils; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; @@ -65,33 +72,79 @@ public class UefiGuid { System.out.println("XXXX EMPTYYYYYYY, before trying to grab file from code"); - System.out.println("XXXX getClass: " + getClass()); - System.out.println("XXXX getClassResource: " + getClass().getResource("/vendor-table2.json")); + uefiVendorRef = JsonUtils.getJsonObject("vendor-table2.json", + StandardCharsets.UTF_8); + +// System.out.println("XXXX getClass: " + getClass()); +// System.out.println("XXXX getClassResource: " + getClass().getResource("/vendor-table2.json")); +// +// +// InputStream inpStr = UefiGuid.class +// .getClassLoader().getResourceAsStream("vendor-table2.json"); +// System.out.println("XXXX InputStream: " + inpStr); +// +// Source inpSource = new StreamSource( +// ReferenceManifestValidator.class.getClassLoader() +// .getResourceAsStream("vendor-table2.json")); +// System.out.println("XXXX Source: " + inpSource); +// +// String path = this.getClass().getClassLoader().getResource("vendor-table2.json").toExternalForm(); +// System.out.println("XXXX External Form: " + path); - try { -// private static final String EK_PUBLIC_PATH = "/tpm2/ek.pub"; +// JsonReader reader = new JsonReader(new InputStreamReader(inpStr, "UTF-8")); -// Source source = new StreamSource( -// getClass().getClassLoader().getResourceAsStream("identity_transform.xslt")); - - - Path fPath = Paths.get(getClass().getResource("/vendor-table2.json").toURI()); -// URL url = ClassLoader.getSystemResources("vendor-table.json").nextElement(); -// URL url = ClassLoader.getSystemResources("vendor-table.json"); -// Path fPath = Paths.get(url.toURI()); - uefiVendorRef = JsonUtils.getSpecificJsonObject(fPath, - "VendorTable"); - - } -// catch (IOException e) { -// System.out.print("XXXX IOException"); +// try { +// BufferedReader streamReader = new BufferedReader(new InputStreamReader(inpStr, "UTF-8")); +// StringBuilder responseStrBuilder = new StringBuilder(); +// +// String inputStr; +// while ((inputStr = streamReader.readLine()) != null) +// responseStrBuilder.append(inputStr); +// +// String test = responseStrBuilder.toString(); +//// Gson gson = new Gson(); +// +// JsonObject testjj = parser. +// +// String temp = "hello"; +// +//// JsonObject jsonObject = new JsonObject(responseStrBuilder.toString()); +// +// } catch (UnsupportedEncodingException e) { +// throw new RuntimeException(e); +// } catch (IOException e) { +// throw new RuntimeException(e); +// } + +// byte[] rawLogBytes = IOUtils.toByteArray(inpStr); + + +// Path fPath2 = Paths.get(inpStr.toURI()); + +// try { +//// private static final String EK_PUBLIC_PATH = "/tpm2/ek.pub"; +// +//// Source source = new StreamSource( +//// getClass().getClassLoader().getResourceAsStream("identity_transform.xslt")); +// +// +// Path fPath = Paths.get(getClass().getResource("/vendor-table2.json").toURI()); +//// URL url = ClassLoader.getSystemResources("vendor-table.json").nextElement(); +//// URL url = ClassLoader.getSystemResources("vendor-table.json"); +//// Path fPath = Paths.get(url.toURI()); +// uefiVendorRef = JsonUtils.getSpecificJsonObject(fPath, +// "VendorTable"); +// +// } +//// catch (IOException e) { +//// System.out.print("XXXX IOException"); +//// throw new RuntimeException(e); +//// } +// catch (URISyntaxException e) { +// System.out.print("XXXX URISyntaxException"); // throw new RuntimeException(e); // } - catch (URISyntaxException e) { - System.out.print("XXXX URISyntaxException"); - throw new RuntimeException(e); - } } if(isVendorTableReferenceHandleEmpty()) {