CA user query tool

Source: Internet
Author: User
Tags ldap

The CA is easy to establish, but it still needs to develop a set of interfaces to quickly obtain data information.

The backend connects to LDAP through PHP to obtain data and certificates. The front-end can directly obtain the parsed content for use, or export the certificate (in PEM format ).

Notes:

1. Pay attention to the search items in LDAP. You only need to select the items to be searched, which saves time.

2. PHP versions must be later than version 5.23, so OpenSSL functions can identify certificate Extension content.

3. The default certificate format in the LDAP function of PHP is PEM, And the binary format needs to be converted.

4. Flash Security is a problem. The file storage prompts securityerror: Error #2176: some actions (such as the pop-up window action ), so you cannot download the certificate immediately after you click it ...... (What Adobe thinks ).

5. When binary data is stored as der. The format is incorrect. We recommend that you use the PEM format.

The PHP code is as follows, which is very simple as writing PHP for the first time.

<? Php <br/> $ Server = "59. 203. ***. * ** "; <br/> $ admin = '*******'; <br/> $ passwd = '******'; <br/> $ Ds; </P> <p> function ldapinit () {<br/> global $ server, $ admin, $ passwd, $ Ds; <br/> $ DS = ldap_connect ("59.203.14.228"); // assuming the LDAP server is on this host <br/> if ($ DS) {<br/> ldap_set_option ($ ds, ldap_opt_timelimit, 10); <br/> $ r = ldap_bind ($ ds, $ admin, $ passwd ); <br/> If (! $ R) Die ("ldap_bind failed <br>"); <br/> return "ldap_bind success"; <br/> // ldap_close ($ DS ); <br/>} else {<br/> return "unable to connect to LDAP server "; <br/>}< br/> function getrecords ($ ou) {<br/> global $ Ds; <br/> If (ldapinit () = "ldap_bind success") {<br/> $ justthese = array ("cn", "Sn"); <br/> $ sr = ldap_search ($ ds, "O = Anhui province, c = Cn, O = Sicca", "(& (CN = *) (ou = ". $ ou. ")", $ justthese); <br/> $ info = Ldap_get_entries ($ ds, $ SR); <br/> return $ Info; <br/>}< br/> function getrecord ($ ou, $ CN) {<br/> global $ Ds; <br/> If (ldapinit () = "ldap_bind success ") {<br/> $ sr = ldap_search ($ ds, "O = Anhui province, c = Cn, O = Sicca", "(& (ou = $ ou) (CN = $ CN) "); <br/> $ info = ldap_get_entries ($ ds, $ SR); <br/> return $ Info; <br/>}< br/> function getcert ($ Sn) {<br/> global $ Ds; <br/> If (ldapinit () = "ldap_bind success ") {<Br/> $ sr = ldap_search ($ ds," O = Anhui province, c = Cn, O = Sicca "," Sn = ". $ Sn); <br/> $ info = ldap_get_entries ($ ds, $ SR); <br/> $ Cert = $ info [0] ["usercertificate; binary "] [0]; <br/> $ Cert = der2pem ($ CERT); <br/> return $ Cert; <br/>}< br/> function verifysndec ($ Sn) {<br/> global $ Ds; <br/> If (ldapinit () = "ldap_bind success") {<br/> $ sr = ldap_search ($ ds, "O = Anhui, c = Cn, O = Sicca", "Sn = ". $ Sn); <br/> $ info = LDAP _ Get_entries ($ ds, $ SR); <br/> $ Cert = $ info [0] ["usercertificate; binary"] [0]; <br/> $ Cert = der2pem ($ CERT); <br/> $ DATA = openssl_x509_parse ($ CERT); <br/> return $ data; <br/>}< br/> function verifysn ($ Sn) {<br/> global $ Ds; <br/> $ Sn = hex2dec ($ Sn); <br/> If (ldapinit () = "ldap_bind success ") {<br/> $ sr = ldap_search ($ ds, "O = Anhui province, c = Cn, O = Sicca", "Sn = ". $ Sn); <br/> $ info = ldap_get_entries ($ ds, $ SR ); <Br/> $ Cert = $ info [0] ["usercertificate; binary"] [0]; <br/> // $ DATA = openssl_get_publickey ($ CERT ); <br/> // $ DATA = openssl_x509_parse ($ CERT); <br/> // $ filename = "Cs. CER "; <br/> // $ fp = fopen ($ filename," rb "); <br/> // $ Cert = fread ($ FP, 8192 ); <br/> // fclose ($ FP); <br/> $ Cert = der2pem ($ CERT); <br/> // print_r ($ CERT ); <br/> // openssl_x509_read ($ CERT); <br/> $ DATA = openssl_x509_parse ($ CERT); <br/> print_r ($ d ATA); <br/> // openssl_x509_free ($ CERT); <br/> return $ data; <br/>}< br/> function der2pem ($ der_data) {<br/> $ PEM = chunk_split (base64_encode ($ der_data), 64, "/N"); <br/> $ PEM = "----- begin certificate -----/N ". $ PEM. "----- end certificate -----/N"; <br/> return $ PEM; <br/>}</P> <p> function hex2dec ($ HEX) {<br/> $ dec = 0; <br/> $ Len = strlen ($ HEX); <br/> for ($ I = 1; $ I <= $ Len; $ I ++) <br/> $ dec = Bcadd ($ Dec, bcmul (strval (hexdec ($ hex [$ I-1]), bcpow ('16', strval ($ len-$ I )))); <br/> return $ Dec; <br/>}< br/> function dec2hex ($ number) {<br/> $ hexvalues = array ('0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A ', 'B', 'C', 'D', 'E', 'F'); <br/> $ hexval = ''; <br/> while ($ number! = '0') {<br/> $ hexval = $ hexvalues [bcmod ($ number, '16')]. $ hexval; <br/> $ number = bcdiv ($ number, '16', 0); <br/>}< br/> return $ hexval; <br/>}< br/> // print_r ($ certinfo [0] ["usercertificate; binary"]); <br/> verifysn ("25a25e94b9045b7dd4114903 "); <br/>?> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.