3 # Copyright (c) 2005-2007 The OpenSSL Project.
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.
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.
15 CC=${FIPSLD_CC:-${CC}}
16 [ -n "${CC}" ] || { echo '$CC is not defined'; exit 1; }
18 # Initially -c wasn't intended to be interpreted here, but it might
19 # make life easier for those who want to build FIPS-ified applications
20 # with minimal [if any] modifications to their Makefiles...
21 ( while [ "x$1" != "x" -a "x$1" != "x-c" -a "x$1" != "x-E" ]; do shift; done;
25 TARGET=`(while [ "x$1" != "x" -a "x$1" != "x-o" ]; do shift; done; echo $2)`
27 # If using an auto-tooled (autoconf/automake/libtool) project,
28 # configure will fail when testing the compiler or even performing
29 # simple checks. Pass-through to compiler directly if application is
30 # is not being linked with libcrypto, allowing auto-tooled applications
31 # to utilize fipsld (e.g. CC=/usr/local/ssl/bin/fipsld FIPSLD_CC=gcc
32 # ./configure && make). But keep in mind[!] that if certified code
33 # resides in a shared library, then fipsld *may not* be used and
34 # end-developer should not modify application configuration and build
35 # procedures. This is because in-core fingerprint and associated
36 # procedures are already embedded into and executed in shared library
38 case `basename "${TARGET}"` in
39 libcrypto*|libfips*|*.dll) ;;
41 *libcrypto.a*|*-lcrypto*) ;;
46 [ -n "${TARGET}" ] || { echo 'no -o specified'; exit 1; }
48 # Turn on debugging output?
49 ( while [ "x$1" != "x" -a "x$1" != "x-DDEBUG_FINGERPRINT_PREMAIN" ]; do shift; done;
53 THERE="`echo $0 | sed -e 's|[^/]*$||'`"..
55 # If set, FIPSLIBDIR is location of installed validated FIPS module
56 if [ -n "${FIPSLIBDIR}" ]; then
57 CANISTER_O="${FIPSLIBDIR}/fipscanister.o"
58 elif [ -f "${THERE}/fips-1.0/fipscanister.o" ]; then
59 CANISTER_O="${THERE}/fips-1.0/fipscanister.o"
60 elif [ -f "${THERE}/lib/fipscanister.o" ]; then
61 CANISTER_O="${THERE}/lib/fipscanister.o"
63 [ -f ${CANISTER_O} ] || { echo "unable to find ${CANISTER_O}"; exit 1; }
65 PREMAIN_C=`dirname "${CANISTER_O}"`/fips_premain.c
67 HMAC_KEY="etaonrishdlcupfm"
69 case "`(uname -s) 2>/dev/null`" in
70 OSF1|IRIX*) _WL_PREMAIN="-Wl,-init,FINGERPRINT_premain" ;;
71 HP-UX) _WL_PREMAIN="-Wl,+init,FINGERPRINT_premain" ;;
72 AIX) _WL_PREMAIN="-Wl,-binitfini:FINGERPRINT_premain,-bnoobjreorder";;
73 Darwin) ( while [ "x$1" != "x" -a "x$1" != "x-dynamiclib" ]; do shift; done;
75 ) && _WL_PREMAIN="-Wl,-init,_FINGERPRINT_premain" ;;
79 [!/]*) TARGET=./${TARGET} ;;
82 case `basename "${TARGET}"` in
84 # libfips.so creation can be taking place in the source
86 FINGERTYPE="${THERE}/fips-1.0/fips_standalone_sha1"
87 # fipscanister.o should be specified on command line...
88 CANISTER_O=`(while [ "x$1" != "x" ]; do case "$1" in *fipscanister.o) echo $1; exit;; esac; shift; done)`
89 [ -n "$CANISTER_O" ] || { echo "fipscanister.o is not found"; exit 1; }
90 PREMAIN_C=`dirname "${CANISTER_O}"`/fips_premain.c
92 # verify fipspremain.c against its detached signature...
93 ${FINGERTYPE} "${PREMAIN_C}" | sed "s/(.*\//(/" | \
94 diff -w "${PREMAIN_C}.sha1" - || \
95 { echo "${PREMAIN_C} fingerprint mismatch"; exit 1; }
96 # verify fipscanister.o against its detached signature...
97 ${FINGERTYPE} "${CANISTER_O}" | sed "s/(.*\//(/" | \
98 diff -w "${CANISTER_O}.sha1" - || \
99 { echo "${CANISTER_O} fingerprint mismatch"; exit 1; }
101 /bin/rm -f "${TARGET}"
102 ${CC} "${PREMAIN_C}" ${_WL_PREMAIN} "$@"
104 # generate signature...
105 SIG=`"${THERE}/fips-1.0/fips_premain_dso" "${TARGET}"`
106 /bin/rm -f "${TARGET}"
107 if [ -z "${SIG}" ]; then
108 echo "unable to collect signature"; exit 1
111 # recompile with signature...
112 ${CC} -DHMAC_SHA1_SIG=\"${SIG}\" "${PREMAIN_C}" ${_WL_PREMAIN} "$@"
114 libcrypto*|*.dll) # must be linking a shared lib...
115 # Shared lib creation can be taking place in the source
116 # directory only, but fipscanister.o can reside elsewhere...
117 FINGERTYPE="${THERE}/fips-1.0/fips_standalone_sha1"
119 # verify fipspremain.c against its detached signature...
120 ${FINGERTYPE} "${PREMAIN_C}" | sed "s/(.*\//(/" | \
121 diff -w "${PREMAIN_C}.sha1" - || \
122 { echo "${PREMAIN_C} fingerprint mismatch"; exit 1; }
123 # verify fipscanister.o against its detached signature...
124 ${FINGERTYPE} "${CANISTER_O}" | sed "s/(.*\//(/" | \
125 diff -w "${CANISTER_O}.sha1" - || \
126 { echo "${CANISTER_O} fingerprint mismatch"; exit 1; }
128 # Temporarily remove fipscanister.o from libcrypto.a!
129 # We are required to use the standalone copy...
130 trap 'ar r "${THERE}/libcrypto.a" "${CANISTER_O}";
131 (ranlib "${THERE}/libcrypto.a") 2>/dev/null;
133 touch -c "${TARGET}"' 0
135 ar d "${THERE}/libcrypto.a" fipscanister.o 2>&1 > /dev/null || :
136 (ranlib "${THERE}/libcrypto.a") 2>/dev/null || :
138 /bin/rm -f "${TARGET}"
139 ${CC} "${CANISTER_O}" \
143 # generate signature...
144 SIG=`"${THERE}/fips-1.0/fips_premain_dso" "${TARGET}"`
145 /bin/rm -f "${TARGET}"
146 if [ -z "${SIG}" ]; then
147 echo "unable to collect signature"; exit 1
150 # recompile with signature...
151 ${CC} "${CANISTER_O}" \
152 -DHMAC_SHA1_SIG=\"${SIG}\" "${PREMAIN_C}" \
156 *) # must be linking statically...
157 # Static linking can be taking place either in the source
158 # directory or off the installed binary target destination.
159 if [ -x "${THERE}/fips-1.0/fips_standalone_sha1" ]; then
160 FINGERTYPE="${THERE}/fips-1.0/fips_standalone_sha1"
161 else # Installed tree is expected to contain
162 # lib/fipscanister.o, lib/fipscanister.o.sha1 and
163 # lib/fips_premain.c [not to mention bin/openssl].
164 FINGERTYPE="${THERE}/bin/openssl sha1 -hmac ${HMAC_KEY}"
167 # verify fipscanister.o against its detached signature...
168 ${FINGERTYPE} "${CANISTER_O}" | sed "s/(.*\//(/" | \
169 diff -w "${CANISTER_O}.sha1" - || \
170 { echo "${CANISTER_O} fingerprint mismatch"; exit 1; }
172 # verify fips_premain.c against its detached signature...
173 ${FINGERTYPE} "${PREMAIN_C}" | sed "s/(.*\//(/" | \
174 diff -w "${PREMAIN_C}.sha1" - || \
175 { echo "${PREMAIN_C} fingerprint mismatch"; exit 1; }
177 /bin/rm -f "${TARGET}"
178 ${CC} "${CANISTER_O}" \
182 # generate signature...
184 /bin/rm -f "${TARGET}"
185 if [ -z "${SIG}" ]; then
186 echo "unable to collect signature"; exit 1
189 # recompile with signature...
190 ${CC} "${CANISTER_O}" \
191 -DHMAC_SHA1_SIG=\"${SIG}\" "${PREMAIN_C}" \