序列号是X509证书的唯一密钥吗?

发布于 2025-01-01 16:34:28 字数 1121 浏览 1 评论 0原文

证书序列号是X509证书的唯一密钥吗? 用户选择证书,程序将序列号存储在首选项中。 以下代码会返回所选证书吗?

public static X509Certificate2 GetCertificateBySerialNumber(string serialNumber)
{
    X509Certificate2 selectedCertificate = null;
    X509Store store = null;
    try
    {
        // get certificate from the store "My", "CurrentUser"
        store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        X509Certificate2Collection allCertificates = (X509Certificate2Collection)store.Certificates;
        X509Certificate2Collection foundCertificates = (X509Certificate2Collection)allCertificates.Find(X509FindType.FindBySerialNumber, serialNumber, false);

        // select the first certificate in collection
        foreach (X509Certificate2 certificate in foundCertificates)
        {
            selectedCertificate = certificate;
            break;
        }
    }
    finally
    {
        if (store != null)
        {
            store.Close();
        }
    }

    return selectedCertificate;
}

更新:按照 jglouie 的建议,我最终使用了证书指纹。

Is certificate serial number a unique key for X509 certificate?
User selects a certificate, and program stores serial number in preferences.
Will the following code return the selected certificate?

public static X509Certificate2 GetCertificateBySerialNumber(string serialNumber)
{
    X509Certificate2 selectedCertificate = null;
    X509Store store = null;
    try
    {
        // get certificate from the store "My", "CurrentUser"
        store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        X509Certificate2Collection allCertificates = (X509Certificate2Collection)store.Certificates;
        X509Certificate2Collection foundCertificates = (X509Certificate2Collection)allCertificates.Find(X509FindType.FindBySerialNumber, serialNumber, false);

        // select the first certificate in collection
        foreach (X509Certificate2 certificate in foundCertificates)
        {
            selectedCertificate = certificate;
            break;
        }
    }
    finally
    {
        if (store != null)
        {
            store.Close();
        }
    }

    return selectedCertificate;
}

UPDATE: I ended up using certificate thumbprint, as suggested by jglouie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水水月牙 2025-01-08 16:34:28

不需要。例如,OpenSSL 允许用户在创建证书时设置序列号。

请参阅:https://www.openssl.org/docs/manmaster/ man1/openssl-x509.html

-set_serial n

指定要使用的序列号。此选项可与-key-signkey-CA 选项一起使用。如果与-CA选项结合使用,则不使用序列号文件(由-CAserial选项指定)。

序列号可以是十进制或十六进制(如果前面有 0x)。

No. For example, OpenSSL allows the user to set the serial number when creating certificates.

See: https://www.openssl.org/docs/manmaster/man1/openssl-x509.html

-set_serial n

Specifies the serial number to use. This option can be used with the -key, -signkey, or -CA options. If used in conjunction with the -CA option the serial number file (as specified by the -CAserial option) is not used.

The serial number can be decimal or hex (if preceded by 0x).

望她远 2025-01-08 16:34:28

TL;DR:您必须使用颁发者名称 + 序列号的复合密钥。如果您需要简单的密钥,请使用证书的指纹。


引用来自 security.stackexchange 的@ThomasPornin:

证书中,选择序列号由 CA 颁发
证书。只是写在证书上而已。 CA 可以
以任何认为合适的方式选择序列号,不一定
随机(并且必须适合 20 个字节)。 CA应该选择
唯一的序列号,即 CA 的唯一序列号。你不能指望
全球唯一的序列号;在X.509的梦幻世界里,
它是全球唯一的发行者DN+序列号对(每个CA
拥有自己独特的专有名称,并注意不要重复使用
序列号)。

指纹是根据完整证书计算的哈希值,
其中包括其所有字段,包括签名。那个是
对于给定的证书,全球独一无二,直至其固有的
所使用的哈希函数的抗碰撞性。微软软件
倾向于使用 SHA-1,其一些理论缺陷是已知的,
但尚未产生实际碰撞。

来自:https://security.stackexchange.com/questions/35691/what-is-the-difference- Between-serial-号码和指纹

TL;DR: You must use a composite key of issuer name + serial number. If you need a simple key, use certificate's thumbprint.


Quoting @ThomasPornin from security.stackexchange:

In a certificate, the serial number is chosen by the CA which issued
the certificate. It is just written in the certificate. The CA can
choose the serial number in any way as it sees fit, not necessarily
randomly (and it has to fit in 20 bytes). A CA is supposed to choose
unique serial numbers, that is, unique for the CA. You cannot count on
a serial number being unique worldwide; in the dream world of X.509,
it is the pair issuerDN+serial which is unique worldwide (each CA
having its own unique distinguished name, and taking care not to reuse
serial numbers).

The thumbprint is a hash value computed over the complete certificate,
which includes all its fields, including the signature. That one is
unique worldwide, for a given certificate, up to the inherent
collision resistance of the used hash function. Microsoft software
tends to use SHA-1, for which some theoretical weaknesses are known,
but no actual collision has been produced (yet).

From: https://security.stackexchange.com/questions/35691/what-is-the-difference-between-serial-number-and-thumbprint

奶气 2025-01-08 16:34:28

正如另一个答案中提到的,序列号在 CA 内必须是唯一的。因此序列号不能单独用作证书的唯一ID——来自不同CA的证书可以具有相同的序列号。您需要存储 Issuer 和 SerialNumber 属性的组合。另外,对于自签名证书和自制的CA软件编号很可能会发生冲突,因为很多人会从0开始编号。

As mentioned in another answer, the serial number must be unique within the CA. So serial number alone can't be used as a unique ID of the certificate -- certificates from different CAs can have the same serial number. You need to store combination of Issuer and SerialNumber properties. Also, for self-signed certificates and home-made CA software numbers will most likely collide as many people will start numbering from 0.

自我难过 2025-01-08 16:34:28

是的,根据 X.509 规范,序列号对于特定 CA 是唯一的:

4.1.2.2 序列号

序列号是 CA 分配给每个
证书。对于由某个机构颁发的每个证书来说,它必须是唯一的
给定的 CA(即发行者名称和序列号标识唯一的
证书)。

Yes, according to X.509 specification serial number is unique for specific CA:

4.1.2.2 Serial number

The serial number is an integer assigned by the CA to each
certificate. It MUST be unique for each certificate issued by a
given CA (i.e., the issuer name and serial number identify a unique
certificate).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文