Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCertificate ¶
func NewCertificate(g KeyGenerator, o Options) (keyPEMBlock, certPEMBlock []byte, err error)
NewCertificate returns PEM encoded certificate
Example (Client) ¶
package main import ( "crypto/tls" "crypto/x509" "net/http" "github.com/lufia/tlstest" ) func main() { _, certPEMBlock, err := tlstest.NewCertificate(tlstest.ECDSA256(), tlstest.Options{ Organization: "example&co", }) certPool, err := x509.SystemCertPool() if err != nil { certPool = x509.NewCertPool() } certPool.AppendCertsFromPEM(certPEMBlock) c := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ RootCAs: certPool, }, }, } _ = c }
Output:
Example (Server) ¶
package main import ( "crypto/tls" "fmt" "log" "net/http" "net/http/httptest" "github.com/lufia/tlstest" ) func main() { keyPEMBlock, certPEMBlock, err := tlstest.NewCertificate(tlstest.ECDSA256(), tlstest.Options{ Organization: "example&co", Hosts: []string{"127.0.0.1"}, }) cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock) if err != nil { log.Fatalln("X509KeyPair:", err) } s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello")) })) s.TLS = &tls.Config{ Certificates: []tls.Certificate{cert}, } s.StartTLS() defer s.Close() c := s.Client() resp, err := c.Get(s.URL) if err != nil { log.Fatal(err) } resp.Body.Close() fmt.Println(resp.TLS.PeerCertificates[0].Subject) }
Output: O=example&co
Types ¶
type KeyGenerator ¶
Click to show internal directories.
Click to hide internal directories.