Last-modified: 2017-06-08 (木) 08:54:25
OpenSSL/チートシート

PrivateCA構築

  1. vi /etc/ssl/openssl.cnf
    Everything is expanded.Everything is shortened.
      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
    
    -
    !
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    -
    |
    |
    !
     
     
     
     
     
     
     
     
     
     
     
     
     
    
    # 既存の値を更新
    [ CA_default ]
    dir = /root/privateca
    certificate = $dir/ca.crt
    crl_dir = $dir/ca.crl
    private_key = $dir/private/ca.key
    x509_extensions = v3_ca
    copy_extensions = copy
    default_days = 10950
    default_md = sha256
    email_in_dn = no
     
    [ v3_ca ]
    basicConstraints = critical, CA:true
     
    [ req_distinguished_name ]
    countryName_default = JP
    stateOrProvinceName_default = Tokyo
    0.organizationName_default = Paburica
     
    # 発行する証明書毎に設定を追加
    # user_certをカスタマイズするより、目的に応じて新たなセクションを定義したほうが扱いやすい
    # e.g. IKEv2 VPNサーバー証明書, クライアント証明書
    [ vpn_server_cert ]
    basicConstraints=critical, CA:FALSE
    keyUsage = critical, digitalSignature, keyEncipherment, keyAgreement
    subjectKeyIdentifier=hash
    authorityKeyIdentifier=keyid, issuer
    extendedKeyUsage = critical, serverAuth, 1.3.6.1.5.5.8.2.2
     
    [ vpn_client_cert ]
    basicConstraints=critical, CA:FALSE
    keyUsage = critical, digitalSignature, keyEncipherment
    subjectKeyIdentifier=hash
    authorityKeyIdentifier=keyid, issuer
    extendedKeyUsage = critical, clientAuth
    crlDistributionPoints = URI:https://<CRLのURI>
  2. 構築(ルート証明書(公開鍵:EC, 署名:ECDSA))
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
    
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    
    mkdir /root/privateca && cd $_
    mkdir certs crl csr newcerts
    mkdir -m0700 private
    echo `date +%-d%N%S%M`01 > serial
    echo "00" > crlnumber
    touch index.txt index.txt.attr
     
    ca_cn=PaburicaCA
    o=Paburica
    openssl req \
            -x509 \
            -newkey ec:<(openssl ecparam -name prime256v1) \
            -keyout private/ca.key \
            -out ca.crt \
            -extensions v3_ca \
            -sha256 \
            -days 10950 \
            -subj "/C=JP/ST=Tokyo/CN=${ca_cn}/O=${o}"
    chmod 600 private/ca.key
    openssl ca -updatedb -gencrl -out ca.crl

CSR生成(公開鍵:EC)

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
-
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
# e.g. IKEv2 VPNクライアント証明書のCSR
#      パスフレーズが不要なら、-nodesを追加
#      uuidを入れていない場合、apt-get install uuid
#      SANsは、多プラットフォームのVPNクライアントから接続できるようにする為に指定
uuid=`uuid -v4`
srv_cn=<VPNサーバーFQDN>
srv_ip=<VPNサーバーIP>
o=Paburica
openssl req \
        -newkey ec:<(openssl ecparam -name prime256v1) \
        -keyout private/vc-${uuid}.key \
        -out csr/vc-${uuid}.csr \
        -subj "/C=JP/ST=Tokyo/O=${o}/CN=${uuid}@${srv_cn}" \
        -reqexts SAN \
        -config <(cat /etc/ssl/openssl.cnf \
            <(printf "[SAN]\nsubjectAltName=DNS:${uuid}@${srv_cn},DNS:${uuid}@${srv_ip}"))

署名

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
 
 
-
|
|
|
openssl ca \
    -notext \
    -in <CSRパス> \
    -out <証明書出力パス> \
    -days <有効日数> \
    -extensions <使用するセクション名(e.g. vpn_client_cert)>

失効

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
 
 
 
-
!
 
openssl ca -revoke <証明書のパス>
openssl ca -updatedb -gencrl -out <CRLの出力先パス>
 
# e.g.
openssl ca -revoke newcerts/5134279959475510.pem
openssl ca -updatedb -gencrl -out ca.crl

変換

X.509証明書→PKCS#12証明書

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
 
 
-
|
|
openssl pkcs12 \
        -export \
        -in <X.509証明書パス> \
        -inkey <秘密鍵パス> \
        -out <PKCS#12証明書パス>

DER-PEM相互変換

Everything is expanded.Everything is shortened.
  1
-
openssl <rsa|ec|x509|crl> -in <入力ファイルパス> -inform <der|pem> -out <出力ファイルパス> -outform <pem|der>

内容確認

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
-
!
-
-
|
-
-
|
-
-
|
-
-
# PEM形式ではなく、DER形式なら -inform der が必要
 
# 公開鍵ペア
openssl rsa -text -noout -in <パス>
 
# CSR
openssl req -text -noout -in <パス>
 
# 証明書
openssl x509 -text -noout -in <パス>
 
# CRL
openssl crl -text -noout -in <パス>

IKEv2のCritificate Request Payload値を自前で作成

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
-
|
-
|
-
-
# Certificate Request Payload(https://tools.ietf.org/html/rfc7296)は、
# CAの証明書のSubject Public Key Information部分をSHA-1でハッシュ化した値
openssl <rsa|ec> -inform der -in <公開鍵ペアのパス> -outform der -pubout | openssl sha1
 
# e.g.
openssl ec -inform der -in foo.key -outform der -pubout | openssl sha1

共通秘密鍵で暗号化/復号

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
-
!
 
-
!
# 暗号化
echo <平文> | openssl aes-256-cbc -e -base64
 
# 復号
echo <Base64エンコードされた暗号化データ> | openssl aes-256-cbc -d -base64

ハッシュ値確認

Everything is expanded.Everything is shortened.
  1
  2
  3
 
 
 
openssl <md4|md5|sha1|sha256|sha384|sha512> <ファイルパス>
 
echo <値> | openssl <md4|md5|sha1|sha256|sha384|sha512>

パフォーマンス計測

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
-
!
 
-
!
-
!
-
!
# 選択可能なアルゴリズムは、不正なパラメータを入れてヘルプを出せばいい
openssl speed <アルゴリズム(スペース区切り)>
 
# e.g. ハッシュ
openssl speed md4 md5 sha1 sha256 sha512
# e.g. 共通鍵暗号
openssl speed des des-ede3 
# e.g. 公開鍵暗号(カテゴリ指定のほか、直接指定もできる)
openssl speed dsa2048 rsa2048 rsa4096 ecdsap256 ecdsap384