TwoStepSignHandlerBase.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2018 Intel Corporation. //
  3. // //
  4. // All rights reserved. This program and the accompanying materials //
  5. // are made available under the terms of the Eclipse Public License v1.0 //
  6. // which accompanies this distribution, and is available at //
  7. // http://www.eclipse.org/legal/epl-v10.html //
  8. // //
  9. // Contributors: //
  10. // Intel Corporation - initial implementation and documentation //
  11. ///////////////////////////////////////////////////////////////////////////
  12. package com.intel.sgx.handlers;
  13. import java.io.BufferedReader;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import com.intel.sgx.preferences.PreferenceConstants;
  18. public abstract class TwoStepSignHandlerBase extends SGXHandler {
  19. public String hashFile = null;
  20. public String configFile = null;
  21. public String enclaveFile = null;
  22. public String externalSignPublicKeyFile = null;
  23. public String externallySignedHashFile = null;
  24. public String outputSignedEnclaveFile = null;
  25. protected File signtool;
  26. public TwoStepSignHandlerBase() {
  27. super();
  28. }
  29. protected void executeGenData() throws ErrorException {
  30. validateConfigFile();
  31. validateEnclaveFile();
  32. refreshProject();
  33. executeSignTool(new String[] { "gendata",
  34. "-enclave", enclaveFile,
  35. "-config", configFile,
  36. "-out", hashFile });
  37. refreshProject();
  38. validateHashFile();
  39. }
  40. protected void executeCatSig() throws ErrorException {
  41. validateEnclaveFile();
  42. validateConfigFile();
  43. validateHashFile();
  44. validateExternalSignPublicKeyFile();
  45. validateExternallySignedHashFile();
  46. executeSignTool("catsig",
  47. // enclave data:
  48. "-enclave", enclaveFile,
  49. "-config", configFile,
  50. // previously generated:
  51. "-unsigned", hashFile,
  52. // externally generated
  53. "-key", externalSignPublicKeyFile,
  54. "-sig", externallySignedHashFile,
  55. // output
  56. "-out", outputSignedEnclaveFile
  57. );
  58. refreshProject();
  59. validateOutputSignedEnclaveFile();
  60. info("Two Step Enclave Sign","Enclave signed successfully !");
  61. }
  62. void initializeSigntool() throws ErrorException {
  63. signtool = PreferenceConstants.getSDKDescriptor().getSignerPath();
  64. if (!signtool.exists() || signtool.isDirectory()) {
  65. quitWithError("Error generating hash! Sign Tool Not Found !\n Please make sure to have written in the box the value for Intel(R) SGX SDK Directory in Window->Preferences->Intel(R) SGX Preferences. \n Usually the path is in /opt/intel/sgxsdk/" );
  66. }
  67. }
  68. protected void validateEnclaveFile() throws ErrorException {
  69. File enclave = new File(enclaveFile);
  70. if (!enclave.exists() || enclave.isDirectory()) {
  71. quitWithError("Error generating hash! Unsigned Enclave File Not Found! Try building the enclave first");
  72. }
  73. }
  74. protected void validateConfigFile() throws ErrorException {
  75. if (configFile == null || configFile.isEmpty()) {
  76. quitWithError("Error Enclave Configuration File Not Found !");
  77. }
  78. File config = new File(configFile);
  79. if (!config.exists() || config.isDirectory()) {
  80. quitWithError("Enclave Config File Not Found !");
  81. }
  82. }
  83. protected void validateExternallySignedHashFile() throws ErrorException {
  84. if (externallySignedHashFile == null || externallySignedHashFile.isEmpty()) {
  85. quitWithError("Error signing enclave! Signature File Not Found !");
  86. }
  87. File signature = new File(externallySignedHashFile);
  88. if (!signature.exists() || signature.isDirectory()) {
  89. quitWithError("Error signing enclave! Signature File Not Found !");
  90. }
  91. }
  92. protected void validateExternalSignPublicKeyFile() throws ErrorException {
  93. if (externalSignPublicKeyFile == null || externalSignPublicKeyFile.isEmpty()) {
  94. quitWithError("Public Key File Not Found !");
  95. }
  96. File publickkey = new File(externalSignPublicKeyFile);
  97. if (!publickkey.exists() || publickkey.isDirectory()) {
  98. quitWithError("Error signing enclave! Public Key File Not Found !");
  99. }
  100. }
  101. private void validateOutputSignedEnclaveFile() throws ErrorException {
  102. if(outputSignedEnclaveFile == null || outputSignedEnclaveFile.isEmpty())
  103. {
  104. quitWithError("Output Signed File Not Found !");
  105. }
  106. File outputSignedEnclave = new File(outputSignedEnclaveFile);
  107. if(!outputSignedEnclave.exists() || outputSignedEnclave.isDirectory())
  108. {
  109. quitWithError("Output Signed File Not Found !");
  110. }
  111. // TODO Auto-generated method stub
  112. }
  113. protected void validateHashFile() throws ErrorException {
  114. if(hashFile == null || hashFile.isEmpty())
  115. {
  116. quitWithError("Hash File Not Found !");
  117. }
  118. File hash = new File(hashFile);
  119. if(!hash.exists() || hash.isDirectory())
  120. {
  121. quitWithError("Hash File Not Found !");
  122. }
  123. }
  124. protected void executeSignTool(String... args) throws ErrorException {
  125. Process q;
  126. try {
  127. String[] allArgs = new String[args.length+1];
  128. allArgs[0] = signtool.getAbsolutePath();
  129. System.arraycopy(args, 0, allArgs, 1, args.length);
  130. for (String arg : args){
  131. }
  132. String fullOutput = "";
  133. q = Runtime.getRuntime().exec(allArgs);
  134. BufferedReader stdInput = new BufferedReader(new InputStreamReader(
  135. q.getInputStream()));
  136. BufferedReader stdErr = new BufferedReader(new InputStreamReader(
  137. q.getErrorStream()));
  138. String s = null;
  139. while ((s = stdInput.readLine()) != null) {
  140. }
  141. String[] out = new String[20];
  142. int i = 0;
  143. while ((out[i] = stdErr.readLine()) != null) {
  144. fullOutput += out[i]+"\n";
  145. i++;
  146. }
  147. String result = out[i - 1];
  148. if (!result.equals("Succeed.")) {
  149. // quitWithError("Error generating hash! " + out[i - 2]);
  150. quitWithError("Error generating hash! " + fullOutput);
  151. }
  152. } catch (IOException e) {
  153. quitWithError(e.getLocalizedMessage());
  154. }
  155. }
  156. }