SSL/TLS 자체 서명 (Self-signed) 인증서 생성

먼저 SSL의 개념을 이해하기 위해 SSL 그리고 HTTPS 포스트를 참고하자.

이 포스트에서 설명하는 것은 바로 ‘신뢰할 수 있는 3자’의 역할을 스스로 하는 법이다.
물론 이렇게 생성한 인증서는 root-ca에 등록된 인증서가 아니므로 웹 브라우저에서는 신뢰할 수 없는 인증서라는 메시지를 띄울 것이지만 인트라넷 환경이라거나 인증서 발급 비용이 부담1물론 Let’s encrypt 등 무료로 인증서를 발급해주는 기관도 있다. 되는 경우 자체 서명 인증서를 이용하여 https 서비스를 제공할 수 있게 된다. 적어도 http에 비해 보안성을 향상 시킬 수 있다.

준비
openssl 패키지가 필요하다. 아래 두 가지 방법으로 설치 여부 확인이 가능하다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
haedong@haedong:~:]$ rpm -qa | grep openssl
openssl-libs-1.0.2k-19.el7.x86_64
openssl-1.0.2k-19.el7.x86_64
haedong@haedong:~:]$ yum list installed |grep openssl
openssl.x86_64 1:1.0.2k-19.el7 @anaconda
openssl-libs.x86_64 1:1.0.2k-19.el7 @anaconda
haedong@haedong:~:]$ rpm -qa | grep openssl openssl-libs-1.0.2k-19.el7.x86_64 openssl-1.0.2k-19.el7.x86_64 haedong@haedong:~:]$ yum list installed |grep openssl openssl.x86_64 1:1.0.2k-19.el7 @anaconda openssl-libs.x86_64 1:1.0.2k-19.el7 @anaconda
haedong@haedong:~:]$ rpm -qa | grep openssl
openssl-libs-1.0.2k-19.el7.x86_64
openssl-1.0.2k-19.el7.x86_64
haedong@haedong:~:]$ yum list installed |grep  openssl
openssl.x86_64                     1:1.0.2k-19.el7                 @anaconda
openssl-libs.x86_64                1:1.0.2k-19.el7                 @anaconda

만약 설치 되어있지 않으면 “sudo yum install openssl” 명령으로 설치하자.

인증서를 생성하는 과정은 “개인키 발급” ->”인증요청서 작성” -> “인증 요청” -> “발급”2여기서의 발급은 당연히 파일의 생성이다. 의 과정을 거친다.

개인키 생성

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
haedong@haedong:~:]$ openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
......................................................+++
............................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
haedong@haedong:~:]$ ll
합계 4
-rw-rw-r-- 1 haedong haedong 1743 818 09:02 server.key
haedong@haedong:~:]$ openssl genrsa -des3 -out server.key 2048 Generating RSA private key, 2048 bit long modulus ......................................................+++ ............................................................+++ e is 65537 (0x10001) Enter pass phrase for server.key: Verifying - Enter pass phrase for server.key: haedong@haedong:~:]$ ll 합계 4 -rw-rw-r-- 1 haedong haedong 1743 8월 18 09:02 server.key
haedong@haedong:~:]$ openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
......................................................+++
............................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
haedong@haedong:~:]$ ll
합계 4
-rw-rw-r-- 1 haedong haedong 1743  8월 18 09:02 server.key

개인키 Passphrase 제거
생성된 개인키의 권한 확인을 위한 것으로 이 개인키로 생성된 인증서를 사용하는 서비스는 구동 시 매번 패스워드를 물어본다. 어차피 이 passphrase가 있건 없건 SSL 서비스를 제공하는데엔 아무런 문제도, 차이도 없다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
haedong@haedong:~:]$ cp server.key server.key.original
haedong@haedong:~:]$ openssl rsa -in server.key.original -out server.key
Enter pass phrase for server.key.original:
writing RSA key
haedong@haedong:~:]$ ll
합계 8
-rw-rw-r-- 1 haedong haedong 1675 818 09:06 server.key
-rw-rw-r-- 1 haedong haedong 1743 818 09:06 server.key.original
haedong@haedong:~:]$ cp server.key server.key.original haedong@haedong:~:]$ openssl rsa -in server.key.original -out server.key Enter pass phrase for server.key.original: writing RSA key haedong@haedong:~:]$ ll 합계 8 -rw-rw-r-- 1 haedong haedong 1675 8월 18 09:06 server.key -rw-rw-r-- 1 haedong haedong 1743 8월 18 09:06 server.key.original
haedong@haedong:~:]$ cp server.key server.key.original
haedong@haedong:~:]$ openssl rsa -in server.key.original -out server.key
Enter pass phrase for server.key.original:
writing RSA key
haedong@haedong:~:]$ ll
합계 8
-rw-rw-r-- 1 haedong haedong 1675  8월 18 09:06 server.key
-rw-rw-r-- 1 haedong haedong 1743  8월 18 09:06 server.key.original

인증 요청서 생성

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
haedong@haedong:~:]$ openssl req -new -key server.key -out server.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]:GANGNAMGU <- 시/군/구
Organization Name (eg, company) [Default Company Ltd]:HaeDong <- 조직,회사 이름
Organizational Unit Name (eg, section) []:INFRASECU <- 팀 이름
Common Name (eg, your name or your server's hostname) []:haedong.net <- 서버, 사이트 이름
Email Address []:haedonggang@naver.com <- 관리자 E-mail
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: <- Enter로 넘겨도 무방
An optional company name []: <- Enter로 넘겨도 무방
haedong@haedong:~:]$ ll
합계 12
-rw-rw-r-- 1 haedong haedong 1066 818 09:14 server.csr
-rw-rw-r-- 1 haedong haedong 1675 818 09:06 server.key
-rw-rw-r-- 1 haedong haedong 1743 818 09:06 server.key.original
haedong@haedong:~:]$ openssl req -new -key server.key -out server.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]:GANGNAMGU <- 시/군/구 Organization Name (eg, company) [Default Company Ltd]:HaeDong <- 조직,회사 이름 Organizational Unit Name (eg, section) []:INFRASECU <- 팀 이름 Common Name (eg, your name or your server's hostname) []:haedong.net <- 서버, 사이트 이름 Email Address []:haedonggang@naver.com <- 관리자 E-mail Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: <- Enter로 넘겨도 무방 An optional company name []: <- Enter로 넘겨도 무방 haedong@haedong:~:]$ ll 합계 12 -rw-rw-r-- 1 haedong haedong 1066 8월 18 09:14 server.csr -rw-rw-r-- 1 haedong haedong 1675 8월 18 09:06 server.key -rw-rw-r-- 1 haedong haedong 1743 8월 18 09:06 server.key.original
haedong@haedong:~:]$ openssl req -new -key server.key -out server.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]:GANGNAMGU                       <- 시/군/구
Organization Name (eg, company) [Default Company Ltd]:HaeDong           <- 조직,회사 이름
Organizational Unit Name (eg, section) []:INFRASECU                     <- 팀 이름
Common Name (eg, your name or your server's hostname) []:haedong.net    <- 서버, 사이트 이름
Email Address []:haedonggang@naver.com                                  <- 관리자 E-mail

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:                                                <- Enter로 넘겨도 무방
An optional company name []:                                            <- Enter로 넘겨도 무방
haedong@haedong:~:]$ ll
합계 12
-rw-rw-r-- 1 haedong haedong 1066  8월 18 09:14 server.csr
-rw-rw-r-- 1 haedong haedong 1675  8월 18 09:06 server.key
-rw-rw-r-- 1 haedong haedong 1743  8월 18 09:06 server.key.original

인증서 발급 (생성)
여기서의 인증서는 원래 ‘root-ca’기관에서 발급을 해주는 인증서를 말한다. 하지만 앞 단락에서 이야기 한 것처럼 이 과정은 내가 나 스스로에게 인증서를 요청하고 인증서를 발급 해주는 것이다. 즉 root-ca에서 인증서를 발급 받는 경우는 위에서 생성한 csr 파일을 root-ca로 보내고 root-ca에서 다시 crt 파일을 보내주는 과정을 거쳐야 한다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
haedong@haedong:~:]$ openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=KR/ST=SEOUL/L=GANGNAMGU/O=HaeDong/OU=INFRASECU/CN=haedong.net/emailAddress=haedonggang@naver.com
Getting Private key
haedong@haedong:~:]$ ll
합계 16
-rw-rw-r-- 1 haedong haedong 1322 818 09:21 server.crt
-rw-rw-r-- 1 haedong haedong 1066 818 09:14 server.csr
-rw-rw-r-- 1 haedong haedong 1675 818 09:06 server.key
-rw-rw-r-- 1 haedong haedong 1743 818 09:06 server.key.original
haedong@haedong:~:]$ openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt Signature ok subject=/C=KR/ST=SEOUL/L=GANGNAMGU/O=HaeDong/OU=INFRASECU/CN=haedong.net/emailAddress=haedonggang@naver.com Getting Private key haedong@haedong:~:]$ ll 합계 16 -rw-rw-r-- 1 haedong haedong 1322 8월 18 09:21 server.crt -rw-rw-r-- 1 haedong haedong 1066 8월 18 09:14 server.csr -rw-rw-r-- 1 haedong haedong 1675 8월 18 09:06 server.key -rw-rw-r-- 1 haedong haedong 1743 8월 18 09:06 server.key.original
haedong@haedong:~:]$ openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=KR/ST=SEOUL/L=GANGNAMGU/O=HaeDong/OU=INFRASECU/CN=haedong.net/emailAddress=haedonggang@naver.com
Getting Private key
haedong@haedong:~:]$ ll
합계 16
-rw-rw-r-- 1 haedong haedong 1322  8월 18 09:21 server.crt
-rw-rw-r-- 1 haedong haedong 1066  8월 18 09:14 server.csr
-rw-rw-r-- 1 haedong haedong 1675  8월 18 09:06 server.key
-rw-rw-r-- 1 haedong haedong 1743  8월 18 09:06 server.key.original

위의 crt 파일이 https 적용을 위한 인증서 되시겠다.
활용하고자 하는 서비스에 적용하면 된다. 아래는 제타위키를 참고한 apache https서비스를 위한 설정 파일 수정 예.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
haedong@haedong:~:]$ sudo cp server.key /etc/httpd/conf/
haedong@haedong:~:]$ sudo cp server.crt /etc/httpd/conf/
haedong@haedong:~:]$ sudo ll /etc/httpd/conf
total 60
-rw-r--r--. 1 root root 34417 Sep 20 07:41 httpd.conf
-rw-r--r--. 1 root root 13139 Feb 14 2012 magic
-rw-r--r--. 1 root root 1298 Sep 20 08:45 server.crt
-rw-r--r--. 1 root root 1679 Sep 20 08:45 server.key
haedong@haedong:~:]$ sudo vi /etc/httpd/conf
NameVirtualHost *:443
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/conf/server.crt
SSLCertificateKeyFile /etc/httpd/conf/server.key
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
CustomLog logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
DocumentRoot /var/www/html
</VirtualHost>
haedong@haedong:~:]$ sudo cp server.key /etc/httpd/conf/ haedong@haedong:~:]$ sudo cp server.crt /etc/httpd/conf/ haedong@haedong:~:]$ sudo ll /etc/httpd/conf total 60 -rw-r--r--. 1 root root 34417 Sep 20 07:41 httpd.conf -rw-r--r--. 1 root root 13139 Feb 14 2012 magic -rw-r--r--. 1 root root 1298 Sep 20 08:45 server.crt -rw-r--r--. 1 root root 1679 Sep 20 08:45 server.key haedong@haedong:~:]$ sudo vi /etc/httpd/conf NameVirtualHost *:443 <VirtualHost *:443> SSLEngine on SSLCertificateFile /etc/httpd/conf/server.crt SSLCertificateKeyFile /etc/httpd/conf/server.key SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown CustomLog logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" DocumentRoot /var/www/html </VirtualHost>
haedong@haedong:~:]$ sudo cp server.key /etc/httpd/conf/
haedong@haedong:~:]$ sudo cp server.crt /etc/httpd/conf/
haedong@haedong:~:]$ sudo ll /etc/httpd/conf
total 60
-rw-r--r--. 1 root root 34417 Sep 20 07:41 httpd.conf
-rw-r--r--. 1 root root 13139 Feb 14  2012 magic
-rw-r--r--. 1 root root  1298 Sep 20 08:45 server.crt
-rw-r--r--. 1 root root  1679 Sep 20 08:45 server.key
haedong@haedong:~:]$ sudo vi /etc/httpd/conf
NameVirtualHost *:443
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/conf/server.crt
SSLCertificateKeyFile /etc/httpd/conf/server.key
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
CustomLog logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
DocumentRoot /var/www/html
</VirtualHost>

당연하지만 apache 뿐 아니라 http/https 프로토콜을 연결을 제공하는 모든 서비스에 적용이 가능하다.

일반적으로 ssl 또는 tls 서비스는 443 또는 8443 포트를 사용한다.