英文:
Upgrade a connection to TLS in Go
问题
我有一个打开的TCP连接,并使用for循环从中读取,代码如下:
for {
// tx.Text的类型是textproto.Conn
// 底层连接存储在tx.Conn中
l, err := tx.Text.Reader.ReadLine()
// 处理文本行...
}
现在我想通过以下方式升级连接到TLS(TlsConf
包含使用tls.LoadX509KeyPair
加载的证书)
tx.Conn = tls.Server(tx.Conn, tx.Server.Conf.TlsConf)
tx.Text = textproto.NewConn(tx.Conn)
当我这样做时,当服务器尝试握手时,客户端会出现段错误。我正在实现一个SMTP服务器,并使用swaks的-tls
标志进行测试。swaks的终端输出如下:
-> STARTTLS
<- 220 Start TLS
Segmentation fault: 11
由于swaks是一个经过测试的工具,并且在我之前使用的nodeJS SMTP实现中工作正常,我不认为错误在客户端。
我做错了什么或者缺少了什么?
附注:当从现有的不安全连接启动TLS连接时,会发生什么?客户端是在不同的端口上建立新连接还是重用连接?
英文:
I have an open TCP connection and read from it with a for loop like so
for {
// tx.Text is of type textproto.Conn
// the underlying connection is stored in tx.Conn
l, err := tx.Text.Reader.ReadLine()
// do stuff with the text line ...
}
Now I want to upgrade the connection to TLS like this (TlsConf
contains a certificate loaded with tls.LoadX509KeyPair
)
tx.Conn = tls.Server(tx.Conn, tx.Server.Conf.TlsConf)
tx.Text = textproto.NewConn(tx.Conn)
When I do this I get a segmentation fault on the client when the server attempts a handshake. I'm implementing a SMTP server and am testing it with swaks using the -tls
flag. The terminal output of swaks is the following
-> STARTTLS
<- 220 Start TLS
Segmentation fault: 11
Since swaks is a tested tool, and worked with the nodeJS SMTP implementation I had before, I don't suspect the error is on the client side.
What did I do wrong or what is missing?
PS: When a TLS connection is started from an existing insecure connection, what does exactly happen? Does the client establish a new connection on a different port or is the connection reused?
答案1
得分: 14
这是如何将net.conn升级为tls.conn的方法:
1)在代码的某个地方,你定义了这些变量
var TLSconfig *tls.Config
...
// conn是一个普通的net.Conn类型的连接
conn, err := listener.Accept()
...
2)在上面的某个地方初始化TLSConfig,可以像这样做
cert, err := tls.LoadX509KeyPair("/path/to/cert", "/path/to/key")
if err != nil {
// ...
}
TLSconfig = &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.VerifyClientCertIfGiven,
ServerName: "example.com"}
3)此时你正在读写一个标准连接。
当客户端发出STARTTLS命令时,在你的服务器中执行以下操作:
// 初始化一个新的TLS连接。我需要一个*tls.Conn类型的连接
// 这样我才能执行Handshake()
var tlsConn *tls.Conn
tlsConn = tls.Server(client.socket, TLSconfig)
// 运行握手
tlsConn.Handshake()
// 这是关键。由于我不再需要访问任何TLS函数,
// 我可以将tlsConn转换回net.Conn类型
conn = net.Conn(tlsConn)
接下来,你可能需要使用新的连接更新缓冲区等。
像这样测试你的服务器:
openssl s_client -starttls smtp -crlf -connect example.com:25
这样可以通过tls连接与服务器进行交互,并可以发出一些命令等。
关于Go中的类型转换的更多信息
我猜类型转换是Go如此强大的另一个原因!
http://golang.org/ref/spec#Conversions
http://golang.org/doc/effective_go.html#conversions
英文:
Here's how to upgrade a net.conn to tls.con:
-
Somewhere in your code, you have these variables defined
var TLSconfig *tls.Config
...
// conn is a normal connection of type net.Conn
conn, err := listener.Accept()
... -
Initialize TLSConfig somewhere above, do something like this
cert, err := tls.LoadX509KeyPair("/path/to/cert", "/path/to/key")
if err != nil {
// ...
}
TLSconfig = &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.VerifyClientCertIfGiven,
ServerName: "example.com"} -
At this point you are reading/writing to a standard connection.
When the client issues STARTTLS command, do this in your server:
// Init a new TLS connection. I need a *tls.Conn type
// so that I can do the Handshake()
var tlsConn *tls.Conn
tlsConn = tls.Server(client.socket, TLSconfig)
// run a handshake
tlsConn.Handshake()
// Here is the trick. Since I do not need to access
// any of the TLS functions anymore,
// I can convert tlsConn back in to a net.Conn type
conn = net.Conn(tlsConn)
Next, you may probably update your buffers with the new connection, etc.
Test your server like this:
openssl s_client -starttls smtp -crlf -connect example.com:25
This allows you to interact with the server through the tls connection and you can issue some commands, etc.
More about conversions in Go
I guess conversions are another reason for what makes Go so powerful!
答案2
得分: 1
放弃了swaks,使用Go自带的smtp.SendMail构建了一个小工具来测试TLS:
package main
import (
"fmt"
"net/smtp"
)
func main() {
err := smtp.SendMail(
"127.0.0.1:2525",
nil,
"src@test.local",
[]string{"dst@test.local"},
[]byte("Hello! Just testing."),
)
if err != nil {
panic(err)
}
}
英文:
Ditched swaks, built a small tool to test TLS using Go's own smtp.SendMail:
package main
import (
"fmt"
"net/smtp"
)
func main() {
err := smtp.SendMail(
"127.0.0.1:2525",
nil,
"src@test.local",
[]string{"dst@test.local"},
[]byte("Hello! Just testing."),
)
if err != nil {
panic(err)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论