[英]How to create a secure http server in dart?
我正在尝试将我的dart http服务器设置为仅使用https运行。 所以我收集我需要使用HttpServer.bindSecure
但是我不清楚描述需要传递什么作为certificateName
以及requestClientCertificate
是否真实使得它或多或少安全,或者对安全性没有任何影响。 HttpServer
页面顶部的小样本代码在certificateName: 'localhost_cert'
传递certificateName: 'localhost_cert'
但在此之前它对数据库执行某些操作,但无论如何似乎都没有使用它。 任何人都可以更详细地解释这些价值观是什么以及它们需要什么才能使它们安全?
bindSecure的requestClientCertificate参数用于指定客户端证书。 服务器使用客户端证书来识别和授权客户端,这似乎不是此问题的目标。 应该注意的是,在IE9和Windows 7上使用Dart中的客户端证书存在已知问题 。
certificateName参数用于指定证书数据库中存在的证书的昵称。 使用certutil将证书导入数据库时,使用-n <nickname>
选项指定证书昵称。
使用以下步骤:
安装NSS实用程序(包括certutil),
使用密码<password>
在目录<dir>
创建新的证书数据库,并且
导入由昵称<host>
标识的自签名或购买的证书,以便可以使用以下示例代码创建HTTPS服务器。 虽然昵称可以任意选择,但我们在此示例中使用主机名。 这些步骤已在Ubuntu 14.04和Dart SDK 1.6到(目前最后一个稳定版本)1.8.3中得到确认。
安装NSS实用程序
sudo apt-get install libnss3-tools
cd到包含证书数据库的目录
cd <dir>
创建用于证书数据库的密码文件:
echo "<password>" > pwdfile
创建证书数据库
certutil -N -d 'sql:./' -f pwdfile
或者:
生成自签名证书:
certutil -S -s "cn=<host>" -n "self signed for dart" -x -t "C,C,C" -m 1000 -v 120 -d "sql:./" -k rsa -g 2048 -f pwdfile
其中<host>
是要为其生成证书的主机(“公用名”),例如“localhost”
或者,首先通过为真实域<host>
创建签名请求来购买证书,例如“myhost.com”:
certutil -R -s "CN=<host>, O=None, L=San Diego, ST=California, C=US" -a -g 2048 -o <host>.csr -d "sql:./"
然后在从签名机构购买证书时提示输入CSR时,指定文件<host>
.csr的内容。
将购买的证书复制到名为<host>
.crt的文件中
将证书导入数据库
certutil -A -n <host> -t "p,p,p" -i <host>.crt -d "sql:./"
如果需要使用中间证书,可以按原样导入:
certutil -A -n my_intermediate_certificate -t "p,p,p" -i intermediate.crt -d "sql:./"
其中“intermediate.crt”是从签名机构下载的中间证书文件。
验证数据库中是否存在证书
certutil -L -n <host> -d "sql:./"
certutil -L -n my_intermediate_certificate -d "sql:./"
要使用此证书并创建HTTPS服务器,请执行以下操作:
// Initialize secure socket to use certificate database (note: replace `<dir>`
// with the absolute path to the certificate database directory, and `<password>`
// with the value chosen above)
SecureSocket.initialize(database: "<dir>", password: "<password>");
// Bind secure HTTP server to specified host and port (typically 443)
HttpServer.bindSecure("<host>", 443, certificateName: "<host>")
.then((HttpServer httpServer) {
// Listen for incoming requests
httpServer.listen((HttpRequest httpRequest) {
// TODO: process request
});
})
.catchError((error) {
// TODO: handle error
});
更新
我没有足够的声誉点来回复评论,所以这里有一些可能有助于回答问题的其他细节:客户端证书不用于加密客户端 - 服务器通信,在建立安全通信的常见场景中不需要通过HTTPS的Web浏览器和Web服务器。 上面列出的步骤显示了如何使用bindSecure在Dart中创建HTTPS服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.