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