systemMain.cpp 6.6 KB

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