OpenSSL

2023-10-05 (木) 08:50:43

OpenSSL とは

  • SSLTLSのプロトコルでの通信に使うことができるOSSのソフトウェア。
  • HTTPSの実装の多くに使われている。
  • 様々な暗号化の方式に対応している。

コマンド

openssl

  • プロンプトを起動
    openssl
  • 文字列のハッシュ生成
    echo -n "MESSAGE" | openssl sha1
    echo -n "MESSAGE" | openssl dgst -md5
    echo -n "MESSAGE" | openssl dgst -sha256

公開鍵暗号方式RSA

  • 鍵の生成
    • 秘密鍵を生成 ※標準出力
      openssl genrsa 1024
      openssl genrsa 2048
    • 秘密鍵を生成 ※ファイル出力(PEM形式)
      openssl genrsa -out <private-keyfile.pem> 1024
      openssl genrsa -out <private-keyfile.pem> 2048
    • 秘密鍵から公開鍵を生成 ※ファイル出力(PEM形式)
      openssl rsa -in <private-keyfile.pem> -pubout -out <public-keyfile.pem>
  • 暗号化・復号
    • 公開鍵で暗号化
      openssl rsautl -encrypt -pubin -inkey <public-keyfile.pem> -in <input-file.txt> -out <output-file.rsa>
    • 秘密鍵で復号 ※平文が出力される
      openssl rsautl -decrypt -inkey <private-keyfile.pem> -in <input-file.rsa>
  • 鍵の中身を出力
    • 秘密鍵
      openssl rsa -in <private-keyfile.pem> -text -noout
    • 公開鍵
      openssl rsa -pubin -in <public-keyfile.pem> -text -noout
  • 鍵の形式を変換して出力(PEM→DER)
    • 秘密鍵
      openssl rsa -in <private-keyfile.pem> -out <private-keyfile.der> -outform der
    • 公開鍵
      openssl rsa -pubin -in <public-keyfile.pem> -out <public-keyfile.der> -outform der
  • 鍵の形式を変換して出力(DER→PEM)
    • 秘密鍵
      openssl rsa -in <private-keyfile.der> -inform der -out <private-keyfile.pem>
    • 公開鍵
      openssl rsa -pubin -in <public-keyfile.der> -inform der -out <public-keyfile.pem>

デジタル証明書X.509

  • 鍵の中身を出力
    openssl x509 -in <public-keyfile.crt> -text -noout

その他、例

  • 任意の文字列をSHA-256(バイナリ)に変換後、Base64エンコードする
    echo -n "文字列" | openssl dgst -sha256 -binary | base64

脆弱性

参考サイト

関連用語