IsolatedStorageFile.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. //
  2. // System.IO.IsolatedStorage.IsolatedStorageFile
  3. //
  4. // Authors
  5. // Jonathan Pryor ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Jonathan Pryor
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if !NET_2_1
  31. using System.Collections;
  32. using System.Reflection;
  33. using System.Runtime.InteropServices;
  34. using System.Runtime.Serialization.Formatters.Binary;
  35. using System.Security;
  36. using System.Security.Cryptography;
  37. using System.Security.Permissions;
  38. using System.Security.Policy;
  39. using System.Text;
  40. using Mono.Security.Cryptography;
  41. namespace System.IO.IsolatedStorage {
  42. // This is a terribly named class. It doesn't actually represent a file as
  43. // much as a directory
  44. #if NET_2_0
  45. [ComVisible (true)]
  46. #endif
  47. // FIXME: Further limit the assertion when imperative Assert is implemented
  48. [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
  49. public sealed class IsolatedStorageFile : IsolatedStorage, IDisposable {
  50. private bool _resolved;
  51. private ulong _maxSize;
  52. private Evidence _fullEvidences;
  53. public static IEnumerator GetEnumerator (IsolatedStorageScope scope)
  54. {
  55. Demand (scope);
  56. switch (scope) {
  57. case IsolatedStorageScope.User:
  58. case IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  59. #if NET_2_0
  60. case IsolatedStorageScope.Machine:
  61. #endif
  62. break;
  63. default:
  64. string msg = Locale.GetText ("Invalid scope, only User, User|Roaming and Machine are valid");
  65. throw new ArgumentException (msg);
  66. }
  67. return new IsolatedStorageFileEnumerator (scope, GetIsolatedStorageRoot (scope));
  68. }
  69. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope,
  70. Evidence domainEvidence, Type domainEvidenceType,
  71. Evidence assemblyEvidence, Type assemblyEvidenceType)
  72. {
  73. Demand (scope);
  74. bool domain = ((scope & IsolatedStorageScope.Domain) != 0);
  75. if (domain && (domainEvidence == null))
  76. throw new ArgumentNullException ("domainEvidence");
  77. bool assembly = ((scope & IsolatedStorageScope.Assembly) != 0);
  78. if (assembly && (assemblyEvidence == null))
  79. throw new ArgumentNullException ("assemblyEvidence");
  80. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  81. if (domain) {
  82. if (domainEvidenceType == null) {
  83. storageFile._domainIdentity = GetDomainIdentityFromEvidence (domainEvidence);
  84. } else {
  85. storageFile._domainIdentity = GetTypeFromEvidence (domainEvidence, domainEvidenceType);
  86. }
  87. if (storageFile._domainIdentity == null)
  88. throw new IsolatedStorageException (Locale.GetText ("Couldn't find domain identity."));
  89. }
  90. if (assembly) {
  91. if (assemblyEvidenceType == null) {
  92. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (assemblyEvidence);
  93. } else {
  94. storageFile._assemblyIdentity = GetTypeFromEvidence (assemblyEvidence, assemblyEvidenceType);
  95. }
  96. if (storageFile._assemblyIdentity == null)
  97. throw new IsolatedStorageException (Locale.GetText ("Couldn't find assembly identity."));
  98. }
  99. storageFile.PostInit ();
  100. return storageFile;
  101. }
  102. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, object domainIdentity, object assemblyIdentity)
  103. {
  104. Demand (scope);
  105. if (((scope & IsolatedStorageScope.Domain) != 0) && (domainIdentity == null))
  106. throw new ArgumentNullException ("domainIdentity");
  107. bool assembly = ((scope & IsolatedStorageScope.Assembly) != 0);
  108. if (assembly && (assemblyIdentity == null))
  109. throw new ArgumentNullException ("assemblyIdentity");
  110. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  111. if (assembly)
  112. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  113. storageFile._domainIdentity = domainIdentity;
  114. storageFile._assemblyIdentity = assemblyIdentity;
  115. storageFile.PostInit ();
  116. return storageFile;
  117. }
  118. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
  119. {
  120. Demand (scope);
  121. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  122. if ((scope & IsolatedStorageScope.Domain) != 0) {
  123. storageFile._domainIdentity = GetTypeFromEvidence (AppDomain.CurrentDomain.Evidence, domainEvidenceType);
  124. }
  125. if ((scope & IsolatedStorageScope.Assembly) != 0) {
  126. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  127. storageFile._fullEvidences = e;
  128. if ((scope & IsolatedStorageScope.Domain) != 0)
  129. storageFile._assemblyIdentity = GetTypeFromEvidence (e, assemblyEvidenceType);
  130. else
  131. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  132. }
  133. storageFile.PostInit ();
  134. return storageFile;
  135. }
  136. #if NET_2_0
  137. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, object applicationIdentity)
  138. {
  139. Demand (scope);
  140. if (applicationIdentity == null)
  141. throw new ArgumentNullException ("applicationIdentity");
  142. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  143. storageFile._applicationIdentity = applicationIdentity;
  144. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  145. storageFile.PostInit ();
  146. return storageFile;
  147. }
  148. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, Type applicationEvidenceType)
  149. {
  150. Demand (scope);
  151. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  152. storageFile.InitStore (scope, applicationEvidenceType);
  153. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  154. storageFile.PostInit ();
  155. return storageFile;
  156. }
  157. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByMachine)]
  158. public static IsolatedStorageFile GetMachineStoreForApplication ()
  159. {
  160. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Application;
  161. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  162. storageFile.InitStore (scope, null);
  163. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  164. storageFile.PostInit ();
  165. return storageFile;
  166. }
  167. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByMachine)]
  168. public static IsolatedStorageFile GetMachineStoreForAssembly ()
  169. {
  170. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly;
  171. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  172. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  173. storageFile._fullEvidences = e;
  174. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  175. storageFile.PostInit ();
  176. return storageFile;
  177. }
  178. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.DomainIsolationByMachine)]
  179. public static IsolatedStorageFile GetMachineStoreForDomain ()
  180. {
  181. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
  182. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  183. storageFile._domainIdentity = GetDomainIdentityFromEvidence (AppDomain.CurrentDomain.Evidence);
  184. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  185. storageFile._fullEvidences = e;
  186. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  187. storageFile.PostInit ();
  188. return storageFile;
  189. }
  190. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser)]
  191. public static IsolatedStorageFile GetUserStoreForApplication ()
  192. {
  193. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
  194. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  195. storageFile.InitStore (scope, null);
  196. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  197. storageFile.PostInit ();
  198. return storageFile;
  199. }
  200. #endif
  201. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
  202. public static IsolatedStorageFile GetUserStoreForAssembly ()
  203. {
  204. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
  205. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  206. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  207. storageFile._fullEvidences = e;
  208. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  209. storageFile.PostInit ();
  210. return storageFile;
  211. }
  212. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser)]
  213. public static IsolatedStorageFile GetUserStoreForDomain ()
  214. {
  215. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
  216. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  217. storageFile._domainIdentity = GetDomainIdentityFromEvidence (AppDomain.CurrentDomain.Evidence);
  218. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  219. storageFile._fullEvidences = e;
  220. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  221. storageFile.PostInit ();
  222. return storageFile;
  223. }
  224. public static void Remove (IsolatedStorageScope scope)
  225. {
  226. string dir = GetIsolatedStorageRoot (scope);
  227. Directory.Delete (dir, true);
  228. }
  229. // internal static stuff
  230. // Security Note: We're using InternalGetFolderPath because
  231. // IsolatedStorage must be able to work even if we do not have
  232. // FileIOPermission's PathDiscovery permissions
  233. internal static string GetIsolatedStorageRoot (IsolatedStorageScope scope)
  234. {
  235. // IsolatedStorageScope mixes several flags into one.
  236. // This first level deals with the root directory - it
  237. // is decided based on User, User+Roaming or Machine
  238. string root = null;
  239. if ((scope & IsolatedStorageScope.User) != 0) {
  240. if ((scope & IsolatedStorageScope.Roaming) != 0) {
  241. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.LocalApplicationData);
  242. } else {
  243. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.ApplicationData);
  244. }
  245. #if NET_2_0
  246. } else if ((scope & IsolatedStorageScope.Machine) != 0) {
  247. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.CommonApplicationData);
  248. #endif
  249. }
  250. if (root == null) {
  251. string msg = Locale.GetText ("Couldn't access storage location for '{0}'.");
  252. throw new IsolatedStorageException (String.Format (msg, scope));
  253. }
  254. return Path.Combine (root, ".isolated-storage");
  255. }
  256. private static void Demand (IsolatedStorageScope scope)
  257. {
  258. if (SecurityManager.SecurityEnabled) {
  259. IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
  260. isfp.UsageAllowed = ScopeToContainment (scope);
  261. isfp.Demand ();
  262. }
  263. }
  264. private static IsolatedStorageContainment ScopeToContainment (IsolatedStorageScope scope)
  265. {
  266. switch (scope) {
  267. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.User:
  268. return IsolatedStorageContainment.DomainIsolationByUser;
  269. case IsolatedStorageScope.Assembly | IsolatedStorageScope.User:
  270. return IsolatedStorageContainment.AssemblyIsolationByUser;
  271. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  272. return IsolatedStorageContainment.DomainIsolationByRoamingUser;
  273. case IsolatedStorageScope.Assembly | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  274. return IsolatedStorageContainment.AssemblyIsolationByRoamingUser;
  275. #if NET_2_0
  276. case IsolatedStorageScope.Application | IsolatedStorageScope.User:
  277. return IsolatedStorageContainment.ApplicationIsolationByUser;
  278. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.Machine:
  279. return IsolatedStorageContainment.DomainIsolationByMachine;
  280. case IsolatedStorageScope.Assembly | IsolatedStorageScope.Machine:
  281. return IsolatedStorageContainment.AssemblyIsolationByMachine;
  282. case IsolatedStorageScope.Application | IsolatedStorageScope.Machine:
  283. return IsolatedStorageContainment.ApplicationIsolationByMachine;
  284. case IsolatedStorageScope.Application | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  285. return IsolatedStorageContainment.ApplicationIsolationByRoamingUser;
  286. #endif
  287. default:
  288. // unknown ?!?! then ask for maximum (unrestricted)
  289. return IsolatedStorageContainment.UnrestrictedIsolatedStorage;
  290. }
  291. }
  292. internal static ulong GetDirectorySize (DirectoryInfo di)
  293. {
  294. ulong size = 0;
  295. foreach (FileInfo fi in di.GetFiles ())
  296. size += (ulong) fi.Length;
  297. foreach (DirectoryInfo d in di.GetDirectories ())
  298. size += GetDirectorySize (d);
  299. return size;
  300. }
  301. // non-static stuff
  302. private DirectoryInfo directory;
  303. private IsolatedStorageFile (IsolatedStorageScope scope)
  304. {
  305. storage_scope = scope;
  306. }
  307. internal IsolatedStorageFile (IsolatedStorageScope scope, string location)
  308. {
  309. storage_scope = scope;
  310. directory = new DirectoryInfo (location);
  311. if (!directory.Exists) {
  312. string msg = Locale.GetText ("Invalid storage.");
  313. throw new IsolatedStorageException (msg);
  314. }
  315. // load the identities
  316. }
  317. ~IsolatedStorageFile ()
  318. {
  319. }
  320. private void PostInit ()
  321. {
  322. string root = GetIsolatedStorageRoot (Scope);
  323. string dir = null;
  324. #if NET_2_0
  325. if (_applicationIdentity != null) {
  326. dir = String.Format ("a{0}{1}", SeparatorInternal, GetNameFromIdentity (_applicationIdentity));
  327. } else
  328. #endif
  329. if (_domainIdentity != null) {
  330. dir = String.Format ("d{0}{1}{0}{2}", SeparatorInternal,
  331. GetNameFromIdentity (_domainIdentity), GetNameFromIdentity (_assemblyIdentity));
  332. } else if (_assemblyIdentity != null) {
  333. dir = String.Format ("d{0}none{0}{1}", SeparatorInternal, GetNameFromIdentity (_assemblyIdentity));
  334. } else {
  335. throw new IsolatedStorageException (Locale.GetText ("No code identity available."));
  336. }
  337. root = Path.Combine (root, dir);
  338. // identities have been selected
  339. directory = new DirectoryInfo (root);
  340. if (!directory.Exists) {
  341. directory.Create ();
  342. SaveIdentities (root);
  343. }
  344. }
  345. [CLSCompliant(false)]
  346. public override ulong CurrentSize {
  347. get { return GetDirectorySize (directory); }
  348. }
  349. [CLSCompliant(false)]
  350. public override ulong MaximumSize {
  351. // return an ulong but default is signed long
  352. get {
  353. if (!SecurityManager.SecurityEnabled)
  354. return Int64.MaxValue;
  355. if (_resolved)
  356. return _maxSize;
  357. Evidence e = null;
  358. if (_fullEvidences != null) {
  359. // if possible use the complete evidences we had
  360. // for computing the X identity
  361. e = _fullEvidences;
  362. } else {
  363. e = new Evidence ();
  364. // otherwise use what was provided
  365. if (_assemblyIdentity != null)
  366. e.AddHost (_assemblyIdentity);
  367. }
  368. if (e.Count < 1) {
  369. throw new InvalidOperationException (
  370. Locale.GetText ("Couldn't get the quota from the available evidences."));
  371. }
  372. PermissionSet denied = null;
  373. PermissionSet ps = SecurityManager.ResolvePolicy (e, null, null, null, out denied);
  374. IsolatedStoragePermission isp = GetPermission (ps);
  375. if (isp == null) {
  376. if (ps.IsUnrestricted ()) {
  377. _maxSize = Int64.MaxValue; /* default value */
  378. } else {
  379. throw new InvalidOperationException (
  380. Locale.GetText ("No quota from the available evidences."));
  381. }
  382. } else {
  383. _maxSize = (ulong) isp.UserQuota;
  384. }
  385. _resolved = true;
  386. return _maxSize;
  387. }
  388. }
  389. internal string Root {
  390. get { return directory.FullName; }
  391. }
  392. // methods
  393. public void Close ()
  394. {
  395. }
  396. public void CreateDirectory (string dir)
  397. {
  398. if (dir == null)
  399. throw new ArgumentNullException ("dir");
  400. if (directory.GetFiles (dir).Length > 0)
  401. throw new IOException (Locale.GetText ("Directory name already exists as a file."));
  402. directory.CreateSubdirectory (dir);
  403. }
  404. public void DeleteDirectory (string dir)
  405. {
  406. DirectoryInfo subdir = directory.CreateSubdirectory (dir);
  407. subdir.Delete ();
  408. }
  409. public void DeleteFile (string file)
  410. {
  411. File.Delete (Path.Combine (directory.FullName, file));
  412. }
  413. public void Dispose ()
  414. {
  415. // nothing to dispose, anyway we want to please the tools
  416. GC.SuppressFinalize (this);
  417. }
  418. public string[] GetDirectoryNames (string searchPattern)
  419. {
  420. DirectoryInfo[] adi = directory.GetDirectories (searchPattern);
  421. return GetNames (adi);
  422. }
  423. private string[] GetNames (FileSystemInfo[] afsi)
  424. {
  425. string[] r = new string[afsi.Length];
  426. for (int i = 0; i != afsi.Length; ++i)
  427. r[i] = afsi[i].Name;
  428. return r;
  429. }
  430. public string[] GetFileNames (string searchPattern)
  431. {
  432. if (searchPattern == null)
  433. throw new ArgumentNullException ("searchPattern");
  434. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  435. // so we need to split them to get the right results
  436. string path = Path.GetDirectoryName (searchPattern);
  437. string pattern = Path.GetFileName (searchPattern);
  438. FileInfo[] afi = null;
  439. if (path == null || path.Length == 0) {
  440. afi = directory.GetFiles (searchPattern);
  441. } else {
  442. DirectoryInfo[] subdirs = directory.GetDirectories (path);
  443. // we're looking for a single result, identical to path (no pattern here)
  444. // we're also looking for something under the current path (not outside isolated storage)
  445. if ((subdirs.Length == 1) && (subdirs [0].Name == path) && (subdirs [0].FullName.IndexOf (directory.FullName) >= 0)) {
  446. afi = subdirs [0].GetFiles (pattern);
  447. } else {
  448. // CAS, even in FullTrust, normally enforce IsolatedStorage
  449. throw new SecurityException ();
  450. }
  451. }
  452. return GetNames (afi);
  453. }
  454. public override void Remove ()
  455. {
  456. directory.Delete (true);
  457. }
  458. protected override IsolatedStoragePermission GetPermission (PermissionSet ps)
  459. {
  460. if (ps == null)
  461. return null;
  462. return (IsolatedStoragePermission) ps.GetPermission (typeof (IsolatedStorageFilePermission));
  463. }
  464. // internal stuff
  465. private string GetNameFromIdentity (object identity)
  466. {
  467. // Note: Default evidences return an XML string with ToString
  468. byte[] id = Encoding.UTF8.GetBytes (identity.ToString ());
  469. SHA1 hash = SHA1.Create ();
  470. // this create an unique name for an identity - bad identities like Url
  471. // results in bad (i.e. changing) names.
  472. byte[] full = hash.ComputeHash (id, 0, id.Length);
  473. byte[] half = new byte [10];
  474. Buffer.BlockCopy (full, 0, half, 0, half.Length);
  475. return CryptoConvert.ToHex (half);
  476. }
  477. private static object GetTypeFromEvidence (Evidence e, Type t)
  478. {
  479. foreach (object o in e) {
  480. if (o.GetType () == t)
  481. return o;
  482. }
  483. return null;
  484. }
  485. internal static object GetAssemblyIdentityFromEvidence (Evidence e)
  486. {
  487. // we prefer...
  488. // a. a Publisher evidence
  489. object identity = GetTypeFromEvidence (e, typeof (Publisher));
  490. if (identity != null)
  491. return identity;
  492. // b. a StrongName evidence
  493. identity = GetTypeFromEvidence (e, typeof (StrongName));
  494. if (identity != null)
  495. return identity;
  496. // c. a Url evidence
  497. return GetTypeFromEvidence (e, typeof (Url));
  498. }
  499. internal static object GetDomainIdentityFromEvidence (Evidence e)
  500. {
  501. // we prefer...
  502. // a. a ApplicationDirectory evidence
  503. object identity = GetTypeFromEvidence (e, typeof (ApplicationDirectory));
  504. if (identity != null)
  505. return identity;
  506. // b. a Url evidence
  507. return GetTypeFromEvidence (e, typeof (Url));
  508. }
  509. [Serializable]
  510. private struct Identities {
  511. public object Application;
  512. public object Assembly;
  513. public object Domain;
  514. public Identities (object application, object assembly, object domain)
  515. {
  516. Application = application;
  517. Assembly = assembly;
  518. Domain = domain;
  519. }
  520. }
  521. [SecurityPermission (SecurityAction.Assert, SerializationFormatter = true)]
  522. private void LoadIdentities (string root)
  523. {
  524. if (!File.Exists (root + ".storage"))
  525. throw new IsolatedStorageException (Locale.GetText ("Missing identities."));
  526. using (FileStream fs = File.OpenRead (root + ".storage")) {
  527. BinaryFormatter deformatter = new BinaryFormatter ();
  528. Identities identities = (Identities) deformatter.Deserialize (fs);
  529. fs.Close ();
  530. _applicationIdentity = identities.Application;
  531. _assemblyIdentity = identities.Assembly;
  532. _domainIdentity = identities.Domain;
  533. }
  534. }
  535. [SecurityPermission (SecurityAction.Assert, SerializationFormatter = true)]
  536. private void SaveIdentities (string root)
  537. {
  538. Identities identities = new Identities (_applicationIdentity, _assemblyIdentity, _domainIdentity);
  539. using (FileStream fs = File.OpenWrite (root + ".storage")) {
  540. BinaryFormatter formatter = new BinaryFormatter ();
  541. formatter.Serialize (fs, identities);
  542. fs.Close ();
  543. }
  544. }
  545. }
  546. }
  547. #endif