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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-09-14 22:18:05 +00:00
|
|
|
"bufio"
|
2014-03-02 22:58:14 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
2014-09-11 19:18:08 +00:00
|
|
|
"io"
|
2014-03-02 22:58:14 +00:00
|
|
|
"math/big"
|
2014-05-23 12:43:17 +00:00
|
|
|
mr "math/rand"
|
2014-09-11 19:18:08 +00:00
|
|
|
"net"
|
2014-03-02 22:58:14 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-12-09 09:42:56 +00:00
|
|
|
tlsRSABits = 3072
|
|
|
|
tlsDefaultCommonName = "syncthing"
|
2014-03-02 22:58:14 +00:00
|
|
|
)
|
|
|
|
|
2015-03-29 10:55:27 +00:00
|
|
|
func newCertificate(certFile, keyFile, name string) (tls.Certificate, error) {
|
2014-12-09 09:42:56 +00:00
|
|
|
l.Infof("Generating RSA key and certificate for %s...", name)
|
2014-03-02 22:58:14 +00:00
|
|
|
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, tlsRSABits)
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("generate key:", err)
|
|
|
|
}
|
2014-03-02 22:58:14 +00:00
|
|
|
|
|
|
|
notBefore := time.Now()
|
|
|
|
notAfter := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
2014-05-23 12:43:17 +00:00
|
|
|
SerialNumber: new(big.Int).SetInt64(mr.Int63()),
|
2014-03-02 22:58:14 +00:00
|
|
|
Subject: pkix.Name{
|
2014-12-09 09:42:56 +00:00
|
|
|
CommonName: name,
|
2014-03-02 22:58:14 +00:00
|
|
|
},
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("create cert:", err)
|
|
|
|
}
|
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 {
|
|
|
|
l.Fatalln("save cert:", err)
|
|
|
|
}
|
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 {
|
|
|
|
l.Fatalln("save cert:", err)
|
|
|
|
}
|
2014-09-20 13:31:15 +00:00
|
|
|
err = certOut.Close()
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save cert:", err)
|
|
|
|
}
|
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 {
|
|
|
|
l.Fatalln("save key:", err)
|
|
|
|
}
|
2014-09-20 13:31:15 +00:00
|
|
|
err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save key:", err)
|
|
|
|
}
|
2014-09-20 13:31:15 +00:00
|
|
|
err = keyOut.Close()
|
2014-09-20 13:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save key:", err)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type WrappedConnection struct {
|
|
|
|
io.Reader
|
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
2014-09-14 22:18:05 +00:00
|
|
|
func (l *DowngradingListener) Accept() (net.Conn, error) {
|
|
|
|
conn, err := l.Listener.Accept()
|
2014-09-11 19:18:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-09-14 22:18:05 +00:00
|
|
|
br := bufio.NewReader(conn)
|
2015-08-06 11:07:34 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(1 * time.Second))
|
2014-09-14 22:18:05 +00:00
|
|
|
bs, err := br.Peek(1)
|
2015-08-06 11:07:34 +00:00
|
|
|
conn.SetReadDeadline(time.Time{})
|
2014-09-11 19:18:08 +00:00
|
|
|
if err != nil {
|
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.
|
|
|
|
// We return the connection as is and let whoever tries to use it deal with the error.
|
|
|
|
return conn, nil
|
2014-09-11 19:18:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 22:18:05 +00:00
|
|
|
wrapper := &WrappedConnection{br, conn}
|
2014-09-11 19:18:08 +00:00
|
|
|
|
2014-09-14 22:18:05 +00:00
|
|
|
// 0x16 is the first byte of a TLS handshake
|
|
|
|
if bs[0] == 0x16 {
|
|
|
|
return tls.Server(wrapper, l.TLSConfig), nil
|
2014-09-11 19:18:08 +00:00
|
|
|
}
|
2014-09-14 22:18:05 +00:00
|
|
|
|
2014-09-11 19:18:08 +00:00
|
|
|
return wrapper, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WrappedConnection) Read(b []byte) (n int, err error) {
|
|
|
|
return c.Reader.Read(b)
|
|
|
|
}
|