プライベートCAと証明書の詳細


一、CAと証明書
CA(Certificate Authority)証明書発行機関は、主に証明書の発行、管理、アーカイブ、取り消しを担当しています.
証明書には主に3つの機能があります.暗号化、署名、認証です.X.509:証明書の構造及び認証プロトコル基準を定義
  • バージョン番号
  • シリアル番号
  • 署名アルゴリズム
  • 発行者
  • 有効期限
  • 本体名
  • 証明書の種類:
  • 専門の証明書授権機関の証明書
  • サーバ証明書
  • ユーザ証明書
  • 証明書の取得には、次の2つの方法があります.
  • 自己署名証明書
  • 授権機関が発行する専門証明書①証明書要求(csr)②証明書要求csrをCA機関に送信③CA署名発行証明書
  • 二、私有CA実現証明書の申請と発行
    プライベートCAを確立する2つの方法:
  • OpenCA:OpenCAオープンソース組織Perlを使用してOpenSSLを二次開発した完全なPKI無料ソフトウェア
  • openssl

  • 証明書申請及び署名手順:①.申請依頼②を生成する.RA検査③.CA署名④.証明書の取得
    Opensslのプロファイルの詳細:プロファイルの保存パス:
    /etc/pki/tls/openssl.cnf 
    

    このファイルには、主に証明書リクエスト、署名、crl関連の構成が設定されています.主に関連する擬似コマンドはcaとreqである.x 509の場合、このプロファイルは使用されません.このファイルは機能構造から4つの段落に分かれています.デフォルトセグメント、ca関連セグメント、req関連セグメント、tsa関連セグメントです.各セグメントはname=valueの形式で定義されます.プロファイルの内容(以下はCA関連セグメントの一部とコメントの内容):
    [root@localhost ~]# cat /etc/pki/tls/openssl.cnf 
    #
    ......
    ####################################################################
    [ ca ]
    default_ca	= CA_default		# The default ca section
    
    ####################################################################
    [ CA_default ]
    
    dir		= /etc/pki/CA		# Where everything is kept
    certs		= $dir/certs		# Where the issued certs are kept(        , CA    ) 
    crl_dir		= $dir/crl		# Where the issued crl are kept(    crl    )
    database	= $dir/index.txt	# database index file.
    #   yes database    subject        
    #    subject            
    #     no,               yes
    #unique_subject	= no			# Set to 'no' to allow creation of
    					# several certs with same subject.
    new_certs_dir	= $dir/newcerts		# default place for new certs.(           )
    
    certificate	= $dir/cacert.pem 	# The CA certificate(CA       )
    serial		= $dir/serial 		# The current serial number(        )
    crlnumber	= $dir/crlnumber	# the current crl number(  crl   )
    					# must be commented out to leave a V1 CRL
    crl		= $dir/crl.pem 		# The current CRL(  CRL)
    private_key	= $dir/private/cakey.pem# The private key(        , CA     )
    RANDFILE	= $dir/private/.rand	# private random number file(          )
    
    x509_extensions	= usr_cert		# The extensions to add to the cert(          )
    
    # Comment out the following two lines for the "traditional"
    # (and highly broken) format.
    #              ,     ,     。           
    name_opt 	= ca_default		# Subject Name options
    cert_opt 	= ca_default		# Certificate field options
    #   copy_extensions   ,     
    # Extension copying option: use with caution.
    # copy_extensions = copy 
                            /*          copy  ,    none/copy/copyall */
    						/*     name    none */
    						/*           none    ,          copyall */
    
    # Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs
    # so this is commented out by default to leave a V1 CRL.
    # crlnumber must also be commented out to leave a V1 CRL.
    # crl_extensions	= crl_ext
    
    default_days	= 365			# how long to certify for(        )
    default_crl_days= 30			# how long before next CRL(CRL    )
    default_md	= sha256		# use SHA-256 by default(      )
    preserve	= no			# keep passed DN ordering(Distinguished Name  ,     no)
    /*   yes        IE  )*/
    
    # 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
    #      ,      [ policy_match ]   
    #match               CA      
    #optional              
    #supplied           
    # 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 ]
    ......
    
    [root@localhost ~]# 
    

    プライベートCAの作成(以下はCentos 8以上のバージョンで行う操作)Centos 8以上のバージョンには関連するCAディレクトリがなく、以下のように手動で作成する必要があります.
    [root@localhost ~]# for dir in certs  crl  newcerts  private ;do mkdir -pv /etc/pki/CA/$dir;done
    mkdir: created directory '/etc/pki/CA'
    mkdir: created directory '/etc/pki/CA/certs'
    mkdir: created directory '/etc/pki/CA/crl'
    mkdir: created directory '/etc/pki/CA/newcerts'
    mkdir: created directory '/etc/pki/CA/private'
    [root@localhost ~]# tree /etc/pki/CA/
    /etc/pki/CA/
    ├── certs
    ├── crl
    ├── newcerts
    └── private
    
    4 directories, 0 files
    [root@localhost ~]# 
    
    

    1、自己署名証明書の作成手順
    1.1、CA作成に必要なファイル
    #           
    touch /etc/pki/CA/index.txt
    #             
    echo 01 > /etc/pki/CA/serial
    

    1.2、CA秘密鍵の生成
    [root@localhost ~]# cd /etc/pki/CA/
    [root@localhost CA]# (umask 066; openssl genrsa -out private/cakey.pem 2048)
    Generating RSA private key, 2048 bit long modulus (2 primes)
    ..................................................+++++
    .........................................+++++
    e is 65537 (0x010001)
    [root@localhost CA]# 
    
    

    1.3、CA自己署名証明書の生成
    [root@localhost CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 3650 -out /etc/pki/CA/cacert.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) [XX]:CN 
    #  
    State or Province Name (full name) []:beijing
    #  
    Locality Name (eg, city) [Default City]:beijing
    #      
    Organization Name (eg, company) [Default Company Ltd]:alibaba
    #    
    Organizational Unit Name (eg, section) []:devops
    #          ,       ,  *.swyer.com.cn
    Common Name (eg, your name or your server's hostname) []:ca.alibaba.com
    #    
    Email Address []:[email protected]
    [root@localhost CA]# 
    [root@localhost CA]# ls
    cacert.pem  certs  crl  index.txt  newcerts  private  serial
    [root@localhost CA]# 
    
    
    

    オプションの説明:
    -new:新しい証明書署名要求の生成-x 509:CAによる自己署名証明書の生成専用-key:要求の生成に使用される秘密鍵ファイル-days n:証明書の有効期限-out/PATH/DO/SomeCEERTFILE:証明書の保存経路
    国コード照会
    2、証明書の申請と証明書の発行
    2.1、証明書を使用する必要があるホストに対して秘密鍵を生成する
    [root@localhost CA]# (umask 066;openssl genrsa -out /data/app.key 2048)
    Generating RSA private key, 2048 bit long modulus (2 primes)
    .................+++++
    .........................................................................+++++
    e is 65537 (0x010001)
    [root@localhost CA]# 
    

    2.2、証明書を使用するホストの証明書申請ファイルを生成する
    [root@localhost CA]# (umask 066;openssl genrsa -out /data/app.key 2048)
    Generating RSA private key, 2048 bit long modulus (2 primes)
    .................+++++
    .........................................................................+++++
    e is 65537 (0x010001)
    [root@localhost CA]# openssl req -new -key /data/app.key -out /data/app.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) [XX]:CN
    State or Province Name (full name) []:beijing
    Locality Name (eg, city) [Default City]:haidian
    Organization Name (eg, company) [Default Company Ltd]:alibaba
    Organizational Unit Name (eg, section) []:hr
    Common Name (eg, your name or your server's hostname) []:*.alibaba.com
    Email Address []:[email protected]
    

    注意:プライベートCA証明書のため、デフォルトでは国、省、会社名の3つがCAと一致する必要があります
    2.3.CAで証明書に署名し、証明書を要求者に発行する
    [root@localhost CA]# openssl ca -in /data/app.csr -out /etc/pki/CA/certs/app.crt -days 100
    Using configuration from /etc/pki/tls/openssl.cnf
    Check that the request matches the signature
    Signature ok
    Certificate Details:
            Serial Number: 1 (0x1)
            Validity
                Not Before: Feb  4 14:13:38 2020 GMT
                Not After : May 14 14:13:38 2020 GMT
            Subject:
                countryName               = CN
                stateOrProvinceName       = beijing
                organizationName          = alibaba
                organizationalUnitName    = hr
                commonName                = *.alibaba.com
                emailAddress              = [email protected]
            X509v3 extensions:
                X509v3 Basic Constraints: 
                    CA:FALSE
                Netscape Comment: 
                    OpenSSL Generated Certificate
                X509v3 Subject Key Identifier: 
                    F6:29:09:21:68:8F:13:AA:3C:68:30:1F:B9:B7:EC:BC:01:09:24:F1
                X509v3 Authority Key Identifier: 
                    keyid:CD:83:3E:13:6E:75:E0:F7:21:53:AD:6C:6C:C1:39:71:8C:8E:F5:88
    
    Certificate is to be certified until May 14 14:13:38 2020 GMT (100 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
    [root@localhost CA]# 
    
    

    2.4、証明書の情報を表示する
    [root@localhost CA]# ls
    cacert.pem  crl        index.txt.attr  newcerts  serial
    certs       index.txt  index.txt.old   private   serial.old
    [root@localhost CA]# cd certs/
    [root@localhost certs]# ls
    app.crt
    [root@localhost certs]# openssl x509 -in app.crt -noout -text
    
    #           
    [root@localhost CA]# ls
    cacert.pem  crl        index.txt.attr  newcerts  serial
    certs       index.txt  index.txt.old   private   serial.old
    [root@localhost CA]# cat serial.old 
    01
    [root@localhost CA]# openssl ca -status 01
    Using configuration from /etc/pki/tls/openssl.cnf
    01=Valid (V)
    [root@localhost CA]# 
    

    3、取り消し証明書
    3.1.クライアントで取り消す証明書を取得するserial
    [root@localhost CA]# openssl x509 -in certs/app.crt -noout -serial
    serial=01
    [root@localhost CA]# openssl x509 -in certs/app.crt -noout -serial -subject
    serial=01
    subject=C = CN, ST = beijing, O = alibaba, OU = hr, CN = *.alibaba.com, emailAddress = [email protected]
    [root@localhost CA]# 
    

    3.2.CAにおいて、お客様から提出されたserialとsubjectの情報に基づき、検査が
    index.txt        ,    
    [root@localhost CA]# cd newcerts/
    [root@localhost newcerts]# ls
    01.pem
    [root@localhost newcerts]# openssl ca -revoke /etc/pki/CA/newcerts/01.pem
    Using configuration from /etc/pki/tls/openssl.cnf
    Revoking Certificate 01.
    Data Base Updated
    [root@localhost newcerts]# 
    

    最初の取り消し証明書の番号を指定します.注意:証明書の取り消しリストを最初に更新する前に、実行する必要があります.
    [root@localhost CA]# echo 01 > /etc/pki/CA/crlnumber
    

    証明書取り消しリストの更新
    [root@localhost CA]# openssl ca -gencrl -out /etc/pki/CA/crl.pem
    Using configuration from /etc/pki/tls/openssl.cnf
    

    crlファイルの表示
    [root@localhost CA]# openssl crl -in /etc/pki/CA/crl.pem -noout -text
    Certificate Revocation List (CRL):
            Version 2 (0x1)
            Signature Algorithm: sha256WithRSAEncryption
            Issuer: C = CN, ST = beijing, L = beijing, O = alibaba, OU = devops, CN = ca.alibaba.com, emailAddress = [email protected]
            Last Update: Feb  4 14:28:20 2020 GMT
            Next Update: Mar  5 14:28:20 2020 GMT
            CRL extensions:
                X509v3 CRL Number: 
                    1
    Revoked Certificates:
        Serial Number: 01
            Revocation Date: Feb  4 14:24:30 2020 GMT
        Signature Algorithm: sha256WithRSAEncryption
             8a:25:37:ff:b3:05:3e:df:ac:79:1c:ad:23:7e:81:81:00:60:
             f7:77:f2:fd:7a:86:70:90:d1:5f:fb:1e:69:d1:5a:bc:15:08:
             7b:11:9f:8e:80:e0:14:af:d2:b7:a0:e3:21:a2:31:13:ad:51:
             ce:9b:2e:74:1d:ae:21:cf:04:a5:19:bd:f3:cc:5f:60:42:f3:
             4c:db:27:ea:04:cd:5d:f2:62:0e:39:85:f9:51:e8:0c:8c:bf:
             88:8c:62:3f:b7:11:3d:68:05:ef:23:95:87:c4:c8:df:8d:ca:
             e7:e9:c8:76:34:06:0a:c9:a4:9b:93:7b:b1:9e:56:39:30:4b:
             62:01:35:40:d3:02:07:63:c9:6d:c8:c1:c5:f7:11:33:8c:d9:
             f9:54:8b:0e:70:97:0c:e4:cd:73:36:bd:ab:d1:b1:5f:8a:b2:
             2a:c9:0b:1b:28:a4:85:80:b7:4e:51:4e:a4:4a:a9:e0:3a:0f:
             aa:3e:6e:49:6a:9a:4c:71:7f:06:57:5e:bc:a7:0f:0a:18:90:
             f9:58:4a:78:eb:54:ec:18:79:69:d9:27:49:74:92:ce:aa:d4:
             59:58:79:62:f4:57:5a:cc:5d:d1:f5:90:fd:e6:e3:1f:9c:20:
             73:90:2a:71:62:61:91:b3:be:a9:48:50:20:c5:3f:d7:8b:5b:
             45:12:3e:f2
    [root@localhost CA]#