-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
isolatedstorage.cs
1272 lines (1056 loc) · 43.1 KB
/
isolatedstorage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// <OWNER>Microsoft</OWNER>
//
/*============================================================
*
* Class: IsolatedStorage
*
*
* Purpose: Provides access to Persisted Application / Assembly data
*
* Date: Feb 15, 2000
*
===========================================================*/
namespace System.IO.IsolatedStorage {
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Collections;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;
using System.Security.Cryptography;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
#if FEATURE_SERIALIZATION
using System.Runtime.Serialization.Formatters.Binary;
#endif // FEATURE_SERIALIZATION
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Microsoft.Win32;
using System.Diagnostics.Contracts;
[Serializable]
[Flags]
[System.Runtime.InteropServices.ComVisible(true)]
public enum IsolatedStorageScope
{
// Dependency in native : COMIsolatedStorage.h
None = 0x00,
User = 0x01,
#if !FEATURE_ISOSTORE_LIGHT
Domain = 0x02,
Assembly = 0x04,
Roaming = 0x08,
Machine = 0x10,
#endif // FEATURE_ISOSTORE_LIGHT
Application = 0x20
}
#if !FEATURE_ISOSTORE_LIGHT
// not serializable
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class IsolatedStorage : MarshalByRefObject
{
// Helper constants
internal const IsolatedStorageScope c_Assembly =
(IsolatedStorageScope.User |
IsolatedStorageScope.Assembly);
internal const IsolatedStorageScope c_Domain =
(IsolatedStorageScope.User |
IsolatedStorageScope.Assembly |
IsolatedStorageScope.Domain);
internal const IsolatedStorageScope c_AssemblyRoaming =
(IsolatedStorageScope.Roaming |
IsolatedStorageScope.User |
IsolatedStorageScope.Assembly);
internal const IsolatedStorageScope c_DomainRoaming =
(IsolatedStorageScope.Roaming |
IsolatedStorageScope.User |
IsolatedStorageScope.Assembly |
IsolatedStorageScope.Domain);
internal const IsolatedStorageScope c_MachineAssembly =
(IsolatedStorageScope.Machine |
IsolatedStorageScope.Assembly);
internal const IsolatedStorageScope c_MachineDomain =
(IsolatedStorageScope.Machine |
IsolatedStorageScope.Assembly |
IsolatedStorageScope.Domain);
internal const IsolatedStorageScope c_AppUser =
(IsolatedStorageScope.Application |
IsolatedStorageScope.User);
internal const IsolatedStorageScope c_AppMachine =
(IsolatedStorageScope.Application |
IsolatedStorageScope.Machine);
internal const IsolatedStorageScope c_AppUserRoaming =
(IsolatedStorageScope.Roaming |
IsolatedStorageScope.Application |
IsolatedStorageScope.User);
#if !FEATURE_PAL
private const String s_Publisher = "Publisher";
#endif // !FEATURE_PAL
private const String s_StrongName = "StrongName";
private const String s_Site = "Site";
private const String s_Url = "Url";
private const String s_Zone = "Zone";
private ulong m_Quota;
private bool m_ValidQuota;
private Object m_DomainIdentity;
private Object m_AssemIdentity;
private Object m_AppIdentity;
private String m_DomainName;
private String m_AssemName;
private String m_AppName;
private IsolatedStorageScope m_Scope;
private static volatile IsolatedStorageFilePermission s_PermDomain;
private static volatile IsolatedStorageFilePermission s_PermMachineDomain;
private static volatile IsolatedStorageFilePermission s_PermDomainRoaming;
private static volatile IsolatedStorageFilePermission s_PermAssem;
private static volatile IsolatedStorageFilePermission s_PermMachineAssem;
private static volatile IsolatedStorageFilePermission s_PermAssemRoaming;
private static volatile IsolatedStorageFilePermission s_PermAppUser;
private static volatile IsolatedStorageFilePermission s_PermAppMachine;
private static volatile IsolatedStorageFilePermission s_PermAppUserRoaming;
private static volatile SecurityPermission s_PermControlEvidence;
private static volatile PermissionSet s_PermUnrestricted;
#if _DEBUG
private static bool s_fDebug = false;
private static int s_iDebug = 0;
#endif
// This one should be a macro, expecting JIT to inline this.
internal static bool IsRoaming(IsolatedStorageScope scope)
{
return ((scope & IsolatedStorageScope.Roaming) != 0);
}
internal bool IsRoaming()
{
return ((m_Scope & IsolatedStorageScope.Roaming) != 0);
}
// This one should be a macro, expecting JIT to inline this.
internal static bool IsDomain(IsolatedStorageScope scope)
{
return ((scope & IsolatedStorageScope.Domain) != 0);
}
internal bool IsDomain()
{
return ((m_Scope & IsolatedStorageScope.Domain) != 0);
}
#if false
// Not currently used
// This one should be a macro, expecting JIT to inline this.
internal static bool IsUser(IsolatedStorageScope scope)
{
return ((scope & IsolatedStorageScope.User) != 0);
}
#endif
// This one should be a macro, expecting JIT to inline this.
internal static bool IsMachine(IsolatedStorageScope scope)
{
return ((scope & IsolatedStorageScope.Machine) != 0);
}
internal bool IsAssembly()
{
return ((m_Scope & IsolatedStorageScope.Assembly) != 0);
}
// This one should be a macro, expecting JIT to inline this.
internal static bool IsApp(IsolatedStorageScope scope)
{
return ((scope & IsolatedStorageScope.Application) != 0);
}
internal bool IsApp()
{
return ((m_Scope & IsolatedStorageScope.Application) != 0);
}
private String GetNameFromID(String typeID, String instanceID)
{
return typeID + SeparatorInternal + instanceID;
}
private static String GetPredefinedTypeName(Object o)
{
#if !FEATURE_PAL
if (o is Publisher)
return s_Publisher;
else if (o is StrongName)
return s_StrongName;
#else
if (o is StrongName)
return s_StrongName;
#endif // !FEATURE_PAL
else if (o is Url)
return s_Url;
else if (o is Site)
return s_Site;
else if (o is Zone)
return s_Zone;
return null;
}
#if !FEATURE_PAL
internal static String GetHash(Stream s)
{
using (SHA1 sha1 = new SHA1CryptoServiceProvider())
{
byte[] b = sha1.ComputeHash(s);
return Path.ToBase32StringSuitableForDirName(b);
}
}
#else
internal static String GetHash(Stream s)
{
const int MAX_BUFFER_SIZE = 1024;
byte[] buffer = new byte[MAX_BUFFER_SIZE];
// 160 bits SHA1 output as defined in the Secure Hash Standard
const int MESSAGE_DIGEST_LENGTH = 20;
int digestLength = 0;
byte[] digest = new byte[MESSAGE_DIGEST_LENGTH];
IntPtr hProv = (IntPtr) 0;
IntPtr hHash = (IntPtr) 0;
if( Win32Native.CryptAcquireContext(out hProv, null, null, 0, 0) == false)
throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
if( Win32Native.CryptCreateHash(hProv, Win32Native.CALG_SHA1,(IntPtr) 0, 0, out hHash) == false)
throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
int bytesRead;
do {
bytesRead = s.Read(buffer,0,MAX_BUFFER_SIZE);
if (bytesRead > 0) {
if(Win32Native.CryptHashData(hHash, buffer, bytesRead, 0) == false)
{
throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
}
}
} while (bytesRead > 0);
// perform a sanity check to make sure the digest size equals MESSAGE_DIGEST_LENGTH
int fourBytes = 4;
if( Win32Native.CryptGetHashParam(hHash, Win32Native.HP_HASHSIZE, out digestLength, ref fourBytes, 0) == false
|| (digestLength != MESSAGE_DIGEST_LENGTH)
)
{
throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
}
if( Win32Native.CryptGetHashParam(hHash, Win32Native.HP_HASHVAL, digest, ref digestLength, 0) == false)
throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
if( Win32Native.CryptDestroyHash(hHash) == false)
throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
if( Win32Native.CryptReleaseContext(hProv, 0) == false )
throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
return Path.ToBase32StringSuitableForDirName(digest);
}
#endif // !FEATURE_PAL
#if false
// This is not used.
private static String ToBase64StringSuitableForDirName(byte[] buff)
{
String s = Convert.ToBase64String(buff);
StringBuilder sb = new StringBuilder();
// Remove certain chars not suited for file names
for (int i=0; i<s.Length; ++i)
{
Char c = s[i];
if (Char.IsUpper(s[i]))
{
// NT file system is case in-sensitive. This will
// weaken the hash. Preserve the streangth of the
// hash by adding a new "special char" before all caps
sb.Append("O");
sb.Append(s[i]);
}
else if (c == '/') // replace '/' with '-'
sb.Append("-");
else if (c != '=') // ignore padding
sb.Append(s[i]);
}
return sb.ToString();
}
#endif
private static bool IsValidName(String s)
{
for (int i=0; i<s.Length; ++i)
{
if (!Char.IsLetter(s[i]) && !Char.IsDigit(s[i]))
return false;
}
return true;
}
private static SecurityPermission GetControlEvidencePermission()
{
// Don't sync. OK to create this object more than once.
if (s_PermControlEvidence == null)
s_PermControlEvidence = new SecurityPermission(
SecurityPermissionFlag.ControlEvidence);
return s_PermControlEvidence;
}
private static PermissionSet GetUnrestricted()
{
// Don't sync. OK to create this object more than once.
if (s_PermUnrestricted == null)
s_PermUnrestricted = new PermissionSet(
PermissionState.Unrestricted);
return s_PermUnrestricted;
}
protected virtual char SeparatorExternal
{
#if !FEATURE_PAL
get { return '\\'; }
#else
get { return System.IO.Path.DirectorySeparatorChar; }
#endif //!FEATURE_PAL
}
protected virtual char SeparatorInternal
{
get { return '.'; }
}
// gets "amount of space / resource used"
[CLSCompliant(false)]
[Obsolete("IsolatedStorage.MaximumSize has been deprecated because it is not CLS Compliant. To get the maximum size use IsolatedStorage.Quota")]
public virtual ulong MaximumSize
{
get
{
if (m_ValidQuota)
return m_Quota;
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_QuotaIsUndefined", "MaximumSize"));
}
}
[CLSCompliant(false)]
[Obsolete("IsolatedStorage.CurrentSize has been deprecated because it is not CLS Compliant. To get the current size use IsolatedStorage.UsedSize")]
public virtual ulong CurrentSize
{
get
{
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_CurrentSizeUndefined", "CurrentSize"));
}
}
[ComVisible(false)]
public virtual long UsedSize {
get {
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_CurrentSizeUndefined", "UsedSize"));
}
}
[ComVisible(false)]
public virtual long Quota
{
get
{
if (m_ValidQuota)
return (long) m_Quota;
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_QuotaIsUndefined", "Quota"));
}
internal set
{
m_Quota = (ulong) value;
m_ValidQuota = true;
}
}
[ComVisible(false)]
public virtual long AvailableFreeSpace {
get {
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_QuotaIsUndefined", "AvailableFreeSpace"));
}
}
public Object DomainIdentity
{
[System.Security.SecuritySafeCritical] // auto-generated
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPolicy)]
get {
if (IsDomain())
return m_DomainIdentity;
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_DomainUndefined"));
}
}
[ComVisible(false)]
public Object ApplicationIdentity
{
[System.Security.SecuritySafeCritical] // auto-generated
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPolicy)]
get {
if (IsApp())
return m_AppIdentity;
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_ApplicationUndefined"));
}
}
public Object AssemblyIdentity
{
[System.Security.SecuritySafeCritical] // auto-generated
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPolicy)]
get {
if (IsAssembly())
return m_AssemIdentity;
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_AssemblyUndefined"));
}
}
[ComVisible(false)]
public virtual Boolean IncreaseQuotaTo(Int64 newQuotaSize) {
return false;
}
// Returns the AppDomain Stream (if present).
// Sets assem stream
[System.Security.SecurityCritical] // auto-generated
internal MemoryStream GetIdentityStream(IsolatedStorageScope scope)
{
BinaryFormatter bSer;
MemoryStream ms;
Object o;
GetUnrestricted().Assert();
bSer = new BinaryFormatter();
ms = new MemoryStream();
if (IsApp(scope))
o = m_AppIdentity;
else if (IsDomain(scope))
o = m_DomainIdentity;
else {
o = m_AssemIdentity;
}
if (o != null)
bSer.Serialize(ms, o);
ms.Position = 0;
return ms;
}
public IsolatedStorageScope Scope
{
get { return m_Scope; }
}
internal String DomainName
{
get {
if (IsDomain())
return m_DomainName;
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_DomainUndefined"));
}
}
internal String AssemName
{
get {
if (IsAssembly())
return m_AssemName;
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_AssemblyUndefined"));
}
}
internal String AppName
{
get {
if (IsApp())
return m_AppName;
throw new InvalidOperationException(
Environment.GetResourceString(
"IsolatedStorage_ApplicationUndefined"));
}
}
[System.Security.SecuritySafeCritical] // auto-generated
protected void InitStore(IsolatedStorageScope scope,
Type domainEvidenceType,
Type assemblyEvidenceType)
{
RuntimeAssembly assem;
AppDomain domain;
PermissionSet psAllowed, psDenied;
psAllowed = null;
psDenied = null;
assem = GetCaller();
GetControlEvidencePermission().Assert();
if (IsDomain(scope))
{
domain = Thread.GetDomain();
if (!IsRoaming(scope)) // No quota for roaming
{
psAllowed = domain.PermissionSet;
if (psAllowed == null)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_DomainGrantSet"));
}
_InitStore(scope, domain.Evidence, domainEvidenceType,
assem.Evidence, assemblyEvidenceType,
null,null);
}
else
{
if (!IsRoaming(scope)) // No quota for roaming
{
assem.GetGrantSet(out psAllowed, out psDenied);
if (psAllowed == null)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_AssemblyGrantSet"));
}
_InitStore(scope, null, null, assem.Evidence,
assemblyEvidenceType,
null,null);
}
SetQuota(psAllowed, psDenied);
}
[System.Security.SecuritySafeCritical] // auto-generated
protected void InitStore(IsolatedStorageScope scope,
Type appEvidenceType)
{
Assembly assem;
AppDomain domain;
PermissionSet psAllowed, psDenied;
psAllowed = null;
psDenied = null;
assem = GetCaller();
GetControlEvidencePermission().Assert();
if (IsApp(scope))
{
domain = Thread.GetDomain();
if (!IsRoaming(scope)) // No quota for roaming
{
psAllowed = domain.PermissionSet;
if (psAllowed == null)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_DomainGrantSet"));
}
#if !FEATURE_PAL
ActivationContext activationContext = AppDomain.CurrentDomain.ActivationContext;
if (activationContext == null)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_ApplicationMissingIdentity"));
ApplicationSecurityInfo asi = new ApplicationSecurityInfo(activationContext);
_InitStore(scope, null, null,
null,null,
asi.ApplicationEvidence, appEvidenceType);
#else
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_ApplicationMissingIdentity"));
#endif // !FEATURE_PAL
}
SetQuota(psAllowed, psDenied);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal void InitStore(IsolatedStorageScope scope,
Object domain, Object assem, Object app)
{
RuntimeAssembly callerAssembly;
PermissionSet psAllowed = null, psDenied = null;
Evidence domainEv = null, assemEv = null, appEv = null;
if (IsApp(scope))
{
EvidenceBase applicationEvidenceObject = app as EvidenceBase;
if (applicationEvidenceObject == null)
{
applicationEvidenceObject = new LegacyEvidenceWrapper(app);
}
appEv = new Evidence();
appEv.AddHostEvidence(applicationEvidenceObject);
}
else
{
EvidenceBase assemblyEvidenceObject = assem as EvidenceBase;
if (assemblyEvidenceObject == null)
{
assemblyEvidenceObject = new LegacyEvidenceWrapper(assem);
}
assemEv = new Evidence();
assemEv.AddHostEvidence(assemblyEvidenceObject);
if (IsDomain(scope))
{
EvidenceBase domainEvidenceObject = domain as EvidenceBase;
if (domainEvidenceObject == null)
{
domainEvidenceObject = new LegacyEvidenceWrapper(domain);
}
domainEv = new Evidence();
domainEv.AddHostEvidence(domainEvidenceObject);
}
}
_InitStore(scope, domainEv, null, assemEv, null, appEv, null);
// Set the quota based on the caller, not the evidence supplied
if (!IsRoaming(scope)) // No quota for roaming
{
callerAssembly = GetCaller();
GetControlEvidencePermission().Assert();
callerAssembly.GetGrantSet(out psAllowed, out psDenied);
if (psAllowed == null)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_AssemblyGrantSet"));
}
// This can be called only by trusted assemblies.
// This quota really does not correspond to the permissions
// granted for this evidence.
SetQuota(psAllowed, psDenied);
}
[System.Security.SecurityCritical] // auto-generated
internal void InitStore(IsolatedStorageScope scope,
Evidence domainEv, Type domainEvidenceType,
Evidence assemEv, Type assemEvidenceType,
Evidence appEv, Type appEvidenceType)
{
PermissionSet psAllowed = null;
if (!IsRoaming(scope))
{
if (IsApp(scope))
{
psAllowed = SecurityManager.GetStandardSandbox(appEv);
}
else if (IsDomain(scope))
{
psAllowed = SecurityManager.GetStandardSandbox(domainEv);
}
else
{
psAllowed = SecurityManager.GetStandardSandbox(assemEv);
}
}
_InitStore(scope, domainEv, domainEvidenceType,
assemEv, assemEvidenceType,
appEv, appEvidenceType);
SetQuota(psAllowed, null);
}
[System.Security.SecuritySafeCritical] // auto-generated
internal bool InitStore(IsolatedStorageScope scope,
Stream domain, Stream assem, Stream app, String domainName, String assemName, String appName)
{
BinaryFormatter bSer;
try {
GetUnrestricted().Assert();
bSer = new BinaryFormatter();
if (IsApp(scope))
{
// Get the Application Info
m_AppIdentity = bSer.Deserialize(app);
m_AppName = appName;
}
else
{
// Get the Assem Info
m_AssemIdentity = bSer.Deserialize(assem);
m_AssemName = assemName;
if (IsDomain(scope))
{
// Get the AppDomain Info
m_DomainIdentity = bSer.Deserialize(domain);
m_DomainName = domainName;
}
}
}
catch
{
return false;
}
Contract.Assert(m_ValidQuota == false, "Quota should be invalid here");
m_Scope = scope;
return true;
}
[System.Security.SecurityCritical] // auto-generated
private void _InitStore(IsolatedStorageScope scope,
Evidence domainEv, Type domainEvidenceType,
Evidence assemEv, Type assemblyEvidenceType,
Evidence appEv, Type appEvidenceType)
{
VerifyScope(scope);
// If its app-scoped, we only use the appId and appName. Else assume assembly evidence present
// and check if we need domain scoping too
// Input arg checks
if (IsApp(scope))
{
if (appEv == null)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_ApplicationMissingIdentity"));
}
else
{
if (assemEv == null)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_AssemblyMissingIdentity"));
if (IsDomain(scope) && (domainEv == null))
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_DomainMissingIdentity"));
}
// Security checks
DemandPermission(scope);
String typeHash = null, instanceHash = null;
if (IsApp(scope))
{
m_AppIdentity = GetAccountingInfo(appEv, appEvidenceType,
IsolatedStorageScope.Application, out typeHash, out instanceHash);
m_AppName = GetNameFromID(typeHash, instanceHash);
}
else
{
m_AssemIdentity = GetAccountingInfo(
assemEv, assemblyEvidenceType, IsolatedStorageScope.Assembly,
out typeHash, out instanceHash);
m_AssemName = GetNameFromID(typeHash, instanceHash);
if (IsDomain(scope))
{
m_DomainIdentity = GetAccountingInfo(domainEv, domainEvidenceType,
IsolatedStorageScope.Domain, out typeHash, out instanceHash);
m_DomainName = GetNameFromID(typeHash, instanceHash);
}
}
m_Scope = scope;
}
[System.Security.SecurityCritical] // auto-generated
private static Object GetAccountingInfo(
Evidence evidence, Type evidenceType, IsolatedStorageScope fAssmDomApp,
out String typeName, out String instanceName)
{
Object o, oNormalized = null;
MemoryStream ms;
BinaryWriter bw;
BinaryFormatter bSer;
o = _GetAccountingInfo(evidence, evidenceType, fAssmDomApp,
out oNormalized);
// Get the type name
typeName = GetPredefinedTypeName(o);
if (typeName == null)
{
// This is not a predefined type. Serialize the type
// and get a hash for the serialized stream
GetUnrestricted().Assert();
ms = new MemoryStream();
bSer = new BinaryFormatter();
bSer.Serialize(ms, o.GetType());
ms.Position = 0;
typeName = GetHash(ms);
#if _DEBUG
DebugLog(o.GetType(), ms);
#endif
CodeAccessPermission.RevertAssert();
}
instanceName = null;
// Get the normalized instance name if present.
if (oNormalized != null)
{
if (oNormalized is Stream)
{
instanceName = GetHash((Stream)oNormalized);
}
else if (oNormalized is String)
{
if (IsValidName((String)oNormalized))
{
instanceName = (String)oNormalized;
}
else
{
// The normalized name has illegal chars
// serialize and get the hash.
ms = new MemoryStream();
bw = new BinaryWriter(ms);
bw.Write((String)oNormalized);
ms.Position = 0;
instanceName = GetHash(ms);
#if _DEBUG
DebugLog(oNormalized, ms);
#endif
}
}
}
else
{
oNormalized = o;
}
if (instanceName == null)
{
// Serialize the instance and get the hash for the
// serialized stream
GetUnrestricted().Assert();
ms = new MemoryStream();
bSer = new BinaryFormatter();
bSer.Serialize(ms, oNormalized);
ms.Position = 0;
instanceName = GetHash(ms);
#if _DEBUG
DebugLog(oNormalized, ms);
#endif
CodeAccessPermission.RevertAssert();
}
return o;
}
private static Object _GetAccountingInfo(
Evidence evidence, Type evidenceType, IsolatedStorageScope fAssmDomApp,
out Object oNormalized)
{
Object o = null;
Contract.Assert(evidence != null, "evidence != null");
if (evidenceType == null)
{
// Caller does not have any preference
// Order of preference is Publisher, Strong Name, Url, Site
#if !FEATURE_PAL
o = evidence.GetHostEvidence<Publisher>();
if (o == null)
o = evidence.GetHostEvidence<StrongName>();
#endif // !FEATURE_PAL
if (o == null)
o = evidence.GetHostEvidence<Url>();
if (o == null)
o = evidence.GetHostEvidence<Site>();
if (o == null)
o = evidence.GetHostEvidence<Zone>();
if (o == null)
{
// The evidence object can have tons of other objects
// creatd by the policy system. Ignore those.
if (fAssmDomApp == IsolatedStorageScope.Domain)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_DomainNoEvidence"));
else if (fAssmDomApp == IsolatedStorageScope.Application)
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_ApplicationNoEvidence"));
else
throw new IsolatedStorageException(
Environment.GetResourceString(
"IsolatedStorage_AssemblyNoEvidence"));