@@ -72,7 +72,7 @@ func NewTLSCA(keyBits int, rootCert *x509.Certificate, rootKey *rsa.PrivateKey)
7272}
7373
7474// LoadTLSCA create new tls CA
75- func LoadTLSCA (keyPath , certPath string ) (* TLSCA , error ) {
75+ func LoadTLSCA (keyPath , certPath , password string ) (* TLSCA , error ) {
7676 keyBytes , kErr := ioutil .ReadFile (keyPath )
7777 certBytes , cErr := ioutil .ReadFile (certPath )
7878 if kErr != nil {
@@ -85,10 +85,39 @@ func LoadTLSCA(keyPath, certPath string) (*TLSCA, error) {
8585 keyBlock , _ := pem .Decode (keyBytes )
8686 if keyBlock == nil {
8787 return nil , fmt .Errorf ("decode key is nil" )
88- } else if sort .SearchStrings (supportPemType , keyBlock .Type ) < 0 {
88+ } else if supportPemType [ sort .SearchStrings (supportPemType , keyBlock .Type )] != keyBlock . Type {
8989 return nil , fmt .Errorf ("unsupport PEM type %s" , keyBlock .Type )
9090 }
91- key , err := x509 .ParsePKCS1PrivateKey (keyBlock .Bytes )
91+
92+ /* Fix x-ca/ca root/tls key Problem
93+ * https://github.com/x-ca/ca/blob/f82f6cc529662d5a751b79d87698a13c65f342ec/etc/root-ca.conf#L15
94+ * https://security.stackexchange.com/questions/93417/what-encryption-is-applied-on-a-key-generated-by-openssl-req
95+ * https://rfc-editor.org/rfc/rfc1423.html
96+ * openssl asn1parse -in root-ca.key -i | cut -c-90
97+ * - golang code
98+ *
99+ * if x509.IsEncryptedPEMBlock(keyBlock) == true {
100+ * der, err := x509.DecryptPEMBlock(keyBlock, []byte("pwd"))
101+ * key, _ = x509.ParsePKCS1PrivateKey(der)
102+ * } else {
103+ * key, err = x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
104+ * }
105+ *
106+ * Raise error: `Error: fromPEMBytes: x509: no DEK-Info header in block`
107+ *
108+ * - fix run: `openssl rsa -in root-ca.key -des3`
109+ */
110+ var key * rsa.PrivateKey
111+ var err error
112+ if x509 .IsEncryptedPEMBlock (keyBlock ) == true {
113+ der , err := x509 .DecryptPEMBlock (keyBlock , []byte (password ))
114+ if err != nil {
115+ return nil , err
116+ }
117+ key , _ = x509 .ParsePKCS1PrivateKey (der )
118+ } else {
119+ key , err = x509 .ParsePKCS1PrivateKey (keyBlock .Bytes )
120+ }
92121 if err != nil {
93122 return nil , fmt .Errorf ("load private key %s, error %s" , keyPath , err )
94123 }
@@ -294,13 +323,13 @@ func (c *TLSCA) Sign(commonName string, domains []string, ips []net.IP, days, ke
294323func (c * TLSCA ) WriteCert (commonName string , key * rsa.PrivateKey , cert * x509.Certificate , tlsChainPath string ) error {
295324 // mkdir
296325 var dir = strings .Replace (commonName , "*." , "" , - 1 )
297- err := os .MkdirAll (fmt .Sprintf ("certs/%s" , dir ), 0700 )
326+ err := os .MkdirAll (fmt .Sprintf ("x-ca/ certs/%s" , dir ), 0700 )
298327 if err != nil && ! os .IsExist (err ) {
299328 return err
300329 }
301330
302331 // write key
303- keyPath := fmt .Sprintf ("certs/%s/%s.key" , dir , commonName )
332+ keyPath := fmt .Sprintf ("x-ca/ certs/%s/%s.key" , dir , commonName )
304333 keyFile , err := os .OpenFile (keyPath , os .O_CREATE | os .O_EXCL | os .O_WRONLY , 0600 )
305334 if err != nil {
306335 return err
@@ -316,7 +345,7 @@ func (c *TLSCA) WriteCert(commonName string, key *rsa.PrivateKey, cert *x509.Cer
316345 }
317346
318347 // write cert
319- certPath := fmt .Sprintf ("certs/%s/%s.crt" , dir , commonName )
348+ certPath := fmt .Sprintf ("x-ca/ certs/%s/%s.crt" , dir , commonName )
320349 certFile , err := os .OpenFile (certPath , os .O_CREATE | os .O_EXCL | os .O_WRONLY , 0600 )
321350 if err != nil {
322351 return err
@@ -332,7 +361,7 @@ func (c *TLSCA) WriteCert(commonName string, key *rsa.PrivateKey, cert *x509.Cer
332361 }
333362
334363 // write cert chain
335- certChainPath := fmt .Sprintf ("certs/%s/%s.bundle.crt" , dir , commonName )
364+ certChainPath := fmt .Sprintf ("x-ca/ certs/%s/%s.bundle.crt" , dir , commonName )
336365 certChainFile , err := os .OpenFile (certChainPath , os .O_CREATE | os .O_EXCL | os .O_WRONLY , 0600 )
337366 if err != nil {
338367 return err
@@ -355,7 +384,7 @@ func (c *TLSCA) WriteCert(commonName string, key *rsa.PrivateKey, cert *x509.Cer
355384 }
356385
357386 // print
358- fmt .Println ("write cert to" , fmt .Sprintf ("./certs/%s/{%s.key,%s.crt,%s.bundle.crt}" , commonName , commonName , commonName , commonName ))
387+ fmt .Println ("write cert to" , fmt .Sprintf ("./x-ca/ certs/%s/{%s.key,%s.crt,%s.bundle.crt}" , commonName , commonName , commonName , commonName ))
359388
360389 return nil
361390}
0 commit comments