0

目前我的应用程序(在 C 中)使用 SSL 证书对 Web 服务器进行身份验证。我现在将大部分功能(如果不是全部)移至 Tcl。

我找不到任何关于如何做到这一点的教程或示例(我更喜欢使用 Tcl ::http:: 但 TclCurl 会很好)。

有什么建议么?

谢谢

4

2 回答 2

3

Johannes 的回答是对的,除非您想为不同的站点提供不同的身份。在这种情况下,您可以使用,它允许您在调用该命令之前tls::init设置默认的 TLS 相关选项。tls::socket

package require http
package require tls
http::register https 443 ::tls::socket

# Where is our identity?
tls::init -keyfile "my_key.p12" -cafile "the_server_id.pem"

# Now, how to provide the password (don't know what the arguments are)
proc tls::password args {
    return "the_pass";  # Return whatever the password is
}

# Do the secure connection
set token [http::geturl https://my.secure.site/]

# Disable the key
tls::init -keyfile {}

Note that the way of providing the password is bizarre, and I know for sure that this mechanism isn't going to be nice when doing asynchronous connections. (There's a standing Feature Request for improving the integration between the http and tls packages…)

于 2013-03-29T17:24:15.580 回答
2

要将 https 与 tcl 一起使用,您通常使用该tls包。http 包的手册页为您提供了如何执行此操作的示例:

package require http
package require tls

::http::register https 443 ::tls::socket

set token [::http::geturl https://my.secure.site/]

如果您阅读tls包的文档tls::socket,您会发现有一些选项可以通过客户端证书。结合起来给你:

::http::register https 443 [list ::tls::socket \
        -cafile caPublic.pem -certfile client.pem]

-password如果证书文件受密码保护,您可能必须指定回调参数。

请注意,此解决方案对来自您的应用程序的每个 https(无论目标如何)请求都使用客户端证书。

编辑:正如 Donal 所建议的,使用它可能tls::init比使用::http::register.
一个例子:

package require http
package require tls

::http::register https 443 ::tls::socket

proc ::get_cert_pass {} {
     return "passw0rd"
}

# Add the options here
::tls::init -cafile caPublic.pem -certfile client.pem -password ::get_cert_pass
set tok [::http::geturl https://my.secure.site/]

要发出请求,请始终使用最后 2 行。

于 2013-03-29T15:36:12.307 回答