简体   繁体   English

在 C# 中临时加载用于客户端身份验证的 SSL 客户端密钥

[英]Temporarily load SSL Client Key for Client Authentication in C#

I am using the WebBrowser control to add a WebInterface to C# app.我正在使用 WebBrowser 控件将 WebInterface 添加到 C# 应用程序。 My desire is to verify that only such app is able to connect to our Web server using SSL client certificates.我的愿望是验证只有这样的应用程序才能使用 SSL 客户端证书连接到我们的 Web 服务器。

My idea was to embed the client certificate in the app and just use when connecting via my app.我的想法是将客户端证书嵌入到应用程序中,并在通过我的应用程序连接时使用。 Anybody have a sugestion on how to do this?有人对如何做到这一点有建议吗? Or the only way to make it work is to load the key in the X509Store.或者让它工作的唯一方法是在 X509Store 中加载密钥。

If I put it in X509Store, will it make my key available for general Internet Explorer Usage?如果我将它放在 X509Store 中,它是否会使我的密钥可用于一般 Internet Explorer 使用?

So, several thoughts here:所以,这里有几个想法:

1. 1.

I agree with 'erickson', validating that ONLY your app can communicate with the app is nearly impossible with your current design.我同意 'erickson',用你当前的设计来验证只有你的应用程序可以与应用程序通信几乎是不可能的。 It's just a matter of time before someone reverse engineer's your app and then its game over (if that's you only form of security).有人对您的应用程序进行逆向工程,然后游戏结束(如果这只是您的安全形式),这只是时间问题。 If you want to validate that its your app and a valid user then you need to authenticate the user as well as some mechanism of checking the signature of the app in question (which I don't believe is possible in a client-server model...after all I can always lie and say that my 'hackyou' app has the same signature as your 'realapp' and you can't verify that from the server-side)如果您想验证它是您的应用程序和有效用户,那么您需要对用户进行身份验证以及检查相关应用程序签名的某种机制(我认为这在客户端 - 服务器模型中是不可能的。 ..毕竟我总是可以撒谎说我的“hackyou”应用程序与您的“realapp”应用程序具有相同的签名,并且您无法从服务器端进行验证)

2. 2.

Remember the WebBrowser control is essentially a wrapper around IE, so without some tricks (which I'll get to in a sec) you would have to add the cert to the user store.请记住,WebBrowser 控件本质上是 IE 的包装器,因此如果没有一些技巧(我将在稍后介绍),您将不得不将证书添加到用户存储中。

3. 3.

Here's a hacky way to accomplish what you're asking (even though its a bad idea):这是一种完成您所要求的操作的hacky方法(即使这是一个坏主意):

  • First use the WebRequest.Create to create a HttpWebRequest object首先使用WebRequest.Create创建一个 HttpWebRequest 对象
  • Manually load a X509Certificate2 object from either a file or the binary stream encoded in the program从文件或程序中编码的二进制流中手动加载 X509Certificate2 对象
  • use the HttpWebRequest.ClientCertificates to add your cert to the webrequest使用HttpWebRequest.ClientCertificates将您的证书添加到 webrequest
  • Send the request, get the response发送请求,得到响应
  • Send the response to the WebBrowser by pushing the ResponseStream of the HttpWebResponse to the DocumentStream of the WebBrowser通过将 HttpWebResponse 的 ResponseStream 推送到 WebBrowser 的 DocumentStream 将响应发送到 WebBrowser

This essentially means that you will have to write some wrapper classes to handle the Requests and Responses to and from the Server and are just using the WebBrowser to handling the viewing of the HTML.这实质上意味着您将不得不编写一些包装类来处理与服务器之间的请求和响应,并且仅使用 WebBrowser 来处理 HTML 的查看。

In reality, you need to redesign and look at the threats you're trying to handle!实际上,您需要重新设计并查看您要处理的威胁!

Are you sure this is what you want to do?你确定这是你想要做的吗? If you embed the private key in your application (as your approach entails), an attacker can extract it and use it to authenticate their rogue software.如果您将私钥嵌入到您的应用程序中(正如您的方法所需要的那样),攻击者可以提取它并使用它来验证他们的流氓软件。

A server cannot authenticate client software.服务器无法验证客户端软件。 It can only test whether a client possesses some secret.它只能测试客户端是否拥有某个秘密。 When you embed a private key in your client and distribute it, it will not be a secret anymore.当您在客户端中嵌入私钥并分发它时,它就不再是秘密了。

I'd recommend authenticating users of your software, rather than the software itself.我建议对您的软件的用户进行身份验证,而不是对软件本身进行身份验证。 You need to let users generate their own secret, whether it is a password or a private key, and give them an incentive to protect it.您需要让用户生成他们自己的秘密,无论是密码还是私钥,并激励他们保护它。

The intent of using the key is not so much to validate the users as to restrict access to users of the app instead of using any WebBrowser.使用密钥的目的不是验证用户,而是限制对应用程序用户的访问,而不是使用任何 WebBrowser。 This is sort of an intranet behavior over the public internet.这是一种通过公共互联网的内部网行为。

This is a poor man's DRM.这是穷人的DRM。 The losses due to people extracting the key are not that important.由于人们提取密钥而造成的损失并不那么重要。 I think the risk of this happening is low and what we could loose is minimal.我认为发生这种情况的风险很低,我们可能失去的东西也很小。

Nevertheless if there is any other idea to restrict access to the WebServer to only users of the App I am open for any suggestions.尽管如此,如果有任何其他想法将 WebServer 的访问权限限制为仅该应用程序的用户,我愿意接受任何建议。 Basically my desire is now to have a public WebServer wide open to be read by anyone, but access over the public network from diverse places is necessary so setting up a intranet infrastructure is not possible either.基本上,我现在的愿望是拥有一个公开的 Web 服务器,任何人都可以阅读,但是从不同的地方通过公共网络进行访问是必要的,因此也无法建立内部网基础设施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM