示例#1
0
 public TheDefaultCrypto(ICDESecrets pSecrets = null, ICDESystemLog pSysLog = null)
 {
     if (pSecrets != null)
     {
         MySecrets = pSecrets;
     }
     else
     {
         MySecrets = TheBaseAssets.MySecrets;
     }
     MySYSLOG = pSysLog;
 }
示例#2
0
 public SampleScopeManager(ICDESecrets pSecrets, ICDESystemLog pSysLog = null)
 {
     MySecrets = pSecrets;
     MySYSLOG  = pSysLog;
 }
示例#3
0
 public TheSampleCrypto(ICDESecrets pSecrets, ICDESystemLog pSysLog = null)
 {
     MySecrets = pSecrets;
     MySYSLOG  = pSysLog;
 }
示例#4
0
 public TheDefaultCodeSigning(ICDESecrets pSecrets, ICDESystemLog pSysLog = null)
 {
     MySecrets = pSecrets;
     MySYSLOG  = pSysLog;
 }
示例#5
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Loads a crypto library for all Security related methods. </summary>
        ///
        /// <remarks>   Chris, 3/27/2020. </remarks>
        ///
        /// <param name="pDLLName"> Name of the crypto DLL. </param>
        /// <param name="bDontVerifyTrust"></param>
        /// <param name="pFromFile"></param>
        /// <param name="bVerifyTrustPath"></param>
        /// <param name="bDontVerifyIntegrity"></param>
        /// <param name="pMySYSLOG"></param>
        ///
        /// <returns>   Error string if load failed - then the default security in the CDE is used. Null if succeeded </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        public static string LoadCrypto(string pDLLName, ICDESystemLog pMySYSLOG = null, bool bDontVerifyTrust = false, string pFromFile = null, bool bVerifyTrustPath = true, bool bDontVerifyIntegrity = false)
        {
            if (MasterSwitch)
            {
                return(CryptoLoadMessage = "LoadCrypto is not allowed after the Node has been started");
            }
            try
            {
                var inDLLName = pDLLName;
                if (string.IsNullOrEmpty(pDLLName))
                {
                    pDLLName = "cdeCryptoLib.dll";
                }
                Dictionary <string, string> tL = new Dictionary <string, string>();
                Assembly tCryptoAssembly       = null;
                string   codeSignThumb;
                if (AppDomain.CurrentDomain?.FriendlyName != "RootDomain" && AppDomain.CurrentDomain?.FriendlyName != "MonoTouch") //Android and IOS
                {
                    if (MyCodeSigner == null)
                    {
                        MyCodeSigner = new TheDefaultCodeSigning(MySecrets, pMySYSLOG);
                    }
                    codeSignThumb = MyCodeSigner.GetAppCert(bDontVerifyTrust, pFromFile, bVerifyTrustPath, bDontVerifyIntegrity);
                    if (!bDontVerifyTrust && string.IsNullOrEmpty(codeSignThumb))
                    {
                        return(CryptoLoadMessage = $"No code-signing certificate found but required");
                    }
                    if (!pDLLName.Contains(Path.DirectorySeparatorChar))
                    {
                        pDLLName = Path.Combine(AppDomain.CurrentDomain.RelativeSearchPath != null ? AppDomain.CurrentDomain.RelativeSearchPath : AppDomain.CurrentDomain.BaseDirectory, pDLLName);
                    }
                    AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
                    var pLoader = new CryptoReferenceLoader();

                    tL = pLoader.ScanLibrary(pDLLName, out TSM tTSM, out tCryptoAssembly);
                    if (tTSM != null)
                    {
                        TheSystemMessageLog.ToCo($"Domain:{AppDomain.CurrentDomain?.FriendlyName} DLL:{pFromFile} Cert Found:{codeSignThumb} {tTSM.TXT} {tTSM.PLS}");
                    }
                }
                else
                {
                    var types = AppDomain.CurrentDomain.GetAssemblies();
                    tCryptoAssembly = types.FirstOrDefault(g => g.Location.Contains(pDLLName));
                    if (tCryptoAssembly != null)
                    {
                        var CDEPlugins = from t in tCryptoAssembly.GetTypes()
                                         let ifs = t.GetInterfaces()
                                                   where ifs != null && ifs.Length > 0 && (ifs.Any(s => _KnownInterfaces.Contains(s.Name)))
                                                   select new { Type = t, t.Namespace, t.Name, t.FullName };
                        foreach (var Plugin in CDEPlugins)
                        {
                            if (!(Plugin?.Type?.IsAbstract == true))
                            {
                                var ints = Plugin.Type.GetInterfaces();
                                foreach (var tI in ints)
                                {
                                    if (_KnownInterfaces.Contains(tI.Name))
                                    {
                                        tL[tI.Name] = Plugin.FullName;
                                    }
                                }
                            }
                        }
                    }
                }
                if ((bDontVerifyTrust || MyCodeSigner?.IsTrusted(pDLLName) == true) && tL?.Count > 0)
                {
                    if (tCryptoAssembly == null)
                    {
                        tCryptoAssembly = Assembly.LoadFrom(pDLLName);
                    }
                    if (tL.ContainsKey("ICDESecrets"))
                    {
                        var tSecType = tCryptoAssembly.GetTypes().First(s => s.FullName == tL["ICDESecrets"]);
                        MySecrets = Activator.CreateInstance(tSecType) as ICDESecrets;
                    }
                    else
                    {
                        MySecrets = new TheDefaultSecrets();
                    }
                    foreach (string tInter in _KnownInterfaces)
                    {
                        if (tL?.ContainsKey(tInter) == true)
                        {
                            var tNType = tCryptoAssembly.GetTypes().First(s => s.FullName == tL[tInter]);
                            try
                            {
                                switch (tInter)
                                {
                                case "ICDEScopeManager": MyScopeManager = Activator.CreateInstance(tNType, new object[] { MySecrets, pMySYSLOG }) as ICDEScopeManager; break;

                                case "ICDECodeSigning": MyCodeSigner = Activator.CreateInstance(tNType, new object[] { MySecrets, pMySYSLOG }) as ICDECodeSigning; break;

                                case "ICDEActivation": MyActivationManager = Activator.CreateInstance(tNType, new object[] { MySecrets, pMySYSLOG }) as ICDEActivation; break;

                                case "ICDECrypto": MyCrypto = Activator.CreateInstance(tNType, new object[] { MySecrets, pMySYSLOG }) as ICDECrypto; break;
                                }
                            }
                            catch (Exception e)
                            {
                                pMySYSLOG?.WriteToLog(0, 1, "LoadCrypto", $"Failed to create implementation of {tInter}. Most likely constructor not implemented correctly (must take ICDESecrets as first parameter): {e}", eMsgLevel.l1_Error);
                            }
                        }
                    }
                }
                else
                {
                    return(CryptoLoadMessage = $"The {pDLLName} is not trusted");
                }
            }
            catch (Exception e)
            {
                return(CryptoLoadMessage = $"Errors During Load: {e}");
            }
            return(null);
        }