aboutsummaryrefslogtreecommitdiff
path: root/crypto.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-01-05 13:25:40 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-01-05 13:25:40 +0100
commit0c949602178b96ff42f95243074d851ac778b329 (patch)
tree5a48e1e550f3f6233df4da7bdbb29efa7a6709bb /crypto.go
parenteb4849347e7f8acb15201649bf1af52d23438c2e (diff)
Add more helper methods
Diffstat (limited to 'crypto.go')
-rw-r--r--crypto.go37
1 files changed, 30 insertions, 7 deletions
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) {