PageRenderTime 2687ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/jni/openssl/import_openssl.sh

https://gitlab.com/ayham-hassan/Signal-Android
Shell | 722 lines | 497 code | 102 blank | 123 comment | 55 complexity | 1e17ca840cb465490e89210de286d8e2 MD5 | raw file
  1. #!/bin/bash
  2. #
  3. # Copyright (C) 2009 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. # This script imports new versions of OpenSSL (http://openssl.org/source) into the
  19. # Android source tree. To run, (1) fetch the appropriate tarball from the OpenSSL repository,
  20. # (2) check the gpg/pgp signature, and then (3) run:
  21. # ./import_openssl.sh import openssl-*.tar.gz
  22. #
  23. # IMPORTANT: See README.android for additional details.
  24. # turn on exit on error as well as a warning when it happens
  25. set -e
  26. trap "echo WARNING: Exiting on non-zero subprocess exit code" ERR;
  27. # Make sure we're in the right directory.
  28. cd $(dirname $0)
  29. # Ensure consistent sorting order / tool output.
  30. export LANG=C
  31. export LC_ALL=C
  32. PERL_EXE="perl -C0"
  33. function die() {
  34. declare -r message=$1
  35. echo $message
  36. exit 1
  37. }
  38. function usage() {
  39. declare -r message=$1
  40. if [ ! "$message" = "" ]; then
  41. echo $message
  42. fi
  43. echo "Usage:"
  44. echo " ./import_openssl.sh import </path/to/openssl-*.tar.gz>"
  45. echo " ./import_openssl.sh regenerate <patch/*.patch>"
  46. echo " ./import_openssl.sh generate <patch/*.patch> </path/to/openssl-*.tar.gz>"
  47. exit 1
  48. }
  49. function main() {
  50. if [ ! -d patches ]; then
  51. die "OpenSSL patch directory patches/ not found"
  52. fi
  53. if [ ! -f openssl.version ]; then
  54. die "openssl.version not found"
  55. fi
  56. source ./openssl.version
  57. if [ "$OPENSSL_VERSION" == "" ]; then
  58. die "Invalid openssl.version; see README.android for more information"
  59. fi
  60. OPENSSL_DIR=openssl-$OPENSSL_VERSION
  61. OPENSSL_DIR_ORIG=$OPENSSL_DIR.orig
  62. if [ ! -f openssl.config ]; then
  63. die "openssl.config not found"
  64. fi
  65. source ./openssl.config
  66. if [ "$CONFIGURE_ARGS" == "" -o "$UNNEEDED_SOURCES" == "" -o "$NEEDED_SOURCES" == "" ]; then
  67. die "Invalid openssl.config; see README.android for more information"
  68. fi
  69. declare -r command=$1
  70. shift || usage "No command specified. Try import, regenerate, or generate."
  71. if [ "$command" = "import" ]; then
  72. declare -r tar=$1
  73. shift || usage "No tar file specified."
  74. import $tar
  75. elif [ "$command" = "regenerate" ]; then
  76. declare -r patch=$1
  77. shift || usage "No patch file specified."
  78. [ -d $OPENSSL_DIR ] || usage "$OPENSSL_DIR not found, did you mean to use generate?"
  79. [ -d $OPENSSL_DIR_ORIG ] || usage "$OPENSSL_DIR_ORIG not found, did you mean to use generate?"
  80. regenerate $patch
  81. elif [ "$command" = "generate" ]; then
  82. declare -r patch=$1
  83. shift || usage "No patch file specified."
  84. declare -r tar=$1
  85. shift || usage "No tar file specified."
  86. generate $patch $tar
  87. else
  88. usage "Unknown command specified $command. Try import, regenerate, or generate."
  89. fi
  90. }
  91. # Compute the name of an assembly source file generated by one of the
  92. # gen_asm_xxxx() functions below. The logic is the following:
  93. # - if "$2" is not empty, output it directly
  94. # - otherwise, change the file extension of $1 from .pl to .S and output
  95. # it.
  96. # Usage: default_asm_file "$1" "$2"
  97. # or default_asm_file "$@"
  98. #
  99. # $1: generator path (perl script)
  100. # $2: optional output file name.
  101. function default_asm_file () {
  102. if [ "$2" ]; then
  103. echo "$2"
  104. else
  105. echo "${1%%.pl}.S"
  106. fi
  107. }
  108. # Generate an ARM assembly file.
  109. # $1: generator (perl script)
  110. # $2: [optional] output file name
  111. function gen_asm_arm () {
  112. local OUT
  113. OUT=$(default_asm_file "$@")
  114. $PERL_EXE "$1" void "$OUT" > "$OUT"
  115. }
  116. # Generate an ARMv8 64-bit assembly file.
  117. # $1: generator (perl script)
  118. # $2: [optional] output file name
  119. function gen_asm_arm64 () {
  120. local OUT
  121. OUT=$(default_asm_file "$@")
  122. $PERL_EXE "$1" linux64 "$OUT" > "$OUT"
  123. }
  124. function gen_asm_mips () {
  125. local OUT
  126. OUT=$(default_asm_file "$@")
  127. # The perl scripts expect to run the target compiler as $CC to determine
  128. # the endianess of the target. Setting CC to true is a hack that forces the scripts
  129. # to generate little endian output
  130. CC=true $PERL_EXE "$1" o32 > "$OUT"
  131. }
  132. function gen_asm_x86 () {
  133. local OUT
  134. OUT=$(default_asm_file "$@")
  135. $PERL_EXE "$1" elf -fPIC $(print_values_with_prefix -D $OPENSSL_CRYPTO_DEFINES_x86) > "$OUT"
  136. }
  137. function gen_asm_x86_64 () {
  138. local OUT
  139. OUT=$(default_asm_file "$@")
  140. $PERL_EXE "$1" elf "$OUT" > "$OUT"
  141. }
  142. # Filter all items in a list that match a given pattern.
  143. # $1: space-separated list
  144. # $2: egrep pattern.
  145. # Out: items in $1 that match $2
  146. function filter_by_egrep() {
  147. declare -r pattern=$1
  148. shift
  149. echo "$@" | tr ' ' '\n' | grep -e "$pattern" | tr '\n' ' '
  150. }
  151. # Sort and remove duplicates in a space-separated list
  152. # $1: space-separated list
  153. # Out: new space-separated list
  154. function uniq_sort () {
  155. echo "$@" | tr ' ' '\n' | sort -u | tr '\n' ' '
  156. }
  157. function print_autogenerated_header() {
  158. echo "# Auto-generated - DO NOT EDIT!"
  159. echo "# To regenerate, edit openssl.config, then run:"
  160. echo "# ./import_openssl.sh import /path/to/openssl-$OPENSSL_VERSION.tar.gz"
  161. echo "#"
  162. }
  163. function run_verbose() {
  164. echo Running: $@
  165. $@
  166. }
  167. function scan_opensslconf_for_flags() {
  168. for flag in "$@"; do
  169. awk "/^#define ${flag}$/ { print \$2 }" crypto/opensslconf.h
  170. done
  171. }
  172. CRYPTO_CONF_FLAGS=(
  173. OPENSSL_CPUID_OBJ
  174. DES_LONG
  175. DES_PTR
  176. DES_RISC1
  177. DES_RISC2
  178. DES_UNROLL
  179. RC4_INT
  180. RC4_CHUNK
  181. RC4_INDEX
  182. )
  183. function check_asm_flags() {
  184. local arch="$1"
  185. local target="$2"
  186. local unsorted_flags
  187. local expected_flags
  188. local actual_flags
  189. local defines="OPENSSL_CRYPTO_DEFINES_$arch"
  190. PERL=/usr/bin/perl run_verbose ./Configure $CONFIGURE_ARGS $target
  191. unsorted_flags="$(awk '/^CFLAG=/ { sub(/^CFLAG= .*-Wall /, ""); gsub(/-D/, ""); print; }' Makefile)"
  192. unsorted_flags="$unsorted_flags $(scan_opensslconf_for_flags "${CRYPTO_CONF_FLAGS[@]}")"
  193. expected_flags="$(echo $unsorted_flags | tr ' ' '\n' | sort | tr '\n' ' ')"
  194. actual_flags="$(echo ${!defines} | tr ' ' '\n' | sort | tr '\n' ' ')"
  195. if [[ $actual_flags != $expected_flags ]]; then
  196. echo ${defines} is wrong!
  197. echo " $actual_flags"
  198. echo Please update to:
  199. echo " $expected_flags"
  200. exit 1
  201. fi
  202. }
  203. # Run Configure and generate headers
  204. # $1: 32 for 32-bit arch, 64 for 64-bit arch, trusty for Trusty
  205. # $2: 1 if building for static version
  206. # Out: returns the cflags and depflags in variable $flags
  207. function generate_build_config_headers() {
  208. chmod +x ./Configure
  209. local configure_args_bits=CONFIGURE_ARGS_$1
  210. local configure_args_stat=''
  211. local outname=$1
  212. if [[ $2 == 1 ]] ; then
  213. configure_args_stat=CONFIGURE_ARGS_STATIC
  214. outname="static-$1"
  215. fi
  216. if [[ $1 == trusty ]] ; then
  217. PERL=/usr/bin/perl run_verbose ./Configure $CONFIGURE_ARGS_TRUSTY
  218. else
  219. PERL=/usr/bin/perl run_verbose ./Configure $CONFIGURE_ARGS ${!configure_args_bits} ${!configure_args_stat}
  220. fi
  221. rm -f apps/CA.pl.bak crypto/opensslconf.h.bak
  222. mv -f crypto/opensslconf.h crypto/opensslconf-$outname.h
  223. cp -f crypto/opensslconf-$outname.h include/openssl/opensslconf-$outname.h
  224. local tmpfile=$(mktemp tmp.XXXXXXXXXX)
  225. (grep -e -D Makefile | grep -v CONFIGURE_ARGS= | grep -v OPTIONS= | \
  226. grep -v -e -DOPENSSL_NO_DEPRECATED) > $tmpfile
  227. declare -r cflags=$(filter_by_egrep "^-D" $(grep -e "^CFLAG=" $tmpfile))
  228. declare -r depflags=$(filter_by_egrep "^-D" $(grep -e "^DEPFLAG=" $tmpfile))
  229. rm -f $tmpfile
  230. flags="$cflags $depflags"
  231. }
  232. # Run Configure and generate makefiles
  233. function generate_build_config_mk() {
  234. chmod +x ./Configure
  235. for bits in 32 64 trusty; do
  236. # Header flags are output in $flags, first static, then dynamic
  237. generate_build_config_headers $bits 1
  238. local flags_static=$flags
  239. generate_build_config_headers $bits
  240. echo "Generating build-config-$bits.mk"
  241. (
  242. print_autogenerated_header
  243. echo "openssl_cflags_$bits := \\"
  244. for flag in $flags ; do echo " $flag \\" ; done
  245. echo ""
  246. echo "openssl_cflags_static_$bits := \\"
  247. for flag in $flags_static; do echo " $flag \\" ; done
  248. echo ""
  249. ) > ../build-config-$bits.mk
  250. done
  251. }
  252. # Generate crypto/opensslconf.h file including arch-specific files
  253. function generate_opensslconf_h() {
  254. echo "Generating opensslconf.h"
  255. (
  256. echo "// Auto-generated - DO NOT EDIT!"
  257. echo "#ifndef OPENSSL_SYS_TRUSTY"
  258. echo "#if defined(__LP64__)"
  259. echo "#include \"opensslconf-64.h\""
  260. echo "#else"
  261. echo "#include \"opensslconf-32.h\""
  262. echo "#endif"
  263. echo "#else"
  264. echo "#include \"opensslconf-trusty.h\""
  265. echo "#endif"
  266. ) > crypto/opensslconf.h
  267. # Generate a compatible version for the static library builds
  268. echo "Generating opensslconf-static.h"
  269. (
  270. echo "// Auto-generated - DO NOT EDIT!"
  271. echo "#if defined(__LP64__)"
  272. echo "#include \"opensslconf-static-64.h\""
  273. echo "#else"
  274. echo "#include \"opensslconf-static-32.h\""
  275. echo "#endif"
  276. ) > crypto/opensslconf-static.h
  277. # move it to output include files as well
  278. cp -f crypto/opensslconf-static.h include/openssl/opensslconf-static.h
  279. }
  280. # Return the value of a computed variable name.
  281. # E.g.:
  282. # FOO=foo
  283. # BAR=bar
  284. # echo $(var_value FOO_$BAR) -> prints the value of ${FOO_bar}
  285. # $1: Variable name
  286. # Out: variable value
  287. var_value() {
  288. # Note: don't use 'echo' here, because it's sensitive to values
  289. # that begin with an underscore (e.g. "-n")
  290. eval printf \"%s\\n\" \$$1
  291. }
  292. # Same as var_value, but returns sorted output without duplicates.
  293. # $1: Variable name
  294. # Out: variable value (if space-separated list, sorted with no duplicates)
  295. var_sorted_value() {
  296. uniq_sort $(var_value $1)
  297. }
  298. # Print the values in a list with a prefix
  299. # $1: prefix to use
  300. # $2+: values of list
  301. print_values_with_prefix() {
  302. declare -r prefix=$1
  303. shift
  304. for src; do
  305. echo -n " $prefix$src "
  306. done
  307. }
  308. # Print the definition of a given variable in a GNU Make build file.
  309. # $1: Variable name (e.g. common_src_files)
  310. # $2: prefix for each variable contents
  311. # $3+: Variable value (e.g. list of sources)
  312. print_vardef_with_prefix_in_mk() {
  313. declare -r varname=$1
  314. declare -r prefix=$2
  315. shift
  316. shift
  317. if [ -z "$1" ]; then
  318. echo "$varname :="
  319. else
  320. echo "$varname := \\"
  321. for src; do
  322. echo " $prefix$src \\"
  323. done
  324. fi
  325. echo ""
  326. }
  327. # Print the definition of a given variable in a GNU Make build file.
  328. # $1: Variable name (e.g. common_src_files)
  329. # $2+: Variable value (e.g. list of sources)
  330. print_vardef_in_mk() {
  331. declare -r varname=$1
  332. shift
  333. print_vardef_with_prefix_in_mk $varname "" $@
  334. }
  335. # Same as print_vardef_in_mk, but print a CFLAGS definition from
  336. # a list of compiler defines.
  337. # $1: Variable name (e.g. common_cflags)
  338. # $2: List of defines (e.g. OPENSSL_NO_CAMELLIA ...)
  339. print_defines_in_mk() {
  340. declare -r varname=$1
  341. shift
  342. if [ -z "$1" ]; then
  343. echo "$varname :="
  344. else
  345. echo "$varname := \\"
  346. for def; do
  347. echo " -D$def \\"
  348. done
  349. fi
  350. echo ""
  351. }
  352. # Generate a configuration file like Crypto-config.mk
  353. # This uses variable definitions from openssl.config to build a config
  354. # file that can compute the list of target- and host-specific sources /
  355. # compiler flags for a given component.
  356. #
  357. # $1: Target file name. (e.g. Crypto-config.mk)
  358. # $2: Variable prefix. (e.g. CRYPTO)
  359. # $3: "host" or "target"
  360. function generate_config_mk() {
  361. declare -r output="$1"
  362. declare -r prefix="$2"
  363. declare -r all_archs="arm arm64 x86 x86_64 mips"
  364. echo "Generating $(basename $output)"
  365. (
  366. print_autogenerated_header
  367. echo \
  368. "# This script will append to the following variables:
  369. #
  370. # LOCAL_CFLAGS
  371. # LOCAL_C_INCLUDES
  372. # LOCAL_SRC_FILES_\$(TARGET_ARCH)
  373. # LOCAL_SRC_FILES_\$(TARGET_2ND_ARCH)
  374. # LOCAL_CFLAGS_\$(TARGET_ARCH)
  375. # LOCAL_CFLAGS_\$(TARGET_2ND_ARCH)
  376. # LOCAL_ADDITIONAL_DEPENDENCIES
  377. LOCAL_ADDITIONAL_DEPENDENCIES += \$(LOCAL_PATH)/$(basename $output)
  378. "
  379. common_defines=$(var_sorted_value OPENSSL_${prefix}_DEFINES)
  380. print_defines_in_mk common_cflags $common_defines
  381. common_sources=$(var_sorted_value OPENSSL_${prefix}_SOURCES)
  382. print_vardef_in_mk common_src_files $common_sources
  383. common_includes=$(var_sorted_value OPENSSL_${prefix}_INCLUDES)
  384. print_vardef_with_prefix_in_mk common_c_includes external/openssl/ $common_includes
  385. for arch in $all_archs; do
  386. arch_defines=$(var_sorted_value OPENSSL_${prefix}_DEFINES_${arch})
  387. print_defines_in_mk ${arch}_cflags $arch_defines
  388. arch_sources=$(var_sorted_value OPENSSL_${prefix}_SOURCES_${arch})
  389. print_vardef_in_mk ${arch}_src_files $arch_sources
  390. arch_exclude_sources=$(var_sorted_value OPENSSL_${prefix}_SOURCES_EXCLUDES_${arch})
  391. print_vardef_in_mk ${arch}_exclude_files $arch_exclude_sources
  392. done
  393. if [ $3 == "target" ]; then
  394. echo "
  395. LOCAL_CFLAGS += \$(common_cflags)
  396. LOCAL_C_INCLUDES += \$(common_c_includes)"
  397. for arch in $all_archs; do
  398. echo "
  399. LOCAL_SRC_FILES_${arch} += \$(filter-out \$(${arch}_exclude_files),\$(common_src_files) \$(${arch}_src_files))
  400. LOCAL_CFLAGS_${arch} += \$(${arch}_cflags)"
  401. done
  402. else
  403. echo "
  404. LOCAL_CFLAGS += \$(common_cflags)
  405. LOCAL_C_INCLUDES += \$(common_c_includes) \$(local_c_includes)
  406. ifeq (\$(HOST_OS),linux)
  407. LOCAL_CFLAGS_x86 += \$(x86_cflags)
  408. LOCAL_SRC_FILES_x86 += \$(filter-out \$(x86_exclude_files), \$(common_src_files) \$(x86_src_files))
  409. LOCAL_CFLAGS_x86_64 += \$(x86_64_cflags)
  410. LOCAL_SRC_FILES_x86_64 += \$(filter-out \$(x86_64_exclude_files), \$(common_src_files) \$(x86_64_src_files))
  411. else
  412. \$(warning Unknown host OS \$(HOST_OS))
  413. LOCAL_SRC_FILES += \$(common_src_files)
  414. endif"
  415. fi
  416. ) > "$output"
  417. }
  418. function import() {
  419. declare -r OPENSSL_SOURCE=$1
  420. untar $OPENSSL_SOURCE readonly
  421. applypatches $OPENSSL_DIR
  422. convert_iso8859_to_utf8 $OPENSSL_DIR
  423. cd $OPENSSL_DIR
  424. # Check the ASM flags for each arch
  425. check_asm_flags arm linux-armv4
  426. check_asm_flags arm64 linux-aarch64
  427. check_asm_flags x86 linux-elf
  428. check_asm_flags x86_64 linux-x86_64
  429. generate_build_config_mk
  430. generate_opensslconf_h
  431. cp -f LICENSE ../NOTICE
  432. touch ../MODULE_LICENSE_BSD_LIKE
  433. # Avoid checking in symlinks
  434. for i in `find include/openssl -type l`; do
  435. target=`readlink $i`
  436. rm -f $i
  437. if [ -f include/openssl/$target ]; then
  438. cp include/openssl/$target $i
  439. fi
  440. done
  441. # Generate arm asm
  442. gen_asm_arm crypto/aes/asm/aes-armv4.pl
  443. gen_asm_arm crypto/aes/asm/aesv8-armx.pl
  444. gen_asm_arm crypto/aes/asm/bsaes-armv7.pl
  445. gen_asm_arm crypto/bn/asm/armv4-gf2m.pl
  446. gen_asm_arm crypto/bn/asm/armv4-mont.pl
  447. gen_asm_arm crypto/modes/asm/ghash-armv4.pl
  448. gen_asm_arm crypto/modes/asm/ghashv8-armx.pl
  449. gen_asm_arm crypto/sha/asm/sha1-armv4-large.pl
  450. gen_asm_arm crypto/sha/asm/sha256-armv4.pl
  451. gen_asm_arm crypto/sha/asm/sha512-armv4.pl
  452. # Generate armv8 asm
  453. gen_asm_arm64 crypto/aes/asm/aesv8-armx.pl crypto/aes/asm/aesv8-armx-64.S
  454. gen_asm_arm64 crypto/modes/asm/ghashv8-armx.pl crypto/modes/asm/ghashv8-armx-64.S
  455. gen_asm_arm64 crypto/sha/asm/sha1-armv8.pl
  456. gen_asm_arm64 crypto/sha/asm/sha512-armv8.pl crypto/sha/asm/sha256-armv8.S
  457. gen_asm_arm64 crypto/sha/asm/sha512-armv8.pl
  458. # Generate mips asm
  459. gen_asm_mips crypto/aes/asm/aes-mips.pl
  460. gen_asm_mips crypto/bn/asm/mips.pl crypto/bn/asm/bn-mips.S
  461. gen_asm_mips crypto/bn/asm/mips-mont.pl
  462. gen_asm_mips crypto/sha/asm/sha1-mips.pl
  463. gen_asm_mips crypto/sha/asm/sha512-mips.pl crypto/sha/asm/sha256-mips.S
  464. # Generate x86 asm
  465. gen_asm_x86 crypto/x86cpuid.pl
  466. gen_asm_x86 crypto/aes/asm/aes-586.pl
  467. gen_asm_x86 crypto/aes/asm/vpaes-x86.pl
  468. gen_asm_x86 crypto/aes/asm/aesni-x86.pl
  469. gen_asm_x86 crypto/bn/asm/bn-586.pl
  470. gen_asm_x86 crypto/bn/asm/co-586.pl
  471. gen_asm_x86 crypto/bn/asm/x86-mont.pl
  472. gen_asm_x86 crypto/bn/asm/x86-gf2m.pl
  473. gen_asm_x86 crypto/modes/asm/ghash-x86.pl
  474. gen_asm_x86 crypto/sha/asm/sha1-586.pl
  475. gen_asm_x86 crypto/sha/asm/sha256-586.pl
  476. gen_asm_x86 crypto/sha/asm/sha512-586.pl
  477. gen_asm_x86 crypto/md5/asm/md5-586.pl
  478. gen_asm_x86 crypto/des/asm/des-586.pl
  479. gen_asm_x86 crypto/des/asm/crypt586.pl
  480. gen_asm_x86 crypto/bf/asm/bf-586.pl
  481. # Generate x86_64 asm
  482. gen_asm_x86_64 crypto/x86_64cpuid.pl
  483. gen_asm_x86_64 crypto/sha/asm/sha1-x86_64.pl
  484. gen_asm_x86_64 crypto/sha/asm/sha512-x86_64.pl crypto/sha/asm/sha256-x86_64.S
  485. gen_asm_x86_64 crypto/sha/asm/sha512-x86_64.pl
  486. gen_asm_x86_64 crypto/modes/asm/ghash-x86_64.pl
  487. gen_asm_x86_64 crypto/aes/asm/aesni-x86_64.pl
  488. gen_asm_x86_64 crypto/aes/asm/vpaes-x86_64.pl
  489. gen_asm_x86_64 crypto/aes/asm/bsaes-x86_64.pl
  490. gen_asm_x86_64 crypto/aes/asm/aes-x86_64.pl
  491. gen_asm_x86_64 crypto/aes/asm/aesni-sha1-x86_64.pl
  492. gen_asm_x86_64 crypto/md5/asm/md5-x86_64.pl
  493. gen_asm_x86_64 crypto/bn/asm/modexp512-x86_64.pl
  494. gen_asm_x86_64 crypto/bn/asm/x86_64-mont.pl
  495. gen_asm_x86_64 crypto/bn/asm/x86_64-gf2m.pl
  496. gen_asm_x86_64 crypto/bn/asm/x86_64-mont5.pl
  497. gen_asm_x86_64 crypto/rc4/asm/rc4-x86_64.pl
  498. gen_asm_x86_64 crypto/rc4/asm/rc4-md5-x86_64.pl
  499. # Setup android.testssl directory
  500. mkdir android.testssl
  501. cat test/testssl | \
  502. sed 's#../util/shlib_wrap.sh ./ssltest#adb shell /system/bin/ssltest#' | \
  503. sed 's#../util/shlib_wrap.sh ../apps/openssl#adb shell /system/bin/openssl#' | \
  504. sed 's#adb shell /system/bin/openssl no-dh#[ `adb shell /system/bin/openssl no-dh` = no-dh ]#' | \
  505. sed 's#adb shell /system/bin/openssl no-rsa#[ `adb shell /system/bin/openssl no-rsa` = no-dh ]#' | \
  506. sed 's#../apps/server2.pem#/sdcard/android.testssl/server2.pem#' | \
  507. cat > \
  508. android.testssl/testssl
  509. chmod +x android.testssl/testssl
  510. cat test/Uss.cnf | sed 's#./.rnd#/sdcard/android.testssl/.rnd#' >> android.testssl/Uss.cnf
  511. cat test/CAss.cnf | sed 's#./.rnd#/sdcard/android.testssl/.rnd#' >> android.testssl/CAss.cnf
  512. cp apps/server2.pem android.testssl/
  513. cp ../patches/testssl.sh android.testssl/
  514. cd ..
  515. generate_config_mk Crypto-config-target.mk CRYPTO target
  516. generate_config_mk Crypto-config-host.mk CRYPTO host
  517. generate_config_mk Crypto-config-trusty.mk CRYPTO_TRUSTY target
  518. generate_config_mk Ssl-config-target.mk SSL target
  519. generate_config_mk Ssl-config-host.mk SSL host
  520. generate_config_mk Apps-config-target.mk APPS target
  521. generate_config_mk Apps-config-host.mk APPS host
  522. # Prune unnecessary sources
  523. prune
  524. NEEDED_SOURCES="$NEEDED_SOURCES android.testssl"
  525. for i in $NEEDED_SOURCES; do
  526. echo "Updating $i"
  527. rm -r $i
  528. mv $OPENSSL_DIR/$i .
  529. done
  530. cleantar
  531. }
  532. function regenerate() {
  533. declare -r patch=$1
  534. generatepatch $patch
  535. }
  536. function generate() {
  537. declare -r patch=$1
  538. declare -r OPENSSL_SOURCE=$2
  539. untar $OPENSSL_SOURCE
  540. applypatches $OPENSSL_DIR_ORIG $patch
  541. prune
  542. for i in $NEEDED_SOURCES; do
  543. echo "Restoring $i"
  544. rm -r $OPENSSL_DIR/$i
  545. cp -rf $i $OPENSSL_DIR/$i
  546. done
  547. generatepatch $patch
  548. cleantar
  549. }
  550. # Find all files in a sub-directory that are encoded in ISO-8859
  551. # $1: Directory.
  552. # Out: list of files in $1 that are encoded as ISO-8859.
  553. function find_iso8859_files() {
  554. find $1 -type f -print0 | xargs -0 file --mime-encoding | grep -i "iso-8859" | cut -d: -f1
  555. }
  556. # Convert all ISO-8859 files in a given subdirectory to UTF-8
  557. # $1: Directory name
  558. function convert_iso8859_to_utf8() {
  559. declare -r iso_files=$(find_iso8859_files "$1")
  560. for iso_file in $iso_files; do
  561. iconv --from-code iso-8859-1 --to-code utf-8 $iso_file > $iso_file.tmp
  562. rm -f $iso_file
  563. mv $iso_file.tmp $iso_file
  564. done
  565. }
  566. function untar() {
  567. declare -r OPENSSL_SOURCE=$1
  568. declare -r readonly=$2
  569. # Remove old source
  570. cleantar
  571. # Process new source
  572. tar -zxf $OPENSSL_SOURCE
  573. cp -RfP $OPENSSL_DIR $OPENSSL_DIR_ORIG
  574. if [ ! -z $readonly ]; then
  575. find $OPENSSL_DIR_ORIG -type f -print0 | xargs -0 chmod a-w
  576. fi
  577. }
  578. function prune() {
  579. echo "Removing $UNNEEDED_SOURCES"
  580. (cd $OPENSSL_DIR_ORIG && rm -rf $UNNEEDED_SOURCES)
  581. (cd $OPENSSL_DIR && rm -r $UNNEEDED_SOURCES)
  582. }
  583. function cleantar() {
  584. rm -rf $OPENSSL_DIR_ORIG
  585. rm -rf $OPENSSL_DIR
  586. }
  587. function applypatches () {
  588. declare -r dir=$1
  589. declare -r skip_patch=$2
  590. cd $dir
  591. # Apply appropriate patches
  592. patches=(../patches/[0-9][0-9][0-9][0-9]-*.patch)
  593. for i in "${patches[@]}"; do
  594. if [[ $skip_patch != ${i##*/} ]]; then
  595. echo "Applying patch $i"
  596. patch -p1 < $i || die "Could not apply $i. Fix source and run: $0 regenerate patches/${i##*/}"
  597. else
  598. echo "Skiping patch ${i##*/}"
  599. fi
  600. done
  601. # Cleanup patch output
  602. find . \( -type f -o -type l \) -name "*.orig" -print0 | xargs -0 rm -f
  603. cd ..
  604. }
  605. function generatepatch() {
  606. declare -r patch=$1
  607. # Cleanup stray files before generating patch
  608. find $OPENSSL_DIR -type f -name "*.orig" -print0 | xargs -0 rm -f
  609. find $OPENSSL_DIR -type f -name "*~" -print0 | xargs -0 rm -f
  610. # Find the files the patch touches and only keep those in the output patch
  611. declare -r sources=`patch -p1 --dry-run -d $OPENSSL_DIR < $patch | awk '/^patching file / { print $3 }'`
  612. rm -f $patch
  613. touch $patch
  614. for i in $sources; do
  615. LC_ALL=C TZ=UTC0 diff -aup $OPENSSL_DIR_ORIG/$i $OPENSSL_DIR/$i >> $patch && die "ERROR: No diff for patch $path in file $i"
  616. done
  617. echo "Generated patch $patch"
  618. echo "NOTE To make sure there are not unwanted changes from conflicting patches, be sure to review the generated patch."
  619. }
  620. main $@