c_rehash 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/bin/sh
  2. #
  3. # Ben Secrest <blsecres@gmail.com>
  4. #
  5. # sh c_rehash script, scan all files in a directory
  6. # and add symbolic links to their hash values.
  7. #
  8. # based on the c_rehash perl script distributed with openssl
  9. #
  10. # LICENSE: See OpenSSL license
  11. # ^^acceptable?^^
  12. #
  13. # default certificate location
  14. DIR=/opt/ti-processor-sdk-linux-am335x-evm-04.02.00.09/linux-devkit/sysroots/x86_64-arago-linux/etc/ssl
  15. # for filetype bitfield
  16. IS_CERT=$(( 1 << 0 ))
  17. IS_CRL=$(( 1 << 1 ))
  18. # check to see if a file is a certificate file or a CRL file
  19. # arguments:
  20. # 1. the filename to be scanned
  21. # returns:
  22. # bitfield of file type; uses ${IS_CERT} and ${IS_CRL}
  23. #
  24. check_file()
  25. {
  26. local IS_TYPE=0
  27. # make IFS a newline so we can process grep output line by line
  28. local OLDIFS=${IFS}
  29. IFS=$( printf "\n" )
  30. # XXX: could be more efficient to have two 'grep -m' but is -m portable?
  31. for LINE in $( grep '^-----BEGIN .*-----' ${1} )
  32. do
  33. if echo ${LINE} \
  34. | grep -q -E '^-----BEGIN (X509 |TRUSTED )?CERTIFICATE-----'
  35. then
  36. IS_TYPE=$(( ${IS_TYPE} | ${IS_CERT} ))
  37. if [ $(( ${IS_TYPE} & ${IS_CRL} )) -ne 0 ]
  38. then
  39. break
  40. fi
  41. elif echo ${LINE} | grep -q '^-----BEGIN X509 CRL-----'
  42. then
  43. IS_TYPE=$(( ${IS_TYPE} | ${IS_CRL} ))
  44. if [ $(( ${IS_TYPE} & ${IS_CERT} )) -ne 0 ]
  45. then
  46. break
  47. fi
  48. fi
  49. done
  50. # restore IFS
  51. IFS=${OLDIFS}
  52. return ${IS_TYPE}
  53. }
  54. #
  55. # use openssl to fingerprint a file
  56. # arguments:
  57. # 1. the filename to fingerprint
  58. # 2. the method to use (x509, crl)
  59. # returns:
  60. # none
  61. # assumptions:
  62. # user will capture output from last stage of pipeline
  63. #
  64. fingerprint()
  65. {
  66. ${SSL_CMD} ${2} -fingerprint -noout -in ${1} | sed 's/^.*=//' | tr -d ':'
  67. }
  68. #
  69. # link_hash - create links to certificate files
  70. # arguments:
  71. # 1. the filename to create a link for
  72. # 2. the type of certificate being linked (x509, crl)
  73. # returns:
  74. # 0 on success, 1 otherwise
  75. #
  76. link_hash()
  77. {
  78. local FINGERPRINT=$( fingerprint ${1} ${2} )
  79. local HASH=$( ${SSL_CMD} ${2} -hash -noout -in ${1} )
  80. local SUFFIX=0
  81. local LINKFILE=''
  82. local TAG=''
  83. if [ ${2} = "crl" ]
  84. then
  85. TAG='r'
  86. fi
  87. LINKFILE=${HASH}.${TAG}${SUFFIX}
  88. while [ -f ${LINKFILE} ]
  89. do
  90. if [ ${FINGERPRINT} = $( fingerprint ${LINKFILE} ${2} ) ]
  91. then
  92. echo "NOTE: Skipping duplicate file ${1}" >&2
  93. return 1
  94. fi
  95. SUFFIX=$(( ${SUFFIX} + 1 ))
  96. LINKFILE=${HASH}.${TAG}${SUFFIX}
  97. done
  98. echo "${3} => ${LINKFILE}"
  99. # assume any system with a POSIX shell will either support symlinks or
  100. # do something to handle this gracefully
  101. ln -s ${3} ${LINKFILE}
  102. return 0
  103. }
  104. # hash_dir create hash links in a given directory
  105. hash_dir()
  106. {
  107. echo "Doing ${1}"
  108. cd ${1}
  109. ls -1 * 2>/dev/null | while read FILE
  110. do
  111. if echo ${FILE} | grep -q -E '^[[:xdigit:]]{8}\.r?[[:digit:]]+$' \
  112. && [ -h "${FILE}" ]
  113. then
  114. rm ${FILE}
  115. fi
  116. done
  117. ls -1 *.pem *.cer *.crt *.crl 2>/dev/null | while read FILE
  118. do
  119. REAL_FILE=${FILE}
  120. # if we run on build host then get to the real files in rootfs
  121. if [ -n "${SYSROOT}" -a -h ${FILE} ]
  122. then
  123. FILE=$( readlink ${FILE} )
  124. # check the symlink is absolute (or dangling in other word)
  125. if [ "x/" = "x$( echo ${FILE} | cut -c1 -)" ]
  126. then
  127. REAL_FILE=${SYSROOT}/${FILE}
  128. fi
  129. fi
  130. check_file ${REAL_FILE}
  131. local FILE_TYPE=${?}
  132. local TYPE_STR=''
  133. if [ $(( ${FILE_TYPE} & ${IS_CERT} )) -ne 0 ]
  134. then
  135. TYPE_STR='x509'
  136. elif [ $(( ${FILE_TYPE} & ${IS_CRL} )) -ne 0 ]
  137. then
  138. TYPE_STR='crl'
  139. else
  140. echo "NOTE: ${FILE} does not contain a certificate or CRL: skipping" >&2
  141. continue
  142. fi
  143. link_hash ${REAL_FILE} ${TYPE_STR} ${FILE}
  144. done
  145. }
  146. # choose the name of an ssl application
  147. if [ -n "${OPENSSL}" ]
  148. then
  149. SSL_CMD=$(which ${OPENSSL} 2>/dev/null)
  150. else
  151. SSL_CMD=/usr/bin/openssl
  152. OPENSSL=${SSL_CMD}
  153. export OPENSSL
  154. fi
  155. # fix paths
  156. PATH=${PATH}:${DIR}/bin
  157. export PATH
  158. # confirm existance/executability of ssl command
  159. if ! [ -x ${SSL_CMD} ]
  160. then
  161. echo "${0}: rehashing skipped ('openssl' program not available)" >&2
  162. exit 0
  163. fi
  164. # determine which directories to process
  165. old_IFS=$IFS
  166. if [ ${#} -gt 0 ]
  167. then
  168. IFS=':'
  169. DIRLIST=${*}
  170. elif [ -n "${SSL_CERT_DIR}" ]
  171. then
  172. DIRLIST=$SSL_CERT_DIR
  173. else
  174. DIRLIST=${DIR}/certs
  175. fi
  176. IFS=':'
  177. # process directories
  178. for CERT_DIR in ${DIRLIST}
  179. do
  180. if [ -d ${CERT_DIR} -a -w ${CERT_DIR} ]
  181. then
  182. IFS=$old_IFS
  183. hash_dir ${CERT_DIR}
  184. IFS=':'
  185. fi
  186. done