Skip to content

Commit

Permalink
Merge pull request #111 from rekby/devel
Browse files Browse the repository at this point in the history
Add ecdsa certificates support
  • Loading branch information
rekby authored Feb 8, 2020
2 parents 424b70b + 35c72c3 commit 9f4eb9b
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 365 deletions.
26 changes: 0 additions & 26 deletions internal/cert_manager/cert-desctiption.go

This file was deleted.

45 changes: 45 additions & 0 deletions internal/cert_manager/cert_description_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cert_manager

import (
"testing"

"go.uber.org/zap"

"github.com/maxatome/go-testdeep"
)

func TestCertDescription_CertStoreName(t *testing.T) {
td := testdeep.NewT(t)
td.Cmp(CertDescription{MainDomain: "asd.ru", KeyType: KeyRSA}.CertStoreName(), "asd.ru.rsa.cer")
}

func TestCertDescription_DomainNames(t *testing.T) {
td := testdeep.NewT(t)
td.Cmp(CertDescription{MainDomain: "asd.ru", KeyType: KeyRSA}.DomainNames(), []DomainName{"asd.ru", "www.asd.ru"})
}

func TestCertDescription_KeyStoreName(t *testing.T) {
td := testdeep.NewT(t)
td.Cmp(CertDescription{MainDomain: "asd.ru", KeyType: KeyRSA}.KeyStoreName(), "asd.ru.rsa.key")
}

func TestCertDescription_LockName(t *testing.T) {
td := testdeep.NewT(t)
td.Cmp(CertDescription{MainDomain: "asd.ru", KeyType: KeyRSA}.LockName(), "asd.ru.lock")
}

func TestCertDescription_MetaStoreName(t *testing.T) {
td := testdeep.NewT(t)
td.Cmp(CertDescription{MainDomain: "asd.ru", KeyType: KeyRSA}.MetaStoreName(), "asd.ru.rsa.json")
}

func TestCertDescription_String(t *testing.T) {
td := testdeep.NewT(t)
td.Cmp(CertDescription{MainDomain: "asd.ru", KeyType: KeyRSA}.String(), "asd.ru.rsa")
}

func TestCertDescription_ZapField(t *testing.T) {
td := testdeep.NewT(t)
cd := CertDescription{MainDomain: "asd.ru", KeyType: KeyRSA}
td.Cmp(cd.ZapField(), zap.Stringer("cert_name", cd))
}
48 changes: 48 additions & 0 deletions internal/cert_manager/cert_desctiption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//nolint:golint
package cert_manager

import (
"strings"

"go.uber.org/zap"
)

type CertDescription struct {
MainDomain string
KeyType KeyType
}

func (n CertDescription) CertStoreName() string {
return n.MainDomain + "." + n.KeyType.String() + ".cer"
}

func (n CertDescription) DomainNames() []DomainName {
return []DomainName{DomainName(n.MainDomain), DomainName("www." + n.MainDomain)}
}

func (n CertDescription) KeyStoreName() string {
return n.MainDomain + "." + n.KeyType.String() + ".key"
}

func (n CertDescription) LockName() string {
return n.MainDomain + ".lock"
}

func (n CertDescription) MetaStoreName() string {
return n.MainDomain + "." + n.KeyType.String() + ".json"
}

func (n CertDescription) String() string {
return n.MainDomain + "." + n.KeyType.String()
}

func (n CertDescription) ZapField() zap.Field {
return zap.Stringer("cert_name", n)
}

func CertDescriptionFromDomain(domain DomainName, keyType KeyType) CertDescription {
return CertDescription{
MainDomain: strings.TrimPrefix(domain.String(), "www."),
KeyType: keyType,
}
}
14 changes: 12 additions & 2 deletions internal/cert_manager/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cert_manager

import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
Expand Down Expand Up @@ -99,10 +100,19 @@ func validCertTLS(cert *tls.Certificate, domains []DomainName, useAsIs bool, now
case *rsa.PublicKey:
prv, ok := cert.PrivateKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("private key type does not match public key type")
return nil, errors.New("rsa private key type does not match public key type")
}
if pub.N.Cmp(prv.N) != 0 {
return nil, errors.New("private key does not match public key")
return nil, errors.New("rsa private key does not match public key")
}
case *ecdsa.PublicKey:
prv, ok := cert.PrivateKey.(*ecdsa.PrivateKey)
if !ok {
return nil, errors.New("ecdsa private key type does not match public key type")
}
pubFromPriv := prv.Public().(*ecdsa.PublicKey)
if pub.X.Cmp(pubFromPriv.X) != 0 || pub.Y.Cmp(pubFromPriv.Y) != 0 {
return nil, errors.New("ecdsa private key does not match public key")
}
default:
return nil, errors.New("unknown public key algorithm")
Expand Down
Loading

0 comments on commit 9f4eb9b

Please sign in to comment.