python - How to connect to a LDAP server using a p12 certificate -
I want to connect to an LDAP server instead of using a .p12 certificate instead of using a username and password. Java solution looks for this
string ldapURL = "ldaps: //" + host + ":" + port; System.setProperty ("javax.net.ssl.keyStoreType", "PKCS 12"); System.setProperty ("javax.net.ssl.keyStore", keystroke); System.setProperty ("javax.net.ssl.keyStorePassword", keystore password); Hashtable env = new hashtable (); Env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); Env.put (references. PROVIDER_URL, ldapURL); Env.put (Context.SECURITY_PROTOCOL, "SSL"); Env.put (reference. REFERRAL, "Follow"); Try {// Initial Reference LdapContext ctx = New InitialLdapContext (env, null); // TLS credentials ctx.addToEnvironment (reference. SECURITY_AUTHENICATION, "External") to authenticate clients; SearchControls ctls = New SearchControls (); // string filter = "(object class = *) to match the search filter"; // Find objects using filters naming Nomination answer = CTX. Search ("ou =" + AMM type [I] + "," + site specific money, filter, CTL); ...
Can I do the same using Python? I can see an example example how to connect to a LDAP server using Python-ldap using a username and password, but it is not what I want. if this. If it is not possible to use the P12 certificate, it will also help me, if there is a solution using x509 certificates (.pem format).
If you use Python-LDAP, you can use these parameters to set You can.
Ldpkset_opshn (Ldpkopt_ksh_tls_kakertfile, "/pth/to/trustedsrtskpem") Ldpkset_opshn (Ldpkopt_ksh_tls_srtfile, "/pth/to/userkertkpem") LDAP. set_option (ldap.OPT_X_TLS_KEYFILE, "/path/to/user.key.pem") DS = ldap.initialize ( "ldaps: //ldap.example.com: port /" # ds = ldap.initialize () # If instead START_TLS using ldaps "ldap: //ldap.example.com: port /") # ds.start_tls_s ()
in this case:
-
trustedcerts .pem
is equivalent to the Trust Store. This is a combination of trusted certificates that you want in PEM format. You can alsoOPT_X_TLS_CACERTFILE
with individual can use a list with the certificate, but I think it is not supported by GNUTLS, so it depends on which TLS library Python-ldap And its OpenLDAP client library has been compiled against. More information on built-in direcives in -
usercert.pem
The certificate is in PEM format (if you extracted from your PKCS # 12 file) -
user.key .pem
is your private key (again, it should be removed from p12 file)
certificate and key extraction file OpenSSL from a PKCS # 12 with the use of this can be done:
openssl PKCS12 -in userstore.p12 -clcerts -nokeys out usercert.pem openssl PKCS12 -in userstore.p12 -nocerts - Note: If you have the private key (user.key .pem) in this manner ( -nodes
) For,
Note: strong> this will not be password-protected , so you have to make sure that this file is not readable by anyone else. I do not think that OpenLDAP (and its python binding too low) gives you a password interactively to solve that problem, but I'm not sure.
Comments
Post a Comment