fips_premain.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* ====================================================================
  2. * Copyright (c) 2005 The OpenSSL Project. Rights for redistribution
  3. * and usage in source and binary forms are granted according to the
  4. * OpenSSL license.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #if defined(__unix) || defined(__unix__) || defined(__ANDROID__)
  10. #include <unistd.h>
  11. #endif
  12. #ifndef FINGERPRINT_PREMAIN_DSO_LOAD
  13. #if defined(__GNUC__) && __GNUC__>=2
  14. void FINGERPRINT_premain(void) __attribute__((constructor));
  15. /* Most commonly this results in pointer to premain to be dropped
  16. * to .ctors segment, which is traversed by GCC crtbegin.o upon
  17. * program startup. Except on a.out OpenBSD where it results in
  18. * _GLOBAL_$I$premain() {premain();} being auto-generated by
  19. * compiler... But one way or another this is believed to cover
  20. * *all* GCC targets. */
  21. #elif defined(_MSC_VER)
  22. # ifdef _WINDLL
  23. __declspec(dllexport) /* this is essentially cosmetics... */
  24. # endif
  25. void FINGERPRINT_premain(void);
  26. static int premain_wrapper(void) { FINGERPRINT_premain(); return 0; }
  27. # ifdef _WIN64
  28. # pragma section(".CRT$XCU",read)
  29. __declspec(allocate(".CRT$XCU"))
  30. # else
  31. # pragma data_seg(".CRT$XCU")
  32. # endif
  33. static int (*p)(void) = premain_wrapper;
  34. /* This results in pointer to premain to appear in .CRT segment,
  35. * which is traversed by Visual C run-time initialization code.
  36. * This applies to both Win32 and [all flavors of] Win64. */
  37. # pragma data_seg()
  38. #elif defined(__SUNPRO_C)
  39. void FINGERPRINT_premain(void);
  40. # pragma init(FINGERPRINT_premain)
  41. /* This results in a call to premain to appear in .init segment. */
  42. #elif defined(__DECC) && (defined(__VMS) || defined(VMS))
  43. void FINGERPRINT_premain(void);
  44. # pragma __nostandard
  45. globaldef { "LIB$INITIALIZ" } readonly _align (LONGWORD)
  46. int spare[8] = {0};
  47. globaldef { "LIB$INITIALIZE" } readonly _align (LONGWORD)
  48. void (*x_FINGERPRINT_premain)(void) = FINGERPRINT_premain;
  49. /* Refer to LIB$INITIALIZE to ensure it exists in the image. */
  50. int lib$initialize();
  51. globaldef int (*lib_init_ref)() = lib$initialize;
  52. # pragma __standard
  53. #elif 0
  54. The rest has to be taken care of through command line:
  55. -Wl,-init,FINGERPRINT_premain on OSF1 and IRIX
  56. -Wl,+init,FINGERPRINT_premain on HP-UX
  57. -Wl,-binitfini:FINGERPRINT_premain on AIX
  58. On ELF platforms this results in a call to premain to appear in
  59. .init segment...
  60. #endif
  61. #ifndef HMAC_SHA1_SIG
  62. #define HMAC_SHA1_SIG "?have to make sure this string is unique"
  63. #endif
  64. static const unsigned char FINGERPRINT_ascii_value[41] = HMAC_SHA1_SIG;
  65. #define atox(c) ((c)>='a'?((c)-'a'+10):((c)>='A'?(c)-'A'+10:(c)-'0'))
  66. extern const void *FIPS_text_start(), *FIPS_text_end();
  67. extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
  68. extern unsigned char FIPS_signature[20];
  69. extern unsigned int FIPS_incore_fingerprint(unsigned char *,unsigned int);
  70. /*
  71. * As name suggests this code is executed prior main(). We use this
  72. * opportunity to fingerprint sequestered code in virtual address
  73. * space of target application.
  74. */
  75. void FINGERPRINT_premain(void)
  76. { unsigned char sig[sizeof(FIPS_signature)];
  77. const unsigned char * volatile p=FINGERPRINT_ascii_value;
  78. unsigned int len=sizeof(sig),i;
  79. /* "volatilization" is done to disengage unwanted optimization... */
  80. if (*((volatile unsigned char *)p)=='?')
  81. { if (FIPS_text_start()==NULL)
  82. { fprintf(stderr,"FIPS_text_start() returns NULL\n");
  83. _exit(1);
  84. }
  85. #if defined(DEBUG_FINGERPRINT_PREMAIN)
  86. fprintf(stderr,".text:%p+%d=%p\n",FIPS_text_start(),
  87. (int)((size_t)FIPS_text_end()-(size_t)FIPS_text_start()),
  88. FIPS_text_end());
  89. fprintf(stderr,".rodata:%p+%d=%p\n",FIPS_rodata_start,
  90. (int)((size_t)FIPS_rodata_end-(size_t)FIPS_rodata_start),
  91. FIPS_rodata_end);
  92. #endif
  93. len=FIPS_incore_fingerprint(sig,sizeof(sig));
  94. if (len!=sizeof(sig))
  95. { fprintf(stderr,"fingerprint length mismatch: %u\n",len);
  96. _exit(1);
  97. }
  98. for (i=0;i<len;i++) printf("%02x",sig[i]);
  99. printf("\n");
  100. fflush(stdout);
  101. _exit(0);
  102. }
  103. else if (FIPS_signature[0]=='\0') do
  104. { for (i=0;i<sizeof(FIPS_signature);i++,p+=2)
  105. FIPS_signature[i] = (atox(p[0])<<4)|atox(p[1]);
  106. #if defined(DEBUG_FINGERPRINT_PREMAIN)
  107. if (getenv("OPENSSL_FIPS")==NULL) break;
  108. len=FIPS_incore_fingerprint(sig,sizeof(sig));
  109. if (memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
  110. { fprintf(stderr,"FINGERPRINT_premain: FIPS_signature mismatch\n");
  111. _exit(1);
  112. }
  113. #endif
  114. } while(0);
  115. }
  116. #else
  117. #include <openssl/bio.h>
  118. #include <openssl/dso.h>
  119. #include <openssl/err.h>
  120. int main(int argc,char *argv[])
  121. { DSO *dso;
  122. DSO_FUNC_TYPE func;
  123. BIO *bio_err;
  124. if (argc < 2)
  125. { fprintf (stderr,"usage: %s libcrypto.dso\n",argv[0]);
  126. return 1;
  127. }
  128. if ((bio_err=BIO_new(BIO_s_file())) == NULL)
  129. { fprintf (stderr,"unable to allocate BIO\n");
  130. return 1;
  131. }
  132. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  133. ERR_load_crypto_strings();
  134. dso = DSO_load(NULL,argv[1],NULL,DSO_FLAG_NO_NAME_TRANSLATION);
  135. if (dso == NULL)
  136. { ERR_print_errors(bio_err);
  137. return 1;
  138. }
  139. /* This is not normally reached, because FINGERPRINT_premain should
  140. * have executed and terminated application already upon DSO_load... */
  141. func = DSO_bind_func(dso,"FINGERPRINT_premain");
  142. if (func == NULL)
  143. { ERR_print_errors(bio_err);
  144. return 1;
  145. }
  146. (*func)();
  147. return 0;
  148. }
  149. #endif