Dart
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final socket = CkSocket();
// Connect to the server using TLS.
final bTls = true;
final maxWaitMs = 5000;
try {
socket.connect('example.com', 5000, bTls, maxWaitMs);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Copy the certificate presented by the remote TLS server, then inspect it. This can be used to
// validate the server's identity.
final cert = CkCert();
try {
socket.getServerCert(cert);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Server certificate subject: ${cert.subjectCN}');
print('Issued by: ${cert.issuerCN}');
}