systemMain.cpp 7.1 KB

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