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
(PureBasic) Duplicate openssl req -newkey rsa:2048 -nodes -keyout mydomain.pem -out mydomain.csrDemonstrates how to duplicate this OpenSSL command: openssl req -newkey rsa:2048 -nodes -keyout mydomain.pem -out mydomain.csr This command creates 2 files:
The second file is needed to pair with the certificate that will later be received from the CA.
IncludeFile "CkAsn.pb" IncludeFile "CkXml.pb" IncludeFile "CkBinData.pb" IncludeFile "CkPrivateKey.pb" IncludeFile "CkRsa.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. rsa.i = CkRsa::ckCreate() If rsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Generate a 2048-bit key. Chilkat RSA supports ; key sizes ranging from 512 bits to 8192 bits. success.i = CkRsa::ckGenerateKey(rsa,2048) If success <> 1 Debug CkRsa::ckLastErrorText(rsa) CkRsa::ckDispose(rsa) ProcedureReturn EndIf privKey.i = CkRsa::ckExportPrivateKeyObj(rsa) ; Save the private key to unencrypted PKCS8 PEM success = CkPrivateKey::ckSavePkcs8PemFile(privKey,"mydomain.pem") ; (alternatively) Save the private key to encrypted PKCS8 PEM success = CkPrivateKey::ckSavePkcs8EncryptedPemFile(privKey,"myPassword","mydomain_enc.pem") ; We'll need the private key's modulus for the CSR. ; The modulus is not something that needs to be protected. Most people don't realize ; that a public key is actually just a subset of the private key. The public parts of ; an RSA private key are the modulus and exponent. The exponent is always 65537. privKeyXml.i = CkXml::ckCreate() If privKeyXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkXml::ckLoadXml(privKeyXml,CkPrivateKey::ckGetXml(privKey)) ; Get the modulus in base64 format: keyModulus.s = CkXml::ckGetChildContent(privKeyXml,"Modulus") ; -------------------------------------------------------------------------------- ; Now build the CSR using Chilkat's ASN.1 API. ; The keyModulus will be embedded within the ASN.1. ; A new ASN.1 object is automatically a SEQUENCE. ; Given that the CSR's root item is a SEQUENCE, we can use ; this as the root of our CSR. asnRoot.i = CkAsn::ckCreate() If asnRoot.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Beneath the root, we have a SEQUENCE (the certificate request info), ; another SEQUENCE (the algorithm identifier), and a BITSTRING (the signature data) success = CkAsn::ckAppendSequence(asnRoot) success = CkAsn::ckAppendSequence(asnRoot) ; ---------------------------------- ; Build the Certificate Request Info ; ---------------------------------- asnCertReqInfo.i = CkAsn::ckGetSubItem(asnRoot,0) success = CkAsn::ckAppendInt(asnCertReqInfo,0) ; Build the Subject part of the Certificate Request Info asnCertSubject.i = CkAsn::ckAppendSequenceR(asnCertReqInfo) ; Add each subject part.. asnTemp.i = CkAsn::ckAppendSetR(asnCertSubject) success = CkAsn::ckAppendSequence2(asnTemp) ; AppendSequence2 updates the internal reference to the newly appended SEQUENCE. ; The OID and printable string are added to the SEQUENCE. success = CkAsn::ckAppendOid(asnTemp,"2.5.4.6") success = CkAsn::ckAppendString(asnTemp,"printable","US") CkAsn::ckDispose(asnTemp) asnTemp = CkAsn::ckAppendSetR(asnCertSubject) success = CkAsn::ckAppendSequence2(asnTemp) success = CkAsn::ckAppendOid(asnTemp,"2.5.4.8") success = CkAsn::ckAppendString(asnTemp,"utf8","Utah") CkAsn::ckDispose(asnTemp) asnTemp = CkAsn::ckAppendSetR(asnCertSubject) success = CkAsn::ckAppendSequence2(asnTemp) success = CkAsn::ckAppendOid(asnTemp,"2.5.4.7") success = CkAsn::ckAppendString(asnTemp,"utf8","Lindon") CkAsn::ckDispose(asnTemp) asnTemp = CkAsn::ckAppendSetR(asnCertSubject) success = CkAsn::ckAppendSequence2(asnTemp) success = CkAsn::ckAppendOid(asnTemp,"2.5.4.10") success = CkAsn::ckAppendString(asnTemp,"utf8","DigiCert Inc.") CkAsn::ckDispose(asnTemp) asnTemp = CkAsn::ckAppendSetR(asnCertSubject) success = CkAsn::ckAppendSequence2(asnTemp) success = CkAsn::ckAppendOid(asnTemp,"2.5.4.11") success = CkAsn::ckAppendString(asnTemp,"utf8","DigiCert") CkAsn::ckDispose(asnTemp) asnTemp = CkAsn::ckAppendSetR(asnCertSubject) success = CkAsn::ckAppendSequence2(asnTemp) success = CkAsn::ckAppendOid(asnTemp,"2.5.4.3") success = CkAsn::ckAppendString(asnTemp,"utf8","example.digicert.com") CkAsn::ckDispose(asnTemp) CkAsn::ckDispose(asnCertSubject) ; Build the Public Key Info part of the Certificate Request Info asnPubKeyInfo.i = CkAsn::ckAppendSequenceR(asnCertReqInfo) asnPubKeyAlgId.i = CkAsn::ckAppendSequenceR(asnPubKeyInfo) success = CkAsn::ckAppendOid(asnPubKeyAlgId,"1.2.840.113549.1.1.1") success = CkAsn::ckAppendNull(asnPubKeyAlgId) CkAsn::ckDispose(asnPubKeyAlgId) ; The public key itself is a BIT STRING, but the bit string is composed of ASN.1 ; for the RSA public key. We'll first build the RSA ASN.1 for the public key ; (containing the 2048 bit modulus and exponent), and encoded it to DER, and then add ; the DER bytes as a BIT STRING (as a sub-item of asnPubKeyInfo) ; This is already a SEQUENCE.. asnRsaKey.i = CkAsn::ckCreate() If asnRsaKey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; The RSA modulus is a big integer. success = CkAsn::ckAppendBigInt(asnRsaKey,keyModulus,"base64") success = CkAsn::ckAppendInt(asnRsaKey,65537) rsaKeyDerBase64.s = CkAsn::ckGetEncodedDer(asnRsaKey,"base64") ; Now add the RSA key DER as a BIT STRING. success = CkAsn::ckAppendBits(asnPubKeyInfo,rsaKeyDerBase64,"base64") CkAsn::ckDispose(asnPubKeyInfo) ; The last part of the certificate request info is an empty context-specific constructed item ; with a tag equal to 0. success = CkAsn::ckAppendContextConstructed(asnCertReqInfo,0) ; Get the DER of the asnCertReqInfo. ; This will be signed using the RSA private key. bdDer.i = CkBinData::ckCreate() If bdDer.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkAsn::ckWriteBd(asnCertReqInfo,bdDer) ; Add the signature to the ASN.1 bdSig.i = CkBinData::ckCreate() If bdSig.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkRsa::ckSignBd(rsa,bdDer,"SHA1",bdSig) success = CkAsn::ckAppendBits(asnRoot,CkBinData::ckGetEncoded(bdSig,"base64"),"base64") CkAsn::ckDispose(asnCertReqInfo) ; ---------------------------------- ; Finally, add the algorithm identifier, which is the 2nd sub-item under the root. ; ---------------------------------- asnAlgId.i = CkAsn::ckGetSubItem(asnRoot,1) success = CkAsn::ckAppendOid(asnAlgId,"1.2.840.113549.1.1.5") success = CkAsn::ckAppendNull(asnAlgId) CkAsn::ckDispose(asnAlgId) ; Write the CSR to a DER encoded binary file: success = CkAsn::ckWriteBinaryDer(asnRoot,"qa_output/mydomain.csr") If success <> 1 Debug CkAsn::ckLastErrorText(asnRoot) CkRsa::ckDispose(rsa) CkXml::ckDispose(privKeyXml) CkAsn::ckDispose(asnRoot) CkAsn::ckDispose(asnRsaKey) CkBinData::ckDispose(bdDer) CkBinData::ckDispose(bdSig) ProcedureReturn EndIf ; It is also possible to get the CSR in base64 format: csrBase64.s = CkAsn::ckGetEncodedDer(asnRoot,"base64") Debug "Base64 CSR:" Debug csrBase64 CkPrivateKey::ckDispose(privKey) CkRsa::ckDispose(rsa) CkXml::ckDispose(privKeyXml) CkAsn::ckDispose(asnRoot) CkAsn::ckDispose(asnRsaKey) CkBinData::ckDispose(bdDer) CkBinData::ckDispose(bdSig) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.