Program.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2024 Intel Corporation.
  3. * Copyright (C) 2024 University of Neuchatel, Switzerland.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. */
  7. using System.Diagnostics;
  8. using System.Runtime.InteropServices;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Text.Json;
  12. // Set the reference values below
  13. byte[] mrEnclaveReference =
  14. {
  15. 0xDA, 0xE0, 0xDA, 0x2F, 0x8A, 0x53, 0xA0, 0xB4, 0x8F, 0x92, 0x6A, 0x3B, 0xC0, 0x48, 0xD6, 0xA9,
  16. 0x67, 0xD4, 0x7C, 0x86, 0x19, 0x86, 0x76, 0x6F, 0x8F, 0x5A, 0xB1, 0xC0, 0xA8, 0xD8, 0x8E, 0x44
  17. };
  18. byte[] mrSignerReference =
  19. {
  20. 0x83, 0xD7, 0x19, 0xE7, 0x7D, 0xEA, 0xCA, 0x14, 0x70, 0xF6, 0xBA, 0xF6, 0x2A, 0x4D, 0x77, 0x43,
  21. 0x03, 0xC8, 0x99, 0xDB, 0x69, 0x02, 0x0F, 0x9C, 0x70, 0xEE, 0x1D, 0xFC, 0x08, 0xC7, 0xCE, 0x9E
  22. };
  23. const ushort securityVersionReference = 0;
  24. const ushort productIdReference = 0;
  25. string nonce = "This is a sample.\0"; // Notice the \0 at the end, which is mandatory as C-strings are terminated with this char
  26. string evidenceAsString = """{"type":"sgx_ecdsa","report_base64":"[..]","report_len":[..]}""";
  27. string wasmFilePath = "../build/wasm-app/test.wasm";
  28. // Parse and compute the claims
  29. EvidenceJson? evidenceAsJson = JsonSerializer.Deserialize<EvidenceJson>(evidenceAsString, new JsonSerializerOptions
  30. {
  31. PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
  32. });
  33. Debug.Assert(evidenceAsJson != null, "The evidence cannot be parsed.");
  34. byte[] wasmFileContent = await File.ReadAllBytesAsync(wasmFilePath);
  35. byte[] nonceAsBytes = Encoding.UTF8.GetBytes(nonce);
  36. byte[] computedUserData = await ComputeUserData(wasmFileContent, nonceAsBytes);
  37. byte[] evidenceAsBytes = Convert.FromBase64String(evidenceAsJson.ReportBase64);
  38. Evidence evidence = new(evidenceAsBytes);
  39. int libRatsReturnValue = LibRats.VerifyEvidenceFromJson(evidenceAsString, await ComputeUserData(wasmFileContent, nonceAsBytes));
  40. // Compare and display the results
  41. Console.WriteLine($"User data, evidence: {BitConverter.ToString(evidence.UserData)}");
  42. Console.WriteLine($"User Data, computed: {BitConverter.ToString(computedUserData)}");
  43. Console.WriteLine($"Do the two user data match? {evidence.UserData.SequenceEqual(computedUserData)}");
  44. Console.WriteLine($"MrEnclave: {BitConverter.ToString(evidence.MrEnclave)}");
  45. Console.WriteLine($"Do the MrEnclave match? {mrEnclaveReference.SequenceEqual(evidence.MrEnclave)}");
  46. Console.WriteLine($"MrSigner: {BitConverter.ToString(evidence.MrSigner)}");
  47. Console.WriteLine($"Do the MrSigner match? {mrSignerReference.SequenceEqual(evidence.MrSigner)}");
  48. Console.WriteLine($"Security Version: {evidence.SecurityVersion}, expected: {securityVersionReference}");
  49. Console.WriteLine($"Product ID: {evidence.ProductId}, expected: {productIdReference}");
  50. Console.WriteLine($"VerifyJsonUsingLibrats returned: {libRatsReturnValue:X}");
  51. // Compute the user data as computed by WAMR
  52. static async ValueTask<byte[]> ComputeUserData(byte[] wasmFileContent, byte[] nonce)
  53. {
  54. using var sha256 = SHA256.Create();
  55. var wasmFileContentHash = sha256.ComputeHash(wasmFileContent);
  56. using MemoryStream stream = new();
  57. await stream.WriteAsync(wasmFileContentHash);
  58. await stream.WriteAsync(nonce);
  59. stream.Position = 0;
  60. byte[] computedUserData = await sha256.ComputeHashAsync(stream);
  61. return computedUserData;
  62. }
  63. /// <summary>
  64. /// The layout of the JSON is given by librats.
  65. /// </summary>
  66. class EvidenceJson
  67. {
  68. public required string Type { get; init; }
  69. public required string ReportBase64 { get; init; }
  70. public required int ReportLen { get; init; }
  71. }
  72. /// <summary>
  73. /// The start of the _report_body_t struct from Intel SGX is at offset 0x30.
  74. /// </summary>
  75. /// <remarks>
  76. /// _report_body_t struct: https://github.com/intel/linux-sgx/blob/a1eeccba5a72b3b9b342569d2cc469ece106d3e9/common/inc/sgx_report.h#L93-L111
  77. /// Attestation flow: https://www.intel.com/content/www/us/en/developer/articles/code-sample/software-guard-extensions-remote-attestation-end-to-end-example.html
  78. /// </remarks>
  79. class Evidence(byte[] evidenceAsBytes)
  80. {
  81. public byte[] MrEnclave => evidenceAsBytes[0x70..0x90];
  82. public byte[] MrSigner => evidenceAsBytes[0xB0..0xD0];
  83. public ushort ProductId => BitConverter.ToUInt16(evidenceAsBytes.AsSpan(0x130, 2));
  84. public ushort SecurityVersion => BitConverter.ToUInt16(evidenceAsBytes.AsSpan(0x132, 2));
  85. public byte[] UserData => evidenceAsBytes[0x170..0x190];
  86. }
  87. static class LibRats
  88. {
  89. /// <summary>
  90. /// Verifies the evidence using librats native function.
  91. /// </summary>
  92. /// <remarks>
  93. /// Original signature: int librats_verify_evidence_from_json(const char *json_string, const uint8_t *hash);
  94. /// </remarks>
  95. [DllImport("/usr/local/lib/librats/librats_lib.so", EntryPoint = "librats_verify_evidence_from_json")]
  96. public static extern int VerifyEvidenceFromJson(string json, byte[] hash);
  97. }