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

enabling support for Subject email (E) overriding #1393

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions api/sign/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cloudflare/cfssl/api"
"github.com/cloudflare/cfssl/auth"
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/csr"
"github.com/cloudflare/cfssl/signer"
)

Expand Down Expand Up @@ -317,6 +318,17 @@ var signTests = []signTest{
ExpectedSuccess: true,
ExpectedErrorCode: 0,
},
{
Hosts: []string{},
Subject: &signer.Subject{
CN: "example.com",
Names: []csr.Name{csr.Name{E: "[email protected]"}},
},
CSRFile: testCSRFile,
ExpectedHTTPStatus: http.StatusOK,
ExpectedSuccess: true,
ExpectedErrorCode: 0,
},
{
Hosts: nil,
CSRFile: testCSRFile,
Expand Down
62 changes: 60 additions & 2 deletions csr/csr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,49 @@ const (
curveP521 = 521
)

var (
emailOidInts = []int{1, 2, 840, 113549, 1, 9, 1}
emailOid = asn1.ObjectIdentifier(emailOidInts)
uidOidInts = []int{0, 9, 2342, 19200300, 100, 1, 1}
uidOid = asn1.ObjectIdentifier(uidOidInts)
dcOidInts = []int{0, 9, 2342, 19200300, 100, 1, 25}
dcOid = asn1.ObjectIdentifier(dcOidInts)
)

// Returns email from the attributes if it exists
func GetEmail(extraNames []pkix.AttributeTypeAndValue) (ok bool, email string) {
for _, en := range extraNames {
if emailOid.Equal(en.Type) {
sval, svalOk := en.Value.(string)
if svalOk {
return true, sval
}
}
}
return false, ""
}

// Adds the email to the attributes
func AddEmail(extraNames []pkix.AttributeTypeAndValue, email string) []pkix.AttributeTypeAndValue {
extraNames = append(extraNames,
pkix.AttributeTypeAndValue{Type: emailOid, Value: email})
return extraNames
}

// Adds the UID to the attributes
func AddUid(extraNames []pkix.AttributeTypeAndValue, uid string) []pkix.AttributeTypeAndValue {
extraNames = append(extraNames,
pkix.AttributeTypeAndValue{Type: uidOid, Value: uid})
return extraNames
}

// Adds the Domain Component to the attributes
func AddDc(extraNames []pkix.AttributeTypeAndValue, domainComponent string) []pkix.AttributeTypeAndValue {
extraNames = append(extraNames,
pkix.AttributeTypeAndValue{Type: dcOid, Value: domainComponent})
return extraNames
}

// A Name contains the SubjectInfo fields.
type Name struct {
C string `json:"C,omitempty" yaml:"C,omitempty"` // Country
Expand Down Expand Up @@ -209,7 +252,7 @@ func (cr *CertificateRequest) Name() (pkix.Name, error) {
name.ExtraNames = append(name.ExtraNames, pkix.AttributeTypeAndValue{Type: oid, Value: v})
}
if n.E != "" {
name.ExtraNames = append(name.ExtraNames, pkix.AttributeTypeAndValue{Type: asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}, Value: n.E})
name.ExtraNames = append(name.ExtraNames, pkix.AttributeTypeAndValue{Type: emailOid, Value: n.E})
}
}
name.SerialNumber = cr.SerialNumber
Expand Down Expand Up @@ -339,7 +382,18 @@ func getNames(sub pkix.Name) []Name {
nl := len(sub.Locality)
np := len(sub.Province)

n := max(nc, norg, nou, nl, np)
ne := 0
emailOk, email := GetEmail(sub.ExtraNames)
if emailOk {
ne = 1
} else {
emailOk, email = GetEmail(sub.Names)
if emailOk {
ne = 1
}
}

n := max(nc, norg, nou, nl, np, ne)

names := make([]Name, n)
for i := range names {
Expand All @@ -358,6 +412,10 @@ func getNames(sub pkix.Name) []Name {
if i < np {
names[i].ST = sub.Province[i]
}

if ne == 1 {
names[i].E = email
}
}
return names
}
Expand Down
11 changes: 11 additions & 0 deletions signer/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/cloudflare/cfssl/certdb"
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/csr"
cferr "github.com/cloudflare/cfssl/errors"
"github.com/cloudflare/cfssl/helpers"
"github.com/cloudflare/cfssl/info"
Expand Down Expand Up @@ -254,6 +255,16 @@ func PopulateSubjectFromCSR(s *signer.Subject, req pkix.Name) pkix.Name {
if name.SerialNumber == "" {
name.SerialNumber = req.SerialNumber
}
// Handle Email by the OID
emailOk, email := csr.GetEmail(name.ExtraNames)
if !emailOk || email == "" {
emailOk, email = csr.GetEmail(req.ExtraNames)
if emailOk {
name.Names = csr.AddEmail(req.Names, email)
name.ExtraNames = csr.AddEmail(req.ExtraNames, email)
}
}

return name
}

Expand Down
6 changes: 6 additions & 0 deletions signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func (s *Subject) Name() pkix.Name {
appendIf(n.L, &name.Locality)
appendIf(n.O, &name.Organization)
appendIf(n.OU, &name.OrganizationalUnit)

// We have to handle Email by its OID
if n.E != "" {
name.Names = csr.AddEmail(name.Names, n.E)
name.ExtraNames = csr.AddEmail(name.ExtraNames, n.E)
}
}
name.SerialNumber = s.SerialNumber
return name
Expand Down