forked from nanobox-io/golang-nanoauth
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.go
34 lines (29 loc) · 808 Bytes
/
load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package microauth
import (
"crypto/tls"
"encoding/pem"
"io/ioutil"
"go.step.sm/crypto/pemutil"
)
// Load is a helper function to load a certificate and key from password protected files.
func Load(certFile, keyFile, password string) (*tls.Certificate, error) {
certPEMBlock, err := ioutil.ReadFile(certFile)
if err != nil {
return &tls.Certificate{}, err
}
rawKeyPEMBlock, err := ioutil.ReadFile(keyFile)
if err != nil {
return &tls.Certificate{}, err
}
temp, _ := pem.Decode(rawKeyPEMBlock)
keyPEMBlock, err := pemutil.DecryptPEMBlock(temp, []byte(password))
if err != nil {
if err.Error() == "unsupported encrypted PEM" {
keyPEMBlock = rawKeyPEMBlock
} else {
return &tls.Certificate{}, err
}
}
crt, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
return &crt, err
}