Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Generate a CSR with keyUsage, extKeyUsage, and other ExtensionsSee more CSR ExamplesDemonstrates how to generate a CSR containing a 1.2.840.113549.1.9.14 extensionRequest with the following extensions:
#include <C_CkEccW.h> #include <C_CkPrngW.h> #include <C_CkPrivateKeyW.h> #include <C_CkCsrW.h> #include <C_CkBinDataW.h> #include <C_CkXmlW.h> #include <C_CkPublicKeyW.h> void ChilkatSample(void) { HCkEccW ecc; HCkPrngW prng; HCkPrivateKeyW privKey; HCkCsrW csr; const wchar_t *s; HCkBinDataW bdTemp; const wchar_t *s_base64_utf16be; HCkXmlW xml; HCkPublicKeyW pubKey; HCkBinDataW bdPubKeyDer; const wchar_t *ski; const wchar_t *csrPem; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // This example will generate a secp256r1 ECDSA key for the CSR. ecc = CkEccW_Create(); prng = CkPrngW_Create(); privKey = CkEccW_GenEccKey(ecc,L"secp256r1",prng); if (CkEccW_getLastMethodSuccess(ecc) == FALSE) { wprintf(L"Failed to generate a new ECDSA private key.\n"); CkEccW_Dispose(ecc); CkPrngW_Dispose(prng); return; } csr = CkCsrW_Create(); // Add common CSR fields: CkCsrW_putCommonName(csr,L"mysubdomain.mydomain.com"); CkCsrW_putCountry(csr,L"GB"); CkCsrW_putState(csr,L"Yorks"); CkCsrW_putLocality(csr,L"York"); CkCsrW_putCompany(csr,L"Internet Widgits Pty Ltd"); CkCsrW_putEmailAddress(csr,L"support@mydomain.com"); // Add the following 1.2.840.113549.1.9.14 extensionRequest // Note: The easiest way to know the content and format of the XML to be added is to examine // a pre-existing CSR with the same desired extensionRequest. You can use Chilkat to // get the extensionRequest from an existing CSR. // // Here is a sample extension request: // <?xml version="1.0" encoding="utf-8"?> // <set> // <sequence> // <sequence> // <oid>1.3.6.1.4.1.311.20.2</oid> // <asnOctets> // <universal tag="30" constructed="0">AEUAbgBkAEUAbgB0AGkAdAB5AEMAbABpAGUAbgB0AEEAdQB0AGgAQwBlAHIAdABpAGYAaQBjAGEAdABl // AF8AQwBTAFIAUABhAHMAcwB0AGgAcgBvAHUAZwBoAC8AVgAx</universal> // </asnOctets> // </sequence> // <sequence> // <oid>2.5.29.15</oid> // <bool>1</bool> // <asnOctets> // <bits n="3">A0</bits> // </asnOctets> // </sequence> // <sequence> // <oid>2.5.29.37</oid> // <asnOctets> // <sequence> // <oid>1.3.6.1.5.5.7.3.3</oid> // </sequence> // </asnOctets> // </sequence> // <sequence> // <oid>2.5.29.14</oid> // <asnOctets> // <octets>MCzBMQAViXBz8IDt8LsgmJxJ4Xg=</octets> // </asnOctets> // </sequence> // </sequence> // </set> // Use this online tool to generate code from sample XML: // Generate Code to Create XML // A few notes: // The string "AEUAbgBkAEUAbgB0AGkAdAB5AEMAbABpAGUAbgB0AEEAdQB0AGgAQwBlAHIAdABpAGYAaQBjAGEAdABlAF8AQwBTAFIAUABhAHMAcwB0AGgAcgBvAHUAZwBoAC8AVgAx" // is the base64 encoding of the utf-16be byte representation of the string "EndEntityClientAuthCertificate_CSRPassthrough/V1" s = L"EndEntityClientAuthCertificate_CSRPassthrough/V1"; bdTemp = CkBinDataW_Create(); CkBinDataW_AppendString(bdTemp,s,L"utf-16be"); s_base64_utf16be = CkBinDataW_getEncoded(bdTemp,L"base64"); // The string should be "AEUA....." wprintf(L"%s\n",s_base64_utf16be); // Here's the code to generate the above extension request. xml = CkXmlW_Create(); CkXmlW_putTag(xml,L"set"); CkXmlW_UpdateChildContent(xml,L"sequence|sequence|oid",L"1.3.6.1.4.1.311.20.2"); CkXmlW_UpdateAttrAt(xml,L"sequence|sequence|asnOctets|universal",TRUE,L"tag",L"30"); CkXmlW_UpdateAttrAt(xml,L"sequence|sequence|asnOctets|universal",TRUE,L"constructed",L"0"); CkXmlW_UpdateChildContent(xml,L"sequence|sequence|asnOctets|universal",s_base64_utf16be); CkXmlW_UpdateChildContent(xml,L"sequence|sequence[1]|oid",L"2.5.29.15"); CkXmlW_UpdateChildContent(xml,L"sequence|sequence[1]|bool",L"1"); CkXmlW_UpdateAttrAt(xml,L"sequence|sequence[1]|asnOctets|bits",TRUE,L"n",L"3"); // A0 is hex for decimal 160. CkXmlW_UpdateChildContent(xml,L"sequence|sequence[1]|asnOctets|bits",L"A0"); CkXmlW_UpdateChildContent(xml,L"sequence|sequence[2]|oid",L"2.5.29.37"); CkXmlW_UpdateChildContent(xml,L"sequence|sequence[2]|asnOctets|sequence|oid",L"1.3.6.1.5.5.7.3.3"); // This is the subjectKeyIdentifier extension. // The string "MCzBMQAViXBz8IDt8LsgmJxJ4Xg=" is base64 that decodes to 20 bytes, which is a SHA1 hash. // This is simply a hash of the DER of the public key. pubKey = CkPrivateKeyW_GetPublicKey(privKey); bdPubKeyDer = CkBinDataW_Create(); CkBinDataW_AppendEncoded(bdPubKeyDer,CkPublicKeyW_getEncoded(pubKey,TRUE,L"base64"),L"base64"); ski = CkBinDataW_getHash(bdPubKeyDer,L"sha1",L"base64"); CkPublicKeyW_Dispose(pubKey); CkXmlW_UpdateChildContent(xml,L"sequence|sequence[3]|oid",L"2.5.29.14"); CkXmlW_UpdateChildContent(xml,L"sequence|sequence[3]|asnOctets|octets",ski); // Add the extension request to the CSR CkCsrW_SetExtensionRequest(csr,xml); // Generate the CSR with the extension request csrPem = CkCsrW_genCsrPem(csr,privKey); if (CkCsrW_getLastMethodSuccess(csr) == FALSE) { wprintf(L"%s\n",CkCsrW_lastErrorText(csr)); CkPrivateKeyW_Dispose(privKey); CkEccW_Dispose(ecc); CkPrngW_Dispose(prng); CkCsrW_Dispose(csr); CkBinDataW_Dispose(bdTemp); CkXmlW_Dispose(xml); CkBinDataW_Dispose(bdPubKeyDer); return; } wprintf(L"%s\n",csrPem); CkPrivateKeyW_Dispose(privKey); CkEccW_Dispose(ecc); CkPrngW_Dispose(prng); CkCsrW_Dispose(csr); CkBinDataW_Dispose(bdTemp); CkXmlW_Dispose(xml); CkBinDataW_Dispose(bdPubKeyDer); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.