php网站实现https加密访问,wamp下配置openssl

所做的项目基本上都是内部使用的且敏感的数据。因此项目都是基于lamp上使用了ssl双向认证访问CRM。

安装环境:Wampserver集成安装包;window7系统。openssl.exe所在集成包的位置为D:\wamp5.3\bin\apache\Apache2.2.21\bin。

步骤如下:

1.指明配置文件。

 
 
  1. set OPENSSL_CONF=../conf/openssl.cnf

2.进入上面的目录,生成需要的私钥key

生成私钥文件

 
 
  1. openssl genrsa > root.key   // 生成根密钥
  2. openssl genrsa > server.key  // 生成服务端密钥
  3. openssl genrsa > client.key  // 生成客户端密钥

3.生成自签名的根证书

 
 
  1. openssl req -x509 -new -key root.key >root.crt

-new 生成一个新的文件 -key filename 参数filename指明我们的私有密钥文件名 -x509 将产生自签名的证书,一般用来测试用,或者自己玩下做个Root CA.证书的扩展在config文件里面指定 4.生成服务端,客户端签名请求文件

生成请求签名文件

 
 
  1. openssl req -new -key server.key -out server.csr
  2. openssl req -new -key client.key -out client.csr

按提示输入一系列的参数,

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

5.使用根证书为服务端及客户端签名

生成签名证书openssl的ca指令用途:模拟CA行为的工具,有了它,你就是一个CA,不过估计是nobody trusted CA。可以用来给各种格式的CSR签名,用来产生和维护CRL(证书回收列,详细记录了所有的作废证书),他还维护者一个文本数据库,记录了所有经手颁发 的证书及那些证书的状态(会在配置信息指定的目录生成pem文件)。

 
 
  1. openssl ca -in server.csr -cert root.crt -keyfile root.key -out server.crt
  2. openssl ca -in client.csr -cert root.crt -keyfile root.key -out client.crt

-in filename要签名的csr文件 -cert CA本身的证书文件名 -keyfile CA自己的私有密钥文件 -out filename签名后的证书文件名。证书的细节也会给写进去

这里返回了一个ERROR,如下描述:

 
 
  1. Using configuration from ../conf/openssl.cnf
  2. Error opening CA private key ./demoCa/private/cakey.pem
  3. 3528:error:02001003:system library:fopen:No such proccess:.\crypto\bio\bss_file.c:352:fopen('./demoCa/private/cakey.pem','rb')
  4. 3528:error:20074002:BIO routines:FILE_CTRL:system lib:\crypto\bio\bss_file.c:354:
  5. unable to load CA private key

提示缺少./demoCa/private/cakey.pem,进入../conf/openssl.cnf查看配置,缺少很多文件。并谷歌查看 具体错误,看别人如何获取生成cakey.pem这个文件。具体可以查看这个地址http://kamailio.org/docs/modules /stable/modules/tls.html解决错误的信息如下:

 
 
  1. Creating CA certificate
  2. -----------------------
  3. 1. create CA dir
  4. mkdir ca
  5. cd ca
  6.  
  7. 2. create ca dir structure and files  (see ca(1))
  8. mkdir demoCA #default CA name, edit /etc/ss/openssl.cnf
  9. mkdir  demoCA/private
  10. mkdir demoCA/newcerts
  11. touch demoCA/index.txt
  12. echo 01 >demoCA/serial
  13. echo 01 >demoCA/crlnumber
  14.  
  15. 2. create CA private key
  16. openssl genrsa -out demoCA/private/cakey.pem 2048
  17. chmod 600 demoCA/private/cakey.pem
  18.  
  19. 3. create CA self-signed certificate
  20. openssl req -out demoCA/cacert.pem   -x509 -new -key demoCA/private/cakey.pem

6.根据上面的说明信息,配置openssl.cnf,创建文件路径,生成所需cakey.pem(CA的key文件)和cacert.pem(CA的crt文件)文件

 
 
  1. openssl genrsa -out demoCA/private/cakey.pem 2048
  2. openssl req -out demoCA/cacert.pem -x509 -new -key demoCA/private/cakey.pem

手动在当前目录下创建demoCA/newcerts/,demoCA/private/这两个文件。配置openssl.cnf下面几行内容 openssl.cnf 内容(去除注释后)(只贴出有可能修改过的地方)

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

7.再次执行步骤5的操作,这次执行通过。如需查看文件的信息,可以使用如下命令 查看证书命令

 
 
  1. openssl x509 -noout -text -in server.crt

8.将客户端证书转成pfx格式,浏览器安装 生成pfx文件

 
 
  1. openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.pfx

到此openssl相关的证书文件就结束了。

下面接着配置apache相关的文件

1.打开apache配置文件httpd.conf,查找#LoadModule sslmodule modules/modssl.so和#Include conf/extra/httpd-ssl.conf,去掉前面的#;

2.打开httpd-ssl.conf配置一下信息

 
 
  1. SSLSessionCache        "shmcb:c:/Apache2/logs/ssl_scache(512000)"
  2. SSLMutex default
  3. <VirtualHost _default_:443>
  4. DocumentRoot "D:/wamp5.3/www"
  5. ServerName www.laoniangke.com:443
  6. ServerAdmin php@admin.com
  7. ErrorLog "D:/wamp5.3/logs/error.log"
  8. TransferLog "D:/wamp5.3/logs/access.log"
  9.  
  10. SSLEngine on
  11. SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
  12. SSLCertificateFile "D:/wamp5.3/bin/apache/Apache2.2.21/bin/server.crt"
  13. SSLCertificateKeyFile "D:/wamp5.3/bin/apache/Apache2.2.21/bin/server.key"
  14.  
  15. #进行双向验证
  16. SSLCACertificateFile "D:/wamp5.3/bin/apache/Apache2.2.21/bin/root.crt"
  17. SSLVerifyClient require
  18. SSLVerifyDepth  10
  19. </VirtualHost>

以及一些apache的CustomLog与cgi_bin文件路径。重启apache就可以了

如果程序中需要获取客户端证书信息,需要配置SSLOptions。 crm组所有项目都是通过获取客户端证书的公钥去除第一行(-----BEGIN CERTIFICATE-----)和最后一行(-----END CERTIFICATE-----),并去除换行符"\n"。将最后的值进行MD5,再去数据库中查找证书信息是否存在,判断是否有权限进行登录。 这里涉及到获取证书的公钥信息,而默认的配置不会返回$SERVER['SSLCLIENT_CERT']。查看httpd-ssl.conf中 SSLOptions 指令ExportCertData中的这句"These contain the PEM-encoded X.509 Certificates of server and client for the current HTTPS connection and can be used by CGI scripts for deeper Certificate checking",大致意思是可以通过脚本获取证书的PEM-encoded X.509值 需要在SSLOptions中加入一个ExportCertData,即 SSLOptions +FakeBasicAuth -StrictRequire

 
 
  1. <FilesMatch ~ "\.(cgi|shtmlphtml|php)$">
  2. SSLOptions +StdEnvVars +ExportCertData
  3. </FilesMatch>

这样就可以在本地模拟apache+mysql+php+ssl进行开发,而无需每次都在本地开发之后必须提交到服务器进行测试(因为偶尔存在多人同时在修改同一文件发生覆盖的情况)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值