Sample code for 30+ languages & platforms
React Native

Get the Server Certificate from a TLS Connection

See more Socket/SSL/TLS Examples

Demonstrates Socket.GetServerCert, which copies the certificate presented by the remote TLS server into a Cert object for inspection.

Background. The method returns false when the socket is not connected, is not using TLS, or no server certificate is available. Inspecting the certificate lets an application validate the server's identity.

Chilkat React Native Downloads

React Native
import { Cert, Socket } from '@chilkat/react-native'

async function chilkatExample() {
  const socket = new Socket();

  // Connect to the server using TLS.
  const bTls = true;
  const maxWaitMs = 5000;
  try {
    await socket.connectAsync('example.com', 5000, bTls, maxWaitMs);
  } catch {
    console.log(socket.lastErrorText);
    return;
  }

  // Copy the certificate presented by the remote TLS server, then inspect it.  This can be used to
  // validate the server's identity.
  const cert = new Cert();
  try {
    socket.getServerCert(cert);
  } catch {
    console.log(socket.lastErrorText);
    return;
  }

  console.log(`Server certificate subject: ${cert.subjectCN}`);
  console.log(`Issued by: ${cert.issuerCN}`);
}