replace certificate path with tls.Config instance throughout the server

The example server now reads the certificate and key data itself. Tests
use the new testdata package, where the sample key & cert are stored.

Fixes #24
This commit is contained in:
Lucas Clemente
2016-05-03 16:41:25 +02:00
parent 7adafb4c5f
commit c068cbcb8f
9 changed files with 105 additions and 44 deletions

View File

@@ -8,8 +8,9 @@ import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"errors"
"github.com/lucas-clemente/quic-go/utils"
)
@@ -21,25 +22,23 @@ type rsaSigner struct {
}
// NewRSASigner loads the key and cert from files
func NewRSASigner(certFileName string, keyFileName string) (Signer, error) {
keyDER, err := ioutil.ReadFile(keyFileName)
if err != nil {
return nil, err
func NewRSASigner(tlsConfig *tls.Config) (Signer, error) {
if len(tlsConfig.Certificates) == 0 {
return nil, errors.New("Expected at least one certificate in TLS config")
}
key, err := x509.ParsePKCS1PrivateKey(keyDER)
if err != nil {
return nil, err
}
certDER, err := ioutil.ReadFile(certFileName)
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(certDER)
cert := tlsConfig.Certificates[0]
x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, err
}
return &rsaSigner{key: key, cert: cert}, nil
rsaKey, ok := cert.PrivateKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("Only RSA private keys are supported for now")
}
return &rsaSigner{key: rsaKey, cert: x509Cert}, nil
}
// SignServerProof signs CHLO and server config for use in the server proof

View File

@@ -7,7 +7,8 @@ import (
"crypto"
"crypto/rsa"
"crypto/x509"
"os"
"github.com/lucas-clemente/quic-go/testdata"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@@ -30,8 +31,7 @@ var _ = Describe("ProofRsa", func() {
})
It("gives valid signatures", func() {
path := os.Getenv("GOPATH") + "/src/github.com/lucas-clemente/quic-go/example/"
kd, err := NewRSASigner(path+"cert.der", path+"key.der")
kd, err := NewRSASigner(testdata.GetTLSConfig())
Expect(err).ToNot(HaveOccurred())
signature, err := kd.SignServerProof([]byte{'C', 'H', 'L', 'O'}, []byte{'S', 'C', 'F', 'G'})
Expect(err).ToNot(HaveOccurred())