Java keytool 사용법

■ 참고

  1. java keytool 사용법 – Keystore 생성, 키쌍 생성, 인증서 등록 및 관리
  2. keytool – Key and Certificate Management Tool

■ 현재 등록되어 있는 인증서 목록 확인, 여기서 cacerts는 JRE에 포함된 기본 인증기관(ca) 인증서 목록 파일이다. cacerts의 기본 비밀번호는 changeit이다.

$ keytool -list -keystore /usr/local/java/jre/lib/security/cacerts
Enter keystore password : changeit <-- default password  

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 93 entries

digicertassuredidrootca, Apr 16, 2008, trustedCertEntry,
Certificate fingerprint (SHA1): 05:63:B8:63:0D:62:D7:5A:BB:C8:AB:1E:4B:DF:B5:A8:99:B2:4D:43
comodorsaca, May 13, 2015, trustedCertEntry,
Certificate fingerprint (SHA1): AF:E5:D2:44:A8:D1:19:42:30:FF:47:9F:E2:F8:97:BB:CD:7A:8C:B4
thawtepremiumserverca, May 26, 2015, trustedCertEntry,
Certificate fingerprint (SHA1): E0:AB:05:94:20:72:54:93:05:60:62:02:36:70:F7:CD:2E:FC:66:66
.......

■ keytool 도움말 확인(-help), -certreq, -genkeypair, storepasswd등 keytool의 명령어 목록을 확인할 수 있다.

$ keytool -help
Key and Certificate Management Tool

Commands:

-certreq            Generates a certificate request
...
-genkeypair         Generates a key pair
...
-storepasswd        Changes the store password of a keystore

Use "keytool -command_name -help" for usage of command_name

■ keytool command 사용법 조회, 아래는 인증서 목록 파일을 만드는 genkeypair명령어의 사용법을 조회하는 방법이다. genkeypair명령어 사용시 가능한 옵션 목록을 볼 수 있다.

$ keytool -genkeypair -help
keytool -genkeypair [OPTION]...

Generates a key pair

Options:

-alias <alias>                  alias name of the entry to process
...
-keystore <keystore>            keystore name
-storepass <arg>                keystore password
-storetype <storetype>          keystore type
...
-protected                      password through protected mechanism

Use "keytool -help" for all available commands

■ 새로운 인증서 목록 파일 만드는 방법, -keystore 옵션은 인증서를 저장할 파일을 지정한다. 이 옵션을 사용하지 않는다면 디폴트로 사용자의 홈디렉토리에 .keystore라는 파일명으로 생성된다. -storetype옵션은 암호화 타입을 지정하는데 이 옵션을 사용하지 않으면 디폴트 jks(Java KeyStore)이며 다른 표준 암호화 타입은 PKCS12가 있다.

$ keytool -genkeypair -keystore jks_keystore -storetype jks
Enter keystore password: 123456  
Re-enter new password: 123456
What is your first and last name?
  [Unknown]:  first
What is the name of your organizational unit?
  [Unknown]:  org unit
What is the name of your organization?
  [Unknown]:  org nm
What is the name of your City or Locality?
  [Unknown]:  city
What is the name of your State or Province?
  [Unknown]:  state
What is the two-letter country code for this unit?
  [Unknown]:  KR
Is CN=first, OU=org unit, O=org nm, L=city, ST=state, C=KR correct?
  [no]:  yes

Enter key password for <mykey>
    (RETURN if same as keystore password): 엔터

■ keystore 암호 변경, jks_keystore 파일의 암호를 변경하는 방법이다.

$ keytool -storepasswd -keystore jks_keystore
Enter keystore password: 기존비밀번호 
New keystore password: 새로운 비밀번호
Re-enter new keystore password: 새로운 비밀번호 확인

■ key 암호변경, jks_keystore라는 키스토어 파일안에 mykey라는 alias를 가진 개인키의 암호를 변경한다.

# jks_keystore파일내 개인키 목록을 조회한다.
$ keytool -list -keystore jks_keystore
Enter keystore password: 123456  
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 1 entry

mykey, Mar 14, 2019, PrivateKeyEntry,
Certificate fingerprint (SHA1): 14:D4:3D:AD:AE:EA:C3:DB:08:2A:08:E5:3D:C1:CE:6F:82:6B:A9:DA

$ keytool -keypasswd -alias mykey -keystore jks_keystore
Enter keystore password: 기존비밀번호  
New key password for <mykey>: 새로운 비밀번호
Re-enter new key password for <mykey>: 새로운 비밀번호 확인

■ 개인키 alias 변경, jks_keystore에 저장된 newkey라는 alias를 가진 개인키를 mykey라는 alias로 변경한다.

$ keytool -changealias -keystore jks_keystore -alias newkey -destalias mykey
Enter keystore password: 키스토어 비밀번호  
Enter key password for <newkey> newkey(개인키) 비밀번호

■ jks_keystore에 개인키 ‘yourkey’ 추가

$ keytool -list -keystore jks_keystore
Enter keystore password: 123456
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 1 entry

mykey, Mar 14, 2019, PrivateKeyEntry,
Certificate fingerprint (SHA1): 14:D4:3D:AD:AE:EA:C3:DB:08:2A:08:E5:3D:C1:CE:6F:82:6B:A9:DA
$ keytool -genkeypair -alias yourkey -keystore jks_keystore -keypass 123456
Enter keystore password: 123456  
What is your first and last name?
  [Unknown]:  your
What is the name of your organizational unit?
  [Unknown]:  org unit 2
What is the name of your organization?
  [Unknown]:  org nm 2
What is the name of your City or Locality?
  [Unknown]:  city 2
What is the name of your State or Province?
  [Unknown]:  state 2
What is the two-letter country code for this unit?
  [Unknown]:  KR
Is CN=your, OU=org unit 2, O=org nm 2, L=city 2, ST=state 2, C=KR correct?
  [no]:  yes


$ keytool -list -keystore jks_keystore
Enter keystore password: 123456  

Keystore type: JKS
Keystore provider: SUN


Your keystore contains 2 entries


yourkey, Mar 14, 2019, PrivateKeyEntry,
Certificate fingerprint (SHA1): 55:6D:C0:E5:7B:1C:BC:A7:C0:E9:6E:32:7F:44:14:45:50:65:BC:60
mykey, Mar 14, 2019, PrivateKeyEntry,
Certificate fingerprint (SHA1): 14:D4:3D:AD:AE:EA:C3:DB:08:2A:08:E5:3D:C1:CE:6F:82:6B:A9:DA