日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于OpenSSL自建CA和颁发SSL证书

發(fā)布時(shí)間:2025/5/22 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于OpenSSL自建CA和颁发SSL证书 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

關(guān)于SSL/TLS介紹見文章?SSL/TLS原理詳解

關(guān)于證書授權(quán)中心CA以及數(shù)字證書等概念,請(qǐng)移步?OpenSSL 與 SSL 數(shù)字證書概念貼?。

openssl是一個(gè)開源程序的套件、這個(gè)套件有三個(gè)部分組成:一是libcryto,這是一個(gè)具有通用功能的加密庫,里面實(shí)現(xiàn)了眾多的加密庫;二是libssl,這個(gè)是實(shí)現(xiàn)ssl機(jī)制的,它是用于實(shí)現(xiàn)TLS/SSL的功能;三是openssl,是個(gè)多功能命令行工具,它可以實(shí)現(xiàn)加密解密,甚至還可以當(dāng)CA來用,可以讓你創(chuàng)建證書、吊銷證書。

默認(rèn)情況ubuntu和CentOS上都已安裝好openssl。CentOS 6.x 上有關(guān)ssl證書的目錄結(jié)構(gòu):

1 2 3 4 5 6 7 8 9 10 11 /etc/pki/CA/ newcerts 存放CA簽署(頒發(fā))過的數(shù)字證書(證書備份目錄) private 用于存放CA的私鑰 crl 吊銷的證書 /etc/pki/tls/ cert.pem 軟鏈接到certs/ca-bundle.crt certs/ 該服務(wù)器上的證書存放目錄,可以房子自己的證書和內(nèi)置證書 ca-bundle.crt 內(nèi)置信任的證書 private 證書密鑰存放目錄 openssl.cnf openssl的CA主配置文件

?

1. 頒發(fā)證書

1.1 修改CA的一些配置文件

CA要給別人頒發(fā)證書,首先自己得有一個(gè)作為根證書,我們得在一切工作之前修改好CA的配置文件、序列號(hào)、索引等等。

vi /etc/pki/tls/openssl.cnf

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 ... [ 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 ... default_days = 3650 # how long to certify for ... # For the CA policy [ policy_match ] countryName = match stateOrProvinceName = optional localityName = optional organizationName = optional organizationalUnitName = optional commonName = supplied emailAddress = optional ... [ req_distinguished_name ] countryName = Country Name (2 letter code) countryName_default = CN countryName_min = 2 countryName_max = 2 stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = GD ... [ req_distinguished_name ] 部分主要是頒證時(shí)一些默認(rèn)的值,可以不動(dòng)

?

一定要注意[ policy_match ]中的設(shè)定的匹配規(guī)則,是有可能因?yàn)樽C書使用的工具不一樣,導(dǎo)致即使設(shè)置了csr中看起來有相同的countryName,stateOrProvinceName等,但在最終生成證書時(shí)依然報(bào)錯(cuò):

1 2 3 4 5 Using configuration from /usr/lib/ssl/openssl.cnf Check that the request matches the signature Signature ok The stateOrProvinceName field needed to be the same in the CA certificate (GuangDong) and the request (GuangDong)

?

touch index.txt serial
在CA目錄下創(chuàng)建兩個(gè)初始文件:

1 2 # touch index.txt serial # echo 01 > serial

?

1.2 生成根密鑰

1 2 # cd /etc/pki/CA/ # openssl genrsa -out private/cakey.pem 2048

為了安全起見,修改cakey.pem私鑰文件權(quán)限為600或400,也可以使用子shell生成( umask 077; openssl genrsa -out private/cakey.pem 2048 ),下面不再重復(fù)。

1.3 生成根證書

使用req命令生成自簽證書:

1 # openssl req -new -x509 -key private/cakey.pem -out cacert.pem

?

會(huì)提示輸入一些內(nèi)容,因?yàn)槭撬接械?#xff0c;所以可以隨便輸入(之前修改的openssl.cnf會(huì)在這里呈現(xiàn)),最好記住能與后面保持一致。上面的自簽證書cacert.pem應(yīng)該生成在/etc/pki/CA下。

1.4 為我們的nginx web服務(wù)器生成ssl密鑰

以上都是在CA服務(wù)器上做的操作,而且只需進(jìn)行一次,現(xiàn)在轉(zhuǎn)到nginx服務(wù)器上執(zhí)行:

1 2 # cd /etc/nginx/ssl # openssl genrsa -out nginx.key 2048

?

這里測(cè)試的時(shí)候CA中心與要申請(qǐng)證書的服務(wù)器是同一個(gè)。

1.5 為nginx生成證書簽署請(qǐng)求

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # openssl req -new -key nginx.key -out nginx.csr ... Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:GD Locality Name (eg, city) []:SZ Organization Name (eg, company) [Internet Widgits Pty Ltd]:COMPANY Organizational Unit Name (eg, section) []:IT_SECTION Common Name (e.g. server FQDN or YOUR name) []:your.domain.com Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: ...

同樣會(huì)提示輸入一些內(nèi)容,其它隨便,除了Commone Name一定要是你要授予證書的服務(wù)器域名或主機(jī)名,challenge password不填。

1.6 私有CA根據(jù)請(qǐng)求來簽署證書

接下來要把上一步生成的證書請(qǐng)求csr文件,發(fā)到CA服務(wù)器上,在CA上執(zhí)行:

1 2 3 4 # openssl ca -in nginx.csr -out nginx.crt 另外在極少數(shù)情況下,上面的命令生成的證書不能識(shí)別,試試下面的命令: # openssl x509 -req -in server.csr -CA /etc/pki/CA/cacert.pem -CAkey /etc/pki/CA/private/cakey.pem -CAcreateserial -out server.crt

?

上面簽發(fā)過程其實(shí)默認(rèn)使用了-cert cacert.pem -keyfile cakey.pem,這兩個(gè)文件就是前兩步生成的位于/etc/pki/CA下的根密鑰和根證書。將生成的crt證書發(fā)回nginx服務(wù)器使用。

到此我們已經(jīng)擁有了建立ssl安全連接所需要的所有文件,并且服務(wù)器的crt和key都位于配置的目錄下,剩下的是如何使用證書的問題。

2. 使用ssl證書

2.1 一般瀏覽器

瀏覽器作為客戶端去訪問https加密的服務(wù)器,一般不用去手動(dòng)做其他設(shè)置,如https://www.google.com.hk,這是因?yàn)镃hrome、FireFox、Safari、IE等瀏覽器已經(jīng)內(nèi)置了大部分常用的CA的根證書,但自建CA的根證書就不再瀏覽器的信任列表中,訪問時(shí)會(huì)提示如下:
IE瀏覽器

谷歌瀏覽器

安裝網(wǎng)站證書后(同時(shí)也有信任的根證書),地址欄一般會(huì)顯示綠色小鎖

證書信息

導(dǎo)入證書到瀏覽器的方法:http://cnzhx.net/blog/self-signed-certificate-as-trusted-root-ca-in-windows/

2.2 為linux系統(tǒng)添加根證書

這一步不是必須的,一般出現(xiàn)在開發(fā)測(cè)試環(huán)境中,而且具體的應(yīng)用程序應(yīng)該提供添加證書的方法。

curl工具可以在linux上模擬發(fā)送請(qǐng)求,但當(dāng)它去訪問https加密網(wǎng)站時(shí)就會(huì)提示如下信息:

1 2 3 4 5 6 7 8 9 10 11 12 13 # curl https://sean:sean@registry.domain.com:8000/ curl: (60) Peer certificate cannot be authenticated with known CA certificates More details here: http://curl.haxx.se/docs/sslcerts.html curl performs SSL certificate verification by default, using a "bundle" of Certificate Authority (CA) public keys (CA certs). If the default bundle file isn't adequate, you can specify an alternate file using the --cacert option. If this HTTPS server uses a certificate signed by a CA represented in the bundle, the certificate verification probably failed due to a problem with the certificate (it might be expired, or the name might not match the domain name in the URL). If you'd like to turn off curl's verification of the certificate, use the -k (or --insecure) option.

?

提示上面的信息說明curl在linux的證書信任集里沒有找到根證書,你可以使用curl --insecure來不驗(yàn)證證書的可靠性,這只能保證數(shù)據(jù)是加密傳輸?shù)牡珶o法保證對(duì)方是我們要訪問的服務(wù)。使用curl --cacert cacert.pem可以手動(dòng)指定根證書路徑。我們也可以把根證書添加到系統(tǒng)(CentOS 5,6)默認(rèn)的bundle:

1 2 3 4 5 # cp /etc/pki/tls/certs/ca-bundle.crt{,.bak} 備份以防出錯(cuò) # cat /etc/pki/CA/cacert.pem >> /etc/pki/tls/certs/ca-bundle.crt # curl https://sean:sean@registry.domain.com:8000 "docker-registry server (dev) (v0.8.1)"

?

2.3 nginx

在nginx配置文件(可能是/etc/nginx/sites-available/default)的server指令下添加:

1 2 3 ssl on; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key;

?

同時(shí)注意 server_name 與證書申請(qǐng)時(shí)的 Common Name 要相同,打開443端口。當(dāng)然關(guān)于web服務(wù)器加密還有其他配置內(nèi)容,如只對(duì)部分URL加密,對(duì)URL重定向?qū)崿F(xiàn)強(qiáng)制https訪問,請(qǐng)參考其他資料。

3 關(guān)于證書申請(qǐng)

注意,如果對(duì)于一般的應(yīng)用,管理員只需生成“證書請(qǐng)求”(后綴大多為.csr),它包含你的名字和公鑰,然后把這份請(qǐng)求交給諸如verisign等有CA服務(wù)公司(當(dāng)然,連同幾百美金),你的證書請(qǐng)求經(jīng)驗(yàn)證后,CA用它的私鑰簽名,形成正式的證書發(fā)還給你。管理員再在web server上導(dǎo)入這個(gè)證書就行了。如果你不想花那筆錢,或者想了解一下原理,可以自己做CA。從ca的角度講,你需要CA的私鑰和公鑰。從想要證書的服務(wù)器角度將,需要把服務(wù)器的證書請(qǐng)求交給CA。

如果你要自己做CA,別忘了客戶端需要導(dǎo)入CA的證書(CA的證書是自簽名的,導(dǎo)入它意味著你“信任”這個(gè)CA簽署的證書)。而商業(yè)CA的一般不用,因?yàn)樗鼈円呀?jīng)內(nèi)置在你的瀏覽器中了。

參考

  • CentOS6.5下openssl加密解密及CA自簽頒發(fā)證書詳解
  • 基于 OpenSSL 的 CA 建立及證書簽發(fā)
  • openssl建立證書,非常詳細(xì)配置ssl+apache
  • The Secure Sockets Layer and Transport Layer Security
閱讀原文 openssl自簽證書腳本

轉(zhuǎn)載于:https://www.cnblogs.com/276815076/p/8184632.html

總結(jié)

以上是生活随笔為你收集整理的基于OpenSSL自建CA和颁发SSL证书的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。