Create a certificate using cryptapi

Source: Internet
Author: User

// Makecert. cpp: defines the entry point for the console application.
//

# Include "stdafx. H"
# Include <windows. h>

DWORD makecert ();
Void log (char * error_msg, int error_num );

Int _ tmain (INT argc, _ tchar * argv [])
{
Int T;
If (t = makecert ()! = 0)
Printf ("error num:> D", t );
Return 0;
}

DWORD makecert ()
{
Hcryptprov hprov;
Hcryptkey hkey;

Bool cret = cryptacquirecontext (& AMT; hprov, "licontainer", ms_enhanced_prov, prov_rsa_full, crypt_verifycontext );
If (cret = false)
{
Cret = cryptacquirecontext (& AMT; hprov, "licontainer", ms_enhanced_prov, prov_rsa_full, crypt_deletekeyset );

Cret = cryptacquirecontext (& AMT; hprov, "licontainer", ms_enhanced_prov, prov_rsa_full, crypt_newkeyset );
If (cret = false)
Return getlasterror ();
}

Cret = cryptgenkey (hprov, at_signature, crypt_exportable, & AMT; hkey); // | crypt_user_protected
If (cret = false)
Return getlasterror ();

Cert_info Cert;
Memset (void *) & AMT; Cert, 0, sizeof (cert_info ));
// 1. Version
CERT. dwversion = 2;

// 2. serialnumber
Byte serialnum [] = "\ x00 \ x01 \ x02 \ x03 \ x04 \ x05 \ x06 \ x07 \ x08 \ x09 \ x0a \ x0b \ x0c \ x0d \ x0e \ x0f ";
CERT. serialnumber. cbdata = 16;
CERT. serialnumber. pbdata = serialnum;

// 3. Algorithm
CERT. signaturealgorithm. pszobjid = szoid_rsa_sha1rsa;
CERT. signaturealgorithm. Parameters. cbdata = 0;

// 4. issuer. encode the issuer name with ASN.1, reference msdn Source
Char * cert_issuer_name = "My name is Li ";
Cert_rdn_attr rgnameattr =
{
Szoid_common_name, // The OID
Cert_rdn_printable_string, // type of string
(DWORD) strlen (cert_issuer_name) + 1, // String Length including
(Byte *) cert_issuer_name // pointer to the string
};
Cert_rdn rgrdn [] =
{
1, // The number of elements in the array
& AMT; rgnameattr // pointer to the array
};

Cert_name_info certname =
{
1, // number of elements in the cert_rnd's Array
Rgrdn
};

DWORD cbencoded; // variable to hold
Byte * pbencoded; // variable to hold a pointer to

Cret = cryptencodeobjectex (x509_asn_encoding, x509_name,
& AMT; certname, 0, null, null, & AMT; cbencoded );
If (cret = NULL)
Return getlasterror ();

Pbencoded = (byte *) malloc (cbencoded );
If (pbencoded = NULL)
Return getlasterror ();

Cret = cryptencodeobjectex (x509_asn_encoding, x509_name,
& AMT; certname, 0, null, pbencoded, & AMT; cbencoded );
If (cret = NULL)
Return getlasterror ();

CERT. issuer. cbdata = cbencoded;
CERT. issuer. pbdata = pbencoded;

// 5. utctime. process the time of cert. systemtimetofiletime
Systemtime receivime;
Getsystemtime (& AMT; systime );
Systemtimetofiletime (& AMT; systime, & AMT; Cert. notbefore );

Required ime. wyear + = 10;
Systemtimetofiletime (& AMT; systime, & AMT; Cert. notafter );

// 6. Subject
Char * cert_subject_name = "A Good Day ";

Rgnameattr. pszobjid = szoid_common_name;
Rgnameattr. dwvaluetype = cert_rdn_printable_string;
Rgnameattr. value. cbdata = (DWORD) strlen (cert_subject_name) + 1;
Rgnameattr. value. pbdata = (pbyte) cert_subject_name;

Cret = cryptencodeobjectex (x509_asn_encoding, x509_name,
& AMT; certname, 0, null, null, & AMT; cbencoded );
If (cret = NULL)
Return getlasterror ();

Pbencoded = (byte *) malloc (cbencoded );
If (pbencoded = NULL)
Return getlasterror ();

Cret = cryptencodeobjectex (x509_asn_encoding, x509_name,
& AMT; certname, 0, null, pbencoded, & AMT; cbencoded );
If (cret = NULL)
Return getlasterror ();

CERT. Subject. cbdata = cbencoded;
CERT. Subject. pbdata = pbencoded;

// 7. publickey
Pcert_public_key_info pubkeybuf; // reference racrypt. cpp. Don't know why
DWORD pubkeylen;
Cret = cryptexportpublickeyinfo (hprov, at_signature,
X509_asn_encoding, null, & AMT; pubkeylen );
If (cret = false)
Return getlasterror ();

Pubkeybuf = (pcert_public_key_info) malloc (pubkeylen );
If (pubkeybuf = NULL)
Return getlasterror ();

Cret = cryptexportpublickeyinfo (hprov, at_signature,
X509_asn_encoding, pubkeybuf, & AMT; pubkeylen );
If (cret = false)
Return getlasterror ();

CERT. subjectpublickeyinfo = * pubkeybuf;

// Extendsion
CERT. cextension = 0;
CERT. rgextension = NULL;
CERT. issueruniqueid. cbdata = 0;
CERT. subjectuniqueid. cbdata = 0;

// Make Certificate
Crypt_algorithm_identifier algid;
Byte paradata [16];
Paradata [0] = 0x05; paradata [1] = 0x00;

Algid. pszobjid = szoid_rsa_sha1rsa;
Algid. Parameters. cbdata = 2;
Algid. Parameters. pbdata = paradata;

/*-------------------------------------------------------------
Cryptsignandencodecertificate
The cryptsignandencodecertificate function encodes and signs a certificate, CRL, CTL or certificate request.
This function performs the following operations:
1-> CILS cryptencodeobject using lpszstructtype to encode the "to be signed" information.
2-> CILS cryptsigncertificate to sign this encoded information.
3-> CILS cryptencodeobject again, with lpszstructtype set to x509_cert,
To further encode the resulting signed, encoded information.
-------------------------------------------------------------*/

// Export as X.509 Certificate

Pbyte pcertout;
DWORD certlen;
Cret = cryptsignandencodecertificate (hprov, at_signature,
X509_asn_encoding, x509_cert_to_be_signed, (void *) & AMT; Cert, & AMT; algid,
Null, null, & AMT; certlen );
If (cret = false)
{
Printf ("cryptsignandencodecertificate error \ n ");
Return getlasterror ();
}

Pcertout = (pbyte) malloc (certlen );
If (certlen = NULL)
Return getlasterror ();

Cret = cryptsignandencodecertificate (hprov, at_signature,
X509_asn_encoding, x509_cert_to_be_signed, (void *) & AMT; Cert, & AMT; algid,
Null, pcertout, & AMT; certlen );
If (cret = false)
Return getlasterror ();

Char file [128] = "D: \ Li-X509.cer ";
DWORD Len;
Handle hfile = createfile (file, generic_read | generic_write,
File_pai_read | file_pai_write,
Null, create_always,
Null, null );
If (hfile = NULL)
Return getlasterror ();

Cret = writefile (hfile, pcertout, (DWORD) certlen, & AMT; Len, null );
If (cret = false)
Return getlasterror ();

Cret = closehandle (hfile );
If (cret = false)
Return getlasterror ();

// Export as PKCS #7 Certificate

Strcpy (file, "d: \ Li-PKCS # 7.cer ");

Hfile = createfile (file, generic_read | generic_write,
File_pai_read | file_pai_write,
Null, create_always,
Null, null );
If (hfile = NULL)
Return getlasterror ();

Hcertstore hstore = certopenstore (cert_store_prov_memory, pkcs_7_asn_encoding, hprov, cert_store_open_existing_flag, null );
If (hstore = NULL)
Return getlasterror ();

Void * pvdata = NULL;
DWORD cbdata = 0;
Cret = certgetstoreproperty (hstore, cert_store_localized_name_prop_id, null, & AMT; cbdata );
If (cret = false)
Return getlasterror ();

Pvdata = malloc (cbdata );
If (pvdata = NULL)
Return getlasterror ();

Cret = certgetstoreproperty (hstore, cert_store_localized_name_prop_id, pvdata, & AMT; cbdata );
If (cret = false)
Return getlasterror ();

Cret = certsavestore (hstore, x509_asn_encoding, cert_store_save_as_pkcs7, cert_store_save_to_file, hfile, 0 );
If (cret = false)
Return getlasterror ();

Cret = closehandle (hfile );
If (cret = false)
Return getlasterror ();

Free (pcertout );
Free (pbencoded );
Free (pubkeybuf );
Cret = cryptreleasecontext (hprov, 0 );
If (cret = false)
Return getlasterror ();

Return 0;
}

Void log (char * error_msg, int error_num)
{
DWORD dwwrite;
Char file [] = "D: \ log.txt ";
DWORD filelen;
Char Buf [128];

If (error_msg! = NULL)
Printf ("> S", error_msg );

If (error_num! = 0)
Printf ("error num:> d \ n", error_num );

Handle hfile = createfile (file, generic_read | generic_write,
File_pai_read | file_pai_write,
Null, open_existing,
Null, null );

Filelen = getfilesize (hfile, null );

Setfilepointer (hfile, filelen, file_begin, null );

Sprintf (BUF, "error num:> d \ r \ n", error_num );
Writefile (hfile, Buf, (DWORD) strlen (BUF ),
& AMT; dwwrite, null );

Closehandle (hfile );

}

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.