2017-06-06 03:58:58 +08:00
|
|
|
// Copyright (C) 2017. See AUTHORS.
|
2014-05-10 05:38:13 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-01-19 03:34:19 +08:00
|
|
|
|
|
|
|
package openssl
|
|
|
|
|
|
|
|
import (
|
2014-02-26 02:45:14 +08:00
|
|
|
"errors"
|
|
|
|
"net"
|
2022-04-15 15:04:29 +08:00
|
|
|
"time"
|
2014-01-19 03:34:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type listener struct {
|
2014-02-26 02:45:14 +08:00
|
|
|
net.Listener
|
|
|
|
ctx *Ctx
|
2014-01-19 03:34:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *listener) Accept() (c net.Conn, err error) {
|
2014-02-26 02:45:14 +08:00
|
|
|
c, err = l.Listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-21 16:27:58 +08:00
|
|
|
ssl_c, err := Server(c, l.ctx)
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ssl_c, nil
|
2014-01-19 03:34:19 +08:00
|
|
|
}
|
|
|
|
|
2014-01-19 14:55:13 +08:00
|
|
|
// NewListener wraps an existing net.Listener such that all accepted
|
|
|
|
// connections are wrapped as OpenSSL server connections using the provided
|
|
|
|
// context ctx.
|
2014-01-19 03:34:19 +08:00
|
|
|
func NewListener(inner net.Listener, ctx *Ctx) net.Listener {
|
2014-02-26 02:45:14 +08:00
|
|
|
return &listener{
|
|
|
|
Listener: inner,
|
|
|
|
ctx: ctx}
|
2014-01-19 03:34:19 +08:00
|
|
|
}
|
|
|
|
|
2014-01-19 14:55:13 +08:00
|
|
|
// Listen is a wrapper around net.Listen that wraps incoming connections with
|
|
|
|
// an OpenSSL server connection using the provided context ctx.
|
2014-01-19 03:34:19 +08:00
|
|
|
func Listen(network, laddr string, ctx *Ctx) (net.Listener, error) {
|
2014-02-26 02:45:14 +08:00
|
|
|
if ctx == nil {
|
|
|
|
return nil, errors.New("no ssl context provided")
|
|
|
|
}
|
|
|
|
l, err := net.Listen(network, laddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewListener(l, ctx), nil
|
2014-01-19 03:34:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type DialFlags int
|
|
|
|
|
|
|
|
const (
|
2014-04-29 17:00:44 +08:00
|
|
|
InsecureSkipHostVerification DialFlags = 1 << iota
|
|
|
|
DisableSNI
|
2014-01-19 03:34:19 +08:00
|
|
|
)
|
|
|
|
|
2014-01-19 14:55:13 +08:00
|
|
|
// Dial will connect to network/address and then wrap the corresponding
|
|
|
|
// underlying connection with an OpenSSL client connection using context ctx.
|
|
|
|
// If flags includes InsecureSkipHostVerification, the server certificate's
|
|
|
|
// hostname will not be checked to match the hostname in addr. Otherwise, flags
|
|
|
|
// should be 0.
|
|
|
|
//
|
|
|
|
// Dial probably won't work for you unless you set a verify location or add
|
|
|
|
// some certs to the certificate store of the client context you're using.
|
|
|
|
// This library is not nice enough to use the system certificate store by
|
|
|
|
// default for you yet.
|
2014-01-19 03:34:19 +08:00
|
|
|
func Dial(network, addr string, ctx *Ctx, flags DialFlags) (*Conn, error) {
|
2015-06-03 03:27:29 +08:00
|
|
|
return DialSession(network, addr, ctx, flags, nil)
|
|
|
|
}
|
|
|
|
|
2022-04-15 15:04:29 +08:00
|
|
|
// DialTimeout acts like Dial but takes a timeout for network dial.
|
|
|
|
//
|
|
|
|
// The timeout includes only network dial. It does not include OpenSSL calls.
|
|
|
|
//
|
|
|
|
// See func Dial for a description of the network, addr, ctx and flags
|
|
|
|
// parameters.
|
|
|
|
func DialTimeout(network, addr string, timeout time.Duration, ctx *Ctx,
|
|
|
|
flags DialFlags) (*Conn, error) {
|
2022-06-06 21:38:07 +08:00
|
|
|
d := net.Dialer{Timeout: timeout}
|
2022-04-15 15:04:29 +08:00
|
|
|
return dialSession(d, network, addr, ctx, flags, nil)
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:27:29 +08:00
|
|
|
// DialSession will connect to network/address and then wrap the corresponding
|
|
|
|
// underlying connection with an OpenSSL client connection using context ctx.
|
|
|
|
// If flags includes InsecureSkipHostVerification, the server certificate's
|
|
|
|
// hostname will not be checked to match the hostname in addr. Otherwise, flags
|
|
|
|
// should be 0.
|
|
|
|
//
|
|
|
|
// Dial probably won't work for you unless you set a verify location or add
|
|
|
|
// some certs to the certificate store of the client context you're using.
|
|
|
|
// This library is not nice enough to use the system certificate store by
|
|
|
|
// default for you yet.
|
|
|
|
//
|
|
|
|
// If session is not nil it will be used to resume the tls state. The session
|
|
|
|
// can be retrieved from the GetSession method on the Conn.
|
|
|
|
func DialSession(network, addr string, ctx *Ctx, flags DialFlags,
|
|
|
|
session []byte) (*Conn, error) {
|
2022-04-15 15:04:29 +08:00
|
|
|
var d net.Dialer
|
2022-06-06 21:38:07 +08:00
|
|
|
return dialSession(d, network, addr, ctx, flags, session)
|
2022-04-15 15:04:29 +08:00
|
|
|
}
|
2015-06-03 03:27:29 +08:00
|
|
|
|
2022-04-15 15:04:29 +08:00
|
|
|
func dialSession(d net.Dialer, network, addr string, ctx *Ctx, flags DialFlags,
|
|
|
|
session []byte) (*Conn, error) {
|
2014-04-29 17:00:44 +08:00
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-26 02:45:14 +08:00
|
|
|
if ctx == nil {
|
|
|
|
var err error
|
|
|
|
ctx, err = NewCtx()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO: use operating system default certificate chain?
|
|
|
|
}
|
2022-04-15 15:04:29 +08:00
|
|
|
|
|
|
|
c, err := d.Dial(network, addr)
|
2014-02-26 02:45:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conn, err := Client(c, ctx)
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-03 03:27:29 +08:00
|
|
|
if session != nil {
|
|
|
|
err := conn.setSession(session)
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 17:00:44 +08:00
|
|
|
if flags&DisableSNI == 0 {
|
|
|
|
err = conn.SetTlsExtHostName(host)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 02:45:14 +08:00
|
|
|
err = conn.Handshake()
|
|
|
|
if err != nil {
|
2014-05-16 01:00:26 +08:00
|
|
|
conn.Close()
|
2014-02-26 02:45:14 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if flags&InsecureSkipHostVerification == 0 {
|
|
|
|
err = conn.VerifyHostname(host)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conn, nil
|
2014-01-19 03:34:19 +08:00
|
|
|
}
|