stunnel配置实例

Finisky Garden · · 3366 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

stunnel简介

stunnel是一个开源跨平台进行通信加密的软件。它可以对原生不支持加密通信的服务(如FTP, Telnet等)在上层提供加密功能,而无须修改这些服务的代码。

stunnel分为client和server两种不同的角色,二者之间的通信使用X509证书进行加密。在初装时,stunnel会自动生成一个自签名的证书用于加密,但它更推荐用户使用自己生成的证书。

stunnel证书生成

用OpenSSL生成自签名的证书其实非常容易,在已安装OpenSSL的Linux环境下:

$ openssl req -new -x509 -days 365 -nodes -out stunnel.pem -keyout stunnel.pem
上述命令中唯一需要注意的是-days 365参数,声明该自签名证书的有效期,可自行按需更改。运行后需要键入一些信息,可一路回车使用默认值。

输出内容:

Generating a 2048 bit RSA private key
..+++
.............................+++
unable to write 'random state'
writing new private key to 'stunnel.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:
此时,证书已经在当前目录生成好了,包括了私钥和证书,cat stunnel.pem显示它大概长这样
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDFfPlpkHBRLadK
wJP9+bfRflQzmGUAYiy6DaXPzYuHboVAKblTGqinMD9tqSWbTG6nbA+d30xc2eFM
+4YiXgvMCHh+uszimcRZHAOmePCN4AjJTOcT8WmSBdyiq4OT6a0URPhWpE7pCDqQ
KPa394vJbXK+T1UfGVDLEOiNXGsohx3CVEnTlSSo3BaODNpM2zUn3JQZSZY80Vxt
bfsg2dxP/ssoS62YPqsTNn7sZvkDR8AIR+Q4tW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h5FuRhJi13z0LSFMmxh1Mw
DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAlM7Bt8Vp//mtUBmLxiQI
XLTThPAxjgGvMaoyhPGRajuENpQuQlqlAw92kw8hMAYxx5I1PGRSx4Hdok1RWaTo
cmbio6VhID3iDWpBHShv8ZlfAILnBq6RXNc+/c6nOIsnB8/wt43XxdVmEiHK/PmX
jdtJV9IW9doavXLb36iOh6Z5EvJFvegJsUB4Vjbtvz8Ukr9vEviWCaMbinFueT1G
BIw7mhyUrQhVHTAapQ9d2JvecQyolzxvDsUMU1qedTyowsoJ/eCs9uu/cHrae3Va
YGRSJ1Un/k9LzbXiliAOVpSVAMbwHfJZtSVLn0b2GRqHO5SKSnPAtABCCbifuLvZ
9g==
-----END CERTIFICATE-----
此时需要将此生成的证书放在一个稳妥的目录下,如下文所提到的默认配置文件目录。

stunnel配置说明

stunnel的默认配置文件存放目录:

  • Linux:/etc/stunnel

  • Windows:C:Files (x86) ## Server配置 典型的Server配置如下:

    cert = /etc/stunnel/stunnel.pem # server使用的证书
    key = /etc/stunnel/stunnel.pem # server使用的私钥
    CAfile = /etc/stunnel/stunnel.pem # 验证client合法性的证书
    verify = 3 # 验证方式,下面详述
    client = no # server配置成no
    
    [telnet]
    accept = 1234 # [HOST:]PORT, 不指定HOST, 则监听所有IPv4地址
    connect = 127.0.0.1:6789 # [HOST:]PORT
    
    [ftp]
    accept = 12345
    connect = 127.0.0.1:67890
    其中verify这个参数用于指定对对方的验证方式:

  • 不指定:不验证客户端出示的证书。

  • verify = 1:如果对方出示了证书,则验证;不出示则通过。

  • verify = 2:证书的CA必须与CAfile指定的匹配,否则不通过。

  • verify = 3:证书的CA与证书内容必须与CAfile指定的匹配,否则不通过。

对于自签名的证书,建议使用verify = 3的方式,然后需要通过安全的方式线下将证书分发到client。

Client配置

对应的client配置示例:

cert = /etc/stunnel/stunnel.pem # client使用的证书,与server端CAfile一致才能通过验证
client = yes # client配置成yes

[telnet]
accept = 127.0.0.1:6789 # [HOST:]PORT
connect = {serverip}:1234 # 替换serverip
此处不再设置verify参数,一般无须验证server的合法性。

References

stunnel

stunnel HOWTO

本文来自:Finisky Garden

感谢作者:Finisky Garden

查看原文:stunnel配置实例

3366 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传