postfixとdovecotのSSL化@CentOS8

iphoneのメール設定の現状確認

outgoing server
 SMTP: 
       Use SSL: on
       Authentication: Password
       Server Port: 587

Advanced - Incoming Settings
  Use SSL: off
  Authentication: Password
  IMAP Path Prefix: /
  Server Port: 143

この状態で送受信できている。

postfixはssl対応できている。

Incoming Settingsで、 Use SSLをonにすると、

Server Portが 993になるが、設定検証でnot respondingとなる。

dovecotがssl対応していない

一応、postfixの設定確認

/etc/postfix/main.cf
smtp_tls_security_level = may
→これでtlsが有効になっている
※smtpd_enforce_tls=yes は古い設定

tlsの付く設定は

smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem
smtpd_tls_key_file = /etc/pki/tls/private/postfix.key
smtp_tls_CApath = /etc/pki/tls/certs
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
smtpd_tls_security_level = may

証明書関連ファイルの確認

postfix.pem
postfix.key
ca-bundle.crt
 -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt

これらの証明書は存在していて、タイムスタンプがどうやらcentos8をインストールした日のようだ。

最初から入っていた?

とりあえず動いてるっぽい。

dovecotの設定

/etc/dovecot/dovecot.conf

protocols=imap pop3

で、imaps pop3sが有効になっていない?

protocols=imap3 pop3s

に変更してdovecot再起動すると

 5月 07 10:13:53 god dovecot[1269884]: doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:28: 
 ssl_cert_file has been replaced by ssl_cert = <file
 5月 07 10:13:53 god dovecot[1269884]: doveconf: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:29: 
 ssl_key_file has been replaced by ssl_key = <file
 5月 07 10:13:53 god dovecot[1269884]: master: Dovecot v2.3.8 (9df20d2db) starting up for imap, pop3
 5月 07 10:13:53 god dovecot[1269887]: config: Warning: NOTE: 
 You can get a new clean config file with: doveconf -Pn > dovecot-new.conf
 5月 07 10:13:53 god dovecot[1269887]: config: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:26: 
 'imaps' protocol can no longer be specified (use protocol>
 5月 07 10:13:53 god dovecot[1269887]: config: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:26: 
 'pop3s' protocol can no longer be specified (use protocol>
 5月 07 10:13:53 god dovecot[1269887]: config: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:28: 
 ssl_cert_file has been replaced by ssl_cert = <file
 5月 07 10:13:53 god dovecot[1269887]: config: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:29: 
 ssl_key_file has been replaced by ssl_key = <file

古い設定方法だったようだ。

imaps pop3sは imap pop3に戻す

/etc/dovecot/conf.d/10-ssl.conf

に設定する

ssl = no

を変更

ssl = yes

下記設定はデフォルトなのだが、見るとやっぱり証明書がある。

ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem

firewalldでimaps pop3sを開ける

# firewall-cmd --add-service={pop3s,imaps} --permanent
success

# firewall-cmd --reload
success

# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: cockpit dhcpv6-client dns http imap imaps pop3 pop3s samba smtp smtp-submission smtps
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

dovecot再起動すると

May  7 10:27:57 god dovecot[1270364]: imap-login: Error: Failed to initialize SSL server context: 
Can't load DH parameters: error:1408518A:SSL routines:ssl3_ctx_ctrl:dh key too small: 

セキュリティ的に厳しくなった的なことらしい。

https://doteya.at.webry.info/201907/article_5.html
5月 07 10:34:05 god dovecot[1270813]: doveconf: Warning: 
Obsolete setting in /etc/dovecot/conf.d/10-ssl.conf:50: ssl_>
 5月 07 10:34:05 god dovecot[1270813]: master: Dovecot v2.3.8 (9df20d2db) 
 starting up for imap, pop3
 5月 07 10:34:05 god dovecot[1270816]: config: Warning: 
 NOTE: You can get a new clean config file with: doveconf -Pn >>
 5月 07 10:34:05 god dovecot[1270816]: config: Warning: 
 Obsolete setting in /etc/dovecot/dovecot.conf:28: ssl_cert_fil>
 5月 07 10:34:05 god dovecot[1270816]: config: Warning: 
 Obsolete setting in /etc/dovecot/dovecot.conf:29: ssl_key_file>
 5月 07 10:34:05 god dovecot[1270816]: config: Warning: 
 Obsolete setting in /etc/dovecot/conf.d/10-ssl.conf:50: ssl_dh>
 5月 07 10:34:06 god dovecot[1270816]: config: Warning: 
 please set ssl_dh=

ssl_dh= を設定してください、だそうだ。

下記設定を書いて、dh.pemを作成する。

# DH parameters length to use.
# 2021/05/07
#ssl_dh_parameters_length = 1024
ssl_dh_parameters_length = 2048
ssl_dh=</etc/dovecot/dh.pem


# openssl dhparam 2048 -out /etc/dovecot/dh.pem
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
..............................
(略)

take a long timeと表示されるが1分もかからない

dovecot再起動すると

May  7 10:37:28 god dovecot[1271026]: doveconf: Fatal: 
Error in configuration file /etc/dovecot/conf.d/10-ssl.conf 
line 51: ssl_dh: Can't open file /etc/dovecot/dh.pem: No such file or directory

見てみると確かにdh.pemがない。

もう一回やるがやっぱりできない。

ディレクトリを変えてカレントディレクトリに作ったりしても、できない。

調べると、opensslのコマンドオプションの書き方がバージョンにより変わったらしい。

https://iww.hateblo.jp/entry/20191109/dhparam
# openssl dhparam -out /etc/dovecot/dh.pem 2048
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
.............+.................
(略)

# ls -l
合計 32
drwxr-xr-x. 2 root root 4096  5月  7 10:35 conf.d
-rw-r--r--. 1 root root  424  5月  7 10:47 dh.pem
-rw-r--r--. 1 root root 4507  5月  7 10:18 dovecot.conf

dh.pemができた!

dovecotを再起動する。

# systemctl status dovecot
● dovecot.service - Dovecot IMAP/POP3 email server
   Loaded: loaded (/usr/lib/systemd/system/dovecot.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-05-07 10:48:33 JST; 10s ago
     Docs: man:dovecot(1)
           http://wiki2.dovecot.org/
  Process: 1271015 ExecStop=/usr/bin/doveadm stop (code=exited, status=0/SUCCESS)
  Process: 1271512 ExecStartPre=/usr/libexec/dovecot/prestartscript (code=exited, status=0/SUCCESS)
 Main PID: 1271518 (dovecot)
    Tasks: 4 (limit: 4993)
   Memory: 5.2M
   CGroup: /system.slice/dovecot.service
           tq1271518 /usr/sbin/dovecot -F
           tq1271521 dovecot/anvil
           tq1271522 dovecot/log
           mq1271523 dovecot/config

 5月 07 10:48:33 god systemd[1]: Started Dovecot IMAP/POP3 email server.
 5月 07 10:48:33 god dovecot[1271518]: doveconf: Warning: NOTE: 
 You can get a new clean config file with: doveconf -Pn > dovecot-new.conf
 5月 07 10:48:33 god dovecot[1271518]: doveconf: Warning: 
 Obsolete setting in /etc/dovecot/dovecot.conf:28: 
 ssl_cert_file has been replaced by ssl_cert = <file
 5月 07 10:48:33 god dovecot[1271518]: doveconf: Warning: 
 Obsolete setting in /etc/dovecot/dovecot.conf:29: 
 ssl_key_file has been replaced by ssl_key = <file
 5月 07 10:48:33 god dovecot[1271518]: doveconf: Warning: 
 Obsolete setting in /etc/dovecot/conf.d/10-ssl.conf:50: 
 ssl_dh_parameters_length is no longer needed
 5月 07 10:48:33 god dovecot[1271518]: master: Dovecot v2.3.8 (9df20d2db) 
 starting up for imap, pop3
 5月 07 10:48:33 god dovecot[1271522]: config: Warning: NOTE: 
 You can get a new clean config file with: doveconf -Pn > dovecot-new.conf
 5月 07 10:48:33 god dovecot[1271522]: config: Warning: 
 Obsolete setting in /etc/dovecot/dovecot.conf:28: 
 ssl_cert_file has been replaced by ssl_cert = <file
 5月 07 10:48:33 god dovecot[1271522]: config: Warning: 
 Obsolete setting in /etc/dovecot/dovecot.conf:29: 
 ssl_key_file has been replaced by ssl_key = <file
 5月 07 10:48:33 god dovecot[1271522]: config: Warning: 
 Obsolete setting in /etc/dovecot/conf.d/10-ssl.conf:50: 
 ssl_dh_parameters_length is no longer needed

ssl_dh_parameter_lengthは必要ないようだ。

warningとかはまだ出てるけど、とりあえず動いたっぽい。

iphoneのメールアカウント設定で incoming serverのsslを有効にする。

verifyでサーバ証明書の警告がでた。

continueをタップする。

メールを送ってみる。

受信できた!

これで送受信ともにssl化できた!