網絡安全(3)之製作數字證書

 利用linux上的openssl命令製作證書

       一張表格說明openssl常用命令(可以先看後面,再看本節)

對象 用途 針對證書申請者命令 針對CA(證書頒發者)的命令
私鑰 生成私鑰 openssl genrsa -out Test.key 2048 openssl genrsa -out RootCA.key 2048
  查看私鑰 cat Test.key  
CSR 生成CSR openssl req -new -key Test.key -out Test.csr openssl req -new -key RootCA.key -out RootCA.csr
  查看CSR openssl req -in Test.csr -text  
證書 生成自簽發證書 N/A openssl req -new -x509 -days 3650 -key RootCA.key -out RootCA.pem
  簽發CSR形成證書 N/A openssl ca -days 3650 -in Test.csr -cert RootCA.pem -keyfile RootCA.key -out Test.pem
  查看證書 openssl x509 -in Test.csr -text  
  證書格式轉換 openssl x509 -in Test.pem -inform PEM -outform DER -out Test.der  
公鑰 私鑰生成公鑰 openssl rsa -in Test.key -pubout -out Test-pub.key  
  CSR裏面提取公鑰 openssl req -in Test.pem -pubkey -out Test_pub.key  
  證書裏提取公鑰 openssl x509 -in Test.pem -pubkey -out Test-pub.key  
  查看公鑰 cat Test-pub.key  

 

1 證書製作準備

   證書製作流程   

    1.CA提前在本地生成一對公私鑰,根據公鑰生成自己的自簽發證書,ROOT CA的證書是自簽發的,

    2.申請者本地生成一對公私鑰, 根據公私鑰生成CSR文件(CSR文件裏包含了證書的公鑰,一般不包含私鑰),上傳CSR文件給CA,

    3.CA根據自己的證書,私鑰對申請者的CSR進行簽名產生申請者的證書文件,簽名時會用到簽名算法,其他設置.

    因此證書製作流程可以歸納爲: 私鑰 --> CSR文件 --> 證書, 至於公鑰都是放在CSR/證書裏面,所以沒有單獨拿出來.

   搭建自己的CA服務器

    CA服務器,一般也叫作證書服務器, Linux和Windows上都有相應的證書服務器軟件, Cisco iOS也可以用作CA.

    證書的申請可以離線申請,也可以在線申請. 離線申請就是申請者提交CSR文件給CA時,走帶外,例如複製粘貼. 在線申請則是申請者通過SCEP協議帶內發送CSR文件給CA.

    openssl不支持在線申請,Windows和Cisco iOS都支持在線申請.

    本文以Linux上的openssl來充當證書服務器,用來給證書申請者簽發證書.一般Linux默認安裝好了openssl程序.

    證書申請者

    證書申請者一般都是你需要用證書向別人證明自己身份的設備,比如web服務器, ssl vpn網關, AAA服務器,

    當然在SSL/TLS雙證場景和EAP-TLS認證時,客戶端需要向服務器提供證書,這時候客戶端也需要申請證書.

    本文也以Linux上的openssl做申請者,生成證書請求文件,即CSR文件.

    證書與非對稱加密算法(公鑰算法或者叫私鑰算法)

    從上面的證書中可以看到,每一張證書都包含了一個公鑰,也就是說證書和公鑰是一一對應關係,而公鑰和私鑰是一一對應關係,所以證書和私鑰也是一一對應關係.

    在製作證書時,可只生成私鑰,公鑰可不用生成,除非強烈要求.公鑰可以根據私鑰來生成,也可以從證書裏面提前出來.

    證書也可以根據公鑰類型分爲,RSA證書, DSA證書, ECDSA證書, ECC證書等.

    本文以RSA證書爲例, 講述證書製作過程.

    自簽發證書和第三方簽發證書的區別

    自簽發證書,顧名思義就是證書的擁有者和頒發者(CA)都是自己,第三方簽發證書的簽發者不是證書擁有者本身,而是第三方的CA.

    自簽發證書的好處是,隨時隨地可以簽發,但當用戶訪問自簽發證書的設備時,會不信任該證書,需要用戶干預是否信任本證書.當然用戶可以提前把設備的證書存放下來,放在自己的證書信任列表(CTL)裏面,這樣雖然不會每次彈出是否信任證書的窗口,但是每當用戶訪問一個攜帶證書的設備時,都需要提前存好設備的證書到CTL,如果用戶訪問的設備數量很大,那這證書存儲和查找也是一個很大的問題.

    第三方簽發證書的簽發機構一般是業內公認的證書頒發機構,一般的系統(Windows, Linux, Mac等等)都已經提前安裝好了這些機構(CA)的證書. 對於證書使用者而言,只需要向這些機構申請證書就好. 好處是這些知名的頒發機構數量不多,很容易存放在系統CTL裏面,每當訪問這些機構簽發的證書的設備時,系統不會彈出告警. 不好就是要花錢,申請證書,也可以說是買證書.

 

2 製作CA證書(自簽發證書, 故CA爲Root CA)

     在CA上生成私鑰

wenca@wenca-dell:/etc/ssl$ sudo openssl genrsa -out RootCA.key 2048
Generating RSA private key, 2048 bit long modulus
....................................................+++
..............................................+++
e is 65537 (0x010001)

     在CA上製作一張自簽發的證書

wenca@wenca-dell:/etc/ssl$ sudo openssl req -new -x509 -days 3650 -key RootCA.key -out RootCA.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:CN                   
State or Province Name (full name) [Shanghai]:Shanghai
Locality Name (eg, city) []:Shanghai
Organization Name (eg, company) [Cisco Corp Ltd]:Cisco
Organizational Unit Name (eg, section) [CRDC]:CRDC
Common Name (e.g. server FQDN or YOUR name) []:RootCA.cisco.com
Email Address []:

     查看生成的私鑰,證書文件

wenca@wenca-dell:/etc/ssl$ ll RootCA*
-rw------- 1 root root 1675 Apr  8 13:38 RootCA.key
-rw-r--r-- 1 root root 1338 Apr  8 13:45 RootCA.pem

 

3 申請者生成CSR文件

     在證書申請的設備上生成私鑰

wenca@wenca-dell:/etc/ssl$ sudo openssl genrsa -out Test.key 2048
Generating RSA private key, 2048 bit long modulus
................+++
..........+++
e is 65537 (0x010001)

     在證書申請的設備上生成一個CSR文件

wenca@wenca-dell:/etc/ssl$ sudo openssl req -new -key Test.key -out Test.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:CN
State or Province Name (full name) [Shanghai]:Shanghai
Locality Name (eg, city) []:Shanghai
Organization Name (eg, company) [Cisco Corp Ltd]:Cisco
Organizational Unit Name (eg, section) [CRDC]:CRDC
Common Name (e.g. server FQDN or YOUR name) []:Test.crdc.cisco.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

    查看生成的私鑰和CSR文件

wenca@wenca-dell:/etc/ssl$ ll Test*
-rw-r--r-- 1 root root 1017 Apr  8 14:05 Test.csr
-rw------- 1 root root 1679 Apr  8 14:03 Test.key

4 CA簽發CSR文件,頒發申請者證書

    上傳CSR文件到CA

    可以通過ftp,ssh,複製粘貼等等各種方式上傳申請者CSR文件到CA服務器.

    在CA上利用CA自身的證書和私鑰對證書申請者的CSR進行簽名,生成申請者的證書

wenca@wenca-dell:/etc/ssl$ sudo openssl ca -days 3650 -in Test.csr -cert RootCA.pem -keyfile RootCA.key -out Test.pem
Using configuration from /usr/lib/ssl/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 6 (0x6)
        Validity
            Not Before: Apr  8 06:18:54 2019 GMT
            Not After : Apr  5 06:18:54 2029 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Shanghai
            organizationName          = Cisco
            organizationalUnitName    = CRDC
            commonName                = Test.crdc.cisco.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                22:C4:A8:0F:9B:AA:E5:8F:F8:C0:88:02:3B:73:05:7A:9A:7D:4E:E0
            X509v3 Authority Key Identifier: 
                keyid:70:A0:C8:3A:69:DD:CB:B0:60:BE:43:0E:8D:AE:BB:A5:B9:57:69:3F

Certificate is to be certified until Apr  5 06:18:54 2029 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

5 openssl.cnf文件說明與應用

    利用openssl生成CSR文件,自簽發證書,以及給簽發CSR形成第三方簽發證書時,或多或是都會遇到些問題,而這大部分都可以通過修改openssl.cnf文件內容加以解決.

    openssl.cnf文件爲openssl命令的配置文件. 文件裏面的內容是 section-by-section 形式組織的.每個section都以 [section title] 打頭的. section title也說明了該section的用途.下面介紹一些常用的section.

5.1 openssl生成CSR時的section

[ req ]
default_bits            = 2048
default_keyfile         = privkey.pem
distinguished_name      = req_distinguished_name
attributes              = req_attributes
x509_extensions = v3_ca # The extensions to add to the self signed cert

# Passwords for private keys if not present they will be prompted for
# input_password = secret
# output_password = secret

# This sets a mask for permitted string types. There are several options. 
# default: PrintableString, T61String, BMPString.
# pkix   : PrintableString, BMPString (PKIX recommendation before 2004)
# utf8only: only UTF8Strings (PKIX recommendation after 2004).
# nombstr : PrintableString, T61String (no BMPStrings or UTF8Strings).
# MASK:XXXX a literal mask value.
# WARNING: ancient versions of Netscape crash on BMPStrings or UTF8Strings.
string_mask = utf8only

# req_extensions = v3_req # The extensions to add to a certificate request

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

stateOrProvinceName             = State or Province Name (full name)
stateOrProvinceName_default     = Some-State

localityName                    = Locality Name (eg, city)

0.organizationName              = Organization Name (eg, company)
0.organizationName_default      = Internet Widgits Pty Ltd

# we can do this but it is not needed normally :-)
#1.organizationName             = Second Organization Name (eg, company)
#1.organizationName_default     = World Wide Web Pty Ltd

organizationalUnitName          = Organizational Unit Name (eg, section)
#organizationalUnitName_default =

commonName                      = Common Name (e.g. server FQDN or YOUR name)
commonName_max                  = 64

emailAddress                    = Email Address
emailAddress_max                = 64

# SET-ex3                       = SET extension number 3

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

unstructuredName                = An optional company name

    該部分的section,則是在openssl生成CSR文件時,對用戶填寫的信息的要求以及默認值.

[ v3_req ]

# Extensions to add to a certificate request

basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment

    該section,用在openssl生成CSR時,除了基本信息外,新添加的一些extensions.

    比如basicConstraints則表示該CSR生成證書後能否用作CA,繼續給其他申請者簽發證書,如果是CA:FALSE,則不能做CA,一般用於製作服務器或者客戶端的CSR,如果是CA:TRUE,則可以做CA,一般用於製作中間CA的CSR.

    比如keyUsage則用來說明該CSR裏面公鑰的用途,是做加密(keyEncipherment),還是數字簽名(digitalSignature).

[ v3_ca ]


# Extensions for a typical CA


# PKIX recommendation.

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid:always,issuer

basicConstraints = critical,CA:true

# Key usage: this is typical for a CA certificate. However since it will
# prevent it being used as an test self-signed certificate it is best
# left out by default.
# keyUsage = cRLSign, keyCertSign

# Some might want this also
# nsCertType = sslCA, emailCA

# Include email address in subject alt name: another PKIX recommendation
# subjectAltName=email:copy
# Copy issuer details
# issuerAltName=issuer:copy

# DER hex encoding of an extension: beware experts only!
# obj=DER:02:03
# Where 'obj' is a standard or added object
# You can even override a supported extension:
# basicConstraints= critical, DER:30:03:01:01:FF

    該section用於openssl給自簽發證書生成CSR時,新增的一些extensions.

 

5.2 openssl簽名CSR時的section

# 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

   該section主要是用在CA用自身證書籤發申請者CSR文件時的限制條件,如果不匹配,則無法簽發證書.

    可以看到限制的參數有 countryName, stateOrProvinceName, organizationName, organizationalUnitName, commonName, emailAddress, 限制要求有 match, optional, supplied等.

   如果是match則對CA證書和申請者CSR裏面的參數必須匹配; 如果是optional則對CA證書和申請者CSR裏面的參數不做限制; 如果是supplied則表面該參數不能爲空,且不能相同.

[ usr_cert ]

# These extensions are added when 'ca' signs a request.

# This goes against PKIX guidelines but some CAs do it and some software
# requires this to avoid interpreting an end user certificate as a CA.

basicConstraints=CA:FALSE

# Here are some examples of the usage of nsCertType. If it is omitted
# the certificate can be used for anything *except* object signing.

# This is OK for an SSL server.
# nsCertType                    = server

# For an object signing certificate this would be used.
# nsCertType = objsign

# For normal client use this is typical
# nsCertType = client, email

# and for everything including object signing:
# nsCertType = client, email, objsign

# This is typical in keyUsage for a client certificate.
# keyUsage = nonRepudiation, digitalSignature, keyEncipherment

# This will be displayed in Netscape's comment listbox.
nsComment                       = "OpenSSL Generated Certificate"

# PKIX recommendations harmless if included in all certificates.
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer

# This stuff is for subjectAltName and issuerAltname.
# Import the email address.
# subjectAltName=email:copy
# An alternative to produce certificates that aren't
# deprecated according to PKIX.
# subjectAltName=email:move

# Copy subject details
# issuerAltName=issuer:copy

#nsCaRevocationUrl              = http://www.domain.dom/ca-crl.pem
#nsBaseUrl
#nsRevocationUrl
#nsRenewalUrl
#nsCaPolicyUrl
#nsSslServerName

# This is required for TSA certificates.
# extendedKeyUsage = critical,timeStamping

    該section用於openssl給CSR簽名時,在原來的CSR上新加一些extensions.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章