SecurityDeclaration.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // SecurityDeclaration.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Threading;
  30. using Mono.Collections.Generic;
  31. namespace Mono.Cecil
  32. {
  33. public enum SecurityAction : ushort
  34. {
  35. Request = 1,
  36. Demand = 2,
  37. Assert = 3,
  38. Deny = 4,
  39. PermitOnly = 5,
  40. LinkDemand = 6,
  41. InheritDemand = 7,
  42. RequestMinimum = 8,
  43. RequestOptional = 9,
  44. RequestRefuse = 10,
  45. PreJitGrant = 11,
  46. PreJitDeny = 12,
  47. NonCasDemand = 13,
  48. NonCasLinkDemand = 14,
  49. NonCasInheritance = 15
  50. }
  51. public interface ISecurityDeclarationProvider : IMetadataTokenProvider
  52. {
  53. bool HasSecurityDeclarations { get; }
  54. Collection<SecurityDeclaration> SecurityDeclarations { get; }
  55. }
  56. public sealed class SecurityAttribute : ICustomAttribute
  57. {
  58. TypeReference attribute_type;
  59. internal Collection<CustomAttributeNamedArgument> fields;
  60. internal Collection<CustomAttributeNamedArgument> properties;
  61. public TypeReference AttributeType
  62. {
  63. get { return attribute_type; }
  64. set { attribute_type = value; }
  65. }
  66. public bool HasFields
  67. {
  68. get { return !Mixin.IsNullOrEmpty(fields); }
  69. }
  70. public Collection<CustomAttributeNamedArgument> Fields
  71. {
  72. get { return fields ?? (fields = new Collection<CustomAttributeNamedArgument>()); }
  73. }
  74. public bool HasProperties
  75. {
  76. get { return !Mixin.IsNullOrEmpty(properties); }
  77. }
  78. public Collection<CustomAttributeNamedArgument> Properties
  79. {
  80. get { return properties ?? (properties = new Collection<CustomAttributeNamedArgument>()); }
  81. }
  82. public SecurityAttribute(TypeReference attributeType)
  83. {
  84. this.attribute_type = attributeType;
  85. }
  86. }
  87. public sealed class SecurityDeclaration
  88. {
  89. readonly internal uint signature;
  90. byte[] blob;
  91. readonly ModuleDefinition module;
  92. internal bool resolved;
  93. SecurityAction action;
  94. internal Collection<SecurityAttribute> security_attributes;
  95. public SecurityAction Action
  96. {
  97. get { return action; }
  98. set { action = value; }
  99. }
  100. public bool HasSecurityAttributes
  101. {
  102. get
  103. {
  104. Resolve();
  105. return !Mixin.IsNullOrEmpty(security_attributes);
  106. }
  107. }
  108. public Collection<SecurityAttribute> SecurityAttributes
  109. {
  110. get
  111. {
  112. Resolve();
  113. return security_attributes ?? (security_attributes = new Collection<SecurityAttribute>());
  114. }
  115. }
  116. internal bool HasImage
  117. {
  118. get { return module != null && module.HasImage; }
  119. }
  120. internal SecurityDeclaration(SecurityAction action, uint signature, ModuleDefinition module)
  121. {
  122. this.action = action;
  123. this.signature = signature;
  124. this.module = module;
  125. }
  126. public SecurityDeclaration(SecurityAction action)
  127. {
  128. this.action = action;
  129. this.resolved = true;
  130. }
  131. public SecurityDeclaration(SecurityAction action, byte[] blob)
  132. {
  133. this.action = action;
  134. this.resolved = false;
  135. this.blob = blob;
  136. }
  137. public byte[] GetBlob()
  138. {
  139. if (blob != null)
  140. return blob;
  141. if (!HasImage || signature == 0)
  142. throw new NotSupportedException();
  143. return blob = module.Read(this, (declaration, reader) => reader.ReadSecurityDeclarationBlob(declaration.signature));
  144. }
  145. void Resolve()
  146. {
  147. if (resolved || !HasImage)
  148. return;
  149. module.Read(this, (declaration, reader) =>
  150. {
  151. reader.ReadSecurityDeclarationSignature(declaration);
  152. return this;
  153. });
  154. resolved = true;
  155. }
  156. }
  157. static partial class Mixin
  158. {
  159. public static bool GetHasSecurityDeclarations(
  160. ISecurityDeclarationProvider self,
  161. ModuleDefinition module)
  162. {
  163. return Mixin.HasImage(module) && module.Read(self, (provider, reader) => reader.HasSecurityDeclarations(provider));
  164. }
  165. public static Collection<SecurityDeclaration> GetSecurityDeclarations(
  166. ISecurityDeclarationProvider self,
  167. ref Collection<SecurityDeclaration> variable,
  168. ModuleDefinition module)
  169. {
  170. return Mixin.HasImage(module)
  171. ? module.Read(ref variable, self, (provider, reader) => reader.ReadSecurityDeclarations(provider))
  172. : variable = new Collection<SecurityDeclaration>();
  173. }
  174. }
  175. }