我想知道是否有一种简单/推荐的方法来验证 Cordova 中的远程站点证书。我希望我的应用程序验证 $remote.thumbprint 是否在预期指纹列表中,并且没有任何 MITM。代码(和列表)应该通过应用商店部署在手机上(我只是假设它们是可信的)。
最好是不需要针对 Android、IOS 和 WP 的平台特定代码的直接解决方案?
最佳答案
为了查看远程站点上的证书信息,您必须有权访问该远程服务器。但是假设您可以访问服务器,您可以编写一些服务器代码来返回指纹值列表以及您可能需要返回的任何其他内容。以下是使用 C# 使用 asp.net 的方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;
namespace FIPWS01
{
public partial class certtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
X509Store store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
// X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindBySubjectName, "Kilpatrick", false);
X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindBySubjectName, "[your info here]", false);
Response.Write("Number of certificates: " + fcollection.Count + "<br>");
foreach (X509Certificate2 x509 in fcollection)
{
byte[] rawdata = x509.RawData;
Response.Write("Friendly Name: " + x509.FriendlyName + "<br>");
Response.Write("Simple Name: " + x509.GetNameInfo(X509NameType.SimpleName, true) + "<br>");
Response.Write("Thumb Print: " + x509.Thumbprint + "<br>");
}
store.Close();
}
catch (CryptographicException)
{
Response.Write("Information could not be written out for this certificate.");
}
}
}
}
关于authentication - 使用 Cordova 进行证书验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836724/