Mostrando información del certificado TLS remoto de SMTP con org.apache.commons.net.smtp. La librería org.apache.commons.net.smtp es una herramienta útil para comunicarse con un servidor SMTP de forma remota. Para obtener información sobre el certificado TLS remoto de SMTP, se puede utilizar la siguiente función: “`java import java.io.IOException; import org.apache.commons.net.smtp.SMTPSClient; import javax.net.ssl.SSLSession; public class SMTPClientExample { public static void main(String[] args) { String smtpServer = “smtp.example.com”; int smtpPort = 587; SMTPSClient client = new SMTPSClient(); try { client.connect(smtpServer, smtpPort); client.login(); SSLSession session = client.getSession(); javax.security.cert.X509Certificate[] certificates = session.getPeerCertificateChain(); // Displaying the remote SMTP TLS certificate information for (javax.security.cert.X509Certificate certificate : certificates) { System.out.println(“Subject: ” + certificate.getSubjectDN()); System.out.println(“Issuer: ” + certificate.getIssuerDN()); System.out.println(“Serial number: ” + certificate.getSerialNumber()); System.out.println(“Valid from: ” + certificate.getNotBefore()); System.out.println(“Valid until: ” + certificate.getNotAfter()); System.out.println(“————————————————–“); } client.logout(); client.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } “` Esta clase de ejemplo muestra cómo conectarse a un servidor SMTP remoto utilizando org.apache.commons.net.smtp.SMTPSClient. Luego, se obtiene la sesión TLS y se obtiene un array de certificados X509 del servidor remoto. A continuación, se muestra la información del certificado TLS remoto obteniendo su sujeto, emisor, número de serie y fechas de validez. Tenga en cuenta que para ejecutar este código, necesita agregar la dependencia de Apache Commons Net a su proyecto.
Estoy utilizando org.apache.commons.net.smtp.AuthenticatingSMTPClient
para establecer conexiones con servidores SMTP. Después de establecer la conexión inicial, utilizo client.execTLS()
para enviar STARTTLS
. Quiero obtener la información del certificado SMTP TLS para los certificados utilizados por el servidor remoto. Me pregunto si es posible hacerlo únicamente con la API ofrecida por la biblioteca org.apache.commons.net.smtp
.
Alternativamente, ¿qué opciones tengo dentro del ecosistema de Java para mostrar el certificado SMTP TLS en un formato legible, preferiblemente utilizando el socket que ya fue abierto por client.execTLS()
?
Versión de Java: 11.
davy.ai
Desafortunadamente, la biblioteca
org.apache.commons.net.smtp
no proporciona ninguna API para obtener información del certificado de TLS SMTP. Sin embargo, puede utilizarjavax.net.ssl.SSLSession
para obtener la información del certificado.Después de llamar a
client.execTLS()
, puede obtener el socket subyacente llamando aclient.getUrgentDataHandler().getSocket()
. Luego puede obtener laSSLSession
llamando a((SSLSocket)socket).getSession()
.Desde la
SSLSession
, puede obtener las instancias deX509Certificate
utilizandosslSession.getPeerCertificates()
, lo cual devolverá una matriz de objetosCertificate
. Luego puede convertir cada elemento aX509Certificate
y obtener su información como emisor, sujeto, etc.Alternativamente, puede utilizar bibliotecas de terceros como Bouncy Castle o OpenSSL para mostrar la información del certificado TLS SMTP en un formato más legible.