apache https 双向证书生成
Https分单向认证和双向认证
单向认证表现形式:网站URL链接为https://xxx.com格式
双向认证表现心事:网站URL链接为https://xxx.com格式,并且需要客户端浏览器安装一个client.pfx格式的证书文件才能打开网址
需求说明:假设需要实现本地环境newdefend.com域名双向认证。单向认证,直接忽略本文中黄色底的操作步骤即可。也不需要生成和安装客户端证书:client.pfx
参考网址:http://blog.163.com/hr_php/blog/static/235853083201503011428985/
安装环境:Wampserver集成安装包;window7系统。
第一步:cmd进入apache的bin目录。eg:D:\wamp5.3\bin\apache\Apache2.2.21\bin
指明配置文件
1
|
set OPENSSL_CONF=../conf/openssl.cnf |
配置参考(不需要手动去改这个文件的内容,这里只是贴出代码,提供参考。下面建立文件夹,主要就是从这里看的):

dir = ./demoCA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
database = $dir/index.txt # database index file.
# several ctificates with same subject.
new_certs_dir = $dir/newcerts # default place for new certs. certificate = $dir/cacert.pem # The CA certificate
serial = $dir/serial # The current serial number
crlnumber = $dir/crlnumber # the current crl number
# must be commented out to leave a V1 CRL
crl = $dir/crl.pem # The current CRL
private_key = $dir/private/cakey.pem# The private key
RANDFILE = $dir/private/.rand # private random number file

新建一些目录和文件。创建目录的原因:由于配置文件openssl.cnf里面会用到这些目录或者文件,继续下面的命令时,会操作这些目录和文件
在apache/bin目录创建如下文件夹
demoCA/newcerts/
demoCA/private/
创建如下文件
demoCA/index.txt
demoCA/serial //内容写 2个(0-9,A-F)的字符即可,如1A
第二步:生成需要的私钥key
1
2
3
|
openssl genrsa > root.key // 生成根密钥 openssl genrsa > server.key // 生成服务端密钥 openssl genrsa > client.key // 生成客户端密钥 |
第三步:生成自签名的根证书
1
|
openssl req -x509 - new -key root.key >root.crt |
说明:-new 生成一个新的文件 -key filename 参数filename指明我们的私有密钥文件名 -x509 将产生自签名的证书,一般用来测试用,或者自己玩下做个Root CA.证书的扩展在config文件里面指定
第四步:生成服务端,客户端签名请求文件。
1
2
|
openssl req - new -key server.key - out server.csr openssl req - new -key client.key - out client.csr //此处可能会有错误信息,请看下面的错误解决方案 |
说明:按提示输入一系列的参数 CN - SH - SH - '' - '' - 域名/IP 。除了Common Name处填写域名/ip需要特别注意,国家地区填写“中国CN上海SH”。其余地方可以为空

Country Name (2 letter code) [AU]:CNISO国家代码(只支持两位字符)
State or Province Name (full name) [Some-State]:ZJ所在省份
Locality Name (eg, city) []:HZ所在城市
Organization Name (eg, company):THS公司名称
Organizational Unit Name (eg, section) []:THS组织名称
Common Name (eg, YOUR name) []:localhost(申请证书的域名或IP地址)
Email Address []:laoniangke@xxx.com管理员邮箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:交换密钥
An optional company name []:

提示:错误解决方案(出现在最开始创建的那堆文件里)

//错误提示
Sign the certificate? [y/n]:y
failed to update database
TXT_DB error number 2 //产生的原因是:
This thing happens when certificates share common data. You cannot have two
certificates that look otherwise the same. //解决办法
//方法一:
修改demoCA下 index.txt.attr
unique_subject = yes 为 unique_subject = no
//方法二:
删除demoCA下的index.txt里面的内容
//方法三:
将 common name设置成不同的

第五步:使用根证书为服务端及客户端签名
openssl ca -in server.csr -cert root.crt -keyfile root.key -out server.crt
openssl ca -in client.csr -cert root.crt -keyfile root.key -out client.crt
说明:-in filename要签名的csr文件 -cert CA本身的证书文件名 -keyfile CA自己的私有密钥文件 -out filename签名后的证书文件名。证书的细节也会给写进去
第六步:配置openssl.cnf,创建文件路径,生成所需cakey.pem(CA的key文件)和cacert.pem(CA的crt文件)文件
openssl genrsa -out demoCA/private/cakey.pem 2048
openssl req -out demoCA/cacert.pem -x509 -new -key demoCA/private/cakey.pem
第七步:客户端证书转成pfx格式,生成后,直接双击文件安装到浏览器 (重要)
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.pfx
注意:这个时候填写的密码,是客户安装证书的时候,需要填写的密码。密码可以为空
到此openssl相关的证书文件就结束了
第八步:配置apache目录httpd.conf文件
LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf
第九步:配置httpd-ssl.conf。说明:如果apache下有多个站点,还要配置http-vhosts.conf

SSLSessionCache "shmcb:D:/wamp/bin/apache/apache2.4.9/logs/ssl_scache(512000)"
<VirtualHost _default_:443> DocumentRoot "D:/wamp/www"
ServerName localhost SSLEngine on
SSLCertificateFile "D:/wamp/bin/apache/apache2.4.9/bin/server.crt"
SSLCertificateKeyFile "D:/wamp/bin/apache/apache2.4.9/bin/server.key" # 如果是单向认证,下面3行注释掉
SSLCACertificateFile "D:/wamp/bin/apache/apache2.4.9/bin/root.crt"
SSLVerifyClient require
SSLVerifyDepth 1
CustomLog "D:/wamp/bin/apache/apache2.4.9/logs/ssl_request.log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" </VirtualHost>

http-vhosts.conf

<VirtualHost *:80>
DocumentRoot "D:/wamp/www/www.newdefend.com"
ServerName my.newdefend.com
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "D:/wamp/www/www.newdefend.com"
ServerName my.newdefend.com
ErrorLog "D:/wamp/www/logs/error.log"
TransferLog "D:/wamp/www/logs/access.log" SSLEngine on
SSLCertificateFile "D:/wamp/bin/apache/apache2.4.9/bin/server.crt"
SSLCertificateKeyFile "D:/wamp/bin/apache/apache2.4.9/bin/server.key"
# 如果是单向认证,下面3行注释掉
SSLCACertificateFile "D:/wamp/bin/apache/apache2.4.9/bin/root.crt"
SSLVerifyClient require
SSLVerifyDepth 10
</VirtualHost>

重启wamp
双向认证效果展示:
下图表示:client.pfx文件还没有在电脑上安装,请双击安装
下图表示:client.pfx已经安装,客户端打开时,需要选择证书
下图表示:成功,直接访问即可。因为是本地生成的证书,所以不被浏览器信任。真是上线的时候,找个权威的CA机构就不会出现这种情况了
最后:查看电脑上已经安装了那些证书,请参考:https://jingyan.baidu.com/article/c275f6baf8622ae33d756794.html
效果展示如下:192.168.184.53就是我自己生成的证书
#以上内容为转载内容 为windows 的部署方式下面内容是经过测试 linux 环境下配置的
cd /etc/pki/CA mkdir newcerts
mkdir private
touch index.txt
echo 1B > serial
openssl genrsa > root.key
openssl genrsa > server.key
openssl genrsa > client.key
openssl req -x509 -new -key root.key >root.crt
openssl req -new -key server.key -out server.csr
openssl req -new -key client.key -out client.csr
#生成证书填写相关信息必须要填写 国家及城市和主机名,否则会不成功缺少必要字段
openssl ca -in server.csr -cert root.crt -keyfile root.key -out server.crt
openssl ca -in client.csr -cert root.crt -keyfile root.key -out client.crt openssl ca -in client.csr -cert root.crt -keyfile root.key -out client.crt openssl genrsa -out private/cakey.pem 2048
openssl req -out cacert.pem -x509 -new -key private/cakey.pem #生成客户端密钥
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.pfx #apache 配置
vim /etc/conf/httpd.conf #增加,如没有mod_ssl.so 模块需要安装 yum -y install mod_ssl
IncludeOptional conf.d/*.conf
LoadModule ssl_module modules/mod_ssl.so #httpd-ssl 配置
SSLVerifyClient require
SSLVerifyDepth 1
SSLCACertificateFile /etc/pki/CA/root.crt
SSLCertificateKeyFile /etc/pki/CA/server.key
SSLCertificateKeyFile /etc/pki/CA/server.key
#注意mac用户安装数字证书必须有密码,否则安装失败,解决方法是生成一个有密码 的证书
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.pfx
apache https 双向证书生成的更多相关文章
- mkcert本地 HTTPS 加密证书生成工具
软件介绍: mkcert 是一个生成本地 HTTPS 加密证书的工具,一个命令就可以生成证书,不需要任何配置. 下载地址: https://github.com/FiloSottile/mkcert/ ...
- apache https 双向认证
Https分单向认证和双向认证 单向认证表现形式:网站URL链接为https://xxx.com格式 双向认证表现心事:网站URL链接为https://xxx.com格式,并且需要客户端浏览器安装一个 ...
- linux下安装Apache(https) 服务器证书安装配置指南
一. 安装准备 1. 安装Openssl 要使Apache支持SSL,需要首先安装Openssl支持.推荐下载安装openssl-0.9.8k.tar.gz 下载Openssl:http: ...
- https 双向证书
一:服务器端 1.首先需要配置站点的SSl证书 打开Nginx安装目录下conf目录中的nginx.conf文件 找到 # HTTPS server # #server { ...
- Https:证书生成 .p12 .keyStore 和 .truststore文件理解
当我们需要SSL证书时,可以自动生成SSL证书,但是每个系统都申请一次证书会比较麻烦,所以用到了如下几个文件格式: .p12(PKCS #12) 我们的每一个证书都可以生成一个.p12文件,这个文 ...
- [svc]HTTPS证书生成原理和部署细节
参考: http://www.barretlee.com/blog/2015/10/05/how-to-build-a-https-server/ 今天摸索了下 HTTPS 的证书生成,以及它在 Ng ...
- nodejs之https双向认证
说在前面 之前我们总结了https的相关知识,如果不懂可以看我另一篇文章:白话理解https 有关证书生成可以参考:自签证书生成 正题 今天使用nodejs来实现https双向认证 话不多说,直接进入 ...
- Tomcat服务器配置https双向认证(使用keytool生成证书)
一,HTTPS原理 1,HTTP.HTTPS.SSL.TLS介绍与相互关系 (1)HTTP:平时浏览网页时候使用的一种协议.HTTP协议传输的数据都是未加密的(明文),因此使用HTTP协议传输隐私 ...
- linux:Nginx+https双向验证(数字安全证书)
本文由邓亚运提供 Nginx+https双向验证 说明: 要想实现nginx的https,nginx必须启用http_ssl模块:在编译时加上--with-http_ssl_module参数就ok.另 ...
随机推荐
- 对象、对象监视器、同步队列、执行线程关系(synchronized的实现细节或原理)
synchronized在使用的时候底层细节你了解吗,相信很多同学对细节很少关注:比如竞争失败了的线程怎么安置,每个对象的监视器,线程执行synchronized时,其实是获取对象的监视器才能进入同步 ...
- Redis底层数据结构实现
REDIS 较宽泛的支持5种数据结构 分别为 字符串 列表 集合 散列 有序集合 关于这几种数据结构的使用 相信网上有很多资料,查看官网API 也很详细了 读者可以自己随意翻阅 很方便 . 接下 ...
- JS中var与let的区别
区别: var声明的变量,其作用域在该语句所在的函数之内,存在着变量提升的现象. let声明的变量,其作用域为该句所在的代码块内,不存在变量提升的问题. let相比于var,其不允许在相同作用域内,重 ...
- FAAS -- Serverless
FAAS概念,无服务器运算,功能即服务,function-as-a-service 初创企业-大型企业.民间组织-政府机构 ===>>>> 上云 云计算第三代技术 -- Ser ...
- flume的一些使用
一.第一层采集通道的编写 1.第一层采集脚本Source的选择①Source: 数据源在日志文件中! 读取日志中的数据,可以使用以下Source ExecSource: 可以执行一个linux命令,例 ...
- spark的 structStreaming 一些介绍
转发 https://www.toutiao.com/a6696339998905467403/?tt_from=mobile_qq&utm_campaign=client_share& ...
- LeetCode 二分查找模板 III
模板 #3: int binarySearch(vector<int>& nums, int target){ if (nums.size() == 0) return -1; i ...
- Spring框架之websocket源码完全解析
Spring框架之websocket源码完全解析 Spring框架从4.0版开始支持WebSocket,先简单介绍WebSocket协议(详细介绍参见"WebSocket协议中文版" ...
- Openstack 启动一个实例(九)
Openstack 启动一个实例(九) 创建一个提供者网络: # 创建一个提供者网络: openstack network create --share --external --provider-p ...
- 【Linux】nohup和&的区别
同样都是后台执行进程,但是nohup和&有什么区别呢? & 是指后台运行: nohup 的功能和& 之间的功能并不相同. 其中,nohup 可以使得命令永远运行下去和用户终端没 ...