スキップしてメイン コンテンツに移動

OpenSSLで代替名入りの自己証明書をコマンド一発で作成する

自己証明書を作成するスクリプトは探せばみつかるとはおもいますが、代替名入りの自己証明書をコマンド一発で作成する記事は見かけなかったので、備忘録として挙げておくことにしました。
以下のスクリプトを 例えば sv113-gen-cert.shとして保存し、実行するだけでOKです。
#!/bin/bash
openssl req -x509 -days 3650 -nodes -newkey rsa:4096 -keyout server.key -out server.crt -extensions v3_req -config - << EOF
HOME			= .

####################################################################
[ ca ]
default_ca	= CA_default		# The default ca section

####################################################################
[ CA_default ]

x509_extensions	= usr_cert		# The extentions to add to the cert

# Comment out the following two lines for the "traditional"
# (and highly broken) format.
name_opt 	= ca_default		# Subject Name options
cert_opt 	= ca_default		# Certificate field options

# A few difference way of specifying how similar the request should look
# For type CA, the listed attributes must be the same, and the optional
# and supplied fields are just that :-)
policy		= policy_match

# For the CA policy
[ policy_match ]
countryName		= match
stateOrProvinceName	= match
organizationName	= match
organizationalUnitName	= optional
commonName		= supplied
emailAddress		= optional

####################################################################
[ req ]
distinguished_name	= req_distinguished_name
x509_extensions	= v3_ca	# The extentions to add to the self signed cert

prompt = no

# This sets a mask for permitted string types. There are several options.
# default: PrintableString, T61String, BMPString.
# pkix	 : PrintableString, BMPString.
# utf8only: only UTF8Strings.
# nombstr : PrintableString, T61String (no BMPStrings or UTF8Strings).
# MASK:XXXX a literal mask value.
# WARNING: current versions of Netscape crash on BMPStrings or UTF8Strings
# so use this option with caution!
string_mask = nombstr
req_extensions = v3_req # The extensions to add to a certificate request

[ req_distinguished_name ]
countryName			= JP

stateOrProvinceName		= jp

localityName			= jp

0.organizationName		= Example Company

organizationalUnitName		= location00

commonName			= sv113.location00.example.com

emailAddress			= admin@example.com

[ v3_req ]

# Extensions to add to a certificate request

basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth,clientAuth

subjectAltName = @alt_names

[ v3_ca ]

# Extensions for a typical CA

# PKIX recommendation.

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid:always,issuer:always

# This is what PKIX recommends but some broken software chokes on critical
# extensions.
#basicConstraints = critical,CA:true
# So we do this instead.
basicConstraints = CA:true

[ alt_names ]
DNS.1 = sv113.location00.xronos-msys.com
IP.1 = 172.16.113.113
EOF
今回は決め打ちにしていますが、例えば commonNameやDNS.1などを変数にしたい場合は、コマンド一発ではありませんが、以下のようにすると良いと思います。
# 変数を環境変数として設定
DNS1=sv113.location00.xronos-msys.com
IP1=172.16.113.113 
# 環境変数を含むヒアドキュメントを標準入力で与える
openssl req -x509 -days 3650 -nodes -newkey rsa:4096 -keyout server.key -out server.crt -extensions v3_req -config - << EOF
--snip
commonName			= $DNS1
--snip
[ alt_names ]
DNS.1 = $DNS1
IP.1 = $IP1
なお、"-extenions v3_req" の記述がないと代替名が入りませんので注意してください。
今回は以上です。それでは。

コメント

このブログの人気の投稿

wsdd を使ってSamba サーバをネットワークに表示

Windows 10のアップデートで、セキュリティー対応のため、smbv1がデフォルトではインストールされなくなり、Samba serverがエクスプローラーのネットワークに表示されなくなってしまいました。そこで、いくつか方法を調べたのですが、linuxでwsdの実装がないか探したところ、 https://github.com/christgau/wsdd が、見つかりましたので、さっそくインストールしてみました。まだパッケージにはないようですが、インストール自身は簡単です。wsdd自体は以下のように取得し、linkを張っておきます。 cd /usr/local/bin/ sudo wget https://raw.githubusercontent.com/christgau/wsdd/master/src/wsdd.py sudo chmod 755 wsdd.py sudo ln -sf wsdd.py wsdd こちらのsambaサーバはDebianなので、/etc/systemd/system/wsdd.serviceは以下のようにしました。 [Unit] Description=Web Services Dynamic Discovery host daemon Requires=network-online.target After=network.target network-online.target multi-user.target [Service] Type=simple ExecStart=/usr/local/bin/wsdd -d MYDOMAIN [Install] WantedBy=multi-user.target wsdd -d MYDOMAINのところを、環境にあわせて書き換えてください。 次に、systemdに登録・起動テストを行います。 systemctl enable wsdd systemctl start wsdd 起動に成功すると、エクスプローラーのネットワークに表示されます。  なおこのwsddはpython3が必要です。一度試してみてください。SMBv1/CIFSを停止していても、大丈夫です。 cで書かれたほかのwsddの実装もあるようなので、いずれパッケージになるかも...

フレッツ光クロス:MAP-E ROUTER by Debian Box (iptables)

フレッツ光クロスがようやく開通したので、Debianにてrouterを構成し接続してみました。なお、プロバイダーを選ぶにあたっては、IPoE方式がそれぞれ異なるため検討したところ、IPoEでは、MAP-Eでもv6plusとocnバーチャルコネクトがあり、前者がポート数240なのに対し、後者は約4倍のポート数が使えるようなネットの情報をみて、OCNバーチャルコネクトを選択しました。(プロバイダーとしてはぷららです。なおDS-LiteはCE側でのNATではないので今回は見送りました。)そこで、OCN バーチャルコネクトをDebian(iptables)で実現するとどうなるかと思い、ネットの情報を頼りにしつつ、設定した次第です。 実際に試した結果、とりあえず通信できていますが、MAP-Eは本来マッピングルールをマップサーバから取得するはずなので、今回のやり方が正解とはいえませんし、仕様変更されると通信できなくなる可能性があります。あくまでも参考程度ですが、本稿をUPしてみました。 2023/03/16追記: こちら にゲームコンソールNAT越え(Nintendo Switch ナットタイプ A判定)対応版を投稿しました。 2023/03/28追記:※1の記述および3行無効化によりNAT越え(Nintendo Switch ナットタイプ B判定)できるようになりました。 構成は以下の通りです。 ルーターがDebianで回線がOCNバーチャルコネクトであること以外はなにも特別なところはない構成です。 さて、いきなり設定ですが、まず、割り当てられたプレフィックスを確認します。 確認は、 dhclient -6 -d -P enp2s0 とします。出力の中に 前略 RCV: | | X-- IAPREFIX 2400:4050:5c71:af00::/56 後略 このようにプレフィックスが表示されるので、その確認したプレフィックスを書き留めておきます。これを こちらで 入力します。すると、 CE: 2400:4050:5c71:af00:99:f171:c600:2f00 IPv4 アドレス: 153.241.113.198 ポート番号:(1776-1791 2800-2815 3824-3839) 4848-4863 5872-5887 6896-...

efibootmgr を使ってブートエントリーを整理する

テスト用に OS を複数回同じ機材にインストールするとEFIブートエントリーが NVRAM に残ったままになり、どれがどれなのかわかりにくくなってしまいます。そこで不要なエントリーをしらべ整理したいと思ったのですが、意外とまとまった記事がなかったので、備忘録として挙げてみることにしました。 まず最初にエントリーを表示させます。 efibootmgr -v BootOrder: 0000,0015,0014,0012,0006,0007,0009,000A,000B,000C,0011 Boot0000* Debian_01 HD(1,GPT,XXXXXXXX-23f7-4f5e-b436-260e5fd276a7,0x800,0x100000)/File(\EFI\DEBIAN\GRUBX64.EFI) Boot0006* UEFI: PXE IP4 Mellanox Network Adapter - 50:6B:WW:WW:XX:XX PciRoot(0x1)/Pci(0x0,0x0)/Pci(0x0,0x0)/MAC(506bWWWWXXXX,1)/IPv4(0.0.0.00.0.0.0,0,0)..BO Boot0007* UEFI: PXE IP4 Mellanox Network Adapter - 50:6B:WW:WW:XX:XX PciRoot(0x1)/Pci(0x0,0x0)/Pci(0x0,0x1)/MAC(506bWWWWXXXX,1)/IPv4(0.0.0.00.0.0.0,0,0)..BO Boot0009* UEFI: PXE IP4 Intel(R) I350 Gigabit Network Connection PciRoot(0x0)/Pci(0x1c,0x0)/Pci(0x0,0x0)/MAC(901bWWWWXXXX,1)/IPv4(0.0.0.00.0.0.0,0,0)..BO Boot000A* UEFI: PXE IP6 Intel(R) I350 Gigabit Network Connection PciRoot(0x0)/Pci(0x1c,0x0)/Pci(0x0,0x0)/MAC(901bWWWWXXXX,1)/IPv6([::]: [::]:,0,0)....