Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ALPN #30

Merged
merged 1 commit into from
Sep 20, 2024
Merged

Support ALPN #30

merged 1 commit into from
Sep 20, 2024

Conversation

ZBCccc
Copy link
Contributor

@ZBCccc ZBCccc commented Sep 11, 2024

Added new constants, types, and functions to manage ALPN protocols.
Implemented ALPN callback functions and integrated them into the server and client examples.
Updated the Ctx struct to include an ALPN callback and provided methods to set ALPN protocols.
Enhanced the client to specify and negotiate ALPN protocols.
Added a method to retrieve the negotiated ALPN protocol from a connection.
Fixed minor typos in existing constants.

@ZBCccc ZBCccc changed the title ALPN bug修复 Support ALPN Sep 12, 2024
conn.go Outdated
var protoLen C.uint
C.SSL_get0_alpn_selected(c.ssl, &proto, &protoLen)
if protoLen == 0 {
return "", nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要设置error而不是nil,区别成功和失败情况

.gitignore Outdated
.idea/

# crt and key files
*.crt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

证书和密钥可能会更新或新增,不要放到.gitignore里面

shim.c Outdated
@@ -179,6 +179,10 @@ long X_SSL_CTX_set_tlsext_servername_callback(
return SSL_CTX_set_tlsext_servername_callback(ctx, cb);
}

void X_SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx, int (*cb)(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg), void *arg) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接调用SSL_CTX_set_alpn_select_cb就行,没必要封装成X_SSL_CTX_set_alpn_select_cb()

ntls_test.go Outdated
ret := ssl.SslSelectNextProto(out, outlen, unsafe.Pointer(&protoList[0]), uint(len(protoList)), in, inlen)

if ret != OPENSSL_NPN_NEGOTIATED {
return SSLTLSExtErrNoAck
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

协商失败应该返回SSL_TLSEXT_ERR_ALERT_FATAL

@@ -101,6 +104,28 @@ func newNTLSServerWithSNI(acceptAddr string, certKeyPairs map[string]crypto.GMDo
return nil, err
}

// Set ALPN callback
ctx.SetTLSExtAlpnCallback(func(ssl *ts.SSL, out unsafe.Pointer, outlen unsafe.Pointer, in unsafe.Pointer, inlen uint, arg unsafe.Pointer) ts.SSLTLSExtErr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前看觉得没有必要给应用提供SetTLSExtAlpnCallback接口,可以直接内部调用SSL_select_next_proto。

服务端设置ALPN无疑就是和客户端协商而已,所以可以统一客户和服务端接口即可。

ntls_test.go Outdated
@@ -608,15 +609,15 @@ func handleConn(conn net.Conn) {
}
}

func TestSNI(t *testing.T) {
func TestSNIAndALPN(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SNI和ALPN测试分离,后面可能会包含更多扩展功能,独立测试

ctx.go Outdated

// SetTLSExtAlpnCallback sets callback function for Application Layer Protocol Negotiation
// (ALPN) rfc7301 (https://tools.ietf.org/html/rfc7301).
func (c *Ctx) SetTLSExtAlpnCallback(alpn_cb TLSExtAlpnCallback, arg unsafe.Pointer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成内部接口,不需要暴露给上层应用

ctx.go Outdated
}

// SetALPNProtos sets the ALPN protocol list
func (ctx *Ctx) SetALPNProtos(protos []string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名字改一下,SetALPNProtos -> SetClientALPNProtos

@@ -101,6 +102,10 @@ func newNTLSServerWithSNI(acceptAddr string, certKeyPairs map[string]crypto.GMDo
return nil, err
}

// Set ALPN
supportedProtos := []string{"h2", "http/1.1"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成命令行传入协议列表

ssl.go Outdated
// It takes pointers to the output buffer, output length, server buffer,
// server length, input buffer, and input length as parameters. The function
// returns an integer indicating the result of the selection process.
func (s *SSL) SslSelectNextProto(out unsafe.Pointer, outlen unsafe.Pointer, server unsafe.Pointer, serverlen uint, in unsafe.Pointer, inlen uint) C.int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SslSelectNextProto是不是也可以改成内部接口?没必要暴露吧

…he SSL/TLS context and connection handling.

Added new constants, types, and functions to manage ALPN protocols.
Implemented ALPN callback functions and integrated them into the server and client examples.
Updated the Ctx struct to include an ALPN callback and provided methods to set ALPN protocols.
Enhanced the client to specify and negotiate ALPN protocols.
Added a method to retrieve the negotiated ALPN protocol from a connection.
Fixed minor typos in existing constants.
Copy link
Member

@dongbeiouba dongbeiouba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dongbeiouba dongbeiouba merged commit afd46e8 into Tongsuo-Project:main Sep 20, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants