OpenSSL
OpenSSL は SSL と TLS プロトコルのオープンソース実装で、可能な限り柔軟になるように設計されています。BSD, Linux, OpenVMS, Solaris, Windows などの様々なプラットフォームをサポートしています。
目次
- 1 インストール
- 2 Configuration
- 3 Usage
- 3.1 Generate a Curve25519 private key
- 3.2 Generate an ECDSA private key
- 3.3 Generate an RSA private key
- 3.4 Generate a certificate signing request
- 3.5 Generate a self-signed certificate
- 3.6 Generate a self-signed certificate with private key in a single command
- 3.7 Generate Diffie–Hellman parameters
- 3.8 Show certificate information
- 3.9 Show certificate fingerprint
- 4 トラブルシューティング
- 5 参照
インストール
openssl は coreutils の依存関係として、デフォルトで Arch Linux にインストールされます。
開発者向けに様々な OpenSSL ライブラリのバインディングが用意されています。
- python-pyopenssl
- perl-net-ssleay
- lua-sec, lua52-sec, lua51-sec
- haskell-hsopenssl
- haskell-openssl-streams
Configuration
On Arch Linux the OPENSSLDIR
is /etc/ssl
.
The OpenSSL configuration file, conventionally placed in /etc/ssl/openssl.cnf
, may appear complicated at first. Remember that variables may be expanded in assignments, much like how shell scripts work. For a thorough explanation of the configuration file format, see 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
Usage
This sections assumes you have read Transport Layer Security#Obtaining a certificate.
Generate a Curve25519 private key
$ openssl genpkey -algorithm x25519 -out file
Generate an ECDSA private key
$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out file
Generate an RSA private key
With genpkey(1ssl), which supersedes genrsa according to openssl(1ssl):
$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:keysize -out file
If an encrypted key is desired, use the -aes-256-cbc
option.
Generate a certificate signing request
Use req(1ssl):
$ openssl req -new -sha256 -key private_key -out filename
Generate a self-signed certificate
$ openssl req -key private_key -x509 -new -days days -out filename
Generate a self-signed certificate with private key in a single command
You can combine the above command in OpenSSL into a single command which might be convenient in some cases:
$ openssl req -x509 -newkey rsa:4096 -days days -keyout key_filename -out cert_filename
Generate Diffie–Hellman parameters
See Diffie–Hellman key exchange for more information.
Current best practice is to use one of the standard DH groups from RFC 7919, eg. ffdhe2048.
Alternatively you can generate a random group of your own:
$ openssl dhparam -out filename 2048
Show certificate information
$ openssl x509 -text -in cert_filename
Show certificate fingerprint
$ openssl x509 -noout -in cert_filename -fingerprint -digest
-digest
is optional and one of -md5
, -sha1
, -sha256
, or -sha512
. See "-digest" in x509(1ssl) § Input, Output, and General Purpose Options for when the digest is unspecified.
トラブルシューティング
復号時に "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.
参照
- Wikipedia の OpenSSL のページ
- OpenSSL プロジェクトページ
- FreeBSD ハンドブック
- 署名済みの SSL 証明書を作成するステップバイステップガイド
- OpenSSL Certificate Authority
- Bulletproof SSL and TLS by Ivan Ristić, a more formal introduction to SSL/TLS