본문 바로가기
프로그래밍/암호화

[암호화] OpenSSL

by 소나기_레드 2023. 3. 9.

출처 : http://www.solanara.net/solanara/openssl

개요

  • SSL(Secure Socket Layer)과 TLS(Transport Layer Security)는 보안 통신을 위해 사용하는 프로토콜의 이름이다. 이 프로토콜을 구현한 라이브러리중 하나가 OpenSSL이며 오픈 소스로 구현되어있다. OpenSSL라이브러리는 통신 전문을 암호화 하는 것은 물론, 문서를 암호화 할 수도 있다. (하지만 문서를 암호화 한다면 PGP를 사용하는게 더 쉬울 것이다. 윈디하나의 솔라나라: PGP, GnuPG를 참고하자)
  • SSL을 이용해 통신 내용을 암호화 하는 방법은, 패킷 스나이핑과 IP스푸핑으로 인한 피해를 줄여줄 뿐 그 이상은 안된다. 시스템이 해킹되지 않도록 하는게 아니라 해킹 되어도 피해를 줄일 수 있는 역할을 한다는 의미다. 즉 웹서버에 SSL 통신을 위한 인증서를 설치하고 '보안은 끝'이라고 생각하는 것은 문제가 있다는 의미다. [SSL을 설치했는데 왜 해킹을 당하나요?]라는 질문을 실제로 받은 적이 있는데 참 바보같은 질문인 셈이다
  • SSL의 단점은 한가지있다. 속도가 느리다는 것이다. 플레인과 비교한다면 너무 느리다. 패킷의 인코딩/디코딩에 많은 CPU자원을 소모하기 때문이다. 속도가 문제가 되는 경우에는 OpenSSL을 64비트로 컴파일 해보거나, CPU를 업그레이드 하거나, 암호화 가속기(Cryptographic accelerator)를 사용해보는것이 좋다.
  • OpenSSL의 소스가 공개되어있다고 해서 OpenSSL에 포함되어있는 알고리즘도 '무료'는 아니다. OpenSSL에 있는 알고리즘은 저작권이 있고, 대부분 특허가 취득되어 있다. 만약 OpenSSL의 특정 알고리즘을 사용하려면 사용하기 전에 법률가와 상의 하는것이 좋다. [./config no-idea no-mdc2 no-rc5]와 같이 특허권 문제가 될만한 알고리즘은 아예 사용 안하도록 설정하는 것도 하나의 방법이다. 자세한 사항은 배포 파일에 들어있는 README 파일의 PATENTS 섹션을 참고하자.
  • 또한 OpenSSL의 라이선스는 [OpenSSL License]와 [SSLeay License]를 따른다. 쉽게 말해 GPL이 아니기 때문에 OpenSSL을 이용해 개발할 때에는 라이선스를 확인해야 할 것이다. OpenSSL의 라이선스는 GPL과 호환되지 않기 때문에 GPL을 따르는 소프트웨어를 개발할때 OpenSSL을 사용할 수 없다. 이 경우는 Gnu TLS 라이브러리의 사용을 고려해야 한다.
  • 이외에도 SSL을 구현한 오픈 소스 라이브러리로는 LGPL라이선스의 GnuTLS(The GNU Transport Layer Security Library)가 있다.
  • OpenSSL The Open Source toolkit for SSL-TLS

OpenSSL 설치

아래의 방법 중 하나를 사용해 설치하자. 솔라나라는 [소스 설치]를 사용했으며 성능을 위해 소스로 설치할 것을 권한다.

  • 소스 설치1) 64bit 솔라리스를 사용한다면 OpenSSL에서 기본적으로 [solaris64-x86_64-cc] 으로 컴파일 할것이다. 이는 64비트 라이브러리를 생성하기 때문에 32비트 어플로 컴파일하는 경우 맞지 않다. 따라서 [./Configure shared solaris-x86-cc]명령을 사용해 강제로 32비트로 컴파일 해야 한다.
    2) OpenSSL은 반드시 테스트를 하자. OpenSSL은 컴파일시 기본 옵션으로 풀 옵티마이징을 하도록 되어있다. 이런 이유로 흔하지는 않지만 gcc, cc의 버그로 인해 테스트에 실패하는 경우가 있다. 이런경우 패치를 하거나, gcc, cc, openssl 버전을 낮추거나 gcc, cc의 옵티마이징을 꺼야 한다. [compiler: cc ...] 부분은 시스템마다 다를 수 있다.
  • root@wl ~/src # wget http://www.openssl.org/source/openssl-1.0.0g.tar.gz root@wl ~/src # tar xvfz openssl-1.0.0g.tar.gz root@wl ~/src # cd openssl-1.0.0g root@wl ~/src/openssl-1.0.0g # ./config shared enable-md2 zlib ... Configured for solaris-x86-cc. 1) root@wl ~/src/openssl-1.0.0g # make 메시지 생략 making all in tools... root@wl ~/src/openssl-1.0.0g # make test 2) ... compiler: cc -KPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -fast -O -Xa OPENSSLDIR: "/usr/local/ssl" 'test' is up to date. root@wl ~/src/openssl-1.0.0g # make install ... OpenSSL shared libraries have been installed in: /usr/local/ssl If this directory is not in a standard system path for dynamic/shared libraries, then you will have problems linking and executing applications that use OpenSSL libraries UNLESS: * you link with static (archive) libraries. If you are truly paranoid about security, you should use static libraries. * you use the GNU libtool code during linking (http://www.gnu.org/software/libtool/libtool.html) * you use pkg-config during linking (this requires that PKG_CONFIG_PATH includes the path to the OpenSSL shared library directory), and make use of -R or -rpath. (http://www.freedesktop.org/software/pkgconfig/) * you specify the system-wide link path via a command such as crle(1) on Solaris systems. * you add the OpenSSL shared library directory to /etc/ld.so.conf and run ldconfig(8) on Linux systems. * you define the LD_LIBRARY_PATH, LIBPATH, SHLIB_PATH (HP), DYLD_LIBRARY_PATH (MacOS X) or PATH (Cygwin and DJGPP) environment variable and add the OpenSSL shared library directory to it. One common tool to check the dynamic dependencies of an executable or dynamic library is ldd(1) on most UNIX systems. See any operating system documentation and manpages about shared libraries for your version of UNIX. The following manpages may be helpful: ld(1), ld.so(1), ld.so.1(1) [Solaris], dld.sl(1) [HP], ldd(1), crle(1) [Solaris], pldd(1) [Solaris], ldconfig(8) [Linux], chatr(1) [HP]. ... chmod 644 /usr/local/ssl/lib/pkgconfig/openssl.pc root@wl ~/src/openssl-1.0.0g # # 환경 설정 root@wl ~ # vi /etc/profile # for OpenSSL located at /usr/local/ssl if [ -d /usr/local/ssl ] then PATH=$PATH:/usr/local/ssl/bin LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/ssl/lib fi
  • 번들 사용
  • # 솔라리스 10u9 에는 0.9.7d 이 번들되어있다. PATH만 걸어준다. root@wl ~ # cd /usr/local/bin root@wl /usr/local/bin # ln -s /usr/sfw/bin/openssl openssl

OpenSSL 인증서 만들기

root@wl ~ # cd /usr/local/ssl/certs
root@wl /usr/local/ssl/certs # openssl req -newkey rsa:2048 -nodes -out req.pem -keyout req.key 1)
Generating a 2048 bit RSA private key
.......++++++
.......++++++
writing new private key to '/usr/local/ssl/certs/root.cert'
-----
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) [AU]:KR
State or Province Name (full name) [Some-State]:GyungGiDo
Locality Name (eg, city) []:Suwon
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Solanara
Organizational Unit Name (eg, section) []:SolanaraTeam
Common Name (eg, YOUR name) []:*.solanara.net 2)
Email Address []:admin@solanara.net
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
root@wl /usr/local/ssl/certs # ls -alF 3)

1) OpenSSL 버전이 낮으면 [-newkey rsa:2048]에서 오류가 난다. [-new]로 대체한다.
2) Apache 설정파일인 httpd.conf의 ServerName에 들어갈 값을 적어준다. 아파치는 이부분을 검사한다. www.solanara.net 과 같이 전체를 다 써줘도 되고, 위와 같이 '별표'를 사용할 수 있으며, 192.186.0.1과 같이 IP를 써도 된다. 다른 내용을 쓰면 인증을 다시 받아야 하는 불상사가 생길 수 있다. 유료 루트 인증기관에 보내기 전에 반드시 확인해야 한다.
3) 총 2개의 파일이 생성되었다. req.key는 공개키로, 유출되지 않고 잃어버리지 않도록 보관해야한다. req.pem (certificate signing request)파일은 CA(Certification Authority=인증기관, 예: 베리사인, 타우트)에게 보내 인증 받아야 한다.

Root CA가 되기

CA에서 인증 받으려면 비용이 들기 때문에 테스트용으로는 자신이 CA가 될 필요가 있다. 그런데 CA가 되려면 Root CA(최상위 인증 기관)의 인증이 필요하다. 그리고 그 인증을 받기 위해서는 비용이 든다. 따라서 여기서는 Root CA가 되는 방법을 소개한다. Root CA도 CA중 하나이므로 CA가 하는 일을 모두 할 수 있다.

root@wl /usr/local/ssl/certs # openssl genrsa -des3 -out ca.key 2048 1)
Generating RSA private key, 2048 bit long modulus
............................++++++
............++++++
e is 65537 (0x10001)
Enter pass phrase for ca.key:*****
Verifying - Enter pass phrase for ca.key: *****
root@wl /usr/local/ssl/certs # openssl req -new -x509 -days 365 -key ca.key -out ca.crt 2)
Enter pass phrase for ca.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) [AU]:KR
State or Province Name (full name) [Some-State]:GyungGiDo
Locality Name (eg, city) []:Suwon
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Solanara
Organizational Unit Name (eg, section) []:SolanaraTeam
Common Name (eg, YOUR name) []:Solanara CA
Email Address []:admin@solanara.net
root@wl /usr/local/ssl/certs # openssl x509 -req -CA ca.crt -CAkey ca.key -days 365 -in req.pem -out signed-req.pem -CAcreateserial 3)
Signature ok
subject=/C=KR/ST=GyungGiDo/L=Suwon/O=Solanara/OU=SolanaraTeam/CN=wl.solanara.net/emailAddress=admin@solanara.net
Getting CA Private Key
Enter pass phrase for ca.key:*****
root@wl /usr/local/ssl/certs # chmod 600 *
root@wl /usr/local/ssl/certs # ls -alF 4)
총 18
drwxr-xr-x   2 root     bin          512 10월 26일  13:44 ./
drwxr-xr-x  10 root     bin          512 10월 26일  13:40 ../
-rw-------   1 root     other       1671  4월  8일  11:27 ca.crt
-rw-------   1 root     other       1751  4월  8일  11:26 ca.key
-rw-------   1 root     other         17  4월  8일  11:49 ca.srl
-rw-------   1 root     other       1679  4월  8일  11:48 req.key
-rw-------   1 root     other       1074  4월  8일  11:48 req.pem
-rw-------   1 root     other       1330  4월  8일  11:49 signed-req.pem
root@wl /usr/local/ssl/certs # 

1) Root CA의 2048bit private key 파일을 만든다. 결과 파일은 ca.key 이다.
2) 인증서를 만든다. 결과파일은 ca.crt파일이다. ca.crt를 Internet Explorer에 등록해주면 '올바르지 않은 CA'라는 에러메시지를 없앨 수 있다.
3) 이제 Root CA가 되는 일은 끝났다. 클라이언트가 요청한 req.pem을 싸인해주자. 결과파일은 signed-req.pem이다. 베리싸인과 같은 인증 기관은 이 명령 한줄만 실행 해주면 된다. (그러면서 도대체 얼마를 받는거야... ㅡ,.ㅡ)
4) ca.key와 req.key는 소중하게 보관해야 한다. 절대 외부에 유출되서는 안된다.
※ 아파치를 포함한 유닉스 데몬에서 SSL을 사용할 때 사용하는 필수 정보는, req.key와 signed-req.pem 파일에 저장되어있다. 몇몇 데몬은 ca.crt의 내용까지 요구하는 경우도 있다. (형태가 파일인 경우가 대부분이지만 DB에 저장된 문자열일 수도 있다. 실제로 req.key 와 signed-req.pem 은 vi 에디터등으로 열어 내용을 볼 수 있는 텍스트 파일이다)

인증서 생성 정리

인증서를 빨리 생성하기 위한 명령을 정리했다.

root@wl /usr/local/ssl/certs # openssl req -newkey rsa:4096 -nodes -out req.pem -keyout req.key \
  -subj "/C=KR/ST=GyungGiDo/L=Suwon/O=Solanara/OU=SolanaraTeam/CN=*.solanara.net/emailAddress=admin@solanara.net"
root@wl /usr/local/ssl/certs # openssl genrsa -des3 -out ca.key 4096
Enter pass phrase for ca.key:12345
Verifying - Enter pass phrase for ca.key:12345
root@wl /usr/local/ssl/certs # openssl req -new -x509 -days 3650 -key ca.key -out ca.crt \
  -subj "/C=KR/ST=GyungGiDo/L=Suwon/O=Solanara/OU=SolanaraTeam/CN=Solanara CA/emailAddress=admin@solanara.net"
Enter pass phrase for ca.key:12345
root@wl /usr/local/ssl/certs # openssl x509 -req -CA ca.crt -CAkey ca.key -days 3650 -in req.pem -out signed-req.pem -CAcreateserial
Enter pass phrase for ca.key:12345
root@wl /usr/local/ssl/certs #

댓글