Extending ssl/tls-message to be re-entrant in a java program

谁都会走 提交于 2021-02-11 12:26:54

问题


I'm extending the following question/solution to be re-entrant for connecting/submitting subsequent emails to an SMTP email server (e.g., Comcast), with TLS encryption.

Unsupported or unrecognized ssl-message exception at startHandshake after issuing STARTTLS command in a java program

I've been successful in studying and prototyping user9191556's algorithm and adapting it for subsequent emails. After much trial-n-error, I was successful in applying his/her TLS encryption capability to my prior algorithm and my code works, consistently ... although, I don't think it's correct. (yes, I lack the fundamentals of email server connectivity protocol and could also use a reference) I'm not satisfied with a non-dependent sleep before handshaking (i.e., 100ms) Works for my environment, but doubt it's guaranteed for general. I tried looping on the TLS input stream (sslIn) to see when it's 'ready', but it always times out. Hence, the 100ms sleep.

Is there a more formal/correct method for managing the inputStreams - both Socket and SSLSocket verions?

Thanks Bob Buckley Castle Rock, CO ... yep, it's snowing.

    //------------------------------------------------------------------------------
    public class SSLComcastTLSPrototype
    {
      //----------------------------------------------------------------------------
      public static void main(String[] args) throws InterruptedException
      {
        for (int i=0; i<5; ++i)
        {
          System.out.println("----------------------------------------------");
          System.out.println("sendMessage #" + i);
          System.out.flush();
    
          sendMessage(i);
        }
      }
    
      //----------------------------------------------------------------------------
      public static void sendMessage(int msgNum)
      {
        Date        dDate       = new Date();
        DateFormat  dFormat     = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
        int         SMTPPort    = 587;
        String      SMTPServer  = "smtp.comcast.net";
    
        try (Socket   smtpSocket = new Socket(SMTPServer, SMTPPort);
             DataOutputStream os = new DataOutputStream(smtpSocket.getOutputStream());
             BufferedReader   is = new BufferedReader(new nputStreamReader(smtpSocket.getInputStream ())))
        {
          int i = 0;
          while (! is.ready() && ++i < 10)
            Thread.sleep(100);

          if (! is.ready())
          {
            System.err.println("BufferedReader InputStream Timed Out - Email failed");
            System.err.flush();
            return;
          }

          new Thread(new ReadInput(is)).start();

          os.writeBytes("EHLO WOPR\r\n");
          os.writeBytes("STARTTLS\r\n");

          SSLContext ctx = SSLContext.getInstance("TLSv1.2");
          ctx.init(null, null, null);

          SSLSocketFactory factory = ctx.getSocketFactory();
          try (SSLSocket sslSocket = (SSLSocket) factory.createSocket(
              smtpSocket,
              smtpSocket.getInetAddress().getHostAddress(),
              smtpSocket.getPort(),
              true))
          {
            String[] suites = sslSocket.getSupportedCipherSuites();
            sslSocket.setEnabledCipherSuites(suites);

            try (DataOutputStream sslOut = new DataOutputStream(sslSocket.getOutputStream());
                   BufferedReader sslIn = new BufferedReader(new InputStreamReader(sslSocket.getInputStream())))
            {
              Thread.sleep(100);

              sslSocket.startHandshake();

              sslOut.writeBytes("EHLO WOPR\r\n");
              sslOut.writeBytes("AUTH LOGIN\r\n");
              sslOut.writeBytes("<encoded username>=\r\n");    // Username, BASE64 encoded
              sslOut.writeBytes("<encoded password>\r\n");     // Password, BASE64 encoded
              sslOut.writeBytes("MAIL From: Joe@Comcast.net\r\n");
              sslOut.writeBytes("RCPT To: JoeCool@Comcast.net\r\n");
              //sslOut.writeBytes("RCPT Cc: <Jim@AnyCompany.com>\r\n");
              sslOut.writeBytes("DATA\r\n");
              sslOut.writeBytes("X-Mailer: Via Java\r\n");
              sslOut.writeBytes("DATE: " + dFormat.format(dDate) + "\r\n");
              sslOut.writeBytes("From: Runway <Runway@Airfield.com>\r\n");
              sslOut.writeBytes("To: Joe Pilot\r\n");
              //sslOut.writeBytes("Cc: CCDUDE <CCPerson@TheirCompany.com>\r\n");
              //sslOut.writeBytes("RCPT Bcc: BCCDude<BCC@InvisibleCompany.com>\r\n");
              sslOut.writeBytes("Subject: Foo Foo\r\n");

              sslOut.writeBytes("MIME-VERSION: 1.0\r\n");
              sslOut.writeBytes("CONTENT-TYPE: TEXT/HTML; charset=\"ISO-8859-1\"\r\n");
              sslOut.writeBytes("CONTENT-TRANSFER-ENCODING: 7bit\r\n");

              sslOut.writeBytes("\r\n");
              sslOut.writeBytes("<html>\r\n");
              //sslOut.writeBytes("<head>\r\n");
              sslOut.writeBytes("<title>G'day, mate!</title>\r\n");
              //sslOut.writeBytes("</head>\r\n");

              sslOut.writeBytes("Message #" + msgNum + "\r\n\r\n\r\n");

              DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
              sslOut.writeBytes("\r\n");
              sslOut.writeBytes("This is the introduction of my email<br>\r\n");
              sslOut.writeBytes("Heading of the Date<br>\r\n");
              sslOut.writeBytes(df.format(new Date()) + "<br><br>\r\n");
              sslOut.writeBytes("</html>\r\n");

              sslOut.writeBytes("\r\n.\r\n");
              sslOut.writeBytes("QUIT\r\n");

              //System.out.println("EMail Message Submitted Successfully");
              //System.out.flush();

              String response;
              while ((response = sslIn.readLine()) != null)
              {
                //System.out.println("response: " + response);
                //System.out.flush();
           
                if (response.contains("recipient invalid domain"))
                {
                  System.err.println("***** Invalid Email Address *****");
                  System.err.flush();
                }              
              }
            }
          }
        }
        catch (NoSuchAlgorithmException | KeyManagementException | IOException | InterruptedException ex)
        {
          Logger.getLogger(SSLComcastTLSPrototype.class.getName()).log(Level.SEVERE, null, ex);
        }
      }

      //----------------------------------------------------------------------------
      static class ReadInput implements Runnable 
      {
        //--------------------------------------------------------------------------
        private final BufferedReader br;
  
        //--------------------------------------------------------------------------
        public ReadInput(BufferedReader br)
        {
          this.br = br;
        }

        //--------------------------------------------------------------------------
        @Override
        public void run()
        {
          try
          {
            int input;
            String allInput = "";
            while ((input = br.read()) >= 0)
            {
              allInput += (char) input;

              if (allInput.toLowerCase().endsWith("start tls\r\n"))
                break;
            }
          }
          catch (IOException e)
          {
            System.err.println("***** Exception: readInput *****");
            System.err.flush();
            e.printStackTrace();
          }
        }
      }
    }

来源:https://stackoverflow.com/questions/65362799/extending-ssl-tls-message-to-be-re-entrant-in-a-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!