1#!/bin/bash 2# 3# Copyright (C) 2010 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18# 19# This script imports new versions of Bouncy Castle (http://bouncycastle.org) into the 20# Android source tree. To run, (1) fetch the appropriate tarball from the Bouncy Castle repository, 21# (2) check the checksum, and then (3) run: 22# ./import_bouncycastle.sh bcprov-jdk*-*.tar.gz 23# 24# IMPORTANT: See README.android for additional details. 25 26# turn on exit on error as well as a warning when it happens 27set -e 28trap "echo WARNING: Exiting on non-zero subprocess exit code" ERR; 29 30function die() { 31 declare -r message=$1 32 33 echo $message 34 exit 1 35} 36 37function usage() { 38 declare -r message=$1 39 40 if [ ! "$message" = "" ]; then 41 echo $message 42 fi 43 echo "Usage:" 44 echo " ./import_bouncycastle.sh import </path/to/bcprov-jdk*-*.tar.gz>" 45 echo " ./import_bouncycastle.sh regenerate <patch/*.patch>" 46 echo " ./import_bouncycastle.sh generate <patch/*.patch> </path/to/bcprov-jdk*-*.tar.gz>" 47 exit 1 48} 49 50function main() { 51 if [ ! -d patches ]; then 52 die "Bouncy Castle patch directory patches/ not found" 53 fi 54 55 if [ ! -f bouncycastle.version ]; then 56 die "bouncycastle.version not found" 57 fi 58 59 source bouncycastle.version 60 if [ "$BOUNCYCASTLE_JDK" == "" -o "$BOUNCYCASTLE_VERSION" == "" ]; then 61 die "Invalid bouncycastle.version; see README.android for more information" 62 fi 63 64 BOUNCYCASTLE_DIR=bcprov-jdk$BOUNCYCASTLE_JDK-$BOUNCYCASTLE_VERSION 65 BOUNCYCASTLE_DIR_ORIG=$BOUNCYCASTLE_DIR.orig 66 67 if [ ! -f bouncycastle.config ]; then 68 die "bouncycastle.config not found" 69 fi 70 71 source bouncycastle.config 72 if [ "$UNNEEDED_SOURCES" == "" -o "$NEEDED_SOURCES" == "" ]; then 73 die "Invalid bouncycastle.config; see README.android for more information" 74 fi 75 76 declare -r command=$1 77 shift || usage "No command specified. Try import, regenerate, or generate." 78 if [ "$command" = "import" ]; then 79 declare -r tar=$1 80 shift || usage "No tar file specified." 81 import $tar 82 elif [ "$command" = "regenerate" ]; then 83 declare -r patch=$1 84 shift || usage "No patch file specified." 85 [ -d $BOUNCYCASTLE_DIR ] || usage "$BOUNCYCASTLE_DIR not found, did you mean to use generate?" 86 [ -d $BOUNCYCASTLE_DIR_ORIG ] || usage "$BOUNCYCASTLE_DIR_ORIG not found, did you mean to use generate?" 87 regenerate $patch 88 elif [ "$command" = "generate" ]; then 89 declare -r patch=$1 90 shift || usage "No patch file specified." 91 declare -r tar=$1 92 shift || usage "No tar file specified." 93 generate $patch $tar 94 else 95 usage "Unknown command specified $command. Try import, regenerate, or generate." 96 fi 97} 98 99function import() { 100 declare -r BOUNCYCASTLE_SOURCE=$1 101 102 untar $BOUNCYCASTLE_SOURCE 103 applypatches 104 105 cd $BOUNCYCASTLE_DIR 106 107 sed 's/<p>/& <BR>/g' LICENSE.html | html2text -width 102 -nobs -ascii > ../NOTICE 108 touch ../MODULE_LICENSE_BSD_LIKE 109 110 cd .. 111 112 rm -r src 113 mkdir -p src/main/java/ 114 for i in $NEEDED_SOURCES; do 115 echo "Updating $i" 116 mv $BOUNCYCASTLE_DIR/$i src/main/java/ 117 done 118 119 # if [ $BOUNCYCASTLE_VERSION -ge 145 ]; then 120 # # move test directories from src/main/java to src/test/java 121 # for from in `find src/main/java -name test`; do 122 # to=`dirname $from | sed s,src/main/java/,src/test/java/,` 123 # echo "Moving $from to $to" 124 # mkdir -p $to 125 # mv $from $to 126 # done 127 # fi 128 129 # # move stray test files from src/main/java to src/test/java 130 # if [ $BOUNCYCASTLE_VERSION -ge 137 ]; then 131 # mkdir -p src/test/java/org/bouncycastle/util/ 132 # echo "Moving src/main/java/org/bouncycastle/util tests" 133 # mv src/main/java/org/bouncycastle/util/*Test*.java src/test/java/org/bouncycastle/util/ 134 # fi 135 136 cleantar 137} 138 139function regenerate() { 140 declare -r patch=$1 141 142 generatepatch $patch 143} 144 145function generate() { 146 declare -r patch=$1 147 declare -r BOUNCYCASTLE_SOURCE=$2 148 149 untar $BOUNCYCASTLE_SOURCE 150 applypatches 151 152 # # restore stray test files from src/test/java back to src/main/java 153 # if [ $BOUNCYCASTLE_VERSION -ge 137 ]; then 154 # echo "Restoring src/test/java/org/bouncycastle/util" 155 # mv src/test/java/org/bouncycastle/util/* src/main/java/org/bouncycastle/util/ 156 # fi 157 158 # # restore test directories from src/test/java back to src/main/java 159 # if [ $BOUNCYCASTLE_VERSION -ge 145 ]; then 160 # for from in `find src/test/java -name test`; do 161 # to=`dirname $from | sed s,src/test/java/,src/main/java/,` 162 # echo "Restoring $from to $to" 163 # mkdir -p $to 164 # mv $from $to 165 # done 166 # fi 167 168 for i in $NEEDED_SOURCES; do 169 echo "Restoring $i" 170 rm -r $BOUNCYCASTLE_DIR/$i 171 cp -rf src/main/java/$i $BOUNCYCASTLE_DIR/$i 172 done 173 174 generatepatch $patch 175 cleantar 176} 177 178function untar() { 179 declare -r BOUNCYCASTLE_SOURCE=$1 180 181 # Remove old source 182 cleantar 183 184 # Process new source 185 tar -zxf $BOUNCYCASTLE_SOURCE 186 mv $BOUNCYCASTLE_DIR $BOUNCYCASTLE_DIR_ORIG 187 find $BOUNCYCASTLE_DIR_ORIG -type f -print0 | xargs -0 chmod a-w 188 (cd $BOUNCYCASTLE_DIR_ORIG && unzip -q src.zip) 189 tar -zxf $BOUNCYCASTLE_SOURCE 190 (cd $BOUNCYCASTLE_DIR && unzip -q src.zip) 191 192 # Prune unnecessary sources 193 echo "Removing $UNNEEDED_SOURCES" 194 (cd $BOUNCYCASTLE_DIR_ORIG && rm -rf $UNNEEDED_SOURCES) 195 (cd $BOUNCYCASTLE_DIR && rm -r $UNNEEDED_SOURCES) 196} 197 198function cleantar() { 199 rm -rf $BOUNCYCASTLE_DIR_ORIG 200 rm -rf $BOUNCYCASTLE_DIR 201} 202 203function applypatches () { 204 cd $BOUNCYCASTLE_DIR 205 206 # Apply appropriate patches 207 for i in $BOUNCYCASTLE_PATCHES; do 208 echo "Applying patch $i" 209 patch -p1 < ../patches/$i || die "Could not apply patches/$i. Fix source and run: $0 regenerate patches/$i" 210 211 # make sure no UNNEEDED_SOURCES got into the patch 212 problem=0 213 for s in $UNNEEDED_SOURCES; do 214 if [ -e $s ]; then 215 echo Unneeded source $s restored by patch $i 216 problem=1 217 fi 218 done 219 if [ $problem = 1 ]; then 220 exit 1 221 fi 222 done 223 224 # Cleanup patch output 225 find . -type f -name "*.orig" -print0 | xargs -0 rm -f 226 227 cd .. 228} 229 230function generatepatch() { 231 declare -r patch=$1 232 233 # Cleanup stray files before generating patch 234 find $BOUNCYCASTLE_DIR -type f -name "*.orig" -print0 | xargs -0 rm -f 235 find $BOUNCYCASTLE_DIR -type f -name "*~" -print0 | xargs -0 rm -f 236 237 rm -f $patch 238 LC_ALL=C TZ=UTC0 diff -Naur $BOUNCYCASTLE_DIR_ORIG $BOUNCYCASTLE_DIR >> $patch && die "ERROR: No diff for patch $path in file $i" 239 echo "Generated patch $patch" 240} 241 242main $@ 243