Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a certificate is self-signed?

I'm using C#.NET and need to install a bunch of certificates into the Windows certificate store.

I need to check which of those certificates are root certificates (i.e. self-signed), so I can install them into the "Trusted root certificates" store.

I'm using the standard X509Certificate2 class. My current idea is to check whether the Issuer and Subject are the same.

I've noticed that X509Certificate2 has Issuer - IssuerName and Subject - SubjectName.

Is it better to compare Issuer to Subject, or IssuerName to SubjectName? Or doesn't it really matter?

Also, is this a reliable method or would I be better off using another approach?

like image 693
MarioDS Avatar asked Dec 09 '15 08:12

MarioDS


People also ask

Where can I find self-signed certificate in Windows?

msc in the windows search bar and choosing "Run as administrator." Expand both the "Personal" and "Trusted Root Certification" directories. In the Personal Certificates folder, you will find both the CA and the Self-Signed Certificate that we created in the previous steps.

Which digital certificates are self-signed?

Self-signed certificates include SSL/TLS certificates, code signing certificates, and S/MIME certificates. Self-Signed certificates are created, issued, and signed by the organization responsible for the website or the signed software.

What makes a certificate self-signed?

A self-signed certificate is an SSL certificate not signed by a publicly trusted certificate authority (CA) but by one's own private key. The certificate is not validated by a third party and is generally used in low-risk internal networks or in the software development phase.


1 Answers

See this post: java - Find if a certificate is self signed or CA signed

While it's not C#, the comment from the solution notes

If the subject and issuer are the same, it is self-signed

means you're correct about the way you're trying to validate it.

IssuerName and SubjectName return a DistinguishedName which contains RawData (a byte[] containing the raw information for the issuer/subject). You'd be best off comparing this field, though I believe comparing Subject and Issuer is just as valid.

So, you could write something like this:

public static bool IsSelfSigned(X509Certificate2 cert)
{
    return cert.SubjectName.RawData.SequenceEqual(cert.IssuerName.RawData);
}
like image 85
Rob Avatar answered Oct 31 '22 14:10

Rob