2015-06-11 22:08:04 +02:00
|
|
|
// Copyright (c) 2013-2015 The btcsuite developers
|
2014-01-10 20:38:26 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
_ "crypto/sha512" // Needed for RegisterHash in init
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewTLSCertPair returns a new PEM-encoded x.509 certificate pair
|
|
|
|
// based on a 521-bit ECDSA private key. The machine's local interface
|
|
|
|
// addresses and all variants of IPv4 and IPv6 localhost are included as
|
|
|
|
// valid IP addresses.
|
|
|
|
func NewTLSCertPair(organization string, validUntil time.Time, extraHosts []string) (cert, key []byte, err error) {
|
|
|
|
now := time.Now()
|
|
|
|
if validUntil.Before(now) {
|
|
|
|
return nil, nil, errors.New("validUntil would create an already-expired certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// end of ASN.1 time
|
|
|
|
endOfTime := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
|
|
|
|
if validUntil.After(endOfTime) {
|
|
|
|
validUntil = endOfTime
|
|
|
|
}
|
|
|
|
|
2014-01-29 10:41:59 +01:00
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to generate serial number: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-01-10 20:38:26 +01:00
|
|
|
host, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-11 22:08:04 +02:00
|
|
|
ipAddresses := []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}
|
|
|
|
dnsNames := []string{host}
|
|
|
|
if host != "localhost" {
|
|
|
|
dnsNames = append(dnsNames, "localhost")
|
|
|
|
}
|
|
|
|
|
|
|
|
addIP := func(ipAddr net.IP) {
|
|
|
|
for _, ip := range ipAddresses {
|
|
|
|
if bytes.Equal(ip, ipAddr) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ipAddresses = append(ipAddresses, ipAddr)
|
2014-01-10 20:38:26 +01:00
|
|
|
}
|
2015-06-11 22:08:04 +02:00
|
|
|
addHost := func(host string) {
|
|
|
|
for _, dnsName := range dnsNames {
|
|
|
|
if host == dnsName {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dnsNames = append(dnsNames, host)
|
2014-01-10 20:38:26 +01:00
|
|
|
}
|
|
|
|
|
2014-08-29 10:44:41 +02:00
|
|
|
addrs, err := interfaceAddrs()
|
2014-01-10 20:38:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
for _, a := range addrs {
|
2015-06-11 22:08:04 +02:00
|
|
|
ipAddr, _, err := net.ParseCIDR(a.String())
|
2014-01-10 20:38:26 +01:00
|
|
|
if err == nil {
|
2015-06-11 22:08:04 +02:00
|
|
|
addIP(ipAddr)
|
2014-01-10 20:38:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hostStr := range extraHosts {
|
|
|
|
host, _, err := net.SplitHostPort(hostStr)
|
|
|
|
if err != nil {
|
|
|
|
host = hostStr
|
|
|
|
}
|
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
2015-06-11 22:08:04 +02:00
|
|
|
addIP(ip)
|
2014-01-10 20:38:26 +01:00
|
|
|
} else {
|
2015-06-11 22:08:04 +02:00
|
|
|
addHost(host)
|
2014-01-10 20:38:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 22:08:04 +02:00
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{organization},
|
|
|
|
CommonName: host,
|
|
|
|
},
|
|
|
|
NotBefore: now.Add(-time.Hour * 24),
|
|
|
|
NotAfter: validUntil,
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature |
|
|
|
|
x509.KeyUsageCertSign,
|
|
|
|
IsCA: true, // so can sign self.
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
|
|
|
|
DNSNames: dnsNames,
|
|
|
|
IPAddresses: ipAddresses,
|
2014-01-10 20:38:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template,
|
|
|
|
&template, &priv.PublicKey, priv)
|
|
|
|
if err != nil {
|
2014-12-13 16:29:36 +01:00
|
|
|
return nil, nil, fmt.Errorf("failed to create certificate: %v", err)
|
2014-01-10 20:38:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
certBuf := &bytes.Buffer{}
|
2014-12-13 16:29:36 +01:00
|
|
|
err = pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to encode certificate: %v", err)
|
|
|
|
}
|
2014-01-10 20:38:26 +01:00
|
|
|
|
|
|
|
keybytes, err := x509.MarshalECPrivateKey(priv)
|
|
|
|
if err != nil {
|
2014-12-13 16:29:36 +01:00
|
|
|
return nil, nil, fmt.Errorf("failed to marshal private key: %v", err)
|
2014-01-10 20:38:26 +01:00
|
|
|
}
|
2014-12-13 16:29:36 +01:00
|
|
|
|
2014-01-10 20:38:26 +01:00
|
|
|
keyBuf := &bytes.Buffer{}
|
2014-12-13 16:29:36 +01:00
|
|
|
err = pem.Encode(keyBuf, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keybytes})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to encode private key: %v", err)
|
|
|
|
}
|
2014-01-10 20:38:26 +01:00
|
|
|
|
|
|
|
return certBuf.Bytes(), keyBuf.Bytes(), nil
|
|
|
|
}
|