2013-03-29 57 views
0

當前,我的應用程序(使用C語言)使用SSL證書向Web服務器進行身份驗證。我現在將大部分功能(如果不是全部的話)轉移到Tcl。發送客戶端證書到Tcl中的服務器

我找不到任何教程或示例如何做到這一點(我喜歡使用Tcl :: http ::但TclCurl會很好)。

有什麼建議嗎?

感謝

回答

2

要使用https與TCL您通常使用的tls包。對於http package手冊頁給你一個例子,如何做到這一點:

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行,然後。

+0

IIRC,你通過創建一個名爲':: tls :: password'的命令來處理密碼([see example](http:// wiki .tcl.tk/2630#pagetocf176242a))返回相關的密碼。 (不,我不知道什麼樣的正式論點。) –

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 {} 

注意提供密碼的方式是奇怪的,我肯定知道,這個機制是不會做異步連接時是好的。 (有一個長期的功能請求,以改善http和tls包之間的集成...)

+0

根據tls文檔,不推薦使用'tls :: password'。更好地使用'-password'(默認IS'tls :: password')參數來指定回調。沒有參數被追加。請參閱文檔的[Callback Section](http://tls.cvs.sourceforge.net/viewvc/tls/tls/tls.htm?revision=HEAD#CALLBACK%20OPTIONS)。 –

+0

@Johannes看來,這只是在文檔的某些版本(不包括我看到的[版本](http://www.sensus.org/tcl/tls.htm))。伊克。 –

相關問題