Parse X509 Certificate

Source: Internet
Author: User

Original article: http://hi.baidu.com/lidhcn/item/5476d292a0710eda1a49df60

1. Read certificate data from the certificate file on the disk

Unsigned char * pbx509data; // certificate data
Unsigned long ulx509datalen; // Certificate Data Length
2. Get certcontext
Pccert_context pcertcontext = certcreatecertificatecontext (x509_asn_encoding, pbx509data, ulx509datalen );
3. Obtain certificate information
Pcertcontext-> pcertinfo-> dwversion; // certificate version
Crypt_integer_blob snblob = pcertcontext-> pcertinfo-> serialnumber; // certificate Sn
Cert_name_blob issuerblob = pcertcontext-> pcertinfo-> issuer; // Certificate Issuer
Cert_name_blob subjectblob = pcertcontext-> pcertinfo-> subject; // certificate topic
// Certificate validity Start Date
Systemtime receivime;
Memset (& systime, 0, sizeof (systime ));
Filetimetosystemtime (& pcertcontext-> pcertinfo-> notbefore, & policime );
Char sztime [128] = {0 };
Sprintf_s (sztime, 128, "% d: % d", systime. wyear, too ime. wmonth, too ime. wday, too ime. whour, mongoime. wminute, policime. wsecond );
// Valid end date of Certificate
Memset (& systime, 0, sizeof (systime ));
Filetimetosystemtime (& pcertcontext-> pcertinfo-> notafter, & systime );
Memset (sztime, 0, sizeof (sztime ));
Sprintf_s (sztime, 128, "% d: % d", systime. wyear, too ime. wmonth, too ime. wday, too ime. whour, mongoime. wminute, policime. wsecond );
4. Create a temporary key container
Hcryptprov htmpprov = NULL;
Cryptacquirecontext (& htmpprov, "my_temporary_container", null, prov_rsa_aes, 0); // null indicates using the system default CSP
5. Import the public key to the container to obtain the public key handle.
Hcryptkey hkey = NULL;
Cert_public_key_info certpubkeyinfo = pcertcontext-> pcertinfo-> subjectpublickeyinfo;
Cryptimppubpublickeyinfo (htmpprov, x509_asn_encoding | pkcs_7_asn_encoding, & certpubkeyinfo, & hkey );
6. Export the Public Key (the second call method is recommended)
Unsigned char * pbuf = NULL;
Unsigned long ulbuflen = 0;
Cryptexportkey (hkey, 0, publickeyblob, 0, pbuf, & ulbuflen );
Pbuf = new unsigned char [ulbuflen];
Memset (pbuf, 0, ulbuflen );
Cryptexportkey (hkey, 0, publickeyblob, 0, pbuf, & ulbuflen );
7. obtain public key information
Unsigned char * P = pbuf + sizeof (publickeystruc );
(* (Rsapubkey *) P). bitlen; // public key module length (in BIT)
(* (Rsapubkey *) P). pubexp; // E of the public key (note the byte order)
P + = sizeof (rsapubkey); // n of the public key (note the byte order)
8. Cleanup
Delete [] pbuf;
Pbuf = NULL;
Cryptdestroykey (hkey );
Cryptreleasecontext (htmpprov, 0 );
Certfreecertificatecontext (pcertcontext );

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.