windows下使用openssl產生認證

來源:互聯網
上載者:User

標籤:des   style   blog   code   tar   ext   

一:下載

使用的是0.9.8

1:openssl下載,http://www.openssl.org/source/

2:安裝vs2010,並安裝

3:下載perl,http://www.activestate.com/ActivePerl,並安裝。

二:安裝openssl

1:解壓到系統硬碟C:\openssl-0.9.8v

2、配置WIN32環境

開啟CMD命令列,進入C:\openssl-0.9.8v目錄,執行命令

perl Configure VC-WIN32

注意區分大小寫

3、進入VC BIN目錄,配置VC環境變數

進入VS安裝路徑VC/Bin目錄下,運行:

VCVARS32.BAT

設定環境變數。

4、返回OpenSSL目錄,建立makefile檔案

ms\do_ms

該命令不執行組合語言編譯,如報告文末錯誤,可以嘗試ms\do_masm(使用組合語言)、ms\do_nasm、ms\do_nt等,這幾個設定檔是針對不同的系統配置寫的批處理。

5、在Openssl目錄下,執行編譯

nmake -f ms\ntdll.mak

最終編譯動態庫完成後,輸出都在out32dll目錄下:包括可執行檔、兩個dll 和兩個lib檔案: libeay32.dll, libeay32.lib, ssleay32.dll, ssleay32.lib,如果使用VS/VC編程只需按照下文的方法進行即可,如果需要使用openssl命令,還需要在系統內容變數path中增加C:\openssl-0.9.8v\out32dll路徑,因為openssl.exe就在該目錄下,聲明後可以直接在命令列中使用openssl命令。

三:產生認證

1、添加設定檔(openssl.cnf)的環境變數:OPENSSL_CONF。設定檔可從OpenSSL解壓後根目錄下的apps目錄下拷貝,再自行修改配置。也可以在openssl命令中用-config指定設定檔的位置。

我的設定檔:

#
# SSLeay example properties file.
# This is mostly being used for generation of certificate requests.
#

RANDFILE = .rnd

####################################################################
[ ca ]
default_ca = CA_default # The default ca section

####################################################################
[ CA_default ]

dir = C:\\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.
new_certs_dir = $dir\\newcerts # default place for new certs.

certificate = $dir\\cacert.pem # The CA certificate
serial = $dir\\serial # The current serial number
crl = $dir\\crl.pem # The current CRL
private_key = $dir\\private\\cakey.pem # The private key
RANDFILE = $dir\\private\\private.rnd # private random number file

x509_extensions = x509v3_extensions # The extentions to add to the cert
default_days = 365 # how long to certify for
default_crl_days = 30 # how long before next CRL
default_md = md5 # which md to use.
preserve = no # keep passed DN ordering

# A few difference way of specifying how similar the request should look
# For type CA, the listed attributes must be the same, and the optional
# and supplied fields are just that :-)
policy = policy_match

# For the CA policy
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional

# For the ’anything’ policy
# At this point in time, you must list all acceptable ’object’
# types.
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional

####################################################################
[ req ]
default_bits = 1024
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes

[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_min = 2
countryName_max = 2

stateOrProvinceName = State or Province Name (full name)

localityName = Locality Name (eg, city)

0.organizationName = Organization Name (eg, company)

organizationalUnitName = Organizational Unit Name (eg, section)

commonName = Common Name (eg, your website’s domain name)
commonName_max = 64

emailAddress = Email Address
emailAddress_max = 40

[ req_attributes ]
challengePassword = A challenge password
challengePassword_min = 4
challengePassword_max = 20

[ x509v3_extensions ]

<完>

 

$dir下建立一系列目錄和檔案:

現在可以使用openssl命令來產生認證了:

1.首先要產生伺服器端的私密金鑰(key檔案):
openssl genrsa -des3 -out server.key 1024
運行時會提示輸入密碼,此密碼用於加密key檔案(參數des3便是指密碼編譯演算法,當然也可以選用其他你認為安全的演算法.),以後每當需讀取此檔案(通過openssl提供的命令或API)都需輸入口令.如果覺得不方便,也可以去除這個口令,但一定要採取其他的保護措施!
去除key檔案口令的命令:
openssl rsa -in server.key -out server.key

2.openssl req -new -key server.key -out server.csr -config openssl.cnf
產生Certificate Signing Request(CSR),產生的csr檔案交給CA簽名後形成服務端自己的認證.螢幕上將有提示,依照其指示一步一步輸入要求的個人資訊即可.

3.對用戶端也作同樣的命令產生key及csr檔案:
openssl genrsa -des3 -out client.key 1024
openssl req -new -key client.key -out client.csr -config openssl.cnf

4.CSR檔案必須有CA的簽名才可形成認證.可將此檔案發送到verisign等地方由它驗證,要交一大筆錢.自己做CA.
openssl req -new -x509 -keyout ca.key -out ca.crt -config openssl.cnf

5.用產生的CA的認證為剛才產生的server.csr,client.csr檔案簽名:
Openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key -config openssl.cnf
Openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key -config openssl.cnf

client使用的檔案有:ca.crt,client.crt,client.key
server使用的檔案有:ca.crt,server.crt,server.key
.crt檔案和.key可以合到一個檔案裡面,本人把2個檔案合成了一個.pem檔案(直接拷貝過去就行了)

 

openssl req 命令參數:

openssl req[-inform PEM|DER] [-outform PEM|DER] [-in filename] [-passin arg] [-out filename] [-passout arg] [-text] [-pubkey] [-noout] [-verify] [-modulus] [-new] [-rand file(s)] [-newkey rsa:bits] [-newkey alg:file] [-nodes] [-key filename] [-keyform PEM|DER] [-keyout filename] [-keygen_engine id] [-[digest]] [-config filename] [-subj arg] [-multivalue-rdn] [-x509] [-days n] [-set_serial n] [-asn1-kludge] [-no-asn1-kludge] [-newhdr] [-extensions section] [-reqexts section] [-utf8] [-nameopt] [-reqopt] [-subject] [-subj arg] [-batch] [-verbose] [-engine id]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.