2004-07-14 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Permissions / StrongNamePermissionAttribute.cs
1 //
2 // System.Security.Permissions.StrongNameIdentityPermissionAttribute.cs
3 //
4 // Duncan Mak <duncan@ximian.com>
5 //
6 // (C) 2002 Ximian, Inc.                        http://www.ximian.com
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33
34 namespace System.Security.Permissions {
35
36         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
37                          AttributeTargets.Struct | AttributeTargets.Constructor |
38                          AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
39         [Serializable]
40         public sealed class StrongNameIdentityPermissionAttribute : CodeAccessSecurityAttribute {
41
42                 // Fields
43                 private string name;
44                 private string key;
45                 private string version;
46                 
47                 // Constructor
48                 public StrongNameIdentityPermissionAttribute (SecurityAction action) : base (action) {}
49                 
50                 // Properties
51                 public string Name
52                 {
53                         get { return name; }
54                         set { name = value; }
55                 }
56
57                 public string PublicKey
58                 {
59                         get { return key; }
60                         set { key = value; }
61                 }
62
63                 public string Version
64                 {
65                         get { return version; }
66                         set { version = value; }
67                 }
68                          
69                 // Methods
70                 public override IPermission CreatePermission ()
71                 {
72                         if (this.Unrestricted)
73                                 throw new ArgumentException ("Unsupported PermissionState.Unrestricted");
74
75                         StrongNameIdentityPermission perm = null;
76                         if ((name == null) && (key == null) && (version == null))
77                                 perm = new StrongNameIdentityPermission (PermissionState.None);
78                         else {
79                                 if (key == null)
80                                         throw new ArgumentException ("PublicKey is required");
81
82                                 byte[] keyblob = Convert.FromBase64String (key);
83                                 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (keyblob);
84                                 
85                                 Version v = null;
86                                 if (version != null)
87                                         v = new Version (version);
88                                 else
89                                         v = new Version ();
90
91                                 if (name == null)
92                                         name = String.Empty;
93
94                                 perm = new StrongNameIdentityPermission (blob, name, v);
95                         }
96                         return perm;
97                 }
98         }
99 }