OpenLDAPのSSL設定(自己署名証明書編)

概要

OpenLDAPの自己署名証明書でのSSL設定について記載します。

環境

OSRHEL7.5
認証管理openldap-2.4.44-13.el7.x86_64

前提条件

下記プロダクトがインストールされ、構築されていること。
※openldap-clientsも同サーバにインストールされている前提で記載していますが、別サーバでも問題ありません。

  • openldap
  • openldap-servers
  • openldap-clients

解説

OpenLDAPでのSSL化

OpenLDAPでのSSLとは、ldapsearch時など接続時にSSLを実施することになります。
必要になるのは、OpenLDAP側でSSL接続出来るような設定に変えることと、またSSL接続時に利用する証明書です。
今回は自己署名証明書を使った方法をご紹介します。

自己署名証明書の作成方法

OpenLDAPをSSL接続させるために使う自己署名証明書ですので必要最低限の作成方法をご紹介します。
※前提条件がクリアされている場合、何かを追加でインストールする必要はありません。
OpenLDAPの機能を使って作成します。

構築の流れ

  1. 証明書作成
  2. 設定変更
  3. 設定適用
  4. SSL接続確認

構築方法

[1] 証明書作成

  • 秘密鍵作成

パスフレーズを使って秘密鍵を作成します。

# cd /etc/pki/tls/certs/
# make ldaps.key
umask 77 ; \
/usr/bin/openssl genrsa -aes128 2048 > ldaps.key
Generating RSA private key, 2048 bit long modulus
...............+++
.........................................................................+++
e is 65537 (0x10001)
Enter pass phrase:パスフレーズを入力
Verifying - Enter pass phrase:パスフレーズを入力
# ls
Makefile       ca-bundle.trust.crt  make-dummy-cert
ca-bundle.crt  ldaps.key            renew-dummy-cert
  • パスフレーズ解除

パスフレーズを入力してEnterを押します。

# openssl rsa -in ldaps.key -out ldaps.key
Enter pass phrase for ldaps.key:パスフレーズを入力
writing RSA key
  • 証明書作成

証明書の入力項目は必要最低限あればいいので国名の「JP」以外は何も入力していません。

# make ldaps.csr
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key ldaps.key -out ldaps.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]:JP
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
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 []:
  • 有効期限設定

有効期限は10年(3650日)で設定します。

# openssl req -x509 -days 3650 -in ldaps.csr -key ldaps.key -out ldaps.crt
# ls
Makefile       ca-bundle.trust.crt  ldaps.csr  make-dummy-cert
ca-bundle.crt  ldaps.crt            ldaps.key  renew-dummy-cert
  • 証明書移動

OpenLDAPでは「/etc/openldap/certs/」という証明書を配置するディレクトリがインストール時に作成されるため、先ほど作成したファイルをこちらに移動します。

# cp ca-bundle.crt /etc/openldap/certs/
# mv ldaps.* /etc/openldap/certs/
# ls
Makefile  ca-bundle.crt  ca-bundle.trust.crt  make-dummy-cert  renew-dummy-cert
# ls /etc/openldap/certs/
ca-bundle.crt  key3.db    ldaps.csr  password
cert8.db       ldaps.crt  ldaps.key  secmod.db
  • 権限変更

移動したファイルをldapユーザが読み込めるように権限を変更します。

# chown ldap:ldap /etc/openldap/certs/ldaps.*
合計 292
-r--r--r--  1 root root 219895  7月 23  2018 ca-bundle.crt
-rw-r--r--. 1 root root  65536  8月  1 15:33 cert8.db
-rw-r--r--. 1 root root  16384  8月  1 15:33 key3.db
-rw-r--r--  1 ldap ldap   1220  1月 20 17:05 ldaps.crt
-rw-------  1 ldap ldap    952  1月 20 16:59 ldaps.csr
-rw-------  1 ldap ldap   1679  1月 20 16:50 ldaps.key
-r--r-----. 1 root ldap     45  7月 23  2018 password
-rw-r--r--. 1 root root  16384  7月 23  2018 secmod.db

[2] 設定変更

ファイル変更前にはバックアップを取っておくことを推奨します。

  • 接続時オプション設定変更

接続時オプションの設定ファイルを変更します。

# vi /etc/sysconfig/slapd
(変更前)
SLAPD_URLS="ldapi:/// ldap:///"
(変更後)
SLAPD_URLS="ldapi:/// ldap:/// ldaps:///"

※ちなみにですが、SLAPD_URLSの記述内容を制限(ldapiのみやldapsのみ等)することによってLDAP接続を制限することが可能です。
ですが、実施する際はよく検討した上で実施してください。

  • 動的設定用ファイル作成

更新用ldifファイルを作成します。

# vi /etc/openldap/ssl.ldif
dn: cn=config
changetype: modify
add: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/openldap/certs/ca-bundle.crt
-
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/openldap/certs/ldaps.crt
-
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/openldap/certs/ldaps.key
  • クライアント側の接続時オプション設定
# vi /etc/sysconfig/slapd
(追加)
TLS_REQCERT never

[3] 設定適用

  • 動的設定の適用
# ldapadd -Y EXTERNAL -H ldapi:// -f /etc/openldap/ssl.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "cn=config"
  • サービス再起動

サービスを再起動し、サービスが正常に上がっていることを確認します。

# systemctl restart slapd
# systemctl status slapd
  • ポート確認

LDAPS(636)が立ち上がっていれば接続可能です。

# ss -natu | grep 636
tcp    LISTEN     0      128       *:636                   *:*

[4] SSL接続確認

特に問題が起きなかった場合は確認してみましょう。
元々接続できていたldapsearch文を使うのが分かりやすくていいかと思います。

  • LDAP検索
  • 結果取得可能なldapsearchを確認
# ldapsearch -x -D "cn=Manager,dc=testdomain,dc=com" -w 設定したパスワード -b "dc=testdomain,dc=com"
  • ldapsを追加指定して確認
# ldapsearch -x -D "cn=Manager,dc=testdomain,dc=com" -w 設定したパスワード -b "dc=testdomain,dc=com"  -H ldaps:///

正常に結果が返ってきたらSSL化成功です。

  • ログ確認

ログはログ出力指定ファイルに出力されるため、どのファイルを確認すべきかは適宜読み替えてください。

# less /var/log/ldap/ldap.log
~省略~
XXX  X XX:XX:XX サーバ名 slapd[XXXX]: conn=1002 fd=11 TLS established tls_ssf=256 ssf=256
~省略~

これでLDAPSポートが立ち上がり、LDAPS接続可能なことが分かりました。

技術情報

商標について

関連サービス

OSS導入支援
OSS保守サポート

お気軽にお問い合わせください。応対時間 9:30-17:30 [ 土・日・祝日除く ]

お問い合わせ
  • X