Skip to content

Commit

Permalink
Add missing rsa.PrivateKey into validating config
Browse files Browse the repository at this point in the history
validates private keys of certs to be one of RSA, ECDSA or ED25519
  • Loading branch information
misak113 authored and Sean-Der committed Apr 2, 2021
1 parent 7afa896 commit b3e235f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Check out the **[contributing wiki](https://github.com/pion/webrtc/wiki/Contribu
* [Jim Wert](https://github.com/bocajim)
* [Alvaro Viebrantz](https://github.com/alvarowolfx)
* [Kegan Dougal](https://github.com/Kegsay)
* [Michael Zabka](https://github.com/misak113)

### License
MIT License - see [LICENSE](LICENSE) for full text
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"io"
Expand Down Expand Up @@ -182,6 +183,7 @@ func validateConfig(config *Config) error {
switch cert.PrivateKey.(type) {
case ed25519.PrivateKey:
case *ecdsa.PrivateKey:
case *rsa.PrivateKey:
default:
return errInvalidPrivateKey
}
Expand Down
26 changes: 19 additions & 7 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dtls

import (
"crypto/dsa" //nolint
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"testing"

Expand Down Expand Up @@ -68,14 +69,20 @@ func TestValidateConfig(t *testing.T) {
}

// Invalid private key
block, _ := pem.Decode([]byte(rawPrivateKey))
rsaKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
dsaPrivateKey := &dsa.PrivateKey{}
err = dsa.GenerateParameters(&dsaPrivateKey.Parameters, rand.Reader, dsa.L1024N160)
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), parsing RSA private key", err)
t.Fatalf("TestValidateConfig: Config validation error(%v), DSA parameters not generated", err)
return
}
err = dsa.GenerateKey(dsaPrivateKey, rand.Reader)
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), DSA private key not generated", err)
return
}
config = &Config{
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
Certificates: []tls.Certificate{{Certificate: cert.Certificate, PrivateKey: rsaKey}},
Certificates: []tls.Certificate{{Certificate: cert.Certificate, PrivateKey: dsaPrivateKey}},
}
if err = validateConfig(config); !errors.Is(err, errInvalidPrivateKey) {
t.Fatalf("TestValidateConfig: Client error exp(%v) failed(%v)", errInvalidPrivateKey, err)
Expand All @@ -97,9 +104,14 @@ func TestValidateConfig(t *testing.T) {
}

// Valid config
rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), RSA private key not generated", err)
return
}
config = &Config{
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
Certificates: []tls.Certificate{cert},
Certificates: []tls.Certificate{cert, {Certificate: cert.Certificate, PrivateKey: rsaPrivateKey}},
}
if err = validateConfig(config); err != nil {
t.Fatalf("TestValidateConfig: Client error exp(%v) failed(%v)", nil, err)
Expand Down

0 comments on commit b3e235f

Please sign in to comment.