File size: 26,817 Bytes
8c763fb | 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Management.Automation;
using System.Net;
using System.Net.Mail;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.AccessControl;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using Microsoft.Management.Infrastructure;
using Dbg = System.Management.Automation.Diagnostics;
// TODO/FIXME: Move this class to src/cimSupport/other directory (to map to the namespace it lives in and functionality it implements [cmdletization independent])
namespace Microsoft.PowerShell.Cim
{
internal sealed class CimSensitiveValueConverter : IDisposable
{
private sealed class SensitiveString : IDisposable
{
private GCHandle _gcHandle;
private string _string;
public string Value { get { return _string; } }
internal SensitiveString(int numberOfCharacters)
{
_string = new string('\0', numberOfCharacters);
Debug.Assert(
string.IsInterned(_string) == null,
"We will overwrite string contents - we can't / shouldn't do this for interned strings.");
/* The string is pinned (while still being filled with insignificant data)
* to prevent copying of sensitive data by garbage collection.
* This allows the sensitive data to be cleaned-up from SensitiveString.Dispose method.
*/
_gcHandle = GCHandle.Alloc(_string, GCHandleType.Pinned);
}
private unsafe void Copy(char* source, int offset, int charsToCopy)
{
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(offset, _string.Length);
ArgumentOutOfRangeException.ThrowIfGreaterThan(offset + charsToCopy, _string.Length, nameof(charsToCopy));
fixed (char* target = _string)
{
for (int i = 0; i < charsToCopy; i++)
{
target[offset + i] = source[i];
}
}
}
internal void Copy(string source, int offset)
{
unsafe
{
fixed (char* sourcePointer = source)
{
Copy(sourcePointer, offset, source.Length);
}
}
}
internal void Copy(SecureString source, int offset)
{
IntPtr plainTextString = Marshal.SecureStringToCoTaskMemUnicode(source);
try
{
unsafe
{
Copy((char*)plainTextString, offset, source.Length);
}
}
finally
{
Marshal.ZeroFreeCoTaskMemUnicode(plainTextString);
}
}
~SensitiveString()
{
Dispose(false);
}
/// <summary>
/// Releases resources associated with this object.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases resources associated with this object.
/// </summary>
private void Dispose(bool disposing)
{
if (_string == null)
{
return;
}
/* this clobbers sensitive data */
Copy(new string('\0', _string.Length), 0);
/* this allows garbage collector to move and/or free the string */
_gcHandle.Free();
_string = null;
}
}
private readonly List<IDisposable> _trackedDisposables = new();
/// <summary>
/// Releases resources associated with this object.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases resources associated with this object.
/// </summary>
private void Dispose(bool disposing)
{
lock (_trackedDisposables)
{
foreach (var d in _trackedDisposables)
{
d.Dispose();
}
_trackedDisposables.Clear();
}
}
private const string PSCredentialDelimiter = ":";
internal object ConvertFromDotNetToCim(object dotNetObject)
{
if (dotNetObject == null)
{
return null;
}
PSObject psObject = PSObject.AsPSObject(dotNetObject);
Type dotNetType = psObject.BaseObject.GetType();
if (typeof(PSCredential).IsAssignableFrom(dotNetType))
{
var credential = (PSCredential)(psObject.BaseObject);
string escapedUsername = credential.UserName;
escapedUsername = escapedUsername.Replace("\\", "\\\\"); // Esc backslashes
escapedUsername = escapedUsername.Replace(PSCredentialDelimiter, "\\" + PSCredentialDelimiter);
var sensitiveString = new SensitiveString(escapedUsername.Length + PSCredentialDelimiter.Length + credential.Password.Length);
lock (_trackedDisposables) { _trackedDisposables.Add(sensitiveString); }
sensitiveString.Copy(escapedUsername, 0);
sensitiveString.Copy(PSCredentialDelimiter, escapedUsername.Length);
sensitiveString.Copy(credential.Password, escapedUsername.Length + PSCredentialDelimiter.Length);
return sensitiveString.Value;
}
if (typeof(SecureString).IsAssignableFrom(dotNetType))
{
SecureString secureString = (SecureString)psObject.BaseObject;
var sensitiveString = new SensitiveString(secureString.Length);
lock (_trackedDisposables) { _trackedDisposables.Add(sensitiveString); }
sensitiveString.Copy(secureString, 0);
return sensitiveString.Value;
}
if (dotNetType.IsArray)
{
Type dotNetElementType = CimValueConverter.GetElementType(dotNetType);
if (dotNetElementType != null)
{
var dotNetArray = (Array)psObject.BaseObject;
Type cimElementType = GetCimType(dotNetElementType);
Array cimArray = Array.CreateInstance(cimElementType, dotNetArray.Length);
for (int i = 0; i < cimArray.Length; i++)
{
object cimElement = ConvertFromDotNetToCim(dotNetArray.GetValue(i));
cimArray.SetValue(cimElement, i);
}
return cimArray;
}
}
return CimValueConverter.ConvertFromDotNetToCim(dotNetObject);
}
internal static Type GetCimType(Type dotNetType)
{
if (dotNetType == typeof(SecureString))
{
return typeof(string);
}
if (dotNetType == typeof(PSCredential))
{
return typeof(string);
}
return CimValueConverter.GetCimType(dotNetType);
}
}
internal static class CimValueConverter
{
/// <exception cref="PSInvalidCastException">The only kind of exception this method can throw.</exception>
internal static object ConvertFromDotNetToCim(object dotNetObject)
{
if (dotNetObject == null)
{
return null;
}
PSObject psObject = PSObject.AsPSObject(dotNetObject);
Type dotNetType = psObject.BaseObject.GetType();
Dbg.Assert(
!(dotNetType.GetTypeInfo().IsGenericType && dotNetType.GetGenericTypeDefinition() == typeof(Nullable<>)),
"GetType on a boxed object should never return Nullable<T>");
if (LanguagePrimitives.IsCimIntrinsicScalarType(dotNetType))
{
return psObject.BaseObject;
}
if (typeof(CimInstance).IsAssignableFrom(dotNetType))
{
return psObject.BaseObject;
}
if (typeof(PSReference).IsAssignableFrom(dotNetType))
{
PSReference psReference = (PSReference)psObject.BaseObject;
if (psReference.Value == null)
{
return null;
}
else
{
PSObject innerPso = PSObject.AsPSObject(psReference.Value);
return ConvertFromDotNetToCim(innerPso.BaseObject);
}
}
if (dotNetType.IsArray)
{
Type dotNetElementType = GetElementType(dotNetType);
if (dotNetElementType != null)
{
var dotNetArray = (Array)psObject.BaseObject;
Type cimElementType = CimValueConverter.GetCimType(dotNetElementType);
Array cimArray = Array.CreateInstance(cimElementType, dotNetArray.Length);
for (int i = 0; i < cimArray.Length; i++)
{
object cimElement = ConvertFromDotNetToCim(dotNetArray.GetValue(i));
cimArray.SetValue(cimElement, i);
}
return cimArray;
}
}
Type convertibleCimType = GetConvertibleCimType(dotNetType);
if (convertibleCimType != null)
{
object cimIntrinsicValue = LanguagePrimitives.ConvertTo(dotNetObject, convertibleCimType, CultureInfo.InvariantCulture);
return cimIntrinsicValue;
}
if (typeof(ObjectSecurity).IsAssignableFrom(dotNetType))
{
string cimIntrinsicValue = Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase.GetSddl(psObject);
return cimIntrinsicValue;
}
if (typeof(X509Certificate2).IsAssignableFrom(dotNetType))
{
var cert = (X509Certificate2)(psObject.BaseObject);
byte[] cimIntrinsicValue = cert.RawData;
return cimIntrinsicValue;
}
if (typeof(X500DistinguishedName).IsAssignableFrom(dotNetType))
{
var x500name = (X500DistinguishedName)(psObject.BaseObject);
byte[] cimIntrinsicValue = x500name.RawData;
return cimIntrinsicValue;
}
if (typeof(PhysicalAddress).IsAssignableFrom(dotNetType))
{
object cimIntrinsicValue = LanguagePrimitives.ConvertTo(dotNetObject, typeof(string), CultureInfo.InvariantCulture);
return cimIntrinsicValue;
}
if (typeof(IPEndPoint).IsAssignableFrom(dotNetType))
{
object cimIntrinsicValue = LanguagePrimitives.ConvertTo(dotNetObject, typeof(string), CultureInfo.InvariantCulture);
return cimIntrinsicValue;
}
if (typeof(WildcardPattern).IsAssignableFrom(dotNetType))
{
var wildcardPattern = (WildcardPattern)(psObject.BaseObject);
return wildcardPattern.ToWql();
}
if (typeof(XmlDocument).IsAssignableFrom(dotNetType))
{
var xmlDocument = (XmlDocument)(psObject.BaseObject);
string cimIntrinsicValue = xmlDocument.OuterXml;
return cimIntrinsicValue;
}
// unrecognized type = throw invalid cast exception
throw CimValueConverter.GetInvalidCastException(
null, /* inner exception */
"InvalidDotNetToCimCast",
dotNetObject,
CmdletizationResources.CimConversion_CimIntrinsicValue);
}
/// <exception cref="PSInvalidCastException">The only kind of exception this method can throw.</exception>
internal static object ConvertFromCimToDotNet(object cimObject, Type expectedDotNetType)
{
ArgumentNullException.ThrowIfNull(expectedDotNetType);
if (cimObject == null)
{
return null;
}
if (expectedDotNetType.GetTypeInfo().IsGenericType && expectedDotNetType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
expectedDotNetType = expectedDotNetType.GetGenericArguments()[0];
}
if (LanguagePrimitives.IsCimIntrinsicScalarType(expectedDotNetType))
{
return LanguagePrimitives.ConvertTo(cimObject, expectedDotNetType, CultureInfo.InvariantCulture);
}
if (expectedDotNetType == typeof(CimInstance))
{
return LanguagePrimitives.ConvertTo(cimObject, expectedDotNetType, CultureInfo.InvariantCulture);
}
if (expectedDotNetType.IsArray)
{
Type dotNetElementType = GetElementType(expectedDotNetType);
if (dotNetElementType != null)
{
var cimArray = (Array)LanguagePrimitives.ConvertTo(cimObject, typeof(Array), CultureInfo.InvariantCulture);
Array dotNetArray = Array.CreateInstance(dotNetElementType, cimArray.Length);
for (int i = 0; i < dotNetArray.Length; i++)
{
object dotNetElement = ConvertFromCimToDotNet(cimArray.GetValue(i), dotNetElementType);
dotNetArray.SetValue(dotNetElement, i);
}
return dotNetArray;
}
}
Type convertibleCimType = GetConvertibleCimType(expectedDotNetType);
if (convertibleCimType != null)
{
object cimIntrinsicValue = LanguagePrimitives.ConvertTo(cimObject, convertibleCimType, CultureInfo.InvariantCulture);
object dotNetObject = LanguagePrimitives.ConvertTo(cimIntrinsicValue, expectedDotNetType, CultureInfo.InvariantCulture);
return dotNetObject;
}
Func<Func<object>, object> exceptionSafeReturn = (Func<object> innerAction) =>
{
try
{
return innerAction();
}
catch (Exception e)
{
throw CimValueConverter.GetInvalidCastException(
e,
"InvalidCimToDotNetCast",
cimObject,
expectedDotNetType.FullName);
}
};
if (typeof(ObjectSecurity).IsAssignableFrom(expectedDotNetType))
{
var sddl = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
return exceptionSafeReturn(delegate
{
var objectSecurity = (ObjectSecurity)Activator.CreateInstance(expectedDotNetType);
objectSecurity.SetSecurityDescriptorSddlForm(sddl);
return objectSecurity;
});
}
if (typeof(X509Certificate2) == expectedDotNetType)
{
var cimIntrinsicValue = (byte[])LanguagePrimitives.ConvertTo(cimObject, typeof(byte[]), CultureInfo.InvariantCulture);
return exceptionSafeReturn(delegate
{
#pragma warning disable SYSLIB0057
return new X509Certificate2(cimIntrinsicValue);
#pragma warning restore SYSLIB0057
});
}
if (typeof(X500DistinguishedName) == expectedDotNetType)
{
var cimIntrinsicValue = (byte[])LanguagePrimitives.ConvertTo(cimObject, typeof(byte[]), CultureInfo.InvariantCulture);
return exceptionSafeReturn(delegate
{
return new X500DistinguishedName(cimIntrinsicValue);
});
}
if (typeof(PhysicalAddress) == expectedDotNetType)
{
var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
return exceptionSafeReturn(delegate
{
return PhysicalAddress.Parse(cimIntrinsicValue);
});
}
if (typeof(IPEndPoint) == expectedDotNetType)
{
var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
return exceptionSafeReturn(delegate
{
int indexOfLastColon = cimIntrinsicValue.LastIndexOf(':');
int port = int.Parse(cimIntrinsicValue.AsSpan(indexOfLastColon + 1), NumberStyles.Integer, CultureInfo.InvariantCulture);
IPAddress address = IPAddress.Parse(cimIntrinsicValue.AsSpan(0, indexOfLastColon));
return new IPEndPoint(address, port);
});
}
// WildcardPattern is only supported as an "in" parameter - we do not support the reverse translation (i.e. from "a%" to "a*")
if (typeof(XmlDocument) == expectedDotNetType)
{
var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
return exceptionSafeReturn(delegate
{
XmlDocument doc = InternalDeserializer.LoadUnsafeXmlDocument(
cimIntrinsicValue,
true, /* preserve non elements: whitespace, processing instructions, comments, etc. */
null); /* default maxCharactersInDocument */
return doc;
});
}
// unrecognized type = throw invalid cast exception
throw CimValueConverter.GetInvalidCastException(
null, /* inner exception */
"InvalidCimToDotNetCast",
cimObject,
expectedDotNetType.FullName);
}
internal static CimType GetCimTypeEnum(Type dotNetType)
{
Dbg.Assert(dotNetType != null, "Caller should make sure that dotNetType != null");
if (typeof(PSReference).IsAssignableFrom(dotNetType))
{
return CimType.Reference;
}
if (typeof(PSReference[]).IsAssignableFrom(dotNetType))
{
return CimType.ReferenceArray;
}
else
{
return CimConverter.GetCimType(dotNetType);
}
}
internal static Type GetCimType(Type dotNetType)
{
if (dotNetType.IsArray)
{
return GetCimType(GetElementType(dotNetType)).MakeArrayType();
}
if (dotNetType.GetTypeInfo().IsGenericType && dotNetType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return GetCimType(dotNetType.GetGenericArguments()[0]);
}
if (LanguagePrimitives.IsCimIntrinsicScalarType(dotNetType))
{
return dotNetType;
}
if (dotNetType == typeof(CimInstance))
{
return dotNetType;
}
if (dotNetType == typeof(PSReference))
{
return dotNetType;
}
Type result = CimValueConverter.GetConvertibleCimType(dotNetType);
if (result != null)
{
return result;
}
if (typeof(ObjectSecurity).IsAssignableFrom(dotNetType))
{
return typeof(string);
}
if (typeof(X509Certificate2) == dotNetType)
{
return typeof(byte[]);
}
if (typeof(X500DistinguishedName) == dotNetType)
{
return typeof(byte[]);
}
if (typeof(PhysicalAddress) == dotNetType)
{
return typeof(string);
}
if (typeof(IPEndPoint) == dotNetType)
{
return typeof(string);
}
if (typeof(WildcardPattern) == dotNetType)
{
return typeof(string);
}
if (typeof(XmlDocument) == dotNetType)
{
return typeof(string);
}
if (typeof(PSCredential) == dotNetType)
{
return typeof(string);
}
Dbg.Assert(false, ".NET Type that is not supported in a .NET <-> CIM conversion");
return null;
}
/// <summary>
/// Returns a type of CIM representation if conversion from/to CIM can be done purely with LanguagePrimitives.ConvertTo.
/// </summary>
/// <param name="dotNetType"></param>
/// <returns></returns>
private static Type GetConvertibleCimType(Type dotNetType)
{
Dbg.Assert(
!(dotNetType.GetTypeInfo().IsGenericType && dotNetType.GetGenericTypeDefinition() == typeof(Nullable<>)),
"Caller should strip out Nullable<T> before calling CimValueConverter.GetConvertibleCimType");
if (dotNetType.GetTypeInfo().IsEnum)
{
return Enum.GetUnderlyingType(dotNetType);
}
if (dotNetType == typeof(SwitchParameter))
{
return typeof(bool);
}
if (dotNetType == typeof(Guid) ||
dotNetType == typeof(Uri) || // TODO/FIXME - CliXml does some magic around relative URIs - do we want to duplicate this?
dotNetType == typeof(Version) ||
dotNetType == typeof(IPAddress) ||
dotNetType == typeof(MailAddress))
{
return typeof(string);
}
return null;
}
internal static Type GetElementType(Type arrayType)
{
Dbg.Assert(arrayType != null, "Caller should verify arrayType != null");
Dbg.Assert(arrayType.IsArray, "Caller should verify arrayType.IsArray");
// MOF syntax from Appendix A of DSP0004 doesn't allow expressing
// of 1) nested arrays and 2) multi-dimensional arrays
// (see production for "array" and how this production is used in
// other productions like "propertyDeclaration" or "parameter")
if (arrayType.GetArrayRank() != 1)
{
return null;
}
Type elementType = arrayType.GetElementType();
if (elementType.IsArray)
{
return null;
}
return elementType;
}
internal static PSInvalidCastException GetInvalidCastException(
Exception innerException,
string errorId,
object sourceValue,
string descriptionOfTargetType)
{
Dbg.Assert(!string.IsNullOrEmpty(errorId), "Caller should verify !string.IsNullOrEmpty(errorId)");
Dbg.Assert(sourceValue != null, "Caller should verify sourceValue != null");
Dbg.Assert(!string.IsNullOrEmpty(descriptionOfTargetType), "Caller should verify !string.IsNullOrEmpty(descriptionOfTargetType)");
throw new PSInvalidCastException(
errorId,
innerException,
ExtendedTypeSystem.InvalidCastException,
sourceValue,
PSObject.AsPSObject(sourceValue).BaseObject.GetType().FullName,
descriptionOfTargetType);
}
[Conditional("DEBUG")]
internal static void AssertIntrinsicCimValue(object value)
{
Dbg.Assert(value != null, "Caller should verify value != null");
Type type = value.GetType();
AssertIntrinsicCimType(type);
Dbg.Assert(!typeof(PSReference).IsAssignableFrom(type) && typeof(PSReference[]) != type,
"PSReference cannot be used as a CIM *value* (PSReference is only ok as a type)");
}
[Conditional("DEBUG")]
internal static void AssertIntrinsicCimType(Type type)
{
Dbg.Assert(type != null, "Caller should verify type != null");
Dbg.Assert(
LanguagePrimitives.IsCimIntrinsicScalarType(type) ||
(type.IsArray && LanguagePrimitives.IsCimIntrinsicScalarType(type.GetElementType())) ||
typeof(CimInstance).IsAssignableFrom(type) ||
typeof(PSReference).IsAssignableFrom(type) ||
type == typeof(CimInstance[]) ||
type == typeof(PSReference[]),
"Caller should verify that type is an intrinsic CIM type");
}
}
}
|