mirror of
https://github.com/slackhq/nebula.git
synced 2025-01-25 17:48:25 +00:00
e0185c4b01
* Support NIST curve P256 This change adds support for NIST curve P256. When you use `nebula-cert ca` or `nebula-cert keygen`, you can specify `-curve P256` to enable it. The curve to use is based on the curve defined in your CA certificate. Internally, we use ECDSA P256 to sign certificates, and ECDH P256 to do Noise handshakes. P256 is not supported natively in Noise Protocol, so we define `DHP256` in the `noiseutil` package to implement support for it. You cannot have a mixed network of Curve25519 and P256 certificates, since the Noise protocol will only attempt to parse using the Curve defined in the host's certificate. * verify the curves match in VerifyPrivateKey This would have failed anyways once we tried to actually use the bytes in the private key, but its better to detect the issue up front with a better error message. * add cert.Curve argument to Sign method * fix mismerge * use crypto/ecdh This is the preferred method for doing ECDH functions now, and also has a boringcrypto specific codepath. * remove other ecdh uses of crypto/elliptic use crypto/ecdh instead
94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/slackhq/nebula/cert"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
//TODO: test file permissions
|
|
|
|
func Test_keygenSummary(t *testing.T) {
|
|
assert.Equal(t, "keygen <flags>: create a public/private key pair. the public key can be passed to `nebula-cert sign`", keygenSummary())
|
|
}
|
|
|
|
func Test_keygenHelp(t *testing.T) {
|
|
ob := &bytes.Buffer{}
|
|
keygenHelp(ob)
|
|
assert.Equal(
|
|
t,
|
|
"Usage of "+os.Args[0]+" keygen <flags>: create a public/private key pair. the public key can be passed to `nebula-cert sign`\n"+
|
|
" -curve string\n"+
|
|
" \tECDH Curve (25519, P256) (default \"25519\")\n"+
|
|
" -out-key string\n"+
|
|
" \tRequired: path to write the private key to\n"+
|
|
" -out-pub string\n"+
|
|
" \tRequired: path to write the public key to\n",
|
|
ob.String(),
|
|
)
|
|
}
|
|
|
|
func Test_keygen(t *testing.T) {
|
|
ob := &bytes.Buffer{}
|
|
eb := &bytes.Buffer{}
|
|
|
|
// required args
|
|
assertHelpError(t, keygen([]string{"-out-pub", "nope"}, ob, eb), "-out-key is required")
|
|
assert.Equal(t, "", ob.String())
|
|
assert.Equal(t, "", eb.String())
|
|
|
|
assertHelpError(t, keygen([]string{"-out-key", "nope"}, ob, eb), "-out-pub is required")
|
|
assert.Equal(t, "", ob.String())
|
|
assert.Equal(t, "", eb.String())
|
|
|
|
// failed key write
|
|
ob.Reset()
|
|
eb.Reset()
|
|
args := []string{"-out-pub", "/do/not/write/pleasepub", "-out-key", "/do/not/write/pleasekey"}
|
|
assert.EqualError(t, keygen(args, ob, eb), "error while writing out-key: open /do/not/write/pleasekey: "+NoSuchDirError)
|
|
assert.Equal(t, "", ob.String())
|
|
assert.Equal(t, "", eb.String())
|
|
|
|
// create temp key file
|
|
keyF, err := ioutil.TempFile("", "test.key")
|
|
assert.Nil(t, err)
|
|
defer os.Remove(keyF.Name())
|
|
|
|
// failed pub write
|
|
ob.Reset()
|
|
eb.Reset()
|
|
args = []string{"-out-pub", "/do/not/write/pleasepub", "-out-key", keyF.Name()}
|
|
assert.EqualError(t, keygen(args, ob, eb), "error while writing out-pub: open /do/not/write/pleasepub: "+NoSuchDirError)
|
|
assert.Equal(t, "", ob.String())
|
|
assert.Equal(t, "", eb.String())
|
|
|
|
// create temp pub file
|
|
pubF, err := ioutil.TempFile("", "test.pub")
|
|
assert.Nil(t, err)
|
|
defer os.Remove(pubF.Name())
|
|
|
|
// test proper keygen
|
|
ob.Reset()
|
|
eb.Reset()
|
|
args = []string{"-out-pub", pubF.Name(), "-out-key", keyF.Name()}
|
|
assert.Nil(t, keygen(args, ob, eb))
|
|
assert.Equal(t, "", ob.String())
|
|
assert.Equal(t, "", eb.String())
|
|
|
|
// read cert and key files
|
|
rb, _ := ioutil.ReadFile(keyF.Name())
|
|
lKey, b, err := cert.UnmarshalX25519PrivateKey(rb)
|
|
assert.Len(t, b, 0)
|
|
assert.Nil(t, err)
|
|
assert.Len(t, lKey, 32)
|
|
|
|
rb, _ = ioutil.ReadFile(pubF.Name())
|
|
lPub, b, err := cert.UnmarshalX25519PublicKey(rb)
|
|
assert.Len(t, b, 0)
|
|
assert.Nil(t, err)
|
|
assert.Len(t, lPub, 32)
|
|
}
|