当前位置:网站首页>【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,得到如下,需要手动信任
边栏推荐
- 蓝牙技术|物联网的可穿戴设备新工作模式,蓝牙BLE助力新工作模式
- Keras深度学习实战——基于VGG19模型实现性别分类
- Yingguang single chip microcomputer development specification pmc131 with AD chip to detect battery voltage single chip microcomputer sop8/14
- Virtual lab basic experiment tutorial -7 Polarization (1)
- Larvel document reading notes custom authentication login and registration using larvel 8
- AtCoder Beginner Contest 237 VP补题
- HDU - 1114 Piggy-Bank(完全背包)
- Common SQL statements (complete example)
- Pfc232-sop8/14/16 should be wide-ranging and can be tape programmed with burning program
- Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
猜你喜欢

Easyswoole3.2 restart failed

From a professional background, I can't get into a small company for interview

体验一下阿里云文字识别OCR

【网络是怎么连接的】第四章 探索接入网和网络运营商

Rk1126 platform project summary

chrome瀏覽器快速訪問stackoverflow

Taiwan Feiling fm8pb513b MCU provides MCU program development product design

Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service

Pfc232-sop8/14/16 should be wide-ranging and can be tape programmed with burning program

pytorch支持32位吗?
随机推荐
HBuilderX运行到手机或模拟器提示没有找到设备
Does pytorch support 32 bits?
When the industrial Internet began to enter the deep-water area, it appeared more in the form of industry
每日一题——倒置字符串
uva1169
Laravel文档阅读笔记-Custom Authentication Login And Registration Using Laravel 8
【网络是怎样连接的】第五章 探索服务器
JDBC
把xshell连接服务器关掉,运行的jar包就自动停止的解决方案
[非线性控制理论]8_三种鲁棒控制器的比较
Yingguang single chip microcomputer development specification pmc131 with AD chip to detect battery voltage single chip microcomputer sop8/14
Yingguang single chip microcomputer (MCU popular science)
Viewing technological changes through Huawei Corps (VI): smart highway
POJ - 1458 common subsequence (longest common subsequence)
Experience Alibaba cloud character recognition OCR
RK1126平台项目总结
应广单片机003烧录器自定义封装使用技巧
chrome浏览器快速访问stackoverflow
vector的底层模拟实现
What is the experience of maintaining Wanxing open source vector database