security/nss/tests/iopr/cert_iopr.sh

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial