systemMain.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /*
  29. // signal(SIGSEGV, SIG_IGN); //sigsegv_handler);
  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. foo();
  53. if (JNI_CreateJavaVM(&vm, (void **)&env, &vm_args) != JNI_OK) {
  54. delete options;
  55. printf("Failed to create Java VMn");
  56. return 1;
  57. }
  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)->ExceptionClear();
  85. printf("Exception occurred\n"); fflush(stdout);
  86. }
  87. */
  88. printf("HELLOWORLD\n");fflush(stdout);
  89. // Py_SetProgramName("verifier");
  90. printf("HELLOWORLD 2\n"); fflush(stdout);
  91. Py_Initialize();
  92. printf("HELLOWORLD 3\n");fflush(stdout);
  93. PyRun_SimpleString("from time import time,ctime\n"
  94. "print 'Today is',ctime(time())\n");
  95. Py_Finalize();
  96. // return 0;
  97. uint8_t expected_apache_mr_signer[32] = {0};
  98. std::string apache_signature_keypair_private("1234567890");
  99. std::string apache_public_key;
  100. std::string apache_private_key2;
  101. // generate_rsa_keypair(apache_public_key, apache_private_key2);
  102. // uint8_t decryptor_mr_enclave[SGX_HASH_SIZE] = {0x1};
  103. // uint8_t decryptor_mr_signer[SGX_HASH_SIZE] = {0x2};
  104. uint32_t return_sgx; uint32_t return_internal;
  105. std::string recovered_plaintext;
  106. uint32_t expected_sealed_msg_size=0;
  107. FILE* fp = fopen("./apache_signature_keypair.pem", "w+");
  108. if(fp == NULL)
  109. {
  110. perror("Could not create the file ./apache_signature_keypair.pem due to error: "); fflush(stderr); return 0xffffffff;
  111. }
  112. // RSA_signing_keypair rsa_signing_keypair();
  113. return_internal = generate_rsa_keypair(fp, apache_public_key, apache_private_key2); //, expected_apache_mr_signer);
  114. if(return_internal !=0)
  115. {
  116. printf("Could not generate RSA keypair - error 0x%x\n", return_internal); fflush(stdout); return return_internal;
  117. }
  118. printf("Generated key pair - outside the fun\n"); fflush(stdout);
  119. int sealed_file_fd = open("sealed_msg.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  120. if(sealed_file_fd == -1)
  121. {
  122. perror("\nError in opening or creating the file sealed_msg.txt - ");
  123. fflush(stderr);
  124. return 0xFFFFFFFF;
  125. }
  126. printf("\nSuccessfully opened a file to seal the apache signature keypair to.\n");
  127. fflush(stdout);
  128. return_sgx = seal_message_to_file(sealed_file_fd, apache_signature_keypair_private, &expected_sealed_msg_size);
  129. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  130. {
  131. printf("Sealing SGX error %x", return_sgx);
  132. fflush(stdout);
  133. return return_sgx;
  134. }
  135. else if(return_sgx == 0xFFFFFFFF)
  136. {
  137. perror("Successful SGX sealing, but error in writing to a file or write returned 0 bytes because the disk was full etc.\n");
  138. fflush(stdout);
  139. return return_sgx;
  140. }
  141. printf("\nSuccessfully sealed the plaintext %s to length 0x%x.\n", apache_signature_keypair_private.c_str(), expected_sealed_msg_size);
  142. fflush(stdout);
  143. /*
  144. return_sgx = local_attestation_initiator(DECRYPTOR_PORT);
  145. if(return_sgx != 0)
  146. {
  147. if(return_sgx== 0xFFFFFFFF)
  148. {
  149. perror("\nCould not set up the socket: had the following error: ");
  150. fflush(stderr);
  151. }
  152. else
  153. {
  154. printf("\nHad the following error in SGX local attestation: 0x%x", return_sgx);
  155. fflush(stdout);
  156. }
  157. return return_sgx;
  158. }
  159. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  160. fflush(stdout);
  161. // sleep(50);
  162. 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");
  163. return_sgx = unseal_message_from_file(sealed_file_fd, recovered_plaintext, &expected_sealed_msg_size);
  164. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  165. {
  166. printf("Successful read from file, but error in SGX unsealing: %x.\n", return_sgx);
  167. fflush(stdout);
  168. return return_sgx;
  169. }
  170. else if(return_sgx == 0xFFFFFFFF)
  171. {
  172. perror("\n Could not read the file.\n");
  173. fflush(stdout);
  174. return return_sgx;
  175. }
  176. printf("\n Unsealed the keypair.\n");
  177. fflush(stdout);
  178. std::string path("../apache/source/code/path");
  179. return_internal = verify_apache(path, apache_signature_keypair_private);
  180. if(return_internal != 0)
  181. {
  182. printf("\nThe signed manifest was not created due to the above errors.\n");
  183. fflush(stdout);
  184. return return_internal;
  185. }
  186. printf("Successfully verified the Apache enclave and signed its manifest.\n");
  187. fflush(stdout);
  188. */
  189. return 0;
  190. }