乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      使用 OpenSSL 創(chuàng)建私有 CA:1 根證書

       印度阿三17 2019-02-14

      OpenSSL 創(chuàng)建私有 CA 三部曲:
      使用 OpenSSL 創(chuàng)建私有 CA:1 根證書
      使用 OpenSSL 創(chuàng)建私有 CA:2 中間證書
      使用 OpenSSL 創(chuàng)建私有 CA:3 用戶證書

      OpenSSL 是一個(gè)免費(fèi)開源的庫(kù),它提供了一些處理數(shù)字證書的命令行工具。其中一些工具可以用作證書頒發(fā)機(jī)構(gòu)(Certificate Authority 即 CA)。
      證書頒發(fā)機(jī)構(gòu)(CA)是簽署數(shù)字證書的實(shí)體。許多網(wǎng)站需要讓他們的客戶知道連接是安全的,所以需要從一個(gè)被廣泛信任的CA(例如VeriSign, DigiCert)來為他們的域名簽署證書,也就是我們常說的 HTTPS 證書。
      本系列文章的目的不是讓你創(chuàng)建像 VeriSign 那樣的 CA 機(jī)構(gòu)。在很多情況下,我們需要在公司局域網(wǎng)內(nèi)的網(wǎng)站中啟用 HTTPS,比如測(cè)試環(huán)境,或者是公司內(nèi)網(wǎng)中的管理系統(tǒng)(現(xiàn)在主流的瀏覽器都把 HTTP 站點(diǎn)標(biāo)記為不安全)。此時(shí)通過 OpenSSL 創(chuàng)建私有 CA 并頒發(fā)證書就是很好的選擇了。
      本系列分為三篇文章,即本文《使用 OpenSSL 創(chuàng)建私有 CA:1 根證書》后續(xù)的《使用 OpenSSL 創(chuàng)建私有 CA:2 中間證書》和《使用 OpenSSL 創(chuàng)建私有 CA:3 用戶證書》。
      說明:本系列文章的演示環(huán)境為 Ubuntu 18.04,OpenSSL 的版本為 1.1.0g。

      基本目錄結(jié)構(gòu)

      創(chuàng)建 myca 目錄保存 CA 相關(guān)的所有內(nèi)容,然后創(chuàng)建 myca/rootca 目錄用來保存根證書相關(guān)的內(nèi)容:

      至于上圖中的 myca/powerca 和 myca/usercert 則存放后面要介紹的中間證書和用戶證書的內(nèi)容。

      準(zhǔn)備根證書的配置文件

      OpenSSL 程序提供了一個(gè)默認(rèn)的配置文件:/etc/ssl/openssl.cnf,但是由于我們自定義的內(nèi)容比較多,所以干脆創(chuàng)建一個(gè)套單獨(dú)的配置文件。創(chuàng)建文件 rootca/rootca.cnf,編輯其內(nèi)容如下:

      # OpenSSL root CA configuration file.
      # v1
      [ ca ]
      # `man ca`
      default_ca = CA_default
      [ CA_default ]
      # Directory and file locations.
      dir = /home/nick/projects/myca/rootca
      certs = $dir/certs
      crl_dir = $dir/crl
      new_certs_dir = $dir/newcerts
      database = $dir/db/index
      serial = $dir/db/serial
      RANDFILE = $dir/private/random
      # The root key and root certificate.
      private_key = $dir/private/rootca.key.pem
      certificate = $dir/certs/rootca.cert.pem
      # For certificate revocation lists.
      crlnumber = $dir/db/crlnumber
      crl = $dir/crl/rootca.crl.pem
      crl_extensions = crl_ext
      default_crl_days = 30
      # SHA-1 is deprecated, so use SHA-2 instead.
      default_md = sha256
      name_opt = ca_default
      cert_opt = ca_default
      default_days = 3750
      preserve = no
      policy = policy_strict
      [ policy_strict ]
      # The root CA should only sign intermediate certificates that match.
      # See the POLICY FORMAT section of `man ca`.
      countryName = match
      stateOrProvinceName = match
      organizationName = match
      organizationalUnitName = optional
      commonName = supplied
      emailAddress = optional
      [ req ]
      # Options for the `req` tool (`man req`).
      # Optionally, specify some defaults.
      prompt = no
      input_password = 123456
      
      default_bits = 2048
      distinguished_name = req_distinguished_name
      string_mask = utf8only
      # SHA-1 is deprecated, so use SHA-2 instead.
      default_md = sha256
      # Extension to add when the -x509 option is used.
      # make sure use x509_extensions, do not use req_extensions.
      x509_extensions = v3_ca
      # use the req_extensions not work.
      #req_extensions = v3_ca
      [ req_distinguished_name ]
      # See <https://en./wiki/Certificate_signing_request>.
      countryName = CN
      stateOrProvinceName = ShaanXi
      localityName = Xian
      organizationName = NickLi Ltd
      organizationalUnitName = NickLi Ltd CA
      commonName = NickLi Root CA
      emailAddress = ljfpower@163.com
      [ v3_ca ]
      # Extensions for a typical CA (`man x509v3_config`).
      subjectKeyIdentifier = hash
      authorityKeyIdentifier = keyid:always,issuer
      basicConstraints = critical, CA:true
      keyUsage = critical, digitalSignature, cRLSign, keyCertSign
      [ v3_intermediate_ca ]
      # Extensions for a typical intermediate CA (`man x509v3_config`).
      subjectKeyIdentifier = hash
      authorityKeyIdentifier = keyid:always,issuer
      basicConstraints = critical, CA:true, pathlen:0
      keyUsage = critical, digitalSignature, cRLSign, keyCertSign
      [ crl_ext ]
      # Extension for CRLs (`man x509v3_config`).
      authorityKeyIdentifier=keyid:always
      [ ocsp ]
      # Extension for OCSP signing certificates (`man ocsp`).
      basicConstraints = CA:FALSE
      subjectKeyIdentifier = hash
      authorityKeyIdentifier = keyid,issuer
      keyUsage = critical, digitalSignature
      extendedKeyUsage = critical, OCSPSigning

      這是一個(gè)非常重要的配置文件,指定了很多配置的目錄,其中最重要的一個(gè)已經(jīng)被抽取成了變量 dir。我們要做的就是指定 dir 的路徑,并且根據(jù)下面的步驟來創(chuàng)建其余的目錄和文件。
      為了把創(chuàng)建證書的過程也自動(dòng)化掉,筆者在 req 段添加了下面的配置:

      [ req ]
      prompt = no
      input_password = 123456

      其中的 123456 就是后面創(chuàng)建的秘鑰的密碼,這樣在創(chuàng)建 Certificate Signing Requests(csr) 時(shí)就不需要以交互的方式輸入密碼了。
      下面的內(nèi)容是默認(rèn)的 CA 信息:

      [ req_distinguished_name ]
      countryName = CN
      stateOrProvinceName = ShaanXi
      localityName = Xian
      organizationName = NickLi Ltd
      organizationalUnitName = NickLi Ltd CA
      commonName = NickLi Root CA
      emailAddress = ljfpower@163.com

      準(zhǔn)備目錄和文件

      在 rootca 目錄下創(chuàng)建下面的目錄和文件:

      rootca/certs/           # 存放新建的證書
      rootca/db/              # spenssl 用來存放信息的目錄
      rootca/private/         # 存放私鑰
      rootca/crl/                                 
      rootca/newcerts/
      
      rootca/db/index
      rootca/db/serial
      rootca/db/crlnumber

      其中的 rootca/private 目錄需要 700 的權(quán)限,主要是為了既保證私鑰的安全有能夠創(chuàng)建私鑰文件。rootca/db/serial 和 rootca/db/crlnumber 則需要被初始化為特定的值。簡(jiǎn)單起見我們可以使用下面的腳本來創(chuàng)建這些目錄和文件:

      #!/bin/bash
      
      # create dir certs db private crl newcerts under rootca dir.
      if [ ! -d rootca/certs ]; then
          mkdir -p rootca/certs
      fi
      
      if [ ! -d rootca/db ]; then
          mkdir -p rootca/db
          touch rootca/db/index
          openssl rand -hex 16 > rootca/db/serial
          echo 1001 > rootca/db/crlnumber
      fi
      
      if [ ! -d rootca/private ]; then
          mkdir -p rootca/private
          chmod 700 rootca/private
      fi
      
      if [ ! -d rootca/crl ]; then
          mkdir -p rootca/crl
      fi
      
      if [ ! -d rootca/newcerts ]; then
          mkdir -p rootca/newcerts
      fi

      把上面的代碼保存到 myca/roothelpler.sh 文件中,然后 cd 到 myca 目錄下執(zhí)行:

      $ ./roothelpler.sh

      此時(shí)當(dāng)前目錄為 myca,rootca 下的子目錄和文件都已經(jīng)創(chuàng)建成功。

      創(chuàng)建 root 秘鑰

      進(jìn)入 rootca 目錄:

      $ cd rootca

      執(zhí)行下面的命令創(chuàng)建私鑰:

      $ openssl genrsa -aes256 -out private/rootca.key.pem 4096

      這里筆者設(shè)置的密碼為:123456,記住這個(gè)密碼,后面還會(huì)用到。然后為了確保安全,把秘鑰的訪問權(quán)限設(shè)置為 400:

      $ chmod 400 private/rootca.key.pem

      此時(shí)當(dāng)前目錄為 myca/rootca。

      創(chuàng)建 Certificate Signing Requests(csr)

      執(zhí)行下面的命令創(chuàng)建 csr:

      $ openssl req -new     -config rootca.cnf     -sha256     -key private/rootca.key.pem     -out csr/rootca.csr.pem

      下面的命令可以檢查生成的 csr:

      $ openssl req -text -noout -in csr/rootca.csr.pem

      注意,csr 中包含了 CA 的基本信息,和公鑰信息。

      創(chuàng)建 CA 的根證書

      有了前一步中生成的 csr,我們就可以通過下面的命令生成 CA 的根證書了:

      $ openssl ca -selfsign     -config rootca.cnf     -in csr/rootca.csr.pem     -extensions v3_ca     -days 7300     -out certs/rootca.cert.pem

      在交互式的提示中輸入私鑰的密碼 123456,并同意其它的確認(rèn)提示,就完成了根證書的生成操作。注意,上面命令中的 -selfsing 選項(xiàng),它說明所有的根證書都是自簽名的。同樣,我們也可以通過命令來查看證書的詳細(xì)信息:

      $ openssl x509 -noout -text -in certs/rootca.cert.pem

      其中的 Signature Algorithm 為簽名算法;Issurer 是簽發(fā)方,即簽署證書的實(shí)體;Validity 指明證書的有效期為 2018-11-26 號(hào)至 2038-11-21 號(hào);然后是公鑰信息;Subject 指明證書自身的信息,這里 Issurer 和 Subject 的信息是一樣的;下面還有 X509 協(xié)議相關(guān)的信息,這部分信息由配置文件 rootca.cnf 中的 [ v3_ca ] 段控制:

      紅框中的信息表明這個(gè)證書被用作 CA。

      總結(jié)

      至此我們已經(jīng)為私有的 CA 創(chuàng)建了根證書,在接下來的《使用 OpenSSL 創(chuàng)建私有 CA:中間證書》一文中我們將詳細(xì)的介紹如何創(chuàng)建 CA 的中間證書。

      參考:
      OpenSSL Certificate Authority
      《openssl-cookbook》

      來源:http://www./content-4-113951.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多