systemMain.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <string>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <stdio.h>
  9. #include "crypto.h"
  10. #include "SealerWrapper.h"
  11. #include "DeploymentStageLogic.h"
  12. using namespace std;
  13. #define DECRYPTOR_PORT 3824
  14. #define SGX_HASH_SIZE 32
  15. int __ImageBase=0;
  16. int verify_apache(std::string& path, std::string& keypair) {return 0; }
  17. void foo() //sigsegv_handler(int signo)
  18. {
  19. printf("foo hehe ---------------------\n");
  20. fflush(stdout);
  21. }
  22. int main(int argc, char** argv)
  23. {
  24. // signal(SIGSEGV, SIG_IGN); //sigsegv_handler);
  25. /*
  26. JavaVM *vm;
  27. JNIEnv *env;
  28. JavaVMInitArgs vm_args;
  29. jint res;
  30. jclass cls;
  31. jmethodID mid;
  32. jstring jstr;
  33. jobjectArray main_args;
  34. JavaVMOption* options = new JavaVMOption[7];
  35. options[0].optionString = "-Dpixy.home=/home/m2mazmud/pixy-master";
  36. options[1].optionString = "-Djava.class.path=/home/m2mazmud/pixy-master/lib:/home/m2mazmud/pixy-master/build/class";
  37. options[2].optionString = "-Xcheck:jni";
  38. options[3].optionString = "-Xms32m";
  39. options[4].optionString = "-Xmx128m";
  40. options[5].optionString = "-XX:MaxMetaspaceSize=128m";
  41. options[6].optionString = "-Xss256k";
  42. vm_args.options = options;
  43. vm_args.nOptions = 7;
  44. printf("Helloworld\n"); fflush(stdout);
  45. vm_args.ignoreUnrecognized = JNI_TRUE;
  46. vm_args.version = JNI_VERSION_1_8;
  47. // sleep(60);
  48. if (JNI_CreateJavaVM(&vm, (void **)&env, &vm_args) != JNI_OK) {
  49. delete options;
  50. printf("Failed to create Java VMn");
  51. return 1;
  52. }
  53. printf("WOO HOO CREATED JVM\n"); fflush(stdout);
  54. delete options;
  55. cls = (env)->FindClass("at/ac/tuwien/infosys/www/pixy/Checker");
  56. if (cls == NULL) {
  57. printf("Failed to find Main classn");
  58. return 1;
  59. }
  60. mid = (env)->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
  61. if (mid == NULL) {
  62. printf("Failed to find main functionnnnn");
  63. return 1;
  64. }
  65. printf("Hehehehe\n"); fflush(stdout);
  66. jclass classString = (env)->FindClass("java/lang/String");
  67. jstr = (env)->NewStringUTF("");
  68. main_args = (env)->NewObjectArray( 8, classString, jstr);
  69. (env)->SetObjectArrayElement( main_args, 0, (env)->NewStringUTF( "-aAvLf"));
  70. (env)->SetObjectArrayElement( main_args, 1, (env)->NewStringUTF( "-s"));
  71. (env)->SetObjectArrayElement( main_args, 2, (env)->NewStringUTF( "/home/m2mazmud/pixy-master/config/sinks_xss.txt"));
  72. (env)->SetObjectArrayElement( main_args, 3, (env)->NewStringUTF( "-o"));
  73. (env)->SetObjectArrayElement( main_args, 4, (env)->NewStringUTF( "/home/m2mazmud/pixy-master/sample_mitigator/results"));
  74. (env)->SetObjectArrayElement( main_args, 5, (env)->NewStringUTF( "-y"));
  75. (env)->SetObjectArrayElement( main_args, 6, (env)->NewStringUTF( "xss"));
  76. if(argc>=1)
  77. (env)->SetObjectArrayElement( main_args, 7, (env)->NewStringUTF( argv[1]));
  78. (env)->CallStaticVoidMethod( cls, mid, main_args);
  79. if((env)->ExceptionOccurred()) {
  80. (env)->ExceptionDescribe();
  81. (env)->ExceptionClear();
  82. printf("Exception occurred\n"); fflush(stdout);
  83. }
  84. /*
  85. printf("HELLOWORLD\n");fflush(stdout);
  86. // Py_SetProgramName("verifier");
  87. printf("HELLOWORLD 2\n"); fflush(stdout);
  88. Py_Initialize();
  89. printf("HELLOWORLD 3\n");fflush(stdout);
  90. PyRun_SimpleString("from time import time,ctime\n"
  91. "print 'Today is',ctime(time())\n");
  92. Py_Finalize();
  93. // return 0;
  94. */
  95. /*
  96. // uint8_t expected_apache_mr_signer[32] = {0};
  97. std::string apache_signature_keypair_private("1234567890");
  98. std::string apache_public_key;
  99. std::string apache_private_key2;
  100. // generate_rsa_keypair(apache_public_key, apache_private_key2);
  101. uint32_t return_sgx; uint32_t return_internal;
  102. std::string recovered_plaintext;
  103. uint32_t expected_sealed_msg_size=0;
  104. FILE* fp = fopen("./apache_signature_keypair.pem", "w+");
  105. if(fp == NULL)
  106. {
  107. perror("Could not create the file ./apache_signature_keypair.pem due to error: "); fflush(stderr); return 0xffffffff;
  108. }
  109. // RSA_signing_keypair rsa_signing_keypair();
  110. return_internal = generate_rsa_keypair(fp, apache_public_key, apache_private_key2); //, expected_apache_mr_signer);
  111. if(return_internal !=0)
  112. {
  113. printf("Could not generate RSA keypair - error 0x%x\n", return_internal); fflush(stdout); return return_internal;
  114. }
  115. printf("Generated key pair - outside the fun\n"); fflush(stdout);
  116. int sealed_file_fd = open("sealed_msg.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  117. if(sealed_file_fd == -1)
  118. {
  119. perror("\nError in opening or creating the file sealed_msg.txt - ");
  120. fflush(stderr);
  121. return 0xFFFFFFFF;
  122. }
  123. printf("\nSuccessfully opened a file to seal the apache signature keypair to.\n");
  124. fflush(stdout);
  125. return_sgx = seal_message_to_file(sealed_file_fd, apache_signature_keypair_private, &expected_sealed_msg_size);
  126. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  127. {
  128. printf("Sealing SGX error %x", return_sgx);
  129. fflush(stdout);
  130. return return_sgx;
  131. }
  132. else if(return_sgx == 0xFFFFFFFF)
  133. {
  134. perror("Successful SGX sealing, but error in writing to a file or write returned 0 bytes because the disk was full etc.\n");
  135. fflush(stdout);
  136. return return_sgx;
  137. }
  138. printf("\nSuccessfully sealed the plaintext %s to length 0x%x.\n", apache_signature_keypair_private.c_str(), expected_sealed_msg_size);
  139. fflush(stdout);
  140. */
  141. uint8_t target_hash[32] = {0x54,0x24,0x5d,0x63,0x5c,0x8f,0xec,0xcf,0x37,0xb9,0xcf,0x9e,0xb8,0xd3,0x22,0x04,0x57,0x5b,0xb2,0xfc,0xa6,0xd3,0x11,0xfb,0x87,0xb7,0x01,0x06,0x2f,0x18,0x25,0xc1};
  142. DeploymentStageLogic deploymentStage;
  143. deploymentStage.set_target_hash(target_hash);
  144. printf("Setting up SOCKET:\n"); fflush(stdout);
  145. int fd = deploymentStage.set_up_socket_connect(DECRYPTOR_PORT);
  146. if(fd <= 0)
  147. // Set up an IPC channel for local attestation and post-LA messages.
  148. {
  149. printf("\nCould not set up the socket: had the following error: %d\n", fd);
  150. fflush(stdout);
  151. return fd;
  152. }
  153. printf("Starting main logic with fd: %d\n", fd); fflush(stdout);
  154. int ret_status = deploymentStage.main_logic(fd);
  155. if(ret_status != 0)
  156. return ret_status;
  157. /*
  158. // sleep(50);
  159. printf("\n z z z z z z z z z z z z z (sleeping for a bit) z z z z z z z z (meant to emulate the '2nd' stage of validator, that will be rerun whenever Apache changes)\n");
  160. return_sgx = unseal_message_from_file(sealed_file_fd, recovered_plaintext, &expected_sealed_msg_size);
  161. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  162. {
  163. printf("Successful read from file, but error in SGX unsealing: %x.\n", return_sgx);
  164. fflush(stdout);
  165. return return_sgx;
  166. }
  167. else if(return_sgx == 0xFFFFFFFF)
  168. {
  169. perror("\n Could not read the file.\n");
  170. fflush(stdout);
  171. return return_sgx;
  172. }
  173. printf("\n Unsealed the keypair.\n");
  174. fflush(stdout);
  175. std::string path("../apache/source/code/path");
  176. return_internal = verify_apache(path, apache_signature_keypair_private);
  177. if(return_internal != 0)
  178. {
  179. printf("\nThe signed manifest was not created due to the above errors.\n");
  180. fflush(stdout);
  181. return return_internal;
  182. }
  183. printf("Successfully verified the Apache enclave and signed its manifest.\n");
  184. fflush(stdout);
  185. */
  186. return 0;
  187. }