本篇文章中,将描述如何使用go创建CA,并使用CA签署证书。在使用openssl创建证书时,遵循的步骤是 创建秘钥 >创建CA >生成要颁发证书的秘钥 >使用CA签发证书。这种步骤,那么我们现在就来尝试下。
首先,会从将从创建CA开始。 CA会被用来签署其他证书
接下来需要对证书生成公钥和私钥
然后生成证书:
我们看到的证书内容是PEM编码后的,现在 caBytes 我们有了生成的证书,我们将其进行 PEM 编码以供以后使用:
证书的 x509.Certificate 与CA的 x509.Certificate 属性有稍微不同,需要进行一些修改
为该证书创建私钥和公钥:
有了上述的内容后,可以创建证书并用CA进行签名
要保存成证书格式需要做PEM编码
创建一个 ca.go 里面是创建ca和颁发证书的逻辑
如果需要使用的话,可以引用这些函数
panic: x509: unsupported public key type: rsa.PublicKey
这里是因为 x509.CreateCertificate 的参数 privatekey 需要传入引用变量,而传入的是一个普通变量
extendedKeyUsage :增强型密钥用法(参见"new_oids"字段):服务器身份验证、客户端身份验证、时间戳。
keyUsage : 密钥用法,防否认(nonRepudiation)、数字签名(digitalSignature)、密钥加密(keyEncipherment)。
文章来自https://www.cnblogs.com/Cylon/p/16436126.html
周末在家无趣,研究了一个golang里面的Certificate.Verify函数。
golang的官方定义在这里: https://golang.org/pkg/crypto/x509/#Certificate.Verify
函数原型声明如下:
func (c Certificate) Verify(opts VerifyOptions) (chains [][] Certificate, err error)
其中:
这个函数的功能是:
Verify attempts to verify c by building one or more chains from c to a certificate in opts.Roots, using certificates in opts.Intermediates if needed. If successful, it returns one or more chains where the first element of the chain is c and the last element is from opts.Roots.
If opts.Roots is nil and system roots are unavailable the returned error will be of type SystemRootsError.
解释一下就是:
举一个例子:
假设存在证书链签出关系:C1 ->C2 ->C3 ->C4,即C1签出C2,C2签出C3,C3签出C4;现在使用函数:
我们根据Intermediates和Roots的值不同,比较输出结果: