Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 1 | // Copyright (C) 2009 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package org.apache.commons.net.smtp; |
| 16 | |
David Pursehouse | 19c63fa | 2015-10-08 15:46:47 +0900 | [diff] [blame] | 17 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | |
David Pursehouse | a6270b9 | 2019-10-12 15:02:27 +0900 | [diff] [blame] | 19 | import com.google.common.io.BaseEncoding; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 20 | import com.google.gerrit.util.ssl.BlindSSLSocketFactory; |
Liu Yubao | 34d4d19 | 2012-05-24 11:28:45 +0800 | [diff] [blame] | 21 | import java.io.BufferedReader; |
| 22 | import java.io.BufferedWriter; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 23 | import java.io.IOException; |
Liu Yubao | 34d4d19 | 2012-05-24 11:28:45 +0800 | [diff] [blame] | 24 | import java.io.InputStreamReader; |
| 25 | import java.io.OutputStreamWriter; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 26 | import java.io.UnsupportedEncodingException; |
| 27 | import java.net.SocketException; |
| 28 | import java.security.InvalidKeyException; |
| 29 | import java.security.NoSuchAlgorithmException; |
| 30 | import java.util.Arrays; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 31 | import java.util.List; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 32 | import javax.crypto.Mac; |
| 33 | import javax.crypto.spec.SecretKeySpec; |
Edwin Kempin | d1e2c2f | 2016-03-13 10:24:17 +0100 | [diff] [blame] | 34 | import javax.net.ssl.SSLParameters; |
| 35 | import javax.net.ssl.SSLSocket; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 36 | import javax.net.ssl.SSLSocketFactory; |
| 37 | |
| 38 | public class AuthSMTPClient extends SMTPClient { |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 39 | private String authTypes; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 40 | |
Han-Wen Nienhuys | b0fb0a7 | 2017-06-13 18:10:03 +0200 | [diff] [blame] | 41 | public AuthSMTPClient(String charset) { |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 42 | super(charset); |
| 43 | } |
| 44 | |
Han-Wen Nienhuys | b0fb0a7 | 2017-06-13 18:10:03 +0200 | [diff] [blame] | 45 | public void enableSSL(boolean verify) { |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 46 | _socketFactory_ = sslFactory(verify); |
| 47 | } |
| 48 | |
Han-Wen Nienhuys | b0fb0a7 | 2017-06-13 18:10:03 +0200 | [diff] [blame] | 49 | public boolean startTLS(String hostname, int port, boolean verify) |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 50 | throws SocketException, IOException { |
| 51 | if (sendCommand("STARTTLS") != 220) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | _socket_ = sslFactory(verify).createSocket(_socket_, hostname, port, true); |
Liu Yubao | 34d4d19 | 2012-05-24 11:28:45 +0800 | [diff] [blame] | 56 | |
John Viklund | 7dfc4dd | 2016-08-11 16:09:09 +0200 | [diff] [blame] | 57 | if (verify) { |
| 58 | SSLParameters sslParams = new SSLParameters(); |
| 59 | sslParams.setEndpointIdentificationAlgorithm("HTTPS"); |
Dave Borowitz | 292fa15 | 2016-11-13 09:56:32 -0800 | [diff] [blame] | 60 | ((SSLSocket) _socket_).setSSLParameters(sslParams); |
John Viklund | 7dfc4dd | 2016-08-11 16:09:09 +0200 | [diff] [blame] | 61 | } |
Edwin Kempin | d1e2c2f | 2016-03-13 10:24:17 +0100 | [diff] [blame] | 62 | |
Liu Yubao | 34d4d19 | 2012-05-24 11:28:45 +0800 | [diff] [blame] | 63 | // XXX: Can't call _connectAction_() because SMTP server doesn't |
| 64 | // give banner information again after STARTTLS, thus SMTP._connectAction_() |
| 65 | // will wait on __getReply() forever, see source code of commons-net-2.2. |
| 66 | // |
| 67 | // The lines below are copied from SocketClient._connectAction_() and |
| 68 | // SMTP._connectAction_() in commons-net-2.2. |
| 69 | _socket_.setSoTimeout(_timeout_); |
| 70 | _input_ = _socket_.getInputStream(); |
| 71 | _output_ = _socket_.getOutputStream(); |
Dave Borowitz | 292fa15 | 2016-11-13 09:56:32 -0800 | [diff] [blame] | 72 | _reader = new BufferedReader(new InputStreamReader(_input_, UTF_8)); |
| 73 | _writer = new BufferedWriter(new OutputStreamWriter(_output_, UTF_8)); |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
Han-Wen Nienhuys | b0fb0a7 | 2017-06-13 18:10:03 +0200 | [diff] [blame] | 77 | private static SSLSocketFactory sslFactory(boolean verify) { |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 78 | if (verify) { |
| 79 | return (SSLSocketFactory) SSLSocketFactory.getDefault(); |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 80 | } |
David Pursehouse | 3232472 | 2016-06-01 21:20:02 +0900 | [diff] [blame] | 81 | return (SSLSocketFactory) BlindSSLSocketFactory.getDefault(); |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 84 | @Override |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 85 | public boolean login() throws IOException { |
| 86 | final String name = getLocalAddress().getHostName(); |
| 87 | if (name == null) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | boolean ok = SMTPReply.isPositiveCompletion(sendCommand("EHLO", name)); |
| 92 | authTypes = ""; |
| 93 | for (String line : getReplyStrings()) { |
| 94 | if (line != null && (line.startsWith("250 AUTH ") || line.startsWith("250-AUTH "))) { |
| 95 | authTypes = line; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return ok; |
| 101 | } |
| 102 | |
| 103 | public boolean auth(String smtpUser, String smtpPass) throws IOException { |
| 104 | List<String> types = Arrays.asList(authTypes.split(" ")); |
| 105 | if ("".equals(authTypes)) { |
| 106 | // Server didn't advertise authentication support. |
| 107 | // |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | if (smtpPass == null) { |
| 112 | smtpPass = ""; |
| 113 | } |
| 114 | if (types.contains("CRAM-SHA1")) { |
| 115 | return authCram(smtpUser, smtpPass, "SHA1"); |
| 116 | } |
| 117 | if (types.contains("CRAM-MD5")) { |
| 118 | return authCram(smtpUser, smtpPass, "MD5"); |
| 119 | } |
Bob Foerster | a1b2d45 | 2011-03-10 11:55:11 -0500 | [diff] [blame] | 120 | if (types.contains("LOGIN")) { |
| 121 | return authLogin(smtpUser, smtpPass); |
| 122 | } |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 123 | if (types.contains("PLAIN")) { |
| 124 | return authPlain(smtpUser, smtpPass); |
| 125 | } |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 126 | throw new IOException("Unsupported AUTH: " + authTypes); |
| 127 | } |
| 128 | |
| 129 | private boolean authCram(String smtpUser, String smtpPass, String alg) |
| 130 | throws UnsupportedEncodingException, IOException { |
| 131 | final String macName = "Hmac" + alg; |
| 132 | if (sendCommand("AUTH", "CRAM-" + alg) != 334) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | final String enc = getReplyStrings()[0].split(" ", 2)[1]; |
David Pursehouse | a6270b9 | 2019-10-12 15:02:27 +0900 | [diff] [blame] | 137 | final byte[] nonce = BaseEncoding.base64().decode(enc); |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 138 | final String sec; |
| 139 | try { |
| 140 | Mac mac = Mac.getInstance(macName); |
| 141 | mac.init(new SecretKeySpec(smtpPass.getBytes(UTF_8), macName)); |
| 142 | sec = toHex(mac.doFinal(nonce)); |
Urs Wolfer | 64cfaf2 | 2015-06-24 20:06:25 +0200 | [diff] [blame] | 143 | } catch (NoSuchAlgorithmException | InvalidKeyException e) { |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 144 | throw new IOException("Cannot use CRAM-" + alg, e); |
| 145 | } |
| 146 | |
| 147 | String token = smtpUser + ' ' + sec; |
| 148 | String cmd = encodeBase64(token.getBytes(UTF_8)); |
| 149 | return SMTPReply.isPositiveCompletion(sendCommand(cmd)); |
| 150 | } |
| 151 | |
Dave Borowitz | 292fa15 | 2016-11-13 09:56:32 -0800 | [diff] [blame] | 152 | private boolean authLogin(String smtpUser, String smtpPass) |
| 153 | throws UnsupportedEncodingException, IOException { |
Bob Foerster | a1b2d45 | 2011-03-10 11:55:11 -0500 | [diff] [blame] | 154 | if (sendCommand("AUTH", "LOGIN") != 334) { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | String cmd = encodeBase64(smtpUser.getBytes(UTF_8)); |
David Pursehouse | c5ccbf1 | 2016-04-11 19:43:20 +0900 | [diff] [blame] | 159 | if (sendCommand(cmd) != 334) { |
Bob Foerster | a1b2d45 | 2011-03-10 11:55:11 -0500 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| 163 | cmd = encodeBase64(smtpPass.getBytes(UTF_8)); |
| 164 | return SMTPReply.isPositiveCompletion(sendCommand(cmd)); |
| 165 | } |
| 166 | |
Dave Borowitz | 292fa15 | 2016-11-13 09:56:32 -0800 | [diff] [blame] | 167 | private static final char[] hexchar = { |
| 168 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 169 | }; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 170 | |
Han-Wen Nienhuys | b0fb0a7 | 2017-06-13 18:10:03 +0200 | [diff] [blame] | 171 | private String toHex(byte[] b) { |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 172 | final StringBuilder sec = new StringBuilder(); |
alex.ryazantsev | 05872ef | 2013-12-04 01:02:53 +0400 | [diff] [blame] | 173 | for (byte c : b) { |
| 174 | final int u = (c >> 4) & 0xf; |
| 175 | final int l = c & 0xf; |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 176 | sec.append(hexchar[u]); |
| 177 | sec.append(hexchar[l]); |
| 178 | } |
| 179 | return sec.toString(); |
| 180 | } |
| 181 | |
Dave Borowitz | 292fa15 | 2016-11-13 09:56:32 -0800 | [diff] [blame] | 182 | private boolean authPlain(String smtpUser, String smtpPass) |
| 183 | throws UnsupportedEncodingException, IOException { |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 184 | String token = '\0' + smtpUser + '\0' + smtpPass; |
| 185 | String cmd = "PLAIN " + encodeBase64(token.getBytes(UTF_8)); |
| 186 | return SMTPReply.isPositiveCompletion(sendCommand("AUTH", cmd)); |
| 187 | } |
| 188 | |
Han-Wen Nienhuys | b0fb0a7 | 2017-06-13 18:10:03 +0200 | [diff] [blame] | 189 | private static String encodeBase64(byte[] data) { |
David Pursehouse | a6270b9 | 2019-10-12 15:02:27 +0900 | [diff] [blame] | 190 | return BaseEncoding.base64().encode(data); |
Shawn O. Pearce | 44671f5 | 2009-11-07 12:55:26 -0800 | [diff] [blame] | 191 | } |
| 192 | } |