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

Unicode C++ 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
Cloud Signature CSC
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
uncategorized

 

 

 

(Unicode C++) Get Certificate As XML (and gets Signature Algorithm Identifier)

Demonstrates how to convert a certificate to XML such that the contents can easily be identified and accessed. Typically, the Chilkat Cert API provides properties and methods to access the most commonly needed pieces of information from a certificate. If a bit of information is not readily available then it can likely be retrieved from the XML representation. One such case is the certificate's signature algorithm identifier.

The basic internal format of a certificate is as follows (from section 4.1 of RFC 5280).

   Certificate  ::=  SEQUENCE  {
        tbsCertificate       TBSCertificate,
        signatureAlgorithm   AlgorithmIdentifier,
        signatureValue       BIT STRING  }

   TBSCertificate  ::=  SEQUENCE  {
        version         [0]  EXPLICIT Version DEFAULT v1,
        serialNumber         CertificateSerialNumber,
        signature            AlgorithmIdentifier,
        issuer               Name,
        validity             Validity,
        subject              Name,
        subjectPublicKeyInfo SubjectPublicKeyInfo,
        issuerUniqueID  [1]  IMPLICIT UniqueIdentifier OPTIONAL,
        subjectUniqueID [2]  IMPLICIT UniqueIdentifier OPTIONAL,
                             -- If present, version MUST be v2 or v3
        extensions      [3]  EXPLICIT Extensions OPTIONAL
                             -- If present, version MUST be v3
        }

Chilkat does not expect you to fully understand this notation and structure, but you can see that at a high level, the certificate is a sequence of three things, one after the other (a TBSCertifciate, an AlgorithmIdentifier, and a BIT STRING that is the signature). In XML format, it will look like this:

<sequence>
    <sequence>
        TBSCertificate goes here...
    </sequence>
    <sequence>
        <oid>1.2.840.113549.1.1.11</oid>
        <null />
    </sequence>
    <bits n="2048">3D20E895... 8DA307EC</bits>
</sequence>

The "oid" specifies the signature algorithm (such as sha256RSA), but it is an OID. In this case, the OID "1.2.840.113549.1.1.11" indicates "sha256WithRSAEncryption". (Google "1.2.840.113549.1.1.11" and you will find the 1st search result is http://www.alvestrand.no/objectid/1.2.840.113549.1.1.11.html. You can lookup any OID this way, to see what it means.)

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <CkSocketW.h>
#include <CkCertW.h>
#include <CkXmlW.h>

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

    CkSocketW socket;

    bool useTls = true;
    int maxWaitMillisec = 20000;
    const wchar_t *tlsServerHost = L"www.google.com";

    bool success = socket.Connect(tlsServerHost,443,useTls,maxWaitMillisec);
    if (success != true) {
        wprintf(L"%s\n",socket.lastErrorText());
        return;
    }

    CkCertW *cert = socket.GetSslServerCert();
    if (socket.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",socket.lastErrorText());
        return;
    }

    // Close the connection with the server
    // Wait a max of 20 millsec for a clean TLS shutdown.  
    socket.Close(20);

    // Examine the server cert in XML format
    const wchar_t *certXml = cert->exportCertXml();
    wprintf(L"%s\n",certXml);

    // Let's parse the XML to get the signature algorithm identifier.
    CkXmlW xml;
    xml.LoadXml(certXml);

    // The 2nd child is at index 1.
    CkXmlW *algId = xml.GetChild(1);
    const wchar_t *oid = algId->getChildContent(L"oid");
    wprintf(L"Signature Algorithm OID = %s\n",oid);

    delete algId;
    delete cert;
    }

 

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