Merge branch 'stable-3.0' into stable-3.1

* stable-3.0:
  Verify hostname when sending emails via SMTP server with SMTPSClient
  Docs: Clarify that 'm' push option sets patch set description

Change-Id: I1b69e72b1ad64c842bce2f174b52b073c9e5edac
This commit is contained in:
Marco Miller
2020-11-16 18:02:25 -05:00
6 changed files with 89 additions and 75 deletions

View File

@@ -89,7 +89,7 @@ evolves, such as "Added more unit tests." Unlike the change description, a patch
set description does not become a part of the project's history.
To add a patch set description, click *Add a patch set description*, located in
the file list.
the file list, or provide it link:user-upload.html#patch_set_description[on upload].
GERRIT
------

View File

@@ -314,11 +314,11 @@ link:intro-user.html#work-in-progress-by-default[user preference]. If the
preference is set so the default behavior is to create `work-in-progress`
changes, this can be overridden with the `ready` option.
[[message]]
==== Message
[[patch_set_description]]
==== Patch Set Description
A comment message can be applied to the change by using the `message` (or `m`)
option:
A link:concept-patch-sets.html#_description[patch set description] can be
applied by using the `message` (or `m`) option:
----
git push ssh://john.doe@git.example.com:29418/kernel/common HEAD:refs/for/experimental%m=This_is_a_rebase_on_master%21

View File

@@ -392,11 +392,7 @@ public class SmtpEmailSender implements EmailSender {
}
private SMTPClient open() throws EmailException {
final AuthSMTPClient client = new AuthSMTPClient(UTF_8.name());
if (smtpEncryption == Encryption.SSL) {
client.enableSSL(sslVerify);
}
final AuthSMTPClient client = new AuthSMTPClient(smtpEncryption == Encryption.SSL, sslVerify);
client.setConnectTimeout(connectTimeout);
try {
@@ -412,7 +408,7 @@ public class SmtpEmailSender implements EmailSender {
}
if (smtpEncryption == Encryption.TLS) {
if (!client.startTLS(smtpHost, smtpPort, sslVerify)) {
if (!client.execTLS()) {
throw new EmailException("SMTP server does not support TLS");
}
if (!client.login()) {

View File

@@ -20,7 +20,6 @@ import java.net.Socket;
import java.net.UnknownHostException;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
@@ -32,19 +31,7 @@ public class BlindSSLSocketFactory extends SSLSocketFactory {
private static final BlindSSLSocketFactory INSTANCE;
static {
final X509TrustManager dummyTrustManager =
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
};
final X509TrustManager dummyTrustManager = new BlindTrustManager();
try {
final SSLContext context = SSLContext.getInstance("SSL");

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.util.ssl;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/** TrustManager implementation that accepts all certificates without validation. */
public class BlindTrustManager implements X509TrustManager {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
}

View File

@@ -16,69 +16,67 @@ package org.apache.commons.net.smtp;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gerrit.util.ssl.BlindSSLSocketFactory;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import com.google.gerrit.util.ssl.BlindTrustManager;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import org.apache.commons.codec.binary.Base64;
public class AuthSMTPClient extends SMTPClient {
/**
* SMTP Client with authentication support and optional SSL processing and verification. {@link
* org.apache.commons.net.smtp.SMTPSClient} is used for the SSL handshake and hostname verification.
*
* <p>If shouldHandshakeOnConnect mode is selected, SSL/TLS negotiation starts right after the
* connection has been established. Otherwise SSL/TLS negotiation will only occur if {@link
* AuthSMTPClient#execTLS} is explicitly called and the server accepts the command.
*
* <p>Examples:
*
* <ul>
* <li>For SSL connection:
* <pre>
* AuthSMTPClient c = new AuthSMTPClient(true, sslVerify);
* c.connect("127.0.0.1", 465);
* </pre>
* <li>For TLS connection:
* <pre>
* AuthSMTPClient c = new AuthSMTPClient(false, sslVerify);
* c.connect("127.0.0.1", 25);
* if (c.execTLS()) { /rest of the commands here/ }
* </pre>
* <li>If SSL encryption is not required:
* <pre>
* AuthSMTPClient c = new AuthSMTPClient(false, false);
* c.connect("127.0.0.1", port);
* </pre>
*/
public class AuthSMTPClient extends SMTPSClient {
private String authTypes;
public AuthSMTPClient(String charset) {
super(charset);
}
public void enableSSL(boolean verify) {
_socketFactory_ = sslFactory(verify);
}
public boolean startTLS(String hostname, int port, boolean verify)
throws SocketException, IOException {
if (sendCommand("STARTTLS") != 220) {
return false;
/**
* Constructs AuthSMTPClient.
*
* @param shouldHandshakeOnConnect the SSL processing mode, {@code true} if SSL negotiation should
* start right after connect, {@code false} if it will be started by the user explicitly or
* SSL negotiation is not required.
* @param sslVerificationEnabled {@code true} if the SMTP server's SSL certificate and hostname
* should be verified, {@code false} otherwise.
*/
public AuthSMTPClient(boolean shouldHandshakeOnConnect, boolean sslVerificationEnabled) {
// If SSL Encryption is required, SMTPSClient is used for the handshake.
// Otherwise, use SMTPSClient in 'explicit' mode without calling execTLS().
// See SMTPSClient._connectAction_ in commons-net-3.6.
super("TLS", shouldHandshakeOnConnect, UTF_8.name());
this.setEndpointCheckingEnabled(sslVerificationEnabled);
if (!sslVerificationEnabled) {
this.setTrustManager(new BlindTrustManager());
}
_socket_ = sslFactory(verify).createSocket(_socket_, hostname, port, true);
if (verify) {
SSLParameters sslParams = new SSLParameters();
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
((SSLSocket) _socket_).setSSLParameters(sslParams);
}
// XXX: Can't call _connectAction_() because SMTP server doesn't
// give banner information again after STARTTLS, thus SMTP._connectAction_()
// will wait on __getReply() forever, see source code of commons-net-2.2.
//
// The lines below are copied from SocketClient._connectAction_() and
// SMTP._connectAction_() in commons-net-2.2.
_socket_.setSoTimeout(_timeout_);
_input_ = _socket_.getInputStream();
_output_ = _socket_.getOutputStream();
_reader = new BufferedReader(new InputStreamReader(_input_, UTF_8));
_writer = new BufferedWriter(new OutputStreamWriter(_output_, UTF_8));
return true;
}
private static SSLSocketFactory sslFactory(boolean verify) {
if (verify) {
return (SSLSocketFactory) SSLSocketFactory.getDefault();
}
return (SSLSocketFactory) BlindSSLSocketFactory.getDefault();
}
@Override