18
18
//! ```
19
19
use std:: fmt:: Write ;
20
20
21
+ use crate :: asn1:: Asn1Object ;
21
22
use crate :: error:: ErrorStack ;
22
23
use crate :: nid:: Nid ;
23
- use crate :: x509:: { Asn1Object , GeneralName , Stack , X509Extension , X509v3Context } ;
24
+ use crate :: x509:: { GeneralName , Stack , X509Extension , X509v3Context } ;
24
25
use foreign_types:: ForeignType ;
25
26
26
27
/// An extension which indicates whether a certificate is a CA certificate.
@@ -223,18 +224,7 @@ impl KeyUsage {
223
224
/// for which the certificate public key can be used for.
224
225
pub struct ExtendedKeyUsage {
225
226
critical : bool ,
226
- server_auth : bool ,
227
- client_auth : bool ,
228
- code_signing : bool ,
229
- email_protection : bool ,
230
- time_stamping : bool ,
231
- ms_code_ind : bool ,
232
- ms_code_com : bool ,
233
- ms_ctl_sign : bool ,
234
- ms_sgc : bool ,
235
- ms_efs : bool ,
236
- ns_sgc : bool ,
237
- other : Vec < String > ,
227
+ items : Vec < String > ,
238
228
}
239
229
240
230
impl Default for ExtendedKeyUsage {
@@ -248,18 +238,7 @@ impl ExtendedKeyUsage {
248
238
pub fn new ( ) -> ExtendedKeyUsage {
249
239
ExtendedKeyUsage {
250
240
critical : false ,
251
- server_auth : false ,
252
- client_auth : false ,
253
- code_signing : false ,
254
- email_protection : false ,
255
- time_stamping : false ,
256
- ms_code_ind : false ,
257
- ms_code_com : false ,
258
- ms_ctl_sign : false ,
259
- ms_sgc : false ,
260
- ms_efs : false ,
261
- ns_sgc : false ,
262
- other : vec ! [ ] ,
241
+ items : vec ! [ ] ,
263
242
}
264
243
}
265
244
@@ -271,101 +250,74 @@ impl ExtendedKeyUsage {
271
250
272
251
/// Sets the `serverAuth` flag to `true`.
273
252
pub fn server_auth ( & mut self ) -> & mut ExtendedKeyUsage {
274
- self . server_auth = true ;
275
- self
253
+ self . other ( "serverAuth" )
276
254
}
277
255
278
256
/// Sets the `clientAuth` flag to `true`.
279
257
pub fn client_auth ( & mut self ) -> & mut ExtendedKeyUsage {
280
- self . client_auth = true ;
281
- self
258
+ self . other ( "clientAuth" )
282
259
}
283
260
284
261
/// Sets the `codeSigning` flag to `true`.
285
262
pub fn code_signing ( & mut self ) -> & mut ExtendedKeyUsage {
286
- self . code_signing = true ;
287
- self
263
+ self . other ( "codeSigning" )
288
264
}
289
265
290
266
/// Sets the `emailProtection` flag to `true`.
291
267
pub fn email_protection ( & mut self ) -> & mut ExtendedKeyUsage {
292
- self . email_protection = true ;
293
- self
268
+ self . other ( "emailProtection" )
294
269
}
295
270
296
271
/// Sets the `timeStamping` flag to `true`.
297
272
pub fn time_stamping ( & mut self ) -> & mut ExtendedKeyUsage {
298
- self . time_stamping = true ;
299
- self
273
+ self . other ( "timeStamping" )
300
274
}
301
275
302
276
/// Sets the `msCodeInd` flag to `true`.
303
277
pub fn ms_code_ind ( & mut self ) -> & mut ExtendedKeyUsage {
304
- self . ms_code_ind = true ;
305
- self
278
+ self . other ( "msCodeInd" )
306
279
}
307
280
308
281
/// Sets the `msCodeCom` flag to `true`.
309
282
pub fn ms_code_com ( & mut self ) -> & mut ExtendedKeyUsage {
310
- self . ms_code_com = true ;
311
- self
283
+ self . other ( "msCodeCom" )
312
284
}
313
285
314
286
/// Sets the `msCTLSign` flag to `true`.
315
287
pub fn ms_ctl_sign ( & mut self ) -> & mut ExtendedKeyUsage {
316
- self . ms_ctl_sign = true ;
317
- self
288
+ self . other ( "msCTLSign" )
318
289
}
319
290
320
291
/// Sets the `msSGC` flag to `true`.
321
292
pub fn ms_sgc ( & mut self ) -> & mut ExtendedKeyUsage {
322
- self . ms_sgc = true ;
323
- self
293
+ self . other ( "msSGC" )
324
294
}
325
295
326
296
/// Sets the `msEFS` flag to `true`.
327
297
pub fn ms_efs ( & mut self ) -> & mut ExtendedKeyUsage {
328
- self . ms_efs = true ;
329
- self
298
+ self . other ( "msEFS" )
330
299
}
331
300
332
301
/// Sets the `nsSGC` flag to `true`.
333
302
pub fn ns_sgc ( & mut self ) -> & mut ExtendedKeyUsage {
334
- self . ns_sgc = true ;
335
- self
303
+ self . other ( "nsSGC" )
336
304
}
337
305
338
306
/// Sets a flag not already defined.
339
307
pub fn other ( & mut self , other : & str ) -> & mut ExtendedKeyUsage {
340
- self . other . push ( other. to_owned ( ) ) ;
308
+ self . items . push ( other. to_string ( ) ) ;
341
309
self
342
310
}
343
311
344
312
/// Return the `ExtendedKeyUsage` extension as an `X509Extension`.
345
313
pub fn build ( & self ) -> Result < X509Extension , ErrorStack > {
346
- let mut value = String :: new ( ) ;
347
- let mut first = true ;
348
- append ( & mut value, & mut first, self . critical , "critical" ) ;
349
- append ( & mut value, & mut first, self . server_auth , "serverAuth" ) ;
350
- append ( & mut value, & mut first, self . client_auth , "clientAuth" ) ;
351
- append ( & mut value, & mut first, self . code_signing , "codeSigning" ) ;
352
- append (
353
- & mut value,
354
- & mut first,
355
- self . email_protection ,
356
- "emailProtection" ,
357
- ) ;
358
- append ( & mut value, & mut first, self . time_stamping , "timeStamping" ) ;
359
- append ( & mut value, & mut first, self . ms_code_ind , "msCodeInd" ) ;
360
- append ( & mut value, & mut first, self . ms_code_com , "msCodeCom" ) ;
361
- append ( & mut value, & mut first, self . ms_ctl_sign , "msCTLSign" ) ;
362
- append ( & mut value, & mut first, self . ms_sgc , "msSGC" ) ;
363
- append ( & mut value, & mut first, self . ms_efs , "msEFS" ) ;
364
- append ( & mut value, & mut first, self . ns_sgc , "nsSGC" ) ;
365
- for other in & self . other {
366
- append ( & mut value, & mut first, true , other) ;
314
+ let mut stack = Stack :: new ( ) ?;
315
+ for item in & self . items {
316
+ stack. push ( Asn1Object :: from_str ( item) ?) ?;
317
+ }
318
+ unsafe {
319
+ X509Extension :: new_internal ( Nid :: EXT_KEY_USAGE , self . critical , stack. as_ptr ( ) . cast ( ) )
367
320
}
368
- X509Extension :: new_nid ( None , None , Nid :: EXT_KEY_USAGE , & value)
369
321
}
370
322
}
371
323
0 commit comments