当前位置:网站首页>【Golang | gRPC】使用openssl生成证书
【Golang | gRPC】使用openssl生成证书
2022-07-02 15:56:00 【田土豆】
环境:
OpenSSL:3.0.4
1. 密钥
1.1 创建RSA私钥
使用openssl genrsa
# 生成1024位私钥,输出到控制台
openssl genrsa 1024
# 生成2048位私钥,保存到server.key文件中
openssl genrsa -out server.key 2048
# 生成2048位带加密的私钥(交互方式输入密码),保存到server_passphrase.key文件中
openssl genrsa -out server_passphrase.key -des3 2048
-out: 将私钥输出到指定文件,如不加则输出到控制台2048: 生成2048位的私钥-des3: 生成加密私钥
1.2 使用RSA私钥生成公钥
# 如果是加密私钥,需要交互输入私钥密码
openssl rsa -in server.key -pubout -out server_public.key
-in: 导入私钥文件-pubout: 输出公钥-out: 将输出公钥保存到指定文件里
1.3 查看密钥
1.3.1 查看私钥
openssl rsa -in server.key -text
1.3.2 查看公钥
openssl rsa -RSAPublicKey_in -in server_public.key -text

2. 证书请求文件
有了私钥后,就可以创建Certificate Signing Request(csr,证书请求文件)。使用私钥对csr进行sign(签名),同时csr中包含与私钥对应的公钥
2.1 使用已有的私钥创建csr
2.1.1 使用交互模式
使用openssl req
# 使用私钥server.key以交互模式生成csr,
openssl req -new -key server.key -out server.csr
- 一般来说,交互时直接敲
enter就行,表示使用默认值;如果想某些字段为空,可以输入.再敲enter -new: 生成新的csr-key: 使用指定的私钥用于签名,同时csr会包含私钥对应的公钥-out: 将生成的csr保存到指定文件中
2.1.2 使用配置文件
新建配置文件csr.cnf
[req]
prompt = no
distinguished_name = dn
req_extensions = ext
input_password = tian # 加密私钥的密码
[dn]
CN = www.feistyduck.com # Common Name 公用名称,一般是网站主机名
emailAddress = [email protected]
O = Feisty Duck Ltd # Organization 组织
L = London # Locality 所在地
C = GB # Country 国家,如中国:CN
[ext]
subjectAltName = DNS:www.feistyduck.com,DNS:feistyduck.com
补充:
http://www.sina.com.cn/为例,http是通信使用的协议,sina.com.cn是域名,www是提供服务的机器的名字(服务器名),服务器名+域名才是主机名,即www.sina.com.cn是主机名subjectAltName(X.509拓展之SAN),如果只使用CN,那么一个证书通常只对应一个主机名,如果要使用多主机名的证书,通过subjectAltName进行配置,一般来说,主机名最少包含两种,一个带www.的,一个不带;也可以使用通配符的方式,比如shannont.com和*.shannont.com
使用配置文件生成csr
openssl req -new -config csr.cnf -key server.key -out server2.csr
-config: 指定使用的配置文件
2.2 查看csr的内容
openssl req -in server.csr -text -noout
- csr中包含公钥,签名以及之前交互时输入值为非空的字段等
- 当前版本摘要算法默认是
sha256 -text: 输出csr的文本形式-noout: 不输出-----BEGIN CERTIFICATE REQUEST-----...-----END CERTIFICATE REQUEST-----之间的内容
3. 证书
3.1 自签名证书
有了csr后,不一定要去向CA申请一个证书,大多时候开发阶段为了自测可以申请一个自签名的证书(就是使用自己的私钥来给证书签名)
3.1.1 使用csr生成证书
使用openssl x509命令
# 如果签名用的是加密私钥需要交互输入私钥密码
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
x509: X.509 是密码学里公钥证书的格式标准-req: 指明输入的文件是csr-days: 证书的有效时长-in: 输入的文件-signkey: 等同于-key,指明签名用的私钥
3.1.2 使用私钥直接生成证书
# 交互模式,需要输入国家,组织,所在地等一些字段值,如果是加密私钥,还要输入私钥密码
openssl req -new -x509 -days 365 -key server.key -out server2.crt
# 使用上文csr的配置文件直接生成证书
openssl req -new -x509 -days 365 -key server.key -config csr.cnf -out server3.crt
# 使用-subj选项指定国家,组织,所在地等一些字段值,如果是加密私钥,还要输入私钥密码
openssl req -new -x509 -days 365 -key server.key -out server4.crt -subj "/C=CN/L=ShangHai"
3.2 查看证书
证书中关于公钥的部分正好等同于1.3.2节中公钥的内容
openssl x509 -text -in server.crt -noout

3.3 检查证书
使用openssl s_server创建一个server,默认端口是4433,导入证书和私钥
openssl s_server -cert server.crt -key server.key -www
打开浏览器,输入https://127.0.0.1:4433,得到如下,需要手动信任
边栏推荐
- Navigateur Chrome pour un accès rapide au stackoverflow
- 开发一个禁止删除namespace的控制器
- Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
- 链表求和[dummy+尾插法+函数处理链表引用常见坑位]
- 应广单片机开发案例
- 台风来袭,多景区暂时关闭,省文旅厅提醒注意安全!
- 泡椒凤爪制作教程
- Modbus协议通信异常
- 阿里云子账户 - 权限策略 - 授权给某个账户某个 OSS Bucket 的完全控制权限
- [how to connect the network] Chapter 5 explore the server
猜你喜欢

【历史上的今天】7 月 2 日:BitTorrent 问世;商业系统 Linspire 被收购;索尼部署 PlayStation Now

Map集合详细讲解

This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W

一日2篇Nature!中科大校友段镶锋团队纳米材料新成果,曾是贝尔比奖章第三位华人得主...

我的创作纪念日

Modbus协议通信异常

Chrome browser quick access stackoverflow

维护万星开源向量数据库是什么体验

Viewing technological changes through Huawei Corps (VI): smart highway

Pfc232-sop8/14/16 should be wide-ranging and can be tape programmed with burning program
随机推荐
Map集合详细讲解
【目标跟踪】|SiamFC
[非线性控制理论]8_三种鲁棒控制器的比较
Yingguang single chip microcomputer pms150/pmc150/pms150c consumer single chip microcomputer
chrome浏览器快速访问stackoverflow
【曆史上的今天】7 月 2 日:BitTorrent 問世;商業系統 Linspire 被收購;索尼部署 PlayStation Now
PMS132B单片机TWS数码管蓝牙充电仓方案开发
嵌入式 ~ 介绍
Rk1126 platform project summary
567.字符串中的排列
Daily question - "number of daffodils"
Huimang micro IO MCU ft60f011a-rb
HBuilderX运行到手机或模拟器提示没有找到设备
Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
外包干了五年,废了...
Many scenic spots are temporarily closed due to the typhoon. The provincial culture and tourism department reminds you to pay attention to safety!
Making tutorial of chicken feet with pickled peppers
No such file or directory: ‘/tmp/tmpxxx/tmpxxx.py‘
How to create a new page for SAP Spartacus storefront
[target tracking] |siamfc