1+ extern  crate  dirs; 
12extern  crate  openssl; 
23extern  crate  rand; 
34
45use  std:: fs:: File ; 
6+ use  std:: fs:: DirBuilder ; 
57use  std:: io:: Read ; 
68use  std:: io:: Write ; 
79use  std:: net:: IpAddr ; 
@@ -21,6 +23,8 @@ use self::openssl::x509::*;
2123
2224use  self :: rand:: prelude:: random; 
2325
26+ use  super :: utils; 
27+ 
2428pub  struct  Cert  { 
2529    ca :  Option < X509 > , 
2630
@@ -41,7 +45,7 @@ impl Cert {
4145    } 
4246
4347    pub  fn  init ( & mut  self ,  name :  & str ,  length :  u32 ,  force :  bool )  { 
44-         if  ( Path :: new ( "ca.key" ) . exists ( )  || Path :: new ( "ca.pem" ) . exists ( ) )  && !force { 
48+         if  ( Path :: new ( & utils :: key_path ( ) ) . exists ( )  || Path :: new ( & utils :: ca_path ( ) ) . exists ( ) )  && !force { 
4549            eprintln ! ( 
4650                "{}" , 
4751                "old ca exist, use -f or --force to force init a new ca" 
@@ -55,8 +59,8 @@ impl Cert {
5559    } 
5660
5761    pub  fn  load_ca ( & mut  self )  -> bool  { 
58-         let  ca = File :: open ( "ca.pem" ) ; 
59-         let  key = File :: open ( "ca.key" ) ; 
62+         let  ca = File :: open ( utils :: ca_path ( ) ) ; 
63+         let  key = File :: open ( utils :: key_path ( ) ) ; 
6064
6165        if  let  ( Ok ( mut  ca) ,  Ok ( mut  key) )  = ( ca,  key)  { 
6266            let  mut  buf = Vec :: < u8 > :: new ( ) ; 
@@ -130,8 +134,7 @@ impl Cert {
130134        x509. append_extension ( sid) . unwrap ( ) ; 
131135
132136        let  mut  aid = AuthorityKeyIdentifier :: new ( ) ; 
133-         let  aid = aid
134-             . keyid ( true ) 
137+         let  aid = aid. keyid ( true ) 
135138            . build ( & x509. x509v3_context ( None ,  None ) ) 
136139            . unwrap ( ) ; 
137140        x509. append_extension ( aid) . unwrap ( ) ; 
@@ -145,17 +148,25 @@ impl Cert {
145148    } 
146149
147150    pub  fn  save_ca ( & mut  self )  { 
151+         let  easycert_dir = utils:: easycert_dir ( ) ; 
152+ 
153+         let  easycert_dir = Path :: new ( & easycert_dir) ; 
154+ 
155+         if  !easycert_dir. exists ( )  { 
156+             DirBuilder :: new ( ) . create ( easycert_dir) . unwrap ( ) ; 
157+         } 
158+ 
148159        if  let  Some ( ref  ca)  = self . ca  { 
149160            let  pem = ca. to_pem ( ) . unwrap ( ) ; 
150161
151-             let  mut  file = File :: create ( "ca.pem" ) . unwrap ( ) ; 
162+             let  mut  file = File :: create ( utils :: ca_path ( ) ) . unwrap ( ) ; 
152163            file. write ( pem. as_slice ( ) ) . unwrap ( ) ; 
153164        } 
154165
155166        if  let  Some ( ref  key)  = self . pkey  { 
156167            let  key = key. private_key_to_pem_pkcs8 ( ) . unwrap ( ) ; 
157168
158-             let  mut  file = File :: create ( "ca.key" ) . unwrap ( ) ; 
169+             let  mut  file = File :: create ( utils :: key_path ( ) ) . unwrap ( ) ; 
159170            file. write ( key. as_slice ( ) ) . unwrap ( ) ; 
160171        } 
161172    } 
0 commit comments