一个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); // 根证书是自签名。所以能够通过。

非常easy,由于根证书是自签名的,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);
}
}
}

上面的代码也非常easy,事实上就是把整个证书链里面的每个证书打印信息一下。详细的函数调用參数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(); } }

版权声明:本文博客原创文章。博客,未经同意,不得转载。

cer证书签名验证的更多相关文章

  1. JAVA调用 keytool 生成keystore 和 cer 证书

    keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据: 密钥实体( ...

  2. 安装.cer证书并将证书从.cer格式转化为.pem格式

    ## 安装.cer证书并将证书从.cer格式转化为.pem格式 ### 安装.cer证书到本地 打开*运行*窗口 输入MMC.exe, 单击*确定* 在打开的控制台1的窗口中. 选择*文件*, 选择* ...

  3. 下载https协议需要的cer证书

    一:https简介 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全 ...

  4. android手机导入.cer证书文件的方法

    访问很多https协议的网站需要安装证书,手机也可以导入cer文件,你知道么?本文将通过简单的两步告诉你手机安装cer文件的方法. 步骤一:请先将数字证书文件“******.cer”文件复制到SD卡的 ...

  5. java 调用 keytool 生成keystore 和 cer 证书

    keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据:密钥实体(K ...

  6. .pfx和.Cer 证书

    通常情况下,作为文件形式存在的证书一般有三种格式: 第一种:带有私钥的证书,由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进 ...

  7. (苹果AppleWWDRCA.cer证书过期)Failed to locate or generate matching signing assets

    从2月14号开始,上传AppStore会碰到:Failed to locate or generate matching signing assets 字数462 阅读13571 评论16 喜欢61 ...

  8. 从自签名证书导出pfx和cer证书

    完整代码: public sealed class DataCertificate { #region 生成证书 /// <summary> /// 根据指定的证书名和makecert全路 ...

  9. 将.cer证书导入java密钥库?

    导入.cer从浏览器下载的证书文件(打开网址并挖掘详细信息)到cacerts keystore中java_home\jre\lib\security为我工作,而不是尝试生成和使用我自己的密钥库. 去你 ...

随机推荐

  1. iOS_数据库3_sqlite3基本操作

    终于效果图: Sqlite3函数总结  1.打开数据库 int sqlite3_open( const char *filename, // 数据库的文件路径 sqlite3 **ppDb // 数据 ...

  2. EL字符串表达式和常用功能用途拦截

    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ${wj ...

  3. [LeetCode55]Jump Game

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  4. [Java][Android][Process] 分享 Process 运行命令行封装类型

    我在以前的文章中提到,使用Java不会有一个问题,创建运行命令来创建太多进程后创建进程行语句. [Android] ProcessBuilder与Runtime.getRuntime().exec分别 ...

  5. VS2010使整个过程说明了安装包

    该项目的第一个版本出来,要成为一个包,很长一段时间没做了一些被遗忘,上差了差资料,写了一个,总结下,可能还不是非常完好,仅作參考. 1.首先在打开 VS2010    >新建>项目 2.创 ...

  6. 玩转Web之Jsp(一)-----jsp中的静态包含(<%@include file="url"%>)与动态包含(<jsp:include>)

    在jsp中include有两种形式,其中<%@include file="url"%>是指令元素,<jsp:include page="" f ...

  7. int a[5]={1,2,3,4,5};printf(&quot;%d\n&quot;, *((int*)(&amp;a+1)-2);

    有说服力的笔试题有一定的期限,问:什么是结果,答案是4,为什么要挤? 我明白(不知道是不正确): &a这是一个数组指针,类型int[5],然后&a添加1其实a+sizeof(int)* ...

  8. 一天JavaScript示例-点击图片显示大图片添加鼠标

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. UML部署图和图九组件图

    前言     UML大部分描写叙述了逻辑和设计方面的信息.实现图用来描写叙述实现方面的信息.实现图包含部署图和构件图. 构件图     1. 概念      构件图从软件架构的角度来描写叙述一个系统的 ...

  10. Apple Watch开发了一些细节和总结

    本文旨在总结最近Watch在发展中遇到的问题和细节 1.左右Watch真机调试问题 一般的情况下,你为IOS主应用创建了一个extention,比方说Today Extension .Xcode都会自 ...