tlstest

package module
v0.0.0-...-e72546c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

README

tlstest

Package tlstest provides some utilities for testing.

GoDev Actions Status Coverage Status

EXAMPLE

See pkg.go.dev/github.com/lufia/tlstest.

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 AlgoECDSA

type AlgoECDSA struct {
	Curve elliptic.Curve
}

func ECDSA256

func ECDSA256() *AlgoECDSA

func (*AlgoECDSA) GenerateKey

func (e *AlgoECDSA) GenerateKey(r io.Reader) (interface{}, error)

type AlgoRSA

type AlgoRSA struct {
	Bits int
}

func RSA2048

func RSA2048() *AlgoRSA

func (*AlgoRSA) GenerateKey

func (e *AlgoRSA) GenerateKey(r io.Reader) (interface{}, error)

type KeyGenerator

type KeyGenerator interface {
	GenerateKey(r io.Reader) (interface{}, error)
}

type Options

type Options struct {
	Hosts        []string
	Organization string
	NotBefore    time.Time
	NotAfter     time.Time

	Rand io.Reader
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL