Fix XMM scanning on Mac x86.
[mono.git] / mcs / class / referencesource / System / net / System / Net / NetworkCredential.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="NetworkCredential.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 #if MONO
8 #undef FEATURE_PAL
9 #endif
10
11 namespace System.Net {
12
13     using System.IO;
14     using System.Runtime.InteropServices;
15     using System.Security;
16     using System.Security.Cryptography;
17     using System.Security.Permissions;
18     using System.Text;
19     using System.Threading;
20     using Microsoft.Win32;
21
22
23     /// <devdoc>
24     ///    <para>Provides credentials for password-based
25     ///       authentication schemes such as basic, digest, NTLM and Kerberos.</para>
26     /// </devdoc>
27     public class NetworkCredential : ICredentials,ICredentialsByHost {
28
29 #if FEATURE_MONO_CAS
30         private static volatile EnvironmentPermission m_environmentUserNamePermission;
31         private static volatile EnvironmentPermission m_environmentDomainNamePermission;
32         private static readonly object lockingObject = new object();
33 #endif
34         private string m_domain;
35         private string m_userName;
36 #if !FEATURE_PAL
37         private SecureString m_password;
38 #else  //FEATURE_PAL
39         private string m_password;
40 #endif //FEATURE_PAL
41
42         public NetworkCredential()
43         : this(string.Empty, string.Empty, string.Empty) {
44         }
45
46         /// <devdoc>
47         ///    <para>
48         ///       Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
49         ///       class with name and password set as specified.
50         ///    </para>
51         /// </devdoc>
52         public NetworkCredential(string userName, string password)
53         : this(userName, password, string.Empty) {
54         }
55
56 #if !FEATURE_PAL
57         /// <devdoc>
58         ///    <para>
59         ///       Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
60         ///       class with name and password set as specified.
61         ///    </para>
62         /// </devdoc>
63         public NetworkCredential(string userName, SecureString password)
64         : this(userName, password, string.Empty) {
65         }
66 #endif //!FEATURE_PAL        
67         
68         /// <devdoc>
69         ///    <para>
70         ///       Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
71         ///       class with name and password set as specified.
72         ///    </para>
73         /// </devdoc>
74         public NetworkCredential(string userName, string password, string domain) {
75             UserName = userName;
76             Password = password;
77             Domain = domain;
78         }
79
80 #if !FEATURE_PAL
81         /// <devdoc>
82         ///    <para>
83         ///       Initializes a new instance of the <see cref='System.Net.NetworkCredential'/>
84         ///       class with name and password set as specified.
85         ///    </para>
86         /// </devdoc>
87         public NetworkCredential(string userName, SecureString password, string domain) {
88             UserName = userName;
89             SecurePassword = password;
90             Domain = domain;
91         }
92 #endif //!FEATURE_PAL        
93
94 #if FEATURE_MONO_CAS
95         void InitializePart1() {
96             if (m_environmentUserNamePermission == null) {
97                 lock(lockingObject) {
98                     if (m_environmentUserNamePermission == null) {
99                         m_environmentDomainNamePermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERDOMAIN");
100                         m_environmentUserNamePermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME");
101                     }
102                 }
103             }
104         }
105 #endif
106
107         /// <devdoc>
108         ///    <para>
109         ///       The user name associated with this credential.
110         ///    </para>
111         /// </devdoc>
112         public string UserName {
113             get {
114 #if FEATURE_MONO_CAS
115                 InitializePart1();
116                 m_environmentUserNamePermission.Demand();
117 #endif
118                 return InternalGetUserName();
119             }
120             set {
121                 if (value == null)
122                     m_userName = String.Empty;
123                 else
124                     m_userName = value;
125                 // GlobalLog.Print("NetworkCredential::set_UserName: m_userName: \"" + m_userName + "\"" );
126             }
127         }
128
129         /// <devdoc>
130         ///    <para>
131         ///       The password for the user name.
132         ///    </para>
133         /// </devdoc>
134         public string Password {
135             get {
136 #if FEATURE_MONO_CAS
137                 ExceptionHelper.UnmanagedPermission.Demand();
138 #endif
139                 return InternalGetPassword();
140             }
141             set {
142 #if FEATURE_PAL
143                 if (value == null)
144                     m_password = String.Empty;
145                 else
146                     m_password = value;
147 //                GlobalLog.Print("NetworkCredential::set_Password: m_password: \"" + m_password + "\"" );
148 #else //!FEATURE_PAL
149                 m_password = UnsafeNclNativeMethods.SecureStringHelper.CreateSecureString(value);
150 //                GlobalLog.Print("NetworkCredential::set_Password: value = " + value);
151 //                GlobalLog.Print("NetworkCredential::set_Password: m_password:");
152 //                GlobalLog.Dump(m_password);
153 #endif //!FEATURE_PAL
154             }
155         }
156
157 #if !FEATURE_PAL
158         /// <devdoc>
159         ///    <para>
160         ///       The password for the user name.
161         ///    </para>
162         /// </devdoc>
163         public SecureString SecurePassword {
164             get {
165 #if FEATURE_MONO_CAS
166                 ExceptionHelper.UnmanagedPermission.Demand();
167 #endif
168                 return InternalGetSecurePassword().Copy();
169             }
170             set {
171                 if (value == null)
172                     m_password = new SecureString(); // makes 0 length string
173                 else
174                     m_password = value.Copy();
175             }
176         }
177 #endif //!FEATURE_PAL
178         
179         /// <devdoc>
180         ///    <para>
181         ///       The machine name that verifies
182         ///       the credentials. Usually this is the host machine.
183         ///    </para>
184         /// </devdoc>
185         public string Domain {
186             get {
187 #if FEATURE_MONO_CAS
188                 InitializePart1();
189                 m_environmentDomainNamePermission.Demand();
190 #endif
191                 return InternalGetDomain();
192             }
193             set {
194                 if (value == null)
195                     m_domain = String.Empty;
196                 else
197                     m_domain = value;
198 //                GlobalLog.Print("NetworkCredential::set_Domain: m_domain: \"" + m_domain + "\"" );
199             }
200         }
201
202         internal string InternalGetUserName() {
203             // GlobalLog.Print("NetworkCredential::get_UserName: returning \"" + m_userName + "\"");
204             return m_userName;
205         }
206
207         internal string InternalGetPassword() {
208 #if FEATURE_PAL
209             // GlobalLog.Print("NetworkCredential::get_Password: returning \"" + m_password + "\"");
210             return m_password;
211 #else //!FEATURE_PAL
212             string decryptedString = UnsafeNclNativeMethods.SecureStringHelper.CreateString(m_password);
213
214             // GlobalLog.Print("NetworkCredential::get_Password: returning \"" + decryptedString + "\"");
215             return decryptedString;
216 #endif //!FEATURE_PAL
217         }
218
219 #if !FEATURE_PAL
220         internal SecureString InternalGetSecurePassword()
221         {
222             return m_password;
223         }
224 #endif //!FEATURE_PAL
225
226         internal string InternalGetDomain()
227         {
228             // GlobalLog.Print("NetworkCredential::get_Domain: returning \"" + m_domain + "\"");
229             return m_domain;
230         }
231
232         internal string InternalGetDomainUserName() {
233             string domainUserName = InternalGetDomain();
234             if (domainUserName.Length != 0)
235                 domainUserName += "\\";
236             domainUserName += InternalGetUserName();
237             return domainUserName;
238         }
239
240         /// <devdoc>
241         ///    <para>
242         ///       Returns an instance of the NetworkCredential class for a Uri and
243         ///       authentication type.
244         ///    </para>
245         /// </devdoc>
246         public NetworkCredential GetCredential(Uri uri, String authType) {
247             return this;
248         }
249
250         public NetworkCredential GetCredential(string host, int port, String authenticationType) {
251             return this;
252         }
253
254 #if DEBUG
255         // this method is only called as part of an assert
256         internal bool IsEqualTo(object compObject) {
257             if ((object)compObject == null)
258                 return false;
259             if ((object)this == (object)compObject)
260                 return true;
261             NetworkCredential compCred = compObject as NetworkCredential;
262             if ((object)compCred == null)
263                 return false;
264 #if FEATURE_PAL
265             return(InternalGetUserName() == compCred.InternalGetUserName() &&
266                    InternalGetPassword() == compCred.InternalGetPassword() &&
267                    InternalGetDomain()  == compCred.InternalGetDomain());
268 #else //!FEATURE_PAL
269             return (InternalGetUserName() == compCred.InternalGetUserName() &&
270                     InternalGetDomain() == compCred.InternalGetDomain() &&
271                     UnsafeNclNativeMethods.SecureStringHelper.AreEqualValues(InternalGetSecurePassword(), 
272                                                                              compCred.InternalGetSecurePassword()));
273 #endif //!FEATURE_PAL
274         }
275 #endif //DEBUG
276     } // class NetworkCredential
277 } // namespace System.Net