blob: 5319605af82e9d8a0bacbbd1016bed39b8fdc0cc [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001"""
2This module generates tool auth, which combines public certificate and other functions
3such as DA binding.
4"""
5import os
6import sys
7from lib import gfh
8from lib import cert
9
10
11def get_file_sizeb(file_path):
12 """
13 get binary file size
14 """
15 if not os.path.isfile(file_path):
16 return 0
17 file_handle = open(file_path, "rb")
18 file_handle.seek(0, 2)
19 file_size = file_handle.tell()
20 file_handle.close()
21 return file_size
22
23
24def concatb(file1_path, file2_path):
25 """
26 concatenate two binary files
27 """
28 file2_size = get_file_sizeb(file2_path)
29 file1 = open(file1_path, "ab+")
30 file2 = open(file2_path, "rb")
31 file1.write(file2.read(file2_size))
32 file2.close()
33 file1.close()
34
35
36class ToolAuth(object):
37 """
38 class for tool auth, which is a public key certificate for DAA/SLA.
39 """
40 def __init__(self, out_path, tool_auth_path):
41 self.m_out_path = out_path
42 if not os.path.exists(self.m_out_path):
43 os.makedirs(self.m_out_path)
44 self.m_tool_auth_path = tool_auth_path
45 self.m_gfh = gfh.ImageGFH()
46 self.m_sig_handler = None
47
48 def create_gfh(self, gfh_config):
49 """
50 create GFH(generic file header) for tool auth.
51 """
52 self.m_gfh.load_ini(gfh_config)
53 return
54
55 def sign(self, key_ini_path):
56 """
57 generate signature for tool auth.
58 """
59 # tool auth contains only gfh and signature, no extra content
60 self.m_gfh.finalize(0, key_ini_path)
61 # write tbs_tool_auth
62 tbs_toolauth_file_path = os.path.join(self.m_out_path,
63 "tbs_toolauth.bin")
64 tbs_tool_auth_file = open(tbs_toolauth_file_path, "wb")
65 tbs_tool_auth_file.write(self.m_gfh.pack())
66 tbs_tool_auth_file.close()
67 print "===tool_auth signing==="
68 if self.m_gfh.get_sig_type() == "SINGLE":
69 self.m_sig_handler = cert.SigSingle(self.m_gfh.get_pad_type())
70 self.m_sig_handler.set_out_path(self.m_out_path)
71 self.m_sig_handler.create(key_ini_path, tbs_toolauth_file_path)
72 self.m_sig_handler.sign()
73 sig_name = "toolauth.sig"
74 sig_file_path = os.path.join(self.m_out_path, sig_name)
75 self.m_sig_handler.output(self.m_out_path, sig_name)
76 # create final toolauth file
77 if os.path.isfile(self.m_tool_auth_path):
78 os.remove(self.m_tool_auth_path)
79 concatb(self.m_tool_auth_path, tbs_toolauth_file_path)
80 concatb(self.m_tool_auth_path, sig_file_path)
81 else:
82 print "unknown signature type"
83 # clean up
84 os.remove(tbs_toolauth_file_path)
85 os.remove(sig_file_path)
86 return
87
88
89def main():
90 """
91 entry point if this module is executed from cmdline.
92 """
93 # parameter parsing
94 idx = 1
95 key_ini_path = ""
96 gfh_config_ini_path = ""
97 while idx < len(sys.argv):
98 if sys.argv[idx][0] == '-':
99 if sys.argv[idx][1] == 'i':
100 print "key: " + sys.argv[idx + 1]
101 key_ini_path = sys.argv[idx + 1]
102 idx += 2
103 elif sys.argv[idx][1] == 'g':
104 print "gfh: " + sys.argv[idx + 1]
105 gfh_config_ini_path = sys.argv[idx + 1]
106 idx += 2
107 else:
108 print "unknown input"
109 idx += 2
110 else:
111 tool_auth_path = sys.argv[idx]
112 print "tool_auth_path: " + tool_auth_path
113 idx += 1
114
115 if not key_ini_path:
116 print "key path is not given!"
117 return -1
118 if not gfh_config_ini_path:
119 print "gfh config path is not given!"
120 return -1
121 if not tool_auth_path:
122 print "tool_auth path is not given!"
123 return -1
124
125 out_path = os.path.dirname(os.path.abspath(tool_auth_path))
126
127 tool_auth_obj = ToolAuth(out_path, tool_auth_path)
128 tool_auth_obj.create_gfh(gfh_config_ini_path)
129 tool_auth_obj.sign(key_ini_path)
130
131 return 0
132
133
134if __name__ == '__main__':
135 main()