fipsld 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/bin/sh -e
  2. #
  3. # Copyright (c) 2005-2011 The OpenSSL Project.
  4. #
  5. # Depending on output file name, the script either embeds fingerprint
  6. # into libcrypto.so or static application. "Static" refers to static
  7. # libcrypto.a, not [necessarily] application per se.
  8. #
  9. # Even though this script is called fipsld, it expects C compiler
  10. # command line syntax and $FIPSLD_CC or $CC environment variable set
  11. # and can even be used to compile source files.
  12. #set -x
  13. CC=${FIPSLD_CC:-${CC}}
  14. [ -n "${CC}" ] || { echo '$CC is not defined'; exit 1; }
  15. # Initially -c wasn't intended to be interpreted here, but it might
  16. # make life easier for those who want to build FIPS-ified applications
  17. # with minimal [if any] modifications to their Makefiles...
  18. ( while [ "x$1" != "x" -a "x$1" != "x-c" -a "x$1" != "x-E" ]; do shift; done;
  19. [ $# -ge 1 ]
  20. ) && exec ${CC} "$@"
  21. TARGET=`(while [ "x$1" != "x" -a "x$1" != "x-o" ]; do shift; done; echo $2)`
  22. # If using an auto-tooled (autoconf/automake/libtool) project,
  23. # configure will fail when testing the compiler or even performing
  24. # simple checks. Pass-through to compiler directly if application is
  25. # is not being linked with libcrypto, allowing auto-tooled applications
  26. # to utilize fipsld (e.g. CC=/usr/local/ssl/bin/fipsld FIPSLD_CC=gcc
  27. # ./configure && make). But keep in mind[!] that if certified code
  28. # resides in a shared library, then fipsld *may not* be used and
  29. # end-developer should not modify application configuration and build
  30. # procedures. This is because in-core fingerprint and associated
  31. # procedures are already embedded into and executed in shared library
  32. # context.
  33. case `basename "${TARGET}"` in
  34. libcrypto*|libfips*|*.dll) ;;
  35. *) case "$*" in
  36. *libcrypto.a*|*-lcrypto*|*fipscanister.o*) ;;
  37. *) exec ${CC} "$@" ;;
  38. esac
  39. esac
  40. [ -n "${TARGET}" ] || { echo 'no -o specified'; exit 1; }
  41. # Turn on debugging output?
  42. ( while [ "x$1" != "x" -a "x$1" != "x-DDEBUG_FINGERPRINT_PREMAIN" ]; do shift; done;
  43. [ $# -ge 1 ]
  44. ) && set -x
  45. THERE="`echo $0 | sed -e 's|[^/]*$||'`"..
  46. # fipscanister.o can appear in command line
  47. CANISTER_O=`(while [ "x$1" != "x" ]; do case "$1" in *fipscanister.o) echo $1; exit;; esac; shift; done)`
  48. if [ -z "${CANISTER_O}" ]; then
  49. # If set, FIPSLIBDIR is location of installed validated FIPS module
  50. if [ -n "${FIPSLIBDIR}" ]; then
  51. CANISTER_O="${FIPSLIBDIR}/fipscanister.o"
  52. elif [ -f "${THERE}/fips/fipscanister.o" ]; then
  53. CANISTER_O="${THERE}/fips/fipscanister.o"
  54. elif [ -f "${THERE}/lib/fipscanister.o" ]; then
  55. CANISTER_O="${THERE}/lib/fipscanister.o"
  56. fi
  57. CANISTER_O_CMD="${CANISTER_O}"
  58. fi
  59. [ -f ${CANISTER_O} ] || { echo "unable to find ${CANISTER_O}"; exit 1; }
  60. PREMAIN_C=`dirname "${CANISTER_O}"`/fips_premain.c
  61. HMAC_KEY="etaonrishdlcupfm"
  62. case "`(uname -s) 2>/dev/null`" in
  63. OSF1|IRIX*) _WL_PREMAIN="-Wl,-init,FINGERPRINT_premain" ;;
  64. HP-UX) _WL_PREMAIN="-Wl,+init,FINGERPRINT_premain" ;;
  65. AIX) _WL_PREMAIN="-Wl,-binitfini:FINGERPRINT_premain,-bnoobjreorder";;
  66. Darwin) ( while [ "x$1" != "x" -a "x$1" != "x-dynamiclib" ]; do shift; done;
  67. [ $# -ge 1 ]
  68. ) && _WL_PREMAIN="-Wl,-init,_FINGERPRINT_premain" ;;
  69. esac
  70. case "${TARGET}" in
  71. [!/]*) TARGET=./${TARGET} ;;
  72. esac
  73. case `basename "${TARGET}"` in
  74. lib*|*.dll) # must be linking a shared lib...
  75. # Shared lib creation can be taking place in the source
  76. # directory only, but fipscanister.o can reside elsewhere...
  77. if [ -x "${THERE}/fips/fips_standalone_sha1" ]; then
  78. FINGERTYPE="${THERE}/fips/fips_standalone_sha1"
  79. PREMAIN_DSO="${THERE}/fips/fips_premain_dso"
  80. elif [ -x "${THERE}/bin/fips_standalone_sha1" ]; then
  81. FINGERTYPE="${THERE}/bin/fips_standalone_sha1"
  82. PREMAIN_DSO="./fips_premain_dso"
  83. fi
  84. # verify fipspremain.c against its detached signature...
  85. ${FINGERTYPE} "${PREMAIN_C}" | sed "s/(.*\//(/" | \
  86. diff -w "${PREMAIN_C}.sha1" - || \
  87. { echo "${PREMAIN_C} fingerprint mismatch"; exit 1; }
  88. # verify fipscanister.o against its detached signature...
  89. ${FINGERTYPE} "${CANISTER_O}" | sed "s/(.*\//(/" | \
  90. diff -w "${CANISTER_O}.sha1" - || \
  91. { echo "${CANISTER_O} fingerprint mismatch"; exit 1; }
  92. [ -z "${FIPSLD_LIBCRYPTO}" -a -f "${THERE}/libcrypto.a" ] && \
  93. FIPSLD_LIBCRYPTO="${THERE}/libcrypto.a"
  94. # Temporarily remove fipscanister.o from libcrypto.a!
  95. # We are required to use the standalone copy...
  96. if [ -n "${FIPSLD_LIBCRYPTO}" ]; then
  97. if ar d "${FIPSLD_LIBCRYPTO}" fipscanister.o; then
  98. (ranlib "${FIPSLD_LIBCRYPTO}") 2>/dev/null || :
  99. trap 'ar r "${FIPSLD_LIBCRYPTO}" "${CANISTER_O}";
  100. (ranlib "${FIPSLD_LIBCRYPTO}") 2>/dev/null || :;
  101. sleep 1;
  102. touch -c "${TARGET}"' 0
  103. fi
  104. fi
  105. /bin/rm -f "${TARGET}"
  106. ${CC} ${CANISTER_O_CMD:+"${CANISTER_O_CMD}"} \
  107. "${PREMAIN_C}" \
  108. ${_WL_PREMAIN} "$@"
  109. if [ "x${FIPS_SIG}" != "x" ]; then
  110. # embed signature
  111. "${FIPS_SIG}" "${TARGET}"
  112. [ $? -ne 42 ] && exit $?
  113. fi
  114. # generate signature...
  115. SIG=`"${PREMAIN_DSO}" "${TARGET}"`
  116. /bin/rm -f "${TARGET}"
  117. if [ -z "${SIG}" ]; then
  118. echo "unable to collect signature"; exit 1
  119. fi
  120. # recompile with signature...
  121. ${CC} ${CANISTER_O_CMD:+"${CANISTER_O_CMD}"} \
  122. -DHMAC_SHA1_SIG=\"${SIG}\" "${PREMAIN_C}" \
  123. ${_WL_PREMAIN} "$@"
  124. ;;
  125. *) # must be linking statically...
  126. # Static linking can be taking place either in the source
  127. # directory or off the installed binary target destination.
  128. if [ -x "${THERE}/fips/fips_standalone_sha1" ]; then
  129. FINGERTYPE="${THERE}/fips/fips_standalone_sha1"
  130. elif [ -x "${THERE}/bin/fips_standalone_sha1" ]; then
  131. FINGERTYPE="${THERE}/bin/fips_standalone_sha1"
  132. else # Installed tree is expected to contain
  133. # lib/fipscanister.o, lib/fipscanister.o.sha1 and
  134. # lib/fips_premain.c [not to mention bin/openssl].
  135. FINGERTYPE="${THERE}/bin/openssl sha1 -hmac ${HMAC_KEY}"
  136. fi
  137. # verify fipscanister.o against its detached signature...
  138. ${FINGERTYPE} "${CANISTER_O}" | sed "s/(.*\//(/" | \
  139. diff -w "${CANISTER_O}.sha1" - || \
  140. { echo "${CANISTER_O} fingerprint mismatch"; exit 1; }
  141. # verify fips_premain.c against its detached signature...
  142. ${FINGERTYPE} "${PREMAIN_C}" | sed "s/(.*\//(/" | \
  143. diff -w "${PREMAIN_C}.sha1" - || \
  144. { echo "${PREMAIN_C} fingerprint mismatch"; exit 1; }
  145. /bin/rm -f "${TARGET}"
  146. ${CC} ${CANISTER_O_CMD:+"${CANISTER_O_CMD}"} \
  147. "${PREMAIN_C}" \
  148. ${_WL_PREMAIN} "$@"
  149. if [ "x${FIPS_SIG}" != "x" ]; then
  150. # embed signature
  151. "${FIPS_SIG}" "${TARGET}"
  152. [ $? -ne 42 ] && exit $?
  153. fi
  154. # generate signature...
  155. SIG=`"${TARGET}"`
  156. /bin/rm -f "${TARGET}"
  157. if [ -z "${SIG}" ]; then
  158. echo "unable to collect signature"; exit 1
  159. fi
  160. # recompile with signature...
  161. ${CC} ${CANISTER_O_CMD:+"${CANISTER_O_CMD}"} \
  162. -DHMAC_SHA1_SIG=\"${SIG}\" "${PREMAIN_C}" \
  163. ${_WL_PREMAIN} "$@"
  164. ;;
  165. esac