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

Add RSA support to TLS libraries #3135

Merged
merged 2 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
22 changes: 17 additions & 5 deletions pkg/tls/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,28 @@ func encode(buf *bytes.Buffer, blk *pem.Block) {

// === DECODE ===

// DecodePEMKey parses a PEM-encoded ECDSA private key from the named path.
func DecodePEMKey(txt string) (*ecdsa.PrivateKey, error) {
// DecodePEMKey parses a PEM-encoded private key from the named path.
func DecodePEMKey(txt string) (GenericPrivateKey, error) {
block, _ := pem.Decode([]byte(txt))
if block == nil {
return nil, errors.New("Not PEM-encoded")
}
if block.Type != "EC PRIVATE KEY" {
return nil, fmt.Errorf("Expected 'EC PRIVATE KEY'; found: '%s'", block.Type)
switch block.Type {
case "EC PRIVATE KEY":
k, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return PrivateKeyEC{k}, nil
case "RSA PRIVATE KEY":
k, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return PrivateKeyRSA{k}, nil
default:
return nil, fmt.Errorf("Unsupported block type: '%s'", block.Type)
alpeb marked this conversation as resolved.
Show resolved Hide resolved
}
return x509.ParseECPrivateKey(block.Bytes)
}

// DecodePEMCertificates parses a string containing PEM-encoded certificates.
Expand Down
54 changes: 42 additions & 12 deletions pkg/tls/cred.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
Expand All @@ -12,9 +13,25 @@ import (
)

type (
// PrivateKeyEC wraps an EC private key
PrivateKeyEC struct {
alpeb marked this conversation as resolved.
Show resolved Hide resolved
*ecdsa.PrivateKey
}

// PrivateKeyRSA wraps an RSA private key
PrivateKeyRSA struct {
*rsa.PrivateKey
}

// GenericPrivateKey represents either an EC or an RSA private key
GenericPrivateKey interface {
matchesCertificate(*x509.Certificate) bool
marshal() ([]byte, error)
}

// Cred is a container for a certificate, trust chain, and private key.
Cred struct {
PrivateKey *ecdsa.PrivateKey
PrivateKey GenericPrivateKey
Crt
}

Expand All @@ -28,9 +45,28 @@ type (
}
)

func (k PrivateKeyEC) matchesCertificate(c *x509.Certificate) bool {
pub, ok := c.PublicKey.(*ecdsa.PublicKey)
return ok && pub.X.Cmp(k.X) == 0 && pub.Y.Cmp(k.Y) == 0
}

func (k PrivateKeyEC) marshal() ([]byte, error) {
return x509.MarshalECPrivateKey(k.PrivateKey)
}

func (k PrivateKeyRSA) matchesCertificate(c *x509.Certificate) bool {
pub, ok := c.PublicKey.(*rsa.PublicKey)
return ok && pub.N.Cmp(k.N) == 0 && pub.E == k.E
}

func (k PrivateKeyRSA) marshal() ([]byte, error) {
return x509.MarshalPKCS1PrivateKey(k.PrivateKey), nil
}

// validCredOrPanic creates a Cred, panicking if the key does not match the certificate.
func validCredOrPanic(k *ecdsa.PrivateKey, crt Crt) Cred {
if !certificateMatchesKey(crt.Certificate, k) {
func validCredOrPanic(ecKey *ecdsa.PrivateKey, crt Crt) Cred {
k := PrivateKeyEC{ecKey}
if !k.matchesCertificate(crt.Certificate) {
panic("Cert's public key does not match private key")
}
return Cred{Crt: crt, PrivateKey: k}
Expand Down Expand Up @@ -89,9 +125,9 @@ func (crt *Crt) EncodeCertificatePEM() string {

// EncodePrivateKeyPEM emits the private key as PEM-encoded text.
func (cred *Cred) EncodePrivateKeyPEM() string {
b, err := x509.MarshalECPrivateKey(cred.PrivateKey)
b, err := cred.PrivateKey.marshal()
if err != nil {
panic(fmt.Sprintf("Invalid elliptic curve: %s", err))
panic(fmt.Sprintf("Invalid private key: %s", err))
}

return string(pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: b}))
Expand All @@ -102,12 +138,6 @@ func (cred *Cred) EncodePrivateKeyP8() ([]byte, error) {
return x509.MarshalPKCS8PrivateKey(cred.PrivateKey)
}

// certificateMatchesKey returns whether the key and certificate match.
func certificateMatchesKey(c *x509.Certificate, k *ecdsa.PrivateKey) bool {
pub, ok := c.PublicKey.(*ecdsa.PublicKey)
return ok && pub.X.Cmp(k.X) == 0 && pub.Y.Cmp(k.Y) == 0
}

// SignCrt uses this Cred to sign a new certificate.
//
// This may fail if the Cred contains an end-entity certificate.
Expand Down Expand Up @@ -154,7 +184,7 @@ func ReadPEMCreds(keyPath, crtPath string) (*Cred, error) {
if err != nil {
return nil, err
}
if !certificateMatchesKey(crt.Certificate, key) {
if !key.matchesCertificate(crt.Certificate) {
return nil, errors.New("tls: Public and private key do not match")
}
return &Cred{PrivateKey: key, Crt: *crt}, nil
Expand Down