2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2015-09-02 20:05:54 +00:00
|
|
|
package tlsutil
|
2014-03-02 22:58:14 +00:00
|
|
|
|
|
|
|
import (
|
2015-11-27 08:09:39 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2014-03-02 22:58:14 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
|
|
|
"math/big"
|
2014-09-11 19:18:08 +00:00
|
|
|
"net"
|
2014-03-02 22:58:14 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
2016-05-26 07:02:56 +00:00
|
|
|
|
2019-11-23 15:20:54 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2016-05-26 07:02:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2014-03-02 22:58:14 +00:00
|
|
|
)
|
|
|
|
|
2015-09-02 20:24:13 +00:00
|
|
|
var (
|
2020-03-03 21:40:00 +00:00
|
|
|
ErrIdentificationFailed = errors.New("failed to identify socket type")
|
2015-09-02 20:24:13 +00:00
|
|
|
)
|
|
|
|
|
2018-10-21 05:17:50 +00:00
|
|
|
var (
|
2021-04-26 08:04:35 +00:00
|
|
|
// The list of cipher suites we will use / suggest for TLS 1.2 connections.
|
|
|
|
cipherSuites = []uint16{
|
|
|
|
// Suites that are good and fast on hardware *without* AES-NI.
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
|
|
|
|
|
|
|
// Suites that are good and fast on hardware with AES-NI. These are
|
|
|
|
// reordered from the Go default to put the 256 bit ciphers above the
|
|
|
|
// 128 bit ones - because that looks cooler, even though there is
|
|
|
|
// probably no relevant difference in strength yet.
|
2018-10-21 05:17:50 +00:00
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
|
2021-04-26 08:04:35 +00:00
|
|
|
// The rest of the suites, minus DES stuff.
|
2018-10-21 05:17:50 +00:00
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
|
|
|
|
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-04-26 08:04:35 +00:00
|
|
|
// SecureDefault returns a tls.Config with reasonable, secure defaults set.
|
|
|
|
// This variant allows only TLS 1.3.
|
|
|
|
func SecureDefaultTLS13() *tls.Config {
|
|
|
|
return &tls.Config{
|
|
|
|
// TLS 1.3 is the minimum we accept
|
|
|
|
MinVersion: tls.VersionTLS13,
|
|
|
|
}
|
2018-10-21 05:17:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 08:04:35 +00:00
|
|
|
// SecureDefaultWithTLS12 returns a tls.Config with reasonable, secure
|
|
|
|
// defaults set. This variant allows TLS 1.2.
|
|
|
|
func SecureDefaultWithTLS12() *tls.Config {
|
2018-10-21 05:17:50 +00:00
|
|
|
// paranoia
|
|
|
|
cs := make([]uint16, len(cipherSuites))
|
|
|
|
copy(cs, cipherSuites)
|
|
|
|
|
|
|
|
return &tls.Config{
|
|
|
|
// TLS 1.2 is the minimum we accept
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2019-02-26 10:49:02 +00:00
|
|
|
// The cipher suite lists built above. These are ignored in TLS 1.3.
|
2018-10-21 05:17:50 +00:00
|
|
|
CipherSuites: cs,
|
|
|
|
// We've put some thought into this choice and would like it to
|
|
|
|
// matter.
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCertificate generates and returns a new TLS certificate.
|
2019-10-16 18:31:46 +00:00
|
|
|
func NewCertificate(certFile, keyFile, commonName string, lifetimeDays int) (tls.Certificate, error) {
|
2018-10-21 05:17:50 +00:00
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "generate key")
|
2014-09-20 13:42:20 +00:00
|
|
|
}
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2019-10-16 18:31:46 +00:00
|
|
|
notBefore := time.Now().Truncate(24 * time.Hour)
|
|
|
|
notAfter := notBefore.Add(time.Duration(lifetimeDays*24) * time.Hour)
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2020-11-09 16:02:56 +00:00
|
|
|
// NOTE: update lib/api.shouldRegenerateCertificate() appropriately if
|
|
|
|
// you add or change attributes in here, especially DNSNames or
|
|
|
|
// IPAddresses.
|
2014-03-02 22:58:14 +00:00
|
|
|
template := x509.Certificate{
|
2020-06-17 08:43:58 +00:00
|
|
|
SerialNumber: new(big.Int).SetUint64(rand.Uint64()),
|
2014-03-02 22:58:14 +00:00
|
|
|
Subject: pkix.Name{
|
2020-11-09 16:02:56 +00:00
|
|
|
CommonName: commonName,
|
|
|
|
Organization: []string{"Syncthing"},
|
|
|
|
OrganizationalUnit: []string{"Automatically Generated"},
|
2014-03-02 22:58:14 +00:00
|
|
|
},
|
2020-07-30 11:36:11 +00:00
|
|
|
DNSNames: []string{commonName},
|
2018-10-21 05:17:50 +00:00
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
SignatureAlgorithm: x509.ECDSAWithSHA256,
|
2014-03-02 22:58:14 +00:00
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
2015-11-27 08:09:39 +00:00
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "create cert")
|
2014-09-20 13:42:20 +00:00
|
|
|
}
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2015-03-29 10:55:27 +00:00
|
|
|
certOut, err := os.Create(certFile)
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
2014-09-20 13:42:20 +00:00
|
|
|
}
|
2014-09-20 13:31:15 +00:00
|
|
|
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
2014-09-20 13:42:20 +00:00
|
|
|
}
|
2014-09-20 13:31:15 +00:00
|
|
|
err = certOut.Close()
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
2014-09-20 13:42:20 +00:00
|
|
|
}
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2015-03-29 10:55:27 +00:00
|
|
|
keyOut, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "save key")
|
2014-09-20 13:42:20 +00:00
|
|
|
}
|
2015-11-27 08:09:39 +00:00
|
|
|
|
|
|
|
block, err := pemBlockForKey(priv)
|
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "save key")
|
2015-11-27 08:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pem.Encode(keyOut, block)
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "save key")
|
2014-09-20 13:42:20 +00:00
|
|
|
}
|
2014-09-20 13:31:15 +00:00
|
|
|
err = keyOut.Close()
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return tls.Certificate{}, errors.Wrap(err, "save key")
|
2014-09-20 13:42:20 +00:00
|
|
|
}
|
2015-03-29 10:55:27 +00:00
|
|
|
|
|
|
|
return tls.LoadX509KeyPair(certFile, keyFile)
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
2014-09-11 19:18:08 +00:00
|
|
|
|
|
|
|
type DowngradingListener struct {
|
|
|
|
net.Listener
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
}
|
|
|
|
|
2014-09-14 22:18:05 +00:00
|
|
|
func (l *DowngradingListener) Accept() (net.Conn, error) {
|
2015-09-02 21:00:51 +00:00
|
|
|
conn, isTLS, err := l.AcceptNoWrapTLS()
|
2015-09-02 20:24:13 +00:00
|
|
|
|
|
|
|
// We failed to identify the socket type, pretend that everything is fine,
|
|
|
|
// and pass it to the underlying handler, and let them deal with it.
|
|
|
|
if err == ErrIdentificationFailed {
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isTLS {
|
|
|
|
return tls.Server(conn, l.TLSConfig), nil
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2015-09-02 21:00:51 +00:00
|
|
|
func (l *DowngradingListener) AcceptNoWrapTLS() (net.Conn, bool, error) {
|
2014-09-14 22:18:05 +00:00
|
|
|
conn, err := l.Listener.Accept()
|
2014-09-11 19:18:08 +00:00
|
|
|
if err != nil {
|
2015-09-02 20:24:13 +00:00
|
|
|
return nil, false, err
|
2014-09-11 19:18:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 06:40:34 +00:00
|
|
|
union := &UnionedConnection{Conn: conn}
|
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(1 * time.Second))
|
2021-09-21 06:40:34 +00:00
|
|
|
n, err := conn.Read(union.first[:])
|
2019-02-02 11:16:27 +00:00
|
|
|
conn.SetReadDeadline(time.Time{})
|
2017-08-31 07:34:48 +00:00
|
|
|
if err != nil || n == 0 {
|
2014-09-18 09:45:48 +00:00
|
|
|
// We hit a read error here, but the Accept() call succeeded so we must not return an error.
|
2015-09-02 20:24:13 +00:00
|
|
|
// We return the connection as is with a special error which handles this
|
|
|
|
// special case in Accept().
|
|
|
|
return conn, false, ErrIdentificationFailed
|
2014-09-11 19:18:08 +00:00
|
|
|
}
|
2014-09-14 22:18:05 +00:00
|
|
|
|
2021-09-21 06:40:34 +00:00
|
|
|
return union, union.first[0] == 0x16, nil
|
2014-09-11 19:18:08 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 21:00:51 +00:00
|
|
|
type UnionedConnection struct {
|
2021-09-21 06:40:34 +00:00
|
|
|
first [1]byte
|
|
|
|
firstDone bool
|
2015-09-02 20:05:54 +00:00
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
2015-09-02 21:00:51 +00:00
|
|
|
func (c *UnionedConnection) Read(b []byte) (n int, err error) {
|
2021-09-21 06:40:34 +00:00
|
|
|
if !c.firstDone {
|
2017-08-31 07:34:48 +00:00
|
|
|
if len(b) == 0 {
|
|
|
|
// this probably doesn't happen, but handle it anyway
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
b[0] = c.first[0]
|
2021-09-21 06:40:34 +00:00
|
|
|
c.firstDone = true
|
2017-08-31 07:34:48 +00:00
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
return c.Conn.Read(b)
|
2014-09-11 19:18:08 +00:00
|
|
|
}
|
2015-11-27 08:09:39 +00:00
|
|
|
|
|
|
|
func publicKey(priv interface{}) interface{} {
|
|
|
|
switch k := priv.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
|
|
|
return &k.PublicKey
|
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
return &k.PublicKey
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pemBlockForKey(priv interface{}) (*pem.Block, error) {
|
|
|
|
switch k := priv.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
|
|
|
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}, nil
|
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
b, err := x509.MarshalECPrivateKey(k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}, nil
|
|
|
|
default:
|
2020-03-03 21:40:00 +00:00
|
|
|
return nil, errors.New("unknown key type")
|
2015-11-27 08:09:39 +00:00
|
|
|
}
|
|
|
|
}
|