From 0c949602178b96ff42f95243074d851ac778b329 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 5 Jan 2016 13:25:40 +0100 Subject: Add more helper methods --- crypto.go | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'crypto.go') diff --git a/crypto.go b/crypto.go index c0fbfd0..c8fc656 100644 --- a/crypto.go +++ b/crypto.go @@ -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) { -- cgit v1.2.3