如何实现服务器端 SMTP STARTTLS?
Posted
技术标签:
【中文标题】如何实现服务器端 SMTP STARTTLS?【英文标题】:How can I implement server side SMTP STARTTLS? 【发布时间】:2016-01-03 05:45:33 【问题描述】:我正在尝试使用 Vala 和 GLib + GIO 实现一个简单的 SMTP 服务器。
到目前为止,纯文本通信没有问题,但是当涉及到使用 STARTTLS 的 TLS 时,事情就变得更加困难了。
这是我目前的代码:
const string appname = "vsmtpd";
const string hostname = "myserver";
const uint16 listenport = 10025;
const string keyfile = "vsmtpd.key";
const string certfile = "vsmtpd.crt";
// TODO: Parse EHLO instead of constant string
const string username = "myclient";
void process_request_plain (InputStream input, OutputStream output) throws Error
output.write (@"220 $hostname ESMTP $appname\n".data);
var data_in = new DataInputStream (input);
string line;
while ((line = data_in.read_line (null)) != null)
stdout.printf ("%s\n", line);
line = line.chomp ();
if (line.substring (0, 5) == "EHLO ")
output.write (@"250-$hostname Hello $username\n".data);
output.write ("250 STARTTLS\n".data);
else if (line == "STARTTLS")
output.write ("220 Go ahead\n".data);
break;
else
output.write ("502 Command not implemented\n".data);
int main ()
try
TlsCertificate cert = new TlsCertificate.from_files
(certfile, keyfile);
var service = new SocketService ();
service.add_inet_port (listenport, null);
service.start ();
while (true)
SocketConnection conn = service.accept (null);
process_request_plain (conn.input_stream, conn.output_stream);
TlsServerConnection tlsconn = TlsServerConnection.@new (conn, cert);
assert_nonnull (tlsconn);
// TODO: Is this neccessary?
tlsconn.accept_certificate.connect ((peer_cert, errors) =>
stdout.printf ("TLS accepting peer cert\n");
return true;
);
try
tlsconn.handshake ();
stdout.printf ("TLS handshake ok\n");
catch (Error e)
stdout.printf ("TLS handshake failed\n");
stderr.printf ("%s\n", e.message);
catch (Error e)
stderr.printf ("%s\n", e.message);
return 0;
在vsmtpd.key
和vsmtpd.crt
(我使用openssl req -x509 -newkey rsa:2048 -keyout vsmtpd.key -out vsmtpd.pem -days 365 -nodes
生成)中给定一个有效的SSL 证书,我启动程序并运行这个OpenSSL 命令来测试STARTTLS:
openssl s_client -connect localhost:10025 -starttls smtp -debug
我的程序的输出是:
EHLO openssl.client.net
STARTTLS
TLS handshake failed
Stream is already closed
OpenSSL 的输出是:
CONNECTED(00000003)
read from 0x6ae470 [0x6af050] (4096 bytes => 26 (0x1A))
0000 - 32 32 30 20 6d 79 73 65-72 76 65 72 20 45 53 4d 220 myserver ESM
0010 - 54 50 20 76 73 6d 74 70-64 0a TP vsmtpd.
write to 0x6ae470 [0x6b0060] (25 bytes => 25 (0x19))
0000 - 45 48 4c 4f 20 6f 70 65-6e 73 73 6c 2e 63 6c 69 EHLO openssl.cli
0010 - 65 6e 74 2e 6e 65 74 0d-0a ent.net..
read from 0x6ae470 [0x6af050] (4096 bytes => 28 (0x1C))
0000 - 32 35 30 2d 6d 79 73 65-72 76 65 72 20 48 65 6c 250-myserver Hel
0010 - 6c 6f 20 6d 79 63 6c 69-65 6e 74 0a lo myclient.
read from 0x6ae470 [0x6af050] (4096 bytes => 13 (0xD))
0000 - 32 35 30 20 53 54 41 52-54 54 4c 53 0a 250 STARTTLS.
write to 0x6ae470 [0x7ffdb4aea9e0] (10 bytes => 10 (0xA))
0000 - 53 54 41 52 54 54 4c 53-0d 0a STARTTLS..
read from 0x6ae470 [0x6a13a0] (8192 bytes => 13 (0xD))
0000 - 32 32 30 20 47 6f 20 61-68 65 61 64 0a 220 Go ahead.
write to 0x6ae470 [0x6aefa0] (204 bytes => 204 (0xCC))
0000 - 16 03 01 00 c7 01 00 00-c3 03 03 0e ac 05 35 45 ..............5E
0010 - db 95 f6 a7 37 55 d8 ca-14 d7 5f 8e 6a 62 08 50 ....7U...._.jb.P
0020 - c9 81 b7 55 75 a8 4c 17-c0 a1 53 00 00 76 00 a5 ...Uu.L...S..v..
0030 - 00 a3 00 a1 00 9f 00 6b-00 6a 00 69 00 68 00 39 .......k.j.i.h.9
0040 - 00 38 00 37 00 36 00 88-00 87 00 86 00 85 00 9d .8.7.6..........
0050 - 00 3d 00 35 00 84 00 a4-00 a2 00 a0 00 9e 00 67 .=.5...........g
0060 - 00 40 00 3f 00 3e 00 33-00 32 00 31 00 30 00 9a .@.?.>.3.2.1.0..
0070 - 00 99 00 98 00 97 00 45-00 44 00 43 00 42 00 9c .......E.D.C.B..
0080 - 00 3c 00 2f 00 96 00 41-00 07 00 05 00 04 00 16 .<./...A........
0090 - 00 13 00 10 00 0d 00 0a-00 15 00 12 00 0f 00 0c ................
00a0 - 00 09 00 ff 02 01 00 00-23 00 23 00 00 00 0d 00 ........#.#.....
00b0 - 16 00 14 06 01 06 02 05-01 05 02 04 01 04 02 03 ................
00c0 - 01 03 02 02 01 02 02 00-0f 00 01 01 ............
read from 0x6ae470 [0x6b4500] (7 bytes => -1 (0xFFFFFFFFFFFFFFFF))
write:errno=104
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 80 bytes and written 239 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
---
我从输出中了解到,我的程序在 TLS 握手完成之前关闭了连接。 (我也尝试过使用 Thunderbird 和 Claws Mail)
我在这里做错了什么?
PS:我找不到任何关于如何在 STARTTLS 情况下使用 GTLsServerConnection
的示例。
更新:
我尝试了 OpenSSL 的 -ssl2、-ssl3、-tls1、-tls1_1、-tls1_2 选项,但也不起作用。
openssl s_client -connect localhost:10025 -starttls smtp -state
产量:
CONNECTED(00000003)
SSL_connect:before/connect initialization
SSL_connect:SSLv2/v3 write client hello A
SSL_connect:error in SSLv2/v3 read server hello A
write:errno=104
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 100 bytes and written 239 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
---
所以客户端发送“client hello A”,但服务器没有发送正确的“server hello A”。
您也可以尝试gnutls-cli --crlf --starttls-proto=smtp --port 10025 localhost
。
GNUTLS_DEBUG_LEVEL=11 ./vsmtpd
的输出是:
gnutls[2]: Enabled GnuTLS logging...
gnutls[2]: Intel SSSE3 was detected
gnutls[2]: Intel AES accelerator was detected
gnutls[2]: Intel GCM accelerator was detected
gnutls[2]: Enabled GnuTLS logging...
gnutls[2]: Intel SSSE3 was detected
gnutls[2]: Intel AES accelerator was detected
gnutls[2]: Intel GCM accelerator was detected
gnutls[3]: ASSERT: x509_b64.c:299
gnutls[9]: Could not find '-----BEGIN RSA PRIVATE KEY'
gnutls[3]: ASSERT: x509_b64.c:299
gnutls[9]: Could not find '-----BEGIN DSA PRIVATE KEY'
gnutls[3]: ASSERT: x509_b64.c:299
gnutls[9]: Could not find '-----BEGIN EC PRIVATE KEY'
gnutls[3]: ASSERT: privkey.c:503
gnutls[2]: Falling back to PKCS #8 key decoding
EHLO openssl.client.net
STARTTLS
gnutls[5]: REC[0xfa67e0]: Allocating epoch #0
gnutls[3]: ASSERT: gnutls_constate.c:586
gnutls[5]: REC[0xfa67e0]: Allocating epoch #1
gnutls[3]: ASSERT: gnutls_buffers.c:1138
gnutls[10]: READ: -1 returned from 0xfa4120, errno=0 gerrno=5
gnutls[3]: ASSERT: gnutls_buffers.c:364
gnutls[3]: ASSERT: gnutls_buffers.c:572
gnutls[3]: ASSERT: gnutls_record.c:1058
gnutls[3]: ASSERT: gnutls_record.c:1179
gnutls[3]: ASSERT: gnutls_buffers.c:1392
gnutls[3]: ASSERT: gnutls_handshake.c:1428
gnutls[3]: ASSERT: gnutls_handshake.c:3098
gnutls[3]: ASSERT: gnutls_db.c:334
TLS handshake failed
Stream is already closed
gnutls[5]: REC[0xfa67e0]: Start of epoch cleanup
gnutls[5]: REC[0xfa67e0]: End of epoch cleanup
gnutls[5]: REC[0xfa67e0]: Epoch #0 freed
gnutls[5]: REC[0xfa67e0]: Epoch #1 freed
【问题讨论】:
如果你能让它工作,这将是一个很好的例子。你至少得到了一个响应 - (7字节=> -1(0xFFFFFFFFFFFFFFFF))。您可以按照***.com/questions/24457408/… 中的建议尝试一些 openssl 选项的变体 我不确定 Vala 代码是否存在问题。 developer.gnome.org/gio/stable/… 说握手可以被称为“如果你想确定初始握手是成功还是失败”。因此,在初始握手失败后,“流已关闭”正如预期的那样。问题可能出在您的证书上。我注意到您使用“myserver”但连接到“localhost”。检查服务器名称? 我也相信 GnuTLS 是后端,所以尝试设置 GNUTLS_DEBUG_LEVEL - 请参阅gnutls.org/manual/html_node/Debugging-and-auditing.html 您也可以尝试使用 ssldump 来监听连接,例如sslshopper.com/article-debugging-ssl-communications.html @AIThomas:我已经添加了 gnutls 的调试输出,还添加了我用来创建密钥文件的命令。 好的,这是信息。看起来好像它可能找不到私钥。可能需要在 /etc/ssl/certs/ 我也得到你不了解域名和证书的印象。您还需要了解证书颁发机构和自签名证书。这可能会给出一些指示:help.ubuntu.com/community/GnuTLS 【参考方案1】:问题隐藏在DataInputStream
的实现中。
一旦我删除它并使用以下替换 read_line ()
代替,它就可以正常工作。
string? read_line (InputStream input) throws Error
var buffer = new uint8[1];
var sb = new StringBuilder ();
buffer[0] = '\0';
while (buffer[0] != '\n')
input.read (buffer);
sb.append_c ((char) buffer[0]);
return (string) sb.data;
void process_request_plain (InputStream input, OutputStream output) throws Error
output.write (@"220 $hostname ESMTP $appname\n".data);
string line;
while ((line = read_line (input)) != null)
stdout.printf ("%s\n", line);
line = line.chomp ();
if (line.substring (0, 5) == "EHLO ")
output.write (@"250-$hostname Hello $username\n".data);
output.write ("250 STARTTLS\n".data);
else if (line == "STARTTLS")
output.write ("220 Go ahead\n".data);
break;
else
output.write ("502 Command not implemented\n".data);
【讨论】:
以上是关于如何实现服务器端 SMTP STARTTLS?的主要内容,如果未能解决你的问题,请参考以下文章
SMTP -> 错误:服务器不接受 AUTH:530 5.7.0 必须先发出 STARTTLS 命令。 c3sm5545732pax.9 - gsmtp PHPMailer
PHP - 使用 STARTTLS 和自签名证书的 Swiftmailer