-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Derek Anderson <[email protected]> add x509 generation Signed-off-by: Derek Anderson <[email protected]> add tests Signed-off-by: Derek Anderson <[email protected]> add tls config with cert Signed-off-by: Derek Anderson <[email protected]> x509 generation from libp2p cert, TLS upgrade for WS transport. Signed-off-by: Derek Anderson <[email protected]> cleanup Signed-off-by: Derek Anderson <[email protected]> put back in trap comment Signed-off-by: Derek Anderson <[email protected]> load the private key, and then generate the x509 Signed-off-by: Derek Anderson <[email protected]> update makefile for platform detection Signed-off-by: Derek Anderson <[email protected]> update make file to run node Signed-off-by: Derek Anderson <[email protected]> update certificate creation for ed25519, update makefile Signed-off-by: Derek Anderson <[email protected]> configure org name Signed-off-by: Derek Anderson <[email protected]>
- Loading branch information
Showing
6 changed files
with
238 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package host | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"fmt" | ||
"math/big" | ||
"time" | ||
|
||
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto" | ||
) | ||
|
||
// Convert a libp2p PrivKey to a crypto.PrivateKey | ||
func convertLibp2pPrivKeyToCryptoPrivKey(privKey libp2pcrypto.PrivKey) (crypto.PrivateKey, error) { | ||
rawKey, err := privKey.Raw() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch privKey.Type() { | ||
case libp2pcrypto.RSA: | ||
return x509.ParsePKCS1PrivateKey(rawKey) | ||
case libp2pcrypto.ECDSA: | ||
return x509.ParseECPrivateKey(rawKey) | ||
case libp2pcrypto.Ed25519: | ||
return ed25519.PrivateKey(rawKey), nil | ||
default: | ||
return nil, fmt.Errorf("unsupported key type for X.509 conversion") | ||
} | ||
} | ||
|
||
func generateX509Certificate(privKey crypto.PrivateKey) (tls.Certificate, error) { | ||
// Define certificate template | ||
template := &x509.Certificate{ | ||
SerialNumber: big.NewInt(1), | ||
Subject: pkix.Name{ | ||
Organization: []string{"b7s"}, | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(365 * 24 * time.Hour), // 1 year validity | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
} | ||
|
||
pubKey := publicKey(privKey) | ||
|
||
// Create the certificate | ||
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, pubKey, privKey) | ||
if err != nil { | ||
return tls.Certificate{}, err | ||
} | ||
|
||
// Encode the certificate and private key | ||
cert := tls.Certificate{ | ||
Certificate: [][]byte{derBytes}, | ||
PrivateKey: privKey, | ||
} | ||
|
||
return cert, nil | ||
} | ||
|
||
func publicKey(priv crypto.PrivateKey) crypto.PublicKey { | ||
switch key := priv.(type) { | ||
case *rsa.PrivateKey: | ||
return &key.PublicKey | ||
case *ecdsa.PrivateKey: | ||
return &key.PublicKey | ||
case ed25519.PrivateKey: | ||
return key.Public().(ed25519.PublicKey) | ||
default: | ||
panic("unsupported key type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package host | ||
|
||
import ( | ||
"crypto/rand" | ||
"testing" | ||
|
||
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertLibp2pPrivKeyToCryptoPrivKey(t *testing.T) { | ||
// Generate a libp2p ECDSA key pair for testing | ||
priv, _, err := libp2pcrypto.GenerateECDSAKeyPair(rand.Reader) | ||
assert.NoError(t, err, "failed to generate libp2p ECDSA key pair") | ||
|
||
// Convert the libp2p private key to a crypto.PrivateKey | ||
cryptoPriv, err := convertLibp2pPrivKeyToCryptoPrivKey(priv) | ||
assert.NoError(t, err, "failed to convert libp2p private key to crypto private key") | ||
assert.NotNil(t, cryptoPriv, "converted crypto private key should not be nil") | ||
} | ||
|
||
func TestGenerateX509Certificate(t *testing.T) { | ||
// Generate a libp2p ECDSA key pair for testing | ||
priv, _, err := libp2pcrypto.GenerateECDSAKeyPair(rand.Reader) | ||
assert.NoError(t, err, "failed to generate libp2p ECDSA key pair") | ||
|
||
// Convert the libp2p private key to a crypto.PrivateKey | ||
cryptoPriv, err := convertLibp2pPrivKeyToCryptoPrivKey(priv) | ||
assert.NoError(t, err, "failed to convert libp2p private key") | ||
|
||
// Generate an X.509 certificate | ||
cert, err := generateX509Certificate(cryptoPriv) | ||
assert.NoError(t, err, "failed to generate X.509 certificate") | ||
assert.NotEmpty(t, cert.Certificate, "certificate should contain at least one DER encoded block") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters