使用Cordova进行证书验证

时间:2014-05-23 15:12:07

标签: authentication cordova ssl certificate

我想知道在Cordova中是否有一种简单/推荐的方法来验证远程站点证书。我希望我的应用验证$ remote.thumbprint在预期的指纹列表中,没有一个MITM。代码(和列表)应该通过应用程序商店部署在手机上(我只是假设它们是可信的)。

最好是直接的解决方案,不需要针对Android,IOS和WP的平台特定代码吗?

1 个答案:

答案 0 :(得分:0)

要查看远程站点上的证书信息,您必须有权访问该远程服务器。但假设您可以访问服务器,您可以编写一些服务器代码,返回一个thumbrint值列表以及您可能需要返回的其他内容。以下是使用asp.net实现C#的方法:

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.");

                }




        }
    }
}