この記事に書いてあること
ファイルの暗号化を行う 記事 で,テスト用に「他人」=「正当な秘密鍵を持っていない人」を再現したので,一応その手順を残しておく.
やったこと
ある秘密鍵を持たない人を再現するにあたって,自分の PC だとちょっとめんどくさそうだった.なぜなら, GPG が気を利かせて適切な鍵を使ってしまうかもしれないからだ.また,「秘密鍵を持っているがキャンセルした」といった出力ではなく,「秘密鍵を持っていない」ことを示す出力を見たかったから.
したがって自分の PC 以外の環境が必要だが,他人の PC を使うわけにもいかないし,テスト用でもサーバに秘密情報を置いたりしたくなかった.
そのため, Docker を使うことにした.公式ではないが GPG の Docker イメージ が公開されていたので,それを使わせていただく.
1. docker run
$ docker run -it --rm --entrypoint sh vladgh/gpg
Unable to find image 'vladgh/gpg:latest' locally
latest: Pulling from vladgh/gpg
a0d0a0d46f8b: Pull complete
60b2aa58f8f6: Pull complete
Digest: sha256:95c5d7fa3782183eeb03b4427cd8f4a237e3660bc4b155cd5bee3d4ef83f9d54
Status: Downloaded newer image for vladgh/gpg:latest
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
/ #
デフォルトだと気を利かせて gpg
が起動するので,エントリーポイントとしてシェルを指定する. bash はないらしい.
2. 鍵を生成する
/ # gpg --quick-gen-key hoge
gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
About to create a key for:
"hoge"
Continue? (Y/n) y
パスフレーズを聞かれたりするので,適当に設定する.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key E9FE1B76DBF8B1D3 marked as ultimately trusted
gpg: directory '/root/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/root/.gnupg/openpgp-revocs.d/1DCFC4D1554F769937DEBA00E9FE1B76DBF8B1D3.rev'
public and secret key created and signed.
pub rsa3072 2022-01-23 [SC] [expires: 2024-01-23]
1DCFC4D1554F769937DEBA00E9FE1B76DBF8B1D3
uid hoge
sub rsa3072 2022-01-23 [E]
/ #
3. やりたいことをやる
ある秘密鍵を持たない誰かとして再現したい好きな動きをやる.
今回は暗号化されたファイルの復号に失敗する人を再現したかったので,以下のようにした. #
が Docker コンテナ内で, $
はホストマシンである.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8112f224d570 vladgh/gpg "sh" 5 minutes ago Up 5 minutes tender_rosalind
$ docker cp ./test.tgz.gpg tender_rosalind:/
/ # ls
bin etc lib mnt proc run srv test.tgz.gpg usr
dev home media opt root sbin sys tmp var
/ # gpg --decrypt test.tgz.gpg
gpg: encrypted with ECDH key, ID 02ABDEDC13AB19F4
gpg: decryption failed: No secret key
最後の三行(復号に失敗した時の出力)が取れたので,目的が達成できた.
まとめ
Docker を使って,ある秘密鍵を持っていない人 = 第三者 の環境を再現した.