21
21
22
22
class SSHKRL (object ):
23
23
''' SSHKRL Class '''
24
- ca_host_key = ''
25
- ca_user_key = ''
26
24
ca_krl_file = ''
27
- ca_tmp_work_dir = ''
28
- ca_tmp_work_delete_flag = True
29
25
krl_buf_len = 0
30
26
krl = {}
31
27
32
28
def __init__ (self ):
33
29
app .logger .debug ("Enter" )
34
30
self .ca_dir = app .config ["SSH_CA_DIR" ]
35
- self .ca_host_key = self .ca_dir + "/" + app .config ["SSH_CA_HOST_KEY" ]
36
- self .ca_user_key = self .ca_dir + "/" + app .config ["SSH_CA_USER_KEY" ]
37
31
self .ca_krl_file = self .ca_dir + "/" + app .config ["SSH_CA_KRL_FILE" ]
38
- self .ca_tmp_work_dir = self .ca_dir + "/" + app .config ["SSH_CA_TMP_WORK_DIR" ]
39
- self .ca_tmp_work_delete_flag = app .config ["SSH_CA_TMP_DELETE_FLAG" ]
40
32
41
33
try :
42
34
with open (self .ca_krl_file , mode = "rb" ) as krl_file :
@@ -63,38 +55,15 @@ def __init__(self):
63
55
self .krl ["krl_flags" ] = krlbuf [krl_buf_ptr :krl_buf_ptr + 8 ]
64
56
krl_buf_ptr += 8
65
57
66
- string_size = struct .unpack ('>i' , krlbuf [krl_buf_ptr :krl_buf_ptr + 4 ])[0 ]
67
- krl_buf_ptr += 4
68
-
69
- if (self .krl_buf_len < krl_buf_ptr + string_size ):
70
- app .logger .error ("KRL Parse Error at reserved string " + str (krl_buf_ptr ))
71
- raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
72
-
73
- krl_buf_ptr += string_size
74
- string_size = struct .unpack ('>i' , krlbuf [krl_buf_ptr :krl_buf_ptr + 4 ])[0 ]
75
- krl_buf_ptr += 4
76
-
77
- if (self .krl_buf_len < krl_buf_ptr + string_size ):
78
- app .logger .error ("KRL Parse Error at comment " + str (krl_buf_ptr ))
79
- raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
58
+ krl_buf_ptr , reserved_string = self .read_string_from_buf (krlbuf , krl_buf_ptr )
59
+ krl_buf_ptr , self .krl ["krl_comment" ] = self .read_string_from_buf (krlbuf , krl_buf_ptr )
80
60
81
- if (string_size > 0 ):
82
- self .krl ["krl_comment" ] = krlbuf [krl_buf_ptr :krl_buf_ptr + string_size ]
83
- krl_buf_ptr += string_size
84
-
85
61
# Parse sections
86
62
while (krl_buf_ptr < self .krl_buf_len ):
87
63
section_type = struct .unpack ('c' , krlbuf [krl_buf_ptr :krl_buf_ptr + 1 ])[0 ]
88
64
krl_buf_ptr += 1
89
- string_size = struct .unpack ('>i' , krlbuf [krl_buf_ptr :krl_buf_ptr + 4 ])[0 ]
90
- krl_buf_ptr += 4
91
65
92
- if (self .krl_buf_len < krl_buf_ptr + string_size ):
93
- app .logger .error ("KRL Parse Error at Sections " + str (krl_buf_ptr ))
94
- raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
95
-
96
- section_data = krlbuf [krl_buf_ptr :krl_buf_ptr + string_size ]
97
- krl_buf_ptr += string_size
66
+ krl_buf_ptr , section_data = self .read_string_from_buf (krlbuf , krl_buf_ptr )
98
67
99
68
if (section_type == b'\x01 ' ):
100
69
section_ptr = 0
@@ -103,41 +72,16 @@ def __init__(self):
103
72
if ("krl_certs" not in self .krl ):
104
73
self .krl ["krl_certs" ] = []
105
74
while (section_ptr < section_data_len ):
106
- string_size = struct .unpack ('>i' , section_data [section_ptr :section_ptr + 4 ])[0 ]
107
- section_ptr += 4
108
-
109
- if (section_data_len < section_ptr + string_size ):
110
- app .logger .error ("KRL Parse Error at section 1. Section PTR: " + str (section_ptr ))
111
- raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
112
-
113
75
krl_certs = {}
114
-
115
- krl_certs ["ca_key" ] = section_data [section_ptr :section_ptr + string_size ]
116
- section_ptr += string_size
117
-
118
- string_size = struct .unpack ('>i' , section_data [section_ptr :section_ptr + 4 ])[0 ]
119
- section_ptr += 4
120
-
121
- if (section_data_len < section_ptr + string_size ):
122
- app .logger .error ("KRL Parse Error at section 1. Section PTR: " + str (section_ptr ))
123
- raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
124
-
125
- section_ptr += string_size
76
+ section_ptr , krl_certs ["ca_key" ] = self .read_string_from_buf (section_data , section_ptr )
77
+ section_ptr , reserved_string = self .read_string_from_buf (section_data , section_ptr )
126
78
127
79
cert_section_type = struct .unpack ('c' , section_data [section_ptr :section_ptr + 1 ])[0 ]
128
80
section_ptr += 1
129
81
130
- string_size = struct .unpack ('>i' , section_data [section_ptr :section_ptr + 4 ])[0 ]
131
- section_ptr += 4
132
-
133
- if (section_data_len < section_ptr + string_size ):
134
- app .logger .error ("KRL Parse Error at section 1. Section PTR: " + str (section_ptr ))
135
- raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
82
+ section_ptr , cert_serial_list = self .read_string_from_buf (section_data , section_ptr )
136
83
137
84
if (cert_section_type == b'\x20 ' ):
138
- cert_serial_list = section_data [section_ptr :section_ptr + string_size ]
139
- section_ptr += string_size
140
-
141
85
cert_serial_list_ptr = 0
142
86
cert_serial_list_len = len (cert_serial_list )
143
87
@@ -147,49 +91,57 @@ def __init__(self):
147
91
app .logger .debug ("Cert Serial No: " + str (struct .unpack ('>q' , cert_serial_list [cert_serial_list_ptr :cert_serial_list_ptr + 8 ])[0 ]))
148
92
cert_serial_list_ptr += 8
149
93
app .logger .debug ("Cert Serial List Size: " + str (len (krl_certs ["cert_serial_list" ])))
150
- else :
151
- section_ptr += string_size
152
94
153
95
self .krl ["krl_certs" ].append (krl_certs )
154
96
elif (section_type == b'\x02 ' ):
155
97
section_ptr = 0
156
98
section_data_len = len (section_data )
157
99
if ("krl_keys" not in self .krl ):
158
100
self .krl ["krl_keys" ] = []
101
+
159
102
while (section_ptr < section_data_len ):
160
- string_size = struct .unpack ('>i' , section_data [section_ptr :section_ptr + 4 ])[0 ]
161
- section_ptr += 4
162
-
163
- if (section_data_len < section_ptr + string_size ):
164
- app .logger .error ("KRL Parse Error at section 2. Section PTR: " + str (section_ptr ))
165
- raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
103
+ section_ptr , krl_key = self .read_string_from_buf (section_data , section_ptr )
104
+ self .krl ["krl_keys" ].append (krl_key )
166
105
167
- self .krl ["krl_keys" ].append (section_data [section_ptr :section_ptr + string_size ])
168
- section_ptr += string_size
169
106
app .logger .debug ("KRL Keys Size: " + str (len (self .krl ["krl_keys" ])))
170
107
elif (section_type == b'\x05 ' ):
171
108
section_ptr = 0
172
109
section_data_len = len (section_data )
173
110
if ("krl_key_hash" not in self .krl ):
174
111
self .krl ["krl_key_hash" ] = []
175
112
while (section_ptr < section_data_len ):
176
- string_size = struct .unpack ('>i' , section_data [section_ptr :section_ptr + 4 ])[0 ]
177
- section_ptr += 4
113
+ section_ptr , krl_key_hash = self .read_string_from_buf (section_data , section_ptr )
114
+ self .krl ["krl_key_hash" ].append (krl_key_hash )
115
+ app .logger .debug ("Key Hash: " + str (krl_key_hash ))
178
116
179
- if (section_data_len < section_ptr + string_size ):
180
- app .logger .error ("KRL Parse Error at section 5. Section PTR: " + str (section_ptr ))
181
- raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
182
-
183
- self .krl ["krl_key_hash" ].append (section_data [section_ptr :section_ptr + string_size ])
184
- app .logger .debug ("Key Hash: " + section_data [section_ptr :section_ptr + string_size ].hex ())
185
- section_ptr += string_size
186
117
app .logger .debug ("KRL Key Hash Size: " + str (len (self .krl ["krl_key_hash" ])))
187
118
except OSError as e :
188
119
app .logger .error ("OS error: " + str (e ))
189
120
raise KeyperError (errors ["OSError" ].get ("msg" ), errors ["OSError" ].get ("status" ))
190
121
191
122
app .logger .debug ("Exit" )
192
123
124
+ def read_string_from_buf (self , buf , ptr ):
125
+ ''' Returns a string from buffer '''
126
+ app .logger .debug ("Enter" )
127
+
128
+ result_string = None
129
+ result_ptr = ptr
130
+ buf_len = len (buf )
131
+
132
+ string_size = struct .unpack ('>i' , buf [result_ptr :result_ptr + 4 ])[0 ]
133
+ result_ptr += 4
134
+
135
+ if (buf_len < result_ptr + string_size ):
136
+ app .logger .error ("KRL Parse Error at section. PTR: " + str (result_ptr ))
137
+ raise KeyperError (errors ["KRLParseError" ].get ("msg" ), errors ["KRLParseError" ].get ("status" ))
138
+
139
+ result_string = buf [result_ptr :result_ptr + string_size ]
140
+ result_ptr += string_size
141
+
142
+ app .logger .debug ("Exit" )
143
+ return result_ptr , result_string
144
+
193
145
def is_key_revoked (self , key_hash ):
194
146
''' Checks if key hash in KRL '''
195
147
app .logger .debug ("Enter" )
0 commit comments