es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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.

Tags:  , ,

Answer

  1. Avatar for 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 utilizar javax.net.ssl.SSLSession para obtener la información del certificado.

    Después de llamar a client.execTLS(), puede obtener el socket subyacente llamando a client.getUrgentDataHandler().getSocket(). Luego puede obtener la SSLSession llamando a ((SSLSocket)socket).getSession().

    Desde la SSLSession, puede obtener las instancias de X509Certificate utilizando sslSession.getPeerCertificates(), lo cual devolverá una matriz de objetos Certificate. Luego puede convertir cada elemento a X509Certificate y obtener su información como emisor, sujeto, etc.

    // Obtenga la SSLSession
    SSLSocket sslSocket = (SSLSocket) client.getUrgentDataHandler().getSocket();
    SSLSession sslSession = sslSocket.getSession();
    
    // Obtenga la cadena de certificados de la SSLSession
    Certificate[] certificates = sslSession.getPeerCertificates();
    
    // Muestre la información del certificado en la consola
    for (Certificate certificate : certificates) {
        if (certificate instanceof X509Certificate) {
            X509Certificate x509Certificate = (X509Certificate) certificate;
            System.out.println("Emisor: " + x509Certificate.getIssuerDN());
            System.out.println("Sujeto: " + x509Certificate.getSubjectDN());
            // ... 
        }
    }
    

    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.

Comments are closed.