Skip to content

Commit 746e4d3

Browse files
Merge pull request #18837 from simo5/RDNOrder
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
2 parents e752be6 + f3e52f9 commit 746e4d3

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

pkg/cmd/server/crypto/crypto.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,46 @@ func (ca *CA) MakeClientCertificate(certFile, keyFile string, u user.Info, expir
588588
return GetTLSCertificateConfig(certFile, keyFile)
589589
}
590590

591+
type sortedForDER []string
592+
593+
func (s sortedForDER) Len() int {
594+
return len(s)
595+
}
596+
func (s sortedForDER) Swap(i, j int) {
597+
s[i], s[j] = s[j], s[i]
598+
}
599+
func (s sortedForDER) Less(i, j int) bool {
600+
l1 := len(s[i])
601+
l2 := len(s[j])
602+
if l1 == l2 {
603+
return s[i] < s[j]
604+
}
605+
return l1 < l2
606+
}
607+
591608
func userToSubject(u user.Info) pkix.Name {
609+
// Ok we are going to order groups in a peculiar way here to workaround a
610+
// 2 bugs, 1 in golang (https://github.com/golang/go/issues/24254) which
611+
// incorrectly encodes Multivalued RDNs and another in GNUTLS clients
612+
// which are too picky (https://gitlab.com/gnutls/gnutls/issues/403)
613+
// and try to "correct" this issue when reading client certs.
614+
//
615+
// This workaround should be killed once Golang's pkix module is fixed to
616+
// generate a correct DER encoding.
617+
//
618+
// The workaround relies on the fact that the first octect that differs
619+
// between the encoding of two group RDNs will end up being the encoded
620+
// length which is directly related to the group name's length. So we'll
621+
// sort such that shortest names come first.
622+
ugroups := u.GetGroups()
623+
groups := make([]string, len(ugroups))
624+
copy(groups, ugroups)
625+
sort.Sort(sortedForDER(groups))
626+
592627
return pkix.Name{
593628
CommonName: u.GetName(),
594629
SerialNumber: u.GetUID(),
595-
Organization: u.GetGroups(),
630+
Organization: groups,
596631
}
597632
}
598633

0 commit comments

Comments
 (0)