2010-05-05 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpClientCertificate.cs
1 //
2 // System.Web.HttpClientCertificate class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
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
29 using System.Collections.Specialized;
30 using System.Globalization;
31 using System.Security.Permissions;
32 using System.Web.Util;
33
34 namespace System.Web
35 {
36         // CAS
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         public class HttpClientCertificate : NameValueCollection
40         {
41                 HttpWorkerRequest hwr;
42                 int flags;
43                 DateTime from;
44                 DateTime until;
45
46                 internal HttpClientCertificate (HttpWorkerRequest hwr)
47                 {
48                         // we don't check hwr for null so we end up throwing a 
49                         // NullReferenceException just like MS implementation
50                         // if the public ctor for HttpRequest is used
51                         this.hwr = hwr;
52                         flags = GetIntNoPresense ("CERT_FLAGS");
53                         if (IsPresent) {
54                                 from = hwr.GetClientCertificateValidFrom ();
55                                 until = hwr.GetClientCertificateValidUntil ();
56                         } else {
57                                 from = DateTime.Now;
58                                 until = from;
59                         }
60                 }
61
62                 public byte[] BinaryIssuer {
63                         get { return hwr.GetClientCertificateBinaryIssuer (); }
64                 }
65
66                 public int CertEncoding {
67                         get { return hwr.GetClientCertificateEncoding (); }
68                 }
69
70                 public byte[] Certificate {
71                         get { return hwr.GetClientCertificate (); }
72                 }
73
74                 public string Cookie {
75                         get { return GetString ("CERT_COOKIE"); }
76                 }
77
78                 public int Flags {
79                         get { return flags; }
80                 }
81
82                 public bool IsPresent {
83                         get { return ((flags & 0x01) == 0x01); }
84                 }
85
86                 public string Issuer {
87                         get { return GetString ("CERT_ISSUER"); }
88                 }
89
90                 public bool IsValid {
91                         get {
92                                 if (!IsPresent)
93                                         return true; // lame but true
94                                 // low on details
95                                 return ((flags & 0x02) == 0x00);
96                         }
97                 }
98
99                 public int KeySize {
100                         get { return GetInt ("CERT_KEYSIZE"); }
101                 }
102
103                 public byte[] PublicKey {
104                         get { return hwr.GetClientCertificatePublicKey (); }
105                 }
106
107                 public int SecretKeySize {
108                         get { return GetInt ("CERT_SECRETKEYSIZE"); }
109                 }
110
111                 public string SerialNumber {
112                         get { return GetString ("CERT_SERIALNUMBER"); }
113                 }
114
115                 public string ServerIssuer {
116                         get { return GetString ("CERT_SERVER_ISSUER"); }
117                 }
118
119                 public string ServerSubject {
120                         get { return GetString ("CERT_SERVER_SUBJECT"); }
121                 }
122
123                 public string Subject {
124                         get { return GetString ("CERT_SUBJECT"); }
125                 }
126
127                 public DateTime ValidFrom {
128                         get { return from; }
129                 }
130
131                 public DateTime ValidUntil {
132                         get { return until; }
133                 }
134
135
136                 // LAMESPEC: this doesn't return values added with Add(string,string)
137                 public override string Get (string field)
138                 {
139                         switch (field) {
140                         default:
141                                 return String.Empty;
142                         }
143                 }
144
145                 // stuff
146                 int GetInt (string variable)
147                 {
148                         if (!IsPresent)
149                                 return 0;
150
151                         return GetIntNoPresense (variable);
152                 }
153
154                 int GetIntNoPresense (string variable)
155                 {
156                         string s = hwr.GetServerVariable (variable);
157                         if (s == null)
158                                 return 0;
159
160                         try {
161                                 return Int32.Parse (s, Helpers.InvariantCulture);
162                         }
163                         catch {
164                                 return 0;
165                         }
166                 }
167
168                 string GetString (string variable)
169                 {
170                         if (!IsPresent)
171                                 return String.Empty;
172
173                         string s = hwr.GetServerVariable (variable);
174                         return (s == null) ? String.Empty : s;
175                 }
176         }
177 }