github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/crypto/tls/tls.go (about)

     1  // TINYGO: The following is copied and modified from Go 1.21.4 official implementation.
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Package tls partially implements TLS 1.2, as specified in RFC 5246,
     8  // and TLS 1.3, as specified in RFC 8446.
     9  package tls
    10  
    11  // BUG(agl): The crypto/tls package only implements some countermeasures
    12  // against Lucky13 attacks on CBC-mode encryption, and only on SHA1
    13  // variants. See http://www.isg.rhul.ac.uk/tls/TLStiming.pdf and
    14  // https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"fmt"
    20  	"net"
    21  )
    22  
    23  // Client returns a new TLS client side connection
    24  // using conn as the underlying transport.
    25  // The config cannot be nil: users must set either ServerName or
    26  // InsecureSkipVerify in the config.
    27  func Client(conn net.Conn, config *Config) *net.TLSConn {
    28  	panic("tls.Client() not implemented")
    29  	return nil
    30  }
    31  
    32  // A listener implements a network listener (net.Listener) for TLS connections.
    33  type listener struct {
    34  	net.Listener
    35  	config *Config
    36  }
    37  
    38  // NewListener creates a Listener which accepts connections from an inner
    39  // Listener and wraps each connection with Server.
    40  // The configuration config must be non-nil and must include
    41  // at least one certificate or else set GetCertificate.
    42  func NewListener(inner net.Listener, config *Config) net.Listener {
    43  	l := new(listener)
    44  	l.Listener = inner
    45  	l.config = config
    46  	return l
    47  }
    48  
    49  // DialWithDialer connects to the given network address using dialer.Dial and
    50  // then initiates a TLS handshake, returning the resulting TLS connection. Any
    51  // timeout or deadline given in the dialer apply to connection and TLS
    52  // handshake as a whole.
    53  //
    54  // DialWithDialer interprets a nil configuration as equivalent to the zero
    55  // configuration; see the documentation of Config for the defaults.
    56  //
    57  // DialWithDialer uses context.Background internally; to specify the context,
    58  // use Dialer.DialContext with NetDialer set to the desired dialer.
    59  func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*net.TLSConn, error) {
    60  	switch network {
    61  	case "tcp", "tcp4":
    62  	default:
    63  		return nil, fmt.Errorf("Network %s not supported", network)
    64  	}
    65  
    66  	return net.DialTLS(addr)
    67  }
    68  
    69  // Dial connects to the given network address using net.Dial
    70  // and then initiates a TLS handshake, returning the resulting
    71  // TLS connection.
    72  // Dial interprets a nil configuration as equivalent to
    73  // the zero configuration; see the documentation of Config
    74  // for the defaults.
    75  func Dial(network, addr string, config *Config) (*net.TLSConn, error) {
    76  	return DialWithDialer(new(net.Dialer), network, addr, config)
    77  }
    78  
    79  // Dialer dials TLS connections given a configuration and a Dialer for the
    80  // underlying connection.
    81  type Dialer struct {
    82  	// NetDialer is the optional dialer to use for the TLS connections'
    83  	// underlying TCP connections.
    84  	// A nil NetDialer is equivalent to the net.Dialer zero value.
    85  	NetDialer *net.Dialer
    86  
    87  	// Config is the TLS configuration to use for new connections.
    88  	// A nil configuration is equivalent to the zero
    89  	// configuration; see the documentation of Config for the
    90  	// defaults.
    91  	Config *Config
    92  }
    93  
    94  // DialContext connects to the given network address and initiates a TLS
    95  // handshake, returning the resulting TLS connection.
    96  //
    97  // The provided Context must be non-nil. If the context expires before
    98  // the connection is complete, an error is returned. Once successfully
    99  // connected, any expiration of the context will not affect the
   100  // connection.
   101  //
   102  // The returned Conn, if any, will always be of type *Conn.
   103  func (d *Dialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
   104  	return nil, errors.New("tls:DialContext not implmented")
   105  }
   106  
   107  // LoadX509KeyPair reads and parses a public/private key pair from a pair
   108  // of files. The files must contain PEM encoded data. The certificate file
   109  // may contain intermediate certificates following the leaf certificate to
   110  // form a certificate chain. On successful return, Certificate.Leaf will
   111  // be nil because the parsed form of the certificate is not retained.
   112  func LoadX509KeyPair(certFile, keyFile string) (Certificate, error) {
   113  	return Certificate{}, errors.New("tls:LoadX509KeyPair not implemented")
   114  }