Yubikey5を手に入れたのでGPGを使ってみる(2/2)(Yubikeyに移行編)

blog.akashisn.info

前回はmasterキーとsubキー、失効証明書の作成をした。 今回は作成したキー達を暗号化したボリュームにバックアップを行いYubikeyに書き込んでいく。

暗号化ボリュームの作成

ボリュームの初期化

microSDカードを挿入して、デバイスマップを確認する。

$ sudo dmesg | tail
[ 1003.591053] mmc0: cannot verify signal voltage switch
[ 1003.717876] mmc0: new ultra high speed SDR104 SDHC card at address aaaa
[ 1003.736654] mmcblk0: mmc0:aaaa SH32G 29.7 GiB 
[ 1003.755806]  mmcblk0: p1

/dev/mmcblk0であるとわかったので、パーティションを確認しておく。

$ sudo fdisk -l /dev/mmcblk0 
Disk /dev/mmcblk0: 29.74 GiB, 31914983424 bytes, 62333952 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device         Boot Start      End  Sectors  Size Id Type
/dev/mmcblk0p1       8192 62333951 62325760 29.7G  c W95 FAT32 (LBA)

暗号化するため、/dev/urandomで上書きしておく

$ sudo dd if=/dev/urandom of=/dev/mmcblk0 bs=4M status=progress 
31893487616 bytes (32 GB, 30 GiB) copied, 846 s, 37.7 MB/s 
dd: error writing '/dev/mmcblk0': No space left on device
7610+0 records in
7609+0 records out
31914983424 bytes (32 GB, 30 GiB) copied, 846.685 s, 37.7 MB/s

パーティションの作成

空のDOS領域を作成する。

o -> w と入力する。

$ sudo fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): o
Created a new DOS disklabel with disk identifier 0x1cb530ee.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

先頭から25MBの暗号化用パーティションを作成する。

n -> p -> 1 -> (enter) -> +25M -> w と入力する。

$ sudo fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-62333951, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62333951, default 62333951): +25M

Created a new partition 1 of type 'Linux' and of size 25 MiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

パーティションの暗号化

暗号化ボリュームのパスフレーズを作成する。

$ gpg --gen-random --armor 0 24

先ほど作成したパーティションをLUKSで暗号化する。

YES と入力し、先ほど作成したパスフレーズを入力する。

$ sudo cryptsetup luksFormat /dev/mmcblk0p1 

WARNING!
========
This will overwrite data on /dev/mmcblk0p1 irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase for /dev/mmcblk0p1: 
Verify passphrase: 

暗号化したパーティションのマウント

パスフレーズを入力してロックされているパーティションを解除する。

$ sudo cryptsetup luksOpen /dev/mmcblk0p1 secret
Enter passphrase for /dev/mmcblk0p1:

暗号化したパーティションをEXT2でフォーマットする。

$ sudo mkfs.ext2 /dev/mapper/secret -L gpg-$(date +%F)
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 2304 4k blocks and 2304 inodes

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

マウントポイントを作成してマウントする。

$ sudo mkdir /mnt/encrypted-storage
$ sudo mount /dev/mapper/secret /mnt/encrypted-storage

秘密鍵のバックアップ

$ sudo cp -avi $GNUPGHOME /mnt/encrypted-storage/
$ sudo mv /mnt/encrypted-storage/tmp.* /mnt/encrypted-storage/gpgkeys

バックアップが終わったらアンマウントしてパーティションをロックする。

$ sudo umount /mnt/encrypted-storage/
$ sudo cryptsetup luksClose secret

公開鍵のバックアップ

パーティションの作成

公開鍵をバックアップするための暗号化していないパーティションを作成する。

n -> p -> 2 -> (enter) -> +25M -> w と入力する。

$ sudo fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (53248-62333951, default 53248): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (53248-62333951, default 62333951): +25M

Created a new partition 2 of type 'Linux' and of size 25 MiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
$ sudo mkfs.vfat -F 32 /dev/mmcblk0p2

パーティションのマウント

マウントポイントを作成してマウントする。

$ sudo mkdir /mnt/public
$ sudo mount /dev/mmcblk0p2 /mnt/public/

公開鍵のエクスポート

公開鍵をエクスポートする。

$ gpg --armor --export $KEYID | sudo tee /mnt/public/gpg-$KEYID-$(date +%F).txt

バックアップが終わったらアンマウントする。

$ sudo umount /mnt/public/

Yubikey5に秘密鍵を移動

YubiKey Managerのインストール

developers.yubico.com

$ sudo apt-add-repository ppa:yubico/stable
$ sudo apt update
$ sudo apt install yubikey-manager

Yubikeyのリセット

Yubikey5をPCに接続して、リセットを行う。

$ ykman openpgp reset
WARNING! This will delete all stored OpenPGP keys and data and restore factory settings? [y/N]: y
Resetting OpenPGP data, don't remove your YubiKey...
Success! All data has been cleared and default PINs are set.
PIN:         123456
Reset code:  NOT SET
Admin PIN:   12345678

$ ykman piv reset
WARNING! This will delete all stored PIV data and restore factory settings. Proceed? [y/N]: y
Resetting PIV data...
Success! All PIV data have been cleared from your YubiKey.
Your YubiKey now has the default PIN, PUK and Management Key:
    PIN:    123456
    PUK:    12345678
    Management Key: 010203040506070801020304050607080102030405060708

PINの変更

PIV機能のPIN

Yubikey5のPIV機能には3種類のPINがある。

  • PIN : 普段使用するPINコード。6~8桁の数値
  • PUK : PINコードを紛失したときやPINを複数回間違えてロックされた時に解除するときに使う。6~8桁の英数字が使える。
  • Management Key : 24byte 3DESのkey。鍵の生成や証明書のimport時に使う。PINによって保護されたランダムのキーを用いる。
$ ykman piv change-pin
Enter your current PIN: 
Enter your new PIN: 
Repeat for confirmation: 
New PIN set.

$ ykman piv change-puk
Enter your current PUK: 
Enter your new PUK: 
Repeat for confirmation: 
New PUK set.

$ ykman piv change-management-key --protect
Enter PIN: 
Enter your current management key [blank to use default key]:

PINの変更が出来たらいったんYubikeyを抜き差しする。

さらにややこしいのが、このPIVのPINとOpenPGPのPINはレイヤーが違うので、OpenPGPでもPINを変更する必要がある。

OpenPGP機能のPIN

www.gnupg.org

PIV機能のPINと同じく、デフォルトのAdmin PINは12345678、普通のPINは123456である。

GPG側でOpenPGP機能のPINの変更を行う。

admin -> passwd -> 1 -> 123456 -> 新たなPIN(2回) -> 3 -> 12345678 -> 新たなAdmin PIN(2回) -> q と入力する。

$ gpg --card-edit 

Reader ...........: Yubico YubiKey OTP FIDO CCID 00 00
Application ID ...: Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Application type .: OpenPGP
Version ..........: 3.4
Manufacturer .....: Yubico
Serial number ....: xxxxxxxx
Name of cardholder: [not set]
Language prefs ...: [not set]
Salutation .......: 
URL of public key : [not set]
Login data .......: [not set]
Signature PIN ....: not forced
Key attributes ...: rsa2048 rsa2048 rsa2048
Max. PIN lengths .: 127 127 127
PIN retry counter : 3 0 3
Signature counter : 0
KDF setting ......: off
Signature key ....: [none]
Encryption key....: [none]
Authentication key: [none]
General key info..: [none]

gpg/card> admin
Admin commands are allowed

gpg/card> passwd
gpg: OpenPGP card no. Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx detected

1 - change PIN
2 - unblock PIN
3 - change Admin PIN
4 - set the Reset Code
Q - quit

Your selection? 1
PIN changed.

1 - change PIN
2 - unblock PIN
3 - change Admin PIN
4 - set the Reset Code
Q - quit

Your selection? 3
PIN changed.

1 - change PIN
2 - unblock PIN
3 - change Admin PIN
4 - set the Reset Code
Q - quit

Your selection? q

Yubikey情報の変更

名前、言語、アカウント名などを指定する。

gpg/card> name
Cardholder's surname: <姓>
Cardholder's given name: <名>

gpg/card> lang
Language preferences: ja

gpg/card> login
Login data (account name): <Email等>

gpg/card> list

Reader ...........: Yubico YubiKey OTP FIDO CCID 00 00
Application ID ...: Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
Application type .: OpenPGP
Version ..........: 3.4
Manufacturer .....: Yubico
Serial number ....: xxxxxxxx
Name of cardholder: <名><姓>
Language prefs ...: ja
Salutation .......: 
URL of public key : [not set]
Login data .......: <Email等>
Signature PIN ....: not forced
Key attributes ...: rsa2048 rsa2048 rsa2048
Max. PIN lengths .: 127 127 127
PIN retry counter : 3 0 3
Signature counter : 0
KDF setting ......: off
Signature key ....: [none]
Encryption key....: [none]
Authentication key: [none]
General key info..: [none]

gpg/card> q

秘密鍵の移動

選択しているキーにはssb*とアスタリスクがつく。

keytocardは破壊的な操作でYubikeyに移した秘密鍵はもう取り戻せないのでバックアップが確実に行われていることを確認しておく。

MasterキーのパスフレーズとAdmin PINの入力が求められる。

$ gpg --edit-key $KEYID 
gpg (GnuPG) 2.2.19; Copyright (C) 2019 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Secret key is available.

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb  nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb  nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb  nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

gpg>

署名キー

key 1 -> keytocard -> 1 と入力し、MasterキーのパスフレーズとAdmin PINを入力する。

gpg> key 1

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb* nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb  nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb  nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

gpg> keytocard
Please select where to store the key:
   (1) Signature key
   (3) Authentication key
Your selection? 1

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb* nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb  nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb  nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

暗号化キー

key 1 -> key 2 -> keytocard -> 2 と入力し、MasterキーのパスフレーズとAdmin PINを入力する。

gpg> key 1

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb  nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb  nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb  nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

gpg> key 2

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb  nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb* nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb  nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

gpg> keytocard
Please select where to store the key:
   (2) Encryption key
Your selection? 2

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb  nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb* nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb  nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

認証キー

key 2 -> key 3 -> keytocard -> 3 と入力し、MasterキーのパスフレーズとAdmin PINを入力する。

gpg> key 2

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb  nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb  nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb  nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

gpg> key 3

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb  nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb  nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb* nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

gpg> keytocard
Please select where to store the key:
   (3) Authentication key
Your selection? 3

sec  nistp521/BF8A708B020A19BB
     created: 2020-12-09  expires: never       usage: C   
     trust: ultimate      validity: ultimate
ssb  nistp521/983982062E1BB33D
     created: 2020-12-09  expires: never       usage: S   
ssb  nistp521/008734F36B087C1C
     created: 2020-12-09  expires: never       usage: E   
ssb* nistp521/26BAE38657860920
     created: 2020-12-09  expires: never       usage: A   
[ultimate] (1). <名前> <メールアドレス>

変更を保存する。

gpg> save

検証

subキーの秘密鍵が移動されたかを確認する。

$ gpg -K
/tmp/tmp.nqKpgHfYNN/pubring.kbx
-------------------------------
sec   nistp521 2020-12-09 [C]
      D6FCE147DE91797391B2D70BBF8A708B020A19BB
uid           [ultimate] <名前> <メールアドレス>
ssb>  nistp521 2020-12-09 [S]
ssb>  nistp521 2020-12-09 [E]
ssb>  nistp521 2020-12-09 [A]

ssb>となってればちゃんと移動している。

クリーンアップ

$ gpg --delete-secret-key $KEYID
$ sudo srm -r $GNUPGHOME || sudo rm -rf $GNUPGHOME
$ unset GNUPGHOME

続きはこちら(執筆中)