Skip to content

Commit e80042a

Browse files
committed
more registry states exception handling
1 parent e36360c commit e80042a

1 file changed

Lines changed: 104 additions & 83 deletions

File tree

FlashpointSecurePlayer/RegistryStates.cs

Lines changed: 104 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -312,21 +312,17 @@ private RegistryKey OpenBaseKeyInRegistryView(string keyName, RegistryView regis
312312
if (registryHive == null) {
313313
return null;
314314
}
315-
316-
try {
317-
return RegistryKey.OpenBaseKey(registryHive.GetValueOrDefault(), registryView);
318-
} catch (ArgumentException) {
319-
return null;
320-
}
315+
return RegistryKey.OpenBaseKey(registryHive.GetValueOrDefault(), registryView);
321316
}
322317

323318
private RegistryKey OpenKeyInRegistryView(string keyName, bool writable, RegistryView registryView) {
324319
RegistryKey registryKey = OpenBaseKeyInRegistryView(keyName, registryView);
325320

326321
if (registryKey == null) {
322+
// base key does not exist
327323
return null;
328324
}
329-
325+
330326
int subKeyNameIndex = keyName.IndexOf("\\") + 1;
331327

332328
if (subKeyNameIndex > 0) {
@@ -335,54 +331,14 @@ private RegistryKey OpenKeyInRegistryView(string keyName, bool writable, Registr
335331
return registryKey;
336332
}
337333

338-
private string TestKeyDeletedInRegistryView(string keyName, RegistryView registryView) {
339-
List<string> keyNames = keyName.Split('\\').ToList();
340-
341-
if (!keyNames.Any()) {
342-
return keyName;
343-
}
344-
345-
RegistryKey registryKey = OpenBaseKeyInRegistryView(keyName, registryView);
346-
347-
// base key is deleted
348-
if (registryKey == null) {
349-
return keyNames.First();
350-
}
351-
352-
RegistryKey registrySubKey = null;
353-
354-
try {
355-
for (int i = 0; i < keyNames.Count - 1; i++) {
356-
try {
357-
registrySubKey = registryKey.OpenSubKey(keyNames[i + 1]);
358-
} catch {
359-
registrySubKey = null;
360-
}
361-
362-
registryKey.Dispose();
363-
registryKey = registrySubKey;
364-
365-
if (registryKey == null) {
366-
return String.Join("\\", keyNames.Take(i + 2).ToArray());
367-
}
368-
}
369-
return String.Empty;
370-
} finally {
371-
if (registryKey != null) {
372-
registryKey.Dispose();
373-
registryKey = null;
374-
}
375-
}
376-
}
377-
378334
private RegistryKey CreateKeyInRegistryView(string keyName, RegistryKeyPermissionCheck registryKeyPermissionCheck, RegistryView registryView) {
379335
RegistryKey registryKey = OpenBaseKeyInRegistryView(keyName, registryView);
380336

381337
if (registryKey == null) {
382338
// base key does not exist
383339
return null;
384340
}
385-
341+
386342
int subKeyNameIndex = keyName.IndexOf("\\") + 1;
387343

388344
if (subKeyNameIndex > 0) {
@@ -391,6 +347,18 @@ private RegistryKey CreateKeyInRegistryView(string keyName, RegistryKeyPermissio
391347
return registryKey;
392348
}
393349

350+
private void SetKeyInRegistryView(string keyName, RegistryView registryView) {
351+
RegistryKey registryKey = CreateKeyInRegistryView(keyName, RegistryKeyPermissionCheck.Default, registryView);
352+
353+
if (registryKey == null) {
354+
// key is invalid
355+
throw new ArgumentException("The key \"" + keyName + "\" is invalid.");
356+
}
357+
358+
registryKey.Dispose();
359+
registryKey = null;
360+
}
361+
394362
private void DeleteKeyInRegistryView(string keyName, RegistryView registryView) {
395363
RegistryKey registryKey = OpenBaseKeyInRegistryView(keyName, registryView);
396364

@@ -408,18 +376,6 @@ private void DeleteKeyInRegistryView(string keyName, RegistryView registryView)
408376
}
409377
}
410378

411-
private void SetKeyInRegistryView(string keyName, RegistryView registryView) {
412-
RegistryKey registryKey = CreateKeyInRegistryView(keyName, RegistryKeyPermissionCheck.Default, registryView);
413-
414-
if (registryKey == null) {
415-
// key is invalid
416-
throw new ArgumentException("The key \"" + keyName + "\" is invalid.");
417-
}
418-
419-
registryKey.Dispose();
420-
registryKey = null;
421-
}
422-
423379
private object GetValueInRegistryView(string keyName, string valueName, RegistryView registryView) {
424380
RegistryKey registryKey = OpenKeyInRegistryView(keyName, false, registryView);
425381

@@ -493,7 +449,7 @@ private void DeleteValueInRegistryView(string keyName, string valueName, Registr
493449
RegistryKey registryKey = OpenKeyInRegistryView(keyName, true, registryView);
494450

495451
if (registryKey == null) {
496-
// base key does not exist (good!)
452+
// key does not exist (good!)
497453
return;
498454
}
499455

@@ -506,6 +462,7 @@ private void DeleteValueInRegistryView(string keyName, string valueName, Registr
506462
RegistryKey registryKey = OpenKeyInRegistryView(keyName, false, registryView);
507463

508464
if (registryKey == null) {
465+
// key does not exist
509466
return null;
510467
}
511468

@@ -514,6 +471,67 @@ private void DeleteValueInRegistryView(string keyName, string valueName, Registr
514471
}
515472
}
516473

474+
private string TestKeyDeletedInRegistryView(string keyName, RegistryView registryView) {
475+
List<string> keyNames = keyName.Split('\\').ToList();
476+
477+
if (!keyNames.Any()) {
478+
return keyName;
479+
}
480+
481+
RegistryKey registryKey = null;
482+
483+
try {
484+
registryKey = OpenBaseKeyInRegistryView(keyName, registryView);
485+
} catch (UnauthorizedAccessException) {
486+
// key exists but we can't get it
487+
throw new TaskRequiresElevationException("The key \"" + keyName + "\" cannot be accessed by the user.");
488+
} catch (SecurityException) {
489+
// key exists but we can't get it
490+
throw new TaskRequiresElevationException("The key \"" + keyName + "\" cannot be accessed by the user.");
491+
} catch {
492+
// key doesn't exist
493+
}
494+
495+
// base key is deleted
496+
if (registryKey == null) {
497+
return keyNames.First();
498+
}
499+
500+
RegistryKey registrySubKey = null;
501+
502+
try {
503+
for (int i = 0; i < keyNames.Count - 1; i++) {
504+
keyName = keyNames[i + 1];
505+
506+
try {
507+
registrySubKey = registryKey.OpenSubKey(keyName);
508+
} catch (UnauthorizedAccessException) {
509+
// key exists but we can't get it
510+
throw new TaskRequiresElevationException("The key \"" + keyName + "\" cannot be accessed by the user.");
511+
} catch (SecurityException) {
512+
// key exists but we can't get it
513+
throw new TaskRequiresElevationException("The key \"" + keyName + "\" cannot be accessed by the user.");
514+
} catch {
515+
// key doesn't exist
516+
registrySubKey = null;
517+
}
518+
519+
registryKey.Dispose();
520+
registryKey = registrySubKey;
521+
522+
if (registryKey == null) {
523+
return String.Join("\\", keyNames.Take(i + 2).ToArray());
524+
}
525+
}
526+
return String.Empty;
527+
} finally {
528+
if (registryKey != null) {
529+
registryKey.Dispose();
530+
registryKey = null;
531+
}
532+
}
533+
}
534+
517535
// older than Windows XP 64-bit
518536
private readonly bool notWOW64Version = !Environment.Is64BitOperatingSystem
519537
|| Environment.OSVersion.Version < new Version(5, 1);
@@ -1080,18 +1098,18 @@ public async Task StopImportAsync() {
10801098
case TYPE.KEY:
10811099
try {
10821100
SetKeyInRegistryView(keyName, registryView);
1101+
} catch (UnauthorizedAccessException) {
1102+
// key exists and we can't modify it
1103+
} catch (SecurityException) {
1104+
// key exists and we can't modify it
10831105
} catch (InvalidOperationException) {
10841106
// key marked for deletion
10851107
Deactivate();
10861108
throw new InvalidRegistryStateException("The key \"" + keyName + "\" is marked for deletion.");
1087-
} catch (ArgumentException) {
1109+
} catch {
10881110
// key doesn't exist and can't be created
10891111
Deactivate();
10901112
throw new TaskRequiresElevationException("Creating the key \"" + keyName + "\" requires elevation.");
1091-
} catch (UnauthorizedAccessException) {
1092-
// key exists and we can't modify it
1093-
} catch (SecurityException) {
1094-
// key exists and we can't modify it
10951113
}
10961114
break;
10971115
case TYPE.VALUE:
@@ -1105,26 +1123,26 @@ public async Task StopImportAsync() {
11051123
registryStateElement.ValueKind.GetValueOrDefault(),
11061124
registryView
11071125
);
1126+
} catch (UnauthorizedAccessException) {
1127+
// value exists and we can't modify it
1128+
Deactivate();
1129+
throw new TaskRequiresElevationException("Modifying the value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" requires elevation.");
1130+
} catch (SecurityException) {
1131+
// value exists and we can't modify it
1132+
Deactivate();
1133+
throw new TaskRequiresElevationException("Modifying the value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" requires elevation.");
11081134
} catch (FormatException) {
1109-
// value marked for deletion
1135+
// value must be Base64
11101136
Deactivate();
11111137
throw new InvalidRegistryStateException("The value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" must be Base64.");
11121138
} catch (InvalidOperationException) {
11131139
// value marked for deletion
11141140
Deactivate();
11151141
throw new InvalidRegistryStateException("The value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" is marked for deletion.");
1116-
} catch (ArgumentException) {
1142+
} catch {
11171143
// value doesn't exist and can't be created
11181144
Deactivate();
1119-
throw new TaskRequiresElevationException("Creating the value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" requires elevation.");
1120-
} catch (UnauthorizedAccessException) {
1121-
// value exists and we can't modify it
1122-
Deactivate();
1123-
throw new TaskRequiresElevationException("Modifying the value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" requires elevation.");
1124-
} catch (SecurityException) {
1125-
// value exists and we can't modify it
1126-
Deactivate();
1127-
throw new TaskRequiresElevationException("Modifying the value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" requires elevation.");
1145+
throw new InvalidRegistryStateException("The value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" cannot be created.");
11281146
}
11291147
break;
11301148
}
@@ -1357,18 +1375,21 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod = M
13571375
activeRegistryStateElement.ValueKind.GetValueOrDefault(),
13581376
registryView
13591377
);
1360-
} catch (InvalidOperationException) {
1361-
// value doesn't exist and can't be created
1362-
throw new InvalidRegistryStateException("The value \"" + activeRegistryStateElement.ValueName + "\" in key \"" + keyName + "\" cannot be created.");
1363-
} catch (ArgumentException) {
1364-
// value doesn't exist and can't be created
1365-
throw new InvalidRegistryStateException("The value \"" + activeRegistryStateElement.ValueName + "\" in key \"" + keyName + "\" cannot be created.");
13661378
} catch (UnauthorizedAccessException) {
13671379
// value exists and we can't modify it
13681380
throw new TaskRequiresElevationException("Setting the value \"" + activeRegistryStateElement.ValueName + "\" in key \"" + keyName + "\" requires elevation.");
13691381
} catch (SecurityException) {
13701382
// value exists and we can't modify it
13711383
throw new TaskRequiresElevationException("Setting the value \"" + activeRegistryStateElement.ValueName + "\" in key \"" + keyName + "\" requires elevation.");
1384+
} catch (FormatException) {
1385+
// value must be Base64
1386+
throw new InvalidRegistryStateException("The value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" must be Base64.");
1387+
} catch (InvalidOperationException) {
1388+
// value marked for deletion
1389+
throw new InvalidRegistryStateException("The value \"" + registryStateElement.ValueName + "\" in key \"" + keyName + "\" is marked for deletion.");
1390+
} catch {
1391+
// value doesn't exist and can't be created
1392+
throw new InvalidRegistryStateException("The value \"" + activeRegistryStateElement.ValueName + "\" in key \"" + keyName + "\" cannot be created.");
13721393
}
13731394
} else {
13741395
try {

0 commit comments

Comments
 (0)