diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2016-01-05 13:25:40 +0100 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2016-01-05 13:25:40 +0100 |
commit | 0c949602178b96ff42f95243074d851ac778b329 (patch) | |
tree | 5a48e1e550f3f6233df4da7bdbb29efa7a6709bb | |
parent | eb4849347e7f8acb15201649bf1af52d23438c2e (diff) |
Add more helper methods
-rw-r--r-- | crypto.go | 37 | ||||
-rw-r--r-- | desire.go | 6 |
2 files changed, 32 insertions, 11 deletions
@@ -13,6 +13,15 @@ import ( "path" ) +func LoadKeyFile(fname string) (*rsa.PrivateKey, error) { + fd, err := os.Open(fname) + if err != nil { + return nil, err + } + defer fd.Close() + return LoadKey(fd) +} + func LoadKey(r io.Reader) (*rsa.PrivateKey, error) { der, err := ioutil.ReadAll(r) if err != nil { @@ -54,21 +63,35 @@ func NewCSR(altnames []string, key *rsa.PrivateKey) (string, error) { return base64.RawURLEncoding.EncodeToString(der), nil } -func SaveCert(w io.Writer, crt *x509.Certificate) error { - block := &pem.Block{ - Type: "CERTIFICATE", - Bytes: crt.Raw, +func SaveCert(w io.Writer, crt []*x509.Certificate) error { + for _, c := range crt { + block := &pem.Block{ + Type: "CERTIFICATE", + Bytes: c.Raw, + } + if err := pem.Encode(w, block); err != nil { + return err + } } - return pem.Encode(w, block) + return nil +} + +func LoadCertFile(fname string) ([]*x509.Certificate, error) { + fd, err := os.Open(fname) + if err != nil { + return nil, err + } + defer fd.Close() + return LoadCert(fd) } -func LoadCert(r io.Reader) (*x509.Certificate, error) { +func LoadCert(r io.Reader) ([]*x509.Certificate, error) { der, err := ioutil.ReadAll(r) if err != nil { return nil, err } block, _ := pem.Decode(der) - return x509.ParseCertificate(block.Bytes) + return x509.ParseCertificates(block.Bytes) } func CreateKeyFile(fname string) (io.WriteCloser, error) { @@ -40,10 +40,8 @@ func (d *Desire) SaveCert(fname string) error { return err } defer fd.Close() - for _, cert := range d.cert { - if err := SaveCert(fd, cert); err != nil { - return err - } + if err := SaveCert(fd, d.cert); err != nil { + return err } return nil } |