Tag Archives: OpenSSL

Self-Signed SSL로 Apache HTTPS 구현하기

apache-logo

기존의 HTTP(HyperText Transfer Protocol)과 다르게 HTTPS(Hypertext Transfer Protocol over Secure Socket Layer)는 SSL위에서 돌아가는 HTTP의 평문 전송 대신에 암호화된 통신을 하는 프로토콜입니다.

이런 HTTPS를 통신을 서버에서 구현하기 위해서는 신뢰할 수 있는 상위 기업이 발급한 인증서가 필요로 한데 이런 발급 기관을 CA(Certificate authority)라고 합니다. 하지만 이러한 기업의 인증서를 발급받는것은 무료가 아니며 단순히 모바일앱과의 통신이라던가 테스트 목적에서 발급을 받기에는 부담스러운 부분이 있을 수 있습니다.

이런 경우 자체적으로 인증서를 발급하여 사용하는 방법을 고려해 볼 수 있습니다. 이 경우 브라우저 접속시에 보안 경고가 발생하므로 주의하시기 바랍니다. 이 글에서는 CentOS와 Apache 웹서버 구동 환경에서의 HTTPS 구축하는 방법을 정리해 보겠습니다.

# yum install openssl mod_ssl

우선 위와 같이 openssl과 mod_ssl을 설치해줍니다. 만약 설치가 되어있는 상태라면 무시해도 됩니다. 이번엔 다음과 같은 명령을 사용하여 인증서를 생성해 줍니다.

(선택 1) 한번에 원하는 인증서를 발급하기

따로 원하는 CA를 둘것없이 간편하게 서버에서 사용할 인증서를 발급하는 방법은 다음과 같습니다.

# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/certs/mycert.key -out /etc/ssl/certs/mycert.crt
Generating a 2048 bit RSA private key
........................+++
writing new private key to '/etc/ssl/certs/mycert.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) []:Gyeonggi-do
Locality Name (eg, city) [Default City]:Seongnam-si
Organization Name (eg, company) [Default Company Ltd]:TheEye Company
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

(선택 2) 자체 CA 인증서를 생성하고 이를 통해 인증서를 발급하기

자체적으로 CA를 구축하는 방법도 있습니다. 세상 누구도 알아주지 않겠지만 다양한 서비스를 동시에 운영중이라면 해볼만한 시도라고 생각이 됩니다. 우선 CA 인증서부터 발급합니다.

# openssl genrsa -out /etc/ssl/certs/rootCA.key 2048
Generating RSA private key, 2048 bit long modulus
......................................................+++
..........+++
e is 65537 (0x10001)

# openssl req -x509 -new -nodes -key /etc/ssl/certs/rootCA.key -days 365 -out /etc/ssl/certs/rootCA.crt
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) []:Gyeonggi-do
Locality Name (eg, city) [Default City]:Seongnam-si
Organization Name (eg, company) [Default Company Ltd]:Theeye Company
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

이번엔 CA에 인증서 발급을 요청하기 위한 CSR(Certificate Signing Request)를 생성합니다.

# openssl genrsa -out /etc/ssl/certs/mycert.key 2048    
Generating RSA private key, 2048 bit long modulus
........+++
..........................+++
e is 65537 (0x10001)

# openssl req -new -key /etc/ssl/certs/mycert.key -out /etc/ssl/certs/mycert.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) []:Gyeonggi-do
Locality Name (eg, city) [Default City]:Seongnam-si
Organization Name (eg, company) [Default Company Ltd]:TheEye Company
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

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

이번엔 마지막으로 CA 인증서와 CSR을 이용하여 서비스에 사용할 인증서를 발급해 보도록 하겠습니다.

# openssl x509 -req -in /etc/ssl/certs/mycert.csr -CA /etc/ssl/certs/rootCA.crt -CAkey /etc/ssl/certs/rootCA.key -CAcreateserial -out /etc/ssl/certs/mycert.crt -days 365
Signature ok
subject=/C=KR/ST=Gyeonggi-do/L=Seongnam-si/O=TheEye Company
Getting CA Private Key

아파치 웹서버 설정에 적용

이제 /etc/ssl/certs 디렉토리에 mycert.key 파일과 mycert.crt 파일이 생성되었습니다. 이제 Apache 설정에 다음과 같은 형태로 이 키를 지정해 줍니다.

LoadModule ssl_module modules/mod_ssl.so

...

<VirtualHost 124.217.198.56:443>
  DocumentRoot /home/theeye/public_html
  ServerName theeye.pe.kr
  SSLEngine on
  SSLCertificateFile /etc/ssl/certs/mycert.crt
  SSLCertificateKeyFile /etc/ssl/certs/mycert.key
</VirtualHost>

이제 Apache 데몬을 재시작 한 뒤 해당 도메인에 https 로 접속하면 다음과 같은 보안 경고가 뜨게 됩니다.

not_secure_on_apache_https

하지만 아래의 안전하지 않음으로 이동을 통해 사이트에 정상적으로 접속하는 것이 가능합니다. 만약 iOS/Android에서 구동되는 어플리케이션의 경우 별도의 처리가 필요할 것입니다.

[Linux/SSL] 리눅스 서버에 Apache 2 설치 및 SSL 설정하기(mod_ssl, openssl)

세상에서 가장 많이 사용하고 있다는 아파치 웹서버에 SSL 설정을 하여 결과적으로 HTTPS를 사용할 수 있게 하는 방법에 대해 간략하게 정리해 보겠습니다. 아래는 개인적으로 자체 키를 생성해서 사용하는 방법을 적어볼 것이며 이것은 브라우저에서 접속시에 인증되지 않은 접속으로 경고가 뜨게 됩니다. 공인 인증 기관에서 키를 발급받을 경우 키 생성 부분만 발급 기관에서 요구하는 방법대로 수행을 하시면 됩니다.

1. Apache 웹서버 다운로드 및 설치

http://httpd.apache.org/ 에 방문하여 최신버전의 웹서버를 다운받습니다. 2.2.17 버전을 예로 들어 설명해 보겠습니다. 다운받은 파일을 적절한 위치로 이동후에 다음과 같은 명령어로 SSL을 활성화 시켜줍니다.

$ ./configure --prefix=/usr/local/apache2 --enable-ssl --enable-so
$ make
$ make install

위의 설명에서는 아파치 웹서버를 기본적으로 /usr/local/apache2 위치에 설치하는것으로 하였습니다.

2. SSL 설정하기

위와 같이 설치할 경우 아파치 웹서버의 설정 파일인 httpd.conf 파일은 /usr/local/apache2/conf 에 위치하게 됩니다. /usr/local/apache2/conf/httpd.conf 설정 파일을 열어 httpd-ssl.conf 추가 설정의 주석을 해제해 줍니다.

$ vi /usr/local/apache2/conf/httpd.conf

Include conf/extra/httpd-ssl.conf

3. server.crt 및 server.key 생성하기

우선 openssl을 이용하여 server.key를 생성합니다. 계정의 루트 디렉토리에서 작업을 하도록 하겠습니다.

$ cd ~
$ openssl genrsa -des3 -out server.key 1024

위의 명령을 수행할 경우 암호를 물어보게 됩니다. 여기서 설정하는 암호를 잘 기억해 두셔야 합니다. 나중에 아파치 실행시에 물어봅니다. 여기서 암호를 지정하지 않을경우 아파치 실행시에 암호를 물어오지 않습니다.

이번에는 위에서 생성한 server.key파일을 이용하여 인증요청파일을 생성합니다.

$ openssl req -new -key server.key -out server.csr

마지막으로 위에서 생성한 server.key 및 server.csr파일을 이용하여 자체적으로 서명을 한 server.crt파일을 생성합니다.

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

좀더 자세한 정보를 원할 경우 [다음]을 참고하도록 합시다.

이제 만들어진 인증키 파일을 아파치 설정 디렉토리로 옮기도록 합니다. 이 디렉토리는 적당한 위치를 사용하시면 됩니다.

$ cd ~
$ cp server.key /usr/local/apache2/conf/
$ cp server.crt /usr/local/apache2/conf/

4. SSL이 적용된 Apache 웹서버 구동하기

이제 설정이 끝난 아파치를 구동하도록 합시다.

$ /usr/local/apache2/bin/apachectl start

실행시에 위의 server.key를 생성할때 물어보았던 개인키 암호를 물어보게 되는데 입력해 주도록 합시다.

Apache/2.2.17 mod_ssl/2.2.17 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server www.example.com:443 (RSA)
Enter pass phrase:

OK: Pass Phrase Dialog successful.

5. 가상호스트(VirtualHost) 적용하기

필요한 경우 하나의 서버에 다수의 가상호스트를 적용해야 할 수도 있습니다. 이경우 다음과 같은 가상 호스트 설정을 사용하시면 됩니다. 물론 다수의 키를 사용해도 되고 중복 사용해도 별 문제는 없습니다.

<VirtualHost *:443>
    ServerAdmin eye@example.com
    DocumentRoot /usr/local/apache2/htdocs
    ServerName example.com:443

    ...

    SSLEngine on
    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    SSLCertificateFile "/usr/local/apache2/conf/server.crt"
    SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
</VirtualHost>

* CC 인증을 위한 키 생성

CC 인증을 받기 위해 SSL키 설정시 제약 사항이 추가되게 됩니다. 가령 다음과 같습니다.

  • 암호알고리즘으로 AES128 이상
  • 무결성알고리즘으로 SHA-2 (SHA256)
  • 키 알고리즘(RSA 1024이상)

이 경우 다음과 같이 키를 생성하시면 됩니다.

$ openssl genrsa -aes128 -out server.key 1024 
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -sha256 -req -days 365 -in server.csr -signkey server.key -out server.crt

참고 : http://www.thegeekstuff.com/2011/03/install-apache2-ssl/