Openldap配置TLS加密传输(完整版——手动配置)
首先要實(shí)現(xiàn)openLDAP的編譯安裝以及配置
? ? ??? ??openLDAP的編譯安裝以及配置
????????? ?注意:上篇中的??3.?主配置文件slapd.conf? 中? 信息如下所示:? ?????????? ??
#修改域名及管理員賬戶名;???? suffix "dc=mirage,dc=com" rootdn "cn=AuthUsers,dc=mirage,dc=com"
為啥要用TLS?
Openldap默認(rèn)使用簡單驗(yàn)證,對(duì)slapd的所有訪問都使用明文密碼通過未加密通道進(jìn)行。為了確保信息安全,需要對(duì)信息進(jìn)行加密傳輸,SSL(Secure Sockets Layer)是一個(gè)可靠的解決方案。
它使用 X.509 證書,由可信任第三方(Certificate Authority (CA))進(jìn)行數(shù)字簽名的一個(gè)標(biāo)準(zhǔn)格式的數(shù)據(jù)。有效的數(shù)字簽名意味著已簽名的數(shù)據(jù)沒有被篡改。如果簽名的數(shù)據(jù)被更改,將不會(huì)通過驗(yàn)證
SSL/TLS 加密原理簡介
SSL/TLS 是基于 PKI 機(jī)制的加密方式,包括證書認(rèn)證、密鑰交換、非對(duì)稱加密、對(duì)稱加密。SSL/TLS 采用 CA 作為服務(wù)端和客戶端都信賴的具有權(quán)威性的組織,證書的頒發(fā)和認(rèn)證都依賴于 CA,并假定 CA 頒發(fā)的證書是可靠的、可信賴的,證書里面的內(nèi)容是真實(shí)的、有效的,并可用于客戶機(jī)和服務(wù)器進(jìn)行安全的可靠的通信加密。
SSL/TLS 證書用來認(rèn)證服務(wù)器和客戶機(jī)雙方的身份,并用于密鑰交換時(shí)候的非對(duì)稱加密。密鑰交換完畢之后,就可以用這個(gè)密鑰做通信數(shù)據(jù)的對(duì)稱加密了,具體的加密算法是由客戶機(jī)和服務(wù)器互相協(xié)商得來。服務(wù)器和客戶機(jī)由于 SSL/TLS 庫的不同以及用戶的配置不同,雙方支持的算法列表不完全相同,當(dāng)雙方做 SSL/TLS 握手的時(shí)候,就需要將自己支持的算法列表及優(yōu)先順序告知對(duì)方,一旦對(duì)方按照優(yōu)先順序找到了第一個(gè)支持的算法,那么協(xié)商完成,否則雙方協(xié)商失敗,SSL/TLS 連接斷開。
環(huán)境說明:
主機(jī)名 | 角色 | IP地址 |
ldapsrv02.contoso.com | openldap server | 192.168.49.139 |
ldapclient.contoso.com | openldap client + CA | 192.168.49.136 |
ldapsrv02是新添加的一臺(tái)CentOS 6.5,這里直接使用之前的openldap的安裝腳本進(jìn)行安裝,里面并未有任何數(shù)據(jù),是一個(gè)空的域(域名也是contoso.com,為之后做雙機(jī)同步做準(zhǔn)備)。
真實(shí)配置如下
注意: [root@ldapclient?~]?都是在ip為192.168.1.22的機(jī)器上的操作(client) [root@ldapsrv02?~] 都是在ip為192.168.1.93的機(jī)器上的操作(server) root@192.168.49.136:/root???都需要改為192.168.1.22 root@192.168.49.139:/root???都需要改為192.168.1.93 在本次操作中。CA為獨(dú)立的服務(wù)器,則需要將用戶的證書頒發(fā)請(qǐng)求通過ssh傳至CA服務(wù)端中,當(dāng)服務(wù)端完成簽發(fā)后,再通過ssh將用戶證書文件傳送給客戶端即可。 若OpenLDAP服務(wù)端 也作為CA,所以不需要通過SSH進(jìn)行傳輸數(shù)據(jù)。在生產(chǎn)環(huán)境中部署時(shí)不建議二者混合使用,而推薦采用獨(dú)立的服務(wù)器作為CA。 |
?
準(zhǔn)備工作:
在兩臺(tái)CentOS上安裝openssl.(這個(gè)是必須的)
yum -y install openssl
[root@ldapclient ~]# rpm -qa openssl
openssl-1.0.1e-15.el6.x86_64
[root@ldapsrv02 ~]# rpm -qa openssl
openssl-1.0.1e-15.el6.x86_64
一、建立CA中心
1)配置CA簽發(fā)信息
以下是在client端的操作,(可以先跳過這里):
這里填寫的信息 要記住,要與后邊CA服務(wù)器生成公鑰填寫的信息一致
cd /etc/pki/CA/
touch index.txt
echo 01 >serial
cp /etc/pki/tls/openssl.cnf /etc/pki/tls/openssl.cnf.bak$(date +%F)
vi /etc/pki/tls/openssl.cnf
[root@ldapclient CA]# diff /etc/pki/tls/openssl.cnf.bak2016-09-18 /etc/pki/tls/openssl.cnf
50c50
< certificate = $dir/cacert.pem # The CA certificate
---
> certificate = $dir/CA.crt ? ? # The CA certificate
55c55
< private_key = $dir/private/cakey.pem# The private key
---
> private_key = $dir/private/CA.key ? # The private key
130c130
< countryName_default = XX
---
> countryName_default = CN
135c135
< #stateOrProvinceName_default = Default Province
---
> stateOrProvinceName_default = Guangdong
138c138
< localityName_default = Default City
---
> localityName_default = Shenzhen
141c141
< 0.organizationName_default = Default Company Ltd
---
> 0.organizationName_default = contoso
148c148
< #organizationalUnitName_default =
---
> organizationalUnitName_default = Tech
2)CA服務(wù)器生成自己的私鑰
[root@ldapclient CA]# (umask 077;openssl genrsa -out private/CA.key)
Generating RSA private key, 1024 bit long modulus
...............++++++
..........++++++
e is 65537 (0x10001)
3)CA服務(wù)器生成公鑰(CA證書)
真實(shí)配置如下
? |
[root@ldapclient CA]# openssl req -new -x509 -key private/CA.key >CA.crt
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) [CN]:
State or Province Name (full name) [Guangdong]:
Locality Name (eg, city) [Shenzhen]:
Organization Name (eg, company) [contoso]:
Organizational Unit Name (eg, section) [Tech]:
Common Name (eg, your name or your server's hostname) []:ca.contoso.com
Email Address []:1217406852@qq.com
二、openldap server生成私鑰及證書請(qǐng)求文件
1)ldapsrv02生成自己的私鑰
[root@ldapsrv02 ~]# openssl genrsa -out ldapsrv02.key
Generating RSA private key, 1024 bit long modulus
...........................++++++
.....++++++
e is 65537 (0x10001)
2)ldapsrv02生成證書請(qǐng)求文件
真實(shí)配置如下
? 注意: ?在OpenLDAP服務(wù)器向CA申請(qǐng)證書簽署請(qǐng)求,時(shí)候。除了Common Name、Email Address 以外,以上所有值必須和CA證書所填寫的信息保持一致,否則無法得到驗(yàn)證。 |
[root@ldapsrv02 ~]# openssl req -new -key ldapsrv02.key -out ldapsrv02.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]:CN
State or Province Name (full name) []:Guangdong ?
Locality Name (eg, city) [Default City]:Shenzhen
Organization Name (eg, company) [Default Company Ltd]:contoso
Organizational Unit Name (eg, section) []:Tech
Common Name (eg, your name or your server's hostname) []:ldapsrv02.contoso.com
Email Address []:1217406852@qq.com
?
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:123456
三、CA服務(wù)器向openldap server簽發(fā)證書
1)ldapsrv02向CA請(qǐng)求證書
[root@ldapsrv02 ~]# scp ldapsrv02.csr root@192.168.49.136:/root
The authenticity of host '192.168.49.136 (192.168.49.136)' can't be established.
RSA key fingerprint is f9:ce:14:5d:cd:bb:3c:b4:0d:0b:fc:21:3a:92:43:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.49.136' (RSA) to the list of known hosts.
root@192.168.49.136's password:?
ldapsrv02.csr ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 100% ?777 ? ? 0.8KB/s ? 00:00 ? ?
2)CA服務(wù)器頒發(fā)證書
[root@ldapclient CA]# openssl ca -in /root/ldapsrv02.csr -out /root/ldapsrv02.crt
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
? ? ? ? Serial Number: 1 (0x1)
? ? ? ? Validity
? ? ? ? ? ? Not Before: Sep 18 08:22:46 2016 GMT
? ? ? ? ? ? Not After : Sep 18 08:22:46 2017 GMT
? ? ? ? Subject:
? ? ? ? ? ? countryName ? ? ? ? ? ? ? = CN
? ? ? ? ? ? stateOrProvinceName ? ? ? = Guangdong
? ? ? ? ? ? organizationName ? ? ? ? ?= contoso
? ? ? ? ? ? organizationalUnitName ? ?= Tech
? ? ? ? ? ? commonName ? ? ? ? ? ? ? ?= ldapsrv02.contoso.com
? ? ? ? ? ? emailAddress ? ? ? ? ? ? ?= 1217406852@qq.com
? ? ? ? X509v3 extensions:
? ? ? ? ? ? X509v3 Basic Constraints:?
? ? ? ? ? ? ? ? CA:FALSE
? ? ? ? ? ? Netscape Comment:?
? ? ? ? ? ? ? ? OpenSSL Generated Certificate
? ? ? ? ? ? X509v3 Subject Key Identifier:?
? ? ? ? ? ? ? ? 39:FB:0E:BD:7D:03:7E:F3:9E:C3:BE:5B:4D:45:8B:63:B7:8B:7F:26
? ? ? ? ? ? X509v3 Authority Key Identifier:?
? ? ? ? ? ? ? ? keyid:08:FF:94:52:EC:BD:97:12:5B:96:DD:1E:36:08:43:FF:AD:2F:7B:C8
?
Certificate is to be certified until Sep 18 08:22:46 2017 GMT (365 days)
Sign the certificate? [y/n]:y
?
?
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
四、openldap server下載并安裝證書
1)ldapsrv02下載證書
[root@ldapclient CA]# scp /root/ldapsrv02.crt root@192.168.49.139:/root
The authenticity of host '192.168.49.139 (192.168.49.139)' can't be established.
RSA key fingerprint is 88:23:68:20:34:ef:8b:5c:28:ef:c1:d5:ea:ef:4f:55.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.49.139' (RSA) to the list of known hosts.
root@192.168.49.139's password:?
ldapsrv02.crt ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 100% 3271 ? ? 3.2KB/s ? 00:00 ? ??
[root@ldapclient CA]# scp CA.crt ldapsrv02:/etc/openldap/certs/
The authenticity of host 'ldapsrv02 (192.168.49.139)' can't be established.
RSA key fingerprint is 88:23:68:20:34:ef:8b:5c:28:ef:c1:d5:ea:ef:4f:55.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ldapsrv02' (RSA) to the list of known hosts.
root@ldapsrv02's password:?
CA.crt ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?100% 1078 ? ? 1.1KB/s ? 00:00 ? ?
2)ldapsrv02安裝證書
mv ldapsrv02.crt /etc/openldap/certs/
mv ldapsrv02.key /etc/openldap/certs/
chown -R ldap:ldap /etc/openldap/certs/
vi /etc/openldap/ldap.conf ? ? #編輯ldap.conf文件
[root@ldapsrv02 ~]# tail -3 /etc/openldap/ldap.conf?
URI ? ?ldap://ldapsrv02.contoso.com
BASE ? dc=contoso,dc=com
TLS_CACERTDIR /etc/openldap/certs
vi /etc/openldap/slapd.conf
[root@ldapsrv02 ~]# grep TLS /etc/openldap/slapd.conf ?#編輯slapd.conf文件,主要是TLS配置
# The next three lines allow use of TLS for encrypting connections using a
TLSCACertificatePath /etc/openldap/certs
TLSCertificateFile /etc/openldap/certs/ldapsrv02.crt
TLSCertificateKeyFile /etc/openldap/certs/ldapsrv02.key
[root@ldapsrv02 ~]# service slapd stop ?#關(guān)閉slapd服務(wù)
Stopping slapd: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]
[root@ldapsrv02 ~]# slapd -h "ldap://192.168.49.139/ ldaps://192.168.49.139/" ?#重啟slapd服務(wù),這里同時(shí)監(jiān)聽加密和不加密端口
[root@ldapsrv02 ~]# lsof -i :389 ? ?#查看不加密的slapd端口389
COMMAND ?PID USER ? FD ? TYPE DEVICE SIZE/OFF NODE NAME
slapd ? 1757 root ? ?7u ?IPv4 ?14100 ? ? ?0t0 ?TCP *:ldap (LISTEN)
slapd ? 1757 root ? ?8u ?IPv6 ?14101 ? ? ?0t0 ?TCP *:ldap (LISTEN)
[root@ldapsrv02 ~]# lsof -i :636 ? ?#查看加密的slapd端口636
COMMAND ?PID USER ? FD ? TYPE DEVICE SIZE/OFF NODE NAME
slapd ? 1757 root ? ?9u ?IPv4 ?14104 ? ? ?0t0 ?TCP *:ldaps (LISTEN)
slapd ? 1757 root ? 10u ?IPv6 ?14105 ? ? ?0t0 ?TCP *:ldaps (LISTEN)
[root@ldapsrv02 ~]# netstat -tunlp|grep slapd ?#查看slapd監(jiān)聽的端口
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:636 ? ? ? ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?1757/slapd ? ? ? ? ?
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:389 ? ? ? ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?1757/slapd ? ? ? ? ?
tcp ? ? ? ?0 ? ? ?0 :::636 ? ? ? ? ? ? ? ? ? ? ?:::* ? ? ? ? ? ? ? ? ? ? ? ?LISTEN ? ? ?1757/slapd ? ? ? ? ?
tcp ? ? ? ?0 ? ? ?0 :::389 ? ? ? ? ? ? ? ? ? ? ?:::* ? ? ? ? ? ? ? ? ? ? ? ?LISTEN ? ? ?1757/slapd
五、ldapclient客戶端測試
1)下載CA公鑰
cp /etc/pki/CA/private/CA.key ?/etc/openldap/certs/
說明:因?yàn)槲业?/span>CA證書中心也同時(shí)作為ldap客戶端,所以這里只需要拷貝到對(duì)應(yīng)目錄即可,如果CA證書中心和ldap客戶端不在同一臺(tái)服務(wù)器上,需要使用scp或者rz等其他方式拷貝到ldap客戶端上。
2)修改ldap客戶端配置
cp /etc/openldap/ldap.conf /etc/openldap/ldap.conf.bak$(date +%F)
vi /etc/openldap/ldap.conf
[root@ldapclient CA]# /etc/init.d/nslcd restart
Stopping nslcd: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]
Starting nslcd: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]
[root@ldapclient CA]# ldapwhoami -v -x -Z
ldap_initialize( <DEFAULT> )
ldap_start_tls: Can't contact LDAP server (-1)
additional info: TLS error -8172:Peer's certificate issuer has been marked as not trusted by the user.
ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
上述錯(cuò)誤的解決方法是:
? ? ? ? 編輯/etc/openldap/ldap.conf,添加““TLS_REQCERT allow”,然后重啟nslcd服務(wù)即可。
vi /etc/openldap/ldap.conf
[root@ldapclient CA]# egrep -v "#|^$" /etc/openldap/ldap.conf
TLS_REQCERT ?allow
TLS_CACERTDIR /etc/openldap/certs
BASE dc=contoso,dc=com
URI ldaps://ldapsrv02.contoso.com/
/etc/init.d/nslcd restart
[root@ldapclient CA]# ldapwhoami -v -x -Z
ldap_initialize( <DEFAULT> )
ldap_start_tls: Operations error (1)
additional info: TLS already started
anonymous
Result: Success (0)
[root@ldapclient CA]# ldapsearch -x -D "cn=admin,dc=contoso,dc=com" -H ldaps://192.168.49.139 -W -b "dc=contoso,dc=com"
Enter LDAP Password:?
# extended LDIF
#
# LDAPv3
# base <dc=contoso,dc=com> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
?
# search result
search: 2
result: 32 No such object
?
# numResponses: 1
#因?yàn)閘dapsrv02僅僅是安裝了openldap,并沒有導(dǎo)入數(shù)據(jù),所以這里的查詢結(jié)果是正確的。
??
綜上所述: 配置后的文件大致如下:
? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?
在測試TLS傳輸加密時(shí)候可以進(jìn)行抓包查看 服務(wù)器端(192.168.1.93)--- ?監(jiān)聽: #tcpdump -i eth0 -w /dev/shm/ldap.pcap | grep -v ssh ? 客戶端(192.168.1.22)--- 查詢: #ldapsearch -x -LLL -D "cn=AuthUsers,dc=mirage,dc=com" -w asdfgh -H ldaps://192.168.1.93 -b "dc=mirage,dc=com" |
?
查看.crt / .pem /.csr文件:
[root@localhost CA]# openssl x509 -noout -text -in CA.crt
ldapsrv02.crt ?ldapsrv02.csr ?ldapsrv02.key?
測試過程 |
服務(wù)器端188: 1、首先查看slapd服務(wù)器進(jìn)程 [root@localhost ~]# ps aux | grep slapd 2、若不存在,執(zhí)行下面的語句 [root@localhost openldap]# /usr/local/libexec/slapd -h "ldap://192.168.1.188 ldaps://192.168.1.188" 3、再次查看 [root@localhost ~]# ps aux | grep slapd 4、若沒有正常啟動(dòng) 執(zhí)行下面語句 [root@localhost openldap]# /usr/local/libexec/slapd -h "ldap://192.168.1.188 ldaps://192.168.1.188" -d 256 (服務(wù)器端188:-d 256 是為debug) (若提示5b20eb1e daemon: bind(7) failed errno=98 (Address already in use) :說明slapd進(jìn)行顯示) (若提示庫TLS: error:0200A014:system library:opendir:Not a directory ssl_cert.c:821有問題:則說明是slapd.d下面的文件在改完slapd.conf文件后沒有更新) 5、服務(wù)器端查詢目錄樹條目(ldapsearch會(huì)通過/usr/local/etc/openldap/ldap.conf配置文件讀取basea和url值進(jìn)行查詢) [root@localhost ~]# ldapsearch -x -LLL (若提示ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1):說明ldap.conf中修改URI ?ldap://服務(wù)器IP地址或者使用域名形式(能被解析即可) ?或者是因?yàn)?/span>?slapd進(jìn)程沒有啟動(dòng)成功) |
? (單查看服務(wù)器端是否成功)確認(rèn)當(dāng)前的套接字是否能夠通過CA認(rèn)證(說明OpenLDAP服務(wù)端使用SSL/TLS加密傳輸協(xié)議) ? 服務(wù)器端: [root@localhost certs]# killall slapd [root@localhost certs]# /usr/local/libexec/slapd -h "ldaps://192.168.1.188" [root@localhost certs]# openssl s_client -connect 192.168.1.188:636 -showcerts -state -CAfile /usr/local/etc/openldap/certs/CA.crt? (成功顯示 ????00a0 - 28 86 fa 51 6d 17 4f be-41 0e 7f de c5 27 5e 6a ??(..Qm.O.A....'^j ? ????Compression: 1 (zlib compression) ????Start Time: 1528885067 ????Timeout ??: 300 (sec) ????Verify return code: 0 (ok)) |
? |
客戶端177: 客戶端端查詢目錄樹條目 [root@slave1 openldap]# ldapsearch -x -H "ldap://192.168.1.188" -b "dc=mirage,dc=com" (若提示ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1):說明cd /usr/local/etc/openldap/ldap.conf中TLS_REQCERT allow 這句沒有添加 或者是因?yàn)?/span>?服務(wù)器端slapd進(jìn)程沒有啟動(dòng)成功,需要去服務(wù)器端查看一下) |
? 客戶端查看通過CA證書公鑰驗(yàn)證OpenLDAP服務(wù)端證書的合法性 客戶端執(zhí)行# openssl verify -CAfile /etc/pki/CA/CA.crt /root/ldapsrv02.crt /root/ldapsrv02.crt: OK ??服務(wù)器端執(zhí)行[root@localhost certs]# openssl verify -CAfile /usr/local/etc/openldap/certs/CA.crt /root/openldap_server/ldapsrv02.crt? /root/openldap_server/ldapsrv02.crt: OK |
參考的鏈接:https://www.cnblogs.com/lemon-le/p/6266921.html
????????????????? ? https://m.linuxidc.com/Linux/2017-10/147558.htm&http:/m.linuxidc.com/Linux/2017-10/147558.htm
總結(jié)
以上是生活随笔為你收集整理的Openldap配置TLS加密传输(完整版——手动配置)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 函数 —— strncpy() (内存重
- 下一篇: openLDAP的编译安装以及配置