公私钥、CA证书生成

摘要:
公钥和私钥以及证书的生成本文以Linux系统为例,模拟了http服务器身份验证证书的CA生成。http服务器操作1.生成私钥。使用OpenSSL工具生成服务器私钥文件[nginx@nginx-node01~]$opensslgenrsa1024˃˃$HOSTNAME.keyGeneratingRSAprivatekey,1024bit长模数…………++++++。。。。。
公私钥、证书生成

本文以Linux系统为例模拟CA生成http服务器的认证证书

http服务器操作

1.生成私钥

使用OpenSSL工具生成服务器私钥key文件

[nginx@nginx-node01 ~]$ openssl genrsa 1024 >> $HOSTNAME.key  
Generating RSA private key, 1024 bit long modulus
.................++++++
...........................++++++
e is 65537 (0x10001)
[nginx@nginx-node01 ~]$ ls
nginx-node01.key

2.生成证书预签csr文件

[nginx@nginx-node01 ~]$ openssl req -new -key $HOSTNAME.key -out $HOSTNAME.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:henan
Locality Name (eg, city) [Default City]:zhengzhou
Organization Name (eg, company) [Default Company Ltd]:kov
Organizational Unit Name (eg, section) []:Dev
Common Name (eg, your name or your server's hostname) []:www.kov.com
Email Address []:sys@kov.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[nginx@nginx-node01 ~]$ ls
nginx-node01.csr  nginx-node01.key

CA服务器操作

1.创建所需要的文件

touch /etc/pki/CA/index.txt 生成证书索引数据库文件
echo 01 > /etc/pki/CA/serial 指定第一个颁发证书的序列号  

2.生成CA私钥

[root@ca ~]# hostname
ca
[root@ca ~]# openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048

3. CA生成自签证书

[root@ca private]# openssl req -new -x509 -key cakey.pem -out /etc/pki/CA/cacert.pem -days 7300
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:henan
Locality Name (eg, city) [Default City]:zhengzhou
Organization Name (eg, company) [Default Company Ltd]:kov
Organizational Unit Name (eg, section) []:Dev
Common Name (eg, your name or your server's hostname) []:xx.kov.com
Email Address []:xx@kov.com

4.CA签署证书

将http服务器证书预签csr文件发给CA,由CA对服务器的预签文件csr进行签署,最后得到最终证书文件crt。(默认国家,省,公司名称三项必须和CA一致)

[root@ca private]# openssl ca -in /root/nginx-node01.csr -out /etc/pki/CA/certs/nginx-node01.crt -days 365    
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jul 13 15:31:40 2020 GMT
            Not After : Jul 13 15:31:40 2021 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = henan
            organizationName          = kov
            organizationalUnitName    = Dev
            commonName                = www.kov.com
            emailAddress              = sys@kov.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                81:DB:3C:4E:6D:0E:BD:5A:78:2D:F2:86:62:CD:B3:03:45:F1:AB:F3
            X509v3 Authority Key Identifier: 
                keyid:DF:B4:69:95:C5:71:44:EE:0B:9C:2E:CB:1C:CD:37:E3:0E:FD:AC:E8

Certificate is to be certified until Jul 13 15:31:40 2021 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

5.验证私钥、证书是否匹配

验证ca签署的http服务器证书文件nginx-node01.crt和http服务器私钥nginx-node01.key是否匹配

openssl rsa  -noout -modulus -in nginx-node01.key |openssl md5
openssl x509 -noout -modulus -in nginx-node01.crt |openssl md5

配置Nginx验证https

参考《Nginx配置4 Https配置》

补充

CA签署指定域名证书

生成证书签署扩展文件

[root@ca CA]# cat kov.ext 
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName=@SubjectAlternativeName

[ SubjectAlternativeName ]
DNS.1=kov.com
DNS.2=www.kov.com

重新签署证书

[root@ca ~]# openssl x509 -req -in /root/nginx-node01.csr -CA /etc/pki/CA/cacert.pem -CAkey /etc/pki/CA/private/cakey.pem -CAcreateserial -out nginx-node01.crt -days 3650 -sha256 -extfile  kov.ext

免责声明:文章转载自《公私钥、CA证书生成》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇html 常用代码块Atlassian JIRA服务器模板注入漏洞复现(CVE-2019-11581)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Nginx报错:Sorry, the page you are looking for is currently unavailable. Please try again later.

查看了进程, nginx, php-fpm都在运行, 排除程序错误, 那么就是配置的问题了. 一个可能的错误, 是由于配置中的fastcgi_pass 配置错了 错误的配置如下 server { listen 80; server_name localhost; #charset koi8-r; #a...

nginx 配置详解(新手必看)

内容来源:http://blog.csdn.net/david_xtd/article/details/16967837 译者注:不知道其他开发者是否和我一样,参与或者写了很多Web项目,但是却没有真正的去完整的部署应用,很多时候都是交给ops即运维的同学帮忙来做。而作为一个有节操的开发者,我认为了解一些服务器方面的知识是很有必要的,读了这篇文章之后,自己...

Nginx 配置访问静态资源

做个简单的配置: 以txt/png/mp4结尾的请求都会按照如下规则寻找返回文件 关键词: location、root location ~ .(mp4|png|txt) {   root /usr/local/tomcat9/webapps/scm;   autoindex on; } 主义root代表的是替换请求URL中域名的值,如请求的URL为...

制作openresty的docker镜像 + nginx笔记 调试rewrite和location Nginx 学习笔记

1.Dockerfile FROM centos:7.6.1810 MAINTAINER openresty1.15.8.2 <dingxiaowei@do1.com> RUN yum install -y yum-utils && yum-config-manager --add-repo https://open...

nginx 及 tomcat 端口冲突问题

nginx 监听 80 转发 8088 tomcat 原先通过80 及 8088 都能访问,区别是 80就走nginx转发,8088则直接走tomcat 证据为: 80端口,nginx强行过滤掉css和js后,页面出来无样式和响应,而走8088的话,直接走tomcat,页面完整 再回到主题:原先80及8088都能访问,突然就不能访问了,百思不得其解,最后...

基于Centos 7 vue+nginx+docker 的前端项目部署

1.先在本地将vue项目建立,并且确定能跑起来使用npm run serve  ctrl+鼠标左键在本地查看。 然后使用npm run build,会在本地生成一个dist文件。  然后建一个文件Dockerfile,文件内容可以在VS Code里面先写好,或者在linux服务器上面编辑都可以。 然后需要一台服务器,我这里是用的公司内部的linux服务...