#!/bin/sh html
# git
# ssl 證書輸出的根目錄。 web
sslOutputRoot="/etc/apache_ssl" apache
if [ $# -eq 1 ]; then 安全
sslOutputRoot=$1 服務器
fi ide
if [ ! -d ${sslOutputRoot} ]; then ui
mkdir -p ${sslOutputRoot} spa
fi .net
cd ${sslOutputRoot}
echo "開始建立CA根證書..."
#
# 建立CA根證書,稍後用來簽署用於服務器的證書。若是是經過商業性CA如
# Verisign 或 Thawte 簽署證書,則不須要本身來建立根證書,而是應該
# 把後面生成的服務器 csr 文件內容貼入一個web表格,支付簽署費用並
# 等待簽署的證書。關於商業性CA的更多信息請參見:
# Verisign - http://digitalid.verisign.com/server/apacheNotice.htm
# Thawte Consulting - http://www.thawte.com/certs/server/request.html
# CertiSign Certificadora Digital Ltda. - http://www.certisign.com.br
# IKS GmbH - http://www.iks-jena.de/produkte/ca /
# Uptime Commerce Ltd. - http://www.uptimecommerce.com
# BelSign NV/SA - http://www.belsign.be
# 生成CA根證書私鑰
openssl genrsa -des3 -out ca.key 1024
# 生成CA根證書
# 根據提示填寫各個字段, 但注意 Common Name 最好是有效根域名(如 zeali.net ),
# 而且不能和後來服務器證書籤署請求文件中填寫的 Common Name 徹底同樣,不然會
# 致使證書生成的時候出現
# error 18 at 0 depth lookup:self signed certificate 錯誤
openssl req -new -x509 -days 365 -key ca.key -out ca.crt
echo "CA根證書建立完畢。"
echo "開始生成服務器證書籤署文件及私鑰 ..."
#
# 生成服務器私鑰
openssl genrsa -des3 -out server.key 1024
# 生成服務器證書籤署請求文件, Common Name 最好填寫使用該證書的完整域名
# (好比: security.zeali.net )
openssl req -new -key server.key -out server.csr
ls -altrh ${sslOutputRoot}/server.*
echo "服務器證書籤署文件及私鑰生成完畢。"
echo "開始使用CA根證書籤署服務器證書籤署文件 ..."
#
# 簽署服務器證書,生成server.crt文件
# 參見 http://www.faqs.org/docs/securing/chap24sec195.html
# sign.sh START
#
# Sign a SSL Certificate Request (CSR)
# Copyright (c) 1998-1999 Ralf S. Engelschall, All Rights Reserved.
#
CSR=server.csr
case $CSR in
*.csr ) CERT="`echo $CSR | sed -e 's/\.csr/.crt/'`" ;;
* ) CERT="$CSR.crt" ;;
esac
# make sure environment exists
if [ ! -d ca.db.certs ]; then
mkdir ca.db.certs
fi
if [ ! -f ca.db.serial ]; then
echo '01' >ca.db.serial
fi
if [ ! -f ca.db.index ]; then
cp /dev/null ca.db.index
fi
# create an own SSLeay config
# 若是須要×××的有效期限,請修改下面的 default_days 參數.
# 當前設置爲10年.
cat >ca.config <<EOT
[ ca ]
default_ca = CA_own
[ CA_own ]
dir = .
certs = ./certs
new_certs_dir = ./ca.db.certs
database = ./ca.db.index
serial = ./ca.db.serial
RANDFILE = ./ca.db.rand
certificate = ./ca.crt
private_key = ./ca.key
default_days = 3650
default_crl_days = 30
default_md = md5
preserve = no
policy = policy_anything
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
EOT
# sign the certificate
echo "CA signing: $CSR -> $CERT:"
openssl ca -config ca.config -out $CERT -infiles $CSR
echo "CA verifying: $CERT <-> CA cert"
openssl verify -CAfile ./certs/ca.crt $CERT
# cleanup after SSLeay
rm -f ca.config
rm -f ca.db.serial.old
rm -f ca.db.index.old
# sign.sh END
echo "使用CA根證書籤署服務器證書籤署文件完畢。"
# 使用了 ssl 以後,每次啓動 apache 都要求輸入 server.key 的口令,
# 你能夠經過下面的方法去掉口令輸入(若是不但願去掉請註釋如下幾行代碼):
echo "去除 apache 啓動時必須手工輸入密鑰密碼的限制:"
cp -f server.key server.key.org
openssl rsa -in server.key.org -out server.key
echo "去除完畢。"
# 修改 server.key 的權限,保證密鑰安全
chmod 400 server.key
echo "Now u can configure apache ssl with following:"
echo -e "\tSSLCertificateFile ${sslOutputRoot}/server.crt"
echo -e "\tSSLCertificateKeyFile ${sslOutputRoot}/server.key"
# die gracefully
exit 0