/
GPG

GPG

gpg 세팅

https://gnupg.org/

https://gnupg.org/gph/en/manual.html

https://help.github.com/articles/generating-a-new-gpg-key/ 참고

http://irtfweb.ifa.hawaii.edu/~lockhart/gpg/


사전에 임의의 패스워드 생성. 

$ openssl rand -base64 32


gpg key 만들기

  • what kind of key : RSA and RSA (default)
  • keysize : 4096
  • how long the key should be valid ; 0 으로 기간 제한 없이 만든다. 참고로 ubuntu 16.04 에서는 기간을 선택하는 화면이 나왔는데 mac의 brew 로 설치한 경우에는 2년으로 설정이 되었다.
  • Real name : key 를 확인하기 위한 user ID
$ gpg --gen-key
$ gpg --list-secret-keys --keyid-format LONG
/home/ubuntu/.gnupg/secring.gpg
-------------------------------
sec   4096R/B080A6AA58F52948 2018-06-28
uid                          devops <taejoon.moon@example.com>
ssb   4096R/DC47C264EB4D56A5 2018-06-28
$ gpg --armor --export B080A6AA58F52948 > devops-public-key.asc
$ gpg --armor --export-secret-keys B080A6AA58F52948  > devops-private-key.asc


다른 서버에 gpg key 옮기기. 위에서 생성한 key 파일을 먼저 옮긴 후 작업을 한다. secret-key 를 import 하면 자동으로 public key도 import 가 된다.

패스워드 물어보면 위에서 생성한 패스워드 이용하면 됨.

$ gpg --allow-secret-key-import --import  devops-private-key.asc
$ gpg --import devops-public-key.asc

# 위에서 import한 key 를 원하는 서버로 옮긴다.

$ gpg --list-keys
/Users/a201709045/.gnupg/pubring.kbx
------------------------------------
pub   rsa4096 2018-06-28 [SC]
      3B014E3B920341368DBCEDFBB080A6AA58F52948
uid           [ unknown] devops <taejoon.moon@example.com>
sub   rsa4096 2018-06-28 [E]

$ gpg --list-secret-keys --keyid-format LONG
/Users/a201709045/.gnupg/pubring.kbx
------------------------------------
sec   rsa4096/B080A6AA58F52948 2018-06-28 [SC]
      3B014E3B920341368DBCEDFBB080A6AA58F52948
uid                 [ unknown] devops <taejoon.moon@example.com>
ssb   rsa4096/DC47C264EB4D56A5 2018-06-28 [E]

문제처리

gpg 비밀번호 입력후 timeout 이 있음. 

~/.gnupg/gpg-agent.conf 파일에 pinentry-timeout 0 를 넣어주면 되는 듯 한데... 확인 필요


gpg passphrase 를 stdin 에서 받거나 파일에서 받기

gpg passphrase 를 stdin 에서 받거나 파일에서 받을 수 있다. passphrase를 echo 에서 넣어주거나 파일에 넣어둔 경우이다.

이 예제는 Ubuntu 16.04 에서는 잘 작동을 했지만 mac에서는 stdin으로 passphrase 입력 받는 것이 안 되었다. 

$ echo "xxxxx" | gpg --batch --passphrase-fd 0 --decrypt gpgtest.bin
$ cat passwd | gpg --batch --passphrase-fd 0 --decrypt gpgtest.bin


tty 설정

gpg 에서 secret key를 관리하는 것은 gpg-agent 프로그램이며 이 프로그램은 gpg에서 자동으로 관리를 한다.

mac에서 스크립트로 이용시 다음과 같은 에러가 나왔다.

gpg: public key decryption failed: Inappropriate ioctl for device 


이 부분은 gpg-agent 에서 tty를 설정하는 이슈이며 .bashrc 등에 다음 설정을 추가해 주면 된다.

export GPG_TTY=$(tty)


https://www.gnupg.org/documentation/manuals/gnupg/Invoking-GPG_002dAGENT.html

You should always add the following lines to your .bashrc or whatever initialization file is used for all shell invocations:

GPG_TTY=$(tty)
export GPG_TTY

It is important that this environment variable always reflects the output of the tty command. For W32 systems this option is not required.

...

https://www.gnupg.org/documentation/manuals/gnupg/Common-Problems.html

The Curses based Pinentry does not work

The far most common reason for this is that the environment variable GPG_TTY has not been set correctly. Make sure that it has been set to a real tty device and not just to ‘/dev/tty’; i.e. ‘GPG_TTY=tty’ is plainly wrong; what you want is ‘GPG_TTY=`tty`’ — note the back ticks. Also make sure that this environment variable gets exported, that is you should follow up the setting with an ‘export GPG_TTY’ (assuming a Bourne style shell). Even for GUI based Pinentries; you should have set GPG_TTY. See the section on installing the gpg-agent on how to do it.

Related content

terraform and gpg 세팅
terraform and gpg 세팅
More like this
SSL서비스 구축 및 OpenSSL (2007)
SSL서비스 구축 및 OpenSSL (2007)
More like this
Github Actions 으로 terraform 실행하기
Github Actions 으로 terraform 실행하기
More like this
AWS CLI 가이드
AWS CLI 가이드
More like this
GCP Cloud SDK
GCP Cloud SDK
More like this
AWS instance 연결하기
AWS instance 연결하기
More like this