Home | History | Annotate | Download | only in util
      1 #!/bin/bash
      2 
      3 #
      4 # Copyright (C) 2016 The Android Open Source Project
      5 #
      6 # Licensed under the Apache License, Version 2.0 (the "License");
      7 # you may not use this file except in compliance with the License.
      8 # You may obtain a copy of the License at
      9 #
     10 #      http://www.apache.org/licenses/LICENSE-2.0
     11 #
     12 # Unless required by applicable law or agreed to in writing, software
     13 # distributed under the License is distributed on an "AS IS" BASIS,
     14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 # See the License for the specific language governing permissions and
     16 # limitations under the License.
     17 #
     18 
     19 # Exit in error if we use an undefined variable (i.e. commit a typo).
     20 set -u
     21 
     22 usage () { #show usage and bail out
     23 	echo "USAGE:" >&2
     24 	echo "    $1 <app.napp> [-e <ENCR_KEY_NUM> <ENCR_KEY_FILE>] [-s <PRIV_KEY_FILE> <PUB_KEY_FILE> [<SIG_TO_CHAIN_1> [<SIG_TO_CHAIN_2> [...]]]]" >&2
     25 	exit 1
     26 }
     27 
     28 if [ $# -ge 1 ] ; then
     29 app=${1%.napp}
     30 shift
     31 else
     32 usage $0
     33 fi
     34 
     35 args=( $@ )
     36 
     37 #get encryption key if it exists & encrypt app
     38 encr_key_num=""
     39 if [ ${#args[@]} -ge 1 ]
     40 then
     41 	if [[ ${args[0]} = "-e" ]]
     42 	then
     43 		if [ ${#args[@]} -lt 3 ]
     44 		then
     45 			usage $0
     46 		fi
     47 		encr_key_num=${args[1]}
     48 		encr_key_file=${args[2]}
     49 		args=("${args[@]:3}")
     50 
     51 		if [ ! -f "$encr_key_file" ]; then
     52 			usage $0
     53 		fi
     54 
     55 		nanoapp_encr -e -i "$encr_key_num" -k "$encr_key_file" "${app}.napp" "${app}.encr.napp"
     56 		app="${app}.encr"
     57 	fi
     58 fi
     59 
     60 #handle signing
     61 if [ ${#args[@]} -ge 1 ]
     62 then
     63 	if [[ ${args[0]} = "-s" ]]
     64 	then
     65 		if [ ${#args[@]} -lt 3 ]
     66 		then
     67 			usage $0
     68 		fi
     69 		priv1=${args[1]}
     70 		pub1=${args[2]}
     71 
     72 		#make sure files exist
     73 		i=1
     74 		while [ $i -lt ${#args[@]} ]
     75 		do
     76 			if [ ! -f "${args[$i]}" ]; then
     77 				usage $0
     78 			fi
     79 			i=$[$i+1]
     80 		done
     81 
     82 		nanoapp_sign -s -e "$priv1" -m "$pub1" "${app}.napp" "${app}.sign.napp"
     83 
     84 		#append remaining chunks
     85 		i=3
     86 		while [ $i -lt ${#args[@]} ]
     87 		do
     88 			cat "${args[$i]}" >> "${app}.sign.napp"
     89 			i=$[$i+1]
     90 		done
     91 	else
     92 		usage $0
     93 	fi
     94 fi
     95