当前位置:网站首页>X.509 certificate based on go language
X.509 certificate based on go language
2022-07-07 00:19:00 【biyezuopinvip】
Resource download address :https://download.csdn.net/download/sheziqiong/85926996
Resource download address :https://download.csdn.net/download/sheziqiong/85926996
One 、X.509 Certificate description
In cryptography ,X.509 Is the format standard of public key certificate , It has been applied in many Internet protocols .X.509 Format of public key certificate , Revocation certificate list (CRLs), The certificate verification path algorithm is specified .
One X.509 The certificate contains its version number , Certificate serial number , Signature algorithm , Issuer , Certificate subject , The period of validity , Public key , Public key and other information . The information in the certificate is used ASN.1 Encoding ,ASN.1 The data in this paper is represented by tag, length , Value . The basic structure of the certificate is RFC 5280 in 4.1 Section has the following provisions :
Certificate ::= SEQUENCE {
tbsCertificate TBSCertificate,
signatureAlgorithm AlgorithmIdentifier,
signatureValue BIT STRING
}
( Certificate subject , Signature algorithm and signature value )
TBSCertificate ::= SEQUENCE {
version [0] EXPLICIT Version DEFAULT v1,
serialNumber CertificateSerialNumber,
signature AlgorithmIdentifier,
issuer Name,
validity Validity,
subject Name,
subjectPublicKeyInfo SubjectPublicKeyInfo,
issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
-- If present, version MUST be v2 or v3
subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
-- If present, version MUST be v2 or v3
extensions [3] EXPLICIT Extensions OPTIONAL
-- If present, version MUST be v3
}
( Certificate subject , Include version number , Serial number , Signature algorithm identification , Signer information , The period of validity , Certificate subject , Certificate public key information , Issuer ID, The main body ID And extension segment )
Version ::= INTEGER { v1(0), v2(1), v3(2) }
( Certificate version , The value can be 0,1,2, Each represents version 1,2,3)
CertificateSerialNumber ::= INTEGER
( Certificate serial number )
Validity ::= SEQUENCE {
notBefore Time,
notAfter Time
}
( The certificate is valid for , It consists of start and end times )
Time ::= CHOICE {
utcTime UTCTime,
generalTime GeneralizedTime
}
UniqueIdentifier ::= BIT STRING
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier,
subjectPublicKey BIT STRING
}
( Public key information includes public key algorithm and public key data )
Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Extension ::= SEQUENCE {
extnID OBJECT IDENTIFIER,
critical BOOLEAN DEFAULT FALSE,
extnValue OCTET STRING
-- contains the DER encoding of an ASN.1 value
-- corresponding to the extension type identified
-- by extnID
}
AlgorithmIdentifier ::= SEQUENCE {
algorithm OBJECT IDENTIFIER,
parameters ANY DEFINED BY algorithm OPTIONAL
}
Two 、 Read X.509 certificate
Used in the submitted program Go Language writing and reading X.509 The program , Called Go Linguistic encoding/asn1 library ASN.1 Reading of encoded content and crypto/x509/pkix The library reads the issuer and certificate subject information .
In the code, according to the above X.509 The certificate structure defines the following structure for ASN.1 Read :
- CertificateData, Corresponding to Certificate:
type CertificateData struct {
TBSCertificate tbsCertificate
SignatureAlgorithm AlgorithmIdentifier
SignatureValue asn1.BitString
}
- TbsCertificate, Corresponding to TBSCertificate:
type tbsCertificate struct {
Version int `asn1:"optional,explicit,default:0,tag:0"`
SerialNumber *big.Int
Signature AlgorithmIdentifier
Issuer asn1.RawValue
Validity timeSpan
Subject asn1.RawValue
PublicKey publicKeyInfo
UniqueId asn1.BitString `asn1:"optional,tag:1"`
SubjectUniqueId asn1.BitString `asn1:"optional,tag:2"`
Extensions []extension `asn1:"optional,explicit,tag:3"`
}
- timeSpan, Corresponding to the above structure Validity:
type timeSpan struct {
NotBefore, NotAfter time.Time
}
- publicKeyInfo, Corresponding to the above SubjectPublicKeyInfo:
type publicKeyInfo struct {
Algorithm AlgorithmIdentifier
PublicKey asn1.BitString
}
- extension, Corresponding to the above structure Extension
type extension struct {
ExtnID asn1.ObjectIdentifier
Critical bool `asn1:"default:false"`
ExtnValue []byte
}
- AlgorithmIdentifier, Corresponding to the above AlgorithmIdentifier:
type AlgorithmIdentifier struct {
Algorithm asn1.ObjectIdentifier
Parameters asn1.RawValue `asn1:"optional"`
}
3、 ... and 、 Brief description of program structure
The submitted code contains three code files :x509cert/certificate.go It is the definition of the certificate structure and the identification function of the algorithm in the certificate , And define the following certInfo The structure is convenient for other codes to obtain certificate information :
type CertInfo struct {
Version int
Serial *big.Int
Signature AlgorithmIdentifier
Issuer IssuerType
Validity timeSpan
Subject IssuerType
PublicKey publicKeyInfo
UniqueId asn1.BitString
SubjectUniqueId asn1.BitString
Extensions []extension
SignatureAlgorithm AlgorithmIdentifier
SignatureValue asn1.BitString
}
type IssuerType struct {
Country string
Province string
City string
Organization string
Unit string
}
x509cert/static.go The file contains some static data during the certificate reading process, such as the name of the algorithm and oid etc. .
main.go The file contains the main function of the program , Based on the pem Format or DER Read the file in format and print the certificate information to standard output .
Four 、 Program compilation run results
Program usage go Compile , Execute the command under the code directory go build –o x509 It can be compiled and generated and named x509 The executable of . The submitted bin The folder already contains the use go1.11.1 Compiled Windows,MacOS as well as Linux The executable under .
The use method of the program is :
./x509 [--DER] filename
If DER Options , Then the program will use DER Way to read the certificate , If not specified, the program will use PEM Way to read the certificate .
The certificate used in the test run is openssl Generate self signed root certificate , The order is as follows :
openssl req -new -x509 -days 365 -keyout rsa.key -out rsa.pem
After the command is executed, you need to enter the certificate subject information :

Using this command will use RSA The validity period of algorithm signature generation is 365 Root certificate of days , Use the certificate tool provided by the system to view the certificate information as follows :

Use the following command to generate pem Certificate to DER Certificate for testing :
openssl x509 -in rsa.pem -outform der -out rsa.crt
Generated after conversion rsa.crt Certificate file , The viewing result is the same as the original certificate .
Use the following command to generate ECDSA The certificate of the signing algorithm is used for testing :
openssl ecparam -name secp256k1 -genkey -param_enc explicit -out ecparam.pem
openssl req -new -x509 -key ecparam.pem -out ec.pem -days 365
Enter the certificate subject information to generate a certificate :

The effect of using the written program to execute under the certificate directory is shown in the figure :
- rsa.pem
./…/bin/x509-darwin-amd64 rsa.pem

The program displays the version number of the certificate , Serial number , Signature algorithm , Issuer , The main body , The period of validity , Public key information ,RSA Public key data and certificate signature , The content of the certificate displayed by the system tool is the same .
- Use DER The result of reading the certificate file is shown in the figure :

And PEM The result of certificate reading in mode is the same .
- Read ecdsa The encrypted certificate results are as follows :

The same information as when generating the certificate .
Resource download address :https://download.csdn.net/download/sheziqiong/85926996
Resource download address :https://download.csdn.net/download/sheziqiong/85926996
边栏推荐
- 【CVPR 2022】半监督目标检测:Dense Learning based Semi-Supervised Object Detection
- Use package FY in Oracle_ Recover_ Data. PCK to recover the table of truncate misoperation
- How to use vector_ How to use vector pointer
- Oracle EMCC 13.5 environment in docker every minute
- 【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
- What is AVL tree?
- Automatic test tool katalon (WEB) test operation instructions
- Devops can help reduce technology debt in ten ways
- js导入excel&导出excel
- Wind chime card issuing network source code latest version - commercially available
猜你喜欢

智能运维应用之道,告别企业数字化转型危机

DevOps可以帮助减少技术债务的十种方式

Rider离线使用Nuget包的方法

How rider uses nuget package offline

准备好在CI/CD中自动化持续部署了吗?

@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.

How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms

刘永鑫报告|微生物组数据分析与科学传播(晚7点半)

Introduction to GPIO

Interface joint debugging test script optimization v4.0
随机推荐
37頁數字鄉村振興智慧農業整體規劃建設方案
DAY TWO
DAY SIX
17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster
Compilation of kickstart file
基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】
How to use vector_ How to use vector pointer
MySQL主从之多源复制(3主1从)搭建及同步测试
vector的使用方法_vector指针如何使用
刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
Command line kills window process
Leecode brush question record sword finger offer 58 - ii Rotate string left
Interface joint debugging test script optimization v4.0
GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
Google, Baidu and Yahoo are general search engines developed by Chinese companies_ Baidu search engine URL
Why should a complete knapsack be traversed in sequence? Briefly explain
kubernetes部署ldap
智能运维应用之道,告别企业数字化转型危机
谷歌百度雅虎都是中国公司开发的通用搜索引擎_百度搜索引擎url
web渗透测试是什么_渗透实战