欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

openldap加密传输 nslcd之 startTLS & LDAPS 有大用

http://www.openldap.org/faq/data/cache/185.html
https://www.ibm.com/developerworks/cn/linux/1312_zhangchao_opensslldap/
http://blog.sina.com.cn/s/blog_88cdde9f01019vdt.html
http://phorum.study-area.org/index.php?topic=68194.0
http://wiki.weithenn.org/cgi-bin/wiki.pl?OpenLDAP-SSL_TLS_%E8%A8%AD%E5%AE%9A

startTLS & LDAPS

  1. Transport Layer Security (TLS) is the standard name for the Secure Socket Layer (SSL). The terms (unless qualified with specific version numbers) are generally interchangable.
  2. StartTLS is the name of the standard LDAP operation for initiating TLS/SSL. TLS/SSL is initiated upon successful completion of this LDAP operation. No alternative port is necessary. It is sometimes referred to as the TLS upgrade operation, as it upgrades a normal LDAP connection to one protected by TLS/SSL.
  3. ldaps:// and LDAPS refers to "LDAP over TLS/SSL" or "LDAP Secured". TLS/SSL is initated upon connection to an alternative port (normally 636). Though the LDAPS port (636) is registered for this use, the particulars of the TLS/SSL initiation mechanism are not standardized.
  4. Once initiated, there is no difference between ldaps:// and StartTLS. They share the same configuration options (excepting ldaps:// requires configuration of a separate listener, see slapd(8)'s -h option) and result in like security services being established.

服务端

复制证书

cp /etc/pki/CA/{openldap.key,openldap.crt,ca.crt} /etc/openldap/certs/

配置slapd.conf

  1. TLSVerifyClient never # 设置是否验证 client 的身份,其值可以是 never/allow/try/demand,
  2. #never  不需要验证 client 端的身份,Client 端只需要有 CA 证书就可以了
  3. #allow  Server会要求 client 提供证书,如果 client 端没有提供证书,会话会正常进行
  4. #try    Client端提供了证书,但是 Server 端有可能不能校验这个证书,这个证书会被忽略,会话正常进行
  5. #demand Server端需要认证 client 端的身份,Client 端需要有自己的证书和私钥
  1. vim /etc/openldap/slapd.conf
  2. 添加以下项目
  3. TLSCACertificateFile /etc/openldap/certs/ca.crt
  4. TLSCertificateFile /etc/openldap/certs/openldap.crt
  5. TLSCertificateKeyFile /etc/openldap/certs/openldap.key
  6. TLSVerifyClient never #

启用LDAPS

  1. vim /etc/sysconfig/slapd
  2. SLAPD_URLS="ldapi:/// ldap:///" -> SLAPD_URLS="ldapi:/// ldaps:///"
  3. # 如果使用StartTLS,这个步骤不用执行

配置生效

  1. rm -rf /etc/openldap/slapd.d/*
  2. slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
  3. chown -R ldap:ldap /etc/openldap/slapd.d
  4. systemctl restart slapd

服务端口

  1. #StartTLS 继续使用389端口
  2. netstat -nlp -t |grep :389
  3. Active Internet connections (only servers)
  4. Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  5. tcp        0      0 0.0.0.0:389             0.0.0.0:*               LISTEN      1981/slapd
  6. tcp6       0      0 :::389                  :::*                    LISTEN      1981/slapd
  7. #LDAPS 启用636端口
  8. netstat -nlp -t |grep :636
  9. Active Internet connections (only servers)
  10. Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  11. tcp        0      0 0.0.0.0:636             0.0.0.0:*               LISTEN      1981/slapd
  12. tcp6       0      0 :::636                  :::*                    LISTEN      1981/slapd

测试StartTLS

  1. ldap服务器/etc/openldap/ldap.conf
  2. 添加以下内容
  3. TLS_REQCERT  never
  4. 执行ldapsearch -x -ZZ后,查看日志,内容有 TLS established tls_ssf=256 ssf=256, 服务端配置正常
  5. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 fd=22 ACCEPT from IP=[::1]:39720 (IP=[::]:389)
  6. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 op=0 EXT oid=1.3.6.1.4.1.1466.20037
  7. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 op=0 STARTTLS
  8. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 op=0 RESULT oid= err=0 text=
  9. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 fd=22 TLS established tls_ssf=256 ssf=256
  10. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 op=1 BIND dn="" method=128
  11. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 op=1 RESULT tag=97 err=0 text=
  12. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 op=2 SRCH base="" scope=2 deref=0 filter="(objectClass=*)"
  13. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 op=2 SEARCH RESULT tag=101 err=32 nentries=0 text=
  14. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 op=3 UNBIND
  15. Sep 22 15:23:23 openldap-01 slapd[1981]: conn=1003 fd=22 closed

测试LDAPS

  1. # openssl verify -CAfile /etc/openldap/certs/ca.crt /etc/openldap/certs/openldap.crt
  2. /etc/openldap/certs/openldap.crt: OK
  3. # openssl s_client -connect slave.local:636 -showcerts -state -CAfile /etc/openldap/certs/ca.crt
  4. ---
  5. Server certificate
  6. subject=/C=CN/ST=Beijing/O=TVM/OU=Tech Dept/CN=OPENLDAP
  7. issuer=/C=CN/ST=Beijing/L=Beijing/O=TVM/OU=Tech Dept/CN=CA
  8. ---
  9. No client certificate CA names sent
  10. Server Temp Key: ECDH, prime256v1, 256 bits
  11. ---
  12. SSL handshake has read 2354 bytes and written 375 bytes
  13. ---
  14. New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
  15. Server public key is 2048 bit
  16. Secure Renegotiation IS supported
  17. Compression: NONE
  18. Expansion: NONE
  19. SSL-Session:
  20. Protocol  : TLSv1.2
  21. Cipher    : ECDHE-RSA-AES256-GCM-SHA384
  22. Session-ID: 022E6922974AD42984230001FC3CD5923A44B73FFE94CE324BA12A58B120DDBF
  23. Session-ID-ctx:
  24. Master-Key: CCFF58FFF333BA758C31123C9DC469F4BA752B2464B6CE5C4B998012C329D319898F873617CD98F6970AEA7CE5F413D8
  25. Key-Arg   : None
  26. Krb5 Principal: None
  27. PSK identity: None
  28. PSK identity hint: None
  29. Start Time: 1474511415
  30. Timeout   : 300 (sec)
  31. Verify return code: 0 (ok)
  32. ---

客户端

使用nslcd(Naming services LDAP client daemon)

yum -y install openldap-clients nss-pam-ldapd

配置客户端

  1. # StartTLS
  2. authconfig --enableldap --enableldapauth --enableldaptls --ldapserver=ldap://master.local,ldap://slave.local --ldapbasedn='dc=suntv,dc=tv' --enablemkhomedir --update
  3. # LDAPS
  4. authconfig --enableldap --enableldapauth --enableldaptls --ldapserver=ldaps://master.local,ldaps://slave.local --ldapbasedn='dc=suntv,dc=tv' --enablemkhomedir --update
  5. # 注意 --ldapserver=ldaps://master.local,ldaps://slave.local

下载服务器的ca证书

wget http://master.local/ca.crt -O /etc/openldap/cacerts/ca.crt
  1. # ls -lh /etc/openldap/cacerts
  2. total 4.0K
  3. lrwxrwxrwx 1 root root    6 Sep 22 12:31 100934e9.0 -> ca.crt
  4. -rw------- 1 root root 1.3K Sep 22 12:30 ca.crt

配置/etc/openldap/ldap.conf

  1. TLS_REQCERT [never、allow、try、demand | hard] # 设置是否在TLS会话中检查server证书。
  2. Never:不检查任何证书。
  3. Allow:检查server证书,没有证书或证书错误,都允许连接。
  4. Try:检查server证书,没有证书(允许连接),证书错误(终止连接)。
  5. demand | hard:检查server证书,没有证书或证书错误都将立即终止连接。
  1. TLS_CACERTDIR /etc/openldap/cacerts
  2. TLS_CACERT /etc/openldap/cacerts/ca.crt
  3. TLS_REQCERT never

配置/etc/nslcd.conf

  1. ssl start_tls # StartTLS
  2. 或ssl on # LDAPS
  3. tls_cacertdir /etc/openldap/cacerts
  4. tls_cacertfile /etc/openldap/cacerts/ca.crt
  5. tls_reqcert never

重启nslcd服务

  1. systemctl restart nslcd
  2. systemctl enable nslcd

配置/etc/nsswitch.conf

  1. 变更为
  2. passwd:     files ldap
  3. shadow:     files ldap
  4. group:      files ldap

测试

  1. # ldapwhoami -v -x -Z
  2. ldap_initialize( <DEFAULT> )
  3. ldap_start_tls: Operations error (1)
  4. additional info: TLS already started
  5. anonymous
  6. Result: Success (0)
  1. # ldapsearch -x -Z -H ldaps://slave.local -b 'ou=group,dc=suntv,dc=tv'
  2. ldap_start_tls: Operations error (1)
  3. additional info: TLS already started
  4. # extended LDIF
  5. #
  6. # LDAPv3
  7. # base <ou=group,dc=suntv,dc=tv> with scope subtree
  8. # filter: (objectclass=*)
  9. # requesting: ALL
  10. #
  11. # Group, suntv.tv
  12. dn: ou=Group,dc=suntv,dc=tv
  13. ou: Group
  14. objectClass: top
  15. objectClass: organizationalUnit
  16. # g01, Group, suntv.tv
  17. dn: cn=g01,ou=Group,dc=suntv,dc=tv
  18. objectClass: posixGroup
  19. objectClass: top
  20. cn: g01
  21. gidNumber: 2001
  22. # g02, Group, suntv.tv
  23. dn: cn=g02,ou=Group,dc=suntv,dc=tv
  24. objectClass: posixGroup
  25. objectClass: top
  26. cn: g02
  27. gidNumber: 2002
  28. # search result
  29. search: 3
  30. result: 0 Success
  31. # numResponses: 4
  32. # numEntries: 3

帐号登录测试

  1. ssh u01@10.0.1.53
  2. passwd




转载至https://www.cnblogs.com/liujitao79/p/5893561.html


来自  https://blog.csdn.net/vic_qxz/article/details/80909582

普通分类: