dinggela 发表于 2018-11-19 09:51:11

Apache 使用CA证书实现https配置详解

  https会话建立过程
  1、client发起三次握手请求
  2、完成tcp/ip三次握手
  3、client和server协商使用的加密算法
  4、协商完成后,server发送自己的证书给client
  5、client验证server端证书,验证完成后生成一个对称秘钥发送给服务器端
  6、client发送请求页面给server端,server通过client发送的秘钥加密后回送给client
  证书生成过程
  1、client和server端验证对方证书都是通过第三方颁发机构CA来验证。
  2、CA给server端下发证书
  3、客户端信任CA机构,把CA的验证证书放在自己家中,来验证对方的证书
  4、服务器端生成一对秘钥,把公钥发送给CA。CA签署生成证书,回送给server
  5、server配置自己的服务器以使用证书,在客户端请求时把证书发送给client。
  6、client使用保存在自己的证书来验证server端。最终完成ssl会话。
  注:ssl会话是不可以通过主机名来区分的,仅能支持IP地址来进行。也就是说如果服务器配置多个虚拟主机,只可配置一个虚拟主机来支持
  实验拓扑
CA:192.168.11.101  web服务器:192.168.11.100
  client:192.168.11.7
  实验过程
  1、要使http支持ssl功能,需要先安装mod_sll功能(192.168.11.100)
yum -y install mod_ssl            #主配置文件在/etc/httpd/conf.d/ssl.conf  2、生成CA字签证书(192.168.11.101)
cd /etc/pki/CA  #生成秘钥
  (umask 077; openssl genrsa -out private/cakey.pem 2048)      #使用genrsa加密算法,-out表示存放路径,2048表示加密位数,umask 077表示秘钥权限
  编辑生成证书的默认信息
vim /etc/pki/tls/openssl.cnf       #配置文件  countryName_default             = CN            #国家
  stateOrProvinceName_default   = HeNan             #省份
  localityName_default            = zhengzhou            #城市
  0.organizationName_default      = yuliang             #公司
  organizationalUnitName_default= Tec            #部门
  生成自签证书
#openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655  (-new表示新创建证书,-x509表示使用的协议,-days 3655表示证书有效期)
  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) :      #国家
  State or Province Name (full name) :       #省份
  Locality Name (eg, city) :         #城市
  Organization Name (eg, company) :      #公司
  Organizational Unit Name (eg, section) :          #部门
  Common Name (eg, your name or your server's hostname) []:ca.yuliang.com      服务器名称
  Email Address []:admin@yuliang.com         #联系邮箱
  修改CA服务器证书路径
[ CA_default ]  dir                = /etc/pki/CA             # 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.
  #unique_subject = no                     # Set to 'no' to allow creation of
  # 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
  x509_extensions = usr_cert               # The extentions to add to the cert
  根据配置中的路径,创建所需文件
mkdir certs crl newcerts  touch index.txt
  echo 01 > serial          #01表示证书颁发序列号从01开始
  至此,CA准备完成。如果有web server需要申请证书,只需要自己生成一对密钥,并且把申请发送给CA,发出证书申请请求到CA服务器上,由CA来做签署。
  3、在web server中生成密钥(192.168.11.100)
cd /etc/httpd/  mkdir ssl && cd ssl
  (umask 077; openssl genrsa 1024 >httpd.key)    #生成密钥,>和上面的-out用法一样
  在web server中生成证书请求
openssl req -new -key httpd.key -out httpd.csr  依次输入国家,城市,公司等信息。注意要与CA保持一致
  ps:需注意Common Name项一定要填写最终让用户访问ssl的域名
  生成完成后将证书申请请求发送到CA服务器
scp httpd.csr 192.168.11.101:/tmp  4、CA服务器签署web server证书请求(192.168.11.101)
openssl ca -in /tmp/httpd.csr -out /tmp/httpd.crt -days 3650  #生成名叫httpd.crt的证书,证书有效期为10年
  生成完成后将证书发送会web server(这里从web server直接拷会)
scp 192.168.11.101:/tmp/httpd.crt /etc/httpd/ssl/  注意:在证书申请完成后一定要把/tmp目录下的临时文件删除
  5、配置web server可以使用证书(192.168.11.100)
vim /etc/httpd/conf.d/ssl.conf   (注意先备份)  Listen 443    #监听端口
  AddType application/x-x509-ca-cert .crt          #支持的证书类型
  AddType application/x-pkcs7-crl .crl          #证书吊销列表
  SSLSessionCache         shmcb:/var/cache/mod_ssl/scache(512000)      #ssl缓存机制
  SSLSessionCacheTimeout300            #缓存清空时间
     #_default_表示使用本机默认地址,如果本机有多个地址,需要将_default_改为指定地址
  ServerName
www.margan.com      #还需增加指定服务名称  DocumentRoot "/www/html/"   #指定服务目录,需注意要和web保持一致
  ErrorLog/logs/ssl_errot_log      #错误日志保存目录
  TransferLog/log/ssl_access_log   #指定访问日志保存目录
  LogLevel    #日志级别
  SSLEngineon   #表示启用ssl
  SSLProtocol all -SSLv2      #指定ssl可用协议,但不支持SSLv2
  SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW    #指定加密机制
  SSLCertificateFile /etc/httpd/ssl/httpd.crt      #指定证书路径
  SSLCertificateKeyFile /etc/httpd/ssl/http.key       #指定web server私钥
  至此配置结束,检测配置语法,并重启服务器
httpd -t  service httpd restart
  在客户端修改hosts文件,即可通过https://www.morgan.com访问
  但访问时会提示证书不可信
  可将服务器证书下载下来,并改为cacert.crt文件,并放入到可信任的根证书颁发机构。
  注:如果web server不小心丢失或者损坏了CA签署的证书,再次申请会报错
failed to update database  TXT_DB error number 2
  解决办法是:
删除/etc/pki/CA下的index.txt文件  然后touch index.txt

页: [1]
查看完整版本: Apache 使用CA证书实现https配置详解