DataFlex
DataFlex
Duplicate openssl req -newkey rsa:2048 -nodes -keyout mydomain.pem -out mydomain.csr
See more OpenSSL Examples
Demonstrates how to duplicate this OpenSSL command:openssl req -newkey rsa:2048 -nodes -keyout mydomain.pem -out mydomain.csr
This command creates 2 files:
- mydomain.csr: this is the file to send to DigiCert or Let's Encrypt (or any other CA)
- mydomain.pem: this is the private key of the domain.
The second file is needed to pair with the certificate that will later be received from the CA.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoRsa
Variant vPrivKey
Handle hoPrivKey
Variant vPrivKey
Xml Handle hoPrivKeyXml
String sKeyModulus
Handle hoAsnRoot
Variant vAsnCertReqInfo
Handle hoAsnCertReqInfo
Variant vAsnCertSubject
Handle hoAsnCertSubject
Variant vAsnTemp
Handle hoAsnTemp
Variant vAsnPubKeyInfo
Handle hoAsnPubKeyInfo
Variant vAsnPubKeyAlgId
Handle hoAsnPubKeyAlgId
Handle hoAsnRsaKey
String sRsaKeyDerBase64
Variant vBdDer
Handle hoBdDer
Variant vBdSig
Handle hoBdSig
Variant vAsnAlgId
Handle hoAsnAlgId
String sCsrBase64
String sTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatRsa)) To hoRsa
If (Not(IsComObjectCreated(hoRsa))) Begin
Send CreateComObject of hoRsa
End
// Generate a 2048-bit key. Chilkat RSA supports
// key sizes ranging from 512 bits to 8192 bits.
Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
If (Not(IsComObjectCreated(hoPrivKey))) Begin
Send CreateComObject of hoPrivKey
End
Get pvComObject of hoPrivKey to vPrivKey
Get ComGenKey Of hoRsa 2048 vPrivKey To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRsa To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoPrivKey to vPrivKey
Get ComUsePrivateKey Of hoRsa vPrivKey To iSuccess
// Save the private key to unencrypted PKCS8 PEM
Get ComSavePkcs8PemFile Of hoPrivKey "mydomain.pem" To iSuccess
// (alternatively) Save the private key to encrypted PKCS8 PEM
Get ComSavePkcs8EncryptedPemFile Of hoPrivKey "myPassword" "mydomain_enc.pem" To iSuccess
// 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.
Get Create (RefClass(cComChilkatXml)) To hoPrivKeyXml
If (Not(IsComObjectCreated(hoPrivKeyXml))) Begin
Send CreateComObject of hoPrivKeyXml
End
Get ComGetXml Of hoPrivKey To sTemp1
Get ComLoadXml Of hoPrivKeyXml sTemp1 To iSuccess
// Get the modulus in base64 format:
Get ComGetChildContent Of hoPrivKeyXml "Modulus" To sKeyModulus
// --------------------------------------------------------------------------------
// 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.
Get Create (RefClass(cComChilkatAsn)) To hoAsnRoot
If (Not(IsComObjectCreated(hoAsnRoot))) Begin
Send CreateComObject of hoAsnRoot
End
// Beneath the root, we have a SEQUENCE (the certificate request info),
// another SEQUENCE (the algorithm identifier), and a BITSTRING (the signature data)
Get ComAppendSequence Of hoAsnRoot To iSuccess
Get ComAppendSequence Of hoAsnRoot To iSuccess
// ----------------------------------
// Build the Certificate Request Info
// ----------------------------------
Get ComGetSubItem Of hoAsnRoot 0 To vAsnCertReqInfo
If (IsComObject(vAsnCertReqInfo)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnCertReqInfo
Set pvComObject Of hoAsnCertReqInfo To vAsnCertReqInfo
End
Get ComAppendInt Of hoAsnCertReqInfo 0 To iSuccess
// Build the Subject part of the Certificate Request Info
Get ComAppendSequenceR Of hoAsnCertReqInfo To vAsnCertSubject
If (IsComObject(vAsnCertSubject)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnCertSubject
Set pvComObject Of hoAsnCertSubject To vAsnCertSubject
End
// Add each subject part..
Get ComAppendSetR Of hoAsnCertSubject To vAsnTemp
If (IsComObject(vAsnTemp)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnTemp
Set pvComObject Of hoAsnTemp To vAsnTemp
End
Get ComAppendSequence2 Of hoAsnTemp To iSuccess
// AppendSequence2 updates the internal reference to the newly appended SEQUENCE.
// The OID and printable string are added to the SEQUENCE.
Get ComAppendOid Of hoAsnTemp "2.5.4.6" To iSuccess
Get ComAppendString Of hoAsnTemp "printable" "US" To iSuccess
Send Destroy of hoAsnTemp
Get ComAppendSetR Of hoAsnCertSubject To vAsnTemp
If (IsComObject(vAsnTemp)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnTemp
Set pvComObject Of hoAsnTemp To vAsnTemp
End
Get ComAppendSequence2 Of hoAsnTemp To iSuccess
Get ComAppendOid Of hoAsnTemp "2.5.4.8" To iSuccess
Get ComAppendString Of hoAsnTemp "utf8" "Utah" To iSuccess
Send Destroy of hoAsnTemp
Get ComAppendSetR Of hoAsnCertSubject To vAsnTemp
If (IsComObject(vAsnTemp)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnTemp
Set pvComObject Of hoAsnTemp To vAsnTemp
End
Get ComAppendSequence2 Of hoAsnTemp To iSuccess
Get ComAppendOid Of hoAsnTemp "2.5.4.7" To iSuccess
Get ComAppendString Of hoAsnTemp "utf8" "Lindon" To iSuccess
Send Destroy of hoAsnTemp
Get ComAppendSetR Of hoAsnCertSubject To vAsnTemp
If (IsComObject(vAsnTemp)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnTemp
Set pvComObject Of hoAsnTemp To vAsnTemp
End
Get ComAppendSequence2 Of hoAsnTemp To iSuccess
Get ComAppendOid Of hoAsnTemp "2.5.4.10" To iSuccess
Get ComAppendString Of hoAsnTemp "utf8" "DigiCert Inc." To iSuccess
Send Destroy of hoAsnTemp
Get ComAppendSetR Of hoAsnCertSubject To vAsnTemp
If (IsComObject(vAsnTemp)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnTemp
Set pvComObject Of hoAsnTemp To vAsnTemp
End
Get ComAppendSequence2 Of hoAsnTemp To iSuccess
Get ComAppendOid Of hoAsnTemp "2.5.4.11" To iSuccess
Get ComAppendString Of hoAsnTemp "utf8" "DigiCert" To iSuccess
Send Destroy of hoAsnTemp
Get ComAppendSetR Of hoAsnCertSubject To vAsnTemp
If (IsComObject(vAsnTemp)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnTemp
Set pvComObject Of hoAsnTemp To vAsnTemp
End
Get ComAppendSequence2 Of hoAsnTemp To iSuccess
Get ComAppendOid Of hoAsnTemp "2.5.4.3" To iSuccess
Get ComAppendString Of hoAsnTemp "utf8" "example.digicert.com" To iSuccess
Send Destroy of hoAsnTemp
Send Destroy of hoAsnCertSubject
// Build the Public Key Info part of the Certificate Request Info
Get ComAppendSequenceR Of hoAsnCertReqInfo To vAsnPubKeyInfo
If (IsComObject(vAsnPubKeyInfo)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnPubKeyInfo
Set pvComObject Of hoAsnPubKeyInfo To vAsnPubKeyInfo
End
Get ComAppendSequenceR Of hoAsnPubKeyInfo To vAsnPubKeyAlgId
If (IsComObject(vAsnPubKeyAlgId)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnPubKeyAlgId
Set pvComObject Of hoAsnPubKeyAlgId To vAsnPubKeyAlgId
End
Get ComAppendOid Of hoAsnPubKeyAlgId "1.2.840.113549.1.1.1" To iSuccess
Get ComAppendNull Of hoAsnPubKeyAlgId To iSuccess
Send Destroy of hoAsnPubKeyAlgId
// 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..
Get Create (RefClass(cComChilkatAsn)) To hoAsnRsaKey
If (Not(IsComObjectCreated(hoAsnRsaKey))) Begin
Send CreateComObject of hoAsnRsaKey
End
// The RSA modulus is a big integer.
Get ComAppendBigInt Of hoAsnRsaKey sKeyModulus "base64" To iSuccess
Get ComAppendInt Of hoAsnRsaKey 65537 To iSuccess
Get ComGetEncodedDer Of hoAsnRsaKey "base64" To sRsaKeyDerBase64
// Now add the RSA key DER as a BIT STRING.
Get ComAppendBits Of hoAsnPubKeyInfo sRsaKeyDerBase64 "base64" To iSuccess
Send Destroy of hoAsnPubKeyInfo
// The last part of the certificate request info is an empty context-specific constructed item
// with a tag equal to 0.
Get ComAppendContextConstructed Of hoAsnCertReqInfo 0 To iSuccess
// Get the DER of the asnCertReqInfo.
// This will be signed using the RSA private key.
Get Create (RefClass(cComChilkatBinData)) To hoBdDer
If (Not(IsComObjectCreated(hoBdDer))) Begin
Send CreateComObject of hoBdDer
End
Get pvComObject of hoBdDer to vBdDer
Get ComWriteBd Of hoAsnCertReqInfo vBdDer To iSuccess
// Add the signature to the ASN.1
Get Create (RefClass(cComChilkatBinData)) To hoBdSig
If (Not(IsComObjectCreated(hoBdSig))) Begin
Send CreateComObject of hoBdSig
End
Get pvComObject of hoBdDer to vBdDer
Get pvComObject of hoBdSig to vBdSig
Get ComSignBd Of hoRsa vBdDer "SHA1" vBdSig To iSuccess
Get ComGetEncoded Of hoBdSig "base64" To sTemp1
Get ComAppendBits Of hoAsnRoot sTemp1 "base64" To iSuccess
Send Destroy of hoAsnCertReqInfo
// ----------------------------------
// Finally, add the algorithm identifier, which is the 2nd sub-item under the root.
// ----------------------------------
Get ComGetSubItem Of hoAsnRoot 1 To vAsnAlgId
If (IsComObject(vAsnAlgId)) Begin
Get Create (RefClass(cComChilkatAsn)) To hoAsnAlgId
Set pvComObject Of hoAsnAlgId To vAsnAlgId
End
Get ComAppendOid Of hoAsnAlgId "1.2.840.113549.1.1.5" To iSuccess
Get ComAppendNull Of hoAsnAlgId To iSuccess
Send Destroy of hoAsnAlgId
// Write the CSR to a DER encoded binary file:
Get ComWriteBinaryDer Of hoAsnRoot "qa_output/mydomain.csr" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoAsnRoot To sTemp1
Showln sTemp1
Procedure_Return
End
// It is also possible to get the CSR in base64 format:
Get ComGetEncodedDer Of hoAsnRoot "base64" To sCsrBase64
Showln "Base64 CSR:"
Showln sCsrBase64
End_Procedure