Skip to content

Commit

Permalink
Reorder Groups in Cert Subjects
Browse files Browse the repository at this point in the history
This is to workaround Bug openshift#18715, which is caused by Golang Crypto's x509
certificate generation ordering Subjects RDN incorrectly *and* GNUTLS'
bug that "fixes" client certs on read with the correct encoding.

To avoid issues until both are fixed we set the correct ordering on our
own...

Signed-off-by: Simo Sorce <[email protected]>
  • Loading branch information
simo5 committed Mar 6, 2018
1 parent 3addbb0 commit f3e52f9
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pkg/cmd/server/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,46 @@ func (ca *CA) MakeClientCertificate(certFile, keyFile string, u user.Info, expir
return GetTLSCertificateConfig(certFile, keyFile)
}

type sortedForDER []string

func (s sortedForDER) Len() int {
return len(s)
}
func (s sortedForDER) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortedForDER) Less(i, j int) bool {
l1 := len(s[i])
l2 := len(s[j])
if l1 == l2 {
return s[i] < s[j]
}
return l1 < l2
}

func userToSubject(u user.Info) pkix.Name {
// Ok we are going to order groups in a peculiar way here to workaround a
// 2 bugs, 1 in golang (https://github.com/golang/go/issues/24254) which
// incorrectly encodes Multivalued RDNs and another in GNUTLS clients
// which are too picky (https://gitlab.com/gnutls/gnutls/issues/403)
// and try to "correct" this issue when reading client certs.
//
// This workaround should be killed once Golang's pkix module is fixed to
// generate a correct DER encoding.
//
// The workaround relies on the fact that the first octect that differs
// between the encoding of two group RDNs will end up being the encoded
// length which is directly related to the group name's length. So we'll
// sort such that shortest names come first.
ugroups := u.GetGroups()
groups := make([]string, len(ugroups))
copy(groups, ugroups)
sort.Sort(sortedForDER(groups))

return pkix.Name{
CommonName: u.GetName(),
SerialNumber: u.GetUID(),
Organization: u.GetGroups(),
Organization: groups,
}
}

Expand Down

0 comments on commit f3e52f9

Please sign in to comment.