示例#1
0
    private static IXmlDecryptor CreateDecryptor(IActivator activator, string decryptorTypeName)
    {
        var resolvedTypeName = TypeForwardingActivator.TryForwardTypeName(decryptorTypeName, out var forwardedTypeName)
            ? forwardedTypeName
            : decryptorTypeName;
        var type = Type.GetType(resolvedTypeName, throwOnError: false);

        if (type == typeof(DpapiNGXmlDecryptor))
        {
            return(activator.CreateInstance <DpapiNGXmlDecryptor>(decryptorTypeName));
        }
        else if (type == typeof(DpapiXmlDecryptor))
        {
            return(activator.CreateInstance <DpapiXmlDecryptor>(decryptorTypeName));
        }
        else if (type == typeof(EncryptedXmlDecryptor))
        {
            return(activator.CreateInstance <EncryptedXmlDecryptor>(decryptorTypeName));
        }
        else if (type == typeof(NullXmlDecryptor))
        {
            return(activator.CreateInstance <NullXmlDecryptor>(decryptorTypeName));
        }

        return(activator.CreateInstance <IXmlDecryptor>(decryptorTypeName));
    }
示例#2
0
        /// <summary>
        /// Instantiates the <paramref name="targetType"/> with the given <paramref name="activator"/> and <paramref name="constructorArguments"/>.
        /// </summary>
        /// <param name="activator">The <see cref="IActivator{T}"/> instance that will be responsible for creating the <paramref name="targetType"/>.</param>
        /// <param name="targetType">The type to be created.</param>
        /// <param name="constructorArguments">The arguments that will be passed to the constructor.</param>
        /// <returns>An object reference that matches the given <paramref name="targetType"/>.</returns>
        public static object CreateInstance(this IActivator <IActivationContext> activator, Type targetType,
                                            object[] constructorArguments)
        {
            var context = new ActivationContext(targetType, constructorArguments);

            return(activator.CreateInstance(context));
        }
        private object CreateFromConstructor(Type type, ConstructorInfo constructorInfo)
        {
            var    parameters          = constructorInfo.GetParameters();
            var    parametersInstances = parameters.Select(p => ConstructInstanceOfType(p.ParameterType));
            object instance            = _activator.CreateInstance(type, parametersInstances.ToArray());

            return(instance);
        }
        public IStatement Create(Statement statement, Type component, Action <IDromedaryComponent> configureComponent)
        {
            if (configureComponent == null)
            {
                throw new ArgumentNullException(nameof(configureComponent));
            }

            if (component.IsInstanceOfType(typeof(IDromedaryComponent)))
            {
                throw new DromedaryIsNotDromedaryComponent(component);
            }

            var dromedaryComponent = (IDromedaryComponent)_activator.CreateInstance(component);

            configureComponent(dromedaryComponent);
            return(new DefaultStatement(dromedaryComponent.CreateEndpoint(), statement));
        }
        private object CreateInstanceFromActivator(Type type, IList <object> args)
        {
            var suitableConstructor = constructorCacher.GetConstructor(type);
            var resolvedArgs        = ResolveArgs(suitableConstructor, args);
            var instance            = activator.CreateInstance(suitableConstructor, resolvedArgs);

            InjectIntoInstance(instance);

            return(instance);
        }
示例#6
0
    /// <summary>
    /// Creates an instance of <paramref name="implementationTypeName"/> and ensures
    /// that it is assignable to <typeparamref name="T"/>.
    /// </summary>
    public static T CreateInstance <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(this IActivator activator, string implementationTypeName)
        where T : class
    {
        if (implementationTypeName == null)
        {
            throw new ArgumentNullException(nameof(implementationTypeName));
        }

        return(activator.CreateInstance(typeof(T), implementationTypeName) as T
               ?? CryptoUtil.Fail <T>("CreateInstance returned null."));
    }
        private object CreateFromConstructor(Type type, ConstructorInfo constructorInfo)
        {
            ParameterInfo[] parameters          = constructorInfo.GetParameters();
            List <object>   parametersInstances = new List <object>(parameters.Length);

            Array.ForEach(parameters, p => parametersInstances.Add(ConstructInstanceOfType(p.ParameterType)));

            object instance = _activator.CreateInstance(type, parametersInstances.ToArray());

            return(instance);
        }
        /// <summary>
        /// Creates an instance of <paramref name="implementationTypeName"/> and ensures
        /// that it is assignable to <typeparamref name="T"/>.
        /// </summary>
        public static T CreateInstance <T>(this IActivator activator, string implementationTypeName)
            where T : class
        {
            if (implementationTypeName == null)
            {
                throw new ArgumentNullException(nameof(implementationTypeName));
            }

            return(activator.CreateInstance(typeof(T), implementationTypeName) as T
                   ?? CryptoUtil.Fail <T>("CreateInstance returned null."));
        }
示例#9
0
        public ITemplate CreateInstance(InstanceContext context)
        {
            var template = defaultActivator.CreateInstance(context);

            var extTemplate = template as IExtensibleTemplate;

            if (extTemplate != null)
            {
                extTemplate.Filter = new FilterProxy(filters);
            }

            return(template);
        }
        public static XElement DecryptElement(this XElement element, IActivator activator)
        {
            // If no decryption necessary, return original element.
            if (!DoesElementOrDescendentRequireDecryption(element))
            {
                return element;
            }

            // Deep copy the element (since we're going to mutate) and put
            // it into a document to guarantee it has a parent.
            var doc = new XDocument(new XElement(element));

            // We remove elements from the document as we decrypt them and perform
            // fix-up later. This keeps us from going into an infinite loop in
            // the case of a null decryptor (which returns its original input which
            // is still marked as 'requires decryption').
            var placeholderReplacements = new Dictionary<XElement, XElement>();

            while (true)
            {
                var elementWhichRequiresDecryption = doc.Descendants(XmlConstants.EncryptedSecretElementName).FirstOrDefault();
                if (elementWhichRequiresDecryption == null)
                {
                    // All encryption is finished.
                    break;
                }

                // Decrypt the clone so that the decryptor doesn't inadvertently modify
                // the original document or other data structures. The element we pass to
                // the decryptor should be the child of the 'encryptedSecret' element.
                var clonedElementWhichRequiresDecryption = new XElement(elementWhichRequiresDecryption);
                var innerDoc = new XDocument(clonedElementWhichRequiresDecryption);
                string decryptorTypeName = (string)clonedElementWhichRequiresDecryption.Attribute(XmlConstants.DecryptorTypeAttributeName);
                var decryptorInstance = activator.CreateInstance<IXmlDecryptor>(decryptorTypeName);
                var decryptedElement = decryptorInstance.Decrypt(clonedElementWhichRequiresDecryption.Elements().Single());

                // Put a placeholder into the original document so that we can continue our
                // search for elements which need to be decrypted.
                var newPlaceholder = new XElement("placeholder");
                placeholderReplacements[newPlaceholder] = decryptedElement;
                elementWhichRequiresDecryption.ReplaceWith(newPlaceholder);
            }

            // Finally, perform fixup.
            Debug.Assert(placeholderReplacements.Count > 0);
            foreach (var entry in placeholderReplacements)
            {
                entry.Key.ReplaceWith(entry.Value);
            }
            return doc.Root;
        }
示例#11
0
        public static XElement DecryptElement(this XElement element, IActivator activator)
        {
            // If no decryption necessary, return original element.
            if (!DoesElementOrDescendentRequireDecryption(element))
            {
                return(element);
            }

            // Deep copy the element (since we're going to mutate) and put
            // it into a document to guarantee it has a parent.
            var doc = new XDocument(new XElement(element));

            // We remove elements from the document as we decrypt them and perform
            // fix-up later. This keeps us from going into an infinite loop in
            // the case of a null decryptor (which returns its original input which
            // is still marked as 'requires decryption').
            var placeholderReplacements = new Dictionary <XElement, XElement>();

            while (true)
            {
                var elementWhichRequiresDecryption = doc.Descendants(XmlConstants.EncryptedSecretElementName).FirstOrDefault();
                if (elementWhichRequiresDecryption == null)
                {
                    // All encryption is finished.
                    break;
                }

                // Decrypt the clone so that the decryptor doesn't inadvertently modify
                // the original document or other data structures. The element we pass to
                // the decryptor should be the child of the 'encryptedSecret' element.
                var    clonedElementWhichRequiresDecryption = new XElement(elementWhichRequiresDecryption);
                var    innerDoc          = new XDocument(clonedElementWhichRequiresDecryption);
                string decryptorTypeName = (string)clonedElementWhichRequiresDecryption.Attribute(XmlConstants.DecryptorTypeAttributeName);
                var    decryptorInstance = activator.CreateInstance <IXmlDecryptor>(decryptorTypeName);
                var    decryptedElement  = decryptorInstance.Decrypt(clonedElementWhichRequiresDecryption.Elements().Single());

                // Put a placeholder into the original document so that we can continue our
                // search for elements which need to be decrypted.
                var newPlaceholder = new XElement("placeholder");
                placeholderReplacements[newPlaceholder] = decryptedElement;
                elementWhichRequiresDecryption.ReplaceWith(newPlaceholder);
            }

            // Finally, perform fixup.
            Debug.Assert(placeholderReplacements.Count > 0);
            foreach (var entry in placeholderReplacements)
            {
                entry.Key.ReplaceWith(entry.Value);
            }
            return(doc.Root);
        }
示例#12
0
        private InstanceWrapper CreateInstance(CycleCounter cycleCounter, Type type, object[] args)
        {
            cycleCounter.Indent();

            var suitableConstructor = GetConstructor(type);
            var resolvedArgs        = ResolveArgs(cycleCounter, suitableConstructor, args);
            var instance            = activator.CreateInstance(suitableConstructor, resolvedArgs);

            cycleCounter.Unindent();

            InjectIntoInstance(cycleCounter, instance);

            return(new InstanceWrapper(instance));
        }
示例#13
0
    private RegistryPolicy?ResolvePolicyCore(RegistryKey?policyRegKey)
    {
        if (policyRegKey == null)
        {
            return(null);
        }

        // Read the encryption options type: CNG-CBC, CNG-GCM, Managed
        AlgorithmConfiguration?configuration = null;

        var encryptionType = (string?)policyRegKey.GetValue("EncryptionType");

        if (string.Equals(encryptionType, "CNG-CBC", StringComparison.OrdinalIgnoreCase))
        {
            configuration = GetCngCbcAuthenticatedEncryptorConfiguration(policyRegKey);
        }
        else if (string.Equals(encryptionType, "CNG-GCM", StringComparison.OrdinalIgnoreCase))
        {
            configuration = GetCngGcmAuthenticatedEncryptorConfiguration(policyRegKey);
        }
        else if (string.Equals(encryptionType, "Managed", StringComparison.OrdinalIgnoreCase))
        {
            configuration = GetManagedAuthenticatedEncryptorConfiguration(policyRegKey);
        }
        else if (!string.IsNullOrEmpty(encryptionType))
        {
            throw CryptoUtil.Fail("Unrecognized EncryptionType: " + encryptionType);
        }

        // Read ancillary data

        var defaultKeyLifetime = (int?)policyRegKey.GetValue("DefaultKeyLifetime");
        var escrowSinks        = ReadKeyEscrowSinks(policyRegKey);
        var keyEscrowSinks     = escrowSinks.Count is 0 ?
                                 Array.Empty <IKeyEscrowSink>() :
                                 new IKeyEscrowSink[escrowSinks.Count];

        for (var i = 0; i < keyEscrowSinks.Length; i++)
        {
            keyEscrowSinks[i] = _activator.CreateInstance <IKeyEscrowSink>(escrowSinks[i]);
        }

        return(new RegistryPolicy(configuration, keyEscrowSinks, defaultKeyLifetime));
    }
示例#14
0
        /// <summary>
        /// Creates an instance of <see cref="ITemplate"/> from the specified template.
        /// </summary>
        /// <param name="template">The template to compile.</param>
        /// <param name="modelType">The model type.</param>
        /// <returns>An instance of <see cref="ITemplate"/>.</returns>
        internal ITemplate CreateTemplate(string template, Type modelType)
        {
            var context = new TypeContext
            {
                TemplateType    = templateType,
                TemplateContent = template,
                ModelType       = modelType
            };

            foreach (string @namespace in Namespaces)
            {
                context.Namespaces.Add(@namespace);
            }

            Type instanceType = compilerService.CompileType(context);
            var  instance     = activator.CreateInstance(instanceType);

            return(instance);
        }
示例#15
0
        private RegistryPolicy?ResolvePolicyCore(RegistryKey?policyRegKey)
        {
            if (policyRegKey == null)
            {
                return(null);
            }

            // Read the encryption options type: CNG-CBC, CNG-GCM, Managed
            AlgorithmConfiguration?configuration = null;

            var encryptionType = (string?)policyRegKey.GetValue("EncryptionType");

            if (String.Equals(encryptionType, "CNG-CBC", StringComparison.OrdinalIgnoreCase))
            {
                configuration = new CngCbcAuthenticatedEncryptorConfiguration();
            }
            else if (String.Equals(encryptionType, "CNG-GCM", StringComparison.OrdinalIgnoreCase))
            {
                configuration = new CngGcmAuthenticatedEncryptorConfiguration();
            }
            else if (String.Equals(encryptionType, "Managed", StringComparison.OrdinalIgnoreCase))
            {
                configuration = new ManagedAuthenticatedEncryptorConfiguration();
            }
            else if (!String.IsNullOrEmpty(encryptionType))
            {
                throw CryptoUtil.Fail("Unrecognized EncryptionType: " + encryptionType);
            }
            if (configuration != null)
            {
                PopulateOptions(configuration, policyRegKey);
            }

            // Read ancillary data

            var defaultKeyLifetime = (int?)policyRegKey.GetValue("DefaultKeyLifetime");

            var keyEscrowSinks = ReadKeyEscrowSinks(policyRegKey).Select(item => _activator.CreateInstance <IKeyEscrowSink>(item));

            return(new RegistryPolicy(configuration, keyEscrowSinks, defaultKeyLifetime));
        }
        IAuthenticatedEncryptorDescriptor IInternalXmlKeyManager.DeserializeDescriptorFromKeyElement(XElement keyElement)
        {
            try
            {
                // Figure out who will be deserializing this
                XElement descriptorElement = keyElement.Element(DescriptorElementName);
                string   descriptorDeserializerTypeName = (string)descriptorElement.Attribute(DeserializerTypeAttributeName);

                // Decrypt the descriptor element and pass it to the descriptor for consumption
                XElement unencryptedInputToDeserializer = descriptorElement.Elements().Single().DecryptElement(_activator);
                var      deserializerInstance           = _activator.CreateInstance <IAuthenticatedEncryptorDescriptorDeserializer>(descriptorDeserializerTypeName);
                var      descriptorInstance             = deserializerInstance.ImportFromXml(unencryptedInputToDeserializer);

                return(descriptorInstance ?? CryptoUtil.Fail <IAuthenticatedEncryptorDescriptor>("ImportFromXml returned null."));
            }
            catch (Exception ex)
            {
                WriteKeyDeserializationErrorToLog(ex, keyElement);
                throw;
            }
        }
示例#17
0
        public static object CreateCommandPipeline(Type commandType, IActivator activator)
        {
            Type commandPipelineType = GetCommandPipelineType(commandType);

            return(activator.CreateInstance(commandPipelineType));
        }
        object IActivator.CreateInstance(Type type)
        {
            var constructor = constructorCacher.GetConstructor(type);

            return(activator.CreateInstance(constructor, new object[0]));
        }
示例#19
0
 /// <summary>
 /// Instantiates the <paramref name="targetType"/> with the given <paramref name="activator"/> and <paramref name="constructorArguments"/>.
 /// </summary>
 /// <param name="activator">The <see cref="IActivator{T}"/> instance that will be responsible for creating the target type.</param>
 /// <param name="constructorArguments">The arguments that will be passed to the constructor.</param>
 /// <typeparam name="T">The target type that will be instantiated by the activator.</typeparam>
 /// <returns>An object reference that matches the given <paramref name="targetType"/>.</returns>
 public static T CreateInstance <T>(this IActivator <IActivationContext> activator, object[] constructorArguments)
 {
     return((T)activator.CreateInstance(typeof(T), constructorArguments));
 }
 public override IMigration CreateMigration(IActivator <IMigration> activator)
 {
     return(activator.CreateInstance(this.clrType));
 }
示例#21
0
        private IInstaller ResolveInstaller(Type installerType)
        {
            IActivator activator = Resolve <IActivator>();

            return((IInstaller)activator.CreateInstance(installerType));
        }