From 5121bb568961b377a033144ee1e4ff71f98d1bc3 Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Thu, 3 Dec 2020 19:40:30 +0800 Subject: [PATCH] cert --- proxy/cert.go | 24 ++++++++++++++++++++++++ proxy/cert_test.go | 8 ++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/proxy/cert.go b/proxy/cert.go index c1323ad..3edb375 100644 --- a/proxy/cert.go +++ b/proxy/cert.go @@ -6,6 +6,7 @@ import ( "encoding/pem" "errors" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -126,3 +127,26 @@ func (ca *CA) load() error { func (ca *CA) create() error { return nil } + +func (ca *CA) saveTo(out io.Writer) error { + keyBytes, err := x509.MarshalPKCS8PrivateKey(&ca.PrivateKey) + if err != nil { + return err + } + err = pem.Encode(out, &pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes}) + if err != nil { + return err + } + + err = pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: ca.RootCert.Raw}) + if err != nil { + return err + } + + return nil +} + +func (ca *CA) save() error { + + return nil +} diff --git a/proxy/cert_test.go b/proxy/cert_test.go index b0d361a..69a3d55 100644 --- a/proxy/cert_test.go +++ b/proxy/cert_test.go @@ -1,7 +1,7 @@ package proxy import ( - "fmt" + "os" "testing" ) @@ -20,5 +20,9 @@ func TestNewCA(t *testing.T) { if err != nil { t.Error(err) } - fmt.Println(ca) + + err = ca.saveTo(os.Stdout) + if err != nil { + t.Error(err) + } }