Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move go-libp2p-tls here #1466

Merged
merged 76 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
82f9c87
initial commit
marten-seemann Nov 24, 2018
b70779f
add a license
marten-seemann Nov 24, 2018
09e1e2a
fix handshake tests
marten-seemann Nov 24, 2018
63843cc
close the underlying connection when the context is canceled
marten-seemann Nov 28, 2018
1c0f10c
return the context cancelation error
marten-seemann Nov 29, 2018
eaf15fd
simplify returning of context cancellation errors
marten-seemann Nov 29, 2018
21cff47
Merge pull request #5 from libp2p/use-context
marten-seemann Nov 30, 2018
955b805
add support for ECDSA keys
marten-seemann Jan 11, 2019
936688b
Merge pull request #9 from libp2p/ecdsa
marten-seemann Jan 12, 2019
10b3e2a
avoid using interface{} when generating certificates
marten-seemann Feb 10, 2019
517ac00
Merge pull request #13 from libp2p/better-type-checking
marten-seemann Feb 17, 2019
9f8a324
remove unneeded marshaling / unmarshaling when generating cert chain
marten-seemann Feb 17, 2019
a16ab88
Merge pull request #12 from libp2p/optimize-cert-chain-generation
marten-seemann Feb 17, 2019
5ecc2f9
drop support for Go 1.10
marten-seemann Feb 10, 2019
4909818
improve logging in tests
marten-seemann Feb 10, 2019
0b45a8d
make sure to close the connection if the context is already canceled
marten-seemann Feb 10, 2019
1c09b02
switch to TLS 1.3
marten-seemann Feb 10, 2019
df91dee
Merge pull request #11 from libp2p/tls13
marten-seemann Feb 17, 2019
f799512
derive and save the client's pub key in tls.Config.VerifyPeerCertificate
marten-seemann Feb 21, 2019
5677418
derive and save the server's pub key in tls.Config.VerifyPeerCertificate
marten-seemann Feb 21, 2019
92fedfe
make peer verification use a channel
Stebalien Feb 27, 2019
ebc4872
fix duplicate import
marten-seemann Feb 28, 2019
7d129c2
add an example server and client
marten-seemann Feb 20, 2019
e4b8bb7
add a command line flag to set the key type
marten-seemann Feb 20, 2019
e996c4a
rename example to cmd, move to a single .go file
marten-seemann Feb 21, 2019
b405c82
Merge pull request #14 from libp2p/example
marten-seemann Feb 28, 2019
2000551
fix package name of tlsdiag
marten-seemann Feb 28, 2019
e31e5a8
Merge pull request #17 from libp2p/peer-verification
marten-seemann Feb 28, 2019
c6c1a34
implement the new handshake
marten-seemann Feb 20, 2019
afcc2e4
fix logging when using secp256k1 key in tests
raulk Mar 11, 2019
a2bf05d
use the new Protocol Labs PEN for the certificate extension
marten-seemann Mar 14, 2019
a49a4b1
use ChaCha if one of the peers doesn't have AES hardware support
marten-seemann Mar 11, 2019
5f29402
Merge pull request #23 from libp2p/chacha-preference
marten-seemann Mar 14, 2019
2b073e1
use a prefix when signing the public key
marten-seemann Mar 14, 2019
d8bd9ab
Merge pull request #26 from libp2p/cert-prefix
marten-seemann Apr 7, 2019
2684cc1
disable session tickets
marten-seemann Apr 7, 2019
173abf7
Merge pull request #20 from libp2p/new-handshake
marten-seemann Apr 9, 2019
c37e733
migrate to consolidated types. (#30)
raulk May 27, 2019
250af20
don't use deprecated go-libp2p-crypto.MarshalPublicKey
marten-seemann Jul 27, 2019
08736ac
set an ALPN value in the tls.Config
marten-seemann Jul 27, 2019
f7ede37
expose the function to derive the peer's public key from the cert chain
marten-seemann Jul 29, 2019
cca3e39
Merge pull request #33 from libp2p/expose-pubkey-from-certchain
marten-seemann Aug 1, 2019
ea13d7a
make the error check for not receiving a public key more explicit
marten-seemann Aug 1, 2019
0328485
Fix: Connection Closed after handshake
hsanjuan Nov 1, 2019
6b8d597
Merge pull request #37 from libp2p/fix/handshake-cancelled
Stebalien Nov 1, 2019
7c0aea2
Merge pull request #34 from libp2p/no-key-error-check
Stebalien Nov 12, 2019
3b381d8
close the underlying connection when the handshake fails
marten-seemann Nov 24, 2019
3fc3210
Merge pull request #39 from libp2p/close-conn-on-error
marten-seemann Nov 26, 2019
04be62c
chore: update deps
Stebalien Dec 7, 2019
21893f7
Merge pull request #43 from libp2p/chore/update
Stebalien Dec 7, 2019
1ca1358
update to Go 1.14
marten-seemann Mar 13, 2020
5a96fcd
Merge pull request #54 from libp2p/go1.14
Stebalien Mar 13, 2020
4fa1ab4
improve the error message returned when peer verification fails
marten-seemann Apr 21, 2020
d3b17bf
Merge pull request #57 from libp2p/improve-verification-error
marten-seemann Apr 21, 2020
8aa3448
remove setting of the TLS 1.3 GODEBUG flag
marten-seemann Jul 30, 2020
14a65b8
Merge pull request #68 from libp2p/remove-tls13-godebug-check
marten-seemann Jul 30, 2020
aaa62b9
fix usage of deprecated peer.IDB58Decode
marten-seemann Mar 30, 2021
aa3fa7d
fix deprecated call to key.Bytes
marten-seemann Jul 23, 2021
6e77466
Merge pull request #86 from libp2p/fix-deprecated-key-bytes-calls
marten-seemann Jul 23, 2021
1e7a4d7
fix: don't fail the handshake when the libp2p extension is critical
marten-seemann Aug 7, 2021
0f4d03d
Merge pull request #88 from libp2p/handle-critical-extension
marten-seemann Aug 7, 2021
958fc8e
fix keys used for generating cert chain in tests
marten-seemann Sep 5, 2021
52f323e
Merge pull request #91 from libp2p/web3-bot/sync
marten-seemann Sep 5, 2021
2bf69fb
add the peer ID to SecureInbound
marten-seemann Sep 5, 2021
d7493ef
Merge pull request #94 from libp2p/check-peer-id-on-inbound
marten-seemann Sep 8, 2021
1b09f16
set a random certificate issuer
marten-seemann Nov 9, 2021
433e650
set an actual NotBefore time on the certificate
marten-seemann Nov 22, 2021
414ea4c
migrate the extension tests away from Ginkgo
marten-seemann Jan 4, 2022
07909fc
migrate the transport tests away from Ginkgo
marten-seemann Jan 4, 2022
5ffe478
remove the Ginkgo test suite
marten-seemann Jan 4, 2022
b4e9948
Merge pull request #105 from libp2p/bye-ginkgo
marten-seemann Jan 4, 2022
7ee67dd
use tls.Conn.HandshakeContext instead of tls.Conn.Handshake (#106)
marten-seemann Apr 10, 2022
6d7a396
feat: catch panics in TLS negotiation
Stebalien Apr 19, 2022
6c27b17
Merge pull request #111 from libp2p/feat/catch-panic
Stebalien Apr 19, 2022
57ea005
move go-libp2p-tls here
marten-seemann Apr 27, 2022
27cfd3f
switch from github.com/libp2p/go-libp2p-tls to p2p/security/tls
marten-seemann Apr 27, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/security/noise"
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
Expand All @@ -16,7 +17,6 @@ import (

"github.com/libp2p/go-libp2p-peerstore/pstoremem"
rcmgr "github.com/libp2p/go-libp2p-resource-manager"
tls "github.com/libp2p/go-libp2p-tls"
"github.com/multiformats/go-multiaddr"
)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/libp2p/go-libp2p-pnet v0.2.0
github.com/libp2p/go-libp2p-resource-manager v0.2.1
github.com/libp2p/go-libp2p-testing v0.9.2
github.com/libp2p/go-libp2p-tls v0.4.1
github.com/libp2p/go-mplex v0.7.0
github.com/libp2p/go-msgio v0.2.0
github.com/libp2p/go-netroute v0.2.0
Expand All @@ -52,6 +51,7 @@ require (
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad
)

require (
Expand Down Expand Up @@ -82,6 +82,7 @@ require (
github.com/libp2p/go-libp2p-blankhost v0.3.0 // indirect
github.com/libp2p/go-libp2p-quic-transport v0.17.0 // indirect
github.com/libp2p/go-libp2p-swarm v0.10.2 // indirect
github.com/libp2p/go-libp2p-tls v0.4.1 // indirect
github.com/libp2p/go-libp2p-transport-upgrader v0.7.1 // indirect
github.com/libp2p/go-libp2p-yamux v0.9.1 // indirect
github.com/libp2p/go-nat v0.1.0 // indirect
Expand Down Expand Up @@ -117,7 +118,6 @@ require (
go.uber.org/zap v1.21.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20220418201149-a630d4f3e7a2 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/grpc v1.45.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions p2p/security/tls/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Marten Seemann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 changes: 6 additions & 0 deletions p2p/security/tls/cmd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# TLS handshake example

Run
```bash
go run cmd/tlsdiag.go server
```
33 changes: 33 additions & 0 deletions p2p/security/tls/cmd/tlsdiag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"
"os"

"github.com/libp2p/go-libp2p/p2p/security/tls/cmd/tlsdiag"
)

func main() {
if len(os.Args) <= 1 {
fmt.Println("missing argument: client / server")
return
}

role := os.Args[1]
// remove the role argument from os.Args
os.Args = append([]string{os.Args[0]}, os.Args[2:]...)

var err error
switch role {
case "client":
err = tlsdiag.StartClient()
case "server":
err = tlsdiag.StartServer()
default:
fmt.Println("invalid argument. Expected client / server")
return
}
if err != nil {
panic(err)
}
}
63 changes: 63 additions & 0 deletions p2p/security/tls/cmd/tlsdiag/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tlsdiag

import (
"context"
"flag"
"fmt"
"io/ioutil"
"net"
"time"

libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"

"github.com/libp2p/go-libp2p-core/peer"
)

func StartClient() error {
port := flag.Int("p", 5533, "port")
peerIDString := flag.String("id", "", "peer ID")
keyType := flag.String("key", "ecdsa", "rsa, ecdsa, ed25519 or secp256k1")
flag.Parse()

priv, err := generateKey(*keyType)
if err != nil {
return err
}

peerID, err := peer.Decode(*peerIDString)
if err != nil {
return err
}

id, err := peer.IDFromPrivateKey(priv)
if err != nil {
return err
}
fmt.Printf(" Peer ID: %s\n", id.Pretty())
tp, err := libp2ptls.New(priv)
if err != nil {
return err
}

remoteAddr := fmt.Sprintf("localhost:%d", *port)
fmt.Printf("Dialing %s\n", remoteAddr)
conn, err := net.Dial("tcp", remoteAddr)
if err != nil {
return err
}
fmt.Printf("Dialed raw connection to %s\n", conn.RemoteAddr())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
sconn, err := tp.SecureOutbound(ctx, conn, peerID)
if err != nil {
return err
}
fmt.Printf("Authenticated server: %s\n", sconn.RemotePeer().Pretty())
data, err := ioutil.ReadAll(sconn)
if err != nil {
return err
}
fmt.Printf("Received message from server: %s\n", string(data))
return nil
}
28 changes: 28 additions & 0 deletions p2p/security/tls/cmd/tlsdiag/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tlsdiag

import (
"crypto/rand"
"fmt"

ic "github.com/libp2p/go-libp2p-core/crypto"
)

func generateKey(keyType string) (priv ic.PrivKey, err error) {
switch keyType {
case "rsa":
fmt.Printf("Generated new peer with an RSA key.")
priv, _, err = ic.GenerateRSAKeyPair(2048, rand.Reader)
case "ecdsa":
fmt.Printf("Generated new peer with an ECDSA key.")
priv, _, err = ic.GenerateECDSAKeyPair(rand.Reader)
case "ed25519":
fmt.Printf("Generated new peer with an Ed25519 key.")
priv, _, err = ic.GenerateEd25519Key(rand.Reader)
case "secp256k1":
fmt.Printf("Generated new peer with an Secp256k1 key.")
priv, _, err = ic.GenerateSecp256k1Key(rand.Reader)
default:
return nil, fmt.Errorf("unknown key type: %s", keyType)
}
return
}
68 changes: 68 additions & 0 deletions p2p/security/tls/cmd/tlsdiag/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package tlsdiag

import (
"context"
"flag"
"fmt"
"net"
"time"

libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"

"github.com/libp2p/go-libp2p-core/peer"
)

func StartServer() error {
port := flag.Int("p", 5533, "port")
keyType := flag.String("key", "ecdsa", "rsa, ecdsa, ed25519 or secp256k1")
flag.Parse()

priv, err := generateKey(*keyType)
if err != nil {
return err
}

id, err := peer.IDFromPrivateKey(priv)
if err != nil {
return err
}
fmt.Printf(" Peer ID: %s\n", id.Pretty())
tp, err := libp2ptls.New(priv)
if err != nil {
return err
}

ln, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
if err != nil {
return err
}
fmt.Printf("Listening for new connections on %s\n", ln.Addr())
fmt.Printf("Now run the following command in a separate terminal:\n")
fmt.Printf("\tgo run cmd/tlsdiag.go client -p %d -id %s\n", *port, id.Pretty())

for {
conn, err := ln.Accept()
if err != nil {
return err
}
fmt.Printf("Accepted raw connection from %s\n", conn.RemoteAddr())
go func() {
if err := handleConn(tp, conn); err != nil {
fmt.Printf("Error handling connection from %s: %s\n", conn.RemoteAddr(), err)
}
}()
}
}

func handleConn(tp *libp2ptls.Transport, conn net.Conn) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
sconn, err := tp.SecureInbound(ctx, conn, "")
if err != nil {
return err
}
fmt.Printf("Authenticated client: %s\n", sconn.RemotePeer().Pretty())
fmt.Fprintf(sconn, "Hello client!")
fmt.Printf("Closing connection to %s\n", conn.RemoteAddr())
return sconn.Close()
}
37 changes: 37 additions & 0 deletions p2p/security/tls/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package libp2ptls

import (
"crypto/tls"

ci "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/sec"
)

type conn struct {
*tls.Conn

localPeer peer.ID
privKey ci.PrivKey

remotePeer peer.ID
remotePubKey ci.PubKey
}

var _ sec.SecureConn = &conn{}

func (c *conn) LocalPeer() peer.ID {
return c.localPeer
}

func (c *conn) LocalPrivateKey() ci.PrivKey {
return c.privKey
}

func (c *conn) RemotePeer() peer.ID {
return c.remotePeer
}

func (c *conn) RemotePublicKey() ci.PubKey {
return c.remotePubKey
}
Loading