Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read certificate when running in Linux Docker container - works on Windows

I'm trying to read information from an SSL certificate (in this case I've saved a copy of StackOverflow's via Chrome) but for some reason the moment I build it and run it from within a Linux Docker container, it fails to output anything.

On Windows the output is what I expect:

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

But the output from the same application on the Docker container is as follows:

S
E

Here is my code:

const string ALT_NAME_KEY = "Subject Alternative Name";
string certificateData = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tDQpNSUlJUERDQ0J5U2dBd0lCQWdJUUIyWEdUblRsa2RhQU9jb3FoSFZqOERBTkJna3Foa2lHOXcwQkFRc0ZBREJ3DQpNUXN3Q1FZRFZRUUdFd0pWVXpFVk1CTUdBMVVFQ2hNTVJHbG5hVU5sY25RZ1NXNWpNUmt3RndZRFZRUUxFeEIzDQpkM2N1WkdsbmFXTmxjblF1WTI5dE1TOHdMUVlEVlFRREV5WkVhV2RwUTJWeWRDQlRTRUV5SUVocFoyZ2dRWE56DQpkWEpoYm1ObElGTmxjblpsY2lCRFFUQWVGdzB4T0RFd01EVXdNREF3TURCYUZ3MHhPVEE0TVRReE1qQXdNREJhDQpNR294Q3pBSkJnTlZCQVlUQWxWVE1Rc3dDUVlEVlFRSUV3Sk9XVEVSTUE4R0ExVUVCeE1JVG1WM0lGbHZjbXN4DQpIVEFiQmdOVkJBb1RGRk4wWVdOcklFVjRZMmhoYm1kbExDQkpibU11TVJ3d0dnWURWUVFEREJNcUxuTjBZV05yDQpaWGhqYUdGdVoyVXVZMj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Qoydjc4RmN2UjhqMEVMZmd5ZWh0V3BqRjN2ektTcS9yWTJzTVhUWXpTUHFNZWtzc2VITmhTTVlVNld3OWg5cHlNDQphVm53OXZhaHFmN25LSEhjQzJWUlRVZ2tRZm45eURtbUJPbzBuUThYZ2ZwZDY1L1BheFZmQm51S2ZFa1hCZnBNDQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tDQo=";
var secretData = Convert.FromBase64String(certificateData);
string[] urls = null;
using (var cert = new X509Certificate2(secretData))
{
    urls = cert.Extensions
        .OfType<X509Extension>()
        .Where(e => e.Oid.FriendlyName == ALT_NAME_KEY)
        .Select(e => new AsnEncodedData(e.Oid, e.RawData))
        .Append(new AsnEncodedData(cert.SubjectName.Oid, cert.SubjectName.RawData))
        .Select(d => d.Format(true))
        .SelectMany(d => Regex.Matches(d, "\\b(?:CN|DNS Name)=(?<domain>.*)(?:\\b|$)").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");

Is this a bug in .NET Core (2.2), or do I need to read the certificate differently under Linux?

like image 770
DiplomacyNotWar Avatar asked Jan 25 '19 03:01

DiplomacyNotWar


People also ask

Can Linux Docker images run on Windows?

You can run both Linux and Windows programs and executables in Docker containers. The Docker platform runs natively on Linux (on x86-64, ARM and many other CPU architectures) and on Windows (x86-64). Docker Inc. builds products that let you build and run containers on Linux, Windows and macOS.

Where are Docker certificates Linux?

A custom certificate is configured by creating a directory under /etc/docker/certs.

Does Docker use OpenSSL?

Generating SSL certificates from Docker containersThat image conveniently comes with OpenSSL built-in. (If your image doesn't contain OpenSSL, you could always add it to the image yourself or, more easily, install it in the container once it starts).


1 Answers

Thanks to Nkosi pointing me to this answer, I've been able to use PeNet to extract the certificate URLs:

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();

Where FlattenNodes looks like

private IEnumerable<Asn1Node> FlattenNodes(Asn1Node parentNode)
{
    yield return parentNode;
    foreach (var childNode in parentNode.Nodes)
    {
        foreach (var grandChildNode in FlattenNodes(childNode))
        {
            yield return grandChildNode;
        }
    }
}

I know that flattening all of the nodes probably isn't the most efficient way to do this by far, but in my case performance isn't really important. It's simply checking a handful of certificates in a Kubernetes cluster as a daily cron job.

I would be interested to know if it's possible without a third party library, especially since it is on Windows.

like image 133
DiplomacyNotWar Avatar answered Oct 24 '22 05:10

DiplomacyNotWar