summaryrefslogtreecommitdiff
path: root/util/nanoapp_prepare.sh
blob: cb8a53576dba58d4b81ebe695a4ef4a9a1556371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash

#
# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Exit in error if we use an undefined variable (i.e. commit a typo).
set -u

terminate() { #cleanup and exit
	rm -rf $stage
	exit $1
}

usage () { #show usage and bail out
	echo "USAGE:" >&2
	echo "    $1 [-e <ENCR_KEY_NUM> <ENCR_KEY_FILE>] [-s <PRIV_KEY_FILE> <PUB_KEY_FILE> [<SIG_TO_CHAIN_1> [<SIG_TO_CHAIN_2> [...]]]] < app.napp > app.final.napp" >&2
	terminate -1
}

putchar() {
	hexch="0123456789abcdef"
	h=$[$1/16]
	l=$[$1%16]
	h=${hexch:$h:1}
	l=${hexch:$l:1}
	e="\x"$h$l
	echo -ne $e
}

printhex() {
	w3=$[$1/16777216]
	t=$[$w3*16777216]
	a=$[$1-$t]

	w2=$[$a/65536]
	t=$[$w2*65536]
	a=$[$a-$t]

	w1=$[$a/256]
	w0=$[$a%256]

	putchar $w0
	putchar $w1
	putchar $w2
	putchar $w3
}

#save args and create temp dir
stage=$(mktemp -dt "$(basename $0).XXXXXXXXXX")
args=( "$@" )

#sanity checks (on the user)
if [ -t 1 ]
then
	usage $0
fi

if [ -t 0 ]
then
	usage $0
fi


#get encryption key if it exists & encrypt app
encr_key_num=""
if [ ${#args[@]} -ge 1 ]
then
	if [[ ${args[0]} = "-e" ]]
	then
		if [ ${#args[@]} -lt 3 ]
		then
			usage $0
		fi
		encr_key_num=${args[1]}
		encr_key_file=${args[2]}
		args=("${args[@]:3}")

		if [ ! -f "$encr_key_file" ]; then
			usage $0
		fi

		nanoapp_encr encr "$encr_key_num" "$encr_key_file" > "$stage/postencr"
	fi
fi


#if app is not encrypted, just copy it to staging area
if [ ! -f "$stage/postencr" ]; then
	cat > "$stage/postencr"
fi

#handle signing
if [ ${#args[@]} -ge 1 ]
then
	if [[ ${args[0]} = "-s" ]]
	then
		if [ ${#args[@]} -lt 3 ]
		then
			usage $0
		fi
		priv1=${args[1]}
		pub1=${args[2]}

		#make sure files exist
		i=1
		while [ $i -lt ${#args[@]} ]
		do
			if [ ! -f "${args[$i]}" ]; then
				usage $0
			fi
			i=$[$i+1]
		done

		#get and save file size
		signed_sz=$(du -b "$stage/postencr" | cut -f1)

		nanoapp_sign sign "$priv1" "$pub1" < "$stage/postencr" > "$stage/sig"

		#pad data to 16 bytes
		t=$signed_sz
		while [ $[$t%16] -ne 0 ]
		do
			echo -ne "\0" >> "$stage/postencr"
			t=$(du -b "$stage/postencr" | cut -f1)
		done

		#produce signed output
		cat "$stage/postencr" "$stage/sig" "$pub1" > "$stage/signed"

		#append remaining chunks
		i=3
		while [ $i -lt ${#args[@]} ]
		do
			cat "${args[$i]}" >> "$stage/signed"
			i=$[$i+1]
		done

		#create header
		num_sigs=$[${#args[@]}-2]

		echo -n SigndApp > "$stage/finished"
		printhex $signed_sz >> "$stage/finished"
		printhex $num_sigs >> "$stage/finished"
		echo -ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" >> "$stage/finished"
		cat "$stage/signed" >> "$stage/finished"
	else
		usage $0
	fi
fi

#if app is not signed, just copy it to staging area
if [ ! -f "$stage/finished" ]; then
	mv "$stage/postencr" "$stage/finished"
fi

#produce output
cat "$stage/finished"

terminate 0