/security/nss/tests/iopr/cert_iopr.sh

http://github.com/zpao/v8monkey · Shell · 437 lines · 278 code · 37 blank · 122 comment · 22 complexity · 512c41061a1c87223b410f2c3251bd30 MD5 · raw file

  1. #! /bin/bash
  2. #
  3. # ***** BEGIN LICENSE BLOCK *****
  4. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. #
  6. # The contents of this file are subject to the Mozilla Public License Version
  7. # 1.1 (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. # http://www.mozilla.org/MPL/
  10. #
  11. # Software distributed under the License is distributed on an "AS IS" basis,
  12. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. # for the specific language governing rights and limitations under the
  14. # License.
  15. #
  16. # The Original Code is the Netscape security libraries.
  17. #
  18. # The Initial Developer of the Original Code is
  19. # Netscape Communications Corporation.
  20. # Portions created by the Initial Developer are Copyright (C) 1994-2009
  21. # the Initial Developer. All Rights Reserved.
  22. #
  23. # Contributors:
  24. #
  25. # Alternatively, the contents of this file may be used under the terms of
  26. # either the GNU General Public License Version 2 or later (the "GPL"), or
  27. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. # in which case the provisions of the GPL or the LGPL are applicable instead
  29. # of those above. If you wish to allow use of your version of this file only
  30. # under the terms of either the GPL or the LGPL, and not to allow others to
  31. # use your version of this file under the terms of the MPL, indicate your
  32. # decision by deleting the provisions above and replace them with the notice
  33. # and other provisions required by the GPL or the LGPL. If you do not delete
  34. # the provisions above, a recipient may use your version of this file under
  35. # the terms of any one of the MPL, the GPL or the LGPL.
  36. #
  37. # ***** END LICENSE BLOCK *****
  38. ########################################################################
  39. #
  40. # mozilla/security/nss/tests/iopr/cert_iopr.sh
  41. #
  42. # Certificate generating and handeling for NSS interoperability QA. This file
  43. # is included from cert.sh
  44. #
  45. # needs to work on all Unix and Windows platforms
  46. #
  47. # special strings
  48. # ---------------
  49. # FIXME ... known problems, search for this string
  50. # NOTE .... unexpected behavior
  51. ########################################################################
  52. IOPR_CERT_SOURCED=1
  53. ########################################################################
  54. # function wraps calls to pk12util, also: writes action and options
  55. # to stdout.
  56. # Params are the same as to pk12util.
  57. # Returns pk12util status
  58. #
  59. pk12u()
  60. {
  61. echo "${CU_ACTION} --------------------------"
  62. echo "pk12util $@"
  63. ${BINDIR}/pk12util $@
  64. RET=$?
  65. return $RET
  66. }
  67. ########################################################################
  68. # Initializes nss db directory and files if they don't exists
  69. # Params:
  70. # $1 - directory location
  71. #
  72. createDBDir() {
  73. trgDir=$1
  74. if [ -z "`ls $trgDir | grep db`" ]; then
  75. trgDir=`cd ${trgDir}; pwd`
  76. if [ "${OS_ARCH}" = "WINNT" -a "$OS_NAME" = "CYGWIN_NT" ]; then
  77. trgDir=`cygpath -m ${trgDir}`
  78. fi
  79. CU_ACTION="Initializing DB at ${trgDir}"
  80. certu -N -d "${trgDir}" -f "${R_PWFILE}" 2>&1
  81. if [ "$RET" -ne 0 ]; then
  82. return $RET
  83. fi
  84. CU_ACTION="Loading root cert module to Cert DB at ${trgDir}"
  85. modu -add "RootCerts" -libfile "${ROOTCERTSFILE}" -dbdir "${trgDir}" 2>&1
  86. if [ "$RET" -ne 0 ]; then
  87. return $RET
  88. fi
  89. fi
  90. }
  91. ########################################################################
  92. # takes care of downloading config, cert and crl files from remote
  93. # location.
  94. # Params:
  95. # $1 - name of the host file will be downloaded from
  96. # $2 - path to the file as it appeared in url
  97. # $3 - target directory the file will be saved at.
  98. # Returns tstclnt status.
  99. #
  100. download_file() {
  101. host=$1
  102. filePath=$2
  103. trgDir=$3
  104. file=$trgDir/`basename $filePath`
  105. createDBDir $trgDir || return $RET
  106. # echo wget -O $file http://${host}${filePath}
  107. # wget -O $file http://${host}${filePath}
  108. # ret=$?
  109. req=$file.$$
  110. echo "GET $filePath HTTP/1.0" > $req
  111. echo >> $req
  112. echo ${BINDIR}/tstclnt -d $trgDir -S -h $host -p $IOPR_DOWNLOAD_PORT \
  113. -v -w ${R_PWFILE} -o
  114. ${BINDIR}/tstclnt -d $trgDir -S -h $host -p $IOPR_DOWNLOAD_PORT \
  115. -v -w ${R_PWFILE} -o < $req > $file
  116. ret=$?
  117. rm -f $_tmp;
  118. return $ret
  119. }
  120. ########################################################################
  121. # Uses pk12util, certutil of cerlutil to import files to an nss db located
  122. # at <dir>(the value of $1 parameter). Chooses a utility to use based on
  123. # a file extension. Initializing a db if it does not exists.
  124. # Params:
  125. # $1 - db location directory
  126. # $2 - file name to import
  127. # $3 - nick name an object in the file will be associated with
  128. # $4 - trust arguments
  129. # Returns status of import
  130. #
  131. importFile() {
  132. dir=$1\
  133. file=$2
  134. certName=$3
  135. certTrust=$4
  136. [ ! -d $dir ] && mkdir -p $dir;
  137. createDBDir $dir || return $RET
  138. case `basename $file | sed 's/^.*\.//'` in
  139. p12)
  140. CU_ACTION="Importing p12 $file to DB at $dir"
  141. pk12u -d $dir -i $file -k ${R_PWFILE} -W iopr
  142. [ $? -ne 0 ] && return 1
  143. CU_ACTION="Modifying trust for cert $certName at $dir"
  144. certu -M -n "$certName" -t "$certTrust" -f "${R_PWFILE}" -d "${dir}"
  145. return $?
  146. ;;
  147. crl)
  148. CU_ACTION="Importing crl $file to DB at $dir"
  149. crlu -d ${dir} -I -n TestCA -i $file
  150. return $?
  151. ;;
  152. crt | cert)
  153. CU_ACTION="Importing cert $certName with trust $certTrust to $dir"
  154. certu -A -n "$certName" -t "$certTrust" -f "${R_PWFILE}" -d "${dir}" \
  155. -i "$file"
  156. return $?
  157. ;;
  158. *)
  159. echo "Unknown file extension: $file:"
  160. return 1
  161. ;;
  162. esac
  163. }
  164. #########################################################################
  165. # Downloads and installs test certs and crl from a remote webserver.
  166. # Generates server cert for reverse testing if reverse test run is turned on.
  167. # Params:
  168. # $1 - host name to download files from.
  169. # $2 - directory at which CA cert will be installed and used for
  170. # signing a server cert.
  171. # $3 - path to a config file in webserver context.
  172. # $4 - ssl server db location
  173. # $5 - ssl client db location
  174. # $5 - ocsp client db location
  175. #
  176. # Returns 0 upon success, otherwise, failed command error code.
  177. #
  178. download_install_certs() {
  179. host=$1
  180. caDir=$2
  181. confPath=$3
  182. sslServerDir=$4
  183. sslClientDir=$5
  184. ocspClientDir=$6
  185. [ ! -d "$caDir" ] && mkdir -p $caDir;
  186. #=======================================================
  187. # Getting config file
  188. #
  189. download_file $host "$confPath/iopr_server.cfg" $caDir
  190. RET=$?
  191. if [ $RET -ne 0 -o ! -f $caDir/iopr_server.cfg ]; then
  192. html_failed "Fail to download website config file(ws: $host)"
  193. return 1
  194. fi
  195. . $caDir/iopr_server.cfg
  196. RET=$?
  197. if [ $RET -ne 0 ]; then
  198. html_failed "Fail to source config file(ws: $host)"
  199. return $RET
  200. fi
  201. #=======================================================
  202. # Getting CA file
  203. #
  204. #----------------- !!!WARNING!!! -----------------------
  205. # Do NOT copy this scenario. CA should never accompany its
  206. # cert with the private key when deliver cert to a customer.
  207. #----------------- !!!WARNING!!! -----------------------
  208. download_file $host $certDir/$caCertName.p12 $caDir
  209. RET=$?
  210. if [ $RET -ne 0 -o ! -f $caDir/$caCertName.p12 ]; then
  211. html_failed "Fail to download $caCertName cert(ws: $host)"
  212. return 1
  213. fi
  214. tmpFiles="$caDir/$caCertName.p12"
  215. importFile $caDir $caDir/$caCertName.p12 $caCertName "TC,C,C"
  216. RET=$?
  217. if [ $RET -ne 0 ]; then
  218. html_failed "Fail to import $caCertName cert to CA DB(ws: $host)"
  219. return $RET
  220. fi
  221. CU_ACTION="Exporting Root CA cert(ws: $host)"
  222. certu -L -n $caCertName -r -d ${caDir} -o $caDir/$caCertName.cert
  223. if [ "$RET" -ne 0 ]; then
  224. Exit 7 "Fatal - failed to export $caCertName cert"
  225. fi
  226. #=======================================================
  227. # Check what tests we want to run
  228. #
  229. doSslTests=0; doOcspTests=0
  230. # XXX remove "_new" from variables below
  231. [ -n "`echo ${supportedTests_new} | grep -i ssl`" ] && doSslTests=1
  232. [ -n "`echo ${supportedTests_new} | grep -i ocsp`" ] && doOcspTests=1
  233. if [ $doSslTests -eq 1 ]; then
  234. if [ "$reverseRunCGIScript" ]; then
  235. [ ! -d "$sslServerDir" ] && mkdir -p $sslServerDir;
  236. #=======================================================
  237. # Import CA cert to server DB
  238. #
  239. importFile $sslServerDir $caDir/$caCertName.cert server-client-CA \
  240. "TC,C,C"
  241. RET=$?
  242. if [ $RET -ne 0 ]; then
  243. html_failed "Fail to import server-client-CA cert to \
  244. server DB(ws: $host)"
  245. return $RET
  246. fi
  247. #=======================================================
  248. # Creating server cert
  249. #
  250. CERTNAME=$HOSTADDR
  251. CU_ACTION="Generate Cert Request for $CERTNAME (ws: $host)"
  252. CU_SUBJECT="CN=$CERTNAME, E=${CERTNAME}@bogus.com, O=BOGUS NSS, \
  253. L=Mountain View, ST=California, C=US"
  254. certu -R -d "${sslServerDir}" -f "${R_PWFILE}" -z "${R_NOISE_FILE}"\
  255. -o $sslServerDir/req 2>&1
  256. tmpFiles="$tmpFiles $sslServerDir/req"
  257. # NOTE:
  258. # For possible time synchronization problems (bug 444308) we generate
  259. # certificates valid also some time in past (-w -1)
  260. CU_ACTION="Sign ${CERTNAME}'s Request (ws: $host)"
  261. certu -C -c "$caCertName" -m `date +"%s"` -v 60 -w -1 \
  262. -d "${caDir}" \
  263. -i ${sslServerDir}/req -o $caDir/${CERTNAME}.cert \
  264. -f "${R_PWFILE}" 2>&1
  265. importFile $sslServerDir $caDir/$CERTNAME.cert $CERTNAME ",,"
  266. RET=$?
  267. if [ $RET -ne 0 ]; then
  268. html_failed "Fail to import $CERTNAME cert to server\
  269. DB(ws: $host)"
  270. return $RET
  271. fi
  272. tmpFiles="$tmpFiles $caDir/$CERTNAME.cert"
  273. #=======================================================
  274. # Download and import CA crl to server DB
  275. #
  276. download_file $host "$certDir/$caCrlName.crl" $sslServerDir
  277. RET=$?
  278. if [ $? -ne 0 ]; then
  279. html_failed "Fail to download $caCertName crl\
  280. (ws: $host)"
  281. return $RET
  282. fi
  283. tmpFiles="$tmpFiles $sslServerDir/$caCrlName.crl"
  284. importFile $sslServerDir $sslServerDir/TestCA.crl
  285. RET=$?
  286. if [ $RET -ne 0 ]; then
  287. html_failed "Fail to import TestCA crt to server\
  288. DB(ws: $host)"
  289. return $RET
  290. fi
  291. fi # if [ "$reverseRunCGIScript" ]
  292. [ ! -d "$sslClientDir" ] && mkdir -p $sslClientDir;
  293. #=======================================================
  294. # Import CA cert to ssl client DB
  295. #
  296. importFile $sslClientDir $caDir/$caCertName.cert server-client-CA \
  297. "TC,C,C"
  298. RET=$?
  299. if [ $RET -ne 0 ]; then
  300. html_failed "Fail to import server-client-CA cert to \
  301. server DB(ws: $host)"
  302. return $RET
  303. fi
  304. fi
  305. if [ $doOcspTests -eq 1 ]; then
  306. [ ! -d "$ocspClientDir" ] && mkdir -p $ocspClientDir;
  307. #=======================================================
  308. # Import CA cert to ocsp client DB
  309. #
  310. importFile $ocspClientDir $caDir/$caCertName.cert server-client-CA \
  311. "TC,C,C"
  312. RET=$?
  313. if [ $RET -ne 0 ]; then
  314. html_failed "Fail to import server-client-CA cert to \
  315. server DB(ws: $host)"
  316. return $RET
  317. fi
  318. fi
  319. #=======================================================
  320. # Import client certs to client DB
  321. #
  322. for fileName in $downloadFiles; do
  323. certName=`echo $fileName | sed 's/\..*//'`
  324. if [ -n "`echo $certName | grep ocsp`" -a $doOcspTests -eq 1 ]; then
  325. clientDir=$ocspClientDir
  326. elif [ $doSslTests -eq 1 ]; then
  327. clientDir=$sslClientDir
  328. else
  329. continue
  330. fi
  331. download_file $host "$certDir/$fileName" $clientDir
  332. RET=$?
  333. if [ $RET -ne 0 -o ! -f $clientDir/$fileName ]; then
  334. html_failed "Fail to download $certName cert(ws: $host)"
  335. return $RET
  336. fi
  337. tmpFiles="$tmpFiles $clientDir/$fileName"
  338. importFile $clientDir $clientDir/$fileName $certName ",,"
  339. RET=$?
  340. if [ $RET -ne 0 ]; then
  341. html_failed "Fail to import $certName cert to client DB\
  342. (ws: $host)"
  343. return $RET
  344. fi
  345. done
  346. rm -f $tmpFiles
  347. return 0
  348. }
  349. #########################################################################
  350. # Initial point for downloading config, cert, crl files for multiple hosts
  351. # involved in interoperability testing. Called from nss/tests/cert/cert.sh
  352. # It will only proceed with downloading if environment variable
  353. # IOPR_HOSTADDR_LIST is set and has a value of host names separated by space.
  354. #
  355. # Returns 1 if interoperability testing is off, 0 otherwise.
  356. #
  357. cert_iopr_setup() {
  358. if [ "$IOPR" -ne 1 ]; then
  359. return 1
  360. fi
  361. num=1
  362. IOPR_HOST_PARAM=`echo "${IOPR_HOSTADDR_LIST} " | cut -f 1 -d' '`
  363. while [ "$IOPR_HOST_PARAM" ]; do
  364. IOPR_HOSTADDR=`echo $IOPR_HOST_PARAM | cut -f 1 -d':'`
  365. IOPR_DOWNLOAD_PORT=`echo "$IOPR_HOST_PARAM:" | cut -f 2 -d':'`
  366. [ -z "$IOPR_DOWNLOAD_PORT" ] && IOPR_DOWNLOAD_PORT=443
  367. IOPR_CONF_PATH=`echo "$IOPR_HOST_PARAM:" | cut -f 3 -d':'`
  368. [ -z "$IOPR_CONF_PATH" ] && IOPR_CONF_PATH="/iopr"
  369. echo "Installing certs for $IOPR_HOSTADDR:$IOPR_DOWNLOAD_PORT:\
  370. $IOPR_CONF_PATH"
  371. download_install_certs ${IOPR_HOSTADDR} ${IOPR_CADIR}_${IOPR_HOSTADDR} \
  372. ${IOPR_CONF_PATH} ${IOPR_SSL_SERVERDIR}_${IOPR_HOSTADDR} \
  373. ${IOPR_SSL_CLIENTDIR}_${IOPR_HOSTADDR} \
  374. ${IOPR_OCSP_CLIENTDIR}_${IOPR_HOSTADDR}
  375. if [ $? -ne 0 ]; then
  376. echo "wsFlags=\"NOIOPR $wsParam\"" >> \
  377. ${IOPR_CADIR}_${IOPR_HOSTADDR}/iopr_server.cfg
  378. fi
  379. num=`expr $num + 1`
  380. IOPR_HOST_PARAM=`echo "${IOPR_HOSTADDR_LIST} " | cut -f $num -d' '`
  381. done
  382. return 0
  383. }