Il semble donc que le problème principal soit double :
- Le "nom convivial" pour "Subject Alternative Name" diffère selon le système d'exploitation.
- La chaîne formatée diffère selon le système d'exploitation.
Windows :
Friendly name: "Subject Alternative Name"
Formatted data:
DNS Name=*.stackexchange.com
DNS Name=stackexchange.com
DNS Name=stackoverflow.com
DNS Name=*.stackoverflow.com
DNS Name=stackauth.com
DNS Name=sstatic.net
DNS Name=*.sstatic.net
DNS Name=serverfault.com
DNS Name=*.serverfault.com
DNS Name=superuser.com
DNS Name=*.superuser.com
DNS Name=stackapps.com
DNS Name=openid.stackauth.com
DNS Name=*.meta.stackexchange.com
DNS Name=meta.stackexchange.com
DNS Name=mathoverflow.net
DNS Name=*.mathoverflow.net
DNS Name=askubuntu.com
DNS Name=*.askubuntu.com
DNS Name=stacksnippets.net
DNS Name=*.blogoverflow.com
DNS Name=blogoverflow.com
DNS Name=*.meta.stackoverflow.com
DNS Name=*.stackoverflow.email
DNS Name=stackoverflow.email
DNS Name=stackoverflow.blog
Linux dans Docker (et Mac, apparemment) :
Friendly name: "X509v3 Subject Alternative Name"
Formatted data:
DNS:*.stackexchange.com, DNS:stackexchange.com, DNS:stackoverflow.com, DNS:*.stackoverflow.com, DNS:stackauth.com, DNS:sstatic.net, DNS:*.sstatic.net, DNS:serverfault.com, DNS:*.serverfault.com, DNS:superuser.com, DNS:*.superuser.com, DNS:stackapps.com, DNS:openid.stackauth.com, DNS:*.meta.stackexchange.com, DNS:meta.stackexchange.com, DNS:mathoverflow.net, DNS:*.mathoverflow.net, DNS:askubuntu.com, DNS:*.askubuntu.com, DNS:stacksnippets.net, DNS:*.blogoverflow.com, DNS:blogoverflow.com, DNS:*.meta.stackoverflow.com, DNS:*.stackoverflow.email, DNS:stackoverflow.email, DNS:stackoverflow.blog
Pour résoudre le problème n° 1, j'ai commencé à utiliser la valeur plutôt que le nom convivial :2.5.29.17
Pour résoudre le problème #2, j'ai dû mettre à jour légèrement mon expression régulière. Bien que cela fonctionnait bien pour les données multilignes (Windows), cela ne fonctionnait pas bien pour les données séparées par des virgules Linux/Mac.
La nouvelle expression est maintenant :((DNS:|DNS Name=|CN=)(?<domain>.*?),?(?=\s|$))+
En mettant tout cela ensemble, nous obtenons :
const string ALT_NAME_VALUE = "2.5.29.17";
string certificateData = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tDQpNSUlJUERDQ0J5U2dBd0lCQWdJUUIyWEdUblRsa2RhQU9jb3FoSFZqOERBTkJna3Foa2lHOXcwQkFRc0ZBREJ3DQpNUXN3Q1FZRFZRUUdFd0pWVXpFVk1CTUdBMVVFQ2hNTVJHbG5hVU5sY25RZ1NXNWpNUmt3RndZRFZRUUxFeEIzDQpkM2N1WkdsbmFXTmxjblF1WTI5dE1TOHdMUVlEVlFRREV5WkVhV2RwUTJWeWRDQlRTRUV5SUVocFoyZ2dRWE56DQpkWEpoYm1ObElGTmxjblpsY2lCRFFUQWVGdzB4T0RFd01EVXdNREF3TURCYUZ3MHhPVEE0TVRReE1qQXdNREJhDQpNR294Q3pBSkJnTlZCQVlUQWxWVE1Rc3dDUVlEVlFRSUV3Sk9XVEVSTUE4R0ExVUVCeE1JVG1WM0lGbHZjbXN4DQpIVEFiQmdOVkJBb1RGRk4wWVdOcklFVjRZMmhoYm1kbExDQkpibU11TVJ3d0dnWURWUVFEREJNcUxuTjBZV05yDQpaWGhqYUdGdVoyVXVZMjl0TUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUE5S2s2DQpOWFVQMW9jWHQ4OW1UMWNJeGFkQmh6Q0wwWVRxUDAxL0RTb3RVSFJ6VjcwcU9DVDdBZE1UMEsxSmk2ckZ5YXB6DQpSaXFVSWhBa2hFc2VYUnAwTU5yMjFmU1V3NFZvQ2IrSW1PNmduSWx6b2xraHJwSzZJeTM0S3lVM3p5dDhYWUQrDQptWTNpOEdqUFpPeXNSSk5MeTNvdVFCbXp1T21VLzJGb21ubWlFR0YwMm1WZ2IzZXY4UHJj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W84TlpnRStSdmZ1T04zelE3SURkd1FBQUFCDQpaa0lKK1NNQUFBUURBRWd3UmdJaEFQQ2FkeHY0N2NCNFFPT3ZOOXMvUjIzRWVwRWJTSTQvTXVBZGY1dktlVTc5DQpBaUVBMmdaM084bnp1VVZhblJXOWZnNm1nZnNMMDhObi9aR203M08vRjNJR1gyVUFkd0NIZGIvbldYejRqRU9aDQpYNzN6YnY5V2pVZFdOdjlLdFdEQnRPci9YcUNERHdBQUFXWkNDZm9HQUFBRUF3QklNRVlDSVFEUWpmbVZCcSsvDQp5MmdCSy9lRTl4Nmp6OWhUWjV0SWZoa1N0Uzg1Zk1BeGVnSWhBTUF1Tmt5dU80dDA2RWlFZ01XaWFsSlp1QW5rDQpRdzI5R2NlSUJHOHIxQXAzTUEwR0NTcUdTSWIzRFFFQkN3VUFBNElCQVFBQWs4NzMvKzJRc3dLZkpTUW4raVplDQpaYzh1aUdnOTlwbWQwMDhFMmNtR0VycU56UGNsSzlJTmJQand4bDl6SWdUY1hwRi9VdEJWVlMxWjdYbzgzcWZzDQpHTVBkTXpZdDNGK2hRcFFZTGhsR0YrNUpmMng2WmIxempUL2FNM0dNZEdpKzZPUFYrWUhsL3dnVWU0NU42a1J1DQpEWm5WTDE2NytXM2wybkQrbVNoTy83eHF3SGladXowR0h5QkhScDVpNDNibEgwdmc2N3NKOGd1TjgxcGFwdXBZDQoydjc4RmN2UjhqMEVMZmd5ZWh0V3BqRjN2ektTcS9yWTJzTVhUWXpTUHFNZWtzc2VITmhTTVlVNld3OWg5cHlNDQphVm53OXZhaHFmN25LSEhjQzJWUlRVZ2tRZm45eURtbUJPbzBuUThYZ2ZwZDY1L1BheFZmQm51S2ZFa1hCZnBNDQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tDQo=";
var secretData = Convert.FromBase64String(certificateData);
string[] urls = null;
var tmp = new X509Certificate2Collection();
tmp.Import(secretData);
var cert = tmp.OfType<X509Certificate2>().FirstOrDefault();
urls = cert.Extensions
.OfType<X509Extension>()
.Where(e => e.Oid.Value == ALT_NAME_VALUE)
// I removed the part appending the original subject because I realise the same
// information is available in the SAN object, so it was just duplicating data
// this also allowed me to combine Format with creating the AsnEncodedData object
.Select(e => new AsnEncodedData(e.Oid, e.RawData).Format(true))
.SelectMany(d => Regex.Matches(d, "((DNS:|DNS Name=|CN=)(?<domain>.*?),?(?=\\s|$))+").OfType<Match>().Where(m => m.Success).Select(m => m.Groups["domain"].Value))
.Distinct()
.ToArray();
Console.WriteLine("S");
foreach (var r in urls)
{
Console.WriteLine(r);
}
Console.WriteLine("E");
Et maintenant, la sortie sur Windows et Linux est la même :
S
*.stackexchange.com
stackexchange.com
stackoverflow.com
*.stackoverflow.com
stackauth.com
sstatic.net
*.sstatic.net
serverfault.com
*.serverfault.com
superuser.com
*.superuser.com
stackapps.com
openid.stackauth.com
*.meta.stackexchange.com
meta.stackexchange.com
mathoverflow.net
*.mathoverflow.net
askubuntu.com
*.askubuntu.com
stacksnippets.net
*.blogoverflow.com
blogoverflow.com
*.meta.stackoverflow.com
*.stackoverflow.email
stackoverflow.email
stackoverflow.blog
E
Grâce à Nkosi qui m'a indiqué cette réponse, j'ai pu utiliser PeNet pour extraire les URL des certificats :
var node = Asn1Node.ReadNode(cert.RawData);
urls =
FlattenNodes(node)
.Where(n => n.Nodes.Count > 0 && n.Nodes[0] is Asn1ObjectIdentifier && ((Asn1ObjectIdentifier)n.Nodes[0]).FriendlyName == "subjectAltName")
.SelectMany(n => n.Nodes).OfType<Asn1OctetString>()
.SelectMany(o => Asn1Sequence.ReadNode(o.Data).Nodes).OfType<Asn1CustomNode>()
.Select(o => System.Text.Encoding.UTF8.GetString(o.Data))
.ToArray();
Où FlattenNodes
ressemble
private IEnumerable<Asn1Node> FlattenNodes(Asn1Node parentNode)
{
yield return parentNode;
foreach (var childNode in parentNode.Nodes)
{
foreach (var grandChildNode in FlattenNodes(childNode))
{
yield return grandChildNode;
}
}
}
Je sais que l'aplatissement de tous les nœuds n'est probablement pas de loin le moyen le plus efficace de le faire, mais dans mon cas, les performances ne sont pas vraiment importantes. Il s'agit simplement de vérifier une poignée de certificats dans un cluster Kubernetes en tant que tâche cron quotidienne.
Je serais intéressé de savoir si c'est possible sans une bibliothèque tierce, d'autant plus que c'est sous Windows.