#!/bin/sh
#
# ssl 證書輸出的根目錄。
sslOutputRoot="/etc/apache_ssl"
if [ $# -eq 1 ]; then
sslOutputRoot=$1
fi
if [ ! -d ${sslOutputRoot} ]; then
mkdir -p ${sslOutputRoot}
fi
cd ${sslOutputRoot}
echo "開始創(chuàng)建CA根證書..."
#
# 創(chuàng)建CA根證書,稍后用來簽署用于服務(wù)器的證書。如果是通過商業(yè)性CA如
# Verisign 或 Thawte 簽署證書,則不需要自己來創(chuàng)建根證書,而是應(yīng)該
# 把后面生成的服務(wù)器 csr 文件內(nèi)容貼入一個web表格,支付簽署費用并
# 等待簽署的證書。關(guān)于商業(yè)性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根證書
# 根據(jù)提示填寫各個字段, 但注意 Common Name 最好是有效根域名(如 zeali.net ),
# 并且不能和后來服務(wù)器證書簽署請求文件中填寫的 Common Name 完全一樣,否則會
# 導(dǎo)致證書生成的時候出現(xiàn)
# error 18 at 0 depth lookup:self signed certificate 錯誤
openssl req -new -x509 -days 365 -key ca.key -out ca.crt
echo "CA根證書創(chuàng)建完畢。"
echo "開始生成服務(wù)器證書簽署文件及私鑰 ..."
#
# 生成服務(wù)器私鑰
openssl genrsa -des3 -out server.key 1024
# 生成服務(wù)器證書簽署請求文件, Common Name 最好填寫使用該證書的完整域名
# (比如: security.zeali.net )
openssl req -new -key server.key -out server.csr
ls -altrh ${sslOutputRoot}/server.*
echo "服務(wù)器證書簽署文件及私鑰生成完畢。"
echo "開始使用CA根證書簽署服務(wù)器證書簽署文件 ..."
#
# 簽署服務(wù)器證書,生成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 參數(shù).
# 當(dāng)前設(shè)置為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根證書簽署服務(wù)器證書簽署文件完畢。"
# 使用了 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 的權(quán)限,保證密鑰安全
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