문서의 이전 판입니다!


SSL on Ubuntu 8.10 Apache2

OpenSSL ROOT CA

웹서비스에 https 를 적용할 경우 SSL 인증서를 VeriSign 이나 Thawte, GeoTrust 등에서 인증서를 발급받아야 하지만 비용이 발생하므로 실제 운영 서버가 아니면 발급 받는데 부담이 될 수 있다. 이럴때 OpenSSL 을 이용하여 인증기관을 만들고 Self signed certificate 를 생성하고 SSL 인증서를 발급하는 법을 정리해 본다. 발급된 SSL 인증서는 apache httpd 등의 Web Server 에 설치하여 손쉽게 https 서비스를 제공할 수 있다.

https://www.lesstif.com/pages/viewpage.action?pageId=6979614

Self Signed Certificate(SSC)란 ? 인증서(digital certificate)는 개인키 소유자의 공개키(public key)에 인증기관의 개인키로 전자서명한 데이타다. 모든 인증서는 발급기관(CA) 이 있어야 하나 최상위에 있는 인증기관(root ca)은 서명해줄 상위 인증기관이 없으므로 root ca의 개인키로 스스로의 인증서에 서명하여 최상위 인증기관 인증서를 만든다. 이렇게 스스로 서명한 ROOT CA 인증서를 Self Signed Certificate 라고 부른다. IE, FireFox, Chrome 등의 Web Browser 제작사는 VeriSign 이나 comodo 같은 유명 ROOT CA 들의 인증서를 신뢰하는 CA로 미리 등록해 놓으므로 저런 기관에서 발급된 SSL 인증서를 사용해야 browser 에서는 해당 SSL 인증서를 신뢰할수 있는데 OpenSSL 로 만든 ROOT CA와 SSL 인증서는 Browser가 모르는 기관이 발급한 인증서이므로 보안 경고를 발생시킬 것이나 테스트 사용에는 지장이 없다. ROOT CA 인증서를 Browser에 추가하여 보안 경고를 발생시키지 않으려면 Browser 에 SSL 인증서 발급기관 추가하기 를 참고하자.

CSR(Certificate Signing Request)은? 공개키 기반(PKI)은 private key(개인키)와 public key(공개키)로 이루어져 있다. 인증서라고 하는 것은 내 공개키가 맞다고 인증기관(CA)이 전자서명하여 주는 것이며 나와 보안 통신을 하려는 당사자는 내 인증서를 구해서 그 안에 있는 공개키를 이용하여 보안 통신을 할 수 있다. CSR 은 인증기관에 인증서 발급 요청을 하는 특별한 ASN.1 형식의 파일이며(PKCS#10 - RFC2986) 그 안에는 내 공개키 정보와 사용하는 알고리즘 정보등이 들어 있다. 개인키는 외부에 유출되면 안 되므로 저런 특별한 형식의 파일을 만들어서 인증기관에 전달하여 인증서를 발급 받는다. SSL 인증서 발급시 CSR 생성은 Web Server 에서 이루어지는데 Web Server 마다 방식이 상이하여 사용자들이 CSR 생성등을 어려워하니 인증서 발급 대행 기관에서 개인키까지 생성해서 보내주고는 한다. ROOT CA 인증서 생성 openssl 로 root ca 의 개인키와 인증서를 만들어 보자 CA 가 사용할 RSA key pair(public, private key) 생성 2048bit 개인키 생성

openssl genrsa -aes256 -out /etc/pki/tls/private/vaslor-rootca.key 2048

개인키 분실에 대비해 AES 256bit 로 암호화한다. AES 이므로 암호(pass phrase)를 분실하면 개인키를 얻을수 없으니 꼭 기억해야 한다. 개인키 권한 설정 보안 경고 개인키의 유출 방지를 위해 group 과 other의 permission 을 모두 제거한다. chmod 600 /etc/pki/tls/private/vaslor-rootca.key CSR(Certificate Signing Request) 생성을 위한 rootca_openssl.conf 로 저장 rootca_openssl.conf [ req ] default_bits = 2048 default_md = sha1 default_keyfile = vaslor-rootca.key distinguished_name = req_distinguished_name extensions = v3_ca req_extensions = v3_ca

[ v3_ca ] basicConstraints = critical, CA:TRUE, pathlen:0 subjectKeyIdentifier = hash ##authorityKeyIdentifier = keyid:always, issuer:always keyUsage = keyCertSign, cRLSign nsCertType = sslCA, emailCA, objCA [req_distinguished_name ] countryName = Country Name (2 letter code) countryName_default = KR countryName_min = 2 countryName_max = 2

# 회사명 입력 organizationName = Organization Name (eg, company) organizationName_default = vaslor Inc.

# 부서 입력 #organizationalUnitName = Organizational Unit Name (eg, section) #organizationalUnitName_default = Condor Project

# SSL 서비스할 domain 명 입력 commonName = Common Name (eg, your name or your server's hostname) commonName_default = vaslor's Self Signed CA commonName_max = 64 인증서 요청 생성 root@vaslor:~:> openssl req -new -key /etc/pki/tls/private/vaslor-rootca.key -out /etc/pki/tls/certs/vaslor-rootca.csr -config rootca_openssl.conf 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) [KR]: Organization Name (eg, company) [vaslor Inc]:vaslor Inc. Common Name (eg, your name or your servers hostname) [vaslor's Self Signed CA]:vaslor's Self Signed CA 10년짜리 self-signed 인증서 생성 -extensions v3_ca 옵션을 추가해야 한다.

openssl x509 -req -days 3650 -extensions v3_ca -set_serial 1 -in /etc/pki/tls/certs/vaslor-rootca.csr \ -signkey /etc/pki/tls/private/vaslor-rootca.key -out /etc/pki/tls/certs/vaslor-rootca.crt -extfile rootca_openssl.conf

제대로 생성되었는지 확인을 위해 인증서의 정보를 출력해 본다. openssl x509 -text -in /etc/pki/tls/certs/vaslor-rootca.crt

SSL 인증서 생성 위에서 생성한 root ca 서명키로 SSL 인증서를 발급해 보자 SSL 호스트에서 사용할 RSA key pair(public, private key) 생성 2048bit 개인키 생성

openssl genrsa -aes256 -out /etc/pki/tls/private/vaslor.com.key 2048

Remove Passphrase from key 개인키를 보호하기 위해 Key-Derived Function 으로 개인키 자체가 암호화되어 있다. 인터넷 뱅킹등에 사용되는 개인용 인증서는 당연히 저렇게 보호되어야 하지만 SSL 에 사용하려는 키가 암호가 걸려있으면 httpd 구동때마다 pass phrase 를 입력해야 하므로 암호를 제거한다. 개인키 pass phrase 제거 cp /etc/pki/tls/private/vaslor.com.key /etc/pki/tls/private/vaslor.com.key.enc

openssl rsa -in /etc/pki/tls/private/vaslor.com.key.enc -out /etc/pki/tls/private/vaslor.com.key

보안 경고 개인키의 유출 방지를 위해 group 과 other의 permission 을 모두 제거한다. chmod 600 /etc/pki/tls/private/vaslor.com.key* CSR(Certificate Signing Request) 생성을 위한 host_openssl.conf 로 저장 host_openssl.conf [ req ] default_bits = 2048 default_md = sha1 default_keyfile = vaslor-rootca.key distinguished_name = req_distinguished_name extensions = v3_user ## 인증서 요청시에도 extension 이 들어가면 authorityKeyIdentifier 를 찾지 못해 에러가 나므로 막아둔다. ## req_extensions = v3_user

[ v3_user ] # Extensions to add to a certificate request basicConstraints = CA:FALSE authorityKeyIdentifier = keyid,issuer subjectKeyIdentifier = hash keyUsage = nonRepudiation, digitalSignature, keyEncipherment ## SSL 용 확장키 필드 extendedKeyUsage = serverAuth,clientAuth subjectAltName = @alt_names [ alt_names] ## Subject AltName의 DNSName field에 SSL Host 의 도메인 이름을 적어준다. ## 멀티 도메인일 경우 *.vaslor.com 처럼 쓸 수 있다. DNS.1 = www.vaslor.com DNS.2 = vaslor.com DNS.3 = *.vaslor.com

[req_distinguished_name ] countryName = Country Name (2 letter code) countryName_default = KR countryName_min = 2 countryName_max = 2

# 회사명 입력 organizationName = Organization Name (eg, company) organizationName_default = vaslor Inc.

# 부서 입력 organizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = vaslor SSL Project

# SSL 서비스할 domain 명 입력 commonName = Common Name (eg, your name or your server's hostname) commonName_default = vaslor.com commonName_max = 64 SSL 인증서 요청 root@vaslor:~:> openssl req -new -key /etc/pki/tls/private/vaslor.com.key -out /etc/pki/tls/certs/vaslor.com.csr -config host_openssl.conf 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) [KR]: Organization Name (eg, company) [vaslor Inc]:vaslor's Self Signed CA Common Name (eg, your name or your servers hostname) [vaslor.com]:*.vaslor.com 5년짜리 vaslor.com 용 SSL 인증서 발급 (서명시 ROOT CA 개인키로 서명) openssl x509 -req -days 1825 -extensions v3_user -in /etc/pki/tls/certs/vaslor.com.csr \ -CA /etc/pki/tls/certs/vaslor-rootca.crt -CAcreateserial \ -CAkey /etc/pki/tls/private/vaslor-rootca.key \ -out /etc/pki/tls/certs/vaslor.com.crt -extfile host_openssl.conf 제대로 생성되었는지 확인을 위해 인증서의 정보를 출력해 본다. openssl x509 -text -in /etc/pki/tls/certs/vaslor.com.crt

SSL

Setting up SSL with Ubuntu 8.10 is a simple process but it does have a few gotchas that you need to be aware of. The setup has changed from 8.04. One issue is that the +CompatEnvVars is no longer used as it created a bug in 8.10 and you will have to enable the default-ssl site to get everything working.

First, log on to your server Install Apache:

sudo apt-get install apache2

Change to the /etc/apache2/mods-available directory and look at the available modules. Then change to the /etc/apache2/mods-enabled directory to see what modules are enabled:

cd /etc/apache2/mods-available ls cd /etc/apache2/mods-enabled ls

Now, install and enable SSL:

sudo a2enmod ssl sudo /etc/init.d/apache2 force-reload

Change to the default webserver directory, and create a simple web page: cd /var/www sudo vim index.html

Add the following content: <html> <head> <title>Welcome to Your_Name’s Web Site</title> </head> <body> <p>This is the best web site in the whole wide world. </p> </body> </html>

Save and exit. On your own local computer, open a tab or window for your web browser. For the URL, enter:

http://IP_address_of_my_server

You should be able to view your web page. Now, you’ll want to encrypt your site. Create the server encryption keys:

cd /etc/apache2 sudo openssl genrsa -des3 -out server.key 1024

Use this set of keys to create a certificate request:

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

When asked to input data, use your imagination to create something appropriate. Be sure to write down your passphrase. Use this request to create your self-signed certificate:

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

Install the key and certificate:

sudo cp server.crt /etc/ssl/certs/ sudo cp server.key /etc/ssl/private/

Open the “defaults” file for editing:

cd /etc/apache2/sites-available sudo vim default-ssl

This file is basically set up but you will want to uncomment the SSLOptions line and also change the SSLCertificate lines to reflect the location and name of your new information.

SSLEngine on SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire SSLCertificateFile /etc/ssl/certs/server.crt SSLCertificateKeyFile /etc/ssl/private/server.key

The port 443 is enabled when you use SSL so that is ready to go.

Enable the default SSL site: sudo a2ensite default-ssl

If you do not enable the default-ssl you will get this error: “ssl_error_rx_record_too_long apache”

Restart Apache.

sudo /etc/init.d/apache2 restart

That should do it.

누구나 수정하실 수 있습니다. 문법은 Formatting Syntax참조하세요.