1
1
#![ cfg( all( feature = "builder" , feature = "pem" ) ) ]
2
2
3
- use der:: { asn1:: UIntRef , pem:: LineEnding , EncodePem } ;
3
+ use der:: { asn1:: UIntRef , pem:: LineEnding , Decode , EncodePem } ;
4
4
use spki:: SubjectPublicKeyInfo ;
5
5
use std:: {
6
6
fs:: File ,
@@ -10,7 +10,7 @@ use std::{
10
10
} ;
11
11
use tempfile:: tempdir;
12
12
use x509_cert:: {
13
- builder:: { CertificateBuilder , CertificateVersion , Signer , UniqueIds } ,
13
+ builder:: { CertificateBuilder , CertificateVersion , Profile , Signer , UniqueIds } ,
14
14
certificate:: { Certificate , TbsCertificate } ,
15
15
constants,
16
16
name:: Name ,
@@ -72,6 +72,8 @@ fn check_certificate(cert: &Certificate) {
72
72
. read_to_end ( & mut output_buf)
73
73
. expect ( "read zlint output" ) ;
74
74
75
+ //println!("{}", String::from_utf8(output_buf.clone()).unwrap());
76
+
75
77
let output: zlint:: LintResult =
76
78
serde_json:: from_slice ( & output_buf) . expect ( "parse zlint output" ) ;
77
79
@@ -91,16 +93,19 @@ fn basic_certificate() {
91
93
let serial_number = 42u32 . to_be_bytes ( ) ;
92
94
let serial_number = UIntRef :: new ( & serial_number[ ..] ) . expect ( "create serial" ) ;
93
95
let validity = Validity :: from_now ( Duration :: new ( 5 , 0 ) ) . unwrap ( ) ;
94
- let issuer = Name :: default ( ) ;
95
- let subject = Name :: default ( ) ;
96
+ let profile = Profile :: Root ;
97
+ let subject =
98
+ Name :: encode_from_string ( "CN=World domination task force,O=World domination Inc,C=US" )
99
+ . unwrap ( ) ;
100
+ let subject = Name :: from_der ( & subject) . unwrap ( ) ;
96
101
let pub_key = SubjectPublicKeyInfo :: try_from ( RSA_2048_DER_EXAMPLE ) . expect ( "get rsa pub key" ) ;
97
102
98
103
let mut builder = CertificateBuilder :: new (
99
- CertificateVersion :: V3 ( uids) ,
104
+ profile,
105
+ CertificateVersion :: V3 { uids } ,
100
106
serial_number,
101
107
constants:: RsaWithSha256 ,
102
108
validity,
103
- issuer,
104
109
subject,
105
110
pub_key,
106
111
)
@@ -116,7 +121,11 @@ fn basic_certificate() {
116
121
}
117
122
}
118
123
119
- check_certificate ( & builder. build ( MockSigner ) . unwrap ( ) ) ;
124
+ let certificate = builder. build ( MockSigner ) . unwrap ( ) ;
125
+
126
+ println ! ( "{}" , openssl:: text_output( & certificate) ) ;
127
+
128
+ check_certificate ( & certificate) ;
120
129
}
121
130
122
131
#[ test]
@@ -140,11 +149,14 @@ mod zlint {
140
149
141
150
#[ derive( Debug , Copy , Clone , PartialEq ) ]
142
151
pub enum Status {
143
- NA ,
152
+ NotApplicable ,
153
+ NotEffective ,
144
154
Pass ,
155
+ Notice ,
145
156
Info ,
146
157
Warn ,
147
158
Error ,
159
+ Fatal ,
148
160
}
149
161
150
162
impl Status {
@@ -176,8 +188,11 @@ mod zlint {
176
188
while let Some ( ( key, value) ) = access. next_entry :: < & str , & str > ( ) ? {
177
189
if key == "result" {
178
190
value_output = Some ( match value {
179
- "NA" => Status :: NA ,
191
+ "NA" => Status :: NotApplicable ,
192
+ "NE" => Status :: NotEffective ,
180
193
"pass" => Status :: Pass ,
194
+ "notice" => Status :: Notice ,
195
+ "fatal" => Status :: Fatal ,
181
196
"error" => Status :: Error ,
182
197
"warn" => Status :: Warn ,
183
198
"info" => Status :: Info ,
@@ -208,10 +223,11 @@ mod zlint {
208
223
209
224
impl LintResult {
210
225
pub fn check_lints ( & self , ignored : & [ & str ] ) -> bool {
211
- let mut failed = Vec :: new ( ) ;
226
+ let mut failed = HashMap :: < String , Status > :: new ( ) ;
227
+
212
228
for ( key, value) in & self . 0 {
213
229
if !value. is_successful ( ) && !ignored. contains ( & key. as_str ( ) ) {
214
- failed. push ( String :: from ( key) ) ;
230
+ failed. insert ( String :: from ( key) , value . clone ( ) ) ;
215
231
}
216
232
}
217
233
@@ -221,3 +237,46 @@ mod zlint {
221
237
}
222
238
}
223
239
}
240
+
241
+ mod openssl {
242
+ use der:: { pem:: LineEnding , EncodePem } ;
243
+ use std:: {
244
+ fs:: File ,
245
+ io:: { Read , Write } ,
246
+ process:: { Command , Stdio } ,
247
+ } ;
248
+ use tempfile:: tempdir;
249
+ use x509_cert:: certificate:: Certificate ;
250
+
251
+ pub fn text_output ( cert : & Certificate ) -> String {
252
+ let tmp_dir = tempdir ( ) . expect ( "create tempdir" ) ;
253
+ let cert_path = tmp_dir. path ( ) . join ( "cert.pem" ) ;
254
+
255
+ let pem = cert. to_pem ( LineEnding :: LF ) . expect ( "generate pem" ) ;
256
+ let mut cert_file = File :: create ( & cert_path) . expect ( "create pem file" ) ;
257
+ cert_file
258
+ . write_all ( pem. as_bytes ( ) )
259
+ . expect ( "Create pem file" ) ;
260
+
261
+ let mut child = Command :: new ( "openssl" )
262
+ . arg ( "x509" )
263
+ . arg ( "-in" )
264
+ . arg ( & cert_path)
265
+ . arg ( "-noout" )
266
+ . arg ( "-text" )
267
+ . stderr ( Stdio :: inherit ( ) )
268
+ . stdout ( Stdio :: piped ( ) )
269
+ . spawn ( )
270
+ . expect ( "zlint failed" ) ;
271
+ let mut stdout = child. stdout . take ( ) . unwrap ( ) ;
272
+ let exit_status = child. wait ( ) . expect ( "get openssl x509 status" ) ;
273
+
274
+ assert ! ( exit_status. success( ) , "openssl failed" ) ;
275
+ let mut output_buf = Vec :: new ( ) ;
276
+ stdout
277
+ . read_to_end ( & mut output_buf)
278
+ . expect ( "read openssl output" ) ;
279
+
280
+ String :: from_utf8 ( output_buf. clone ( ) ) . unwrap ( )
281
+ }
282
+ }
0 commit comments