systemMain.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. 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. uint8_t 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};
  144. // TODO: Error conditions.
  145. DeploymentStageLogic deploymentStage;
  146. deploymentStage.set_target_hash(hash);
  147. deploymentStage.set_up_socket_connect(DECRYPTOR_PORT);
  148. deploymentStage.main_logic();
  149. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  150. fflush(stdout);
  151. /*
  152. // sleep(50);
  153. 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");
  154. return_sgx = unseal_message_from_file(sealed_file_fd, recovered_plaintext, &expected_sealed_msg_size);
  155. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  156. {
  157. printf("Successful read from file, but error in SGX unsealing: %x.\n", return_sgx);
  158. fflush(stdout);
  159. return return_sgx;
  160. }
  161. else if(return_sgx == 0xFFFFFFFF)
  162. {
  163. perror("\n Could not read the file.\n");
  164. fflush(stdout);
  165. return return_sgx;
  166. }
  167. printf("\n Unsealed the keypair.\n");
  168. fflush(stdout);
  169. std::string path("../apache/source/code/path");
  170. return_internal = verify_apache(path, apache_signature_keypair_private);
  171. if(return_internal != 0)
  172. {
  173. printf("\nThe signed manifest was not created due to the above errors.\n");
  174. fflush(stdout);
  175. return return_internal;
  176. }
  177. printf("Successfully verified the Apache enclave and signed its manifest.\n");
  178. fflush(stdout);
  179. */
  180. return 0;
  181. }