File: ProtectedConfigurationSection.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (169 lines) | stat: -rw-r--r-- 6,897 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//------------------------------------------------------------------------------
// <copyright file="ProtectedConfigurationSection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Configuration
{
    using System.Collections;
    using System.Collections.Specialized;
    using System.Xml;
    using System.Globalization;
    using System.Security.Permissions;
    using System.Diagnostics.CodeAnalysis;

    public sealed class ProtectedConfigurationSection : ConfigurationSection
    {
        internal ProtectedConfigurationProvider GetProviderFromName(string providerName)
        {
            ProviderSettings ps = Providers[providerName];

            if (ps == null) {
                throw new Exception(SR.GetString(SR.ProtectedConfigurationProvider_not_found, providerName));
            }

            return InstantiateProvider(ps);
        }

        internal ProtectedConfigurationProviderCollection GetAllProviders()
        {
            ProtectedConfigurationProviderCollection coll = new ProtectedConfigurationProviderCollection();
            foreach(ProviderSettings ps in Providers)
            {
                coll.Add(InstantiateProvider(ps));
            }
            return coll;
        }

        [PermissionSet(SecurityAction.Assert, Unrestricted=true)]
        [SuppressMessage("Microsoft.Security", "CA2106:SecureAsserts", Justification = "This assert is potentially dangerous and shouldn't be present but is necessary for back-compat.")]
        private ProtectedConfigurationProvider CreateAndInitializeProviderWithAssert(Type t, ProviderSettings pn) {
            ProtectedConfigurationProvider provider = (ProtectedConfigurationProvider)TypeUtil.CreateInstanceWithReflectionPermission(t);
            NameValueCollection pars = pn.Parameters;
            NameValueCollection cloneParams = new NameValueCollection(pars.Count);

            foreach (string key in pars) {
                cloneParams[key] = pars[key];
            }

            provider.Initialize(pn.Name, cloneParams);
            return provider;
        }

        private ProtectedConfigurationProvider InstantiateProvider(ProviderSettings pn)
        {
            Type t = TypeUtil.GetTypeWithReflectionPermission(pn.Type, true);
            if (!typeof(ProtectedConfigurationProvider).IsAssignableFrom(t)) {
                throw new Exception(SR.GetString(SR.WrongType_of_Protected_provider));
            }

            // Needs to check APTCA bit.  See VSWhidbey 429996.
            if (!TypeUtil.IsTypeAllowedInConfig(t)) {
                throw new Exception(SR.GetString(SR.Type_from_untrusted_assembly, t.FullName));
            }

            // Needs to check Assert Fulltrust in order for runtime to work.  See VSWhidbey 429996.
            return CreateAndInitializeProviderWithAssert(t, pn);
        }

        internal static string DecryptSection(string encryptedXml, ProtectedConfigurationProvider provider) {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(encryptedXml);
            XmlNode resultNode = provider.Decrypt(doc.DocumentElement);
            return resultNode.OuterXml;
        }

        private const string    EncryptedSectionTemplate = "<{0} {1}=\"{2}\"> {3} </{0}>";

        internal static string FormatEncryptedSection(string encryptedXml, string sectionName, string providerName) {
            return String.Format(CultureInfo.InvariantCulture, EncryptedSectionTemplate,
                        sectionName,    // The section to encrypt
                        BaseConfigurationRecord.KEYWORD_PROTECTION_PROVIDER, // protectionProvider keyword
                        providerName,  // The provider name
                        encryptedXml   // the encrypted xml
                        );
        }
        
        internal static string EncryptSection(string clearXml, ProtectedConfigurationProvider provider) {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.PreserveWhitespace = true;
            xmlDocument.LoadXml(clearXml);
            string sectionName = xmlDocument.DocumentElement.Name;
            XmlNode encNode = provider.Encrypt(xmlDocument.DocumentElement);
            return encNode.OuterXml;
        }


        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////

        private static ConfigurationPropertyCollection _properties;
        private static readonly ConfigurationProperty _propProviders =
            new ConfigurationProperty("providers", 
                                        typeof(ProtectedProviderSettings), 
                                        new ProtectedProviderSettings(), 
                                        ConfigurationPropertyOptions.None);
        
        private static readonly ConfigurationProperty _propDefaultProvider =
            new ConfigurationProperty("defaultProvider", 
                                        typeof(string), 
                                        "RsaProtectedConfigurationProvider", 
                                        null,
                                        ConfigurationProperty.NonEmptyStringValidator,
                                        ConfigurationPropertyOptions.None);

        static ProtectedConfigurationSection()
        {
            // Property initialization
            _properties = new ConfigurationPropertyCollection();
            _properties.Add(_propProviders);
            _properties.Add(_propDefaultProvider);
        }

        public ProtectedConfigurationSection()
        {
        }

        protected internal override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        private ProtectedProviderSettings _Providers
        {
            get
            {
                return (ProtectedProviderSettings)base[_propProviders];
            }
        }

        [ConfigurationProperty("providers")]
        public ProviderSettingsCollection Providers
        {
            get
            {
                return _Providers.Providers;
            }
        }

        [ConfigurationProperty("defaultProvider", DefaultValue = "RsaProtectedConfigurationProvider")]
        public string DefaultProvider
        {
            get
            {
                return (string)base[_propDefaultProvider];
            }
            set
            {
                base[_propDefaultProvider] = value;
            }
        }

    }
}