diff options
| author | adnano <[email protected]> | 2020-11-01 14:19:18 -0500 |
|---|---|---|
| committer | adnano <[email protected]> | 2020-11-01 14:19:18 -0500 |
| commit | 22d57dfc9e4f38690f787266f1bdef394b5fc198 (patch) | |
| tree | f9b49af7667ee4638b6117a18fb58a8477e66954 /examples/cert.go | |
| parent | Update examples/html.go (diff) | |
| download | go-gemini-22d57dfc9e4f38690f787266f1bdef394b5fc198.tar.xz go-gemini-22d57dfc9e4f38690f787266f1bdef394b5fc198.zip | |
Update examples/cert.go
Diffstat (limited to 'examples/cert.go')
| -rw-r--r-- | examples/cert.go | 64 |
1 files changed, 3 insertions, 61 deletions
diff --git a/examples/cert.go b/examples/cert.go index 9d04cd8..c90a4ca 100644 --- a/examples/cert.go +++ b/examples/cert.go @@ -3,10 +3,6 @@ package main import ( - "bytes" - "crypto/tls" - "crypto/x509" - "encoding/pem" "fmt" "log" "os" @@ -33,63 +29,9 @@ func main() { if err != nil { log.Fatal(err) } - if err := writeCertificate(host, cert); err != nil { + certPath := host + ".crt" + keyPath := host + ".key" + if err := gemini.WriteCertificate(cert, certPath, keyPath); err != nil { log.Fatal(err) } } - -// writeCertificate writes the provided certificate and private key -// to path.crt and path.key respectively. -func writeCertificate(path string, cert tls.Certificate) error { - crt, err := marshalX509Certificate(cert.Leaf.Raw) - if err != nil { - return err - } - key, err := marshalPrivateKey(cert.PrivateKey) - if err != nil { - return err - } - - // Write the certificate - crtPath := path + ".crt" - crtOut, err := os.OpenFile(crtPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return err - } - if _, err := crtOut.Write(crt); err != nil { - return err - } - - // Write the private key - keyPath := path + ".key" - keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return err - } - if _, err := keyOut.Write(key); err != nil { - return err - } - return nil -} - -// marshalX509Certificate returns a PEM-encoded version of the given raw certificate. -func marshalX509Certificate(cert []byte) ([]byte, error) { - var b bytes.Buffer - if err := pem.Encode(&b, &pem.Block{Type: "CERTIFICATE", Bytes: cert}); err != nil { - return nil, err - } - return b.Bytes(), nil -} - -// marshalPrivateKey returns PEM encoded versions of the given certificate and private key. -func marshalPrivateKey(priv interface{}) ([]byte, error) { - var b bytes.Buffer - privBytes, err := x509.MarshalPKCS8PrivateKey(priv) - if err != nil { - return nil, err - } - if err := pem.Encode(&b, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil { - return nil, err - } - return b.Bytes(), nil -} |