双向认证:
- 客户端向服务器发送消息,首先把消息用客户端证书加密然后连同时把客户端证书一起发送到服务器端,
- 服务器接到消息后用首先用客户端证书把消息解密,然后用服务器私钥把消息加密,把服务器证书和消息一起发送到客户端,
- 客户端用发来的服务器证书对消息进行解密,然后用服务器的证书对消息加密,然后在用客户端的证书对消息在进行一次加密,连同加密消息和客户端证书一起发送到服务器端,
- 到服务器端首先用客户端传来的证书对消息进行解密,确保消息是这个客户发来的,然后用服务器端的私钥对消息在进行解密这个便得到了明文数据。
单向认证:
- 客户端向服务器发送消息,
- 服务器接到消息后,用服务器端的密钥库中的私钥对数据进行加密,然后把加密后的数据和服务器端的公钥一起发送到客户端,
- 客户端用服务器发送来的公钥对数据解密,然后在用传到客户端的服务器公钥对数据加密传给服务器端,
- 服务器用私钥对数据进行解密,
这就完成了客户端和服务器之间通信的安全问题,但是单向认证没有验证客户端的合法性。
在OpenSSL的安装目录下的misc目录下,运行脚本
% /usr/local/ssl/misc/CA.sh -newca
运行完后会生成一个demoCA的目录,里面存放了CA的证书和私钥。
生成server证书
1.创建私钥 :
% openssl genrsa -out server/server-key.pem 1024
2.创建证书请求 :
% openssl req -new -out server/server-req.csr -key server/server-key.pem
-----
Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:bj
Locality Name (eg, city) []:bj
Organization Name (eg, company) [Internet Widgits Pty Ltd]:tb
Organizational Unit Name (eg, section) []:tb
Common Name (eg, YOUR name) []:localhost #此处一定要写服务器所在ip
Email Address []:server@server.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3.自签署证书 :
% openssl x509 -req -in server/server-req.csr -out server/server-cert.pem -signkey server/server-key.pem -CA demoCA/cacert.pem -CAkey demoCA/private/cakey.pem -CAcreateserial -days 3650
4.将证书导出成浏览器支持的.p12格式 :
% openssl pkcs12 -export -clcerts -in server/server-cert.pem -inkey server/server-key.pem -out server/server.p12
密码:123456
1.创建私钥 :
% openssl genrsa -out client/client-key.pem 1024
2.创建证书请求 :
% openssl req -new -out client/client-req.csr -key client/client-key.pem
-----
Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:bj
Locality Name (eg, city) []:bj
Organization Name (eg, company) [Internet Widgits Pty Ltd]:tb
Organizational Unit Name (eg, section) []:tb
Common Name (eg, YOUR name) []:dong
Email Address []:dong@dong.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3.自签署证书 :
% openssl x509 -req -in client/client-req.csr -out client/client-cert.pem -signkey client/client-key.pem -CA demoCA/cacert.pem -CAkey demoCA/private/cakey.pem -CAcreateserial -days 3650
4.将证书导出成浏览器支持的.p12格式 :
% openssl pkcs12 -export -clcerts -in client/client-cert.pem -inkey client/client-key.pem -out client/client.p12
密码:123456
验证可以:openssl verify -CAfile /etc/ins/ca.crt /etc/ins/client.crt
生成 apache ssl 证书
% cp server/server-req.csr newreq.pem
/usr/local/ssl/misc/CA.sh -sign
创建 apache 需要 ssl 证书内容:
% mkdir ssl
% cp newcert.pem ssl/server.crt
% cp demoCA/cacert.pem ssl/cacert.pem
同时复制一份证书:
% cp demoCA/cacert.pem ssl/ca.crt
apache配置
% vi /usr/local/apache/conf/extra/ssl.conf
# 指定服务器证书位置
SSLCertificateFile /usr/local/apache/conf/ssl/server.crt
# 指定服务器证书key位置
SSLCertificateKeyFile /usr/local/apache/conf/ssl/server.key
# 证书目录
SSLCACertificatePath /usr/local/apache/conf/ssl
# 根证书位置
SSLCACertificateFile /usr/local/apache/conf/ssl/cacert.pem
% vi /usr/local/apache/conf/extra/httpd_vhosts.conf
listen 443 https
NameVirtualHost *:443
<VirtualHost _default_:443>
DocumentRoot "/home/zhangy/www/metbee/trunk/src/web"
ServerName *:443
ErrorLog "/home/zhangy/apache/www.metbee.com-error.log"
CustomLog "/home/zhangy/apache/www.metbee.com-access.log" common
Include conf/extra/ssl.conf
</VirtualHost>
来源
http://zhumeng8337797.blog.163.com/blog/static/10076891420133130138156/
...