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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Subversion安装成Windows服务

    Subversion安装成Windows服务

    2021年8月17日
    55
  • java集合框架源码解析_java源代码怎么用

    java集合框架源码解析_java源代码怎么用概述我们知道,java中容器分为Map集合和Collection集合,其中Collection中的又分为Queue,List,Set三大子接口。其下实现类与相关的实现类子类数量繁

    2022年8月16日
    7
  • Markdown基本语法

    Markdown基本语法

    2021年11月5日
    42
  • 查看linux系统版本centos,CentOS下查看系统版本的4种方法

    查看linux系统版本centos,CentOS下查看系统版本的4种方法Linux有很多的发行版,不同的版本会有一些细微区别,所以经常需要查看服务器系统的版本号。下面来看下CentOS下如何查看CentOS版本。方法1:cat/etc/issue执行命令:[www@pythontab.com]$cat/etc/issueCentOSrelease6.8(Final)Kernelonanm我当前系统版本就是:CentOS6.8知识扩展:etc目录…

    2022年6月24日
    29
  • Java线程池中三种方式创建 ThreadFactory 设置线程名称

    本文讲一下Java线程池中创建 ThreadFactory 设置线程名称的三种方式。看代码即可。文章目录第一种 CustomizableThreadFactory第二种 ThreadFactoryBuilder第三种 BasicThreadFactory总结第一种 CustomizableThreadFactorySpring 框架提供的 CustomizableThreadFactory。ThreadFactory springThreadFactory = new CustomizableTh

    2022年2月28日
    77
  • 高速电平转换芯片_电平转换电路分压

    高速电平转换芯片_电平转换电路分压现在很多SOC器件为了降低功耗,都把IO口的电平设计成了1.8V,核电压0.85V,当这种SOC做主平台时,在做接口设计需要格外关注电平的匹配。单板中经常需要将1.8V的电平转换成3.3V或者转成5V。如果没有注意到输入和输出信号之间的电平匹配,系统就无法正常工作。这篇文章主要从两个简单的案例入手,分析电平转换电路需要注意的一些问题,以及在此类芯片数据手册中几个重要参数的解读,对开发人员来说,掌握这些器件的参数是器件选型必须关注的点。三极管做电平转换以常见的三极管做1.8V转3.3V为案例。电路图

    2022年8月10日
    9

发表回复

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

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