Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) Duplicate CSR Created by OpenSSL with Config.cnf

See more CSR Examples

Demonstrates how to duplicate a CSR created by the following commands:

# Generate Private Key
openssl ecparam -name secp256k1 -genkey -noout -out PrivateKey.pem

#Generate CSR
openssl req -new -sha256 -key PrivateKey.pem -extensions v3_req -config Config.cnf -out CSR.csr

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoSbCsr
    Boolean iSuccess
0    Handle hoCsr0
    Variant vXml
0    Handle hoXml0
    Variant vXml
    Handle hoXml
    Handle hoEcdsa
    Variant vPrng
    Handle hoPrng
    Variant vPrivKey
    Handle hoPrivKey
    Handle hoCsr
    String sCsrPem
    String sTemp1
    Boolean bTemp1

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example duplicates the CSR created by OpenSSL with the following config file:

    // oid_section = OIDs
    // [ OIDs ]
    // certificateTemplateName= 1.3.6.1.4.1.311.20.2
    // 
    // [ req ]
    // default_bits 	= 2048
    // emailAddress 	= it@example.sa
    // req_extensions	= v3_req
    // x509_extensions 	= v3_ca
    // prompt = no
    // default_md = sha256
    // req_extensions = req_ext
    // distinguished_name = dn
    // 
    // [ v3_req ]
    // basicConstraints = CA:FALSE
    // keyUsage = digitalSignature, nonRepudiation, keyEncipherment
    // 
    // [req_ext]
    // certificateTemplateName = ASN1:PRINTABLESTRING:ZATCA-Code-Signing
    // subjectAltName = dirName:alt_names
    // 
    // [ dn ]
    // CN =EXAMPLE-CORP  				# Common Name
    // C=SA									# Country Code e.g SA
    // OU=HEAD-OFFICE							# Organization Unit Name
    // O=ASC									# Organization Name
    // 
    // [alt_names]
    // SN=1-ASC|2-V01|3-1234567890				# EGS Serial Number 1-ABC|2-PQR|3-XYZ
    // UID=312345678900003						# Organization Identifier (VAT Number)
    // title=1100								# Invoice Type
    // registeredAddress=Dammam  	 			# Address
    // businessCategory=IT						# Business Category

    // The OpenSSL commands we are duplicating:

    // openssl ecparam -name secp256k1 -genkey -noout -out PrivateKey.pem
    // openssl req -new -sha256 -key PrivateKey.pem -extensions v3_req -config Config.cnf -out CSR.csr

    // The 1st step is to actually use OpenSSL to generate a sample CSR.csr that we wish to duplicate.
    // With the sample CSR.csr, we get the ExtensionRequest as XML.  
    // For example:

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbCsr
    If (Not(IsComObjectCreated(hoSbCsr))) Begin
        Send CreateComObject of hoSbCsr
    End
    Get ComLoadFile Of hoSbCsr "qa_data/csr/openssl_cnf/CSR.csr" "utf-8" To iSuccess
    If (iSuccess = False) Begin
        Showln "Failed to load CSR.csr"
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCsr)) To hoCsr0
    If (Not(IsComObjectCreated(hoCsr0))) Begin
        Send CreateComObject of hoCsr0
    End
    Get ComGetAsString Of hoSbCsr To sTemp1
    Get ComLoadCsrPem Of hoCsr0 sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCsr0 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatXml)) To hoXml0
    If (Not(IsComObjectCreated(hoXml0))) Begin
        Send CreateComObject of hoXml0
    End
    Get pvComObject of hoXml0 to vXml0
    Get ComGetExtensionRequest Of hoCsr0 vXml0 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCsr0 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Let's examine the extension request..
    Get ComGetXml Of hoXml0 To sTemp1
    Showln sTemp1

    // <?xml version="1.0" encoding="utf-8"?>
    // <set>
    //     <sequence>
    //         <sequence>
    //             <oid>1.3.6.1.4.1.311.20.2</oid>
    //             <asnOctets>
    //                 <printable>ZATCA-Code-Signing</printable>
    //             </asnOctets>
    //         </sequence>
    //         <sequence>
    //             <oid>2.5.29.17</oid>
    //             <asnOctets>
    //                 <sequence>
    //                     <contextSpecific tag="4" constructed="1">
    //                         <sequence>
    //                             <set>
    //                                 <sequence>
    //                                     <oid>2.5.4.4</oid>
    //                                     <utf8>1-ASC|2-V01|3-1234567890</utf8>
    //                                 </sequence>
    //                             </set>
    //                             <set>
    //                                 <sequence>
    //                                     <oid>0.9.2342.19200300.100.1.1</oid>
    //                                     <utf8>312345678900003</utf8>
    //                                 </sequence>
    //                             </set>
    //                             <set>
    //                                 <sequence>
    //                                     <oid>2.5.4.12</oid>
    //                                     <utf8>1100</utf8>
    //                                 </sequence>
    //                             </set>
    //                             <set>
    //                                 <sequence>
    //                                     <oid>2.5.4.26</oid>
    //                                     <utf8>Dammam</utf8>
    //                                 </sequence>
    //                             </set>
    //                             <set>
    //                                 <sequence>
    //                                     <oid>2.5.4.15</oid>
    //                                     <utf8>IT</utf8>
    //                                 </sequence>
    //                             </set>
    //                         </sequence>
    //                     </contextSpecific>
    //                 </sequence>
    //             </asnOctets>
    //         </sequence>
    //     </sequence>
    // </set>

    // If you wish to generate the above XML without going through the above steps, copy the XML into
    // the online tool at https://tools.chilkat.io/xmlCreate

    // Here is the generated code for the above XML:

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "set"
    Send ComUpdateChildContent To hoXml "sequence|sequence|oid" "1.3.6.1.4.1.311.20.2"
    Send ComUpdateChildContent To hoXml "sequence|sequence|asnOctets|printable" "ZATCA-Code-Signing"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|oid" "2.5.29.17"
    Get ComUpdateAttrAt Of hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific" True "tag" "4" To iSuccess
    Get ComUpdateAttrAt Of hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific" True "constructed" "1" To iSuccess
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set|sequence|oid" "2.5.4.4"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set|sequence|utf8" "1-ASC|2-V01|3-1234567890"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set[1]|sequence|oid" "0.9.2342.19200300.100.1.1"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set[1]|sequence|utf8" "312345678900003"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set[2]|sequence|oid" "2.5.4.12"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set[2]|sequence|utf8" "1100"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set[3]|sequence|oid" "2.5.4.26"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set[3]|sequence|utf8" "Dammam"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set[4]|sequence|oid" "2.5.4.15"
    Send ComUpdateChildContent To hoXml "sequence|sequence[1]|asnOctets|sequence|contextSpecific|sequence|set[4]|sequence|utf8" "IT"

    // We'll need a new secp256k1 private key, so let's generate it.
    Get Create (RefClass(cComChilkatEcc)) To hoEcdsa
    If (Not(IsComObjectCreated(hoEcdsa))) Begin
        Send CreateComObject of hoEcdsa
    End
    Get Create (RefClass(cComChilkatPrng)) To hoPrng
    If (Not(IsComObjectCreated(hoPrng))) Begin
        Send CreateComObject of hoPrng
    End
    Get pvComObject of hoPrng to vPrng
    Get ComGenEccKey Of hoEcdsa "secp256k1" vPrng To vPrivKey
    If (IsComObject(vPrivKey)) Begin
        Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
        Set pvComObject Of hoPrivKey To vPrivKey
    End
    Get ComLastMethodSuccess Of hoEcdsa To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoEcdsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Generated secp256k1 private key."

    // Use a new CSR object to generate a CSR with the private key and extension request.
    Get Create (RefClass(cComChilkatCsr)) To hoCsr
    If (Not(IsComObjectCreated(hoCsr))) Begin
        Send CreateComObject of hoCsr
    End

    // Add the [dn] fields
    //  [ dn ]
    //  CN =EXAMPLE-CORP  				# Common Name
    //  C=SA									# Country Code e.g SA
    //  OU=HEAD-OFFICE							# Organization Unit Name
    //  O=ASC									# Organization Name
    Set ComCommonName Of hoCsr To "EXAMPLE-CORP"
    Set ComCountry Of hoCsr To "SA"
    Set ComCompanyDivision Of hoCsr To "HEAD-OFFICE"
    Set ComCompany Of hoCsr To "ASC"

    // Add the extension request to the CSR
    Get pvComObject of hoXml to vXml
    Get ComSetExtensionRequest Of hoCsr vXml To iSuccess

    // Generate the CSR with the extension request
    Get ComGenCsrPem Of hoCsr vPrivKey To sCsrPem
    Get ComLastMethodSuccess Of hoCsr To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoCsr To sTemp1
        Showln sTemp1
        Send Destroy of hoPrivKey
        Procedure_Return
    End

    Showln sCsrPem

    Send Destroy of hoPrivKey

    // Sample output:

    // -----BEGIN CERTIFICATE REQUEST-----
    // MIIBuDCCAV8CAQAwSDEVMBMGA1UEAwwMRVhBTVBMRS1DT1JQMQswCQYDVQQGEwJT
    // QTEUMBIGA1UECwwLSEVBRC1PRkZJQ0UxDDAKBgNVBAoMA0FTQzBWMBAGByqGSM49
    // AgEGBSuBBAAKA0IABFI5rusr76HiJcMMr1r4L0B0BOAs6azLkt/RwHoT6A0xFRRt
    // tulWT40tNhx3qJ4I5ePNgMceOEtuK1kMGVTovI6ggbcwgbQGCSqGSIb3DQEJDjGB
    // pjCBozAhBgkrBgEEAYI3FAIEFBMSWkFUQ0EtQ29kZS1TaWduaW5nMH4GA1UdEQR3
    // MHWkczBxMSEwHwYDVQQEDBgxLUFTQ3wyLVYwMXwzLTEyMzQ1Njc4OTAxHzAdBgoJ
    // kiaJk/IsZAEBDA8zMTIzNDU2Nzg5MDAwMDMxDTALBgNVBAwMBDExMDAxDzANBgNV
    // BBoMBkRhbW1hbTELMAkGA1UEDwwCSVQwCgYIKoZIzj0EAwIDRwAwRAIgJnbgpSGb
    // diB+0M1VTqc1GU9sFsfnOvVN/8WhWRRxQIwCIF5eH9vgMgXyoU284X8Bx3dqOJ4q
    // xashGWci87POxSvT
    // -----END CERTIFICATE REQUEST-----


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.