localca

package module
v0.0.0-...-25c5c68 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2016 License: BSD-2-Clause Imports: 15 Imported by: 0

README

localca

Package localca is a simple solution for using https or http2 in your local area network with go.

You need certificates. At least if you want to play with http2 or don't own or trust your wifi-router with your local web traffic.

But using certificates for devices in local networks has some problems:

  • Generating, installing and managing certificates for multiple devices is not fun.
  • There are many ways to talk to your device but only those listed in the certificate are accepted by browsers.
  • With DHCP your local devices may change IPs.

With localca you can create a server that:

  • serves a PEM encoded CA certificate on http, users need to import it only once into the browser certificates.
  • automatically self-signs server certificates including the name or IP used to talk to the server.

Example

// read pem encoded key and ca certificate
key, err := localca.ReadKey(keyPEM)
if err != nil {
	log.Fatal(err)
}
ca, err := localca.Read(nil, key, caPEM)
if err != nil {
	log.Fatal(err)
}

// start a http server on port 8080 that serves the ca certificate
go http.ListenAndServe(":8080", ca)

// listen and sign new certificates on demand
ln, err := localca.Listen(":4443", ca, nil)
if err != nil {
	log.Fatal(err)
}

// ready to serve
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "hello local http2 client")
})
srv := &http.Server{Addr: ":4443"}
err = srv.Serve(ln)
if err != nil {
	log.Fatal(err)
}

Documentation at http://godoc.org/github.com/mb0/localca

Documentation

Overview

Package localca is a simple solution for using https or http2 in your local area network.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	CATmpl: x509.Certificate{
		Subject: pkix.Name{
			Organization: []string{"local ca"},
		},
		KeyUsage:              x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
		BasicConstraintsValid: true,
		IsCA:                  true,
		MaxPathLenZero:        true,
	},
	CertTmpl: x509.Certificate{
		Subject: pkix.Name{
			Organization: []string{"local cert"},
		},
		KeyUsage:    x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageKeyAgreement,
		ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
	},
	Valid: 12 * 365 * 24 * time.Hour,
}

DefaultConfig is a minimal default configuration. A shallow copy is used as default configuration for the New and Read.

Functions

func FillNames

func FillNames(t *x509.Certificate, names []string)

FillNames adds DNS names and IP addresses to the certificate template t

Types

type CA

type CA struct {
	PEM string
	// contains filtered or unexported fields
}

CA represents a self-signed certificate authority. It can be used to generate new server certificates.

func New

func New(conf *Config, key Key) (ca *CA, err error)

New returns a new self-signed certificate authority.

func Read

func Read(conf *Config, key Key, caPEM string) (ca *CA, err error)

Read returns a CA from an existing certificate in PEM format.

func (*CA) CertFor

func (ca *CA) CertFor(addr string) (cert *tls.Certificate, err error)

CertFor returns a server certificate for addr. It will auto-generate a new certificate if addr was not already included.

func (*CA) NewCert

func (ca *CA) NewCert(names ...string) (*tls.Certificate, error)

NewCert returns a new server certificate including names.

func (*CA) ServeHTTP

func (ca *CA) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the CA certificate as PEM file to HTTP GET requests.

type Config

type Config struct {
	CATmpl   x509.Certificate
	CertTmpl x509.Certificate
	Valid    time.Duration // default duration for NotAfter
}

Config holds templates for generating a certificate authority and server certificates. The fields SerialNumber, SubjectKeyId and AuthorityKeyId are always regenerated. The fields NotBefore and NotAfter are generated if not specified

type Key

type Key struct {
	PEM string
	*ecdsa.PrivateKey
}

Key represents the same ECDSA both PEM encoded and as structure.

func NewKey

func NewKey() (Key, error)

NewKey returns a new ECDSA 256bit key.

func ReadKey

func ReadKey(keyPEM string) (Key, error)

ReadKey reads a PEM encoded ECDSA key.

func (Key) SubjectKeyID

func (k Key) SubjectKeyID() []byte

SubjectKeyID creates new subject key id used for certificates.

type Listener

type Listener struct {
	*net.TCPListener
	*tls.Config
	// contains filtered or unexported fields
}

Listener is a TCP TLS net.Listener that signs certificates for all requested DNS names and IPs. TCP keep-alive is set to 3 min, as is the default listener in net/http.

func Listen

func Listen(addr string, ca *CA, config *tls.Config) (*Listener, error)

Listen returns a new TCP listener at addr using ca to sign new certificates and optionally additinal config. The GetCertificate field of the tls.Config will be set to provide the certificates to clients and generate them for new DNS names.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept accepts new TLS connections and regenerates the server certificate for connections to new local IPs.

Jump to

Keyboard shortcuts

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