File: cert-table-header.sh

package info (click to toggle)
sbsigntool 0.9.4-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 980 kB
  • sloc: ansic: 8,390; sh: 194; makefile: 159
file content (69 lines) | stat: -rwxr-xr-x 1,758 bytes parent folder | download | duplicates (4)
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
#!/bin/bash -e

# Parse the data directory of a PE/COFF file and returns two hex values:
# the file offset and size of the signature table.
function sigtable_params() {
	filename="$1"
	objdump -p "$filename" | awk '/^Entry 4/ {print "0x"$3 " " "0x"$4}'
}

# Extract the signature from a file containing a signature table,
# and write to stdout
function extract_sig() {
	filename="$1"
	cert_table_header_size=8

	params=($(hexdump -n$cert_table_header_size \
				-e '/4 "%u " /2 "%04x " /2 "%04x\n"' \
				"$filename"))
	cert_size=${params[0]}
	cert_revision=${params[1]}
	cert_type=${params[2]}

	# check type & revision
	[ "$cert_revision" -eq '0200' ]
	[ "$cert_type" -eq '0002' ]

	dd if="$filename" bs=1 skip=$cert_table_header_size \
	       count=$(($cert_size - $cert_table_header_size)) 2>/dev/null
}

function repeat() {
	str=$1
	count=$2
	for (( i = 0; $i < $count; i++ ))
	do
		echo -n "$str"
	done
}

cert="test.cert"
signed="test.signed"

for i in {1..8}
do
	# generate a variable-length parameter for the certificate subject
	subj="/CN=$(repeat 'x' $i)"

	# create a temporary cert, and sign the image with it
	openssl req -x509 -sha256 -subj "$subj" -new -key "$key" -out "$cert"
	"$sbsign" --cert "$cert" --key "$key" --output "$signed" "$image"

	# extract the sigtable
	params=($(sigtable_params "$signed"))

	# split and convert to base-10
	sigtable_offset=$((${params[0]}))
	sigtable_size=$((${params[1]}))

	# check that we have a correctly-padded sigtable
	[ $(($sigtable_size % 8)) -eq 0 ]

	sigtable='test.sigtable'

	dd if="$signed" bs=1 skip=$sigtable_offset count=$sigtable_size \
	       of=$sigtable 2>/dev/null

	# extract sig, and feed to openssl's PKCS7 parser
	extract_sig "$sigtable" | openssl pkcs7 -inform DER -noout
done