PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkSocket.pb"
IncludeFile "CkCert.pb"
Procedure ChilkatExample()
success.i = 0
socket.i = CkSocket::ckCreate()
If socket.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect to the server using TLS.
bTls.i = 1
maxWaitMs.i = 5000
success = CkSocket::ckConnect(socket,"example.com",5000,bTls,maxWaitMs)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSocket::ckDispose(socket)
ProcedureReturn
EndIf
; Copy the certificate presented by the remote TLS server, then inspect it. This can be used to
; validate the server's identity.
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkSocket::ckGetServerCert(socket,cert)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSocket::ckDispose(socket)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
Debug "Server certificate subject: " + CkCert::ckSubjectCN(cert)
Debug "Issued by: " + CkCert::ckIssuerCN(cert)
CkSocket::ckDispose(socket)
CkCert::ckDispose(cert)
ProcedureReturn
EndProcedure