[OpenSSL] command line 명령으로 TLS 서버 실행하기

기본적으로 s_server 인자와 -accept 를 사용한다. cert파일과 key파일은 자체 인증서를 생성하든지, letsencrypt에서 얻든지 한다. https에 사용되는 인증서를 letsencrypt로부터 얻는 방법은 [TLS] Letsencrypt로부터 https 사용을 위한 인증서 얻기를 참고한다.

$ openssl s_server -accept 443 -key /etc/letsencrypt/live/blog.dasomoli.org/privkey.pem -cert /etc/letsencrypt/live/blog.dasomoli.org/fullchain.pem

-msg 를 사용하면 TLS 관련 메시지를 볼 수 있다. decrypted된 text만 보고 싶다면 -quiet 옵션을 주면 된다. stdout으로 출력된다.

[Security] TLS-PSK

일단 기본 개념부터.

PSK는 Pre-Shared Key의 줄임말로, 보안이 필요한 통신을 하는 양자 간에 미리 공유된 symmetric 키를 말한다.

TLS는 Transport Layer Security의 줄임말로, SSL이 표준화되면서 쓰이는 이름이다.

TLS-PSK는 PSK를 이용해서 TLS연결을 맺는 것으로 RFC 4279에 문서화되어 있다.

RFC 4279의 내용을 살펴보자.

      Client                                               Server
      ------                                               ------

      ClientHello                  -------->
                                                      ServerHello
                                                    (Certificate)
                                               ServerKeyExchange*
                                             (CertificateRequest)
                                   <--------      ServerHelloDone
      (Certificate)
      ClientKeyExchange
      (CertificateVerify)
      ChangeCipherSpec
      Finished                     -------->
                                                 ChangeCipherSpec
                                   <--------             Finished
      Application Data             <------->     Application Data

위 그림이 TLS handshake과정을 나타낸다.

1. 클라이언트는 ClientHello 메시지에 TLS Version과 하나 이상의 cipher-suite을 실어 보낸다.

2. 서버는 ServerHello 메시지에 사용할 TLS Version과 사용할 cipher-suite를 실어 보낸다. ServerKeyExchange 메시지로 클라이언트가 어떤 PSK를 사용할 지, PSK identity hint를 함께 보낸다. ServerHelloDone도 보낸다.

3. 클라이언트는 ServerKeyExchange에 포함된 PSK identity hint로 사용할 PSK를 고른다. 고른 PSK의 PSK identity를 ClientKeyExchange 메시지에 실어 보낸다. Premaster secret은 PSK가 N octets라면, uint16 타입의 N, N개의 0 octets, 두번째 uint16타입의 N, 그리고 PSK N octets.

OpenSSL에서는 Client 쪽에서는 PSK callback을 SSL_CTX_set_psk_client_callback()이나 SSL_set_psk_client_callback()을 통해 설정하도록 되어있는데, 이 callback을 Server로부터 받은 PSK Identity Hint를 읽어 valid한지를 check하고, 해당 hint에 맞는 PSK를 psk에, PSK identity를 identity argument에 채워주는 역할을 한다. callback은 psk의 length를 return해야 한다. 0이나 그 이하는 error를 나타낸다.

Server쪽에서는 SSL_CTX_use_psk_identity_hint()나 SSL_use_psk_identity_hint()를 통해서 사용할 PSK Identity Hint를 Setting하고, SSL_CTX_set_psk_server_callback()나 SSL_set_psk_server_callback()을 통해 callback을 셋팅한다. 이 callback은 PSK identity가 valid한지를 체크하고, PSK identity가 있다면, pre-shared key를 psk argument에 채우고, PSK의 길이를 return한다.

 

* 참고

RFC 4279 – PSK Ciphersuites for TLS(https://tools.ietf.org/html/rfc4279)

TLS: https://en.wikipedia.org/wiki/Transport_Layer_Security, https://namu.wiki/w/TLS

TLS-PSK: https://en.wikipedia.org/wiki/TLS-PSK

OpenSSL

SSL_CTX_set_psk_client_callback: https://www.openssl.org/docs/manmaster/ssl/SSL_CTX_set_psk_client_callback.html

SSL_CTX_use_psk_identity_hint: https://www.openssl.org/docs/manmaster/ssl/SSL_CTX_use_psk_identity_hint.html