fips_premain.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(__vxworks) || defined(__ANDROID__) || defined(__APPLE__)
  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 defined(_TMS320C6X)
  54. # if defined(__TI_EABI__)
  55. asm("\t.sect \".init_array\"\n\t.align 4\n\t.field FINGERPRINT_premain,32");
  56. # else
  57. asm("\t.sect \".pinit\"\n\t.align 4\n\t.field _FINGERPRINT_premain,32");
  58. # endif
  59. #elif 0
  60. The rest has to be taken care of through command line:
  61. -Wl,-init,FINGERPRINT_premain on OSF1 and IRIX
  62. -Wl,+init,FINGERPRINT_premain on HP-UX
  63. -Wl,-binitfini:FINGERPRINT_premain on AIX
  64. On ELF platforms this results in a call to premain to appear in
  65. .init segment...
  66. #endif
  67. #ifndef HMAC_SHA1_SIG
  68. #define HMAC_SHA1_SIG "?have to make sure this string is unique"
  69. #endif
  70. #if defined(_MSC_VER)
  71. # pragma const_seg("fipsro")
  72. # pragma const_seg()
  73. __declspec(allocate("fipsro"))
  74. #endif
  75. static const unsigned char FINGERPRINT_ascii_value[41] = HMAC_SHA1_SIG;
  76. #define atox(c) ((c)>='a'?((c)-'a'+10):((c)>='A'?(c)-'A'+10:(c)-'0'))
  77. extern const void *FIPS_text_start(), *FIPS_text_end();
  78. extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
  79. extern unsigned char FIPS_signature[20];
  80. extern unsigned int FIPS_incore_fingerprint(unsigned char *,unsigned int);
  81. /*
  82. * As name suggests this code is executed prior main(). We use this
  83. * opportunity to fingerprint sequestered code in virtual address
  84. * space of target application.
  85. */
  86. void FINGERPRINT_premain(void)
  87. { unsigned char sig[sizeof(FIPS_signature)];
  88. const unsigned char * volatile p=FINGERPRINT_ascii_value;
  89. unsigned int len=sizeof(sig),i;
  90. /* "volatilization" is done to disengage unwanted optimization... */
  91. if (*((volatile unsigned char *)p)=='?')
  92. { if (FIPS_text_start()==NULL)
  93. { fprintf(stderr,"FIPS_text_start() returns NULL\n");
  94. _exit(1);
  95. }
  96. #if defined(DEBUG_FINGERPRINT_PREMAIN)
  97. fprintf(stderr,".text:%p+%d=%p\n",FIPS_text_start(),
  98. (int)((size_t)FIPS_text_end()-(size_t)FIPS_text_start()),
  99. FIPS_text_end());
  100. fprintf(stderr,".rodata:%p+%d=%p\n",FIPS_rodata_start,
  101. (int)((size_t)FIPS_rodata_end-(size_t)FIPS_rodata_start),
  102. FIPS_rodata_end);
  103. #endif
  104. len=FIPS_incore_fingerprint(sig,sizeof(sig));
  105. if (len!=sizeof(sig))
  106. { fprintf(stderr,"fingerprint length mismatch: %u\n",len);
  107. _exit(1);
  108. }
  109. for (i=0;i<len;i++) printf("%02x",sig[i]);
  110. printf("\n");
  111. fflush(stdout);
  112. _exit(0);
  113. }
  114. else if (FIPS_signature[0]=='\0') do
  115. { for (i=0;i<sizeof(FIPS_signature);i++,p+=2)
  116. FIPS_signature[i] = (atox(p[0])<<4)|atox(p[1]);
  117. #if defined(DEBUG_FINGERPRINT_PREMAIN)
  118. if (getenv("OPENSSL_FIPS")==NULL) break;
  119. len=FIPS_incore_fingerprint(sig,sizeof(sig));
  120. if (memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
  121. { fprintf(stderr,"FINGERPRINT_premain: FIPS_signature mismatch\n");
  122. _exit(1);
  123. }
  124. #endif
  125. } while(0);
  126. #if defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC)
  127. fips_openssl_cpuid_setup();
  128. #endif
  129. }
  130. #else
  131. #include <openssl/bio.h>
  132. #include <openssl/dso.h>
  133. #include <openssl/err.h>
  134. int main(int argc,char *argv[])
  135. { DSO *dso;
  136. DSO_FUNC_TYPE func;
  137. BIO *bio_err;
  138. if (argc < 2)
  139. { fprintf (stderr,"usage: %s libcrypto.dso\n",argv[0]);
  140. return 1;
  141. }
  142. if ((bio_err=BIO_new(BIO_s_file())) == NULL)
  143. { fprintf (stderr,"unable to allocate BIO\n");
  144. return 1;
  145. }
  146. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  147. ERR_load_crypto_strings();
  148. dso = DSO_load(NULL,argv[1],NULL,DSO_FLAG_NO_NAME_TRANSLATION);
  149. if (dso == NULL)
  150. { ERR_print_errors(bio_err);
  151. return 1;
  152. }
  153. /* This is not normally reached, because FINGERPRINT_premain should
  154. * have executed and terminated application already upon DSO_load... */
  155. func = DSO_bind_func(dso,"FINGERPRINT_premain");
  156. if (func == NULL)
  157. { ERR_print_errors(bio_err);
  158. return 1;
  159. }
  160. (*func)();
  161. return 0;
  162. }
  163. #endif