cer证书签名验证[通俗易懂]

cer证书签名验证[通俗易懂]一个cer证书本身也是需要签名的,这是为了防止cer证书被篡改。证书有两种类型:1.根证书2.根证书签发的子证书。根证书比较特殊,它是自签名的。而其他子证书的签名公钥都保存在它的上级证书里面。可以用C#来做一些验证。首先是根证书的签名验证。//验证根证书签名X509Certificate2x509Root=newX

大家好,又见面了,我是你们的朋友全栈君。

一个cer证书本身也是需要签名的,这是为了防止cer证书被篡改。

证书有两种类型:

1. 根证书

2. 根证书签发的子证书。

根证书比较特殊,它是自签名的。而其他子证书的签名公钥都保存在它的上级证书里面。

可以用C#来做一些验证。

首先是根证书的签名验证。

        // 验证根证书签名
        X509Certificate2 x509Root = new X509Certificate2("C:\\Users\\kevin\\Desktop\\KevinRoot.cer");
        Console.WriteLine("Root Certificate Verified?: {0}{1}", x509Root.Verify(), Environment.NewLine);  // 根证书是自签名,所以可以通过。

很简单,因为根证书是自签名的,x509Root.Verify()会返回true。

然后是子证书的验证,

       X509Certificate2 x509 = new X509Certificate2("C:\\Users\\kevin\\Desktop\\ChildSubject2.cer");

        byte[] rawdata = x509.RawData;
        Console.WriteLine("Content Type: {0}{1}", X509Certificate2.GetCertContentType(rawdata), Environment.NewLine);
        Console.WriteLine("Friendly Name: {0}{1}", x509.FriendlyName, Environment.NewLine);
        Console.WriteLine("Certificate Verified?: {0}{1}", x509.Verify(), Environment.NewLine);
        Console.WriteLine("Simple Name: {0}{1}", x509.GetNameInfo(X509NameType.SimpleName, true), Environment.NewLine);
        Console.WriteLine("Signature Algorithm: {0}{1}", x509.SignatureAlgorithm.FriendlyName, Environment.NewLine);
    //    Console.WriteLine("Private Key: {0}{1}", x509.PrivateKey.ToXmlString(false), Environment.NewLine);  // cer里面并没有私钥信息
        Console.WriteLine("Public Key: {0}{1}", x509.PublicKey.Key.ToXmlString(false), Environment.NewLine);
        Console.WriteLine("Certificate Archived?: {0}{1}", x509.Archived, Environment.NewLine);
        Console.WriteLine("Length of Raw Data: {0}{1}", x509.RawData.Length, Environment.NewLine);

这里我用自己创建的子证书,x509.Verify()总是返回false,就算我把根证书导入到“trust”里面,还是返回false,不知道为什么。但是如果我用公司的证书(verisign颁发的),却可以返回true。不知道是不是我自己创建的根证书,子证书有什么配置问题,有空再研究。反正验证也就这么回事。

下面的代码,用来检查整个证书链。

        //Output chain information of the selected certificate.
        X509Chain ch = new X509Chain();
        ch.Build(x509);
        Console.WriteLine("Chain Information");
        ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
        Console.WriteLine("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag);
        Console.WriteLine("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode);
        Console.WriteLine("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags);
        Console.WriteLine("Chain verification time: {0}", ch.ChainPolicy.VerificationTime);
        Console.WriteLine("Chain status length: {0}", ch.ChainStatus.Length);
        Console.WriteLine("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count);
        Console.WriteLine("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine);
        //Output chain element information.
        Console.WriteLine("Chain Element Information");
        Console.WriteLine("Number of chain elements: {0}", ch.ChainElements.Count);
        Console.WriteLine("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine);

    //    int index = 0;
        foreach (X509ChainElement element in ch.ChainElements)
        {
            Console.WriteLine("Element subject name: {0}", element.Certificate.Subject);
            Console.WriteLine("Element issuer name: {0}", element.Certificate.Issuer);
            Console.WriteLine("Element certificate valid until: {0}", element.Certificate.NotAfter);
            Console.WriteLine("Element certificate is valid: {0}", element.Certificate.Verify());
            Console.WriteLine("Element error status length: {0}", element.ChainElementStatus.Length);
            Console.WriteLine("Element information: {0}", element.Information);
            Console.WriteLine("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine);

            string a = element.Certificate.Thumbprint;
       //     string b = ch.ChainPolicy.ExtraStore[0].Thumbprint;
            //ch.ChainPolicy.ExtraStore[index - 1].Thumbprint;

            if (ch.ChainStatus.Length > 1)
            {
                for (int index = 0; index < element.ChainElementStatus.Length; index++)
                {
                    Console.WriteLine(element.ChainElementStatus[index].Status);
                    Console.WriteLine(element.ChainElementStatus[index].StatusInformation);
                }
            }
        }

上面的代码也很简单,其实就是把整个证书链里面的每一个证书信息打印一下。具体的函数调用参数msdn。

下面是完整代码,注意里面的几个证书路径是我写死的,如果想测试下面的代码,只需要自己创建几个证书。

using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;

class CertSelect
{
    static void Main()
    {
        // 验证根证书签名
        X509Certificate2 x509Root = new X509Certificate2("C:\\Users\\kevin\\Desktop\\KevinRoot.cer");
        Console.WriteLine("Root Certificate Verified?: {0}{1}", x509Root.Verify(), Environment.NewLine);  // 根证书是自签名,所以可以通过。

        X509Certificate2 x509 = new X509Certificate2("C:\\Users\\kevin\\Desktop\\ChildSubject2.cer");

        byte[] rawdata = x509.RawData;
        Console.WriteLine("Content Type: {0}{1}", X509Certificate2.GetCertContentType(rawdata), Environment.NewLine);
        Console.WriteLine("Friendly Name: {0}{1}", x509.FriendlyName, Environment.NewLine);
        Console.WriteLine("Certificate Verified?: {0}{1}", x509.Verify(), Environment.NewLine);
        Console.WriteLine("Simple Name: {0}{1}", x509.GetNameInfo(X509NameType.SimpleName, true), Environment.NewLine);
        Console.WriteLine("Signature Algorithm: {0}{1}", x509.SignatureAlgorithm.FriendlyName, Environment.NewLine);
    //    Console.WriteLine("Private Key: {0}{1}", x509.PrivateKey.ToXmlString(false), Environment.NewLine);  // cer里面并没有私钥信息
        Console.WriteLine("Public Key: {0}{1}", x509.PublicKey.Key.ToXmlString(false), Environment.NewLine);
        Console.WriteLine("Certificate Archived?: {0}{1}", x509.Archived, Environment.NewLine);
        Console.WriteLine("Length of Raw Data: {0}{1}", x509.RawData.Length, Environment.NewLine);


        //Output chain information of the selected certificate.
        X509Chain ch = new X509Chain();
        ch.Build(x509);
        Console.WriteLine("Chain Information");
        ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
        Console.WriteLine("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag);
        Console.WriteLine("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode);
        Console.WriteLine("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags);
        Console.WriteLine("Chain verification time: {0}", ch.ChainPolicy.VerificationTime);
        Console.WriteLine("Chain status length: {0}", ch.ChainStatus.Length);
        Console.WriteLine("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count);
        Console.WriteLine("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine);
        //Output chain element information.
        Console.WriteLine("Chain Element Information");
        Console.WriteLine("Number of chain elements: {0}", ch.ChainElements.Count);
        Console.WriteLine("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine);

    //    int index = 0;
        foreach (X509ChainElement element in ch.ChainElements)
        {
            Console.WriteLine("Element subject name: {0}", element.Certificate.Subject);
            Console.WriteLine("Element issuer name: {0}", element.Certificate.Issuer);
            Console.WriteLine("Element certificate valid until: {0}", element.Certificate.NotAfter);
            Console.WriteLine("Element certificate is valid: {0}", element.Certificate.Verify());
            Console.WriteLine("Element error status length: {0}", element.ChainElementStatus.Length);
            Console.WriteLine("Element information: {0}", element.Information);
            Console.WriteLine("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine);

            string a = element.Certificate.Thumbprint;
       //     string b = ch.ChainPolicy.ExtraStore[0].Thumbprint;
            //ch.ChainPolicy.ExtraStore[index - 1].Thumbprint;

            if (ch.ChainStatus.Length > 1)
            {
                for (int index = 0; index < element.ChainElementStatus.Length; index++)
                {
                    Console.WriteLine(element.ChainElementStatus[index].Status);
                    Console.WriteLine(element.ChainElementStatus[index].StatusInformation);
                }
            }
        }

        x509.Reset();
        
    }
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/131872.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何下载ts文件

    如何下载ts文件首先,什么是ts,请自行百度,网上资料很多,了解下基本概念就行,这里就不多做介绍了。网页中是如何播放ts文件的:网页中一般是在一个文件中描述排列顺序,这个文件一般都以m3u8为后缀,然后通过分片段不

    2022年7月1日
    23
  • 软件工程之软件设计③(概要设计说明书,详细设计说明书)

    软件工程之软件设计③(概要设计说明书,详细设计说明书)需求分析确定了系统的开发目标,下一步工作就是软件设计。软件设计可以进一步地分为两个阶段:总体设计和详细设计。总体设计又称概要设计,即确定系统的具体实现方案、给出软件的模块结构、编写总体设计说明书。详细设计又称过程设计,这一步的工作,就是要对系统中的每个模块给出足够详细的过程性描述。这种描述不是程序的书写,而是用一些工具来表示每个模块,所以这种描述是不…

    2022年5月10日
    35
  • SQL Server 2008R2安装图解教程(附下载链接)「建议收藏」

    SQL Server 2008R2安装图解教程(附下载链接)「建议收藏」下载链接及密码:https://pan.baidu.com/s/1AUrOTnZ_uN0-q7syV2vpGg密码:0u08注:安装SQLServer2008之前,必须预先安装NETFramework3.51、双击下载好的安装文件setup.exe。(在Windows7操作系统系,启动MicrosoftSQL2008安装程序后,系统兼容性助手将提示软件存在兼容性…

    2022年6月23日
    31
  • 从零开始学习UCOSII操作系统12–内存管理

    从零开始学习UCOSII操作系统12–内存管理从零开始学习UCOSII操作系统12–内存管理前言:在标准的C语言中,可以用malloc()和free()2个动态的分配内存和释放内存,但是在嵌入式中,调用malloc()和free()却是非常危险的。因为多次调用这两个函数,会把原来的很大的一块连续的内存区域逐渐的分割成许多非常小的而且彼此又不相邻的内存块,也就是所谓的内存碎片。这样子的话,使得程序后面连一段非常小的内存都分

    2022年5月4日
    95
  • HttpSession解析[通俗易懂]

    HttpSession解析[通俗易懂]1 .HttpSession概述 1.1 什么是HttpSesssion javax.servlet.http.HttpSession接口表示一个会话,我们可以把一个会话内需要共享的数据保存到HttSession对象中!1.2 获取HttpSession对象HttpSessionrequest.getSesssion():如果当前会话已经有了session对象那么直接返回

    2022年7月12日
    17
  • C++之Error无法解析的外部符号[通俗易懂]

    C++之Error无法解析的外部符号[通俗易懂]C++之VisualStudio的使用遇到问题解决文章目录C++之VisualStudio的使用遇到问题解决问题一无法解析的外部符号问题二无法打开文件lib问题三debug不可以运行,release可以运行问题一无法解析的外部符号[问题描述]在编译中遇到,viaualstudio无法解析的外部符号该符号在外部函数中被引用[问题处理]1.分析问题,这个错误定义为一个:连接错误。2.根本原因是函数虽然申明了,但是没有定义函数的实现3.排查问题出现的几

    2022年6月28日
    33

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号