简体   繁体   中英

cordova plugin x509store

I have to develop an app with cordova targeting mainly Windows platform. In this app, I have to manipulate the certification store. Long story short answer, I have to do a cordova plugin (or maybe an activeX trick). I made a windows runtime component in C#, as explained here , to use X509Store (as I need Windows). I used visual studio 2015 to make a windows runtime component project (I tried universal windows and 8.1). It works, I can call C# methods in my .js.

But the problem is: a windows runtime component project doesn't have the namespace System.Security.Cryptography.X509Certificates. So I can't access to X509Store.

As a workaround, I made a Class library (.NetCore, .dll) which call X509Store and return strings, at least to show the certificate (json stringify). A classic class library can also access to x509store but it makes target error when I reference it in this windows runtime component project. (a portable/universal dll project doesn't have X509Store neither). My .netcore dll works, I tried it in a Console Application (.NetCore) project and it showed all my certificates. But when I call it from the cordova app (cordova app -> plugin -> runtime -> .netcore dll) it's empty (no certificate found, and the current user is undefined). I think it's because the execution context is not the same (webapp vs console app). And i don't think it's a good solution (which doesn't even work).

So, how can I access to the certification store (of the windows user) in a windows runtime component ? As I don't think it's possible with javascript.

Thanks

PS: I can provide some source code if needed

EDIT:
I forgot that there is an assembly conflict in the runtime project with the .netcore dll which I solved by referencing the right dll in plugin.xml file (System.Runtime.dll etc ..) as I didn't manage to solve it in visual studio

//script inside cordova, called after device ready
signatureplugin.getAllCertNames(function(a) { }, function(a) { }, 0);

//signatureplugin.js
var exec = require('cordova/exec');

module.exports = {  
    getAllCertNames: function(successCallback, errorCallback, args) {
        exec(successCallback, errorCallback, "SignaturePlugin", "getAllCertNames", [args]);
    }
};

//signaturepluginProxy.js
module.exports = {  
    getAllCertNames: function(successCallback, errorCallback, args) {
        var res = SignerVerifier.PluginRT.getAllCertNames();
        for (var i = 0, len = res.length; i < len; i++) {
            alert(res[i]);
        }
    }
};

require("cordova/exec/proxy").add("SignaturePlugin", module.exports);


//windows runtime component
using CertStore;

namespace SignerVerifier
{
    public sealed class PluginRT
    {
        public static string[] getAllCertNames()
        {
            var certStore = new StoreManager();
            var res = certStore.getAllCertificates();
            return res;
        }

    }
}

//.NetCore dll
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using System;

namespace CertStore
{
    public class StoreManager
    {
        private X509Store store;

        public StoreManager()
        {
            store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        }

        public string[] getAllCertificates()
        {
            List<string> res = new List<string>();

            store.Open(OpenFlags.ReadOnly);
            var certificates = store.Certificates;
            foreach (var cert in certificates)
            {
                res.Add(JsonConvert.SerializeObject(cert));
            }
            store.Dispose();

            return res.ToArray();
        }
    }
}

If i do a javascript blank app project + Windows Runtime Component project (projects from "universal", I don't have "Windows Store" and I use windows 10) then add the .netcore dll i got the conflict which lead to an exception:

System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

(in cordova I prevent this exception by referencing System.Runtime.dll, etc ..., from the nuget package NETStandard.Library.1.6.0 used in .netcore) I must miss something. .NetCore dll doesn't seem compatible, but the windows runtime project target .NetCore

EDIT 2: (solved)
vcsjones's answer = workaround useless (and no problem from the previous edit). BUT in anycase there is a security issue, and I have to check "Shared User Certificates" in Capabilities in the appxmanifest

WinRT does certificate management different from the Desktop and Core framework. For WinRT, you would use the Windows.Security namespace.

You can open and manage the certificate store using the Windows.Security.Cryptography.Certificates.CertificateStore class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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