OpenSSHで使えるRSA鍵ペアをOpenSSLで生成する

OpenSSH 7.1/7.1p1でRSA鍵長は最低1024bitになりました.

 * Refusing all RSA keys smaller than 1024 bits (the current minimum
   is 768 bits)

1024bitより短い鍵長の鍵を作ろうとすると怒られます.

$ ssh-keygen -t rsa -b 768
Invalid RSA key length: minimum is 1024 bits

以下のMLで古い機器が1024bitに対応していない場合の相談でOpenSSLでOpenSSHで利用できるRSA鍵ペアが作れるのを知りました.

You can use openssl to generate a shorter key:

	$ openssl genprsa -out key.pem 768
	$ ssh-keygen -y -f key.pem > key.pub # optional to get public key

This works with a 768-bit RSA key (client: OpenSSH_7.2p2, OpenSSL
1.0.2g; server: OpenSSH_7.2p2, OpenSSL 1.0.2g) but not a 256-bit RSA
key: I can generate the shorter key but the server requires a minimum of
768-bits.

使うことがあるかわからないけど手元の環境で試してみました.

opensslでRSA 768の秘密鍵の作成
$ openssl genrsa -out id_rsa768 768
Generating RSA private key, 768 bit long modulus (2 primes)
........+++++++
................................+++++++
e is 65537 (0x010001)
パスフレーズありのRSA 768の秘密鍵の作成
$ openssl genrsa -out id_rsa768 -aes256 768
Generating RSA private key, 768 bit long modulus (2 primes)
..+++++++
..........+++++++
e is 65537 (0x010001)
Enter pass phrase for id_rsa768:
Verifying - Enter pass phrase for id_rsa768:

OpenSSLから書き出された鍵ファイルのパーミッションはumaskに関係なく(0000でも)600になっています :)

秘密鍵の確認
$ openssl rsa -text < id_rsa768

秘密鍵が出来たので続いてペアとなる公開鍵を作ります.
MLの例と同じように ssh-keygen を使うと鍵の長さで怒られます.

$ ssh-keygen -y -f id_rsa768 > id_rsa768.pub
Load key "id_rsa768": Invalid key length

公開鍵もOpenSSLで作ります.

OpenSSL公開鍵を作る
$ openssl rsa -pubout < id_rsa768 > id_rsa768.pub
writing RSA key
公開鍵の確認
$ openssl rsa -text -pubin < id_rsa768.pub
出来上がった鍵ペアでログインを試みると鍵長のせいで認証に失敗
$ ssh -v -i ~/.ssh/id_rsa768 localhost
   :
debug1: Trying private key: /home/matoken/.ssh/id_rsa768
Load key "/home/matoken/.ssh/id_rsa768": Invalid key length

てことで使いみちがなさそうですがメモしておきます.
#今ならed25519使いますしね…….

ちなみに1024bitより長い鍵なら普通に使えてssh-keygenでコメントを書き換えたりパスフレーズを書き換えたりでしました.

RSA 4096の鍵
$ openssl genrsa -out id_rsa4096 -aes256 4096
Generating RSA private key, 4096 bit long modulus (2 primes)
................++++
.....................................................................................................++++
e is 65537 (0x010001)
Enter pass phrase for id_rsa4096:
Verifying - Enter pass phrase for id_rsa4096:
$ ssh-keygen -c -C 'OpenSSL generate key' -f ./id_rsa4096
Enter passphrase:
No existing comment
Comment 'OpenSSL generated key' applied
$ ssh-keygen -p -f ./id_rsa4096
Enter old passphrase:
Key has comment 'OpenSSL generated key'
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.
環境
$ dpkg-query -W openssl openssh-client openssh-server
openssh-client  1:8.1p1-2
openssh-server  1:8.1p1-2
openssl 1.1.1d-2
$ lsb_release -dr
Description:    Debian GNU/Linux bullseye/sid
Release:        unstable
$ uname -m
x86_64

OpenSSLでSHA512のPASSWORDを生成する

Linux の /etc/shadow のsha512形式の暗号化パスワードを生成するのに OpenSSL を利用したメモです.

最近は SHA512 が規定値になっている

$ grep ^ENCRYPT_METHOD /etc/login.defs
ENCRYPT_METHOD SHA512

openssl passwd は 1.1.1 から sha512 がサポートされている

*) 'openssl passwd' can now produce SHA256 and SHA512 based output,

-6 オプションが SHA512

$ openssl version
OpenSSL 1.1.1a  20 Nov 2018 (Library: OpenSSL 1.1.1b  26 Feb 2019)
$ openssl passwd --help
Usage: passwd [options]
Valid options are:
 -help               Display this summary
 -in infile          Read passwords from file
 -noverify           Never verify when reading password from terminal
 -quiet              No warnings
 -table              Format output as table
 -reverse            Switch table columns
 -salt val           Use provided salt
 -stdin              Read passwords from stdin
 -6                  SHA512-based password algorithm
 -5                  SHA256-based password algorithm
 -apr1               MD5-based password algorithm, Apache variant
 -1                  MD5-based password algorithm
 -aixmd5             AIX MD5-based password algorithm
 -crypt              Standard Unix password algorithm (default)
 -rand val           Load the file(s) into the random number generator
 -writerand outfile  Write random data to the specified file

こんな感じで作れる
$ が区切り文字になっていて, 6 部分が暗号形式のid,SALT 部分がsalt,その後ろが暗号化パスワードになっている

$ openssl passwd -salt SALT -6 PASSWORD
$6$SALT$io0TPmhM8ythCm7Idt0AfYvTuFCLyA1CMVmeT3EUqarf2NQcTuLKEgP9.4Q8fgClzP7OCnyOY1wo1xDw0jtyH1

この文字列を, /etc/shadow の第2フィールドに埋め込むとlogin に利用できる

SALT もランダムにしたい場合は,
/dev/random や

$ tr -cd '[:alnum:]' < /dev/random | head -c 8
ouOpUJoq

openssl rand が使える

$ openssl rand --help
Usage: rand [flags] num
Valid options are:
 -help               Display this summary
 -out outfile        Output file
 -rand val           Load the file(s) into the random number generator
 -writerand outfile  Write random data to the specified file
 -base64             Base64 encode output
 -hex                Hex encode output
 -engine val         Use engine, possibly a hardware device
$ openssl rand -base64 6
Gy/YhLzM

salt 8 文字で sha512 なパスワードを作成

$ openssl passwd -6 -salt $(openssl rand -base64 6) PASSWORD
$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000

実際に /etc/shadow に設定して認証を試してみる

$ sudo useradd testuser
$ sudo sed -i 's,^testuser:[^:]*,testuser:$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000,' /etc/shadow
$ sudo grep ^testuser: /etc/shadow
testuser:$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000:17970:0:99999:7:::
$ su testuser
Password:
$ whoami
testuser
$

openssl のバージョンが古くてSHA512に対応していない場合は適当なscriptで生成できる
salt を同じものにして試すと同じ文字列が得られるのが確認できる

$ perl -e 'print crypt("PASSWORD", ( "\$6$" . "O2bwYkq/" ));'
$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000
$ python -c "import crypt, getpass, pwd; print crypt.crypt('PASSWORD','\$6$O2bwYkq/$')"
$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000

環境

$ dpkg-query -W openssl login
login   1:4.5-1.1
openssl 1.1.1a-1
$ lsb_release -dr
Description:    Debian GNU/Linux buster/sid
Release:        testing
$ uname -m
aarch64

参考URL