Skip to content

Commit

Permalink
Merge pull request #18837 from simo5/RDNOrder
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue (batch tested with PRs 18835, 18857, 18641, 18656, 18837).

Reorder groups in cert Subjects

This is to workaround Bug #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 ourself

Fixes #18715
xref golang/go#24254 https://gitlab.com/gnutls/gnutls/issues/403#note_61687722
  • Loading branch information
openshift-merge-robot authored Mar 7, 2018
2 parents e752be6 + f3e52f9 commit 746e4d3
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 746e4d3

Please sign in to comment.