본문 바로가기
  • 오늘처럼
소프트웨어 아키텍처/Linux

TLS 인증서 (3) - openssl 을 통해 key, csr, crt 파일 만들기

by bluefriday 2022. 6. 17.
반응형

*명령어는 최하단에 별도로 정리하였습니다.


이번에는 실제 명령어를 사용하여 key, csr, crt 파일을 생성해본다.

 

1. key 파일 생성

먼저 비대칭 암호화 통신에 사용되는 개인키(비밀키)를 생성한다.

# 개인키 만들기
명령어 : openssl genrsa [-out filename] [numbits]
- numbits 의 경우 개인키의 사이즈이며 반드시 명령어 마지막에 위치 (디폴트 값은 512)

openssl genrsa -out test.key 2048

생성된  개인키의 키 값과 파일 내용을 각각 확인한다.

# 완성된 개인키의 파일 내용 확인
cat ./test.key

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAr9IpTEnzxy660fPz7HeTJthOMYQw+HXiDG1LDbsmRI/cwYYP
IpoQgQeQ8jwI164fgChNsv3jcamO/ufgp9BF0HtkugnuMs7WkMtfivLMj8xrmM6e
yghlNne/Wkx+g1iPaYXb3vvdYGkyra4omWZzphOrvR3o6wMN8c7yU4wukjXrP/0G
...
lTBnEH/O1dGazwDUsKZOxhdpF4niF12GmKG87BTykjIh5ZjEXDGTWg==
-----END RSA PRIVATE KEY-----


# 완성된 개인키의 정보를 확인
openssl rsa -in test.key -text

Private-Key: (2048 bit)
modulus:
    00:af:d2:29:4c:49:f3:c7:2e:ba:d1:f3:f3:ec:77:
...
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAr9IpTEnzxy660fPz7HeTJthOMYQw+HXiDG1LDbsmRI/cwYYP
IpoQgQeQ8jwI164fgChNsv3jcamO/ufgp9BF0HtkugnuMs7WkMtfivLMj8xrmM6e
yghlNne/Wkx+g1iPaYXb3vvdYGkyra4omWZzphOrvR3o6wMN8c7yU4wukjXrP/0G
...
lTBnEH/O1dGazwDUsKZOxhdpF4niF12GmKG87BTykjIh5ZjEXDGTWg==
-----END RSA PRIVATE KEY-----

 

key 파일을 생성할 때, 보안성을 높이기 위하여 암호화 알고리즘을 적용할 수 있다. aes128, aes192, aes256 등의 암호화 알고리즘을 선택 가능하며, 1-1) 이미 패스워드 없이 만들어진 key 에 암호를 추가 하거나, 1-2) 처음부터 패스워드를 포함하여 key를 만들수 있으며 추가로 1-3) key에서 패스워드를 제거할 수도 있다.

 

1-1. 패스워드 없이 생성한 key 에 패스워드 추가

하단 예시에서는 aes256 알고리즘을 사용하며 그 외의 알고리즘은 man 명령어 (```man genrsa```) 를 참고한다. 

명령어 : openssl rsa -in 키파일 -out 키파일 [암호화 알고리즘]

openssl rsa -in test.key -out test.key -aes256
writing RSA key
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

 

1-2. 패스워드 를 부여하여 key 파일 생성

처음에 key 파일을 생성할 때부터 암호화 옵션을 추가하여 생성 가능하다.

# 암호를 추가하여 개인키를 생성
openssl genrsa -aes256 -out test.key 2048
Generating RSA private key, 2048 bit long modulus
..+++
........+++
e is 65537 (0x10001)
Enter pass phrase for test.key: <암호 화면입력>
Verifying - Enter pass phrase for test.key: <암호 화면입력(재확인)

 

1-3. 패스워드를 부여하여 생성한 key 파일에 패스워드 제거

명령어 : openssl rsa -in 키파일 -out 키파일

openssl rsa -in test.key -out test.key
Enter pass phrase for test.key: <암호 입력>
writing RSA key

 

이렇게 생성한 개인키 파일로부터 공개키를 추출 가능하다. 인증서 서명과는 별도로, 개인키-공개키 비대칭 키 암호화를 통한 ssh 서버 접속 등에 사용될 수 있다.

1-4. 개인키로부터 공개키 추출

명령어 : openssl rsa -in 개인키 -pubout -out 공개키

openssl rsa -in test.key -pubout -out test_pub.key

2. csr 파일 생성

csr 파일을 생성하기 위하여 개인키가 필요하며, 개인키는 위 1번 과정을 통해 만들 수 있다. 이 과정에서 다음과 같은 값들을 대화형 인터페이스로 입력한다. CN(Common Name) 등의 값을 주의해서 입력하도록 한다.

명령어 : openssl req -new -key <사용할 key 파일명> -out <생성할 csr 파일명>

openssl req -new -key test.key -out test.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:KR
State or Province Name (full name) []:Seoul
Locality Name (eg, city) [Default City]:Gwangjin
Organization Name (eg, company) [Default Company Ltd]:hello.com
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:trylhc.tistory.com
Email Address []:myemail@gmail.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

3. crt 파일 생성

위 csr 을 사용하여 인증서를 만든다. 파라미터로 csr 파일과 key 파일이 필요하며, 인증 유효 기간 등을 설정할 수 있다.

명령어 : openssl x509 -req -days <유효기간> -in <csr파일> -signkey <key파일> -out <crt파일명>

openssl x509 -req -days 365 -in test.csr -signkey test.key  -out test.crt

생성한 인증서의 정보를 다음의 명령어로 확인한다. 위 csr 파일 생성 당시에 설정한 CN 값 등을 확인 가능하다.

명령어 : openssl x509 -in <crt파일명> -noout -text

openssl x509 -in test.crt -noout -text
Certificate:
       ....
        Validity
            Not Before: Jun 29 05:23:08 2022 GMT
            Not After : Jun 29 05:23:08 2023 GMT
        Subject: C=KR, ST=Seoul, L=Gwangjin, O=hello.com, CN=trylhc.tistory.com/emailAddress=myemail@gmail.com
        ...

 

3.1 key, csr 파일 없이 crt 생성

key 생성, csr 생성과 같은 과정을 거치지 않고 바로 crt 파일을 생성가능하다. 이 경우 중간 과정인 csr 파일은 생성하지 않으며, key 파일과 crt 파일을 생성한다. 명령어는 하단과 같으며, key 파일의 패스워드를 제거하기 위해 '-nodes' 파라미터를 추가한다.

명령어 : openssl req -new -x509 -days <만료유효기간> -nodes -keyout <생성할 key파일> -out <생성할 crt파일>

openssl req -new -x509 -days 365 -nodes -keyout test.key -out test.crt
Generating a 2048 bit RSA private key
.......+++
...............+++
writing new private key to 'test.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:KR
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:test.com
Email Address []:

*.명령어 정리

하단 명령어 예제의 key,csr,crt 파일명은 test.key, test.csr, test.crt 를 사용한다.

key 에 암호화 알고리즘을 추가/제거하거나, csr(crt) 파일 생성 시의 대화형 입력에 대한 부분은 상단 내용을 참고한다.

# key 파일 생성
openssl genrsa -out test.key 2048

# csr 파일 생성
openssl req -new -key test.key -out test.csr

# crt 파일 생성
openssl x509 -req -days 365 -in test.csr -signkey test.key  -out test.crt

# csr없이 key,crt 파일 한번에 생성
openssl req -new -x509 -days 365 -nodes -keyout test.key -out test.crt

 

 

 

 

 

 

댓글