简体   繁体   English

如何使用p12证书连接到LDAP服务器

[英]How to connect to a LDAP server using a p12 certificate

I want to connect to a LDAP server using a .p12 certificate instead of using a username and password. 我想使用.p12证书而不是用户名和密码连接到LDAP服务器。 The Java solution for this looks like Java解决方案看起来像

String ldapURL = "ldaps://"+host+":"+port;   

System.setProperty("javax.net.ssl.keyStoreType", "PKCS12" );  
System.setProperty("javax.net.ssl.keyStore",keystore);
System.setProperty("javax.net.ssl.keyStorePassword", keystorePassword);   

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapURL);
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.REFERRAL, "follow");

try 
{
    // Create initial context
    LdapContext ctx = new InitialLdapContext(env, null);
    // Perform client authentication using TLS credentials
    ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "EXTERNAL");

    SearchControls ctls = new SearchControls();
    // Specify the search filter to match
    String filter = "(objectClass=*)";
    // Search for objects using the filter
NamingEnumeration answer = ctx.search("ou="+elemType[i]+","+siteSpecificBaseDN, filter, ctls);

...

Can I do the same using python? 我可以使用python做同样的事情吗? I only could find examples showing how to connect to a LDAP server with python-ldap using a username and a password, but that is not what I need. 我只能找到显示如何使用用户名和密码使用python-ldap连接到LDAP服务器的示例,但这不是我所需要的。 If it is not possible using .p12 certificate, it would also help me, if there is a solution using x509 certificates (.pem format). 如果无法使用.p12证书,如果有使用x509证书(.pem格式)的解决方案,它也对我有帮助。

If you use python-ldap, you can use the TLS options to set these parameters. 如果使用python-ldap,则可以使用TLS选项设置这些参数。

ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, "/path/to/trustedcerts.pem")
ldap.set_option(ldap.OPT_X_TLS_CERTFILE, "/path/to/usercert.pem")
ldap.set_option(ldap.OPT_X_TLS_KEYFILE, "/path/to/user.key.pem")

ds = ldap.initialize("ldaps://ldap.example.com:port/")
# If using START_TLS instead of ldaps:
# ds = ldap.initialize("ldap://ldap.example.com:port/")
# ds.start_tls_s()

In this case: 在这种情况下:

  • trustedcerts.pem is the equivalent of the trust store. trustedcerts.pem与信任库等效。 It's a concatenation of the trusted certificates you want in PEM format. 它是您要使用PEM格式的受信任证书的串联。 You could also use a directory with individual certificates with OPT_X_TLS_CACERTFILE , but I think it's not supported by GnuTLS, so it depends on which TLS library python-ldap and its OpenLDAP client library have been compiled against. 您还可以使用带有OPT_X_TLS_CACERTFILE具有单个证书的目录,但是我认为GnuTLS不支持该目录,因此它取决于编译哪个TLS库python-ldap及其OpenLDAP客户端库。 More details on the underlying direcives in the OpenLDAP manual . OpenLDAP手册中有更多关于基本收益的详细信息。
  • usercert.pem is your user certificate, in PEM format (you'll have to extract it from your PKCS#12 file) usercert.pem是您的用户证书,采用PEM格式(您必须从PKCS#12文件中将其提取)
  • user.key.pem is your private key (again, it needs to be extracted from the p12 file) user.key.pem是您的私钥(同样,它需要从p12文件中提取)

Certificate and key extraction from a PKCS#12 file can be done with OpenSSL using this: 可以使用OpenSSL使用以下命令从PKCS#12文件中提取证书和密钥:

openssl pkcs12 -in userstore.p12 -clcerts -nokeys -out usercert.pem
openssl pkcs12 -in userstore.p12 -nocerts -nodes -out user.key.pem

Note: if you extract the private key (in user.key.pem) this way ( -nodes ), it will not be password-protected , so you'll need to make sure this file is not readable by anyone else. 注意:如果以此方式( -nodes )提取私钥(在user.key.pem中), 它将不受密码保护 ,因此您需要确保该文件不能被其他任何人读取。 I don't think OpenLDAP (and even less its Python binding) let you prompt for a password interactively to get around that problem, but I'm not sure. 我认为OpenLDAP(甚至更不用说它的Python绑定)不能让您交互式地提示输入密码来解决该问题,但是我不确定。

It looks like ldaptor can provide you with this functionality. 看起来ldaptor可以为您提供此功能。 It's built on top of twisted, which has support for SSL built into the twisted.internet.ssl module. 它建立在twisted之上, twisted.internet.ssl模块内置了对SSL的支持。

See: ldaptor.protocols.ldap.ldapclient.startTLS() 请参阅: ldaptor.protocols.ldap.ldapclient.startTLS()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM