@@ -8,7 +8,7 @@ use env_logger::Builder;
8
8
use log:: { debug, LevelFilter } ;
9
9
use std:: {
10
10
fmt:: { self , Debug , Formatter } ,
11
- process:: Command ,
11
+ process:: { Command , Output } ,
12
12
} ;
13
13
14
14
/// Execute HIF operations exposed by the RoT Attest task.
@@ -98,27 +98,41 @@ impl AttestHiffy {
98
98
/// to be decimal. Currently this function ignores the interface and
99
99
/// operation names from the string. Future work may check that these are
100
100
/// consistent with the operation executed.
101
- fn u32_from_stdout ( output : & [ u8 ] ) -> Result < u32 > {
102
- // check interface & operation name?
103
- let output = String :: from_utf8_lossy ( output) ;
104
- let output: Vec < & str > = output. trim ( ) . split ( ' ' ) . collect ( ) ;
105
- let output = output[ output. len ( ) - 1 ] ;
106
- debug ! ( "output: {}" , output) ;
107
-
108
- let ( output, radix) = match output. strip_prefix ( "0x" ) {
109
- Some ( s) => {
110
- debug ! ( "prefix stripped: \" {}\" " , s) ;
111
- ( s, 16 )
112
- }
113
- None => ( output, 10 ) ,
114
- } ;
115
- let log_len = u32:: from_str_radix ( output, 16 ) . with_context ( || {
116
- format ! ( "Failed to parse \" {}\" as base {} u32" , output, radix)
117
- } ) ?;
118
-
119
- debug ! ( "output u32: {}" , log_len) ;
120
-
121
- Ok ( log_len)
101
+ fn u32_from_cmd_output ( output : Output ) -> Result < u32 > {
102
+ if output. status . success ( ) {
103
+ // check interface & operation name?
104
+ let output = String :: from_utf8_lossy ( & output. stdout ) ;
105
+ let output: Vec < & str > = output. trim ( ) . split ( ' ' ) . collect ( ) ;
106
+ let output = output[ output. len ( ) - 1 ] ;
107
+ debug ! ( "output: {}" , output) ;
108
+
109
+ let ( output, radix) = match output. strip_prefix ( "0x" ) {
110
+ Some ( s) => {
111
+ debug ! ( "prefix stripped: \" {}\" " , s) ;
112
+ ( s, 16 )
113
+ }
114
+ None => ( output, 10 ) ,
115
+ } ;
116
+
117
+ let log_len =
118
+ u32:: from_str_radix ( output, 16 ) . with_context ( || {
119
+ format ! (
120
+ "Failed to parse \" {}\" as base {} u32" ,
121
+ output, radix
122
+ )
123
+ } ) ?;
124
+
125
+ debug ! ( "output u32: {}" , log_len) ;
126
+
127
+ Ok ( log_len)
128
+ } else {
129
+ Err ( anyhow ! (
130
+ "command failed with status: {}\n stdout: \" {}\" \n stderr: \" {}\" " ,
131
+ output. status,
132
+ String :: from_utf8_lossy( & output. stdout) ,
133
+ String :: from_utf8_lossy( & output. stderr)
134
+ ) )
135
+ }
122
136
}
123
137
124
138
/// Get length of the certificate chain from the Attest task. This cert
@@ -135,16 +149,7 @@ impl AttestHiffy {
135
149
debug ! ( "executing command: {:?}" , cmd) ;
136
150
137
151
let output = cmd. output ( ) ?;
138
- if output. status . success ( ) {
139
- Self :: u32_from_stdout ( & output. stdout )
140
- } else {
141
- Err ( anyhow ! (
142
- "command failed with status: {}\n stdout: \" {}\" \n stderr: \" {}\" " ,
143
- output. status,
144
- String :: from_utf8_lossy( & output. stdout) ,
145
- String :: from_utf8_lossy( & output. stderr)
146
- ) )
147
- }
152
+ Self :: u32_from_cmd_output ( output)
148
153
}
149
154
150
155
/// Get length of the certificate at the provided index in bytes.
@@ -159,16 +164,7 @@ impl AttestHiffy {
159
164
debug ! ( "executing command: {:?}" , cmd) ;
160
165
161
166
let output = cmd. output ( ) ?;
162
- if output. status . success ( ) {
163
- Self :: u32_from_stdout ( & output. stdout )
164
- } else {
165
- Err ( anyhow ! (
166
- "command failed with status: {}\n stdout: \" {}\" \n stderr: \" {}\" " ,
167
- output. status,
168
- String :: from_utf8_lossy( & output. stdout) ,
169
- String :: from_utf8_lossy( & output. stderr)
170
- ) )
171
- }
167
+ Self :: u32_from_cmd_output ( output)
172
168
}
173
169
174
170
/// Get length of the measurement log in bytes.
@@ -181,16 +177,7 @@ impl AttestHiffy {
181
177
debug ! ( "executing command: {:?}" , cmd) ;
182
178
183
179
let output = cmd. output ( ) ?;
184
- if output. status . success ( ) {
185
- Self :: u32_from_stdout ( & output. stdout )
186
- } else {
187
- Err ( anyhow ! (
188
- "command failed with status: {}\n stdout: \" {}\" \n stderr: \" {}\" " ,
189
- output. status,
190
- String :: from_utf8_lossy( & output. stdout) ,
191
- String :: from_utf8_lossy( & output. stderr)
192
- ) )
193
- }
180
+ Self :: u32_from_cmd_output ( output)
194
181
}
195
182
196
183
/// Get length of the measurement log in bytes.
@@ -203,16 +190,7 @@ impl AttestHiffy {
203
190
debug ! ( "executing command: {:?}" , cmd) ;
204
191
205
192
let output = cmd. output ( ) ?;
206
- if output. status . success ( ) {
207
- Self :: u32_from_stdout ( & output. stdout )
208
- } else {
209
- Err ( anyhow ! (
210
- "command failed with status: {}\n stdout: \" {}\" \n stderr: \" {}\" " ,
211
- output. status,
212
- String :: from_utf8_lossy( & output. stdout) ,
213
- String :: from_utf8_lossy( & output. stderr)
214
- ) )
215
- }
193
+ Self :: u32_from_cmd_output ( output)
216
194
}
217
195
}
218
196
0 commit comments