zoukankan      html  css  js  c++  java
  • OpenLDAP配置TLS加密传输

    原文发表于cu2016-07-04

    参考文档:

    1. 基于OpenSSL自建CA与颁发SSL证书:http://seanlook.com/2015/01/18/openssl-self-sign-ca/
    2. OpenLDAP with TLS:http://my.oschina.net/aiguozhe/blog/151554

    一.环境

    Server:基于CentOS-7-x86_64-1511

    Server IP: 172.18.12.203

    OpenLDAP:openldap-2.4.44已安装

    二.准备工作

    1. 依赖包

    #理论上只需要openssl与openssl-devel
    yum install *openssl* -y

    openldap编译需要开启"--with-tls"选项,可通过"./configure --help"查看相关说明,请参考:http://www.cnblogs.com/netonline/p/7486832.html

    openssl相关依赖包一定要安装在openldap安装之前,在openldap安装之后再yum安装openssl相关依赖包,运行ldaps命令时时报" 573d212b daemon: TLS not supported (ldaps://0.0.0.0:636/)"错(如下图),安装openssl相关包之后重新编译安装openldap解决。

    可以使用"/usr/local/openldap-2.4.44/libexec/slapd"命令查看执行命令是否关联相应libraries,上面就是通过此方法定位故障点的:http://comments.gmane.org/gmane.network.openldap.technical/874

    2. iptables

    OpenLDAP with TLS/SSL默认使用tcp 636端口,提前在iptables放行。

    三.配置TLS

    在OpenLDAP的home目录创建1个子目录,后续操作均在此目录进行
    [root@localhost ~]# cd /usr/local/openldap-2.4.44
    [root@localhost openldap-2.4.44]# mkdir -p certs
    [root@localhost openldap-2.4.44]# cd certs/

    1. CA中心操作(如已有CA证书则跳过)

    生成CA根密钥(1)

    #带上”-des3”参数时,创建的私钥采取Triple DES算法加密,命令执行后会要求输入密码,这样后续在调用此密钥时都会要求输入密码,如 “openssl genrsa -des3 -out ca-key.pem 2048”,这里为了方便省略此参数
    [root@localhost certs]# openssl genrsa -out cakey.pem 2048
    
    #可以查看生成的rsa 私钥
    [root@localhost certs]# openssl rsa -noout -text -in cakey.pem
    
    #option选项,基于安全性考虑,建议修改根密钥权限为600或400
    [root@localhost certs]# chmod 600 cakey.pem

    生成CA根证书(2)

    #利用req命令与CA根证书生成自签署的根证书,证书有效期1年;
    #生成证书时,上方红色粗体字部分是要求输入的信息,其中需要注意的是”Common Name”请填写服务器域或IP
    [root@localhost certs]# openssl req -new -x509 -days 365 -key cakey.pem -out 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) [XX]:CN
    State or Province Name (full name) []:Sichuan
    Locality Name (eg, city) [Default City]:Chengdu
    Organization Name (eg, company) [Default Company Ltd]:SYS
    Organizational Unit Name (eg, section) []:IT
    Common Name (eg, your name or your server's hostname) []:172.18.12.203
    Email Address []:xxx@sys.com
    
    #可以查看生成的根证书
    [root@localhost certs]# openssl x509 -noout -text -in ca.crt

    2. OpenLDAP服务器端操作

    生成OpenLDAP服务器私钥(3)

    #同上,可带"-des3"参数,同步骤(1)
    [root@localhost certs]# openssl genrsa -out ldapkey.pem 2048

    生成OpenLDAP服务器证书签署请求文件(4)

    #请求文件需要发给CA中心签署生成证书,相当于公钥;
    #同步骤(2)会要求输入一些信息,相对于步骤(2)额外的信息可忽略
    [root@localhost certs]# openssl req -new -key ldapkey.pem -out ldapserver.csr
    
    #查看请求文件
    [root@localhost certs]# openssl req -noout -text -in ldapserver.csr

    3. CA中心签署证书

    签署证书的准备工作 (5)

    #如果CA中心准备工作已经做好,此步可跳过。
    [root@localhost certs]# cp /etc/pki/tls/openssl.cnf ./
    [root@localhost certs]# mkdir -p newcerts
    [root@localhost certs]# touch index.txt
    [root@localhost certs]# echo "00" > serial
    
    #修改第42行,证书生成配置文件的工作目录”dir    =/etc/pki/CA”修改为当前配置文件工作的目录,如下:
    [root@localhost certs]# vim openssl.cnf
    42 dir             = /usr/local/openldap-2.4.44/certs

    签署证书(6)

    #需要将OpenLDAP服务器生成的csr请求文件发给CA中;
    #生成证书会有两次确认,”y”即可;
    #如果重新签署证书,需要先将index.txt的内容用index.txt.old还原
    [root@localhost certs]# openssl ca -days 365 -cert ca.crt -keyfile cakey.pem -in ldapserver.csr -out ldapserver.crt -config openssl.cnf

    4. 复制证书/密钥到工作目录(7)

    #主要涉及CA中心的证书,CA中心为OpenLDAP服务器签署的证书与私钥
    [root@localhost certs]# mkdir -p /usr/local/openldap-2.4.44/etc/openldap/cacerts
    [root@localhost certs]# cp ca.crt /usr/local/openldap-2.4.44/etc/openldap/cacerts
    [root@localhost certs]# cp ldapserver.crt /usr/local/openldap-2.4.44/etc/openldap/
    [root@localhost certs]# cp ldapkey.pem /usr/local/openldap-2.4.44/etc/openldap/

    5. 修改OpenLDAP主配置文件slapd.conf(8)

    #可以在文件最后添加步骤(7)中证书/密钥的工作路径
    [root@localhost certs]# cd /usr/local/openldap-2.4.44/etc/openldap/
    [root@localhost openldap]# vim slapd.conf
    TLSCACertificateFile /usr/local/openldap-2.4.44/etc/openldap/cacerts/ca.crt
    TLSCertificateFile /usr/local/openldap-2.4.44/etc/openldap/ldapserver.crt
    TLSCertificateKeyFile /usr/local/openldap-2.4.44/etc/openldap/ldapkey.pem

    6. 启动LDAPS

    #”-d 256”是为debug,后台运行不需要;
    [root@localhost ~]# /usr/local/openldap-2.4.44/libexec/slapd -h “ldaps://0.0.0.0:636/” -d 256
    
    #或者:[root@localhost ~]# /usr/local/openldap-2.4.44/libexec/slapd -h “ldaps:///” -d 256
    
    #或者同时启动389与636端口:[root@localhost ~]# /usr/local/openldap-2.4.44/libexec/slapd -h “ldap:/// ldaps:///” -d 256

    7. 启动验证

    [root@localhost ~]# netstat –tunlp 

    8. 通过ldapdmin验证

    1) 修改已通过389端口可访问数据库的属性:数据库名,右键-->Properties;

    2) 修改389端口为636端口;

    3) 弹出窗口提示,选择" View Certificate",点击"Yes";

    4) 可以查看证书相关信息,选择"安装证书",一路默认"下一步";

    5) 证书导入成功;

    6) 通过636端口已可以访问数据库。

  • 相关阅读:
    java格式化sql
    Java 实现对Sql语句解析
    java 解析四则混合运算表达式并计算结果
    SQL转Java代码小工具
    eclipse导入maven工程步骤
    xml文件的schema也是经过jdk编译器编译的,如果xsd没引入完整,而xml中又用到了这些标签,就会编译不通过啊。
    eclipse bug之'<>'operator is not allowed for source level below 1.7
    eclipse默认指定项目的编译器版本
    eclipse导入maven项目后依赖jar包更新问题->update project按钮
    maven也是apache下的项目
  • 原文地址:https://www.cnblogs.com/netonline/p/7517685.html
Copyright © 2011-2022 走看看