设为首页 收藏本站
查看: 476|回复: 0

[经验分享] nginx 配置双向 SSL 认证

[复制链接]

尚未签到

发表于 2016-12-25 10:03:21 | 显示全部楼层 |阅读模式
  根据此博客(http://blog.csdn.net/rosw/article/details/3441187)  加以改进
  1) 创建一个CA根证书
2) 创建一个自签名的服务器证书
3) 设置Nginx
4) 创建客户端证书
  1. 找到openssl 目录下的openssl.cnf. 打开并加以修改, 上面博客是自己新建了一个CA配置,我是直接在CA_default上修改的.
  dir = /etc/ssl/private
  private_key = $dir/ca.key
  certificate = $dir/ca.crt
  default_days = 3650
  new_certs_dir = $dir
  #crlnumber      = $dir/crlnumber
  commonName = optional
  2)
创建一个新的CA根证书
  在/etc/ssl 目录下新建一个private文件夹
下面的几个脚本我都放在/etc/ssl目录下

new_ca.sh:
#!/bin/sh
# Generate the key.
openssl genrsa -out private/ca.key
# Generate a certificate request.
openssl req -new -key private/ca.key -out private/ca.csr
# Self signing key is bad... this could work with a third party signed key... registeryfly has them on for $16 but I'm too cheap lazy to get one on a lark.
# I'm also not 100% sure if any old certificate will work or if you have to buy a special one that you can sign with. I could investigate further but since this
# service will never see the light of an unencrypted Internet see the cheap and lazy remark.
# So self sign our root key.
openssl x509 -req -days 3650 -in private/ca.csr -signkey private/ca.key -out private/ca.crt
# Setup the first serial number for our keys... can be any 4 digit hex string... not sure if there are broader bounds but everything I've seen uses 4 digits.
echo FACE > private/serial
# Create the CA's key database.
touch private/index.txt
# Create a Certificate Revocation list for removing 'user certificates.'
openssl ca -gencrl -out /etc/ssl/private/ca.crl -crldays 7

  拷贝以上代码上不要多加回车
执行 sh new_ca.sh 生成新的CA证书, 这里我是一路回车,在让你输入密码时,才输入
  
3)
生成服务器证书的脚本

new_server.sh:
# Create us a key. Don't bother putting a password on it since you will need it to start apache. If you have a better work around I'd love to hear it.
openssl genrsa -out private/server.key
# Take our key and create a Certificate Signing Request for it.
openssl req -new -key 
private/server.key -out private/server.csr
# Sign this bastard key with our bastard CA key.
openssl ca -in 
private/server.csr -cert private/ca.crt -keyfile private/ca.key -out private/server.crt

执行 sh new_server.sh 生成新服务器的证书, 这里我是一路回车,在让你输入密码时,才输入

4)
配置 nginx 的ssl支持

我的配置如下:

# HTTPS server
#
server {
listen   443;
server_name  localhost;

# 打开ssl
ssl  on;
# 上一步生成的服务器证书
ssl_certificate  /etc/ssl/private/server.crt;
# 服务器证书公钥
ssl_certificate_key  /etc/ssl/private/server.key;
# 客户端证书签名 也就是第二步生成的CA签名证书
ssl_client_certificate   /etc/ssl/private/ca.crt;
# ssl session 超时
ssl_session_timeout  5m;
# 打开SSL客户端校验 (双向证书检测)
ssl_verify_client on;

#ssl_protocols  SSLv2 SSLv3 TLSv1;
#ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers   on;

location / {
root   /var/www/nginx-default;
index  index.html index.htm;
}

启动你的nginx ,等待客户连接

5)
现在来生成客户端证书

新建 new_client.sh:
#!/bin/sh
# The base of where our SSL stuff lives.
base="/etc/ssl/private"
# Were we would like to store keys... in this case we take the username given to us and store everything there.
mkdir -p $base/users/$1/

# Let's create us a key for this user... yeah not sure why people want to use DES3 but at least let's make us a nice big key.
openssl genrsa -des3 -out $base/users/$1/$1.key 1024
# Create a Certificate Signing Request for said key.
openssl req -new -key $base/users/$1/$1.key -out $base/users/$1/$1.csr
# Sign the key with our CA's key and cert and create the user's certificate out of it.
openssl ca -in $base/users/$1/$1.csr -cert $base/ca.crt -keyfile $base/ca.key -out $base/users/$1/$1.crt

# This is the tricky bit... convert the certificate into a form that most browsers will understand PKCS12 to be specific.
# The export password is the password used for the browser to extract the bits it needs and insert the key into the user's keychain.
# Take the same precaution with the export password that would take with any other password based authentication scheme.
openssl pkcs12 -export -clcerts -in $base/users/$1/$1.crt -inkey $base/users/$1/$1.key -out $base/users/$1/$1.p12

  执行之前需要把/etc/ssl/private/index.txt 文件删除,重新建立一个新的,
  即 rm -f index.txt,  touch index.txt 否则以下命令会失败. 每次创建新客户端时都要这样做
执行 sh new_client.sh yourname 来生成一个 yourname 的client证书
按照提示一步一步来,这里要注意的是客户证书的几个项目要和根证书匹配
也就是第一步时配置的:  如果你之前一路回车的话,就不需要担心这个
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = match


不一致的话无法生成最后的客户证书
  6)
发送上一步生成的 yourname.p12 到客户端。
IE下双击安装就可以导入。
FireFox安装 :
Go into preferences.
Advanced.
View Certificates.
Import.
Enter master password for FireFox (if you don't have one set one here otherwise stolen laptop = easy access).
Enter in the export password given to you by the dude who created your cert.
Hit OK like a mad man.

打开网站会弹出对话框来要求你选择使用哪个证书,选择刚才安装的证书。选择接受服务器证书。现在你可以正常访问服务器拉。如果没弄对的话就会出现400 Bad request certification的错误
  利用 SOAPUI 进行WEB-SERVICE测试
  把P12文件转换成 jks文件
  keytool -importkeystore -srckeystore keystore.p12 -srcstoretype PKCS12 -deststoretype JKS -destkeystore keystore.jks
  接着在preferrence-ssl settings- keystore browse and keystore password and selected client authenticated

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-319072-1-1.html 上篇帖子: nginx+tomcat+session共享 . 下篇帖子: hive导入nginx日志
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表