| 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 |
#if !SILVERLIGHT && !CF |
| 30 |
|
| 31 |
using System; |
| 32 |
using System.Security; |
| 33 |
using SSP = System.Security.Permissions; |
| 34 |
|
| 35 |
namespace Mono.Cecil.Rocks { |
| 36 |
|
| 37 |
#if INSIDE_ROCKS |
| 38 |
public |
| 39 |
#endif |
| 40 |
static class SecurityDeclarationRocks { |
| 41 |
|
| 42 |
public static PermissionSet ToPermissionSet (this SecurityDeclaration self) |
| 43 |
{ |
| 44 |
if (self == null) |
| 45 |
throw new ArgumentNullException ("self"); |
| 46 |
|
| 47 |
PermissionSet set; |
| 48 |
if (TryProcessPermissionSetAttribute (self, out set)) |
| 49 |
return set; |
| 50 |
|
| 51 |
return CreatePermissionSet (self); |
| 52 |
} |
| 53 |
|
| 54 |
static bool TryProcessPermissionSetAttribute (SecurityDeclaration declaration, out PermissionSet set) |
| 55 |
{ |
| 56 |
set = null; |
| 57 |
|
| 58 |
if (!declaration.HasSecurityAttributes && declaration.SecurityAttributes.Count != 1) |
| 59 |
return false; |
| 60 |
|
| 61 |
var security_attribute = declaration.SecurityAttributes [0]; |
| 62 |
if (!security_attribute.AttributeType.IsTypeOf ("System.Security.Permissions", "PermissionSetAttribute")) |
| 63 |
return false; |
| 64 |
|
| 65 |
var attribute = new SSP.PermissionSetAttribute ((SSP.SecurityAction) declaration.Action); |
| 66 |
|
| 67 |
var named_argument = security_attribute.Properties [0]; |
| 68 |
string value = (string) named_argument.Argument.Value; |
| 69 |
switch (named_argument.Name) { |
| 70 |
case "XML": |
| 71 |
attribute.XML = value; |
| 72 |
break; |
| 73 |
case "Name": |
| 74 |
attribute.Name = value; |
| 75 |
break; |
| 76 |
default: |
| 77 |
throw new NotImplementedException (named_argument.Name); |
| 78 |
} |
| 79 |
|
| 80 |
set = attribute.CreatePermissionSet (); |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
static PermissionSet CreatePermissionSet (SecurityDeclaration declaration) |
| 85 |
{ |
| 86 |
var set = new PermissionSet (SSP.PermissionState.None); |
| 87 |
|
| 88 |
foreach (var attribute in declaration.SecurityAttributes) { |
| 89 |
var permission = CreatePermission (declaration, attribute); |
| 90 |
set.AddPermission (permission); |
| 91 |
} |
| 92 |
|
| 93 |
return set; |
| 94 |
} |
| 95 |
|
| 96 |
static IPermission CreatePermission (SecurityDeclaration declaration, SecurityAttribute attribute) |
| 97 |
{ |
| 98 |
var attribute_type = Type.GetType (attribute.AttributeType.FullName); |
| 99 |
if (attribute_type == null) |
| 100 |
throw new ArgumentException ("attribute"); |
| 101 |
|
| 102 |
var security_attribute = CreateSecurityAttribute (attribute_type, declaration); |
| 103 |
if (security_attribute == null) |
| 104 |
throw new InvalidOperationException (); |
| 105 |
|
| 106 |
CompleteSecurityAttribute (security_attribute, attribute); |
| 107 |
|
| 108 |
return security_attribute.CreatePermission (); |
| 109 |
} |
| 110 |
|
| 111 |
static void CompleteSecurityAttribute (SSP.SecurityAttribute security_attribute, SecurityAttribute attribute) |
| 112 |
{ |
| 113 |
if (attribute.HasFields) |
| 114 |
CompleteSecurityAttributeFields (security_attribute, attribute); |
| 115 |
|
| 116 |
if (attribute.HasProperties) |
| 117 |
CompleteSecurityAttributeProperties (security_attribute, attribute); |
| 118 |
} |
| 119 |
|
| 120 |
static void CompleteSecurityAttributeFields (SSP.SecurityAttribute security_attribute, SecurityAttribute attribute) |
| 121 |
{ |
| 122 |
var type = security_attribute.GetType (); |
| 123 |
|
| 124 |
foreach (var named_argument in attribute.Fields) |
| 125 |
type.GetField (named_argument.Name).SetValue (security_attribute, named_argument.Argument.Value); |
| 126 |
} |
| 127 |
|
| 128 |
static void CompleteSecurityAttributeProperties (SSP.SecurityAttribute security_attribute, SecurityAttribute attribute) |
| 129 |
{ |
| 130 |
var type = security_attribute.GetType (); |
| 131 |
|
| 132 |
foreach (var named_argument in attribute.Properties) |
| 133 |
type.GetProperty (named_argument.Name).SetValue (security_attribute, named_argument.Argument.Value, null); |
| 134 |
} |
| 135 |
|
| 136 |
static SSP.SecurityAttribute CreateSecurityAttribute (Type attribute_type, SecurityDeclaration declaration) |
| 137 |
{ |
| 138 |
SSP.SecurityAttribute security_attribute; |
| 139 |
try { |
| 140 |
security_attribute = (SSP.SecurityAttribute) Activator.CreateInstance ( |
| 141 |
attribute_type, new object [] { (SSP.SecurityAction) declaration.Action }); |
| 142 |
} catch (MissingMethodException) { |
| 143 |
security_attribute = (SSP.SecurityAttribute) Activator.CreateInstance (attribute_type, new object [0]); |
| 144 |
} |
| 145 |
|
| 146 |
return security_attribute; |
| 147 |
} |
| 148 |
|
| 149 |
public static SecurityDeclaration ToSecurityDeclaration (this PermissionSet self, SecurityAction action, ModuleDefinition module) |
| 150 |
{ |
| 151 |
if (self == null) |
| 152 |
throw new ArgumentNullException ("self"); |
| 153 |
if (module == null) |
| 154 |
throw new ArgumentNullException ("module"); |
| 155 |
|
| 156 |
var declaration = new SecurityDeclaration (action); |
| 157 |
|
| 158 |
var attribute = new SecurityAttribute ( |
| 159 |
module.TypeSystem.LookupType ("System.Security.Permissions", "PermissionSetAttribute")); |
| 160 |
|
| 161 |
attribute.Properties.Add ( |
| 162 |
new CustomAttributeNamedArgument ( |
| 163 |
"XML", |
| 164 |
new CustomAttributeArgument ( |
| 165 |
module.TypeSystem.String, self.ToXml ().ToString ()))); |
| 166 |
|
| 167 |
declaration.SecurityAttributes.Add (attribute); |
| 168 |
|
| 169 |
return declaration; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
#endif |