-
Notifications
You must be signed in to change notification settings - Fork 8
/
signature_block.py
executable file
·169 lines (142 loc) · 6.69 KB
/
signature_block.py
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
#! /usr/bin/env python
#
# Copyright (c) 2015 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from __future__ import print_function
from struct import pack_into, unpack_from
from util import display_binary_data, error
# TFTF Signature algorithm and associated dictionary of types and names
# NOTE: When adding new types, both the "define" and the dictionary
# need to be updated. (see "--algorithm" in sign-tftf and pem2arakeys)
TFTF_SIGNATURE_TYPE_UNKNOWN = 0x00
TFTF_SIGNATURE_ALGORITHM_RSA_2048_SHA_256 = 0x01
TFTF_SIGNATURE_ALGORITHMS = \
{"rsa2048-sha256": TFTF_SIGNATURE_ALGORITHM_RSA_2048_SHA_256}
TFTF_SIGNATURE_ALGORITHM_NAMES = \
{TFTF_SIGNATURE_ALGORITHM_RSA_2048_SHA_256: "rsa2048-sha256"}
# TFTF Signature Block layout
TFTF_SIGNATURE_KEY_NAME_LENGTH = 96
TFTF_SIGNATURE_SIGNATURE_LENGTH = 256
# TFTF signature block field lengths
TFTF_SIGNATURE_LEN_LENGTH = 4
TFTF_SIGNATURE_LEN_TYPE = 4
TFTF_SIGNATURE_LEN_KEY_NAME = TFTF_SIGNATURE_KEY_NAME_LENGTH
TFTF_SIGNATURE_LEN_KEY_SIGNATURE = TFTF_SIGNATURE_SIGNATURE_LENGTH
TFTF_SIGNATURE_LEN_FIXED_PART = (TFTF_SIGNATURE_LEN_LENGTH +
TFTF_SIGNATURE_LEN_TYPE +
TFTF_SIGNATURE_LEN_KEY_NAME)
# TFTF signature block field offsets
TFTF_SIGNATURE_OFF_LENGTH = 0
TFTF_SIGNATURE_OFF_TYPE = (TFTF_SIGNATURE_OFF_LENGTH +
TFTF_SIGNATURE_LEN_LENGTH)
TFTF_SIGNATURE_OFF_KEY_NAME = (TFTF_SIGNATURE_OFF_TYPE +
TFTF_SIGNATURE_LEN_TYPE)
TFTF_SIGNATURE_OFF_KEY_SIGNATURE = (TFTF_SIGNATURE_OFF_KEY_NAME +
TFTF_SIGNATURE_LEN_KEY_NAME)
def get_signature_algorithm(algorithm_type_string):
"""convert a string into a key_type (TFTF_SIGNATURE_TYPE_xxx)
returns a numeric key_type, or raises an exception if invalid
"""
try:
return TFTF_SIGNATURE_ALGORITHMS[algorithm_type_string]
except:
raise ValueError("Unknown algorithm type: '{0:s}'".
format(algorithm_type_string))
def get_signature_algorithm_name(algorithm):
""" Convert a algorithm_type (TFTF_SIGNATURE_TYPE_xxx) into a string
returns a key name, or raises an exception if invalid
"""
try:
return TFTF_SIGNATURE_ALGORITHM_NAMES[algorithm]
except:
raise ValueError("Unknown algorithm type: '{0:d}'".format(algorithm))
def signature_block_write_map(wf, base_offset, prefix=""):
"""Display the field names and offsets of a single TFTF header"""
# Add the symbol for the start of this header
if prefix:
wf.write("{0:s} {1:08x}\n".
format(prefix, base_offset))
prefix += "."
# Add the header fields
wf.write("{0:s}length {1:08x}\n".
format(prefix, base_offset + TFTF_SIGNATURE_OFF_LENGTH))
wf.write("{0:s}type {1:08x}\n".
format(prefix, base_offset + TFTF_SIGNATURE_OFF_TYPE))
wf.write("{0:s}key_name {1:08x}\n".
format(prefix, base_offset + TFTF_SIGNATURE_OFF_KEY_NAME))
wf.write("{0:s}key_signature {1:08x}\n".
format(prefix, base_offset + TFTF_SIGNATURE_OFF_KEY_SIGNATURE))
class SignatureBlock:
"""TFTF signature block representation"""
def __init__(self, buf=None, signature_type=None, key_name=None,
signature=None):
"""Constructor
Initialize the signature block from the supplied buf OR
(signature_type + key_name + signature.)
"""
if buf:
self.unpack(buf)
elif signature_type and key_name and signature:
self.signature_type = signature_type
self.key_name = key_name
self.signature = signature
self.length = TFTF_SIGNATURE_LEN_FIXED_PART + len(signature)
else:
error("Invalid SignatureBlock creation")
self.signature_type = TFTF_SIGNATURE_TYPE_UNKNOWN
self.key_name = None
self.signature = None
self.length = TFTF_SIGNATURE_LEN_FIXED_PART
def pack(self):
"""Pack the signature data into a binary blob
Returns a binary blob containing the packed signature block data
"""
buf = bytearray(self.length)
pack_into("<LL96s", buf, 0,
self.length,
self.signature_type,
self.key_name)
buf[TFTF_SIGNATURE_LEN_FIXED_PART:self.length] = self.signature
return buf
def unpack(self, buf):
"""Unpack the signature block from a binary buffer"""
sig_block = unpack_from("<LL96s", buf, 0)
self.length = sig_block[0]
self.signature_type = sig_block[1]
self.key_name = sig_block[2]
self.signature = \
buf[TFTF_SIGNATURE_LEN_FIXED_PART:self.length]
def display(self, indent=""):
"""Display the signature block"""
signature_name = get_signature_algorithm_name(self.signature_type)
print("{0:s} Length: {1:08x}".format(indent, self.length))
print("{0:s} Sig. type: {1:d} ({2:s})".format(
indent, self.signature_type, signature_name))
print("{0:s} Key name:".format(indent))
print("{0:s} '{1:4s}'".format(indent, self.key_name))
print("{0:s} Signature:".format(indent))
display_binary_data(self.signature, True, indent + " ")