fips_premain.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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__)
  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. # pragma data_seg(".CRT$XCU")
  28. static int (*p)(void) = premain_wrapper;
  29. /* This results in pointer to premain to appear in .CRT segment,
  30. * which is traversed by Visual C run-time initialization code.
  31. * This applies to both Win32 and [all flavors of] Win64. */
  32. # pragma data_seg()
  33. #elif defined(__SUNPRO_C)
  34. void FINGERPRINT_premain(void);
  35. # pragma init(FINGERPRINT_premain)
  36. /* This results in a call to premain to appear in .init segment. */
  37. #elif defined(__DECC) && (defined(__VMS) || defined(VMS))
  38. void FINGERPRINT_premain(void);
  39. # pragma __nostandard
  40. globaldef { "LIB$INITIALIZ" } readonly _align (LONGWORD)
  41. int spare[8] = {0};
  42. globaldef { "LIB$INITIALIZE" } readonly _align (LONGWORD)
  43. void (*x_FINGERPRINT_premain)(void) = FINGERPRINT_premain;
  44. /* Refer to LIB$INITIALIZE to ensure it exists in the image. */
  45. int lib$initialize();
  46. globaldef int (*lib_init_ref)() = lib$initialize;
  47. # pragma __standard
  48. #elif 0
  49. The rest has to be taken care of through command line:
  50. -Wl,-init,FINGERPRINT_premain on OSF1 and IRIX
  51. -Wl,+init,FINGERPRINT_premain on HP-UX
  52. -Wl,-binitfini:FINGERPRINT_premain on AIX
  53. On ELF platforms this results in a call to premain to appear in
  54. .init segment...
  55. #endif
  56. #ifndef HMAC_SHA1_SIG
  57. #define HMAC_SHA1_SIG "?have to make sure this string is unique"
  58. #endif
  59. static const unsigned char FINGERPRINT_ascii_value[40] = HMAC_SHA1_SIG;
  60. #define atox(c) ((c)>='a'?((c)-'a'+10):((c)>='A'?(c)-'A'+10:(c)-'0'))
  61. extern const void *FIPS_text_start(), *FIPS_text_end();
  62. extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
  63. extern unsigned char FIPS_signature[20];
  64. extern unsigned int FIPS_incore_fingerprint(unsigned char *,unsigned int);
  65. /*
  66. * As name suggests this code is executed prior main(). We use this
  67. * opportunity to fingerprint sequestered code in virtual address
  68. * space of target application.
  69. */
  70. void FINGERPRINT_premain(void)
  71. { unsigned char sig[sizeof(FIPS_signature)];
  72. const unsigned char *p=FINGERPRINT_ascii_value;
  73. unsigned int len=sizeof(sig),i;
  74. /* "volatilization" is done to disengage unwanted optimization... */
  75. if (*((volatile unsigned char *)p)=='?')
  76. { if (FIPS_text_start()==NULL)
  77. { fprintf(stderr,"FIPS_text_start() returns NULL\n");
  78. _exit(1);
  79. }
  80. #if defined(DEBUG_FINGERPRINT_PREMAIN)
  81. fprintf(stderr,".text:%p+%d=%p\n",FIPS_text_start(),
  82. (int)((size_t)FIPS_text_end()-(size_t)FIPS_text_start()),
  83. FIPS_text_end());
  84. fprintf(stderr,".rodata:%p+%d=%p\n",FIPS_rodata_start,
  85. (int)((size_t)FIPS_rodata_end-(size_t)FIPS_rodata_start),
  86. FIPS_rodata_end);
  87. #endif
  88. len=FIPS_incore_fingerprint(sig,sizeof(sig));
  89. if (len!=sizeof(sig))
  90. { fprintf(stderr,"fingerprint length mismatch: %u\n",len);
  91. _exit(1);
  92. }
  93. for (i=0;i<len;i++) printf("%02x",sig[i]);
  94. printf("\n");
  95. fflush(stdout);
  96. _exit(0);
  97. }
  98. else if (FIPS_signature[0]=='\0') do
  99. { for (i=0;i<sizeof(FIPS_signature);i++,p+=2)
  100. FIPS_signature[i] = (atox(p[0])<<4)|atox(p[1]);
  101. #if defined(DEBUG_FINGERPRINT_PREMAIN)
  102. if (getenv("OPENSSL_FIPS")==NULL) break;
  103. len=FIPS_incore_fingerprint(sig,sizeof(sig));
  104. if (memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
  105. { fprintf(stderr,"FINGERPRINT_premain: FIPS_signature mismatch\n");
  106. _exit(1);
  107. }
  108. #endif
  109. } while(0);
  110. }
  111. #else
  112. #include <openssl/bio.h>
  113. #include <openssl/dso.h>
  114. #include <openssl/err.h>
  115. int main(int argc,char *argv[])
  116. { DSO *dso;
  117. DSO_FUNC_TYPE func;
  118. BIO *bio_err;
  119. if (argc < 2)
  120. { fprintf (stderr,"usage: %s libcrypto.dso\n",argv[0]);
  121. return 1;
  122. }
  123. if ((bio_err=BIO_new(BIO_s_file())) == NULL)
  124. { fprintf (stderr,"unable to allocate BIO\n");
  125. return 1;
  126. }
  127. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  128. ERR_load_crypto_strings();
  129. dso = DSO_load(NULL,argv[1],NULL,DSO_FLAG_NO_NAME_TRANSLATION);
  130. if (dso == NULL)
  131. { ERR_print_errors(bio_err);
  132. return 1;
  133. }
  134. /* This is not normally reached, because FINGERPRINT_premain should
  135. * have executed and terminated application already upon DSO_load... */
  136. func = DSO_bind_func(dso,"FINGERPRINT_premain");
  137. if (func == NULL)
  138. { ERR_print_errors(bio_err);
  139. return 1;
  140. }
  141. (*func)();
  142. return 0;
  143. }
  144. #endif