Skip to content
This repository has been archived by the owner on Feb 7, 2020. It is now read-only.

Commit

Permalink
(#34) Validate email addresses in SANs
Browse files Browse the repository at this point in the history
SAN fields can contain email addresses, and the default X509 Verify
doesn't include these.  Loop through until we find a match, return an
error otherwise.
  • Loading branch information
vjanelle committed Apr 20, 2019
1 parent b259ec5 commit 37f230a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
15 changes: 14 additions & 1 deletion filesec/file_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ func (s *FileSecurity) VerifyCertificate(certpem []byte, name string) error {
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}

if name != "" {
// If there is an email address in the name passed, we should not search by DNSName
// in the CN or SAN
if name != "" && !strings.HasPrefix(name, "email:") {
opts.DNSName = name
}

Expand All @@ -452,6 +454,17 @@ func (s *FileSecurity) VerifyCertificate(certpem []byte, name string) error {
return err
}

if len(cert.EmailAddresses) > 0 && strings.HasPrefix(name, "email:") {
s.log.Debug("Email addresses found in certificate, attempting verification")
for _, email := range cert.EmailAddresses {
if strings.TrimPrefix(name, "email:") == email {
return nil
}
}

return fmt.Errorf("email address not found in SAN: %s, %v", name, cert.EmailAddresses)
}

return nil
}

Expand Down
34 changes: 34 additions & 0 deletions filesec/file_security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,40 @@ var _ = Describe("FileSSL", func() {
err = prov.VerifyCertificate(pem, "rip.mcollective")
Expect(err).ToNot(HaveOccurred())
})

It("Should work with email addresses", func() {
c, err := config.NewDefaultConfig()
Expect(err).ToNot(HaveOccurred())

c.Choria.FileSecurityCA = filepath.Join("..", "testdata", "intermediate", "certs", "ca_chain_ca.pem")
c.Choria.FileSecurityCache = filepath.Join("..", "testdata", "intermediate", "certs")

prov, err := New(WithChoriaConfig(c), WithLog(l.WithFields(logrus.Fields{})))
Expect(err).ToNot(HaveOccurred())

pem, err = ioutil.ReadFile(filepath.Join("..", "testdata", "intermediate", "certs", "email-chain-rip.mcollective.pem"))
Expect(err).ToNot(HaveOccurred())

err = prov.VerifyCertificate(pem, "email:test@choria-io.com")
Expect(err).ToNot(HaveOccurred())
})

It("Should not work with wrong addresses", func() {
c, err := config.NewDefaultConfig()
Expect(err).ToNot(HaveOccurred())

c.Choria.FileSecurityCA = filepath.Join("..", "testdata", "intermediate", "certs", "ca_chain_ca.pem")
c.Choria.FileSecurityCache = filepath.Join("..", "testdata", "intermediate", "certs")

prov, err := New(WithChoriaConfig(c), WithLog(l.WithFields(logrus.Fields{})))
Expect(err).ToNot(HaveOccurred())

pem, err = ioutil.ReadFile(filepath.Join("..", "testdata", "intermediate", "certs", "email-chain-rip.mcollective.pem"))
Expect(err).ToNot(HaveOccurred())

err = prov.VerifyCertificate(pem, "email:bad@choria-io.com")
Expect(err).To(HaveOccurred())
})
})

Describe("PublicCertPem", func() {
Expand Down
7 changes: 7 additions & 0 deletions testdata/intermediate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ ca_chain:
cat rip.mcollective.pem > certs/ca_chain_rip.mcollective.pem
openssl verify -CAfile certs/ca_chain_ca.pem certs/ca_chain_rip.mcollective.pem

email:
cfssl genkey email.json | cfssljson -bare email.rip.mcollective
cfssl gencsr -key email.rip.mcollective-key.pem email.json | cfssljson -bare email.rip.mcollective
cfssl sign -ca intermediate.pem -ca-key intermediate-key.pem email.rip.mcollective.csr subject.json | cfssljson -bare email.rip.mcollective && openssl x509 -in email.rip.mcollective.pem -noout -text
cat email.rip.mcollective.pem intermediate.pem > email-chain-rip.mcollective.pem
openssl verify -CAfile ca.pem -untrusted email-chain-rip.mcollective.pem email-chain-rip.mcollective.pem
cp email-chain-rip.mcollective.pem certs/email-chain-rip.mcollective.pem

deploy:
cp ca.pem certs/ca.pem
Expand Down
34 changes: 34 additions & 0 deletions testdata/intermediate/certs/email-chain-rip.mcollective.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-----BEGIN CERTIFICATE-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HZ4tprfJkOSkkJjf+ei+FlCi1EmOR
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIICZDCCAgmgAwIBAgIUMHE90peOTHN6Iv2S2R2astND6lswCgYIKoZIzj0EAwIw
eTELMAkGA1UEBhMCWFgxETAPBgNVBAgTCExvY2FsaXR5MQ0wCwYDVQQHEwRDaXR5
MQ8wDQYDVQQKEwZDaG9yaWExJTAjBgNVBAsTHFVuaXQgdGVzdGluZyBJbnRlcm1l
ZGlhdGUgQ0ExEDAOBgNVBAMTB1Jvb3QgQ0EwHhcNMTgxMTEzMDEyMzAwWhcNNDgx
MTA1MDEyMzAwWjCBgTELMAkGA1UEBhMCWFgxETAPBgNVBAgTCExvY2FsaXR5MQ0w
CwYDVQQHEwRDaXR5MQ8wDQYDVQQKEwZDaG9yaWExJTAjBgNVBAsTHFVuaXQgdGVz
dGluZyBJbnRlcm1lZGlhdGUgQ0ExGDAWBgNVBAMTD0ludGVybWVkaWF0ZSBDQTBZ
MBMGByqGSM49AgEGCCqGSM49AwEHA0IABNGtHy1coQANdtEj/OK8JjgVxQ+owXlq
X3PWtohIhx1dlD4MS78sPoEblHcU5NAfSPTN23gPw2kalFjV5NJH3I+jZjBkMA4G
A1UdDwEB/wQEAwIBpjASBgNVHRMBAf8ECDAGAQH/AgEAMB0GA1UdDgQWBBSlQrdE
6JCCk8azRsWXnRuk2ctF+jAfBgNVHSMEGDAWgBQ2M6o4bz7r8MgG9Q0/7rN8Ogoi
ETAKBggqhkjOPQQDAgNJADBGAiEAueRTGMy56l9024iI0tE+huS5E0wEu1ZyQfpI
AnqVQ70CIQCqVCe23uL3Po9THrXrmpVF7n+CJLQnKdpM3uxxsPWAIg==
-----END CERTIFICATE-----
11 changes: 11 additions & 0 deletions testdata/intermediate/email.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hosts": [
"rip.mcollective",
"test@choria-io.com"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [ ]
}

0 comments on commit 37f230a

Please sign in to comment.