如何将 pyOpenSSL verify_cb 的 ssl 证书中的 cn 关联到生成的套接字

发布于 2024-07-06 07:48:09 字数 145 浏览 5 评论 0原文

我对 pyOpenSSL 有点陌生。 我试图弄清楚如何将生成的套接字关联到 ssl 证书。 verify_cb 被调用,这使我可以访问证书和 conn,但发生这种情况时如何关联这些内容:

cli,addr = self.server.accept()

I am a little new to pyOpenSSL. I am trying to figure out how to associate the generated socket to an ssl cert. verify_cb gets called which give me access to the cert and a conn but how do I associate those things when this happens:

cli,addr = self.server.accept()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-07-13 07:48:09

握手完成后,即可获取客户端证书。 虽然客户端证书也可以在验证回调 (verify_cb) 中使用,但除了在该回调中验证证书之外,没有任何理由尝试执行任何操作。 最好在握手完全成功后设置特定于应用程序的映射。 因此,请考虑使用accept方法返回的OpenSSL.SSL.Connection实例来获取证书(并从那里获取commonName)并将其与此时的连接对象关联起来。 例如,

client, clientAddress = self.server.accept()
client.do_handshake()
commonNamesToConnections[client.get_peer_certificate().commonName] = client

您可能需要检查映射以确保不会覆盖任何现有连接(可能使用连接列表,而不是仅将每个公用名称映射到一个连接)。 当然,当连接丢失时,您需要删除条目。

“do_handshake”调用强制握手真正发生。 如果没有这个,当应用程序数据首次通过连接传输时,就会发生握手。 这很好,但它会使设置此映射稍微复杂一些。

After the handshake is complete, you can get the client certificate. While the client certificate is also available in the verify callback (verify_cb), there's not really any reason to try to do anything aside from verify the certificate in that callback. Setting up an application-specific mapping is better done after the handshake has completely successfully. So, consider using the OpenSSL.SSL.Connection instance returned by the accept method to get the certificate (and from there, the commonName) and associate it with the connection object at that point. For example,

client, clientAddress = self.server.accept()
client.do_handshake()
commonNamesToConnections[client.get_peer_certificate().commonName] = client

You might want to check the mapping to make sure you're not overwriting any existing connection (perhaps using a list of connections instead of just mapping each common name to one). And of course you need to remove entries when connections are lost.

The `do_handshake´ call forces the handshake to actually happen. Without this, the handshake will happen when application data is first transferred over the connection. That's fine, but it would make setting up this mapping slightly more complicated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文