OpenSSL

提供: ArchWiki
2022年5月6日 (金) 14:55時点におけるKusanaginoturugi (トーク | 投稿記録)による版 (→‎設定: Merge を削除)
ナビゲーションに移動 検索に移動

関連記事

OpenSSL は SSL と TLS プロトコルのオープンソース実装で、可能な限り柔軟になるように設計されています。BSD, Linux, OpenVMS, Solaris, Windows などの様々なプラットフォームをサポートしています。

警告: 2015年5月に公開された、OpenSSL のプロトコルの利用に関する共同研究によって、SSL 接続に "Logjam" 攻撃と呼ばれる、さらなる危険が存在することが判明しました。結果については https://weakdh.org/ を、推奨されるサーバー側の設定の変更については https://weakdh.org/sysadmin.html を見て下さい。

インストール

opensslcoreutils の依存関係として、デフォルトで Arch Linux にインストールされます。

開発者向けに様々な OpenSSL ライブラリのバインディングが用意されています。

設定

Arch Linux では OPENSSLDIR/etc/ssl です。

OpenSSL 設定ファイルは、慣習的に /etc/ssl/openssl.cnf に置かれ、最初は複雑に見えるかもしれません。シェルスクリプトの動作と同じように、変数が代入で展開されることがあることを覚えておいてください。設定ファイルの形式についての詳しい説明は、config(5ssl) を参照してください。

req section

Settings related to generating keys, requests and self-signed certificates.

The req section is responsible for the DN prompts. A general misconception is the Common Name (CN) prompt, which suggests that it should have the user's proper name as a value. End-user certificates need to have the machine hostname as CN, whereas CA should not have a valid TLD, so that there is no chance that, between the possible combinations of certified end-users' CN and the CA certificate's, there is a match that could be misinterpreted by some software as meaning that the end-user certificate is self-signed. Some CA certificates do not even have a CN, such as Equifax:

$ openssl x509 -subject -noout < /etc/ssl/certs/Equifax_Secure_CA.pem
subject= /C=US/O=Equifax/OU=Equifax Secure Certificate Authority

使用方法

この章は、Transport Layer Security#証明書を取得を読んだ事を前提としています。

Curve25519 秘密鍵を生成

$ openssl genpkey -algorithm x25519 -out file

ECDSA 秘密鍵を生成

$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out file

RSA 秘密鍵を生成

genrsa に取って代わる(openssl(1ssl) によると)、genpkey(1ssl) を使用。

$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:keysize -out file

暗号化された鍵が必要な場合は、-aes-256-cbc オプションを使用します。

証明書署名要求を生成

req(1ssl) を使用。

$ openssl req -new -sha256 -key private_key -out filename

自己証明証明書を生成

$ openssl req -key private_key -x509 -new -days days -out filename

一つのコマンドで自己証明証明書と秘密鍵を生成

OpenSSL では、上記のコマンドを 1 つにまとめることができるので、場合によっては便利かもしれません。

 $ openssl req -x509 -newkey rsa:4096 -days days -keyout key_filename -out cert_filename

Diffie–Hellman パラメーターを生成

詳細については、Diffie–Hellman key exchange を参照してください。

現在のベストプラクティスは、RFC 7919の標準的なDHグループの1つ、例えば ffdhe2048 を使用することです。

あるいは、自分でランダムなグループを生成することもできます。

$ openssl dhparam -out filename 2048
ヒント: 特にハイエンドのハードウェアでない場合、生成を高速化するには、-dsaparam オプションを追加します[1]

証明書情報を表示

$ openssl x509 -text -in cert_filename

証明書の指紋を表示

$ openssl x509 -noout -in cert_filename -fingerprint -digest

-digest はオプションで、-md5-sha1-sha256 または -sha512 のいずれか 1 つです。digest が指定されていない場合については、x509(1ssl) § Input, Output, and General Purpose Options の「-digest」 を参照してください。

トラブルシューティング

復号時に "bad decrypt" と表示される

OpenSSL 1.1.0 から dgst と enc コマンドのデフォルトのダイジェストアルゴリズムが MD5 から SHA256 に変更されています [2]

OpenSSL 1.0.2 以前を使ってファイルを暗号化した場合、復号化しようとすると新しいバージョンでは以下のようにエラーが発生します:

error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:crypto/evp/evp_enc.c:540

-md md5 オプションを指定することで問題は解決します:

$ openssl enc -d -md md5 -in encrypted -out decrypted

Python 3.10 and "ca md too weak" errors

In Python 3.10 by default there is a hardcoded list of allowed OpenSSL ciphers. Some of the less secure, like MD5, have been disabled at the ssl module level, ignoring the system-wide configuration of OpenSSL. It results sometimes in strange errors on older certificates, sometimes even when establishing https connections, like:

requests.exceptions.SSLError: HTTPSConnectionPool(host='a.kind.of.example.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(398, '[SSL: CA_MD_TOO_WEAK] ca md too weak (_ssl.c:3862)')))

To make Python follow the system configuration, you may have to rebuild it, adding --with-ssl-default-suites=openssl parameter to ./configure. The issue has been also reported as FS#73549.

参照