Source File
tls.go
Belonging Package
crypto/tls
package tls
import (
)
func ( net.Conn, *Config) *Conn {
:= &Conn{
conn: ,
config: ,
}
.handshakeFn = .serverHandshake
return
}
func (, string, *Config) (net.Listener, error) {
if == nil || len(.Certificates) == 0 &&
.GetCertificate == nil && .GetConfigForClient == nil {
return nil, errors.New("tls: neither Certificates, GetCertificate, nor GetConfigForClient set in Config")
}
, := net.Listen(, )
if != nil {
return nil,
}
return NewListener(, ), nil
}
type timeoutError struct{}
func (timeoutError) () string { return "tls: DialWithDialer timed out" }
func (timeoutError) () bool { return true }
func (timeoutError) () bool { return true }
func (, string) (Certificate, error) {
, := os.ReadFile()
if != nil {
return Certificate{},
}
, := os.ReadFile()
if != nil {
return Certificate{},
}
return X509KeyPair(, )
}
func (, []byte) (Certificate, error) {
:= func( error) (Certificate, error) { return Certificate{}, }
var Certificate
var []string
for {
var *pem.Block
, = pem.Decode()
if == nil {
break
}
if .Type == "CERTIFICATE" {
.Certificate = append(.Certificate, .Bytes)
} else {
= append(, .Type)
}
}
if len(.Certificate) == 0 {
if len() == 0 {
return (errors.New("tls: failed to find any PEM data in certificate input"))
}
if len() == 1 && strings.HasSuffix([0], "PRIVATE KEY") {
return (errors.New("tls: failed to find certificate PEM data in certificate input, but did find a private key; PEM inputs may have been switched"))
}
return (fmt.Errorf("tls: failed to find \"CERTIFICATE\" PEM block in certificate input after skipping PEM blocks of the following types: %v", ))
}
= [:0]
var *pem.Block
for {
, = pem.Decode()
if == nil {
if len() == 0 {
return (errors.New("tls: failed to find any PEM data in key input"))
}
if len() == 1 && [0] == "CERTIFICATE" {
return (errors.New("tls: found a certificate rather than a key in the PEM for the private key"))
}
return (fmt.Errorf("tls: failed to find PEM block with type ending in \"PRIVATE KEY\" in key input after skipping PEM blocks of the following types: %v", ))
}
if .Type == "PRIVATE KEY" || strings.HasSuffix(.Type, " PRIVATE KEY") {
break
}
= append(, .Type)
}
, := x509.ParseCertificate(.Certificate[0])
if != nil {
return ()
}
.PrivateKey, = parsePrivateKey(.Bytes)
if != nil {
return ()
}
switch pub := .PublicKey.(type) {
case *rsa.PublicKey:
, := .PrivateKey.(*rsa.PrivateKey)
if ! {
return (errors.New("tls: private key type does not match public key type"))
}
if .N.Cmp(.N) != 0 {
return (errors.New("tls: private key does not match public key"))
}
case *ecdsa.PublicKey:
, := .PrivateKey.(*ecdsa.PrivateKey)
if ! {
return (errors.New("tls: private key type does not match public key type"))
}
if .X.Cmp(.X) != 0 || .Y.Cmp(.Y) != 0 {
return (errors.New("tls: private key does not match public key"))
}
case ed25519.PublicKey:
, := .PrivateKey.(ed25519.PrivateKey)
if ! {
return (errors.New("tls: private key type does not match public key type"))
}
if !bytes.Equal(.Public().(ed25519.PublicKey), ) {
return (errors.New("tls: private key does not match public key"))
}
default:
return (errors.New("tls: unknown public key algorithm"))
}
return , nil
}
func ( []byte) (crypto.PrivateKey, error) {
if , := x509.ParsePKCS1PrivateKey(); == nil {
return , nil
}
if , := x509.ParsePKCS8PrivateKey(); == nil {
switch key := .(type) {
case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey:
return , nil
default:
return nil, errors.New("tls: found unknown private key type in PKCS#8 wrapping")
}
}
if , := x509.ParseECPrivateKey(); == nil {
return , nil
}
return nil, errors.New("tls: failed to parse private key")
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |