0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SSL 自己証明書作成(ワンライナー)

Last updated at Posted at 2024-01-17

ずばり。

一発作成。
 
OpenSSLの場合
サーバー証明・クライアント証明・コード署名・電子メール保護 目的の場合

openssl req -x509 -nodes -sha256 -days 3650 -newkey rsa:2048 -keyout server.key -out server.pem -subj "/CN=www.example.com" -addext "subjectAltName = DNS:www.example.com" -addext "basicConstraints=CA:FALSE" -addext "extendedKeyUsage = serverAuth, clientAuth, codeSigning, emailProtection" -addext "keyUsage = nonRepudiation, digitalSignature, keyEncipherment"

サーバー証明・クライアント証明 のみ場合

openssl req -x509 -nodes -sha256 -days 3650 -newkey rsa:2048 -keyout server.key -out server.pem -subj "/CN=www.example.com" -addext "subjectAltName = DNS:www.example.com" -addext "basicConstraints=CA:FALSE" -addext "extendedKeyUsage = serverAuth, clientAuth" -addext "keyUsage = digitalSignature, keyEncipherment"

CommonNameにwildcard指定も可能。 その場合は /CN=*.example.com になる。
CommonNameにIPアドレスを指定することもできる。

-addextオプションはOpenSSL 1.1.1 以降で対応しており、
以下のように指定することで SAN をコマンドライン引数で指定可能。

-addext 'subjectAltName = DNS:<ドメイン名>'
-addext 'subjectAltName = DNS:<ドメイン名1>, DNS:<ドメイン名2>'
-addext 'subjectAltName = IP:<IPアドレス>'

こちらもwildcard指定する場合はDNS:*.example.comとなる。
 
server.key = 秘密鍵
server.csr = サーバー証明書(自己証明書)
 

-addext 'crlDistributionPoints = URI:http://example.com/myca.crl'
-addext 'crlDistributionPoints = URI:http://example.com/myca.crl, URI:http://example.org/my.crl'

 
秘密鍵と公開鍵をPKCS#12形式に変換

openssl pkcs12 -export -inkey server.key -in server.pem -out server.p12 -name "FriendlyName"

秘密鍵と公開鍵をPKCS#7形式に変換

openssl pkcs7 -export -inkey server.key -in server.pem -out server.p7b -name "FriendlyName"

フレンドリ名指定は任意(省略可能)。
パスワード入力を求められたら入力(省略可能)。設定した場合はWindowsでインポートする時に入力する。
 
秘密鍵の内容確認

RSAの場合
openssl rsa -in server.key -noout -text
ECDSAの場合
openssl ec -in server.key -noout -text

 
証明書の内容確認

openssl x509 -in server.pem -noout -text

 
サーバーに設定されている証明書を確認

openssl s_client -connect localhost:443 -showcerts

PKCS #12 形式の証明書から、各種情報(PEM形式)をまとめて取り出す

openssl pkcs12 -in server.pfx -nodes -out fullchain.pem

出力には、サーバ証明書と中間CA証明書の「-----BEGIN CERTIFICATE-----」がそれぞれ含まれ、秘密鍵の「BEGIN PRIVATE KEY」も含まれます。
-nodes オプションを指定しなければ、秘密鍵は暗号化された状態で出力されます(暗号化に必要なパスフレーズが求められます)。
 
PowerShellの場合

PS> New-SelfSignedCertificate `
-FriendlyName "<フレンドリ名>"
-DnsName "<ドメイン名>" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-CertStoreLocation "Cert:\LocalMachine\My" `
-NotAfter (Get-Date).AddYears(100) 

自己署名証明書を作成
-FriendlyName (フレンドリ名)
-DnsName (DNS名)
-KeyAlgorithm (RSA | ECDSA)
-KeyLength (鍵長)
-CertStoreLocation (証明書ストア)
-NotAfter (有効期間 : 例は 100年)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?