git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] send-email: improve SSL certificate verification

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jul 16, 2013, 00:15 UTC
Message-ID
<20130716001506.GG11097@vauxhall.crustytoothpaste.net>
In-Reply-To
<51E3677D.6040903@web.de>

The SSL and TLS code for SMTP is non-trivial, so refactor it into a separate function for ease of use. Handle both files and directories as sources for CA certificates. Also add handling for older version of IO::Socket::SSL that do not support the SSL_VERIFY_PEER and SSL_VERIFY_NONE constants; in this case, print a warning and inform the user of this fact.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
This is completely untested.  I used perl -c, but that's it.
 git-send-email.perl | 50 ++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 14 deletions(-)
diff --git a/git-send-email.perl b/git-send-email.perl
index 095b6fb..11fb2d0 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -1083,6 +1083,37 @@ sub smtp_auth_maybe {
 	return $auth;
 }
 
+sub ssl_verify_params {
+	require IO::Socket::SSL;
+	eval {
+		IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
+	};
+	if ($@) {
+		print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
+		return;
+	}
+
+	if (!defined $smtp_ssl_cert_path) {
+		$smtp_ssl_cert_path ||= "/etc/ssl/certs";
+	}
+
+	if (!$smtp_ssl_cert_path) {
+		return (SSL_verify_mode => SSL_VERIFY_NONE());
+	}
+	elsif (-d $smtp_ssl_cert_path) {
+		return (SSL_verify_mode => SSL_VERIFY_PEER(),
+			SSL_ca_path => $smtp_ssl_cert_path);
+	}
+	elsif (-f $smtp_ssl_cert_path) {
+		return (SSL_verify_mode => SSL_VERIFY_PEER(),
+			SSL_ca_file => $smtp_ssl_cert_path);
+	}
+	else {
+		print STDERR "Not using SSL_VERIFY_PEER because the CA path does not exist.\n";
+		return (SSL_verify_mode => SSL_VERIFY_NONE());
+	}
+}
+
 # Returns 1 if the message was sent, and 0 otherwise.
 # In actuality, the whole program dies when there
 # is an error sending a message.
@@ -1187,7 +1218,8 @@ X-Mailer: git-send-email $gitversion
 			$smtp_domain ||= maildomain();
 			$smtp ||= Net::SMTP::SSL->new($smtp_server,
 						      Hello => $smtp_domain,
-						      Port => $smtp_server_port);
+						      Port => $smtp_server_port,
+							  ssl_verify_params());
 		}
 		else {
 			require Net::SMTP;
@@ -1203,19 +1235,9 @@ X-Mailer: git-send-email $gitversion
 				$smtp->command('STARTTLS');
 				$smtp->response();
 				if ($smtp->code == 220) {
-					# Attempt to use a ca-certificate by default
-					$smtp_ssl_cert_path |= "/etc/ssl/certs";
-					if (-d $smtp_ssl_cert_path) {
-						$smtp = Net::SMTP::SSL->start_SSL($smtp,
-										  SSL_verify_mode => SSL_VERIFY_PEER,
-										  SSL_ca_path => $smtp_ssl_cert_path)
-							or die "STARTTLS failed! ".$smtp->message;
-					} else {
-						print STDERR "warning: Using SSL_VERIFY_NONE.  See sendemail.smtpsslcertpath.\n";
-						$smtp = Net::SMTP::SSL->start_SSL($smtp,
-										  SSL_verify_mode => SSL_VERIFY_NONE)
-							or die "STARTTLS failed! ".$smtp->message;
-					}
+					$smtp = Net::SMTP::SSL->start_SSL($smtp,
+									  ssl_verify_params())
+						or die "STARTTLS failed! ".$smtp->message;
 					$smtp_encryption = '';
 					# Send EHLO again to receive fresh
 					# supported commands
-- 
1.8.3.2.923.g2a18ff8.dirty


-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187
Previous: Junio C HamanoNext: Torsten Bögershausen
Message 11 of 32 in “Squelch warning from send-email”
  1. 0/2 Squelch warning from send-emailRamkumar Ramachandra, Jul 5, 2013
  2. 1/2 send-email: squelch warning from Net::SMTP::SSLRamkumar Ramachandra, Jul 5, 2013
  3. Torsten BögershausenJul 6, 2013
  4. brian m. carlsonJul 6, 2013
  5. Torsten BögershausenJul 6, 2013
  6. Ramkumar RamachandraJul 14, 2013
  7. brian m. carlsonJul 14, 2013
  8. Ramkumar RamachandraJul 14, 2013
  9. Torsten BögershausenJul 15, 2013
  10. Junio C HamanoJul 15, 2013
  11. send-email: improve SSL certificate verificationbrian m. carlson, Jul 16, 2013
  12. Torsten BögershausenJul 16, 2013
  13. brian m. carlsonJul 16, 2013
  14. Re* [PATCH] send-email: improve SSL certificate verificationJunio C Hamano, Jul 18, 2013
  15. Ramkumar RamachandraJul 18, 2013
  16. 2/2 send-email: introduce sendemail.smtpsslcertpathRamkumar Ramachandra, Jul 5, 2013
  17. Eric SunshineJul 5, 2013
  18. Ramkumar RamachandraJul 5, 2013
  19. brian m. carlsonJul 5, 2013
  20. Ramkumar RamachandraJul 5, 2013
  21. brian m. carlsonJul 5, 2013
  22. Junio C HamanoJul 5, 2013
  23. John KeepingJul 5, 2013
  24. Junio C HamanoJul 5, 2013
  25. John KeepingJul 5, 2013
  26. Junio C HamanoJul 6, 2013
  27. John KeepingJul 6, 2013
  28. Junio C HamanoJul 7, 2013
  29. John KeepingJul 7, 2013
  30. brian m. carlsonJul 5, 2013
  31. Jeff KingJul 7, 2013
  32. Junio C HamanoJul 7, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.