您现在的位置是:网站首页> 编程资料编程资料
golang开发及数字证书研究分享_Golang_
2023-05-26
668人已围观
简介 golang开发及数字证书研究分享_Golang_
在go语言提供的系统包中包含了大量和数字证书有关的方法。在这些方法中就有私钥生成的方法、私钥解析的方法、证书请求生成的方法、证书生成的方法等等。通过这些方法应该能够实现和openssl命令类似的功能。
仿照openssl生成证书的流程(从私钥的生成—>证书请求的生成—>证书的生成)用go语言进行模拟。
私钥的生成
在go的x509包下有go定义的证书的结构,该结构如下:
Raw []byte // Complete ASN.1 DER content (certificate, signature algorithm and signature). RawTBSCertificate []byte // Certificate part of raw ASN.1 DER content. RawSubjectPublicKeyInfo []byte // DER encoded SubjectPublicKeyInfo. RawSubject []byte // DER encoded Subject RawIssuer []byte // DER encoded Issuer Signature []byte SignatureAlgorithm SignatureAlgorithm PublicKeyAlgorithm PublicKeyAlgorithm PublicKey interface{} Version int SerialNumber *big.Int Issuer pkix.Name Subject pkix.Name NotBefore, NotAfter time.Time // Validity bounds. KeyUsage KeyUsage Extensions []pkix.Extension ExtraExtensions []pkix.Extension UnhandledCriticalExtensions []asn1.ObjectIdentifier ExtKeyUsage []ExtKeyUsage // Sequence of extended key usages. UnknownExtKeyUsage []asn1.ObjectIdentifier // Encountered extended key usages unknown to this package. BasicConstraintsValid bool // if true then the next two fields are valid. IsCA bool MaxPathLen int MaxPathLenZero bool SubjectKeyId []byte AuthorityKeyId []byte OCSPServer []string IssuingCertificateURL []string // Subject Alternate Name values DNSNames []string EmailAddresses []string IPAddresses []net.IP PermittedDNSDomainsCritical bool // if true then the name constraints are marked critical. PermittedDNSDomains []string CRLDistributionPoints []string PolicyIdentifiers []asn1.ObjectIdentifier在该结构中有PublicKeyAlgorithm字段,该字段用来表示生成公钥的算法。该字段的变量中可使用的字段如下:
const ( UnknownPublicKeyAlgorithm PublicKeyAlgorithm = iota RSA DSA ECDSA )
一共定义了4中情况。除去Unknown的情况。剩下的三种的实现分别在crypto/rsa、crypto/dsa、crypto/ecdsa这三个包中定义了实现。
RSA
使用RSA方法生成公私钥的方式非常简单。在crypto/rsa包中直接提供了生成方法。
func GenerateKey(random io.Reader, bits int) (*PrivateKey, error)
该方法生成一个rsa的私钥。查找整个包所提供的方法并没有什么方法能够生成公钥。但在包中有公钥的结构说明。查看私钥的结构:
type PrivateKey struct { PublicKey // public part. D *big.Int // private exponent Primes []*big.Int // prime factors of N, has >= 2 elements. Precomputed PrecomputedValues }赫然发现,公钥包含在私钥的结构中。换句话说,只要生成的私钥,公钥就同时拥有了(ECDSA和DSA的公钥也是如此)。
ECDSA
使用ECDSA生成公私钥的方式和RSA的方式非常类似:
func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error)
在crypto/elliptic为参数c提供了4中实现方式。分别为:
func P224() Curve func P256() Curve func P384() Curve func P521() Curve
DSA
使用DSA生成公私钥的方式和上面两种有些不同:
func GenerateKey(priv *PrivateKey, rand io.Reader) error
私钥并不是作为结果返回,而是作为参数传入。那很简单,我直接初始化一个DSA的私钥,然后把该私钥作为参数传入不就可以了嘛。事实是,仅仅是实例化了一个DSA的私钥是无法完成公私钥的生成的。生成的结果如下:
priv:&{PublicKey:{Parameters:{P: Q: G:} Y:} X:} 可以发现公钥中的所有内容都是为nil(空),由此可以说明无法只通过GenerateKey()方法生成DSA的私钥。
在crypto/dsa包中还提供了:
func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) error
通过该方法的描述,可以了解到该方法是为DSA设置参数。那又如何和公私钥有关呢?,在DSA的私钥结构中包含公钥,在公钥的结构中就包含该方法所需要传入的参数Parameters。由此,我便想到可以先使用该方法对一些参数进行初始化,然后再生成私钥。
priv := &dsa.PrivateKey{} dsa.GenerateParameters(&priv.Parameters, rand.Reader, dsa.L1024N160) dsa.GenerateKey(priv, rand.Reader)生成的私钥内容如下:
priv:&{PublicKey:{Parameters:{P:+91268520972047344779510472614939006285152176630742165979533208518526258287540244526987668731096217967904150874969731516661412604963023247030101570715552650277776208098462838867711078025572452557692674802977527475661989210578136725258241385474445330497234586673407237238372329018550727884900161895964574509801 Q:+767580094855879488293276223470508701563202760721 G:+42393651221310072390273970570719382707264443685255379637082820177806079494092036767507554061381644533127114802103872901363724639317297276457243780033980909021336576570837756106975221868617534717069925676009421223798208864916837561389117514471387385853288499961716794226875046226553216578582138687489881455573} Y:+68767508229940365112562020548287141674708444377336699267991474890690034611201698420418573204906537903040876819582645033160073997940957577512216430788561800033703926395782022182868300960590402743043934344374390498368316144177816214923367214895567903510165216432049170686626889267028482641530556275670781873053} X:+628682865942164859869306394087148223993136336500}注意:Golang 对DSA证书没有完整的支持。
给私钥上锁(加访问密码)
在使用openssl进行私钥生成的时候,openssl需要我提供私钥的访问密码。那使用go进行私钥时,应该也有该功能。那应该在什么时候添加这个密码呢?是在生成私钥的时候,还是在生成pem文件的时候。我首先想到的是在生成秘密的时候,但是在crypto/rsa、crypto/dsa、crypto/ecdsa这三个包中查找时并没有发现任何和密码有关的词眼。那就应该在生成pem文件的时候加上密码。生成pem文件的方法在encoding/pem这个包中。但该包中只有两个编码,一个解码的方法,和密码有没有任何关系,唯一的存在的关系就是Block结构中的Header字段。
使用openssl生成的私钥文件中会存在这样的字段:
Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,02a0ba59e8cfd431
使用该字段来说明使用加密方式和提供用于解密的初始值向量。
在生成私钥和生成文件都无法把密码添加进去。那我就在想是否是在得到私钥的时候对私钥的byte数组进行加密。但这样就需要自己实现了。讲道理的话,go应该会为这种普遍性的东西提供已经封装好的方法。来回重新看api文档。发现自己漏看一个非常重要的包crypto/x509。在该包提供的方法中。很轻松的就找到了如下两个方法:
func DecryptPEMBlock(b *pem.Block, password []byte) ([]byte, error) func EncryptPEMBlock(rand io.Reader, blockType string, data, password []byte, alg PEMCipher) (*pem.Block, error)
在这两个方法中又要pem,password,恩应该就是这两个方法了,正好一个生成一个解析。
同在x509包下提供了:
func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error)
把RSA和ECDSA私钥转换成byte数组的方法,但是没有找到把DSA私钥转换成byte数组的方法。
生成证书请求
证书请求生成很简单在crypto/x509中直接提供了现成的方法。
func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, priv interface{}) (csr []byte, err error)但使用用该方法有一个限制条件:
All keys types that are implemented via crypto.Signer are supported (This includes *rsa.PublicKey and *ecdsa.PublicKey.)
无法使用*dsa.PublicKey类型的公钥。而传入的参数是一个私钥,因此无法使用dsa类型的私钥。
go对dsa类型的证书
该方法需要通过一个证书请求的模板,在go中CertificateRequest是如下定义的:
Raw []byte // Complete ASN.1 DER content (CSR, signature algorithm and signature). RawTBSCertificateRequest []byte // Certificate request info part of raw ASN.1 DER content. RawSubjectPublicKeyInfo []byte // DER encoded SubjectPublicKeyInfo. RawSubject []byte // DER encoded Subject. Version int Signature []byte SignatureAlgorithm SignatureAlgorithm PublicKeyAlgorithm PublicKeyAlgorithm PublicKey interface{} Subject pkix.Name Attributes []pkix.AttributeTypeAndValueSET Extensions []pkix.Extension ExtraExtensions []pkix.Extension DNSNames []string EmailAddresses []string IPAddresses []net.IP有一些内容可以不用填写。如果填写了,在后面生成证书时将作为内容直接填入,我就根据openssl生成证书请求时在控制台所展现的内容进行填写。即添加Subject中的内容。Subject是这样定义的:
type Name struct { Country, Organization, OrganizationalUnit []string Locality, Province []string StreetAddress, PostalCode []string SerialNumber, CommonName string Names []AttributeTypeAndValue ExtraNames []AttributeTypeAndValue }生成证书
在go提供的crypto/x509包下并没有生成CA的方法,生成证书的方法也只有一个方法:
func CreateCertificate(rand io.Reader, template, parent *Certificate, pub, priv interface{}) (cert []byte, err error)它的参数中使用的是两个证书,和我们之前生成的CertificateRequest没有关系,而且在整个crypto/x509中的方法中都没有找到把CertificateRequest转换成Certificate的方法,而且CertificateRequest和Certificate中的部分数据结构是一样的,因此猜想是通过把CertificateRequest中的部分内容复制到Certificate中。然后再通过CreateCertificate进行签发。
如果传入的两个证书参数是一样的,那么生成的证书是一张自签发的根证书。如果传入的两张证书不同,生成的就是普通的证书了。使用的公钥和私钥是签发者的公私钥即参数parent的公私钥。和生成CertificateRequest一样,在这个方法中使用的公私钥不能是DSA类型的。
坑
设置CA
在Certificate这个结构体中有
IsCA这个字段。用来标识该证书是CA证书,但是在设置该字段为true后生成的证书在扩展中并没有显示这个证书是CA证书的。原因是在如果要使IsCA生效,需要设置BasicCons
