http://www.blogjava.net/alwayscy/archive/2006/12/01/84852.html
這裡抄錄LDAP+OpenSSL集中認證配置一文的一部分:
公私密金鑰:公開金鑰可以唯一解密私密金鑰加密過的資料,反之亦然。以下用P指代公開金鑰,V指代私密金鑰。
SSL過程:需要兩對公私密金鑰(P1,V1),(P2,V2),假設通訊雙方是A和B,B是伺服器,A要確認和它通訊的是B:
A->B: hello
B->A: 用V2加密過的P1(即使用者認證,A就用P2解密出P1)
A->B: ok
B->A: 用V1加密的一段資訊
A->B: 用P1加密一個自動產生的K(用之前的P1解密成功這段資訊則認為B是可信的了)
B->A: 用K加密的資料(之後兩對密鑰功能結束,由K來加解密資料)
這裡,P2就是第3方的CA認證,由於非對稱式加密很慢,所以公私密金鑰只是用來保證K的傳送安全,之後通訊是用K的對稱式加密演算法來保證。
為什麼通過以上過程A就能夠確定肯定是B,而不是某個C在假裝B了呢?因為這個過程中,B用V1加密過一段資訊發給A,A也成功解開了。我們開頭談到公開金鑰(P1)只可以唯一解密私密金鑰(V1)加密過的資訊,這樣A就可以完全相信B是擁有V1的,而V1是嚴格保密,只被服務提供公司擁有,所以保證了通訊的服務方正確性。
這裡(P2,V2)就是certificate authority (CA)用來給客戶簽名用的公私密金鑰。
(P1,V1)是客戶自己的公私密金鑰,提交給CA,CA所做的事情就是用(P2,V2)來給客戶的(P1,V1)簽名,簡單吧?
V2是CA公司要保密的,而P2就是公用CA認證。用V2加密過(簽名過)的P1,稱為使用者認證,一般被安裝在伺服器端。
下面我們OpenSSL來做這一整件事情。
先產生CA的公私密金鑰(Root Certificate )
準備工作
mkdir CA
cd CA
mkdir newcerts private
echo '01' > serial
touch index.txt
組建組態檔案。由於openssl命令列參數太多,所以就用檔案來組織各種選項。
其中,req_distinguished_name 節表示需要提示使用者輸入的資訊。
v3_ca是有關CA公私密金鑰產生的,v3_req是有關使用者認證產生的。
ca_default是用CA公私密金鑰簽名的時候,使用者認證的預設資訊。
vi ./openssl.cnfdir = .
[ req ]
default_bits = 1024 # Size of keys
default_keyfile = key.pem # name of generated keys
default_md = md5 # message digest algorithm
string_mask = nombstr # permitted characters
distinguished_name = req_distinguished_name
req_extensions = v3_req
[ req_distinguished_name ]
# Variable name Prompt string
#---------------------- ----------------------------------
0.organizationName = Organization Name (company)
organizationalUnitName = Organizational Unit Name (department, division)
emailAddress = Email Address
emailAddress_max = 40
localityName = Locality Name (city, district)
stateOrProvinceName = State or Province Name (full name)
countryName = Country Name (2 letter code)
countryName_min = 2
countryName_max = 2
commonName = Common Name (hostname, IP, or your name)
commonName_max = 64
# Default values for the above, for consistency and less typing.
# Variable name Value
#------------------------------ ------------------------------
0.organizationName_default = EB Company
localityName_default = Shen Zhen
stateOrProvinceName_default = Guan Dong
countryName_default = CN
[ v3_ca ]
basicConstraints = CA:TRUE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
[ v3_req ]
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
[ ca ]
default_ca = CA_default
[ CA_default ]
serial = $dir/serial
database = $dir/index.txt
new_certs_dir = $dir/newcerts
certificate = $dir/cacert.pem
private_key = $dir/private/cakey.pem
default_days = 365
default_md = md5
preserve = no
email_in_dn = no
nameopt = default_ca
certopt = default_ca
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
產生CA公私密金鑰:openssl req -new -x509 -extensions v3_ca -keyout private/cakey.pem -out cacert.pem -days 3650 -config ./openssl.cnf
會提示輸入密碼,當用它給使用者認證簽名時需要輸入,以避免其它人用它隨意產生使用者認證。
-days表示有效期間,因為它是根憑證,所以時間一定要很長,否則由它產生的使用者認證容易到期。
這時就產生了:
P1
cacert.pem
V1
private/cakey.pem
查看資訊用:
openssl x509 -in cacert.pem -noout -text
產生P2,V2,即Certificate Signing Request (CSR)
執行:
openssl req -new -nodes -out req.pem -config ./openssl.cnf
這樣就產生了:
P2
req.pem
V2
key.pem
用此命令查看:
openssl req -in req.pem -text -verify -noout
用CA的私密金鑰V1為P2簽名,即產生使用者認證
執行:
openssl ca -out cert.pem -config ./openssl.cnf -infiles req.pem
產生使用者認證:
cert.pem
此時,會拷貝一份到newcerts目錄下。並會更新資料庫檔案:index.txt以及serail檔案
用命令查看:
openssl x509 -in cert.pem -noout -text -purpose | more
如果要去除可讀資訊部分,執行:
mv cert.pem tmp.pem
openssl x509 -in tmp.pem -out cert.pem
安裝認證
key.pem(V2)和cert.pem(用V1加密過的P2)安裝到服務端
有的伺服器需要把這兩個檔案連為一個,可以執行:
cat key.pem cert.pem >key-cert.pem
cacert.pem安裝到用戶端
Apache的配置:
File Comment
/home/httpd/html Apache DocumentRoot
/home/httpd/ssl SSL-related files
/home/httpd/ssl/cert.pem Site certificate
/home/httpd/ssl/key.pem Site private key
Stunnel的配置
stunnel -p /etc/ssl/certs/key-cert.pem
編輯於08.4.26,另有兩個例子:
用OpenSSL與JAVA(JSSE)通訊
Perl與Java的SSL通訊樣本
有興趣可以訪問下我的生活部落格:qqmovie.qzone.com
使用OpenSSL建立根CA及自我簽署憑證製作過程 [轉載]