Golang(十一)TLS 相关知识(二)OpenSSL 生成证书
发布时间:2020-12-16 09:20:55 所属栏目:大数据 来源:网络整理
导读:0. 前言 接前一篇文章,上篇文章我们介绍了数字签名、数字证书等基本概念和原理 本篇我们尝试自己生成证书 参考文献:TLS完全指南(二):OpenSSL操作指南 1. OpenSSL 简介 OpenSSL 是一个开源项目,其组成主要包括三个组件: openssl:多用途的命令行工具 l
0. 前言
1. OpenSSL 简介
1.1 指令
1.1.1 genrsa
[args]: args1 对生成的私钥文件是否要使用加密算法进行对称加密: -des : CBC模式的DES加密 -des3 : CBC模式的3DES加密 -aes128 : CBC模式的AES128加密 -aes192 : CBC模式的AES192加密 -aes256 : CBC模式的AES256加密 args2 对称加密密码 -passout passwords 其中passwords为对称加密(des、3des、aes)的密码(使用这个参数就省去了console交互提示输入密码的环节) args3 输出文件 -out file : 输出证书私钥文件 [numbits]: 密钥长度,理解为私钥长度
1.1.2 req
[args] args1 是输入输入文件格式:-inform arg -inform DER 使用输入文件格式为 DER -inform PEM 使用输入文件格式为 PEM args2 输出文件格式:-outform arg -outform DER 使用输出文件格式为 DER -outform PEM 使用输出文件格式为 PEM args3 是待处理文件 -in inputfilepath args4 待输出文件 -out outputfilepath args5 用于签名待生成的请求证书的私钥文件的解密密码 -passin passwords args6 用于签名待生成的请求证书的私钥文件 -key file args7 指定输入密钥的编码格式 -keyform arg -keyform DER -keyform NET -keyform PEM args8 生成新的证书请求 -new args9 输出一个 X509 格式的证书,签名证书时使用 -x509 args10 使用 X509 签名证书的有效时间 -days // -days 3650 有效期 10 年 args11 生成一个 bits 长度的 RSA 私钥文件,用于签发,与-key互斥,生成证书请求或者自签名证书时自动生成密钥,然后生成的密钥名称由 -keyout 参数指定 -newkey rsa:bits args12 设置 HASH 算法-[digest],指定对创建请求时提供的申请者信息进行数字签名时指定的 hash 算法 -md5 -sha1 // 高版本浏览器开始不信任这种算法 -md2 -mdc2 -md4 args13 指定 openssl 配置文件,很多内容不容易通过参数配置,可以指定配置文件 -config filepath args14 显示格式 txt(用于查看证书、私钥信息) -text
1.1.3 x509
[args] args1 是输入输入文件格式:-inform arg -inform DER 使用输入文件格式为 DER -inform PEM 使用输入文件格式为 PEM args2 输出文件格式:-outform arg -outform DER 使用输出文件格式为 DER -outform PEM 使用输出文件格式为 PEM args3 是待处理 X509 证书文件 -in inputfilepath args4 待输出 X509 证书文件 -out outputfilepath args5 表明输入文件是一个“请求签发证书文件(CSR)”,等待进行签发 -req args6 签名证书的有效时间 -days // -days 3650 有效期 10 年 args7 指定用于签发请求证书的根 CA 证书 -CA arg args8 根 CA 证书格式(默认是 PEM) -CAform arg args9 指定用于签发请求证书的 CA 私钥证书文件 -CAkey arg args10 指定根 CA 私钥证书文件格式(默认为 PEM 格式) -CAkeyform arg args11 指定序列号文件(serial number file) -CAserial arg args12 如果序列号文件(serial number file)没有指定,则自动创建它 -CAcreateserial args12 设置 HASH 算法-[digest],指定对创建请求时提供的申请者信息进行数字签名时指定的 hash 算法 -md5 -sha1 // 高版本浏览器开始不信任这种算法 -md2 -mdc2 -md4
2. 具体使用2.1 生成 RSA 秘钥对
2.2 生成身份证申请
2.3 生成数字证书
2.4 HTTPS 验证
package main import ( "io" "log" "net/http" ) func main() { http.HandleFunc("/",func(w http.ResponseWriter,req *http.Request) { io.WriteString(w,"hello,world!n") }) if e := http.ListenAndServeTLS("0.0.0.0:5200","/home/ao/Documents/certs/review/server.crt","/home/ao/Documents/certs/review/server.key",nil); e != nil { log.Fatal("ListenAndServe: ",e) } //if e := http.ListenAndServe("0.0.0.0:5200",nil); e != nil { // log.Fatal("ListenAndServe: ",e) //} }
package main import ( "crypto/tls" "crypto/x509" "io" "io/ioutil" "log" "net/http" "os" ) func loadCA(caFile string) *x509.CertPool { pool := x509.NewCertPool() ca,err := ioutil.ReadFile(caFile) if err != nil { log.Fatal("ReadFile: ",err) } pool.AppendCertsFromPEM(ca) return pool } func main() { //c := &http.Client{ // Transport: &http.Transport{ // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},// }} c := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{RootCAs: loadCA("/home/ao/Documents/certs/review/server.crt")},}} resp,err := c.Get("https://localhost:5200") if err != nil { log.Fatal("http.Client.Get: ",err) } defer resp.Body.Close() io.Copy(os.Stdout,resp.Body) }
$ go run client.go
hello,world!
$ go run client.go 2019/09/30 15:11:41 http.Client.Get: Get https://127.0.0.1:5200: x509: cannot validate certificate for 127.0.0.1 because it doesn‘t contain any IP SANs exit status 1
2019/09/30 15:11:41 http: TLS handshake error from 127.0.0.1:33596: remote error: tls: bad certificate
$ go run client.go 2019/09/30 15:11:41 http.Client.Get: Get https://127.0.0.1:5200: x509: cannot validate certificate for 127.0.0.1 because it doesn‘t contain any IP SANs exit status 1
2.5 配置域名为 IP
$ go run client.go 2019/09/30 15:19:24 http.Client.Get: Get https://127.0.0.1:5200: x509: cannot validate certificate for 127.0.0.1 because it doesn‘t contain any IP SANs exit status 1
$ openssl genrsa -out server.key 2048 $ echo subjectAltName = IP:127.0.0.1 > extfile.cnf $ openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -extfile extfile.cnf -out server.crt
2.6 不使用自签名证书
$ openssl genrsa -out ca.key 2048 $ openssl req -x509 -new -nodes -key ca.key -days 10000 -out ca.crt -subj "/CN=localhost.ca.com"
$ openssl genrsa -out server.key 2048 $ openssl req -new -key server.key -subj "/CN=127.0.0.1" -out server.csr $ openssl x509 -req -sha256 -days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -extfile extfile.cnf
2.7 CSR 配置多个域名和 IP
[req] distinguished_name = req_distinguished_name req_extensions = v3_req [req_distinguished_name] countryName = CN countryName_default = CN stateOrProvinceName = Beijing stateOrProvinceName_default = Beijing localityName = Beijing localityName_default = Beijing organizationName = WangAo organizationName_default = WangAo organizationalUnitName = Dev organizationalUnitName_default = Dev commonName = test.openssl.com commonName_default = test.openssl.com commonName_max = 64 [v3_req] basicConstraints = CA:TRUE subjectAltName = @alt_names [alt_names] DNS.1 = test.openssl.com IP.1 = 127.0.0.1 IP.2 = 10.0.2.15
$ openssl genrsa -out server.key 2048 $ openssl req -nodes -new -key server.key -out server.csr -subj "/CN=test.openssl.com" $ openssl x509 -req -sha256 -days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -extensions v3_req -extfile openssl.cnf
3. 小结?
3. 参考文献
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |